2006-11-21 01:10:36 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/* Port to Busybox Copyright (C) 2006 Jesse Dutton <jessedutton@gmail.com>
|
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2006-11-21 01:10:36 +05:30
|
|
|
*
|
2006-11-21 16:53:11 +05:30
|
|
|
* DHCP Relay for 'DHCPv4 Configuration of IPSec Tunnel Mode' support
|
|
|
|
* Copyright (C) 2002 Mario Strasser <mast@gmx.net>,
|
2006-11-21 01:10:36 +05:30
|
|
|
* Zuercher Hochschule Winterthur,
|
2006-11-21 16:53:11 +05:30
|
|
|
* Netbeat AG
|
2006-11-21 01:10:36 +05:30
|
|
|
* Upstream has GPL v2 or later
|
|
|
|
*/
|
|
|
|
#include "common.h"
|
|
|
|
|
2010-10-06 05:15:24 +05:30
|
|
|
#define SERVER_PORT 67
|
|
|
|
|
|
|
|
/* lifetime of an xid entry in sec. */
|
|
|
|
#define MAX_LIFETIME 2*60
|
|
|
|
/* select timeout in sec. */
|
|
|
|
#define SELECT_TIMEOUT (MAX_LIFETIME / 8)
|
2006-11-21 01:10:36 +05:30
|
|
|
|
|
|
|
/* This list holds information about clients. The xid_* functions manipulate this list. */
|
2007-09-28 15:59:17 +05:30
|
|
|
struct xid_item {
|
2007-09-30 23:24:10 +05:30
|
|
|
unsigned timestamp;
|
2007-09-28 15:59:17 +05:30
|
|
|
int client;
|
2007-01-22 19:34:27 +05:30
|
|
|
uint32_t xid;
|
2006-11-21 01:10:36 +05:30
|
|
|
struct sockaddr_in ip;
|
|
|
|
struct xid_item *next;
|
2007-09-28 15:59:17 +05:30
|
|
|
};
|
2006-11-21 01:10:36 +05:30
|
|
|
|
2007-09-28 15:59:17 +05:30
|
|
|
#define dhcprelay_xid_list (*(struct xid_item*)&bb_common_bufsiz1)
|
2006-11-21 01:10:36 +05:30
|
|
|
|
2007-08-03 04:01:05 +05:30
|
|
|
static struct xid_item *xid_add(uint32_t xid, struct sockaddr_in *ip, int client)
|
2006-11-21 01:10:36 +05:30
|
|
|
{
|
|
|
|
struct xid_item *item;
|
2006-11-21 16:53:11 +05:30
|
|
|
|
2006-11-21 01:10:36 +05:30
|
|
|
/* create new xid entry */
|
|
|
|
item = xmalloc(sizeof(struct xid_item));
|
2006-11-21 16:53:11 +05:30
|
|
|
|
2006-11-21 01:10:36 +05:30
|
|
|
/* add xid entry */
|
|
|
|
item->ip = *ip;
|
|
|
|
item->xid = xid;
|
|
|
|
item->client = client;
|
2007-09-30 23:24:10 +05:30
|
|
|
item->timestamp = monotonic_sec();
|
2006-11-21 01:10:36 +05:30
|
|
|
item->next = dhcprelay_xid_list.next;
|
|
|
|
dhcprelay_xid_list.next = item;
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xid_expire(void)
|
|
|
|
{
|
|
|
|
struct xid_item *item = dhcprelay_xid_list.next;
|
|
|
|
struct xid_item *last = &dhcprelay_xid_list;
|
2007-09-30 23:24:10 +05:30
|
|
|
unsigned current_time = monotonic_sec();
|
2006-11-21 01:10:36 +05:30
|
|
|
|
|
|
|
while (item != NULL) {
|
2007-08-03 04:01:05 +05:30
|
|
|
if ((current_time - item->timestamp) > MAX_LIFETIME) {
|
2006-11-21 01:10:36 +05:30
|
|
|
last->next = item->next;
|
|
|
|
free(item);
|
|
|
|
item = last->next;
|
|
|
|
} else {
|
|
|
|
last = item;
|
|
|
|
item = item->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-03 04:01:05 +05:30
|
|
|
static struct xid_item *xid_find(uint32_t xid)
|
2006-11-21 01:10:36 +05:30
|
|
|
{
|
|
|
|
struct xid_item *item = dhcprelay_xid_list.next;
|
|
|
|
while (item != NULL) {
|
|
|
|
if (item->xid == xid) {
|
2010-10-06 05:15:24 +05:30
|
|
|
break;
|
2006-11-21 01:10:36 +05:30
|
|
|
}
|
|
|
|
item = item->next;
|
|
|
|
}
|
2010-10-06 05:15:24 +05:30
|
|
|
return item;
|
2006-11-21 01:10:36 +05:30
|
|
|
}
|
|
|
|
|
2007-01-22 19:34:27 +05:30
|
|
|
static void xid_del(uint32_t xid)
|
2006-11-21 01:10:36 +05:30
|
|
|
{
|
|
|
|
struct xid_item *item = dhcprelay_xid_list.next;
|
|
|
|
struct xid_item *last = &dhcprelay_xid_list;
|
|
|
|
while (item != NULL) {
|
|
|
|
if (item->xid == xid) {
|
|
|
|
last->next = item->next;
|
|
|
|
free(item);
|
|
|
|
item = last->next;
|
|
|
|
} else {
|
|
|
|
last = item;
|
|
|
|
item = item->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get_dhcp_packet_type - gets the message type of a dhcp packet
|
|
|
|
* p - pointer to the dhcp packet
|
|
|
|
* returns the message type on success, -1 otherwise
|
|
|
|
*/
|
2009-06-17 15:27:09 +05:30
|
|
|
static int get_dhcp_packet_type(struct dhcp_packet *p)
|
2006-11-21 01:10:36 +05:30
|
|
|
{
|
2006-12-20 02:02:02 +05:30
|
|
|
uint8_t *op;
|
2006-11-21 01:10:36 +05:30
|
|
|
|
|
|
|
/* it must be either a BOOTREQUEST or a BOOTREPLY */
|
|
|
|
if (p->op != BOOTREQUEST && p->op != BOOTREPLY)
|
|
|
|
return -1;
|
|
|
|
/* get message type option */
|
2010-03-22 18:59:13 +05:30
|
|
|
op = udhcp_get_option(p, DHCP_MESSAGE_TYPE);
|
2006-11-21 01:10:36 +05:30
|
|
|
if (op != NULL)
|
|
|
|
return op[0];
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-10-06 05:15:24 +05:30
|
|
|
* make_iface_list - parses client/server interface names
|
2006-11-21 01:10:36 +05:30
|
|
|
* returns array
|
|
|
|
*/
|
2010-10-06 05:15:24 +05:30
|
|
|
static char **make_iface_list(char **client_and_server_ifaces, int *client_number)
|
2006-11-21 01:10:36 +05:30
|
|
|
{
|
2010-10-06 05:15:24 +05:30
|
|
|
char *s, **iface_list;
|
2006-11-21 16:53:11 +05:30
|
|
|
int i, cn;
|
2006-11-21 01:10:36 +05:30
|
|
|
|
2010-10-06 05:15:24 +05:30
|
|
|
/* get number of items */
|
|
|
|
cn = 2; /* 1 server iface + at least 1 client one */
|
|
|
|
s = client_and_server_ifaces[0]; /* list of client ifaces */
|
2007-09-30 23:24:10 +05:30
|
|
|
while (*s) {
|
2010-10-06 05:15:24 +05:30
|
|
|
if (*s == ',')
|
2006-11-21 16:53:11 +05:30
|
|
|
cn++;
|
2007-09-30 23:24:10 +05:30
|
|
|
s++;
|
|
|
|
}
|
|
|
|
*client_number = cn;
|
2006-11-21 01:10:36 +05:30
|
|
|
|
2007-09-30 23:24:10 +05:30
|
|
|
/* create vector of pointers */
|
2010-10-06 05:15:24 +05:30
|
|
|
iface_list = xzalloc(cn * sizeof(iface_list[0]));
|
|
|
|
|
|
|
|
iface_list[0] = client_and_server_ifaces[1]; /* server iface */
|
|
|
|
|
2007-09-30 23:24:10 +05:30
|
|
|
i = 1;
|
2010-10-06 05:15:24 +05:30
|
|
|
s = xstrdup(client_and_server_ifaces[0]); /* list of client ifaces */
|
|
|
|
goto store_client_iface_name;
|
|
|
|
|
|
|
|
while (i < cn) {
|
|
|
|
if (*s++ == ',') {
|
|
|
|
s[-1] = '\0';
|
|
|
|
store_client_iface_name:
|
|
|
|
iface_list[i++] = s;
|
|
|
|
}
|
2006-11-21 01:10:36 +05:30
|
|
|
}
|
2010-10-06 05:15:24 +05:30
|
|
|
|
|
|
|
return iface_list;
|
2006-11-21 01:10:36 +05:30
|
|
|
}
|
|
|
|
|
2009-04-13 09:03:46 +05:30
|
|
|
/* Creates listen sockets (in fds) bound to client and server ifaces,
|
|
|
|
* and returns numerically max fd.
|
|
|
|
*/
|
2010-10-06 05:15:24 +05:30
|
|
|
static int init_sockets(char **iface_list, int num_clients, int *fds)
|
2006-11-21 01:10:36 +05:30
|
|
|
{
|
2007-09-28 15:59:17 +05:30
|
|
|
int i, n;
|
2006-11-21 01:10:36 +05:30
|
|
|
|
2010-10-06 05:15:24 +05:30
|
|
|
n = 0;
|
|
|
|
for (i = 0; i < num_clients; i++) {
|
|
|
|
fds[i] = udhcp_listen_socket(/*INADDR_ANY,*/ SERVER_PORT, iface_list[i]);
|
|
|
|
if (n < fds[i])
|
2007-09-28 15:59:17 +05:30
|
|
|
n = fds[i];
|
2006-11-21 01:10:36 +05:30
|
|
|
}
|
2007-09-28 15:59:17 +05:30
|
|
|
return n;
|
2006-11-21 01:10:36 +05:30
|
|
|
}
|
|
|
|
|
2010-10-06 05:15:24 +05:30
|
|
|
static int sendto_ip4(int sock, const void *msg, int msg_len, struct sockaddr_in *to)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
err = sendto(sock, msg, msg_len, 0, (struct sockaddr*) to, sizeof(*to));
|
|
|
|
err -= msg_len;
|
|
|
|
if (err)
|
|
|
|
bb_perror_msg("sendto");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2006-11-21 01:10:36 +05:30
|
|
|
/**
|
2009-04-13 09:03:46 +05:30
|
|
|
* pass_to_server() - forwards dhcp packets from client to server
|
2006-11-21 01:10:36 +05:30
|
|
|
* p - packet to send
|
|
|
|
* client - number of the client
|
|
|
|
*/
|
2009-06-17 15:27:09 +05:30
|
|
|
static void pass_to_server(struct dhcp_packet *p, int packet_len, int client, int *fds,
|
2006-11-21 01:10:36 +05:30
|
|
|
struct sockaddr_in *client_addr, struct sockaddr_in *server_addr)
|
|
|
|
{
|
2010-10-06 05:15:24 +05:30
|
|
|
int type;
|
2006-11-21 01:10:36 +05:30
|
|
|
|
|
|
|
/* check packet_type */
|
|
|
|
type = get_dhcp_packet_type(p);
|
|
|
|
if (type != DHCPDISCOVER && type != DHCPREQUEST
|
|
|
|
&& type != DHCPDECLINE && type != DHCPRELEASE
|
|
|
|
&& type != DHCPINFORM
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create new xid entry */
|
2010-07-04 07:56:55 +05:30
|
|
|
xid_add(p->xid, client_addr, client);
|
2006-11-21 01:10:36 +05:30
|
|
|
|
2010-10-06 05:15:24 +05:30
|
|
|
/* forward request to server */
|
|
|
|
/* note that we send from fds[0] which is bound to SERVER_PORT (67).
|
|
|
|
* IOW: we send _from_ SERVER_PORT! Although this may look strange,
|
|
|
|
* RFC 1542 not only allows, but prescribes this for BOOTP relays.
|
|
|
|
*/
|
|
|
|
sendto_ip4(fds[0], p, packet_len, server_addr);
|
2006-11-21 01:10:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-04-13 09:03:46 +05:30
|
|
|
* pass_to_client() - forwards dhcp packets from server to client
|
2006-11-21 01:10:36 +05:30
|
|
|
* p - packet to send
|
|
|
|
*/
|
2009-06-17 15:27:09 +05:30
|
|
|
static void pass_to_client(struct dhcp_packet *p, int packet_len, int *fds)
|
2006-11-21 01:10:36 +05:30
|
|
|
{
|
2010-10-06 05:15:24 +05:30
|
|
|
int type;
|
2006-11-21 01:10:36 +05:30
|
|
|
struct xid_item *item;
|
|
|
|
|
|
|
|
/* check xid */
|
|
|
|
item = xid_find(p->xid);
|
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check packet type */
|
|
|
|
type = get_dhcp_packet_type(p);
|
|
|
|
if (type != DHCPOFFER && type != DHCPACK && type != DHCPNAK) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-10-06 05:15:24 +05:30
|
|
|
//TODO: also do it if (p->flags & htons(BROADCAST_FLAG)) is set!
|
2006-11-21 01:10:36 +05:30
|
|
|
if (item->ip.sin_addr.s_addr == htonl(INADDR_ANY))
|
|
|
|
item->ip.sin_addr.s_addr = htonl(INADDR_BROADCAST);
|
2010-10-06 05:15:24 +05:30
|
|
|
|
|
|
|
if (sendto_ip4(fds[item->client], p, packet_len, &item->ip) != 0) {
|
|
|
|
return; /* send error occurred */
|
2006-11-21 01:10:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* remove xid entry */
|
|
|
|
xid_del(p->xid);
|
|
|
|
}
|
|
|
|
|
2009-04-13 09:03:46 +05:30
|
|
|
int dhcprelay_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int dhcprelay_main(int argc, char **argv)
|
2006-11-21 01:10:36 +05:30
|
|
|
{
|
2009-04-13 09:03:46 +05:30
|
|
|
struct sockaddr_in server_addr;
|
2010-10-06 05:15:24 +05:30
|
|
|
char **iface_list;
|
2009-04-13 09:03:46 +05:30
|
|
|
int *fds;
|
|
|
|
int num_sockets, max_socket;
|
2009-06-16 13:53:55 +05:30
|
|
|
uint32_t our_nip;
|
2009-04-13 09:03:46 +05:30
|
|
|
|
|
|
|
server_addr.sin_family = AF_INET;
|
2010-10-06 05:15:24 +05:30
|
|
|
server_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
|
2009-04-13 09:03:46 +05:30
|
|
|
server_addr.sin_port = htons(SERVER_PORT);
|
|
|
|
|
2010-10-06 05:15:24 +05:30
|
|
|
/* dhcprelay CLIENT_IFACE1[,CLIENT_IFACE2...] SERVER_IFACE [SERVER_IP] */
|
2009-04-13 09:03:46 +05:30
|
|
|
if (argc == 4) {
|
|
|
|
if (!inet_aton(argv[3], &server_addr.sin_addr))
|
|
|
|
bb_perror_msg_and_die("bad server IP");
|
2010-10-06 05:15:24 +05:30
|
|
|
} else if (argc != 3) {
|
2009-04-13 09:03:46 +05:30
|
|
|
bb_show_usage();
|
|
|
|
}
|
|
|
|
|
2010-10-06 05:15:24 +05:30
|
|
|
iface_list = make_iface_list(argv + 1, &num_sockets);
|
2009-04-13 09:03:46 +05:30
|
|
|
|
|
|
|
fds = xmalloc(num_sockets * sizeof(fds[0]));
|
|
|
|
|
|
|
|
/* Create sockets and bind one to every iface */
|
2010-10-06 05:15:24 +05:30
|
|
|
max_socket = init_sockets(iface_list, num_sockets, fds);
|
2009-04-13 09:03:46 +05:30
|
|
|
|
|
|
|
/* Get our IP on server_iface */
|
2009-06-16 13:53:55 +05:30
|
|
|
if (udhcp_read_interface(argv[2], NULL, &our_nip, NULL))
|
2009-04-13 09:03:46 +05:30
|
|
|
return 1;
|
2006-11-21 01:10:36 +05:30
|
|
|
|
2009-04-13 09:03:46 +05:30
|
|
|
/* Main loop */
|
2007-09-28 15:59:17 +05:30
|
|
|
while (1) {
|
2010-10-06 05:15:24 +05:30
|
|
|
// reinit stuff from time to time? go back to make_iface_list
|
|
|
|
// every N minutes?
|
|
|
|
fd_set rfds;
|
2009-04-13 09:03:46 +05:30
|
|
|
struct timeval tv;
|
|
|
|
int i;
|
|
|
|
|
2006-11-21 01:10:36 +05:30
|
|
|
FD_ZERO(&rfds);
|
|
|
|
for (i = 0; i < num_sockets; i++)
|
|
|
|
FD_SET(fds[i], &rfds);
|
|
|
|
tv.tv_sec = SELECT_TIMEOUT;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
if (select(max_socket + 1, &rfds, NULL, NULL, &tv) > 0) {
|
2010-10-06 05:15:24 +05:30
|
|
|
int packlen;
|
|
|
|
struct dhcp_packet dhcp_msg;
|
|
|
|
|
2006-11-21 01:10:36 +05:30
|
|
|
/* server */
|
|
|
|
if (FD_ISSET(fds[0], &rfds)) {
|
2008-05-21 12:35:06 +05:30
|
|
|
packlen = udhcp_recv_kernel_packet(&dhcp_msg, fds[0]);
|
2006-11-21 01:10:36 +05:30
|
|
|
if (packlen > 0) {
|
2009-04-13 09:03:46 +05:30
|
|
|
pass_to_client(&dhcp_msg, packlen, fds);
|
2006-11-21 01:10:36 +05:30
|
|
|
}
|
|
|
|
}
|
2010-10-06 05:15:24 +05:30
|
|
|
|
2009-04-13 09:03:46 +05:30
|
|
|
/* clients */
|
2006-11-21 01:10:36 +05:30
|
|
|
for (i = 1; i < num_sockets; i++) {
|
2010-10-06 05:15:24 +05:30
|
|
|
struct sockaddr_in client_addr;
|
|
|
|
socklen_t addr_size;
|
|
|
|
|
2006-11-21 01:10:36 +05:30
|
|
|
if (!FD_ISSET(fds[i], &rfds))
|
|
|
|
continue;
|
2010-10-06 05:15:24 +05:30
|
|
|
|
|
|
|
addr_size = sizeof(client_addr);
|
2006-11-21 01:10:36 +05:30
|
|
|
packlen = recvfrom(fds[i], &dhcp_msg, sizeof(dhcp_msg), 0,
|
2009-04-13 09:03:46 +05:30
|
|
|
(struct sockaddr *)(&client_addr), &addr_size);
|
2006-11-21 01:10:36 +05:30
|
|
|
if (packlen <= 0)
|
|
|
|
continue;
|
2009-04-13 09:03:46 +05:30
|
|
|
|
|
|
|
/* Get our IP on corresponding client_iface */
|
2010-10-06 05:15:24 +05:30
|
|
|
// RFC 1542
|
|
|
|
// 4.1 General BOOTP Processing for Relay Agents
|
|
|
|
// 4.1.1 BOOTREQUEST Messages
|
|
|
|
// If the relay agent does decide to relay the request, it MUST examine
|
|
|
|
// the 'giaddr' ("gateway" IP address) field. If this field is zero,
|
|
|
|
// the relay agent MUST fill this field with the IP address of the
|
|
|
|
// interface on which the request was received. If the interface has
|
|
|
|
// more than one IP address logically associated with it, the relay
|
|
|
|
// agent SHOULD choose one IP address associated with that interface and
|
|
|
|
// use it consistently for all BOOTP messages it relays. If the
|
|
|
|
// 'giaddr' field contains some non-zero value, the 'giaddr' field MUST
|
|
|
|
// NOT be modified. The relay agent MUST NOT, under any circumstances,
|
|
|
|
// fill the 'giaddr' field with a broadcast address as is suggested in
|
|
|
|
// [1] (Section 8, sixth paragraph).
|
|
|
|
|
|
|
|
// but why? what if server can't route such IP? Client ifaces may be, say, NATed!
|
|
|
|
|
|
|
|
// 4.1.2 BOOTREPLY Messages
|
|
|
|
// BOOTP relay agents relay BOOTREPLY messages only to BOOTP clients.
|
|
|
|
// It is the responsibility of BOOTP servers to send BOOTREPLY messages
|
|
|
|
// directly to the relay agent identified in the 'giaddr' field.
|
|
|
|
// (yeah right, unless it is impossible... see comment above)
|
|
|
|
// Therefore, a relay agent may assume that all BOOTREPLY messages it
|
|
|
|
// receives are intended for BOOTP clients on its directly-connected
|
|
|
|
// networks.
|
|
|
|
//
|
|
|
|
// When a relay agent receives a BOOTREPLY message, it should examine
|
|
|
|
// the BOOTP 'giaddr', 'yiaddr', 'chaddr', 'htype', and 'hlen' fields.
|
|
|
|
// These fields should provide adequate information for the relay agent
|
|
|
|
// to deliver the BOOTREPLY message to the client.
|
|
|
|
//
|
|
|
|
// The 'giaddr' field can be used to identify the logical interface from
|
|
|
|
// which the reply must be sent (i.e., the host or router interface
|
|
|
|
// connected to the same network as the BOOTP client). If the content
|
|
|
|
// of the 'giaddr' field does not match one of the relay agent's
|
|
|
|
// directly-connected logical interfaces, the BOOTREPLY messsage MUST be
|
|
|
|
// silently discarded.
|
|
|
|
if (udhcp_read_interface(iface_list[i], NULL, &dhcp_msg.gateway_nip, NULL)) {
|
|
|
|
/* Fall back to our IP on server iface */
|
|
|
|
// this makes more sense!
|
2009-06-16 13:53:55 +05:30
|
|
|
dhcp_msg.gateway_nip = our_nip;
|
2009-04-13 09:03:46 +05:30
|
|
|
}
|
2010-10-06 05:15:24 +05:30
|
|
|
// maybe dhcp_msg.hops++? drop packets with too many hops (RFC 1542 says 4 or 16)?
|
2009-04-13 09:03:46 +05:30
|
|
|
pass_to_server(&dhcp_msg, packlen, i, fds, &client_addr, &server_addr);
|
2006-11-21 01:10:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
xid_expire();
|
2009-04-13 09:03:46 +05:30
|
|
|
} /* while (1) */
|
2006-11-21 01:10:36 +05:30
|
|
|
|
2007-09-28 15:59:17 +05:30
|
|
|
/* return 0; - not reached */
|
2006-11-21 01:10:36 +05:30
|
|
|
}
|