static err_t dhcp_release(struct dhcp_state *state) { err_t result; u16_t msecs; DEBUGF(DHCP_DEBUG, ("dhcp_release()\n")); // and idle DHCP client dhcp_set_state(state, DHCP_OFF); // create and initialize the DHCP message header result = dhcp_create_request(state); if (result == ERR_OK) { dhcp_option(state, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN); dhcp_option_byte(state, DHCP_RELEASE); dhcp_option_trailer(state); pbuf_realloc(state->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + state->options_out_len); udp_bind(state->pcb, IP_ADDR_ANY, DHCP_CLIENT_PORT); udp_connect(state->pcb, &state->server_ip_addr, DHCP_SERVER_PORT); udp_send(state->pcb, state->p_out); dhcp_delete_request(state); } state->tries++; msecs = state->tries < 10 ? state->tries * 1000 : 10 * 1000; state->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS; DEBUGF(DHCP_DEBUG, ("dhcp_release(): request timeout %u msecs\n", msecs)); // remove IP address from interface netif_set_ipaddr(state->netif, IP_ADDR_ANY); netif_set_gw(state->netif, IP_ADDR_ANY); netif_set_netmask(state->netif, IP_ADDR_ANY); return result; }
// decline a static err_t dhcp_decline(struct dhcp_state *state) { err_t result = ERR_OK; u16_t msecs; DEBUGF(DHCP_DEBUG, ("dhcp_decline()\n")); dhcp_set_state(state, DHCP_BACKING_OFF); // create and initialize the DHCP message header result = dhcp_create_request(state); if (result == ERR_OK) { dhcp_option(state, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN); dhcp_option_byte(state, DHCP_DECLINE); dhcp_option(state, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN); dhcp_option_short(state, 576); dhcp_option_trailer(state); // resize pbuf to reflect true size of options pbuf_realloc(state->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + state->options_out_len); udp_bind(state->pcb, IP_ADDR_ANY, DHCP_CLIENT_PORT); udp_connect(state->pcb, &state->server_ip_addr, DHCP_SERVER_PORT); udp_send(state->pcb, state->p_out); dhcp_delete_request(state); } state->tries++; msecs = 10*1000; state->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS; DEBUGF(DHCP_DEBUG, ("dhcp_decline(): request timeout %u msecs\n", msecs)); return result; }
void dhcp_inform(struct netif *netif) { struct dhcp_state *state = NULL; err_t result = ERR_OK; state = mem_malloc(sizeof(struct dhcp_state)); if (state == NULL) { DEBUGF(DHCP_DEBUG, ("dhcp_inform(): could not allocate dhcp_state\n")); return; } memset(state, 0, sizeof(struct dhcp_state)); DEBUGF(DHCP_DEBUG, ("dhcp_inform(): allocated dhcp_state\n")); state->pcb = udp_new(); if (state->pcb == NULL) { DEBUGF(DHCP_DEBUG, ("dhcp_inform(): could not obtain pcb\n")); mem_free((void *)state); return; } DEBUGF(DHCP_DEBUG, ("dhcp_inform(): created new udp pcb\n")); state->netif = netif; // we are last in list state->next = NULL; // create and initialize the DHCP message header result = dhcp_create_request(state); if (result == ERR_OK) { dhcp_option(state, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN); dhcp_option_byte(state, DHCP_INFORM); dhcp_option(state, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN); dhcp_option_short(state, 576); dhcp_option_trailer(state); pbuf_realloc(state->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + state->options_out_len); udp_bind(state->pcb, IP_ADDR_ANY, DHCP_CLIENT_PORT); udp_connect(state->pcb, IP_ADDR_BROADCAST, DHCP_SERVER_PORT); udp_send(state->pcb, state->p_out); udp_connect(state->pcb, IP_ADDR_ANY, DHCP_SERVER_PORT); dhcp_delete_request(state); } if (state != NULL) { if (state->pcb != NULL) udp_remove(state->pcb); state->pcb = NULL; mem_free((void *)state); } }
/** * Start the DHCP process, discover a server * */ static err_t dhcp_discover(struct dhcp_state *state) { err_t result = ERR_OK; u16_t msecs; DEBUGF(DHCP_DEBUG, ("dhcp_discover(%p)\n", state)); ip_addr_set(&state->offered_ip_addr, IP_ADDR_ANY); // create and initialize the DHCP message header DEBUGF(DHCP_DEBUG, ("set ip addr, creating request()\n")); result = dhcp_create_request(state); DEBUGF(DHCP_DEBUG, ("created request\n")); if (result == ERR_OK) { dhcp_option(state, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN); dhcp_option_byte(state, DHCP_DISCOVER); dhcp_option(state, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN); dhcp_option_short(state, 576); dhcp_option(state, DHCP_OPTION_PARAMETER_REQUEST_LIST, 3); dhcp_option_byte(state, DHCP_OPTION_SUBNET_MASK); dhcp_option_byte(state, DHCP_OPTION_ROUTER); dhcp_option_byte(state, DHCP_OPTION_BROADCAST); dhcp_option_trailer(state); pbuf_realloc(state->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + state->options_out_len); DEBUGF(DHCP_DEBUG, ("about to do udp recv\n")); udp_recv(state->pcb, dhcp_recv, state); DEBUGF(DHCP_DEBUG, ("about to do udp bind\n")); udp_bind(state->pcb, IP_ADDR_ANY, DHCP_CLIENT_PORT); DEBUGF(DHCP_DEBUG, ("about to do udp connect\n")); udp_connect(state->pcb, IP_ADDR_BROADCAST, DHCP_SERVER_PORT); DEBUGF(DHCP_DEBUG, ("about to do udp send\n")); udp_send(state->pcb, state->p_out); udp_bind(state->pcb, IP_ADDR_ANY, DHCP_CLIENT_PORT); udp_connect(state->pcb, IP_ADDR_ANY, DHCP_SERVER_PORT); dhcp_delete_request(state); } state->tries++; msecs = state->tries < 4 ? (state->tries + 1) * 1000 : 10 * 1000; state->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS; DEBUGF(DHCP_DEBUG, ("dhcp_discover(): request timeout %u msecs\n", msecs)); dhcp_set_state(state, DHCP_SELECTING); return result; }
/** * Select a DHCP server offer out of all offers. * * Simply select the first offer received, ignore others */ static err_t dhcp_select(struct dhcp_state *state) { err_t result; u32_t msecs; DEBUGF(DHCP_DEBUG, ("dhcp_select()\n")); // create and initialize the DHCP message header result = dhcp_create_request(state); if (result == ERR_OK) { dhcp_option(state, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN); dhcp_option_byte(state, DHCP_REQUEST); dhcp_option(state, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN); dhcp_option_short(state, 576); /* MUST request the offered IP address */ dhcp_option(state, DHCP_OPTION_REQUESTED_IP, 4); dhcp_option_long(state, ntohl(state->offered_ip_addr.addr)); dhcp_option(state, DHCP_OPTION_SERVER_ID, 4); dhcp_option_long(state, ntohl(state->server_ip_addr.addr)); dhcp_option(state, DHCP_OPTION_PARAMETER_REQUEST_LIST, 3); dhcp_option_byte(state, DHCP_OPTION_SUBNET_MASK); dhcp_option_byte(state, DHCP_OPTION_ROUTER); dhcp_option_byte(state, DHCP_OPTION_BROADCAST); dhcp_option_trailer(state); pbuf_realloc(state->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + state->options_out_len); udp_bind(state->pcb, IP_ADDR_ANY, DHCP_CLIENT_PORT); udp_connect(state->pcb, IP_ADDR_BROADCAST, DHCP_SERVER_PORT); udp_send(state->pcb, state->p_out); // reconnect to any (or to server here?!) udp_connect(state->pcb, IP_ADDR_ANY, DHCP_SERVER_PORT); dhcp_delete_request(state); } state->tries++; msecs = state->tries < 4 ? state->tries * 1000 : 4 * 1000; state->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS; DEBUGF(DHCP_DEBUG, ("dhcp_select(): request timeout %u msecs\n", msecs)); dhcp_set_state(state, DHCP_REQUESTING); return result; }
err_t dhcp_renew(struct dhcp_state *state) { err_t result; u16_t msecs; DEBUGF(DHCP_DEBUG, ("dhcp_renew()\n")); dhcp_set_state(state, DHCP_RENEWING); // create and initialize the DHCP message header result = dhcp_create_request(state); if (result == ERR_OK) { dhcp_option(state, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN); dhcp_option_byte(state, DHCP_REQUEST); dhcp_option(state, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN); dhcp_option_short(state, 576); #if 0 dhcp_option(state, DHCP_OPTION_REQUESTED_IP, 4); dhcp_option_long(state, ntohl(state->offered_ip_addr.addr)); #endif #if 0 dhcp_option(state, DHCP_OPTION_SERVER_ID, 4); dhcp_option_long(state, ntohl(state->server_ip_addr.addr)); #endif dhcp_option_trailer(state); pbuf_realloc(state->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + state->options_out_len); udp_bind(state->pcb, IP_ADDR_ANY, DHCP_CLIENT_PORT); udp_connect(state->pcb, &state->server_ip_addr, DHCP_SERVER_PORT); udp_send(state->pcb, state->p_out); dhcp_delete_request(state); } state->tries++; // back-off on retries, but to a maximum of 20 seconds msecs = state->tries < 10 ? state->tries * 2000 : 20 * 1000; state->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS; DEBUGF(DHCP_DEBUG, ("dhcp_renew(): request timeout %u msecs\n", msecs)); return result; }
/** * Copy settings to DHCP packet * * @v dest Destination DHCP packet * @v source Source settings block * @v encapsulator Encapsulating setting tag number, or zero * @ret rc Return status code */ static int copy_encap_settings ( struct dhcp_packet *dest, struct settings *source, unsigned int encapsulator ) { struct setting setting = { .name = "" }; unsigned int subtag; unsigned int tag; int len; int check_len; 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: /* Process encapsulated settings */ if ( ( rc = copy_encap_settings ( dest, source, tag ) ) != 0 ) return rc; break; default: /* Copy setting, if present */ setting.tag = tag; len = fetch_setting_len ( source, &setting ); if ( len < 0 ) break; { char buf[len]; check_len = fetch_setting ( source, &setting, buf, sizeof (buf)); assert ( check_len == len ); if ( ( rc = dhcppkt_store ( dest, tag, buf, sizeof(buf) )) !=0) return rc; } break; } } return 0; } /** * Copy settings to DHCP packet * * @v dest Destination DHCP packet * @v source Source settings block * @ret rc Return status code */ static int copy_settings ( struct dhcp_packet *dest, struct settings *source ) { return copy_encap_settings ( dest, source, 0 ); } /** * Create fake DHCPDISCOVER packet * * @v netdev Network device * @v data Buffer for DHCP packet * @v max_len Size of DHCP packet buffer * @ret rc Return status code * * Used by external code. */ int create_fakedhcpdiscover ( struct net_device *netdev, void *data, size_t max_len ) { struct dhcp_packet dhcppkt; struct in_addr ciaddr = { 0 }; int rc; if ( ( rc = dhcp_create_request ( &dhcppkt, netdev, DHCPDISCOVER, ciaddr, data, max_len ) ) != 0 ) { DBG ( "Could not create DHCPDISCOVER: %s\n", strerror ( rc ) ); return rc; } return 0; } /** * Create fake DHCPACK packet * * @v netdev Network device * @v data Buffer for DHCP packet * @v max_len Size of DHCP packet buffer * @ret rc Return status code * * Used by external code. */ int create_fakedhcpack ( struct net_device *netdev, void *data, size_t max_len ) { struct dhcp_packet dhcppkt; int rc; /* Create base DHCPACK packet */ if ( ( rc = dhcp_create_packet ( &dhcppkt, netdev, DHCPACK, NULL, 0, data, max_len ) ) != 0 ) { DBG ( "Could not create DHCPACK: %s\n", strerror ( rc ) ); return rc; } /* Merge in globally-scoped settings, then netdev-specific * settings. Do it in this order so that netdev-specific * settings take precedence regardless of stated priorities. */ if ( ( rc = copy_settings ( &dhcppkt, NULL ) ) != 0 ) { DBG ( "Could not set DHCPACK global settings: %s\n", strerror ( rc ) ); return rc; } if ( ( rc = copy_settings ( &dhcppkt, netdev_settings ( netdev ) ) ) != 0 ) { DBG ( "Could not set DHCPACK netdev settings: %s\n", strerror ( rc ) ); return rc; } return 0; } /** * Create fake PXE Boot Server ACK packet * * @v netdev Network device * @v data Buffer for DHCP packet * @v max_len Size of DHCP packet buffer * @ret rc Return status code * * Used by external code. */ int create_fakepxebsack ( struct net_device *netdev, void *data, size_t max_len ) { struct dhcp_packet dhcppkt; struct settings *proxy_settings; struct settings *pxebs_settings; int rc; /* Identify available settings */ proxy_settings = find_settings ( PROXYDHCP_SETTINGS_NAME ); pxebs_settings = find_settings ( PXEBS_SETTINGS_NAME ); if ( ( ! proxy_settings ) && ( ! pxebs_settings ) ) { /* No PXE boot server; return the regular DHCPACK */ return create_fakedhcpack ( netdev, data, max_len ); } /* Create base DHCPACK packet */ if ( ( rc = dhcp_create_packet ( &dhcppkt, netdev, DHCPACK, NULL, 0, data, max_len ) ) != 0 ) { DBG ( "Could not create PXE BS ACK: %s\n", strerror ( rc ) ); return rc; } /* Merge in ProxyDHCP options */ if ( proxy_settings && ( ( rc = copy_settings ( &dhcppkt, proxy_settings ) ) != 0 ) ) { DBG ( "Could not copy ProxyDHCP settings: %s\n", strerror ( rc ) ); return rc; } /* Merge in BootServerDHCP options, if present */ if ( pxebs_settings && ( ( rc = copy_settings ( &dhcppkt, pxebs_settings ) ) != 0 ) ) { DBG ( "Could not copy PXE BS settings: %s\n", strerror ( rc ) ); return rc; } return 0; }