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; }
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; }
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); }
static void test_get_addresses(sd_rtnl *rtnl) { _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL; sd_rtnl_message *m; assert_se(sd_rtnl_message_new_addr(rtnl, &req, RTM_GETADDR, 0, AF_UNSPEC) >= 0); assert_se(sd_rtnl_call(rtnl, req, 0, &reply) >= 0); for (m = reply; m; m = sd_rtnl_message_next(m)) { uint16_t type; unsigned char scope, flags; int family, ifindex; assert_se(sd_rtnl_message_get_type(m, &type) >= 0); assert_se(type == RTM_NEWADDR); assert_se(sd_rtnl_message_addr_get_ifindex(m, &ifindex) >= 0); assert_se(sd_rtnl_message_addr_get_family(m, &family) >= 0); assert_se(sd_rtnl_message_addr_get_scope(m, &scope) >= 0); assert_se(sd_rtnl_message_addr_get_flags(m, &flags) >= 0); assert_se(ifindex > 0); assert_se(family == AF_INET || family == AF_INET6); log_info("got IPv%u address on ifindex %i", family == AF_INET ? 4: 6, ifindex); } }
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_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; }
static int start_loopback(sd_rtnl *rtnl) { _cleanup_rtnl_message_unref_ sd_rtnl_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_rtnl_call(rtnl, req, 0, NULL); if (r < 0) return r; return 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, ð_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); }
static bool check_loopback(sd_rtnl *rtnl) { _cleanup_rtnl_message_unref_ sd_rtnl_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_rtnl_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; }
static void test_address_get(sd_rtnl *rtnl, int ifindex) { sd_rtnl_message *m; sd_rtnl_message *r; struct in_addr in_data; assert_se(sd_rtnl_message_new_addr(rtnl, &m, RTM_GETADDR, ifindex, AF_INET) >= 0); assert_se(m); assert_se(sd_rtnl_call(rtnl, m, -1, &r) == 1); assert_se(sd_rtnl_message_read_in_addr(r, IFA_LOCAL, &in_data) == 0); assert_se(sd_rtnl_message_read_in_addr(r, IFA_ADDRESS, &in_data) == 0); assert_se(sd_rtnl_message_read_in_addr(r, IFA_LABEL, &in_data) == 0); assert_se(sd_rtnl_message_read_in_addr(r, IFA_CACHEINFO, &in_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); }
int rtnl_set_link_name(sd_rtnl *rtnl, int ifindex, const char *name) { _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *message = NULL; int r; assert(rtnl); assert(ifindex > 0); assert(name); r = sd_rtnl_message_link_new(RTM_SETLINK, ifindex, 0, 0, &message); if (r < 0) return r; r = sd_rtnl_message_append(message, IFLA_IFNAME, name); if (r < 0) return r; r = sd_rtnl_call(rtnl, message, 0, NULL); if (r < 0) return r; return 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; }
int local_gateways(sd_rtnl *context, int ifindex, int af, struct local_address **ret) { _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL; _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL; _cleanup_free_ struct local_address *list = NULL; sd_rtnl_message *m = NULL; size_t n_list = 0, n_allocated = 0; int r; assert(ret); if (context) rtnl = sd_rtnl_ref(context); else { r = sd_rtnl_open(&rtnl, 0); if (r < 0) return r; } r = sd_rtnl_message_new_route(rtnl, &req, RTM_GETROUTE, af, RTPROT_UNSPEC); if (r < 0) return r; r = sd_rtnl_message_request_dump(req, true); if (r < 0) return r; r = sd_rtnl_call(rtnl, req, 0, &reply); if (r < 0) return r; for (m = reply; m; m = sd_rtnl_message_next(m)) { struct local_address *a; uint16_t type; unsigned char dst_len, src_len; uint32_t ifi; int family; r = sd_rtnl_message_get_errno(m); if (r < 0) return r; r = sd_rtnl_message_get_type(m, &type); if (r < 0) return r; if (type != RTM_NEWROUTE) continue; /* We only care for default routes */ r = sd_rtnl_message_route_get_dst_prefixlen(m, &dst_len); if (r < 0) return r; if (dst_len != 0) continue; r = sd_rtnl_message_route_get_src_prefixlen(m, &src_len); if (r < 0) return r; if (src_len != 0) continue; r = sd_rtnl_message_read_u32(m, RTA_OIF, &ifi); if (r < 0) return r; if (ifindex > 0 && (int) ifi != ifindex) continue; r = sd_rtnl_message_route_get_family(m, &family); if (r < 0) return r; if (af != AF_UNSPEC && af != family) continue; if (!GREEDY_REALLOC0(list, n_allocated, n_list + 1)) return -ENOMEM; a = list + n_list; switch (family) { case AF_INET: r = sd_rtnl_message_read_in_addr(m, RTA_GATEWAY, &a->address.in); if (r < 0) continue; break; case AF_INET6: r = sd_rtnl_message_read_in6_addr(m, RTA_GATEWAY, &a->address.in6); if (r < 0) continue; break; default: continue; } sd_rtnl_message_read_u32(m, RTA_PRIORITY, &a->metric); a->ifindex = ifi; a->family = family; n_list++; } if (n_list > 0) qsort(list, n_list, sizeof(struct local_address), address_compare); *ret = list; list = NULL; return (int) n_list; }
int local_addresses(sd_rtnl *context, int ifindex, int af, struct local_address **ret) { _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL; _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL; _cleanup_free_ struct local_address *list = NULL; size_t n_list = 0, n_allocated = 0; sd_rtnl_message *m; int r; assert(ret); if (context) rtnl = sd_rtnl_ref(context); else { r = sd_rtnl_open(&rtnl, 0); if (r < 0) return r; } r = sd_rtnl_message_new_addr(rtnl, &req, RTM_GETADDR, 0, af); if (r < 0) return r; r = sd_rtnl_call(rtnl, req, 0, &reply); if (r < 0) return r; for (m = reply; m; m = sd_rtnl_message_next(m)) { struct local_address *a; unsigned char flags; uint16_t type; int ifi, family; r = sd_rtnl_message_get_errno(m); if (r < 0) return r; r = sd_rtnl_message_get_type(m, &type); if (r < 0) return r; if (type != RTM_NEWADDR) continue; r = sd_rtnl_message_addr_get_ifindex(m, &ifi); if (r < 0) return r; if (ifindex > 0 && ifi != ifindex) continue; r = sd_rtnl_message_addr_get_family(m, &family); if (r < 0) return r; if (af != AF_UNSPEC && af != family) continue; r = sd_rtnl_message_addr_get_flags(m, &flags); if (r < 0) return r; if (flags & IFA_F_DEPRECATED) continue; if (!GREEDY_REALLOC0(list, n_allocated, n_list+1)) return -ENOMEM; a = list + n_list; r = sd_rtnl_message_addr_get_scope(m, &a->scope); if (r < 0) return r; if (ifindex == 0 && (a->scope == RT_SCOPE_HOST || a->scope == RT_SCOPE_NOWHERE)) continue; switch (family) { case AF_INET: r = sd_rtnl_message_read_in_addr(m, IFA_LOCAL, &a->address.in); if (r < 0) { r = sd_rtnl_message_read_in_addr(m, IFA_ADDRESS, &a->address.in); if (r < 0) continue; } break; case AF_INET6: r = sd_rtnl_message_read_in6_addr(m, IFA_LOCAL, &a->address.in6); if (r < 0) { r = sd_rtnl_message_read_in6_addr(m, IFA_ADDRESS, &a->address.in6); if (r < 0) continue; } break; default: continue; } a->ifindex = ifi; a->family = family; n_list++; }; if (n_list > 0) qsort(list, n_list, sizeof(struct local_address), address_compare); *ret = list; list = NULL; return (int) n_list; }
int main(void) { sd_rtnl *rtnl; sd_rtnl_message *m; sd_rtnl_message *r; void *data; int if_loopback; uint16_t type; unsigned int mtu = 0; unsigned int *mtu_reply; test_multiple(); test_route(); test_container(); assert(sd_rtnl_open(0, &rtnl) >= 0); assert(rtnl); if_loopback = (int) if_nametoindex("lo"); assert(if_loopback > 0); test_async(if_loopback); test_pipe(if_loopback); test_event_loop(if_loopback); test_link_configure(rtnl, if_loopback); assert(sd_rtnl_message_link_new(RTM_GETLINK, if_loopback, 0, 0, &m) >= 0); assert(m); assert(sd_rtnl_message_get_type(m, &type) >= 0); assert(type == RTM_GETLINK); assert(sd_rtnl_message_read(m, &type, &data) == 0); assert(sd_rtnl_call(rtnl, m, 0, &r) == 1); assert(sd_rtnl_message_get_type(r, &type) >= 0); assert(type == RTM_NEWLINK); assert(sd_rtnl_message_read(m, &type, &data) == 0); assert((r = sd_rtnl_message_unref(r)) == NULL); assert(sd_rtnl_call(rtnl, m, -1, &r) == -EPERM); assert((m = sd_rtnl_message_unref(m)) == NULL); assert((r = sd_rtnl_message_unref(r)) == NULL); assert(sd_rtnl_message_link_new(RTM_GETLINK, if_loopback, 0, 0, &m) >= 0); assert(m); assert(sd_rtnl_message_append(m, IFLA_MTU, &mtu) >= 0); assert(sd_rtnl_message_read(m, &type, (void **) &mtu_reply) == 1); assert(type == IFLA_MTU); assert(*mtu_reply == 0); assert(sd_rtnl_message_read(m, &type, &data) == 0); assert(sd_rtnl_call(rtnl, m, -1, &r) == 1); while (sd_rtnl_message_read(r, &type, &data) > 0) { switch (type) { // case IFLA_MTU: // assert(*(unsigned int *) data == 65536); // break; // case IFLA_QDISC: // assert(streq((char *) data, "noqueue")); // break; case IFLA_IFNAME: assert(streq((char *) data, "lo")); break; } } assert(sd_rtnl_flush(rtnl) >= 0); assert((m = sd_rtnl_message_unref(m)) == NULL); assert((r = sd_rtnl_message_unref(r)) == NULL); assert((rtnl = sd_rtnl_unref(rtnl)) == NULL); return EXIT_SUCCESS; }
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, ð_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); }
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 get_gateway_description( sd_rtnl *rtnl, sd_hwdb *hwdb, int ifindex, int family, union in_addr_union *gateway, char **gateway_description) { _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL; sd_rtnl_message *m; int r; assert(rtnl); assert(ifindex >= 0); assert(family == AF_INET || family == AF_INET6); assert(gateway); assert(gateway_description); r = sd_rtnl_message_new_neigh(rtnl, &req, RTM_GETNEIGH, ifindex, family); if (r < 0) return r; r = sd_rtnl_message_request_dump(req, true); if (r < 0) return r; r = sd_rtnl_call(rtnl, req, 0, &reply); if (r < 0) return r; for (m = reply; m; m = sd_rtnl_message_next(m)) { union in_addr_union gw = {}; struct ether_addr mac = {}; uint16_t type; int ifi, fam; r = sd_rtnl_message_get_errno(m); if (r < 0) { log_error_errno(r, "got error: %m"); continue; } r = sd_rtnl_message_get_type(m, &type); if (r < 0) { log_error_errno(r, "could not get type: %m"); continue; } if (type != RTM_NEWNEIGH) { log_error("type is not RTM_NEWNEIGH"); continue; } r = sd_rtnl_message_neigh_get_family(m, &fam); if (r < 0) { log_error_errno(r, "could not get family: %m"); continue; } if (fam != family) { log_error("family is not correct"); continue; } r = sd_rtnl_message_neigh_get_ifindex(m, &ifi); if (r < 0) { log_error_errno(r, "could not get ifindex: %m"); continue; } if (ifindex > 0 && ifi != ifindex) continue; switch (fam) { case AF_INET: r = sd_rtnl_message_read_in_addr(m, NDA_DST, &gw.in); if (r < 0) continue; break; case AF_INET6: r = sd_rtnl_message_read_in6_addr(m, NDA_DST, &gw.in6); if (r < 0) continue; break; default: continue; } if (!in_addr_equal(fam, &gw, gateway)) continue; r = sd_rtnl_message_read_ether_addr(m, NDA_LLADDR, &mac); if (r < 0) continue; r = ieee_oui(hwdb, &mac, gateway_description); if (r < 0) continue; return 0; } return -ENODATA; }
static int list_links(int argc, char *argv[], void *userdata) { _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL; _cleanup_udev_unref_ struct udev *udev = NULL; _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL; _cleanup_free_ LinkInfo *links = NULL; int r, c, i; pager_open_if_enabled(); r = sd_rtnl_open(&rtnl, 0); if (r < 0) return log_error_errno(r, "Failed to connect to netlink: %m"); udev = udev_new(); if (!udev) return log_error_errno(errno, "Failed to connect to udev: %m"); r = sd_rtnl_message_new_link(rtnl, &req, RTM_GETLINK, 0); if (r < 0) return rtnl_log_create_error(r); r = sd_rtnl_message_request_dump(req, true); if (r < 0) return rtnl_log_create_error(r); r = sd_rtnl_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_udev_device_unref_ struct udev_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); d = udev_device_new_from_device_id(udev, 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; }