]>
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> | |
da651289 JHS |
16 | #include <fcntl.h> |
17 | #include <sys/socket.h> | |
18 | #include <netinet/in.h> | |
19 | #include <arpa/inet.h> | |
20 | #include <string.h> | |
21 | #include <linux/netdevice.h> | |
22 | ||
23 | #include "rt_names.h" | |
24 | #include "utils.h" | |
25 | #include "tc_util.h" | |
26 | #include <linux/tc_act/tc_skbmod.h> | |
27 | ||
28 | static void skbmod_explain(void) | |
29 | { | |
30 | fprintf(stderr, | |
557b7054 SH |
31 | "Usage:... skbmod {[set <SETTABLE>] [swap <SWAPABLE>]} [CONTROL] [index INDEX]\n" |
32 | "where SETTABLE is: [dmac DMAC] [smac SMAC] [etype ETYPE]\n" | |
33 | "where SWAPABLE is: \"mac\" to swap mac addresses\n" | |
34 | "note: \"swap mac\" is done after any outstanding D/SMAC change\n" | |
da651289 JHS |
35 | "\tDMAC := 6 byte Destination MAC address\n" |
36 | "\tSMAC := optional 6 byte Source MAC address\n" | |
37 | "\tETYPE := optional 16 bit ethertype\n" | |
d19f72f7 JP |
38 | "\tCONTROL := reclassify | pipe | drop | continue | ok |\n" |
39 | "\t goto chain <CHAIN_INDEX>\n" | |
da651289 JHS |
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 | 126 | |
3572e01a | 127 | NEXT_ARG_FWD(); |
da651289 JHS |
128 | if (argc) { |
129 | if (matches(*argv, "index") == 0) { | |
130 | NEXT_ARG(); | |
131 | if (get_u32(&p.index, *argv, 0)) { | |
132 | fprintf(stderr, "skbmod: Illegal \"index\"\n"); | |
133 | return -1; | |
134 | } | |
135 | ok++; | |
136 | argc--; | |
137 | argv++; | |
138 | } | |
139 | } | |
140 | ||
141 | if (!ok) { | |
142 | fprintf(stderr, "skbmod requires at least one option\n"); | |
143 | skbmod_usage(); | |
144 | } | |
145 | ||
146 | tail = NLMSG_TAIL(n); | |
147 | addattr_l(n, MAX_MSG, tca_id, NULL, 0); | |
148 | addattr_l(n, MAX_MSG, TCA_SKBMOD_PARMS, &p, sizeof(p)); | |
149 | ||
150 | if (daddr) | |
151 | addattr_l(n, MAX_MSG, TCA_SKBMOD_DMAC, dbuf, ETH_ALEN); | |
152 | if (skbmod_etype) | |
153 | addattr16(n, MAX_MSG, TCA_SKBMOD_ETYPE, skbmod_etype); | |
154 | if (saddr) | |
155 | addattr_l(n, MAX_MSG, TCA_SKBMOD_SMAC, sbuf, ETH_ALEN); | |
156 | ||
157 | tail->rta_len = (void *)NLMSG_TAIL(n) - (void *)tail; | |
158 | ||
159 | *argc_p = argc; | |
160 | *argv_p = argv; | |
161 | return 0; | |
162 | } | |
163 | ||
164 | static int print_skbmod(struct action_util *au, FILE *f, struct rtattr *arg) | |
165 | { | |
166 | struct tc_skbmod *p = NULL; | |
167 | struct rtattr *tb[TCA_SKBMOD_MAX + 1]; | |
168 | __u16 skbmod_etype = 0; | |
169 | int has_optional = 0; | |
170 | SPRINT_BUF(b1); | |
171 | SPRINT_BUF(b2); | |
172 | ||
173 | if (arg == NULL) | |
174 | return -1; | |
175 | ||
176 | parse_rtattr_nested(tb, TCA_SKBMOD_MAX, arg); | |
177 | ||
178 | if (tb[TCA_SKBMOD_PARMS] == NULL) { | |
179 | fprintf(f, "[NULL skbmod parameters]"); | |
180 | return -1; | |
181 | } | |
182 | ||
183 | p = RTA_DATA(tb[TCA_SKBMOD_PARMS]); | |
184 | ||
e67aba55 JP |
185 | fprintf(f, "skbmod "); |
186 | print_action_control(f, "", p->action, " "); | |
da651289 JHS |
187 | |
188 | if (tb[TCA_SKBMOD_ETYPE]) { | |
189 | skbmod_etype = rta_getattr_u16(tb[TCA_SKBMOD_ETYPE]); | |
190 | has_optional = 1; | |
191 | fprintf(f, "set etype 0x%X ", skbmod_etype); | |
192 | } | |
193 | ||
194 | if (has_optional) | |
195 | fprintf(f, "\n\t "); | |
196 | ||
197 | if (tb[TCA_SKBMOD_DMAC]) { | |
198 | has_optional = 1; | |
199 | fprintf(f, "set dmac %s ", | |
200 | ll_addr_n2a(RTA_DATA(tb[TCA_SKBMOD_DMAC]), | |
201 | RTA_PAYLOAD(tb[TCA_SKBMOD_DMAC]), 0, b1, | |
202 | sizeof(b1))); | |
203 | ||
204 | } | |
205 | ||
206 | if (tb[TCA_SKBMOD_SMAC]) { | |
207 | has_optional = 1; | |
208 | fprintf(f, "set smac %s ", | |
209 | ll_addr_n2a(RTA_DATA(tb[TCA_SKBMOD_SMAC]), | |
210 | RTA_PAYLOAD(tb[TCA_SKBMOD_SMAC]), 0, b2, | |
211 | sizeof(b2))); | |
212 | } | |
213 | ||
214 | if (p->flags & SKBMOD_F_SWAPMAC) | |
215 | fprintf(f, "swap mac "); | |
216 | ||
53075318 | 217 | fprintf(f, "\n\t index %u ref %d bind %d", p->index, p->refcnt, |
da651289 JHS |
218 | p->bindcnt); |
219 | if (show_stats) { | |
220 | if (tb[TCA_SKBMOD_TM]) { | |
221 | struct tcf_t *tm = RTA_DATA(tb[TCA_SKBMOD_TM]); | |
222 | ||
223 | print_tm(f, tm); | |
224 | } | |
225 | } | |
226 | ||
227 | fprintf(f, "\n"); | |
228 | ||
229 | return 0; | |
230 | } | |
231 | ||
232 | struct action_util skbmod_action_util = { | |
233 | .id = "skbmod", | |
234 | .parse_aopt = parse_skbmod, | |
235 | .print_aopt = print_skbmod, | |
236 | }; |