Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 1 | /* 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-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 6 | * DHCP Relay for 'DHCPv4 Configuration of IPSec Tunnel Mode' support |
| 7 | * Copyright (C) 2002 Mario Strasser <mast@gmx.net>, |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 8 | * Zuercher Hochschule Winterthur, |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 9 | * Netbeat AG |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 10 | * Upstream has GPL v2 or later |
| 11 | */ |
| 12 | |
| 13 | #include "common.h" |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 14 | #include "options.h" |
| 15 | |
| 16 | /* constants */ |
Denis Vlasenko | d55fe3e | 2008-02-04 13:12:16 +0000 | [diff] [blame] | 17 | #define SERVER_PORT 67 |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 18 | #define SELECT_TIMEOUT 5 /* select timeout in sec. */ |
| 19 | #define MAX_LIFETIME 2*60 /* lifetime of an xid entry in sec. */ |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 20 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 21 | /* This list holds information about clients. The xid_* functions manipulate this list. */ |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 22 | struct xid_item { |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 23 | unsigned timestamp; |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 24 | int client; |
Denis Vlasenko | 35d4da0 | 2007-01-22 14:04:27 +0000 | [diff] [blame] | 25 | uint32_t xid; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 26 | struct sockaddr_in ip; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 27 | struct xid_item *next; |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 28 | }; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 29 | |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 30 | #define dhcprelay_xid_list (*(struct xid_item*)&bb_common_bufsiz1) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 31 | |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 32 | static struct xid_item *xid_add(uint32_t xid, struct sockaddr_in *ip, int client) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 33 | { |
| 34 | struct xid_item *item; |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 35 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 36 | /* create new xid entry */ |
| 37 | item = xmalloc(sizeof(struct xid_item)); |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 38 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 39 | /* add xid entry */ |
| 40 | item->ip = *ip; |
| 41 | item->xid = xid; |
| 42 | item->client = client; |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 43 | item->timestamp = monotonic_sec(); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 44 | item->next = dhcprelay_xid_list.next; |
| 45 | dhcprelay_xid_list.next = item; |
| 46 | |
| 47 | return item; |
| 48 | } |
| 49 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 50 | static void xid_expire(void) |
| 51 | { |
| 52 | struct xid_item *item = dhcprelay_xid_list.next; |
| 53 | struct xid_item *last = &dhcprelay_xid_list; |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 54 | unsigned current_time = monotonic_sec(); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 55 | |
| 56 | while (item != NULL) { |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 57 | if ((current_time - item->timestamp) > MAX_LIFETIME) { |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 58 | 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 Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 68 | static struct xid_item *xid_find(uint32_t xid) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 69 | { |
| 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 Vlasenko | 35d4da0 | 2007-01-22 14:04:27 +0000 | [diff] [blame] | 80 | static void xid_del(uint32_t xid) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 81 | { |
| 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 Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 96 | /** |
| 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 | */ |
| 101 | static int get_dhcp_packet_type(struct dhcpMessage *p) |
| 102 | { |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 103 | uint8_t *op; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 104 | |
| 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 Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 116 | * get_client_devices - parses the devices list |
| 117 | * dev_list - comma separated list of devices |
| 118 | * returns array |
| 119 | */ |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 120 | static char **get_client_devices(char *dev_list, int *client_number) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 121 | { |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 122 | char *s, **client_dev; |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 123 | int i, cn; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 124 | |
| 125 | /* copy list */ |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 126 | dev_list = xstrdup(dev_list); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 127 | |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 128 | /* 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-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 134 | cn++; |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 135 | } |
| 136 | s++; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 137 | } |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 138 | *client_number = cn; |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 139 | |
| 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 Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 148 | return client_dev; |
| 149 | } |
| 150 | |
| 151 | |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 152 | /* Creates listen sockets (in fds) and returns numerically max fd. */ |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 153 | static int init_sockets(char **client, int num_clients, |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 154 | char *server, int *fds) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 155 | { |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 156 | int i, n; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 157 | |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 158 | /* talk to real server on bootps */ |
Denis Vlasenko | b76b9a4 | 2008-01-25 22:46:34 +0000 | [diff] [blame] | 159 | fds[0] = listen_socket(/*INADDR_ANY,*/ SERVER_PORT, server); |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 160 | n = fds[0]; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 161 | |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 162 | for (i = 1; i < num_clients; i++) { |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 163 | /* listen for clients on bootps */ |
Denis Vlasenko | b76b9a4 | 2008-01-25 22:46:34 +0000 | [diff] [blame] | 164 | fds[i] = listen_socket(/*INADDR_ANY,*/ SERVER_PORT, client[i-1]); |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 165 | if (fds[i] > n) |
| 166 | n = fds[i]; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 167 | } |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 168 | return n; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 169 | } |
| 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 | */ |
| 177 | static 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 | */ |
| 208 | static 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 Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 227 | 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 | |
| 238 | static void dhcprelay_loop(int *fds, int num_sockets, int max_socket, char **clients, |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 239 | struct sockaddr_in *server_addr, uint32_t gw_ip) NORETURN; |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 240 | static void dhcprelay_loop(int *fds, int num_sockets, int max_socket, char **clients, |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 241 | struct sockaddr_in *server_addr, uint32_t gw_ip) |
| 242 | { |
| 243 | struct dhcpMessage dhcp_msg; |
| 244 | fd_set rfds; |
Denis Vlasenko | 666da5e | 2006-12-26 18:17:42 +0000 | [diff] [blame] | 245 | size_t packlen; |
| 246 | socklen_t addr_size; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 247 | struct sockaddr_in client_addr; |
| 248 | struct timeval tv; |
| 249 | int i; |
| 250 | |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 251 | while (1) { |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 252 | 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 Vlasenko | 6de8994 | 2008-05-21 07:05:06 +0000 | [diff] [blame] | 260 | packlen = udhcp_recv_kernel_packet(&dhcp_msg, fds[0]); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 261 | if (packlen > 0) { |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 262 | pass_back(&dhcp_msg, packlen, fds); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 263 | } |
| 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 Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 274 | if (read_interface(clients[i-1], NULL, &dhcp_msg.giaddr, NULL)) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 275 | 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 Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 283 | int dhcprelay_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 284 | int dhcprelay_main(int argc, char **argv) |
| 285 | { |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 286 | int num_sockets, max_socket; |
| 287 | int *fds; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 288 | uint32_t gw_ip; |
| 289 | char **clients; |
| 290 | struct sockaddr_in server_addr; |
| 291 | |
| 292 | server_addr.sin_family = AF_INET; |
Denis Vlasenko | b76b9a4 | 2008-01-25 22:46:34 +0000 | [diff] [blame] | 293 | server_addr.sin_port = htons(SERVER_PORT); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 294 | 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 Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 302 | |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 303 | clients = get_client_devices(argv[1], &num_sockets); |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 304 | num_sockets++; /* for server socket at fds[0] */ |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 305 | fds = xmalloc(num_sockets * sizeof(fds[0])); |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 306 | max_socket = init_sockets(clients, num_sockets, argv[2], fds); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 307 | |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 308 | if (read_interface(argv[2], NULL, &gw_ip, NULL)) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 309 | return 1; |
| 310 | |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 311 | /* doesn't return */ |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 312 | dhcprelay_loop(fds, num_sockets, max_socket, clients, &server_addr, gw_ip); |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 313 | /* return 0; - not reached */ |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 314 | } |