]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/f_route.c
Use NLMSG_TAIL
[mirror_iproute2.git] / tc / f_route.c
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"
25 #include "tc_util.h"
26
27 static void explain(void)
28 {
29 fprintf(stderr, "Usage: ... route [ from REALM | fromif TAG ] [ to REALM ]\n");
30 fprintf(stderr, " [ flowid CLASSID ] [ police POLICE_SPEC ]\n");
31 fprintf(stderr, " POLICE_SPEC := ... look at TBF\n");
32 fprintf(stderr, " CLASSID := X:Y\n");
33 }
34
35 #define usage() return(-1)
36
37 static 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
57 tail = NLMSG_TAIL(n);
58 addattr_l(n, 4096, TCA_OPTIONS, NULL, 0);
59
60 while (argc > 0) {
61 if (matches(*argv, "to") == 0) {
62 __u32 id;
63 NEXT_ARG();
64 if (rtnl_rtrealm_a2n(&id, *argv)) {
65 fprintf(stderr, "Illegal \"to\"\n");
66 return -1;
67 }
68 addattr_l(n, 4096, TCA_ROUTE4_TO, &id, 4);
69 fh &= ~0x80FF;
70 fh |= id&0xFF;
71 } else if (matches(*argv, "from") == 0) {
72 __u32 id;
73 NEXT_ARG();
74 if (rtnl_rtrealm_a2n(&id, *argv)) {
75 fprintf(stderr, "Illegal \"from\"\n");
76 return -1;
77 }
78 addattr_l(n, 4096, TCA_ROUTE4_FROM, &id, 4);
79 fh &= 0xFFFF;
80 fh |= id<<16;
81 } else if (matches(*argv, "fromif") == 0) {
82 struct rtnl_handle rth;
83 __u32 id;
84 NEXT_ARG();
85 if (rtnl_open(&rth, 0) == 0) {
86 ll_init_map(&rth);
87 rtnl_close(&rth);
88 }
89 if ((id=ll_name_to_index(*argv)) <= 0) {
90 fprintf(stderr, "Illegal \"fromif\"\n");
91 return -1;
92 }
93 addattr_l(n, 4096, TCA_ROUTE4_IIF, &id, 4);
94 fh &= 0xFFFF;
95 fh |= (0x8000|id)<<16;
96 } else if (matches(*argv, "classid") == 0 ||
97 strcmp(*argv, "flowid") == 0) {
98 unsigned handle;
99 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;
112 } else if (matches(*argv, "order") == 0) {
113 NEXT_ARG();
114 if (get_u32(&order, *argv, 0)) {
115 fprintf(stderr, "Illegal \"order\"\n");
116 return -1;
117 }
118 } else if (strcmp(*argv, "help") == 0) {
119 explain();
120 return -1;
121 } else {
122 fprintf(stderr, "What is \"%s\"?\n", *argv);
123 explain();
124 return -1;
125 }
126 argc--; argv++;
127 }
128 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
129 if (order) {
130 fh &= ~0x7F00;
131 fh |= (order<<8)&0x7F00;
132 }
133 if (!t->tcm_handle)
134 t->tcm_handle = fh;
135 return 0;
136 }
137
138 static int route_print_opt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 handle)
139 {
140 struct rtattr *tb[TCA_ROUTE4_MAX+1];
141 SPRINT_BUF(b1);
142
143 if (opt == NULL)
144 return 0;
145
146 memset(tb, 0, sizeof(tb));
147 if (opt)
148 parse_rtattr(tb, TCA_ROUTE4_MAX, RTA_DATA(opt), RTA_PAYLOAD(opt));
149
150 if (handle)
151 fprintf(f, "fh 0x%08x ", handle);
152 if (handle&0x7F00)
153 fprintf(f, "order %d ", (handle>>8)&0x7F);
154
155 if (tb[TCA_ROUTE4_CLASSID]) {
156 SPRINT_BUF(b1);
157 fprintf(f, "flowid %s ", sprint_tc_classid(*(__u32*)RTA_DATA(tb[TCA_ROUTE4_CLASSID]), b1));
158 }
159 if (tb[TCA_ROUTE4_TO])
160 fprintf(f, "to %s ", rtnl_rtrealm_n2a(*(__u32*)RTA_DATA(tb[TCA_ROUTE4_TO]), b1, sizeof(b1)));
161 if (tb[TCA_ROUTE4_FROM])
162 fprintf(f, "from %s ", rtnl_rtrealm_n2a(*(__u32*)RTA_DATA(tb[TCA_ROUTE4_FROM]), b1, sizeof(b1)));
163 if (tb[TCA_ROUTE4_IIF])
164 fprintf(f, "fromif %s", ll_index_to_name(*(int*)RTA_DATA(tb[TCA_ROUTE4_IIF])));
165 if (tb[TCA_ROUTE4_POLICE])
166 tc_print_police(f, tb[TCA_ROUTE4_POLICE]);
167 return 0;
168 }
169
170 struct filter_util route_filter_util = {
171 .id = "route",
172 .parse_fopt = route_parse_opt,
173 .print_fopt = route_print_opt,
174 };