📄 dhcpcboot.c
字号:
/* dhcpcBoot.c - DHCP client finite state machine definition (boot time) *//* Copyright 1984 - 2002 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------01m,25apr02,wap transmit DHCPDECLINEs as broadcasts, not unicasts (SPR #75119)01l,23apr02,wap use dhcpTime() instead of time() (SPR #68900) and initialize dhcpcBootStates correctly (SPR #75042)01k,12oct01,rae merge from truestack ver 01q, base 01g SPRs 65264, 70116, 27426, 6973101j,26oct00,spm fixed byte order of transaction identifier; removed reset code01i,26oct00,spm fixed modification history after tor3_x merge01h,23oct00,niq merged from version 01j of tor3_x branch (base version 01f)01g,04dec97,spm added code review modifications01f,06oct97,spm removed reference to deleted endDriver global; replaced with support for dynamic driver type detection01e,02sep97,spm removed name conflicts with runtime DHCP client (SPR #9241)01d,26aug97,spm major overhaul: included version 01h of dhcpcState1.c and version 01i of dhcpc_subr.c to retain single-lease library for boot time use01c,06aug97,spm removed parameters linked list to reduce memory required01b,07apr97,spm rewrote documentation01a,29jan97,spm created by modifying dhcpc.c module*//*DESCRIPTIONThis library contains the boot-time DHCP client implementation. The librarycontains routines to obtain a set of boot parameters using DHCP. Because the bootstrap loader handles all the system configuration, the boot-time client will not apply any configuration parameters received to the underlying networkinterface.INTERNAL This module contains a modified version of the DHCP client finite statemachine which eliminates all states from BOUND onwards. Provided that the obtained lease exceeds the download time for the runtime image, these routinesare not needed by the ROM-resident bootstrap loader. Their removal reduces the size of the boot ROM image so that the DHCP client can be used with targets like the MV147 which have limited ROM capacity. The code size is further reduced by eliminating the routines which change the network interface settingsand maintain the correct routing table entries. Although necessary to prevent access to an unconfigured interface during runtime, it is overkill at boot time, since the system will not start if an error occurs. Finally, the runtime DHCP client supports the retrieval and maintenance of multiple leases and allows users to renew or release the corresponding parameters. Those capabilities are not necessary at boot time.INCLUDE_FILES: dhcpcBootLib.h*//* * WIDE Project DHCP Implementation * Copyright (c) 1995 Akihiro Tominaga * Copyright (c) 1995 WIDE Project * All rights reserved. * * Permission to use, copy, modify and distribute this software and its * documentation is hereby granted, provided only with the following * conditions are satisfied: * * 1. Both the copyright notice and this permission notice appear in * all copies of the software, derivative works or modified versions, * and any portions thereof, and that both notices appear in * supporting documentation. * 2. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by WIDE Project and * its contributors. * 3. Neither the name of WIDE Project nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND WIDE * PROJECT DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES * WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. ALSO, THERE * IS NO WARRANTY IMPLIED OR OTHERWISE, NOR IS SUPPORT PROVIDED. * * Feedback of the results generated from any improvements or * extensions made to this software would be much appreciated. * Any such feedback should be sent to: * * Akihiro Tominaga * WIDE Project * Keio University, Endo 5322, Kanagawa, Japan * (E-mail: dhcp-dist@wide.ad.jp) * * WIDE project has the rights to redistribute these changes. *//* includes */#include "vxWorks.h"#include "vxLib.h" /* checksum() declaration. */#include "netLib.h"#include "inetLib.h"#include "taskLib.h"#include "sysLib.h"#include "ioLib.h"#include "sockLib.h"#include "wdLib.h"#include "rngLib.h"#include "logLib.h"#include "muxLib.h"#include "stdio.h"#include "stdlib.h"#include "sys/ioctl.h"#include "time.h"#include "netinet/in.h"#include "netinet/ip.h"#include "netinet/udp.h"#include "dhcpcBootLib.h"#include "dhcp/dhcpcCommonLib.h"#include "bpfDrv.h"/* defines */#define MAX_BOOT_STATES (INFORMING + 1) /* Function pointers for all states. */#define SLEEP_RANDOM(timer) ( (timer - 1) + (rand () % 2) )#define REQUEST_RETRANS 4 /* Number of retries, maximum of 5. (RFC 1541). */ /* Berkeley Packet Filter instructions for catching ARP messages. */LOCAL struct bpf_insn arpfilter[] = { BPF_STMT(BPF_LD+BPF_TYPE, 0), /* Save lltype in accumulator */ BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ETHERTYPE_ARP, 0, 9), /* ARP in frame? */ /* * The remaining statements use the (new) BPF_HLEN alias to avoid * any link-layer dependencies. */ BPF_STMT(BPF_LD+BPF_H+BPF_ABS+BPF_HLEN, 6), /* A <- ARP OP field */ BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ARPOP_REPLY, 0, 7), /* ARP reply ? */ BPF_STMT(BPF_LDX+BPF_HLEN, 0), /* X <- frame data offset */ BPF_STMT(BPF_LD+BPF_B+BPF_IND, 4), /* A <- hardware size */ BPF_STMT(BPF_ALU+BPF_ADD+BPF_X, 0), /* A <- sum of variable offsets */ BPF_STMT(BPF_MISC+BPF_TAX, 0), /* X <- sum of variable offsets */ BPF_STMT(BPF_LD+BPF_W+BPF_IND, 8), /* A <- sender IP address */ BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, -1, 0, 1), /* -1 replaced with real IP */ BPF_STMT(BPF_RET+BPF_K, -1), /* copy entire frame */ BPF_STMT(BPF_RET+BPF_K, 0) /* unrecognized message: ignore frame */ };LOCAL struct bpf_program arpread = { sizeof (arpfilter) / sizeof (struct bpf_insn), arpfilter };LOCAL int bpfArpDev; /* Gets any replies to ARP probes. */LOCAL u_short dhcps_port; /* Server port, in network byte order. */LOCAL u_short dhcpc_port; /* Client port, in network byte order. */IMPORT struct bpf_insn dhcpfilter[]; /* For updating xid test */IMPORT struct bpf_program dhcpread;/* globals */struct dhcp_param * dhcpcBootParam = NULL; /* Configuration parameters */IMPORT LEASE_DATA dhcpcBootLeaseData;IMPORT int dhcpcBindType; /* DHCP or BOOTP reply selected? *//* locals */LOCAL BOOL dhcpcOldFlag; /* Use old (padded) messages? */LOCAL int dhcpcCurrState; /* Current state */LOCAL int dhcpcPrevState; /* Previous state */LOCAL int dhcpcMsgLength; /* Current message size. */LOCAL struct if_info dhcpcIntface; /* Network interface */LOCAL struct buffer sbuf; /* Buffer for outgoing messages */LOCAL struct msg dhcpcMsgIn; /* Incoming message components */LOCAL struct msg dhcpcMsgOut; /* Outgoing message components */LOCAL unsigned char dhcpCookie [MAGIC_LEN] = RFC1048_MAGIC;LOCAL struct ps_udph spudph;LOCAL int (*dhcpcBootStates [MAX_BOOT_STATES]) ();LOCAL int handle_ip();LOCAL int handle_num();LOCAL int handle_ips();LOCAL int handle_str();LOCAL int handle_bool();LOCAL int handle_ippairs();LOCAL int handle_nums();LOCAL int handle_list();LOCAL int (*handle_param [MAXTAGNUM]) () = { NULL, /* PAD */ handle_ip, /* SUBNET_MASK */ handle_num, /* TIME_OFFSET */ handle_ips, /* ROUTER */ handle_ips, /* TIME_SERVER */ handle_ips, /* NAME_SERVER */ handle_ips, /* DNS_SERVER */ handle_ips, /* LOG_SERVER */ handle_ips, /* COOKIE_SERVER */ handle_ips, /* LPR_SERVER */ handle_ips, /* IMPRESS_SERVER */ handle_ips, /* RLS_SERVER */ handle_str, /* HOSTNAME */ handle_num, /* BOOTSIZE */ handle_str, /* MERIT_DUMP */ handle_str, /* DNS_DOMAIN */ handle_ip, /* SWAP_SERVER */ handle_str, /* ROOT_PATH */ handle_str, /* EXTENSIONS_PATH */ handle_bool, /* IP_FORWARD */ handle_bool, /* NONLOCAL_SRCROUTE */ handle_ippairs, /* POLICY_FILTER */ handle_num, /* MAX_DGRAM_SIZE */ handle_num, /* DEFAULT_IP_TTL */ handle_num, /* MTU_AGING_TIMEOUT */ handle_nums, /* MTU_PLATEAU_TABLE */ handle_num, /* IF_MTU */ handle_bool, /* ALL_SUBNET_LOCAL */ handle_ip, /* BRDCAST_ADDR */ handle_bool, /* MASK_DISCOVER */ handle_bool, /* MASK_SUPPLIER */ handle_bool, /* ROUTER_DISCOVER */ handle_ip, /* ROUTER_SOLICIT */ handle_ippairs, /* STATIC_ROUTE */ handle_bool, /* TRAILER */ handle_num, /* ARP_CACHE_TIMEOUT */ handle_bool, /* ETHER_ENCAP */ handle_num, /* DEFAULT_TCP_TTL */ handle_num, /* KEEPALIVE_INTER */ handle_bool, /* KEEPALIVE_GARBA */ handle_str, /* NIS_DOMAIN */ handle_ips, /* NIS_SERVER */ handle_ips, /* NTP_SERVER */ handle_list, /* VENDOR_SPEC */ handle_ips, /* NBN_SERVER */ handle_ips, /* NBDD_SERVER */ handle_num, /* NB_NODETYPE */ handle_str, /* NB_SCOPE */ handle_ips, /* XFONT_SERVER */ handle_ips, /* XDISPLAY_MANAGER */ NULL, /* REQUEST_IPADDR */ handle_num, /* LEASE_TIME */ NULL, /* OPT_OVERLOAD */ NULL, /* DHCP_MSGTYPE */ handle_ip, /* SERVER_ID */ NULL, /* REQ_LIST */ handle_str, /* DHCP_ERRMSG */ NULL, /* DHCP_MAXMSGSIZE */ handle_num, /* DHCP_T1 */ handle_num, /* DHCP_T2 */ NULL, /* CLASS_ID */ NULL, /* CLIENT_ID */ NULL, NULL, handle_str, /* NISP_DOMAIN */ handle_ips, /* NISP_SERVER */ handle_str, /* TFTP_SERVERNAME */ handle_str, /* BOOTFILE */ handle_ips, /* MOBILEIP_HA */ handle_ips, /* SMTP_SERVER */ handle_ips, /* POP3_SERVER */ handle_ips, /* NNTP_SERVER */ handle_ips, /* DFLT_WWW_SERVER */ handle_ips, /* DFLT_FINGER_SERVER */ handle_ips, /* DFLT_IRC_SERVER */ handle_ips, /* STREETTALK_SERVER */ handle_ips /* STDA_SERVER */ };/* forward declarations */int dhcp_boot_client (struct if_info *, int, int, BOOL);LOCAL void msgAlign (struct msg *, char *);LOCAL int arp_check (struct in_addr *, struct if_info *);LOCAL int initialize (int, int);LOCAL void set_declinfo (struct dhcp_reqspec *, char *);LOCAL int make_discover (void);LOCAL int make_request (struct dhcp_param *, int);LOCAL int dhcp_decline (struct dhcp_reqspec *);LOCAL int dhcp_msgtoparam (struct dhcp *, int, struct dhcp_param *);LOCAL int merge_param (struct dhcp_param *, struct dhcp_param *);LOCAL long generate_xid ();LOCAL int clean_param (struct dhcp_param *);LOCAL int dhcpcBootInitState();LOCAL int dhcpcBootWaitOffer();LOCAL int dhcpcBootSelecting();LOCAL int dhcpcBootRequesting();LOCAL int dhcpcBootInforming();#ifdef DHCPC_DEBUGLOCAL int nvttostr (char *, char *, int);#endif/********************************************************************************* dhcp_boot_client - Exercise client finite state machine** This routine uses the finite state machine to obtain a set of configuration* parameters. When the ROM-resident bootstrap loader issues a dhcpcBootBind()* call, this routine executes the state machine up until the BOUND state.* The states from BOUND onward are omitted to reduce the size of the boot ROM* images. If an address is already present, the routine executes the isolated* portion of the state machine which sends a DHCP INFORM message and waits for* an acknowledgement containing any additional parameters. The <bindFlag>* argument selects between these alternatives. In the first case, the boot* file downloaded by the bootstrap loader must spawn a task containing the* full state machine defined in the DHCP client runtime library to monitor* the established lease. ** RETURNS: OK if message exchange completed, or value of failed state on error.** ERRNO: N/A** SEE ALSO: dhcpcLib** NOMANUAL*/int dhcp_boot_client ( struct if_info *ifp, int serverPort, /* port used by DHCP servers */ int clientPort, /* port used by DHCP clients */ BOOL bindFlag /* establish new lease? */ ) { int next_state = 0; int retval = 0; dhcpcBootStates [INIT] = dhcpcBootInitState; dhcpcBootStates [WAIT_OFFER] = dhcpcBootWaitOffer; dhcpcBootStates [SELECTING] = dhcpcBootSelecting; dhcpcBootStates [REQUESTING] = dhcpcBootRequesting; dhcpcBootStates [INFORMING] = dhcpcBootInforming; dhcpcIntface = *ifp; if ( (retval = initialize (serverPort, clientPort)) < 0) return (retval); if (bindFlag) { /* * Full lease negotiation process. Use the state machine to * obtain configuration parameters, including an address. */ dhcpcPrevState = dhcpcCurrState = INIT; } else { /* * Partial message exchange for known (externally assigned) address. * Send an INFORM message and wait for an acknowledgement containing * additional parameters. */ dhcpcPrevState = dhcpcCurrState = INFORMING; } while (next_state != BOUND) { if ( (next_state = (*dhcpcBootStates [dhcpcCurrState]) ()) < 0) {#ifdef DHCPC_DEBUG logMsg ("Error in finite state machine.\n", 0, 0, 0, 0, 0, 0);#endif if (dhcpcBootParam != NULL) { clean_param (dhcpcBootParam); free (dhcpcBootParam); dhcpcBootParam = NULL; } close (bpfArpDev); bpfDevDelete ("/bpf/dhcpc-arp"); free (sbuf.buf); return (ERROR); } /* If not a full lease negotiation, keep indication of known address. */ if (dhcpcPrevState != INFORMING) dhcpcPrevState = dhcpcCurrState; dhcpcCurrState = next_state;#ifdef DHCPC_DEBUG logMsg ("next state= %d\n", next_state, 0, 0, 0, 0, 0);#endif } close (bpfArpDev); bpfDevDelete ("/bpf/dhcpc-arp"); free (sbuf.buf); return (OK); /* Bind successful. */ } /********************************************************************************* gen_retransmit - generic retransmission after timeout** This routine retransmits the current DHCP client message (a discover or* a request message) when the appropriate timeout interval expires.* It is called from multiple locations in the finite state machine.** RETURNS: 0 if transmission completed, or negative value on error.** ERRNO: N/A** NOMANUAL*/LOCAL int gen_retransmit
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -