chiark / gitweb /
networkd: introduce ipip tunnel
[elogind.git] / src / network / networkd-link.c
index 0a228b29a13d3b31eba9f079ae6265ab9821b8ce..77c505fc784f3f6d3276f9103e994b4de6012b5d 100644 (file)
@@ -160,7 +160,7 @@ void link_drop(Link *link) {
 
         link->state = LINK_STATE_LINGER;
 
-        log_debug_link(link, "dropped");
+        log_debug_link(link, "link removed");
 
         link_unref(link);
 
@@ -420,7 +420,7 @@ static int route_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata)
         }
 
         r = sd_rtnl_message_get_errno(m);
-        if (r < 0 && r != -ENOENT)
+        if (r < 0 && r != -ESRCH)
                 log_struct_link(LOG_WARNING, link,
                                 "MESSAGE=%s: could not drop route: %s",
                                 link->ifname, strerror(-r),
@@ -623,7 +623,7 @@ static int address_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdat
         }
 
         r = sd_rtnl_message_get_errno(m);
-        if (r < 0 && r != -ENOENT)
+        if (r < 0 && r != -EADDRNOTAVAIL)
                 log_struct_link(LOG_WARNING, link,
                                 "MESSAGE=%s: could not drop address: %s",
                                 link->ifname, strerror(-r),
@@ -1226,8 +1226,13 @@ bool link_has_carrier(unsigned flags, uint8_t operstate) {
         return false;
 }
 
+#define FLAG_STRING(string, flag, old, new) \
+        (((old ^ new) & flag) \
+                ? ((old & flag) ? (" -" string) : (" +" string)) \
+                : "")
+
 static int link_update_flags(Link *link, sd_rtnl_message *m) {
-        unsigned flags, flags_added, flags_removed, generic_flags;
+        unsigned flags, unknown_flags_added, unknown_flags_removed, unknown_flags;
         uint8_t operstate;
         bool carrier_gained = false, carrier_lost = false;
         int r;
@@ -1249,70 +1254,46 @@ static int link_update_flags(Link *link, sd_rtnl_message *m) {
         if ((link->flags == flags) && (link->operstate == operstate))
                 return 0;
 
-        flags_added = (link->flags ^ flags) & flags;
-        flags_removed = (link->flags ^ flags) & link->flags;
-        generic_flags = ~(IFF_UP | IFF_LOWER_UP | IFF_DORMANT | IFF_DEBUG |
-                          IFF_MULTICAST | IFF_BROADCAST | IFF_PROMISC |
-                          IFF_NOARP | IFF_MASTER | IFF_SLAVE | IFF_RUNNING);
-
-        if (flags_added & IFF_UP)
-                log_debug_link(link, "link is up");
-        else if (flags_removed & IFF_UP)
-                log_debug_link(link, "link is down");
-
-        if (flags_added & IFF_LOWER_UP)
-                log_debug_link(link, "link is lower up");
-        else if (flags_removed & IFF_LOWER_UP)
-                log_debug_link(link, "link is lower down");
-
-        if (flags_added & IFF_DORMANT)
-                log_debug_link(link, "link is dormant");
-        else if (flags_removed & IFF_DORMANT)
-                log_debug_link(link, "link is not dormant");
-
-        if (flags_added & IFF_DEBUG)
-                log_debug_link(link, "debugging enabled in the kernel");
-        else if (flags_removed & IFF_DEBUG)
-                log_debug_link(link, "debugging disabled in the kernel");
-
-        if (flags_added & IFF_MULTICAST)
-                log_debug_link(link, "multicast enabled");
-        else if (flags_removed & IFF_MULTICAST)
-                log_debug_link(link, "multicast disabled");
-
-        if (flags_added & IFF_BROADCAST)
-                log_debug_link(link, "broadcast enabled");
-        else if (flags_removed & IFF_BROADCAST)
-                log_debug_link(link, "broadcast disabled");
-
-        if (flags_added & IFF_PROMISC)
-                log_debug_link(link, "promiscuous mode enabled");
-        else if (flags_removed & IFF_PROMISC)
-                log_debug_link(link, "promiscuous mode disabled");
-
-        if (flags_added & IFF_NOARP)
-                log_debug_link(link, "ARP protocol disabled");
-        else if (flags_removed & IFF_NOARP)
-                log_debug_link(link, "ARP protocol enabled");
-
-        if (flags_added & IFF_MASTER)
-                log_debug_link(link, "link is master");
-        else if (flags_removed & IFF_MASTER)
-                log_debug_link(link, "link is no longer master");
-
-        if (flags_added & IFF_SLAVE)
-                log_debug_link(link, "link is slave");
-        else if (flags_removed & IFF_SLAVE)
-                log_debug_link(link, "link is no longer slave");
-
-        /* link flags are currently at most 18 bits, let's default to printing 20 */
-        if (flags_added & generic_flags)
-                log_debug_link(link, "unknown link flags gained: %#.5x (ignoring)",
-                               flags_added & generic_flags);
-
-        if (flags_removed & generic_flags)
-                log_debug_link(link, "unknown link flags lost: %#.5x (ignoring)",
-                               flags_removed & generic_flags);
+        if (link->flags != flags) {
+                log_debug_link(link, "flags change:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
+                               FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags),
+                               FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags),
+                               FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags),
+                               FLAG_STRING("UP", IFF_UP, link->flags, flags),
+                               FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags),
+                               FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags),
+                               FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags),
+                               FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags),
+                               FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags),
+                               FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags),
+                               FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags),
+                               FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags),
+                               FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags),
+                               FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags),
+                               FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags),
+                               FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags),
+                               FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags),
+                               FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags),
+                               FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags));
+
+                unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP |
+                                  IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING |
+                                  IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT |
+                                  IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL |
+                                  IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP |
+                                  IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO);
+                unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags);
+                unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags);
+
+                /* link flags are currently at most 18 bits, let's align to printing 20 */
+                if (unknown_flags_added)
+                        log_debug_link(link, "unknown link flags gained: %#.5x (ignoring)",
+                                       unknown_flags_added);
+
+                if (unknown_flags_removed)
+                        log_debug_link(link, "unknown link flags lost: %#.5x (ignoring)",
+                                       unknown_flags_removed);
+        }
 
         carrier_gained = !link_has_carrier(link->flags, link->operstate) &&
                        link_has_carrier(flags, operstate);
