Home | History | Annotate | Download | only in tc
      1 /*
      2  * m_action.c		Action Management
      3  *
      4  *		This program is free software; you can distribute 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:  J Hadi Salim (hadi (at) cyberus.ca)
     10  *
     11  * TODO:
     12  * - parse to be passed a filedescriptor for logging purposes
     13  *
     14 */
     15 
     16 #include <stdio.h>
     17 #include <stdlib.h>
     18 #include <unistd.h>
     19 #include <syslog.h>
     20 #include <fcntl.h>
     21 #include <sys/socket.h>
     22 #include <netinet/in.h>
     23 #include <arpa/inet.h>
     24 #include <string.h>
     25 #include <dlfcn.h>
     26 
     27 #include "utils.h"
     28 #include "tc_common.h"
     29 #include "tc_util.h"
     30 
     31 static struct action_util * action_list;
     32 
     33 #ifdef ANDROID
     34 extern struct action_util mirred_action_util;
     35 #endif
     36 
     37 #ifdef CONFIG_GACT
     38 int gact_ld = 0 ; //fuckin backward compatibility
     39 #endif
     40 int tab_flush = 0;
     41 
     42 static void act_usage(void)
     43 {
     44 	/*XXX: In the near future add a action->print_help to improve
     45 	 * usability
     46 	 * This would mean new tc will not be backward compatible
     47 	 * with any action .so from the old days. But if someone really
     48 	 * does that, they would know how to fix this ..
     49 	 *
     50 	*/
     51 	fprintf (stderr, "usage: tc actions <ACTSPECOP>*\n");
     52 	fprintf(stderr,
     53 		"Where: \tACTSPECOP := ACR | GD | FL\n"
     54 			"\tACR := add | change | replace <ACTSPEC>* \n"
     55 			"\tGD := get | delete | <ACTISPEC>*\n"
     56 			"\tFL := ls | list | flush | <ACTNAMESPEC>\n"
     57 			"\tACTNAMESPEC :=  action <ACTNAME>\n"
     58 			"\tACTISPEC := <ACTNAMESPEC> <INDEXSPEC>\n"
     59 			"\tACTSPEC := action <ACTDETAIL> [INDEXSPEC]\n"
     60 			"\tINDEXSPEC := index <32 bit indexvalue>\n"
     61 			"\tACTDETAIL := <ACTNAME> <ACTPARAMS>\n"
     62 			"\t\tExample ACTNAME is gact, mirred, bpf, etc\n"
     63 			"\t\tEach action has its own parameters (ACTPARAMS)\n"
     64 			"\n");
     65 
     66 	exit(-1);
     67 }
     68 
     69 static int print_noaopt(struct action_util *au, FILE *f, struct rtattr *opt)
     70 {
     71 	if (opt && RTA_PAYLOAD(opt))
     72 		fprintf(f, "[Unknown action, optlen=%u] ",
     73 			(unsigned) RTA_PAYLOAD(opt));
     74 	return 0;
     75 }
     76 
     77 static int parse_noaopt(struct action_util *au, int *argc_p, char ***argv_p, int code, struct nlmsghdr *n)
     78 {
     79 	int argc = *argc_p;
     80 	char **argv = *argv_p;
     81 
     82 	if (argc) {
     83 		fprintf(stderr, "Unknown action \"%s\", hence option \"%s\" is unparsable\n", au->id, *argv);
     84 	} else {
     85 		fprintf(stderr, "Unknown action \"%s\"\n", au->id);
     86 	}
     87 	return -1;
     88 }
     89 
     90 static struct action_util *get_action_kind(char *str)
     91 {
     92 #ifdef ANDROID
     93 	if (!strcmp(str, "mirred")) {
     94 		return &mirred_action_util;
     95 	} else {
     96 		fprintf(stderr, "Android does not support action '%s'", str);
     97 		return NULL;
     98 	}
     99 #endif
    100 	static void *aBODY;
    101 	void *dlh;
    102 	char buf[256];
    103 	struct action_util *a;
    104 #ifdef CONFIG_GACT
    105 	int looked4gact = 0;
    106 restart_s:
    107 #endif
    108 	for (a = action_list; a; a = a->next) {
    109 		if (strcmp(a->id, str) == 0)
    110 			return a;
    111 	}
    112 
    113 	snprintf(buf, sizeof(buf), "%s/m_%s.so", get_tc_lib(), str);
    114 	dlh = dlopen(buf, RTLD_LAZY | RTLD_GLOBAL);
    115 	if (dlh == NULL) {
    116 		dlh = aBODY;
    117 		if (dlh == NULL) {
    118 			dlh = aBODY = dlopen(NULL, RTLD_LAZY);
    119 			if (dlh == NULL)
    120 				goto noexist;
    121 		}
    122 	}
    123 
    124 	snprintf(buf, sizeof(buf), "%s_action_util", str);
    125 	a = dlsym(dlh, buf);
    126 	if (a == NULL)
    127 		goto noexist;
    128 
    129 reg:
    130 	a->next = action_list;
    131 	action_list = a;
    132 	return a;
    133 
    134 noexist:
    135 #ifdef CONFIG_GACT
    136 	if (!looked4gact) {
    137 		looked4gact = 1;
    138 		strcpy(str,"gact");
    139 		goto restart_s;
    140 	}
    141 #endif
    142 	a = malloc(sizeof(*a));
    143 	if (a) {
    144 		memset(a, 0, sizeof(*a));
    145 		strncpy(a->id, "noact", 15);
    146 		a->parse_aopt = parse_noaopt;
    147 		a->print_aopt = print_noaopt;
    148 		goto reg;
    149 	}
    150 	return a;
    151 }
    152 
    153 static int
    154 new_cmd(char **argv)
    155 {
    156 	if ((matches(*argv, "change") == 0) ||
    157 		(matches(*argv, "replace") == 0)||
    158 		(matches(*argv, "delete") == 0)||
    159 		(matches(*argv, "get") == 0)||
    160 		(matches(*argv, "add") == 0))
    161 			return 1;
    162 
    163 	return 0;
    164 
    165 }
    166 
    167 int
    168 parse_action(int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n)
    169 {
    170 	int argc = *argc_p;
    171 	char **argv = *argv_p;
    172 	struct rtattr *tail, *tail2;
    173 	char k[16];
    174 	int ok = 0;
    175 	int eap = 0; /* expect action parameters */
    176 
    177 	int ret = 0;
    178 	int prio = 0;
    179 
    180 	if (argc <= 0)
    181 		return -1;
    182 
    183 	tail = tail2 = NLMSG_TAIL(n);
    184 
    185 	addattr_l(n, MAX_MSG, tca_id, NULL, 0);
    186 
    187 	while (argc > 0) {
    188 
    189 		memset(k, 0, sizeof (k));
    190 
    191 		if (strcmp(*argv, "action") == 0 ) {
    192 			argc--;
    193 			argv++;
    194 			eap = 1;
    195 #ifdef CONFIG_GACT
    196 			if (!gact_ld) {
    197 				get_action_kind("gact");
    198 			}
    199 #endif
    200 			continue;
    201 		} else if (strcmp(*argv, "flowid") == 0) {
    202 			break;
    203 		} else if (strcmp(*argv, "classid") == 0) {
    204 			break;
    205 		} else if (strcmp(*argv, "help") == 0) {
    206 			return -1;
    207 		} else if (new_cmd(argv)) {
    208 			goto done0;
    209 		} else {
    210 			struct action_util *a = NULL;
    211 			strncpy(k, *argv, sizeof (k) - 1);
    212 			eap = 0;
    213 			if (argc > 0 ) {
    214 				a = get_action_kind(k);
    215 			} else {
    216 done0:
    217 				if (ok)
    218 					break;
    219 				else
    220 					goto done;
    221 			}
    222 
    223 			if (NULL == a) {
    224 				goto bad_val;
    225 			}
    226 
    227 			tail = NLMSG_TAIL(n);
    228 			addattr_l(n, MAX_MSG, ++prio, NULL, 0);
    229 			addattr_l(n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
    230 
    231 			ret = a->parse_aopt(a,&argc, &argv, TCA_ACT_OPTIONS, n);
    232 
    233 			if (ret < 0) {
    234 				fprintf(stderr,"bad action parsing\n");
    235 				goto bad_val;
    236 			}
    237 			tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
    238 			ok++;
    239 		}
    240 
    241 	}
    242 
    243 	if (eap > 0) {
    244 		fprintf(stderr,"bad action empty %d\n",eap);
    245 		goto bad_val;
    246 	}
    247 
    248 	tail2->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail2;
    249 
    250 done:
    251 	*argc_p = argc;
    252 	*argv_p = argv;
    253 	return 0;
    254 bad_val:
    255 	/* no need to undo things, returning from here should
    256 	 * cause enough pain */
    257 	fprintf(stderr, "parse_action: bad value (%d:%s)!\n",argc,*argv);
    258 	return -1;
    259 }
    260 
    261 static int
    262 tc_print_one_action(FILE * f, struct rtattr *arg)
    263 {
    264 
    265 	struct rtattr *tb[TCA_ACT_MAX + 1];
    266 	int err = 0;
    267 	struct action_util *a = NULL;
    268 
    269 	if (arg == NULL)
    270 		return -1;
    271 
    272 	parse_rtattr_nested(tb, TCA_ACT_MAX, arg);
    273 
    274 	if (tb[TCA_ACT_KIND] == NULL) {
    275 		fprintf(stderr, "NULL Action!\n");
    276 		return -1;
    277 	}
    278 
    279 
    280 	a = get_action_kind(RTA_DATA(tb[TCA_ACT_KIND]));
    281 	if (NULL == a)
    282 		return err;
    283 
    284 	err = a->print_aopt(a, f, tb[TCA_ACT_OPTIONS]);
    285 
    286 	if (0 > err)
    287 		return err;
    288 
    289 	if (show_stats && tb[TCA_ACT_STATS]) {
    290 		fprintf(f, "\tAction statistics:\n");
    291 		print_tcstats2_attr(f, tb[TCA_ACT_STATS], "\t", NULL);
    292 		fprintf(f, "\n");
    293 	}
    294 
    295 	return 0;
    296 }
    297 
    298 static int
    299 tc_print_action_flush(FILE *f, const struct rtattr *arg)
    300 {
    301 
    302 	struct rtattr *tb[TCA_MAX + 1];
    303 	int err = 0;
    304 	struct action_util *a = NULL;
    305 	__u32 *delete_count = 0;
    306 
    307 	parse_rtattr_nested(tb, TCA_MAX, arg);
    308 
    309 	if (tb[TCA_KIND] == NULL) {
    310 		fprintf(stderr, "NULL Action!\n");
    311 		return -1;
    312 	}
    313 
    314 	a = get_action_kind(RTA_DATA(tb[TCA_KIND]));
    315 	if (NULL == a)
    316 		return err;
    317 
    318 	delete_count = RTA_DATA(tb[TCA_FCNT]);
    319 	fprintf(f," %s (%d entries)\n", a->id, *delete_count);
    320 	tab_flush = 0;
    321 	return 0;
    322 }
    323 
    324 int
    325 tc_print_action(FILE *f, const struct rtattr *arg)
    326 {
    327 
    328 	int i;
    329 	struct rtattr *tb[TCA_ACT_MAX_PRIO + 1];
    330 
    331 	if (arg == NULL)
    332 		return 0;
    333 
    334 	parse_rtattr_nested(tb, TCA_ACT_MAX_PRIO, arg);
    335 
    336 	if (tab_flush && NULL != tb[0]  && NULL == tb[1])
    337 		return tc_print_action_flush(f, tb[0]);
    338 
    339 	for (i = 0; i < TCA_ACT_MAX_PRIO; i++) {
    340 		if (tb[i]) {
    341 			fprintf(f, "\n\taction order %d: ", i);
    342 			if (0 > tc_print_one_action(f, tb[i])) {
    343 				fprintf(f, "Error printing action\n");
    344 			}
    345 		}
    346 
    347 	}
    348 
    349 	return 0;
    350 }
    351 
    352 int print_action(const struct sockaddr_nl *who,
    353 			   struct nlmsghdr *n,
    354 			   void *arg)
    355 {
    356 	FILE *fp = (FILE*)arg;
    357 	struct tcamsg *t = NLMSG_DATA(n);
    358 	int len = n->nlmsg_len;
    359 	struct rtattr * tb[TCAA_MAX+1];
    360 
    361 	len -= NLMSG_LENGTH(sizeof(*t));
    362 
    363 	if (len < 0) {
    364 		fprintf(stderr, "Wrong len %d\n", len);
    365 		return -1;
    366 	}
    367 
    368 	parse_rtattr(tb, TCAA_MAX, TA_RTA(t), len);
    369 
    370 	if (NULL == tb[TCA_ACT_TAB]) {
    371 		if (n->nlmsg_type != RTM_GETACTION)
    372 			fprintf(stderr, "print_action: NULL kind\n");
    373 		return -1;
    374 	}
    375 
    376 	if (n->nlmsg_type == RTM_DELACTION) {
    377 		if (n->nlmsg_flags & NLM_F_ROOT) {
    378 			fprintf(fp, "Flushed table ");
    379 			tab_flush = 1;
    380 		} else {
    381 			fprintf(fp, "deleted action ");
    382 		}
    383 	}
    384 
    385 	if (n->nlmsg_type == RTM_NEWACTION)
    386 		fprintf(fp, "Added action ");
    387 	tc_print_action(fp, tb[TCA_ACT_TAB]);
    388 
    389 	return 0;
    390 }
    391 
    392 static int tc_action_gd(int cmd, unsigned flags, int *argc_p, char ***argv_p)
    393 {
    394 	char k[16];
    395 	struct action_util *a = NULL;
    396 	int argc = *argc_p;
    397 	char **argv = *argv_p;
    398 	int prio = 0;
    399 	int ret = 0;
    400 	__u32 i;
    401 	struct sockaddr_nl nladdr;
    402 	struct rtattr *tail;
    403 	struct rtattr *tail2;
    404 	struct nlmsghdr *ans = NULL;
    405 
    406 	struct {
    407 		struct nlmsghdr         n;
    408 		struct tcamsg           t;
    409 		char                    buf[MAX_MSG];
    410 	} req;
    411 
    412 	req.t.tca_family = AF_UNSPEC;
    413 
    414 	memset(&req, 0, sizeof(req));
    415 
    416 	memset(&nladdr, 0, sizeof(nladdr));
    417 	nladdr.nl_family = AF_NETLINK;
    418 
    419 	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg));
    420 	req.n.nlmsg_flags = NLM_F_REQUEST|flags;
    421 	req.n.nlmsg_type = cmd;
    422 	argc -=1;
    423 	argv +=1;
    424 
    425 
    426 	tail = NLMSG_TAIL(&req.n);
    427 	addattr_l(&req.n, MAX_MSG, TCA_ACT_TAB, NULL, 0);
    428 
    429 	while (argc > 0) {
    430 		if (strcmp(*argv, "action") == 0 ) {
    431 			argc--;
    432 			argv++;
    433 			continue;
    434 		} else if (strcmp(*argv, "help") == 0) {
    435 			return -1;
    436 		}
    437 
    438 		strncpy(k, *argv, sizeof (k) - 1);
    439 		a = get_action_kind(k);
    440 		if (NULL == a) {
    441 			fprintf(stderr, "Error: non existent action: %s\n",k);
    442 			ret = -1;
    443 			goto bad_val;
    444 		}
    445 		if (strcmp(a->id, k) != 0) {
    446 			fprintf(stderr, "Error: non existent action: %s\n",k);
    447 			ret = -1;
    448 			goto bad_val;
    449 		}
    450 
    451 		argc -=1;
    452 		argv +=1;
    453 		if (argc <= 0) {
    454 			fprintf(stderr, "Error: no index specified action: %s\n",k);
    455 			ret = -1;
    456 			goto bad_val;
    457 		}
    458 
    459 		if (matches(*argv, "index") == 0) {
    460 			NEXT_ARG();
    461 			if (get_u32(&i, *argv, 10)) {
    462 				fprintf(stderr, "Illegal \"index\"\n");
    463 				ret = -1;
    464 				goto bad_val;
    465 			}
    466 			argc -=1;
    467 			argv +=1;
    468 		} else {
    469 			fprintf(stderr, "Error: no index specified action: %s\n",k);
    470 			ret = -1;
    471 			goto bad_val;
    472 		}
    473 
    474 		tail2 = NLMSG_TAIL(&req.n);
    475 		addattr_l(&req.n, MAX_MSG, ++prio, NULL, 0);
    476 		addattr_l(&req.n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
    477 		addattr32(&req.n, MAX_MSG, TCA_ACT_INDEX, i);
    478 		tail2->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail2;
    479 
    480 	}
    481 
    482 	tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
    483 
    484 	req.n.nlmsg_seq = rth.dump = ++rth.seq;
    485 	if (cmd == RTM_GETACTION)
    486 		ans = &req.n;
    487 
    488 	if (rtnl_talk(&rth, &req.n, ans, MAX_MSG) < 0) {
    489 		fprintf(stderr, "We have an error talking to the kernel\n");
    490 		return 1;
    491 	}
    492 
    493 	if (ans && print_action(NULL, &req.n, (void*)stdout) < 0) {
    494 		fprintf(stderr, "Dump terminated\n");
    495 		return 1;
    496 	}
    497 
    498 	*argc_p = argc;
    499 	*argv_p = argv;
    500 bad_val:
    501 	return ret;
    502 }
    503 
    504 static int tc_action_modify(int cmd, unsigned flags, int *argc_p, char ***argv_p)
    505 {
    506 	int argc = *argc_p;
    507 	char **argv = *argv_p;
    508 	int ret = 0;
    509 
    510 	struct rtattr *tail;
    511 	struct {
    512 		struct nlmsghdr         n;
    513 		struct tcamsg           t;
    514 		char                    buf[MAX_MSG];
    515 	} req;
    516 
    517 	req.t.tca_family = AF_UNSPEC;
    518 
    519 	memset(&req, 0, sizeof(req));
    520 
    521 	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg));
    522 	req.n.nlmsg_flags = NLM_F_REQUEST|flags;
    523 	req.n.nlmsg_type = cmd;
    524 	tail = NLMSG_TAIL(&req.n);
    525 	argc -=1;
    526 	argv +=1;
    527 	if (parse_action(&argc, &argv, TCA_ACT_TAB, &req.n)) {
    528 		fprintf(stderr, "Illegal \"action\"\n");
    529 		return -1;
    530 	}
    531 	tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
    532 
    533 	if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) {
    534 		fprintf(stderr, "We have an error talking to the kernel\n");
    535 		ret = -1;
    536 	}
    537 
    538 	*argc_p = argc;
    539 	*argv_p = argv;
    540 
    541 	return ret;
    542 }
    543 
    544 static int tc_act_list_or_flush(int argc, char **argv, int event)
    545 {
    546 	int ret = 0, prio = 0, msg_size = 0;
    547 	char k[16];
    548 	struct rtattr *tail,*tail2;
    549 	struct action_util *a = NULL;
    550 	struct {
    551 		struct nlmsghdr         n;
    552 		struct tcamsg           t;
    553 		char                    buf[MAX_MSG];
    554 	} req;
    555 
    556 	req.t.tca_family = AF_UNSPEC;
    557 
    558 	memset(&req, 0, sizeof(req));
    559 
    560 	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcamsg));
    561 
    562 	tail = NLMSG_TAIL(&req.n);
    563 	addattr_l(&req.n, MAX_MSG, TCA_ACT_TAB, NULL, 0);
    564 	tail2 = NLMSG_TAIL(&req.n);
    565 
    566 	strncpy(k, *argv, sizeof (k) - 1);
    567 #ifdef CONFIG_GACT
    568 	if (!gact_ld) {
    569 		get_action_kind("gact");
    570 	}
    571 #endif
    572 	a = get_action_kind(k);
    573 	if (NULL == a) {
    574 		fprintf(stderr,"bad action %s\n",k);
    575 		goto bad_val;
    576 	}
    577 	if (strcmp(a->id, k) != 0) {
    578 		fprintf(stderr,"bad action %s\n",k);
    579 		goto bad_val;
    580 	}
    581 	strncpy(k, *argv, sizeof (k) - 1);
    582 
    583 	addattr_l(&req.n, MAX_MSG, ++prio, NULL, 0);
    584 	addattr_l(&req.n, MAX_MSG, TCA_ACT_KIND, k, strlen(k) + 1);
    585 	tail2->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail2;
    586 	tail->rta_len = (void *) NLMSG_TAIL(&req.n) - (void *) tail;
    587 
    588 	msg_size = NLMSG_ALIGN(req.n.nlmsg_len) - NLMSG_ALIGN(sizeof(struct nlmsghdr));
    589 
    590 	if (event == RTM_GETACTION) {
    591 		if (rtnl_dump_request(&rth, event, (void *)&req.t, msg_size) < 0) {
    592 			perror("Cannot send dump request");
    593 			return 1;
    594 		}
    595 		ret = rtnl_dump_filter(&rth, print_action, stdout);
    596 	}
    597 
    598 	if (event == RTM_DELACTION) {
    599 		req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len);
    600 		req.n.nlmsg_type = RTM_DELACTION;
    601 		req.n.nlmsg_flags |= NLM_F_ROOT;
    602 		req.n.nlmsg_flags |= NLM_F_REQUEST;
    603 		if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) {
    604 			fprintf(stderr, "We have an error flushing\n");
    605 			return 1;
    606 		}
    607 
    608 	}
    609 
    610 bad_val:
    611 
    612 	return ret;
    613 }
    614 
    615 int do_action(int argc, char **argv)
    616 {
    617 
    618 	int ret = 0;
    619 
    620 	while (argc > 0) {
    621 
    622 		if (matches(*argv, "add") == 0) {
    623 			ret =  tc_action_modify(RTM_NEWACTION, NLM_F_EXCL|NLM_F_CREATE, &argc, &argv);
    624 		} else if (matches(*argv, "change") == 0 ||
    625 			  matches(*argv, "replace") == 0) {
    626 			ret = tc_action_modify(RTM_NEWACTION, NLM_F_CREATE|NLM_F_REPLACE, &argc, &argv);
    627 		} else if (matches(*argv, "delete") == 0) {
    628 			argc -=1;
    629 			argv +=1;
    630 			ret = tc_action_gd(RTM_DELACTION, 0,  &argc, &argv);
    631 		} else if (matches(*argv, "get") == 0) {
    632 			argc -=1;
    633 			argv +=1;
    634 			ret = tc_action_gd(RTM_GETACTION, 0,  &argc, &argv);
    635 		} else if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
    636 						|| matches(*argv, "lst") == 0) {
    637 			if (argc <= 2) {
    638 				act_usage();
    639 				return -1;
    640 			}
    641 			return tc_act_list_or_flush(argc-2, argv+2, RTM_GETACTION);
    642 		} else if (matches(*argv, "flush") == 0) {
    643 			if (argc <= 2) {
    644 				act_usage();
    645 				return -1;
    646 			}
    647 			return tc_act_list_or_flush(argc-2, argv+2, RTM_DELACTION);
    648 		} else if (matches(*argv, "help") == 0) {
    649 			act_usage();
    650 			return -1;
    651 		} else {
    652 
    653 			ret = -1;
    654 		}
    655 
    656 		if (ret < 0) {
    657 			fprintf(stderr, "Command \"%s\" is unknown, try \"tc actions help\".\n", *argv);
    658 			return -1;
    659 		}
    660 	}
    661 
    662 	return 0;
    663 }
    664