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