added full udhcp integration
[oweals/busybox.git] / networking / udhcp / clientpacket.c
1 /* clientpacket.c
2  *
3  * Packet generation and dispatching functions for the DHCP client.
4  *
5  * Russ Dill <Russ.Dill@asu.edu> July 2001
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21  
22 #include <string.h>
23 #include <sys/socket.h>
24 #include <features.h>
25 #if __GLIBC__ >=2 && __GLIBC_MINOR >= 1
26 #include <netpacket/packet.h>
27 #include <net/ethernet.h>
28 #else
29 #include <asm/types.h>
30 #include <linux/if_packet.h>
31 #include <linux/if_ether.h>
32 #endif
33 #include <stdlib.h>
34 #include <time.h>
35 #include <unistd.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38
39
40 #include "dhcpd.h"
41 #include "packet.h"
42 #include "options.h"
43 #include "dhcpc.h"
44 #include "debug.h"
45
46
47 /* Create a random xid */
48 unsigned long random_xid(void)
49 {
50         static int initialized;
51         if (!initialized) {
52                 srand(time(0));
53                 initialized++;
54         }
55         return rand();
56 }
57
58
59 /* initialize a packet with the proper defaults */
60 static void init_packet(struct dhcpMessage *packet, char type)
61 {
62         struct vendor  {
63                 char vendor, length;
64                 char str[sizeof("udhcp "VERSION)];
65         } vendor_id = { DHCP_VENDOR,  sizeof("udhcp "VERSION) - 1, "udhcp "VERSION};
66         
67         init_header(packet, type);
68         memcpy(packet->chaddr, client_config.arp, 6);
69         add_option_string(packet->options, client_config.clientid);
70         if (client_config.hostname) add_option_string(packet->options, client_config.hostname);
71         add_option_string(packet->options, (unsigned char *) &vendor_id);
72 }
73
74
75 /* Add a paramater request list for stubborn DHCP servers. Pull the data
76  * from the struct in options.c. Don't do bounds checking here because it
77  * goes towards the head of the packet. */
78 static void add_requests(struct dhcpMessage *packet)
79 {
80         int end = end_option(packet->options);
81         int i, len = 0;
82
83         packet->options[end + OPT_CODE] = DHCP_PARAM_REQ;
84         for (i = 0; options[i].code; i++)
85                 if (options[i].flags & OPTION_REQ)
86                         packet->options[end + OPT_DATA + len++] = options[i].code;
87         packet->options[end + OPT_LEN] = len;
88         packet->options[end + OPT_DATA + len] = DHCP_END;
89
90 }
91
92
93 /* Broadcast a DHCP discover packet to the network, with an optionally requested IP */
94 int send_discover(unsigned long xid, unsigned long requested)
95 {
96         struct dhcpMessage packet;
97
98         init_packet(&packet, DHCPDISCOVER);
99         packet.xid = xid;
100         if (requested)
101                 add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
102
103         add_requests(&packet);
104         LOG(LOG_DEBUG, "Sending discover...");
105         return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, 
106                                 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
107 }
108
109
110 /* Broadcasts a DHCP request message */
111 int send_selecting(unsigned long xid, unsigned long server, unsigned long requested)
112 {
113         struct dhcpMessage packet;
114         struct in_addr addr;
115
116         init_packet(&packet, DHCPREQUEST);
117         packet.xid = xid;
118
119         add_simple_option(packet.options, DHCP_REQUESTED_IP, requested);
120         add_simple_option(packet.options, DHCP_SERVER_ID, server);
121         
122         add_requests(&packet);
123         addr.s_addr = requested;
124         LOG(LOG_DEBUG, "Sending select for %s...", inet_ntoa(addr));
125         return raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST, 
126                                 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
127 }
128
129
130 /* Unicasts or broadcasts a DHCP renew message */
131 int send_renew(unsigned long xid, unsigned long server, unsigned long ciaddr)
132 {
133         struct dhcpMessage packet;
134         int ret = 0;
135
136         init_packet(&packet, DHCPREQUEST);
137         packet.xid = xid;
138         packet.ciaddr = ciaddr;
139
140         add_requests(&packet);
141         LOG(LOG_DEBUG, "Sending renew...");
142         if (server) 
143                 ret = kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
144         else ret = raw_packet(&packet, INADDR_ANY, CLIENT_PORT, INADDR_BROADCAST,
145                                 SERVER_PORT, MAC_BCAST_ADDR, client_config.ifindex);
146         return ret;
147 }       
148
149
150 /* Unicasts a DHCP release message */
151 int send_release(unsigned long server, unsigned long ciaddr)
152 {
153         struct dhcpMessage packet;
154
155         init_packet(&packet, DHCPRELEASE);
156         packet.xid = random_xid();
157         packet.ciaddr = ciaddr;
158         
159         add_simple_option(packet.options, DHCP_REQUESTED_IP, ciaddr);
160         add_simple_option(packet.options, DHCP_SERVER_ID, server);
161
162         LOG(LOG_DEBUG, "Sending release...");
163         return kernel_packet(&packet, ciaddr, CLIENT_PORT, server, SERVER_PORT);
164 }
165
166
167 /* return -1 on errors that are fatal for the socket, -2 for those that aren't */
168 int get_raw_packet(struct dhcpMessage *payload, int fd)
169 {
170         int bytes;
171         struct udp_dhcp_packet packet;
172         u_int32_t source, dest;
173         u_int16_t check;
174
175         memset(&packet, 0, sizeof(struct udp_dhcp_packet));
176         bytes = read(fd, &packet, sizeof(struct udp_dhcp_packet));
177         if (bytes < 0) {
178                 DEBUG(LOG_INFO, "couldn't read on raw listening socket -- ignoring");
179                 usleep(500000); /* possible down interface, looping condition */
180                 return -1;
181         }
182         
183         if (bytes < (int) (sizeof(struct iphdr) + sizeof(struct udphdr))) {
184                 DEBUG(LOG_INFO, "message too short, ignoring");
185                 return -2;
186         }
187         
188         if (bytes < ntohs(packet.ip.tot_len)) {
189                 DEBUG(LOG_INFO, "Truncated packet");
190                 return -2;
191         }
192         
193         /* ignore any extra garbage bytes */
194         bytes = ntohs(packet.ip.tot_len);
195         
196         /* Make sure its the right packet for us, and that it passes sanity checks */
197         if (packet.ip.protocol != IPPROTO_UDP || packet.ip.version != IPVERSION ||
198             packet.ip.ihl != sizeof(packet.ip) >> 2 || packet.udp.dest != htons(CLIENT_PORT) ||
199             bytes > (int) sizeof(struct udp_dhcp_packet) ||
200             ntohs(packet.udp.len) != (short) (bytes - sizeof(packet.ip))) {
201                 DEBUG(LOG_INFO, "unrelated/bogus packet");
202                 return -2;
203         }
204
205         /* check IP checksum */
206         check = packet.ip.check;
207         packet.ip.check = 0;
208         if (check != checksum(&(packet.ip), sizeof(packet.ip))) {
209                 DEBUG(LOG_INFO, "bad IP header checksum, ignoring");
210                 return -1;
211         }
212         
213         /* verify the UDP checksum by replacing the header with a psuedo header */
214         source = packet.ip.saddr;
215         dest = packet.ip.daddr;
216         check = packet.udp.check;
217         packet.udp.check = 0;
218         memset(&packet.ip, 0, sizeof(packet.ip));
219
220         packet.ip.protocol = IPPROTO_UDP;
221         packet.ip.saddr = source;
222         packet.ip.daddr = dest;
223         packet.ip.tot_len = packet.udp.len; /* cheat on the psuedo-header */
224         if (check && check != checksum(&packet, bytes)) {
225                 DEBUG(LOG_ERR, "packet with bad UDP checksum received, ignoring");
226                 return -2;
227         }
228         
229         memcpy(payload, &(packet.data), bytes - (sizeof(packet.ip) + sizeof(packet.udp)));
230         
231         if (ntohl(payload->cookie) != DHCP_MAGIC) {
232                 LOG(LOG_ERR, "received bogus message (bad magic) -- ignoring");
233                 return -2;
234         }
235         DEBUG(LOG_INFO, "oooooh!!! got some!");
236         return bytes - (sizeof(packet.ip) + sizeof(packet.udp));
237         
238 }
239