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