blob: f82ac05b430221701ae49bc2ca4bd7c04ab920a0 [file] [log] [blame]
Denis Vlasenko736230e2006-11-20 19:40:36 +00001/* vi: set sw=4 ts=4: */
2/* Port to Busybox Copyright (C) 2006 Jesse Dutton <jessedutton@gmail.com>
3 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02004 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko736230e2006-11-20 19:40:36 +00005 *
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +00006 * DHCP Relay for 'DHCPv4 Configuration of IPSec Tunnel Mode' support
7 * Copyright (C) 2002 Mario Strasser <mast@gmx.net>,
Denis Vlasenko736230e2006-11-20 19:40:36 +00008 * Zuercher Hochschule Winterthur,
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +00009 * Netbeat AG
Denis Vlasenko736230e2006-11-20 19:40:36 +000010 * Upstream has GPL v2 or later
11 */
Pere Orga5bc8c002011-04-11 03:29:49 +020012
13//usage:#define dhcprelay_trivial_usage
14//usage: "CLIENT_IFACE[,CLIENT_IFACE2]... SERVER_IFACE [SERVER_IP]"
15//usage:#define dhcprelay_full_usage "\n\n"
16//usage: "Relay DHCP requests between clients and server"
17
Denis Vlasenko736230e2006-11-20 19:40:36 +000018#include "common.h"
Denis Vlasenko736230e2006-11-20 19:40:36 +000019
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +020020#define SERVER_PORT 67
21
22/* lifetime of an xid entry in sec. */
23#define MAX_LIFETIME 2*60
24/* select timeout in sec. */
25#define SELECT_TIMEOUT (MAX_LIFETIME / 8)
Denis Vlasenko736230e2006-11-20 19:40:36 +000026
Denis Vlasenko736230e2006-11-20 19:40:36 +000027/* This list holds information about clients. The xid_* functions manipulate this list. */
Denis Vlasenkob9256052007-09-28 10:29:17 +000028struct xid_item {
Denis Vlasenkod7b33502007-09-30 17:54:10 +000029 unsigned timestamp;
Denis Vlasenkob9256052007-09-28 10:29:17 +000030 int client;
Denis Vlasenko35d4da02007-01-22 14:04:27 +000031 uint32_t xid;
Denis Vlasenko736230e2006-11-20 19:40:36 +000032 struct sockaddr_in ip;
Denis Vlasenko736230e2006-11-20 19:40:36 +000033 struct xid_item *next;
Denys Vlasenkodf168912011-01-16 01:25:34 +010034} FIX_ALIASING;
Denis Vlasenko736230e2006-11-20 19:40:36 +000035
Denis Vlasenkob9256052007-09-28 10:29:17 +000036#define dhcprelay_xid_list (*(struct xid_item*)&bb_common_bufsiz1)
Denis Vlasenko736230e2006-11-20 19:40:36 +000037
Denis Vlasenko80edead2007-08-02 22:31:05 +000038static struct xid_item *xid_add(uint32_t xid, struct sockaddr_in *ip, int client)
Denis Vlasenko736230e2006-11-20 19:40:36 +000039{
40 struct xid_item *item;
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +000041
Denis Vlasenko736230e2006-11-20 19:40:36 +000042 /* create new xid entry */
43 item = xmalloc(sizeof(struct xid_item));
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +000044
Denis Vlasenko736230e2006-11-20 19:40:36 +000045 /* add xid entry */
46 item->ip = *ip;
47 item->xid = xid;
48 item->client = client;
Denis Vlasenkod7b33502007-09-30 17:54:10 +000049 item->timestamp = monotonic_sec();
Denis Vlasenko736230e2006-11-20 19:40:36 +000050 item->next = dhcprelay_xid_list.next;
51 dhcprelay_xid_list.next = item;
52
53 return item;
54}
55
Denis Vlasenko736230e2006-11-20 19:40:36 +000056static void xid_expire(void)
57{
58 struct xid_item *item = dhcprelay_xid_list.next;
59 struct xid_item *last = &dhcprelay_xid_list;
Denis Vlasenkod7b33502007-09-30 17:54:10 +000060 unsigned current_time = monotonic_sec();
Denis Vlasenko736230e2006-11-20 19:40:36 +000061
62 while (item != NULL) {
Denis Vlasenko80edead2007-08-02 22:31:05 +000063 if ((current_time - item->timestamp) > MAX_LIFETIME) {
Denis Vlasenko736230e2006-11-20 19:40:36 +000064 last->next = item->next;
65 free(item);
66 item = last->next;
67 } else {
68 last = item;
69 item = item->next;
70 }
71 }
72}
73
Denis Vlasenko80edead2007-08-02 22:31:05 +000074static struct xid_item *xid_find(uint32_t xid)
Denis Vlasenko736230e2006-11-20 19:40:36 +000075{
76 struct xid_item *item = dhcprelay_xid_list.next;
77 while (item != NULL) {
78 if (item->xid == xid) {
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +020079 break;
Denis Vlasenko736230e2006-11-20 19:40:36 +000080 }
81 item = item->next;
82 }
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +020083 return item;
Denis Vlasenko736230e2006-11-20 19:40:36 +000084}
85
Denis Vlasenko35d4da02007-01-22 14:04:27 +000086static void xid_del(uint32_t xid)
Denis Vlasenko736230e2006-11-20 19:40:36 +000087{
88 struct xid_item *item = dhcprelay_xid_list.next;
89 struct xid_item *last = &dhcprelay_xid_list;
90 while (item != NULL) {
91 if (item->xid == xid) {
92 last->next = item->next;
93 free(item);
94 item = last->next;
95 } else {
96 last = item;
97 item = item->next;
98 }
99 }
100}
101
Denis Vlasenko736230e2006-11-20 19:40:36 +0000102/**
103 * get_dhcp_packet_type - gets the message type of a dhcp packet
104 * p - pointer to the dhcp packet
105 * returns the message type on success, -1 otherwise
106 */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200107static int get_dhcp_packet_type(struct dhcp_packet *p)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000108{
Denis Vlasenko28703012006-12-19 20:32:02 +0000109 uint8_t *op;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000110
111 /* it must be either a BOOTREQUEST or a BOOTREPLY */
112 if (p->op != BOOTREQUEST && p->op != BOOTREPLY)
113 return -1;
114 /* get message type option */
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100115 op = udhcp_get_option(p, DHCP_MESSAGE_TYPE);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000116 if (op != NULL)
117 return op[0];
118 return -1;
119}
120
121/**
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200122 * make_iface_list - parses client/server interface names
Denis Vlasenko736230e2006-11-20 19:40:36 +0000123 * returns array
124 */
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200125static char **make_iface_list(char **client_and_server_ifaces, int *client_number)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000126{
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200127 char *s, **iface_list;
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000128 int i, cn;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000129
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200130 /* get number of items */
131 cn = 2; /* 1 server iface + at least 1 client one */
132 s = client_and_server_ifaces[0]; /* list of client ifaces */
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000133 while (*s) {
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200134 if (*s == ',')
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000135 cn++;
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000136 s++;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000137 }
Denis Vlasenko736230e2006-11-20 19:40:36 +0000138 *client_number = cn;
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000139
140 /* create vector of pointers */
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200141 iface_list = xzalloc(cn * sizeof(iface_list[0]));
142
143 iface_list[0] = client_and_server_ifaces[1]; /* server iface */
144
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000145 i = 1;
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200146 s = xstrdup(client_and_server_ifaces[0]); /* list of client ifaces */
147 goto store_client_iface_name;
148
149 while (i < cn) {
150 if (*s++ == ',') {
151 s[-1] = '\0';
152 store_client_iface_name:
153 iface_list[i++] = s;
154 }
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000155 }
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200156
157 return iface_list;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000158}
159
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000160/* Creates listen sockets (in fds) bound to client and server ifaces,
161 * and returns numerically max fd.
162 */
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200163static int init_sockets(char **iface_list, int num_clients, int *fds)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000164{
Denis Vlasenkob9256052007-09-28 10:29:17 +0000165 int i, n;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000166
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200167 n = 0;
168 for (i = 0; i < num_clients; i++) {
169 fds[i] = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT, iface_list[i]);
170 if (n < fds[i])
Denis Vlasenkob9256052007-09-28 10:29:17 +0000171 n = fds[i];
Denis Vlasenko736230e2006-11-20 19:40:36 +0000172 }
Denis Vlasenkob9256052007-09-28 10:29:17 +0000173 return n;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000174}
175
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200176static int sendto_ip4(int sock, const void *msg, int msg_len, struct sockaddr_in *to)
177{
178 int err;
179
180 errno = 0;
181 err = sendto(sock, msg, msg_len, 0, (struct sockaddr*) to, sizeof(*to));
182 err -= msg_len;
183 if (err)
184 bb_perror_msg("sendto");
185 return err;
186}
187
Denis Vlasenko736230e2006-11-20 19:40:36 +0000188/**
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000189 * pass_to_server() - forwards dhcp packets from client to server
Denis Vlasenko736230e2006-11-20 19:40:36 +0000190 * p - packet to send
191 * client - number of the client
192 */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200193static void pass_to_server(struct dhcp_packet *p, int packet_len, int client, int *fds,
Denis Vlasenko736230e2006-11-20 19:40:36 +0000194 struct sockaddr_in *client_addr, struct sockaddr_in *server_addr)
195{
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200196 int type;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000197
198 /* check packet_type */
199 type = get_dhcp_packet_type(p);
200 if (type != DHCPDISCOVER && type != DHCPREQUEST
201 && type != DHCPDECLINE && type != DHCPRELEASE
202 && type != DHCPINFORM
203 ) {
204 return;
205 }
206
207 /* create new xid entry */
Denys Vlasenko243d1752010-07-04 04:26:55 +0200208 xid_add(p->xid, client_addr, client);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000209
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200210 /* forward request to server */
211 /* note that we send from fds[0] which is bound to SERVER_PORT (67).
212 * IOW: we send _from_ SERVER_PORT! Although this may look strange,
213 * RFC 1542 not only allows, but prescribes this for BOOTP relays.
214 */
215 sendto_ip4(fds[0], p, packet_len, server_addr);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000216}
217
218/**
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000219 * pass_to_client() - forwards dhcp packets from server to client
Denis Vlasenko736230e2006-11-20 19:40:36 +0000220 * p - packet to send
221 */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200222static void pass_to_client(struct dhcp_packet *p, int packet_len, int *fds)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000223{
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200224 int type;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000225 struct xid_item *item;
226
227 /* check xid */
228 item = xid_find(p->xid);
229 if (!item) {
230 return;
231 }
232
233 /* check packet type */
234 type = get_dhcp_packet_type(p);
235 if (type != DHCPOFFER && type != DHCPACK && type != DHCPNAK) {
236 return;
237 }
238
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200239//TODO: also do it if (p->flags & htons(BROADCAST_FLAG)) is set!
Denis Vlasenko736230e2006-11-20 19:40:36 +0000240 if (item->ip.sin_addr.s_addr == htonl(INADDR_ANY))
241 item->ip.sin_addr.s_addr = htonl(INADDR_BROADCAST);
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200242
243 if (sendto_ip4(fds[item->client], p, packet_len, &item->ip) != 0) {
244 return; /* send error occurred */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000245 }
246
247 /* remove xid entry */
248 xid_del(p->xid);
249}
250
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000251int dhcprelay_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
252int dhcprelay_main(int argc, char **argv)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000253{
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000254 struct sockaddr_in server_addr;
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200255 char **iface_list;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000256 int *fds;
257 int num_sockets, max_socket;
Denys Vlasenko990a6172009-06-16 10:23:55 +0200258 uint32_t our_nip;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000259
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000260 server_addr.sin_family = AF_INET;
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200261 server_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000262 server_addr.sin_port = htons(SERVER_PORT);
263
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200264 /* dhcprelay CLIENT_IFACE1[,CLIENT_IFACE2...] SERVER_IFACE [SERVER_IP] */
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000265 if (argc == 4) {
266 if (!inet_aton(argv[3], &server_addr.sin_addr))
267 bb_perror_msg_and_die("bad server IP");
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200268 } else if (argc != 3) {
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000269 bb_show_usage();
270 }
271
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200272 iface_list = make_iface_list(argv + 1, &num_sockets);
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000273
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000274 fds = xmalloc(num_sockets * sizeof(fds[0]));
275
276 /* Create sockets and bind one to every iface */
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200277 max_socket = init_sockets(iface_list, num_sockets, fds);
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000278
279 /* Get our IP on server_iface */
Denys Vlasenko990a6172009-06-16 10:23:55 +0200280 if (udhcp_read_interface(argv[2], NULL, &our_nip, NULL))
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000281 return 1;
282
283 /* Main loop */
Denis Vlasenkob9256052007-09-28 10:29:17 +0000284 while (1) {
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200285// reinit stuff from time to time? go back to make_iface_list
286// every N minutes?
287 fd_set rfds;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000288 struct timeval tv;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000289 int i;
290
Denis Vlasenko736230e2006-11-20 19:40:36 +0000291 FD_ZERO(&rfds);
292 for (i = 0; i < num_sockets; i++)
293 FD_SET(fds[i], &rfds);
294 tv.tv_sec = SELECT_TIMEOUT;
295 tv.tv_usec = 0;
296 if (select(max_socket + 1, &rfds, NULL, NULL, &tv) > 0) {
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200297 int packlen;
298 struct dhcp_packet dhcp_msg;
299
Denis Vlasenko736230e2006-11-20 19:40:36 +0000300 /* server */
301 if (FD_ISSET(fds[0], &rfds)) {
Denis Vlasenko6de89942008-05-21 07:05:06 +0000302 packlen = udhcp_recv_kernel_packet(&dhcp_msg, fds[0]);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000303 if (packlen > 0) {
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000304 pass_to_client(&dhcp_msg, packlen, fds);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000305 }
306 }
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200307
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000308 /* clients */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000309 for (i = 1; i < num_sockets; i++) {
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200310 struct sockaddr_in client_addr;
311 socklen_t addr_size;
312
Denis Vlasenko736230e2006-11-20 19:40:36 +0000313 if (!FD_ISSET(fds[i], &rfds))
314 continue;
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200315
316 addr_size = sizeof(client_addr);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000317 packlen = recvfrom(fds[i], &dhcp_msg, sizeof(dhcp_msg), 0,
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000318 (struct sockaddr *)(&client_addr), &addr_size);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000319 if (packlen <= 0)
320 continue;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000321
322 /* Get our IP on corresponding client_iface */
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200323// RFC 1542
324// 4.1 General BOOTP Processing for Relay Agents
325// 4.1.1 BOOTREQUEST Messages
326// If the relay agent does decide to relay the request, it MUST examine
327// the 'giaddr' ("gateway" IP address) field. If this field is zero,
328// the relay agent MUST fill this field with the IP address of the
329// interface on which the request was received. If the interface has
330// more than one IP address logically associated with it, the relay
331// agent SHOULD choose one IP address associated with that interface and
332// use it consistently for all BOOTP messages it relays. If the
333// 'giaddr' field contains some non-zero value, the 'giaddr' field MUST
334// NOT be modified. The relay agent MUST NOT, under any circumstances,
335// fill the 'giaddr' field with a broadcast address as is suggested in
336// [1] (Section 8, sixth paragraph).
337
338// but why? what if server can't route such IP? Client ifaces may be, say, NATed!
339
340// 4.1.2 BOOTREPLY Messages
341// BOOTP relay agents relay BOOTREPLY messages only to BOOTP clients.
342// It is the responsibility of BOOTP servers to send BOOTREPLY messages
343// directly to the relay agent identified in the 'giaddr' field.
344// (yeah right, unless it is impossible... see comment above)
345// Therefore, a relay agent may assume that all BOOTREPLY messages it
346// receives are intended for BOOTP clients on its directly-connected
347// networks.
348//
349// When a relay agent receives a BOOTREPLY message, it should examine
350// the BOOTP 'giaddr', 'yiaddr', 'chaddr', 'htype', and 'hlen' fields.
351// These fields should provide adequate information for the relay agent
352// to deliver the BOOTREPLY message to the client.
353//
354// The 'giaddr' field can be used to identify the logical interface from
355// which the reply must be sent (i.e., the host or router interface
356// connected to the same network as the BOOTP client). If the content
357// of the 'giaddr' field does not match one of the relay agent's
358// directly-connected logical interfaces, the BOOTREPLY messsage MUST be
359// silently discarded.
360 if (udhcp_read_interface(iface_list[i], NULL, &dhcp_msg.gateway_nip, NULL)) {
361 /* Fall back to our IP on server iface */
362// this makes more sense!
Denys Vlasenko990a6172009-06-16 10:23:55 +0200363 dhcp_msg.gateway_nip = our_nip;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000364 }
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200365// maybe dhcp_msg.hops++? drop packets with too many hops (RFC 1542 says 4 or 16)?
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000366 pass_to_server(&dhcp_msg, packlen, i, fds, &client_addr, &server_addr);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000367 }
368 }
369 xid_expire();
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000370 } /* while (1) */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000371
Denis Vlasenkob9256052007-09-28 10:29:17 +0000372 /* return 0; - not reached */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000373}