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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 4 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 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 | */ |
Denys Vlasenko | f7683cd | 2016-11-23 18:54:59 +0100 | [diff] [blame] | 12 | //applet:IF_DHCPRELAY(APPLET(dhcprelay, BB_DIR_USR_SBIN, BB_SUID_DROP)) |
| 13 | |
| 14 | //kbuild:lib-$(CONFIG_DHCPRELAY) += dhcprelay.o |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 15 | |
| 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" |
| 20 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 21 | #include "common.h" |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 22 | |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 23 | #define SERVER_PORT 67 |
| 24 | |
| 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) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 29 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 30 | /* This list holds information about clients. The xid_* functions manipulate this list. */ |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 31 | struct xid_item { |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 32 | unsigned timestamp; |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 33 | int client; |
Denis Vlasenko | 35d4da0 | 2007-01-22 14:04:27 +0000 | [diff] [blame] | 34 | uint32_t xid; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 35 | struct sockaddr_in ip; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 36 | struct xid_item *next; |
Denys Vlasenko | df16891 | 2011-01-16 01:25:34 +0100 | [diff] [blame] | 37 | } FIX_ALIASING; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 38 | |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 39 | #define dhcprelay_xid_list (*(struct xid_item*)bb_common_bufsiz1) |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 40 | #define INIT_G() do { setup_common_bufsiz(); } while (0) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 41 | |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 42 | 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] | 43 | { |
| 44 | struct xid_item *item; |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 45 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 46 | /* create new xid entry */ |
| 47 | item = xmalloc(sizeof(struct xid_item)); |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 48 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 49 | /* add xid entry */ |
| 50 | item->ip = *ip; |
| 51 | item->xid = xid; |
| 52 | item->client = client; |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 53 | item->timestamp = monotonic_sec(); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 54 | item->next = dhcprelay_xid_list.next; |
| 55 | dhcprelay_xid_list.next = item; |
| 56 | |
| 57 | return item; |
| 58 | } |
| 59 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 60 | static void xid_expire(void) |
| 61 | { |
| 62 | struct xid_item *item = dhcprelay_xid_list.next; |
| 63 | struct xid_item *last = &dhcprelay_xid_list; |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 64 | unsigned current_time = monotonic_sec(); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 65 | |
| 66 | while (item != NULL) { |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 67 | if ((current_time - item->timestamp) > MAX_LIFETIME) { |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 68 | last->next = item->next; |
| 69 | free(item); |
| 70 | item = last->next; |
| 71 | } else { |
| 72 | last = item; |
| 73 | item = item->next; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 78 | static struct xid_item *xid_find(uint32_t xid) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 79 | { |
| 80 | struct xid_item *item = dhcprelay_xid_list.next; |
| 81 | while (item != NULL) { |
| 82 | if (item->xid == xid) { |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 83 | break; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 84 | } |
| 85 | item = item->next; |
| 86 | } |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 87 | return item; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Denis Vlasenko | 35d4da0 | 2007-01-22 14:04:27 +0000 | [diff] [blame] | 90 | static void xid_del(uint32_t xid) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 91 | { |
| 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; |
| 97 | free(item); |
| 98 | item = last->next; |
| 99 | } else { |
| 100 | last = item; |
| 101 | item = item->next; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 106 | /** |
| 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 |
| 110 | */ |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 111 | static int get_dhcp_packet_type(struct dhcp_packet *p) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 112 | { |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 113 | uint8_t *op; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 114 | |
| 115 | /* it must be either a BOOTREQUEST or a BOOTREPLY */ |
| 116 | if (p->op != BOOTREQUEST && p->op != BOOTREPLY) |
| 117 | return -1; |
| 118 | /* get message type option */ |
Denys Vlasenko | dde8bdc | 2010-03-22 14:29:13 +0100 | [diff] [blame] | 119 | op = udhcp_get_option(p, DHCP_MESSAGE_TYPE); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 120 | if (op != NULL) |
| 121 | return op[0]; |
| 122 | return -1; |
| 123 | } |
| 124 | |
| 125 | /** |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 126 | * make_iface_list - parses client/server interface names |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 127 | * returns array |
| 128 | */ |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 129 | static char **make_iface_list(char **client_and_server_ifaces, int *client_number) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 130 | { |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 131 | char *s, **iface_list; |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 132 | int i, cn; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 133 | |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 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 */ |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 137 | while (*s) { |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 138 | if (*s == ',') |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 139 | cn++; |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 140 | s++; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 141 | } |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 142 | *client_number = cn; |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 143 | |
| 144 | /* create vector of pointers */ |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 145 | iface_list = xzalloc(cn * sizeof(iface_list[0])); |
| 146 | |
| 147 | iface_list[0] = client_and_server_ifaces[1]; /* server iface */ |
| 148 | |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 149 | i = 1; |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 150 | s = xstrdup(client_and_server_ifaces[0]); /* list of client ifaces */ |
| 151 | goto store_client_iface_name; |
| 152 | |
| 153 | while (i < cn) { |
| 154 | if (*s++ == ',') { |
| 155 | s[-1] = '\0'; |
| 156 | store_client_iface_name: |
| 157 | iface_list[i++] = s; |
| 158 | } |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 159 | } |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 160 | |
| 161 | return iface_list; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 164 | /* Creates listen sockets (in fds) bound to client and server ifaces, |
| 165 | * and returns numerically max fd. |
| 166 | */ |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 167 | static int init_sockets(char **iface_list, int num_clients, int *fds) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 168 | { |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 169 | int i, n; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 170 | |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 171 | n = 0; |
| 172 | for (i = 0; i < num_clients; i++) { |
| 173 | fds[i] = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT, iface_list[i]); |
| 174 | if (n < fds[i]) |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 175 | n = fds[i]; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 176 | } |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 177 | return n; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 180 | static int sendto_ip4(int sock, const void *msg, int msg_len, struct sockaddr_in *to) |
| 181 | { |
| 182 | int err; |
| 183 | |
| 184 | errno = 0; |
| 185 | err = sendto(sock, msg, msg_len, 0, (struct sockaddr*) to, sizeof(*to)); |
| 186 | err -= msg_len; |
| 187 | if (err) |
| 188 | bb_perror_msg("sendto"); |
| 189 | return err; |
| 190 | } |
| 191 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 192 | /** |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 193 | * pass_to_server() - forwards dhcp packets from client to server |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 194 | * p - packet to send |
| 195 | * client - number of the client |
| 196 | */ |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 197 | static void pass_to_server(struct dhcp_packet *p, int packet_len, int client, int *fds, |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 198 | struct sockaddr_in *client_addr, struct sockaddr_in *server_addr) |
| 199 | { |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 200 | int type; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 201 | |
| 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 |
| 207 | ) { |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | /* create new xid entry */ |
Denys Vlasenko | 243d175 | 2010-07-04 04:26:55 +0200 | [diff] [blame] | 212 | xid_add(p->xid, client_addr, client); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 213 | |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 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. |
| 218 | */ |
| 219 | sendto_ip4(fds[0], p, packet_len, server_addr); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /** |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 223 | * pass_to_client() - forwards dhcp packets from server to client |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 224 | * p - packet to send |
| 225 | */ |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 226 | static void pass_to_client(struct dhcp_packet *p, int packet_len, int *fds) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 227 | { |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 228 | int type; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 229 | struct xid_item *item; |
| 230 | |
| 231 | /* check xid */ |
| 232 | item = xid_find(p->xid); |
| 233 | if (!item) { |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | /* check packet type */ |
| 238 | type = get_dhcp_packet_type(p); |
| 239 | if (type != DHCPOFFER && type != DHCPACK && type != DHCPNAK) { |
| 240 | return; |
| 241 | } |
| 242 | |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 243 | //TODO: also do it if (p->flags & htons(BROADCAST_FLAG)) is set! |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 244 | if (item->ip.sin_addr.s_addr == htonl(INADDR_ANY)) |
| 245 | item->ip.sin_addr.s_addr = htonl(INADDR_BROADCAST); |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 246 | |
| 247 | if (sendto_ip4(fds[item->client], p, packet_len, &item->ip) != 0) { |
| 248 | return; /* send error occurred */ |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /* remove xid entry */ |
| 252 | xid_del(p->xid); |
| 253 | } |
| 254 | |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 255 | int dhcprelay_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 256 | int dhcprelay_main(int argc, char **argv) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 257 | { |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 258 | struct sockaddr_in server_addr; |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 259 | char **iface_list; |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 260 | int *fds; |
| 261 | int num_sockets, max_socket; |
Denys Vlasenko | 990a617 | 2009-06-16 10:23:55 +0200 | [diff] [blame] | 262 | uint32_t our_nip; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 263 | |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 264 | INIT_G(); |
| 265 | |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 266 | server_addr.sin_family = AF_INET; |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 267 | server_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST); |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 268 | server_addr.sin_port = htons(SERVER_PORT); |
| 269 | |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 270 | /* dhcprelay CLIENT_IFACE1[,CLIENT_IFACE2...] SERVER_IFACE [SERVER_IP] */ |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 271 | if (argc == 4) { |
| 272 | if (!inet_aton(argv[3], &server_addr.sin_addr)) |
| 273 | bb_perror_msg_and_die("bad server IP"); |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 274 | } else if (argc != 3) { |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 275 | bb_show_usage(); |
| 276 | } |
| 277 | |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 278 | iface_list = make_iface_list(argv + 1, &num_sockets); |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 279 | |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 280 | fds = xmalloc(num_sockets * sizeof(fds[0])); |
| 281 | |
| 282 | /* Create sockets and bind one to every iface */ |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 283 | max_socket = init_sockets(iface_list, num_sockets, fds); |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 284 | |
| 285 | /* Get our IP on server_iface */ |
Denys Vlasenko | 990a617 | 2009-06-16 10:23:55 +0200 | [diff] [blame] | 286 | if (udhcp_read_interface(argv[2], NULL, &our_nip, NULL)) |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 287 | return 1; |
| 288 | |
| 289 | /* Main loop */ |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 290 | while (1) { |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 291 | // reinit stuff from time to time? go back to make_iface_list |
| 292 | // every N minutes? |
| 293 | fd_set rfds; |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 294 | struct timeval tv; |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 295 | int i; |
| 296 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 297 | FD_ZERO(&rfds); |
| 298 | for (i = 0; i < num_sockets; i++) |
| 299 | FD_SET(fds[i], &rfds); |
| 300 | tv.tv_sec = SELECT_TIMEOUT; |
| 301 | tv.tv_usec = 0; |
| 302 | if (select(max_socket + 1, &rfds, NULL, NULL, &tv) > 0) { |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 303 | int packlen; |
| 304 | struct dhcp_packet dhcp_msg; |
| 305 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 306 | /* server */ |
| 307 | if (FD_ISSET(fds[0], &rfds)) { |
Denis Vlasenko | 6de8994 | 2008-05-21 07:05:06 +0000 | [diff] [blame] | 308 | packlen = udhcp_recv_kernel_packet(&dhcp_msg, fds[0]); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 309 | if (packlen > 0) { |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 310 | pass_to_client(&dhcp_msg, packlen, fds); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 313 | |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 314 | /* clients */ |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 315 | for (i = 1; i < num_sockets; i++) { |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 316 | struct sockaddr_in client_addr; |
| 317 | socklen_t addr_size; |
| 318 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 319 | if (!FD_ISSET(fds[i], &rfds)) |
| 320 | continue; |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 321 | |
| 322 | addr_size = sizeof(client_addr); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 323 | packlen = recvfrom(fds[i], &dhcp_msg, sizeof(dhcp_msg), 0, |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 324 | (struct sockaddr *)(&client_addr), &addr_size); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 325 | if (packlen <= 0) |
| 326 | continue; |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 327 | |
| 328 | /* Get our IP on corresponding client_iface */ |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 329 | // RFC 1542 |
| 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). |
| 343 | |
| 344 | // but why? what if server can't route such IP? Client ifaces may be, say, NATed! |
| 345 | |
| 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 |
| 353 | // networks. |
| 354 | // |
| 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. |
| 359 | // |
| 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 |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 364 | // directly-connected logical interfaces, the BOOTREPLY message MUST be |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 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! |
Denys Vlasenko | 990a617 | 2009-06-16 10:23:55 +0200 | [diff] [blame] | 369 | dhcp_msg.gateway_nip = our_nip; |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 370 | } |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 371 | // maybe dhcp_msg.hops++? drop packets with too many hops (RFC 1542 says 4 or 16)? |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 372 | pass_to_server(&dhcp_msg, packlen, i, fds, &client_addr, &server_addr); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | xid_expire(); |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 376 | } /* while (1) */ |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 377 | |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 378 | /* return 0; - not reached */ |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 379 | } |