123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682 |
-
-
- #include <string.h>
- #include <errno.h>
- #include <assert.h>
- #include <byteswap.h>
- #include <gpxe/if_ether.h>
- #include <gpxe/netdevice.h>
- #include <gpxe/udp.h>
- #include <gpxe/dhcp.h>
-
-
-
-
- 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_MAX_MESSAGE_SIZE, DHCP_WORD ( ETH_MAX_MTU ),
- DHCP_VENDOR_CLASS_ID,
- DHCP_STRING ( 'E', 't', 'h', 'e', 'r', 'b', 'o', 'o', 't' ),
- DHCP_PARAMETER_REQUEST_LIST,
- DHCP_OPTION ( DHCP_SUBNET_MASK, DHCP_ROUTERS, DHCP_HOST_NAME ),
- DHCP_END
- };
-
-
- static inline const char * dhcp_msgtype_name ( unsigned int msgtype ) {
- switch ( msgtype ) {
- case 0: 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>";
- }
- }
-
-
- static struct dhcp_option_block dhcp_request_options = {
- .data = dhcp_request_options_data,
- .max_len = sizeof ( dhcp_request_options_data ),
- .len = sizeof ( dhcp_request_options_data ),
- };
-
-
- static int set_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
- unsigned int tag, const void *data,
- size_t len ) {
- struct dhcphdr *dhcphdr = dhcppkt->dhcphdr;
- struct dhcp_option_block *options = dhcppkt->options;
- struct dhcp_option *option = NULL;
-
-
- switch ( tag ) {
- case DHCP_OPTION_OVERLOAD:
-
- return 0;
- case DHCP_EB_YIADDR:
- memcpy ( &dhcphdr->yiaddr, data, sizeof ( dhcphdr->yiaddr ) );
- return 0;
- case DHCP_EB_SIADDR:
- memcpy ( &dhcphdr->siaddr, data, sizeof ( dhcphdr->siaddr ) );
- return 0;
- case DHCP_MESSAGE_TYPE:
- case DHCP_REQUESTED_ADDRESS:
-
-
- options = &dhcppkt->options[OPTS_MAIN];
- break;
- default:
-
- break;
- }
-
-
- for ( ; options < &dhcppkt->options[NUM_OPT_BLOCKS] ; options++ ) {
- option = set_dhcp_option ( options, tag, data, len );
- if ( option )
- break;
- }
-
-
- dhcppkt->len = ( offsetof ( typeof ( *dhcppkt->dhcphdr ), options )
- + dhcppkt->options[OPTS_MAIN].len );
-
- return ( option ? 0 : -ENOSPC );
- }
-
-
- static int copy_dhcp_packet_option ( struct dhcp_packet *dhcppkt,
- struct dhcp_option_block *options,
- unsigned int tag, unsigned int new_tag ) {
- struct dhcp_option *option;
- int rc;
-
- option = find_dhcp_option ( options, tag );
- if ( option ) {
- if ( ( rc = set_dhcp_packet_option ( dhcppkt, new_tag,
- &option->data,
- option->len ) ) != 0 )
- return rc;
- }
- return 0;
- }
-
-
- static int copy_dhcp_packet_encap_options ( struct dhcp_packet *dhcppkt,
- struct dhcp_option_block *options,
- unsigned int encapsulator ) {
- unsigned int subtag;
- unsigned int tag;
- int rc;
-
- for ( subtag = DHCP_MIN_OPTION; subtag <= DHCP_MAX_OPTION; subtag++ ) {
- tag = DHCP_ENCAP_OPT ( encapsulator, subtag );
- switch ( tag ) {
- case DHCP_EB_ENCAP:
- case DHCP_VENDOR_ENCAP:
-
- if ( ( rc = copy_dhcp_packet_encap_options ( dhcppkt,
- options,
- tag)) !=0)
- return rc;
- break;
- default:
-
- if ( ( rc = copy_dhcp_packet_option ( dhcppkt, options,
- tag, tag ) ) !=0)
- return rc;
- break;
- };
- }
-
- return 0;
- }
-
-
- static int copy_dhcp_packet_options ( struct dhcp_packet *dhcppkt,
- struct dhcp_option_block *options ) {
- return copy_dhcp_packet_encap_options ( dhcppkt, options, 0 );
- }
-
-
- static int create_dhcp_packet ( struct dhcp_session *dhcp, uint8_t msgtype,
- void *data, size_t max_len,
- struct dhcp_packet *dhcppkt ) {
- struct dhcphdr *dhcphdr = data;
- static const uint8_t overloading = ( DHCP_OPTION_OVERLOAD_FILE |
- DHCP_OPTION_OVERLOAD_SNAME );
- int rc;
-
-
- if ( max_len < sizeof ( *dhcphdr ) )
- return -ENOSPC;
-
-
- memset ( dhcphdr, 0, max_len );
- dhcphdr->xid = dhcp->xid;
- dhcphdr->magic = htonl ( DHCP_MAGIC_COOKIE );
- dhcphdr->htype = ntohs ( dhcp->netdev->ll_protocol->ll_proto );
- dhcphdr->hlen = dhcp->netdev->ll_protocol->ll_addr_len;
- memcpy ( dhcphdr->chaddr, dhcp->netdev->ll_addr, dhcphdr->hlen );
- dhcphdr->op = dhcp_op[msgtype];
-
-
- dhcppkt->dhcphdr = dhcphdr;
- dhcppkt->max_len = max_len;
- init_dhcp_options ( &dhcppkt->options[OPTS_MAIN], dhcphdr->options,
- ( max_len -
- offsetof ( typeof ( *dhcphdr ), options ) ) );
- init_dhcp_options ( &dhcppkt->options[OPTS_FILE], dhcphdr->file,
- sizeof ( dhcphdr->file ) );
- init_dhcp_options ( &dhcppkt->options[OPTS_SNAME], dhcphdr->sname,
- sizeof ( dhcphdr->sname ) );
-
-
- if ( set_dhcp_option ( &dhcppkt->options[OPTS_MAIN],
- DHCP_OPTION_OVERLOAD, &overloading,
- sizeof ( overloading ) ) == NULL )
- return -ENOSPC;
-
-
- if ( ( rc = set_dhcp_packet_option ( dhcppkt, DHCP_MESSAGE_TYPE,
- &msgtype,
- sizeof ( msgtype ) ) ) != 0 )
- return rc;
-
- return 0;
- }
-
-
- static size_t dhcp_field_len ( const void *data, size_t max_len ) {
- struct dhcp_option_block options;
- struct dhcp_option *end;
-
- options.data = ( ( void * ) data );
- options.len = max_len;
- end = find_dhcp_option ( &options, DHCP_END );
- return ( end ? ( ( ( void * ) end ) - data ) : 0 );
- }
-
-
- static void merge_dhcp_field ( struct dhcp_option_block *options,
- const void *data, size_t max_len,
- unsigned int tag ) {
- size_t len;
- void *dest;
- struct dhcp_option *end;
-
- if ( tag ) {
- set_dhcp_option ( options, tag, data, strlen ( data ) );
- } else {
- len = dhcp_field_len ( data, max_len );
- dest = ( options->data + options->len - 1 );
- memcpy ( dest, data, len );
- options->len += len;
- end = ( dest + len );
- end->tag = DHCP_END;
- }
- }
-
-
- static struct dhcp_option_block * dhcp_parse ( struct dhcphdr *dhcphdr,
- size_t len ) {
- struct dhcp_option_block *options;
- size_t options_len;
- unsigned int overloading;
-
-
- if ( len < sizeof ( *dhcphdr ) )
- return NULL;
-
-
-
- options_len = ( ( len - offsetof ( typeof ( *dhcphdr ), options ) ) - 1
- + ( sizeof ( dhcphdr->file ) + 1 )
- + ( sizeof ( dhcphdr->sname ) + 1 )
- + 15
- + 1 );
-
-
- options = alloc_dhcp_options ( options_len );
- if ( ! options ) {
- DBG ( "DHCP could not allocate %d-byte option block\n",
- options_len );
- return NULL;
- }
-
-
- if ( dhcphdr->magic == htonl ( DHCP_MAGIC_COOKIE ) ) {
- merge_dhcp_field ( options, dhcphdr->options,
- ( len -
- offsetof ( typeof (*dhcphdr), options ) ),
- 0 );
- }
-
-
- overloading = find_dhcp_num_option ( options, DHCP_OPTION_OVERLOAD );
-
-
- merge_dhcp_field ( options, dhcphdr->file, sizeof ( dhcphdr->file ),
- ( ( overloading & DHCP_OPTION_OVERLOAD_FILE ) ?
- DHCP_BOOTFILE_NAME : 0 ) );
- merge_dhcp_field ( options, dhcphdr->sname, sizeof ( dhcphdr->sname ),
- ( ( overloading & DHCP_OPTION_OVERLOAD_SNAME ) ?
- DHCP_TFTP_SERVER_NAME : 0 ) );
-
-
- if ( dhcphdr->yiaddr.s_addr ) {
- set_dhcp_option ( options, DHCP_EB_YIADDR,
- &dhcphdr->yiaddr, sizeof (dhcphdr->yiaddr) );
- }
- if ( dhcphdr->siaddr.s_addr ) {
- set_dhcp_option ( options, DHCP_EB_SIADDR,
- &dhcphdr->siaddr, sizeof (dhcphdr->siaddr) );
- }
-
- assert ( options->len <= options->max_len );
-
- return options;
- }
-
-
-
- static inline struct dhcp_session *
- udp_to_dhcp ( struct udp_connection *conn ) {
- return container_of ( conn, struct dhcp_session, udp );
- }
-
-
- static void dhcp_done ( struct dhcp_session *dhcp, int rc ) {
-
- async_done ( &dhcp->aop, rc );
- }
-
-
- static struct sockaddr sa_dhcp_server = {
- .sa_family = AF_INET,
- .sin = {
- .sin_addr.s_addr = INADDR_BROADCAST,
- .sin_port = htons ( BOOTPS_PORT ),
- },
- };
-
-
- static void dhcp_senddata ( struct udp_connection *conn,
- void *buf, size_t len ) {
- struct dhcp_session *dhcp = udp_to_dhcp ( conn );
- struct dhcp_packet dhcppkt;
- int rc;
-
- DBG ( "Transmitting %s\n", dhcp_msgtype_name ( dhcp->state ) );
-
- assert ( ( dhcp->state == DHCPDISCOVER ) ||
- ( dhcp->state == DHCPREQUEST ) );
-
-
- if ( ( rc = create_dhcp_packet ( dhcp, dhcp->state, buf, len,
- &dhcppkt ) ) != 0 ) {
- DBG ( "Could not create DHCP packet\n" );
- return;
- }
-
-
- if ( ( rc = copy_dhcp_packet_options ( &dhcppkt,
- &dhcp_request_options ) ) != 0){
- DBG ( "Could not set common DHCP options\n" );
- return;
- }
-
-
- if ( dhcp->options ) {
- if ( ( rc = copy_dhcp_packet_option ( &dhcppkt, dhcp->options,
- DHCP_SERVER_IDENTIFIER,
- DHCP_SERVER_IDENTIFIER ) ) != 0 ) {
- DBG ( "Could not set server identifier option\n" );
- return;
- }
- if ( ( rc = copy_dhcp_packet_option ( &dhcppkt, dhcp->options,
- DHCP_EB_YIADDR,
- DHCP_REQUESTED_ADDRESS ) ) != 0 ) {
- DBG ( "Could not set requested address option\n" );
- return;
- }
- }
-
-
- if ( ( rc = udp_sendto ( conn, &sa_dhcp_server,
- dhcppkt.dhcphdr, dhcppkt.len ) ) != 0 ) {
- DBG ( "Could not transmit UDP packet\n" );
- return;
- }
- }
-
-
- static void dhcp_send_request ( struct dhcp_session *dhcp ) {
- start_timer ( &dhcp->timer );
- udp_senddata ( &dhcp->udp );
- }
-
-
- 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_done ( dhcp, -ETIMEDOUT );
- } else {
- dhcp_send_request ( dhcp );
- }
- }
-
-
- static void dhcp_newdata ( struct udp_connection *conn,
- void *data, size_t len ) {
- struct dhcp_session *dhcp = udp_to_dhcp ( conn );
- struct dhcphdr *dhcphdr = data;
- struct dhcp_option_block *options;
- unsigned int msgtype;
-
-
- if ( dhcphdr->xid != dhcp->xid ) {
- DBG ( "DHCP wrong transaction ID (wanted %08lx, got %08lx)\n",
- ntohl ( dhcphdr->xid ), ntohl ( dhcp->xid ) );
- return;
- };
-
-
- options = dhcp_parse ( dhcphdr, len );
- if ( ! options ) {
- DBG ( "Could not parse DHCP packet\n" );
- return;
- }
-
-
- msgtype = find_dhcp_num_option ( options, DHCP_MESSAGE_TYPE );
- DBG ( "Received %s\n", dhcp_msgtype_name ( msgtype ) );
-
-
- switch ( dhcp->state ) {
- case DHCPDISCOVER:
- if ( msgtype != DHCPOFFER )
- goto out_discard;
- dhcp->state = DHCPREQUEST;
- break;
- case DHCPREQUEST:
- if ( msgtype != DHCPACK )
- goto out_discard;
- dhcp->state = DHCPACK;
- break;
- default:
- assert ( 0 );
- goto out_discard;
- }
-
-
- stop_timer ( &dhcp->timer );
- if ( dhcp->options )
- free_dhcp_options ( dhcp->options );
- dhcp->options = options;
-
-
- if ( dhcp->state < DHCPACK ) {
- dhcp_send_request ( dhcp );
- } else {
- dhcp_done ( dhcp, 0 );
- }
- return;
-
- out_discard:
- free_dhcp_options ( options );
- }
-
-
- static struct udp_operations dhcp_udp_operations = {
- .senddata = dhcp_senddata,
- .newdata = dhcp_newdata,
- };
-
-
- struct async_operation * start_dhcp ( struct dhcp_session *dhcp ) {
- int rc;
-
-
- dhcp->udp.udp_op = &dhcp_udp_operations;
- dhcp->timer.expired = dhcp_timer_expired;
- dhcp->state = DHCPDISCOVER;
-
- memcpy ( &dhcp->xid, ( dhcp->netdev->ll_addr
- + dhcp->netdev->ll_protocol->ll_addr_len
- - sizeof ( dhcp->xid ) ), sizeof ( dhcp->xid ));
-
-
- if ( ( rc = udp_open ( &dhcp->udp, htons ( BOOTPC_PORT ) ) ) != 0 ) {
- async_done ( &dhcp->aop, rc );
- goto out;
- }
-
-
- dhcp_send_request ( dhcp );
-
- out:
- return &dhcp->aop;
- }
|