LCOV - code coverage report
Current view: top level - nspawn - nspawn-network.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 5 389 1.3 %
Date: 2019-08-22 15:41:25 Functions: 1 15 6.7 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include <linux/veth.h>
       4             : #include <sys/file.h>
       5             : 
       6             : #include "sd-device.h"
       7             : #include "sd-id128.h"
       8             : #include "sd-netlink.h"
       9             : 
      10             : #include "alloc-util.h"
      11             : #include "ether-addr-util.h"
      12             : #include "lockfile-util.h"
      13             : #include "missing_network.h"
      14             : #include "netlink-util.h"
      15             : #include "nspawn-network.h"
      16             : #include "parse-util.h"
      17             : #include "siphash24.h"
      18             : #include "socket-util.h"
      19             : #include "stat-util.h"
      20             : #include "string-util.h"
      21             : #include "strv.h"
      22             : #include "util.h"
      23             : 
      24             : #define HOST_HASH_KEY SD_ID128_MAKE(1a,37,6f,c7,46,ec,45,0b,ad,a3,d5,31,06,60,5d,b1)
      25             : #define CONTAINER_HASH_KEY SD_ID128_MAKE(c3,c4,f9,19,b5,57,b2,1c,e6,cf,14,27,03,9c,ee,a2)
      26             : #define VETH_EXTRA_HOST_HASH_KEY SD_ID128_MAKE(48,c7,f6,b7,ea,9d,4c,9e,b7,28,d4,de,91,d5,bf,66)
      27             : #define VETH_EXTRA_CONTAINER_HASH_KEY SD_ID128_MAKE(af,50,17,61,ce,f9,4d,35,84,0d,2b,20,54,be,ce,59)
      28             : #define MACVLAN_HASH_KEY SD_ID128_MAKE(00,13,6d,bc,66,83,44,81,bb,0c,f9,51,1f,24,a6,6f)
      29             : 
      30           0 : static int remove_one_link(sd_netlink *rtnl, const char *name) {
      31           0 :         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
      32             :         int r;
      33             : 
      34           0 :         if (isempty(name))
      35           0 :                 return 0;
      36             : 
      37           0 :         r = sd_rtnl_message_new_link(rtnl, &m, RTM_DELLINK, 0);
      38           0 :         if (r < 0)
      39           0 :                 return log_error_errno(r, "Failed to allocate netlink message: %m");
      40             : 
      41           0 :         r = sd_netlink_message_append_string(m, IFLA_IFNAME, name);
      42           0 :         if (r < 0)
      43           0 :                 return log_error_errno(r, "Failed to add netlink interface name: %m");
      44             : 
      45           0 :         r = sd_netlink_call(rtnl, m, 0, NULL);
      46           0 :         if (r == -ENODEV) /* Already gone */
      47           0 :                 return 0;
      48           0 :         if (r < 0)
      49           0 :                 return log_error_errno(r, "Failed to remove interface %s: %m", name);
      50             : 
      51           0 :         return 1;
      52             : }
      53             : 
      54           0 : static int generate_mac(
      55             :                 const char *machine_name,
      56             :                 struct ether_addr *mac,
      57             :                 sd_id128_t hash_key,
      58             :                 uint64_t idx) {
      59             : 
      60             :         uint64_t result;
      61             :         size_t l, sz;
      62             :         uint8_t *v, *i;
      63             :         int r;
      64             : 
      65           0 :         l = strlen(machine_name);
      66           0 :         sz = sizeof(sd_id128_t) + l;
      67           0 :         if (idx > 0)
      68           0 :                 sz += sizeof(idx);
      69             : 
      70           0 :         v = newa(uint8_t, sz);
      71             : 
      72             :         /* fetch some persistent data unique to the host */
      73           0 :         r = sd_id128_get_machine((sd_id128_t*) v);
      74           0 :         if (r < 0)
      75           0 :                 return r;
      76             : 
      77             :         /* combine with some data unique (on this host) to this
      78             :          * container instance */
      79           0 :         i = mempcpy(v + sizeof(sd_id128_t), machine_name, l);
      80           0 :         if (idx > 0) {
      81           0 :                 idx = htole64(idx);
      82           0 :                 memcpy(i, &idx, sizeof(idx));
      83             :         }
      84             : 
      85             :         /* Let's hash the host machine ID plus the container name. We
      86             :          * use a fixed, but originally randomly created hash key here. */
      87           0 :         result = htole64(siphash24(v, sz, hash_key.bytes));
      88             : 
      89             :         assert_cc(ETH_ALEN <= sizeof(result));
      90           0 :         memcpy(mac->ether_addr_octet, &result, ETH_ALEN);
      91             : 
      92             :         /* see eth_random_addr in the kernel */
      93           0 :         mac->ether_addr_octet[0] &= 0xfe;        /* clear multicast bit */
      94           0 :         mac->ether_addr_octet[0] |= 0x02;        /* set local assignment bit (IEEE802) */
      95             : 
      96           0 :         return 0;
      97             : }
      98             : 
      99           0 : static int add_veth(
     100             :                 sd_netlink *rtnl,
     101             :                 pid_t pid,
     102             :                 const char *ifname_host,
     103             :                 const struct ether_addr *mac_host,
     104             :                 const char *ifname_container,
     105             :                 const struct ether_addr *mac_container) {
     106             : 
     107           0 :         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
     108             :         int r;
     109             : 
     110           0 :         assert(rtnl);
     111           0 :         assert(ifname_host);
     112           0 :         assert(mac_host);
     113           0 :         assert(ifname_container);
     114           0 :         assert(mac_container);
     115             : 
     116           0 :         r = sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0);
     117           0 :         if (r < 0)
     118           0 :                 return log_error_errno(r, "Failed to allocate netlink message: %m");
     119             : 
     120           0 :         r = sd_netlink_message_append_string(m, IFLA_IFNAME, ifname_host);
     121           0 :         if (r < 0)
     122           0 :                 return log_error_errno(r, "Failed to add netlink interface name: %m");
     123             : 
     124           0 :         r = sd_netlink_message_append_ether_addr(m, IFLA_ADDRESS, mac_host);
     125           0 :         if (r < 0)
     126           0 :                 return log_error_errno(r, "Failed to add netlink MAC address: %m");
     127             : 
     128           0 :         r = sd_netlink_message_open_container(m, IFLA_LINKINFO);
     129           0 :         if (r < 0)
     130           0 :                 return log_error_errno(r, "Failed to open netlink container: %m");
     131             : 
     132           0 :         r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "veth");
     133           0 :         if (r < 0)
     134           0 :                 return log_error_errno(r, "Failed to open netlink container: %m");
     135             : 
     136           0 :         r = sd_netlink_message_open_container(m, VETH_INFO_PEER);
     137           0 :         if (r < 0)
     138           0 :                 return log_error_errno(r, "Failed to open netlink container: %m");
     139             : 
     140           0 :         r = sd_netlink_message_append_string(m, IFLA_IFNAME, ifname_container);
     141           0 :         if (r < 0)
     142           0 :                 return log_error_errno(r, "Failed to add netlink interface name: %m");
     143             : 
     144           0 :         r = sd_netlink_message_append_ether_addr(m, IFLA_ADDRESS, mac_container);
     145           0 :         if (r < 0)
     146           0 :                 return log_error_errno(r, "Failed to add netlink MAC address: %m");
     147             : 
     148           0 :         r = sd_netlink_message_append_u32(m, IFLA_NET_NS_PID, pid);
     149           0 :         if (r < 0)
     150           0 :                 return log_error_errno(r, "Failed to add netlink namespace field: %m");
     151             : 
     152           0 :         r = sd_netlink_message_close_container(m);
     153           0 :         if (r < 0)
     154           0 :                 return log_error_errno(r, "Failed to close netlink container: %m");
     155             : 
     156           0 :         r = sd_netlink_message_close_container(m);
     157           0 :         if (r < 0)
     158           0 :                 return log_error_errno(r, "Failed to close netlink container: %m");
     159             : 
     160           0 :         r = sd_netlink_message_close_container(m);
     161           0 :         if (r < 0)
     162           0 :                 return log_error_errno(r, "Failed to close netlink container: %m");
     163             : 
     164           0 :         r = sd_netlink_call(rtnl, m, 0, NULL);
     165           0 :         if (r < 0)
     166           0 :                 return log_error_errno(r, "Failed to add new veth interfaces (%s:%s): %m", ifname_host, ifname_container);
     167             : 
     168           0 :         return 0;
     169             : }
     170             : 
     171           0 : int setup_veth(const char *machine_name,
     172             :                pid_t pid,
     173             :                char iface_name[IFNAMSIZ],
     174             :                bool bridge) {
     175             : 
     176           0 :         _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
     177             :         struct ether_addr mac_host, mac_container;
     178             :         int r, i;
     179             : 
     180           0 :         assert(machine_name);
     181           0 :         assert(pid > 0);
     182           0 :         assert(iface_name);
     183             : 
     184             :         /* Use two different interface name prefixes depending whether
     185             :          * we are in bridge mode or not. */
     186           0 :         snprintf(iface_name, IFNAMSIZ - 1, "%s-%s",
     187             :                  bridge ? "vb" : "ve", machine_name);
     188             : 
     189           0 :         r = generate_mac(machine_name, &mac_container, CONTAINER_HASH_KEY, 0);
     190           0 :         if (r < 0)
     191           0 :                 return log_error_errno(r, "Failed to generate predictable MAC address for container side: %m");
     192             : 
     193           0 :         r = generate_mac(machine_name, &mac_host, HOST_HASH_KEY, 0);
     194           0 :         if (r < 0)
     195           0 :                 return log_error_errno(r, "Failed to generate predictable MAC address for host side: %m");
     196             : 
     197           0 :         r = sd_netlink_open(&rtnl);
     198           0 :         if (r < 0)
     199           0 :                 return log_error_errno(r, "Failed to connect to netlink: %m");
     200             : 
     201           0 :         r = add_veth(rtnl, pid, iface_name, &mac_host, "host0", &mac_container);
     202           0 :         if (r < 0)
     203           0 :                 return r;
     204             : 
     205           0 :         r = parse_ifindex_or_ifname(iface_name, &i);
     206           0 :         if (r < 0)
     207           0 :                 return log_error_errno(r, "Failed to resolve interface %s: %m", iface_name);
     208             : 
     209           0 :         return i;
     210             : }
     211             : 
     212           0 : int setup_veth_extra(
     213             :                 const char *machine_name,
     214             :                 pid_t pid,
     215             :                 char **pairs) {
     216             : 
     217           0 :         _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
     218           0 :         uint64_t idx = 0;
     219             :         char **a, **b;
     220             :         int r;
     221             : 
     222           0 :         assert(machine_name);
     223           0 :         assert(pid > 0);
     224             : 
     225           0 :         if (strv_isempty(pairs))
     226           0 :                 return 0;
     227             : 
     228           0 :         r = sd_netlink_open(&rtnl);
     229           0 :         if (r < 0)
     230           0 :                 return log_error_errno(r, "Failed to connect to netlink: %m");
     231             : 
     232           0 :         STRV_FOREACH_PAIR(a, b, pairs) {
     233             :                 struct ether_addr mac_host, mac_container;
     234             : 
     235           0 :                 r = generate_mac(machine_name, &mac_container, VETH_EXTRA_CONTAINER_HASH_KEY, idx);
     236           0 :                 if (r < 0)
     237           0 :                         return log_error_errno(r, "Failed to generate predictable MAC address for container side of extra veth link: %m");
     238             : 
     239           0 :                 r = generate_mac(machine_name, &mac_host, VETH_EXTRA_HOST_HASH_KEY, idx);
     240           0 :                 if (r < 0)
     241           0 :                         return log_error_errno(r, "Failed to generate predictable MAC address for container side of extra veth link: %m");
     242             : 
     243           0 :                 r = add_veth(rtnl, pid, *a, &mac_host, *b, &mac_container);
     244           0 :                 if (r < 0)
     245           0 :                         return r;
     246             : 
     247           0 :                 idx++;
     248             :         }
     249             : 
     250           0 :         return 0;
     251             : }
     252             : 
     253           0 : static int join_bridge(sd_netlink *rtnl, const char *veth_name, const char *bridge_name) {
     254           0 :         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
     255             :         int r, bridge_ifi;
     256             : 
     257           0 :         assert(rtnl);
     258           0 :         assert(veth_name);
     259           0 :         assert(bridge_name);
     260             : 
     261           0 :         r = parse_ifindex_or_ifname(bridge_name, &bridge_ifi);
     262           0 :         if (r < 0)
     263           0 :                 return r;
     264             : 
     265           0 :         r = sd_rtnl_message_new_link(rtnl, &m, RTM_SETLINK, 0);
     266           0 :         if (r < 0)
     267           0 :                 return r;
     268             : 
     269           0 :         r = sd_rtnl_message_link_set_flags(m, IFF_UP, IFF_UP);
     270           0 :         if (r < 0)
     271           0 :                 return r;
     272             : 
     273           0 :         r = sd_netlink_message_append_string(m, IFLA_IFNAME, veth_name);
     274           0 :         if (r < 0)
     275           0 :                 return r;
     276             : 
     277           0 :         r = sd_netlink_message_append_u32(m, IFLA_MASTER, bridge_ifi);
     278           0 :         if (r < 0)
     279           0 :                 return r;
     280             : 
     281           0 :         r = sd_netlink_call(rtnl, m, 0, NULL);
     282           0 :         if (r < 0)
     283           0 :                 return r;
     284             : 
     285           0 :         return bridge_ifi;
     286             : }
     287             : 
     288           0 : static int create_bridge(sd_netlink *rtnl, const char *bridge_name) {
     289           0 :         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
     290             :         int r;
     291             : 
     292           0 :         r = sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0);
     293           0 :         if (r < 0)
     294           0 :                 return r;
     295             : 
     296           0 :         r = sd_netlink_message_append_string(m, IFLA_IFNAME, bridge_name);
     297           0 :         if (r < 0)
     298           0 :                 return r;
     299             : 
     300           0 :         r = sd_netlink_message_open_container(m, IFLA_LINKINFO);
     301           0 :         if (r < 0)
     302           0 :                 return r;
     303             : 
     304           0 :         r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "bridge");
     305           0 :         if (r < 0)
     306           0 :                 return r;
     307             : 
     308           0 :         r = sd_netlink_message_close_container(m);
     309           0 :         if (r < 0)
     310           0 :                 return r;
     311             : 
     312           0 :         r = sd_netlink_message_close_container(m);
     313           0 :         if (r < 0)
     314           0 :                 return r;
     315             : 
     316           0 :         r = sd_netlink_call(rtnl, m, 0, NULL);
     317           0 :         if (r < 0)
     318           0 :                 return r;
     319             : 
     320           0 :         return 0;
     321             : }
     322             : 
     323           0 : int setup_bridge(const char *veth_name, const char *bridge_name, bool create) {
     324           0 :         _cleanup_(release_lock_file) LockFile bridge_lock = LOCK_FILE_INIT;
     325           0 :         _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
     326             :         int r, bridge_ifi;
     327           0 :         unsigned n = 0;
     328             : 
     329           0 :         assert(veth_name);
     330           0 :         assert(bridge_name);
     331             : 
     332           0 :         r = sd_netlink_open(&rtnl);
     333           0 :         if (r < 0)
     334           0 :                 return log_error_errno(r, "Failed to connect to netlink: %m");
     335             : 
     336           0 :         if (create) {
     337             :                 /* We take a system-wide lock here, so that we can safely check whether there's still a member in the
     338             :                  * bridge before removing it, without risking interference from other nspawn instances. */
     339             : 
     340           0 :                 r = make_lock_file("/run/systemd/nspawn-network-zone", LOCK_EX, &bridge_lock);
     341           0 :                 if (r < 0)
     342           0 :                         return log_error_errno(r, "Failed to take network zone lock: %m");
     343             :         }
     344             : 
     345             :         for (;;) {
     346           0 :                 bridge_ifi = join_bridge(rtnl, veth_name, bridge_name);
     347           0 :                 if (bridge_ifi >= 0)
     348           0 :                         return bridge_ifi;
     349           0 :                 if (bridge_ifi != -ENODEV || !create || n > 10)
     350           0 :                         return log_error_errno(bridge_ifi, "Failed to add interface %s to bridge %s: %m", veth_name, bridge_name);
     351             : 
     352             :                 /* Count attempts, so that we don't enter an endless loop here. */
     353           0 :                 n++;
     354             : 
     355             :                 /* The bridge doesn't exist yet. Let's create it */
     356           0 :                 r = create_bridge(rtnl, bridge_name);
     357           0 :                 if (r < 0)
     358           0 :                         return log_error_errno(r, "Failed to create bridge interface %s: %m", bridge_name);
     359             : 
     360             :                 /* Try again, now that the bridge exists */
     361             :         }
     362             : }
     363             : 
     364           4 : int remove_bridge(const char *bridge_name) {
     365           4 :         _cleanup_(release_lock_file) LockFile bridge_lock = LOCK_FILE_INIT;
     366           4 :         _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
     367             :         const char *path;
     368             :         int r;
     369             : 
     370             :         /* Removes the specified bridge, but only if it is currently empty */
     371             : 
     372           4 :         if (isempty(bridge_name))
     373           4 :                 return 0;
     374             : 
     375           0 :         r = make_lock_file("/run/systemd/nspawn-network-zone", LOCK_EX, &bridge_lock);
     376           0 :         if (r < 0)
     377           0 :                 return log_error_errno(r, "Failed to take network zone lock: %m");
     378             : 
     379           0 :         path = strjoina("/sys/class/net/", bridge_name, "/brif");
     380             : 
     381           0 :         r = dir_is_empty(path);
     382           0 :         if (r == -ENOENT) /* Already gone? */
     383           0 :                 return 0;
     384           0 :         if (r < 0)
     385           0 :                 return log_error_errno(r, "Can't detect if bridge %s is empty: %m", bridge_name);
     386           0 :         if (r == 0) /* Still populated, leave it around */
     387           0 :                 return 0;
     388             : 
     389           0 :         r = sd_netlink_open(&rtnl);
     390           0 :         if (r < 0)
     391           0 :                 return log_error_errno(r, "Failed to connect to netlink: %m");
     392             : 
     393           0 :         return remove_one_link(rtnl, bridge_name);
     394             : }
     395             : 
     396           0 : static int parse_interface(const char *name) {
     397           0 :         _cleanup_(sd_device_unrefp) sd_device *d = NULL;
     398             :         char ifi_str[2 + DECIMAL_STR_MAX(int)];
     399             :         int ifi, r;
     400             : 
     401           0 :         r = parse_ifindex_or_ifname(name, &ifi);
     402           0 :         if (r < 0)
     403           0 :                 return log_error_errno(r, "Failed to resolve interface %s: %m", name);
     404             : 
     405           0 :         sprintf(ifi_str, "n%i", ifi);
     406           0 :         r = sd_device_new_from_device_id(&d, ifi_str);
     407           0 :         if (r < 0)
     408           0 :                 return log_error_errno(r, "Failed to get device for interface %s: %m", name);
     409             : 
     410           0 :         r = sd_device_get_is_initialized(d);
     411           0 :         if (r < 0)
     412           0 :                 return log_error_errno(r, "Failed to determine whether interface %s is initialized or not: %m", name);
     413           0 :         if (r == 0) {
     414           0 :                 log_error("Network interface %s is not initialized yet.", name);
     415           0 :                 return -EBUSY;
     416             :         }
     417             : 
     418           0 :         return ifi;
     419             : }
     420             : 
     421           0 : int move_network_interfaces(pid_t pid, char **ifaces) {
     422           0 :         _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
     423             :         char **i;
     424             :         int r;
     425             : 
     426           0 :         if (strv_isempty(ifaces))
     427           0 :                 return 0;
     428             : 
     429           0 :         r = sd_netlink_open(&rtnl);
     430           0 :         if (r < 0)
     431           0 :                 return log_error_errno(r, "Failed to connect to netlink: %m");
     432             : 
     433           0 :         STRV_FOREACH(i, ifaces) {
     434           0 :                 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
     435             :                 int ifi;
     436             : 
     437           0 :                 ifi = parse_interface(*i);
     438           0 :                 if (ifi < 0)
     439           0 :                         return ifi;
     440             : 
     441           0 :                 r = sd_rtnl_message_new_link(rtnl, &m, RTM_SETLINK, ifi);
     442           0 :                 if (r < 0)
     443           0 :                         return log_error_errno(r, "Failed to allocate netlink message: %m");
     444             : 
     445           0 :                 r = sd_netlink_message_append_u32(m, IFLA_NET_NS_PID, pid);
     446           0 :                 if (r < 0)
     447           0 :                         return log_error_errno(r, "Failed to append namespace PID to netlink message: %m");
     448             : 
     449           0 :                 r = sd_netlink_call(rtnl, m, 0, NULL);
     450           0 :                 if (r < 0)
     451           0 :                         return log_error_errno(r, "Failed to move interface %s to namespace: %m", *i);
     452             :         }
     453             : 
     454           0 :         return 0;
     455             : }
     456             : 
     457           0 : int setup_macvlan(const char *machine_name, pid_t pid, char **ifaces) {
     458           0 :         _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
     459           0 :         unsigned idx = 0;
     460             :         char **i;
     461             :         int r;
     462             : 
     463           0 :         if (strv_isempty(ifaces))
     464           0 :                 return 0;
     465             : 
     466           0 :         r = sd_netlink_open(&rtnl);
     467           0 :         if (r < 0)
     468           0 :                 return log_error_errno(r, "Failed to connect to netlink: %m");
     469             : 
     470           0 :         STRV_FOREACH(i, ifaces) {
     471           0 :                 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
     472           0 :                 _cleanup_free_ char *n = NULL;
     473             :                 struct ether_addr mac;
     474             :                 int ifi;
     475             : 
     476           0 :                 ifi = parse_interface(*i);
     477           0 :                 if (ifi < 0)
     478           0 :                         return ifi;
     479             : 
     480           0 :                 r = generate_mac(machine_name, &mac, MACVLAN_HASH_KEY, idx++);
     481           0 :                 if (r < 0)
     482           0 :                         return log_error_errno(r, "Failed to create MACVLAN MAC address: %m");
     483             : 
     484           0 :                 r = sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0);
     485           0 :                 if (r < 0)
     486           0 :                         return log_error_errno(r, "Failed to allocate netlink message: %m");
     487             : 
     488           0 :                 r = sd_netlink_message_append_u32(m, IFLA_LINK, ifi);
     489           0 :                 if (r < 0)
     490           0 :                         return log_error_errno(r, "Failed to add netlink interface index: %m");
     491             : 
     492           0 :                 n = strjoin("mv-", *i);
     493           0 :                 if (!n)
     494           0 :                         return log_oom();
     495             : 
     496           0 :                 strshorten(n, IFNAMSIZ-1);
     497             : 
     498           0 :                 r = sd_netlink_message_append_string(m, IFLA_IFNAME, n);
     499           0 :                 if (r < 0)
     500           0 :                         return log_error_errno(r, "Failed to add netlink interface name: %m");
     501             : 
     502           0 :                 r = sd_netlink_message_append_ether_addr(m, IFLA_ADDRESS, &mac);
     503           0 :                 if (r < 0)
     504           0 :                         return log_error_errno(r, "Failed to add netlink MAC address: %m");
     505             : 
     506           0 :                 r = sd_netlink_message_append_u32(m, IFLA_NET_NS_PID, pid);
     507           0 :                 if (r < 0)
     508           0 :                         return log_error_errno(r, "Failed to add netlink namespace field: %m");
     509             : 
     510           0 :                 r = sd_netlink_message_open_container(m, IFLA_LINKINFO);
     511           0 :                 if (r < 0)
     512           0 :                         return log_error_errno(r, "Failed to open netlink container: %m");
     513             : 
     514           0 :                 r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "macvlan");
     515           0 :                 if (r < 0)
     516           0 :                         return log_error_errno(r, "Failed to open netlink container: %m");
     517             : 
     518           0 :                 r = sd_netlink_message_append_u32(m, IFLA_MACVLAN_MODE, MACVLAN_MODE_BRIDGE);
     519           0 :                 if (r < 0)
     520           0 :                         return log_error_errno(r, "Failed to append macvlan mode: %m");
     521             : 
     522           0 :                 r = sd_netlink_message_close_container(m);
     523           0 :                 if (r < 0)
     524           0 :                         return log_error_errno(r, "Failed to close netlink container: %m");
     525             : 
     526           0 :                 r = sd_netlink_message_close_container(m);
     527           0 :                 if (r < 0)
     528           0 :                         return log_error_errno(r, "Failed to close netlink container: %m");
     529             : 
     530           0 :                 r = sd_netlink_call(rtnl, m, 0, NULL);
     531           0 :                 if (r < 0)
     532           0 :                         return log_error_errno(r, "Failed to add new macvlan interfaces: %m");
     533             :         }
     534             : 
     535           0 :         return 0;
     536             : }
     537             : 
     538           0 : int setup_ipvlan(const char *machine_name, pid_t pid, char **ifaces) {
     539           0 :         _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
     540             :         char **i;
     541             :         int r;
     542             : 
     543           0 :         if (strv_isempty(ifaces))
     544           0 :                 return 0;
     545             : 
     546           0 :         r = sd_netlink_open(&rtnl);
     547           0 :         if (r < 0)
     548           0 :                 return log_error_errno(r, "Failed to connect to netlink: %m");
     549             : 
     550           0 :         STRV_FOREACH(i, ifaces) {
     551           0 :                 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
     552           0 :                 _cleanup_free_ char *n = NULL;
     553             :                 int ifi;
     554             : 
     555           0 :                 ifi = parse_interface(*i);
     556           0 :                 if (ifi < 0)
     557           0 :                         return ifi;
     558             : 
     559           0 :                 r = sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0);
     560           0 :                 if (r < 0)
     561           0 :                         return log_error_errno(r, "Failed to allocate netlink message: %m");
     562             : 
     563           0 :                 r = sd_netlink_message_append_u32(m, IFLA_LINK, ifi);
     564           0 :                 if (r < 0)
     565           0 :                         return log_error_errno(r, "Failed to add netlink interface index: %m");
     566             : 
     567           0 :                 n = strjoin("iv-", *i);
     568           0 :                 if (!n)
     569           0 :                         return log_oom();
     570             : 
     571           0 :                 strshorten(n, IFNAMSIZ-1);
     572             : 
     573           0 :                 r = sd_netlink_message_append_string(m, IFLA_IFNAME, n);
     574           0 :                 if (r < 0)
     575           0 :                         return log_error_errno(r, "Failed to add netlink interface name: %m");
     576             : 
     577           0 :                 r = sd_netlink_message_append_u32(m, IFLA_NET_NS_PID, pid);
     578           0 :                 if (r < 0)
     579           0 :                         return log_error_errno(r, "Failed to add netlink namespace field: %m");
     580             : 
     581           0 :                 r = sd_netlink_message_open_container(m, IFLA_LINKINFO);
     582           0 :                 if (r < 0)
     583           0 :                         return log_error_errno(r, "Failed to open netlink container: %m");
     584             : 
     585           0 :                 r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "ipvlan");
     586           0 :                 if (r < 0)
     587           0 :                         return log_error_errno(r, "Failed to open netlink container: %m");
     588             : 
     589           0 :                 r = sd_netlink_message_append_u16(m, IFLA_IPVLAN_MODE, IPVLAN_MODE_L2);
     590           0 :                 if (r < 0)
     591           0 :                         return log_error_errno(r, "Failed to add ipvlan mode: %m");
     592             : 
     593           0 :                 r = sd_netlink_message_close_container(m);
     594           0 :                 if (r < 0)
     595           0 :                         return log_error_errno(r, "Failed to close netlink container: %m");
     596             : 
     597           0 :                 r = sd_netlink_message_close_container(m);
     598           0 :                 if (r < 0)
     599           0 :                         return log_error_errno(r, "Failed to close netlink container: %m");
     600             : 
     601           0 :                 r = sd_netlink_call(rtnl, m, 0, NULL);
     602           0 :                 if (r < 0)
     603           0 :                         return log_error_errno(r, "Failed to add new ipvlan interfaces: %m");
     604             :         }
     605             : 
     606           0 :         return 0;
     607             : }
     608             : 
     609           0 : int veth_extra_parse(char ***l, const char *p) {
     610           0 :         _cleanup_free_ char *a = NULL, *b = NULL;
     611             :         int r;
     612             : 
     613           0 :         r = extract_first_word(&p, &a, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
     614           0 :         if (r < 0)
     615           0 :                 return r;
     616           0 :         if (r == 0 || !ifname_valid(a))
     617           0 :                 return -EINVAL;
     618             : 
     619           0 :         r = extract_first_word(&p, &b, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
     620           0 :         if (r < 0)
     621           0 :                 return r;
     622           0 :         if (r == 0 || !ifname_valid(b)) {
     623           0 :                 free(b);
     624           0 :                 b = strdup(a);
     625           0 :                 if (!b)
     626           0 :                         return -ENOMEM;
     627             :         }
     628             : 
     629           0 :         if (p)
     630           0 :                 return -EINVAL;
     631             : 
     632           0 :         r = strv_push_pair(l, a, b);
     633           0 :         if (r < 0)
     634           0 :                 return -ENOMEM;
     635             : 
     636           0 :         a = b = NULL;
     637           0 :         return 0;
     638             : }
     639             : 
     640           0 : int remove_veth_links(const char *primary, char **pairs) {
     641           0 :         _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
     642             :         char **a, **b;
     643             :         int r;
     644             : 
     645             :         /* In some cases the kernel might pin the veth links between host and container even after the namespace
     646             :          * died. Hence, let's better remove them explicitly too. */
     647             : 
     648           0 :         if (isempty(primary) && strv_isempty(pairs))
     649           0 :                 return 0;
     650             : 
     651           0 :         r = sd_netlink_open(&rtnl);
     652           0 :         if (r < 0)
     653           0 :                 return log_error_errno(r, "Failed to connect to netlink: %m");
     654             : 
     655           0 :         remove_one_link(rtnl, primary);
     656             : 
     657           0 :         STRV_FOREACH_PAIR(a, b, pairs)
     658           0 :                 remove_one_link(rtnl, *a);
     659             : 
     660           0 :         return 0;
     661             : }

Generated by: LCOV version 1.14