📄 dhcpcbootlib.c
字号:
/* dhcpcBootLib.c - DHCP boot-time client library *//* Copyright 1984 - 2000 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------01x,27mar02,wap make dhcpcBootParamsGet() return error if dhcpcBootParam hasn't been initialized (SPR #73147)01w,04dec01,wap initialize dhcpcLeaseList correctly to avoid writing to page 0 (NULL pointer dereference) (SPR #69523)01v,17nov00,spm added support for BSD Ethernet devices01u,15nov00,spm fixed incorrect pointer assignment in lease list (SPR #62274)01t,24oct00,spm fixed modification history after merge from tor3_0_x branch01s,23oct00,niq merged from version 01t of tor3_0_x branch (base version 01r); upgrade to BPF replaces tagged frames support01r,16mar99,spm recovered orphaned code from tor2_0_x branch (SPR #25770)01q,16mar99,spm recovered orphaned code from tor1_0_1.sens1_1 (SPR #25770)01p,01dec98,spm added missing memory allocation (SPR #22881)01p,06oct98,spm fixed copying of parameters with IP address pairs (SPR #22416)01o,14dec97,jdi doc: cleanup.01n,dec1097,kbw fiddled manpage format01m,04dec97,spm added code review modifications01l,06oct97,spm removed reference to deleted endDriver global; replaced with support for dynamic driver type detection01k,25sep97,spm restored man page for dhcpcBootOptionSet() routine01j,25sep97,gnn SENS beta feedback fixes01i,02sep97,spm removed name conflicts with runtime DHCP client (SPR #9241)01h,26aug97,spm major overhaul: included version 01j of dhcpcCommonLib.c to retain single-lease library for boot time use01g,12aug97,gnn changes necessitated by MUX/END update.01f,02jun97,spm changed DHCP option tags to prevent name conflicts (SPR #8667) and updated man pages01e,18apr97,spm added call to dhcp_boot_client to reduce boot ROM size01d,15apr97,kbw fixed manpage format01c,07apr97,spm altered to use current value of configAll.h defines, rewrote documentation01b,29jan97,spm added END driver support and modified to fit coding standards01a,14nov96,spm created by removing unneeded functions from dhcpcLib.c*//*DESCRIPTIONThis library contains the interface for the client side of the Dynamic HostConfiguration Protocol (DHCP) used during system boot. DHCP is an extensionof BOOTP, the bootstrap protocol. Like BOOTP, the protocol allows automaticsystem startup by providing an IP address, boot file name, and boot host'sIP address over a network. Additionally, DHCP provides the complete set ofconfiguration parameters defined in the Host Requirements RFCs and allowsautomatic reuse of network addresses by specifying a lease duration for aset of configuration parameters. This library is linked into the boot ROM image automatically if INCLUDE_DHCPC is defined at the time that image is constructed.HIGH-LEVEL INTERFACEThe VxWorks boot program uses this library to obtain configuration parameters with DHCP according to the client-server interaction detailed in RFC 2131 usingthe boot device specified in the boot parameters. The DHCP client supportsdevices attached to the IP protocol with the MUX/END interface. It alsosupports BSD Ethernet devices attached to the IP protocol.To use DHCP, first build a boot ROM image with INCLUDE_DHCPC defined and set the appropriate flag in the boot parameters before initiating booting withthe "@" command. The DHCP client will attempt to retrieve entries for theboot file name, and host IP address, as well as a subnet mask and broadcastaddress for the boot device. If a target IP address is not available, theclient will retrieve those parameters in the context of a lease. Otherwise,it will search for permanent assignments using a simpler message exchange.Any entries retrieved with either method will only be used if the correspondingfields in the boot parameters are blank.NOTEAfter DHCP retrieves the boot parameters, the specified boot file is loadedand the system restarts. As a result, the boot-time DHCP client cannot renewany lease which may be associated with the assigned IP address. To avoidpotential IP address conflicts while loading the boot file, the DHCPC_MIN_LEASEvalue should be set to exceed the file transfer time. In addition, the bootfile must also contain the DHCP client library so that the lease obtainedbefore the restart can be renewed. Otherwise, the network initialization usingthe boot parameters will fail. These restrictions do not apply if the targetIP address is entered manually since the boot parameters do not involve alease in that case.INTERNALThis module was created to reduce the size of the boot ROM image so thatthe DHCP client could be used with target architectures such as the MV147which have limited ROM capacity. The routines in this library are a modifiedversion of similar routines available to the user in the dhcpcLib library. These routines are only intended for use during boot time by the VxWorks system. Among other differences, they do not support multiple leases and they always apply configuration parameters to the underlying network device. They should only be called by the boot program.INCLUDE FILES: dhcpcBootLib.hSEE ALSO: dhcpcLib, RFC 1541, RFC 1533*//* includes */#include "dhcp/copyright_dhcp.h"#include "vxWorks.h"#include "wdLib.h"#include "semLib.h"#include "ioLib.h" /* ioctl() declaration */#include "taskLib.h" /* taskSpawn() declaration */#include "muxLib.h"#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <errno.h>#include <signal.h>#include <fcntl.h>#include <sys/types.h>#include <sys/socket.h>#include <sys/ioctl.h>#include <net/if.h>#include <netinet/in.h>#include <netinet/in_systm.h>#include <netinet/if_ether.h>#include <netinet/ip.h>#include <netinet/udp.h>#include <arpa/inet.h> #include "dhcp/dhcpcCommonLib.h"#include "dhcpcBootLib.h"#include "bpfDrv.h"#include "end.h"#include "ipProto.h"/* defines */#define _BYTESPERWORD 4 /* Conversion factor for IP header */#define _DHCP_MAX_OPTLEN 255 /* Max. number of bytes in an option */#define _IPADDRLEN 4 /* Length of address in an ARP reply */#define _ETHER 1 /* Hardware type for Ethernet */ /* from assigned numbers RFC */#define _ETHERADDRLEN 6 /* Length of an Ethernet address */ /* Host requirements documents default values. */#define _HRD_MAX_DGRAM 576 /* Default maximum datagram size. */#define _HRD_IP_TTL 64 /* Default IP time-to-live (seconds) */#define _HRD_MTU 576 /* Default interface MTU */#define _HRD_ROUTER 0xffffffff /* Default router solication */ /* address - 255.255.255.255 */#define _HRD_ARP_TIME 60 /* Default ARP cache timeout (seconds) */#define _HRD_TCP_TTL 64 /* Default TCP time-to-live (seconds) */#define _HRD_TCP_TIME 7200 /* Default TCP keepalive interval (seconds) */#define _DHCPC_MAX_DEVNAME 21 /* "/bpf/dhcpc" + max unit number */IMPORT struct bpf_insn dhcpfilter[]; /* Needed to update client port. */IMPORT struct bpf_program dhcpread; /* Installs filter for DHCP messages */IMPORT int dhcpcBufSize;IMPORT BOOL _dhcpcBootFlag;/* globals */LEASE_DATA dhcpcBootLeaseData;LEASE_DATA * pDhcpcBootLeaseData;int dhcpcBindType; /* Type of DHCP lease, if any. *//* locals */LOCAL int dhcpcOfferTimeout; /* Set externally when building image */LOCAL int dhcpcDefaultLease; /* Set externally when building image */LOCAL int dhcpcClientPort; /* Set externally when building image */LOCAL int dhcpcServerPort; /* Set externally when building image *//* forward declarations */IMPORT void dhcpcRead (void); /* Retrieve incoming DHCP messages */LOCAL void dhcpcBootCleanup (void);/********************************************************************************* dhcpcBootInit - set up the DHCP client parameters and data structures** This routine creates any necessary data structures and sets the client's * option request list to retrieve a subnet mask and broadcast address for the * network interface indicated by <pIf>. The routine is executed automatically * by the boot program when INCLUDE_DHCPC is defined and the automatic * configuration option is set in the boot flags. The network interface * specified by <pIf> is used to transmit and receive all DHCP messages during * the lease negotiation. The DHCP client supports interfaces attached to the* IP protocol using the MUX/END interface and BSD Ethernet devices attached* to that protocol. The interface must be capable of sending broadcast* messages. The <maxSize> parameter specifies the maximum length supported for* any DHCP message, including the UDP and IP headers and the link level header.* The maximum length of the DHCP options field is based on this value or the* MTU size for the given interface, whichever is less. The smallest valid value* for the <maxSize> parameter is 576 bytes, corresponding to the minimum IP* datagram a host must accept. The MTU size of the network interface must be* large enough to handle those datagrams.** ERRNO: N/A* * RETURNS: Lease handle for later use, or NULL if lease startup fails.*/void * dhcpcBootInit ( struct ifnet * pIf, /* network device used by client */ int serverPort, /* port used by DHCP servers (default 67) */ int clientPort, /* port used by DHCP clients (default 68) */ int maxSize, /* largest DHCP message supported, in bytes */ int offerTimeout, /* interval to get additional DHCP offers */ int defaultLease, /* default value for requested lease length */ int minLease /* minimum accepted lease length */ ) { struct dhcp_reqspec * pReqSpec; LEASE_DATA * pBootLeaseData; BOOL bsdFlag = FALSE; int loop; int offset = 0; int bufSize; /* Receive buffer size (maxSize or MTU + BPF header) */ char bpfDevName [_DHCPC_MAX_DEVNAME]; /* "/bpf/dhcpc" + max unit number */ int bpfDev; int result; dhcpcServerPort = serverPort; dhcpcClientPort = clientPort; if (pIf == NULL) { printf ("Error: network device not specified.\n"); return (NULL); } if (muxDevExists (pIf->if_name, pIf->if_unit) == FALSE) { bsdFlag = TRUE; } /* Verify interface data sizes are appropriate for message. */ bufSize = pIf->if_mtu; if (bufSize == 0) { printf ("Error: unusable network device.\n"); return (NULL); } if (bufSize < DHCP_MSG_SIZE) { /* * Devices must accept messages equal to the minimum IP datagram * of 576 bytes, which corresponds to a DHCP message with up to * 312 bytes of options. */ printf ("Error: unusable network device.\n"); return (NULL); } if (maxSize < DFLTDHCPLEN + UDPHL + IPHL) { /* The buffer size must also allow the minimum IP datagram. */ return (NULL); } if (bpfDrv () == ERROR) { return (NULL); } /* Select buffer size for BPF device less than or equal to the MTU size. */ bufSize += pIf->if_hdrlen; if (bufSize > maxSize) bufSize = maxSize; bufSize += sizeof (struct bpf_hdr); if (bpfDevCreate ("/bpf/dhcpc", 1, bufSize) == ERROR) { bpfDrvRemove (); return (NULL); } /* Create and initialize storage for boot lease. */ pBootLeaseData = &dhcpcBootLeaseData; pDhcpcBootLeaseData = pBootLeaseData; dhcpcLeaseList = &pDhcpcBootLeaseData; dhcpcMaxLeases = 1; /* Only one DHCP session, for boot device. */ dhcpcDefaultLease = defaultLease; dhcpcOfferTimeout = offerTimeout; dhcpcMinLease = minLease; /* Create signalling semaphore for event notification ring. */ dhcpcEventSem = semCCreate (SEM_Q_FIFO, 0); if (dhcpcEventSem == NULL) { bpfDevDelete ("/bpf/dhcpc"); bpfDrvRemove (); return (NULL); } /* Create event storage. */ dhcpcEventRing = rngCreate (EVENT_RING_SIZE); if (dhcpcEventRing == NULL) { bpfDevDelete ("/bpf/dhcpc"); bpfDrvRemove (); semDelete (dhcpcEventSem); return (NULL); } /* Create message storage (matches number of elements in event ring). */ dhcpcMessageList = malloc (10 * sizeof (MESSAGE_DATA)); if (dhcpcMessageList == NULL) { bpfDevDelete ("/bpf/dhcpc"); bpfDrvRemove (); semDelete (dhcpcEventSem); rngDelete (dhcpcEventRing); return (NULL); } /* Allocate (aligned) receive buffers based on maximum message size. */ for (loop = 0; loop < 10; loop++) { dhcpcMessageList [loop].msgBuffer = memalign (4, bufSize); if (dhcpcMessageList [loop].msgBuffer == NULL) break; dhcpcMessageList [loop].writeFlag = TRUE; } if (loop < 10) { for (offset = 0; offset < loop; offset++) free (dhcpcMessageList [offset].msgBuffer); bpfDevDelete ("/bpf/dhcpc"); bpfDrvRemove (); semDelete (dhcpcEventSem); rngDelete (dhcpcEventRing); free (dhcpcMessageList); return (NULL); } /* Reset the message filter to use the specified client port. */ dhcpfilter [12].k = clientPort; /* Get a BPF file descriptor to read/write DHCP messages. */ sprintf (bpfDevName, "/bpf/dhcpc%d", offset); bpfDev = open (bpfDevName, 0, 0); if (bpfDev < 0) { bpfDevDelete ("/bpf/dhcpc"); bpfDrvRemove (); semDelete (dhcpcEventSem); rngDelete (dhcpcEventRing);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -