示例#1
0
static int test_acd(const char *ifname, const char *address) {
        _cleanup_(sd_event_unrefp) sd_event *e = NULL;
        _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL, *reply = NULL;
        union in_addr_union pa;
        struct ether_addr ha;
        int ifindex;

        assert_se(in_addr_from_string(AF_INET, address, &pa) >= 0);

        assert_se(sd_event_new(&e) >= 0);

        assert_se(sd_netlink_open(&rtnl) >= 0);
        assert_se(sd_netlink_attach_event(rtnl, e, 0) >= 0);

        assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, 0) >= 0);
        assert_se(sd_netlink_message_append_string(m, IFLA_IFNAME, ifname) >= 0);
        assert_se(sd_netlink_call(rtnl, m, 0, &reply) >= 0);

        assert_se(sd_rtnl_message_link_get_ifindex(reply, &ifindex) >= 0);
        assert_se(sd_netlink_message_read_ether_addr(reply, IFLA_ADDRESS, &ha) >= 0);

        client_run(ifindex, &pa.in, &ha, e);

        return EXIT_SUCCESS;
}
示例#2
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);
}
示例#3
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);
}
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;
}
示例#5
0
int main(void) {
        sd_rtnl *rtnl;
        sd_rtnl_message *m;
        sd_rtnl_message *r;
        char *string_data;
        int if_loopback;
        uint16_t type;

        test_match();

        test_multiple();

        test_route();

        test_container();

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

        if_loopback = (int) if_nametoindex("lo");
        assert_se(if_loopback > 0);

        test_async(if_loopback);

        test_pipe(if_loopback);

        test_event_loop(if_loopback);

        test_link_configure(rtnl, if_loopback);

        test_get_addresses(rtnl);

        assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, if_loopback) >= 0);
        assert_se(m);

        assert_se(sd_rtnl_message_get_type(m, &type) >= 0);
        assert_se(type == RTM_GETLINK);

        assert_se(sd_rtnl_message_read_string(m, IFLA_IFNAME, &string_data) == -EPERM);

        assert_se(sd_rtnl_call(rtnl, m, 0, &r) == 1);
        assert_se(sd_rtnl_message_get_type(r, &type) >= 0);
        assert_se(type == RTM_NEWLINK);

        assert_se((r = sd_rtnl_message_unref(r)) == NULL);

        assert_se(sd_rtnl_call(rtnl, m, -1, &r) == -EPERM);
        assert_se((m = sd_rtnl_message_unref(m)) == NULL);
        assert_se((r = sd_rtnl_message_unref(r)) == NULL);

        test_link_get(rtnl, if_loopback);
        test_address_get(rtnl, if_loopback);

        assert_se(sd_rtnl_flush(rtnl) >= 0);
        assert_se((m = sd_rtnl_message_unref(m)) == NULL);
        assert_se((r = sd_rtnl_message_unref(r)) == NULL);
        assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);

        return EXIT_SUCCESS;
}
示例#6
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);
}
示例#7
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);
}
示例#8
0
int br_vlan_configure(Link *link, uint16_t pvid, uint32_t *br_vid_bitmap, uint32_t *br_untagged_bitmap) {
        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
        int r;
        uint16_t flags;
        sd_netlink *rtnl;

        assert(link);
        assert(link->manager);
        assert(br_vid_bitmap);
        assert(br_untagged_bitmap);
        assert(link->network);

        /* pvid might not be in br_vid_bitmap yet */
        if (pvid)
                set_bit(pvid, br_vid_bitmap);

        rtnl = link->manager->rtnl;

        /* create new RTM message */
        r = sd_rtnl_message_new_link(rtnl, &req, RTM_SETLINK, link->ifindex);
        if (r < 0)
                return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");

        r = sd_rtnl_message_link_set_family(req, PF_BRIDGE);
        if (r < 0)
                return log_link_error_errno(link, r, "Could not set message family: %m");

        r = sd_netlink_message_open_container(req, IFLA_AF_SPEC);
        if (r < 0)
                return log_link_error_errno(link, r, "Could not open IFLA_AF_SPEC container: %m");

        /* master needs flag self */
        if (!link->network->bridge) {
                flags = BRIDGE_FLAGS_SELF;
                sd_netlink_message_append_data(req, IFLA_BRIDGE_FLAGS, &flags, sizeof(uint16_t));
        }

        /* add vlan info */
        r = append_vlan_info_data(link, req, pvid, br_vid_bitmap, br_untagged_bitmap);
        if (r < 0)
                return log_link_error_errno(link, r, "Could not append VLANs: %m");

        r = sd_netlink_message_close_container(req);
        if (r < 0)
                return log_link_error_errno(link, r, "Could not close IFLA_AF_SPEC container: %m");

        /* send message to the kernel */
        r = netlink_call_async(rtnl, NULL, req, set_brvlan_handler,
                               link_netlink_destroy_callback, link);
        if (r < 0)
                return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");

        link_ref(link);

        return 0;
}
示例#9
0
static void test_pipe(int ifindex) {
        _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
        _cleanup_rtnl_message_unref_ sd_rtnl_message *m1 = NULL, *m2 = NULL;
        int counter = 0;

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

        assert_se(sd_rtnl_message_new_link(rtnl, &m1, RTM_GETLINK, ifindex) >= 0);
        assert_se(sd_rtnl_message_new_link(rtnl, &m2, RTM_GETLINK, ifindex) >= 0);

        counter ++;
        assert_se(sd_rtnl_call_async(rtnl, m1, &pipe_handler, &counter, 0, NULL) >= 0);

        counter ++;
        assert_se(sd_rtnl_call_async(rtnl, m2, &pipe_handler, &counter, 0, NULL) >= 0);

        while (counter > 0) {
                assert_se(sd_rtnl_wait(rtnl, 0) >= 0);
                assert_se(sd_rtnl_process(rtnl, NULL) >= 0);
        }

        assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);
}
示例#10
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;
}
示例#11
0
static void test_message_link_bridge(sd_rtnl *rtnl) {
        _cleanup_rtnl_message_unref_ sd_rtnl_message *message = NULL;
        uint32_t cost;

        assert_se(sd_rtnl_message_new_link(rtnl, &message, RTM_NEWLINK, 1) >= 0);
        assert_se(sd_rtnl_message_link_set_family(message, PF_BRIDGE) >= 0);
        assert_se(sd_rtnl_message_open_container(message, IFLA_PROTINFO) >= 0);
        assert_se(sd_rtnl_message_append_u32(message, IFLA_BRPORT_COST, 10) >= 0);
        assert_se(sd_rtnl_message_close_container(message) >= 0);

        assert_se(sd_rtnl_message_rewind(message) >= 0);

        assert_se(sd_rtnl_message_enter_container(message, IFLA_PROTINFO) >= 0);
        assert_se(sd_rtnl_message_read_u32(message, IFLA_BRPORT_COST, &cost) >= 0);
        assert_se(cost == 10);
        assert_se(sd_rtnl_message_exit_container(message) >= 0);
}
示例#12
0
static int start_loopback(sd_netlink *rtnl) {
        _cleanup_netlink_message_unref_ sd_netlink_message *req = NULL;
        int r;

        r = sd_rtnl_message_new_link(rtnl, &req, RTM_SETLINK, LOOPBACK_IFINDEX);
        if (r < 0)
                return r;

        r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP);
        if (r < 0)
                return r;

        r = sd_netlink_call(rtnl, req, 0, NULL);
        if (r < 0)
                return r;

        return 0;
}
示例#13
0
static void test_link_get(sd_rtnl *rtnl, int ifindex) {
        sd_rtnl_message *m;
        sd_rtnl_message *r;
        unsigned int mtu = 1500;
        const char *str_data;
        uint8_t u8_data;
        uint32_t u32_data;
        struct ether_addr eth_data;

        assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);
        assert_se(m);

        /* u8 test cases  */
        assert_se(sd_rtnl_message_append_u8(m, IFLA_CARRIER, 0) >= 0);
        assert_se(sd_rtnl_message_append_u8(m, IFLA_OPERSTATE, 0) >= 0);
        assert_se(sd_rtnl_message_append_u8(m, IFLA_LINKMODE, 0) >= 0);

        /* u32 test cases */
        assert_se(sd_rtnl_message_append_u32(m, IFLA_MTU, mtu) >= 0);
        assert_se(sd_rtnl_message_append_u32(m, IFLA_GROUP, 0) >= 0);
        assert_se(sd_rtnl_message_append_u32(m, IFLA_TXQLEN, 0) >= 0);
        assert_se(sd_rtnl_message_append_u32(m, IFLA_NUM_TX_QUEUES, 0) >= 0);
        assert_se(sd_rtnl_message_append_u32(m, IFLA_NUM_RX_QUEUES, 0) >= 0);

        assert_se(sd_rtnl_call(rtnl, m, -1, &r) == 1);

        assert_se(sd_rtnl_message_read_string(r, IFLA_IFNAME, &str_data) == 0);

        assert_se(sd_rtnl_message_read_u8(r, IFLA_CARRIER, &u8_data) == 0);
        assert_se(sd_rtnl_message_read_u8(r, IFLA_OPERSTATE, &u8_data) == 0);
        assert_se(sd_rtnl_message_read_u8(r, IFLA_LINKMODE, &u8_data) == 0);

        assert_se(sd_rtnl_message_read_u32(r, IFLA_MTU, &u32_data) == 0);
        assert_se(sd_rtnl_message_read_u32(r, IFLA_GROUP, &u32_data) == 0);
        assert_se(sd_rtnl_message_read_u32(r, IFLA_TXQLEN, &u32_data) == 0);
        assert_se(sd_rtnl_message_read_u32(r, IFLA_NUM_TX_QUEUES, &u32_data) == 0);
        assert_se(sd_rtnl_message_read_u32(r, IFLA_NUM_RX_QUEUES, &u32_data) == 0);

        assert_se(sd_rtnl_message_read_ether_addr(r, IFLA_ADDRESS, &eth_data) == 0);

        assert_se(sd_rtnl_flush(rtnl) >= 0);
        assert_se((m = sd_rtnl_message_unref(m)) == NULL);
        assert_se((r = sd_rtnl_message_unref(r)) == NULL);
}
示例#14
0
static bool check_loopback(sd_netlink *rtnl) {
        _cleanup_netlink_message_unref_ sd_netlink_message *req = NULL, *reply = NULL;
        unsigned flags;
        int r;

        r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, LOOPBACK_IFINDEX);
        if (r < 0)
                return false;

        r = sd_netlink_call(rtnl, req, 0, &reply);
        if (r < 0)
                return false;

        r = sd_rtnl_message_link_get_flags(reply, &flags);
        if (r < 0)
                return false;

        return flags & IFF_UP;
}
示例#15
0
static void test_async(int ifindex) {
        _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
        _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL, *r = NULL;
        uint32_t serial;
        char *ifname;

        ifname = strdup("lo");
        assert_se(ifname);

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

        assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);

        assert_se(sd_rtnl_call_async(rtnl, m, &link_handler, ifname, 0, &serial) >= 0);

        assert_se(sd_rtnl_wait(rtnl, 0) >= 0);
        assert_se(sd_rtnl_process(rtnl, &r) >= 0);

        assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);
}
示例#16
0
int netdev_create_veth(NetDev *netdev, sd_rtnl_message_handler_t callback) {
        _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
        int r;

        assert(netdev);
        assert(netdev->ifname);
        assert(netdev->manager);
        assert(netdev->manager->rtnl);

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

        if(netdev->kind != NETDEV_KIND_VETH)
                return -ENOTSUP;

        r = netdev_fill_veth_rtnl_message(netdev, m);
        if(r < 0)
                return r;

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

        netdev_ref(netdev);

        log_debug_netdev(netdev, "Creating veth netdev: %s",
                         netdev_kind_to_string(netdev->kind));

        netdev->state = NETDEV_STATE_CREATING;

        return 0;
}
示例#17
0
int netdev_create_vxlan(NetDev *netdev, Link *link, sd_rtnl_message_handler_t callback) {
        _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
        int r;

        assert(netdev);
        assert(!(netdev->kind == NETDEV_KIND_VXLAN) || (link && callback));
        assert(netdev->ifname);
        assert(netdev->manager);
        assert(netdev->manager->rtnl);

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

        r = netdev_fill_vxlan_rtnl_message(netdev, link, m);
        if(r < 0)
                return r;

        r = sd_rtnl_call_async(netdev->manager->rtnl, m, 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 vxlan netdev: %s",
                         netdev_kind_to_string(netdev->kind));

        netdev->state = NETDEV_STATE_CREATING;

        return 0;
}
示例#18
0
static void test_event_loop(int ifindex) {
        _cleanup_event_unref_ sd_event *event = NULL;
        _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
        _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
        char *ifname;

        ifname = strdup("lo2");
        assert_se(ifname);

        assert_se(sd_rtnl_open(&rtnl, 0) >= 0);
        assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);

        assert_se(sd_rtnl_call_async(rtnl, m, &link_handler, ifname, 0, NULL) >= 0);

        assert_se(sd_event_default(&event) >= 0);

        assert_se(sd_rtnl_attach_event(rtnl, event, 0) >= 0);

        assert_se(sd_event_run(event, 0) >= 0);

        assert_se(sd_rtnl_detach_event(rtnl) >= 0);

        assert_se((rtnl = sd_rtnl_unref(rtnl)) == NULL);
}
示例#19
0
static int list_links(int argc, char *argv[], void *userdata) {
        _cleanup_netlink_message_unref_ sd_netlink_message *req = NULL, *reply = NULL;
        _cleanup_netlink_unref_ sd_netlink *rtnl = NULL;
        _cleanup_free_ LinkInfo *links = NULL;
        int r, c, i;

        pager_open_if_enabled();

        r = sd_netlink_open(&rtnl);
        if (r < 0)
                return log_error_errno(r, "Failed to connect to netlink: %m");

        r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, 0);
        if (r < 0)
                return rtnl_log_create_error(r);

        r = sd_netlink_message_request_dump(req, true);
        if (r < 0)
                return rtnl_log_create_error(r);

        r = sd_netlink_call(rtnl, req, 0, &reply);
        if (r < 0)
                return log_error_errno(r, "Failed to enumerate links: %m");

        if (arg_legend)
                printf("%3s %-16s %-18s %-11s %-10s\n", "IDX", "LINK", "TYPE", "OPERATIONAL", "SETUP");

        c = decode_and_sort_links(reply, &links);
        if (c < 0)
                return rtnl_log_parse_error(c);

        for (i = 0; i < c; i++) {
                _cleanup_free_ char *setup_state = NULL, *operational_state = NULL;
                _cleanup_device_unref_ sd_device *d = NULL;
                const char *on_color_operational, *off_color_operational,
                           *on_color_setup, *off_color_setup;
                char devid[2 + DECIMAL_STR_MAX(int)];
                _cleanup_free_ char *t = NULL;

                sd_network_link_get_operational_state(links[i].ifindex, &operational_state);
                operational_state_to_color(operational_state, &on_color_operational, &off_color_operational);

                sd_network_link_get_setup_state(links[i].ifindex, &setup_state);
                setup_state_to_color(setup_state, &on_color_setup, &off_color_setup);

                sprintf(devid, "n%i", links[i].ifindex);
                (void)sd_device_new_from_device_id(&d, devid);

                link_get_type_string(links[i].iftype, d, &t);

                printf("%3i %-16s %-18s %s%-11s%s %s%-10s%s\n",
                       links[i].ifindex, links[i].name, strna(t),
                       on_color_operational, strna(operational_state), off_color_operational,
                       on_color_setup, strna(setup_state), off_color_setup);
        }

        if (arg_legend)
                printf("\n%i links listed.\n", c);

        return 0;
}
示例#20
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;
}
示例#21
0
文件: bridge.c 项目: halfline/systemd
static int netdev_bridge_post_create(NetDev *netdev, Link *link, sd_netlink_message *m) {
        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
        Bridge *b;
        int r;

        assert(netdev);

        b = BRIDGE(netdev);

        assert(b);

        r = sd_rtnl_message_new_link(netdev->manager->rtnl, &req, RTM_NEWLINK, netdev->ifindex);
        if (r < 0)
                return log_netdev_error_errno(netdev, r, "Could not allocate RTM_SETLINK message: %m");

        r = sd_netlink_message_set_flags(req, NLM_F_REQUEST | NLM_F_ACK);
        if (r < 0)
                return log_link_error_errno(link, r, "Could not set netlink flags: %m");

        r = sd_netlink_message_open_container(req, IFLA_LINKINFO);
        if (r < 0)
                return log_netdev_error_errno(netdev, r, "Could not append IFLA_PROTINFO attribute: %m");

        r = sd_netlink_message_open_container_union(req, IFLA_INFO_DATA, netdev_kind_to_string(netdev->kind));
        if (r < 0)
                return log_netdev_error_errno(netdev, r, "Could not append IFLA_INFO_DATA attribute: %m");

        /* convert to jiffes */
        if (b->forward_delay != USEC_INFINITY) {
                r = sd_netlink_message_append_u32(req, IFLA_BR_FORWARD_DELAY, usec_to_jiffies(b->forward_delay));
                if (r < 0)
                        return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_FORWARD_DELAY attribute: %m");
        }

        if (b->hello_time > 0) {
                r = sd_netlink_message_append_u32(req, IFLA_BR_HELLO_TIME, usec_to_jiffies(b->hello_time));
                if (r < 0)
                        return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_HELLO_TIME attribute: %m");
        }

        if (b->max_age > 0) {
                r = sd_netlink_message_append_u32(req, IFLA_BR_MAX_AGE, usec_to_jiffies(b->max_age));
                if (r < 0)
                        return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_MAX_AGE attribute: %m");
        }

        if (b->ageing_time != USEC_INFINITY) {
                r = sd_netlink_message_append_u32(req, IFLA_BR_AGEING_TIME, usec_to_jiffies(b->ageing_time));
                if (r < 0)
                        return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_AGEING_TIME attribute: %m");
        }

        if (b->priority > 0) {
                r = sd_netlink_message_append_u16(req, IFLA_BR_PRIORITY, b->priority);
                if (r < 0)
                        return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_PRIORITY attribute: %m");
        }

        if (b->group_fwd_mask > 0) {
                r = sd_netlink_message_append_u16(req, IFLA_BR_GROUP_FWD_MASK, b->group_fwd_mask);
                if (r < 0)
                        return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_GROUP_FWD_MASK attribute: %m");
        }

        if (b->default_pvid != VLANID_INVALID) {
                r = sd_netlink_message_append_u16(req, IFLA_BR_VLAN_DEFAULT_PVID, b->default_pvid);
                if (r < 0)
                        return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_VLAN_DEFAULT_PVID attribute: %m");
        }

        if (b->mcast_querier >= 0) {
                r = sd_netlink_message_append_u8(req, IFLA_BR_MCAST_QUERIER, b->mcast_querier);
                if (r < 0)
                        return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_MCAST_QUERIER attribute: %m");
        }

        if (b->mcast_snooping >= 0) {
                r = sd_netlink_message_append_u8(req, IFLA_BR_MCAST_SNOOPING, b->mcast_snooping);
                if (r < 0)
                        return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_MCAST_SNOOPING attribute: %m");
        }

        if (b->vlan_filtering >= 0) {
                r = sd_netlink_message_append_u8(req, IFLA_BR_VLAN_FILTERING, b->vlan_filtering);
                if (r < 0)
                        return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_VLAN_FILTERING attribute: %m");
        }

        if (b->stp >= 0) {
                r = sd_netlink_message_append_u32(req, IFLA_BR_STP_STATE, b->stp);
                if (r < 0)
                        return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_STP_STATE attribute: %m");
        }

        r = sd_netlink_message_close_container(req);
        if (r < 0)
                return log_netdev_error_errno(netdev, r, "Could not append IFLA_LINKINFO attribute: %m");

        r = sd_netlink_message_close_container(req);
        if (r < 0)
                return log_netdev_error_errno(netdev, r, "Could not append IFLA_INFO_DATA attribute: %m");

        r = sd_netlink_call_async(netdev->manager->rtnl, req, netdev_bridge_set_handler, netdev, 0, NULL);
        if (r < 0)
                return log_netdev_error_errno(netdev, r, "Could not send rtnetlink message: %m");

        netdev_ref(netdev);

        return r;
}
示例#22
0
static int netdev_bridge_post_create(NetDev *netdev, Link *link, sd_netlink_message *m) {
        _cleanup_netlink_message_unref_ sd_netlink_message *req = NULL;
        Bridge *b;
        int r;

        assert(netdev);

        b = BRIDGE(netdev);

        assert(b);

        r = sd_rtnl_message_new_link(netdev->manager->rtnl, &req, RTM_NEWLINK, netdev->ifindex);
        if (r < 0)
                return log_netdev_error_errno(netdev, r, "Could not allocate RTM_SETLINK message: %m");

        r = sd_netlink_message_set_flags(req, NLM_F_REQUEST | NLM_F_ACK);
        if (r < 0)
                return log_link_error_errno(link, r, "Could not set netlink flags: %m");

        r = sd_netlink_message_open_container(req, IFLA_LINKINFO);
        if (r < 0)
                return log_netdev_error_errno(netdev, r, "Could not append IFLA_PROTINFO attribute: %m");

        r = sd_netlink_message_open_container_union(req, IFLA_INFO_DATA, netdev_kind_to_string(netdev->kind));
        if (r < 0)
                return log_netdev_error_errno(netdev, r, "Could not append IFLA_INFO_DATA attribute: %m");

        /* convert to jiffes */
        if (b->forward_delay > 0) {
                r = sd_netlink_message_append_u32(req, IFLA_BR_FORWARD_DELAY, usec_to_jiffies(b->forward_delay));
                if (r < 0)
                        return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_FORWARD_DELAY attribute: %m");
        }

        if (b->hello_time > 0) {
                r = sd_netlink_message_append_u32(req, IFLA_BR_HELLO_TIME, usec_to_jiffies(b->hello_time));
                if (r < 0)
                        return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_HELLO_TIME attribute: %m");
        }

        if (b->max_age > 0) {
                r = sd_netlink_message_append_u32(req, IFLA_BR_MAX_AGE, usec_to_jiffies(b->max_age));
                if (r < 0)
                        return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_MAX_AGE attribute: %m");
        }

        r = sd_netlink_message_close_container(req);
        if (r < 0)
                return log_netdev_error_errno(netdev, r, "Could not append IFLA_LINKINFO attribute: %m");

        r = sd_netlink_message_close_container(req);
        if (r < 0)
                return log_netdev_error_errno(netdev, r, "Could not append IFLA_INFO_DATA attribute: %m");

        r = sd_netlink_call_async(netdev->manager->rtnl, req, netdev_bridge_set_handler, netdev, 0, NULL);
        if (r < 0)
                return log_netdev_error_errno(netdev, r, "Could not send rtnetlink message: %m");

        netdev_ref(netdev);

        return r;
}
示例#23
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;
}
示例#24
0
static void test_link_get(sd_rtnl *rtnl, int ifindex) {
        sd_rtnl_message *m;
        sd_rtnl_message *r;
        unsigned int mtu = 1500;
        unsigned int *mtu_reply;
        void *data;
        char *str_data;
        uint16_t type;
        uint8_t u8_data;
        uint32_t u32_data;
        struct ether_addr eth_data;

        assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);
        assert_se(m);

        /* u8 test cases  */
        assert_se(sd_rtnl_message_append_u8(m, IFLA_CARRIER, 0) >= 0);
        assert_se(sd_rtnl_message_append_u8(m, IFLA_OPERSTATE, 0) >= 0);
        assert_se(sd_rtnl_message_append_u8(m, IFLA_LINKMODE, 0) >= 0);

        /* u32 test cases */
        assert_se(sd_rtnl_message_append_u32(m, IFLA_MTU, mtu) >= 0);
        assert_se(sd_rtnl_message_append_u32(m, IFLA_GROUP, 0) >= 0);
        assert_se(sd_rtnl_message_append_u32(m, IFLA_TXQLEN, 0) >= 0);
        assert_se(sd_rtnl_message_append_u32(m, IFLA_NUM_TX_QUEUES, 0) >= 0);
        assert_se(sd_rtnl_message_append_u32(m, IFLA_NUM_RX_QUEUES, 0) >= 0);

        assert_se(sd_rtnl_call(rtnl, m, -1, &r) == 1);

        /* u8 read back */
        assert_se(sd_rtnl_message_read(m, &type, &data) == 1);
        assert_se(type == IFLA_CARRIER);

        assert_se(sd_rtnl_message_read(m, &type, &data) == 1);
        assert_se(type == IFLA_OPERSTATE);

        assert_se(sd_rtnl_message_read(m, &type, &data) == 1);
        assert_se(type == IFLA_LINKMODE);

        /* u32 read back */
        assert_se(sd_rtnl_message_read(m, &type, (void **) &mtu_reply) == 1);
        assert_se(type == IFLA_MTU);
        assert_se(*mtu_reply == mtu);

        assert_se(sd_rtnl_message_read(m, &type, &data) == 1);
        assert_se(type == IFLA_GROUP);

        assert_se(sd_rtnl_message_read(m, &type, &data) == 1);
        assert_se(type == IFLA_TXQLEN);

        assert_se(sd_rtnl_message_read(m, &type, &data) == 1);
        assert_se(type == IFLA_NUM_TX_QUEUES);

        assert_se(sd_rtnl_message_read(m, &type, &data) == 1);
        assert_se(type == IFLA_NUM_RX_QUEUES);

        while (sd_rtnl_message_read(r, &type, &data) > 0) {
                switch (type) {
//                        case IFLA_MTU:
//                                assert_se(*(unsigned int *) data == 65536);
//                                break;
//                        case IFLA_QDISC:
//                                assert_se(streq((char *) data, "noqueue"));
//                                break;
                        case IFLA_IFNAME:
                                assert_se(streq((char *) data, "lo"));
                                break;
                }
        }

        assert_se(sd_rtnl_message_read_string(r, IFLA_IFNAME, &str_data) == 0);

        assert_se(sd_rtnl_message_read_u8(r, IFLA_CARRIER, &u8_data) == 0);
        assert_se(sd_rtnl_message_read_u8(r, IFLA_OPERSTATE, &u8_data) == 0);
        assert_se(sd_rtnl_message_read_u8(r, IFLA_LINKMODE, &u8_data) == 0);

        assert_se(sd_rtnl_message_read_u32(r, IFLA_MTU, &u32_data) == 0);
        assert_se(sd_rtnl_message_read_u32(r, IFLA_GROUP, &u32_data) == 0);
        assert_se(sd_rtnl_message_read_u32(r, IFLA_TXQLEN, &u32_data) == 0);
        assert_se(sd_rtnl_message_read_u32(r, IFLA_NUM_TX_QUEUES, &u32_data) == 0);
        assert_se(sd_rtnl_message_read_u32(r, IFLA_NUM_RX_QUEUES, &u32_data) == 0);

        assert_se(sd_rtnl_message_read_ether_addr(r, IFLA_ADDRESS, &eth_data) == 0);

        assert_se(sd_rtnl_flush(rtnl) >= 0);
        assert_se((m = sd_rtnl_message_unref(m)) == NULL);
        assert_se((r = sd_rtnl_message_unref(r)) == NULL);
}
示例#25
0
int netdev_create_tunnel(NetDev *netdev, Link *link, sd_rtnl_message_handler_t callback) {
        _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
        int r;

        assert(netdev);
        assert(netdev->ifname);
        assert(netdev->manager);
        assert(netdev->manager->rtnl);
        assert(link);
        assert(link->network);
        assert(link->network->tunnel == netdev);

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

        switch(netdev->kind) {
        case NETDEV_KIND_IPIP:
                r = netdev_fill_ipip_rtnl_message(link, m);
                if(r < 0)
                        return r;
                break;
        case NETDEV_KIND_SIT:
                r = netdev_fill_sit_rtnl_message(link, m);
                if(r < 0)
                        return r;
                break;
        case NETDEV_KIND_VTI:
                netdev_fill_vti_rtnl_message(link, m);
                if(r < 0)
                        return r;
                break;
        case NETDEV_KIND_GRE:
                r = netdev_fill_ipgre_rtnl_message(link, m);
                if(r < 0)
                        return r;
                break;
        default:
                return -ENOTSUP;
        }

        r = sd_rtnl_call_async(netdev->manager->rtnl, m, 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 tunnel netdev: %s",
                         netdev_kind_to_string(netdev->kind));

        netdev->state = NETDEV_STATE_CREATING;

        return 0;
}
示例#26
0
static int test_pppoe_server(sd_event *e) {
        sd_netlink *rtnl;
        sd_netlink_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_netlink_open(&rtnl) >= 0);
        assert_se(sd_netlink_attach_event(rtnl, e, 0) >= 0);

        assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0) >= 0);
        assert_se(sd_netlink_message_append_string(m, IFLA_IFNAME, "pppoe-server") >= 0);
        assert_se(sd_netlink_message_open_container(m, IFLA_LINKINFO) >= 0);
        assert_se(sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "veth") >= 0);
        assert_se(sd_netlink_message_open_container(m, VETH_INFO_PEER) >= 0);
        assert_se(sd_netlink_message_append_string(m, IFLA_IFNAME, "pppoe-client") >= 0);
        assert_se(sd_netlink_message_close_container(m) >= 0);
        assert_se(sd_netlink_message_close_container(m) >= 0);
        assert_se(sd_netlink_message_close_container(m) >= 0);
        assert_se(sd_netlink_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_netlink_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_netlink_call(rtnl, m, 0, NULL) >= 0);

        m = sd_netlink_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_netlink_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_netlink_message_unref(m));
        assert_se(!sd_netlink_unref(rtnl));

        return EXIT_SUCCESS;
}
示例#27
0
文件: geneve.c 项目: clemensg/systemd
static int netdev_geneve_create(NetDev *netdev) {
        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
        Geneve *v;
        int r;

        assert(netdev);

        v = GENEVE(netdev);

        r = sd_rtnl_message_new_link(netdev->manager->rtnl, &m, RTM_NEWLINK, 0);
        if (r < 0)
                return log_netdev_error_errno(netdev, r, "Could not allocate RTM_NEWLINK message: %m");

        r = sd_netlink_message_append_string(m, IFLA_IFNAME, netdev->ifname);
        if (r < 0)
                return log_netdev_error_errno(netdev, r, "Could not append IFLA_IFNAME, attribute: %m");

        if (netdev->mac) {
                r = sd_netlink_message_append_ether_addr(m, IFLA_ADDRESS, netdev->mac);
                if (r < 0)
                        return log_netdev_error_errno(netdev, r, "Could not append IFLA_ADDRESS attribute: %m");
        }

        if (netdev->mtu != 0) {
                r = sd_netlink_message_append_u32(m, IFLA_MTU, netdev->mtu);
                if (r < 0)
                        return log_netdev_error_errno(netdev, r, "Could not append IFLA_MTU attribute: %m");
        }

        r = sd_netlink_message_open_container(m, IFLA_LINKINFO);
        if (r < 0)
                return log_netdev_error_errno(netdev, r, "Could not append IFLA_LINKINFO attribute: %m");

        r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, netdev_kind_to_string(netdev->kind));
        if (r < 0)
                return log_netdev_error_errno(netdev, r, "Could not append IFLA_INFO_DATA attribute: %m");

        if (v->id <= GENEVE_VID_MAX) {
                r = sd_netlink_message_append_u32(m, IFLA_GENEVE_ID, v->id);
                if (r < 0)
                        return log_netdev_error_errno(netdev, r, "Could not append IFLA_GENEVE_ID attribute: %m");
        }

        if (in_addr_is_null(v->remote_family, &v->remote) == 0) {
                if (v->remote_family == AF_INET)
                        r = sd_netlink_message_append_in_addr(m, IFLA_GENEVE_REMOTE, &v->remote.in);
                else
                        r = sd_netlink_message_append_in6_addr(m, IFLA_GENEVE_REMOTE6, &v->remote.in6);
                if (r < 0)
                        return log_netdev_error_errno(netdev, r, "Could not append IFLA_GENEVE_REMOTE/IFLA_GENEVE_REMOTE6 attribute: %m");
        }

        if (v->ttl > 0) {
                r = sd_netlink_message_append_u8(m, IFLA_GENEVE_TTL, v->ttl);
                if (r < 0)
                        return log_netdev_error_errno(netdev, r, "Could not append IFLA_GENEVE_TTL attribute: %m");
        }

        r = sd_netlink_message_append_u8(m, IFLA_GENEVE_TOS, v->tos);
        if (r < 0)
                return log_netdev_error_errno(netdev, r, "Could not append IFLA_GENEVE_TOS attribute: %m");

        r = sd_netlink_message_append_u8(m, IFLA_GENEVE_UDP_CSUM, v->udpcsum);
        if (r < 0)
                return log_netdev_error_errno(netdev, r, "Could not append IFLA_GENEVE_UDP_CSUM attribute: %m");

        r = sd_netlink_message_append_u8(m, IFLA_GENEVE_UDP_ZERO_CSUM6_TX, v->udp6zerocsumtx);
        if (r < 0)
                return log_netdev_error_errno(netdev, r, "Could not append IFLA_GENEVE_UDP_ZERO_CSUM6_TX attribute: %m");

        r = sd_netlink_message_append_u8(m, IFLA_GENEVE_UDP_ZERO_CSUM6_RX, v->udp6zerocsumrx);
        if (r < 0)
                return log_netdev_error_errno(netdev, r, "Could not append IFLA_GENEVE_UDP_ZERO_CSUM6_RX attribute: %m");

        if (v->dest_port != DEFAULT_GENEVE_DESTINATION_PORT) {
                r = sd_netlink_message_append_u16(m, IFLA_GENEVE_PORT, htobe16(v->dest_port));
                if (r < 0)
                        return log_netdev_error_errno(netdev, r, "Could not append IFLA_GENEVE_PORT attribute: %m");
        }

        if (v->flow_label > 0) {
                r = sd_netlink_message_append_u32(m, IFLA_GENEVE_LABEL, htobe32(v->flow_label));
                if (r < 0)
                        return log_netdev_error_errno(netdev, r, "Could not append IFLA_GENEVE_LABEL attribute: %m");
        }

        r = sd_netlink_message_close_container(m);
        if (r < 0)
                return log_netdev_error_errno(netdev, r, "Could not append IFLA_INFO_DATA attribute: %m");

        r = sd_netlink_message_close_container(m);
        if (r < 0)
                return log_netdev_error_errno(netdev, r, "Could not append IFLA_LINKINFO attribute: %m");

        r = netlink_call_async(netdev->manager->rtnl, NULL, m, geneve_netdev_create_handler,
                               netdev_destroy_callback, netdev);
        if (r < 0)
                return log_netdev_error_errno(netdev, r, "Could not send rtnetlink message: %m");

        netdev_ref(netdev);
        netdev->state = NETDEV_STATE_CREATING;

        log_netdev_debug(netdev, "Creating");

        return r;
}