Home | History | Annotate | Download | only in tc
      1 /*
      2  * f_route.c		ROUTE filter.
      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 <arpa/inet.h>
     21 #include <string.h>
     22 
     23 #include "utils.h"
     24 #include "rt_names.h"
     25 #include "tc_common.h"
     26 #include "tc_util.h"
     27 
     28 static void explain(void)
     29 {
     30 	fprintf(stderr, "Usage: ... route [ from REALM | fromif TAG ] [ to REALM ]\n");
     31 	fprintf(stderr, "                [ flowid CLASSID ] [ police POLICE_SPEC ]\n");
     32 	fprintf(stderr, "       POLICE_SPEC := ... look at TBF\n");
     33 	fprintf(stderr, "       CLASSID := X:Y\n");
     34 	fprintf(stderr, "\nNOTE: CLASSID is parsed as hexadecimal input.\n");
     35 }
     36 
     37 #define usage() return(-1)
     38 
     39 static int route_parse_opt(struct filter_util *qu, char *handle, int argc, char **argv, struct nlmsghdr *n)
     40 {
     41 	struct tc_police tp;
     42 	struct tcmsg *t = NLMSG_DATA(n);
     43 	struct rtattr *tail;
     44 	__u32 fh = 0xFFFF8000;
     45 	__u32 order = 0;
     46 
     47 	memset(&tp, 0, sizeof(tp));
     48 
     49 	if (handle) {
     50 		if (get_u32(&t->tcm_handle, handle, 0)) {
     51 			fprintf(stderr, "Illegal \"handle\"\n");
     52 			return -1;
     53 		}
     54 	}
     55 
     56 	if (argc == 0)
     57 		return 0;
     58 
     59 	tail = NLMSG_TAIL(n);
     60 	addattr_l(n, 4096, TCA_OPTIONS, NULL, 0);
     61 
     62 	while (argc > 0) {
     63 		if (matches(*argv, "to") == 0) {
     64 			__u32 id;
     65 			NEXT_ARG();
     66 			if (rtnl_rtrealm_a2n(&id, *argv)) {
     67 				fprintf(stderr, "Illegal \"to\"\n");
     68 				return -1;
     69 			}
     70 			addattr_l(n, 4096, TCA_ROUTE4_TO, &id, 4);
     71 			fh &= ~0x80FF;
     72 			fh |= id&0xFF;
     73 		} else if (matches(*argv, "from") == 0) {
     74 			__u32 id;
     75 			NEXT_ARG();
     76 			if (rtnl_rtrealm_a2n(&id, *argv)) {
     77 				fprintf(stderr, "Illegal \"from\"\n");
     78 				return -1;
     79 			}
     80 			addattr_l(n, 4096, TCA_ROUTE4_FROM, &id, 4);
     81 			fh &= 0xFFFF;
     82 			fh |= id<<16;
     83 		} else if (matches(*argv, "fromif") == 0) {
     84 			__u32 id;
     85 			NEXT_ARG();
     86 			ll_init_map(&rth);
     87 			if ((id=ll_name_to_index(*argv)) <= 0) {
     88 				fprintf(stderr, "Illegal \"fromif\"\n");
     89 				return -1;
     90 			}
     91 			addattr_l(n, 4096, TCA_ROUTE4_IIF, &id, 4);
     92 			fh &= 0xFFFF;
     93 			fh |= (0x8000|id)<<16;
     94 		} else if (matches(*argv, "classid") == 0 ||
     95 			   strcmp(*argv, "flowid") == 0) {
     96 			unsigned handle;
     97 			NEXT_ARG();
     98 			if (get_tc_classid(&handle, *argv)) {
     99 				fprintf(stderr, "Illegal \"classid\"\n");
    100 				return -1;
    101 			}
    102 			addattr_l(n, 4096, TCA_ROUTE4_CLASSID, &handle, 4);
    103 		} else if (matches(*argv, "police") == 0) {
    104 			NEXT_ARG();
    105 			if (parse_police(&argc, &argv, TCA_ROUTE4_POLICE, n)) {
    106 				fprintf(stderr, "Illegal \"police\"\n");
    107 				return -1;
    108 			}
    109 			continue;
    110 		} else if (matches(*argv, "order") == 0) {
    111 			NEXT_ARG();
    112 			if (get_u32(&order, *argv, 0)) {
    113 				fprintf(stderr, "Illegal \"order\"\n");
    114 				return -1;
    115 			}
    116 		} else if (strcmp(*argv, "help") == 0) {
    117 			explain();
    118 			return -1;
    119 		} else {
    120 			fprintf(stderr, "What is \"%s\"?\n", *argv);
    121 			explain();
    122 			return -1;
    123 		}
    124 		argc--; argv++;
    125 	}
    126 	tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
    127 	if (order) {
    128 		fh &= ~0x7F00;
    129 		fh |= (order<<8)&0x7F00;
    130 	}
    131 	if (!t->tcm_handle)
    132 		t->tcm_handle = fh;
    133 	return 0;
    134 }
    135 
    136 static int route_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 handle)
    137 {
    138 	struct rtattr *tb[TCA_ROUTE4_MAX+1];
    139 	SPRINT_BUF(b1);
    140 
    141 	if (opt == NULL)
    142 		return 0;
    143 
    144 	parse_rtattr_nested(tb, TCA_ROUTE4_MAX, opt);
    145 
    146 	if (handle)
    147 		fprintf(f, "fh 0x%08x ", handle);
    148 	if (handle&0x7F00)
    149 		fprintf(f, "order %d ", (handle>>8)&0x7F);
    150 
    151 	if (tb[TCA_ROUTE4_CLASSID]) {
    152 		SPRINT_BUF(b1);
    153 		fprintf(f, "flowid %s ", sprint_tc_classid(*(__u32*)RTA_DATA(tb[TCA_ROUTE4_CLASSID]), b1));
    154 	}
    155 	if (tb[TCA_ROUTE4_TO])
    156 		fprintf(f, "to %s ", rtnl_rtrealm_n2a(*(__u32*)RTA_DATA(tb[TCA_ROUTE4_TO]), b1, sizeof(b1)));
    157 	if (tb[TCA_ROUTE4_FROM])
    158 		fprintf(f, "from %s ", rtnl_rtrealm_n2a(*(__u32*)RTA_DATA(tb[TCA_ROUTE4_FROM]), b1, sizeof(b1)));
    159 	if (tb[TCA_ROUTE4_IIF])
    160 		fprintf(f, "fromif %s", ll_index_to_name(*(int*)RTA_DATA(tb[TCA_ROUTE4_IIF])));
    161 	if (tb[TCA_ROUTE4_POLICE])
    162 		tc_print_police(f, tb[TCA_ROUTE4_POLICE]);
    163 	return 0;
    164 }
    165 
    166 struct filter_util route_filter_util = {
    167 	.id = "route",
    168 	.parse_fopt = route_parse_opt,
    169 	.print_fopt = route_print_opt,
    170 };
    171