]>
Commit | Line | Data |
---|---|---|
da651289 JHS |
1 | /* |
2 | * m_skbmod.c skb modifier action module | |
3 | * | |
4 | * This program is free software; you can distribute 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: J Hadi Salim (jhs@mojatatu.com) | |
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 | #include <linux/netdevice.h> | |
23 | ||
24 | #include "rt_names.h" | |
25 | #include "utils.h" | |
26 | #include "tc_util.h" | |
27 | #include <linux/tc_act/tc_skbmod.h> | |
28 | ||
29 | static void skbmod_explain(void) | |
30 | { | |
31 | fprintf(stderr, | |
557b7054 SH |
32 | "Usage:... skbmod {[set <SETTABLE>] [swap <SWAPABLE>]} [CONTROL] [index INDEX]\n" |
33 | "where SETTABLE is: [dmac DMAC] [smac SMAC] [etype ETYPE]\n" | |
34 | "where SWAPABLE is: \"mac\" to swap mac addresses\n" | |
35 | "note: \"swap mac\" is done after any outstanding D/SMAC change\n" | |
da651289 JHS |
36 | "\tDMAC := 6 byte Destination MAC address\n" |
37 | "\tSMAC := optional 6 byte Source MAC address\n" | |
38 | "\tETYPE := optional 16 bit ethertype\n" | |
39 | "\tCONTROL := reclassify|pipe|drop|continue|ok\n" | |
40 | "\tINDEX := skbmod index value to use\n"); | |
41 | } | |
42 | ||
43 | static void skbmod_usage(void) | |
44 | { | |
45 | skbmod_explain(); | |
46 | exit(-1); | |
47 | } | |
48 | ||
49 | static int parse_skbmod(struct action_util *a, int *argc_p, char ***argv_p, | |
50 | int tca_id, struct nlmsghdr *n) | |
51 | { | |
52 | int argc = *argc_p; | |
53 | char **argv = *argv_p; | |
54 | int ok = 0; | |
55 | struct tc_skbmod p; | |
56 | struct rtattr *tail; | |
57 | char dbuf[ETH_ALEN]; | |
58 | char sbuf[ETH_ALEN]; | |
59 | __u16 skbmod_etype = 0; | |
60 | char *daddr = NULL; | |
61 | char *saddr = NULL; | |
62 | ||
63 | memset(&p, 0, sizeof(p)); | |
da651289 JHS |
64 | |
65 | if (argc <= 0) | |
66 | return -1; | |
67 | ||
68 | while (argc > 0) { | |
69 | if (matches(*argv, "skbmod") == 0) { | |
70 | NEXT_ARG(); | |
71 | continue; | |
72 | } else if (matches(*argv, "swap") == 0) { | |
73 | NEXT_ARG(); | |
74 | continue; | |
75 | } else if (matches(*argv, "mac") == 0) { | |
76 | p.flags |= SKBMOD_F_SWAPMAC; | |
77 | ok += 1; | |
78 | } else if (matches(*argv, "set") == 0) { | |
79 | NEXT_ARG(); | |
80 | continue; | |
81 | } else if (matches(*argv, "etype") == 0) { | |
82 | NEXT_ARG(); | |
83 | if (get_u16(&skbmod_etype, *argv, 0)) | |
84 | invarg("ethertype is invalid", *argv); | |
85 | fprintf(stderr, "skbmod etype 0x%x\n", skbmod_etype); | |
86 | p.flags |= SKBMOD_F_ETYPE; | |
87 | ok += 1; | |
88 | } else if (matches(*argv, "dmac") == 0) { | |
89 | NEXT_ARG(); | |
90 | daddr = *argv; | |
91 | if (sscanf(daddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", | |
92 | dbuf, dbuf + 1, dbuf + 2, | |
93 | dbuf + 3, dbuf + 4, dbuf + 5) != 6) { | |
94 | fprintf(stderr, "Invalid dst mac address %s\n", | |
95 | daddr); | |
96 | return -1; | |
97 | } | |
98 | p.flags |= SKBMOD_F_DMAC; | |
99 | fprintf(stderr, "dst MAC address <%s>\n", daddr); | |
100 | ok += 1; | |
101 | ||
102 | } else if (matches(*argv, "smac") == 0) { | |
103 | NEXT_ARG(); | |
104 | saddr = *argv; | |
105 | if (sscanf(saddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", | |
106 | sbuf, sbuf + 1, sbuf + 2, | |
107 | sbuf + 3, sbuf + 4, sbuf + 5) != 6) { | |
108 | fprintf(stderr, "Invalid smac address %s\n", | |
109 | saddr); | |
110 | return -1; | |
111 | } | |
112 | p.flags |= SKBMOD_F_SMAC; | |
113 | fprintf(stderr, "src MAC address <%s>\n", saddr); | |
114 | ok += 1; | |
115 | } else if (matches(*argv, "help") == 0) { | |
116 | skbmod_usage(); | |
117 | } else { | |
118 | break; | |
119 | } | |
120 | ||
121 | argc--; | |
122 | argv++; | |
123 | } | |
124 | ||
e67aba55 | 125 | parse_action_control_dflt(&argc, &argv, &p.action, false, TC_ACT_PIPE); |
da651289 JHS |
126 | |
127 | if (argc) { | |
128 | if (matches(*argv, "index") == 0) { | |
129 | NEXT_ARG(); | |
130 | if (get_u32(&p.index, *argv, 0)) { | |
131 | fprintf(stderr, "skbmod: Illegal \"index\"\n"); | |
132 | return -1; | |
133 | } | |
134 | ok++; | |
135 | argc--; | |
136 | argv++; | |
137 | } | |
138 | } | |
139 | ||
140 | if (!ok) { | |
141 | fprintf(stderr, "skbmod requires at least one option\n"); | |
142 | skbmod_usage(); | |
143 | } | |
144 | ||
145 | tail = NLMSG_TAIL(n); | |
146 | addattr_l(n, MAX_MSG, tca_id, NULL, 0); | |
147 | addattr_l(n, MAX_MSG, TCA_SKBMOD_PARMS, &p, sizeof(p)); | |
148 | ||
149 | if (daddr) | |
150 | addattr_l(n, MAX_MSG, TCA_SKBMOD_DMAC, dbuf, ETH_ALEN); | |
151 | if (skbmod_etype) | |
152 | addattr16(n, MAX_MSG, TCA_SKBMOD_ETYPE, skbmod_etype); | |
153 | if (saddr) | |
154 | addattr_l(n, MAX_MSG, TCA_SKBMOD_SMAC, sbuf, ETH_ALEN); | |
155 | ||
156 | tail->rta_len = (void *)NLMSG_TAIL(n) - (void *)tail; | |
157 | ||
158 | *argc_p = argc; | |
159 | *argv_p = argv; | |
160 | return 0; | |
161 | } | |
162 | ||
163 | static int print_skbmod(struct action_util *au, FILE *f, struct rtattr *arg) | |
164 | { | |
165 | struct tc_skbmod *p = NULL; | |
166 | struct rtattr *tb[TCA_SKBMOD_MAX + 1]; | |
167 | __u16 skbmod_etype = 0; | |
168 | int has_optional = 0; | |
169 | SPRINT_BUF(b1); | |
170 | SPRINT_BUF(b2); | |
171 | ||
172 | if (arg == NULL) | |
173 | return -1; | |
174 | ||
175 | parse_rtattr_nested(tb, TCA_SKBMOD_MAX, arg); | |
176 | ||
177 | if (tb[TCA_SKBMOD_PARMS] == NULL) { | |
178 | fprintf(f, "[NULL skbmod parameters]"); | |
179 | return -1; | |
180 | } | |
181 | ||
182 | p = RTA_DATA(tb[TCA_SKBMOD_PARMS]); | |
183 | ||
e67aba55 JP |
184 | fprintf(f, "skbmod "); |
185 | print_action_control(f, "", p->action, " "); | |
da651289 JHS |
186 | |
187 | if (tb[TCA_SKBMOD_ETYPE]) { | |
188 | skbmod_etype = rta_getattr_u16(tb[TCA_SKBMOD_ETYPE]); | |
189 | has_optional = 1; | |
190 | fprintf(f, "set etype 0x%X ", skbmod_etype); | |
191 | } | |
192 | ||
193 | if (has_optional) | |
194 | fprintf(f, "\n\t "); | |
195 | ||
196 | if (tb[TCA_SKBMOD_DMAC]) { | |
197 | has_optional = 1; | |
198 | fprintf(f, "set dmac %s ", | |
199 | ll_addr_n2a(RTA_DATA(tb[TCA_SKBMOD_DMAC]), | |
200 | RTA_PAYLOAD(tb[TCA_SKBMOD_DMAC]), 0, b1, | |
201 | sizeof(b1))); | |
202 | ||
203 | } | |
204 | ||
205 | if (tb[TCA_SKBMOD_SMAC]) { | |
206 | has_optional = 1; | |
207 | fprintf(f, "set smac %s ", | |
208 | ll_addr_n2a(RTA_DATA(tb[TCA_SKBMOD_SMAC]), | |
209 | RTA_PAYLOAD(tb[TCA_SKBMOD_SMAC]), 0, b2, | |
210 | sizeof(b2))); | |
211 | } | |
212 | ||
213 | if (p->flags & SKBMOD_F_SWAPMAC) | |
214 | fprintf(f, "swap mac "); | |
215 | ||
53075318 | 216 | fprintf(f, "\n\t index %u ref %d bind %d", p->index, p->refcnt, |
da651289 JHS |
217 | p->bindcnt); |
218 | if (show_stats) { | |
219 | if (tb[TCA_SKBMOD_TM]) { | |
220 | struct tcf_t *tm = RTA_DATA(tb[TCA_SKBMOD_TM]); | |
221 | ||
222 | print_tm(f, tm); | |
223 | } | |
224 | } | |
225 | ||
226 | fprintf(f, "\n"); | |
227 | ||
228 | return 0; | |
229 | } | |
230 | ||
231 | struct action_util skbmod_action_util = { | |
232 | .id = "skbmod", | |
233 | .parse_aopt = parse_skbmod, | |
234 | .print_aopt = print_skbmod, | |
235 | }; |