Add default gateway and DHCP handling (not fully working yet because of a kernel...
authorFelix Fietkau <nbd@openwrt.org>
Mon, 16 Aug 2010 22:48:23 +0000 (00:48 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Tue, 17 Aug 2010 17:31:14 +0000 (19:31 +0200)
dhcp.c
main.c
relayd.h
route.c

diff --git a/dhcp.c b/dhcp.c
index a144346dd2ab8422f981bccb1c5e697407eddf34..9dbea282b739bc11075869cafc269b26e3a2d1c1 100644 (file)
--- a/dhcp.c
+++ b/dhcp.c
@@ -29,6 +29,19 @@ struct ip_packet {
        struct iphdr iph;
 } __packed;
 
+
+enum {
+       DHCP_OPTION_ROUTER = 0x03,
+       DHCP_OPTION_ROUTES = 0x79,
+       DHCP_OPTION_END = 0xff,
+};
+
+struct dhcp_option {
+       uint8_t code;
+       uint8_t len;
+       uint8_t data[];
+};
+
 struct dhcp_header {
        uint8_t op, htype, hlen, hops;
        uint32_t xit;
@@ -37,6 +50,8 @@ struct dhcp_header {
        unsigned char chaddr[16];
        unsigned char sname[64];
        unsigned char file[128];
+       uint32_t cookie;
+       uint8_t option_data[];
 } __packed;
 
 static uint16_t
@@ -65,11 +80,46 @@ chksum(uint16_t sum, const uint8_t *data, uint16_t len)
        return sum;
 }
 
+static void
+parse_dhcp_options(struct relayd_host *host, struct dhcp_header *dhcp, int len)
+{
+       uint8_t *end = (uint8_t *) dhcp + len;
+       struct dhcp_option *opt = (void *)dhcp->option_data;
+       static const uint8_t dest[4] = { 0, 0, 0, 0 };
+
+       while((uint8_t *) opt < end) {
+               if ((uint8_t *) opt + opt->len > end)
+                       break;
+
+               opt = (void *) &opt->data[opt->len];
+               switch(opt->code) {
+               case DHCP_OPTION_ROUTER:
+                       DPRINTF(2, "Found a DHCP router option, len=%d\n", opt->len);
+                       if (!memcmp(opt->data, host->ipaddr, 4))
+                               relayd_add_host_route(host, dest, 0);
+                       else
+                               relayd_add_pending_route(opt->data, dest, 0, 10000);
+                       break;
+               case DHCP_OPTION_ROUTES:
+                       DPRINTF(2, "Found a DHCP static routes option, len=%d\n", opt->len);
+                       break;
+               case DHCP_OPTION_END:
+                       opt = (void *) end;
+                       continue;
+               default:
+                       DPRINTF(3, "Skipping unknown DHCP option %02x\n", opt->code);
+                       continue;
+               }
+
+       }
+}
+
 bool relayd_handle_dhcp_packet(struct relayd_interface *rif, void *data, int len, bool forward)
 {
        struct ip_packet *pkt = data;
        struct udphdr *udp;
        struct dhcp_header *dhcp;
+       struct relayd_host *host;
        int udplen;
        uint16_t sum;
 
@@ -98,8 +148,11 @@ bool relayd_handle_dhcp_packet(struct relayd_interface *rif, void *data, int len
        if (!forward)
                return true;
 
-       if (dhcp->op == 2)
-               relayd_refresh_host(rif, pkt->eth.ether_shost, (void *) &pkt->iph.saddr);
+       if (dhcp->op == 2) {
+               host = relayd_refresh_host(rif, pkt->eth.ether_shost, (void *) &pkt->iph.saddr);
+               if (host)
+                       parse_dhcp_options(host, dhcp, udplen - sizeof(struct udphdr));
+       }
 
        DPRINTF(2, "%s: handling DHCP %s\n", rif->ifname, (dhcp->op == 1 ? "request" : "response"));
 
diff --git a/main.c b/main.c
index 62d7c2b25fd72e3d2fdbd6418755b9a2a337abae..ef669faa26aceb7b17cd611020ed306ae5bc9f0b 100644 (file)
--- a/main.c
+++ b/main.c
@@ -31,6 +31,7 @@
 
 #include "relayd.h"
 
+static LIST_HEAD(pending_routes);
 LIST_HEAD(interfaces);
 int debug;
 
@@ -39,6 +40,12 @@ static int inet_sock;
 static int forward_bcast;
 static int forward_dhcp;
 
+struct relayd_pending_route {
+       struct relayd_route rt;
+       struct uloop_timeout timeout;
+       uint8_t gateway[4];
+};
+
 static struct relayd_host *find_host_by_ipaddr(struct relayd_interface *rif, const uint8_t *ipaddr)
 {
        struct relayd_host *host;
@@ -81,20 +88,55 @@ static void add_arp(struct relayd_host *host)
        ioctl(inet_sock, SIOCSARP, &arp);
 }
 
+static void timeout_host_route(struct uloop_timeout *timeout)
+{
+       struct relayd_pending_route *rt;
+
+       rt = container_of(timeout, struct relayd_pending_route, timeout);
+       list_del(&rt->rt.list);
+       free(rt);
+}
+
+void relayd_add_host_route(struct relayd_host *host, const uint8_t *dest, uint8_t mask)
+{
+       struct relayd_route *rt;
+
+       list_for_each_entry(rt, &host->routes, list) {
+               if (!memcmp(rt->dest, dest, sizeof(rt->dest)) && rt->mask == mask)
+                       return;
+       }
+
+       rt = calloc(1, sizeof(*rt));
+       if (!rt)
+               return;
+
+       list_add(&rt->list, &host->routes);
+       memcpy(rt->dest, dest, sizeof(rt->dest));
+       rt->mask = mask;
+       relayd_add_route(host, rt);
+}
+
 static void del_host(struct relayd_host *host)
 {
+       struct relayd_route *route, *tmp;
+
        DPRINTF(1, "%s: deleting host "IP_FMT" ("MAC_FMT")\n", host->rif->ifname,
                IP_BUF(host->ipaddr), MAC_BUF(host->lladdr));
 
+       list_for_each_entry_safe(route, tmp, &host->routes, list) {
+               relayd_del_route(host, route);
+               list_del(&route->list);
+               free(route);
+       }
        if (host->rif->managed)
-               relayd_del_route(host);
+               relayd_del_route(host, NULL);
        uloop_timeout_cancel(&host->timeout);
        list_del(&host->list);
        free(host);
 }
 
 static void fill_arp_request(struct arp_packet *pkt, struct relayd_interface *rif,
-                             uint8_t spa[4], uint8_t tpa[4])
+                             const uint8_t spa[4], const uint8_t tpa[4])
 {
        memset(pkt, 0, sizeof(*pkt));
 
@@ -111,12 +153,11 @@ static void fill_arp_request(struct arp_packet *pkt, struct relayd_interface *ri
        pkt->arp.arp_pln = 4;
 }
 
-static void send_arp_request(struct relayd_host *host)
+static void send_arp_request(struct relayd_interface *rif, const uint8_t *ipaddr)
 {
-       struct relayd_interface *rif = host->rif;
        struct arp_packet pkt;
 
-       fill_arp_request(&pkt, host->rif, host->rif->src_ip, host->ipaddr);
+       fill_arp_request(&pkt, rif, rif->src_ip, ipaddr);
 
        pkt.arp.arp_op = htons(ARPOP_REQUEST);
        memcpy(pkt.arp.arp_spa, rif->src_ip, ETH_ALEN);
@@ -131,6 +172,36 @@ static void send_arp_request(struct relayd_host *host)
                (struct sockaddr *) &rif->sll, sizeof(rif->sll));
 }
 
+void relayd_add_pending_route(const uint8_t *gateway, const uint8_t *dest, uint8_t mask, int timeout)
+{
+       struct relayd_pending_route *rt;
+       struct relayd_interface *rif;
+       struct relayd_host *host;
+
+       host = find_host_by_ipaddr(NULL, gateway);
+       if (host) {
+               relayd_add_host_route(host, dest, mask);
+               return;
+       }
+
+       rt = calloc(1, sizeof(*rt));
+       if (!rt)
+               return;
+
+       memcpy(rt->gateway, gateway, sizeof(rt->gateway));
+       memcpy(rt->rt.dest, dest, sizeof(rt->rt.dest));
+       rt->rt.mask = mask;
+       list_add(&rt->rt.list, &pending_routes);
+       if (timeout <= 0)
+               return;
+
+       rt->timeout.cb = timeout_host_route;
+       uloop_timeout_set(&rt->timeout, 10000);
+       list_for_each_entry(rif, &interfaces, list) {
+               send_arp_request(rif, gateway);
+       }
+}
+
 static void send_arp_reply(struct relayd_interface *rif, uint8_t spa[4],
                            uint8_t tha[ETH_ALEN], uint8_t tpa[4])
 {
@@ -162,7 +233,7 @@ static void host_entry_timeout(struct uloop_timeout *timeout)
         * giving up on it.
         */
        if (host->rif->managed && host->cleanup_pending < 2) {
-               send_arp_request(host);
+               send_arp_request(host->rif, host->ipaddr);
                host->cleanup_pending++;
                uloop_timeout_set(&host->timeout, 1000);
                return;
@@ -173,11 +244,13 @@ static void host_entry_timeout(struct uloop_timeout *timeout)
 static struct relayd_host *add_host(struct relayd_interface *rif, const uint8_t *lladdr, const uint8_t *ipaddr)
 {
        struct relayd_host *host;
+       struct relayd_pending_route *route, *rtmp;
 
        DPRINTF(1, "%s: adding host "IP_FMT" ("MAC_FMT")\n", rif->ifname,
                        IP_BUF(ipaddr), MAC_BUF(lladdr));
 
        host = calloc(1, sizeof(*host));
+       INIT_LIST_HEAD(&host->routes);
        host->rif = rif;
        memcpy(host->ipaddr, ipaddr, sizeof(host->ipaddr));
        memcpy(host->lladdr, lladdr, sizeof(host->lladdr));
@@ -187,7 +260,20 @@ static struct relayd_host *add_host(struct relayd_interface *rif, const uint8_t
 
        add_arp(host);
        if (rif->managed)
-               relayd_add_route(host);
+               relayd_add_route(host, NULL);
+
+       list_for_each_entry_safe(route, rtmp, &pending_routes, rt.list) {
+               if (memcmp(route->gateway, ipaddr, 4) != 0)
+                       continue;
+
+               relayd_add_host_route(host, route->rt.dest, route->rt.mask);
+               if (!route->timeout.pending)
+                       continue;
+
+               uloop_timeout_cancel(&route->timeout);
+               list_del(&route->rt.list);
+               free(route);
+       }
 
        return host;
 }
@@ -207,8 +293,10 @@ struct relayd_host *relayd_refresh_host(struct relayd_interface *rif, const uint
                 * If the old entry is behind a managed interface, it will be pinged
                 * before we expire it
                 */
-               if (host && !host->cleanup_pending)
+               if (host && !host->cleanup_pending) {
                        uloop_timeout_set(&host->timeout, 1);
+                       return NULL;
+               }
 
                host = add_host(rif, lladdr, ipaddr);
        } else {
@@ -281,7 +369,8 @@ static void recv_arp_reply(struct relayd_interface *rif, struct arp_packet *pkt)
                MAC_BUF(pkt->eth.ether_shost),
                IP_BUF(pkt->arp.arp_tpa));
 
-       relayd_refresh_host(rif, pkt->arp.arp_sha, pkt->arp.arp_spa);
+       if (memcmp(pkt->arp.arp_sha, rif->sll.sll_addr, ETH_ALEN) != 0)
+               relayd_refresh_host(rif, pkt->arp.arp_sha, pkt->arp.arp_spa);
 
        if (!memcmp(pkt->arp.arp_tpa, rif->src_ip, 4))
                return;
@@ -459,6 +548,16 @@ static int init_interface(struct relayd_interface *rif)
        return 0;
 }
 
+static void ping_static_routes(void)
+{
+       struct relayd_pending_route *rt;
+       struct relayd_interface *rif;
+
+       list_for_each_entry(rt, &pending_routes, rt.list)
+               list_for_each_entry(rif, &interfaces, list)
+                       send_arp_request(rif, rt->gateway);
+}
+
 static int init_interfaces(void)
 {
        struct relayd_interface *rif;
@@ -496,24 +595,23 @@ static void free_interfaces(void)
        }
 }
 
-static int alloc_interface(const char *ifname, bool managed)
+static struct relayd_interface *alloc_interface(const char *ifname, bool managed)
 {
        struct relayd_interface *rif;
 
        if (strlen(ifname) >= IFNAMSIZ)
-               return -1;
+               return NULL;
 
        rif = calloc(1, sizeof(*rif));
        if (!rif)
-               return -1;
+               return NULL;
 
-       INIT_LIST_HEAD(&rif->list);
        INIT_LIST_HEAD(&rif->hosts);
        strcpy(rif->ifname, ifname);
        list_add(&rif->list, &interfaces);
        rif->managed = managed;
 
-       return 0;
+       return rif;
 }
 
 static void die(int signo)
@@ -536,6 +634,7 @@ static int usage(const char *progname)
                        "       -i <ifname>     Add an interface for relaying\n"
                        "       -I <ifname>     Same as -i, except with ARP cache and host route management\n"
                        "                       You need to specify at least two interfaces\n"
+                       "       -G <ip>         Set a gateway IP for clients\n"
                        "       -t <timeout>    Host entry expiry timeout\n"
                        "       -T <table>      Set routing table number for automatically added routes\n"
                        "       -B              Enable broadcast forwarding\n"
@@ -547,6 +646,8 @@ static int usage(const char *progname)
 
 int main(int argc, char **argv)
 {
+       struct relayd_interface *rif = NULL;
+       struct in_addr addr;
        bool managed;
        int ifnum = 0;
        int ch;
@@ -562,14 +663,15 @@ int main(int argc, char **argv)
        forward_bcast = 0;
        uloop_init();
 
-       while ((ch = getopt(argc, argv, "I:i:t:BDdT:")) != -1) {
+       while ((ch = getopt(argc, argv, "I:i:t:BDdT:G:")) != -1) {
                switch(ch) {
                case 'I':
                        managed = true;
                        /* fall through */
                case 'i':
                        ifnum++;
-                       if (alloc_interface(optarg, managed) < 0)
+                       rif = alloc_interface(optarg, managed);
+                       if (!rif)
                                return 1;
 
                        managed = false;
@@ -593,6 +695,13 @@ int main(int argc, char **argv)
                        if (route_table <= 0)
                                return usage(argv[0]);
                        break;
+               case 'G':
+                       if (!inet_aton(optarg, &addr)) {
+                               fprintf(stderr, "Address '%s' not found\n", optarg);
+                               return 1;
+                       }
+                       relayd_add_pending_route((uint8_t *) &addr.s_addr, (const uint8_t *) "\x00\x00\x00\x00", 0, 0);
+                       break;
                case '?':
                default:
                        return usage(argv[0]);
@@ -621,6 +730,8 @@ int main(int argc, char **argv)
        if (init_interfaces() < 0)
                return 1;
 
+       ping_static_routes();
+
        uloop_run();
        uloop_done();
 
index 3ae4edcba15d733c56aba37c939fe778019449c6..e21ca08ca3890d8bcc48b0b3d65601635021b42d 100644 (file)
--- a/relayd.h
+++ b/relayd.h
@@ -73,6 +73,7 @@ struct relayd_interface {
 
 struct relayd_host {
        struct list_head list;
+       struct list_head routes;
        struct relayd_interface *rif;
        uint8_t lladdr[ETH_ALEN];
        uint8_t ipaddr[4];
@@ -80,6 +81,12 @@ struct relayd_host {
        int cleanup_pending;
 };
 
+struct relayd_route {
+       struct list_head list;
+       uint8_t dest[4];
+       uint8_t mask;
+};
+
 struct arp_packet {
        struct ether_header eth;
        struct ether_arp arp;
@@ -94,16 +101,16 @@ extern struct list_head interfaces;
 extern int debug;
 extern int route_table;
 
-void rtnl_route_set(struct relayd_host *host, bool add);
+void rtnl_route_set(struct relayd_host *host, struct relayd_route *route, bool add);
 
-static inline void relayd_add_route(struct relayd_host *host)
+static inline void relayd_add_route(struct relayd_host *host, struct relayd_route *route)
 {
-       rtnl_route_set(host, true);
+       rtnl_route_set(host, route, true);
 }
 
-static inline void relayd_del_route(struct relayd_host *host)
+static inline void relayd_del_route(struct relayd_host *host, struct relayd_route *route)
 {
-       rtnl_route_set(host, false);
+       rtnl_route_set(host, route, false);
 }
 
 void relayd_add_interface_routes(struct relayd_interface *rif);
@@ -115,6 +122,8 @@ void relayd_rtnl_done(void);
 struct relayd_host *relayd_refresh_host(struct relayd_interface *rif,
                                        const uint8_t *lladdr,
                                        const uint8_t *ipaddr);
+void relayd_add_host_route(struct relayd_host *host, const uint8_t *ipaddr, uint8_t mask);
+void relayd_add_pending_route(const uint8_t *gateway, const uint8_t *dest, uint8_t mask, int timeout);
 
 void relayd_forward_bcast_packet(struct relayd_interface *from_rif, void *packet, int len);
 bool relayd_handle_dhcp_packet(struct relayd_interface *rif, void *data, int len, bool forward);
diff --git a/route.c b/route.c
index c41100dbf4e7a683375848114a3fdc2122ddd91b..25f949e3f27bbacdb5fcda626eabf6cd6e7b597b 100644 (file)
--- a/route.c
+++ b/route.c
@@ -42,7 +42,9 @@ static void rtnl_flush(void)
        close(fd);
 }
 
-static void rtnl_route_request(struct relayd_interface *rif, struct relayd_host *host, bool add)
+static void
+rtnl_route_request(struct relayd_interface *rif, struct relayd_host *host,
+                  struct relayd_route *route, bool add)
 {
        static struct {
                struct nlmsghdr nl;
@@ -59,10 +61,11 @@ static void rtnl_route_request(struct relayd_interface *rif, struct relayd_host
                        struct rtattr rta;
                        int ifindex;
                } __packed dev;
+               struct {
+                       struct rtattr rta;
+                       uint8_t ipaddr[4];
+               } __packed gw;
        } __packed req = {
-               .nl = {
-                       .nlmsg_len = sizeof(req),
-               },
                .rt = {
                        .rtm_family = AF_INET,
                        .rtm_dst_len = 32,
@@ -73,16 +76,19 @@ static void rtnl_route_request(struct relayd_interface *rif, struct relayd_host
                        .rta_len = sizeof(req.table),
                },
                .dst.rta = {
-                       .rta_type = RTA_DST,
                        .rta_len = sizeof(req.dst),
                },
                .dev.rta = {
                        .rta_type = RTA_OIF,
                        .rta_len = sizeof(req.dev),
                },
+               .gw.rta = {
+                       .rta_type = RTA_GATEWAY,
+                       .rta_len = sizeof(req.gw),
+               },
        };
+       int pktlen = sizeof(req);
 
-       memcpy(req.dst.ipaddr, host->ipaddr, sizeof(req.dst.ipaddr));
        req.dev.ifindex = host->rif->sll.sll_ifindex;
        req.table.table = rif->rt_table;
 
@@ -92,14 +98,40 @@ static void rtnl_route_request(struct relayd_interface *rif, struct relayd_host
                req.nl.nlmsg_flags |= NLM_F_CREATE | NLM_F_REPLACE;
 
                req.rt.rtm_protocol = RTPROT_BOOT;
-               req.rt.rtm_scope = RT_SCOPE_LINK;
+               if (route) {
+                       req.rt.rtm_scope = RT_SCOPE_UNIVERSE;
+               } else {
+                       req.rt.rtm_scope = RT_SCOPE_LINK;
+               }
                req.rt.rtm_type = RTN_UNICAST;
        } else {
                req.nl.nlmsg_type = RTM_DELROUTE;
                req.rt.rtm_scope = RT_SCOPE_NOWHERE;
        }
 
-       send(rtnl_sock.fd, &req, sizeof(req), 0);
+       req.dst.rta.rta_type = RTA_DST;
+       if (route) {
+               DPRINTF(2, "%s: add route to "IP_FMT"/%d via "IP_FMT"\n",
+                       host->rif->ifname, IP_BUF(route->dest), route->mask, IP_BUF(host->ipaddr));
+               if (!route->mask) {
+                       /* No DST -> reuse the DST attribute as the gateway attribute */
+                       memcpy(req.dst.ipaddr, host->ipaddr, sizeof(req.dst.ipaddr));
+                       req.dst.rta.rta_type = RTA_GATEWAY;
+                       pktlen -= sizeof(req.gw);
+                       req.rt.rtm_dst_len = 0;
+               } else {
+                       memcpy(req.gw.ipaddr, host->ipaddr, sizeof(req.gw.ipaddr));
+                       memcpy(req.dst.ipaddr, route->dest, sizeof(req.dst.ipaddr));
+                       req.rt.rtm_dst_len = route->mask;
+               }
+       } else {
+               pktlen -= sizeof(req.gw);
+               req.rt.rtm_dst_len = 32;
+               memcpy(req.dst.ipaddr, host->ipaddr, sizeof(req.dst.ipaddr));
+       }
+
+       req.nl.nlmsg_len = pktlen;
+       send(rtnl_sock.fd, &req, pktlen, 0);
        rtnl_flush();
 }
 
@@ -155,7 +187,8 @@ static void rtnl_rule_request(struct relayd_interface *rif, bool add)
        rtnl_flush();
 }
 
-void rtnl_route_set(struct relayd_host *host, bool add)
+void
+rtnl_route_set(struct relayd_host *host, struct relayd_route *route, bool add)
 {
        struct relayd_interface *rif;
 
@@ -163,7 +196,7 @@ void rtnl_route_set(struct relayd_host *host, bool add)
                if (rif == host->rif)
                        continue;
 
-               rtnl_route_request(rif, host, add);
+               rtnl_route_request(rif, host, route, add);
        }
 }