Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Denys Vlasenko | ebe6d9d | 2017-10-05 14:40:24 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Port to Busybox Copyright (C) 2006 Jesse Dutton <jessedutton@gmail.com> |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 4 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 5 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 6 | * |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 7 | * DHCP Relay for 'DHCPv4 Configuration of IPSec Tunnel Mode' support |
| 8 | * Copyright (C) 2002 Mario Strasser <mast@gmx.net>, |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 9 | * Zuercher Hochschule Winterthur, |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 10 | * Netbeat AG |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 11 | * Upstream has GPL v2 or later |
| 12 | */ |
Denys Vlasenko | f7683cd | 2016-11-23 18:54:59 +0100 | [diff] [blame] | 13 | //applet:IF_DHCPRELAY(APPLET(dhcprelay, BB_DIR_USR_SBIN, BB_SUID_DROP)) |
| 14 | |
| 15 | //kbuild:lib-$(CONFIG_DHCPRELAY) += dhcprelay.o |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 16 | |
| 17 | //usage:#define dhcprelay_trivial_usage |
| 18 | //usage: "CLIENT_IFACE[,CLIENT_IFACE2]... SERVER_IFACE [SERVER_IP]" |
| 19 | //usage:#define dhcprelay_full_usage "\n\n" |
| 20 | //usage: "Relay DHCP requests between clients and server" |
| 21 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 22 | #include "common.h" |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 23 | |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 24 | #define SERVER_PORT 67 |
| 25 | |
| 26 | /* lifetime of an xid entry in sec. */ |
| 27 | #define MAX_LIFETIME 2*60 |
| 28 | /* select timeout in sec. */ |
| 29 | #define SELECT_TIMEOUT (MAX_LIFETIME / 8) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 30 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 31 | /* This list holds information about clients. The xid_* functions manipulate this list. */ |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 32 | struct xid_item { |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 33 | unsigned timestamp; |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 34 | int client; |
Denis Vlasenko | 35d4da0 | 2007-01-22 14:04:27 +0000 | [diff] [blame] | 35 | uint32_t xid; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 36 | struct sockaddr_in ip; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 37 | struct xid_item *next; |
Denys Vlasenko | df16891 | 2011-01-16 01:25:34 +0100 | [diff] [blame] | 38 | } FIX_ALIASING; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 39 | |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 40 | #define dhcprelay_xid_list (*(struct xid_item*)bb_common_bufsiz1) |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 41 | #define INIT_G() do { setup_common_bufsiz(); } while (0) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 42 | |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 43 | 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] | 44 | { |
| 45 | struct xid_item *item; |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 46 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 47 | /* create new xid entry */ |
| 48 | item = xmalloc(sizeof(struct xid_item)); |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 49 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 50 | /* add xid entry */ |
| 51 | item->ip = *ip; |
| 52 | item->xid = xid; |
| 53 | item->client = client; |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 54 | item->timestamp = monotonic_sec(); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 55 | item->next = dhcprelay_xid_list.next; |
| 56 | dhcprelay_xid_list.next = item; |
| 57 | |
| 58 | return item; |
| 59 | } |
| 60 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 61 | static void xid_expire(void) |
| 62 | { |
| 63 | struct xid_item *item = dhcprelay_xid_list.next; |
| 64 | struct xid_item *last = &dhcprelay_xid_list; |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 65 | unsigned current_time = monotonic_sec(); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 66 | |
| 67 | while (item != NULL) { |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 68 | if ((current_time - item->timestamp) > MAX_LIFETIME) { |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 69 | last->next = item->next; |
| 70 | free(item); |
| 71 | item = last->next; |
| 72 | } else { |
| 73 | last = item; |
| 74 | item = item->next; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 79 | static struct xid_item *xid_find(uint32_t xid) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 80 | { |
| 81 | struct xid_item *item = dhcprelay_xid_list.next; |
| 82 | while (item != NULL) { |
| 83 | if (item->xid == xid) { |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 84 | break; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 85 | } |
| 86 | item = item->next; |
| 87 | } |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 88 | return item; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Denis Vlasenko | 35d4da0 | 2007-01-22 14:04:27 +0000 | [diff] [blame] | 91 | static void xid_del(uint32_t xid) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 92 | { |
| 93 | struct xid_item *item = dhcprelay_xid_list.next; |
| 94 | struct xid_item *last = &dhcprelay_xid_list; |
| 95 | while (item != NULL) { |
| 96 | if (item->xid == xid) { |
| 97 | last->next = item->next; |
| 98 | free(item); |
| 99 | item = last->next; |
| 100 | } else { |
| 101 | last = item; |
| 102 | item = item->next; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 107 | /** |
| 108 | * get_dhcp_packet_type - gets the message type of a dhcp packet |
| 109 | * p - pointer to the dhcp packet |
| 110 | * returns the message type on success, -1 otherwise |
| 111 | */ |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 112 | static int get_dhcp_packet_type(struct dhcp_packet *p) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 113 | { |
Denis Vlasenko | 2870301 | 2006-12-19 20:32:02 +0000 | [diff] [blame] | 114 | uint8_t *op; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 115 | |
| 116 | /* it must be either a BOOTREQUEST or a BOOTREPLY */ |
| 117 | if (p->op != BOOTREQUEST && p->op != BOOTREPLY) |
| 118 | return -1; |
| 119 | /* get message type option */ |
Denys Vlasenko | dde8bdc | 2010-03-22 14:29:13 +0100 | [diff] [blame] | 120 | op = udhcp_get_option(p, DHCP_MESSAGE_TYPE); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 121 | if (op != NULL) |
| 122 | return op[0]; |
| 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | /** |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 127 | * make_iface_list - parses client/server interface names |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 128 | * returns array |
| 129 | */ |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 130 | 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] | 131 | { |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 132 | char *s, **iface_list; |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 133 | int i, cn; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 134 | |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 135 | /* get number of items */ |
| 136 | cn = 2; /* 1 server iface + at least 1 client one */ |
| 137 | s = client_and_server_ifaces[0]; /* list of client ifaces */ |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 138 | while (*s) { |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 139 | if (*s == ',') |
Bernhard Reutner-Fischer | a838194 | 2006-11-21 11:23:11 +0000 | [diff] [blame] | 140 | cn++; |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 141 | s++; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 142 | } |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 143 | *client_number = cn; |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 144 | |
| 145 | /* create vector of pointers */ |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 146 | iface_list = xzalloc(cn * sizeof(iface_list[0])); |
| 147 | |
| 148 | iface_list[0] = client_and_server_ifaces[1]; /* server iface */ |
| 149 | |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 150 | i = 1; |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 151 | s = xstrdup(client_and_server_ifaces[0]); /* list of client ifaces */ |
| 152 | goto store_client_iface_name; |
| 153 | |
| 154 | while (i < cn) { |
| 155 | if (*s++ == ',') { |
| 156 | s[-1] = '\0'; |
| 157 | store_client_iface_name: |
| 158 | iface_list[i++] = s; |
| 159 | } |
Denis Vlasenko | d7b3350 | 2007-09-30 17:54:10 +0000 | [diff] [blame] | 160 | } |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 161 | |
| 162 | return iface_list; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 165 | /* Creates listen sockets (in fds) bound to client and server ifaces, |
| 166 | * and returns numerically max fd. |
| 167 | */ |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 168 | static int init_sockets(char **iface_list, int num_clients, int *fds) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 169 | { |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 170 | int i, n; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 171 | |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 172 | n = 0; |
| 173 | for (i = 0; i < num_clients; i++) { |
| 174 | fds[i] = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT, iface_list[i]); |
| 175 | if (n < fds[i]) |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 176 | n = fds[i]; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 177 | } |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 178 | return n; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 181 | static int sendto_ip4(int sock, const void *msg, int msg_len, struct sockaddr_in *to) |
| 182 | { |
| 183 | int err; |
| 184 | |
| 185 | errno = 0; |
| 186 | err = sendto(sock, msg, msg_len, 0, (struct sockaddr*) to, sizeof(*to)); |
| 187 | err -= msg_len; |
| 188 | if (err) |
| 189 | bb_perror_msg("sendto"); |
| 190 | return err; |
| 191 | } |
| 192 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 193 | /** |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 194 | * pass_to_server() - forwards dhcp packets from client to server |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 195 | * p - packet to send |
| 196 | * client - number of the client |
| 197 | */ |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 198 | 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] | 199 | struct sockaddr_in *client_addr, struct sockaddr_in *server_addr) |
| 200 | { |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 201 | int type; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 202 | |
| 203 | /* check packet_type */ |
| 204 | type = get_dhcp_packet_type(p); |
| 205 | if (type != DHCPDISCOVER && type != DHCPREQUEST |
| 206 | && type != DHCPDECLINE && type != DHCPRELEASE |
| 207 | && type != DHCPINFORM |
| 208 | ) { |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | /* create new xid entry */ |
Denys Vlasenko | 243d175 | 2010-07-04 04:26:55 +0200 | [diff] [blame] | 213 | xid_add(p->xid, client_addr, client); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 214 | |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 215 | /* forward request to server */ |
| 216 | /* note that we send from fds[0] which is bound to SERVER_PORT (67). |
| 217 | * IOW: we send _from_ SERVER_PORT! Although this may look strange, |
| 218 | * RFC 1542 not only allows, but prescribes this for BOOTP relays. |
| 219 | */ |
| 220 | sendto_ip4(fds[0], p, packet_len, server_addr); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /** |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 224 | * pass_to_client() - forwards dhcp packets from server to client |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 225 | * p - packet to send |
| 226 | */ |
Denys Vlasenko | 31af3d5 | 2009-06-17 11:57:09 +0200 | [diff] [blame] | 227 | 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] | 228 | { |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 229 | int type; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 230 | struct xid_item *item; |
| 231 | |
| 232 | /* check xid */ |
| 233 | item = xid_find(p->xid); |
| 234 | if (!item) { |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | /* check packet type */ |
| 239 | type = get_dhcp_packet_type(p); |
| 240 | if (type != DHCPOFFER && type != DHCPACK && type != DHCPNAK) { |
| 241 | return; |
| 242 | } |
| 243 | |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 244 | //TODO: also do it if (p->flags & htons(BROADCAST_FLAG)) is set! |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 245 | if (item->ip.sin_addr.s_addr == htonl(INADDR_ANY)) |
| 246 | item->ip.sin_addr.s_addr = htonl(INADDR_BROADCAST); |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 247 | |
| 248 | if (sendto_ip4(fds[item->client], p, packet_len, &item->ip) != 0) { |
| 249 | return; /* send error occurred */ |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /* remove xid entry */ |
| 253 | xid_del(p->xid); |
| 254 | } |
| 255 | |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 256 | int dhcprelay_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 562f63e | 2018-01-08 10:14:17 +0100 | [diff] [blame] | 257 | int dhcprelay_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 258 | { |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 259 | struct sockaddr_in server_addr; |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 260 | char **iface_list; |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 261 | int *fds; |
| 262 | int num_sockets, max_socket; |
Denys Vlasenko | 990a617 | 2009-06-16 10:23:55 +0200 | [diff] [blame] | 263 | uint32_t our_nip; |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 264 | |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 265 | INIT_G(); |
| 266 | |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 267 | server_addr.sin_family = AF_INET; |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 268 | server_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST); |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 269 | server_addr.sin_port = htons(SERVER_PORT); |
| 270 | |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 271 | /* dhcprelay CLIENT_IFACE1[,CLIENT_IFACE2...] SERVER_IFACE [SERVER_IP] */ |
Denys Vlasenko | 562f63e | 2018-01-08 10:14:17 +0100 | [diff] [blame] | 272 | if (!argv[1] || !argv[2]) |
| 273 | bb_show_usage(); |
| 274 | if (argv[3]) { |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 275 | if (!inet_aton(argv[3], &server_addr.sin_addr)) |
| 276 | bb_perror_msg_and_die("bad server IP"); |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 279 | iface_list = make_iface_list(argv + 1, &num_sockets); |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 280 | |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 281 | fds = xmalloc(num_sockets * sizeof(fds[0])); |
| 282 | |
| 283 | /* Create sockets and bind one to every iface */ |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 284 | max_socket = init_sockets(iface_list, num_sockets, fds); |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 285 | |
| 286 | /* Get our IP on server_iface */ |
Denys Vlasenko | 990a617 | 2009-06-16 10:23:55 +0200 | [diff] [blame] | 287 | if (udhcp_read_interface(argv[2], NULL, &our_nip, NULL)) |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 288 | return 1; |
| 289 | |
| 290 | /* Main loop */ |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 291 | while (1) { |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 292 | // reinit stuff from time to time? go back to make_iface_list |
| 293 | // every N minutes? |
| 294 | fd_set rfds; |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 295 | struct timeval tv; |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 296 | int i; |
| 297 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 298 | FD_ZERO(&rfds); |
| 299 | for (i = 0; i < num_sockets; i++) |
| 300 | FD_SET(fds[i], &rfds); |
| 301 | tv.tv_sec = SELECT_TIMEOUT; |
| 302 | tv.tv_usec = 0; |
| 303 | if (select(max_socket + 1, &rfds, NULL, NULL, &tv) > 0) { |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 304 | int packlen; |
| 305 | struct dhcp_packet dhcp_msg; |
| 306 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 307 | /* server */ |
| 308 | if (FD_ISSET(fds[0], &rfds)) { |
Denis Vlasenko | 6de8994 | 2008-05-21 07:05:06 +0000 | [diff] [blame] | 309 | packlen = udhcp_recv_kernel_packet(&dhcp_msg, fds[0]); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 310 | if (packlen > 0) { |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 311 | pass_to_client(&dhcp_msg, packlen, fds); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 312 | } |
| 313 | } |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 314 | |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 315 | /* clients */ |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 316 | for (i = 1; i < num_sockets; i++) { |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 317 | struct sockaddr_in client_addr; |
| 318 | socklen_t addr_size; |
| 319 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 320 | if (!FD_ISSET(fds[i], &rfds)) |
| 321 | continue; |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 322 | |
| 323 | addr_size = sizeof(client_addr); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 324 | packlen = recvfrom(fds[i], &dhcp_msg, sizeof(dhcp_msg), 0, |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 325 | (struct sockaddr *)(&client_addr), &addr_size); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 326 | if (packlen <= 0) |
| 327 | continue; |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 328 | |
| 329 | /* Get our IP on corresponding client_iface */ |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 330 | // RFC 1542 |
| 331 | // 4.1 General BOOTP Processing for Relay Agents |
| 332 | // 4.1.1 BOOTREQUEST Messages |
| 333 | // If the relay agent does decide to relay the request, it MUST examine |
| 334 | // the 'giaddr' ("gateway" IP address) field. If this field is zero, |
| 335 | // the relay agent MUST fill this field with the IP address of the |
| 336 | // interface on which the request was received. If the interface has |
| 337 | // more than one IP address logically associated with it, the relay |
| 338 | // agent SHOULD choose one IP address associated with that interface and |
| 339 | // use it consistently for all BOOTP messages it relays. If the |
| 340 | // 'giaddr' field contains some non-zero value, the 'giaddr' field MUST |
| 341 | // NOT be modified. The relay agent MUST NOT, under any circumstances, |
| 342 | // fill the 'giaddr' field with a broadcast address as is suggested in |
| 343 | // [1] (Section 8, sixth paragraph). |
| 344 | |
| 345 | // but why? what if server can't route such IP? Client ifaces may be, say, NATed! |
| 346 | |
| 347 | // 4.1.2 BOOTREPLY Messages |
| 348 | // BOOTP relay agents relay BOOTREPLY messages only to BOOTP clients. |
| 349 | // It is the responsibility of BOOTP servers to send BOOTREPLY messages |
| 350 | // directly to the relay agent identified in the 'giaddr' field. |
| 351 | // (yeah right, unless it is impossible... see comment above) |
| 352 | // Therefore, a relay agent may assume that all BOOTREPLY messages it |
| 353 | // receives are intended for BOOTP clients on its directly-connected |
| 354 | // networks. |
| 355 | // |
| 356 | // When a relay agent receives a BOOTREPLY message, it should examine |
| 357 | // the BOOTP 'giaddr', 'yiaddr', 'chaddr', 'htype', and 'hlen' fields. |
| 358 | // These fields should provide adequate information for the relay agent |
| 359 | // to deliver the BOOTREPLY message to the client. |
| 360 | // |
| 361 | // The 'giaddr' field can be used to identify the logical interface from |
| 362 | // which the reply must be sent (i.e., the host or router interface |
| 363 | // connected to the same network as the BOOTP client). If the content |
| 364 | // 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] | 365 | // directly-connected logical interfaces, the BOOTREPLY message MUST be |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 366 | // silently discarded. |
| 367 | if (udhcp_read_interface(iface_list[i], NULL, &dhcp_msg.gateway_nip, NULL)) { |
| 368 | /* Fall back to our IP on server iface */ |
| 369 | // this makes more sense! |
Denys Vlasenko | 990a617 | 2009-06-16 10:23:55 +0200 | [diff] [blame] | 370 | dhcp_msg.gateway_nip = our_nip; |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 371 | } |
Denys Vlasenko | c0f39b0 | 2010-10-06 01:45:24 +0200 | [diff] [blame] | 372 | // 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] | 373 | pass_to_server(&dhcp_msg, packlen, i, fds, &client_addr, &server_addr); |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | xid_expire(); |
Denis Vlasenko | e8a7968 | 2009-04-13 03:33:46 +0000 | [diff] [blame] | 377 | } /* while (1) */ |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 378 | |
Denis Vlasenko | b925605 | 2007-09-28 10:29:17 +0000 | [diff] [blame] | 379 | /* return 0; - not reached */ |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 380 | } |