Пример #1
0
static void test_container(void) {
        _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
        uint16_t type;
        uint32_t u32_data;
        void *data;
        int r;
        struct ifinfomsg *ifi;

        assert_se(sd_rtnl_message_new_link(NULL, &m, RTM_NEWLINK, 0) >= 0);

        assert_se(sd_rtnl_message_open_container(m, IFLA_LINKINFO) >= 0);
        assert_se(sd_rtnl_message_open_container(m, IFLA_LINKINFO) == -ENOTSUP);
        assert_se(sd_rtnl_message_append_string(m, IFLA_INFO_KIND, "kind") >= 0);
        assert_se(sd_rtnl_message_open_container(m, IFLA_INFO_DATA) >= 0);
        assert_se(sd_rtnl_message_open_container(m, IFLA_INFO_DATA) == -ENOTSUP);
        assert_se(sd_rtnl_message_append_u16(m, IFLA_VLAN_ID, 100) >= 0);
        assert_se(sd_rtnl_message_close_container(m) >= 0);
        assert_se(sd_rtnl_message_append_string(m, IFLA_INFO_KIND, "kind") >= 0);
        assert_se(sd_rtnl_message_close_container(m) >= 0);
        assert_se(sd_rtnl_message_close_container(m) == -EINVAL);

        assert_se(rtnl_message_seal(NULL, m) >= 0);

        assert_se(sd_rtnl_message_read(m, &type, &data) >= 0);
        assert_se(type == IFLA_LINKINFO);
        assert_se(data == NULL);
/*
        assert_se(sd_rtnl_message_read(m, &type, &data) >= 0);
        assert_se(type == IFLA_INFO_KIND);
        assert_se(streq("kind", (char *)data));
        assert_se(sd_rtnl_message_read(m, &type, &data) >= 0);
        assert_se(type == IFLA_INFO_DATA);
        assert_se(data == NULL);
        assert_se(sd_rtnl_message_read(m, &type, &data) >= 0);
        assert_se(type == IFLA_VLAN_ID);
        assert_se(*(uint16_t *)data == 100);
        assert_se(sd_rtnl_message_read(m, &type, &data) == 0);
        assert_se(sd_rtnl_message_exit_container(m) >= 0);
        assert_se(sd_rtnl_message_read(m, &type, &data) >= 0);
        assert_se(type == IFLA_INFO_KIND);
        assert_se(streq("kind", (char *)data));
        assert_se(sd_rtnl_message_read(m, &type, &data) == 0);
        assert_se(sd_rtnl_message_exit_container(m) >= 0);
*/

        ifi = NLMSG_DATA(m->hdr);
        r = rtnl_message_parse(m,
                               &m->rta_offset_tb,
                               &m->rta_tb_size,
                               IFLA_MAX,
                               IFLA_RTA(ifi),
                               IFLA_PAYLOAD(m->hdr));
        if(r < 0)
                return;

        assert_se(sd_rtnl_message_read_u32(m, IFLA_LINKINFO, &u32_data) == 0);

        assert_se(sd_rtnl_message_exit_container(m) == -EINVAL);
}
Пример #2
0
static void test_link_configure(sd_rtnl *rtnl, int ifindex) {
        _cleanup_rtnl_message_unref_ sd_rtnl_message *message = NULL;
        const char *mac = "98:fe:94:3f:c6:18", *name = "test";
        char buffer[ETHER_ADDR_TO_STRING_MAX];
        unsigned int mtu = 1450, mtu_out;
        const char *name_out;
        struct ether_addr mac_out;

        /* we'd really like to test NEWLINK, but let's not mess with the running kernel */
        assert_se(sd_rtnl_message_new_link(rtnl, &message, RTM_GETLINK, ifindex) >= 0);
        assert_se(sd_rtnl_message_append_string(message, IFLA_IFNAME, name) >= 0);
        assert_se(sd_rtnl_message_append_ether_addr(message, IFLA_ADDRESS, ether_aton(mac)) >= 0);
        assert_se(sd_rtnl_message_append_u32(message, IFLA_MTU, mtu) >= 0);

        assert_se(sd_rtnl_call(rtnl, message, 0, NULL) == 1);
        assert_se(sd_rtnl_message_rewind(message) >= 0);

        assert_se(sd_rtnl_message_read_string(message, IFLA_IFNAME, &name_out) >= 0);
        assert_se(streq(name, name_out));

        assert_se(sd_rtnl_message_read_ether_addr(message, IFLA_ADDRESS, &mac_out) >= 0);
        assert_se(streq(mac, ether_addr_to_string(&mac_out, buffer)));

        assert_se(sd_rtnl_message_read_u32(message, IFLA_MTU, &mtu_out) >= 0);
        assert_se(mtu == mtu_out);
}
Пример #3
0
static void test_container(void) {
        _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
        uint16_t u16_data;
        uint32_t u32_data;
        const char *string_data;

        assert_se(sd_rtnl_message_new_link(NULL, &m, RTM_NEWLINK, 0) >= 0);

        assert_se(sd_rtnl_message_open_container(m, IFLA_LINKINFO) >= 0);
        assert_se(sd_rtnl_message_open_container_union(m, IFLA_INFO_DATA, "vlan") >= 0);
        assert_se(sd_rtnl_message_append_u16(m, IFLA_VLAN_ID, 100) >= 0);
        assert_se(sd_rtnl_message_close_container(m) >= 0);
        assert_se(sd_rtnl_message_append_string(m, IFLA_INFO_KIND, "vlan") >= 0);
        assert_se(sd_rtnl_message_close_container(m) >= 0);
        assert_se(sd_rtnl_message_close_container(m) == -EINVAL);

        assert_se(sd_rtnl_message_rewind(m) >= 0);

        assert_se(sd_rtnl_message_enter_container(m, IFLA_LINKINFO) >= 0);
        assert_se(sd_rtnl_message_read_string(m, IFLA_INFO_KIND, &string_data) >= 0);
        assert_se(streq("vlan", string_data));

        assert_se(sd_rtnl_message_enter_container(m, IFLA_INFO_DATA) >= 0);
        assert_se(sd_rtnl_message_read_u16(m, IFLA_VLAN_ID, &u16_data) >= 0);
        assert_se(sd_rtnl_message_exit_container(m) >= 0);

        assert_se(sd_rtnl_message_read_string(m, IFLA_INFO_KIND, &string_data) >= 0);
        assert_se(streq("vlan", string_data));
        assert_se(sd_rtnl_message_exit_container(m) >= 0);

        assert_se(sd_rtnl_message_read_u32(m, IFLA_LINKINFO, &u32_data) < 0);

        assert_se(sd_rtnl_message_exit_container(m) == -EINVAL);
}
Пример #4
0
static void test_link_configure(sd_rtnl *rtnl, int ifindex) {
        _cleanup_rtnl_message_unref_ sd_rtnl_message *message;
        uint16_t type;
        const char *mac = "98:fe:94:3f:c6:18", *name = "test";
        unsigned int mtu = 1450;
        void *data;

        /* we'd really like to test NEWLINK, but let's not mess with the running kernel */
        assert_se(sd_rtnl_message_new_link(rtnl, &message, RTM_GETLINK, ifindex) >= 0);
        assert_se(sd_rtnl_message_append_string(message, IFLA_IFNAME, name) >= 0);
        assert_se(sd_rtnl_message_append_ether_addr(message, IFLA_ADDRESS, ether_aton(mac)) >= 0);
        assert_se(sd_rtnl_message_append_u32(message, IFLA_MTU, mtu) >= 0);

        assert_se(sd_rtnl_call(rtnl, message, 0, NULL) == 1);

        assert_se(sd_rtnl_message_read(message, &type, &data) > 0);
        assert_se(type == IFLA_IFNAME);
        assert_se(streq(name, (char *) data));

        assert_se(sd_rtnl_message_read(message, &type, &data) > 0);
        assert_se(type == IFLA_ADDRESS);
        assert_se(streq(mac, ether_ntoa(data)));

        assert_se(sd_rtnl_message_read(message, &type, &data) > 0);
        assert_se(type == IFLA_MTU);
        assert_se(mtu == *(unsigned int *) data);
}
Пример #5
0
int sd_rtnl_message_open_container_union(sd_rtnl_message *m, unsigned short type, const char *key) {
        const NLTypeSystemUnion *type_system_union;
        int r;

        assert_return(m, -EINVAL);
        assert_return(!m->sealed, -EPERM);

        r = type_system_get_type_system_union(m->container_type_system[m->n_containers], &type_system_union, type);
        if (r < 0)
                return r;

        r = type_system_union_get_type_system(type_system_union,
                                              &m->container_type_system[m->n_containers + 1],
                                              key);
        if (r < 0)
                return r;

        r = sd_rtnl_message_append_string(m, type_system_union->match, key);
        if (r < 0)
                return r;

        /* do we evere need non-null size */
        r = add_rtattr(m, type, NULL, 0);
        if (r < 0)
                return r;

        m->container_offsets[m->n_containers ++] = r;

        return 0;
}
int rtnl_set_link_name(sd_rtnl **rtnl, int ifindex, const char *name) {
        _cleanup_rtnl_message_unref_ sd_rtnl_message *message = NULL;
        int r;

        assert(rtnl);
        assert(ifindex > 0);
        assert(name);

        if (!*rtnl) {
                r = sd_rtnl_open(rtnl, 0);
                if (r < 0)
                        return r;
        }

        r = sd_rtnl_message_new_link(*rtnl, &message, RTM_SETLINK, ifindex);
        if (r < 0)
                return r;

        r = sd_rtnl_message_append_string(message, IFLA_IFNAME, name);
        if (r < 0)
                return r;

        r = sd_rtnl_call(*rtnl, message, 0, NULL);
        if (r < 0)
                return r;

        return 0;
}
int address_update(Address *address, Link *link,
                   sd_rtnl_message_handler_t callback) {
        _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
        int r;

        assert(address);
        assert(address->family == AF_INET || address->family == AF_INET6);
        assert(link->ifindex > 0);
        assert(link->manager);
        assert(link->manager->rtnl);

        r = sd_rtnl_message_new_addr_update(link->manager->rtnl, &req,
                                     link->ifindex, address->family);
        if (r < 0)
                return log_error_errno(r, "Could not allocate RTM_NEWADDR message: %m");

        r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen);
        if (r < 0)
                return log_error_errno(r, "Could not set prefixlen: %m");

        r = sd_rtnl_message_addr_set_flags(req, IFA_F_PERMANENT);
        if (r < 0)
                return log_error_errno(r, "Could not set flags: %m");

        r = sd_rtnl_message_addr_set_scope(req, address->scope);
        if (r < 0)
                return log_error_errno(r, "Could not set scope: %m");

        if (address->family == AF_INET)
                r = sd_rtnl_message_append_in_addr(req, IFA_LOCAL, &address->in_addr.in);
        else if (address->family == AF_INET6)
                r = sd_rtnl_message_append_in6_addr(req, IFA_LOCAL, &address->in_addr.in6);
        if (r < 0)
                return log_error_errno(r, "Could not append IFA_LOCAL attribute: %m");

        if (address->family == AF_INET) {
                r = sd_rtnl_message_append_in_addr(req, IFA_BROADCAST, &address->broadcast);
                if (r < 0)
                        return log_error_errno(r, "Could not append IFA_BROADCAST attribute: %m");
        }

        if (address->label) {
                r = sd_rtnl_message_append_string(req, IFA_LABEL, address->label);
                if (r < 0)
                        return log_error_errno(r, "Could not append IFA_LABEL attribute: %m");
        }

        r = sd_rtnl_message_append_cache_info(req, IFA_CACHEINFO, &address->cinfo);
        if (r < 0)
                return log_error_errno(r, "Could not append IFA_CACHEINFO attribute: %m");

        r = sd_rtnl_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
        if (r < 0)
                return log_error_errno(r, "Could not send rtnetlink message: %m");

        link_ref(link);

        return 0;
}
Пример #8
0
static int netdev_fill_dummy_rtnl_message(NetDev *netdev, sd_rtnl_message *m) {
        int r;

        assert(netdev);
        assert(m);

        r = sd_rtnl_message_append_string(m, IFLA_IFNAME, netdev->ifname);
        if (r < 0) {
                log_error_netdev(netdev,
                                 "Could not append IFLA_IFNAME, attribute: %s",
                                 strerror(-r));
                return r;
        }

        if (netdev->mac) {
                r = sd_rtnl_message_append_ether_addr(m, IFLA_ADDRESS, netdev->mac);
                if (r < 0) {
                        log_error_netdev(netdev,
                                         "Colud not append IFLA_ADDRESS attribute: %s",
                                         strerror(-r));
                    return r;
                }
        }

        r = sd_rtnl_message_open_container(m, IFLA_LINKINFO);
        if (r < 0) {
                log_error_netdev(netdev,
                                 "Could not append IFLA_LINKINFO attribute: %s",
                                 strerror(-r));
                return r;
        }

        r = sd_rtnl_message_open_container_union(m, IFLA_INFO_DATA,
                                                 netdev_kind_to_string(netdev->kind));
        if (r < 0) {
                log_error_netdev(netdev,
                                 "Could not append IFLA_INFO_DATA attribute: %s",
                                 strerror(-r));
                return r;
        }

        r = sd_rtnl_message_close_container(m);
        if (r < 0) {
                log_error_netdev(netdev,
                                 "Could not append IFLA_LINKINFO attribute: %s",
                                 strerror(-r));
                return r;
        }

        return r;
}
Пример #9
0
int rtnl_set_link_properties(sd_rtnl *rtnl, int ifindex, const char *alias,
                             const struct ether_addr *mac, unsigned mtu) {
        _cleanup_rtnl_message_unref_ sd_rtnl_message *message = NULL;
        bool need_update = false;
        int r;

        assert(rtnl);
        assert(ifindex > 0);

        if (!alias && !mac && mtu == 0)
                return 0;

        r = sd_rtnl_message_new_link(rtnl, &message, RTM_SETLINK, ifindex);
        if (r < 0)
                return r;

        if (alias) {
                r = sd_rtnl_message_append_string(message, IFLA_IFALIAS, alias);
                if (r < 0)
                        return r;

                need_update = true;

        }

        if (mac) {
                r = sd_rtnl_message_append_ether_addr(message, IFLA_ADDRESS, mac);
                if (r < 0)
                        return r;

                need_update = true;
        }

        if (mtu > 0) {
                r = sd_rtnl_message_append_u32(message, IFLA_MTU, mtu);
                if (r < 0)
                        return r;

                need_update = true;
        }

        if  (need_update) {
                r = sd_rtnl_call(rtnl, message, 0, NULL);
                if (r < 0)
                        return r;
        }

        return 0;
}
Пример #10
0
static int netdev_veth_fill_message_create(NetDev *netdev, Link *link, sd_rtnl_message *m) {
        Veth *v = VETH(netdev);
        int r;

        assert(netdev);
        assert(!link);
        assert(v);
        assert(m);

        r = sd_rtnl_message_open_container(m, VETH_INFO_PEER);
        if (r < 0) {
                log_error_netdev(netdev,
                                 "Could not append VETH_INFO_PEER attribute: %s",
                                 strerror(-r));
                return r;
        }

        if (v->ifname_peer) {
                r = sd_rtnl_message_append_string(m, IFLA_IFNAME, v->ifname_peer);
                if (r < 0) {
                        log_error("Failed to add netlink interface name: %s", strerror(-r));
                        return r;
                }
        }

        if (v->mac_peer) {
                r = sd_rtnl_message_append_ether_addr(m, IFLA_ADDRESS, v->mac_peer);
                if (r < 0) {
                        log_error_netdev(netdev,
                                         "Could not append IFLA_ADDRESS attribute: %s",
                                         strerror(-r));
                    return r;
                }
        }

        r = sd_rtnl_message_close_container(m);
        if (r < 0) {
                log_error_netdev(netdev,
                                 "Could not append IFLA_INFO_DATA attribute: %s",
                                 strerror(-r));
                return r;
        }

        return r;
}
Пример #11
0
int address_configure(Address *address, Link *link,
                      sd_rtnl_message_handler_t callback) {
    _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
    int r;

    assert(address);
    assert(address->family == AF_INET || address->family == AF_INET6);
    assert(link);
    assert(link->ifindex > 0);
    assert(link->manager);
    assert(link->manager->rtnl);

    r = sd_rtnl_message_new_addr(RTM_NEWADDR, link->ifindex,
                                 address->family, &req);
    if (r < 0) {
        log_error("Could not allocate RTM_NEWADDR message: %s",
                  strerror(-r));
        return r;
    }

    r = sd_rtnl_message_addr_set_prefixlen(req, address->prefixlen);
    if (r < 0) {
        log_error("Could not set prefixlen: %s", strerror(-r));
        return r;
    }

    r = sd_rtnl_message_addr_set_flags(req, IFA_F_PERMANENT);
    if (r < 0) {
        log_error("Could not set flags: %s", strerror(-r));
        return r;
    }

    r = sd_rtnl_message_addr_set_scope(req, RT_SCOPE_UNIVERSE);
    if (r < 0) {
        log_error("Could not set scope: %s", strerror(-r));
        return r;
    }

    if (address->family == AF_INET)
        r = sd_rtnl_message_append_in_addr(req, IFA_LOCAL, &address->in_addr.in);
    else if (address->family == AF_INET6)
        r = sd_rtnl_message_append_in6_addr(req, IFA_LOCAL, &address->in_addr.in6);
    if (r < 0) {
        log_error("Could not append IFA_LOCAL attribute: %s",
                  strerror(-r));
        return r;
    }

    if (address->family == AF_INET) {
        r = sd_rtnl_message_append_in_addr(req, IFA_BROADCAST, &address->broadcast);
        if (r < 0) {
            log_error("Could not append IFA_BROADCAST attribute: %s",
                      strerror(-r));
            return r;
        }
    }

    if (address->label) {
        r = sd_rtnl_message_append_string(req, IFA_LABEL, address->label);
        if (r < 0) {
            log_error("Could not append IFA_LABEL attribute: %s",
                      strerror(-r));
            return r;
        }
    }

    r = sd_rtnl_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
    if (r < 0) {
        log_error("Could not send rtnetlink message: %s", strerror(-r));
        return r;
    }

    return 0;
}
Пример #12
0
int netdev_create_vlan(NetDev *netdev, Link *link, sd_rtnl_message_handler_t callback) {
        _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
        const char *kind;
        int r;

        assert(netdev);
        assert(netdev->kind == NETDEV_KIND_VLAN);
        assert(link);
        assert(callback);
        assert(netdev->ifname);
        assert(netdev->manager);
        assert(netdev->manager->rtnl);

        r = sd_rtnl_message_new_link(netdev->manager->rtnl, &req, RTM_NEWLINK, 0);
        if (r < 0) {
                log_error_netdev(netdev,
                                 "Could not allocate RTM_NEWLINK message: %s",
                                 strerror(-r));
                return r;
        }

        if (link) {
                r = sd_rtnl_message_append_u32(req, IFLA_LINK, link->ifindex);
                if (r < 0) {
                        log_error_netdev(netdev,
                                         "Could not append IFLA_LINK attribute: %s",
                                         strerror(-r));
                        return r;
                }
        }

        r = sd_rtnl_message_append_string(req, IFLA_IFNAME, netdev->ifname);
        if (r < 0) {
                log_error_netdev(netdev,
                                 "Could not append IFLA_IFNAME attribute: %s",
                                 strerror(-r));
                return r;
        }

        if (netdev->mtu) {
                r = sd_rtnl_message_append_u32(req, IFLA_MTU, netdev->mtu);
                if (r < 0) {
                        log_error_netdev(netdev,
                                         "Could not append IFLA_MTU attribute: %s",
                                         strerror(-r));
                        return r;
                }
        }

        if (netdev->mac) {
                r = sd_rtnl_message_append_ether_addr(req, IFLA_ADDRESS, netdev->mac);
                if (r < 0) {
                        log_error_netdev(netdev,
                                         "Colud not append IFLA_ADDRESS attribute: %s",
                                         strerror(-r));
                    return r;
                }
        }

        r = sd_rtnl_message_open_container(req, IFLA_LINKINFO);
        if (r < 0) {
                log_error_netdev(netdev,
                                 "Could not open IFLA_LINKINFO container: %s",
                                 strerror(-r));
                return r;
        }

        kind = netdev_kind_to_string(netdev->kind);
        if (!kind) {
                log_error_netdev(netdev, "Invalid kind");
                return -EINVAL;
        }

        r = sd_rtnl_message_open_container_union(req, IFLA_INFO_DATA, kind);
        if (r < 0) {
                log_error_netdev(netdev,
                                 "Could not open IFLA_INFO_DATA container: %s",
                                  strerror(-r));
                return r;
        }

        if (netdev->vlanid <= VLANID_MAX) {
                r = sd_rtnl_message_append_u16(req, IFLA_VLAN_ID, netdev->vlanid);
                if (r < 0) {
                        log_error_netdev(netdev,
                                         "Could not append IFLA_VLAN_ID attribute: %s",
                                         strerror(-r));
                        return r;
                }
        }

        r = sd_rtnl_message_close_container(req);
        if (r < 0) {
                log_error_netdev(netdev,
                                 "Could not close IFLA_INFO_DATA container %s",
                                 strerror(-r));
                return r;
        }

        r = sd_rtnl_message_close_container(req);
        if (r < 0) {
                log_error_netdev(netdev,
                                 "Could not close IFLA_LINKINFO container %s",
                                 strerror(-r));
                return r;
        }

        r = sd_rtnl_call_async(netdev->manager->rtnl, req, callback, link, 0, NULL);
        if (r < 0) {
                log_error_netdev(netdev,
                                 "Could not send rtnetlink message: %s", strerror(-r));
                return r;
        }

        link_ref(link);

        log_debug_netdev(netdev, "creating netdev");

        netdev->state = NETDEV_STATE_CREATING;

        return 0;
}
Пример #13
0
static int netdev_fill_vxlan_rtnl_message(NetDev *netdev, Link *link, sd_rtnl_message *m) {
        int r;

        assert(link);
        assert(link->network);
        assert(m);

        r = sd_rtnl_message_append_string(m, IFLA_IFNAME, netdev->ifname);
        if (r < 0) {
                log_error_netdev(netdev,
                                 "Could not append IFLA_IFNAME, attribute: %s",
                                 strerror(-r));
                return r;
        }

        r = sd_rtnl_message_open_container(m, IFLA_LINKINFO);
        if (r < 0) {
                log_error_netdev(netdev,
                                 "Could not append IFLA_LINKINFO attribute: %s",
                                 strerror(-r));
                return r;
        }

        r = sd_rtnl_message_open_container_union(m, IFLA_INFO_DATA,
                                                 netdev_kind_to_string(netdev->kind));
        if (r < 0) {
                log_error_netdev(netdev,
                                 "Could not append IFLA_INFO_DATA attribute: %s",
                                 strerror(-r));
                return r;
        }

        if (netdev->vlanid <= VXLAN_VID_MAX) {
                r = sd_rtnl_message_append_u32(m, IFLA_VXLAN_ID, netdev->vxlanid);
                if (r < 0) {
                        log_error_netdev(netdev,
                                         "Could not append IFLA_VXLAN_ID attribute: %s",
                                         strerror(-r));
                        return r;
                }
        }

        r = sd_rtnl_message_append_in_addr(m, IFLA_VXLAN_GROUP, &netdev->group.in);
        if (r < 0) {
                log_error_netdev(netdev,
                                 "Could not append IFLA_VXLAN_GROUP attribute: %s",
                                 strerror(-r));
                return r;
        }

        r = sd_rtnl_message_append_u32(m, IFLA_VXLAN_LINK, link->ifindex);
        if (r < 0) {
                log_error_netdev(netdev,
                                 "Could not append IFLA_VXLAN_LINK attribute: %s",
                                 strerror(-r));
                return r;
        }

        if(netdev->ttl) {
                r = sd_rtnl_message_append_u8(m, IFLA_VXLAN_TTL, netdev->ttl);
                if (r < 0) {
                        log_error_netdev(netdev,
                                         "Could not append IFLA_VXLAN_TTL attribute: %s",
                                         strerror(-r));
                        return r;
                }
        }

        if(netdev->tos) {
                r = sd_rtnl_message_append_u8(m, IFLA_VXLAN_TOS, netdev->tos);
                if (r < 0) {
                        log_error_netdev(netdev,
                                         "Could not append IFLA_VXLAN_TOS attribute: %s",
                                         strerror(-r));
                        return r;
                }
        }

        r = sd_rtnl_message_append_u8(m, IFLA_VXLAN_LEARNING, netdev->learning);
        if (r < 0) {
                log_error_netdev(netdev,
                                 "Could not append IFLA_VXLAN_LEARNING attribute: %s",
                                 strerror(-r));
                return r;
        }

        r = sd_rtnl_message_close_container(m);
        if (r < 0) {
                log_error_netdev(netdev,
                                 "Could not append IFLA_LINKINFO attribute: %s",
                                 strerror(-r));
                return r;
        }

        return r;
}
static int test_pppoe_server(sd_event *e) {
        sd_rtnl *rtnl;
        sd_rtnl_message *m;
        pid_t pid;
        int r, client_ifindex, server_ifindex;

        r = unshare(CLONE_NEWNET);
        if (r < 0 && errno == EPERM)
                return EXIT_TEST_SKIP;

        assert_se(r >= 0);

        assert_se(sd_rtnl_open(&rtnl, 0) >= 0);
        assert_se(sd_rtnl_attach_event(rtnl, e, 0) >= 0);

        assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0) >= 0);
        assert_se(sd_rtnl_message_append_string(m, IFLA_IFNAME, "pppoe-server") >= 0);
        assert_se(sd_rtnl_message_open_container(m, IFLA_LINKINFO) >= 0);
        assert_se(sd_rtnl_message_open_container_union(m, IFLA_INFO_DATA, "veth") >= 0);
        assert_se(sd_rtnl_message_open_container(m, VETH_INFO_PEER) >= 0);
        assert_se(sd_rtnl_message_append_string(m, IFLA_IFNAME, "pppoe-client") >= 0);
        assert_se(sd_rtnl_message_close_container(m) >= 0);
        assert_se(sd_rtnl_message_close_container(m) >= 0);
        assert_se(sd_rtnl_message_close_container(m) >= 0);
        assert_se(sd_rtnl_call(rtnl, m, 0, NULL) >= 0);

        client_ifindex = (int) if_nametoindex("pppoe-client");
        assert_se(client_ifindex > 0);
        server_ifindex = (int) if_nametoindex("pppoe-server");
        assert_se(server_ifindex > 0);

        m = sd_rtnl_message_unref(m);
        assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_SETLINK, client_ifindex) >= 0);
        assert_se(sd_rtnl_message_link_set_flags(m, IFF_UP, IFF_UP) >= 0);
        assert_se(sd_rtnl_call(rtnl, m, 0, NULL) >= 0);

        m = sd_rtnl_message_unref(m);
        assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_SETLINK, server_ifindex) >= 0);
        assert_se(sd_rtnl_message_link_set_flags(m, IFF_UP, IFF_UP) >= 0);
        assert_se(sd_rtnl_call(rtnl, m, 0, NULL) >= 0);

        pid = fork();
        assert_se(pid >= 0);
        if (pid == 0) {
                /* let the client send some discover messages before the server is started */
                sleep(2);

                /* TODO: manage pppoe-server-options */
                execlp("pppoe-server", "pppoe-server", "-F",
                       "-I", "pppoe-server",
                       "-C", "Test-AC",
                       "-S", "Service-Default",
                       "-S", "Service-First-Auxiliary",
                       "-S", "Service-Second-Auxiliary",
                       NULL);
                assert_not_reached("failed to execute pppoe-server. not installed?");
        }

        client_run("pppoe-client", e);

        assert_se(kill(pid, SIGTERM) >= 0);
        assert_se(wait_for_terminate(pid, NULL) >= 0);

        assert_se(!sd_rtnl_message_unref(m));
        assert_se(!sd_rtnl_unref(rtnl));

        return EXIT_SUCCESS;
}
Пример #15
0
static int netdev_fill_vti_rtnl_message(Link *link, sd_rtnl_message *m) {
        NetDev *netdev;
        int r;

        assert(link);
        assert(link->network);
        assert(link->network->tunnel);
        assert(m);

        netdev = link->network->tunnel;

        assert(netdev->family == AF_INET);

        r = sd_rtnl_message_append_string(m, IFLA_IFNAME, netdev->ifname);
        if (r < 0) {
                log_error_netdev(netdev,
                                 "Could not append IFLA_IFNAME, attribute: %s",
                                 strerror(-r));
                return r;
        }

        if(netdev->mtu) {
                r = sd_rtnl_message_append_u32(m, IFLA_MTU, netdev->mtu);
                if (r < 0) {
                        log_error_netdev(netdev,
                                         "Could not append IFLA_MTU attribute: %s",
                                         strerror(-r));
                        return r;
                }
        }

        if (netdev->mac) {
                r = sd_rtnl_message_append_ether_addr(m, IFLA_ADDRESS, netdev->mac);
                if (r < 0) {
                        log_error_netdev(netdev,
                                         "Colud not append IFLA_ADDRESS attribute: %s",
                                         strerror(-r));
                    return r;
                }
        }

        r = sd_rtnl_message_open_container(m, IFLA_LINKINFO);
        if (r < 0) {
                log_error_netdev(netdev,
                                 "Could not append IFLA_LINKINFO attribute: %s",
                                 strerror(-r));
                return r;
        }

        r = sd_rtnl_message_open_container_union(m, IFLA_INFO_DATA,
                                                 netdev_kind_to_string(netdev->kind));
        if (r < 0) {
                log_error_netdev(netdev,
                                 "Could not append IFLA_INFO_DATA attribute: %s",
                                 strerror(-r));
                return r;
        }

        r = sd_rtnl_message_append_u32(m, IFLA_VTI_LINK, link->ifindex);
        if (r < 0) {
                log_error_netdev(netdev,
                                 "Could not append IFLA_IPTUN_LINK attribute: %s",
                                 strerror(-r));
                return r;
        }

        r = sd_rtnl_message_append_in_addr(m, IFLA_VTI_LOCAL, &netdev->local.in);
        if (r < 0) {
                log_error_netdev(netdev,
                                 "Could not append IFLA_IPTUN_LOCAL attribute: %s",
                                 strerror(-r));
                return r;
        }

        r = sd_rtnl_message_append_in_addr(m, IFLA_VTI_REMOTE, &netdev->remote.in);
        if (r < 0) {
                log_error_netdev(netdev,
                                 "Could not append IFLA_IPTUN_REMOTE attribute: %s",
                                 strerror(-r));
                return r;
        }

        r = sd_rtnl_message_close_container(m);
        if (r < 0) {
                log_error_netdev(netdev,
                                 "Could not append IFLA_INFO_DATA attribute: %s",
                                 strerror(-r));
                return r;
        }

        r = sd_rtnl_message_close_container(m);
        if (r < 0) {
                log_error_netdev(netdev,
                                 "Could not append IFLA_LINKINFO attribute: %s",
                                 strerror(-r));
                return r;
        }

        return r;
}
Пример #16
0
static int test_tunnel_configure(sd_rtnl *rtnl) {
        int r;
        sd_rtnl_message *m, *n;
        struct in_addr local, remote;

        /* skip test if module cannot be loaded */
        r = load_module("ipip");
        if(r < 0)
                return EXIT_TEST_SKIP;

        if(getuid() != 0)
                return EXIT_TEST_SKIP;

        /* IPIP tunnel */
        assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0) >= 0);
        assert_se(m);

        assert_se(sd_rtnl_message_append_string(m, IFLA_IFNAME, "ipip-tunnel") >= 0);
        assert_se(sd_rtnl_message_append_u32(m, IFLA_MTU, 1234)>= 0);

        assert_se(sd_rtnl_message_open_container(m, IFLA_LINKINFO) >= 0);

        assert_se(sd_rtnl_message_open_container_union(m, IFLA_INFO_DATA, "ipip") >= 0);

        inet_pton(AF_INET, "192.168.21.1", &local.s_addr);
        assert_se(sd_rtnl_message_append_u32(m, IFLA_IPTUN_LOCAL, local.s_addr) >= 0);

        inet_pton(AF_INET, "192.168.21.2", &remote.s_addr);
        assert_se(sd_rtnl_message_append_u32(m, IFLA_IPTUN_REMOTE, remote.s_addr) >= 0);

        assert_se(sd_rtnl_message_close_container(m) >= 0);
        assert_se(sd_rtnl_message_close_container(m) >= 0);

        assert_se(sd_rtnl_call(rtnl, m, -1, 0) == 1);

        assert_se((m = sd_rtnl_message_unref(m)) == NULL);

        r = load_module("sit");
        if(r < 0)
                return EXIT_TEST_SKIP;

        /* sit */
        assert_se(sd_rtnl_message_new_link(rtnl, &n, RTM_NEWLINK, 0) >= 0);
        assert_se(n);

        assert_se(sd_rtnl_message_append_string(n, IFLA_IFNAME, "sit-tunnel") >= 0);
        assert_se(sd_rtnl_message_append_u32(n, IFLA_MTU, 1234)>= 0);

        assert_se(sd_rtnl_message_open_container(n, IFLA_LINKINFO) >= 0);

        assert_se(sd_rtnl_message_open_container_union(n, IFLA_INFO_DATA, "sit") >= 0);

        assert_se(sd_rtnl_message_append_u8(n, IFLA_IPTUN_PROTO, IPPROTO_IPIP) >= 0);

        inet_pton(AF_INET, "192.168.21.3", &local.s_addr);
        assert_se(sd_rtnl_message_append_u32(n, IFLA_IPTUN_LOCAL, local.s_addr) >= 0);

        inet_pton(AF_INET, "192.168.21.4", &remote.s_addr);
        assert_se(sd_rtnl_message_append_u32(n, IFLA_IPTUN_REMOTE, remote.s_addr) >= 0);

        assert_se(sd_rtnl_message_close_container(n) >= 0);
        assert_se(sd_rtnl_message_close_container(n) >= 0);

        assert_se(sd_rtnl_call(rtnl, n, -1, 0) == 1);

        assert_se((m = sd_rtnl_message_unref(n)) == NULL);

        return EXIT_SUCCESS;
}