• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
3  * Copyright (c) 2017-2018 The strace developers.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "tests.h"
30 
31 #ifdef HAVE_LINUX_FIB_RULES_H
32 
33 # include <stdio.h>
34 # include <inttypes.h>
35 # include "test_nlattr.h"
36 # include <linux/fib_rules.h>
37 # include <linux/in.h>
38 # include <linux/ip.h>
39 # include <linux/rtnetlink.h>
40 
41 #define FRA_TUN_ID 12
42 #define FRA_TABLE 15
43 #define FRA_UID_RANGE 20
44 #define FRA_PROTOCOL 21
45 #define FRA_IP_PROTO 22
46 #define FRA_SPORT_RANGE 23
47 #define FRA_DPORT_RANGE 24
48 
49 # ifndef HAVE_STRUCT_FIB_RULE_PORT_RANGE
50 struct fib_rule_port_range {
51 	uint16_t start;
52 	uint16_t end;
53 };
54 # endif /* HAVE_STRUCT_FIB_RULE_PORT_RANGE */
55 
56 static void
init_rtmsg(struct nlmsghdr * const nlh,const unsigned int msg_len)57 init_rtmsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
58 {
59 	SET_STRUCT(struct nlmsghdr, nlh,
60 		.nlmsg_len = msg_len,
61 		.nlmsg_type = RTM_GETRULE,
62 		.nlmsg_flags = NLM_F_DUMP
63 	);
64 
65 	struct rtmsg *const msg = NLMSG_DATA(nlh);
66 	SET_STRUCT(struct rtmsg, msg,
67 		.rtm_family = AF_UNIX,
68 		.rtm_tos = IPTOS_LOWDELAY,
69 		.rtm_table = RT_TABLE_UNSPEC,
70 		.rtm_type = FR_ACT_TO_TBL,
71 		.rtm_flags = FIB_RULE_INVERT
72 	);
73 }
74 
75 static void
print_rtmsg(const unsigned int msg_len)76 print_rtmsg(const unsigned int msg_len)
77 {
78 	printf("{len=%u, type=RTM_GETRULE, flags=NLM_F_DUMP"
79 	       ", seq=0, pid=0}, {family=AF_UNIX"
80 	       ", dst_len=0, src_len=0"
81 	       ", tos=IPTOS_LOWDELAY"
82 	       ", table=RT_TABLE_UNSPEC"
83 	       ", action=FR_ACT_TO_TBL"
84 	       ", flags=FIB_RULE_INVERT}",
85 	       msg_len);
86 }
87 
88 int
main(void)89 main(void)
90 {
91 	skip_if_unavailable("/proc/self/fd/");
92 
93 	const int fd = create_nl_socket(NETLINK_ROUTE);
94 	const unsigned int hdrlen = sizeof(struct rtmsg);
95 	void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), NLA_HDRLEN + 8);
96 
97 	static char pattern[4096];
98 	fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
99 
100 	const unsigned int nla_type = 0xffff & NLA_TYPE_MASK;
101 	char nla_type_str[256];
102 	sprintf(nla_type_str, "%#x /* FRA_??? */", nla_type);
103 	TEST_NLATTR_(fd, nlh0, hdrlen,
104 		     init_rtmsg, print_rtmsg,
105 		     nla_type, nla_type_str,
106 		     4, pattern, 4,
107 		     print_quoted_hex(pattern, 4));
108 
109 	TEST_NLATTR(fd, nlh0, hdrlen,
110 		    init_rtmsg, print_rtmsg,
111 		    FRA_DST, 4, pattern, 4,
112 		    print_quoted_hex(pattern, 4));
113 
114 	const uint32_t table_id = RT_TABLE_DEFAULT;
115 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
116 			   init_rtmsg, print_rtmsg,
117 			   FRA_TABLE, pattern, table_id,
118 			   printf("RT_TABLE_DEFAULT"));
119 
120 #ifdef HAVE_STRUCT_FIB_RULE_UID_RANGE
121 	static const struct fib_rule_uid_range range = {
122 		.start = 0xabcdedad,
123 		.end = 0xbcdeadba
124 	};
125 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
126 			   init_rtmsg, print_rtmsg,
127 			   FRA_UID_RANGE, pattern, range,
128 			   PRINT_FIELD_U("{", range, start);
129 			   PRINT_FIELD_U(", ", range, end);
130 			   printf("}"));
131 #endif
132 #if defined HAVE_BE64TOH || defined be64toh
133 	const uint64_t tun_id = 0xabcdcdbeedabadef;
134 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
135 			   init_rtmsg, print_rtmsg,
136 			   FRA_TUN_ID, pattern, tun_id,
137 			   printf("htobe64(%" PRIu64 ")", be64toh(tun_id)));
138 #endif
139 
140 	uint8_t proto;
141 
142 	static const struct {
143 		uint8_t arg;
144 		const char *str;
145 	} proto_args[] = {
146 		{ ARG_STR(RTPROT_UNSPEC) },
147 		{ 5, "0x5 /* RTPROT_??? */" },
148 		{ 17, "RTPROT_MROUTED" },
149 		{ 42, "RTPROT_BABEL" },
150 		{ 43, "0x2b /* RTPROT_??? */" },
151 		{ ARG_STR(0xde) " /* RTPROT_??? */" },
152 	};
153 
154 	for (unsigned i = 0; i < ARRAY_SIZE(proto_args); i++) {
155 		proto = proto_args[i].arg;
156 		TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
157 				   init_rtmsg, print_rtmsg,
158 				   FRA_PROTOCOL, pattern, proto,
159 				   printf("%s", proto_args[i].str));
160 	}
161 
162 	static const struct {
163 		uint8_t arg;
164 		const char *str;
165 	} ipproto_args[] = {
166 		{ ARG_STR(IPPROTO_TCP) },
167 		{ 254, "0xfe /* IPPROTO_??? */" },
168 		{ ARG_STR(IPPROTO_RAW) },
169 	};
170 
171 	for (unsigned i = 0; i < ARRAY_SIZE(ipproto_args); i++) {
172 		proto = ipproto_args[i].arg;
173 		TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
174 				   init_rtmsg, print_rtmsg,
175 				   FRA_IP_PROTO, pattern, proto,
176 				   printf("%s", ipproto_args[i].str));
177 	}
178 
179 	static const struct fib_rule_port_range prange = {
180 		.start = 0xabcd,
181 		.end = 0xfeed,
182 	};
183 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
184 			   init_rtmsg, print_rtmsg,
185 			   FRA_SPORT_RANGE, pattern, prange,
186 			   PRINT_FIELD_U("{", prange, start);
187 			   PRINT_FIELD_U(", ", prange, end);
188 			   printf("}"));
189 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
190 			   init_rtmsg, print_rtmsg,
191 			   FRA_DPORT_RANGE, pattern, prange,
192 			   PRINT_FIELD_U("{", prange, start);
193 			   PRINT_FIELD_U(", ", prange, end);
194 			   printf("}"));
195 
196 	puts("+++ exited with 0 +++");
197 	return 0;
198 }
199 
200 #else
201 
202 SKIP_MAIN_UNDEFINED("HAVE_LINUX_FIB_RULES_H")
203 
204 #endif
205