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" |
| 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 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 22 | /* This list holds information about clients. The xid_* functions manipulate this list. */ |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 23 | struct xid_item { |
| 24 | time_t timestamp; |
| 25 | int client; |
Denis Vlasenko | 35d4da0 | 2007-01-22 14:04:27 +0000 | [diff] [blame] | 26 | uint32_t xid; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 27 | struct sockaddr_in ip; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 28 | struct xid_item *next; |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 29 | }; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 30 | |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 31 | #define dhcprelay_xid_list (*(struct xid_item*)&bb_common_bufsiz1) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 32 | |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 33 | 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] | 34 | { |
| 35 | struct xid_item *item; |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 36 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 37 | /* create new xid entry */ |
| 38 | item = xmalloc(sizeof(struct xid_item)); |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 39 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 40 | /* 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 Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 51 | static 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 Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 58 | if ((current_time - item->timestamp) > MAX_LIFETIME) { |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 59 | 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 Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 69 | static struct xid_item *xid_find(uint32_t xid) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 70 | { |
| 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 Vlasenko | 35d4da0 | 2007-01-22 14:04:27 +0000 | [diff] [blame] | 81 | static void xid_del(uint32_t xid) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 82 | { |
| 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 Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 97 | /** |
| 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 | */ |
| 102 | static int get_dhcp_packet_type(struct dhcpMessage *p) |
| 103 | { |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 104 | uint8_t *op; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 105 | |
| 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 | /** |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 117 | * get_client_devices - parses the devices list |
| 118 | * dev_list - comma separated list of devices |
| 119 | * returns array |
| 120 | */ |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 121 | static char **get_client_devices(char *dev_list, int *client_number) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 122 | { |
| 123 | char *s, *list, **client_dev; |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 124 | int i, cn; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 125 | |
| 126 | /* copy list */ |
| 127 | list = xstrdup(dev_list); |
| 128 | if (list == NULL) return NULL; |
| 129 | |
| 130 | /* get number of items */ |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 131 | for (s = dev_list, cn = 1; *s; s++) |
| 132 | if (*s == ',') |
| 133 | cn++; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 134 | |
| 135 | client_dev = xzalloc(cn * sizeof(*client_dev)); |
| 136 | |
| 137 | /* parse list */ |
| 138 | s = strtok(list, ","); |
| 139 | i = 0; |
| 140 | while (s != NULL) { |
| 141 | client_dev[i++] = xstrdup(s); |
| 142 | s = strtok(NULL, ","); |
| 143 | } |
| 144 | |
| 145 | /* free copy and exit */ |
| 146 | free(list); |
| 147 | *client_number = cn; |
| 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 | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 159 | fds[0] = listen_socket(/*INADDR_ANY,*/ 67, 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 | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 164 | fds[i] = listen_socket(/*NADDR_ANY,*/ 67, 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); |
| 227 | if (item->client > MAX_INTERFACES) |
| 228 | return; |
| 229 | res = sendto(fds[item->client], p, packet_len, 0, (struct sockaddr*)(&item->ip), |
| 230 | sizeof(item->ip)); |
| 231 | if (res != packet_len) { |
| 232 | bb_perror_msg("pass_back"); |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | /* remove xid entry */ |
| 237 | xid_del(p->xid); |
| 238 | } |
| 239 | |
| 240 | static void dhcprelay_loop(int *fds, int num_sockets, int max_socket, char **clients, |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 241 | struct sockaddr_in *server_addr, uint32_t gw_ip) ATTRIBUTE_NORETURN; |
| 242 | 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] | 243 | struct sockaddr_in *server_addr, uint32_t gw_ip) |
| 244 | { |
| 245 | struct dhcpMessage dhcp_msg; |
| 246 | fd_set rfds; |
Denis Vlasenko | 666da5e | 2006-12-26 18:17:42 +0000 | [diff] [blame] | 247 | size_t packlen; |
| 248 | socklen_t addr_size; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 249 | struct sockaddr_in client_addr; |
| 250 | struct timeval tv; |
| 251 | int i; |
| 252 | |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 253 | while (1) { |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 254 | FD_ZERO(&rfds); |
| 255 | for (i = 0; i < num_sockets; i++) |
| 256 | FD_SET(fds[i], &rfds); |
| 257 | tv.tv_sec = SELECT_TIMEOUT; |
| 258 | tv.tv_usec = 0; |
| 259 | if (select(max_socket + 1, &rfds, NULL, NULL, &tv) > 0) { |
| 260 | /* server */ |
| 261 | if (FD_ISSET(fds[0], &rfds)) { |
| 262 | packlen = udhcp_get_packet(&dhcp_msg, fds[0]); |
| 263 | if (packlen > 0) { |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 264 | pass_back(&dhcp_msg, packlen, fds); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | for (i = 1; i < num_sockets; i++) { |
| 268 | /* clients */ |
| 269 | if (!FD_ISSET(fds[i], &rfds)) |
| 270 | continue; |
| 271 | addr_size = sizeof(struct sockaddr_in); |
| 272 | packlen = recvfrom(fds[i], &dhcp_msg, sizeof(dhcp_msg), 0, |
| 273 | (struct sockaddr *)(&client_addr), &addr_size); |
| 274 | if (packlen <= 0) |
| 275 | continue; |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 276 | if (read_interface(clients[i-1], NULL, &dhcp_msg.giaddr, NULL)) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 277 | dhcp_msg.giaddr = gw_ip; |
| 278 | pass_on(&dhcp_msg, packlen, i, fds, &client_addr, server_addr); |
| 279 | } |
| 280 | } |
| 281 | xid_expire(); |
| 282 | } |
| 283 | } |
| 284 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 285 | int dhcprelay_main(int argc, char **argv); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 286 | int dhcprelay_main(int argc, char **argv) |
| 287 | { |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 288 | int num_sockets, max_socket, fds[MAX_INTERFACES]; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 289 | uint32_t gw_ip; |
| 290 | char **clients; |
| 291 | struct sockaddr_in server_addr; |
| 292 | |
| 293 | server_addr.sin_family = AF_INET; |
| 294 | server_addr.sin_port = htons(67); |
| 295 | if (argc == 4) { |
| 296 | if (!inet_aton(argv[3], &server_addr.sin_addr)) |
| 297 | bb_perror_msg_and_die("didn't grok server"); |
| 298 | } else if (argc == 3) { |
| 299 | server_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST); |
| 300 | } else { |
| 301 | bb_show_usage(); |
| 302 | } |
| 303 | clients = get_client_devices(argv[1], &num_sockets); |
| 304 | if (!clients) return 0; |
| 305 | |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 306 | num_sockets++; /* for server socket at fds[0] */ |
| 307 | max_socket = init_sockets(clients, num_sockets, argv[2], fds); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 308 | |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 309 | if (read_interface(argv[2], NULL, &gw_ip, NULL)) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 310 | return 1; |
| 311 | |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 312 | /* doesn't return */ |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 313 | dhcprelay_loop(fds, num_sockets, max_socket, clients, &server_addr, gw_ip); |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 314 | /* return 0; - not reached */ |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 315 | } |