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 
16 #include <netinet/in.h>
17 #include <netinet/ip.h>
18 #include <arpa/inet.h>
19 
20 #include "ip_common.h"  /* #include "libbb.h" is inside */
21 #include "rt_names.h"
22 #include "utils.h"
23 
24 /*
25 static void usage(void) __attribute__((noreturn));
26 
27 static void usage(void)
28 {
29 	fprintf(stderr, "Usage: ip rule [ list | add | del ] SELECTOR ACTION\n");
30 	fprintf(stderr, "SELECTOR := [ from PREFIX ] [ to PREFIX ] [ tos TOS ] [ fwmark FWMARK ]\n");
31 	fprintf(stderr, "            [ dev STRING ] [ pref NUMBER ]\n");
32 	fprintf(stderr, "ACTION := [ table TABLE_ID ] [ nat ADDRESS ]\n");
33 	fprintf(stderr, "          [ prohibit | reject | unreachable ]\n");
34 	fprintf(stderr, "          [ realms [SRCREALM/]DSTREALM ]\n");
35 	fprintf(stderr, "TABLE_ID := [ local | main | default | NUMBER ]\n");
36 	exit(-1);
37 }
38 */
39 
print_rule(const struct sockaddr_nl * who UNUSED_PARAM,struct nlmsghdr * n,void * arg UNUSED_PARAM)40 static int FAST_FUNC print_rule(const struct sockaddr_nl *who UNUSED_PARAM,
41 					struct nlmsghdr *n, void *arg UNUSED_PARAM)
42 {
43 	struct rtmsg *r = NLMSG_DATA(n);
44 	int len = n->nlmsg_len;
45 	int host_len = -1;
46 	struct rtattr * tb[RTA_MAX+1];
47 
48 	if (n->nlmsg_type != RTM_NEWRULE)
49 		return 0;
50 
51 	len -= NLMSG_LENGTH(sizeof(*r));
52 	if (len < 0)
53 		return -1;
54 
55 	memset(tb, 0, sizeof(tb));
56 	parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
57 
58 	if (r->rtm_family == AF_INET)
59 		host_len = 32;
60 	else if (r->rtm_family == AF_INET6)
61 		host_len = 128;
62 /*	else if (r->rtm_family == AF_DECnet)
63 		host_len = 16;
64 	else if (r->rtm_family == AF_IPX)
65 		host_len = 80;
66 */
67 	printf("%u:\t", tb[RTA_PRIORITY] ?
68 					*(unsigned*)RTA_DATA(tb[RTA_PRIORITY])
69 					: 0);
70 	printf("from ");
71 	if (tb[RTA_SRC]) {
72 		if (r->rtm_src_len != host_len) {
73 			printf("%s/%u",
74 				rt_addr_n2a(r->rtm_family, RTA_DATA(tb[RTA_SRC])),
75 				r->rtm_src_len
76 			);
77 		} else {
78 			fputs(format_host(r->rtm_family,
79 						RTA_PAYLOAD(tb[RTA_SRC]),
80 						RTA_DATA(tb[RTA_SRC])),
81 				stdout
82 			);
83 		}
84 	} else if (r->rtm_src_len) {
85 		printf("0/%d", r->rtm_src_len);
86 	} else {
87 		printf("all");
88 	}
89 	bb_putchar(' ');
90 
91 	if (tb[RTA_DST]) {
92 		if (r->rtm_dst_len != host_len) {
93 			printf("to %s/%u ", rt_addr_n2a(r->rtm_family,
94 							 RTA_DATA(tb[RTA_DST])),
95 				r->rtm_dst_len
96 				);
97 		} else {
98 			printf("to %s ", format_host(r->rtm_family,
99 						       RTA_PAYLOAD(tb[RTA_DST]),
100 						       RTA_DATA(tb[RTA_DST])));
101 		}
102 	} else if (r->rtm_dst_len) {
103 		printf("to 0/%d ", r->rtm_dst_len);
104 	}
105 
106 	if (r->rtm_tos) {
107 		printf("tos %s ", rtnl_dsfield_n2a(r->rtm_tos));
108 	}
109 	if (tb[RTA_PROTOINFO]) {
110 		printf("fwmark %#x ", *(uint32_t*)RTA_DATA(tb[RTA_PROTOINFO]));
111 	}
112 
113 	if (tb[RTA_IIF]) {
114 		printf("iif %s ", (char*)RTA_DATA(tb[RTA_IIF]));
115 	}
116 
117 	if (tb[RTA_TABLE])
118 		printf("lookup %s ", rtnl_rttable_n2a(*(uint32_t*)RTA_DATA(tb[RTA_TABLE])));
119 	else if (r->rtm_table)
120 		printf("lookup %s ", rtnl_rttable_n2a(r->rtm_table));
121 
122 	if (tb[RTA_FLOW]) {
123 		uint32_t to = *(uint32_t*)RTA_DATA(tb[RTA_FLOW]);
124 		uint32_t from = to>>16;
125 		to &= 0xFFFF;
126 		if (from) {
127 			printf("realms %s/",
128 				rtnl_rtrealm_n2a(from));
129 		}
130 		printf("%s ",
131 			rtnl_rtrealm_n2a(to));
132 	}
133 
134 	if (r->rtm_type == RTN_NAT) {
135 		if (tb[RTA_GATEWAY]) {
136 			printf("map-to %s ",
137 				format_host(r->rtm_family,
138 					    RTA_PAYLOAD(tb[RTA_GATEWAY]),
139 					    RTA_DATA(tb[RTA_GATEWAY]))
140 			);
141 		} else
142 			printf("masquerade");
143 	} else if (r->rtm_type != RTN_UNICAST)
144 		fputs(rtnl_rtntype_n2a(r->rtm_type), stdout);
145 
146 	bb_putchar('\n');
147 	/*fflush_all();*/
148 	return 0;
149 }
150 
151 /* Return value becomes exitcode. It's okay to not return at all */
iprule_list(char ** argv)152 static int iprule_list(char **argv)
153 {
154 	struct rtnl_handle rth;
155 	int af = preferred_family;
156 
157 	if (af == AF_UNSPEC)
158 		af = AF_INET;
159 
160 	if (*argv) {
161 		//bb_error_msg("\"rule show\" needs no arguments");
162 		bb_warn_ignoring_args(*argv);
163 		return -1;
164 	}
165 
166 	xrtnl_open(&rth);
167 
168 	xrtnl_wilddump_request(&rth, af, RTM_GETRULE);
169 	xrtnl_dump_filter(&rth, print_rule, NULL);
170 
171 	return 0;
172 }
173 
174 /* Return value becomes exitcode. It's okay to not return at all */
iprule_modify(int cmd,char ** argv)175 static int iprule_modify(int cmd, char **argv)
176 {
177 	static const char keywords[] ALIGN1 =
178 		"from\0""to\0""preference\0""order\0""priority\0"
179 		"tos\0""fwmark\0""realms\0""table\0""lookup\0""dev\0"
180 		"iif\0""nat\0""map-to\0""type\0""help\0";
181 	enum {
182 		ARG_from = 1, ARG_to, ARG_preference, ARG_order, ARG_priority,
183 		ARG_tos, ARG_fwmark, ARG_realms, ARG_table, ARG_lookup, ARG_dev,
184 		ARG_iif, ARG_nat, ARG_map_to, ARG_type, ARG_help
185 	};
186 	bool table_ok = 0;
187 	struct rtnl_handle rth;
188 	struct {
189 		struct nlmsghdr n;
190 		struct rtmsg    r;
191 		char            buf[1024];
192 	} req;
193 	smalluint key;
194 
195 	memset(&req, 0, sizeof(req));
196 
197 	req.n.nlmsg_type = cmd;
198 	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
199 	req.n.nlmsg_flags = NLM_F_REQUEST;
200 	req.r.rtm_family = preferred_family;
201 	req.r.rtm_protocol = RTPROT_BOOT;
202 	if (RT_SCOPE_UNIVERSE != 0)
203 		req.r.rtm_scope = RT_SCOPE_UNIVERSE;
204 	/*req.r.rtm_table = 0; - already is */
205 	if (RTN_UNSPEC != 0)
206 		req.r.rtm_type = RTN_UNSPEC;
207 
208 	if (cmd == RTM_NEWRULE) {
209 		req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
210 		req.r.rtm_type = RTN_UNICAST;
211 	}
212 
213 	while (*argv) {
214 		key = index_in_substrings(keywords, *argv) + 1;
215 		if (key == 0) /* no match found in keywords array, bail out. */
216 			invarg_1_to_2(*argv, applet_name);
217 		if (key == ARG_from) {
218 			inet_prefix dst;
219 			NEXT_ARG();
220 			get_prefix(&dst, *argv, req.r.rtm_family);
221 			req.r.rtm_src_len = dst.bitlen;
222 			addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen);
223 		} else if (key == ARG_to) {
224 			inet_prefix dst;
225 			NEXT_ARG();
226 			get_prefix(&dst, *argv, req.r.rtm_family);
227 			req.r.rtm_dst_len = dst.bitlen;
228 			addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
229 		} else if (key == ARG_preference ||
230 			   key == ARG_order ||
231 			   key == ARG_priority
232 		) {
233 			uint32_t pref;
234 			NEXT_ARG();
235 			pref = get_u32(*argv, "preference");
236 			addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref);
237 		} else if (key == ARG_tos) {
238 			uint32_t tos;
239 			NEXT_ARG();
240 			if (rtnl_dsfield_a2n(&tos, *argv))
241 				invarg_1_to_2(*argv, "TOS");
242 			req.r.rtm_tos = tos;
243 		} else if (key == ARG_fwmark) {
244 			uint32_t fwmark;
245 			NEXT_ARG();
246 			fwmark = get_u32(*argv, "fwmark");
247 			addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark);
248 		} else if (key == ARG_realms) {
249 			uint32_t realm;
250 			NEXT_ARG();
251 			if (get_rt_realms(&realm, *argv))
252 				invarg_1_to_2(*argv, "realms");
253 			addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
254 		} else if (key == ARG_table ||
255 			   key == ARG_lookup
256 		) {
257 			uint32_t tid;
258 			NEXT_ARG();
259 			if (rtnl_rttable_a2n(&tid, *argv))
260 				invarg_1_to_2(*argv, "table ID");
261 			if (tid < 256)
262 				req.r.rtm_table = tid;
263 			else {
264 				req.r.rtm_table = RT_TABLE_UNSPEC;
265 				addattr32(&req.n, sizeof(req), RTA_TABLE, tid);
266 			}
267 			table_ok = 1;
268 		} else if (key == ARG_dev ||
269 			   key == ARG_iif
270 		) {
271 			NEXT_ARG();
272 			addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1);
273 		} else if (key == ARG_nat ||
274 			   key == ARG_map_to
275 		) {
276 			NEXT_ARG();
277 			addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv));
278 			req.r.rtm_type = RTN_NAT;
279 		} else {
280 			int type;
281 
282 			if (key == ARG_type) {
283 				NEXT_ARG();
284 			}
285 			if (key == ARG_help)
286 				bb_show_usage();
287 			if (rtnl_rtntype_a2n(&type, *argv))
288 				invarg_1_to_2(*argv, "type");
289 			req.r.rtm_type = type;
290 		}
291 		argv++;
292 	}
293 
294 	if (req.r.rtm_family == AF_UNSPEC)
295 		req.r.rtm_family = AF_INET;
296 
297 	if (!table_ok && cmd == RTM_NEWRULE)
298 		req.r.rtm_table = RT_TABLE_MAIN;
299 
300 	xrtnl_open(&rth);
301 
302 	if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
303 		return 2;
304 
305 	return 0;
306 }
307 
308 /* Return value becomes exitcode. It's okay to not return at all */
do_iprule(char ** argv)309 int FAST_FUNC do_iprule(char **argv)
310 {
311 	static const char ip_rule_commands[] ALIGN1 =
312 		"add\0""delete\0""list\0""show\0";
313 	if (*argv) {
314 		int cmd = index_in_substrings(ip_rule_commands, *argv);
315 		if (cmd < 0)
316 			invarg_1_to_2(*argv, applet_name);
317 		argv++;
318 		if (cmd < 2)
319 			return iprule_modify((cmd == 0) ? RTM_NEWRULE : RTM_DELRULE, argv);
320 	}
321 	return iprule_list(argv);
322 }
323