1 /* vi: set sw=4 ts=4: */
2 /*
3  * This program is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU General Public License
5  * as published by the Free Software Foundation; either version
6  * 2 of the License, or (at your option) any later version.
7  *
8  * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9  *
10  * Changes:
11  *
12  * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
13  * initially integrated into busybox by Bernhard Reutner-Fischer
14  */
15 #include <netinet/in.h>
16 #include <netinet/ip.h>
17 #include <arpa/inet.h>
18 
19 /* from <linux/fib_rules.h>: */
20 #define FRA_FWMARK		10
21 #define FRA_SUPPRESS_IFGROUP	13
22 #define FRA_SUPPRESS_PREFIXLEN	14
23 #define FRA_FWMASK		16
24 
25 #include "ip_common.h"  /* #include "libbb.h" is inside */
26 #include "rt_names.h"
27 #include "utils.h"
28 
29 #include <linux/version.h>
30 /* RTA_TABLE is not a define, can't test with ifdef. */
31 /* As a proxy, test which kernels toolchain expects: */
32 #define HAVE_RTA_TABLE (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19))
33 
34 /* If you add stuff here, update iprule_full_usage */
35 static const char keywords[] ALIGN1 =
36 	"from\0""to\0""preference\0""order\0""priority\0"
37 	"tos\0""fwmark\0""realms\0""table\0""lookup\0"
38 	"suppress_prefixlength\0""suppress_ifgroup\0"
39 	"dev\0""iif\0""nat\0""map-to\0""type\0""help\0"
40 	;
41 #define keyword_preference            (keywords           + sizeof("from") + sizeof("to"))
42 #define keyword_fwmark                (keyword_preference + sizeof("preference") + sizeof("order") + sizeof("priority") + sizeof("tos"))
43 #define keyword_realms                (keyword_fwmark     + sizeof("fwmark"))
44 #define keyword_suppress_prefixlength (keyword_realms     + sizeof("realms") + sizeof("table") + sizeof("lookup"))
45 #define keyword_suppress_ifgroup      (keyword_suppress_prefixlength + sizeof("suppress_prefixlength"))
46 enum {
47 	ARG_from = 1, ARG_to, ARG_preference, ARG_order, ARG_priority,
48 	ARG_tos, ARG_fwmark, ARG_realms, ARG_table, ARG_lookup,
49 	ARG_suppress_prefixlength, ARG_suppress_ifgroup,
50 	ARG_dev, ARG_iif, ARG_nat, ARG_map_to, ARG_type, ARG_help,
51 };
52 
print_rule(const struct sockaddr_nl * who UNUSED_PARAM,struct nlmsghdr * n,void * arg UNUSED_PARAM)53 static int FAST_FUNC print_rule(const struct sockaddr_nl *who UNUSED_PARAM,
54 					struct nlmsghdr *n, void *arg UNUSED_PARAM)
55 {
56 	struct rtmsg *r = NLMSG_DATA(n);
57 	int len = n->nlmsg_len;
58 	int host_len = -1;
59 	struct rtattr * tb[RTA_MAX+1];
60 
61 	if (n->nlmsg_type != RTM_NEWRULE)
62 		return 0;
63 
64 	len -= NLMSG_LENGTH(sizeof(*r));
65 	if (len < 0)
66 		return -1;
67 
68 	//memset(tb, 0, sizeof(tb)); - parse_rtattr does this
69 	parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
70 
71 	if (r->rtm_family == AF_INET)
72 		host_len = 32;
73 	else if (r->rtm_family == AF_INET6)
74 		host_len = 128;
75 /*	else if (r->rtm_family == AF_DECnet)
76 		host_len = 16;
77 	else if (r->rtm_family == AF_IPX)
78 		host_len = 80;
79 */
80 	printf("%u:\t", tb[RTA_PRIORITY] ?
81 					*(unsigned*)RTA_DATA(tb[RTA_PRIORITY])
82 					: 0);
83 	printf("from ");
84 	if (tb[RTA_SRC]) {
85 		if (r->rtm_src_len != host_len) {
86 			printf("%s/%u",
87 				rt_addr_n2a(r->rtm_family, RTA_DATA(tb[RTA_SRC])),
88 				r->rtm_src_len
89 			);
90 		} else {
91 			fputs_stdout(format_host(r->rtm_family,
92 						RTA_PAYLOAD(tb[RTA_SRC]),
93 						RTA_DATA(tb[RTA_SRC]))
94 			);
95 		}
96 	} else if (r->rtm_src_len) {
97 		printf("0/%d", r->rtm_src_len);
98 	} else {
99 		printf("all");
100 	}
101 	bb_putchar(' ');
102 
103 	if (tb[RTA_DST]) {
104 		if (r->rtm_dst_len != host_len) {
105 			printf("to %s/%u ", rt_addr_n2a(r->rtm_family,
106 							 RTA_DATA(tb[RTA_DST])),
107 				r->rtm_dst_len
108 				);
109 		} else {
110 			printf("to %s ", format_host(r->rtm_family,
111 						       RTA_PAYLOAD(tb[RTA_DST]),
112 						       RTA_DATA(tb[RTA_DST])));
113 		}
114 	} else if (r->rtm_dst_len) {
115 		printf("to 0/%d ", r->rtm_dst_len);
116 	}
117 
118 	if (r->rtm_tos) {
119 		printf("tos %s ", rtnl_dsfield_n2a(r->rtm_tos));
120 	}
121 
122 	if (tb[FRA_FWMARK] || tb[FRA_FWMASK]) {
123 		uint32_t mark = 0, mask = 0;
124 
125 		if (tb[FRA_FWMARK])
126 			mark = *(uint32_t*)RTA_DATA(tb[FRA_FWMARK]);
127 		if (tb[FRA_FWMASK]
128 		 && (mask = *(uint32_t*)RTA_DATA(tb[FRA_FWMASK])) != 0xFFFFFFFF
129 		)
130 			printf("fwmark %#x/%#x ", mark, mask);
131 		else
132 			printf("fwmark %#x ", mark);
133 	}
134 
135 	if (tb[RTA_IIF]) {
136 		printf("iif %s ", (char*)RTA_DATA(tb[RTA_IIF]));
137 	}
138 
139 #if HAVE_RTA_TABLE
140 	if (tb[RTA_TABLE])
141 		printf("lookup %s ", rtnl_rttable_n2a(*(uint32_t*)RTA_DATA(tb[RTA_TABLE])));
142 	else
143 #endif
144 	if (r->rtm_table)
145 		printf("lookup %s ", rtnl_rttable_n2a(r->rtm_table));
146 
147 	if (tb[FRA_SUPPRESS_PREFIXLEN]) {
148 		int pl = *(uint32_t*)RTA_DATA(tb[FRA_SUPPRESS_PREFIXLEN]);
149 		if (pl != -1)
150 			printf("%s %d ", keyword_suppress_prefixlength, pl);
151 	}
152 	if (tb[FRA_SUPPRESS_IFGROUP]) {
153 		int grp = *(uint32_t*)RTA_DATA(tb[FRA_SUPPRESS_IFGROUP]);
154 		if (grp != -1)
155 			printf("%s %d ", keyword_suppress_ifgroup, grp);
156 	}
157 
158 	if (tb[RTA_FLOW]) {
159 		uint32_t to = *(uint32_t*)RTA_DATA(tb[RTA_FLOW]);
160 		uint32_t from = to>>16;
161 		to &= 0xFFFF;
162 		if (from) {
163 			printf("realms %s/",
164 				rtnl_rtrealm_n2a(from));
165 		}
166 		printf("%s ",
167 			rtnl_rtrealm_n2a(to));
168 	}
169 
170 	if (r->rtm_type == RTN_NAT) {
171 		if (tb[RTA_GATEWAY]) {
172 			printf("map-to %s ",
173 				format_host(r->rtm_family,
174 					    RTA_PAYLOAD(tb[RTA_GATEWAY]),
175 					    RTA_DATA(tb[RTA_GATEWAY]))
176 			);
177 		} else
178 			printf("masquerade");
179 	} else if (r->rtm_type != RTN_UNICAST)
180 		fputs_stdout(rtnl_rtntype_n2a(r->rtm_type));
181 
182 	bb_putchar('\n');
183 	/*fflush_all();*/
184 	return 0;
185 }
186 
187 /* Return value becomes exitcode. It's okay to not return at all */
iprule_list(char ** argv)188 static int iprule_list(char **argv)
189 {
190 	struct rtnl_handle rth;
191 	int af = preferred_family;
192 
193 	if (af == AF_UNSPEC)
194 		af = AF_INET;
195 
196 	if (*argv) {
197 		//bb_error_msg("\"rule show\" needs no arguments");
198 		bb_warn_ignoring_args(*argv);
199 		return -1;
200 	}
201 
202 	xrtnl_open(&rth);
203 
204 	xrtnl_wilddump_request(&rth, af, RTM_GETRULE);
205 	xrtnl_dump_filter(&rth, print_rule, NULL);
206 
207 	return 0;
208 }
209 
210 /* Return value becomes exitcode. It's okay to not return at all */
iprule_modify(int cmd,char ** argv)211 static int iprule_modify(int cmd, char **argv)
212 {
213 	bool table_ok = 0;
214 	struct rtnl_handle rth;
215 	struct {
216 		struct nlmsghdr n;
217 		struct rtmsg    r;
218 		char            buf[1024];
219 	} req;
220 	smalluint key;
221 
222 	memset(&req, 0, sizeof(req));
223 
224 	req.n.nlmsg_type = cmd;
225 	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
226 	req.n.nlmsg_flags = NLM_F_REQUEST;
227 	req.r.rtm_family = preferred_family;
228 	req.r.rtm_protocol = RTPROT_BOOT;
229 	if (RT_SCOPE_UNIVERSE != 0)
230 		req.r.rtm_scope = RT_SCOPE_UNIVERSE;
231 	/*req.r.rtm_table = 0; - already is */
232 	if (RTN_UNSPEC != 0)
233 		req.r.rtm_type = RTN_UNSPEC;
234 
235 	if (cmd == RTM_NEWRULE) {
236 		req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
237 		req.r.rtm_type = RTN_UNICAST;
238 	}
239 
240 	while (*argv) {
241 		key = index_in_substrings(keywords, *argv) + 1;
242 		if (key == 0) /* no match found in keywords array, bail out. */
243 			invarg_1_to_2(*argv, applet_name);
244 		if (key == ARG_from) {
245 			inet_prefix dst;
246 			NEXT_ARG();
247 			get_prefix(&dst, *argv, req.r.rtm_family);
248 			req.r.rtm_src_len = dst.bitlen;
249 			addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen);
250 		} else if (key == ARG_to) {
251 			inet_prefix dst;
252 			NEXT_ARG();
253 			get_prefix(&dst, *argv, req.r.rtm_family);
254 			req.r.rtm_dst_len = dst.bitlen;
255 			addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
256 		} else if (key == ARG_preference ||
257 			   key == ARG_order ||
258 			   key == ARG_priority
259 		) {
260 			uint32_t pref;
261 			NEXT_ARG();
262 			pref = get_u32(*argv, keyword_preference);
263 			addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref);
264 		} else if (key == ARG_tos) {
265 			uint32_t tos;
266 			NEXT_ARG();
267 			if (rtnl_dsfield_a2n(&tos, *argv))
268 				invarg_1_to_2(*argv, "TOS");
269 			req.r.rtm_tos = tos;
270 		} else if (key == ARG_fwmark) {
271 			char *slash;
272 			uint32_t fwmark, fwmask;
273 			NEXT_ARG();
274 			slash = strchr(*argv, '/');
275 			if (slash)
276 				*slash = '\0';
277 			fwmark = get_u32(*argv, keyword_fwmark);
278 			addattr32(&req.n, sizeof(req), FRA_FWMARK, fwmark);
279 			if (slash) {
280 				fwmask = get_u32(slash + 1, "fwmask");
281 				addattr32(&req.n, sizeof(req), FRA_FWMASK, fwmask);
282 			}
283 		} else if (key == ARG_realms) {
284 			uint32_t realm;
285 			NEXT_ARG();
286 			if (get_rt_realms(&realm, *argv))
287 				invarg_1_to_2(*argv, keyword_realms);
288 			addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
289 		} else if (key == ARG_table ||
290 			   key == ARG_lookup
291 		) {
292 			uint32_t tid;
293 			NEXT_ARG();
294 			if (rtnl_rttable_a2n(&tid, *argv))
295 				invarg_1_to_2(*argv, "table ID");
296 
297 #if HAVE_RTA_TABLE
298 			if (tid > 255) {
299 				req.r.rtm_table = RT_TABLE_UNSPEC;
300 				addattr32(&req.n, sizeof(req), RTA_TABLE, tid);
301 			} else
302 #endif
303 				req.r.rtm_table = tid;
304 
305 			table_ok = 1;
306 		} else if (key == ARG_suppress_prefixlength) {
307 			int prefix_length;
308 			NEXT_ARG();
309 			prefix_length = get_u32(*argv, keyword_suppress_prefixlength);
310 			addattr32(&req.n, sizeof(req), FRA_SUPPRESS_PREFIXLEN, prefix_length);
311 		} else if (key == ARG_suppress_ifgroup) {
312 			int grp;
313 			NEXT_ARG();
314 			grp = get_u32(*argv, keyword_suppress_ifgroup);
315 			addattr32(&req.n, sizeof(req), FRA_SUPPRESS_IFGROUP, grp);
316 		} else if (key == ARG_dev ||
317 			   key == ARG_iif
318 		) {
319 			NEXT_ARG();
320 			addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1);
321 		} else if (key == ARG_nat ||
322 			   key == ARG_map_to
323 		) {
324 			NEXT_ARG();
325 			addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv));
326 			req.r.rtm_type = RTN_NAT;
327 		} else {
328 			int type;
329 
330 			if (key == ARG_type) {
331 				NEXT_ARG();
332 			}
333 			if (key == ARG_help)
334 				bb_show_usage();
335 			if (rtnl_rtntype_a2n(&type, *argv))
336 				invarg_1_to_2(*argv, "type");
337 			req.r.rtm_type = type;
338 		}
339 		argv++;
340 	}
341 
342 	if (req.r.rtm_family == AF_UNSPEC)
343 		req.r.rtm_family = AF_INET;
344 
345 	if (!table_ok && cmd == RTM_NEWRULE)
346 		req.r.rtm_table = RT_TABLE_MAIN;
347 
348 	xrtnl_open(&rth);
349 
350 	if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
351 		return 2;
352 
353 	return 0;
354 }
355 
356 /* Return value becomes exitcode. It's okay to not return at all */
do_iprule(char ** argv)357 int FAST_FUNC do_iprule(char **argv)
358 {
359 	static const char ip_rule_commands[] ALIGN1 =
360 		"add\0""delete\0""list\0""show\0";
361 	if (*argv) {
362 		int cmd = index_in_substrings(ip_rule_commands, *argv);
363 		if (cmd < 0)
364 			invarg_1_to_2(*argv, applet_name);
365 		argv++;
366 		if (cmd < 2)
367 			return iprule_modify((cmd == 0) ? RTM_NEWRULE : RTM_DELRULE, argv);
368 	}
369 	return iprule_list(argv);
370 }
371