1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471 |
-
-
- FILE_LICENCE ( GPL2_OR_LATER );
-
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <errno.h>
- #include <assert.h>
- #include <byteswap.h>
- #include <ipxe/if_ether.h>
- #include <ipxe/iobuf.h>
- #include <ipxe/netdevice.h>
- #include <ipxe/device.h>
- #include <ipxe/xfer.h>
- #include <ipxe/open.h>
- #include <ipxe/job.h>
- #include <ipxe/retry.h>
- #include <ipxe/tcpip.h>
- #include <ipxe/ip.h>
- #include <ipxe/uuid.h>
- #include <ipxe/timer.h>
- #include <ipxe/settings.h>
- #include <ipxe/dhcp.h>
- #include <ipxe/dhcpopts.h>
- #include <ipxe/dhcppkt.h>
- #include <ipxe/dhcp_arch.h>
- #include <ipxe/features.h>
-
-
-
- struct dhcp_session;
- static int dhcp_tx ( struct dhcp_session *dhcp );
-
-
- static const uint8_t dhcp_op[] = {
- [DHCPDISCOVER] = BOOTP_REQUEST,
- [DHCPOFFER] = BOOTP_REPLY,
- [DHCPREQUEST] = BOOTP_REQUEST,
- [DHCPDECLINE] = BOOTP_REQUEST,
- [DHCPACK] = BOOTP_REPLY,
- [DHCPNAK] = BOOTP_REPLY,
- [DHCPRELEASE] = BOOTP_REQUEST,
- [DHCPINFORM] = BOOTP_REQUEST,
- };
-
-
- static uint8_t dhcp_request_options_data[] = {
- DHCP_MESSAGE_TYPE, DHCP_BYTE ( 0 ),
- DHCP_MAX_MESSAGE_SIZE,
- DHCP_WORD ( ETH_MAX_MTU - 20 - 8 ),
- DHCP_CLIENT_ARCHITECTURE, DHCP_ARCH_CLIENT_ARCHITECTURE,
- DHCP_CLIENT_NDI, DHCP_ARCH_CLIENT_NDI,
- DHCP_VENDOR_CLASS_ID, DHCP_ARCH_VENDOR_CLASS_ID,
- DHCP_USER_CLASS_ID, DHCP_STRING ( 'i', 'P', 'X', 'E' ),
- DHCP_PARAMETER_REQUEST_LIST,
- DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_DNS_SERVERS,
- DHCP_LOG_SERVERS, DHCP_HOST_NAME, DHCP_DOMAIN_NAME,
- DHCP_ROOT_PATH, DHCP_VENDOR_ENCAP, DHCP_VENDOR_CLASS_ID,
- DHCP_TFTP_SERVER_NAME, DHCP_BOOTFILE_NAME,
- 128, 129, 130, 131, 132, 133, 134, 135,
- DHCP_EB_ENCAP, DHCP_ISCSI_INITIATOR_IQN ),
- DHCP_END
- };
-
-
- struct setting dhcp_server_setting __setting ( SETTING_MISC ) = {
- .name = "dhcp-server",
- .description = "DHCP server",
- .tag = DHCP_SERVER_IDENTIFIER,
- .type = &setting_type_ipv4,
- };
-
-
- uint32_t dhcp_last_xid;
-
-
- static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
- switch ( msgtype ) {
- case DHCPNONE: return "BOOTP";
- case DHCPDISCOVER: return "DHCPDISCOVER";
- case DHCPOFFER: return "DHCPOFFER";
- case DHCPREQUEST: return "DHCPREQUEST";
- case DHCPDECLINE: return "DHCPDECLINE";
- case DHCPACK: return "DHCPACK";
- case DHCPNAK: return "DHCPNAK";
- case DHCPRELEASE: return "DHCPRELEASE";
- case DHCPINFORM: return "DHCPINFORM";
- default: return "DHCP<invalid>";
- }
- }
-
-
-
- struct dhcp_session;
-
-
- struct dhcp_session_state {
-
- const char *name;
-
-
- int ( * tx ) ( struct dhcp_session *dhcp,
- struct dhcp_packet *dhcppkt,
- struct sockaddr_in *peer );
-
-
- void ( * rx ) ( struct dhcp_session *dhcp,
- struct dhcp_packet *dhcppkt,
- struct sockaddr_in *peer,
- uint8_t msgtype, struct in_addr server_id );
-
-
- void ( * expired ) ( struct dhcp_session *dhcp );
-
- uint8_t tx_msgtype;
-
- uint8_t apply_min_timeout;
- };
-
- static struct dhcp_session_state dhcp_state_discover;
- static struct dhcp_session_state dhcp_state_request;
- static struct dhcp_session_state dhcp_state_proxy;
- static struct dhcp_session_state dhcp_state_pxebs;
-
-
- struct dhcp_session {
-
- struct refcnt refcnt;
-
- struct interface job;
-
- struct interface xfer;
-
-
- struct net_device *netdev;
-
- struct sockaddr_in local;
-
- struct dhcp_session_state *state;
-
- uint32_t xid;
-
-
- struct in_addr offer;
-
- struct in_addr server;
-
- int priority;
-
-
- int no_pxedhcp;
-
- struct in_addr proxy_server;
-
- struct dhcp_packet *proxy_offer;
-
- int proxy_priority;
-
-
- uint16_t pxe_type;
-
- struct in_addr *pxe_attempt;
-
- struct in_addr *pxe_accept;
-
-
- struct retry_timer timer;
-
- unsigned int count;
-
- unsigned long start;
- };
-
-
- static void dhcp_free ( struct refcnt *refcnt ) {
- struct dhcp_session *dhcp =
- container_of ( refcnt, struct dhcp_session, refcnt );
-
- netdev_put ( dhcp->netdev );
- dhcppkt_put ( dhcp->proxy_offer );
- free ( dhcp );
- }
-
-
- static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
-
-
- stop_timer ( &dhcp->timer );
-
-
- intf_shutdown ( &dhcp->xfer, rc );
- intf_shutdown ( &dhcp->job, rc );
- }
-
-
- static void dhcp_set_state ( struct dhcp_session *dhcp,
- struct dhcp_session_state *state ) {
-
- DBGC ( dhcp, "DHCP %p entering %s state\n", dhcp, state->name );
- dhcp->state = state;
- dhcp->start = currticks();
- stop_timer ( &dhcp->timer );
- dhcp->timer.min_timeout =
- ( state->apply_min_timeout ? DHCP_MIN_TIMEOUT : 0 );
- dhcp->timer.max_timeout = DHCP_MAX_TIMEOUT;
- start_timer_nodelay ( &dhcp->timer );
- }
-
-
- static int dhcp_has_pxeopts ( struct dhcp_packet *dhcppkt ) {
-
-
- if ( dhcppkt_fetch ( dhcppkt, DHCP_BOOTFILE_NAME, NULL, 0 ) > 0 )
- return 1;
-
-
- if ( dhcppkt_fetch ( dhcppkt, DHCP_PXE_BOOT_MENU, NULL, 0 ) > 0 )
- return 1;
-
- return 0;
- }
-
-
-
-
- static int dhcp_discovery_tx ( struct dhcp_session *dhcp,
- struct dhcp_packet *dhcppkt __unused,
- struct sockaddr_in *peer ) {
-
- DBGC ( dhcp, "DHCP %p DHCPDISCOVER\n", dhcp );
-
-
- peer->sin_addr.s_addr = INADDR_BROADCAST;
- peer->sin_port = htons ( BOOTPS_PORT );
-
- return 0;
- }
-
-
- static void dhcp_discovery_rx ( struct dhcp_session *dhcp,
- struct dhcp_packet *dhcppkt,
- struct sockaddr_in *peer, uint8_t msgtype,
- struct in_addr server_id ) {
- struct in_addr ip;
- char vci[9];
- int vci_len;
- int has_pxeclient;
- int8_t priority = 0;
- uint8_t no_pxedhcp = 0;
- unsigned long elapsed;
-
- DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
- dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
- ntohs ( peer->sin_port ) );
- if ( server_id.s_addr != peer->sin_addr.s_addr )
- DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
-
-
- ip = dhcppkt->dhcphdr->yiaddr;
- if ( ip.s_addr )
- DBGC ( dhcp, " for %s", inet_ntoa ( ip ) );
-
-
- vci_len = dhcppkt_fetch ( dhcppkt, DHCP_VENDOR_CLASS_ID,
- vci, sizeof ( vci ) );
- has_pxeclient = ( ( vci_len >= ( int ) sizeof ( vci ) ) &&
- ( strncmp ( "PXEClient", vci, sizeof (vci) ) == 0 ));
- if ( has_pxeclient ) {
- DBGC ( dhcp, "%s",
- ( dhcp_has_pxeopts ( dhcppkt ) ? " pxe" : " proxy" ) );
- }
-
-
- dhcppkt_fetch ( dhcppkt, DHCP_EB_PRIORITY, &priority,
- sizeof ( priority ) );
- if ( priority )
- DBGC ( dhcp, " pri %d", priority );
-
-
- dhcppkt_fetch ( dhcppkt, DHCP_EB_NO_PXEDHCP, &no_pxedhcp,
- sizeof ( no_pxedhcp ) );
- if ( no_pxedhcp )
- DBGC ( dhcp, " nopxe" );
- DBGC ( dhcp, "\n" );
-
-
- if ( ip.s_addr && ( peer->sin_port == htons ( BOOTPS_PORT ) ) &&
- ( ( msgtype == DHCPOFFER ) || ( ! msgtype ) ) &&
- ( priority >= dhcp->priority ) ) {
- dhcp->offer = ip;
- dhcp->server = server_id;
- dhcp->priority = priority;
- dhcp->no_pxedhcp = no_pxedhcp;
- }
-
-
- if ( server_id.s_addr && has_pxeclient &&
- ( priority >= dhcp->proxy_priority ) ) {
- dhcppkt_put ( dhcp->proxy_offer );
- dhcp->proxy_server = server_id;
- dhcp->proxy_offer = dhcppkt_get ( dhcppkt );
- dhcp->proxy_priority = priority;
- }
-
-
-
-
-
- if ( ! dhcp->offer.s_addr )
- return;
-
-
- elapsed = ( currticks() - dhcp->start );
- if ( ! ( dhcp->no_pxedhcp || dhcp->proxy_offer ||
- ( elapsed > PROXYDHCP_MAX_TIMEOUT ) ) )
- return;
-
-
- dhcp_set_state ( dhcp, &dhcp_state_request );
- }
-
-
- static void dhcp_discovery_expired ( struct dhcp_session *dhcp ) {
- unsigned long elapsed = ( currticks() - dhcp->start );
-
-
- if ( dhcp->offer.s_addr && ( elapsed > PROXYDHCP_MAX_TIMEOUT ) ) {
- dhcp_set_state ( dhcp, &dhcp_state_request );
- return;
- }
-
-
- dhcp_tx ( dhcp );
- }
-
-
- static struct dhcp_session_state dhcp_state_discover = {
- .name = "discovery",
- .tx = dhcp_discovery_tx,
- .rx = dhcp_discovery_rx,
- .expired = dhcp_discovery_expired,
- .tx_msgtype = DHCPDISCOVER,
- .apply_min_timeout = 1,
- };
-
-
- static int dhcp_request_tx ( struct dhcp_session *dhcp,
- struct dhcp_packet *dhcppkt,
- struct sockaddr_in *peer ) {
- int rc;
-
- DBGC ( dhcp, "DHCP %p DHCPREQUEST to %s:%d",
- dhcp, inet_ntoa ( dhcp->server ), BOOTPS_PORT );
- DBGC ( dhcp, " for %s\n", inet_ntoa ( dhcp->offer ) );
-
-
- if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
- &dhcp->server,
- sizeof ( dhcp->server ) ) ) != 0 )
- return rc;
-
-
- if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_REQUESTED_ADDRESS,
- &dhcp->offer,
- sizeof ( dhcp->offer ) ) ) != 0 )
- return rc;
-
-
- peer->sin_addr.s_addr = INADDR_BROADCAST;
- peer->sin_port = htons ( BOOTPS_PORT );
-
- return 0;
- }
-
-
- static void dhcp_request_rx ( struct dhcp_session *dhcp,
- struct dhcp_packet *dhcppkt,
- struct sockaddr_in *peer, uint8_t msgtype,
- struct in_addr server_id ) {
- struct in_addr ip;
- struct settings *parent;
- struct settings *settings;
- int rc;
-
- DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
- dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
- ntohs ( peer->sin_port ) );
- if ( server_id.s_addr != peer->sin_addr.s_addr )
- DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
-
-
- ip = dhcppkt->dhcphdr->yiaddr;
- if ( ip.s_addr )
- DBGC ( dhcp, " for %s", inet_ntoa ( ip ) );
- DBGC ( dhcp, "\n" );
-
-
- if ( peer->sin_port != htons ( BOOTPS_PORT ) )
- return;
- if ( msgtype && ( msgtype != DHCPACK ) )
- return;
- if ( server_id.s_addr != dhcp->server.s_addr )
- return;
- if ( ip.s_addr != dhcp->offer.s_addr )
- return;
-
-
- dhcp->local.sin_addr = ip;
-
-
- parent = netdev_settings ( dhcp->netdev );
- settings = &dhcppkt->settings;
- if ( ( rc = register_settings ( settings, parent,
- DHCP_SETTINGS_NAME ) ) != 0 ) {
- DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
- dhcp, strerror ( rc ) );
- dhcp_finished ( dhcp, rc );
- return;
- }
-
-
- if ( dhcp->proxy_offer &&
- ( ! dhcp->no_pxedhcp ) ) {
- if ( dhcp_has_pxeopts ( dhcp->proxy_offer ) ) {
-
-
- settings = &dhcp->proxy_offer->settings;
- if ( ( rc = register_settings ( settings, NULL,
- PROXYDHCP_SETTINGS_NAME ) ) != 0 ) {
- DBGC ( dhcp, "DHCP %p could not register "
- "proxy settings: %s\n",
- dhcp, strerror ( rc ) );
- dhcp_finished ( dhcp, rc );
- return;
- }
- } else {
-
- dhcp_set_state ( dhcp, &dhcp_state_proxy );
- return;
- }
- }
-
-
- dhcp_finished ( dhcp, 0 );
- }
-
-
- static void dhcp_request_expired ( struct dhcp_session *dhcp ) {
-
-
- dhcp_tx ( dhcp );
- }
-
-
- static struct dhcp_session_state dhcp_state_request = {
- .name = "request",
- .tx = dhcp_request_tx,
- .rx = dhcp_request_rx,
- .expired = dhcp_request_expired,
- .tx_msgtype = DHCPREQUEST,
- .apply_min_timeout = 0,
- };
-
-
- static int dhcp_proxy_tx ( struct dhcp_session *dhcp,
- struct dhcp_packet *dhcppkt,
- struct sockaddr_in *peer ) {
- int rc;
-
- DBGC ( dhcp, "DHCP %p ProxyDHCP REQUEST to %s\n", dhcp,
- inet_ntoa ( dhcp->proxy_server ) );
-
-
- if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
- &dhcp->proxy_server,
- sizeof ( dhcp->proxy_server ) ) ) != 0 )
- return rc;
-
-
- peer->sin_addr = dhcp->proxy_server;
- peer->sin_port = htons ( PXE_PORT );
-
- return 0;
- }
-
-
- static void dhcp_proxy_rx ( struct dhcp_session *dhcp,
- struct dhcp_packet *dhcppkt,
- struct sockaddr_in *peer, uint8_t msgtype,
- struct in_addr server_id ) {
- struct settings *settings = &dhcppkt->settings;
- int rc;
-
- DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
- dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
- ntohs ( peer->sin_port ) );
- if ( server_id.s_addr != peer->sin_addr.s_addr )
- DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
- DBGC ( dhcp, "\n" );
-
-
- if ( peer->sin_port != ntohs ( PXE_PORT ) )
- return;
- if ( ( msgtype != DHCPOFFER ) && ( msgtype != DHCPACK ) )
- return;
- if ( server_id.s_addr &&
- ( server_id.s_addr != dhcp->proxy_server.s_addr ) )
- return;
-
-
- if ( ( rc = register_settings ( settings, NULL,
- PROXYDHCP_SETTINGS_NAME ) ) != 0 ) {
- DBGC ( dhcp, "DHCP %p could not register proxy settings: %s\n",
- dhcp, strerror ( rc ) );
- dhcp_finished ( dhcp, rc );
- return;
- }
-
-
- dhcp_finished ( dhcp, 0 );
- }
-
-
- static void dhcp_proxy_expired ( struct dhcp_session *dhcp ) {
- unsigned long elapsed = ( currticks() - dhcp->start );
-
-
- if ( elapsed > PROXYDHCP_MAX_TIMEOUT ) {
- dhcp_finished ( dhcp, 0 );
- return;
- }
-
-
- dhcp_tx ( dhcp );
- }
-
-
- static struct dhcp_session_state dhcp_state_proxy = {
- .name = "ProxyDHCP",
- .tx = dhcp_proxy_tx,
- .rx = dhcp_proxy_rx,
- .expired = dhcp_proxy_expired,
- .tx_msgtype = DHCPREQUEST,
- .apply_min_timeout = 0,
- };
-
-
- static int dhcp_pxebs_tx ( struct dhcp_session *dhcp,
- struct dhcp_packet *dhcppkt,
- struct sockaddr_in *peer ) {
- struct dhcp_pxe_boot_menu_item menu_item = { 0, 0 };
- int rc;
-
-
- peer->sin_addr = *(dhcp->pxe_attempt);
- peer->sin_port = ( ( peer->sin_addr.s_addr == INADDR_BROADCAST ) ?
- htons ( BOOTPS_PORT ) : htons ( PXE_PORT ) );
-
- DBGC ( dhcp, "DHCP %p PXEBS REQUEST to %s:%d for type %d\n",
- dhcp, inet_ntoa ( peer->sin_addr ), ntohs ( peer->sin_port ),
- le16_to_cpu ( dhcp->pxe_type ) );
-
-
- menu_item.type = dhcp->pxe_type;
- if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_PXE_BOOT_MENU_ITEM,
- &menu_item, sizeof ( menu_item ) ) ) != 0 )
- return rc;
-
- return 0;
- }
-
-
- static int dhcp_pxebs_accept ( struct dhcp_session *dhcp,
- struct in_addr bs ) {
- struct in_addr *accept;
-
-
- if ( ! dhcp->pxe_accept )
- return 1;
-
-
- for ( accept = dhcp->pxe_accept ; accept->s_addr ; accept++ ) {
- if ( accept->s_addr == bs.s_addr )
- return 1;
- }
-
- DBGC ( dhcp, "DHCP %p rejecting server %s\n",
- dhcp, inet_ntoa ( bs ) );
- return 0;
- }
-
-
- static void dhcp_pxebs_rx ( struct dhcp_session *dhcp,
- struct dhcp_packet *dhcppkt,
- struct sockaddr_in *peer, uint8_t msgtype,
- struct in_addr server_id ) {
- struct dhcp_pxe_boot_menu_item menu_item = { 0, 0 };
- int rc;
-
- DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
- dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
- ntohs ( peer->sin_port ) );
- if ( server_id.s_addr != peer->sin_addr.s_addr )
- DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
-
-
- dhcppkt_fetch ( dhcppkt, DHCP_PXE_BOOT_MENU_ITEM,
- &menu_item, sizeof ( menu_item ) );
- if ( menu_item.type )
- DBGC ( dhcp, " for type %d", ntohs ( menu_item.type ) );
- DBGC ( dhcp, "\n" );
-
-
- if ( ( peer->sin_port != htons ( BOOTPS_PORT ) ) &&
- ( peer->sin_port != htons ( PXE_PORT ) ) )
- return;
- if ( msgtype != DHCPACK )
- return;
- if ( menu_item.type != dhcp->pxe_type )
- return;
- if ( ! dhcp_pxebs_accept ( dhcp, ( server_id.s_addr ?
- server_id : peer->sin_addr ) ) )
- return;
-
-
- if ( ( rc = register_settings ( &dhcppkt->settings, NULL,
- PXEBS_SETTINGS_NAME ) ) != 0 ) {
- DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
- dhcp, strerror ( rc ) );
- dhcp_finished ( dhcp, rc );
- return;
- }
-
-
- dhcp_finished ( dhcp, 0 );
- }
-
-
- static void dhcp_pxebs_expired ( struct dhcp_session *dhcp ) {
- unsigned long elapsed = ( currticks() - dhcp->start );
-
-
-
- if ( elapsed > PXEBS_MAX_TIMEOUT ) {
- dhcp->pxe_attempt++;
- if ( dhcp->pxe_attempt->s_addr ) {
- dhcp_set_state ( dhcp, &dhcp_state_pxebs );
- return;
- } else {
- dhcp_finished ( dhcp, -ETIMEDOUT );
- return;
- }
- }
-
-
- dhcp_tx ( dhcp );
- }
-
-
- static struct dhcp_session_state dhcp_state_pxebs = {
- .name = "PXEBS",
- .tx = dhcp_pxebs_tx,
- .rx = dhcp_pxebs_rx,
- .expired = dhcp_pxebs_expired,
- .tx_msgtype = DHCPREQUEST,
- .apply_min_timeout = 1,
- };
-
-
-
-
- unsigned int dhcp_chaddr ( struct net_device *netdev, void *chaddr,
- uint16_t *flags ) {
- struct ll_protocol *ll_protocol = netdev->ll_protocol;
- struct dhcphdr *dhcphdr;
- int rc;
-
-
-
- if ( ll_protocol->ll_addr_len <= sizeof ( dhcphdr->chaddr ) ) {
- memcpy ( chaddr, netdev->ll_addr, ll_protocol->ll_addr_len );
- return ll_protocol->ll_addr_len;
- }
- if ( flags )
- *flags |= htons ( BOOTP_FL_BROADCAST );
- if ( ( rc = ll_protocol->eth_addr ( netdev->ll_addr, chaddr ) ) == 0 )
- return ETH_ALEN;
- if ( ll_protocol->hw_addr_len <= sizeof ( dhcphdr->chaddr ) ) {
- memcpy ( chaddr, netdev->hw_addr, ll_protocol->hw_addr_len );
- return ll_protocol->hw_addr_len;
- }
- return 0;
- }
-
-
- int dhcp_create_packet ( struct dhcp_packet *dhcppkt,
- struct net_device *netdev, uint8_t msgtype,
- uint32_t xid, const void *options, size_t options_len,
- void *data, size_t max_len ) {
- struct dhcphdr *dhcphdr = data;
- int rc;
-
-
- if ( max_len < ( sizeof ( *dhcphdr ) + options_len ) )
- return -ENOSPC;
-
-
- memset ( dhcphdr, 0, max_len );
- dhcphdr->xid = xid;
- dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
- dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
- dhcphdr->op = dhcp_op[msgtype];
- dhcphdr->hlen = netdev->ll_protocol->ll_addr_len;
- memcpy ( dhcphdr->chaddr, netdev->ll_addr,
- netdev->ll_protocol->ll_addr_len );
- memcpy ( dhcphdr->options, options, options_len );
-
-
-
- if ( netdev->ll_protocol->flags & LL_NAME_ONLY )
- dhcphdr->flags |= htons ( BOOTP_FL_BROADCAST );
-
-
-
- if ( ipv4_has_any_addr ( netdev ) )
- dhcphdr->flags |= htons ( BOOTP_FL_BROADCAST );
-
-
- memset ( dhcppkt, 0, sizeof ( *dhcppkt ) );
- dhcppkt_init ( dhcppkt, data, max_len );
-
-
- if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_MESSAGE_TYPE,
- &msgtype, sizeof ( msgtype ) ) ) != 0 )
- return rc;
-
- return 0;
- }
-
-
- int dhcp_create_request ( struct dhcp_packet *dhcppkt,
- struct net_device *netdev, unsigned int msgtype,
- uint32_t xid, struct in_addr ciaddr,
- void *data, size_t max_len ) {
- struct dhcp_netdev_desc dhcp_desc;
- struct dhcp_client_id client_id;
- struct dhcp_client_uuid client_uuid;
- uint8_t *dhcp_features;
- size_t dhcp_features_len;
- size_t ll_addr_len;
- ssize_t len;
- int rc;
-
-
- if ( ( rc = dhcp_create_packet ( dhcppkt, netdev, msgtype, xid,
- dhcp_request_options_data,
- sizeof ( dhcp_request_options_data ),
- data, max_len ) ) != 0 ) {
- DBG ( "DHCP could not create DHCP packet: %s\n",
- strerror ( rc ) );
- return rc;
- }
-
-
- dhcppkt->dhcphdr->ciaddr = ciaddr;
-
-
- dhcp_features = table_start ( DHCP_FEATURES );
- dhcp_features_len = table_num_entries ( DHCP_FEATURES );
- if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_ENCAP, dhcp_features,
- dhcp_features_len ) ) != 0 ) {
- DBG ( "DHCP could not set features list option: %s\n",
- strerror ( rc ) );
- return rc;
- }
-
-
- fetch_setting ( &netdev->settings.settings, &busid_setting, &dhcp_desc,
- sizeof ( dhcp_desc ) );
- if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_BUS_ID, &dhcp_desc,
- sizeof ( dhcp_desc ) ) ) != 0 ) {
- DBG ( "DHCP could not set bus ID option: %s\n",
- strerror ( rc ) );
- return rc;
- }
-
-
-
- client_id.ll_proto = ntohs ( netdev->ll_protocol->ll_proto );
- ll_addr_len = netdev->ll_protocol->ll_addr_len;
- assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
- memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
- if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_ID, &client_id,
- ( ll_addr_len + 1 ) ) ) != 0 ) {
- DBG ( "DHCP could not set client ID: %s\n",
- strerror ( rc ) );
- return rc;
- }
-
-
-
- client_uuid.type = DHCP_CLIENT_UUID_TYPE;
- if ( ( len = fetch_uuid_setting ( NULL, &uuid_setting,
- &client_uuid.uuid ) ) >= 0 ) {
- uuid_mangle ( &client_uuid.uuid );
- if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_UUID,
- &client_uuid,
- sizeof ( client_uuid ) ) ) != 0 ) {
- DBG ( "DHCP could not set client UUID: %s\n",
- strerror ( rc ) );
- return rc;
- }
- }
-
-
- if ( ( len = fetch_setting_len ( NULL, &user_class_setting ) ) >= 0 ) {
- char user_class[len];
- fetch_setting ( NULL, &user_class_setting, user_class,
- sizeof ( user_class ) );
- if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_USER_CLASS_ID,
- &user_class,
- sizeof ( user_class ) ) ) != 0 ) {
- DBG ( "DHCP could not set user class: %s\n",
- strerror ( rc ) );
- return rc;
- }
- }
-
- return 0;
- }
-
-
-
-
- static int dhcp_tx ( struct dhcp_session *dhcp ) {
- static struct sockaddr_in peer = {
- .sin_family = AF_INET,
- };
- struct xfer_metadata meta = {
- .netdev = dhcp->netdev,
- .src = ( struct sockaddr * ) &dhcp->local,
- .dest = ( struct sockaddr * ) &peer,
- };
- struct io_buffer *iobuf;
- uint8_t msgtype = dhcp->state->tx_msgtype;
- struct dhcp_packet dhcppkt;
- int rc;
-
-
-
- start_timer ( &dhcp->timer );
-
-
- iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
- if ( ! iobuf )
- return -ENOMEM;
-
-
- if ( ( rc = dhcp_create_request ( &dhcppkt, dhcp->netdev, msgtype,
- dhcp->xid, dhcp->local.sin_addr,
- iobuf->data,
- iob_tailroom ( iobuf ) ) ) != 0 ) {
- DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
- dhcp, strerror ( rc ) );
- goto done;
- }
-
-
-
- dhcppkt.dhcphdr->secs = htons ( ( ++(dhcp->count) << 2 ) |
- ( dhcp->offer.s_addr ? 0x02 : 0 ) |
- ( dhcp->proxy_offer ? 0x01 : 0 ) );
-
-
- if ( ( rc = dhcp->state->tx ( dhcp, &dhcppkt, &peer ) ) != 0 ) {
- DBGC ( dhcp, "DHCP %p could not fill DHCP request: %s\n",
- dhcp, strerror ( rc ) );
- goto done;
- }
-
-
- iob_put ( iobuf, dhcppkt_len ( &dhcppkt ) );
- if ( ( rc = xfer_deliver ( &dhcp->xfer, iob_disown ( iobuf ),
- &meta ) ) != 0 ) {
- DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
- dhcp, strerror ( rc ) );
- goto done;
- }
-
- done:
- free_iob ( iobuf );
- return rc;
- }
-
-
- static int dhcp_deliver ( struct dhcp_session *dhcp,
- struct io_buffer *iobuf,
- struct xfer_metadata *meta ) {
- struct sockaddr_in *peer;
- size_t data_len;
- struct dhcp_packet *dhcppkt;
- struct dhcphdr *dhcphdr;
- uint8_t msgtype = 0;
- struct in_addr server_id = { 0 };
- int rc = 0;
-
-
- if ( ! meta->src ) {
- DBGC ( dhcp, "DHCP %p received packet without source port\n",
- dhcp );
- rc = -EINVAL;
- goto err_no_src;
- }
- peer = ( struct sockaddr_in * ) meta->src;
-
-
-
- data_len = iob_len ( iobuf );
- dhcppkt = zalloc ( sizeof ( *dhcppkt ) + data_len );
- if ( ! dhcppkt ) {
- rc = -ENOMEM;
- goto err_alloc_dhcppkt;
- }
- dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
- memcpy ( dhcphdr, iobuf->data, data_len );
- dhcppkt_init ( dhcppkt, dhcphdr, data_len );
-
-
- dhcppkt_fetch ( dhcppkt, DHCP_MESSAGE_TYPE, &msgtype,
- sizeof ( msgtype ) );
-
-
- dhcppkt_fetch ( dhcppkt, DHCP_SERVER_IDENTIFIER,
- &server_id, sizeof ( server_id ) );
-
-
- if ( dhcphdr->xid != dhcp->xid ) {
- DBGC ( dhcp, "DHCP %p %s from %s:%d has bad transaction "
- "ID\n", dhcp, dhcp_msgtype_name ( msgtype ),
- inet_ntoa ( peer->sin_addr ),
- ntohs ( peer->sin_port ) );
- rc = -EINVAL;
- goto err_xid;
- };
-
-
- dhcp->state->rx ( dhcp, dhcppkt, peer, msgtype, server_id );
-
- err_xid:
- dhcppkt_put ( dhcppkt );
- err_alloc_dhcppkt:
- err_no_src:
- free_iob ( iobuf );
- return rc;
- }
-
-
- static struct interface_operation dhcp_xfer_operations[] = {
- INTF_OP ( xfer_deliver, struct dhcp_session *, dhcp_deliver ),
- };
-
-
- static struct interface_descriptor dhcp_xfer_desc =
- INTF_DESC ( struct dhcp_session, xfer, dhcp_xfer_operations );
-
-
- static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
- struct dhcp_session *dhcp =
- container_of ( timer, struct dhcp_session, timer );
-
-
- if ( fail ) {
- dhcp_finished ( dhcp, -ETIMEDOUT );
- return;
- }
-
-
- dhcp->state->expired ( dhcp );
- }
-
-
-
-
- static struct interface_operation dhcp_job_op[] = {
- INTF_OP ( intf_close, struct dhcp_session *, dhcp_finished ),
- };
-
-
- static struct interface_descriptor dhcp_job_desc =
- INTF_DESC ( struct dhcp_session, job, dhcp_job_op );
-
-
-
-
- static struct sockaddr dhcp_peer = {
- .sa_family = AF_INET,
- };
-
-
- int start_dhcp ( struct interface *job, struct net_device *netdev ) {
- struct dhcp_session *dhcp;
- int rc;
-
-
- dhcp = zalloc ( sizeof ( *dhcp ) );
- if ( ! dhcp )
- return -ENOMEM;
- ref_init ( &dhcp->refcnt, dhcp_free );
- intf_init ( &dhcp->job, &dhcp_job_desc, &dhcp->refcnt );
- intf_init ( &dhcp->xfer, &dhcp_xfer_desc, &dhcp->refcnt );
- timer_init ( &dhcp->timer, dhcp_timer_expired, &dhcp->refcnt );
- dhcp->netdev = netdev_get ( netdev );
- dhcp->local.sin_family = AF_INET;
- dhcp->local.sin_port = htons ( BOOTPC_PORT );
- dhcp->xid = random();
-
-
- dhcp_last_xid = dhcp->xid;
-
-
- if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM, &dhcp_peer,
- ( struct sockaddr * ) &dhcp->local ) ) != 0 )
- goto err;
-
-
- dhcp_set_state ( dhcp, &dhcp_state_discover );
-
-
- intf_plug_plug ( &dhcp->job, job );
- ref_put ( &dhcp->refcnt );
- return 0;
-
- err:
- dhcp_finished ( dhcp, rc );
- ref_put ( &dhcp->refcnt );
- return rc;
- }
-
-
- static void pxebs_list ( struct dhcp_session *dhcp, void *raw,
- size_t raw_len, struct in_addr *ip ) {
- struct dhcp_pxe_boot_server *server = raw;
- size_t server_len;
- unsigned int i;
-
- while ( raw_len ) {
- if ( raw_len < sizeof ( *server ) ) {
- DBGC ( dhcp, "DHCP %p malformed PXE server list\n",
- dhcp );
- break;
- }
- server_len = offsetof ( typeof ( *server ),
- ip[ server->num_ip ] );
- if ( raw_len < server_len ) {
- DBGC ( dhcp, "DHCP %p malformed PXE server list\n",
- dhcp );
- break;
- }
- if ( server->type == dhcp->pxe_type ) {
- for ( i = 0 ; i < server->num_ip ; i++ )
- *(ip++) = server->ip[i];
- }
- server = ( ( ( void * ) server ) + server_len );
- raw_len -= server_len;
- }
- }
-
-
- int start_pxebs ( struct interface *job, struct net_device *netdev,
- unsigned int pxe_type ) {
- struct setting pxe_discovery_control_setting =
- { .tag = DHCP_PXE_DISCOVERY_CONTROL };
- struct setting pxe_boot_servers_setting =
- { .tag = DHCP_PXE_BOOT_SERVERS };
- struct setting pxe_boot_server_mcast_setting =
- { .tag = DHCP_PXE_BOOT_SERVER_MCAST };
- ssize_t pxebs_list_len;
- struct dhcp_session *dhcp;
- struct in_addr *ip;
- unsigned int pxe_discovery_control;
- int rc;
-
-
- pxebs_list_len = fetch_setting_len ( NULL, &pxe_boot_servers_setting );
- if ( pxebs_list_len < 0 )
- pxebs_list_len = 0;
-
-
- dhcp = zalloc ( sizeof ( *dhcp ) + sizeof ( *ip ) +
- sizeof ( *ip ) + pxebs_list_len +
- sizeof ( *ip ) );
- if ( ! dhcp )
- return -ENOMEM;
- ref_init ( &dhcp->refcnt, dhcp_free );
- intf_init ( &dhcp->job, &dhcp_job_desc, &dhcp->refcnt );
- intf_init ( &dhcp->xfer, &dhcp_xfer_desc, &dhcp->refcnt );
- timer_init ( &dhcp->timer, dhcp_timer_expired, &dhcp->refcnt );
- dhcp->netdev = netdev_get ( netdev );
- dhcp->local.sin_family = AF_INET;
- fetch_ipv4_setting ( netdev_settings ( netdev ), &ip_setting,
- &dhcp->local.sin_addr );
- dhcp->local.sin_port = htons ( BOOTPC_PORT );
- dhcp->pxe_type = cpu_to_le16 ( pxe_type );
-
-
- pxe_discovery_control =
- fetch_uintz_setting ( NULL, &pxe_discovery_control_setting );
- ip = ( ( ( void * ) dhcp ) + sizeof ( *dhcp ) );
- dhcp->pxe_attempt = ip;
- if ( ! ( pxe_discovery_control & PXEBS_NO_MULTICAST ) ) {
- fetch_ipv4_setting ( NULL, &pxe_boot_server_mcast_setting, ip);
- if ( ip->s_addr )
- ip++;
- }
- if ( ! ( pxe_discovery_control & PXEBS_NO_BROADCAST ) )
- (ip++)->s_addr = INADDR_BROADCAST;
- if ( pxe_discovery_control & PXEBS_NO_UNKNOWN_SERVERS )
- dhcp->pxe_accept = ip;
- if ( pxebs_list_len ) {
- uint8_t buf[pxebs_list_len];
-
- fetch_setting ( NULL, &pxe_boot_servers_setting,
- buf, sizeof ( buf ) );
- pxebs_list ( dhcp, buf, sizeof ( buf ), ip );
- }
- if ( ! dhcp->pxe_attempt->s_addr ) {
- DBGC ( dhcp, "DHCP %p has no PXE boot servers for type %04x\n",
- dhcp, pxe_type );
- rc = -EINVAL;
- goto err;
- }
-
-
- DBGC ( dhcp, "DHCP %p attempting", dhcp );
- for ( ip = dhcp->pxe_attempt ; ip->s_addr ; ip++ )
- DBGC ( dhcp, " %s", inet_ntoa ( *ip ) );
- DBGC ( dhcp, "\n" );
- if ( dhcp->pxe_accept ) {
- DBGC ( dhcp, "DHCP %p accepting", dhcp );
- for ( ip = dhcp->pxe_accept ; ip->s_addr ; ip++ )
- DBGC ( dhcp, " %s", inet_ntoa ( *ip ) );
- DBGC ( dhcp, "\n" );
- }
-
-
- if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM, &dhcp_peer,
- ( struct sockaddr * ) &dhcp->local ) ) != 0 )
- goto err;
-
-
- dhcp_set_state ( dhcp, &dhcp_state_pxebs );
-
-
- intf_plug_plug ( &dhcp->job, job );
- ref_put ( &dhcp->refcnt );
- return 0;
-
- err:
- dhcp_finished ( dhcp, rc );
- ref_put ( &dhcp->refcnt );
- return rc;
- }
-
-
- struct net_device_configurator dhcp_configurator __net_device_configurator = {
- .name = "dhcp",
- .start = start_dhcp,
- };
|