diff options
Diffstat (limited to 'networking/libiproute/iprule.c')
-rw-r--r-- | networking/libiproute/iprule.c | 331 |
1 files changed, 331 insertions, 0 deletions
diff --git a/networking/libiproute/iprule.c b/networking/libiproute/iprule.c new file mode 100644 index 000000000..19338770f --- /dev/null +++ b/networking/libiproute/iprule.c | |||
@@ -0,0 +1,331 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * iprule.c "ip rule". | ||
4 | * | ||
5 | * This program is free software; you can redistribute it and/or | ||
6 | * modify it under the terms of the GNU General Public License | ||
7 | * as published by the Free Software Foundation; either version | ||
8 | * 2 of the License, or (at your option) any later version. | ||
9 | * | ||
10 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> | ||
11 | * | ||
12 | * | ||
13 | * Changes: | ||
14 | * | ||
15 | * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses | ||
16 | */ | ||
17 | |||
18 | #include "libbb.h" | ||
19 | #include <syslog.h> | ||
20 | #include <sys/socket.h> | ||
21 | #include <netinet/in.h> | ||
22 | #include <netinet/ip.h> | ||
23 | #include <arpa/inet.h> | ||
24 | |||
25 | #include "rt_names.h" | ||
26 | #include "utils.h" | ||
27 | /* | ||
28 | static void usage(void) __attribute__((noreturn)); | ||
29 | |||
30 | static void usage(void) | ||
31 | { | ||
32 | fprintf(stderr, "Usage: ip rule [ list | add | del ] SELECTOR ACTION\n"); | ||
33 | fprintf(stderr, "SELECTOR := [ from PREFIX ] [ to PREFIX ] [ tos TOS ] [ fwmark FWMARK ]\n"); | ||
34 | fprintf(stderr, " [ dev STRING ] [ pref NUMBER ]\n"); | ||
35 | fprintf(stderr, "ACTION := [ table TABLE_ID ] [ nat ADDRESS ]\n"); | ||
36 | fprintf(stderr, " [ prohibit | reject | unreachable ]\n"); | ||
37 | fprintf(stderr, " [ realms [SRCREALM/]DSTREALM ]\n"); | ||
38 | fprintf(stderr, "TABLE_ID := [ local | main | default | NUMBER ]\n"); | ||
39 | exit(-1); | ||
40 | } | ||
41 | */ | ||
42 | static int print_rule(struct sockaddr_nl *who ATTRIBUTE_UNUSED, | ||
43 | struct nlmsghdr *n, void *arg) | ||
44 | { | ||
45 | FILE *fp = (FILE*)arg; | ||
46 | struct rtmsg *r = NLMSG_DATA(n); | ||
47 | int len = n->nlmsg_len; | ||
48 | int host_len = -1; | ||
49 | struct rtattr * tb[RTA_MAX+1]; | ||
50 | char abuf[256]; | ||
51 | SPRINT_BUF(b1); | ||
52 | |||
53 | if (n->nlmsg_type != RTM_NEWRULE) | ||
54 | return 0; | ||
55 | |||
56 | len -= NLMSG_LENGTH(sizeof(*r)); | ||
57 | if (len < 0) | ||
58 | return -1; | ||
59 | |||
60 | memset(tb, 0, sizeof(tb)); | ||
61 | parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len); | ||
62 | |||
63 | if (r->rtm_family == AF_INET) | ||
64 | host_len = 32; | ||
65 | else if (r->rtm_family == AF_INET6) | ||
66 | host_len = 128; | ||
67 | /* else if (r->rtm_family == AF_DECnet) | ||
68 | host_len = 16; | ||
69 | else if (r->rtm_family == AF_IPX) | ||
70 | host_len = 80; | ||
71 | */ | ||
72 | if (tb[RTA_PRIORITY]) | ||
73 | fprintf(fp, "%u:\t", *(unsigned*)RTA_DATA(tb[RTA_PRIORITY])); | ||
74 | else | ||
75 | fprintf(fp, "0:\t"); | ||
76 | |||
77 | fprintf(fp, "from "); | ||
78 | if (tb[RTA_SRC]) { | ||
79 | if (r->rtm_src_len != host_len) { | ||
80 | fprintf(fp, "%s/%u", rt_addr_n2a(r->rtm_family, | ||
81 | RTA_PAYLOAD(tb[RTA_SRC]), | ||
82 | RTA_DATA(tb[RTA_SRC]), | ||
83 | abuf, sizeof(abuf)), | ||
84 | r->rtm_src_len | ||
85 | ); | ||
86 | } else { | ||
87 | fprintf(fp, "%s", format_host(r->rtm_family, | ||
88 | RTA_PAYLOAD(tb[RTA_SRC]), | ||
89 | RTA_DATA(tb[RTA_SRC]), | ||
90 | abuf, sizeof(abuf)) | ||
91 | ); | ||
92 | } | ||
93 | } else if (r->rtm_src_len) { | ||
94 | fprintf(fp, "0/%d", r->rtm_src_len); | ||
95 | } else { | ||
96 | fprintf(fp, "all"); | ||
97 | } | ||
98 | fprintf(fp, " "); | ||
99 | |||
100 | if (tb[RTA_DST]) { | ||
101 | if (r->rtm_dst_len != host_len) { | ||
102 | fprintf(fp, "to %s/%u ", rt_addr_n2a(r->rtm_family, | ||
103 | RTA_PAYLOAD(tb[RTA_DST]), | ||
104 | RTA_DATA(tb[RTA_DST]), | ||
105 | abuf, sizeof(abuf)), | ||
106 | r->rtm_dst_len | ||
107 | ); | ||
108 | } else { | ||
109 | fprintf(fp, "to %s ", format_host(r->rtm_family, | ||
110 | RTA_PAYLOAD(tb[RTA_DST]), | ||
111 | RTA_DATA(tb[RTA_DST]), | ||
112 | abuf, sizeof(abuf))); | ||
113 | } | ||
114 | } else if (r->rtm_dst_len) { | ||
115 | fprintf(fp, "to 0/%d ", r->rtm_dst_len); | ||
116 | } | ||
117 | |||
118 | if (r->rtm_tos) { | ||
119 | fprintf(fp, "tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1))); | ||
120 | } | ||
121 | if (tb[RTA_PROTOINFO]) { | ||
122 | fprintf(fp, "fwmark %#x ", *(__u32*)RTA_DATA(tb[RTA_PROTOINFO])); | ||
123 | } | ||
124 | |||
125 | if (tb[RTA_IIF]) { | ||
126 | fprintf(fp, "iif %s ", (char*)RTA_DATA(tb[RTA_IIF])); | ||
127 | } | ||
128 | |||
129 | if (r->rtm_table) | ||
130 | fprintf(fp, "lookup %s ", rtnl_rttable_n2a(r->rtm_table, b1, sizeof(b1))); | ||
131 | |||
132 | if (tb[RTA_FLOW]) { | ||
133 | __u32 to = *(__u32*)RTA_DATA(tb[RTA_FLOW]); | ||
134 | __u32 from = to>>16; | ||
135 | to &= 0xFFFF; | ||
136 | if (from) { | ||
137 | fprintf(fp, "realms %s/", | ||
138 | rtnl_rtrealm_n2a(from, b1, sizeof(b1))); | ||
139 | } | ||
140 | fprintf(fp, "%s ", | ||
141 | rtnl_rtrealm_n2a(to, b1, sizeof(b1))); | ||
142 | } | ||
143 | |||
144 | if (r->rtm_type == RTN_NAT) { | ||
145 | if (tb[RTA_GATEWAY]) { | ||
146 | fprintf(fp, "map-to %s ", | ||
147 | format_host(r->rtm_family, | ||
148 | RTA_PAYLOAD(tb[RTA_GATEWAY]), | ||
149 | RTA_DATA(tb[RTA_GATEWAY]), | ||
150 | abuf, sizeof(abuf))); | ||
151 | } else | ||
152 | fprintf(fp, "masquerade"); | ||
153 | } else if (r->rtm_type != RTN_UNICAST) | ||
154 | fprintf(fp, "%s", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1))); | ||
155 | |||
156 | fprintf(fp, "\n"); | ||
157 | fflush(fp); | ||
158 | return 0; | ||
159 | } | ||
160 | |||
161 | int iprule_list(int argc, char **argv) | ||
162 | { | ||
163 | struct rtnl_handle rth; | ||
164 | int af = preferred_family; | ||
165 | |||
166 | if (af == AF_UNSPEC) | ||
167 | af = AF_INET; | ||
168 | |||
169 | if (argc > 0) { | ||
170 | bb_error_msg("\"rule show\" needs no arguments"); | ||
171 | return -1; | ||
172 | } | ||
173 | |||
174 | if (rtnl_open(&rth, 0) < 0) | ||
175 | return 1; | ||
176 | |||
177 | if (rtnl_wilddump_request(&rth, af, RTM_GETRULE) < 0) { | ||
178 | bb_perror_msg("Cannot send dump request"); | ||
179 | return 1; | ||
180 | } | ||
181 | |||
182 | if (rtnl_dump_filter(&rth, print_rule, stdout, NULL, NULL) < 0) { | ||
183 | bb_error_msg("Dump terminated"); | ||
184 | return 1; | ||
185 | } | ||
186 | |||
187 | return 0; | ||
188 | } | ||
189 | |||
190 | |||
191 | int iprule_modify(int cmd, int argc, char **argv) | ||
192 | { | ||
193 | int table_ok = 0; | ||
194 | struct rtnl_handle rth; | ||
195 | struct { | ||
196 | struct nlmsghdr n; | ||
197 | struct rtmsg r; | ||
198 | char buf[1024]; | ||
199 | } req; | ||
200 | |||
201 | memset(&req, 0, sizeof(req)); | ||
202 | |||
203 | req.n.nlmsg_type = cmd; | ||
204 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); | ||
205 | req.n.nlmsg_flags = NLM_F_REQUEST; | ||
206 | req.r.rtm_family = preferred_family; | ||
207 | req.r.rtm_protocol = RTPROT_BOOT; | ||
208 | req.r.rtm_scope = RT_SCOPE_UNIVERSE; | ||
209 | req.r.rtm_table = 0; | ||
210 | req.r.rtm_type = RTN_UNSPEC; | ||
211 | |||
212 | if (cmd == RTM_NEWRULE) { | ||
213 | req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL; | ||
214 | req.r.rtm_type = RTN_UNICAST; | ||
215 | } | ||
216 | |||
217 | while (argc > 0) { | ||
218 | if (strcmp(*argv, "from") == 0) { | ||
219 | inet_prefix dst; | ||
220 | NEXT_ARG(); | ||
221 | get_prefix(&dst, *argv, req.r.rtm_family); | ||
222 | req.r.rtm_src_len = dst.bitlen; | ||
223 | addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen); | ||
224 | } else if (strcmp(*argv, "to") == 0) { | ||
225 | inet_prefix dst; | ||
226 | NEXT_ARG(); | ||
227 | get_prefix(&dst, *argv, req.r.rtm_family); | ||
228 | req.r.rtm_dst_len = dst.bitlen; | ||
229 | addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen); | ||
230 | } else if (matches(*argv, "preference") == 0 || | ||
231 | matches(*argv, "order") == 0 || | ||
232 | matches(*argv, "priority") == 0) { | ||
233 | __u32 pref; | ||
234 | NEXT_ARG(); | ||
235 | if (get_u32(&pref, *argv, 0)) | ||
236 | invarg("preference value", *argv); | ||
237 | addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref); | ||
238 | } else if (strcmp(*argv, "tos") == 0) { | ||
239 | __u32 tos; | ||
240 | NEXT_ARG(); | ||
241 | if (rtnl_dsfield_a2n(&tos, *argv)) | ||
242 | invarg("TOS value", *argv); | ||
243 | req.r.rtm_tos = tos; | ||
244 | } else if (strcmp(*argv, "fwmark") == 0) { | ||
245 | __u32 fwmark; | ||
246 | NEXT_ARG(); | ||
247 | if (get_u32(&fwmark, *argv, 0)) | ||
248 | invarg("fwmark value", *argv); | ||
249 | addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark); | ||
250 | } else if (matches(*argv, "realms") == 0) { | ||
251 | __u32 realm; | ||
252 | NEXT_ARG(); | ||
253 | if (get_rt_realms(&realm, *argv)) | ||
254 | invarg("realms", *argv); | ||
255 | addattr32(&req.n, sizeof(req), RTA_FLOW, realm); | ||
256 | } else if (matches(*argv, "table") == 0 || | ||
257 | strcmp(*argv, "lookup") == 0) { | ||
258 | int tid; | ||
259 | NEXT_ARG(); | ||
260 | if (rtnl_rttable_a2n(&tid, *argv)) | ||
261 | invarg("table ID", *argv); | ||
262 | req.r.rtm_table = tid; | ||
263 | table_ok = 1; | ||
264 | } else if (strcmp(*argv, "dev") == 0 || | ||
265 | strcmp(*argv, "iif") == 0) { | ||
266 | NEXT_ARG(); | ||
267 | addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1); | ||
268 | } else if (strcmp(*argv, "nat") == 0 || | ||
269 | matches(*argv, "map-to") == 0) { | ||
270 | NEXT_ARG(); | ||
271 | addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv)); | ||
272 | req.r.rtm_type = RTN_NAT; | ||
273 | } else { | ||
274 | int type; | ||
275 | |||
276 | if (strcmp(*argv, "type") == 0) { | ||
277 | NEXT_ARG(); | ||
278 | } | ||
279 | if (matches(*argv, "help") == 0) | ||
280 | bb_show_usage(); | ||
281 | if (rtnl_rtntype_a2n(&type, *argv)) | ||
282 | invarg("Failed to parse rule type", *argv); | ||
283 | req.r.rtm_type = type; | ||
284 | } | ||
285 | argc--; | ||
286 | argv++; | ||
287 | } | ||
288 | |||
289 | if (req.r.rtm_family == AF_UNSPEC) | ||
290 | req.r.rtm_family = AF_INET; | ||
291 | |||
292 | if (!table_ok && cmd == RTM_NEWRULE) | ||
293 | req.r.rtm_table = RT_TABLE_MAIN; | ||
294 | |||
295 | if (rtnl_open(&rth, 0) < 0) | ||
296 | return 1; | ||
297 | |||
298 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) | ||
299 | return 2; | ||
300 | |||
301 | return 0; | ||
302 | } | ||
303 | |||
304 | int do_iprule(int argc, char **argv) | ||
305 | { | ||
306 | static const char * const ip_rule_commands[] = | ||
307 | {"add", "delete", "list", "show", 0}; | ||
308 | int command_num = 2; | ||
309 | int cmd; | ||
310 | |||
311 | if (argc < 1) | ||
312 | return iprule_list(0, NULL); | ||
313 | if (*argv) | ||
314 | command_num = index_in_substr_array(ip_rule_commands, *argv); | ||
315 | switch (command_num) { | ||
316 | case 0: /* add */ | ||
317 | cmd = RTM_NEWRULE; | ||
318 | break; | ||
319 | case 1: /* delete */ | ||
320 | cmd = RTM_DELRULE; | ||
321 | break; | ||
322 | case 2: /* list */ | ||
323 | case 3: /* show */ | ||
324 | return iprule_list(argc-1, argv+1); | ||
325 | break; | ||
326 | default: | ||
327 | bb_error_msg_and_die("unknown command %s", *argv); | ||
328 | } | ||
329 | return iprule_modify(cmd, argc-1, argv+1); | ||
330 | } | ||
331 | |||