|
@@ -233,6 +233,10 @@ static int create_dhcp_packet ( struct dhcp_session *dhcp, uint8_t msgtype,
|
233
|
233
|
DHCP_OPTION_OVERLOAD_SNAME );
|
234
|
234
|
int rc;
|
235
|
235
|
|
|
236
|
+ /* Sanity check */
|
|
237
|
+ if ( max_len < sizeof ( *dhcphdr ) )
|
|
238
|
+ return -ENOSPC;
|
|
239
|
+
|
236
|
240
|
/* Initialise DHCP packet content */
|
237
|
241
|
memset ( dhcphdr, 0, max_len );
|
238
|
242
|
dhcphdr->xid = dhcp->xid;
|
|
@@ -428,6 +432,15 @@ udp_to_dhcp ( struct udp_connection *conn ) {
|
428
|
432
|
return container_of ( conn, struct dhcp_session, udp );
|
429
|
433
|
}
|
430
|
434
|
|
|
435
|
+/** Address for transmitting DHCP requests */
|
|
436
|
+static struct sockaddr sa_dhcp_server = {
|
|
437
|
+ .sa_family = AF_INET,
|
|
438
|
+ .sin = {
|
|
439
|
+ .sin_addr.s_addr = INADDR_BROADCAST,
|
|
440
|
+ .sin_port = htons ( BOOTPS_PORT ),
|
|
441
|
+ },
|
|
442
|
+};
|
|
443
|
+
|
431
|
444
|
/**
|
432
|
445
|
* Transmit DHCP request
|
433
|
446
|
*
|
|
@@ -461,7 +474,11 @@ static void dhcp_senddata ( struct udp_connection *conn,
|
461
|
474
|
}
|
462
|
475
|
|
463
|
476
|
/* Transmit the packet */
|
464
|
|
- udp_send ( conn, dhcppkt.dhcphdr, dhcppkt.len );
|
|
477
|
+ if ( ( rc = udp_sendto ( conn, &sa_dhcp_server,
|
|
478
|
+ dhcppkt.dhcphdr, dhcppkt.len ) ) != 0 ) {
|
|
479
|
+ DBG ( "Could not transmit UDP packet\n" );
|
|
480
|
+ return;
|
|
481
|
+ }
|
465
|
482
|
}
|
466
|
483
|
|
467
|
484
|
/**
|
|
@@ -513,6 +530,8 @@ static struct udp_operations dhcp_udp_operations = {
|
513
|
530
|
* @ret aop Asynchronous operation
|
514
|
531
|
*/
|
515
|
532
|
struct async_operation * start_dhcp ( struct dhcp_session *dhcp ) {
|
|
533
|
+ int rc;
|
|
534
|
+
|
516
|
535
|
dhcp->udp.udp_op = &dhcp_udp_operations;
|
517
|
536
|
dhcp->state = DHCPDISCOVER;
|
518
|
537
|
/* Use least significant 32 bits of link-layer address as XID */
|
|
@@ -520,8 +539,15 @@ struct async_operation * start_dhcp ( struct dhcp_session *dhcp ) {
|
520
|
539
|
+ dhcp->netdev->ll_protocol->ll_addr_len
|
521
|
540
|
- sizeof ( dhcp->xid ) ), sizeof ( dhcp->xid ));
|
522
|
541
|
|
|
542
|
+ /* Bind to local port */
|
|
543
|
+ if ( ( rc = udp_open ( &dhcp->udp, BOOTPC_PORT ) ) != 0 ) {
|
|
544
|
+ async_done ( &dhcp->aop, rc );
|
|
545
|
+ goto out;
|
|
546
|
+ }
|
|
547
|
+
|
523
|
548
|
/* Proof of concept: just send a single DHCPDISCOVER */
|
524
|
549
|
udp_senddata ( &dhcp->udp );
|
525
|
550
|
|
|
551
|
+ out:
|
526
|
552
|
return &dhcp->aop;
|
527
|
553
|
}
|