]>
git.proxmox.com Git - mirror_iproute2.git/blob - ip/iprule.c
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
14 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <netinet/ip.h>
25 #include <arpa/inet.h>
31 static void usage(void) __attribute__((noreturn
));
33 static void usage(void)
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");
45 static int print_rule(const struct sockaddr_nl
*who
, const struct nlmsghdr
*n
,
48 FILE *fp
= (FILE*)arg
;
49 struct rtmsg
*r
= NLMSG_DATA(n
);
50 int len
= n
->nlmsg_len
;
52 struct rtattr
* tb
[RTA_MAX
+1];
56 if (n
->nlmsg_type
!= RTM_NEWRULE
)
59 len
-= NLMSG_LENGTH(sizeof(*r
));
63 memset(tb
, 0, sizeof(tb
));
64 parse_rtattr(tb
, RTA_MAX
, RTM_RTA(r
), len
);
66 if (r
->rtm_family
== AF_INET
)
68 else if (r
->rtm_family
== AF_INET6
)
70 else if (r
->rtm_family
== AF_DECnet
)
72 else if (r
->rtm_family
== AF_IPX
)
76 fprintf(fp
, "%u:\t", *(unsigned*)RTA_DATA(tb
[RTA_PRIORITY
]));
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
]),
89 fprintf(fp
, "from %s ", format_host(r
->rtm_family
,
90 RTA_PAYLOAD(tb
[RTA_SRC
]),
91 RTA_DATA(tb
[RTA_SRC
]),
95 } else if (r
->rtm_src_len
) {
96 fprintf(fp
, "from 0/%d ", r
->rtm_src_len
);
98 fprintf(fp
, "from all ");
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
]),
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
)));
115 } else if (r
->rtm_dst_len
) {
116 fprintf(fp
, "to 0/%d ", r
->rtm_dst_len
);
121 fprintf(fp
, "tos %s ", rtnl_dsfield_n2a(r
->rtm_tos
, b1
, sizeof(b1
)));
123 if (tb
[RTA_PROTOINFO
]) {
124 fprintf(fp
, "fwmark %#x ", *(__u32
*)RTA_DATA(tb
[RTA_PROTOINFO
]));
128 fprintf(fp
, "iif %s ", (char*)RTA_DATA(tb
[RTA_IIF
]));
132 fprintf(fp
, "lookup %s ", rtnl_rttable_n2a(r
->rtm_table
, b1
, sizeof(b1
)));
135 __u32 to
= *(__u32
*)RTA_DATA(tb
[RTA_FLOW
]);
139 fprintf(fp
, "realms %s/",
140 rtnl_rtrealm_n2a(from
, b1
, sizeof(b1
)));
143 rtnl_rtrealm_n2a(to
, b1
, sizeof(b1
)));
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
)));
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
)));
163 int iprule_list(int argc
, char **argv
)
165 struct rtnl_handle rth
;
166 int af
= preferred_family
;
172 fprintf(stderr
, "\"ip rule show\" does not take any arguments.\n");
176 if (rtnl_open(&rth
, 0) < 0)
179 if (rtnl_wilddump_request(&rth
, af
, RTM_GETRULE
) < 0) {
180 perror("Cannot send dump request");
184 if (rtnl_dump_filter(&rth
, print_rule
, stdout
, NULL
, NULL
) < 0) {
185 fprintf(stderr
, "Dump terminated\n");
193 int iprule_modify(int cmd
, int argc
, char **argv
)
196 struct rtnl_handle rth
;
203 memset(&req
, 0, sizeof(req
));
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
;
212 req
.r
.rtm_type
= RTN_UNSPEC
;
214 if (cmd
== RTM_NEWRULE
) {
215 req
.n
.nlmsg_flags
|= NLM_F_CREATE
|NLM_F_EXCL
;
216 req
.r
.rtm_type
= RTN_UNICAST
;
220 if (strcmp(*argv
, "from") == 0) {
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) {
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) {
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) {
243 if (rtnl_dsfield_a2n(&tos
, *argv
))
244 invarg("TOS value is invalid\n", *argv
);
246 } else if (strcmp(*argv
, "fwmark") == 0) {
249 if (get_u32(&fwmark
, *argv
, 0))
250 invarg("fwmark value is invalid\n", *argv
);
251 addattr32(&req
.n
, sizeof(req
), RTA_PROTOINFO
, fwmark
);
252 } else if (matches(*argv
, "realms") == 0) {
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) {
262 if (rtnl_rttable_a2n(&tid
, *argv
))
263 invarg("invalid table ID\n", *argv
);
264 req
.r
.rtm_table
= tid
;
266 } else if (strcmp(*argv
, "dev") == 0 ||
267 strcmp(*argv
, "iif") == 0) {
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) {
273 addattr32(&req
.n
, sizeof(req
), RTA_GATEWAY
, get_addr32(*argv
));
274 req
.r
.rtm_type
= RTN_NAT
;
278 if (strcmp(*argv
, "type") == 0) {
281 if (matches(*argv
, "help") == 0)
283 if (rtnl_rtntype_a2n(&type
, *argv
))
284 invarg("Failed to parse rule type", *argv
);
285 req
.r
.rtm_type
= type
;
291 if (req
.r
.rtm_family
== AF_UNSPEC
)
292 req
.r
.rtm_family
= AF_INET
;
294 if (!table_ok
&& cmd
== RTM_NEWRULE
)
295 req
.r
.rtm_table
= RT_TABLE_MAIN
;
297 if (rtnl_open(&rth
, 0) < 0)
300 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0)
306 int do_iprule(int argc
, char **argv
)
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)
321 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip rule help\".\n", *argv
);