blob: a2c7f359d24a92d3694867040c5726f3b613dcbb [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 */
Denis Vlasenko736230e2006-11-20 19:40:36 +000012#include "common.h"
Denis Vlasenko736230e2006-11-20 19:40:36 +000013
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +000014#define SERVER_PORT 67
Denis Vlasenkod7b33502007-09-30 17:54:10 +000015#define SELECT_TIMEOUT 5 /* select timeout in sec. */
16#define MAX_LIFETIME 2*60 /* lifetime of an xid entry in sec. */
Denis Vlasenko736230e2006-11-20 19:40:36 +000017
Denis Vlasenko736230e2006-11-20 19:40:36 +000018/* This list holds information about clients. The xid_* functions manipulate this list. */
Denis Vlasenkob9256052007-09-28 10:29:17 +000019struct xid_item {
Denis Vlasenkod7b33502007-09-30 17:54:10 +000020 unsigned timestamp;
Denis Vlasenkob9256052007-09-28 10:29:17 +000021 int client;
Denis Vlasenko35d4da02007-01-22 14:04:27 +000022 uint32_t xid;
Denis Vlasenko736230e2006-11-20 19:40:36 +000023 struct sockaddr_in ip;
Denis Vlasenko736230e2006-11-20 19:40:36 +000024 struct xid_item *next;
Denis Vlasenkob9256052007-09-28 10:29:17 +000025};
Denis Vlasenko736230e2006-11-20 19:40:36 +000026
Denis Vlasenkob9256052007-09-28 10:29:17 +000027#define dhcprelay_xid_list (*(struct xid_item*)&bb_common_bufsiz1)
Denis Vlasenko736230e2006-11-20 19:40:36 +000028
Denis Vlasenko80edead2007-08-02 22:31:05 +000029static struct xid_item *xid_add(uint32_t xid, struct sockaddr_in *ip, int client)
Denis Vlasenko736230e2006-11-20 19:40:36 +000030{
31 struct xid_item *item;
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +000032
Denis Vlasenko736230e2006-11-20 19:40:36 +000033 /* create new xid entry */
34 item = xmalloc(sizeof(struct xid_item));
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +000035
Denis Vlasenko736230e2006-11-20 19:40:36 +000036 /* add xid entry */
37 item->ip = *ip;
38 item->xid = xid;
39 item->client = client;
Denis Vlasenkod7b33502007-09-30 17:54:10 +000040 item->timestamp = monotonic_sec();
Denis Vlasenko736230e2006-11-20 19:40:36 +000041 item->next = dhcprelay_xid_list.next;
42 dhcprelay_xid_list.next = item;
43
44 return item;
45}
46
Denis Vlasenko736230e2006-11-20 19:40:36 +000047static void xid_expire(void)
48{
49 struct xid_item *item = dhcprelay_xid_list.next;
50 struct xid_item *last = &dhcprelay_xid_list;
Denis Vlasenkod7b33502007-09-30 17:54:10 +000051 unsigned current_time = monotonic_sec();
Denis Vlasenko736230e2006-11-20 19:40:36 +000052
53 while (item != NULL) {
Denis Vlasenko80edead2007-08-02 22:31:05 +000054 if ((current_time - item->timestamp) > MAX_LIFETIME) {
Denis Vlasenko736230e2006-11-20 19:40:36 +000055 last->next = item->next;
56 free(item);
57 item = last->next;
58 } else {
59 last = item;
60 item = item->next;
61 }
62 }
63}
64
Denis Vlasenko80edead2007-08-02 22:31:05 +000065static struct xid_item *xid_find(uint32_t xid)
Denis Vlasenko736230e2006-11-20 19:40:36 +000066{
67 struct xid_item *item = dhcprelay_xid_list.next;
68 while (item != NULL) {
69 if (item->xid == xid) {
70 return item;
71 }
72 item = item->next;
73 }
74 return NULL;
75}
76
Denis Vlasenko35d4da02007-01-22 14:04:27 +000077static void xid_del(uint32_t xid)
Denis Vlasenko736230e2006-11-20 19:40:36 +000078{
79 struct xid_item *item = dhcprelay_xid_list.next;
80 struct xid_item *last = &dhcprelay_xid_list;
81 while (item != NULL) {
82 if (item->xid == xid) {
83 last->next = item->next;
84 free(item);
85 item = last->next;
86 } else {
87 last = item;
88 item = item->next;
89 }
90 }
91}
92
Denis Vlasenko736230e2006-11-20 19:40:36 +000093/**
94 * get_dhcp_packet_type - gets the message type of a dhcp packet
95 * p - pointer to the dhcp packet
96 * returns the message type on success, -1 otherwise
97 */
Denys Vlasenko31af3d52009-06-17 11:57:09 +020098static int get_dhcp_packet_type(struct dhcp_packet *p)
Denis Vlasenko736230e2006-11-20 19:40:36 +000099{
Denis Vlasenko28703012006-12-19 20:32:02 +0000100 uint8_t *op;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000101
102 /* it must be either a BOOTREQUEST or a BOOTREPLY */
103 if (p->op != BOOTREQUEST && p->op != BOOTREPLY)
104 return -1;
105 /* get message type option */
Denys Vlasenkodde8bdc2010-03-22 14:29:13 +0100106 op = udhcp_get_option(p, DHCP_MESSAGE_TYPE);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000107 if (op != NULL)
108 return op[0];
109 return -1;
110}
111
112/**
Denis Vlasenko736230e2006-11-20 19:40:36 +0000113 * get_client_devices - parses the devices list
114 * dev_list - comma separated list of devices
115 * returns array
116 */
Denis Vlasenko80edead2007-08-02 22:31:05 +0000117static char **get_client_devices(char *dev_list, int *client_number)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000118{
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000119 char *s, **client_dev;
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000120 int i, cn;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000121
122 /* copy list */
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000123 dev_list = xstrdup(dev_list);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000124
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000125 /* get number of items, replace ',' with NULs */
126 s = dev_list;
127 cn = 1;
128 while (*s) {
129 if (*s == ',') {
130 *s = '\0';
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000131 cn++;
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000132 }
133 s++;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000134 }
Denis Vlasenko736230e2006-11-20 19:40:36 +0000135 *client_number = cn;
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000136
137 /* create vector of pointers */
138 client_dev = xzalloc(cn * sizeof(*client_dev));
139 client_dev[0] = dev_list;
140 i = 1;
141 while (i != cn) {
142 client_dev[i] = client_dev[i - 1] + strlen(client_dev[i - 1]) + 1;
143 i++;
144 }
Denis Vlasenko736230e2006-11-20 19:40:36 +0000145 return client_dev;
146}
147
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000148/* Creates listen sockets (in fds) bound to client and server ifaces,
149 * and returns numerically max fd.
150 */
151static int init_sockets(char **client_ifaces, int num_clients,
152 char *server_iface, int *fds)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000153{
Denis Vlasenkob9256052007-09-28 10:29:17 +0000154 int i, n;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000155
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000156 /* talk to real server on bootps */
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000157 fds[0] = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT, server_iface);
Denis Vlasenkob9256052007-09-28 10:29:17 +0000158 n = fds[0];
Denis Vlasenko736230e2006-11-20 19:40:36 +0000159
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000160 for (i = 1; i < num_clients; i++) {
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000161 /* listen for clients on bootps */
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000162 fds[i] = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT, client_ifaces[i-1]);
Denis Vlasenkob9256052007-09-28 10:29:17 +0000163 if (fds[i] > n)
164 n = fds[i];
Denis Vlasenko736230e2006-11-20 19:40:36 +0000165 }
Denis Vlasenkob9256052007-09-28 10:29:17 +0000166 return n;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000167}
168
Denis Vlasenko736230e2006-11-20 19:40:36 +0000169/**
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000170 * pass_to_server() - forwards dhcp packets from client to server
Denis Vlasenko736230e2006-11-20 19:40:36 +0000171 * p - packet to send
172 * client - number of the client
173 */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200174static void pass_to_server(struct dhcp_packet *p, int packet_len, int client, int *fds,
Denis Vlasenko736230e2006-11-20 19:40:36 +0000175 struct sockaddr_in *client_addr, struct sockaddr_in *server_addr)
176{
177 int res, type;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000178
179 /* check packet_type */
180 type = get_dhcp_packet_type(p);
181 if (type != DHCPDISCOVER && type != DHCPREQUEST
182 && type != DHCPDECLINE && type != DHCPRELEASE
183 && type != DHCPINFORM
184 ) {
185 return;
186 }
187
188 /* create new xid entry */
Denys Vlasenko243d1752010-07-04 04:26:55 +0200189 xid_add(p->xid, client_addr, client);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000190
191 /* forward request to LAN (server) */
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000192 errno = 0;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000193 res = sendto(fds[0], p, packet_len, 0, (struct sockaddr*)server_addr,
194 sizeof(struct sockaddr_in));
195 if (res != packet_len) {
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000196 bb_perror_msg("sendto");
Denis Vlasenko736230e2006-11-20 19:40:36 +0000197 }
198}
199
200/**
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000201 * pass_to_client() - forwards dhcp packets from server to client
Denis Vlasenko736230e2006-11-20 19:40:36 +0000202 * p - packet to send
203 */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200204static void pass_to_client(struct dhcp_packet *p, int packet_len, int *fds)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000205{
206 int res, type;
207 struct xid_item *item;
208
209 /* check xid */
210 item = xid_find(p->xid);
211 if (!item) {
212 return;
213 }
214
215 /* check packet type */
216 type = get_dhcp_packet_type(p);
217 if (type != DHCPOFFER && type != DHCPACK && type != DHCPNAK) {
218 return;
219 }
220
221 if (item->ip.sin_addr.s_addr == htonl(INADDR_ANY))
222 item->ip.sin_addr.s_addr = htonl(INADDR_BROADCAST);
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000223 errno = 0;
224 res = sendto(fds[item->client], p, packet_len, 0, (struct sockaddr*) &(item->ip),
225 sizeof(item->ip));
Denis Vlasenko736230e2006-11-20 19:40:36 +0000226 if (res != packet_len) {
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000227 bb_perror_msg("sendto");
Denis Vlasenko736230e2006-11-20 19:40:36 +0000228 return;
229 }
230
231 /* remove xid entry */
232 xid_del(p->xid);
233}
234
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000235int dhcprelay_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
236int dhcprelay_main(int argc, char **argv)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000237{
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200238 struct dhcp_packet dhcp_msg;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000239 struct sockaddr_in server_addr;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000240 struct sockaddr_in client_addr;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000241 fd_set rfds;
242 char **client_ifaces;
243 int *fds;
244 int num_sockets, max_socket;
Denys Vlasenko990a6172009-06-16 10:23:55 +0200245 uint32_t our_nip;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000246
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000247 server_addr.sin_family = AF_INET;
248 server_addr.sin_port = htons(SERVER_PORT);
249
250 /* dhcprelay client_iface1,client_iface2,... server_iface [server_IP] */
251 if (argc == 4) {
252 if (!inet_aton(argv[3], &server_addr.sin_addr))
253 bb_perror_msg_and_die("bad server IP");
254 } else if (argc == 3) {
255 server_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
256 } else {
257 bb_show_usage();
258 }
259
260 /* Produce list of client ifaces */
261 client_ifaces = get_client_devices(argv[1], &num_sockets);
262
263 num_sockets++; /* for server socket at fds[0] */
264 fds = xmalloc(num_sockets * sizeof(fds[0]));
265
266 /* Create sockets and bind one to every iface */
267 max_socket = init_sockets(client_ifaces, num_sockets, argv[2], fds);
268
269 /* Get our IP on server_iface */
Denys Vlasenko990a6172009-06-16 10:23:55 +0200270 if (udhcp_read_interface(argv[2], NULL, &our_nip, NULL))
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000271 return 1;
272
273 /* Main loop */
Denis Vlasenkob9256052007-09-28 10:29:17 +0000274 while (1) {
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000275//reinit stuff from time to time? go back to get_client_devices
276//every N minutes?
277 struct timeval tv;
278 size_t packlen;
279 socklen_t addr_size;
280 int i;
281
Denis Vlasenko736230e2006-11-20 19:40:36 +0000282 FD_ZERO(&rfds);
283 for (i = 0; i < num_sockets; i++)
284 FD_SET(fds[i], &rfds);
285 tv.tv_sec = SELECT_TIMEOUT;
286 tv.tv_usec = 0;
287 if (select(max_socket + 1, &rfds, NULL, NULL, &tv) > 0) {
288 /* server */
289 if (FD_ISSET(fds[0], &rfds)) {
Denis Vlasenko6de89942008-05-21 07:05:06 +0000290 packlen = udhcp_recv_kernel_packet(&dhcp_msg, fds[0]);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000291 if (packlen > 0) {
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000292 pass_to_client(&dhcp_msg, packlen, fds);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000293 }
294 }
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000295 /* clients */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000296 for (i = 1; i < num_sockets; i++) {
Denis Vlasenko736230e2006-11-20 19:40:36 +0000297 if (!FD_ISSET(fds[i], &rfds))
298 continue;
299 addr_size = sizeof(struct sockaddr_in);
300 packlen = recvfrom(fds[i], &dhcp_msg, sizeof(dhcp_msg), 0,
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000301 (struct sockaddr *)(&client_addr), &addr_size);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000302 if (packlen <= 0)
303 continue;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000304
305 /* Get our IP on corresponding client_iface */
306//why? what if server can't route such IP?
Denys Vlasenko990a6172009-06-16 10:23:55 +0200307 if (udhcp_read_interface(client_ifaces[i-1], NULL, &dhcp_msg.gateway_nip, NULL)) {
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000308 /* Fall back to our server_iface's IP */
309//this makes more sense!
Denys Vlasenko990a6172009-06-16 10:23:55 +0200310 dhcp_msg.gateway_nip = our_nip;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000311 }
312//maybe set dhcp_msg.flags |= BROADCAST_FLAG too?
313 pass_to_server(&dhcp_msg, packlen, i, fds, &client_addr, &server_addr);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000314 }
315 }
316 xid_expire();
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000317 } /* while (1) */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000318
Denis Vlasenkob9256052007-09-28 10:29:17 +0000319 /* return 0; - not reached */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000320}