Home | History | Annotate | Download | only in ip
      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 (at) ms2.inr.ac.ru>
     10  *
     11  */
     12 
     13 #include <stdio.h>
     14 #include <stdlib.h>
     15 #include <unistd.h>
     16 #include <syslog.h>
     17 #include <fcntl.h>
     18 #include <sys/socket.h>
     19 #include <netinet/in.h>
     20 #include <netinet/ip.h>
     21 #include <arpa/inet.h>
     22 #include <string.h>
     23 #include <linux/fib_rules.h>
     24 
     25 #include "rt_names.h"
     26 #include "utils.h"
     27 #include "ip_common.h"
     28 
     29 extern struct rtnl_handle rth;
     30 
     31 static void usage(void) __attribute__((noreturn));
     32 
     33 static void usage(void)
     34 {
     35 	fprintf(stderr, "Usage: ip rule [ list | add | del | flush ] SELECTOR ACTION\n");
     36 	fprintf(stderr, "SELECTOR := [ not ] [ from PREFIX ] [ to PREFIX ] [ tos TOS ] [ fwmark FWMARK[/MASK] ]\n");
     37 	fprintf(stderr, "            [ dev STRING ] [ pref NUMBER ]\n");
     38 	fprintf(stderr, "ACTION := [ table TABLE_ID ]\n");
     39 	fprintf(stderr, "          [ prohibit | reject | unreachable ]\n");
     40 	fprintf(stderr, "          [ realms [SRCREALM/]DSTREALM ]\n");
     41 	fprintf(stderr, "          [ goto NUMBER ]\n");
     42 	fprintf(stderr, "TABLE_ID := [ local | main | default | NUMBER ]\n");
     43 	exit(-1);
     44 }
     45 
     46 int print_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
     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 	__u32 table;
     53 	struct rtattr * tb[FRA_MAX+1];
     54 	char abuf[256];
     55 	SPRINT_BUF(b1);
     56 
     57 	if (n->nlmsg_type != RTM_NEWRULE && n->nlmsg_type != RTM_DELRULE)
     58 		return 0;
     59 
     60 	len -= NLMSG_LENGTH(sizeof(*r));
     61 	if (len < 0)
     62 		return -1;
     63 
     64 	parse_rtattr(tb, FRA_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 (n->nlmsg_type == RTM_DELRULE)
     76 		fprintf(fp, "Deleted ");
     77 
     78 	if (tb[FRA_PRIORITY])
     79 		fprintf(fp, "%u:\t", *(unsigned*)RTA_DATA(tb[FRA_PRIORITY]));
     80 	else
     81 		fprintf(fp, "0:\t");
     82 
     83 	if (r->rtm_flags & FIB_RULE_INVERT)
     84 		fprintf(fp, "not ");
     85 
     86 	if (tb[FRA_SRC]) {
     87 		if (r->rtm_src_len != host_len) {
     88 			fprintf(fp, "from %s/%u ", rt_addr_n2a(r->rtm_family,
     89 							 RTA_PAYLOAD(tb[FRA_SRC]),
     90 							 RTA_DATA(tb[FRA_SRC]),
     91 							 abuf, sizeof(abuf)),
     92 				r->rtm_src_len
     93 				);
     94 		} else {
     95 			fprintf(fp, "from %s ", format_host(r->rtm_family,
     96 						       RTA_PAYLOAD(tb[FRA_SRC]),
     97 						       RTA_DATA(tb[FRA_SRC]),
     98 						       abuf, sizeof(abuf))
     99 				);
    100 		}
    101 	} else if (r->rtm_src_len) {
    102 		fprintf(fp, "from 0/%d ", r->rtm_src_len);
    103 	} else {
    104 		fprintf(fp, "from all ");
    105 	}
    106 
    107 	if (tb[FRA_DST]) {
    108 		if (r->rtm_dst_len != host_len) {
    109 			fprintf(fp, "to %s/%u ", rt_addr_n2a(r->rtm_family,
    110 							 RTA_PAYLOAD(tb[FRA_DST]),
    111 							 RTA_DATA(tb[FRA_DST]),
    112 							 abuf, sizeof(abuf)),
    113 				r->rtm_dst_len
    114 				);
    115 		} else {
    116 			fprintf(fp, "to %s ", format_host(r->rtm_family,
    117 						       RTA_PAYLOAD(tb[FRA_DST]),
    118 						       RTA_DATA(tb[FRA_DST]),
    119 						       abuf, sizeof(abuf)));
    120 		}
    121 	} else if (r->rtm_dst_len) {
    122 		fprintf(fp, "to 0/%d ", r->rtm_dst_len);
    123 	}
    124 
    125 	if (r->rtm_tos) {
    126 		SPRINT_BUF(b1);
    127 		fprintf(fp, "tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));
    128 	}
    129 
    130  	if (tb[FRA_FWMARK] || tb[FRA_FWMASK]) {
    131 		__u32 mark = 0, mask = 0;
    132 
    133 		if (tb[FRA_FWMARK])
    134 			mark = *(__u32*)RTA_DATA(tb[FRA_FWMARK]);
    135 
    136 		if (tb[FRA_FWMASK] &&
    137 		    (mask = *(__u32*)RTA_DATA(tb[FRA_FWMASK])) != 0xFFFFFFFF)
    138 			fprintf(fp, "fwmark 0x%x/0x%x ", mark, mask);
    139 		else
    140 			fprintf(fp, "fwmark 0x%x ", mark);
    141 	}
    142 
    143 	if (tb[FRA_IFNAME]) {
    144 		fprintf(fp, "iif %s ", (char*)RTA_DATA(tb[FRA_IFNAME]));
    145 		if (r->rtm_flags & FIB_RULE_DEV_DETACHED)
    146 			fprintf(fp, "[detached] ");
    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[FRA_FLOW]) {
    154 		__u32 to = *(__u32*)RTA_DATA(tb[FRA_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 == FR_ACT_GOTO) {
    175 		fprintf(fp, "goto ");
    176 		if (tb[FRA_GOTO])
    177 			fprintf(fp, "%u", *(__u32 *) RTA_DATA(tb[FRA_GOTO]));
    178 		else
    179 			fprintf(fp, "none");
    180 		if (r->rtm_flags & FIB_RULE_UNRESOLVED)
    181 			fprintf(fp, " [unresolved]");
    182 	} else if (r->rtm_type == FR_ACT_NOP)
    183 		fprintf(fp, "nop");
    184 	else if (r->rtm_type != RTN_UNICAST)
    185 		fprintf(fp, "%s", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
    186 
    187 	fprintf(fp, "\n");
    188 	fflush(fp);
    189 	return 0;
    190 }
    191 
    192 static int iprule_list(int argc, char **argv)
    193 {
    194 	int af = preferred_family;
    195 
    196 	if (af == AF_UNSPEC)
    197 		af = AF_INET;
    198 
    199 	if (argc > 0) {
    200 		fprintf(stderr, "\"ip rule show\" does not take any arguments.\n");
    201 		return -1;
    202 	}
    203 
    204 	if (rtnl_wilddump_request(&rth, af, RTM_GETRULE) < 0) {
    205 		perror("Cannot send dump request");
    206 		return 1;
    207 	}
    208 
    209 	if (rtnl_dump_filter(&rth, print_rule, stdout, NULL, NULL) < 0) {
    210 		fprintf(stderr, "Dump terminated\n");
    211 		return 1;
    212 	}
    213 
    214 	return 0;
    215 }
    216 
    217 
    218 static int iprule_modify(int cmd, int argc, char **argv)
    219 {
    220 	int table_ok = 0;
    221 	struct {
    222 		struct nlmsghdr 	n;
    223 		struct rtmsg 		r;
    224 		char   			buf[1024];
    225 	} req;
    226 
    227 	memset(&req, 0, sizeof(req));
    228 
    229 	req.n.nlmsg_type = cmd;
    230 	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
    231 	req.n.nlmsg_flags = NLM_F_REQUEST;
    232 	req.r.rtm_family = preferred_family;
    233 	req.r.rtm_protocol = RTPROT_BOOT;
    234 	req.r.rtm_scope = RT_SCOPE_UNIVERSE;
    235 	req.r.rtm_table = 0;
    236 	req.r.rtm_type = RTN_UNSPEC;
    237 	req.r.rtm_flags = 0;
    238 
    239 	if (cmd == RTM_NEWRULE) {
    240 		req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
    241 		req.r.rtm_type = RTN_UNICAST;
    242 	}
    243 
    244 	while (argc > 0) {
    245 		if (strcmp(*argv, "not") == 0) {
    246 			req.r.rtm_flags |= FIB_RULE_INVERT;
    247 		} else if (strcmp(*argv, "from") == 0) {
    248 			inet_prefix dst;
    249 			NEXT_ARG();
    250 			get_prefix(&dst, *argv, req.r.rtm_family);
    251 			req.r.rtm_src_len = dst.bitlen;
    252 			addattr_l(&req.n, sizeof(req), FRA_SRC, &dst.data, dst.bytelen);
    253 		} else if (strcmp(*argv, "to") == 0) {
    254 			inet_prefix dst;
    255 			NEXT_ARG();
    256 			get_prefix(&dst, *argv, req.r.rtm_family);
    257 			req.r.rtm_dst_len = dst.bitlen;
    258 			addattr_l(&req.n, sizeof(req), FRA_DST, &dst.data, dst.bytelen);
    259 		} else if (matches(*argv, "preference") == 0 ||
    260 			   matches(*argv, "order") == 0 ||
    261 			   matches(*argv, "priority") == 0) {
    262 			__u32 pref;
    263 			NEXT_ARG();
    264 			if (get_u32(&pref, *argv, 0))
    265 				invarg("preference value is invalid\n", *argv);
    266 			addattr32(&req.n, sizeof(req), FRA_PRIORITY, pref);
    267 		} else if (strcmp(*argv, "tos") == 0) {
    268 			__u32 tos;
    269 			NEXT_ARG();
    270 			if (rtnl_dsfield_a2n(&tos, *argv))
    271 				invarg("TOS value is invalid\n", *argv);
    272 			req.r.rtm_tos = tos;
    273 		} else if (strcmp(*argv, "fwmark") == 0) {
    274 			char *slash;
    275 			__u32 fwmark, fwmask;
    276 			NEXT_ARG();
    277 			if ((slash = strchr(*argv, '/')) != NULL)
    278 				*slash = '\0';
    279 			if (get_u32(&fwmark, *argv, 0))
    280 				invarg("fwmark value is invalid\n", *argv);
    281 			addattr32(&req.n, sizeof(req), FRA_FWMARK, fwmark);
    282 			if (slash) {
    283 				if (get_u32(&fwmask, slash+1, 0))
    284 					invarg("fwmask value is invalid\n", slash+1);
    285 				addattr32(&req.n, sizeof(req), FRA_FWMASK, fwmask);
    286 			}
    287 		} else if (matches(*argv, "realms") == 0) {
    288 			__u32 realm;
    289 			NEXT_ARG();
    290 			if (get_rt_realms(&realm, *argv))
    291 				invarg("invalid realms\n", *argv);
    292 			addattr32(&req.n, sizeof(req), FRA_FLOW, realm);
    293 		} else if (matches(*argv, "table") == 0 ||
    294 			   strcmp(*argv, "lookup") == 0) {
    295 			__u32 tid;
    296 			NEXT_ARG();
    297 			if (rtnl_rttable_a2n(&tid, *argv))
    298 				invarg("invalid table ID\n", *argv);
    299 			if (tid < 256)
    300 				req.r.rtm_table = tid;
    301 			else {
    302 				req.r.rtm_table = RT_TABLE_UNSPEC;
    303 				addattr32(&req.n, sizeof(req), FRA_TABLE, tid);
    304 			}
    305 			table_ok = 1;
    306 		} else if (strcmp(*argv, "dev") == 0 ||
    307 			   strcmp(*argv, "iif") == 0) {
    308 			NEXT_ARG();
    309 			addattr_l(&req.n, sizeof(req), FRA_IFNAME, *argv, strlen(*argv)+1);
    310 		} else if (strcmp(*argv, "nat") == 0 ||
    311 			   matches(*argv, "map-to") == 0) {
    312 			NEXT_ARG();
    313 			fprintf(stderr, "Warning: route NAT is deprecated\n");
    314 			addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv));
    315 			req.r.rtm_type = RTN_NAT;
    316 		} else {
    317 			int type;
    318 
    319 			if (strcmp(*argv, "type") == 0) {
    320 				NEXT_ARG();
    321 			}
    322 			if (matches(*argv, "help") == 0)
    323 				usage();
    324 			else if (matches(*argv, "goto") == 0) {
    325 				__u32 target;
    326 				type = FR_ACT_GOTO;
    327 				NEXT_ARG();
    328 				if (get_u32(&target, *argv, 0))
    329 					invarg("invalid target\n", *argv);
    330 				addattr32(&req.n, sizeof(req), FRA_GOTO, target);
    331 			} else if (matches(*argv, "nop") == 0)
    332 				type = FR_ACT_NOP;
    333 			else if (rtnl_rtntype_a2n(&type, *argv))
    334 				invarg("Failed to parse rule type", *argv);
    335 			req.r.rtm_type = type;
    336 			table_ok = 1;
    337 		}
    338 		argc--;
    339 		argv++;
    340 	}
    341 
    342 	if (req.r.rtm_family == AF_UNSPEC)
    343 		req.r.rtm_family = AF_INET;
    344 
    345 	if (!table_ok && cmd == RTM_NEWRULE)
    346 		req.r.rtm_table = RT_TABLE_MAIN;
    347 
    348 	if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
    349 		return 2;
    350 
    351 	return 0;
    352 }
    353 
    354 
    355 static int flush_rule(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
    356 {
    357 	struct rtnl_handle rth2;
    358 	struct rtmsg *r = NLMSG_DATA(n);
    359 	int len = n->nlmsg_len;
    360 	struct rtattr * tb[FRA_MAX+1];
    361 
    362 	len -= NLMSG_LENGTH(sizeof(*r));
    363 	if (len < 0)
    364 		return -1;
    365 
    366 	parse_rtattr(tb, FRA_MAX, RTM_RTA(r), len);
    367 
    368 	if (tb[FRA_PRIORITY]) {
    369 		n->nlmsg_type = RTM_DELRULE;
    370 		n->nlmsg_flags = NLM_F_REQUEST;
    371 
    372 		if (rtnl_open(&rth2, 0) < 0)
    373 			return -1;
    374 
    375 		if (rtnl_talk(&rth2, n, 0, 0, NULL, NULL, NULL) < 0)
    376 			return -2;
    377 
    378 		rtnl_close(&rth2);
    379 	}
    380 
    381 	return 0;
    382 }
    383 
    384 static int iprule_flush(int argc, char **argv)
    385 {
    386 	int af = preferred_family;
    387 
    388 	if (af == AF_UNSPEC)
    389 		af = AF_INET;
    390 
    391 	if (argc > 0) {
    392 		fprintf(stderr, "\"ip rule flush\" does not allow arguments\n");
    393 		return -1;
    394 	}
    395 
    396 	if (rtnl_wilddump_request(&rth, af, RTM_GETRULE) < 0) {
    397 		perror("Cannot send dump request");
    398 		return 1;
    399 	}
    400 
    401 	if (rtnl_dump_filter(&rth, flush_rule, NULL, NULL, NULL) < 0) {
    402 		fprintf(stderr, "Flush terminated\n");
    403 		return 1;
    404 	}
    405 
    406 	return 0;
    407 }
    408 
    409 int do_iprule(int argc, char **argv)
    410 {
    411 	if (argc < 1) {
    412 		return iprule_list(0, NULL);
    413 	} else if (matches(argv[0], "list") == 0 ||
    414 		   matches(argv[0], "lst") == 0 ||
    415 		   matches(argv[0], "show") == 0) {
    416 		return iprule_list(argc-1, argv+1);
    417 	} else if (matches(argv[0], "add") == 0) {
    418 		return iprule_modify(RTM_NEWRULE, argc-1, argv+1);
    419 	} else if (matches(argv[0], "delete") == 0) {
    420 		return iprule_modify(RTM_DELRULE, argc-1, argv+1);
    421 	} else if (matches(argv[0], "flush") == 0) {
    422 		return iprule_flush(argc-1, argv+1);
    423 	} else if (matches(argv[0], "help") == 0)
    424 		usage();
    425 
    426 	fprintf(stderr, "Command \"%s\" is unknown, try \"ip rule help\".\n", *argv);
    427 	exit(-1);
    428 }
    429 
    430