]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/f_route.c
ip fou: pass family attribute as u8
[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
228569c3 53 tail = NLMSG_TAIL(n);
aba5acdf
SH
54 addattr_l(n, 4096, TCA_OPTIONS, NULL, 0);
55
56 while (argc > 0) {
57 if (matches(*argv, "to") == 0) {
58 __u32 id;
32a121cb 59
aba5acdf
SH
60 NEXT_ARG();
61 if (rtnl_rtrealm_a2n(&id, *argv)) {
62 fprintf(stderr, "Illegal \"to\"\n");
63 return -1;
64 }
65 addattr_l(n, 4096, TCA_ROUTE4_TO, &id, 4);
66 fh &= ~0x80FF;
67 fh |= id&0xFF;
68 } else if (matches(*argv, "from") == 0) {
69 __u32 id;
32a121cb 70
aba5acdf
SH
71 NEXT_ARG();
72 if (rtnl_rtrealm_a2n(&id, *argv)) {
73 fprintf(stderr, "Illegal \"from\"\n");
74 return -1;
75 }
76 addattr_l(n, 4096, TCA_ROUTE4_FROM, &id, 4);
77 fh &= 0xFFFF;
78 fh |= id<<16;
79 } else if (matches(*argv, "fromif") == 0) {
aba5acdf 80 __u32 id;
32a121cb 81
aba5acdf 82 NEXT_ARG();
c1027a75 83 ll_init_map(&rth);
32a121cb 84 if ((id = ll_name_to_index(*argv)) <= 0) {
aba5acdf
SH
85 fprintf(stderr, "Illegal \"fromif\"\n");
86 return -1;
87 }
88 addattr_l(n, 4096, TCA_ROUTE4_IIF, &id, 4);
89 fh &= 0xFFFF;
90 fh |= (0x8000|id)<<16;
91 } else if (matches(*argv, "classid") == 0 ||
92 strcmp(*argv, "flowid") == 0) {
32a121cb
SH
93 unsigned int handle;
94
aba5acdf
SH
95 NEXT_ARG();
96 if (get_tc_classid(&handle, *argv)) {
97 fprintf(stderr, "Illegal \"classid\"\n");
98 return -1;
99 }
100 addattr_l(n, 4096, TCA_ROUTE4_CLASSID, &handle, 4);
101 } else if (matches(*argv, "police") == 0) {
102 NEXT_ARG();
103 if (parse_police(&argc, &argv, TCA_ROUTE4_POLICE, n)) {
104 fprintf(stderr, "Illegal \"police\"\n");
105 return -1;
106 }
107 continue;
287bf3a9
JHS
108 } else if (matches(*argv, "action") == 0) {
109 NEXT_ARG();
110 if (parse_action(&argc, &argv, TCA_ROUTE4_ACT, n)) {
111 fprintf(stderr, "Illegal \"action\"\n");
112 return -1;
113 }
114 continue;
aba5acdf
SH
115 } else if (matches(*argv, "order") == 0) {
116 NEXT_ARG();
117 if (get_u32(&order, *argv, 0)) {
118 fprintf(stderr, "Illegal \"order\"\n");
119 return -1;
120 }
121 } else if (strcmp(*argv, "help") == 0) {
122 explain();
123 return -1;
124 } else {
125 fprintf(stderr, "What is \"%s\"?\n", *argv);
126 explain();
127 return -1;
128 }
129 argc--; argv++;
130 }
228569c3 131 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
aba5acdf
SH
132 if (order) {
133 fh &= ~0x7F00;
134 fh |= (order<<8)&0x7F00;
135 }
136 if (!t->tcm_handle)
137 t->tcm_handle = fh;
138 return 0;
139}
140
141static int route_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 handle)
142{
143 struct rtattr *tb[TCA_ROUTE4_MAX+1];
32a121cb 144
aba5acdf
SH
145 SPRINT_BUF(b1);
146
147 if (opt == NULL)
148 return 0;
149
3b3ecd31 150 parse_rtattr_nested(tb, TCA_ROUTE4_MAX, opt);
aba5acdf
SH
151
152 if (handle)
153 fprintf(f, "fh 0x%08x ", handle);
154 if (handle&0x7F00)
155 fprintf(f, "order %d ", (handle>>8)&0x7F);
156
157 if (tb[TCA_ROUTE4_CLASSID]) {
158 SPRINT_BUF(b1);
ff24746c 159 fprintf(f, "flowid %s ", sprint_tc_classid(rta_getattr_u32(tb[TCA_ROUTE4_CLASSID]), b1));
aba5acdf
SH
160 }
161 if (tb[TCA_ROUTE4_TO])
ff24746c 162 fprintf(f, "to %s ", rtnl_rtrealm_n2a(rta_getattr_u32(tb[TCA_ROUTE4_TO]), b1, sizeof(b1)));
aba5acdf 163 if (tb[TCA_ROUTE4_FROM])
ff24746c 164 fprintf(f, "from %s ", rtnl_rtrealm_n2a(rta_getattr_u32(tb[TCA_ROUTE4_FROM]), b1, sizeof(b1)));
aba5acdf 165 if (tb[TCA_ROUTE4_IIF])
9f1370c0 166 fprintf(f, "fromif %s", ll_index_to_name(rta_getattr_u32(tb[TCA_ROUTE4_IIF])));
aba5acdf
SH
167 if (tb[TCA_ROUTE4_POLICE])
168 tc_print_police(f, tb[TCA_ROUTE4_POLICE]);
287bf3a9 169 if (tb[TCA_ROUTE4_ACT])
9e713525 170 tc_print_action(f, tb[TCA_ROUTE4_ACT], 0);
aba5acdf
SH
171 return 0;
172}
173
6b7dff17
SH
174struct filter_util route_filter_util = {
175 .id = "route",
176 .parse_fopt = route_parse_opt,
177 .print_fopt = route_print_opt,
aba5acdf 178};