]>
Commit | Line | Data |
---|---|---|
aba5acdf SH |
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 | ||
28 | #include "rt_names.h" | |
29 | #include "utils.h" | |
30 | ||
31 | static void usage(void) __attribute__((noreturn)); | |
32 | ||
33 | static void usage(void) | |
34 | { | |
35 | fprintf(stderr, "Usage: ip rule [ list | add | del ] SELECTOR ACTION\n"); | |
36 | fprintf(stderr, "SELECTOR := [ from PREFIX ] [ to PREFIX ] [ tos TOS ] [ fwmark FWMARK ]\n"); | |
37 | fprintf(stderr, " [ dev STRING ] [ pref NUMBER ]\n"); | |
38 | fprintf(stderr, "ACTION := [ table TABLE_ID ] [ nat ADDRESS ]\n"); | |
39 | fprintf(stderr, " [ prohibit | reject | unreachable ]\n"); | |
40 | fprintf(stderr, " [ realms [SRCREALM/]DSTREALM ]\n"); | |
41 | fprintf(stderr, "TABLE_ID := [ local | main | default | NUMBER ]\n"); | |
42 | exit(-1); | |
43 | } | |
44 | ||
6dc9f016 SH |
45 | static int print_rule(const struct sockaddr_nl *who, const struct nlmsghdr *n, |
46 | void *arg) | |
aba5acdf SH |
47 | { |
48 | FILE *fp = (FILE*)arg; | |
49 | struct rtmsg *r = NLMSG_DATA(n); | |
50 | int len = n->nlmsg_len; | |
51 | int host_len = -1; | |
52 | struct rtattr * tb[RTA_MAX+1]; | |
53 | char abuf[256]; | |
54 | SPRINT_BUF(b1); | |
55 | ||
56 | if (n->nlmsg_type != RTM_NEWRULE) | |
57 | return 0; | |
58 | ||
59 | len -= NLMSG_LENGTH(sizeof(*r)); | |
60 | if (len < 0) | |
61 | return -1; | |
62 | ||
63 | memset(tb, 0, sizeof(tb)); | |
64 | parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len); | |
65 | ||
66 | if (r->rtm_family == AF_INET) | |
67 | host_len = 32; | |
68 | else if (r->rtm_family == AF_INET6) | |
69 | host_len = 128; | |
70 | else if (r->rtm_family == AF_DECnet) | |
71 | host_len = 16; | |
72 | else if (r->rtm_family == AF_IPX) | |
73 | host_len = 80; | |
74 | ||
75 | if (tb[RTA_PRIORITY]) | |
76 | fprintf(fp, "%u:\t", *(unsigned*)RTA_DATA(tb[RTA_PRIORITY])); | |
77 | else | |
78 | fprintf(fp, "0:\t"); | |
79 | ||
80 | if (tb[RTA_SRC]) { | |
81 | if (r->rtm_src_len != host_len) { | |
82 | fprintf(fp, "from %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, "from %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, "from 0/%d ", r->rtm_src_len); | |
97 | } else { | |
98 | fprintf(fp, "from all "); | |
99 | } | |
100 | ||
101 | if (tb[RTA_DST]) { | |
102 | if (r->rtm_dst_len != host_len) { | |
103 | fprintf(fp, "to %s/%u ", rt_addr_n2a(r->rtm_family, | |
104 | RTA_PAYLOAD(tb[RTA_DST]), | |
105 | RTA_DATA(tb[RTA_DST]), | |
106 | abuf, sizeof(abuf)), | |
107 | r->rtm_dst_len | |
108 | ); | |
109 | } else { | |
110 | fprintf(fp, "to %s ", format_host(r->rtm_family, | |
111 | RTA_PAYLOAD(tb[RTA_DST]), | |
112 | RTA_DATA(tb[RTA_DST]), | |
113 | abuf, sizeof(abuf))); | |
114 | } | |
115 | } else if (r->rtm_dst_len) { | |
116 | fprintf(fp, "to 0/%d ", r->rtm_dst_len); | |
117 | } | |
118 | ||
119 | if (r->rtm_tos) { | |
120 | SPRINT_BUF(b1); | |
121 | fprintf(fp, "tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1))); | |
122 | } | |
123 | if (tb[RTA_PROTOINFO]) { | |
4fb466f9 | 124 | fprintf(fp, "fwmark %#x ", *(__u32*)RTA_DATA(tb[RTA_PROTOINFO])); |
aba5acdf SH |
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 | __u32 to = *(__u32*)RTA_DATA(tb[RTA_FLOW]); | |
136 | __u32 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 | fprintf(fp, "\n"); | |
159 | fflush(fp); | |
160 | return 0; | |
161 | } | |
162 | ||
163 | int iprule_list(int argc, char **argv) | |
164 | { | |
165 | struct rtnl_handle rth; | |
166 | int af = preferred_family; | |
167 | ||
168 | if (af == AF_UNSPEC) | |
169 | af = AF_INET; | |
170 | ||
171 | if (argc > 0) { | |
172 | fprintf(stderr, "\"ip rule show\" need not eny arguments.\n"); | |
173 | return -1; | |
174 | } | |
175 | ||
176 | if (rtnl_open(&rth, 0) < 0) | |
177 | return 1; | |
178 | ||
179 | if (rtnl_wilddump_request(&rth, af, RTM_GETRULE) < 0) { | |
180 | perror("Cannot send dump request"); | |
181 | return 1; | |
182 | } | |
183 | ||
184 | if (rtnl_dump_filter(&rth, print_rule, stdout, NULL, NULL) < 0) { | |
185 | fprintf(stderr, "Dump terminated\n"); | |
186 | return 1; | |
187 | } | |
188 | ||
189 | return 0; | |
190 | } | |
191 | ||
192 | ||
193 | int iprule_modify(int cmd, int argc, char **argv) | |
194 | { | |
195 | int table_ok = 0; | |
196 | struct rtnl_handle rth; | |
197 | struct { | |
198 | struct nlmsghdr n; | |
199 | struct rtmsg r; | |
200 | char buf[1024]; | |
201 | } req; | |
202 | ||
203 | memset(&req, 0, sizeof(req)); | |
204 | ||
205 | req.n.nlmsg_type = cmd; | |
206 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); | |
207 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
208 | req.r.rtm_family = preferred_family; | |
209 | req.r.rtm_protocol = RTPROT_BOOT; | |
210 | req.r.rtm_scope = RT_SCOPE_UNIVERSE; | |
211 | req.r.rtm_table = 0; | |
212 | req.r.rtm_type = RTN_UNSPEC; | |
213 | ||
214 | if (cmd == RTM_NEWRULE) { | |
215 | req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL; | |
216 | req.r.rtm_type = RTN_UNICAST; | |
217 | } | |
218 | ||
219 | while (argc > 0) { | |
220 | if (strcmp(*argv, "from") == 0) { | |
221 | inet_prefix dst; | |
222 | NEXT_ARG(); | |
223 | get_prefix(&dst, *argv, req.r.rtm_family); | |
224 | req.r.rtm_src_len = dst.bitlen; | |
225 | addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen); | |
226 | } else if (strcmp(*argv, "to") == 0) { | |
227 | inet_prefix dst; | |
228 | NEXT_ARG(); | |
229 | get_prefix(&dst, *argv, req.r.rtm_family); | |
230 | req.r.rtm_dst_len = dst.bitlen; | |
231 | addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen); | |
232 | } else if (matches(*argv, "preference") == 0 || | |
233 | matches(*argv, "order") == 0 || | |
234 | matches(*argv, "priority") == 0) { | |
235 | __u32 pref; | |
236 | NEXT_ARG(); | |
237 | if (get_u32(&pref, *argv, 0)) | |
238 | invarg("preference value is invalid\n", *argv); | |
239 | addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref); | |
240 | } else if (strcmp(*argv, "tos") == 0) { | |
241 | __u32 tos; | |
242 | NEXT_ARG(); | |
243 | if (rtnl_dsfield_a2n(&tos, *argv)) | |
244 | invarg("TOS value is invalid\n", *argv); | |
245 | req.r.rtm_tos = tos; | |
246 | } else if (strcmp(*argv, "fwmark") == 0) { | |
247 | __u32 fwmark; | |
248 | NEXT_ARG(); | |
4fb466f9 | 249 | if (get_u32(&fwmark, *argv, 0)) |
aba5acdf SH |
250 | invarg("fwmark value is invalid\n", *argv); |
251 | addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark); | |
252 | } else if (matches(*argv, "realms") == 0) { | |
253 | __u32 realm; | |
254 | NEXT_ARG(); | |
255 | if (get_rt_realms(&realm, *argv)) | |
256 | invarg("invalid realms\n", *argv); | |
257 | addattr32(&req.n, sizeof(req), RTA_FLOW, realm); | |
258 | } else if (matches(*argv, "table") == 0 || | |
259 | strcmp(*argv, "lookup") == 0) { | |
260 | int tid; | |
261 | NEXT_ARG(); | |
262 | if (rtnl_rttable_a2n(&tid, *argv)) | |
263 | invarg("invalid table ID\n", *argv); | |
264 | req.r.rtm_table = tid; | |
265 | table_ok = 1; | |
266 | } else if (strcmp(*argv, "dev") == 0 || | |
267 | strcmp(*argv, "iif") == 0) { | |
268 | NEXT_ARG(); | |
269 | addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1); | |
270 | } else if (strcmp(*argv, "nat") == 0 || | |
271 | matches(*argv, "map-to") == 0) { | |
272 | NEXT_ARG(); | |
273 | addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv)); | |
274 | req.r.rtm_type = RTN_NAT; | |
275 | } else { | |
276 | int type; | |
277 | ||
278 | if (strcmp(*argv, "type") == 0) { | |
279 | NEXT_ARG(); | |
280 | } | |
281 | if (matches(*argv, "help") == 0) | |
282 | usage(); | |
283 | if (rtnl_rtntype_a2n(&type, *argv)) | |
284 | invarg("Failed to parse rule type", *argv); | |
285 | req.r.rtm_type = type; | |
286 | } | |
287 | argc--; | |
288 | argv++; | |
289 | } | |
290 | ||
291 | if (req.r.rtm_family == AF_UNSPEC) | |
292 | req.r.rtm_family = AF_INET; | |
293 | ||
294 | if (!table_ok && cmd == RTM_NEWRULE) | |
295 | req.r.rtm_table = RT_TABLE_MAIN; | |
296 | ||
297 | if (rtnl_open(&rth, 0) < 0) | |
298 | return 1; | |
299 | ||
300 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) | |
301 | return 2; | |
302 | ||
303 | return 0; | |
304 | } | |
305 | ||
306 | int do_iprule(int argc, char **argv) | |
307 | { | |
308 | if (argc < 1) { | |
309 | return iprule_list(0, NULL); | |
310 | } else if (matches(argv[0], "list") == 0 || | |
311 | matches(argv[0], "lst") == 0 || | |
312 | matches(argv[0], "show") == 0) { | |
313 | return iprule_list(argc-1, argv+1); | |
314 | } else if (matches(argv[0], "add") == 0) { | |
315 | return iprule_modify(RTM_NEWRULE, argc-1, argv+1); | |
316 | } else if (matches(argv[0], "delete") == 0) { | |
317 | return iprule_modify(RTM_DELRULE, argc-1, argv+1); | |
318 | } else if (matches(argv[0], "help") == 0) | |
319 | usage(); | |
320 | ||
321 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip rule help\".\n", *argv); | |
322 | exit(-1); | |
323 | } | |
324 |