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