]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/f_route.c
iproute: Set ip/ip6 lwtunnel flags
[mirror_iproute2.git] / tc / f_route.c
CommitLineData
aba5acdf
SH
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@ms2.inr.ac.ru>
10 *
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
aba5acdf
SH
16#include <fcntl.h>
17#include <sys/socket.h>
18#include <netinet/in.h>
19#include <arpa/inet.h>
20#include <string.h>
21
22#include "utils.h"
23#include "rt_names.h"
c1027a75 24#include "tc_common.h"
aba5acdf
SH
25#include "tc_util.h"
26
27static void explain(void)
28{
29 fprintf(stderr, "Usage: ... route [ from REALM | fromif TAG ] [ to REALM ]\n");
0a83e1ea 30 fprintf(stderr, " [ classid CLASSID ] [ action ACTION_SPEC ]\n");
287bf3a9 31 fprintf(stderr, " ACTION_SPEC := ... look at individual actions\n");
aba5acdf 32 fprintf(stderr, " CLASSID := X:Y\n");
e9acc242 33 fprintf(stderr, "\nNOTE: CLASSID is parsed as hexadecimal input.\n");
aba5acdf
SH
34}
35
aba5acdf
SH
36static int route_parse_opt(struct filter_util *qu, char *handle, int argc, char **argv, struct nlmsghdr *n)
37{
aba5acdf
SH
38 struct tcmsg *t = NLMSG_DATA(n);
39 struct rtattr *tail;
40 __u32 fh = 0xFFFF8000;
41 __u32 order = 0;
42
aba5acdf
SH
43 if (handle) {
44 if (get_u32(&t->tcm_handle, handle, 0)) {
45 fprintf(stderr, "Illegal \"handle\"\n");
46 return -1;
47 }
48 }
49
50 if (argc == 0)
51 return 0;
52
c14f9d92 53 tail = addattr_nest(n, 4096, TCA_OPTIONS);
aba5acdf
SH
54
55 while (argc > 0) {
56 if (matches(*argv, "to") == 0) {
57 __u32 id;
32a121cb 58
aba5acdf
SH
59 NEXT_ARG();
60 if (rtnl_rtrealm_a2n(&id, *argv)) {
61 fprintf(stderr, "Illegal \"to\"\n");
62 return -1;
63 }
64 addattr_l(n, 4096, TCA_ROUTE4_TO, &id, 4);
65 fh &= ~0x80FF;
66 fh |= id&0xFF;
67 } else if (matches(*argv, "from") == 0) {
68 __u32 id;
32a121cb 69
aba5acdf
SH
70 NEXT_ARG();
71 if (rtnl_rtrealm_a2n(&id, *argv)) {
72 fprintf(stderr, "Illegal \"from\"\n");
73 return -1;
74 }
75 addattr_l(n, 4096, TCA_ROUTE4_FROM, &id, 4);
76 fh &= 0xFFFF;
77 fh |= id<<16;
78 } else if (matches(*argv, "fromif") == 0) {
aba5acdf 79 __u32 id;
32a121cb 80
aba5acdf 81 NEXT_ARG();
c1027a75 82 ll_init_map(&rth);
32a121cb 83 if ((id = ll_name_to_index(*argv)) <= 0) {
aba5acdf
SH
84 fprintf(stderr, "Illegal \"fromif\"\n");
85 return -1;
86 }
87 addattr_l(n, 4096, TCA_ROUTE4_IIF, &id, 4);
88 fh &= 0xFFFF;
89 fh |= (0x8000|id)<<16;
90 } else if (matches(*argv, "classid") == 0 ||
91 strcmp(*argv, "flowid") == 0) {
32a121cb
SH
92 unsigned int handle;
93
aba5acdf
SH
94 NEXT_ARG();
95 if (get_tc_classid(&handle, *argv)) {
96 fprintf(stderr, "Illegal \"classid\"\n");
97 return -1;
98 }
99 addattr_l(n, 4096, TCA_ROUTE4_CLASSID, &handle, 4);
100 } else if (matches(*argv, "police") == 0) {
101 NEXT_ARG();
102 if (parse_police(&argc, &argv, TCA_ROUTE4_POLICE, n)) {
103 fprintf(stderr, "Illegal \"police\"\n");
104 return -1;
105 }
106 continue;
287bf3a9
JHS
107 } else if (matches(*argv, "action") == 0) {
108 NEXT_ARG();
109 if (parse_action(&argc, &argv, TCA_ROUTE4_ACT, n)) {
110 fprintf(stderr, "Illegal \"action\"\n");
111 return -1;
112 }
113 continue;
aba5acdf
SH
114 } else if (matches(*argv, "order") == 0) {
115 NEXT_ARG();
116 if (get_u32(&order, *argv, 0)) {
117 fprintf(stderr, "Illegal \"order\"\n");
118 return -1;
119 }
120 } else if (strcmp(*argv, "help") == 0) {
121 explain();
122 return -1;
123 } else {
124 fprintf(stderr, "What is \"%s\"?\n", *argv);
125 explain();
126 return -1;
127 }
128 argc--; argv++;
129 }
c14f9d92 130 addattr_nest_end(n, tail);
aba5acdf
SH
131 if (order) {
132 fh &= ~0x7F00;
133 fh |= (order<<8)&0x7F00;
134 }
135 if (!t->tcm_handle)
136 t->tcm_handle = fh;
137 return 0;
138}
139
140static int route_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 handle)
141{
142 struct rtattr *tb[TCA_ROUTE4_MAX+1];
32a121cb 143
aba5acdf
SH
144 SPRINT_BUF(b1);
145
146 if (opt == NULL)
147 return 0;
148
3b3ecd31 149 parse_rtattr_nested(tb, TCA_ROUTE4_MAX, opt);
aba5acdf
SH
150
151 if (handle)
152 fprintf(f, "fh 0x%08x ", handle);
153 if (handle&0x7F00)
154 fprintf(f, "order %d ", (handle>>8)&0x7F);
155
156 if (tb[TCA_ROUTE4_CLASSID]) {
157 SPRINT_BUF(b1);
ff24746c 158 fprintf(f, "flowid %s ", sprint_tc_classid(rta_getattr_u32(tb[TCA_ROUTE4_CLASSID]), b1));
aba5acdf
SH
159 }
160 if (tb[TCA_ROUTE4_TO])
ff24746c 161 fprintf(f, "to %s ", rtnl_rtrealm_n2a(rta_getattr_u32(tb[TCA_ROUTE4_TO]), b1, sizeof(b1)));
aba5acdf 162 if (tb[TCA_ROUTE4_FROM])
ff24746c 163 fprintf(f, "from %s ", rtnl_rtrealm_n2a(rta_getattr_u32(tb[TCA_ROUTE4_FROM]), b1, sizeof(b1)));
aba5acdf 164 if (tb[TCA_ROUTE4_IIF])
9f1370c0 165 fprintf(f, "fromif %s", ll_index_to_name(rta_getattr_u32(tb[TCA_ROUTE4_IIF])));
aba5acdf
SH
166 if (tb[TCA_ROUTE4_POLICE])
167 tc_print_police(f, tb[TCA_ROUTE4_POLICE]);
287bf3a9 168 if (tb[TCA_ROUTE4_ACT])
9e713525 169 tc_print_action(f, tb[TCA_ROUTE4_ACT], 0);
aba5acdf
SH
170 return 0;
171}
172
6b7dff17
SH
173struct filter_util route_filter_util = {
174 .id = "route",
175 .parse_fopt = route_parse_opt,
176 .print_fopt = route_print_opt,
aba5acdf 177};