1 /* vi: set sw=4 ts=4: */
2 /* Port to Busybox Copyright (C) 2006 Jesse Dutton <jessedutton@gmail.com>
4 * Licensed under GPLv2, see file LICENSE in this source tree.
6 * DHCP Relay for 'DHCPv4 Configuration of IPSec Tunnel Mode' support
7 * Copyright (C) 2002 Mario Strasser <mast@gmx.net>,
8 * Zuercher Hochschule Winterthur,
10 * Upstream has GPL v2 or later
12 //applet:IF_DHCPRELAY(APPLET(dhcprelay, BB_DIR_USR_SBIN, BB_SUID_DROP))
14 //kbuild:lib-$(CONFIG_DHCPRELAY) += dhcprelay.o
16 //usage:#define dhcprelay_trivial_usage
17 //usage: "CLIENT_IFACE[,CLIENT_IFACE2]... SERVER_IFACE [SERVER_IP]"
18 //usage:#define dhcprelay_full_usage "\n\n"
19 //usage: "Relay DHCP requests between clients and server"
23 #define SERVER_PORT 67
25 /* lifetime of an xid entry in sec. */
26 #define MAX_LIFETIME 2*60
27 /* select timeout in sec. */
28 #define SELECT_TIMEOUT (MAX_LIFETIME / 8)
30 /* This list holds information about clients. The xid_* functions manipulate this list. */
35 struct sockaddr_in ip;
36 struct xid_item *next;
39 #define dhcprelay_xid_list (*(struct xid_item*)bb_common_bufsiz1)
40 #define INIT_G() do { setup_common_bufsiz(); } while (0)
42 static struct xid_item *xid_add(uint32_t xid, struct sockaddr_in *ip, int client)
44 struct xid_item *item;
46 /* create new xid entry */
47 item = xmalloc(sizeof(struct xid_item));
52 item->client = client;
53 item->timestamp = monotonic_sec();
54 item->next = dhcprelay_xid_list.next;
55 dhcprelay_xid_list.next = item;
60 static void xid_expire(void)
62 struct xid_item *item = dhcprelay_xid_list.next;
63 struct xid_item *last = &dhcprelay_xid_list;
64 unsigned current_time = monotonic_sec();
66 while (item != NULL) {
67 if ((current_time - item->timestamp) > MAX_LIFETIME) {
68 last->next = item->next;
78 static struct xid_item *xid_find(uint32_t xid)
80 struct xid_item *item = dhcprelay_xid_list.next;
81 while (item != NULL) {
82 if (item->xid == xid) {
90 static void xid_del(uint32_t xid)
92 struct xid_item *item = dhcprelay_xid_list.next;
93 struct xid_item *last = &dhcprelay_xid_list;
94 while (item != NULL) {
95 if (item->xid == xid) {
96 last->next = item->next;
107 * get_dhcp_packet_type - gets the message type of a dhcp packet
108 * p - pointer to the dhcp packet
109 * returns the message type on success, -1 otherwise
111 static int get_dhcp_packet_type(struct dhcp_packet *p)
115 /* it must be either a BOOTREQUEST or a BOOTREPLY */
116 if (p->op != BOOTREQUEST && p->op != BOOTREPLY)
118 /* get message type option */
119 op = udhcp_get_option(p, DHCP_MESSAGE_TYPE);
126 * make_iface_list - parses client/server interface names
129 static char **make_iface_list(char **client_and_server_ifaces, int *client_number)
131 char *s, **iface_list;
134 /* get number of items */
135 cn = 2; /* 1 server iface + at least 1 client one */
136 s = client_and_server_ifaces[0]; /* list of client ifaces */
144 /* create vector of pointers */
145 iface_list = xzalloc(cn * sizeof(iface_list[0]));
147 iface_list[0] = client_and_server_ifaces[1]; /* server iface */
150 s = xstrdup(client_and_server_ifaces[0]); /* list of client ifaces */
151 goto store_client_iface_name;
156 store_client_iface_name:
164 /* Creates listen sockets (in fds) bound to client and server ifaces,
165 * and returns numerically max fd.
167 static int init_sockets(char **iface_list, int num_clients, int *fds)
172 for (i = 0; i < num_clients; i++) {
173 fds[i] = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT, iface_list[i]);
180 static int sendto_ip4(int sock, const void *msg, int msg_len, struct sockaddr_in *to)
185 err = sendto(sock, msg, msg_len, 0, (struct sockaddr*) to, sizeof(*to));
188 bb_perror_msg("sendto");
193 * pass_to_server() - forwards dhcp packets from client to server
195 * client - number of the client
197 static void pass_to_server(struct dhcp_packet *p, int packet_len, int client, int *fds,
198 struct sockaddr_in *client_addr, struct sockaddr_in *server_addr)
202 /* check packet_type */
203 type = get_dhcp_packet_type(p);
204 if (type != DHCPDISCOVER && type != DHCPREQUEST
205 && type != DHCPDECLINE && type != DHCPRELEASE
206 && type != DHCPINFORM
211 /* create new xid entry */
212 xid_add(p->xid, client_addr, client);
214 /* forward request to server */
215 /* note that we send from fds[0] which is bound to SERVER_PORT (67).
216 * IOW: we send _from_ SERVER_PORT! Although this may look strange,
217 * RFC 1542 not only allows, but prescribes this for BOOTP relays.
219 sendto_ip4(fds[0], p, packet_len, server_addr);
223 * pass_to_client() - forwards dhcp packets from server to client
226 static void pass_to_client(struct dhcp_packet *p, int packet_len, int *fds)
229 struct xid_item *item;
232 item = xid_find(p->xid);
237 /* check packet type */
238 type = get_dhcp_packet_type(p);
239 if (type != DHCPOFFER && type != DHCPACK && type != DHCPNAK) {
243 //TODO: also do it if (p->flags & htons(BROADCAST_FLAG)) is set!
244 if (item->ip.sin_addr.s_addr == htonl(INADDR_ANY))
245 item->ip.sin_addr.s_addr = htonl(INADDR_BROADCAST);
247 if (sendto_ip4(fds[item->client], p, packet_len, &item->ip) != 0) {
248 return; /* send error occurred */
251 /* remove xid entry */
255 int dhcprelay_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
256 int dhcprelay_main(int argc, char **argv)
258 struct sockaddr_in server_addr;
261 int num_sockets, max_socket;
266 server_addr.sin_family = AF_INET;
267 server_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
268 server_addr.sin_port = htons(SERVER_PORT);
270 /* dhcprelay CLIENT_IFACE1[,CLIENT_IFACE2...] SERVER_IFACE [SERVER_IP] */
272 if (!inet_aton(argv[3], &server_addr.sin_addr))
273 bb_perror_msg_and_die("bad server IP");
274 } else if (argc != 3) {
278 iface_list = make_iface_list(argv + 1, &num_sockets);
280 fds = xmalloc(num_sockets * sizeof(fds[0]));
282 /* Create sockets and bind one to every iface */
283 max_socket = init_sockets(iface_list, num_sockets, fds);
285 /* Get our IP on server_iface */
286 if (udhcp_read_interface(argv[2], NULL, &our_nip, NULL))
291 // reinit stuff from time to time? go back to make_iface_list
298 for (i = 0; i < num_sockets; i++)
299 FD_SET(fds[i], &rfds);
300 tv.tv_sec = SELECT_TIMEOUT;
302 if (select(max_socket + 1, &rfds, NULL, NULL, &tv) > 0) {
304 struct dhcp_packet dhcp_msg;
307 if (FD_ISSET(fds[0], &rfds)) {
308 packlen = udhcp_recv_kernel_packet(&dhcp_msg, fds[0]);
310 pass_to_client(&dhcp_msg, packlen, fds);
315 for (i = 1; i < num_sockets; i++) {
316 struct sockaddr_in client_addr;
319 if (!FD_ISSET(fds[i], &rfds))
322 addr_size = sizeof(client_addr);
323 packlen = recvfrom(fds[i], &dhcp_msg, sizeof(dhcp_msg), 0,
324 (struct sockaddr *)(&client_addr), &addr_size);
328 /* Get our IP on corresponding client_iface */
330 // 4.1 General BOOTP Processing for Relay Agents
331 // 4.1.1 BOOTREQUEST Messages
332 // If the relay agent does decide to relay the request, it MUST examine
333 // the 'giaddr' ("gateway" IP address) field. If this field is zero,
334 // the relay agent MUST fill this field with the IP address of the
335 // interface on which the request was received. If the interface has
336 // more than one IP address logically associated with it, the relay
337 // agent SHOULD choose one IP address associated with that interface and
338 // use it consistently for all BOOTP messages it relays. If the
339 // 'giaddr' field contains some non-zero value, the 'giaddr' field MUST
340 // NOT be modified. The relay agent MUST NOT, under any circumstances,
341 // fill the 'giaddr' field with a broadcast address as is suggested in
342 // [1] (Section 8, sixth paragraph).
344 // but why? what if server can't route such IP? Client ifaces may be, say, NATed!
346 // 4.1.2 BOOTREPLY Messages
347 // BOOTP relay agents relay BOOTREPLY messages only to BOOTP clients.
348 // It is the responsibility of BOOTP servers to send BOOTREPLY messages
349 // directly to the relay agent identified in the 'giaddr' field.
350 // (yeah right, unless it is impossible... see comment above)
351 // Therefore, a relay agent may assume that all BOOTREPLY messages it
352 // receives are intended for BOOTP clients on its directly-connected
355 // When a relay agent receives a BOOTREPLY message, it should examine
356 // the BOOTP 'giaddr', 'yiaddr', 'chaddr', 'htype', and 'hlen' fields.
357 // These fields should provide adequate information for the relay agent
358 // to deliver the BOOTREPLY message to the client.
360 // The 'giaddr' field can be used to identify the logical interface from
361 // which the reply must be sent (i.e., the host or router interface
362 // connected to the same network as the BOOTP client). If the content
363 // of the 'giaddr' field does not match one of the relay agent's
364 // directly-connected logical interfaces, the BOOTREPLY message MUST be
365 // silently discarded.
366 if (udhcp_read_interface(iface_list[i], NULL, &dhcp_msg.gateway_nip, NULL)) {
367 /* Fall back to our IP on server iface */
368 // this makes more sense!
369 dhcp_msg.gateway_nip = our_nip;
371 // maybe dhcp_msg.hops++? drop packets with too many hops (RFC 1542 says 4 or 16)?
372 pass_to_server(&dhcp_msg, packlen, i, fds, &client_addr, &server_addr);
378 /* return 0; - not reached */