Magellan Linux

Annotation of /trunk/mkinitrd-magellan/busybox/networking/udhcp/clientpacket.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 532 - (hide annotations) (download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 6448 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd

1 niro 532 /* vi: set sw=4 ts=4: */
2     /* clientpacket.c
3     *
4     * Packet generation and dispatching functions for the DHCP client.
5     *
6     * Russ Dill <Russ.Dill@asu.edu> July 2001
7     *
8     * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9     */
10    
11     #include <features.h>
12     #if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION
13     #include <netpacket/packet.h>
14     #include <net/ethernet.h>
15     #else
16     #include <asm/types.h>
17     #include <linux/if_packet.h>
18     #include <linux/if_ether.h>
19     #endif
20    
21     #include "common.h"
22     #include "dhcpd.h"
23     #include "dhcpc.h"
24     #include "options.h"
25    
26    
27     /* Create a random xid */
28     unsigned long random_xid(void)
29     {
30     static int initialized;
31     if (!initialized) {
32     unsigned long seed;
33    
34     if (open_read_close("/dev/urandom", &seed, sizeof(seed)) < 0) {
35     bb_info_msg("Cannot load seed "
36     "from /dev/urandom: %s", strerror(errno));
37     seed = time(0);
38     }
39     srand(seed);
40     initialized++;
41     }
42     return rand();
43     }
44    
45    
46     /* initialize a packet with the proper defaults */
47     static void init_packet(struct dhcpMessage *packet, char type)
48     {
49     udhcp_init_header(packet, type);
50     memcpy(packet->chaddr, client_config.arp, 6);
51     if (client_config.clientid)
52     add_option_string(packet->options, client_config.clientid);
53     if (client_config.hostname) add_option_string(packet->options, client_config.hostname);
54     if (client_config.fqdn) add_option_string(packet->options, client_config.fqdn);
55     add_option_string(packet->options, client_config.vendorclass);
56     }
57    
58    
59     /* Add a parameter request list for stubborn DHCP servers. Pull the data
60     * from the struct in options.c. Don't do bounds checking here because it
61     * goes towards the head of the packet. */
62     static void add_requests(struct dhcpMessage *packet)
63     {
64     int end = end_option(packet->options);
65     int i, len = 0;
66    
67     packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
68     for (i = 0; dhcp_options[i].code; i++)
69     if (dhcp_options[i].flags & OPTION_REQ)
70     packet->options[end + OPT_DATA + len++] = dhcp_options[i].code;
71     packet->options[end + OPT_LEN] = len;
72     packet->options[end + OPT_DATA + len] = DHCP_END;
73    
74     }
75    
76    
77     /* Broadcast a DHCP discover packet to the network, with an optionally requested IP */
78     int send_discover(unsigned long xid, unsigned long requested)
79     {
80     struct dhcpMessage packet;
81    
82     init_packet(&packet, DHCPDISCOVER);
83     packet.xid = xid;
84     if (requested)
85     add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
86    
87     add_requests(&packet);
88     bb_info_msg("Sending discover...");
89     return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
90     SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
91     }
92    
93    
94     /* Broadcasts a DHCP request message */
95     int send_selecting(unsigned long xid, unsigned long server, unsigned long requested)
96     {
97     struct dhcpMessage packet;
98     struct in_addr addr;
99    
100     init_packet(&packet, DHCPREQUEST);
101     packet.xid = xid;
102    
103     add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
104     add_simple_option(packet.options, DHCP_SERVER_ID, server);
105    
106     add_requests(&packet);
107     addr.s_addr = requested;
108     bb_info_msg("Sending select for %s...", inet_ntoa(addr));
109     return udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
110     SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
111     }
112    
113    
114     /* Unicasts or broadcasts a DHCP renew message */
115     int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr)
116     {
117     struct dhcpMessage packet;
118     int ret = 0;
119    
120     init_packet(&packet, DHCPREQUEST);
121     packet.xid = xid;
122     packet.ciaddr = ciaddr;
123    
124     add_requests(&packet);
125     bb_info_msg("Sending renew...");
126     if (server)
127     ret = udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
128     else ret = udhcp_raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
129     SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
130     return ret;
131     }
132    
133    
134     /* Unicasts a DHCP release message */
135     int send_release(unsigned long server, unsigned long ciaddr)
136     {
137     struct dhcpMessage packet;
138    
139     init_packet(&packet, DHCPRELEASE);
140     packet.xid = random_xid();
141     packet.ciaddr = ciaddr;
142    
143     add_simple_option(packet.options, DHCP_REQUESTED_IP, ciaddr);
144     add_simple_option(packet.options, DHCP_SERVER_ID, server);
145    
146     bb_info_msg("Sending release...");
147     return udhcp_kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
148     }
149    
150    
151     /* return -1 on errors that are fatal for the socket, -2 for those that aren't */
152     int get_raw_packet(struct dhcpMessage *payload, int fd)
153     {
154     int bytes;
155     struct udp_dhcp_packet packet;
156     uint32_t source, dest;
157     uint16_t check;
158    
159     memset(&packet, 0, sizeof(struct udp_dhcp_packet));
160     bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet));
161     if (bytes < 0) {
162     DEBUG("Cannot read on raw listening socket - ignoring");
163     usleep(500000); /* possible down interface, looping condition */
164     return -1;
165     }
166    
167     if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) {
168     DEBUG("Message too short, ignoring");
169     return -2;
170     }
171    
172     if (bytes < ntohs(packet.ip.tot_len)) {
173     DEBUG("Truncated packet");
174     return -2;
175     }
176    
177     /* ignore any extra garbage bytes */
178     bytes = ntohs(packet.ip.tot_len);
179    
180     /* Make sure its the right packet for us, and that it passes sanity checks */
181     if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION
182     || packet.ip.ihl != sizeof(packet.ip) >> 2
183     || packet.udp.dest != htons(CLIENT_PORT)
184     || bytes > (int) sizeof(struct udp_dhcp_packet)
185     || ntohs(packet.udp.len) != (uint16_t)(bytes - sizeof(packet.ip))
186     ) {
187     DEBUG("Unrelated/bogus packet");
188     return -2;
189     }
190    
191     /* check IP checksum */
192     check = packet.ip.check;
193     packet.ip.check = 0;
194     if (check != udhcp_checksum(&(packet.ip), sizeof(packet.ip))) {
195     DEBUG("bad IP header checksum, ignoring");
196     return -1;
197     }
198    
199     /* verify the UDP checksum by replacing the header with a psuedo header */
200     source = packet.ip.saddr;
201     dest = packet.ip.daddr;
202     check = packet.udp.check;
203     packet.udp.check = 0;
204     memset(&packet.ip, 0, sizeof(packet.ip));
205    
206     packet.ip.protocol = IPPROTO_UDP;
207     packet.ip.saddr = source;
208     packet.ip.daddr = dest;
209     packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */
210     if (check && check != udhcp_checksum(&packet, bytes)) {
211     bb_error_msg("packet with bad UDP checksum received, ignoring");
212     return -2;
213     }
214    
215     memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
216    
217     if (ntohl(payload->cookie) != DHCP_MAGIC) {
218     bb_error_msg("received bogus message (bad magic) - ignoring");
219     return -2;
220     }
221     DEBUG("oooooh!!! got some!");
222     return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
223    
224     }