arp: fix -H/-t handling.
[oweals/busybox.git] / networking / udhcp / dhcprelay.c
index 4ed65a2d632bbf78a39c2261d0037ea13ba80029..f82ac05b430221701ae49bc2ca4bd7c04ab920a0 100644 (file)
@@ -1,7 +1,7 @@
 /* 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.
+ * Licensed under GPLv2, see file LICENSE in this source tree.
  *
  * DHCP Relay for 'DHCPv4 Configuration of IPSec Tunnel Mode' support
  * Copyright (C) 2002 Mario Strasser <mast@gmx.net>,
  * Upstream has GPL v2 or later
  */
 
+//usage:#define dhcprelay_trivial_usage
+//usage:       "CLIENT_IFACE[,CLIENT_IFACE2]... SERVER_IFACE [SERVER_IP]"
+//usage:#define dhcprelay_full_usage "\n\n"
+//usage:       "Relay DHCP requests between clients and server"
+
 #include "common.h"
-#include "dhcpd.h"
-#include "options.h"
 
-/* constants */
-#define SELECT_TIMEOUT 5 /* select timeout in sec. */
-#define MAX_LIFETIME 2*60 /* lifetime of an xid entry in sec. */
-#define MAX_INTERFACES 9
+#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)
 
 /* This list holds information about clients. The xid_* functions manipulate this list. */
-static struct xid_item {
+struct xid_item {
+       unsigned timestamp;
+       int client;
        uint32_t xid;
        struct sockaddr_in ip;
-       int client;
-       time_t timestamp;
        struct xid_item *next;
-} dhcprelay_xid_list = {0, {0}, 0, 0, NULL};
+} FIX_ALIASING;
 
