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