blob: 4d9f4dcef3b012e3c0babc981369e68511aaad3c [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
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@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"
site!shemmingerc1027a72005-03-14 22:19:16 +000025#include "tc_common.h"
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000026#include "tc_util.h"
27
28static void explain(void)
29{
30 fprintf(stderr, "Usage: ... route [ from REALM | fromif TAG ] [ to REALM ]\n");
Phil Sutter0a83e1e2015-10-23 19:21:17 +020031 fprintf(stderr, " [ classid CLASSID ] [ action ACTION_SPEC ]\n");
Jamal Hadi Salim287bf3a2014-10-06 07:30:17 -040032 fprintf(stderr, " ACTION_SPEC := ... look at individual actions\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000033 fprintf(stderr, " CLASSID := X:Y\n");
PJ Waskiewicze9acc242008-02-13 03:49:09 -080034 fprintf(stderr, "\nNOTE: CLASSID is parsed as hexadecimal input.\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000035}
36
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000037static int route_parse_opt(struct filter_util *qu, char *handle, int argc, char **argv, struct nlmsghdr *n)
38{
39 struct tc_police tp;
40 struct tcmsg *t = NLMSG_DATA(n);
41 struct rtattr *tail;
42 __u32 fh = 0xFFFF8000;
43 __u32 order = 0;
44
45 memset(&tp, 0, sizeof(tp));
46
47 if (handle) {
48 if (get_u32(&t->tcm_handle, handle, 0)) {
49 fprintf(stderr, "Illegal \"handle\"\n");
50 return -1;
51 }
52 }
53
54 if (argc == 0)
55 return 0;
56
4!tgraf228569c2005-01-18 01:24:18 +000057 tail = NLMSG_TAIL(n);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000058 addattr_l(n, 4096, TCA_OPTIONS, NULL, 0);
59
60 while (argc > 0) {
61 if (matches(*argv, "to") == 0) {
62 __u32 id;
Stephen Hemminger32a121c2016-03-21 11:48:36 -070063
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000064 NEXT_ARG();
65 if (rtnl_rtrealm_a2n(&id, *argv)) {
66 fprintf(stderr, "Illegal \"to\"\n");
67 return -1;
68 }
69 addattr_l(n, 4096, TCA_ROUTE4_TO, &id, 4);
70 fh &= ~0x80FF;
71 fh |= id&0xFF;
72 } else if (matches(*argv, "from") == 0) {
73 __u32 id;
Stephen Hemminger32a121c2016-03-21 11:48:36 -070074
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000075 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) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000084 __u32 id;
Stephen Hemminger32a121c2016-03-21 11:48:36 -070085
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000086 NEXT_ARG();
site!shemmingerc1027a72005-03-14 22:19:16 +000087 ll_init_map(&rth);
Stephen Hemminger32a121c2016-03-21 11:48:36 -070088 if ((id = ll_name_to_index(*argv)) <= 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000089 fprintf(stderr, "Illegal \"fromif\"\n");
90 return -1;
91 }
92 addattr_l(n, 4096, TCA_ROUTE4_IIF, &id, 4);
93 fh &= 0xFFFF;
94 fh |= (0x8000|id)<<16;
95 } else if (matches(*argv, "classid") == 0 ||
96 strcmp(*argv, "flowid") == 0) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -070097 unsigned int handle;
98
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000099 NEXT_ARG();
100 if (get_tc_classid(&handle, *argv)) {
101 fprintf(stderr, "Illegal \"classid\"\n");
102 return -1;
103 }
104 addattr_l(n, 4096, TCA_ROUTE4_CLASSID, &handle, 4);
105 } else if (matches(*argv, "police") == 0) {
106 NEXT_ARG();
107 if (parse_police(&argc, &argv, TCA_ROUTE4_POLICE, n)) {
108 fprintf(stderr, "Illegal \"police\"\n");
109 return -1;
110 }
111 continue;
Jamal Hadi Salim287bf3a2014-10-06 07:30:17 -0400112 } else if (matches(*argv, "action") == 0) {
113 NEXT_ARG();
114 if (parse_action(&argc, &argv, TCA_ROUTE4_ACT, n)) {
115 fprintf(stderr, "Illegal \"action\"\n");
116 return -1;
117 }
118 continue;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000119 } else if (matches(*argv, "order") == 0) {
120 NEXT_ARG();
121 if (get_u32(&order, *argv, 0)) {
122 fprintf(stderr, "Illegal \"order\"\n");
123 return -1;
124 }
125 } else if (strcmp(*argv, "help") == 0) {
126 explain();
127 return -1;
128 } else {
129 fprintf(stderr, "What is \"%s\"?\n", *argv);
130 explain();
131 return -1;
132 }
133 argc--; argv++;
134 }
4!tgraf228569c2005-01-18 01:24:18 +0000135 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000136 if (order) {
137 fh &= ~0x7F00;
138 fh |= (order<<8)&0x7F00;
139 }
140 if (!t->tcm_handle)
141 t->tcm_handle = fh;
142 return 0;
143}
144
145static int route_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 handle)
146{
147 struct rtattr *tb[TCA_ROUTE4_MAX+1];
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700148
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000149 SPRINT_BUF(b1);
150
151 if (opt == NULL)
152 return 0;
153
4!tgraf3b3ecd32005-01-18 22:11:58 +0000154 parse_rtattr_nested(tb, TCA_ROUTE4_MAX, opt);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000155
156 if (handle)
157 fprintf(f, "fh 0x%08x ", handle);
158 if (handle&0x7F00)
159 fprintf(f, "order %d ", (handle>>8)&0x7F);
160
161 if (tb[TCA_ROUTE4_CLASSID]) {
162 SPRINT_BUF(b1);
Stephen Hemmingerff247462012-04-10 08:47:55 -0700163 fprintf(f, "flowid %s ", sprint_tc_classid(rta_getattr_u32(tb[TCA_ROUTE4_CLASSID]), b1));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000164 }
165 if (tb[TCA_ROUTE4_TO])
Stephen Hemmingerff247462012-04-10 08:47:55 -0700166 fprintf(f, "to %s ", rtnl_rtrealm_n2a(rta_getattr_u32(tb[TCA_ROUTE4_TO]), b1, sizeof(b1)));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000167 if (tb[TCA_ROUTE4_FROM])
Stephen Hemmingerff247462012-04-10 08:47:55 -0700168 fprintf(f, "from %s ", rtnl_rtrealm_n2a(rta_getattr_u32(tb[TCA_ROUTE4_FROM]), b1, sizeof(b1)));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000169 if (tb[TCA_ROUTE4_IIF])
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700170 fprintf(f, "fromif %s", ll_index_to_name(*(int *)RTA_DATA(tb[TCA_ROUTE4_IIF])));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000171 if (tb[TCA_ROUTE4_POLICE])
172 tc_print_police(f, tb[TCA_ROUTE4_POLICE]);
Jamal Hadi Salim287bf3a2014-10-06 07:30:17 -0400173 if (tb[TCA_ROUTE4_ACT])
174 tc_print_action(f, tb[TCA_ROUTE4_ACT]);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000175 return 0;
176}
177
osdl.net!shemminger6b7dff12004-09-28 18:35:49 +0000178struct filter_util route_filter_util = {
179 .id = "route",
180 .parse_fopt = route_parse_opt,
181 .print_fopt = route_print_opt,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000182};