@@ -1485,11 +1466,35 @@ static int link_enter_enslave(Link *link) {
 
         link_save(link);
 
-        if (!link->network->bridge && !link->network->bond &&
+        if (!link->network->bridge &&
+            !link->network->bond &&
+            !link->network->tunnel &&
             hashmap_isempty(link->network->vlans) &&
             hashmap_isempty(link->network->macvlans))
                 return link_enslaved(link);
 
+        if (link->network->bond) {
+                log_struct_link(LOG_DEBUG, link,
+                                "MESSAGE=%s: enslaving by '%s'",
+                                link->ifname, link->network->bond->name,
+                                NETDEV(link->network->bond),
+                                NULL);
+
+                r = netdev_enslave(link->network->bond, link, &enslave_handler);
+                if (r < 0) {
+                        log_struct_link(LOG_WARNING, link,
+                                        "MESSAGE=%s: could not enslave by '%s': %s",
+                                        link->ifname, link->network->bond->name, strerror(-r),
+                                        NETDEV(link->network->bond),
+                                        NULL);
+                        link_enter_failed(link);
+                        return r;
+                }
+
+                link_ref(link);
+                link->enslaving ++;
+        }
+
         if (link->network->bridge) {
                 log_struct_link(LOG_DEBUG, link,
                                 "MESSAGE=%s: enslaving by '%s'",
@@ -1512,19 +1517,19 @@ static int link_enter_enslave(Link *link) {
                 link->enslaving ++;
         }
 
-        if (link->network->bond) {
+        if (link->network->tunnel) {
                 log_struct_link(LOG_DEBUG, link,
                                 "MESSAGE=%s: enslaving by '%s'",
-                                link->ifname, link->network->bond->name,
-                                NETDEV(link->network->bond),
+                                link->ifname, link->network->tunnel->name,
+                                NETDEV(link->network->tunnel),
                                 NULL);
 
-                r = netdev_enslave(link->network->bond, link, &enslave_handler);
+                r = netdev_enslave(link->network->tunnel, link, &enslave_handler);
                 if (r < 0) {
                         log_struct_link(LOG_WARNING, link,
                                         "MESSAGE=%s: could not enslave by '%s': %s",
-                                        link->ifname, link->network->bond->name, strerror(-r),
-                                        NETDEV(link->network->bond),
+                                        link->ifname, link->network->tunnel->name, strerror(-r),
+                                        NETDEV(link->network->tunnel),
                                         NULL);
                         link_enter_failed(link);
                         return r;
@@ -1685,13 +1690,129 @@ int link_initialized(Link *link, struct udev_device *device) {
         return 0;
 }
 
+int link_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata) {
+        Manager *m = userdata;
+        Link *link = NULL;
+        uint16_t type;
+        _cleanup_address_free_ Address *address = NULL;
+        char buf[INET6_ADDRSTRLEN];
+        int r, ifindex;
+
+        assert(rtnl);
+        assert(message);
+        assert(m);
+
+        r = sd_rtnl_message_get_type(message, &type);
+        if (r < 0) {
+                log_warning("rtnl: could not get message type");
+                return 0;
+        }
+
+        r = sd_rtnl_message_addr_get_ifindex(message, &ifindex);
+        if (r < 0 || ifindex <= 0) {
+                log_warning("rtnl: received address message without valid ifindix, ignoring");
+                return 0;
+        } else {
+                r = link_get(m, ifindex, &link);
+                if (r < 0 || !link) {
+                        log_warning("rtnl: received address for non-existing link, ignoring");
+                        return 0;
+                }
+        }
+
+        r = address_new_dynamic(&address);
+        if (r < 0)
+                return 0;
+
+        r = sd_rtnl_message_addr_get_family(message, &address->family);
+        if (r < 0 || !IN_SET(address->family, AF_INET, AF_INET6)) {
+                log_warning("rtnl: received address with invalid family, ignoring");
+                return 0;
+        }
+
+        r = sd_rtnl_message_addr_get_prefixlen(message, &address->prefixlen);
+        if (r < 0) {
+                log_warning("rtnl: recevied address with invalid prefixlen, ignoring");
+                return 0;
+        }
+
+        switch (address->family) {
+        case AF_INET:
+                r = sd_rtnl_message_read_in_addr(message, IFA_LOCAL, &address->in_addr.in);
+                if (r < 0) {
+                        log_warning("rtnl: received address without valid address, ignoring");
+                        return 0;
+                }
+
+                break;
+
+        case AF_INET6:
+                r = sd_rtnl_message_read_in6_addr(message, IFA_ADDRESS, &address->in_addr.in6);
+                if (r < 0) {
+                        log_warning("rtnl: received address without valid address, ignoring");
+                        return 0;
+                }
+
+                break;
+
+        default:
+                assert_not_reached("invalid address family");
+        }
+
+        if (!inet_ntop(address->family, &address->in_addr, buf, INET6_ADDRSTRLEN)) {
+                log_warning("could not print address");
+                return 0;
+        }
+
+        switch (type) {
+        case RTM_NEWADDR:
+                log_info("added address: %s/%u to ifindex %d", buf,
+                         address->prefixlen, ifindex);
+                break;
+
+        case RTM_DELADDR:
+                log_info("removed address: %s/%u from ifindex %d", buf,
+                         address->prefixlen, ifindex);
+                break;
+        default:
+                assert_not_reached("Received invalid RTNL message type");
+        }
+
+        return 1;
+}
+
+static int link_get_address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
+        Link *link = userdata;
+        int r;
+
+        assert(rtnl);
+        assert(m);
+        assert(link);
+
+        for (; m; m = sd_rtnl_message_next(m)) {
+                r = sd_rtnl_message_get_errno(m);
+                if (r < 0) {
+                        log_debug_link(link, "getting address failed: %s", strerror(-r));
+                        continue;
+                }
+
+                r = link_rtnl_process_address(rtnl, m, link->manager);
+                if (r < 0)
+                        log_warning_link(link, "could not process address: %s", strerror(-r));
+        }
+
+        return 1;
+}
+
 int link_add(Manager *m, sd_rtnl_message *message, Link **ret) {
         Link *link;
+        _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
         _cleanup_udev_device_unref_ struct udev_device *device = NULL;
         char ifindex_str[2 + DECIMAL_STR_MAX(int)];
         int r;
 
         assert(m);
+        assert(m->rtnl);
         assert(message);
         assert(ret);
 
@@ -1703,6 +1824,14 @@ int link_add(Manager *m, sd_rtnl_message *message, Link **ret) {
 
         log_debug_link(link, "link added");
 
+        r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, link->ifindex, 0);
+        if (r < 0)
+                return r;
+
+        r = sd_rtnl_call_async(m->rtnl, req, link_get_address_handler, link, 0, NULL);
+        if (r < 0)
+                return r;
+
         if (detect_container(NULL) <= 0) {
                 /* not in a container, udev will be around */
                 sprintf(ifindex_str, "n%"PRIu64, link->ifindex);
@@ -1733,6 +1862,12 @@ int link_update(Link *link, sd_rtnl_message *m) {
         assert(link->ifname);
         assert(m);
 
+        if (link->state == LINK_STATE_LINGER) {
+                link_ref(link);
+                log_info_link(link, "link readded");
+                link->state = LINK_STATE_ENSLAVING;
+        }
+
         r = sd_rtnl_message_read_string(m, IFLA_IFNAME, &ifname);
         if (r >= 0 && !streq(ifname, link->ifname)) {
                 log_info_link(link, "renamed to %s", ifname);