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); }
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); }
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; }
static void test_container(void) { _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *m = NULL; uint16_t type; void *data; assert(sd_rtnl_message_link_new(RTM_NEWLINK, 0, 0, 0, &m) >= 0); assert(sd_rtnl_message_open_container(m, IFLA_LINKINFO) >= 0); assert(sd_rtnl_message_open_container(m, IFLA_LINKINFO) == -EINVAL); assert(sd_rtnl_message_append(m, IFLA_INFO_KIND, "kind") >= 0); assert(sd_rtnl_message_close_container(m) >= 0); assert(sd_rtnl_message_close_container(m) == -EINVAL); assert(sd_rtnl_message_read(m, &type, &data) == -EINVAL); /* TODO: add support for entering containers assert(sd_rtnl_message_read(m, &type, &data) > 0); assert(type == IFLA_INFO_KIND); assert(streq("kind", (char *) data)); assert(sd_rtnl_message_read(m, &type, &data) == 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); }
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; }
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; }
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; }
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; }
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; }