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