blob: e14325dcaaed3e465fab07dd138a17e8fdfc662d [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 */
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;
178 struct xid_item *item;
179
180 /* check packet_type */
181 type = get_dhcp_packet_type(p);
182 if (type != DHCPDISCOVER && type != DHCPREQUEST
183 && type != DHCPDECLINE && type != DHCPRELEASE
184 && type != DHCPINFORM
185 ) {
186 return;
187 }
188
189 /* create new xid entry */
190 item = xid_add(p->xid, client_addr, client);
191
192 /* forward request to LAN (server) */
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000193 errno = 0;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000194 res = sendto(fds[0], p, packet_len, 0, (struct sockaddr*)server_addr,
195 sizeof(struct sockaddr_in));
196 if (res != packet_len) {
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000197 bb_perror_msg("sendto");
Denis Vlasenko736230e2006-11-20 19:40:36 +0000198 }
199}
200
201/**
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000202 * pass_to_client() - forwards dhcp packets from server to client
Denis Vlasenko736230e2006-11-20 19:40:36 +0000203 * p - packet to send
204 */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200205static void pass_to_client(struct dhcp_packet *p, int packet_len, int *fds)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000206{
207 int res, type;
208 struct xid_item *item;
209
210 /* check xid */
211 item = xid_find(p->xid);
212 if (!item) {
213 return;
214 }
215
216 /* check packet type */
217 type = get_dhcp_packet_type(p);
218 if (type != DHCPOFFER && type != DHCPACK && type != DHCPNAK) {
219 return;
220 }
221
222 if (item->ip.sin_addr.s_addr == htonl(INADDR_ANY))
223 item->ip.sin_addr.s_addr = htonl(INADDR_BROADCAST);
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000224 errno = 0;
225 res = sendto(fds[item->client], p, packet_len, 0, (struct sockaddr*) &(item->ip),
226 sizeof(item->ip));
Denis Vlasenko736230e2006-11-20 19:40:36 +0000227 if (res != packet_len) {
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000228 bb_perror_msg("sendto");
Denis Vlasenko736230e2006-11-20 19:40:36 +0000229 return;
230 }
231
232 /* remove xid entry */
233 xid_del(p->xid);
234}
235
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000236int dhcprelay_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
237int dhcprelay_main(int argc, char **argv)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000238{
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200239 struct dhcp_packet dhcp_msg;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000240 struct sockaddr_in server_addr;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000241 struct sockaddr_in client_addr;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000242 fd_set rfds;
243 char **client_ifaces;
244 int *fds;
245 int num_sockets, max_socket;
Denys Vlasenko990a6172009-06-16 10:23:55 +0200246 uint32_t our_nip;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000247
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000248 server_addr.sin_family = AF_INET;
249 server_addr.sin_port = htons(SERVER_PORT);
250
251 /* dhcprelay client_iface1,client_iface2,... server_iface [server_IP] */
252 if (argc == 4) {
253 if (!inet_aton(argv[3], &server_addr.sin_addr))
254 bb_perror_msg_and_die("bad server IP");
255 } else if (argc == 3) {
256 server_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
257 } else {
258 bb_show_usage();
259 }
260
261 /* Produce list of client ifaces */
262 client_ifaces = get_client_devices(argv[1], &num_sockets);
263
264 num_sockets++; /* for server socket at fds[0] */
265 fds = xmalloc(num_sockets * sizeof(fds[0]));
266
267 /* Create sockets and bind one to every iface */
268 max_socket = init_sockets(client_ifaces, num_sockets, argv[2], fds);
269
270 /* Get our IP on server_iface */
Denys Vlasenko990a6172009-06-16 10:23:55 +0200271 if (udhcp_read_interface(argv[2], NULL, &our_nip, NULL))
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000272 return 1;
273
274 /* Main loop */
Denis Vlasenkob9256052007-09-28 10:29:17 +0000275 while (1) {
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000276//reinit stuff from time to time? go back to get_client_devices
277//every N minutes?
278 struct timeval tv;
279 size_t packlen;
280 socklen_t addr_size;
281 int i;
282
Denis Vlasenko736230e2006-11-20 19:40:36 +0000283 FD_ZERO(&rfds);
284 for (i = 0; i < num_sockets; i++)
285 FD_SET(fds[i], &rfds);
286 tv.tv_sec = SELECT_TIMEOUT;
287 tv.tv_usec = 0;
288 if (select(max_socket + 1, &rfds, NULL, NULL, &tv) > 0) {
289 /* server */
290 if (FD_ISSET(fds[0], &rfds)) {
Denis Vlasenko6de89942008-05-21 07:05:06 +0000291 packlen = udhcp_recv_kernel_packet(&dhcp_msg, fds[0]);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000292 if (packlen > 0) {
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000293 pass_to_client(&dhcp_msg, packlen, fds);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000294 }
295 }
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000296 /* clients */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000297 for (i = 1; i < num_sockets; i++) {
Denis Vlasenko736230e2006-11-20 19:40:36 +0000298 if (!FD_ISSET(fds[i], &rfds))
299 continue;
300 addr_size = sizeof(struct sockaddr_in);
301 packlen = recvfrom(fds[i], &dhcp_msg, sizeof(dhcp_msg), 0,
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000302 (struct sockaddr *)(&client_addr), &addr_size);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000303 if (packlen <= 0)
304 continue;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000305
306 /* Get our IP on corresponding client_iface */
307//why? what if server can't route such IP?
Denys Vlasenko990a6172009-06-16 10:23:55 +0200308 if (udhcp_read_interface(client_ifaces[i-1], NULL, &dhcp_msg.gateway_nip, NULL)) {
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000309 /* Fall back to our server_iface's IP */
310//this makes more sense!
Denys Vlasenko990a6172009-06-16 10:23:55 +0200311 dhcp_msg.gateway_nip = our_nip;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000312 }
313//maybe set dhcp_msg.flags |= BROADCAST_FLAG too?
314 pass_to_server(&dhcp_msg, packlen, i, fds, &client_addr, &server_addr);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000315 }
316 }
317 xid_expire();
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000318 } /* while (1) */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000319
Denis Vlasenkob9256052007-09-28 10:29:17 +0000320 /* return 0; - not reached */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000321}