blob: 53540d1ab9d8a6472d45e5bfbffb146d258cd7a5 [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 *
4 * Licensed under GPL v2, see file LICENSE in this tarball for details.
5 *
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 */
12
13#include "common.h"
Denis Vlasenko736230e2006-11-20 19:40:36 +000014#include "options.h"
15
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +000016#define SERVER_PORT 67
Denis Vlasenkod7b33502007-09-30 17:54:10 +000017#define SELECT_TIMEOUT 5 /* select timeout in sec. */
18#define MAX_LIFETIME 2*60 /* lifetime of an xid entry in sec. */
Denis Vlasenko736230e2006-11-20 19:40:36 +000019
Denis Vlasenko736230e2006-11-20 19:40:36 +000020/* This list holds information about clients. The xid_* functions manipulate this list. */
Denis Vlasenkob9256052007-09-28 10:29:17 +000021struct xid_item {
Denis Vlasenkod7b33502007-09-30 17:54:10 +000022 unsigned timestamp;
Denis Vlasenkob9256052007-09-28 10:29:17 +000023 int client;
Denis Vlasenko35d4da02007-01-22 14:04:27 +000024 uint32_t xid;
Denis Vlasenko736230e2006-11-20 19:40:36 +000025 struct sockaddr_in ip;
Denis Vlasenko736230e2006-11-20 19:40:36 +000026 struct xid_item *next;
Denis Vlasenkob9256052007-09-28 10:29:17 +000027};
Denis Vlasenko736230e2006-11-20 19:40:36 +000028
Denis Vlasenkob9256052007-09-28 10:29:17 +000029#define dhcprelay_xid_list (*(struct xid_item*)&bb_common_bufsiz1)
Denis Vlasenko736230e2006-11-20 19:40:36 +000030
Denis Vlasenko80edead2007-08-02 22:31:05 +000031static struct xid_item *xid_add(uint32_t xid, struct sockaddr_in *ip, int client)
Denis Vlasenko736230e2006-11-20 19:40:36 +000032{
33 struct xid_item *item;
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +000034
Denis Vlasenko736230e2006-11-20 19:40:36 +000035 /* create new xid entry */
36 item = xmalloc(sizeof(struct xid_item));
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +000037
Denis Vlasenko736230e2006-11-20 19:40:36 +000038 /* add xid entry */
39 item->ip = *ip;
40 item->xid = xid;
41 item->client = client;
Denis Vlasenkod7b33502007-09-30 17:54:10 +000042 item->timestamp = monotonic_sec();
Denis Vlasenko736230e2006-11-20 19:40:36 +000043 item->next = dhcprelay_xid_list.next;
44 dhcprelay_xid_list.next = item;
45
46 return item;
47}
48
Denis Vlasenko736230e2006-11-20 19:40:36 +000049static void xid_expire(void)
50{
51 struct xid_item *item = dhcprelay_xid_list.next;
52 struct xid_item *last = &dhcprelay_xid_list;
Denis Vlasenkod7b33502007-09-30 17:54:10 +000053 unsigned current_time = monotonic_sec();
Denis Vlasenko736230e2006-11-20 19:40:36 +000054
55 while (item != NULL) {
Denis Vlasenko80edead2007-08-02 22:31:05 +000056 if ((current_time - item->timestamp) > MAX_LIFETIME) {
Denis Vlasenko736230e2006-11-20 19:40:36 +000057 last->next = item->next;
58 free(item);
59 item = last->next;
60 } else {
61 last = item;
62 item = item->next;
63 }
64 }
65}
66
Denis Vlasenko80edead2007-08-02 22:31:05 +000067static struct xid_item *xid_find(uint32_t xid)
Denis Vlasenko736230e2006-11-20 19:40:36 +000068{
69 struct xid_item *item = dhcprelay_xid_list.next;
70 while (item != NULL) {
71 if (item->xid == xid) {
72 return item;
73 }
74 item = item->next;
75 }
76 return NULL;
77}
78
Denis Vlasenko35d4da02007-01-22 14:04:27 +000079static void xid_del(uint32_t xid)
Denis Vlasenko736230e2006-11-20 19:40:36 +000080{
81 struct xid_item *item = dhcprelay_xid_list.next;
82 struct xid_item *last = &dhcprelay_xid_list;
83 while (item != NULL) {
84 if (item->xid == xid) {
85 last->next = item->next;
86 free(item);
87 item = last->next;
88 } else {
89 last = item;
90 item = item->next;
91 }
92 }
93}
94
Denis Vlasenko736230e2006-11-20 19:40:36 +000095/**
96 * get_dhcp_packet_type - gets the message type of a dhcp packet
97 * p - pointer to the dhcp packet
98 * returns the message type on success, -1 otherwise
99 */
100static int get_dhcp_packet_type(struct dhcpMessage *p)
101{
Denis Vlasenko28703012006-12-19 20:32:02 +0000102 uint8_t *op;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000103
104 /* it must be either a BOOTREQUEST or a BOOTREPLY */
105 if (p->op != BOOTREQUEST && p->op != BOOTREPLY)
106 return -1;
107 /* get message type option */
108 op = get_option(p, DHCP_MESSAGE_TYPE);
109 if (op != NULL)
110 return op[0];
111 return -1;
112}
113
114/**
Denis Vlasenko736230e2006-11-20 19:40:36 +0000115 * get_client_devices - parses the devices list
116 * dev_list - comma separated list of devices
117 * returns array
118 */
Denis Vlasenko80edead2007-08-02 22:31:05 +0000119static char **get_client_devices(char *dev_list, int *client_number)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000120{
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000121 char *s, **client_dev;
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000122 int i, cn;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000123
124 /* copy list */
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000125 dev_list = xstrdup(dev_list);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000126
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000127 /* get number of items, replace ',' with NULs */
128 s = dev_list;
129 cn = 1;
130 while (*s) {
131 if (*s == ',') {
132 *s = '\0';
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000133 cn++;
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000134 }
135 s++;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000136 }
Denis Vlasenko736230e2006-11-20 19:40:36 +0000137 *client_number = cn;
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000138
139 /* create vector of pointers */
140 client_dev = xzalloc(cn * sizeof(*client_dev));
141 client_dev[0] = dev_list;
142 i = 1;
143 while (i != cn) {
144 client_dev[i] = client_dev[i - 1] + strlen(client_dev[i - 1]) + 1;
145 i++;
146 }
Denis Vlasenko736230e2006-11-20 19:40:36 +0000147 return client_dev;
148}
149
150
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000151/* Creates listen sockets (in fds) bound to client and server ifaces,
152 * and returns numerically max fd.
153 */
154static int init_sockets(char **client_ifaces, int num_clients,
155 char *server_iface, int *fds)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000156{
Denis Vlasenkob9256052007-09-28 10:29:17 +0000157 int i, n;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000158
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000159 /* talk to real server on bootps */
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000160 fds[0] = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT, server_iface);
Denis Vlasenkob9256052007-09-28 10:29:17 +0000161 n = fds[0];
Denis Vlasenko736230e2006-11-20 19:40:36 +0000162
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000163 for (i = 1; i < num_clients; i++) {
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000164 /* listen for clients on bootps */
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000165 fds[i] = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT, client_ifaces[i-1]);
Denis Vlasenkob9256052007-09-28 10:29:17 +0000166 if (fds[i] > n)
167 n = fds[i];
Denis Vlasenko736230e2006-11-20 19:40:36 +0000168 }
Denis Vlasenkob9256052007-09-28 10:29:17 +0000169 return n;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000170}
171
172
173/**
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000174 * pass_to_server() - forwards dhcp packets from client to server
Denis Vlasenko736230e2006-11-20 19:40:36 +0000175 * p - packet to send
176 * client - number of the client
177 */
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000178static void pass_to_server(struct dhcpMessage *p, int packet_len, int client, int *fds,
Denis Vlasenko736230e2006-11-20 19:40:36 +0000179 struct sockaddr_in *client_addr, struct sockaddr_in *server_addr)
180{
181 int res, type;
182 struct xid_item *item;
183
184 /* check packet_type */
185 type = get_dhcp_packet_type(p);
186 if (type != DHCPDISCOVER && type != DHCPREQUEST
187 && type != DHCPDECLINE && type != DHCPRELEASE
188 && type != DHCPINFORM
189 ) {
190 return;
191 }
192
193 /* create new xid entry */
194 item = xid_add(p->xid, client_addr, client);
195
196 /* forward request to LAN (server) */
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000197 errno = 0;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000198 res = sendto(fds[0], p, packet_len, 0, (struct sockaddr*)server_addr,
199 sizeof(struct sockaddr_in));
200 if (res != packet_len) {
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000201 bb_perror_msg("sendto");
Denis Vlasenko736230e2006-11-20 19:40:36 +0000202 }
203}
204
205/**
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000206 * pass_to_client() - forwards dhcp packets from server to client
Denis Vlasenko736230e2006-11-20 19:40:36 +0000207 * p - packet to send
208 */
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000209static void pass_to_client(struct dhcpMessage *p, int packet_len, int *fds)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000210{
211 int res, type;
212 struct xid_item *item;
213
214 /* check xid */
215 item = xid_find(p->xid);
216 if (!item) {
217 return;
218 }
219
220 /* check packet type */
221 type = get_dhcp_packet_type(p);
222 if (type != DHCPOFFER && type != DHCPACK && type != DHCPNAK) {
223 return;
224 }
225
226 if (item->ip.sin_addr.s_addr == htonl(INADDR_ANY))
227 item->ip.sin_addr.s_addr = htonl(INADDR_BROADCAST);
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000228 errno = 0;
229 res = sendto(fds[item->client], p, packet_len, 0, (struct sockaddr*) &(item->ip),
230 sizeof(item->ip));
Denis Vlasenko736230e2006-11-20 19:40:36 +0000231 if (res != packet_len) {
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000232 bb_perror_msg("sendto");
Denis Vlasenko736230e2006-11-20 19:40:36 +0000233 return;
234 }
235
236 /* remove xid entry */
237 xid_del(p->xid);
238}
239
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000240int dhcprelay_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
241int dhcprelay_main(int argc, char **argv)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000242{
243 struct dhcpMessage dhcp_msg;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000244 struct sockaddr_in server_addr;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000245 struct sockaddr_in client_addr;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000246 fd_set rfds;
247 char **client_ifaces;
248 int *fds;
249 int num_sockets, max_socket;
250 uint32_t our_ip;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000251
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000252 server_addr.sin_family = AF_INET;
253 server_addr.sin_port = htons(SERVER_PORT);
254
255 /* dhcprelay client_iface1,client_iface2,... server_iface [server_IP] */
256 if (argc == 4) {
257 if (!inet_aton(argv[3], &server_addr.sin_addr))
258 bb_perror_msg_and_die("bad server IP");
259 } else if (argc == 3) {
260 server_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
261 } else {
262 bb_show_usage();
263 }
264
265 /* Produce list of client ifaces */
266 client_ifaces = get_client_devices(argv[1], &num_sockets);
267
268 num_sockets++; /* for server socket at fds[0] */
269 fds = xmalloc(num_sockets * sizeof(fds[0]));
270
271 /* Create sockets and bind one to every iface */
272 max_socket = init_sockets(client_ifaces, num_sockets, argv[2], fds);
273
274 /* Get our IP on server_iface */
275 if (udhcp_read_interface(argv[2], NULL, &our_ip, NULL))
276 return 1;
277
278 /* Main loop */
Denis Vlasenkob9256052007-09-28 10:29:17 +0000279 while (1) {
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000280//reinit stuff from time to time? go back to get_client_devices
281//every N minutes?
282 struct timeval tv;
283 size_t packlen;
284 socklen_t addr_size;
285 int i;
286
Denis Vlasenko736230e2006-11-20 19:40:36 +0000287 FD_ZERO(&rfds);
288 for (i = 0; i < num_sockets; i++)
289 FD_SET(fds[i], &rfds);
290 tv.tv_sec = SELECT_TIMEOUT;
291 tv.tv_usec = 0;
292 if (select(max_socket + 1, &rfds, NULL, NULL, &tv) > 0) {
293 /* server */
294 if (FD_ISSET(fds[0], &rfds)) {
Denis Vlasenko6de89942008-05-21 07:05:06 +0000295 packlen = udhcp_recv_kernel_packet(&dhcp_msg, fds[0]);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000296 if (packlen > 0) {
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000297 pass_to_client(&dhcp_msg, packlen, fds);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000298 }
299 }
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000300 /* clients */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000301 for (i = 1; i < num_sockets; i++) {
Denis Vlasenko736230e2006-11-20 19:40:36 +0000302 if (!FD_ISSET(fds[i], &rfds))
303 continue;
304 addr_size = sizeof(struct sockaddr_in);
305 packlen = recvfrom(fds[i], &dhcp_msg, sizeof(dhcp_msg), 0,
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000306 (struct sockaddr *)(&client_addr), &addr_size);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000307 if (packlen <= 0)
308 continue;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000309
310 /* Get our IP on corresponding client_iface */
311//why? what if server can't route such IP?
312 if (udhcp_read_interface(client_ifaces[i-1], NULL, &dhcp_msg.giaddr, NULL)) {
313 /* Fall back to our server_iface's IP */
314//this makes more sense!
315 dhcp_msg.giaddr = our_ip;
316 }
317//maybe set dhcp_msg.flags |= BROADCAST_FLAG too?
318 pass_to_server(&dhcp_msg, packlen, i, fds, &client_addr, &server_addr);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000319 }
320 }
321 xid_expire();
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000322 } /* while (1) */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000323
Denis Vlasenkob9256052007-09-28 10:29:17 +0000324 /* return 0; - not reached */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000325}