1 // SPDX-License-Identifier: GPL-2.0 2 #include <stdio.h> 3 #include <string.h> 4 5 #include <ynl.h> 6 7 #include <arpa/inet.h> 8 #include <net/if.h> 9 10 #include <kselftest_harness.h> 11 12 #include "rt-route-user.h" 13 14 static void rt_route_print(struct __test_metadata *_metadata, 15 struct rt_route_getroute_rsp *r) 16 { 17 char ifname[IF_NAMESIZE]; 18 char route_str[64]; 19 const char *route; 20 const char *name; 21 22 /* Ignore local */ 23 if (r->_hdr.rtm_table == RT_TABLE_LOCAL) 24 return; 25 26 if (r->_present.oif) { 27 name = if_indextoname(r->oif, ifname); 28 EXPECT_NE(NULL, name); 29 if (name) 30 ksft_print_msg("oif: %-16s ", name); 31 } 32 33 if (r->_len.dst) { 34 route = inet_ntop(r->_hdr.rtm_family, r->dst, 35 route_str, sizeof(route_str)); 36 printf("dst: %s/%d", route, r->_hdr.rtm_dst_len); 37 } 38 39 if (r->_len.gateway) { 40 route = inet_ntop(r->_hdr.rtm_family, r->gateway, 41 route_str, sizeof(route_str)); 42 printf("gateway: %s ", route); 43 } 44 45 printf("\n"); 46 } 47 48 FIXTURE(rt_route) 49 { 50 struct ynl_sock *ys; 51 }; 52 53 FIXTURE_SETUP(rt_route) 54 { 55 struct ynl_error yerr; 56 57 self->ys = ynl_sock_create(&ynl_rt_route_family, &yerr); 58 ASSERT_NE(NULL, self->ys) 59 TH_LOG("failed to create rt-route socket: %s", yerr.msg); 60 } 61 62 FIXTURE_TEARDOWN(rt_route) 63 { 64 ynl_sock_destroy(self->ys); 65 } 66 67 TEST_F(rt_route, dump) 68 { 69 struct rt_route_getroute_req_dump *req; 70 struct rt_route_getroute_list *rsp; 71 struct in6_addr v6_expected; 72 struct in_addr v4_expected; 73 bool found_v4 = false; 74 bool found_v6 = false; 75 76 /* The bash wrapper configures 192.168.1.1/24 and 2001:db8::1/64, 77 * make sure we can find the connected routes in the dump. 78 */ 79 inet_pton(AF_INET, "192.168.1.0", &v4_expected); 80 inet_pton(AF_INET6, "2001:db8::", &v6_expected); 81 82 req = rt_route_getroute_req_dump_alloc(); 83 ASSERT_NE(NULL, req); 84 85 rsp = rt_route_getroute_dump(self->ys, req); 86 rt_route_getroute_req_dump_free(req); 87 ASSERT_NE(NULL, rsp) { 88 TH_LOG("dump failed: %s", self->ys->err.msg); 89 } 90 91 ASSERT_FALSE(ynl_dump_empty(rsp)) { 92 rt_route_getroute_list_free(rsp); 93 TH_LOG("no routes reported"); 94 } 95 96 ynl_dump_foreach(rsp, route) { 97 rt_route_print(_metadata, route); 98 99 if (route->_hdr.rtm_table == RT_TABLE_LOCAL) 100 continue; 101 102 if (route->_len.dst == 4 && route->_hdr.rtm_dst_len == 24) 103 found_v4 |= !memcmp(route->dst, &v4_expected, 4); 104 if (route->_len.dst == 16 && route->_hdr.rtm_dst_len == 64) 105 found_v6 |= !memcmp(route->dst, &v6_expected, 16); 106 } 107 rt_route_getroute_list_free(rsp); 108 109 EXPECT_TRUE(found_v4); 110 EXPECT_TRUE(found_v6); 111 } 112 113 TEST_HARNESS_MAIN 114