]> git.proxmox.com Git - grub2.git/blob - debian/grub-extras/disabled/gpxe/src/net/udp/dhcp.c
grub2 (2.02+dfsg1-20) unstable; urgency=medium
[grub2.git] / debian / grub-extras / disabled / gpxe / src / net / udp / dhcp.c
1 /*
2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 FILE_LICENCE ( GPL2_OR_LATER );
20
21 #include <string.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <ctype.h>
25 #include <errno.h>
26 #include <assert.h>
27 #include <byteswap.h>
28 #include <gpxe/if_ether.h>
29 #include <gpxe/netdevice.h>
30 #include <gpxe/device.h>
31 #include <gpxe/xfer.h>
32 #include <gpxe/open.h>
33 #include <gpxe/job.h>
34 #include <gpxe/retry.h>
35 #include <gpxe/tcpip.h>
36 #include <gpxe/ip.h>
37 #include <gpxe/uuid.h>
38 #include <gpxe/timer.h>
39 #include <gpxe/settings.h>
40 #include <gpxe/dhcp.h>
41 #include <gpxe/dhcpopts.h>
42 #include <gpxe/dhcppkt.h>
43 #include <gpxe/features.h>
44
45 /** @file
46 *
47 * Dynamic Host Configuration Protocol
48 *
49 */
50
51 struct dhcp_session;
52 static int dhcp_tx ( struct dhcp_session *dhcp );
53
54 /**
55 * DHCP operation types
56 *
57 * This table maps from DHCP message types (i.e. values of the @c
58 * DHCP_MESSAGE_TYPE option) to values of the "op" field within a DHCP
59 * packet.
60 */
61 static const uint8_t dhcp_op[] = {
62 [DHCPDISCOVER] = BOOTP_REQUEST,
63 [DHCPOFFER] = BOOTP_REPLY,
64 [DHCPREQUEST] = BOOTP_REQUEST,
65 [DHCPDECLINE] = BOOTP_REQUEST,
66 [DHCPACK] = BOOTP_REPLY,
67 [DHCPNAK] = BOOTP_REPLY,
68 [DHCPRELEASE] = BOOTP_REQUEST,
69 [DHCPINFORM] = BOOTP_REQUEST,
70 };
71
72 /** Raw option data for options common to all DHCP requests */
73 static uint8_t dhcp_request_options_data[] = {
74 DHCP_MAX_MESSAGE_SIZE,
75 DHCP_WORD ( ETH_MAX_MTU - 20 /* IP header */ - 8 /* UDP header */ ),
76 DHCP_CLIENT_ARCHITECTURE, DHCP_WORD ( 0 ),
77 DHCP_CLIENT_NDI, DHCP_OPTION ( 1 /* UNDI */ , 2, 1 /* v2.1 */ ),
78 DHCP_VENDOR_CLASS_ID,
79 DHCP_STRING ( 'P', 'X', 'E', 'C', 'l', 'i', 'e', 'n', 't', ':',
80 'A', 'r', 'c', 'h', ':', '0', '0', '0', '0', '0', ':',
81 'U', 'N', 'D', 'I', ':', '0', '0', '2', '0', '0', '1' ),
82 DHCP_USER_CLASS_ID,
83 DHCP_STRING ( 'g', 'P', 'X', 'E' ),
84 DHCP_PARAMETER_REQUEST_LIST,
85 DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_DNS_SERVERS,
86 DHCP_LOG_SERVERS, DHCP_HOST_NAME, DHCP_DOMAIN_NAME,
87 DHCP_ROOT_PATH, DHCP_VENDOR_ENCAP, DHCP_VENDOR_CLASS_ID,
88 DHCP_TFTP_SERVER_NAME, DHCP_BOOTFILE_NAME,
89 DHCP_EB_ENCAP, DHCP_ISCSI_INITIATOR_IQN ),
90 DHCP_END
91 };
92
93 /** Version number feature */
94 FEATURE_VERSION ( VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH );
95
96 /** DHCP server address setting */
97 struct setting dhcp_server_setting __setting = {
98 .name = "dhcp-server",
99 .description = "DHCP server address",
100 .tag = DHCP_SERVER_IDENTIFIER,
101 .type = &setting_type_ipv4,
102 };
103
104 /** DHCP user class setting */
105 struct setting user_class_setting __setting = {
106 .name = "user-class",
107 .description = "User class identifier",
108 .tag = DHCP_USER_CLASS_ID,
109 .type = &setting_type_string,
110 };
111
112 /**
113 * Name a DHCP packet type
114 *
115 * @v msgtype DHCP message type
116 * @ret string DHCP mesasge type name
117 */
118 static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
119 switch ( msgtype ) {
120 case DHCPNONE: return "BOOTP"; /* Non-DHCP packet */
121 case DHCPDISCOVER: return "DHCPDISCOVER";
122 case DHCPOFFER: return "DHCPOFFER";
123 case DHCPREQUEST: return "DHCPREQUEST";
124 case DHCPDECLINE: return "DHCPDECLINE";
125 case DHCPACK: return "DHCPACK";
126 case DHCPNAK: return "DHCPNAK";
127 case DHCPRELEASE: return "DHCPRELEASE";
128 case DHCPINFORM: return "DHCPINFORM";
129 default: return "DHCP<invalid>";
130 }
131 }
132
133 /**
134 * Calculate DHCP transaction ID for a network device
135 *
136 * @v netdev Network device
137 * @ret xid DHCP XID
138 *
139 * Extract the least significant bits of the hardware address for use
140 * as the transaction ID.
141 */
142 static uint32_t dhcp_xid ( struct net_device *netdev ) {
143 uint32_t xid;
144
145 memcpy ( &xid, ( netdev->ll_addr + netdev->ll_protocol->ll_addr_len
146 - sizeof ( xid ) ), sizeof ( xid ) );
147 return xid;
148 }
149
150 /****************************************************************************
151 *
152 * DHCP session
153 *
154 */
155
156 struct dhcp_session;
157
158 /** DHCP session state operations */
159 struct dhcp_session_state {
160 /** State name */
161 const char *name;
162 /**
163 * Construct transmitted packet
164 *
165 * @v dhcp DHCP session
166 * @v dhcppkt DHCP packet
167 * @v peer Destination address
168 */
169 int ( * tx ) ( struct dhcp_session *dhcp,
170 struct dhcp_packet *dhcppkt,
171 struct sockaddr_in *peer );
172 /** Handle received packet
173 *
174 * @v dhcp DHCP session
175 * @v dhcppkt DHCP packet
176 * @v peer DHCP server address
177 * @v msgtype DHCP message type
178 * @v server_id DHCP server ID
179 */
180 void ( * rx ) ( struct dhcp_session *dhcp,
181 struct dhcp_packet *dhcppkt,
182 struct sockaddr_in *peer,
183 uint8_t msgtype, struct in_addr server_id );
184 /** Handle timer expiry
185 *
186 * @v dhcp DHCP session
187 */
188 void ( * expired ) ( struct dhcp_session *dhcp );
189 /** Transmitted message type */
190 uint8_t tx_msgtype;
191 /** Apply minimum timeout */
192 uint8_t apply_min_timeout;
193 };
194
195 static struct dhcp_session_state dhcp_state_discover;
196 static struct dhcp_session_state dhcp_state_request;
197 static struct dhcp_session_state dhcp_state_proxy;
198 static struct dhcp_session_state dhcp_state_pxebs;
199
200 /** A DHCP session */
201 struct dhcp_session {
202 /** Reference counter */
203 struct refcnt refcnt;
204 /** Job control interface */
205 struct job_interface job;
206 /** Data transfer interface */
207 struct xfer_interface xfer;
208
209 /** Network device being configured */
210 struct net_device *netdev;
211 /** Local socket address */
212 struct sockaddr_in local;
213 /** State of the session */
214 struct dhcp_session_state *state;
215
216 /** Offered IP address */
217 struct in_addr offer;
218 /** DHCP server */
219 struct in_addr server;
220 /** DHCP offer priority */
221 int priority;
222
223 /** ProxyDHCP protocol extensions should be ignored */
224 int no_pxedhcp;
225 /** ProxyDHCP server */
226 struct in_addr proxy_server;
227 /** ProxyDHCP port */
228 uint16_t proxy_port;
229 /** ProxyDHCP server priority */
230 int proxy_priority;
231
232 /** PXE Boot Server type */
233 uint16_t pxe_type;
234 /** List of PXE Boot Servers to attempt */
235 struct in_addr *pxe_attempt;
236 /** List of PXE Boot Servers to accept */
237 struct in_addr *pxe_accept;
238
239 /** Retransmission timer */
240 struct retry_timer timer;
241 /** Start time of the current state (in ticks) */
242 unsigned long start;
243 };
244
245 /**
246 * Free DHCP session
247 *
248 * @v refcnt Reference counter
249 */
250 static void dhcp_free ( struct refcnt *refcnt ) {
251 struct dhcp_session *dhcp =
252 container_of ( refcnt, struct dhcp_session, refcnt );
253
254 netdev_put ( dhcp->netdev );
255 free ( dhcp );
256 }
257
258 /**
259 * Mark DHCP session as complete
260 *
261 * @v dhcp DHCP session
262 * @v rc Return status code
263 */
264 static void dhcp_finished ( struct dhcp_session *dhcp, int rc ) {
265
266 /* Block futher incoming messages */
267 job_nullify ( &dhcp->job );
268 xfer_nullify ( &dhcp->xfer );
269
270 /* Stop retry timer */
271 stop_timer ( &dhcp->timer );
272
273 /* Free resources and close interfaces */
274 xfer_close ( &dhcp->xfer, rc );
275 job_done ( &dhcp->job, rc );
276 }
277
278 /**
279 * Transition to new DHCP session state
280 *
281 * @v dhcp DHCP session
282 * @v state New session state
283 */
284 static void dhcp_set_state ( struct dhcp_session *dhcp,
285 struct dhcp_session_state *state ) {
286
287 DBGC ( dhcp, "DHCP %p entering %s state\n", dhcp, state->name );
288 dhcp->state = state;
289 dhcp->start = currticks();
290 stop_timer ( &dhcp->timer );
291 dhcp->timer.min_timeout =
292 ( state->apply_min_timeout ? DHCP_MIN_TIMEOUT : 0 );
293 dhcp->timer.max_timeout = DHCP_MAX_TIMEOUT;
294 start_timer_nodelay ( &dhcp->timer );
295 }
296
297 /****************************************************************************
298 *
299 * DHCP state machine
300 *
301 */
302
303 /**
304 * Construct transmitted packet for DHCP discovery
305 *
306 * @v dhcp DHCP session
307 * @v dhcppkt DHCP packet
308 * @v peer Destination address
309 */
310 static int dhcp_discovery_tx ( struct dhcp_session *dhcp,
311 struct dhcp_packet *dhcppkt __unused,
312 struct sockaddr_in *peer ) {
313
314 DBGC ( dhcp, "DHCP %p DHCPDISCOVER\n", dhcp );
315
316 /* Set server address */
317 peer->sin_addr.s_addr = INADDR_BROADCAST;
318 peer->sin_port = htons ( BOOTPS_PORT );
319
320 return 0;
321 }
322
323 /**
324 * Handle received packet during DHCP discovery
325 *
326 * @v dhcp DHCP session
327 * @v dhcppkt DHCP packet
328 * @v peer DHCP server address
329 * @v msgtype DHCP message type
330 * @v server_id DHCP server ID
331 */
332 static void dhcp_discovery_rx ( struct dhcp_session *dhcp,
333 struct dhcp_packet *dhcppkt,
334 struct sockaddr_in *peer, uint8_t msgtype,
335 struct in_addr server_id ) {
336 struct in_addr ip;
337 char vci[9]; /* "PXEClient" */
338 int vci_len;
339 int has_pxeclient;
340 int pxeopts_len;
341 int has_pxeopts;
342 int8_t priority = 0;
343 uint8_t no_pxedhcp = 0;
344 unsigned long elapsed;
345
346 DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
347 dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
348 ntohs ( peer->sin_port ) );
349 if ( server_id.s_addr != peer->sin_addr.s_addr )
350 DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
351
352 /* Identify offered IP address */
353 ip = dhcppkt->dhcphdr->yiaddr;
354 if ( ip.s_addr )
355 DBGC ( dhcp, " for %s", inet_ntoa ( ip ) );
356
357 /* Identify "PXEClient" vendor class */
358 vci_len = dhcppkt_fetch ( dhcppkt, DHCP_VENDOR_CLASS_ID,
359 vci, sizeof ( vci ) );
360 has_pxeclient = ( ( vci_len >= ( int ) sizeof ( vci ) ) &&
361 ( strncmp ( "PXEClient", vci, sizeof (vci) ) == 0 ));
362
363 /* Identify presence of vendor-specific options */
364 pxeopts_len = dhcppkt_fetch ( dhcppkt, DHCP_VENDOR_ENCAP, NULL, 0 );
365 has_pxeopts = ( pxeopts_len >= 0 );
366 if ( has_pxeclient )
367 DBGC ( dhcp, "%s", ( has_pxeopts ? " pxe" : " proxy" ) );
368
369 /* Identify priority */
370 dhcppkt_fetch ( dhcppkt, DHCP_EB_PRIORITY, &priority,
371 sizeof ( priority ) );
372 if ( priority )
373 DBGC ( dhcp, " pri %d", priority );
374
375 /* Identify ignore-PXE flag */
376 dhcppkt_fetch ( dhcppkt, DHCP_EB_NO_PXEDHCP, &no_pxedhcp,
377 sizeof ( no_pxedhcp ) );
378 if ( no_pxedhcp )
379 DBGC ( dhcp, " nopxe" );
380 DBGC ( dhcp, "\n" );
381
382 /* Select as DHCP offer, if applicable */
383 if ( ip.s_addr && ( peer->sin_port == htons ( BOOTPS_PORT ) ) &&
384 ( ( msgtype == DHCPOFFER ) || ( ! msgtype /* BOOTP */ ) ) &&
385 ( priority >= dhcp->priority ) ) {
386 dhcp->offer = ip;
387 dhcp->server = server_id;
388 dhcp->priority = priority;
389 dhcp->no_pxedhcp = no_pxedhcp;
390 }
391
392 /* Select as ProxyDHCP offer, if applicable */
393 if ( has_pxeclient && ( msgtype == DHCPOFFER ) &&
394 ( priority >= dhcp->proxy_priority ) ) {
395 /* If the offer already includes the PXE options, then
396 * assume that we can send the ProxyDHCPREQUEST to
397 * port 67 (since the DHCPDISCOVER that triggered this
398 * ProxyDHCPOFFER was sent to port 67). Otherwise,
399 * send the ProxyDHCPREQUEST to port 4011.
400 */
401 dhcp->proxy_server = server_id;
402 dhcp->proxy_port = ( has_pxeopts ? htons ( BOOTPS_PORT )
403 : htons ( PXE_PORT ) );
404 dhcp->proxy_priority = priority;
405 }
406
407 /* We can exit the discovery state when we have a valid
408 * DHCPOFFER, and either:
409 *
410 * o The DHCPOFFER instructs us to ignore ProxyDHCPOFFERs, or
411 * o We have a valid ProxyDHCPOFFER, or
412 * o We have allowed sufficient time for ProxyDHCPOFFERs.
413 */
414
415 /* If we don't yet have a DHCPOFFER, do nothing */
416 if ( ! dhcp->offer.s_addr )
417 return;
418
419 /* If we can't yet transition to DHCPREQUEST, do nothing */
420 elapsed = ( currticks() - dhcp->start );
421 if ( ! ( dhcp->no_pxedhcp || dhcp->proxy_server.s_addr ||
422 ( elapsed > PROXYDHCP_MAX_TIMEOUT ) ) )
423 return;
424
425 /* Transition to DHCPREQUEST */
426 dhcp_set_state ( dhcp, &dhcp_state_request );
427 }
428
429 /**
430 * Handle timer expiry during DHCP discovery
431 *
432 * @v dhcp DHCP session
433 */
434 static void dhcp_discovery_expired ( struct dhcp_session *dhcp ) {
435 unsigned long elapsed = ( currticks() - dhcp->start );
436
437 /* Give up waiting for ProxyDHCP before we reach the failure point */
438 if ( dhcp->offer.s_addr && ( elapsed > PROXYDHCP_MAX_TIMEOUT ) ) {
439 dhcp_set_state ( dhcp, &dhcp_state_request );
440 return;
441 }
442
443 /* Otherwise, retransmit current packet */
444 dhcp_tx ( dhcp );
445 }
446
447 /** DHCP discovery state operations */
448 static struct dhcp_session_state dhcp_state_discover = {
449 .name = "discovery",
450 .tx = dhcp_discovery_tx,
451 .rx = dhcp_discovery_rx,
452 .expired = dhcp_discovery_expired,
453 .tx_msgtype = DHCPDISCOVER,
454 .apply_min_timeout = 1,
455 };
456
457 /**
458 * Construct transmitted packet for DHCP request
459 *
460 * @v dhcp DHCP session
461 * @v dhcppkt DHCP packet
462 * @v peer Destination address
463 */
464 static int dhcp_request_tx ( struct dhcp_session *dhcp,
465 struct dhcp_packet *dhcppkt,
466 struct sockaddr_in *peer ) {
467 int rc;
468
469 DBGC ( dhcp, "DHCP %p DHCPREQUEST to %s:%d",
470 dhcp, inet_ntoa ( dhcp->server ), BOOTPS_PORT );
471 DBGC ( dhcp, " for %s\n", inet_ntoa ( dhcp->offer ) );
472
473 /* Set server ID */
474 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
475 &dhcp->server,
476 sizeof ( dhcp->server ) ) ) != 0 )
477 return rc;
478
479 /* Set requested IP address */
480 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_REQUESTED_ADDRESS,
481 &dhcp->offer,
482 sizeof ( dhcp->offer ) ) ) != 0 )
483 return rc;
484
485 /* Set server address */
486 peer->sin_addr.s_addr = INADDR_BROADCAST;
487 peer->sin_port = htons ( BOOTPS_PORT );
488
489 return 0;
490 }
491
492 /**
493 * Handle received packet during DHCP request
494 *
495 * @v dhcp DHCP session
496 * @v dhcppkt DHCP packet
497 * @v peer DHCP server address
498 * @v msgtype DHCP message type
499 * @v server_id DHCP server ID
500 */
501 static void dhcp_request_rx ( struct dhcp_session *dhcp,
502 struct dhcp_packet *dhcppkt,
503 struct sockaddr_in *peer, uint8_t msgtype,
504 struct in_addr server_id ) {
505 struct in_addr ip;
506 struct settings *parent;
507 int rc;
508
509 DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
510 dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
511 ntohs ( peer->sin_port ) );
512 if ( server_id.s_addr != peer->sin_addr.s_addr )
513 DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
514
515 /* Identify leased IP address */
516 ip = dhcppkt->dhcphdr->yiaddr;
517 if ( ip.s_addr )
518 DBGC ( dhcp, " for %s", inet_ntoa ( ip ) );
519 DBGC ( dhcp, "\n" );
520
521 /* Filter out unacceptable responses */
522 if ( peer->sin_port != htons ( BOOTPS_PORT ) )
523 return;
524 if ( msgtype /* BOOTP */ && ( msgtype != DHCPACK ) )
525 return;
526 if ( server_id.s_addr != dhcp->server.s_addr )
527 return;
528
529 /* Record assigned address */
530 dhcp->local.sin_addr = ip;
531
532 /* Register settings */
533 parent = netdev_settings ( dhcp->netdev );
534 if ( ( rc = register_settings ( &dhcppkt->settings, parent ) ) != 0 ){
535 DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
536 dhcp, strerror ( rc ) );
537 dhcp_finished ( dhcp, rc );
538 return;
539 }
540
541 /* Start ProxyDHCPREQUEST if applicable */
542 if ( dhcp->proxy_server.s_addr /* Have ProxyDHCP server */ &&
543 ( ! dhcp->no_pxedhcp ) /* ProxyDHCP not disabled */ &&
544 ( /* ProxyDHCP server is not just the DHCP server itself */
545 ( dhcp->proxy_server.s_addr != dhcp->server.s_addr ) ||
546 ( dhcp->proxy_port != htons ( BOOTPS_PORT ) ) ) ) {
547 dhcp_set_state ( dhcp, &dhcp_state_proxy );
548 return;
549 }
550
551 /* Terminate DHCP */
552 dhcp_finished ( dhcp, 0 );
553 }
554
555 /**
556 * Handle timer expiry during DHCP discovery
557 *
558 * @v dhcp DHCP session
559 */
560 static void dhcp_request_expired ( struct dhcp_session *dhcp ) {
561
562 /* Retransmit current packet */
563 dhcp_tx ( dhcp );
564 }
565
566 /** DHCP request state operations */
567 static struct dhcp_session_state dhcp_state_request = {
568 .name = "request",
569 .tx = dhcp_request_tx,
570 .rx = dhcp_request_rx,
571 .expired = dhcp_request_expired,
572 .tx_msgtype = DHCPREQUEST,
573 .apply_min_timeout = 0,
574 };
575
576 /**
577 * Construct transmitted packet for ProxyDHCP request
578 *
579 * @v dhcp DHCP session
580 * @v dhcppkt DHCP packet
581 * @v peer Destination address
582 */
583 static int dhcp_proxy_tx ( struct dhcp_session *dhcp,
584 struct dhcp_packet *dhcppkt,
585 struct sockaddr_in *peer ) {
586 int rc;
587
588 DBGC ( dhcp, "DHCP %p ProxyDHCP REQUEST to %s:%d\n", dhcp,
589 inet_ntoa ( dhcp->proxy_server ), ntohs ( dhcp->proxy_port ) );
590
591 /* Set server ID */
592 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_SERVER_IDENTIFIER,
593 &dhcp->proxy_server,
594 sizeof ( dhcp->proxy_server ) ) ) != 0 )
595 return rc;
596
597 /* Set server address */
598 peer->sin_addr = dhcp->proxy_server;
599 peer->sin_port = dhcp->proxy_port;
600
601 return 0;
602 }
603
604 /**
605 * Handle received packet during ProxyDHCP request
606 *
607 * @v dhcp DHCP session
608 * @v dhcppkt DHCP packet
609 * @v peer DHCP server address
610 * @v msgtype DHCP message type
611 * @v server_id DHCP server ID
612 */
613 static void dhcp_proxy_rx ( struct dhcp_session *dhcp,
614 struct dhcp_packet *dhcppkt,
615 struct sockaddr_in *peer, uint8_t msgtype,
616 struct in_addr server_id ) {
617 int rc;
618
619 DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
620 dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
621 ntohs ( peer->sin_port ) );
622 if ( server_id.s_addr != peer->sin_addr.s_addr )
623 DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
624 DBGC ( dhcp, "\n" );
625
626 /* Filter out unacceptable responses */
627 if ( peer->sin_port != dhcp->proxy_port )
628 return;
629 if ( msgtype != DHCPACK )
630 return;
631 if ( server_id.s_addr /* Linux PXE server omits server ID */ &&
632 ( server_id.s_addr != dhcp->proxy_server.s_addr ) )
633 return;
634
635 /* Register settings */
636 dhcppkt->settings.name = PROXYDHCP_SETTINGS_NAME;
637 if ( ( rc = register_settings ( &dhcppkt->settings, NULL ) ) != 0 ) {
638 DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
639 dhcp, strerror ( rc ) );
640 dhcp_finished ( dhcp, rc );
641 return;
642 }
643
644 /* Terminate DHCP */
645 dhcp_finished ( dhcp, 0 );
646 }
647
648 /**
649 * Handle timer expiry during ProxyDHCP request
650 *
651 * @v dhcp DHCP session
652 */
653 static void dhcp_proxy_expired ( struct dhcp_session *dhcp ) {
654 unsigned long elapsed = ( currticks() - dhcp->start );
655
656 /* Give up waiting for ProxyDHCP before we reach the failure point */
657 if ( elapsed > PROXYDHCP_MAX_TIMEOUT ) {
658 dhcp_finished ( dhcp, 0 );
659 return;
660 }
661
662 /* Retransmit current packet */
663 dhcp_tx ( dhcp );
664 }
665
666 /** ProxyDHCP request state operations */
667 static struct dhcp_session_state dhcp_state_proxy = {
668 .name = "ProxyDHCP",
669 .tx = dhcp_proxy_tx,
670 .rx = dhcp_proxy_rx,
671 .expired = dhcp_proxy_expired,
672 .tx_msgtype = DHCPREQUEST,
673 .apply_min_timeout = 0,
674 };
675
676 /**
677 * Construct transmitted packet for PXE Boot Server Discovery
678 *
679 * @v dhcp DHCP session
680 * @v dhcppkt DHCP packet
681 * @v peer Destination address
682 */
683 static int dhcp_pxebs_tx ( struct dhcp_session *dhcp,
684 struct dhcp_packet *dhcppkt,
685 struct sockaddr_in *peer ) {
686 struct dhcp_pxe_boot_menu_item menu_item = { 0, 0 };
687 int rc;
688
689 /* Set server address */
690 peer->sin_addr = *(dhcp->pxe_attempt);
691 peer->sin_port = ( ( peer->sin_addr.s_addr == INADDR_BROADCAST ) ?
692 htons ( BOOTPS_PORT ) : htons ( PXE_PORT ) );
693
694 DBGC ( dhcp, "DHCP %p PXEBS REQUEST to %s:%d for type %d\n",
695 dhcp, inet_ntoa ( peer->sin_addr ), ntohs ( peer->sin_port ),
696 ntohs ( dhcp->pxe_type ) );
697
698 /* Set boot menu item */
699 menu_item.type = dhcp->pxe_type;
700 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_PXE_BOOT_MENU_ITEM,
701 &menu_item, sizeof ( menu_item ) ) ) != 0 )
702 return rc;
703
704 return 0;
705 }
706
707 /**
708 * Check to see if PXE Boot Server address is acceptable
709 *
710 * @v dhcp DHCP session
711 * @v bs Boot Server address
712 * @ret accept Boot Server is acceptable
713 */
714 static int dhcp_pxebs_accept ( struct dhcp_session *dhcp,
715 struct in_addr bs ) {
716 struct in_addr *accept;
717
718 /* Accept if we have no acceptance filter */
719 if ( ! dhcp->pxe_accept )
720 return 1;
721
722 /* Scan through acceptance list */
723 for ( accept = dhcp->pxe_accept ; accept->s_addr ; accept++ ) {
724 if ( accept->s_addr == bs.s_addr )
725 return 1;
726 }
727
728 DBGC ( dhcp, "DHCP %p rejecting server %s\n",
729 dhcp, inet_ntoa ( bs ) );
730 return 0;
731 }
732
733 /**
734 * Handle received packet during PXE Boot Server Discovery
735 *
736 * @v dhcp DHCP session
737 * @v dhcppkt DHCP packet
738 * @v peer DHCP server address
739 * @v msgtype DHCP message type
740 * @v server_id DHCP server ID
741 */
742 static void dhcp_pxebs_rx ( struct dhcp_session *dhcp,
743 struct dhcp_packet *dhcppkt,
744 struct sockaddr_in *peer, uint8_t msgtype,
745 struct in_addr server_id ) {
746 struct dhcp_pxe_boot_menu_item menu_item = { 0, 0 };
747 int rc;
748
749 DBGC ( dhcp, "DHCP %p %s from %s:%d", dhcp,
750 dhcp_msgtype_name ( msgtype ), inet_ntoa ( peer->sin_addr ),
751 ntohs ( peer->sin_port ) );
752 if ( server_id.s_addr != peer->sin_addr.s_addr )
753 DBGC ( dhcp, " (%s)", inet_ntoa ( server_id ) );
754
755 /* Identify boot menu item */
756 dhcppkt_fetch ( dhcppkt, DHCP_PXE_BOOT_MENU_ITEM,
757 &menu_item, sizeof ( menu_item ) );
758 if ( menu_item.type )
759 DBGC ( dhcp, " for type %d", ntohs ( menu_item.type ) );
760 DBGC ( dhcp, "\n" );
761
762 /* Filter out unacceptable responses */
763 if ( ( peer->sin_port != htons ( BOOTPS_PORT ) ) &&
764 ( peer->sin_port != htons ( PXE_PORT ) ) )
765 return;
766 if ( msgtype != DHCPACK )
767 return;
768 if ( menu_item.type != dhcp->pxe_type )
769 return;
770 if ( ! dhcp_pxebs_accept ( dhcp, ( server_id.s_addr ?
771 server_id : peer->sin_addr ) ) )
772 return;
773
774 /* Register settings */
775 dhcppkt->settings.name = PXEBS_SETTINGS_NAME;
776 if ( ( rc = register_settings ( &dhcppkt->settings, NULL ) ) != 0 ) {
777 DBGC ( dhcp, "DHCP %p could not register settings: %s\n",
778 dhcp, strerror ( rc ) );
779 dhcp_finished ( dhcp, rc );
780 return;
781 }
782
783 /* Terminate DHCP */
784 dhcp_finished ( dhcp, 0 );
785 }
786
787 /**
788 * Handle timer expiry during PXE Boot Server Discovery
789 *
790 * @v dhcp DHCP session
791 */
792 static void dhcp_pxebs_expired ( struct dhcp_session *dhcp ) {
793 unsigned long elapsed = ( currticks() - dhcp->start );
794
795 /* Give up waiting before we reach the failure point, and fail
796 * over to the next server in the attempt list
797 */
798 if ( elapsed > PXEBS_MAX_TIMEOUT ) {
799 dhcp->pxe_attempt++;
800 if ( dhcp->pxe_attempt->s_addr ) {
801 dhcp_set_state ( dhcp, &dhcp_state_pxebs );
802 return;
803 } else {
804 dhcp_finished ( dhcp, -ETIMEDOUT );
805 return;
806 }
807 }
808
809 /* Retransmit current packet */
810 dhcp_tx ( dhcp );
811 }
812
813 /** PXE Boot Server Discovery state operations */
814 static struct dhcp_session_state dhcp_state_pxebs = {
815 .name = "PXEBS",
816 .tx = dhcp_pxebs_tx,
817 .rx = dhcp_pxebs_rx,
818 .expired = dhcp_pxebs_expired,
819 .tx_msgtype = DHCPREQUEST,
820 .apply_min_timeout = 1,
821 };
822
823 /****************************************************************************
824 *
825 * Packet construction
826 *
827 */
828
829 /**
830 * Construct DHCP client hardware address field and broadcast flag
831 *
832 * @v netdev Network device
833 * @v hlen DHCP hardware address length to fill in
834 * @v flags DHCP flags to fill in
835 * @ret chaddr DHCP client hardware address
836 */
837 void * dhcp_chaddr ( struct net_device *netdev, uint8_t *hlen,
838 uint16_t *flags ) {
839 struct ll_protocol *ll_protocol = netdev->ll_protocol;
840 typeof ( ( ( struct dhcphdr * ) NULL )->chaddr ) chaddr;
841
842 /* If the link-layer address cannot fit into the chaddr field
843 * (as is the case for IPoIB) then try using the hardware
844 * address instead. If we do this, set the broadcast flag,
845 * since chaddr then does not represent a valid link-layer
846 * address for the return path.
847 *
848 * If even the hardware address is too large, use an empty
849 * chaddr field and set the broadcast flag.
850 *
851 * This goes against RFC4390, but RFC4390 mandates that we use
852 * a DHCP client identifier that conforms with RFC4361, which
853 * we cannot do without either persistent (NIC-independent)
854 * storage, or by eliminating the hardware address completely
855 * from the DHCP packet, which seems unfriendly to users.
856 */
857 if ( ( *hlen = ll_protocol->ll_addr_len ) <= sizeof ( chaddr ) ) {
858 return netdev->ll_addr;
859 }
860 *flags = htons ( BOOTP_FL_BROADCAST );
861 if ( ( *hlen = ll_protocol->hw_addr_len ) <= sizeof ( chaddr ) ) {
862 return netdev->hw_addr;
863 } else {
864 *hlen = 0;
865 return NULL;
866 }
867 }
868
869 /**
870 * Create a DHCP packet
871 *
872 * @v dhcppkt DHCP packet structure to fill in
873 * @v netdev Network device
874 * @v msgtype DHCP message type
875 * @v options Initial options to include (or NULL)
876 * @v options_len Length of initial options
877 * @v data Buffer for DHCP packet
878 * @v max_len Size of DHCP packet buffer
879 * @ret rc Return status code
880 *
881 * Creates a DHCP packet in the specified buffer, and initialise a
882 * DHCP packet structure.
883 */
884 int dhcp_create_packet ( struct dhcp_packet *dhcppkt,
885 struct net_device *netdev, uint8_t msgtype,
886 const void *options, size_t options_len,
887 void *data, size_t max_len ) {
888 struct dhcphdr *dhcphdr = data;
889 void *chaddr;
890 int rc;
891
892 /* Sanity check */
893 if ( max_len < ( sizeof ( *dhcphdr ) + options_len ) )
894 return -ENOSPC;
895
896 /* Initialise DHCP packet content */
897 memset ( dhcphdr, 0, max_len );
898 dhcphdr->xid = dhcp_xid ( netdev );
899 dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
900 dhcphdr->htype = ntohs ( netdev->ll_protocol->ll_proto );
901 dhcphdr->op = dhcp_op[msgtype];
902 chaddr = dhcp_chaddr ( netdev, &dhcphdr->hlen, &dhcphdr->flags );
903 memcpy ( dhcphdr->chaddr, chaddr, dhcphdr->hlen );
904 memcpy ( dhcphdr->options, options, options_len );
905
906 /* Initialise DHCP packet structure */
907 memset ( dhcppkt, 0, sizeof ( *dhcppkt ) );
908 dhcppkt_init ( dhcppkt, data, max_len );
909
910 /* Set DHCP_MESSAGE_TYPE option */
911 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_MESSAGE_TYPE,
912 &msgtype, sizeof ( msgtype ) ) ) != 0 )
913 return rc;
914
915 return 0;
916 }
917
918 /**
919 * Create DHCP request packet
920 *
921 * @v dhcppkt DHCP packet structure to fill in
922 * @v netdev Network device
923 * @v msgtype DHCP message type
924 * @v ciaddr Client IP address
925 * @v data Buffer for DHCP packet
926 * @v max_len Size of DHCP packet buffer
927 * @ret rc Return status code
928 *
929 * Creates a DHCP request packet in the specified buffer, and
930 * initialise a DHCP packet structure.
931 */
932 int dhcp_create_request ( struct dhcp_packet *dhcppkt,
933 struct net_device *netdev, unsigned int msgtype,
934 struct in_addr ciaddr, void *data, size_t max_len ) {
935 struct device_description *desc = &netdev->dev->desc;
936 struct dhcp_netdev_desc dhcp_desc;
937 struct dhcp_client_id client_id;
938 struct dhcp_client_uuid client_uuid;
939 uint8_t *dhcp_features;
940 size_t dhcp_features_len;
941 size_t ll_addr_len;
942 ssize_t len;
943 int rc;
944
945 /* Create DHCP packet */
946 if ( ( rc = dhcp_create_packet ( dhcppkt, netdev, msgtype,
947 dhcp_request_options_data,
948 sizeof ( dhcp_request_options_data ),
949 data, max_len ) ) != 0 ) {
950 DBG ( "DHCP could not create DHCP packet: %s\n",
951 strerror ( rc ) );
952 return rc;
953 }
954
955 /* Set client IP address */
956 dhcppkt->dhcphdr->ciaddr = ciaddr;
957
958 /* Add options to identify the feature list */
959 #if 0
960 dhcp_features = table_start ( DHCP_FEATURES );
961 dhcp_features_len = table_num_entries ( DHCP_FEATURES );
962 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_ENCAP, dhcp_features,
963 dhcp_features_len ) ) != 0 ) {
964 DBG ( "DHCP could not set features list option: %s\n",
965 strerror ( rc ) );
966 return rc;
967 }
968 #endif
969
970 /* Add options to identify the network device */
971 dhcp_desc.type = desc->bus_type;
972 dhcp_desc.vendor = htons ( desc->vendor );
973 dhcp_desc.device = htons ( desc->device );
974 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_EB_BUS_ID, &dhcp_desc,
975 sizeof ( dhcp_desc ) ) ) != 0 ) {
976 DBG ( "DHCP could not set bus ID option: %s\n",
977 strerror ( rc ) );
978 return rc;
979 }
980
981 /* Add DHCP client identifier. Required for Infiniband, and
982 * doesn't hurt other link layers.
983 */
984 client_id.ll_proto = ntohs ( netdev->ll_protocol->ll_proto );
985 ll_addr_len = netdev->ll_protocol->ll_addr_len;
986 assert ( ll_addr_len <= sizeof ( client_id.ll_addr ) );
987 memcpy ( client_id.ll_addr, netdev->ll_addr, ll_addr_len );
988 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_ID, &client_id,
989 ( ll_addr_len + 1 ) ) ) != 0 ) {
990 DBG ( "DHCP could not set client ID: %s\n",
991 strerror ( rc ) );
992 return rc;
993 }
994
995 #if 0
996 /* Add client UUID, if we have one. Required for PXE. */
997 client_uuid.type = DHCP_CLIENT_UUID_TYPE;
998 if ( ( len = fetch_uuid_setting ( NULL, &uuid_setting,
999 &client_uuid.uuid ) ) >= 0 ) {
1000 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_CLIENT_UUID,
1001 &client_uuid,
1002 sizeof ( client_uuid ) ) ) != 0 ) {
1003 DBG ( "DHCP could not set client UUID: %s\n",
1004 strerror ( rc ) );
1005 return rc;
1006 }
1007 }
1008 #endif
1009
1010 /* Add user class, if we have one. */
1011 if ( ( len = fetch_setting_len ( NULL, &user_class_setting ) ) >= 0 ) {
1012 char user_class[len];
1013 fetch_setting ( NULL, &user_class_setting, user_class,
1014 sizeof ( user_class ) );
1015 if ( ( rc = dhcppkt_store ( dhcppkt, DHCP_USER_CLASS_ID,
1016 &user_class,
1017 sizeof ( user_class ) ) ) != 0 ) {
1018 DBG ( "DHCP could not set user class: %s\n",
1019 strerror ( rc ) );
1020 return rc;
1021 }
1022 }
1023
1024 return 0;
1025 }
1026
1027 /****************************************************************************
1028 *
1029 * Data transfer interface
1030 *
1031 */
1032
1033 /**
1034 * Transmit DHCP request
1035 *
1036 * @v dhcp DHCP session
1037 * @ret rc Return status code
1038 */
1039 static int dhcp_tx ( struct dhcp_session *dhcp ) {
1040 static struct sockaddr_in peer = {
1041 .sin_family = AF_INET,
1042 };
1043 struct xfer_metadata meta = {
1044 .netdev = dhcp->netdev,
1045 .src = ( struct sockaddr * ) &dhcp->local,
1046 .dest = ( struct sockaddr * ) &peer,
1047 };
1048 struct io_buffer *iobuf;
1049 uint8_t msgtype = dhcp->state->tx_msgtype;
1050 struct dhcp_packet dhcppkt;
1051 int rc;
1052
1053 /* Start retry timer. Do this first so that failures to
1054 * transmit will be retried.
1055 */
1056 start_timer ( &dhcp->timer );
1057
1058 /* Allocate buffer for packet */
1059 iobuf = xfer_alloc_iob ( &dhcp->xfer, DHCP_MIN_LEN );
1060 if ( ! iobuf )
1061 return -ENOMEM;
1062
1063 /* Create basic DHCP packet in temporary buffer */
1064 if ( ( rc = dhcp_create_request ( &dhcppkt, dhcp->netdev, msgtype,
1065 dhcp->local.sin_addr, iobuf->data,
1066 iob_tailroom ( iobuf ) ) ) != 0 ) {
1067 DBGC ( dhcp, "DHCP %p could not construct DHCP request: %s\n",
1068 dhcp, strerror ( rc ) );
1069 goto done;
1070 }
1071
1072 /* Fill in packet based on current state */
1073 if ( ( rc = dhcp->state->tx ( dhcp, &dhcppkt, &peer ) ) != 0 ) {
1074 DBGC ( dhcp, "DHCP %p could not fill DHCP request: %s\n",
1075 dhcp, strerror ( rc ) );
1076 goto done;
1077 }
1078
1079 /* Transmit the packet */
1080 iob_put ( iobuf, dhcppkt.len );
1081 if ( ( rc = xfer_deliver_iob_meta ( &dhcp->xfer, iob_disown ( iobuf ),
1082 &meta ) ) != 0 ) {
1083 DBGC ( dhcp, "DHCP %p could not transmit UDP packet: %s\n",
1084 dhcp, strerror ( rc ) );
1085 goto done;
1086 }
1087
1088 done:
1089 free_iob ( iobuf );
1090 return rc;
1091 }
1092
1093 /**
1094 * Receive new data
1095 *
1096 * @v xfer Data transfer interface
1097 * @v iobuf I/O buffer
1098 * @v meta Transfer metadata
1099 * @ret rc Return status code
1100 */
1101 static int dhcp_deliver_iob ( struct xfer_interface *xfer,
1102 struct io_buffer *iobuf,
1103 struct xfer_metadata *meta ) {
1104 struct dhcp_session *dhcp =
1105 container_of ( xfer, struct dhcp_session, xfer );
1106 struct sockaddr_in *peer;
1107 size_t data_len;
1108 struct dhcp_packet *dhcppkt;
1109 struct dhcphdr *dhcphdr;
1110 uint8_t msgtype = 0;
1111 struct in_addr server_id = { 0 };
1112 int rc = 0;
1113
1114 /* Sanity checks */
1115 if ( ! meta->src ) {
1116 DBGC ( dhcp, "DHCP %p received packet without source port\n",
1117 dhcp );
1118 rc = -EINVAL;
1119 goto err_no_src;
1120 }
1121 peer = ( struct sockaddr_in * ) meta->src;
1122
1123 /* Create a DHCP packet containing the I/O buffer contents.
1124 * Whilst we could just use the original buffer in situ, that
1125 * would waste the unused space in the packet buffer, and also
1126 * waste a relatively scarce fully-aligned I/O buffer.
1127 */
1128 data_len = iob_len ( iobuf );
1129 dhcppkt = zalloc ( sizeof ( *dhcppkt ) + data_len );
1130 if ( ! dhcppkt ) {
1131 rc = -ENOMEM;
1132 goto err_alloc_dhcppkt;
1133 }
1134 dhcphdr = ( ( ( void * ) dhcppkt ) + sizeof ( *dhcppkt ) );
1135 memcpy ( dhcphdr, iobuf->data, data_len );
1136 dhcppkt_init ( dhcppkt, dhcphdr, data_len );
1137
1138 /* Identify message type */
1139 dhcppkt_fetch ( dhcppkt, DHCP_MESSAGE_TYPE, &msgtype,
1140 sizeof ( msgtype ) );
1141
1142 /* Identify server ID */
1143 dhcppkt_fetch ( dhcppkt, DHCP_SERVER_IDENTIFIER,
1144 &server_id, sizeof ( server_id ) );
1145
1146 /* Check for matching transaction ID */
1147 if ( dhcphdr->xid != dhcp_xid ( dhcp->netdev ) ) {
1148 DBGC ( dhcp, "DHCP %p %s from %s:%d has bad transaction "
1149 "ID\n", dhcp, dhcp_msgtype_name ( msgtype ),
1150 inet_ntoa ( peer->sin_addr ),
1151 ntohs ( peer->sin_port ) );
1152 rc = -EINVAL;
1153 goto err_xid;
1154 };
1155
1156 /* Handle packet based on current state */
1157 dhcp->state->rx ( dhcp, dhcppkt, peer, msgtype, server_id );
1158
1159 err_xid:
1160 dhcppkt_put ( dhcppkt );
1161 err_alloc_dhcppkt:
1162 err_no_src:
1163 free_iob ( iobuf );
1164 return rc;
1165 }
1166
1167 /** DHCP data transfer interface operations */
1168 static struct xfer_interface_operations dhcp_xfer_operations = {
1169 .close = ignore_xfer_close,
1170 .vredirect = xfer_vreopen,
1171 .window = unlimited_xfer_window,
1172 .alloc_iob = default_xfer_alloc_iob,
1173 .deliver_iob = dhcp_deliver_iob,
1174 .deliver_raw = xfer_deliver_as_iob,
1175 };
1176
1177 /**
1178 * Handle DHCP retry timer expiry
1179 *
1180 * @v timer DHCP retry timer
1181 * @v fail Failure indicator
1182 */
1183 static void dhcp_timer_expired ( struct retry_timer *timer, int fail ) {
1184 struct dhcp_session *dhcp =
1185 container_of ( timer, struct dhcp_session, timer );
1186
1187 /* If we have failed, terminate DHCP */
1188 if ( fail ) {
1189 dhcp_finished ( dhcp, -ETIMEDOUT );
1190 return;
1191 }
1192
1193 /* Handle timer expiry based on current state */
1194 dhcp->state->expired ( dhcp );
1195 }
1196
1197 /****************************************************************************
1198 *
1199 * Job control interface
1200 *
1201 */
1202
1203 /**
1204 * Handle kill() event received via job control interface
1205 *
1206 * @v job DHCP job control interface
1207 */
1208 static void dhcp_job_kill ( struct job_interface *job ) {
1209 struct dhcp_session *dhcp =
1210 container_of ( job, struct dhcp_session, job );
1211
1212 /* Terminate DHCP session */
1213 dhcp_finished ( dhcp, -ECANCELED );
1214 }
1215
1216 /** DHCP job control interface operations */
1217 static struct job_interface_operations dhcp_job_operations = {
1218 .done = ignore_job_done,
1219 .kill = dhcp_job_kill,
1220 .progress = ignore_job_progress,
1221 };
1222
1223 /****************************************************************************
1224 *
1225 * Instantiators
1226 *
1227 */
1228
1229 /**
1230 * DHCP peer address for socket opening
1231 *
1232 * This is a dummy address; the only useful portion is the socket
1233 * family (so that we get a UDP connection). The DHCP client will set
1234 * the IP address and source port explicitly on each transmission.
1235 */
1236 static struct sockaddr dhcp_peer = {
1237 .sa_family = AF_INET,
1238 };
1239
1240 /**
1241 * Start DHCP state machine on a network device
1242 *
1243 * @v job Job control interface
1244 * @v netdev Network device
1245 * @ret rc Return status code
1246 *
1247 * Starts DHCP on the specified network device. If successful, the
1248 * DHCPACK (and ProxyDHCPACK, if applicable) will be registered as
1249 * option sources.
1250 */
1251 int start_dhcp ( struct job_interface *job, struct net_device *netdev ) {
1252 struct dhcp_session *dhcp;
1253 int rc;
1254
1255 /* Allocate and initialise structure */
1256 dhcp = zalloc ( sizeof ( *dhcp ) );
1257 if ( ! dhcp )
1258 return -ENOMEM;
1259 dhcp->refcnt.free = dhcp_free;
1260 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
1261 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
1262 dhcp->netdev = netdev_get ( netdev );
1263 dhcp->local.sin_family = AF_INET;
1264 dhcp->local.sin_port = htons ( BOOTPC_PORT );
1265 dhcp->timer.expired = dhcp_timer_expired;
1266
1267 /* Instantiate child objects and attach to our interfaces */
1268 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM, &dhcp_peer,
1269 ( struct sockaddr * ) &dhcp->local ) ) != 0 )
1270 goto err;
1271
1272 /* Enter DHCPDISCOVER state */
1273 dhcp_set_state ( dhcp, &dhcp_state_discover );
1274
1275 /* Attach parent interface, mortalise self, and return */
1276 job_plug_plug ( &dhcp->job, job );
1277 ref_put ( &dhcp->refcnt );
1278 return 0;
1279
1280 err:
1281 dhcp_finished ( dhcp, rc );
1282 ref_put ( &dhcp->refcnt );
1283 return rc;
1284 }
1285
1286 /**
1287 * Retrieve list of PXE boot servers for a given server type
1288 *
1289 * @v dhcp DHCP session
1290 * @v raw DHCP PXE boot server list
1291 * @v raw_len Length of DHCP PXE boot server list
1292 * @v ip IP address list to fill in
1293 *
1294 * The caller must ensure that the IP address list has sufficient
1295 * space.
1296 */
1297 static void pxebs_list ( struct dhcp_session *dhcp, void *raw,
1298 size_t raw_len, struct in_addr *ip ) {
1299 struct dhcp_pxe_boot_server *server = raw;
1300 size_t server_len;
1301 unsigned int i;
1302
1303 while ( raw_len ) {
1304 if ( raw_len < sizeof ( *server ) ) {
1305 DBGC ( dhcp, "DHCP %p malformed PXE server list\n",
1306 dhcp );
1307 break;
1308 }
1309 server_len = offsetof ( typeof ( *server ),
1310 ip[ server->num_ip ] );
1311 if ( raw_len < server_len ) {
1312 DBGC ( dhcp, "DHCP %p malformed PXE server list\n",
1313 dhcp );
1314 break;
1315 }
1316 if ( server->type == dhcp->pxe_type ) {
1317 for ( i = 0 ; i < server->num_ip ; i++ )
1318 *(ip++) = server->ip[i];
1319 }
1320 server = ( ( ( void * ) server ) + server_len );
1321 raw_len -= server_len;
1322 }
1323 }
1324
1325 /**
1326 * Start PXE Boot Server Discovery on a network device
1327 *
1328 * @v job Job control interface
1329 * @v netdev Network device
1330 * @v pxe_type PXE server type
1331 * @ret rc Return status code
1332 *
1333 * Starts PXE Boot Server Discovery on the specified network device.
1334 * If successful, the Boot Server ACK will be registered as an option
1335 * source.
1336 */
1337 int start_pxebs ( struct job_interface *job, struct net_device *netdev,
1338 unsigned int pxe_type ) {
1339 struct setting pxe_discovery_control_setting =
1340 { .tag = DHCP_PXE_DISCOVERY_CONTROL };
1341 struct setting pxe_boot_servers_setting =
1342 { .tag = DHCP_PXE_BOOT_SERVERS };
1343 struct setting pxe_boot_server_mcast_setting =
1344 { .tag = DHCP_PXE_BOOT_SERVER_MCAST };
1345 ssize_t pxebs_list_len;
1346 struct dhcp_session *dhcp;
1347 struct in_addr *ip;
1348 unsigned int pxe_discovery_control;
1349 int rc;
1350
1351 /* Get upper bound for PXE boot server IP address list */
1352 pxebs_list_len = fetch_setting_len ( NULL, &pxe_boot_servers_setting );
1353 if ( pxebs_list_len < 0 )
1354 pxebs_list_len = 0;
1355
1356 /* Allocate and initialise structure */
1357 dhcp = zalloc ( sizeof ( *dhcp ) + sizeof ( *ip ) /* mcast */ +
1358 sizeof ( *ip ) /* bcast */ + pxebs_list_len +
1359 sizeof ( *ip ) /* terminator */ );
1360 if ( ! dhcp )
1361 return -ENOMEM;
1362 dhcp->refcnt.free = dhcp_free;
1363 job_init ( &dhcp->job, &dhcp_job_operations, &dhcp->refcnt );
1364 xfer_init ( &dhcp->xfer, &dhcp_xfer_operations, &dhcp->refcnt );
1365 dhcp->netdev = netdev_get ( netdev );
1366 dhcp->local.sin_family = AF_INET;
1367 fetch_ipv4_setting ( netdev_settings ( netdev ), &ip_setting,
1368 &dhcp->local.sin_addr );
1369 dhcp->local.sin_port = htons ( BOOTPC_PORT );
1370 dhcp->pxe_type = htons ( pxe_type );
1371 dhcp->timer.expired = dhcp_timer_expired;
1372
1373 /* Construct PXE boot server IP address lists */
1374 pxe_discovery_control =
1375 fetch_uintz_setting ( NULL, &pxe_discovery_control_setting );
1376 ip = ( ( ( void * ) dhcp ) + sizeof ( *dhcp ) );
1377 dhcp->pxe_attempt = ip;
1378 if ( ! ( pxe_discovery_control & PXEBS_NO_MULTICAST ) ) {
1379 fetch_ipv4_setting ( NULL, &pxe_boot_server_mcast_setting, ip);
1380 if ( ip->s_addr )
1381 ip++;
1382 }
1383 if ( ! ( pxe_discovery_control & PXEBS_NO_BROADCAST ) )
1384 (ip++)->s_addr = INADDR_BROADCAST;
1385 if ( pxe_discovery_control & PXEBS_NO_UNKNOWN_SERVERS )
1386 dhcp->pxe_accept = ip;
1387 if ( pxebs_list_len ) {
1388 uint8_t buf[pxebs_list_len];
1389
1390 fetch_setting ( NULL, &pxe_boot_servers_setting,
1391 buf, sizeof ( buf ) );
1392 pxebs_list ( dhcp, buf, sizeof ( buf ), ip );
1393 }
1394 if ( ! dhcp->pxe_attempt->s_addr ) {
1395 DBGC ( dhcp, "DHCP %p has no PXE boot servers for type %04x\n",
1396 dhcp, pxe_type );
1397 rc = -EINVAL;
1398 goto err;
1399 }
1400
1401 /* Dump out PXE server lists */
1402 DBGC ( dhcp, "DHCP %p attempting", dhcp );
1403 for ( ip = dhcp->pxe_attempt ; ip->s_addr ; ip++ )
1404 DBGC ( dhcp, " %s", inet_ntoa ( *ip ) );
1405 DBGC ( dhcp, "\n" );
1406 if ( dhcp->pxe_accept ) {
1407 DBGC ( dhcp, "DHCP %p accepting", dhcp );
1408 for ( ip = dhcp->pxe_accept ; ip->s_addr ; ip++ )
1409 DBGC ( dhcp, " %s", inet_ntoa ( *ip ) );
1410 DBGC ( dhcp, "\n" );
1411 }
1412
1413 /* Instantiate child objects and attach to our interfaces */
1414 if ( ( rc = xfer_open_socket ( &dhcp->xfer, SOCK_DGRAM, &dhcp_peer,
1415 ( struct sockaddr * ) &dhcp->local ) ) != 0 )
1416 goto err;
1417
1418 /* Enter PXEBS state */
1419 dhcp_set_state ( dhcp, &dhcp_state_pxebs );
1420
1421 /* Attach parent interface, mortalise self, and return */
1422 job_plug_plug ( &dhcp->job, job );
1423 ref_put ( &dhcp->refcnt );
1424 return 0;
1425
1426 err:
1427 dhcp_finished ( dhcp, rc );
1428 ref_put ( &dhcp->refcnt );
1429 return rc;
1430 }