blob: 2352c34a28b50f3884546298bfadca035c890602 [file] [log] [blame]
Denis Vlasenko736230e2006-11-20 19:40:36 +00001/* vi: set sw=4 ts=4: */
Denys Vlasenkoebe6d9d2017-10-05 14:40:24 +02002/*
3 * Port to Busybox Copyright (C) 2006 Jesse Dutton <jessedutton@gmail.com>
Denis Vlasenko736230e2006-11-20 19:40:36 +00004 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02005 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko736230e2006-11-20 19:40:36 +00006 *
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +00007 * DHCP Relay for 'DHCPv4 Configuration of IPSec Tunnel Mode' support
8 * Copyright (C) 2002 Mario Strasser <mast@gmx.net>,
Denis Vlasenko736230e2006-11-20 19:40:36 +00009 * Zuercher Hochschule Winterthur,
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +000010 * Netbeat AG
Denis Vlasenko736230e2006-11-20 19:40:36 +000011 * Upstream has GPL v2 or later
12 */
Denys Vlasenkof7683cd2016-11-23 18:54:59 +010013//applet:IF_DHCPRELAY(APPLET(dhcprelay, BB_DIR_USR_SBIN, BB_SUID_DROP))
14
15//kbuild:lib-$(CONFIG_DHCPRELAY) += dhcprelay.o
Pere Orga5bc8c002011-04-11 03:29:49 +020016
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 Vlasenko3f2d9692021-09-02 16:23:24 +020020//usage: "Relay DHCP requests between clients and server.\n"
21//usage: "Without SERVER_IP, requests are broadcast on SERVER_IFACE."
Pere Orga5bc8c002011-04-11 03:29:49 +020022
Denis Vlasenko736230e2006-11-20 19:40:36 +000023#include "common.h"
Denis Vlasenko736230e2006-11-20 19:40:36 +000024
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +020025#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 Vlasenko736230e2006-11-20 19:40:36 +000031
Denis Vlasenko736230e2006-11-20 19:40:36 +000032/* This list holds information about clients. The xid_* functions manipulate this list. */
Denis Vlasenkob9256052007-09-28 10:29:17 +000033struct xid_item {
Denis Vlasenkod7b33502007-09-30 17:54:10 +000034 unsigned timestamp;
Denys Vlasenko3f2d9692021-09-02 16:23:24 +020035 unsigned iface_no;
Denis Vlasenko35d4da02007-01-22 14:04:27 +000036 uint32_t xid;
Denis Vlasenko736230e2006-11-20 19:40:36 +000037 struct sockaddr_in ip;
Denis Vlasenko736230e2006-11-20 19:40:36 +000038 struct xid_item *next;
Denys Vlasenkodf168912011-01-16 01:25:34 +010039} FIX_ALIASING;
Denis Vlasenko736230e2006-11-20 19:40:36 +000040
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020041#define dhcprelay_xid_list (*(struct xid_item*)bb_common_bufsiz1)
Denys Vlasenko47cfbf32016-04-21 18:18:48 +020042#define INIT_G() do { setup_common_bufsiz(); } while (0)
Denis Vlasenko736230e2006-11-20 19:40:36 +000043
Denys Vlasenko3f2d9692021-09-02 16:23:24 +020044static struct xid_item *xid_add(uint32_t xid, struct sockaddr_in *ip, unsigned iface_no)
Denis Vlasenko736230e2006-11-20 19:40:36 +000045{
46 struct xid_item *item;
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +000047
Denis Vlasenko736230e2006-11-20 19:40:36 +000048 /* create new xid entry */
49 item = xmalloc(sizeof(struct xid_item));
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +000050
Denis Vlasenko736230e2006-11-20 19:40:36 +000051 /* add xid entry */
52 item->ip = *ip;
53 item->xid = xid;
Denys Vlasenko3f2d9692021-09-02 16:23:24 +020054 item->iface_no = iface_no;
Denis Vlasenkod7b33502007-09-30 17:54:10 +000055 item->timestamp = monotonic_sec();
Denis Vlasenko736230e2006-11-20 19:40:36 +000056 item->next = dhcprelay_xid_list.next;
57 dhcprelay_xid_list.next = item;
58
59 return item;
60}
61
Denis Vlasenko736230e2006-11-20 19:40:36 +000062static void xid_expire(void)
63{
64 struct xid_item *item = dhcprelay_xid_list.next;
65 struct xid_item *last = &dhcprelay_xid_list;
Denis Vlasenkod7b33502007-09-30 17:54:10 +000066 unsigned current_time = monotonic_sec();
Denis Vlasenko736230e2006-11-20 19:40:36 +000067
68 while (item != NULL) {
Denis Vlasenko80edead2007-08-02 22:31:05 +000069 if ((current_time - item->timestamp) > MAX_LIFETIME) {
Denis Vlasenko736230e2006-11-20 19:40:36 +000070 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 Vlasenko80edead2007-08-02 22:31:05 +000080static struct xid_item *xid_find(uint32_t xid)
Denis Vlasenko736230e2006-11-20 19:40:36 +000081{
82 struct xid_item *item = dhcprelay_xid_list.next;
83 while (item != NULL) {
84 if (item->xid == xid) {
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +020085 break;
Denis Vlasenko736230e2006-11-20 19:40:36 +000086 }
87 item = item->next;
88 }
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +020089 return item;
Denis Vlasenko736230e2006-11-20 19:40:36 +000090}
91
Denis Vlasenko35d4da02007-01-22 14:04:27 +000092static void xid_del(uint32_t xid)
Denis Vlasenko736230e2006-11-20 19:40:36 +000093{
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 Vlasenko736230e2006-11-20 19:40:36 +0000108/**
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 Vlasenko31af3d52009-06-17 11:57:09 +0200113static int get_dhcp_packet_type(struct dhcp_packet *p)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000114{
Denis Vlasenko28703012006-12-19 20:32:02 +0000115 uint8_t *op;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000116
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 Vlasenkodde8bdc2010-03-22 14:29:13 +0100121 op = udhcp_get_option(p, DHCP_MESSAGE_TYPE);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000122 if (op != NULL)
123 return op[0];
124 return -1;
125}
126
127/**
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200128 * make_iface_list - parses client/server interface names
Denis Vlasenko736230e2006-11-20 19:40:36 +0000129 * returns array
130 */
Denys Vlasenko3f2d9692021-09-02 16:23:24 +0200131static char **make_iface_list(char **client_and_server_ifaces, unsigned *client_number)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000132{
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200133 char *s, **iface_list;
Denys Vlasenkof0269192021-09-02 17:09:12 +0200134 unsigned i, cn;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000135
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200136 /* 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 Vlasenkod7b33502007-09-30 17:54:10 +0000139 while (*s) {
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200140 if (*s == ',')
Bernhard Reutner-Fischera8381942006-11-21 11:23:11 +0000141 cn++;
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000142 s++;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000143 }
Denis Vlasenko736230e2006-11-20 19:40:36 +0000144 *client_number = cn;
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000145
146 /* create vector of pointers */
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200147 iface_list = xzalloc(cn * sizeof(iface_list[0]));
148
149 iface_list[0] = client_and_server_ifaces[1]; /* server iface */
150
Denis Vlasenkod7b33502007-09-30 17:54:10 +0000151 i = 1;
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200152 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 Vlasenkod7b33502007-09-30 17:54:10 +0000161 }
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200162
163 return iface_list;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000164}
165
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000166/* Creates listen sockets (in fds) bound to client and server ifaces,
167 * and returns numerically max fd.
168 */
Denys Vlasenko3f2d9692021-09-02 16:23:24 +0200169static unsigned init_sockets(char **iface_list, unsigned num_clients, int *fds)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000170{
Denys Vlasenko3f2d9692021-09-02 16:23:24 +0200171 unsigned i, n;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000172
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200173 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 Vlasenkob9256052007-09-28 10:29:17 +0000177 n = fds[i];
Denis Vlasenko736230e2006-11-20 19:40:36 +0000178 }
Denis Vlasenkob9256052007-09-28 10:29:17 +0000179 return n;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000180}
181
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200182static 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 Byrne69374872019-07-02 11:35:03 +0200190 bb_simple_perror_msg("sendto");
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200191 return err;
192}
193
Denis Vlasenko736230e2006-11-20 19:40:36 +0000194/**
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000195 * pass_to_server() - forwards dhcp packets from client to server
Denis Vlasenko736230e2006-11-20 19:40:36 +0000196 * p - packet to send
197 * client - number of the client
198 */
Denys Vlasenko3f2d9692021-09-02 16:23:24 +0200199static void pass_to_server(struct dhcp_packet *p, int packet_len, unsigned from_iface_no, int *fds,
Denis Vlasenko736230e2006-11-20 19:40:36 +0000200 struct sockaddr_in *client_addr, struct sockaddr_in *server_addr)
201{
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200202 int type;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000203
204 /* check packet_type */
205 type = get_dhcp_packet_type(p);
Denys Vlasenko3f2d9692021-09-02 16:23:24 +0200206//FIXME: the above does not consider packet_len!
Denis Vlasenko736230e2006-11-20 19:40:36 +0000207 if (type != DHCPDISCOVER && type != DHCPREQUEST
208 && type != DHCPDECLINE && type != DHCPRELEASE
209 && type != DHCPINFORM
210 ) {
211 return;
212 }
213
214 /* create new xid entry */
Denys Vlasenko3f2d9692021-09-02 16:23:24 +0200215 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 Vlasenko736230e2006-11-20 19:40:36 +0000219
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200220 /* 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 Vlasenko736230e2006-11-20 19:40:36 +0000226}
227
228/**
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000229 * pass_to_client() - forwards dhcp packets from server to client
Denis Vlasenko736230e2006-11-20 19:40:36 +0000230 * p - packet to send
231 */
Denys Vlasenko31af3d52009-06-17 11:57:09 +0200232static void pass_to_client(struct dhcp_packet *p, int packet_len, int *fds)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000233{
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200234 int type;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000235 struct xid_item *item;
236
Denys Vlasenko3f2d9692021-09-02 16:23:24 +0200237 /* 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 Vlasenko736230e2006-11-20 19:40:36 +0000244 /* check xid */
245 item = xid_find(p->xid);
246 if (!item) {
247 return;
248 }
Denys Vlasenko3f2d9692021-09-02 16:23:24 +0200249//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 Vlasenko736230e2006-11-20 19:40:36 +0000254
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200255//TODO: also do it if (p->flags & htons(BROADCAST_FLAG)) is set!
Denis Vlasenko736230e2006-11-20 19:40:36 +0000256 if (item->ip.sin_addr.s_addr == htonl(INADDR_ANY))
257 item->ip.sin_addr.s_addr = htonl(INADDR_BROADCAST);
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200258
Denys Vlasenko3f2d9692021-09-02 16:23:24 +0200259 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 Vlasenko736230e2006-11-20 19:40:36 +0000261
262 /* remove xid entry */
263 xid_del(p->xid);
264}
265
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000266int dhcprelay_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko562f63e2018-01-08 10:14:17 +0100267int dhcprelay_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko736230e2006-11-20 19:40:36 +0000268{
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000269 struct sockaddr_in server_addr;
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200270 char **iface_list;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000271 int *fds;
Denys Vlasenko3f2d9692021-09-02 16:23:24 +0200272 unsigned num_sockets, max_socket;
Denys Vlasenko990a6172009-06-16 10:23:55 +0200273 uint32_t our_nip;
Denis Vlasenko736230e2006-11-20 19:40:36 +0000274
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200275 INIT_G();
276
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000277 server_addr.sin_family = AF_INET;
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200278 server_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000279 server_addr.sin_port = htons(SERVER_PORT);
280
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200281 /* dhcprelay CLIENT_IFACE1[,CLIENT_IFACE2...] SERVER_IFACE [SERVER_IP] */
Denys Vlasenko562f63e2018-01-08 10:14:17 +0100282 if (!argv[1] || !argv[2])
283 bb_show_usage();
284 if (argv[3]) {
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000285 if (!inet_aton(argv[3], &server_addr.sin_addr))
James Byrne69374872019-07-02 11:35:03 +0200286 bb_simple_perror_msg_and_die("bad server IP");
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000287 }
288
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200289 iface_list = make_iface_list(argv + 1, &num_sockets);
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000290
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000291 fds = xmalloc(num_sockets * sizeof(fds[0]));
292
293 /* Create sockets and bind one to every iface */
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200294 max_socket = init_sockets(iface_list, num_sockets, fds);
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000295
296 /* Get our IP on server_iface */
Denys Vlasenko990a6172009-06-16 10:23:55 +0200297 if (udhcp_read_interface(argv[2], NULL, &our_nip, NULL))
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000298 return 1;
299
300 /* Main loop */
Denis Vlasenkob9256052007-09-28 10:29:17 +0000301 while (1) {
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200302// reinit stuff from time to time? go back to make_iface_list
303// every N minutes?
304 fd_set rfds;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000305 struct timeval tv;
Denys Vlasenko3f2d9692021-09-02 16:23:24 +0200306 unsigned i;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000307
Denis Vlasenko736230e2006-11-20 19:40:36 +0000308 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 Vlasenkoc0f39b02010-10-06 01:45:24 +0200314 int packlen;
315 struct dhcp_packet dhcp_msg;
316
Denys Vlasenko3f2d9692021-09-02 16:23:24 +0200317 /* from server */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000318 if (FD_ISSET(fds[0], &rfds)) {
Denis Vlasenko6de89942008-05-21 07:05:06 +0000319 packlen = udhcp_recv_kernel_packet(&dhcp_msg, fds[0]);
Denys Vlasenko3f2d9692021-09-02 16:23:24 +0200320//NB: we do not check source port here. Should we?
321//It should be SERVER_PORT.
Denis Vlasenko736230e2006-11-20 19:40:36 +0000322 if (packlen > 0) {
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000323 pass_to_client(&dhcp_msg, packlen, fds);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000324 }
325 }
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200326
Denys Vlasenko3f2d9692021-09-02 16:23:24 +0200327 /* from clients */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000328 for (i = 1; i < num_sockets; i++) {
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200329 struct sockaddr_in client_addr;
330 socklen_t addr_size;
331
Denis Vlasenko736230e2006-11-20 19:40:36 +0000332 if (!FD_ISSET(fds[i], &rfds))
333 continue;
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200334
335 addr_size = sizeof(client_addr);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000336 packlen = recvfrom(fds[i], &dhcp_msg, sizeof(dhcp_msg), 0,
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000337 (struct sockaddr *)(&client_addr), &addr_size);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000338 if (packlen <= 0)
339 continue;
Denys Vlasenko3f2d9692021-09-02 16:23:24 +0200340//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 Vlasenkoe8a79682009-04-13 03:33:46 +0000345
346 /* Get our IP on corresponding client_iface */
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200347// 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 Vlasenko10ad6222017-04-17 16:13:32 +0200382// directly-connected logical interfaces, the BOOTREPLY message MUST be
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200383// 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 Vlasenko990a6172009-06-16 10:23:55 +0200387 dhcp_msg.gateway_nip = our_nip;
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000388 }
Denys Vlasenkoc0f39b02010-10-06 01:45:24 +0200389// maybe dhcp_msg.hops++? drop packets with too many hops (RFC 1542 says 4 or 16)?
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000390 pass_to_server(&dhcp_msg, packlen, i, fds, &client_addr, &server_addr);
Denis Vlasenko736230e2006-11-20 19:40:36 +0000391 }
392 }
393 xid_expire();
Denis Vlasenkoe8a79682009-04-13 03:33:46 +0000394 } /* while (1) */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000395
Denis Vlasenkob9256052007-09-28 10:29:17 +0000396 /* return 0; - not reached */
Denis Vlasenko736230e2006-11-20 19:40:36 +0000397}