blob: f3b2855d868f9637f9ecffb015bc62f0f891de05 [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
16/* constants */
Denis Vlasenkod55fe3e2008-02-04 13:12:16 +000017#define SERVER_PORT 67
Denis Vlasenkod7b33502007-09-30 17:54:10 +000018#define SELECT_TIMEOUT 5 /* select timeout in sec. */
19#define MAX_LIFETIME 2*60 /* lifetime of an xid entry in sec. */
Denis Vlasenko736230e2006-11-20 19:40:36 +000020
Denis Vlasenko736230e2006-11-20 19:40:36 +000021/* This list holds information about clients. The xid_* functions manipulate this list. */
Denis Vlasenkob9256052007-09-28 10:29:17 +000022struct xid_item {
Denis Vlasenkod7b33502007-09-30 17:54:10 +000023 unsigned timestamp;
Denis Vlasenkob9256052007-09-28 10:29:17 +000024 int client;
Denis Vlasenko35d4da02007-01-22 14:04:27 +000025 uint32_t xid;
Denis Vlasenko736230e2006-11-20 19:40:36 +000026 struct sockaddr_in ip;
Denis Vlasenko736230e2006-11-20 19:40:36 +000027 struct xid_item *next;
Denis Vlasenkob9256052007-09-28 10:29:17 +000028};
Denis Vlasenko736230e2006-11-20 19:40:36 +000029
Denis Vlasenkob9256052007-09-28 10:29:17 +000030#define dhcprelay_xid_list (*(struct xid_item*)&bb_common_bufsiz1)
Denis Vlasenko736230e2006-11-20 19:40:36 +000031
Denis Vlasenko80edead2007-08-02 22:31:05 +000032static struct xid_item *xid_add(uint32_t xid, struct sockaddr_in *ip, int client)
Denis Vlasenko736230e2006-11-20 19:40:36 +000033{
34 struct xid_item *item;
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +000035
Denis Vlasenko736230e2006-11-20 19:40:36 +000036 /* create new xid entry */
37 item = xmalloc(sizeof(struct xid_item));
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +000038
Denis Vlasenko736230e2006-11-20 19:40:36 +000039 /* add xid entry */
40 item->ip = *ip;
41 item->xid = xid;
42 item->client = client;
Denis Vlasenkod7b33502007-09-30 17:54:10 +000043 item->timestamp = monotonic_sec();
Denis Vlasenko736230e2006-11-20 19:40:36 +000044 item->next = dhcprelay_xid_list.next;
45 dhcprelay_xid_list.next = item;
46
47 return item;
48}
49
Denis Vlasenko736230e2006-11-20 19:40:36 +000050static void xid_expire(void)
51{
52 struct xid_item *item = dhcprelay_xid_list.next;
53 struct xid_item *last = &dhcprelay_xid_list;
Denis Vlasenkod7b33502007-09-30 17:54:10 +000054 unsigned current_time = monotonic_sec();
Denis Vlasenko736230e2006-11-20 19:40:36 +000055
56 while (item != NULL) {
Denis Vlasenko80edead2007-08-02 22:31:05 +000057 if ((current_time - item->timestamp) > MAX_LIFETIME) {
Denis Vlasenko736230e2006-11-20 19:40:36 +000058 last->next = item->next;
59 free(item);
60 item = last->next;
61 } else {
62 last = item;
63 item = item->next;
64 }
65 }
66}
67
Denis Vlasenko80edead2007-08-02 22:31:05 +000068static struct xid_item *xid_find(uint32_t xid)
Denis Vlasenko736230e2006-11-20 19:40:36 +000069{
70 struct xid_item *item = dhcprelay_xid_list.next;
71 while (item != NULL) {
72 if (item->xid == xid) {
73 return item;
74 }
75 item = item->next;
76 }
77 return NULL;
78}
79
Denis Vlasenko35d4da02007-01-22 14:04:27 +000080static void xid_del(uint32_t xid)
Denis Vlasenko736230e2006-11-20 19:40:36 +000081{
82 struct xid_item *item = dhcprelay_xid_list.next;
83 struct xid_item *last = &dhcprelay_xid_list;
84 while (item != NULL) {
85 if (item->xid == xid) {
86 last->next = item->next;
87 free(item);
88 item = last->next;
89 } else {
90 last = item;
91 item = item->next;
92 }
93 }
94}
95
Denis Vlasenko736230e2006-11-20 19:40:36 +000096/**
97 * get_dhcp_packet_type - gets the message type of a dhcp packet
98 * p - pointer to the dhcp packet
99 * returns the message type on success, -1 otherwise
100 */
101static int get_dhcp_packet_type(struct dhcpMessage *p)
102{
Denis Vlasenko28703012006-12-19 20:32:02 +0000103 uint8_t *op;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000104
105 /* it must be either a BOOTREQUEST or a BOOTREPLY */
106 if (p->op != BOOTREQUEST && p->op != BOOTREPLY)
107 return -1;
108 /* get message type option */
109 op = get_option(p, DHCP_MESSAGE_TYPE);
110 if (op != NULL)
111 return op[0];
112 return -1;
113}
114
115/**
Denis Vlasenko736230e2006-11-20 19:40:36 +0000116 * get_client_devices - parses the devices list
117 * dev_list - comma separated list of devices
118 * returns array
119 */
Denis Vlasenko80edead2007-08-02 22:31:05 +0000120static char **get_client_devices(char *dev_list, int *client_number)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000121{
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000122 char *s, **client_dev;
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000123 int i, cn;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000124
125 /* copy list */
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000126 dev_list = xstrdup(dev_list);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000127
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000128 /* get number of items, replace ',' with NULs */
129 s = dev_list;
130 cn = 1;
131 while (*s) {
132 if (*s == ',') {
133 *s = '\0';
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000134 cn++;
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000135 }
136 s++;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000137 }
Denis Vlasenko736230e2006-11-20 19:40:36 +0000138 *client_number = cn;
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000139
140 /* create vector of pointers */
141 client_dev = xzalloc(cn * sizeof(*client_dev));
142 client_dev[0] = dev_list;
143 i = 1;
144 while (i != cn) {
145 client_dev[i] = client_dev[i - 1] + strlen(client_dev[i - 1]) + 1;
146 i++;
147 }
Denis Vlasenko736230e2006-11-20 19:40:36 +0000148 return client_dev;
149}
150
151
Denis Vlasenkob9256052007-09-28 10:29:17 +0000152/* Creates listen sockets (in fds) and returns numerically max fd. */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000153static int init_sockets(char **client, int num_clients,
Denis Vlasenkob9256052007-09-28 10:29:17 +0000154 char *server, int *fds)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000155{
Denis Vlasenkob9256052007-09-28 10:29:17 +0000156 int i, n;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000157
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000158 /* talk to real server on bootps */
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000159 fds[0] = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT, server);
Denis Vlasenkob9256052007-09-28 10:29:17 +0000160 n = fds[0];
Denis Vlasenko736230e2006-11-20 19:40:36 +0000161
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000162 for (i = 1; i < num_clients; i++) {
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000163 /* listen for clients on bootps */
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000164 fds[i] = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT, client[i-1]);
Denis Vlasenkob9256052007-09-28 10:29:17 +0000165 if (fds[i] > n)
166 n = fds[i];
Denis Vlasenko736230e2006-11-20 19:40:36 +0000167 }
Denis Vlasenkob9256052007-09-28 10:29:17 +0000168 return n;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000169}
170
171
172/**
173 * pass_on() - forwards dhcp packets from client to server
174 * p - packet to send
175 * client - number of the client
176 */
177static void pass_on(struct dhcpMessage *p, int packet_len, int client, int *fds,
178 struct sockaddr_in *client_addr, struct sockaddr_in *server_addr)
179{
180 int res, type;
181 struct xid_item *item;
182
183 /* check packet_type */
184 type = get_dhcp_packet_type(p);
185 if (type != DHCPDISCOVER && type != DHCPREQUEST
186 && type != DHCPDECLINE && type != DHCPRELEASE
187 && type != DHCPINFORM
188 ) {
189 return;
190 }
191
192 /* create new xid entry */
193 item = xid_add(p->xid, client_addr, client);
194
195 /* forward request to LAN (server) */
196 res = sendto(fds[0], p, packet_len, 0, (struct sockaddr*)server_addr,
197 sizeof(struct sockaddr_in));
198 if (res != packet_len) {
199 bb_perror_msg("pass_on");
200 return;
201 }
202}
203
204/**
205 * pass_back() - forwards dhcp packets from server to client
206 * p - packet to send
207 */
208static void pass_back(struct dhcpMessage *p, int packet_len, int *fds)
209{
210 int res, type;
211 struct xid_item *item;
212
213 /* check xid */
214 item = xid_find(p->xid);
215 if (!item) {
216 return;
217 }
218
219 /* check packet type */
220 type = get_dhcp_packet_type(p);
221 if (type != DHCPOFFER && type != DHCPACK && type != DHCPNAK) {
222 return;
223 }
224
225 if (item->ip.sin_addr.s_addr == htonl(INADDR_ANY))
226 item->ip.sin_addr.s_addr = htonl(INADDR_BROADCAST);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000227 res = sendto(fds[item->client], p, packet_len, 0, (struct sockaddr*)(&item->ip),
228 sizeof(item->ip));
229 if (res != packet_len) {
230 bb_perror_msg("pass_back");
231 return;
232 }
233
234 /* remove xid entry */
235 xid_del(p->xid);
236}
237
238static void dhcprelay_loop(int *fds, int num_sockets, int max_socket, char **clients,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000239 struct sockaddr_in *server_addr, uint32_t gw_ip) NORETURN;
Denis Vlasenkob9256052007-09-28 10:29:17 +0000240static void dhcprelay_loop(int *fds, int num_sockets, int max_socket, char **clients,
Denis Vlasenko736230e2006-11-20 19:40:36 +0000241 struct sockaddr_in *server_addr, uint32_t gw_ip)
242{
243 struct dhcpMessage dhcp_msg;
244 fd_set rfds;
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000245 size_t packlen;
246 socklen_t addr_size;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000247 struct sockaddr_in client_addr;
248 struct timeval tv;
249 int i;
250
Denis Vlasenkob9256052007-09-28 10:29:17 +0000251 while (1) {
Denis Vlasenko736230e2006-11-20 19:40:36 +0000252 FD_ZERO(&rfds);
253 for (i = 0; i < num_sockets; i++)
254 FD_SET(fds[i], &rfds);
255 tv.tv_sec = SELECT_TIMEOUT;
256 tv.tv_usec = 0;
257 if (select(max_socket + 1, &rfds, NULL, NULL, &tv) > 0) {
258 /* server */
259 if (FD_ISSET(fds[0], &rfds)) {
Denis Vlasenko6de89942008-05-21 07:05:06 +0000260 packlen = udhcp_recv_kernel_packet(&dhcp_msg, fds[0]);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000261 if (packlen > 0) {
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000262 pass_back(&dhcp_msg, packlen, fds);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000263 }
264 }
265 for (i = 1; i < num_sockets; i++) {
266 /* clients */
267 if (!FD_ISSET(fds[i], &rfds))
268 continue;
269 addr_size = sizeof(struct sockaddr_in);
270 packlen = recvfrom(fds[i], &dhcp_msg, sizeof(dhcp_msg), 0,
271 (struct sockaddr *)(&client_addr), &addr_size);
272 if (packlen <= 0)
273 continue;
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000274 if (udhcp_read_interface(clients[i-1], NULL, &dhcp_msg.giaddr, NULL))
Denis Vlasenko736230e2006-11-20 19:40:36 +0000275 dhcp_msg.giaddr = gw_ip;
276 pass_on(&dhcp_msg, packlen, i, fds, &client_addr, server_addr);
277 }
278 }
279 xid_expire();
280 }
281}
282
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000283int dhcprelay_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000284int dhcprelay_main(int argc, char **argv)
285{
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000286 int num_sockets, max_socket;
287 int *fds;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000288 uint32_t gw_ip;
289 char **clients;
290 struct sockaddr_in server_addr;
291
292 server_addr.sin_family = AF_INET;
Denis Vlasenkob76b9a42008-01-25 22:46:34 +0000293 server_addr.sin_port = htons(SERVER_PORT);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000294 if (argc == 4) {
295 if (!inet_aton(argv[3], &server_addr.sin_addr))
296 bb_perror_msg_and_die("didn't grok server");
297 } else if (argc == 3) {
298 server_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
299 } else {
300 bb_show_usage();
301 }
Denis Vlasenko736230e2006-11-20 19:40:36 +0000302
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000303 clients = get_client_devices(argv[1], &num_sockets);
Denis Vlasenkob9256052007-09-28 10:29:17 +0000304 num_sockets++; /* for server socket at fds[0] */
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000305 fds = xmalloc(num_sockets * sizeof(fds[0]));
Denis Vlasenkob9256052007-09-28 10:29:17 +0000306 max_socket = init_sockets(clients, num_sockets, argv[2], fds);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000307
Denis Vlasenkof1980f62008-09-26 09:34:59 +0000308 if (udhcp_read_interface(argv[2], NULL, &gw_ip, NULL))
Denis Vlasenko736230e2006-11-20 19:40:36 +0000309 return 1;
310
Denis Vlasenkob9256052007-09-28 10:29:17 +0000311 /* doesn't return */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000312 dhcprelay_loop(fds, num_sockets, max_socket, clients, &server_addr, gw_ip);
Denis Vlasenkob9256052007-09-28 10:29:17 +0000313 /* return 0; - not reached */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000314}