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 | |
| 22 | |
| 23 | /* This list holds information about clients. The xid_* functions manipulate this list. */ |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 24 | static struct xid_item { |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 25 | u_int32_t xid; |
| 26 | 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 | |
| 33 | static struct xid_item * xid_add(u_int32_t xid, struct sockaddr_in *ip, int client) |
| 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 | |
| 51 | |
| 52 | static void xid_expire(void) |
| 53 | { |
| 54 | struct xid_item *item = dhcprelay_xid_list.next; |
| 55 | struct xid_item *last = &dhcprelay_xid_list; |
| 56 | time_t current_time = time(NULL); |
| 57 | |
| 58 | while (item != NULL) { |
| 59 | if ((current_time-item->timestamp) > MAX_LIFETIME) { |
| 60 | last->next = item->next; |
| 61 | free(item); |
| 62 | item = last->next; |
| 63 | } else { |
| 64 | last = item; |
| 65 | item = item->next; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | static struct xid_item * xid_find(u_int32_t xid) |
| 71 | { |
| 72 | struct xid_item *item = dhcprelay_xid_list.next; |
| 73 | while (item != NULL) { |
| 74 | if (item->xid == xid) { |
| 75 | return item; |
| 76 | } |
| 77 | item = item->next; |
| 78 | } |
| 79 | return NULL; |
| 80 | } |
| 81 | |
| 82 | static void xid_del(u_int32_t xid) |
| 83 | { |
| 84 | struct xid_item *item = dhcprelay_xid_list.next; |
| 85 | struct xid_item *last = &dhcprelay_xid_list; |
| 86 | while (item != NULL) { |
| 87 | if (item->xid == xid) { |
| 88 | last->next = item->next; |
| 89 | free(item); |
| 90 | item = last->next; |
| 91 | } else { |
| 92 | last = item; |
| 93 | item = item->next; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | |
| 99 | /** |
| 100 | * get_dhcp_packet_type - gets the message type of a dhcp packet |
| 101 | * p - pointer to the dhcp packet |
| 102 | * returns the message type on success, -1 otherwise |
| 103 | */ |
| 104 | static int get_dhcp_packet_type(struct dhcpMessage *p) |
| 105 | { |
| 106 | u_char *op; |
| 107 | |
| 108 | /* it must be either a BOOTREQUEST or a BOOTREPLY */ |
| 109 | if (p->op != BOOTREQUEST && p->op != BOOTREPLY) |
| 110 | return -1; |
| 111 | /* get message type option */ |
| 112 | op = get_option(p, DHCP_MESSAGE_TYPE); |
| 113 | if (op != NULL) |
| 114 | return op[0]; |
| 115 | return -1; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * signal_handler - handles signals ;-) |
| 120 | * sig - sent signal |
| 121 | */ |
| 122 | static int dhcprelay_stopflag; |
| 123 | static void dhcprelay_signal_handler(int sig) |
| 124 | { |
| 125 | dhcprelay_stopflag = 1; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * get_client_devices - parses the devices list |
| 130 | * dev_list - comma separated list of devices |
| 131 | * returns array |
| 132 | */ |
| 133 | static char ** get_client_devices(char *dev_list, int *client_number) |
| 134 | { |
| 135 | char *s, *list, **client_dev; |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 136 | int i, cn; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 137 | |
| 138 | /* copy list */ |
| 139 | list = xstrdup(dev_list); |
| 140 | if (list == NULL) return NULL; |
| 141 | |
| 142 | /* get number of items */ |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 143 | for (s = dev_list, cn = 1; *s; s++) |
| 144 | if (*s == ',') |
| 145 | cn++; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 146 | |
| 147 | client_dev = xzalloc(cn * sizeof(*client_dev)); |
| 148 | |
| 149 | /* parse list */ |
| 150 | s = strtok(list, ","); |
| 151 | i = 0; |
| 152 | while (s != NULL) { |
| 153 | client_dev[i++] = xstrdup(s); |
| 154 | s = strtok(NULL, ","); |
| 155 | } |
| 156 | |
| 157 | /* free copy and exit */ |
| 158 | free(list); |
| 159 | *client_number = cn; |
| 160 | return client_dev; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | /* Creates listen sockets (in fds) and returns the number allocated. */ |
| 165 | static int init_sockets(char **client, int num_clients, |
| 166 | char *server, int *fds, int *max_socket) |
| 167 | { |
| 168 | int i; |
| 169 | |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 170 | /* talk to real server on bootps */ |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 171 | fds[0] = listen_socket(htonl(INADDR_ANY), 67, server); |
| 172 | if (fds[0] < 0) return -1; |
| 173 | *max_socket = fds[0]; |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 174 | |
| 175 | /* array starts at 1 since server is 0 */ |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 176 | num_clients++; |
| 177 | |
| 178 | for (i=1; i < num_clients; i++) { |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 179 | /* listen for clients on bootps */ |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 180 | fds[i] = listen_socket(htonl(INADDR_ANY), 67, client[i-1]); |
| 181 | if (fds[i] < 0) return -1; |
| 182 | if (fds[i] > *max_socket) *max_socket = fds[i]; |
| 183 | } |
| 184 | |
| 185 | return i; |
| 186 | } |
| 187 | |
| 188 | |
| 189 | /** |
| 190 | * pass_on() - forwards dhcp packets from client to server |
| 191 | * p - packet to send |
| 192 | * client - number of the client |
| 193 | */ |
| 194 | static void pass_on(struct dhcpMessage *p, int packet_len, int client, int *fds, |
| 195 | struct sockaddr_in *client_addr, struct sockaddr_in *server_addr) |
| 196 | { |
| 197 | int res, type; |
| 198 | struct xid_item *item; |
| 199 | |
| 200 | /* check packet_type */ |
| 201 | type = get_dhcp_packet_type(p); |
| 202 | if (type != DHCPDISCOVER && type != DHCPREQUEST |
| 203 | && type != DHCPDECLINE && type != DHCPRELEASE |
| 204 | && type != DHCPINFORM |
| 205 | ) { |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | /* create new xid entry */ |
| 210 | item = xid_add(p->xid, client_addr, client); |
| 211 | |
| 212 | /* forward request to LAN (server) */ |
| 213 | res = sendto(fds[0], p, packet_len, 0, (struct sockaddr*)server_addr, |
| 214 | sizeof(struct sockaddr_in)); |
| 215 | if (res != packet_len) { |
| 216 | bb_perror_msg("pass_on"); |
| 217 | return; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * pass_back() - forwards dhcp packets from server to client |
| 223 | * p - packet to send |
| 224 | */ |
| 225 | static void pass_back(struct dhcpMessage *p, int packet_len, int *fds) |
| 226 | { |
| 227 | int res, type; |
| 228 | struct xid_item *item; |
| 229 | |
| 230 | /* check xid */ |
| 231 | item = xid_find(p->xid); |
| 232 | if (!item) { |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | /* check packet type */ |
| 237 | type = get_dhcp_packet_type(p); |
| 238 | if (type != DHCPOFFER && type != DHCPACK && type != DHCPNAK) { |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | if (item->ip.sin_addr.s_addr == htonl(INADDR_ANY)) |
| 243 | item->ip.sin_addr.s_addr = htonl(INADDR_BROADCAST); |
| 244 | if (item->client > MAX_INTERFACES) |
| 245 | return; |
| 246 | res = sendto(fds[item->client], p, packet_len, 0, (struct sockaddr*)(&item->ip), |
| 247 | sizeof(item->ip)); |
| 248 | if (res != packet_len) { |
| 249 | bb_perror_msg("pass_back"); |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | /* remove xid entry */ |
| 254 | xid_del(p->xid); |
| 255 | } |
| 256 | |
| 257 | static void dhcprelay_loop(int *fds, int num_sockets, int max_socket, char **clients, |
| 258 | struct sockaddr_in *server_addr, uint32_t gw_ip) |
| 259 | { |
| 260 | struct dhcpMessage dhcp_msg; |
| 261 | fd_set rfds; |
| 262 | size_t packlen, addr_size; |
| 263 | struct sockaddr_in client_addr; |
| 264 | struct timeval tv; |
| 265 | int i; |
| 266 | |
| 267 | while (!dhcprelay_stopflag) { |
| 268 | FD_ZERO(&rfds); |
| 269 | for (i = 0; i < num_sockets; i++) |
| 270 | FD_SET(fds[i], &rfds); |
| 271 | tv.tv_sec = SELECT_TIMEOUT; |
| 272 | tv.tv_usec = 0; |
| 273 | if (select(max_socket + 1, &rfds, NULL, NULL, &tv) > 0) { |
| 274 | /* server */ |
| 275 | if (FD_ISSET(fds[0], &rfds)) { |
| 276 | packlen = udhcp_get_packet(&dhcp_msg, fds[0]); |
| 277 | if (packlen > 0) { |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 278 | pass_back(&dhcp_msg, packlen, fds); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | for (i = 1; i < num_sockets; i++) { |
| 282 | /* clients */ |
| 283 | if (!FD_ISSET(fds[i], &rfds)) |
| 284 | continue; |
| 285 | addr_size = sizeof(struct sockaddr_in); |
| 286 | packlen = recvfrom(fds[i], &dhcp_msg, sizeof(dhcp_msg), 0, |
| 287 | (struct sockaddr *)(&client_addr), &addr_size); |
| 288 | if (packlen <= 0) |
| 289 | continue; |
| 290 | if (read_interface(clients[i-1], NULL, &dhcp_msg.giaddr, NULL) < 0) |
| 291 | dhcp_msg.giaddr = gw_ip; |
| 292 | pass_on(&dhcp_msg, packlen, i, fds, &client_addr, server_addr); |
| 293 | } |
| 294 | } |
| 295 | xid_expire(); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | int dhcprelay_main(int argc, char **argv) |
| 300 | { |
| 301 | int i, num_sockets, max_socket, fds[MAX_INTERFACES]; |
| 302 | uint32_t gw_ip; |
| 303 | char **clients; |
| 304 | struct sockaddr_in server_addr; |
| 305 | |
| 306 | server_addr.sin_family = AF_INET; |
| 307 | server_addr.sin_port = htons(67); |
| 308 | if (argc == 4) { |
| 309 | if (!inet_aton(argv[3], &server_addr.sin_addr)) |
| 310 | bb_perror_msg_and_die("didn't grok server"); |
| 311 | } else if (argc == 3) { |
| 312 | server_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST); |
| 313 | } else { |
| 314 | bb_show_usage(); |
| 315 | } |
| 316 | clients = get_client_devices(argv[1], &num_sockets); |
| 317 | if (!clients) return 0; |
| 318 | |
| 319 | signal(SIGTERM, dhcprelay_signal_handler); |
| 320 | signal(SIGQUIT, dhcprelay_signal_handler); |
| 321 | signal(SIGINT, dhcprelay_signal_handler); |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 322 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 323 | num_sockets = init_sockets(clients, num_sockets, argv[2], fds, &max_socket); |
| 324 | if (num_sockets == -1) |
| 325 | bb_perror_msg_and_die("init_sockets() failed"); |
| 326 | |
| 327 | if (read_interface(argv[2], NULL, &gw_ip, NULL) == -1) |
| 328 | return 1; |
| 329 | |
| 330 | dhcprelay_loop(fds, num_sockets, max_socket, clients, &server_addr, gw_ip); |
| 331 | |
| 332 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 333 | for (i = 0; i < num_sockets; i++) { |
| 334 | close(fds[i]); |
| 335 | free(clients[i]); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | return 0; |
| 340 | } |