From e96182e9c28197a8f868df5d2a9975bc51a090e9 Mon Sep 17 00:00:00 2001 From: David Gibson Date: Thu, 3 Aug 2023 17:19:40 +1000 Subject: netlink: Split up functionality of nl_link() nl_link() performs a number of functions: it can bring links up, set MAC address and MTU and also retrieve the existing MAC. This makes for a small number of lines of code, but high conceptual complexity: it's quite hard to follow what's going on both in nl_link() itself and it's also not very obvious which function its callers are intending to use. Clarify this, by splitting nl_link() into nl_link_up(), nl_link_set_mac(), and nl_link_get_mac(). The first brings up a link, optionally setting the MTU, the others get or set the MAC address. This fixes an arguable bug in pasta_ns_conf(): it looks as though that was intended to retrieve the guest MAC whether or not c->pasta_conf_ns is set. However, it only actually does so in the !c->pasta_conf_ns case: the fact that we set up==1 means we would only ever set, never get, the MAC in the nl_link() call in the other path. We get away with this because the MAC will quickly be discovered once we receive packets on the tap interface. Still, it's neater to always get the MAC address here. Signed-off-by: David Gibson Signed-off-by: Stefano Brivio --- pasta.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'pasta.c') diff --git a/pasta.c b/pasta.c index 8c85546..cb509dd 100644 --- a/pasta.c +++ b/pasta.c @@ -272,13 +272,19 @@ void pasta_start_ns(struct ctx *c, uid_t uid, gid_t gid, */ void pasta_ns_conf(struct ctx *c) { - nl_link(1, 1 /* lo */, MAC_ZERO, 1, 0); + nl_link_up(1, 1 /* lo */, 0); + + /* Get or set MAC in target namespace */ + if (MAC_IS_ZERO(c->mac_guest)) + nl_link_get_mac(1, c->pasta_ifi, c->mac_guest); + else + nl_link_set_mac(1, c->pasta_ifi, c->mac_guest); if (c->pasta_conf_ns) { enum nl_op op_routes = c->no_copy_routes ? NL_SET : NL_DUP; enum nl_op op_addrs = c->no_copy_addrs ? NL_SET : NL_DUP; - nl_link(1, c->pasta_ifi, c->mac_guest, 1, c->mtu); + nl_link_up(1, c->pasta_ifi, c->mtu); if (c->ifi4) { nl_addr(op_addrs, c->ifi4, c->pasta_ifi, AF_INET, @@ -294,8 +300,6 @@ void pasta_ns_conf(struct ctx *c) nl_route(op_routes, c->ifi6, c->pasta_ifi, AF_INET6, &c->ip6.gw); } - } else { - nl_link(1, c->pasta_ifi, c->mac_guest, 0, 0); } proto_update_l2_buf(c->mac_guest, NULL, NULL); -- cgit v1.2.3