From 3c6d24dd3021bb294a7aa182a95a9cb868ca6cb4 Mon Sep 17 00:00:00 2001 From: Stefano Brivio Date: Thu, 14 Oct 2021 13:05:56 +0200 Subject: [PATCH] netlink, pasta: Configure MTU of tap interface on --config-net Signed-off-by: Stefano Brivio --- conf.c | 2 +- netlink.c | 44 ++++++++++++++++++++++++++++---------------- netlink.h | 2 +- pasta.c | 6 +++--- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/conf.c b/conf.c index db9c350..483daeb 100644 --- a/conf.c +++ b/conf.c @@ -484,7 +484,7 @@ static void conf_ip(struct ctx *c) memcpy(&c->addr4_seen, &c->addr4, sizeof(c->addr4_seen)); if (!memcmp(c->mac, MAC_ZERO, ETH_ALEN)) - nl_link(0, c->ifi, c->mac, 0); + nl_link(0, c->ifi, c->mac, 0, 0); } if (c->mode == MODE_PASST) diff --git a/netlink.c b/netlink.c index ca2e77f..a26078b 100644 --- a/netlink.c +++ b/netlink.c @@ -454,27 +454,28 @@ next: * @ifi: Interface index * @mac: MAC address to fill, if passed as zero, to set otherwise * @up: If set, bring up the link + * @mtu: If non-zero, set interface MTU */ -void nl_link(int ns, unsigned int ifi, void *mac, int up) +void nl_link(int ns, unsigned int ifi, void *mac, int up, int mtu) { - int change = !MAC_IS_ZERO(mac) || up; + int change = !MAC_IS_ZERO(mac) || up || mtu; struct { struct nlmsghdr nlh; struct ifinfomsg ifm; struct rtattr rta; - unsigned char mac[ETH_ALEN]; + union { + unsigned char mac[ETH_ALEN]; + unsigned int mtu; + }; } req = { - .nlh.nlmsg_type = change ? RTM_NEWLINK : RTM_GETLINK, - .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)), - .nlh.nlmsg_flags = NLM_F_REQUEST | (change ? NLM_F_ACK : 0), - .nlh.nlmsg_seq = nl_seq++, - .ifm.ifi_family = AF_UNSPEC, - .ifm.ifi_index = ifi, - .ifm.ifi_flags = up ? IFF_UP : 0, - .ifm.ifi_change = up ? IFF_UP : 0, - - .rta.rta_type = IFLA_ADDRESS, - .rta.rta_len = RTA_LENGTH(ETH_ALEN), + .nlh.nlmsg_type = change ? RTM_NEWLINK : RTM_GETLINK, + .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)), + .nlh.nlmsg_flags = NLM_F_REQUEST | (change ? NLM_F_ACK : 0), + .nlh.nlmsg_seq = nl_seq++, + .ifm.ifi_family = AF_UNSPEC, + .ifm.ifi_index = ifi, + .ifm.ifi_flags = up ? IFF_UP : 0, + .ifm.ifi_change = up ? IFF_UP : 0, }; struct ifinfomsg *ifm; struct nlmsghdr *nh; @@ -485,13 +486,24 @@ void nl_link(int ns, unsigned int ifi, void *mac, int up) if (!MAC_IS_ZERO(mac)) { req.nlh.nlmsg_len = sizeof(req); memcpy(req.mac, mac, ETH_ALEN); + req.rta.rta_type = IFLA_ADDRESS; + req.rta.rta_len = RTA_LENGTH(ETH_ALEN); + nl_req(ns, buf, &req, req.nlh.nlmsg_len); } - n = nl_req(ns, buf, &req, req.nlh.nlmsg_len); + if (mtu) { + req.nlh.nlmsg_len = sizeof(req); + req.mtu = mtu; + req.rta.rta_type = IFLA_MTU; + req.rta.rta_len = RTA_LENGTH(sizeof(unsigned int)); + nl_req(ns, buf, &req, req.nlh.nlmsg_len); + } - if (!MAC_IS_ZERO(mac) || up) + if (change) return; + n = nl_req(ns, buf, &req, req.nlh.nlmsg_len); + nh = (struct nlmsghdr *)buf; for ( ; NLMSG_OK(nh, n); nh = NLMSG_NEXT(nh, n)) { if (nh->nlmsg_type != RTM_NEWLINK) diff --git a/netlink.h b/netlink.h index 654e17e..460c2f1 100644 --- a/netlink.h +++ b/netlink.h @@ -3,4 +3,4 @@ unsigned int nl_get_ext_if(int *v4, int *v6); void nl_route(int ns, unsigned int ifi, sa_family_t af, void *gw); void nl_addr(int ns, unsigned int ifi, sa_family_t af, void *addr, int prefix_len, void *addr_l); -void nl_link(int ns, unsigned int ifi, void *mac, int up); +void nl_link(int ns, unsigned int ifi, void *mac, int up, int mtu); diff --git a/pasta.c b/pasta.c index 7c53c13..b8f556e 100644 --- a/pasta.c +++ b/pasta.c @@ -230,10 +230,10 @@ void pasta_start_ns(struct ctx *c) */ void pasta_ns_conf(struct ctx *c) { - nl_link(1, 1 /* lo */, MAC_ZERO, 1); + nl_link(1, 1 /* lo */, MAC_ZERO, 1, 0); if (c->pasta_conf_ns) { - nl_link(1, c->pasta_ifi, c->mac_guest, 1); + nl_link(1, c->pasta_ifi, c->mac_guest, 1, c->mtu); if (c->v4) { nl_addr(1, c->pasta_ifi, AF_INET, &c->addr4, @@ -246,7 +246,7 @@ void pasta_ns_conf(struct ctx *c) nl_route(1, c->pasta_ifi, AF_INET6, &c->gw6); } } else { - nl_link(1, c->pasta_ifi, c->mac_guest, 0); + nl_link(1, c->pasta_ifi, c->mac_guest, 0, 0); } proto_update_l2_buf(c->mac_guest, NULL, NULL);