blob: a6483fc1f3cc3804b94c97c46c1baa03b06f8aee [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 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 Vlasenkob9256052007-09-28 10:29:17 +0000151/* Creates listen sockets (in fds) and returns numerically max fd. */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000152static int init_sockets(char **client, int num_clients,
Denis Vlasenkob9256052007-09-28 10:29:17 +0000153 char *server, int *fds)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000154{
Denis Vlasenkob9256052007-09-28 10:29:17 +0000155 int i, n;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000156
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000157 /* talk to real server on bootps */
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000158 fds[0] = listen_socket(/*INADDR_ANY,*/ 67, server);
Denis Vlasenkob9256052007-09-28 10:29:17 +0000159 n = fds[0];
Denis Vlasenko736230e2006-11-20 19:40:36 +0000160
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000161 for (i = 1; i < num_clients; i++) {
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000162 /* listen for clients on bootps */
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000163 fds[i] = listen_socket(/*INADDR_ANY,*/ 67, client[i-1]);
Denis Vlasenkob9256052007-09-28 10:29:17 +0000164 if (fds[i] > n)
165 n = fds[i];
Denis Vlasenko736230e2006-11-20 19:40:36 +0000166 }
Denis Vlasenkob9256052007-09-28 10:29:17 +0000167 return n;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000168}
169
170
171/**
172 * pass_on() - forwards dhcp packets from client to server
173 * p - packet to send
174 * client - number of the client
175 */
176static void pass_on(struct dhcpMessage *p, int packet_len, int client, int *fds,
177 struct sockaddr_in *client_addr, struct sockaddr_in *server_addr)
178{
179 int res, type;
180 struct xid_item *item;
181
182 /* check packet_type */
183 type = get_dhcp_packet_type(p);
184 if (type != DHCPDISCOVER && type != DHCPREQUEST
185 && type != DHCPDECLINE && type != DHCPRELEASE
186 && type != DHCPINFORM
187 ) {
188 return;
189 }
190
191 /* create new xid entry */
192 item = xid_add(p->xid, client_addr, client);
193
194 /* forward request to LAN (server) */
195 res = sendto(fds[0], p, packet_len, 0, (struct sockaddr*)server_addr,
196 sizeof(struct sockaddr_in));
197 if (res != packet_len) {
198 bb_perror_msg("pass_on");
199 return;
200 }
201}
202
203/**
204 * pass_back() - forwards dhcp packets from server to client
205 * p - packet to send
206 */
207static void pass_back(struct dhcpMessage *p, int packet_len, int *fds)
208{
209 int res, type;
210 struct xid_item *item;
211
212 /* check xid */
213 item = xid_find(p->xid);
214 if (!item) {
215 return;
216 }
217
218 /* check packet type */
219 type = get_dhcp_packet_type(p);
220 if (type != DHCPOFFER && type != DHCPACK && type != DHCPNAK) {
221 return;
222 }
223
224 if (item->ip.sin_addr.s_addr == htonl(INADDR_ANY))
225 item->ip.sin_addr.s_addr = htonl(INADDR_BROADCAST);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000226 res = sendto(fds[item->client], p, packet_len, 0, (struct sockaddr*)(&item->ip),
227 sizeof(item->ip));
228 if (res != packet_len) {
229 bb_perror_msg("pass_back");
230 return;
231 }
232
233 /* remove xid entry */
234 xid_del(p->xid);
235}
236
237static void dhcprelay_loop(int *fds, int num_sockets, int max_socket, char **clients,
Denis Vlasenkob9256052007-09-28 10:29:17 +0000238 struct sockaddr_in *server_addr, uint32_t gw_ip) ATTRIBUTE_NORETURN;
239static void dhcprelay_loop(int *fds, int num_sockets, int max_socket, char **clients,
Denis Vlasenko736230e2006-11-20 19:40:36 +0000240 struct sockaddr_in *server_addr, uint32_t gw_ip)
241{
242 struct dhcpMessage dhcp_msg;
243 fd_set rfds;
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000244 size_t packlen;
245 socklen_t addr_size;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000246 struct sockaddr_in client_addr;
247 struct timeval tv;
248 int i;
249
Denis Vlasenkob9256052007-09-28 10:29:17 +0000250 while (1) {
Denis Vlasenko736230e2006-11-20 19:40:36 +0000251 FD_ZERO(&rfds);
252 for (i = 0; i < num_sockets; i++)
253 FD_SET(fds[i], &rfds);
254 tv.tv_sec = SELECT_TIMEOUT;
255 tv.tv_usec = 0;
256 if (select(max_socket + 1, &rfds, NULL, NULL, &tv) > 0) {
257 /* server */
258 if (FD_ISSET(fds[0], &rfds)) {
Denis Vlasenkofff145d2007-12-20 21:11:38 +0000259 packlen = udhcp_recv_packet(&dhcp_msg, fds[0]);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000260 if (packlen > 0) {
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000261 pass_back(&dhcp_msg, packlen, fds);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000262 }
263 }
264 for (i = 1; i < num_sockets; i++) {
265 /* clients */
266 if (!FD_ISSET(fds[i], &rfds))
267 continue;
268 addr_size = sizeof(struct sockaddr_in);
269 packlen = recvfrom(fds[i], &dhcp_msg, sizeof(dhcp_msg), 0,
270 (struct sockaddr *)(&client_addr), &addr_size);
271 if (packlen <= 0)
272 continue;
Denis Vlasenko80edead2007-08-02 22:31:05 +0000273 if (read_interface(clients[i-1], NULL, &dhcp_msg.giaddr, NULL))
Denis Vlasenko736230e2006-11-20 19:40:36 +0000274 dhcp_msg.giaddr = gw_ip;
275 pass_on(&dhcp_msg, packlen, i, fds, &client_addr, server_addr);
276 }
277 }
278 xid_expire();
279 }
280}
281
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000282int dhcprelay_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000283int dhcprelay_main(int argc, char **argv)
284{
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000285 int num_sockets, max_socket;
286 int *fds;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000287 uint32_t gw_ip;
288 char **clients;
289 struct sockaddr_in server_addr;
290
291 server_addr.sin_family = AF_INET;
292 server_addr.sin_port = htons(67);
293 if (argc == 4) {
294 if (!inet_aton(argv[3], &server_addr.sin_addr))
295 bb_perror_msg_and_die("didn't grok server");
296 } else if (argc == 3) {
297 server_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
298 } else {
299 bb_show_usage();
300 }
Denis Vlasenko736230e2006-11-20 19:40:36 +0000301
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000302 clients = get_client_devices(argv[1], &num_sockets);
Denis Vlasenkob9256052007-09-28 10:29:17 +0000303 num_sockets++; /* for server socket at fds[0] */
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000304 fds = xmalloc(num_sockets * sizeof(fds[0]));
Denis Vlasenkob9256052007-09-28 10:29:17 +0000305 max_socket = init_sockets(clients, num_sockets, argv[2], fds);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000306
Denis Vlasenko80edead2007-08-02 22:31:05 +0000307 if (read_interface(argv[2], NULL, &gw_ip, NULL))
Denis Vlasenko736230e2006-11-20 19:40:36 +0000308 return 1;
309
Denis Vlasenkob9256052007-09-28 10:29:17 +0000310 /* doesn't return */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000311 dhcprelay_loop(fds, num_sockets, max_socket, clients, &server_addr, gw_ip);
Denis Vlasenkob9256052007-09-28 10:29:17 +0000312 /* return 0; - not reached */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000313}