whitespace and comment format fixes, no code changes
[oweals/busybox.git] / networking / udhcp / dhcprelay.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Port to Busybox Copyright (C) 2006 Jesse Dutton <jessedutton@gmail.com>
4  *
5  * Licensed under GPLv2, see file LICENSE in this source tree.
6  *
7  * DHCP Relay for 'DHCPv4 Configuration of IPSec Tunnel Mode' support
8  * Copyright (C) 2002 Mario Strasser <mast@gmx.net>,
9  *                   Zuercher Hochschule Winterthur,
10  *                   Netbeat AG
11  * Upstream has GPL v2 or later
12  */
13 //applet:IF_DHCPRELAY(APPLET(dhcprelay, BB_DIR_USR_SBIN, BB_SUID_DROP))
14
15 //kbuild:lib-$(CONFIG_DHCPRELAY) += dhcprelay.o
16
17 //usage:#define dhcprelay_trivial_usage
18 //usage:       "CLIENT_IFACE[,CLIENT_IFACE2]... SERVER_IFACE [SERVER_IP]"
19 //usage:#define dhcprelay_full_usage "\n\n"
20 //usage:       "Relay DHCP requests between clients and server"
21
22 #include "common.h"
23
24 #define SERVER_PORT    67
25
26 /* lifetime of an xid entry in sec. */
27 #define MAX_LIFETIME   2*60
28 /* select timeout in sec. */
29 #define SELECT_TIMEOUT (MAX_LIFETIME / 8)
30
31 /* This list holds information about clients. The xid_* functions manipulate this list. */
32 struct xid_item {
33         unsigned timestamp;
34         int client;
35         uint32_t xid;
36         struct sockaddr_in ip;
37         struct xid_item *next;
38 } FIX_ALIASING;
39
40 #define dhcprelay_xid_list (*(struct xid_item*)bb_common_bufsiz1)
41 #define INIT_G() do { setup_common_bufsiz(); } while (0)
42
43 static struct xid_item *xid_add(uint32_t xid, struct sockaddr_in *ip, int client)
44 {
45         struct xid_item *item;
46
47         /* create new xid entry */
48         item = xmalloc(sizeof(struct xid_item));
49
50         /* add xid entry */
51         item->ip = *ip;
52         item->xid = xid;
53         item->client = client;
54         item->timestamp = monotonic_sec();
55         item->next = dhcprelay_xid_list.next;
56         dhcprelay_xid_list.next = item;
57
58         return item;
59 }
60
61 static void xid_expire(void)
62 {
63         struct xid_item *item = dhcprelay_xid_list.next;
64         struct xid_item *last = &dhcprelay_xid_list;
65         unsigned current_time = monotonic_sec();
66
67         while (item != NULL) {
68                 if ((current_time - item->timestamp) > MAX_LIFETIME) {
69                         last->next = item->next;
70                         free(item);
71                         item = last->next;
72                 } else {
73                         last = item;
74                         item = item->next;
75                 }
76         }
77 }
78
79 static struct xid_item *xid_find(uint32_t xid)
80 {
81         struct xid_item *item = dhcprelay_xid_list.next;
82         while (item != NULL) {
83                 if (item->xid == xid) {
84                         break;
85                 }
86                 item = item->next;
87         }
88         return item;
89 }
90
91 static void xid_del(uint32_t xid)
92 {
93         struct xid_item *item = dhcprelay_xid_list.next;
94         struct xid_item *last = &dhcprelay_xid_list;
95         while (item != NULL) {
96                 if (item->xid == xid) {
97                         last->next = item->next;
98                         free(item);
99                         item = last->next;
100                 } else {
101                         last = item;
102                         item = item->next;
103                 }
104         }
105 }
106
107 /**
108  * get_dhcp_packet_type - gets the message type of a dhcp packet
109  * p - pointer to the dhcp packet
110  * returns the message type on success, -1 otherwise
111  */
112 static int get_dhcp_packet_type(struct dhcp_packet *p)
113 {
114         uint8_t *op;
115
116         /* it must be either a BOOTREQUEST or a BOOTREPLY */
117         if (p->op != BOOTREQUEST && p->op != BOOTREPLY)
118                 return -1;
119         /* get message type option */
120         op = udhcp_get_option(p, DHCP_MESSAGE_TYPE);
121         if (op != NULL)
122                 return op[0];
123         return -1;
124 }
125
126 /**
127  * make_iface_list - parses client/server interface names
128  * returns array
129  */
130 static char **make_iface_list(char **client_and_server_ifaces, int *client_number)
131 {
132         char *s, **iface_list;
133         int i, cn;
134
135         /* get number of items */
136         cn = 2; /* 1 server iface + at least 1 client one */
137         s = client_and_server_ifaces[0]; /* list of client ifaces */
138         while (*s) {
139                 if (*s == ',')
140                         cn++;
141                 s++;
142         }
143         *client_number = cn;
144
145         /* create vector of pointers */
146         iface_list = xzalloc(cn * sizeof(iface_list[0]));
147
148         iface_list[0] = client_and_server_ifaces[1]; /* server iface */
149
150         i = 1;
151         s = xstrdup(client_and_server_ifaces[0]); /* list of client ifaces */
152         goto store_client_iface_name;
153
154         while (i < cn) {
155                 if (*s++ == ',') {
156                         s[-1] = '\0';
157  store_client_iface_name:
158                         iface_list[i++] = s;
159                 }
160         }
161
162         return iface_list;
163 }
164
165 /* Creates listen sockets (in fds) bound to client and server ifaces,
166  * and returns numerically max fd.
167  */
168 static int init_sockets(char **iface_list, int num_clients, int *fds)
169 {
170         int i, n;
171
172         n = 0;
173         for (i = 0; i < num_clients; i++) {
174                 fds[i] = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT, iface_list[i]);
175                 if (n < fds[i])
176                         n = fds[i];
177         }
178         return n;
179 }
180
181 static int sendto_ip4(int sock, const void *msg, int msg_len, struct sockaddr_in *to)
182 {
183         int err;
184
185         errno = 0;
186         err = sendto(sock, msg, msg_len, 0, (struct sockaddr*) to, sizeof(*to));
187         err -= msg_len;
188         if (err)
189                 bb_perror_msg("sendto");
190         return err;
191 }
192
193 /**
194  * pass_to_server() - forwards dhcp packets from client to server
195  * p - packet to send
196  * client - number of the client
197  */
198 static void pass_to_server(struct dhcp_packet *p, int packet_len, int client, int *fds,
199                         struct sockaddr_in *client_addr, struct sockaddr_in *server_addr)
200 {
201         int type;
202
203         /* check packet_type */
204         type = get_dhcp_packet_type(p);
205         if (type != DHCPDISCOVER && type != DHCPREQUEST
206          && type != DHCPDECLINE && type != DHCPRELEASE
207          && type != DHCPINFORM
208         ) {
209                 return;
210         }
211
212         /* create new xid entry */
213         xid_add(p->xid, client_addr, client);
214
215         /* forward request to server */
216         /* note that we send from fds[0] which is bound to SERVER_PORT (67).
217          * IOW: we send _from_ SERVER_PORT! Although this may look strange,
218          * RFC 1542 not only allows, but prescribes this for BOOTP relays.
219          */
220         sendto_ip4(fds[0], p, packet_len, server_addr);
221 }
222
223 /**
224  * pass_to_client() - forwards dhcp packets from server to client
225  * p - packet to send
226  */
227 static void pass_to_client(struct dhcp_packet *p, int packet_len, int *fds)
228 {
229         int type;
230         struct xid_item *item;
231
232         /* check xid */
233         item = xid_find(p->xid);
234         if (!item) {
235                 return;
236         }
237
238         /* check packet type */
239         type = get_dhcp_packet_type(p);
240         if (type != DHCPOFFER && type != DHCPACK && type != DHCPNAK) {
241                 return;
242         }
243
244 //TODO: also do it if (p->flags & htons(BROADCAST_FLAG)) is set!
245         if (item->ip.sin_addr.s_addr == htonl(INADDR_ANY))
246                 item->ip.sin_addr.s_addr = htonl(INADDR_BROADCAST);
247
248         if (sendto_ip4(fds[item->client], p, packet_len, &item->ip) != 0) {
249                 return; /* send error occurred */
250         }
251
252         /* remove xid entry */
253         xid_del(p->xid);
254 }
255
256 int dhcprelay_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
257 int dhcprelay_main(int argc, char **argv)
258 {
259         struct sockaddr_in server_addr;
260         char **iface_list;
261         int *fds;
262         int num_sockets, max_socket;
263         uint32_t our_nip;
264
265         INIT_G();
266
267         server_addr.sin_family = AF_INET;
268         server_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
269         server_addr.sin_port = htons(SERVER_PORT);
270
271         /* dhcprelay CLIENT_IFACE1[,CLIENT_IFACE2...] SERVER_IFACE [SERVER_IP] */
272         if (argc == 4) {
273                 if (!inet_aton(argv[3], &server_addr.sin_addr))
274                         bb_perror_msg_and_die("bad server IP");
275         } else if (argc != 3) {
276                 bb_show_usage();
277         }
278
279         iface_list = make_iface_list(argv + 1, &num_sockets);
280
281         fds = xmalloc(num_sockets * sizeof(fds[0]));
282
283         /* Create sockets and bind one to every iface */
284         max_socket = init_sockets(iface_list, num_sockets, fds);
285
286         /* Get our IP on server_iface */
287         if (udhcp_read_interface(argv[2], NULL, &our_nip, NULL))
288                 return 1;
289
290         /* Main loop */
291         while (1) {
292 // reinit stuff from time to time? go back to make_iface_list
293 // every N minutes?
294                 fd_set rfds;
295                 struct timeval tv;
296                 int i;
297
298                 FD_ZERO(&rfds);
299                 for (i = 0; i < num_sockets; i++)
300                         FD_SET(fds[i], &rfds);
301                 tv.tv_sec = SELECT_TIMEOUT;
302                 tv.tv_usec = 0;
303                 if (select(max_socket + 1, &rfds, NULL, NULL, &tv) > 0) {
304                         int packlen;
305                         struct dhcp_packet dhcp_msg;
306
307                         /* server */
308                         if (FD_ISSET(fds[0], &rfds)) {
309                                 packlen = udhcp_recv_kernel_packet(&dhcp_msg, fds[0]);
310                                 if (packlen > 0) {
311                                         pass_to_client(&dhcp_msg, packlen, fds);
312                                 }
313                         }
314
315                         /* clients */
316                         for (i = 1; i < num_sockets; i++) {
317                                 struct sockaddr_in client_addr;
318                                 socklen_t addr_size;
319
320                                 if (!FD_ISSET(fds[i], &rfds))
321                                         continue;
322
323                                 addr_size = sizeof(client_addr);
324                                 packlen = recvfrom(fds[i], &dhcp_msg, sizeof(dhcp_msg), 0,
325                                                 (struct sockaddr *)(&client_addr), &addr_size);
326                                 if (packlen <= 0)
327                                         continue;
328
329                                 /* Get our IP on corresponding client_iface */
330 // RFC 1542
331 // 4.1 General BOOTP Processing for Relay Agents
332 // 4.1.1 BOOTREQUEST Messages
333 //   If the relay agent does decide to relay the request, it MUST examine
334 //   the 'giaddr' ("gateway" IP address) field.  If this field is zero,
335 //   the relay agent MUST fill this field with the IP address of the
336 //   interface on which the request was received.  If the interface has
337 //   more than one IP address logically associated with it, the relay
338 //   agent SHOULD choose one IP address associated with that interface and
339 //   use it consistently for all BOOTP messages it relays.  If the
340 //   'giaddr' field contains some non-zero value, the 'giaddr' field MUST
341 //   NOT be modified.  The relay agent MUST NOT, under any circumstances,
342 //   fill the 'giaddr' field with a broadcast address as is suggested in
343 //   [1] (Section 8, sixth paragraph).
344
345 // but why? what if server can't route such IP? Client ifaces may be, say, NATed!
346
347 // 4.1.2 BOOTREPLY Messages
348 //   BOOTP relay agents relay BOOTREPLY messages only to BOOTP clients.
349 //   It is the responsibility of BOOTP servers to send BOOTREPLY messages
350 //   directly to the relay agent identified in the 'giaddr' field.
351 // (yeah right, unless it is impossible... see comment above)
352 //   Therefore, a relay agent may assume that all BOOTREPLY messages it
353 //   receives are intended for BOOTP clients on its directly-connected
354 //   networks.
355 //
356 //   When a relay agent receives a BOOTREPLY message, it should examine
357 //   the BOOTP 'giaddr', 'yiaddr', 'chaddr', 'htype', and 'hlen' fields.
358 //   These fields should provide adequate information for the relay agent
359 //   to deliver the BOOTREPLY message to the client.
360 //
361 //   The 'giaddr' field can be used to identify the logical interface from
362 //   which the reply must be sent (i.e., the host or router interface
363 //   connected to the same network as the BOOTP client).  If the content
364 //   of the 'giaddr' field does not match one of the relay agent's
365 //   directly-connected logical interfaces, the BOOTREPLY message MUST be
366 //   silently discarded.
367                                 if (udhcp_read_interface(iface_list[i], NULL, &dhcp_msg.gateway_nip, NULL)) {
368                                         /* Fall back to our IP on server iface */
369 // this makes more sense!
370                                         dhcp_msg.gateway_nip = our_nip;
371                                 }
372 // maybe dhcp_msg.hops++? drop packets with too many hops (RFC 1542 says 4 or 16)?
373                                 pass_to_server(&dhcp_msg, packlen, i, fds, &client_addr, &server_addr);
374                         }
375                 }
376                 xid_expire();
377         } /* while (1) */
378
379         /* return 0; - not reached */
380 }