]> rtime.felk.cvut.cz Git - lisovros/iproute2_canprio.git/blob - ip/iprule.c
Eliminate trailing whitespace
[lisovros/iproute2_canprio.git] / ip / iprule.c
1 /*
2  * iprule.c             "ip rule".
3  *
4  *              This program is free software; you can redistribute it and/or
5  *              modify it under the terms of the GNU General Public License
6  *              as published by the Free Software Foundation; either version
7  *              2 of the License, or (at your option) any later version.
8  *
9  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10  *
11  *
12  * Changes:
13  *
14  * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <syslog.h>
21 #include <fcntl.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <netinet/ip.h>
25 #include <arpa/inet.h>
26 #include <string.h>
27 #include <linux/fib_rules.h>
28
29 #include "rt_names.h"
30 #include "utils.h"
31 #include "ip_common.h"
32
33 extern struct rtnl_handle rth;
34
35 static void usage(void) __attribute__((noreturn));
36
37 static void usage(void)
38 {
39         fprintf(stderr, "Usage: ip rule [ list | add | del | flush ] SELECTOR ACTION\n");
40         fprintf(stderr, "SELECTOR := [ not ] [ from PREFIX ] [ to PREFIX ] [ tos TOS ] [ fwmark FWMARK[/MASK] ]\n");
41         fprintf(stderr, "            [ dev STRING ] [ pref NUMBER ]\n");
42         fprintf(stderr, "ACTION := [ table TABLE_ID ]\n");
43         fprintf(stderr, "          [ prohibit | reject | unreachable ]\n");
44         fprintf(stderr, "          [ realms [SRCREALM/]DSTREALM ]\n");
45         fprintf(stderr, "TABLE_ID := [ local | main | default | NUMBER ]\n");
46         exit(-1);
47 }
48
49 int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
50 {
51         FILE *fp = (FILE*)arg;
52         struct rtmsg *r = NLMSG_DATA(n);
53         int len = n->nlmsg_len;
54         int host_len = -1;
55         __u32 table;
56         struct rtattr * tb[RTA_MAX+1];
57         char abuf[256];
58         SPRINT_BUF(b1);
59
60         if (n->nlmsg_type != RTM_NEWRULE && n->nlmsg_type != RTM_DELRULE)
61                 return 0;
62
63         len -= NLMSG_LENGTH(sizeof(*r));
64         if (len < 0)
65                 return -1;
66
67         parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
68
69         if (r->rtm_family == AF_INET)
70                 host_len = 32;
71         else if (r->rtm_family == AF_INET6)
72                 host_len = 128;
73         else if (r->rtm_family == AF_DECnet)
74                 host_len = 16;
75         else if (r->rtm_family == AF_IPX)
76                 host_len = 80;
77
78         if (n->nlmsg_type == RTM_DELRULE)
79                 fprintf(fp, "Deleted ");
80
81         if (tb[RTA_PRIORITY])
82                 fprintf(fp, "%u:\t", *(unsigned*)RTA_DATA(tb[RTA_PRIORITY]));
83         else
84                 fprintf(fp, "0:\t");
85
86         if (r->rtm_flags & FIB_RULE_INVERT)
87                 fprintf(fp, "not ");
88
89         if (tb[RTA_SRC]) {
90                 if (r->rtm_src_len != host_len) {
91                         fprintf(fp, "from %s/%u ", rt_addr_n2a(r->rtm_family,
92                                                          RTA_PAYLOAD(tb[RTA_SRC]),
93                                                          RTA_DATA(tb[RTA_SRC]),
94                                                          abuf, sizeof(abuf)),
95                                 r->rtm_src_len
96                                 );
97                 } else {
98                         fprintf(fp, "from %s ", format_host(r->rtm_family,
99                                                        RTA_PAYLOAD(tb[RTA_SRC]),
100                                                        RTA_DATA(tb[RTA_SRC]),
101                                                        abuf, sizeof(abuf))
102                                 );
103                 }
104         } else if (r->rtm_src_len) {
105                 fprintf(fp, "from 0/%d ", r->rtm_src_len);
106         } else {
107                 fprintf(fp, "from all ");
108         }
109
110         if (tb[RTA_DST]) {
111                 if (r->rtm_dst_len != host_len) {
112                         fprintf(fp, "to %s/%u ", rt_addr_n2a(r->rtm_family,
113                                                          RTA_PAYLOAD(tb[RTA_DST]),
114                                                          RTA_DATA(tb[RTA_DST]),
115                                                          abuf, sizeof(abuf)),
116                                 r->rtm_dst_len
117                                 );
118                 } else {
119                         fprintf(fp, "to %s ", format_host(r->rtm_family,
120                                                        RTA_PAYLOAD(tb[RTA_DST]),
121                                                        RTA_DATA(tb[RTA_DST]),
122                                                        abuf, sizeof(abuf)));
123                 }
124         } else if (r->rtm_dst_len) {
125                 fprintf(fp, "to 0/%d ", r->rtm_dst_len);
126         }
127
128         if (r->rtm_tos) {
129                 SPRINT_BUF(b1);
130                 fprintf(fp, "tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));
131         }
132         if (tb[RTA_PROTOINFO] || tb[RTA_FWMASK]) {
133                 __u32 mark = 0, mask = 0;
134
135                 if (tb[RTA_PROTOINFO])
136                         mark = *(__u32*)RTA_DATA(tb[RTA_PROTOINFO]);
137
138                 if (tb[RTA_FWMASK] &&
139                     (mask = *(__u32*)RTA_DATA(tb[RTA_FWMASK])) != 0xFFFFFFFF)
140                         fprintf(fp, "fwmark 0x%x/0x%x ", mark, mask);
141                 else
142                         fprintf(fp, "fwmark 0x%x ", mark);
143         }
144
145         if (tb[RTA_IIF]) {
146                 fprintf(fp, "iif %s ", (char*)RTA_DATA(tb[RTA_IIF]));
147         }
148
149         table = rtm_get_table(r, tb);
150         if (table)
151                 fprintf(fp, "lookup %s ", rtnl_rttable_n2a(table, b1, sizeof(b1)));
152
153         if (tb[RTA_FLOW]) {
154                 __u32 to = *(__u32*)RTA_DATA(tb[RTA_FLOW]);
155                 __u32 from = to>>16;
156                 to &= 0xFFFF;
157                 if (from) {
158                         fprintf(fp, "realms %s/",
159                                 rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
160                 }
161                 fprintf(fp, "%s ",
162                         rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
163         }
164
165         if (r->rtm_type == RTN_NAT) {
166                 if (tb[RTA_GATEWAY]) {
167                         fprintf(fp, "map-to %s ",
168                                 format_host(r->rtm_family,
169                                             RTA_PAYLOAD(tb[RTA_GATEWAY]),
170                                             RTA_DATA(tb[RTA_GATEWAY]),
171                                             abuf, sizeof(abuf)));
172                 } else
173                         fprintf(fp, "masquerade");
174         } else if (r->rtm_type != RTN_UNICAST)
175                 fprintf(fp, "%s", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
176
177         fprintf(fp, "\n");
178         fflush(fp);
179         return 0;
180 }
181
182 static int iprule_list(int argc, char **argv)
183 {
184         int af = preferred_family;
185
186         if (af == AF_UNSPEC)
187                 af = AF_INET;
188
189         if (argc > 0) {
190                 fprintf(stderr, "\"ip rule show\" does not take any arguments.\n");
191                 return -1;
192         }
193
194         if (rtnl_wilddump_request(&rth, af, RTM_GETRULE) < 0) {
195                 perror("Cannot send dump request");
196                 return 1;
197         }
198
199         if (rtnl_dump_filter(&rth, print_rule, stdout, NULL, NULL) < 0) {
200                 fprintf(stderr, "Dump terminated\n");
201                 return 1;
202         }
203
204         return 0;
205 }
206
207
208 static int iprule_modify(int cmd, int argc, char **argv)
209 {
210         int table_ok = 0;
211         struct {
212                 struct nlmsghdr         n;
213                 struct rtmsg            r;
214                 char                    buf[1024];
215         } req;
216
217         memset(&req, 0, sizeof(req));
218
219         req.n.nlmsg_type = cmd;
220         req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
221         req.n.nlmsg_flags = NLM_F_REQUEST;
222         req.r.rtm_family = preferred_family;
223         req.r.rtm_protocol = RTPROT_BOOT;
224         req.r.rtm_scope = RT_SCOPE_UNIVERSE;
225         req.r.rtm_table = 0;
226         req.r.rtm_type = RTN_UNSPEC;
227         req.r.rtm_flags = 0;
228
229         if (cmd == RTM_NEWRULE) {
230                 req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
231                 req.r.rtm_type = RTN_UNICAST;
232         }
233
234         while (argc > 0) {
235                 if (strcmp(*argv, "not") == 0) {
236                         req.r.rtm_flags |= FIB_RULE_INVERT;
237                 } else if (strcmp(*argv, "from") == 0) {
238                         inet_prefix dst;
239                         NEXT_ARG();
240                         get_prefix(&dst, *argv, req.r.rtm_family);
241                         req.r.rtm_src_len = dst.bitlen;
242                         addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen);
243                 } else if (strcmp(*argv, "to") == 0) {
244                         inet_prefix dst;
245                         NEXT_ARG();
246                         get_prefix(&dst, *argv, req.r.rtm_family);
247                         req.r.rtm_dst_len = dst.bitlen;
248                         addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
249                 } else if (matches(*argv, "preference") == 0 ||
250                            matches(*argv, "order") == 0 ||
251                            matches(*argv, "priority") == 0) {
252                         __u32 pref;
253                         NEXT_ARG();
254                         if (get_u32(&pref, *argv, 0))
255                                 invarg("preference value is invalid\n", *argv);
256                         addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref);
257                 } else if (strcmp(*argv, "tos") == 0) {
258                         __u32 tos;
259                         NEXT_ARG();
260                         if (rtnl_dsfield_a2n(&tos, *argv))
261                                 invarg("TOS value is invalid\n", *argv);
262                         req.r.rtm_tos = tos;
263                 } else if (strcmp(*argv, "fwmark") == 0) {
264                         char *slash;
265                         __u32 fwmark, fwmask;
266                         NEXT_ARG();
267                         if ((slash = strchr(*argv, '/')) != NULL)
268                                 *slash = '\0';
269                         if (get_u32(&fwmark, *argv, 0))
270                                 invarg("fwmark value is invalid\n", *argv);
271                         addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark);
272                         if (slash) {
273                                 if (get_u32(&fwmask, slash+1, 0))
274                                         invarg("fwmask value is invalid\n", slash+1);
275                                 addattr32(&req.n, sizeof(req), RTA_FWMASK, fwmask);
276                         }
277                 } else if (matches(*argv, "realms") == 0) {
278                         __u32 realm;
279                         NEXT_ARG();
280                         if (get_rt_realms(&realm, *argv))
281                                 invarg("invalid realms\n", *argv);
282                         addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
283                 } else if (matches(*argv, "table") == 0 ||
284                            strcmp(*argv, "lookup") == 0) {
285                         __u32 tid;
286                         NEXT_ARG();
287                         if (rtnl_rttable_a2n(&tid, *argv))
288                                 invarg("invalid table ID\n", *argv);
289                         if (tid < 256)
290                                 req.r.rtm_table = tid;
291                         else {
292                                 req.r.rtm_table = RT_TABLE_UNSPEC;
293                                 addattr32(&req.n, sizeof(req), RTA_TABLE, tid);
294                         }
295                         table_ok = 1;
296                 } else if (strcmp(*argv, "dev") == 0 ||
297                            strcmp(*argv, "iif") == 0) {
298                         NEXT_ARG();
299                         addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1);
300                 } else if (strcmp(*argv, "nat") == 0 ||
301                            matches(*argv, "map-to") == 0) {
302                         NEXT_ARG();
303                         fprintf(stderr, "Warning: route NAT is deprecated\n");
304                         addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv));
305                         req.r.rtm_type = RTN_NAT;
306                 } else {
307                         int type;
308
309                         if (strcmp(*argv, "type") == 0) {
310                                 NEXT_ARG();
311                         }
312                         if (matches(*argv, "help") == 0)
313                                 usage();
314                         if (rtnl_rtntype_a2n(&type, *argv))
315                                 invarg("Failed to parse rule type", *argv);
316                         req.r.rtm_type = type;
317                 }
318                 argc--;
319                 argv++;
320         }
321
322         if (req.r.rtm_family == AF_UNSPEC)
323                 req.r.rtm_family = AF_INET;
324
325         if (!table_ok && cmd == RTM_NEWRULE)
326                 req.r.rtm_table = RT_TABLE_MAIN;
327
328         if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
329                 return 2;
330
331         return 0;
332 }
333
334
335 static int flush_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
336 {
337         struct rtnl_handle rth2;
338         struct rtmsg *r = NLMSG_DATA(n);
339         int len = n->nlmsg_len;
340         struct rtattr * tb[RTA_MAX+1];
341
342         len -= NLMSG_LENGTH(sizeof(*r));
343         if (len < 0)
344                 return -1;
345
346         parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
347
348         if (tb[RTA_PRIORITY]) {
349                 n->nlmsg_type = RTM_DELRULE;
350                 n->nlmsg_flags = NLM_F_REQUEST;
351
352                 if (rtnl_open(&rth2, 0) < 0)
353                         return -1;
354
355                 if (rtnl_talk(&rth2, n, 0, 0, NULL, NULL, NULL) < 0)
356                         return -2;
357
358                 rtnl_close(&rth2);
359         }
360
361         return 0;
362 }
363
364 static int iprule_flush(int argc, char **argv)
365 {
366         int af = preferred_family;
367
368         if (af == AF_UNSPEC)
369                 af = AF_INET;
370
371         if (argc > 0) {
372                 fprintf(stderr, "\"ip rule flush\" does not allow arguments\n");
373                 return -1;
374         }
375
376         if (rtnl_wilddump_request(&rth, af, RTM_GETRULE) < 0) {
377                 perror("Cannot send dump request");
378                 return 1;
379         }
380
381         if (rtnl_dump_filter(&rth, flush_rule, NULL, NULL, NULL) < 0) {
382                 fprintf(stderr, "Flush terminated\n");
383                 return 1;
384         }
385
386         return 0;
387 }
388
389 int do_iprule(int argc, char **argv)
390 {
391         if (argc < 1) {
392                 return iprule_list(0, NULL);
393         } else if (matches(argv[0], "list") == 0 ||
394                    matches(argv[0], "lst") == 0 ||
395                    matches(argv[0], "show") == 0) {
396                 return iprule_list(argc-1, argv+1);
397         } else if (matches(argv[0], "add") == 0) {
398                 return iprule_modify(RTM_NEWRULE, argc-1, argv+1);
399         } else if (matches(argv[0], "delete") == 0) {
400                 return iprule_modify(RTM_DELRULE, argc-1, argv+1);
401         } else if (matches(argv[0], "flush") == 0) {
402                 return iprule_flush(argc-1, argv+1);
403         } else if (matches(argv[0], "help") == 0)
404                 usage();
405
406         fprintf(stderr, "Command \"%s\" is unknown, try \"ip rule help\".\n", *argv);
407         exit(-1);
408 }
409