blob: 86dcb1af0802ee4935d7550fae5c25f130420b9f [file] [log] [blame]
Denis Vlasenko736230e2006-11-20 19:40:36 +00001/* vi: set sw=4 ts=4: */
Denys Vlasenkoebe6d9d2017-10-05 14:40:24 +02002/*
3 * Port to Busybox Copyright (C) 2006 Jesse Dutton <jessedutton@gmail.com>
Denis Vlasenko736230e2006-11-20 19:40:36 +00004 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02005 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko736230e2006-11-20 19:40:36 +00006 *
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +00007 * DHCP Relay for 'DHCPv4 Configuration of IPSec Tunnel Mode' support
8 * Copyright (C) 2002 Mario Strasser <mast@gmx.net>,
Denis Vlasenko736230e2006-11-20 19:40:36 +00009 * Zuercher Hochschule Winterthur,
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +000010 * Netbeat AG
Denis Vlasenko736230e2006-11-20 19:40:36 +000011 * Upstream has GPL v2 or later
12 */
Denys Vlasenkof7683cd2016-11-23 18:54:59 +010013//applet:IF_DHCPRELAY(APPLET(dhcprelay, BB_DIR_USR_SBIN, BB_SUID_DROP))
14
15//kbuild:lib-$(CONFIG_DHCPRELAY) += dhcprelay.o
Pere Orga5bc8c002011-04-11 03:29:49 +020016
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
Denis Vlasenko736230e2006-11-20 19:40:36 +000022#include "common.h"
Denis Vlasenko736230e2006-11-20 19:40:36 +000023
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +020024#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)
Denis Vlasenko736230e2006-11-20 19:40:36 +000030
Denis Vlasenko736230e2006-11-20 19:40:36 +000031/* This list holds information about clients. The xid_* functions manipulate this list. */
Denis Vlasenkob9256052007-09-28 10:29:17 +000032struct xid_item {
Denis Vlasenkod7b33502007-09-30 17:54:10 +000033 unsigned timestamp;
Denis Vlasenkob9256052007-09-28 10:29:17 +000034 int client;
Denis Vlasenko35d4da02007-01-22 14:04:27 +000035 uint32_t xid;
Denis Vlasenko736230e2006-11-20 19:40:36 +000036 struct sockaddr_in ip;
Denis Vlasenko736230e2006-11-20 19:40:36 +000037 struct xid_item *next;
Denys Vlasenkodf168912011-01-16 01:25:34 +010038} FIX_ALIASING;
Denis Vlasenko736230e2006-11-20 19:40:36 +000039
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020040#define dhcprelay_xid_list (*(struct xid_item*)bb_common_bufsiz1)
Denys Vlasenko47cfbf32016-04-21 18:18:48 +020041#define INIT_G() do { setup_common_bufsiz(); } while (0)
Denis Vlasenko736230e2006-11-20 19:40:36 +000042
Denis Vlasenko80edead2007-08-02 22:31:05 +000043static struct xid_item *xid_add(uint32_t xid, struct sockaddr_in *ip, int client)
Denis Vlasenko736230e2006-11-20 19:40:36 +000044{
45 struct xid_item *item;
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +000046
Denis Vlasenko736230e2006-11-20 19:40:36 +000047 /* create new xid entry */
48 item = xmalloc(sizeof(struct xid_item));
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +000049
Denis Vlasenko736230e2006-11-20 19:40:36 +000050 /* add xid entry */
51 item->ip = *ip;
52 item->xid = xid;
53 item->client = client;
Denis Vlasenkod7b33502007-09-30 17:54:10 +000054 item->timestamp = monotonic_sec();
Denis Vlasenko736230e2006-11-20 19:40:36 +000055 item->next = dhcprelay_xid_list.next;
56 dhcprelay_xid_list.next = item;
57
58 return item;
59}
60
Denis Vlasenko736230e2006-11-20 19:40:36 +000061static void xid_expire(void)
62{
63 struct xid_item *item = dhcprelay_xid_list.next;
64 struct xid_item *last = &dhcprelay_xid_list;
Denis Vlasenkod7b33502007-09-30 17:54:10 +000065 unsigned current_time = monotonic_sec();
Denis Vlasenko736230e2006-11-20 19:40:36 +000066
67 while (item != NULL) {
Denis Vlasenko80edead2007-08-02 22:31:05 +000068 if ((current_time - item->timestamp) > MAX_LIFETIME) {
Denis Vlasenko736230e2006-11-20 19:40:36 +000069 last->next = item->next;
70 free(item);
71 item = last->next;
72 } else {
73 last = item;
74 item = item->next;
75 }
76 }
77}
78
Denis Vlasenko80edead2007-08-02 22:31:05 +000079static struct xid_item *xid_find(uint32_t xid)
Denis Vlasenko736230e2006-11-20 19:40:36 +000080{
81 struct xid_item *item = dhcprelay_xid_list.next;
82 while (item != NULL) {
83 if (item->xid == xid) {
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +020084 break;
Denis Vlasenko736230e2006-11-20 19:40:36 +000085 }
86 item = item->next;
87 }
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +020088 return item;
Denis Vlasenko736230e2006-11-20 19:40:36 +000089}
90
Denis Vlasenko35d4da02007-01-22 14:04:27 +000091static void xid_del(uint32_t xid)
Denis Vlasenko736230e2006-11-20 19:40:36 +000092{
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
Denis Vlasenko736230e2006-11-20 19:40:36 +0000107/**
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 */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200112static int get_dhcp_packet_type(struct dhcp_packet *p)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000113{
Denis Vlasenko28703012006-12-19 20:32:02 +0000114 uint8_t *op;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000115
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 */
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100120 op = udhcp_get_option(p, DHCP_MESSAGE_TYPE);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000121 if (op != NULL)
122 return op[0];
123 return -1;
124}
125
126/**
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200127 * make_iface_list - parses client/server interface names
Denis Vlasenko736230e2006-11-20 19:40:36 +0000128 * returns array
129 */
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200130static char **make_iface_list(char **client_and_server_ifaces, int *client_number)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000131{
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200132 char *s, **iface_list;
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000133 int i, cn;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000134
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200135 /* 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 */
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000138 while (*s) {
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200139 if (*s == ',')
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000140 cn++;
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000141 s++;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000142 }
Denis Vlasenko736230e2006-11-20 19:40:36 +0000143 *client_number = cn;
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000144
145 /* create vector of pointers */
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200146 iface_list = xzalloc(cn * sizeof(iface_list[0]));
147
148 iface_list[0] = client_and_server_ifaces[1]; /* server iface */
149
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000150 i = 1;
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200151 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 }
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000160 }
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200161
162 return iface_list;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000163}
164
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000165/* Creates listen sockets (in fds) bound to client and server ifaces,
166 * and returns numerically max fd.
167 */
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200168static int init_sockets(char **iface_list, int num_clients, int *fds)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000169{
Denis Vlasenkob9256052007-09-28 10:29:17 +0000170 int i, n;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000171
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200172 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])
Denis Vlasenkob9256052007-09-28 10:29:17 +0000176 n = fds[i];
Denis Vlasenko736230e2006-11-20 19:40:36 +0000177 }
Denis Vlasenkob9256052007-09-28 10:29:17 +0000178 return n;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000179}
180
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200181static 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
Denis Vlasenko736230e2006-11-20 19:40:36 +0000193/**
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000194 * pass_to_server() - forwards dhcp packets from client to server
Denis Vlasenko736230e2006-11-20 19:40:36 +0000195 * p - packet to send
196 * client - number of the client
197 */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200198static void pass_to_server(struct dhcp_packet *p, int packet_len, int client, int *fds,
Denis Vlasenko736230e2006-11-20 19:40:36 +0000199 struct sockaddr_in *client_addr, struct sockaddr_in *server_addr)
200{
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200201 int type;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000202
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 */
Denys Vlasenko243d1752010-07-04 04:26:55 +0200213 xid_add(p->xid, client_addr, client);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000214
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200215 /* 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);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000221}
222
223/**
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000224 * pass_to_client() - forwards dhcp packets from server to client
Denis Vlasenko736230e2006-11-20 19:40:36 +0000225 * p - packet to send
226 */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200227static void pass_to_client(struct dhcp_packet *p, int packet_len, int *fds)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000228{
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200229 int type;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000230 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
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200244//TODO: also do it if (p->flags & htons(BROADCAST_FLAG)) is set!
Denis Vlasenko736230e2006-11-20 19:40:36 +0000245 if (item->ip.sin_addr.s_addr == htonl(INADDR_ANY))
246 item->ip.sin_addr.s_addr = htonl(INADDR_BROADCAST);
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200247
248 if (sendto_ip4(fds[item->client], p, packet_len, &item->ip) != 0) {
249 return; /* send error occurred */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000250 }
251
252 /* remove xid entry */
253 xid_del(p->xid);
254}
255
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000256int dhcprelay_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko562f63e2018-01-08 10:14:17 +0100257int dhcprelay_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000258{
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000259 struct sockaddr_in server_addr;
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200260 char **iface_list;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000261 int *fds;
262 int num_sockets, max_socket;
Denys Vlasenko990a6172009-06-16 10:23:55 +0200263 uint32_t our_nip;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000264
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200265 INIT_G();
266
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000267 server_addr.sin_family = AF_INET;
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200268 server_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000269 server_addr.sin_port = htons(SERVER_PORT);
270
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200271 /* dhcprelay CLIENT_IFACE1[,CLIENT_IFACE2...] SERVER_IFACE [SERVER_IP] */
Denys Vlasenko562f63e2018-01-08 10:14:17 +0100272 if (!argv[1] || !argv[2])
273 bb_show_usage();
274 if (argv[3]) {
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000275 if (!inet_aton(argv[3], &server_addr.sin_addr))
276 bb_perror_msg_and_die("bad server IP");
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000277 }
278
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200279 iface_list = make_iface_list(argv + 1, &num_sockets);
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000280
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000281 fds = xmalloc(num_sockets * sizeof(fds[0]));
282
283 /* Create sockets and bind one to every iface */
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200284 max_socket = init_sockets(iface_list, num_sockets, fds);
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000285
286 /* Get our IP on server_iface */
Denys Vlasenko990a6172009-06-16 10:23:55 +0200287 if (udhcp_read_interface(argv[2], NULL, &our_nip, NULL))
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000288 return 1;
289
290 /* Main loop */
Denis Vlasenkob9256052007-09-28 10:29:17 +0000291 while (1) {
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200292// reinit stuff from time to time? go back to make_iface_list
293// every N minutes?
294 fd_set rfds;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000295 struct timeval tv;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000296 int i;
297
Denis Vlasenko736230e2006-11-20 19:40:36 +0000298 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) {
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200304 int packlen;
305 struct dhcp_packet dhcp_msg;
306
Denis Vlasenko736230e2006-11-20 19:40:36 +0000307 /* server */
308 if (FD_ISSET(fds[0], &rfds)) {
Denis Vlasenko6de89942008-05-21 07:05:06 +0000309 packlen = udhcp_recv_kernel_packet(&dhcp_msg, fds[0]);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000310 if (packlen > 0) {
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000311 pass_to_client(&dhcp_msg, packlen, fds);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000312 }
313 }
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200314
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000315 /* clients */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000316 for (i = 1; i < num_sockets; i++) {
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200317 struct sockaddr_in client_addr;
318 socklen_t addr_size;
319
Denis Vlasenko736230e2006-11-20 19:40:36 +0000320 if (!FD_ISSET(fds[i], &rfds))
321 continue;
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200322
323 addr_size = sizeof(client_addr);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000324 packlen = recvfrom(fds[i], &dhcp_msg, sizeof(dhcp_msg), 0,
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000325 (struct sockaddr *)(&client_addr), &addr_size);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000326 if (packlen <= 0)
327 continue;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000328
329 /* Get our IP on corresponding client_iface */
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200330// 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
Denys Vlasenko10ad6222017-04-17 16:13:32 +0200365// directly-connected logical interfaces, the BOOTREPLY message MUST be
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200366// 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!
Denys Vlasenko990a6172009-06-16 10:23:55 +0200370 dhcp_msg.gateway_nip = our_nip;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000371 }
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200372// maybe dhcp_msg.hops++? drop packets with too many hops (RFC 1542 says 4 or 16)?
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000373 pass_to_server(&dhcp_msg, packlen, i, fds, &client_addr, &server_addr);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000374 }
375 }
376 xid_expire();
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000377 } /* while (1) */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000378
Denis Vlasenkob9256052007-09-28 10:29:17 +0000379 /* return 0; - not reached */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000380}