xref: /freebsd/tests/sys/netlink/test_snl.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 
5 #include <sys/param.h>
6 #include <sys/module.h>
7 
8 #include <netlink/netlink.h>
9 #include <netlink/netlink_route.h>
10 #include "netlink/netlink_snl.h"
11 #include "netlink/netlink_snl_route.h"
12 #include "netlink/netlink_snl_route_parsers.h"
13 
14 #include <atf-c.h>
15 
16 static void
17 require_netlink(void)
18 {
19 	if (modfind("netlink") == -1)
20 		atf_tc_skip("netlink module not loaded");
21 }
22 
23 ATF_TC(snl_verify_core_parsers);
24 ATF_TC_HEAD(snl_verify_core_parsers, tc)
25 {
26 	atf_tc_set_md_var(tc, "descr", "Tests snl(3) core nlmsg parsers are correct");
27 }
28 
29 ATF_TC_BODY(snl_verify_core_parsers, tc)
30 {
31 	SNL_VERIFY_PARSERS(snl_all_core_parsers);
32 
33 }
34 
35 ATF_TC(snl_verify_route_parsers);
36 ATF_TC_HEAD(snl_verify_route_parsers, tc)
37 {
38 	atf_tc_set_md_var(tc, "descr", "Tests snl(3) route parsers are correct");
39 }
40 
41 ATF_TC_BODY(snl_verify_route_parsers, tc)
42 {
43 	SNL_VERIFY_PARSERS(snl_all_route_parsers);
44 
45 }
46 
47 ATF_TC(snl_parse_errmsg_capped);
48 ATF_TC_HEAD(snl_parse_errmsg_capped, tc)
49 {
50 	atf_tc_set_md_var(tc, "descr", "Tests snl(3) correctly parsing capped errors");
51 }
52 
53 ATF_TC_BODY(snl_parse_errmsg_capped, tc)
54 {
55 	struct snl_state ss;
56 	struct snl_writer nw;
57 
58 	require_netlink();
59 
60 	if (!snl_init(&ss, NETLINK_ROUTE))
61 		atf_tc_fail("snl_init() failed");
62 
63 	atf_tc_skip("does not work");
64 
65 	int optval = 1;
66 	ATF_CHECK(setsockopt(ss.fd, SOL_NETLINK, NETLINK_CAP_ACK, &optval, sizeof(optval)) == 0);
67 
68 	snl_init_writer(&ss, &nw);
69 
70 	struct nlmsghdr *hdr = snl_create_msg_request(&nw, 255);
71 	ATF_CHECK(hdr != NULL);
72 	ATF_CHECK(snl_reserve_msg_object(&nw, struct ifinfomsg) != NULL);
73 	snl_add_msg_attr_string(&nw, 143, "some random string");
74 	ATF_CHECK(snl_finalize_msg(&nw) != NULL);
75 
76 	ATF_CHECK(snl_send_message(&ss, hdr));
77 
78 	struct nlmsghdr *rx_hdr = snl_read_reply(&ss, hdr->nlmsg_seq);
79 	ATF_CHECK(rx_hdr != NULL);
80 	ATF_CHECK(rx_hdr->nlmsg_type == NLMSG_ERROR);
81 
82 	struct snl_errmsg_data e = {};
83 	ATF_CHECK(rx_hdr->nlmsg_len == sizeof(struct nlmsghdr) + sizeof(struct nlmsgerr));
84 	ATF_CHECK(snl_parse_errmsg(&ss, rx_hdr, &e));
85 	ATF_CHECK(e.error != 0);
86 	ATF_CHECK(!memcmp(hdr, e.orig_hdr, sizeof(struct nlmsghdr)));
87 }
88 
89 ATF_TC(snl_parse_errmsg_capped_extack);
90 ATF_TC_HEAD(snl_parse_errmsg_capped_extack, tc)
91 {
92 	atf_tc_set_md_var(tc, "descr", "Tests snl(3) correctly parsing capped errors with extack");
93 }
94 
95 ATF_TC_BODY(snl_parse_errmsg_capped_extack, tc)
96 {
97 	struct snl_state ss;
98 	struct snl_writer nw;
99 
100 	require_netlink();
101 
102 	if (!snl_init(&ss, NETLINK_ROUTE))
103 		atf_tc_fail("snl_init() failed");
104 
105 	int optval = 1;
106 	ATF_CHECK(setsockopt(ss.fd, SOL_NETLINK, NETLINK_CAP_ACK, &optval, sizeof(optval)) == 0);
107 	optval = 1;
108 	ATF_CHECK(setsockopt(ss.fd, SOL_NETLINK, NETLINK_EXT_ACK, &optval, sizeof(optval)) == 0);
109 
110 	snl_init_writer(&ss, &nw);
111 
112 	struct nlmsghdr *hdr = snl_create_msg_request(&nw, 255);
113 	ATF_CHECK(hdr != NULL);
114 	ATF_CHECK(snl_reserve_msg_object(&nw, struct ifinfomsg) != NULL);
115 	snl_add_msg_attr_string(&nw, 143, "some random string");
116 	ATF_CHECK(snl_finalize_msg(&nw) != NULL);
117 
118 	ATF_CHECK(snl_send_message(&ss, hdr));
119 
120 	struct nlmsghdr *rx_hdr = snl_read_reply(&ss, hdr->nlmsg_seq);
121 	ATF_CHECK(rx_hdr != NULL);
122 	ATF_CHECK(rx_hdr->nlmsg_type == NLMSG_ERROR);
123 
124 	struct snl_errmsg_data e = {};
125 	ATF_CHECK(snl_parse_errmsg(&ss, rx_hdr, &e));
126 	ATF_CHECK(e.error != 0);
127 	ATF_CHECK(!memcmp(hdr, e.orig_hdr, sizeof(struct nlmsghdr)));
128 
129 	ATF_CHECK(e.error_str != NULL);
130 }
131 
132 ATF_TC(snl_parse_errmsg_uncapped_extack);
133 ATF_TC_HEAD(snl_parse_errmsg_uncapped_extack, tc)
134 {
135 	atf_tc_set_md_var(tc, "descr", "Tests snl(3) correctly parsing errors with extack");
136 }
137 
138 ATF_TC_BODY(snl_parse_errmsg_uncapped_extack, tc)
139 {
140 	struct snl_state ss;
141 	struct snl_writer nw;
142 
143 	require_netlink();
144 
145 	ATF_CHECK(snl_init(&ss, NETLINK_ROUTE));
146 
147 	int optval = 1;
148 	ATF_CHECK(setsockopt(ss.fd, SOL_NETLINK, NETLINK_EXT_ACK, &optval, sizeof(optval)) == 0);
149 
150 	snl_init_writer(&ss, &nw);
151 
152 	struct nlmsghdr *hdr = snl_create_msg_request(&nw, 255);
153 	ATF_CHECK(hdr != NULL);
154 	ATF_CHECK(snl_reserve_msg_object(&nw, struct ifinfomsg) != NULL);
155 	snl_add_msg_attr_string(&nw, 143, "some random string");
156 	ATF_CHECK(snl_finalize_msg(&nw) != NULL);
157 
158 	ATF_CHECK(snl_send_message(&ss, hdr));
159 
160 	struct nlmsghdr *rx_hdr = snl_read_reply(&ss, hdr->nlmsg_seq);
161 	ATF_CHECK(rx_hdr != NULL);
162 	ATF_CHECK(rx_hdr->nlmsg_type == NLMSG_ERROR);
163 
164 	struct snl_errmsg_data e = {};
165 	ATF_CHECK(snl_parse_errmsg(&ss, rx_hdr, &e));
166 	ATF_CHECK(e.error != 0);
167 	ATF_CHECK(!memcmp(hdr, e.orig_hdr, hdr->nlmsg_len));
168 
169 	ATF_CHECK(e.error_str != NULL);
170 }
171 
172 ATF_TC(snl_list_ifaces);
173 ATF_TC_HEAD(snl_list_ifaces, tc)
174 {
175 	atf_tc_set_md_var(tc, "descr", "Tests snl(3) listing interfaces");
176 }
177 
178 struct nl_parsed_link {
179 	uint32_t		ifi_index;
180 	uint32_t		ifla_mtu;
181 	char			*ifla_ifname;
182 };
183 
184 #define	_IN(_field)	offsetof(struct ifinfomsg, _field)
185 #define	_OUT(_field)	offsetof(struct nl_parsed_link, _field)
186 static struct snl_attr_parser ap_link[] = {
187 	{ .type = IFLA_IFNAME, .off = _OUT(ifla_ifname), .cb = snl_attr_get_string },
188 	{ .type = IFLA_MTU, .off = _OUT(ifla_mtu), .cb = snl_attr_get_uint32 },
189 };
190 static struct snl_field_parser fp_link[] = {
191 	{.off_in = _IN(ifi_index), .off_out = _OUT(ifi_index), .cb = snl_field_get_uint32 },
192 };
193 #undef _IN
194 #undef _OUT
195 SNL_DECLARE_PARSER(link_parser, struct ifinfomsg, fp_link, ap_link);
196 
197 
198 ATF_TC_BODY(snl_list_ifaces, tc)
199 {
200 	struct snl_state ss;
201 	struct snl_writer nw;
202 
203 	require_netlink();
204 
205 	if (!snl_init(&ss, NETLINK_ROUTE))
206 		atf_tc_fail("snl_init() failed");
207 
208 	snl_init_writer(&ss, &nw);
209 
210 	struct nlmsghdr *hdr = snl_create_msg_request(&nw, RTM_GETLINK);
211 	ATF_CHECK(hdr != NULL);
212 	ATF_CHECK(snl_reserve_msg_object(&nw, struct ifinfomsg) != NULL);
213 	ATF_CHECK(snl_finalize_msg(&nw) != NULL);
214 	uint32_t seq_id = hdr->nlmsg_seq;
215 
216 	ATF_CHECK(snl_send_message(&ss, hdr));
217 
218 	struct snl_errmsg_data e = {};
219 	int count = 0;
220 
221 	while ((hdr = snl_read_reply_multi(&ss, seq_id, &e)) != NULL) {
222 		count++;
223 	}
224 	ATF_REQUIRE(e.error == 0);
225 
226 	ATF_REQUIRE_MSG(count > 0, "Empty interface list");
227 }
228 
229 ATF_TP_ADD_TCS(tp)
230 {
231 	ATF_TP_ADD_TC(tp, snl_verify_core_parsers);
232 	ATF_TP_ADD_TC(tp, snl_verify_route_parsers);
233 	ATF_TP_ADD_TC(tp, snl_parse_errmsg_capped);
234 	ATF_TP_ADD_TC(tp, snl_parse_errmsg_capped_extack);
235 	ATF_TP_ADD_TC(tp, snl_parse_errmsg_uncapped_extack);
236 	ATF_TP_ADD_TC(tp, snl_list_ifaces);
237 
238 	return (atf_no_error());
239 }
240