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>
|
|
|
|
*
|
|
|
|
* Licensed under GPL v2, see file LICENSE in this tarball for details.
|
|
|
|
*
|
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"
|
|
|
|
#include "options.h"
|
|
|
|
|
|
|
|
/* constants */
|
2008-02-04 18:42:16 +05:30
|
|
|
#define SERVER_PORT 67
|
2007-09-30 23:24:10 +05:30
|
|
|
#define SELECT_TIMEOUT 5 /* select timeout in sec. */
|
|
|
|
#define MAX_LIFETIME 2*60 /* lifetime of an xid entry in sec. */
|
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) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
item = item->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
static int get_dhcp_packet_type(struct dhcpMessage *p)
|
|
|
|
{
|
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 */
|
|
|
|
op = get_option(p, DHCP_MESSAGE_TYPE);
|
|
|
|
if (op != NULL)
|
|
|
|
return op[0];
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get_client_devices - parses the devices list
|
|
|
|
* dev_list - comma separated list of devices
|
|
|
|
* returns array
|
|
|
|
*/
|
2007-08-03 04:01:05 +05:30
|
|
|
static char **get_client_devices(char *dev_list, int *client_number)
|
2006-11-21 01:10:36 +05:30
|
|
|
{
|
2007-09-30 23:24:10 +05:30
|
|
|
char *s, **client_dev;
|
2006-11-21 16:53:11 +05:30
|
|
|
int i, cn;
|
2006-11-21 01:10:36 +05:30
|
|
|
|
|
|
|
/* copy list */
|
2007-09-30 23:24:10 +05:30
|
|
|
dev_list = xstrdup(dev_list);
|
|
|
|
|
|
|
|
/* get number of items, replace ',' with NULs */
|
|
|
|
s = dev_list;
|
|
|
|
cn = 1;
|
|
|
|
while (*s) {
|
|
|
|
if (*s == ',') {
|
|
|
|
*s = '\0';
|
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 */
|
2006-11-21 01:10:36 +05:30
|
|
|
client_dev = xzalloc(cn * sizeof(*client_dev));
|
2007-09-30 23:24:10 +05:30
|
|
|
client_dev[0] = dev_list;
|
|
|
|
i = 1;
|
|
|
|
while (i != cn) {
|
|
|
|
client_dev[i] = client_dev[i - 1] + strlen(client_dev[i - 1]) + 1;
|
|
|
|
i++;
|
2006-11-21 01:10:36 +05:30
|
|
|
}
|
|
|
|
return client_dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-28 15:59:17 +05:30
|
|
|
/* Creates listen sockets (in fds) and returns numerically max fd. */
|
2006-11-21 01:10:36 +05:30
|
|
|
static int init_sockets(char **client, int num_clients,
|
2007-09-28 15:59:17 +05:30
|
|
|
char *server, 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
|
|
|
|
2006-11-21 16:53:11 +05:30
|
|
|
/* talk to real server on bootps */
|
2008-01-26 04:16:34 +05:30
|
|
|
fds[0] = listen_socket(/*INADDR_ANY,*/ SERVER_PORT, server);
|
2007-09-28 15:59:17 +05:30
|
|
|
n = fds[0];
|
2006-11-21 01:10:36 +05:30
|
|
|
|
2007-08-18 19:46:39 +05:30
|
|
|
for (i = 1; i < num_clients; i++) {
|
2006-11-21 16:53:11 +05:30
|
|
|
/* listen for clients on bootps */
|
2008-01-26 04:16:34 +05:30
|
|
|
fds[i] = listen_socket(/*INADDR_ANY,*/ SERVER_PORT, client[i-1]);
|
2007-09-28 15:59:17 +05:30
|
|
|
if (fds[i] > n)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pass_on() - forwards dhcp packets from client to server
|
|
|
|
* p - packet to send
|
|
|
|
* client - number of the client
|
|
|
|
*/
|
|
|
|
static void pass_on(struct dhcpMessage *p, int packet_len, int client, int *fds,
|
|
|
|
struct sockaddr_in *client_addr, struct sockaddr_in *server_addr)
|
|
|
|
{
|
|
|
|
int res, type;
|
|
|
|
struct xid_item *item;
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
item = xid_add(p->xid, client_addr, client);
|
|
|
|
|
|
|
|
/* forward request to LAN (server) */
|
|
|
|
res = sendto(fds[0], p, packet_len, 0, (struct sockaddr*)server_addr,
|
|
|
|
sizeof(struct sockaddr_in));
|
|
|
|
if (res != packet_len) {
|
|
|
|
bb_perror_msg("pass_on");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pass_back() - forwards dhcp packets from server to client
|
|
|
|
* p - packet to send
|
|
|
|
*/
|
|
|
|
static void pass_back(struct dhcpMessage *p, int packet_len, int *fds)
|
|
|
|
{
|
|
|
|
int res, type;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item->ip.sin_addr.s_addr == htonl(INADDR_ANY))
|
|
|
|
item->ip.sin_addr.s_addr = htonl(INADDR_BROADCAST);
|
|
|
|
res = sendto(fds[item->client], p, packet_len, 0, (struct sockaddr*)(&item->ip),
|
|
|
|
sizeof(item->ip));
|
|
|
|
if (res != packet_len) {
|
|
|
|
bb_perror_msg("pass_back");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove xid entry */
|
|
|
|
xid_del(p->xid);
|
|
|
|
}
|
|
|
|
|
2007-09-28 15:59:17 +05:30
|
|
|
static void dhcprelay_loop(int *fds, int num_sockets, int max_socket, char **clients,
|
|
|
|
struct sockaddr_in *server_addr, uint32_t gw_ip) ATTRIBUTE_NORETURN;
|
2006-11-21 01:10:36 +05:30
|
|
|
static void dhcprelay_loop(int *fds, int num_sockets, int max_socket, char **clients,
|
|
|
|
struct sockaddr_in *server_addr, uint32_t gw_ip)
|
|
|
|
{
|
|
|
|
struct dhcpMessage dhcp_msg;
|
|
|
|
fd_set rfds;
|
2006-12-26 23:47:42 +05:30
|
|
|
size_t packlen;
|
|
|
|
socklen_t addr_size;
|
2006-11-21 01:10:36 +05:30
|
|
|
struct sockaddr_in client_addr;
|
|
|
|
struct timeval tv;
|
|
|
|
int i;
|
|
|
|
|
2007-09-28 15:59:17 +05:30
|
|
|
while (1) {
|
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) {
|
|
|
|
/* server */
|
|
|
|
if (FD_ISSET(fds[0], &rfds)) {
|
2007-12-21 02:41:38 +05:30
|
|
|
packlen = udhcp_recv_packet(&dhcp_msg, fds[0]);
|
2006-11-21 01:10:36 +05:30
|
|
|
if (packlen > 0) {
|
2006-11-21 16:53:11 +05:30
|
|
|
pass_back(&dhcp_msg, packlen, fds);
|
2006-11-21 01:10:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 1; i < num_sockets; i++) {
|
|
|
|
/* clients */
|
|
|
|
if (!FD_ISSET(fds[i], &rfds))
|
|
|
|
continue;
|
|
|
|
addr_size = sizeof(struct sockaddr_in);
|
|
|
|
packlen = recvfrom(fds[i], &dhcp_msg, sizeof(dhcp_msg), 0,
|
|
|
|
(struct sockaddr *)(&client_addr), &addr_size);
|
|
|
|
if (packlen <= 0)
|
|
|
|
continue;
|
2007-08-03 04:01:05 +05:30
|
|
|
if (read_interface(clients[i-1], NULL, &dhcp_msg.giaddr, NULL))
|
2006-11-21 01:10:36 +05:30
|
|
|
dhcp_msg.giaddr = gw_ip;
|
|
|
|
pass_on(&dhcp_msg, packlen, i, fds, &client_addr, server_addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
xid_expire();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int dhcprelay_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2006-11-21 01:10:36 +05:30
|
|
|
int dhcprelay_main(int argc, char **argv)
|
|
|
|
{
|
2007-09-30 23:24:10 +05:30
|
|
|
int num_sockets, max_socket;
|
|
|
|
int *fds;
|
2006-11-21 01:10:36 +05:30
|
|
|
uint32_t gw_ip;
|
|
|
|
char **clients;
|
|
|
|
struct sockaddr_in server_addr;
|
|
|
|
|
|
|
|
server_addr.sin_family = AF_INET;
|
2008-01-26 04:16:34 +05:30
|
|
|
server_addr.sin_port = htons(SERVER_PORT);
|
2006-11-21 01:10:36 +05:30
|
|
|
if (argc == 4) {
|
|
|
|
if (!inet_aton(argv[3], &server_addr.sin_addr))
|
|
|
|
bb_perror_msg_and_die("didn't grok server");
|
|
|
|
} else if (argc == 3) {
|
|
|
|
server_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
|
|
|
|
} else {
|
|
|
|
bb_show_usage();
|
|
|
|
}
|
|
|
|
|
2007-09-30 23:24:10 +05:30
|
|
|
clients = get_client_devices(argv[1], &num_sockets);
|
2007-09-28 15:59:17 +05:30
|
|
|
num_sockets++; /* for server socket at fds[0] */
|
2007-09-30 23:24:10 +05:30
|
|
|
fds = xmalloc(num_sockets * sizeof(fds[0]));
|
2007-09-28 15:59:17 +05:30
|
|
|
max_socket = init_sockets(clients, num_sockets, argv[2], fds);
|
2006-11-21 01:10:36 +05:30
|
|
|
|
2007-08-03 04:01:05 +05:30
|
|
|
if (read_interface(argv[2], NULL, &gw_ip, NULL))
|
2006-11-21 01:10:36 +05:30
|
|
|
return 1;
|
|
|
|
|
2007-09-28 15:59:17 +05:30
|
|
|
/* doesn't return */
|
2006-11-21 01:10:36 +05:30
|
|
|
dhcprelay_loop(fds, num_sockets, max_socket, clients, &server_addr, gw_ip);
|
2007-09-28 15:59:17 +05:30
|
|
|
/* return 0; - not reached */
|
2006-11-21 01:10:36 +05:30
|
|
|
}
|