netlink, pasta: Configure MTU of tap interface on --config-net

Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
Stefano Brivio 2021-10-14 13:05:56 +02:00
parent 54a19002df
commit 3c6d24dd30
4 changed files with 33 additions and 21 deletions

2
conf.c
View file

@ -484,7 +484,7 @@ static void conf_ip(struct ctx *c)
memcpy(&c->addr4_seen, &c->addr4, sizeof(c->addr4_seen)); memcpy(&c->addr4_seen, &c->addr4, sizeof(c->addr4_seen));
if (!memcmp(c->mac, MAC_ZERO, ETH_ALEN)) 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) if (c->mode == MODE_PASST)

View file

@ -454,27 +454,28 @@ next:
* @ifi: Interface index * @ifi: Interface index
* @mac: MAC address to fill, if passed as zero, to set otherwise * @mac: MAC address to fill, if passed as zero, to set otherwise
* @up: If set, bring up the link * @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 {
struct nlmsghdr nlh; struct nlmsghdr nlh;
struct ifinfomsg ifm; struct ifinfomsg ifm;
struct rtattr rta; struct rtattr rta;
unsigned char mac[ETH_ALEN]; union {
unsigned char mac[ETH_ALEN];
unsigned int mtu;
};
} req = { } req = {
.nlh.nlmsg_type = change ? RTM_NEWLINK : RTM_GETLINK, .nlh.nlmsg_type = change ? RTM_NEWLINK : RTM_GETLINK,
.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)), .nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
.nlh.nlmsg_flags = NLM_F_REQUEST | (change ? NLM_F_ACK : 0), .nlh.nlmsg_flags = NLM_F_REQUEST | (change ? NLM_F_ACK : 0),
.nlh.nlmsg_seq = nl_seq++, .nlh.nlmsg_seq = nl_seq++,
.ifm.ifi_family = AF_UNSPEC, .ifm.ifi_family = AF_UNSPEC,
.ifm.ifi_index = ifi, .ifm.ifi_index = ifi,
.ifm.ifi_flags = up ? IFF_UP : 0, .ifm.ifi_flags = up ? IFF_UP : 0,
.ifm.ifi_change = up ? IFF_UP : 0, .ifm.ifi_change = up ? IFF_UP : 0,
.rta.rta_type = IFLA_ADDRESS,
.rta.rta_len = RTA_LENGTH(ETH_ALEN),
}; };
struct ifinfomsg *ifm; struct ifinfomsg *ifm;
struct nlmsghdr *nh; struct nlmsghdr *nh;
@ -485,13 +486,24 @@ void nl_link(int ns, unsigned int ifi, void *mac, int up)
if (!MAC_IS_ZERO(mac)) { if (!MAC_IS_ZERO(mac)) {
req.nlh.nlmsg_len = sizeof(req); req.nlh.nlmsg_len = sizeof(req);
memcpy(req.mac, mac, ETH_ALEN); 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; return;
n = nl_req(ns, buf, &req, req.nlh.nlmsg_len);
nh = (struct nlmsghdr *)buf; nh = (struct nlmsghdr *)buf;
for ( ; NLMSG_OK(nh, n); nh = NLMSG_NEXT(nh, n)) { for ( ; NLMSG_OK(nh, n); nh = NLMSG_NEXT(nh, n)) {
if (nh->nlmsg_type != RTM_NEWLINK) if (nh->nlmsg_type != RTM_NEWLINK)

View file

@ -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_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 nl_addr(int ns, unsigned int ifi, sa_family_t af,
void *addr, int prefix_len, void *addr_l); 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);

View file

@ -230,10 +230,10 @@ void pasta_start_ns(struct ctx *c)
*/ */
void pasta_ns_conf(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) { 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) { if (c->v4) {
nl_addr(1, c->pasta_ifi, AF_INET, &c->addr4, 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); nl_route(1, c->pasta_ifi, AF_INET6, &c->gw6);
} }
} else { } 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); proto_update_l2_buf(c->mac_guest, NULL, NULL);