+#define dhcprelay_xid_list (*(struct xid_item*)&bb_common_bufsiz1)
 
 static struct xid_item *xid_add(uint32_t xid, struct sockaddr_in *ip, int client)
 {
@@ -41,7 +46,7 @@ static struct xid_item *xid_add(uint32_t xid, struct sockaddr_in *ip, int client
        item->ip = *ip;
        item->xid = xid;
        item->client = client;
-       item->timestamp = time(NULL);
+       item->timestamp = monotonic_sec();
        item->next = dhcprelay_xid_list.next;
        dhcprelay_xid_list.next = item;
 
@@ -52,7 +57,7 @@ static void xid_expire(void)
 {
        struct xid_item *item = dhcprelay_xid_list.next;
        struct xid_item *last = &dhcprelay_xid_list;
-       time_t current_time = time(NULL);
+       unsigned current_time = monotonic_sec();
 
        while (item != NULL) {
                if ((current_time - item->timestamp) > MAX_LIFETIME) {
@@ -71,11 +76,11 @@ static struct xid_item *xid_find(uint32_t xid)
        struct xid_item *item = dhcprelay_xid_list.next;
        while (item != NULL) {
                if (item->xid == xid) {
-                       return item;
+                       break;
                }
                item = item->next;
        }
-       return NULL;
+       return item;
 }
 
 static void xid_del(uint32_t xid)
@@ -99,7 +104,7 @@ static void xid_del(uint32_t xid)
  * p - pointer to the dhcp packet
  * returns the message type on success, -1 otherwise
  */
-static int get_dhcp_packet_type(struct dhcpMessage *p)
+static int get_dhcp_packet_type(struct dhcp_packet *p)
 {
        uint8_t *op;
 
@@ -107,92 +112,88 @@ static int get_dhcp_packet_type(struct dhcpMessage *p)
        if (p->op != BOOTREQUEST && p->op != BOOTREPLY)
                return -1;
        /* get message type option */
-       op = get_option(p, DHCP_MESSAGE_TYPE);
+       op = udhcp_get_option(p, DHCP_MESSAGE_TYPE);
        if (op != NULL)
                return op[0];
        return -1;
 }
 
 /**
- * signal_handler - handles signals ;-)
- * sig - sent signal
- */
-static smallint dhcprelay_stopflag;
-
-static void dhcprelay_signal_handler(int sig)
-{
-       dhcprelay_stopflag = 1;
-}
-
-/**
- * get_client_devices - parses the devices list
- * dev_list - comma separated list of devices
+ * make_iface_list - parses client/server interface names
  * returns array
  */
-static char **get_client_devices(char *dev_list, int *client_number)
+static char **make_iface_list(char **client_and_server_ifaces, int *client_number)
 {
-       char *s, *list, **client_dev;
+       char *s, **iface_list;
        int i, cn;
 
-       /* copy list */
-       list = xstrdup(dev_list);
-       if (list == NULL) return NULL;
-
        /* get number of items */
-       for (s = dev_list, cn = 1; *s; s++)
+       cn = 2; /* 1 server iface + at least 1 client one */
+       s = client_and_server_ifaces[0]; /* list of client ifaces */
+       while (*s) {
                if (*s == ',')
                        cn++;
+               s++;
+       }
+       *client_number = cn;
+
+       /* create vector of pointers */
+       iface_list = xzalloc(cn * sizeof(iface_list[0]));
+
+       iface_list[0] = client_and_server_ifaces[1]; /* server iface */
 
-       client_dev = xzalloc(cn * sizeof(*client_dev));
+       i = 1;
+       s = xstrdup(client_and_server_ifaces[0]); /* list of client ifaces */
+       goto store_client_iface_name;
 
-       /* parse list */
-       s = strtok(list, ",");
-       i = 0;
-       while (s != NULL) {
-               client_dev[i++] = xstrdup(s);
-               s = strtok(NULL, ",");
+       while (i < cn) {
+               if (*s++ == ',') {
+                       s[-1] = '\0';
+ store_client_iface_name:
+                       iface_list[i++] = s;
+               }
        }
 
-       /* free copy and exit */
-       free(list);
-       *client_number = cn;
-       return client_dev;
+       return iface_list;
 }
 
-
-/* Creates listen sockets (in fds) and returns the number allocated. */
-static int init_sockets(char **client, int num_clients,
-                       char *server, int *fds, int *max_socket)
+/* Creates listen sockets (in fds) bound to client and server ifaces,
+ * and returns numerically max fd.
+ */
+static int init_sockets(char **iface_list, int num_clients, int *fds)
 {
-       int i;
+       int i, n;
 
-       /* talk to real server on bootps */
-       fds[0] = listen_socket(htonl(INADDR_ANY), 67, server);
-       *max_socket = fds[0];
-
-       /* array starts at 1 since server is 0 */
-       num_clients++;
-
-       for (i=1; i < num_clients; i++) {
-               /* listen for clients on bootps */
-               fds[i] = listen_socket(htonl(INADDR_ANY), 67, client[i-1]);
-               if (fds[i] > *max_socket) *max_socket = fds[i];
+       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])
+                       n = fds[i];
        }
-
-       return i;
+       return n;
 }
 
+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;
+}
 
 /**
- * pass_on() - forwards dhcp packets from client to server
+ * pass_to_server() - 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,
+static void pass_to_server(struct dhcp_packet *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;
+       int type;
 
        /* check packet_type */
        type = get_dhcp_packet_type(p);
@@ -204,24 +205,23 @@ static void pass_on(struct dhcpMessage *p, int packet_len, int client, int *fds,
        }
 
        /* 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;
-       }
+       xid_add(p->xid, client_addr, client);
+
+       /* 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);
 }
 
 /**
- * pass_back() - forwards dhcp packets from server to client
+ * pass_to_client() - forwards dhcp packets from server to client
  * p - packet to send
  */
-static void pass_back(struct dhcpMessage *p, int packet_len, int *fds)
+static void pass_to_client(struct dhcp_packet *p, int packet_len, int *fds)
 {
-       int res, type;
+       int type;
        struct xid_item *item;
 
        /* check xid */
@@ -236,102 +236,138 @@ static void pass_back(struct dhcpMessage *p, int packet_len, int *fds)
                return;
        }
 
+//TODO: also do it if (p->flags & htons(BROADCAST_FLAG)) is set!
        if (item->ip.sin_addr.s_addr == htonl(INADDR_ANY))
                item->ip.sin_addr.s_addr = htonl(INADDR_BROADCAST);
-       if (item->client > MAX_INTERFACES)
-               return;
-       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;
+
+       if (sendto_ip4(fds[item->client], p, packet_len, &item->ip) != 0) {
+               return; /* send error occurred */
        }
 
        /* remove xid entry */
        xid_del(p->xid);
 }
 
-static void dhcprelay_loop(int *fds, int num_sockets, int max_socket, char **clients,
-               struct sockaddr_in *server_addr, uint32_t gw_ip)
+int dhcprelay_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int dhcprelay_main(int argc, char **argv)
 {
-       struct dhcpMessage dhcp_msg;
-       fd_set rfds;
-       size_t packlen;
-       socklen_t addr_size;
-       struct sockaddr_in client_addr;
-       struct timeval tv;
-       int i;
-
-       while (!dhcprelay_stopflag) {
+       struct sockaddr_in server_addr;
+       char **iface_list;
+       int *fds;
+       int num_sockets, max_socket;
+       uint32_t our_nip;
+
+       server_addr.sin_family = AF_INET;
+       server_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
+       server_addr.sin_port = htons(SERVER_PORT);
+
+       /* dhcprelay CLIENT_IFACE1[,CLIENT_IFACE2...] SERVER_IFACE [SERVER_IP] */
+       if (argc == 4) {
+               if (!inet_aton(argv[3], &server_addr.sin_addr))
+                       bb_perror_msg_and_die("bad server IP");
+       } else if (argc != 3) {
+               bb_show_usage();
+       }
+
+       iface_list = make_iface_list(argv + 1, &num_sockets);
+
+       fds = xmalloc(num_sockets * sizeof(fds[0]));
+
+       /* Create sockets and bind one to every iface */
+       max_socket = init_sockets(iface_list, num_sockets, fds);
+
+       /* Get our IP on server_iface */
+       if (udhcp_read_interface(argv[2], NULL, &our_nip, NULL))
+               return 1;
+
+       /* Main loop */
+       while (1) {
+// reinit stuff from time to time? go back to make_iface_list
+// every N minutes?
+               fd_set rfds;
+               struct timeval tv;
+               int i;
+
                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) {
+                       int packlen;
+                       struct dhcp_packet dhcp_msg;
+
                        /* server */
                        if (FD_ISSET(fds[0], &rfds)) {
-                               packlen = udhcp_get_packet(&dhcp_msg, fds[0]);
+                               packlen = udhcp_recv_kernel_packet(&dhcp_msg, fds[0]);
                                if (packlen > 0) {
-                                       pass_back(&dhcp_msg, packlen, fds);
+                                       pass_to_client(&dhcp_msg, packlen, fds);
                                }
                        }
+
+                       /* clients */
                        for (i = 1; i < num_sockets; i++) {
-                               /* clients */
+                               struct sockaddr_in client_addr;
+                               socklen_t addr_size;
+
                                if (!FD_ISSET(fds[i], &rfds))
                                        continue;
-                               addr_size = sizeof(struct sockaddr_in);
+
+                               addr_size = sizeof(client_addr);
                                packlen = recvfrom(fds[i], &dhcp_msg, sizeof(dhcp_msg), 0,
-                                                       (struct sockaddr *)(&client_addr), &addr_size);
+                                               (struct sockaddr *)(&client_addr), &addr_size);
                                if (packlen <= 0)
                                        continue;
-                               if (read_interface(clients[i-1], NULL, &dhcp_msg.giaddr, NULL))
-                                       dhcp_msg.giaddr = gw_ip;
-                               pass_on(&dhcp_msg, packlen, i, fds, &client_addr, server_addr);
+
+                               /* Get our IP on corresponding client_iface */
+// 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!
+                                       dhcp_msg.gateway_nip = our_nip;
+                               }
+// maybe dhcp_msg.hops++? drop packets with too many hops (RFC 1542 says 4 or 16)?
+                               pass_to_server(&dhcp_msg, packlen, i, fds, &client_addr, &server_addr);
                        }
                }
                xid_expire();
-       }
-}
-
-int dhcprelay_main(int argc, char **argv);
-int dhcprelay_main(int argc, char **argv)
-{
-       int i, num_sockets, max_socket, fds[MAX_INTERFACES];
-       uint32_t gw_ip;
-       char **clients;
-       struct sockaddr_in server_addr;
-
-       server_addr.sin_family = AF_INET;
-       server_addr.sin_port = htons(67);
-       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();
-       }
-       clients = get_client_devices(argv[1], &num_sockets);
-       if (!clients) return 0;
-
-       signal(SIGTERM, dhcprelay_signal_handler);
-       signal(SIGQUIT, dhcprelay_signal_handler);
-       signal(SIGINT, dhcprelay_signal_handler);
-
-       num_sockets = init_sockets(clients, num_sockets, argv[2], fds, &max_socket);
-
-       if (read_interface(argv[2], NULL, &gw_ip, NULL))
-               return 1;
-
-       dhcprelay_loop(fds, num_sockets, max_socket, clients, &server_addr, gw_ip);
-
-       if (ENABLE_FEATURE_CLEAN_UP) {
-               for (i = 0; i < num_sockets; i++) {
-                       close(fds[i]);
-                       free(clients[i]);
-               }
-       }
+       } /* while (1) */
 
-       return 0;
+       /* return 0; - not reached */
 }