blob: 4ed65a2d632bbf78a39c2261d0037ea13ba80029 [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"
14#include "dhcpd.h"
15#include "options.h"
16
17/* constants */
18#define SELECT_TIMEOUT 5 /* select timeout in sec. */
19#define MAX_LIFETIME 2*60 /* lifetime of an xid entry in sec. */
20#define MAX_INTERFACES 9
21
22
23/* This list holds information about clients. The xid_* functions manipulate this list. */
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +000024static struct xid_item {
Denis Vlasenko35d4da02007-01-22 14:04:27 +000025 uint32_t xid;
Denis Vlasenko736230e2006-11-20 19:40:36 +000026 struct sockaddr_in ip;
27 int client;
28 time_t timestamp;
29 struct xid_item *next;
30} dhcprelay_xid_list = {0, {0}, 0, 0, NULL};
31
32
Denis Vlasenko80edead2007-08-02 22:31:05 +000033static struct xid_item *xid_add(uint32_t xid, struct sockaddr_in *ip, int client)
Denis Vlasenko736230e2006-11-20 19:40:36 +000034{
35 struct xid_item *item;
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +000036
Denis Vlasenko736230e2006-11-20 19:40:36 +000037 /* create new xid entry */
38 item = xmalloc(sizeof(struct xid_item));
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +000039
Denis Vlasenko736230e2006-11-20 19:40:36 +000040 /* add xid entry */
41 item->ip = *ip;
42 item->xid = xid;
43 item->client = client;
44 item->timestamp = time(NULL);
45 item->next = dhcprelay_xid_list.next;
46 dhcprelay_xid_list.next = item;
47
48 return item;
49}
50
Denis Vlasenko736230e2006-11-20 19:40:36 +000051static void xid_expire(void)
52{
53 struct xid_item *item = dhcprelay_xid_list.next;
54 struct xid_item *last = &dhcprelay_xid_list;
55 time_t current_time = time(NULL);
56
57 while (item != NULL) {
Denis Vlasenko80edead2007-08-02 22:31:05 +000058 if ((current_time - item->timestamp) > MAX_LIFETIME) {
Denis Vlasenko736230e2006-11-20 19:40:36 +000059 last->next = item->next;
60 free(item);
61 item = last->next;
62 } else {
63 last = item;
64 item = item->next;
65 }
66 }
67}
68
Denis Vlasenko80edead2007-08-02 22:31:05 +000069static struct xid_item *xid_find(uint32_t xid)
Denis Vlasenko736230e2006-11-20 19:40:36 +000070{
71 struct xid_item *item = dhcprelay_xid_list.next;
72 while (item != NULL) {
73 if (item->xid == xid) {
74 return item;
75 }
76 item = item->next;
77 }
78 return NULL;
79}
80
Denis Vlasenko35d4da02007-01-22 14:04:27 +000081static void xid_del(uint32_t xid)
Denis Vlasenko736230e2006-11-20 19:40:36 +000082{
83 struct xid_item *item = dhcprelay_xid_list.next;
84 struct xid_item *last = &dhcprelay_xid_list;
85 while (item != NULL) {
86 if (item->xid == xid) {
87 last->next = item->next;
88 free(item);
89 item = last->next;
90 } else {
91 last = item;
92 item = item->next;
93 }
94 }
95}
96
Denis Vlasenko736230e2006-11-20 19:40:36 +000097/**
98 * get_dhcp_packet_type - gets the message type of a dhcp packet
99 * p - pointer to the dhcp packet
100 * returns the message type on success, -1 otherwise
101 */
102static int get_dhcp_packet_type(struct dhcpMessage *p)
103{
Denis Vlasenko28703012006-12-19 20:32:02 +0000104 uint8_t *op;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000105
106 /* it must be either a BOOTREQUEST or a BOOTREPLY */
107 if (p->op != BOOTREQUEST && p->op != BOOTREPLY)
108 return -1;
109 /* get message type option */
110 op = get_option(p, DHCP_MESSAGE_TYPE);
111 if (op != NULL)
112 return op[0];
113 return -1;
114}
115
116/**
117 * signal_handler - handles signals ;-)
118 * sig - sent signal
119 */
Denis Vlasenko80edead2007-08-02 22:31:05 +0000120static smallint dhcprelay_stopflag;
121
Denis Vlasenko736230e2006-11-20 19:40:36 +0000122static void dhcprelay_signal_handler(int sig)
123{
124 dhcprelay_stopflag = 1;
125}
126
127/**
128 * get_client_devices - parses the devices list
129 * dev_list - comma separated list of devices
130 * returns array
131 */
Denis Vlasenko80edead2007-08-02 22:31:05 +0000132static char **get_client_devices(char *dev_list, int *client_number)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000133{
134 char *s, *list, **client_dev;
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000135 int i, cn;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000136
137 /* copy list */
138 list = xstrdup(dev_list);
139 if (list == NULL) return NULL;
140
141 /* get number of items */
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000142 for (s = dev_list, cn = 1; *s; s++)
143 if (*s == ',')
144 cn++;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000145
146 client_dev = xzalloc(cn * sizeof(*client_dev));
147
148 /* parse list */
149 s = strtok(list, ",");
150 i = 0;
151 while (s != NULL) {
152 client_dev[i++] = xstrdup(s);
153 s = strtok(NULL, ",");
154 }
155
156 /* free copy and exit */
157 free(list);
158 *client_number = cn;
159 return client_dev;
160}
161
162
163/* Creates listen sockets (in fds) and returns the number allocated. */
164static int init_sockets(char **client, int num_clients,
165 char *server, int *fds, int *max_socket)
166{
167 int i;
168
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000169 /* talk to real server on bootps */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000170 fds[0] = listen_socket(htonl(INADDR_ANY), 67, server);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000171 *max_socket = fds[0];
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000172
173 /* array starts at 1 since server is 0 */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000174 num_clients++;
175
176 for (i=1; i < num_clients; i++) {
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000177 /* listen for clients on bootps */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000178 fds[i] = listen_socket(htonl(INADDR_ANY), 67, client[i-1]);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000179 if (fds[i] > *max_socket) *max_socket = fds[i];
180 }
181
182 return i;
183}
184
185
186/**
187 * pass_on() - forwards dhcp packets from client to server
188 * p - packet to send
189 * client - number of the client
190 */
191static void pass_on(struct dhcpMessage *p, int packet_len, int client, int *fds,
192 struct sockaddr_in *client_addr, struct sockaddr_in *server_addr)
193{
194 int res, type;
195 struct xid_item *item;
196
197 /* check packet_type */
198 type = get_dhcp_packet_type(p);
199 if (type != DHCPDISCOVER && type != DHCPREQUEST
200 && type != DHCPDECLINE && type != DHCPRELEASE
201 && type != DHCPINFORM
202 ) {
203 return;
204 }
205
206 /* create new xid entry */
207 item = xid_add(p->xid, client_addr, client);
208
209 /* forward request to LAN (server) */
210 res = sendto(fds[0], p, packet_len, 0, (struct sockaddr*)server_addr,
211 sizeof(struct sockaddr_in));
212 if (res != packet_len) {
213 bb_perror_msg("pass_on");
214 return;
215 }
216}
217
218/**
219 * pass_back() - forwards dhcp packets from server to client
220 * p - packet to send
221 */
222static void pass_back(struct dhcpMessage *p, int packet_len, int *fds)
223{
224 int res, type;
225 struct xid_item *item;
226
227 /* check xid */
228 item = xid_find(p->xid);
229 if (!item) {
230 return;
231 }
232
233 /* check packet type */
234 type = get_dhcp_packet_type(p);
235 if (type != DHCPOFFER && type != DHCPACK && type != DHCPNAK) {
236 return;
237 }
238
239 if (item->ip.sin_addr.s_addr == htonl(INADDR_ANY))
240 item->ip.sin_addr.s_addr = htonl(INADDR_BROADCAST);
241 if (item->client > MAX_INTERFACES)
242 return;
243 res = sendto(fds[item->client], p, packet_len, 0, (struct sockaddr*)(&item->ip),
244 sizeof(item->ip));
245 if (res != packet_len) {
246 bb_perror_msg("pass_back");
247 return;
248 }
249
250 /* remove xid entry */
251 xid_del(p->xid);
252}
253
254static void dhcprelay_loop(int *fds, int num_sockets, int max_socket, char **clients,
255 struct sockaddr_in *server_addr, uint32_t gw_ip)
256{
257 struct dhcpMessage dhcp_msg;
258 fd_set rfds;
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000259 size_t packlen;
260 socklen_t addr_size;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000261 struct sockaddr_in client_addr;
262 struct timeval tv;
263 int i;
264
265 while (!dhcprelay_stopflag) {
266 FD_ZERO(&rfds);
267 for (i = 0; i < num_sockets; i++)
268 FD_SET(fds[i], &rfds);
269 tv.tv_sec = SELECT_TIMEOUT;
270 tv.tv_usec = 0;
271 if (select(max_socket + 1, &rfds, NULL, NULL, &tv) > 0) {
272 /* server */
273 if (FD_ISSET(fds[0], &rfds)) {
274 packlen = udhcp_get_packet(&dhcp_msg, fds[0]);
275 if (packlen > 0) {
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000276 pass_back(&dhcp_msg, packlen, fds);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000277 }
278 }
279 for (i = 1; i < num_sockets; i++) {
280 /* clients */
281 if (!FD_ISSET(fds[i], &rfds))
282 continue;
283 addr_size = sizeof(struct sockaddr_in);
284 packlen = recvfrom(fds[i], &dhcp_msg, sizeof(dhcp_msg), 0,
285 (struct sockaddr *)(&client_addr), &addr_size);
286 if (packlen <= 0)
287 continue;
Denis Vlasenko80edead2007-08-02 22:31:05 +0000288 if (read_interface(clients[i-1], NULL, &dhcp_msg.giaddr, NULL))
Denis Vlasenko736230e2006-11-20 19:40:36 +0000289 dhcp_msg.giaddr = gw_ip;
290 pass_on(&dhcp_msg, packlen, i, fds, &client_addr, server_addr);
291 }
292 }
293 xid_expire();
294 }
295}
296
Denis Vlasenko06af2162007-02-03 17:28:39 +0000297int dhcprelay_main(int argc, char **argv);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000298int dhcprelay_main(int argc, char **argv)
299{
300 int i, num_sockets, max_socket, fds[MAX_INTERFACES];
301 uint32_t gw_ip;
302 char **clients;
303 struct sockaddr_in server_addr;
304
305 server_addr.sin_family = AF_INET;
306 server_addr.sin_port = htons(67);
307 if (argc == 4) {
308 if (!inet_aton(argv[3], &server_addr.sin_addr))
309 bb_perror_msg_and_die("didn't grok server");
310 } else if (argc == 3) {
311 server_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
312 } else {
313 bb_show_usage();
314 }
315 clients = get_client_devices(argv[1], &num_sockets);
316 if (!clients) return 0;
317
318 signal(SIGTERM, dhcprelay_signal_handler);
319 signal(SIGQUIT, dhcprelay_signal_handler);
320 signal(SIGINT, dhcprelay_signal_handler);
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000321
Denis Vlasenko736230e2006-11-20 19:40:36 +0000322 num_sockets = init_sockets(clients, num_sockets, argv[2], fds, &max_socket);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000323
Denis Vlasenko80edead2007-08-02 22:31:05 +0000324 if (read_interface(argv[2], NULL, &gw_ip, NULL))
Denis Vlasenko736230e2006-11-20 19:40:36 +0000325 return 1;
326
327 dhcprelay_loop(fds, num_sockets, max_socket, clients, &server_addr, gw_ip);
328
329 if (ENABLE_FEATURE_CLEAN_UP) {
330 for (i = 0; i < num_sockets; i++) {
331 close(fds[i]);
332 free(clients[i]);
333 }
334 }
335
336 return 0;
337}