relayd: put rule requests on stack
[project/relayd.git] / route.c
diff --git a/route.c b/route.c
index cbb624e..c552d1f 100644 (file)
--- a/route.c
+++ b/route.c
 #include <errno.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <time.h>
 
 #include <linux/fib_rules.h>
 
 #include "relayd.h"
 
+#define NLMSG_ALIGNTO        4U
+#define NLMSG_ALIGN(len) ( ((len)+NLMSG_ALIGNTO-1) & ~(NLMSG_ALIGNTO-1) )
+
 static struct uloop_fd rtnl_sock;
 static unsigned int rtnl_seq, rtnl_dump_seq;
 int route_table = 16800;
@@ -58,7 +62,7 @@ static int get_route_table(struct relayd_interface *rif)
 static void
 rtnl_rule_request(struct relayd_interface *rif, int flags)
 {
-       static struct {
+       struct {
                struct nlmsghdr nl;
                struct rtmsg rt;
                struct {
@@ -67,6 +71,10 @@ rtnl_rule_request(struct relayd_interface *rif, int flags)
                } __packed table;
                struct {
                        struct rtattr rta;
+                       int prio;
+               } __packed prio;
+               struct {
+                       struct rtattr rta;
                        char ifname[IFNAMSIZ + 1];
                } __packed dev;
        } __packed req = {
@@ -76,6 +84,11 @@ rtnl_rule_request(struct relayd_interface *rif, int flags)
                        .rtm_scope = RT_SCOPE_UNIVERSE,
                        .rtm_protocol = RTPROT_BOOT,
                },
+               .prio = {
+                       .rta.rta_type = FRA_PRIORITY,
+                       .rta.rta_len = sizeof(req.prio),
+                       .prio = 2,
+               },
                .table.rta = {
                        .rta_type = FRA_TABLE,
                        .rta_len = sizeof(req.table),
@@ -88,15 +101,14 @@ rtnl_rule_request(struct relayd_interface *rif, int flags)
                ifname = rif->ifname;
 
        if (!(flags & RULE_F_DEFGW_WORKAROUND)) {
+               int len = strlen(ifname) + 1;
                req.dev.rta.rta_type = FRA_IFNAME;
-               padding -= strlen(ifname) + 1;
+               padding -= NLMSG_ALIGN(len);
                strcpy(req.dev.ifname, ifname);
-               req.dev.rta.rta_len = sizeof(req.dev.rta) + strlen(ifname) + 1;
+               req.dev.rta.rta_len = sizeof(req.dev.rta) + len;
        } else {
-               req.dev.rta.rta_type = FRA_PRIORITY;
-               req.dev.rta.rta_len = sizeof(req.dev.rta) + sizeof(uint32_t);
-               padding -= sizeof(uint32_t);
-               *((uint32_t *) &req.dev.ifname) = 1;
+               padding = sizeof(req.dev);
+               req.prio.prio--;
        }
        req.table.table = get_route_table(rif);
        req.nl.nlmsg_len = sizeof(req) - padding;
@@ -369,7 +381,7 @@ static void rtnl_dump_request(int nlmsg_type)
 
 int relayd_rtnl_init(void)
 {
-       struct sockaddr_nl snl_local;
+       struct sockaddr_nl snl_local = {};
 
        rtnl_sock.fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
        if (rtnl_sock.fd < 0) {