LCOV - code coverage report
Current view: top level - network/netdev - netdev.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 18 433 4.2 %
Date: 2019-08-22 15:41:25 Functions: 5 23 21.7 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include <net/if.h>
       4             : #include <netinet/in.h>
       5             : 
       6             : #include "alloc-util.h"
       7             : #include "conf-files.h"
       8             : #include "conf-parser.h"
       9             : #include "fd-util.h"
      10             : #include "list.h"
      11             : #include "netdev/bond.h"
      12             : #include "netdev/bridge.h"
      13             : #include "netdev/dummy.h"
      14             : #include "netdev/fou-tunnel.h"
      15             : #include "netdev/geneve.h"
      16             : #include "netdev/ipvlan.h"
      17             : #include "netdev/l2tp-tunnel.h"
      18             : #include "netdev/macsec.h"
      19             : #include "netdev/macvlan.h"
      20             : #include "netdev/netdev.h"
      21             : #include "netdev/netdevsim.h"
      22             : #include "netdev/nlmon.h"
      23             : #include "netdev/tunnel.h"
      24             : #include "netdev/tuntap.h"
      25             : #include "netdev/vcan.h"
      26             : #include "netdev/veth.h"
      27             : #include "netdev/vlan.h"
      28             : #include "netdev/vrf.h"
      29             : #include "netdev/vxcan.h"
      30             : #include "netdev/vxlan.h"
      31             : #include "netdev/wireguard.h"
      32             : #include "netdev/xfrm.h"
      33             : #include "netlink-util.h"
      34             : #include "network-internal.h"
      35             : #include "networkd-link.h"
      36             : #include "networkd-manager.h"
      37             : #include "siphash24.h"
      38             : #include "stat-util.h"
      39             : #include "string-table.h"
      40             : #include "string-util.h"
      41             : #include "strv.h"
      42             : 
      43             : const NetDevVTable * const netdev_vtable[_NETDEV_KIND_MAX] = {
      44             :         [NETDEV_KIND_BRIDGE] = &bridge_vtable,
      45             :         [NETDEV_KIND_BOND] = &bond_vtable,
      46             :         [NETDEV_KIND_VLAN] = &vlan_vtable,
      47             :         [NETDEV_KIND_MACVLAN] = &macvlan_vtable,
      48             :         [NETDEV_KIND_MACVTAP] = &macvtap_vtable,
      49             :         [NETDEV_KIND_IPVLAN] = &ipvlan_vtable,
      50             :         [NETDEV_KIND_IPVTAP] = &ipvtap_vtable,
      51             :         [NETDEV_KIND_VXLAN] = &vxlan_vtable,
      52             :         [NETDEV_KIND_IPIP] = &ipip_vtable,
      53             :         [NETDEV_KIND_GRE] = &gre_vtable,
      54             :         [NETDEV_KIND_GRETAP] = &gretap_vtable,
      55             :         [NETDEV_KIND_IP6GRE] = &ip6gre_vtable,
      56             :         [NETDEV_KIND_IP6GRETAP] = &ip6gretap_vtable,
      57             :         [NETDEV_KIND_SIT] = &sit_vtable,
      58             :         [NETDEV_KIND_VTI] = &vti_vtable,
      59             :         [NETDEV_KIND_VTI6] = &vti6_vtable,
      60             :         [NETDEV_KIND_VETH] = &veth_vtable,
      61             :         [NETDEV_KIND_DUMMY] = &dummy_vtable,
      62             :         [NETDEV_KIND_TUN] = &tun_vtable,
      63             :         [NETDEV_KIND_TAP] = &tap_vtable,
      64             :         [NETDEV_KIND_IP6TNL] = &ip6tnl_vtable,
      65             :         [NETDEV_KIND_VRF] = &vrf_vtable,
      66             :         [NETDEV_KIND_VCAN] = &vcan_vtable,
      67             :         [NETDEV_KIND_GENEVE] = &geneve_vtable,
      68             :         [NETDEV_KIND_VXCAN] = &vxcan_vtable,
      69             :         [NETDEV_KIND_WIREGUARD] = &wireguard_vtable,
      70             :         [NETDEV_KIND_NETDEVSIM] = &netdevsim_vtable,
      71             :         [NETDEV_KIND_FOU] = &foutnl_vtable,
      72             :         [NETDEV_KIND_ERSPAN] = &erspan_vtable,
      73             :         [NETDEV_KIND_L2TP] = &l2tptnl_vtable,
      74             :         [NETDEV_KIND_MACSEC] = &macsec_vtable,
      75             :         [NETDEV_KIND_NLMON] = &nlmon_vtable,
      76             :         [NETDEV_KIND_XFRM] = &xfrm_vtable,
      77             : };
      78             : 
      79             : static const char* const netdev_kind_table[_NETDEV_KIND_MAX] = {
      80             :         [NETDEV_KIND_BRIDGE] = "bridge",
      81             :         [NETDEV_KIND_BOND] = "bond",
      82             :         [NETDEV_KIND_VLAN] = "vlan",
      83             :         [NETDEV_KIND_MACVLAN] = "macvlan",
      84             :         [NETDEV_KIND_MACVTAP] = "macvtap",
      85             :         [NETDEV_KIND_IPVLAN] = "ipvlan",
      86             :         [NETDEV_KIND_IPVTAP] = "ipvtap",
      87             :         [NETDEV_KIND_VXLAN] = "vxlan",
      88             :         [NETDEV_KIND_IPIP] = "ipip",
      89             :         [NETDEV_KIND_GRE] = "gre",
      90             :         [NETDEV_KIND_GRETAP] = "gretap",
      91             :         [NETDEV_KIND_IP6GRE] = "ip6gre",
      92             :         [NETDEV_KIND_IP6GRETAP] = "ip6gretap",
      93             :         [NETDEV_KIND_SIT] = "sit",
      94             :         [NETDEV_KIND_VETH] = "veth",
      95             :         [NETDEV_KIND_VTI] = "vti",
      96             :         [NETDEV_KIND_VTI6] = "vti6",
      97             :         [NETDEV_KIND_DUMMY] = "dummy",
      98             :         [NETDEV_KIND_TUN] = "tun",
      99             :         [NETDEV_KIND_TAP] = "tap",
     100             :         [NETDEV_KIND_IP6TNL] = "ip6tnl",
     101             :         [NETDEV_KIND_VRF] = "vrf",
     102             :         [NETDEV_KIND_VCAN] = "vcan",
     103             :         [NETDEV_KIND_GENEVE] = "geneve",
     104             :         [NETDEV_KIND_VXCAN] = "vxcan",
     105             :         [NETDEV_KIND_WIREGUARD] = "wireguard",
     106             :         [NETDEV_KIND_NETDEVSIM] = "netdevsim",
     107             :         [NETDEV_KIND_FOU] = "fou",
     108             :         [NETDEV_KIND_ERSPAN] = "erspan",
     109             :         [NETDEV_KIND_L2TP] = "l2tp",
     110             :         [NETDEV_KIND_MACSEC] = "macsec",
     111             :         [NETDEV_KIND_NLMON] = "nlmon",
     112             :         [NETDEV_KIND_XFRM] = "xfrm",
     113             : };
     114             : 
     115          70 : DEFINE_STRING_TABLE_LOOKUP(netdev_kind, NetDevKind);
     116             : 
     117           0 : int config_parse_netdev_kind(
     118             :                 const char *unit,
     119             :                 const char *filename,
     120             :                 unsigned line,
     121             :                 const char *section,
     122             :                 unsigned section_line,
     123             :                 const char *lvalue,
     124             :                 int ltype,
     125             :                 const char *rvalue,
     126             :                 void *data,
     127             :                 void *userdata) {
     128             : 
     129           0 :         NetDevKind k, *kind = data;
     130             : 
     131           0 :         assert(rvalue);
     132           0 :         assert(data);
     133             : 
     134           0 :         k = netdev_kind_from_string(rvalue);
     135           0 :         if (k < 0) {
     136           0 :                 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse netdev kind, ignoring assignment: %s", rvalue);
     137           0 :                 return 0;
     138             :         }
     139             : 
     140           0 :         if (*kind != _NETDEV_KIND_INVALID && *kind != k) {
     141           0 :                 log_syntax(unit, LOG_ERR, filename, line, 0,
     142             :                            "Specified netdev kind is different from the previous value '%s', ignoring assignment: %s",
     143             :                            netdev_kind_to_string(*kind), rvalue);
     144           0 :                 return 0;
     145             :         }
     146             : 
     147           0 :         *kind = k;
     148             : 
     149           0 :         return 0;
     150             : }
     151             : 
     152           0 : static void netdev_callbacks_clear(NetDev *netdev) {
     153             :         netdev_join_callback *callback;
     154             : 
     155           0 :         if (!netdev)
     156           0 :                 return;
     157             : 
     158           0 :         while ((callback = netdev->callbacks)) {
     159           0 :                 LIST_REMOVE(callbacks, netdev->callbacks, callback);
     160           0 :                 link_unref(callback->link);
     161           0 :                 free(callback);
     162             :         }
     163             : }
     164             : 
     165           0 : bool netdev_is_managed(NetDev *netdev) {
     166           0 :         if (!netdev || !netdev->manager || !netdev->ifname)
     167           0 :                 return false;
     168             : 
     169           0 :         return hashmap_get(netdev->manager->netdevs, netdev->ifname) == netdev;
     170             : }
     171             : 
     172           0 : static void netdev_detach_from_manager(NetDev *netdev) {
     173           0 :         if (netdev->ifname && netdev->manager)
     174           0 :                 hashmap_remove(netdev->manager->netdevs, netdev->ifname);
     175           0 : }
     176             : 
     177           0 : static NetDev *netdev_free(NetDev *netdev) {
     178           0 :         assert(netdev);
     179             : 
     180           0 :         netdev_callbacks_clear(netdev);
     181             : 
     182           0 :         netdev_detach_from_manager(netdev);
     183             : 
     184           0 :         free(netdev->filename);
     185             : 
     186           0 :         free(netdev->description);
     187           0 :         free(netdev->ifname);
     188           0 :         free(netdev->mac);
     189           0 :         condition_free_list(netdev->conditions);
     190             : 
     191             :         /* Invoke the per-kind done() destructor, but only if the state field is initialized. We conditionalize that
     192             :          * because we parse .netdev files twice: once to determine the kind (with a short, minimal NetDev structure
     193             :          * allocation, with no room for per-kind fields), and once to read the kind's properties (with a full,
     194             :          * comprehensive NetDev structure allocation with enough space for whatever the specific kind needs). Now, in
     195             :          * the first case we shouldn't try to destruct the per-kind NetDev fields on destruction, in the second case we
     196             :          * should. We use the state field to discern the two cases: it's _NETDEV_STATE_INVALID on the first "raw"
     197             :          * call. */
     198           0 :         if (netdev->state != _NETDEV_STATE_INVALID &&
     199           0 :             NETDEV_VTABLE(netdev) &&
     200           0 :             NETDEV_VTABLE(netdev)->done)
     201           0 :                 NETDEV_VTABLE(netdev)->done(netdev);
     202             : 
     203           0 :         return mfree(netdev);
     204             : }
     205             : 
     206          78 : DEFINE_TRIVIAL_REF_UNREF_FUNC(NetDev, netdev, netdev_free);
     207             : 
     208           0 : void netdev_drop(NetDev *netdev) {
     209           0 :         if (!netdev || netdev->state == NETDEV_STATE_LINGER)
     210           0 :                 return;
     211             : 
     212           0 :         netdev->state = NETDEV_STATE_LINGER;
     213             : 
     214           0 :         log_netdev_debug(netdev, "netdev removed");
     215             : 
     216           0 :         netdev_callbacks_clear(netdev);
     217             : 
     218           0 :         netdev_detach_from_manager(netdev);
     219             : 
     220           0 :         netdev_unref(netdev);
     221             : 
     222           0 :         return;
     223             : }
     224             : 
     225           6 : int netdev_get(Manager *manager, const char *name, NetDev **ret) {
     226             :         NetDev *netdev;
     227             : 
     228           6 :         assert(manager);
     229           6 :         assert(name);
     230           6 :         assert(ret);
     231             : 
     232           6 :         netdev = hashmap_get(manager->netdevs, name);
     233           6 :         if (!netdev) {
     234           6 :                 *ret = NULL;
     235           6 :                 return -ENOENT;
     236             :         }
     237             : 
     238           0 :         *ret = netdev;
     239             : 
     240           0 :         return 0;
     241             : }
     242             : 
     243           0 : static int netdev_enter_failed(NetDev *netdev) {
     244           0 :         netdev->state = NETDEV_STATE_FAILED;
     245             : 
     246           0 :         netdev_callbacks_clear(netdev);
     247             : 
     248           0 :         return 0;
     249             : }
     250             : 
     251           0 : static int netdev_enslave_ready(NetDev *netdev, Link* link, link_netlink_message_handler_t callback) {
     252           0 :         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
     253             :         int r;
     254             : 
     255           0 :         assert(netdev);
     256           0 :         assert(netdev->state == NETDEV_STATE_READY);
     257           0 :         assert(netdev->manager);
     258           0 :         assert(netdev->manager->rtnl);
     259           0 :         assert(IN_SET(netdev->kind, NETDEV_KIND_BRIDGE, NETDEV_KIND_BOND, NETDEV_KIND_VRF));
     260           0 :         assert(link);
     261           0 :         assert(callback);
     262             : 
     263           0 :         if (link->flags & IFF_UP && netdev->kind == NETDEV_KIND_BOND) {
     264           0 :                 log_netdev_debug(netdev, "Link '%s' was up when attempting to enslave it. Bringing link down.", link->ifname);
     265           0 :                 r = link_down(link, NULL);
     266           0 :                 if (r < 0)
     267           0 :                         return log_netdev_error_errno(netdev, r, "Could not bring link down: %m");
     268             :         }
     269             : 
     270           0 :         r = sd_rtnl_message_new_link(netdev->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
     271           0 :         if (r < 0)
     272           0 :                 return log_netdev_error_errno(netdev, r, "Could not allocate RTM_SETLINK message: %m");
     273             : 
     274           0 :         r = sd_netlink_message_append_u32(req, IFLA_MASTER, netdev->ifindex);
     275           0 :         if (r < 0)
     276           0 :                 return log_netdev_error_errno(netdev, r, "Could not append IFLA_MASTER attribute: %m");
     277             : 
     278           0 :         r = netlink_call_async(netdev->manager->rtnl, NULL, req, callback,
     279             :                                link_netlink_destroy_callback, link);
     280           0 :         if (r < 0)
     281           0 :                 return log_netdev_error_errno(netdev, r, "Could not send rtnetlink message: %m");
     282             : 
     283           0 :         link_ref(link);
     284             : 
     285           0 :         log_netdev_debug(netdev, "Enslaving link '%s'", link->ifname);
     286             : 
     287           0 :         return 0;
     288             : }
     289             : 
     290           0 : static int netdev_enter_ready(NetDev *netdev) {
     291             :         netdev_join_callback *callback, *callback_next;
     292             :         int r;
     293             : 
     294           0 :         assert(netdev);
     295           0 :         assert(netdev->ifname);
     296             : 
     297           0 :         if (netdev->state != NETDEV_STATE_CREATING)
     298           0 :                 return 0;
     299             : 
     300           0 :         netdev->state = NETDEV_STATE_READY;
     301             : 
     302           0 :         log_netdev_info(netdev, "netdev ready");
     303             : 
     304           0 :         LIST_FOREACH_SAFE(callbacks, callback, callback_next, netdev->callbacks) {
     305             :                 /* enslave the links that were attempted to be enslaved before the
     306             :                  * link was ready */
     307           0 :                 r = netdev_enslave_ready(netdev, callback->link, callback->callback);
     308           0 :                 if (r < 0)
     309           0 :                         return r;
     310             : 
     311           0 :                 LIST_REMOVE(callbacks, netdev->callbacks, callback);
     312           0 :                 link_unref(callback->link);
     313           0 :                 free(callback);
     314             :         }
     315             : 
     316           0 :         if (NETDEV_VTABLE(netdev)->post_create)
     317           0 :                 NETDEV_VTABLE(netdev)->post_create(netdev, NULL, NULL);
     318             : 
     319           0 :         return 0;
     320             : }
     321             : 
     322             : /* callback for netdev's created without a backing Link */
     323           0 : static int netdev_create_handler(sd_netlink *rtnl, sd_netlink_message *m, NetDev *netdev) {
     324             :         int r;
     325             : 
     326           0 :         assert(netdev);
     327           0 :         assert(netdev->state != _NETDEV_STATE_INVALID);
     328             : 
     329           0 :         r = sd_netlink_message_get_errno(m);
     330           0 :         if (r == -EEXIST)
     331           0 :                 log_netdev_info(netdev, "netdev exists, using existing without changing its parameters");
     332           0 :         else if (r < 0) {
     333           0 :                 log_netdev_warning_errno(netdev, r, "netdev could not be created: %m");
     334           0 :                 netdev_drop(netdev);
     335             : 
     336           0 :                 return 1;
     337             :         }
     338             : 
     339           0 :         log_netdev_debug(netdev, "Created");
     340             : 
     341           0 :         return 1;
     342             : }
     343             : 
     344           0 : static int netdev_enslave(NetDev *netdev, Link *link, link_netlink_message_handler_t callback) {
     345             :         int r;
     346             : 
     347           0 :         assert(netdev);
     348           0 :         assert(netdev->manager);
     349           0 :         assert(netdev->manager->rtnl);
     350           0 :         assert(IN_SET(netdev->kind, NETDEV_KIND_BRIDGE, NETDEV_KIND_BOND, NETDEV_KIND_VRF));
     351             : 
     352           0 :         if (netdev->state == NETDEV_STATE_READY) {
     353           0 :                 r = netdev_enslave_ready(netdev, link, callback);
     354           0 :                 if (r < 0)
     355           0 :                         return r;
     356           0 :         } else if (IN_SET(netdev->state, NETDEV_STATE_LINGER, NETDEV_STATE_FAILED)) {
     357           0 :                 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
     358             : 
     359           0 :                 r = rtnl_message_new_synthetic_error(netdev->manager->rtnl, -ENODEV, 0, &m);
     360           0 :                 if (r >= 0)
     361           0 :                         callback(netdev->manager->rtnl, m, link);
     362             :         } else {
     363             :                 /* the netdev is not yet read, save this request for when it is */
     364             :                 netdev_join_callback *cb;
     365             : 
     366           0 :                 cb = new(netdev_join_callback, 1);
     367           0 :                 if (!cb)
     368           0 :                         return log_oom();
     369             : 
     370           0 :                 *cb = (netdev_join_callback) {
     371             :                         .callback = callback,
     372           0 :                         .link = link_ref(link),
     373             :                 };
     374             : 
     375           0 :                 LIST_PREPEND(callbacks, netdev->callbacks, cb);
     376             : 
     377           0 :                 log_netdev_debug(netdev, "Will enslave '%s', when ready", link->ifname);
     378             :         }
     379             : 
     380           0 :         return 0;
     381             : }
     382             : 
     383           0 : int netdev_set_ifindex(NetDev *netdev, sd_netlink_message *message) {
     384             :         uint16_t type;
     385             :         const char *kind;
     386             :         const char *received_kind;
     387             :         const char *received_name;
     388             :         int r, ifindex;
     389             : 
     390           0 :         assert(netdev);
     391           0 :         assert(message);
     392             : 
     393           0 :         r = sd_netlink_message_get_type(message, &type);
     394           0 :         if (r < 0)
     395           0 :                 return log_netdev_error_errno(netdev, r, "Could not get rtnl message type: %m");
     396             : 
     397           0 :         if (type != RTM_NEWLINK) {
     398           0 :                 log_netdev_error(netdev, "Cannot set ifindex from unexpected rtnl message type.");
     399           0 :                 return -EINVAL;
     400             :         }
     401             : 
     402           0 :         r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
     403           0 :         if (r < 0) {
     404           0 :                 log_netdev_error_errno(netdev, r, "Could not get ifindex: %m");
     405           0 :                 netdev_enter_failed(netdev);
     406           0 :                 return r;
     407           0 :         } else if (ifindex <= 0) {
     408           0 :                 log_netdev_error(netdev, "Got invalid ifindex: %d", ifindex);
     409           0 :                 netdev_enter_failed(netdev);
     410           0 :                 return -EINVAL;
     411             :         }
     412             : 
     413           0 :         if (netdev->ifindex > 0) {
     414           0 :                 if (netdev->ifindex != ifindex) {
     415           0 :                         log_netdev_error(netdev, "Could not set ifindex to %d, already set to %d",
     416             :                                          ifindex, netdev->ifindex);
     417           0 :                         netdev_enter_failed(netdev);
     418           0 :                         return -EEXIST;
     419             :                 } else
     420             :                         /* ifindex already set to the same for this netdev */
     421           0 :                         return 0;
     422             :         }
     423             : 
     424           0 :         r = sd_netlink_message_read_string(message, IFLA_IFNAME, &received_name);
     425           0 :         if (r < 0)
     426           0 :                 return log_netdev_error_errno(netdev, r, "Could not get IFNAME: %m");
     427             : 
     428           0 :         if (!streq(netdev->ifname, received_name)) {
     429           0 :                 log_netdev_error(netdev, "Received newlink with wrong IFNAME %s", received_name);
     430           0 :                 netdev_enter_failed(netdev);
     431           0 :                 return r;
     432             :         }
     433             : 
     434           0 :         r = sd_netlink_message_enter_container(message, IFLA_LINKINFO);
     435           0 :         if (r < 0)
     436           0 :                 return log_netdev_error_errno(netdev, r, "Could not get LINKINFO: %m");
     437             : 
     438           0 :         r = sd_netlink_message_read_string(message, IFLA_INFO_KIND, &received_kind);
     439           0 :         if (r < 0)
     440           0 :                 return log_netdev_error_errno(netdev, r, "Could not get KIND: %m");
     441             : 
     442           0 :         r = sd_netlink_message_exit_container(message);
     443           0 :         if (r < 0)
     444           0 :                 return log_netdev_error_errno(netdev, r, "Could not exit container: %m");
     445             : 
     446           0 :         if (netdev->kind == NETDEV_KIND_TAP)
     447             :                 /* the kernel does not distinguish between tun and tap */
     448           0 :                 kind = "tun";
     449             :         else {
     450           0 :                 kind = netdev_kind_to_string(netdev->kind);
     451           0 :                 if (!kind) {
     452           0 :                         log_netdev_error(netdev, "Could not get kind");
     453           0 :                         netdev_enter_failed(netdev);
     454           0 :                         return -EINVAL;
     455             :                 }
     456             :         }
     457             : 
     458           0 :         if (!streq(kind, received_kind)) {
     459           0 :                 log_netdev_error(netdev,
     460             :                                  "Received newlink with wrong KIND %s, "
     461             :                                  "expected %s", received_kind, kind);
     462           0 :                 netdev_enter_failed(netdev);
     463           0 :                 return r;
     464             :         }
     465             : 
     466           0 :         netdev->ifindex = ifindex;
     467             : 
     468           0 :         log_netdev_debug(netdev, "netdev has index %d", netdev->ifindex);
     469             : 
     470           0 :         netdev_enter_ready(netdev);
     471             : 
     472           0 :         return 0;
     473             : }
     474             : 
     475             : #define HASH_KEY SD_ID128_MAKE(52,e1,45,bd,00,6f,29,96,21,c6,30,6d,83,71,04,48)
     476             : 
     477           0 : int netdev_get_mac(const char *ifname, struct ether_addr **ret) {
     478           0 :         _cleanup_free_ struct ether_addr *mac = NULL;
     479             :         uint64_t result;
     480             :         size_t l, sz;
     481             :         uint8_t *v;
     482             :         int r;
     483             : 
     484           0 :         assert(ifname);
     485           0 :         assert(ret);
     486             : 
     487           0 :         mac = new0(struct ether_addr, 1);
     488           0 :         if (!mac)
     489           0 :                 return -ENOMEM;
     490             : 
     491           0 :         l = strlen(ifname);
     492           0 :         sz = sizeof(sd_id128_t) + l;
     493           0 :         v = newa(uint8_t, sz);
     494             : 
     495             :         /* fetch some persistent data unique to the machine */
     496           0 :         r = sd_id128_get_machine((sd_id128_t*) v);
     497           0 :         if (r < 0)
     498           0 :                 return r;
     499             : 
     500             :         /* combine with some data unique (on this machine) to this
     501             :          * netdev */
     502           0 :         memcpy(v + sizeof(sd_id128_t), ifname, l);
     503             : 
     504             :         /* Let's hash the host machine ID plus the container name. We
     505             :          * use a fixed, but originally randomly created hash key here. */
     506           0 :         result = siphash24(v, sz, HASH_KEY.bytes);
     507             : 
     508             :         assert_cc(ETH_ALEN <= sizeof(result));
     509           0 :         memcpy(mac->ether_addr_octet, &result, ETH_ALEN);
     510             : 
     511             :         /* see eth_random_addr in the kernel */
     512           0 :         mac->ether_addr_octet[0] &= 0xfe;        /* clear multicast bit */
     513           0 :         mac->ether_addr_octet[0] |= 0x02;        /* set local assignment bit (IEEE802) */
     514             : 
     515           0 :         *ret = TAKE_PTR(mac);
     516             : 
     517           0 :         return 0;
     518             : }
     519             : 
     520           0 : static int netdev_create(NetDev *netdev, Link *link, link_netlink_message_handler_t callback) {
     521             :         int r;
     522             : 
     523           0 :         assert(netdev);
     524           0 :         assert(!link || callback);
     525             : 
     526             :         /* create netdev */
     527           0 :         if (NETDEV_VTABLE(netdev)->create) {
     528           0 :                 assert(!link);
     529             : 
     530           0 :                 r = NETDEV_VTABLE(netdev)->create(netdev);
     531           0 :                 if (r < 0)
     532           0 :                         return r;
     533             : 
     534           0 :                 log_netdev_debug(netdev, "Created");
     535             :         } else {
     536           0 :                 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
     537             : 
     538           0 :                 r = sd_rtnl_message_new_link(netdev->manager->rtnl, &m, RTM_NEWLINK, 0);
     539           0 :                 if (r < 0)
     540           0 :                         return log_netdev_error_errno(netdev, r, "Could not allocate RTM_NEWLINK message: %m");
     541             : 
     542           0 :                 r = sd_netlink_message_append_string(m, IFLA_IFNAME, netdev->ifname);
     543           0 :                 if (r < 0)
     544           0 :                         return log_netdev_error_errno(netdev, r, "Could not append IFLA_IFNAME, attribute: %m");
     545             : 
     546           0 :                 if (netdev->mac) {
     547           0 :                         r = sd_netlink_message_append_ether_addr(m, IFLA_ADDRESS, netdev->mac);
     548           0 :                         if (r < 0)
     549           0 :                                 return log_netdev_error_errno(netdev, r, "Could not append IFLA_ADDRESS attribute: %m");
     550             :                 }
     551             : 
     552           0 :                 if (netdev->mtu) {
     553           0 :                         r = sd_netlink_message_append_u32(m, IFLA_MTU, netdev->mtu);
     554           0 :                         if (r < 0)
     555           0 :                                 return log_netdev_error_errno(netdev, r, "Could not append IFLA_MTU attribute: %m");
     556             :                 }
     557             : 
     558           0 :                 if (link) {
     559           0 :                         r = sd_netlink_message_append_u32(m, IFLA_LINK, link->ifindex);
     560           0 :                         if (r < 0)
     561           0 :                                 return log_netdev_error_errno(netdev, r, "Could not append IFLA_LINK attribute: %m");
     562             :                 }
     563             : 
     564           0 :                 r = sd_netlink_message_open_container(m, IFLA_LINKINFO);
     565           0 :                 if (r < 0)
     566           0 :                         return log_netdev_error_errno(netdev, r, "Could not append IFLA_LINKINFO attribute: %m");
     567             : 
     568           0 :                 r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, netdev_kind_to_string(netdev->kind));
     569           0 :                 if (r < 0)
     570           0 :                         return log_netdev_error_errno(netdev, r, "Could not append IFLA_INFO_DATA attribute: %m");
     571             : 
     572           0 :                 if (NETDEV_VTABLE(netdev)->fill_message_create) {
     573           0 :                         r = NETDEV_VTABLE(netdev)->fill_message_create(netdev, link, m);
     574           0 :                         if (r < 0)
     575           0 :                                 return r;
     576             :                 }
     577             : 
     578           0 :                 r = sd_netlink_message_close_container(m);
     579           0 :                 if (r < 0)
     580           0 :                         return log_netdev_error_errno(netdev, r, "Could not append IFLA_INFO_DATA attribute: %m");
     581             : 
     582           0 :                 r = sd_netlink_message_close_container(m);
     583           0 :                 if (r < 0)
     584           0 :                         return log_netdev_error_errno(netdev, r, "Could not append IFLA_LINKINFO attribute: %m");
     585             : 
     586           0 :                 if (link) {
     587           0 :                         r = netlink_call_async(netdev->manager->rtnl, NULL, m, callback,
     588             :                                                link_netlink_destroy_callback, link);
     589           0 :                         if (r < 0)
     590           0 :                                 return log_netdev_error_errno(netdev, r, "Could not send rtnetlink message: %m");
     591             : 
     592           0 :                         link_ref(link);
     593             :                 } else {
     594           0 :                         r = netlink_call_async(netdev->manager->rtnl, NULL, m, netdev_create_handler,
     595             :                                                netdev_destroy_callback, netdev);
     596           0 :                         if (r < 0)
     597           0 :                                 return log_netdev_error_errno(netdev, r, "Could not send rtnetlink message: %m");
     598             : 
     599           0 :                         netdev_ref(netdev);
     600             :                 }
     601             : 
     602           0 :                 netdev->state = NETDEV_STATE_CREATING;
     603             : 
     604           0 :                 log_netdev_debug(netdev, "Creating");
     605             :         }
     606             : 
     607           0 :         return 0;
     608             : }
     609             : 
     610           0 : static int netdev_create_after_configured(NetDev *netdev, Link *link) {
     611           0 :         assert(netdev);
     612           0 :         assert(link);
     613           0 :         assert(NETDEV_VTABLE(netdev)->create_after_configured);
     614             : 
     615           0 :         return NETDEV_VTABLE(netdev)->create_after_configured(netdev, link);
     616             : }
     617             : 
     618             : /* the callback must be called, possibly after a timeout, as otherwise the Link will hang */
     619           0 : int netdev_join(NetDev *netdev, Link *link, link_netlink_message_handler_t callback) {
     620             :         int r;
     621             : 
     622           0 :         assert(netdev);
     623           0 :         assert(netdev->manager);
     624           0 :         assert(netdev->manager->rtnl);
     625             : 
     626           0 :         switch (netdev_get_create_type(netdev)) {
     627           0 :         case NETDEV_CREATE_MASTER:
     628           0 :                 r = netdev_enslave(netdev, link, callback);
     629           0 :                 if (r < 0)
     630           0 :                         return r;
     631             : 
     632           0 :                 break;
     633           0 :         case NETDEV_CREATE_STACKED:
     634           0 :                 r = netdev_create(netdev, link, callback);
     635           0 :                 if (r < 0)
     636           0 :                         return r;
     637             : 
     638           0 :                 break;
     639           0 :         case NETDEV_CREATE_AFTER_CONFIGURED:
     640           0 :                 r = netdev_create_after_configured(netdev, link);
     641           0 :                 if (r < 0)
     642           0 :                         return r;
     643           0 :                 break;
     644           0 :         default:
     645           0 :                 assert_not_reached("Can not join independent netdev");
     646             :         }
     647             : 
     648           0 :         return 0;
     649             : }
     650             : 
     651           0 : int netdev_load_one(Manager *manager, const char *filename) {
     652           0 :         _cleanup_(netdev_unrefp) NetDev *netdev_raw = NULL, *netdev = NULL;
     653           0 :         _cleanup_fclose_ FILE *file = NULL;
     654             :         const char *dropin_dirname;
     655           0 :         bool independent = false;
     656             :         int r;
     657             : 
     658           0 :         assert(manager);
     659           0 :         assert(filename);
     660             : 
     661           0 :         file = fopen(filename, "re");
     662           0 :         if (!file) {
     663           0 :                 if (errno == ENOENT)
     664           0 :                         return 0;
     665             : 
     666           0 :                 return -errno;
     667             :         }
     668             : 
     669           0 :         if (null_or_empty_fd(fileno(file))) {
     670           0 :                 log_debug("Skipping empty file: %s", filename);
     671           0 :                 return 0;
     672             :         }
     673             : 
     674           0 :         netdev_raw = new(NetDev, 1);
     675           0 :         if (!netdev_raw)
     676           0 :                 return log_oom();
     677             : 
     678           0 :         *netdev_raw = (NetDev) {
     679             :                 .n_ref = 1,
     680             :                 .kind = _NETDEV_KIND_INVALID,
     681             :                 .state = _NETDEV_STATE_INVALID, /* an invalid state means done() of the implementation won't be called on destruction */
     682             :         };
     683             : 
     684           0 :         dropin_dirname = strjoina(basename(filename), ".d");
     685           0 :         r = config_parse_many(filename, NETWORK_DIRS, dropin_dirname,
     686             :                               "Match\0NetDev\0",
     687             :                               config_item_perf_lookup, network_netdev_gperf_lookup,
     688             :                               CONFIG_PARSE_WARN|CONFIG_PARSE_RELAXED, netdev_raw);
     689           0 :         if (r < 0)
     690           0 :                 return r;
     691             : 
     692             :         /* skip out early if configuration does not match the environment */
     693           0 :         if (!condition_test_list(netdev_raw->conditions, NULL, NULL, NULL)) {
     694           0 :                 log_debug("%s: Conditions in the file do not match the system environment, skipping.", filename);
     695           0 :                 return 0;
     696             :         }
     697             : 
     698           0 :         if (netdev_raw->kind == _NETDEV_KIND_INVALID) {
     699           0 :                 log_warning("NetDev has no Kind= configured in %s. Ignoring", filename);
     700           0 :                 return 0;
     701             :         }
     702             : 
     703           0 :         if (!netdev_raw->ifname) {
     704           0 :                 log_warning("NetDev without Name= configured in %s. Ignoring", filename);
     705           0 :                 return 0;
     706             :         }
     707             : 
     708           0 :         r = fseek(file, 0, SEEK_SET);
     709           0 :         if (r < 0)
     710           0 :                 return -errno;
     711             : 
     712           0 :         netdev = malloc0(NETDEV_VTABLE(netdev_raw)->object_size);
     713           0 :         if (!netdev)
     714           0 :                 return log_oom();
     715             : 
     716           0 :         netdev->n_ref = 1;
     717           0 :         netdev->manager = manager;
     718           0 :         netdev->kind = netdev_raw->kind;
     719           0 :         netdev->state = NETDEV_STATE_LOADING; /* we initialize the state here for the first time,
     720             :                                                  so that done() will be called on destruction */
     721             : 
     722           0 :         if (NETDEV_VTABLE(netdev)->init)
     723           0 :                 NETDEV_VTABLE(netdev)->init(netdev);
     724             : 
     725           0 :         r = config_parse_many(filename, NETWORK_DIRS, dropin_dirname,
     726           0 :                               NETDEV_VTABLE(netdev)->sections,
     727             :                               config_item_perf_lookup, network_netdev_gperf_lookup,
     728             :                               CONFIG_PARSE_WARN, netdev);
     729           0 :         if (r < 0)
     730           0 :                 return r;
     731             : 
     732             :         /* verify configuration */
     733           0 :         if (NETDEV_VTABLE(netdev)->config_verify) {
     734           0 :                 r = NETDEV_VTABLE(netdev)->config_verify(netdev, filename);
     735           0 :                 if (r < 0)
     736           0 :                         return 0;
     737             :         }
     738             : 
     739           0 :         netdev->filename = strdup(filename);
     740           0 :         if (!netdev->filename)
     741           0 :                 return log_oom();
     742             : 
     743           0 :         if (!netdev->mac && NETDEV_VTABLE(netdev)->generate_mac) {
     744           0 :                 r = netdev_get_mac(netdev->ifname, &netdev->mac);
     745           0 :                 if (r < 0)
     746           0 :                         return log_netdev_error_errno(netdev, r,
     747             :                                                       "Failed to generate predictable MAC address for %s: %m",
     748             :                                                       netdev->ifname);
     749             :         }
     750             : 
     751           0 :         r = hashmap_ensure_allocated(&netdev->manager->netdevs, &string_hash_ops);
     752           0 :         if (r < 0)
     753           0 :                 return r;
     754             : 
     755           0 :         r = hashmap_put(netdev->manager->netdevs, netdev->ifname, netdev);
     756           0 :         if (r == -EEXIST) {
     757           0 :                 NetDev *n = hashmap_get(netdev->manager->netdevs, netdev->ifname);
     758             : 
     759           0 :                 assert(n);
     760           0 :                 log_netdev_warning_errno(netdev, r,
     761             :                                          "The setting Name=%s in %s conflicts with the one in %s, ignoring",
     762             :                                          netdev->ifname, netdev->filename, n->filename);
     763             : 
     764             :                 /* Clear ifname before netdev_free() is called. Otherwise, the NetDev object 'n' is
     765             :                  * removed from the hashmap 'manager->netdevs'. */
     766           0 :                 netdev->ifname = mfree(netdev->ifname);
     767           0 :                 return 0;
     768             :         }
     769           0 :         if (r < 0)
     770           0 :                 return r;
     771             : 
     772           0 :         LIST_HEAD_INIT(netdev->callbacks);
     773             : 
     774           0 :         log_netdev_debug(netdev, "loaded %s", netdev_kind_to_string(netdev->kind));
     775             : 
     776           0 :         if (IN_SET(netdev_get_create_type(netdev), NETDEV_CREATE_MASTER, NETDEV_CREATE_INDEPENDENT)) {
     777           0 :                 r = netdev_create(netdev, NULL, NULL);
     778           0 :                 if (r < 0)
     779           0 :                         return r;
     780             :         }
     781             : 
     782           0 :         switch (netdev->kind) {
     783           0 :         case NETDEV_KIND_IPIP:
     784           0 :                 independent = IPIP(netdev)->independent;
     785           0 :                 break;
     786           0 :         case NETDEV_KIND_GRE:
     787           0 :                 independent = GRE(netdev)->independent;
     788           0 :                 break;
     789           0 :         case NETDEV_KIND_GRETAP:
     790           0 :                 independent = GRETAP(netdev)->independent;
     791           0 :                 break;
     792           0 :         case NETDEV_KIND_IP6GRE:
     793           0 :                 independent = IP6GRE(netdev)->independent;
     794           0 :                 break;
     795           0 :         case NETDEV_KIND_IP6GRETAP:
     796           0 :                 independent = IP6GRETAP(netdev)->independent;
     797           0 :                 break;
     798           0 :         case NETDEV_KIND_SIT:
     799           0 :                 independent = SIT(netdev)->independent;
     800           0 :                 break;
     801           0 :         case NETDEV_KIND_VTI:
     802           0 :                 independent = VTI(netdev)->independent;
     803           0 :                 break;
     804           0 :         case NETDEV_KIND_VTI6:
     805           0 :                 independent = VTI6(netdev)->independent;
     806           0 :                 break;
     807           0 :         case NETDEV_KIND_IP6TNL:
     808           0 :                 independent = IP6TNL(netdev)->independent;
     809           0 :                 break;
     810           0 :         case NETDEV_KIND_ERSPAN:
     811           0 :                 independent = ERSPAN(netdev)->independent;
     812           0 :                 break;
     813           0 :         case NETDEV_KIND_XFRM:
     814           0 :                 independent = XFRM(netdev)->independent;
     815           0 :                 break;
     816           0 :         default:
     817           0 :                 break;
     818             :         }
     819             : 
     820           0 :         if (independent) {
     821           0 :                 r = netdev_create(netdev, NULL, NULL);
     822           0 :                 if (r < 0)
     823           0 :                         return r;
     824             :         }
     825             : 
     826           0 :         netdev = NULL;
     827             : 
     828           0 :         return 0;
     829             : }
     830             : 
     831           1 : int netdev_load(Manager *manager) {
     832           1 :         _cleanup_strv_free_ char **files = NULL;
     833             :         char **f;
     834             :         int r;
     835             : 
     836           1 :         assert(manager);
     837             : 
     838           1 :         hashmap_clear_with_destructor(manager->netdevs, netdev_unref);
     839             : 
     840           1 :         r = conf_files_list_strv(&files, ".netdev", NULL, 0, NETWORK_DIRS);
     841           1 :         if (r < 0)
     842           0 :                 return log_error_errno(r, "Failed to enumerate netdev files: %m");
     843             : 
     844           1 :         STRV_FOREACH(f, files) {
     845           0 :                 r = netdev_load_one(manager, *f);
     846           0 :                 if (r < 0)
     847           0 :                         return r;
     848             :         }
     849             : 
     850           1 :         return 0;
     851             : }

Generated by: LCOV version 1.14