route: Fix compile warning with glibc
[project/relayd.git] / route.c
diff --git a/route.c b/route.c
index 5935b82c02e98e63a33165c0de902b2ccb1df1dc..602fdc9e1a9d498de5be3c2ca19aed78cd9644a5 100644 (file)
--- a/route.c
+++ b/route.c
 
 #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;
 
 static void rtnl_flush(void)
 {
+       ssize_t ret;
        int fd;
 
        fd = open("/proc/sys/net/ipv4/route/flush", O_WRONLY);
        if (fd < 0)
                return;
 
-       write(fd, "-1", 2);
+       ret = write(fd, "-1", 2);
+       if (ret != 2)
+               perror("write");
        close(fd);
 }
 
@@ -59,13 +65,17 @@ 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 {
                        struct rtattr rta;
                        int table;
                } __packed table;
+               struct {
+                       struct rtattr rta;
+                       int prio;
+               } __packed prio;
                struct {
                        struct rtattr rta;
                        char ifname[IFNAMSIZ + 1];
@@ -77,6 +87,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),
@@ -89,16 +104,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 {
-               uint32_t val = 1;
-               req.dev.rta.rta_type = FRA_PRIORITY;
-               req.dev.rta.rta_len = sizeof(req.dev.rta) + sizeof(uint32_t);
-               padding -= sizeof(uint32_t);
-               memcpy(&req.dev.ifname, &val, sizeof(val));
+               padding = sizeof(req.dev);
+               req.prio.prio--;
        }
        req.table.table = get_route_table(rif);
        req.nl.nlmsg_len = sizeof(req) - padding;
@@ -371,7 +384,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) {