blob: ea84c0dd7d438ccc03d818a474895661dac418ba [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 */
Denys Vlasenkof7683cd2016-11-23 18:54:59 +010012//applet:IF_DHCPRELAY(APPLET(dhcprelay, BB_DIR_USR_SBIN, BB_SUID_DROP))
13
14//kbuild:lib-$(CONFIG_DHCPRELAY) += dhcprelay.o
Pere Orga5bc8c002011-04-11 03:29:49 +020015
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
Denis Vlasenko736230e2006-11-20 19:40:36 +000021#include "common.h"
Denis Vlasenko736230e2006-11-20 19:40:36 +000022
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +020023#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)
Denis Vlasenko736230e2006-11-20 19:40:36 +000029
Denis Vlasenko736230e2006-11-20 19:40:36 +000030/* This list holds information about clients. The xid_* functions manipulate this list. */
Denis Vlasenkob9256052007-09-28 10:29:17 +000031struct xid_item {
Denis Vlasenkod7b33502007-09-30 17:54:10 +000032 unsigned timestamp;
Denis Vlasenkob9256052007-09-28 10:29:17 +000033 int client;
Denis Vlasenko35d4da02007-01-22 14:04:27 +000034 uint32_t xid;
Denis Vlasenko736230e2006-11-20 19:40:36 +000035 struct sockaddr_in ip;
Denis Vlasenko736230e2006-11-20 19:40:36 +000036 struct xid_item *next;
Denys Vlasenkodf168912011-01-16 01:25:34 +010037} FIX_ALIASING;
Denis Vlasenko736230e2006-11-20 19:40:36 +000038
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020039#define dhcprelay_xid_list (*(struct xid_item*)bb_common_bufsiz1)
Denys Vlasenko47cfbf32016-04-21 18:18:48 +020040#define INIT_G() do { setup_common_bufsiz(); } while (0)
Denis Vlasenko736230e2006-11-20 19:40:36 +000041
Denis Vlasenko80edead2007-08-02 22:31:05 +000042static struct xid_item *xid_add(uint32_t xid, struct sockaddr_in *ip, int client)
Denis Vlasenko736230e2006-11-20 19:40:36 +000043{
44 struct xid_item *item;
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +000045
Denis Vlasenko736230e2006-11-20 19:40:36 +000046 /* create new xid entry */
47 item = xmalloc(sizeof(struct xid_item));
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +000048
Denis Vlasenko736230e2006-11-20 19:40:36 +000049 /* add xid entry */
50 item->ip = *ip;
51 item->xid = xid;
52 item->client = client;
Denis Vlasenkod7b33502007-09-30 17:54:10 +000053 item->timestamp = monotonic_sec();
Denis Vlasenko736230e2006-11-20 19:40:36 +000054 item->next = dhcprelay_xid_list.next;
55 dhcprelay_xid_list.next = item;
56
57 return item;
58}
59
Denis Vlasenko736230e2006-11-20 19:40:36 +000060static void xid_expire(void)
61{
62 struct xid_item *item = dhcprelay_xid_list.next;
63 struct xid_item *last = &dhcprelay_xid_list;
Denis Vlasenkod7b33502007-09-30 17:54:10 +000064 unsigned current_time = monotonic_sec();
Denis Vlasenko736230e2006-11-20 19:40:36 +000065
66 while (item != NULL) {
Denis Vlasenko80edead2007-08-02 22:31:05 +000067 if ((current_time - item->timestamp) > MAX_LIFETIME) {
Denis Vlasenko736230e2006-11-20 19:40:36 +000068 last->next = item->next;
69 free(item);
70 item = last->next;
71 } else {
72 last = item;
73 item = item->next;
74 }
75 }
76}
77
Denis Vlasenko80edead2007-08-02 22:31:05 +000078static struct xid_item *xid_find(uint32_t xid)
Denis Vlasenko736230e2006-11-20 19:40:36 +000079{
80 struct xid_item *item = dhcprelay_xid_list.next;
81 while (item != NULL) {
82 if (item->xid == xid) {
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +020083 break;
Denis Vlasenko736230e2006-11-20 19:40:36 +000084 }
85 item = item->next;
86 }
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +020087 return item;
Denis Vlasenko736230e2006-11-20 19:40:36 +000088}
89
Denis Vlasenko35d4da02007-01-22 14:04:27 +000090static void xid_del(uint32_t xid)
Denis Vlasenko736230e2006-11-20 19:40:36 +000091{
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
Denis Vlasenko736230e2006-11-20 19:40:36 +0000106/**
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 */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200111static int get_dhcp_packet_type(struct dhcp_packet *p)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000112{
Denis Vlasenko28703012006-12-19 20:32:02 +0000113 uint8_t *op;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000114
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 */
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100119 op = udhcp_get_option(p, DHCP_MESSAGE_TYPE);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000120 if (op != NULL)
121 return op[0];
122 return -1;
123}
124
125/**
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200126 * make_iface_list - parses client/server interface names
Denis Vlasenko736230e2006-11-20 19:40:36 +0000127 * returns array
128 */
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200129static char **make_iface_list(char **client_and_server_ifaces, int *client_number)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000130{
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200131 char *s, **iface_list;
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000132 int i, cn;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000133
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200134 /* 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 */
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000137 while (*s) {
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200138 if (*s == ',')
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000139 cn++;
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000140 s++;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000141 }
Denis Vlasenko736230e2006-11-20 19:40:36 +0000142 *client_number = cn;
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000143
144 /* create vector of pointers */
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200145 iface_list = xzalloc(cn * sizeof(iface_list[0]));
146
147 iface_list[0] = client_and_server_ifaces[1]; /* server iface */
148
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000149 i = 1;
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200150 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 }
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000159 }
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200160
161 return iface_list;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000162}
163
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000164/* Creates listen sockets (in fds) bound to client and server ifaces,
165 * and returns numerically max fd.
166 */
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200167static int init_sockets(char **iface_list, int num_clients, int *fds)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000168{
Denis Vlasenkob9256052007-09-28 10:29:17 +0000169 int i, n;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000170
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200171 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])
Denis Vlasenkob9256052007-09-28 10:29:17 +0000175 n = fds[i];
Denis Vlasenko736230e2006-11-20 19:40:36 +0000176 }
Denis Vlasenkob9256052007-09-28 10:29:17 +0000177 return n;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000178}
179
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200180static 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
Denis Vlasenko736230e2006-11-20 19:40:36 +0000192/**
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000193 * pass_to_server() - forwards dhcp packets from client to server
Denis Vlasenko736230e2006-11-20 19:40:36 +0000194 * p - packet to send
195 * client - number of the client
196 */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200197static void pass_to_server(struct dhcp_packet *p, int packet_len, int client, int *fds,
Denis Vlasenko736230e2006-11-20 19:40:36 +0000198 struct sockaddr_in *client_addr, struct sockaddr_in *server_addr)
199{
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200200 int type;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000201
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 */
Denys Vlasenko243d1752010-07-04 04:26:55 +0200212 xid_add(p->xid, client_addr, client);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000213
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200214 /* 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);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000220}
221
222/**
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000223 * pass_to_client() - forwards dhcp packets from server to client
Denis Vlasenko736230e2006-11-20 19:40:36 +0000224 * p - packet to send
225 */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200226static void pass_to_client(struct dhcp_packet *p, int packet_len, int *fds)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000227{
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200228 int type;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000229 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
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200243//TODO: also do it if (p->flags & htons(BROADCAST_FLAG)) is set!
Denis Vlasenko736230e2006-11-20 19:40:36 +0000244 if (item->ip.sin_addr.s_addr == htonl(INADDR_ANY))
245 item->ip.sin_addr.s_addr = htonl(INADDR_BROADCAST);
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200246
247 if (sendto_ip4(fds[item->client], p, packet_len, &item->ip) != 0) {
248 return; /* send error occurred */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000249 }
250
251 /* remove xid entry */
252 xid_del(p->xid);
253}
254
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000255int dhcprelay_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
256int dhcprelay_main(int argc, char **argv)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000257{
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000258 struct sockaddr_in server_addr;
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200259 char **iface_list;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000260 int *fds;
261 int num_sockets, max_socket;
Denys Vlasenko990a6172009-06-16 10:23:55 +0200262 uint32_t our_nip;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000263
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200264 INIT_G();
265
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000266 server_addr.sin_family = AF_INET;
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200267 server_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000268 server_addr.sin_port = htons(SERVER_PORT);
269
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200270 /* dhcprelay CLIENT_IFACE1[,CLIENT_IFACE2...] SERVER_IFACE [SERVER_IP] */
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000271 if (argc == 4) {
272 if (!inet_aton(argv[3], &server_addr.sin_addr))
273 bb_perror_msg_and_die("bad server IP");
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200274 } else if (argc != 3) {
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000275 bb_show_usage();
276 }
277
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200278 iface_list = make_iface_list(argv + 1, &num_sockets);
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000279
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000280 fds = xmalloc(num_sockets * sizeof(fds[0]));
281
282 /* Create sockets and bind one to every iface */
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200283 max_socket = init_sockets(iface_list, num_sockets, fds);
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000284
285 /* Get our IP on server_iface */
Denys Vlasenko990a6172009-06-16 10:23:55 +0200286 if (udhcp_read_interface(argv[2], NULL, &our_nip, NULL))
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000287 return 1;
288
289 /* Main loop */
Denis Vlasenkob9256052007-09-28 10:29:17 +0000290 while (1) {
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200291// reinit stuff from time to time? go back to make_iface_list
292// every N minutes?
293 fd_set rfds;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000294 struct timeval tv;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000295 int i;
296
Denis Vlasenko736230e2006-11-20 19:40:36 +0000297 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) {
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200303 int packlen;
304 struct dhcp_packet dhcp_msg;
305
Denis Vlasenko736230e2006-11-20 19:40:36 +0000306 /* server */
307 if (FD_ISSET(fds[0], &rfds)) {
Denis Vlasenko6de89942008-05-21 07:05:06 +0000308 packlen = udhcp_recv_kernel_packet(&dhcp_msg, fds[0]);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000309 if (packlen > 0) {
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000310 pass_to_client(&dhcp_msg, packlen, fds);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000311 }
312 }
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200313
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000314 /* clients */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000315 for (i = 1; i < num_sockets; i++) {
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200316 struct sockaddr_in client_addr;
317 socklen_t addr_size;
318
Denis Vlasenko736230e2006-11-20 19:40:36 +0000319 if (!FD_ISSET(fds[i], &rfds))
320 continue;
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200321
322 addr_size = sizeof(client_addr);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000323 packlen = recvfrom(fds[i], &dhcp_msg, sizeof(dhcp_msg), 0,
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000324 (struct sockaddr *)(&client_addr), &addr_size);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000325 if (packlen <= 0)
326 continue;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000327
328 /* Get our IP on corresponding client_iface */
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200329// 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
Denys Vlasenko10ad6222017-04-17 16:13:32 +0200364// directly-connected logical interfaces, the BOOTREPLY message MUST be
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200365// 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!
Denys Vlasenko990a6172009-06-16 10:23:55 +0200369 dhcp_msg.gateway_nip = our_nip;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000370 }
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200371// maybe dhcp_msg.hops++? drop packets with too many hops (RFC 1542 says 4 or 16)?
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000372 pass_to_server(&dhcp_msg, packlen, i, fds, &client_addr, &server_addr);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000373 }
374 }
375 xid_expire();
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000376 } /* while (1) */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000377
Denis Vlasenkob9256052007-09-28 10:29:17 +0000378 /* return 0; - not reached */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000379}