Commit | Line | Data |
---|---|---|
ef6980b6 JHS |
1 | /* |
2 | * net/sched/ife.c Inter-FE action based on ForCES WG InterFE LFB | |
3 | * | |
4 | * Refer to: | |
5 | * draft-ietf-forces-interfelfb-03 | |
6 | * and | |
7 | * netdev01 paper: | |
8 | * "Distributing Linux Traffic Control Classifier-Action | |
9 | * Subsystem" | |
10 | * Authors: Jamal Hadi Salim and Damascene M. Joachimpillai | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or | |
13 | * modify it under the terms of the GNU General Public License | |
14 | * as published by the Free Software Foundation; either version | |
15 | * 2 of the License, or (at your option) any later version. | |
16 | * | |
17 | * copyright Jamal Hadi Salim (2015) | |
18 | * | |
19 | */ | |
20 | ||
21 | #include <linux/types.h> | |
22 | #include <linux/kernel.h> | |
23 | #include <linux/string.h> | |
24 | #include <linux/errno.h> | |
25 | #include <linux/skbuff.h> | |
26 | #include <linux/rtnetlink.h> | |
27 | #include <linux/module.h> | |
28 | #include <linux/init.h> | |
29 | #include <net/net_namespace.h> | |
30 | #include <net/netlink.h> | |
31 | #include <net/pkt_sched.h> | |
32 | #include <uapi/linux/tc_act/tc_ife.h> | |
33 | #include <net/tc_act/tc_ife.h> | |
34 | #include <linux/etherdevice.h> | |
295a6e06 | 35 | #include <net/ife.h> |
ef6980b6 | 36 | |
c7d03a00 | 37 | static unsigned int ife_net_id; |
ef6980b6 | 38 | static int max_metacnt = IFE_META_MAX + 1; |
a85a970a | 39 | static struct tc_action_ops act_ife_ops; |
ef6980b6 JHS |
40 | |
41 | static const struct nla_policy ife_policy[TCA_IFE_MAX + 1] = { | |
42 | [TCA_IFE_PARMS] = { .len = sizeof(struct tc_ife)}, | |
43 | [TCA_IFE_DMAC] = { .len = ETH_ALEN}, | |
44 | [TCA_IFE_SMAC] = { .len = ETH_ALEN}, | |
45 | [TCA_IFE_TYPE] = { .type = NLA_U16}, | |
46 | }; | |
47 | ||
6a5d58b6 JHS |
48 | int ife_encode_meta_u16(u16 metaval, void *skbdata, struct tcf_meta_info *mi) |
49 | { | |
50 | u16 edata = 0; | |
51 | ||
52 | if (mi->metaval) | |
53 | edata = *(u16 *)mi->metaval; | |
54 | else if (metaval) | |
55 | edata = metaval; | |
56 | ||
57 | if (!edata) /* will not encode */ | |
58 | return 0; | |
59 | ||
60 | edata = htons(edata); | |
61 | return ife_tlv_meta_encode(skbdata, mi->metaid, 2, &edata); | |
62 | } | |
63 | EXPORT_SYMBOL_GPL(ife_encode_meta_u16); | |
64 | ||
ef6980b6 JHS |
65 | int ife_get_meta_u32(struct sk_buff *skb, struct tcf_meta_info *mi) |
66 | { | |
67 | if (mi->metaval) | |
68 | return nla_put_u32(skb, mi->metaid, *(u32 *)mi->metaval); | |
69 | else | |
70 | return nla_put(skb, mi->metaid, 0, NULL); | |
71 | } | |
72 | EXPORT_SYMBOL_GPL(ife_get_meta_u32); | |
73 | ||
74 | int ife_check_meta_u32(u32 metaval, struct tcf_meta_info *mi) | |
75 | { | |
76 | if (metaval || mi->metaval) | |
77 | return 8; /* T+L+V == 2+2+4 */ | |
78 | ||
79 | return 0; | |
80 | } | |
81 | EXPORT_SYMBOL_GPL(ife_check_meta_u32); | |
82 | ||
6a5d58b6 JHS |
83 | int ife_check_meta_u16(u16 metaval, struct tcf_meta_info *mi) |
84 | { | |
85 | if (metaval || mi->metaval) | |
86 | return 8; /* T+L+(V) == 2+2+(2+2bytepad) */ | |
87 | ||
88 | return 0; | |
89 | } | |
90 | EXPORT_SYMBOL_GPL(ife_check_meta_u16); | |
91 | ||
ef6980b6 JHS |
92 | int ife_encode_meta_u32(u32 metaval, void *skbdata, struct tcf_meta_info *mi) |
93 | { | |
94 | u32 edata = metaval; | |
95 | ||
96 | if (mi->metaval) | |
97 | edata = *(u32 *)mi->metaval; | |
98 | else if (metaval) | |
99 | edata = metaval; | |
100 | ||
101 | if (!edata) /* will not encode */ | |
102 | return 0; | |
103 | ||
104 | edata = htonl(edata); | |
105 | return ife_tlv_meta_encode(skbdata, mi->metaid, 4, &edata); | |
106 | } | |
107 | EXPORT_SYMBOL_GPL(ife_encode_meta_u32); | |
108 | ||
109 | int ife_get_meta_u16(struct sk_buff *skb, struct tcf_meta_info *mi) | |
110 | { | |
111 | if (mi->metaval) | |
112 | return nla_put_u16(skb, mi->metaid, *(u16 *)mi->metaval); | |
113 | else | |
114 | return nla_put(skb, mi->metaid, 0, NULL); | |
115 | } | |
116 | EXPORT_SYMBOL_GPL(ife_get_meta_u16); | |
117 | ||
067a7cd0 | 118 | int ife_alloc_meta_u32(struct tcf_meta_info *mi, void *metaval, gfp_t gfp) |
ef6980b6 | 119 | { |
067a7cd0 | 120 | mi->metaval = kmemdup(metaval, sizeof(u32), gfp); |
ef6980b6 JHS |
121 | if (!mi->metaval) |
122 | return -ENOMEM; | |
123 | ||
124 | return 0; | |
125 | } | |
126 | EXPORT_SYMBOL_GPL(ife_alloc_meta_u32); | |
127 | ||
067a7cd0 | 128 | int ife_alloc_meta_u16(struct tcf_meta_info *mi, void *metaval, gfp_t gfp) |
ef6980b6 | 129 | { |
067a7cd0 | 130 | mi->metaval = kmemdup(metaval, sizeof(u16), gfp); |
ef6980b6 JHS |
131 | if (!mi->metaval) |
132 | return -ENOMEM; | |
133 | ||
134 | return 0; | |
135 | } | |
136 | EXPORT_SYMBOL_GPL(ife_alloc_meta_u16); | |
137 | ||
138 | void ife_release_meta_gen(struct tcf_meta_info *mi) | |
139 | { | |
140 | kfree(mi->metaval); | |
141 | } | |
142 | EXPORT_SYMBOL_GPL(ife_release_meta_gen); | |
143 | ||
144 | int ife_validate_meta_u32(void *val, int len) | |
145 | { | |
28a10c42 | 146 | if (len == sizeof(u32)) |
ef6980b6 JHS |
147 | return 0; |
148 | ||
149 | return -EINVAL; | |
150 | } | |
151 | EXPORT_SYMBOL_GPL(ife_validate_meta_u32); | |
152 | ||
153 | int ife_validate_meta_u16(void *val, int len) | |
154 | { | |
28a10c42 JHS |
155 | /* length will not include padding */ |
156 | if (len == sizeof(u16)) | |
ef6980b6 JHS |
157 | return 0; |
158 | ||
159 | return -EINVAL; | |
160 | } | |
161 | EXPORT_SYMBOL_GPL(ife_validate_meta_u16); | |
162 | ||
163 | static LIST_HEAD(ifeoplist); | |
164 | static DEFINE_RWLOCK(ife_mod_lock); | |
165 | ||
166 | static struct tcf_meta_ops *find_ife_oplist(u16 metaid) | |
167 | { | |
168 | struct tcf_meta_ops *o; | |
169 | ||
8ce5be1c | 170 | read_lock(&ife_mod_lock); |
ef6980b6 JHS |
171 | list_for_each_entry(o, &ifeoplist, list) { |
172 | if (o->metaid == metaid) { | |
173 | if (!try_module_get(o->owner)) | |
174 | o = NULL; | |
8ce5be1c | 175 | read_unlock(&ife_mod_lock); |
ef6980b6 JHS |
176 | return o; |
177 | } | |
178 | } | |
8ce5be1c | 179 | read_unlock(&ife_mod_lock); |
ef6980b6 JHS |
180 | |
181 | return NULL; | |
182 | } | |
183 | ||
184 | int register_ife_op(struct tcf_meta_ops *mops) | |
185 | { | |
186 | struct tcf_meta_ops *m; | |
187 | ||
188 | if (!mops->metaid || !mops->metatype || !mops->name || | |
189 | !mops->check_presence || !mops->encode || !mops->decode || | |
190 | !mops->get || !mops->alloc) | |
191 | return -EINVAL; | |
192 | ||
8ce5be1c | 193 | write_lock(&ife_mod_lock); |
ef6980b6 JHS |
194 | |
195 | list_for_each_entry(m, &ifeoplist, list) { | |
196 | if (m->metaid == mops->metaid || | |
197 | (strcmp(mops->name, m->name) == 0)) { | |
8ce5be1c | 198 | write_unlock(&ife_mod_lock); |
ef6980b6 JHS |
199 | return -EEXIST; |
200 | } | |
201 | } | |
202 | ||
203 | if (!mops->release) | |
204 | mops->release = ife_release_meta_gen; | |
205 | ||
206 | list_add_tail(&mops->list, &ifeoplist); | |
8ce5be1c | 207 | write_unlock(&ife_mod_lock); |
ef6980b6 JHS |
208 | return 0; |
209 | } | |
210 | EXPORT_SYMBOL_GPL(unregister_ife_op); | |
211 | ||
212 | int unregister_ife_op(struct tcf_meta_ops *mops) | |
213 | { | |
214 | struct tcf_meta_ops *m; | |
215 | int err = -ENOENT; | |
216 | ||
8ce5be1c | 217 | write_lock(&ife_mod_lock); |
ef6980b6 JHS |
218 | list_for_each_entry(m, &ifeoplist, list) { |
219 | if (m->metaid == mops->metaid) { | |
220 | list_del(&mops->list); | |
221 | err = 0; | |
222 | break; | |
223 | } | |
224 | } | |
8ce5be1c | 225 | write_unlock(&ife_mod_lock); |
ef6980b6 JHS |
226 | |
227 | return err; | |
228 | } | |
229 | EXPORT_SYMBOL_GPL(register_ife_op); | |
230 | ||
231 | static int ife_validate_metatype(struct tcf_meta_ops *ops, void *val, int len) | |
232 | { | |
233 | int ret = 0; | |
234 | /* XXX: unfortunately cant use nla_policy at this point | |
235 | * because a length of 0 is valid in the case of | |
236 | * "allow". "use" semantics do enforce for proper | |
237 | * length and i couldve use nla_policy but it makes it hard | |
238 | * to use it just for that.. | |
239 | */ | |
240 | if (ops->validate) | |
241 | return ops->validate(val, len); | |
242 | ||
243 | if (ops->metatype == NLA_U32) | |
244 | ret = ife_validate_meta_u32(val, len); | |
245 | else if (ops->metatype == NLA_U16) | |
246 | ret = ife_validate_meta_u16(val, len); | |
247 | ||
248 | return ret; | |
249 | } | |
250 | ||
65787594 | 251 | #ifdef CONFIG_MODULES |
d3f24ba8 RM |
252 | static const char *ife_meta_id2name(u32 metaid) |
253 | { | |
254 | switch (metaid) { | |
255 | case IFE_META_SKBMARK: | |
256 | return "skbmark"; | |
257 | case IFE_META_PRIO: | |
258 | return "skbprio"; | |
259 | case IFE_META_TCINDEX: | |
260 | return "tcindex"; | |
261 | default: | |
262 | return "unknown"; | |
263 | } | |
264 | } | |
65787594 | 265 | #endif |
d3f24ba8 | 266 | |
ef6980b6 | 267 | /* called when adding new meta information |
067a7cd0 | 268 | * under ife->tcf_lock for existing action |
ef6980b6 JHS |
269 | */ |
270 | static int load_metaops_and_vet(struct tcf_ife_info *ife, u32 metaid, | |
54d0d423 VB |
271 | void *val, int len, bool exists, |
272 | bool rtnl_held) | |
ef6980b6 JHS |
273 | { |
274 | struct tcf_meta_ops *ops = find_ife_oplist(metaid); | |
275 | int ret = 0; | |
276 | ||
277 | if (!ops) { | |
278 | ret = -ENOENT; | |
279 | #ifdef CONFIG_MODULES | |
067a7cd0 WC |
280 | if (exists) |
281 | spin_unlock_bh(&ife->tcf_lock); | |
54d0d423 VB |
282 | if (rtnl_held) |
283 | rtnl_unlock(); | |
d3f24ba8 | 284 | request_module("ife-meta-%s", ife_meta_id2name(metaid)); |
54d0d423 VB |
285 | if (rtnl_held) |
286 | rtnl_lock(); | |
067a7cd0 WC |
287 | if (exists) |
288 | spin_lock_bh(&ife->tcf_lock); | |
ef6980b6 JHS |
289 | ops = find_ife_oplist(metaid); |
290 | #endif | |
291 | } | |
292 | ||
293 | if (ops) { | |
294 | ret = 0; | |
295 | if (len) | |
296 | ret = ife_validate_metatype(ops, val, len); | |
297 | ||
298 | module_put(ops->owner); | |
299 | } | |
300 | ||
301 | return ret; | |
302 | } | |
303 | ||
304 | /* called when adding new meta information | |
067a7cd0 | 305 | * under ife->tcf_lock for existing action |
ef6980b6 JHS |
306 | */ |
307 | static int add_metainfo(struct tcf_ife_info *ife, u32 metaid, void *metaval, | |
817e9f2c | 308 | int len, bool atomic) |
ef6980b6 JHS |
309 | { |
310 | struct tcf_meta_info *mi = NULL; | |
311 | struct tcf_meta_ops *ops = find_ife_oplist(metaid); | |
312 | int ret = 0; | |
313 | ||
314 | if (!ops) | |
315 | return -ENOENT; | |
316 | ||
817e9f2c | 317 | mi = kzalloc(sizeof(*mi), atomic ? GFP_ATOMIC : GFP_KERNEL); |
ef6980b6 JHS |
318 | if (!mi) { |
319 | /*put back what find_ife_oplist took */ | |
320 | module_put(ops->owner); | |
321 | return -ENOMEM; | |
322 | } | |
323 | ||
324 | mi->metaid = metaid; | |
325 | mi->ops = ops; | |
326 | if (len > 0) { | |
817e9f2c | 327 | ret = ops->alloc(mi, metaval, atomic ? GFP_ATOMIC : GFP_KERNEL); |
ef6980b6 JHS |
328 | if (ret != 0) { |
329 | kfree(mi); | |
330 | module_put(ops->owner); | |
331 | return ret; | |
332 | } | |
333 | } | |
334 | ||
335 | list_add_tail(&mi->metalist, &ife->metalist); | |
336 | ||
337 | return ret; | |
338 | } | |
339 | ||
340 | static int use_all_metadata(struct tcf_ife_info *ife) | |
341 | { | |
342 | struct tcf_meta_ops *o; | |
343 | int rc = 0; | |
344 | int installed = 0; | |
345 | ||
8ce5be1c | 346 | read_lock(&ife_mod_lock); |
ef6980b6 | 347 | list_for_each_entry(o, &ifeoplist, list) { |
817e9f2c | 348 | rc = add_metainfo(ife, o->metaid, NULL, 0, true); |
ef6980b6 JHS |
349 | if (rc == 0) |
350 | installed += 1; | |
351 | } | |
8ce5be1c | 352 | read_unlock(&ife_mod_lock); |
ef6980b6 JHS |
353 | |
354 | if (installed) | |
355 | return 0; | |
356 | else | |
357 | return -EINVAL; | |
358 | } | |
359 | ||
360 | static int dump_metalist(struct sk_buff *skb, struct tcf_ife_info *ife) | |
361 | { | |
362 | struct tcf_meta_info *e; | |
363 | struct nlattr *nest; | |
364 | unsigned char *b = skb_tail_pointer(skb); | |
365 | int total_encoded = 0; | |
366 | ||
367 | /*can only happen on decode */ | |
368 | if (list_empty(&ife->metalist)) | |
369 | return 0; | |
370 | ||
371 | nest = nla_nest_start(skb, TCA_IFE_METALST); | |
372 | if (!nest) | |
373 | goto out_nlmsg_trim; | |
374 | ||
375 | list_for_each_entry(e, &ife->metalist, metalist) { | |
376 | if (!e->ops->get(skb, e)) | |
377 | total_encoded += 1; | |
378 | } | |
379 | ||
380 | if (!total_encoded) | |
381 | goto out_nlmsg_trim; | |
382 | ||
383 | nla_nest_end(skb, nest); | |
384 | ||
385 | return 0; | |
386 | ||
387 | out_nlmsg_trim: | |
388 | nlmsg_trim(skb, b); | |
389 | return -1; | |
390 | } | |
391 | ||
392 | /* under ife->tcf_lock */ | |
9a63b255 | 393 | static void _tcf_ife_cleanup(struct tc_action *a) |
ef6980b6 | 394 | { |
a85a970a | 395 | struct tcf_ife_info *ife = to_ife(a); |
ef6980b6 JHS |
396 | struct tcf_meta_info *e, *n; |
397 | ||
398 | list_for_each_entry_safe(e, n, &ife->metalist, metalist) { | |
399 | module_put(e->ops->owner); | |
400 | list_del(&e->metalist); | |
401 | if (e->metaval) { | |
402 | if (e->ops->release) | |
403 | e->ops->release(e); | |
404 | else | |
405 | kfree(e->metaval); | |
406 | } | |
407 | kfree(e); | |
408 | } | |
409 | } | |
410 | ||
9a63b255 | 411 | static void tcf_ife_cleanup(struct tc_action *a) |
ef6980b6 | 412 | { |
a85a970a | 413 | struct tcf_ife_info *ife = to_ife(a); |
aa9fd9a3 | 414 | struct tcf_ife_params *p; |
ef6980b6 JHS |
415 | |
416 | spin_lock_bh(&ife->tcf_lock); | |
9a63b255 | 417 | _tcf_ife_cleanup(a); |
ef6980b6 | 418 | spin_unlock_bh(&ife->tcf_lock); |
aa9fd9a3 AA |
419 | |
420 | p = rcu_dereference_protected(ife->params, 1); | |
0a889b94 DC |
421 | if (p) |
422 | kfree_rcu(p, rcu); | |
ef6980b6 JHS |
423 | } |
424 | ||
067a7cd0 WC |
425 | /* under ife->tcf_lock for existing action */ |
426 | static int populate_metalist(struct tcf_ife_info *ife, struct nlattr **tb, | |
54d0d423 | 427 | bool exists, bool rtnl_held) |
ef6980b6 JHS |
428 | { |
429 | int len = 0; | |
430 | int rc = 0; | |
431 | int i = 0; | |
432 | void *val; | |
433 | ||
434 | for (i = 1; i < max_metacnt; i++) { | |
435 | if (tb[i]) { | |
436 | val = nla_data(tb[i]); | |
437 | len = nla_len(tb[i]); | |
438 | ||
54d0d423 VB |
439 | rc = load_metaops_and_vet(ife, i, val, len, exists, |
440 | rtnl_held); | |
ef6980b6 JHS |
441 | if (rc != 0) |
442 | return rc; | |
443 | ||
067a7cd0 | 444 | rc = add_metainfo(ife, i, val, len, exists); |
ef6980b6 JHS |
445 | if (rc) |
446 | return rc; | |
447 | } | |
448 | } | |
449 | ||
450 | return rc; | |
451 | } | |
452 | ||
453 | static int tcf_ife_init(struct net *net, struct nlattr *nla, | |
a85a970a | 454 | struct nlattr *est, struct tc_action **a, |
789871bb VB |
455 | int ovr, int bind, bool rtnl_held, |
456 | struct netlink_ext_ack *extack) | |
ef6980b6 JHS |
457 | { |
458 | struct tc_action_net *tn = net_generic(net, ife_net_id); | |
459 | struct nlattr *tb[TCA_IFE_MAX + 1]; | |
460 | struct nlattr *tb2[IFE_META_MAX + 1]; | |
54d0d423 | 461 | struct tcf_ife_params *p; |
ef6980b6 | 462 | struct tcf_ife_info *ife; |
b522ed6e | 463 | u16 ife_type = ETH_P_IFE; |
ef6980b6 | 464 | struct tc_ife *parm; |
ef6980b6 JHS |
465 | u8 *daddr = NULL; |
466 | u8 *saddr = NULL; | |
b2313077 WC |
467 | bool exists = false; |
468 | int ret = 0; | |
ef6980b6 JHS |
469 | int err; |
470 | ||
fceb6435 | 471 | err = nla_parse_nested(tb, TCA_IFE_MAX, nla, ife_policy, NULL); |
ef6980b6 JHS |
472 | if (err < 0) |
473 | return err; | |
474 | ||
475 | if (!tb[TCA_IFE_PARMS]) | |
476 | return -EINVAL; | |
477 | ||
478 | parm = nla_data(tb[TCA_IFE_PARMS]); | |
479 | ||
734534e9 AA |
480 | /* IFE_DECODE is 0 and indicates the opposite of IFE_ENCODE because |
481 | * they cannot run as the same time. Check on all other values which | |
482 | * are not supported right now. | |
483 | */ | |
484 | if (parm->flags & ~IFE_ENCODE) | |
485 | return -EINVAL; | |
486 | ||
aa9fd9a3 AA |
487 | p = kzalloc(sizeof(*p), GFP_KERNEL); |
488 | if (!p) | |
489 | return -ENOMEM; | |
490 | ||
0190c1d4 | 491 | err = tcf_idr_check_alloc(tn, &parm->index, a, bind); |
01e866bf VB |
492 | if (err < 0) { |
493 | kfree(p); | |
0190c1d4 | 494 | return err; |
01e866bf | 495 | } |
0190c1d4 | 496 | exists = err; |
aa9fd9a3 AA |
497 | if (exists && bind) { |
498 | kfree(p); | |
4e8c8615 | 499 | return 0; |
aa9fd9a3 | 500 | } |
4e8c8615 | 501 | |
4e8c8615 | 502 | if (!exists) { |
65a206c0 | 503 | ret = tcf_idr_create(tn, parm->index, est, a, &act_ife_ops, |
ced273ea | 504 | bind, true); |
aa9fd9a3 | 505 | if (ret) { |
0190c1d4 | 506 | tcf_idr_cleanup(tn, parm->index); |
aa9fd9a3 | 507 | kfree(p); |
ef6980b6 | 508 | return ret; |
aa9fd9a3 | 509 | } |
ef6980b6 | 510 | ret = ACT_P_CREATED; |
4e8ddd7f | 511 | } else if (!ovr) { |
65a206c0 | 512 | tcf_idr_release(*a, bind); |
4e8ddd7f VB |
513 | kfree(p); |
514 | return -EEXIST; | |
ef6980b6 JHS |
515 | } |
516 | ||
a85a970a | 517 | ife = to_ife(*a); |
aa9fd9a3 | 518 | p->flags = parm->flags; |
ef6980b6 JHS |
519 | |
520 | if (parm->flags & IFE_ENCODE) { | |
b522ed6e AA |
521 | if (tb[TCA_IFE_TYPE]) |
522 | ife_type = nla_get_u16(tb[TCA_IFE_TYPE]); | |
ef6980b6 JHS |
523 | if (tb[TCA_IFE_DMAC]) |
524 | daddr = nla_data(tb[TCA_IFE_DMAC]); | |
525 | if (tb[TCA_IFE_SMAC]) | |
526 | saddr = nla_data(tb[TCA_IFE_SMAC]); | |
527 | } | |
528 | ||
ef6980b6 JHS |
529 | if (parm->flags & IFE_ENCODE) { |
530 | if (daddr) | |
aa9fd9a3 | 531 | ether_addr_copy(p->eth_dst, daddr); |
ef6980b6 | 532 | else |
aa9fd9a3 | 533 | eth_zero_addr(p->eth_dst); |
ef6980b6 JHS |
534 | |
535 | if (saddr) | |
aa9fd9a3 | 536 | ether_addr_copy(p->eth_src, saddr); |
ef6980b6 | 537 | else |
aa9fd9a3 | 538 | eth_zero_addr(p->eth_src); |
ef6980b6 | 539 | |
aa9fd9a3 | 540 | p->eth_type = ife_type; |
ef6980b6 JHS |
541 | } |
542 | ||
aa9fd9a3 AA |
543 | if (exists) |
544 | spin_lock_bh(&ife->tcf_lock); | |
545 | ||
ef6980b6 JHS |
546 | if (ret == ACT_P_CREATED) |
547 | INIT_LIST_HEAD(&ife->metalist); | |
548 | ||
549 | if (tb[TCA_IFE_METALST]) { | |
550 | err = nla_parse_nested(tb2, IFE_META_MAX, tb[TCA_IFE_METALST], | |
fceb6435 | 551 | NULL, NULL); |
ef6980b6 JHS |
552 | if (err) { |
553 | metadata_parse_err: | |
067a7cd0 WC |
554 | if (exists) |
555 | spin_unlock_bh(&ife->tcf_lock); | |
4e8ddd7f VB |
556 | tcf_idr_release(*a, bind); |
557 | ||
aa9fd9a3 | 558 | kfree(p); |
ef6980b6 JHS |
559 | return err; |
560 | } | |
561 | ||
54d0d423 | 562 | err = populate_metalist(ife, tb2, exists, rtnl_held); |
ef6980b6 JHS |
563 | if (err) |
564 | goto metadata_parse_err; | |
565 | ||
566 | } else { | |
567 | /* if no passed metadata allow list or passed allow-all | |
568 | * then here we process by adding as many supported metadatum | |
569 | * as we can. You better have at least one else we are | |
570 | * going to bail out | |
571 | */ | |
572 | err = use_all_metadata(ife); | |
573 | if (err) { | |
067a7cd0 WC |
574 | if (exists) |
575 | spin_unlock_bh(&ife->tcf_lock); | |
32039eac VB |
576 | tcf_idr_release(*a, bind); |
577 | ||
aa9fd9a3 | 578 | kfree(p); |
ef6980b6 JHS |
579 | return err; |
580 | } | |
581 | } | |
582 | ||
cbf56c29 | 583 | ife->tcf_action = parm->action; |
54d0d423 VB |
584 | /* protected by tcf_lock when modifying existing action */ |
585 | rcu_swap_protected(ife->params, p, 1); | |
586 | ||
067a7cd0 WC |
587 | if (exists) |
588 | spin_unlock_bh(&ife->tcf_lock); | |
54d0d423 VB |
589 | if (p) |
590 | kfree_rcu(p, rcu); | |
aa9fd9a3 | 591 | |
ef6980b6 | 592 | if (ret == ACT_P_CREATED) |
65a206c0 | 593 | tcf_idr_insert(tn, *a); |
ef6980b6 JHS |
594 | |
595 | return ret; | |
596 | } | |
597 | ||
598 | static int tcf_ife_dump(struct sk_buff *skb, struct tc_action *a, int bind, | |
599 | int ref) | |
600 | { | |
601 | unsigned char *b = skb_tail_pointer(skb); | |
a85a970a | 602 | struct tcf_ife_info *ife = to_ife(a); |
54d0d423 | 603 | struct tcf_ife_params *p; |
ef6980b6 JHS |
604 | struct tc_ife opt = { |
605 | .index = ife->tcf_index, | |
036bb443 VB |
606 | .refcnt = refcount_read(&ife->tcf_refcnt) - ref, |
607 | .bindcnt = atomic_read(&ife->tcf_bindcnt) - bind, | |
ef6980b6 JHS |
608 | }; |
609 | struct tcf_t t; | |
610 | ||
54d0d423 VB |
611 | spin_lock_bh(&ife->tcf_lock); |
612 | opt.action = ife->tcf_action; | |
613 | p = rcu_dereference_protected(ife->params, | |
614 | lockdep_is_held(&ife->tcf_lock)); | |
615 | opt.flags = p->flags; | |
616 | ||
ef6980b6 JHS |
617 | if (nla_put(skb, TCA_IFE_PARMS, sizeof(opt), &opt)) |
618 | goto nla_put_failure; | |
619 | ||
48d8ee16 | 620 | tcf_tm_dump(&t, &ife->tcf_tm); |
9854518e | 621 | if (nla_put_64bit(skb, TCA_IFE_TM, sizeof(t), &t, TCA_IFE_PAD)) |
ef6980b6 JHS |
622 | goto nla_put_failure; |
623 | ||
aa9fd9a3 AA |
624 | if (!is_zero_ether_addr(p->eth_dst)) { |
625 | if (nla_put(skb, TCA_IFE_DMAC, ETH_ALEN, p->eth_dst)) | |
ef6980b6 JHS |
626 | goto nla_put_failure; |
627 | } | |
628 | ||
aa9fd9a3 AA |
629 | if (!is_zero_ether_addr(p->eth_src)) { |
630 | if (nla_put(skb, TCA_IFE_SMAC, ETH_ALEN, p->eth_src)) | |
ef6980b6 JHS |
631 | goto nla_put_failure; |
632 | } | |
633 | ||
aa9fd9a3 | 634 | if (nla_put(skb, TCA_IFE_TYPE, 2, &p->eth_type)) |
ef6980b6 JHS |
635 | goto nla_put_failure; |
636 | ||
637 | if (dump_metalist(skb, ife)) { | |
638 | /*ignore failure to dump metalist */ | |
639 | pr_info("Failed to dump metalist\n"); | |
640 | } | |
641 | ||
54d0d423 | 642 | spin_unlock_bh(&ife->tcf_lock); |
ef6980b6 JHS |
643 | return skb->len; |
644 | ||
645 | nla_put_failure: | |
54d0d423 | 646 | spin_unlock_bh(&ife->tcf_lock); |
ef6980b6 JHS |
647 | nlmsg_trim(skb, b); |
648 | return -1; | |
649 | } | |
650 | ||
4dba87b0 OG |
651 | static int find_decode_metaid(struct sk_buff *skb, struct tcf_ife_info *ife, |
652 | u16 metaid, u16 mlen, void *mdata) | |
ef6980b6 JHS |
653 | { |
654 | struct tcf_meta_info *e; | |
655 | ||
656 | /* XXX: use hash to speed up */ | |
657 | list_for_each_entry(e, &ife->metalist, metalist) { | |
658 | if (metaid == e->metaid) { | |
659 | if (e->ops) { | |
660 | /* We check for decode presence already */ | |
661 | return e->ops->decode(skb, mdata, mlen); | |
662 | } | |
663 | } | |
664 | } | |
665 | ||
f6cd1453 | 666 | return -ENOENT; |
ef6980b6 JHS |
667 | } |
668 | ||
ef6980b6 JHS |
669 | static int tcf_ife_decode(struct sk_buff *skb, const struct tc_action *a, |
670 | struct tcf_result *res) | |
671 | { | |
a85a970a | 672 | struct tcf_ife_info *ife = to_ife(a); |
ef6980b6 | 673 | int action = ife->tcf_action; |
295a6e06 YG |
674 | u8 *ifehdr_end; |
675 | u8 *tlv_data; | |
676 | u16 metalen; | |
ef6980b6 | 677 | |
ced273ea | 678 | bstats_cpu_update(this_cpu_ptr(ife->common.cpu_bstats), skb); |
9c4a4e48 | 679 | tcf_lastuse_update(&ife->tcf_tm); |
ef6980b6 | 680 | |
295a6e06 YG |
681 | if (skb_at_tc_ingress(skb)) |
682 | skb_push(skb, skb->dev->hard_header_len); | |
683 | ||
684 | tlv_data = ife_decode(skb, &metalen); | |
685 | if (unlikely(!tlv_data)) { | |
ced273ea | 686 | qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats)); |
ef6980b6 JHS |
687 | return TC_ACT_SHOT; |
688 | } | |
689 | ||
295a6e06 YG |
690 | ifehdr_end = tlv_data + metalen; |
691 | for (; tlv_data < ifehdr_end; tlv_data = ife_tlv_meta_next(tlv_data)) { | |
692 | u8 *curr_data; | |
693 | u16 mtype; | |
694 | u16 dlen; | |
ef6980b6 | 695 | |
cc74eddd AA |
696 | curr_data = ife_tlv_meta_decode(tlv_data, ifehdr_end, &mtype, |
697 | &dlen, NULL); | |
698 | if (!curr_data) { | |
699 | qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats)); | |
700 | return TC_ACT_SHOT; | |
701 | } | |
ef6980b6 | 702 | |
295a6e06 | 703 | if (find_decode_metaid(skb, ife, mtype, dlen, curr_data)) { |
ef6980b6 JHS |
704 | /* abuse overlimits to count when we receive metadata |
705 | * but dont have an ops for it | |
706 | */ | |
295a6e06 YG |
707 | pr_info_ratelimited("Unknown metaid %d dlen %d\n", |
708 | mtype, dlen); | |
ced273ea | 709 | qstats_overlimit_inc(this_cpu_ptr(ife->common.cpu_qstats)); |
ef6980b6 | 710 | } |
295a6e06 | 711 | } |
ef6980b6 | 712 | |
295a6e06 | 713 | if (WARN_ON(tlv_data != ifehdr_end)) { |
ced273ea | 714 | qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats)); |
295a6e06 | 715 | return TC_ACT_SHOT; |
ef6980b6 JHS |
716 | } |
717 | ||
295a6e06 | 718 | skb->protocol = eth_type_trans(skb, skb->dev); |
ef6980b6 | 719 | skb_reset_network_header(skb); |
295a6e06 | 720 | |
ef6980b6 JHS |
721 | return action; |
722 | } | |
723 | ||
724 | /*XXX: check if we can do this at install time instead of current | |
725 | * send data path | |
726 | **/ | |
727 | static int ife_get_sz(struct sk_buff *skb, struct tcf_ife_info *ife) | |
728 | { | |
729 | struct tcf_meta_info *e, *n; | |
730 | int tot_run_sz = 0, run_sz = 0; | |
731 | ||
732 | list_for_each_entry_safe(e, n, &ife->metalist, metalist) { | |
733 | if (e->ops->check_presence) { | |
734 | run_sz = e->ops->check_presence(skb, e); | |
735 | tot_run_sz += run_sz; | |
736 | } | |
737 | } | |
738 | ||
739 | return tot_run_sz; | |
740 | } | |
741 | ||
742 | static int tcf_ife_encode(struct sk_buff *skb, const struct tc_action *a, | |
aa9fd9a3 | 743 | struct tcf_result *res, struct tcf_ife_params *p) |
ef6980b6 | 744 | { |
a85a970a | 745 | struct tcf_ife_info *ife = to_ife(a); |
ef6980b6 JHS |
746 | int action = ife->tcf_action; |
747 | struct ethhdr *oethh; /* outer ether header */ | |
ef6980b6 JHS |
748 | struct tcf_meta_info *e; |
749 | /* | |
750 | OUTERHDR:TOTMETALEN:{TLVHDR:Metadatum:TLVHDR..}:ORIGDATA | |
751 | where ORIGDATA = original ethernet header ... | |
752 | */ | |
753 | u16 metalen = ife_get_sz(skb, ife); | |
754 | int hdrm = metalen + skb->dev->hard_header_len + IFE_METAHDRLEN; | |
295a6e06 | 755 | unsigned int skboff = 0; |
ef6980b6 JHS |
756 | int new_len = skb->len + hdrm; |
757 | bool exceed_mtu = false; | |
295a6e06 YG |
758 | void *ife_meta; |
759 | int err = 0; | |
ef6980b6 | 760 | |
a5135bcf | 761 | if (!skb_at_tc_ingress(skb)) { |
ef6980b6 JHS |
762 | if (new_len > skb->dev->mtu) |
763 | exceed_mtu = true; | |
764 | } | |
765 | ||
ced273ea | 766 | bstats_cpu_update(this_cpu_ptr(ife->common.cpu_bstats), skb); |
9c4a4e48 | 767 | tcf_lastuse_update(&ife->tcf_tm); |
ef6980b6 JHS |
768 | |
769 | if (!metalen) { /* no metadata to send */ | |
770 | /* abuse overlimits to count when we allow packet | |
771 | * with no metadata | |
772 | */ | |
ced273ea | 773 | qstats_overlimit_inc(this_cpu_ptr(ife->common.cpu_qstats)); |
ef6980b6 JHS |
774 | return action; |
775 | } | |
776 | /* could be stupid policy setup or mtu config | |
777 | * so lets be conservative.. */ | |
778 | if ((action == TC_ACT_SHOT) || exceed_mtu) { | |
ced273ea | 779 | qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats)); |
ef6980b6 JHS |
780 | return TC_ACT_SHOT; |
781 | } | |
782 | ||
a5135bcf | 783 | if (skb_at_tc_ingress(skb)) |
ef6980b6 JHS |
784 | skb_push(skb, skb->dev->hard_header_len); |
785 | ||
295a6e06 | 786 | ife_meta = ife_encode(skb, metalen); |
ef6980b6 | 787 | |
ced273ea AA |
788 | spin_lock(&ife->tcf_lock); |
789 | ||
ef6980b6 JHS |
790 | /* XXX: we dont have a clever way of telling encode to |
791 | * not repeat some of the computations that are done by | |
792 | * ops->presence_check... | |
793 | */ | |
794 | list_for_each_entry(e, &ife->metalist, metalist) { | |
795 | if (e->ops->encode) { | |
295a6e06 | 796 | err = e->ops->encode(skb, (void *)(ife_meta + skboff), |
ef6980b6 JHS |
797 | e); |
798 | } | |
799 | if (err < 0) { | |
800 | /* too corrupt to keep around if overwritten */ | |
ef6980b6 | 801 | spin_unlock(&ife->tcf_lock); |
ced273ea | 802 | qstats_drop_inc(this_cpu_ptr(ife->common.cpu_qstats)); |
ef6980b6 JHS |
803 | return TC_ACT_SHOT; |
804 | } | |
805 | skboff += err; | |
806 | } | |
aa9fd9a3 | 807 | spin_unlock(&ife->tcf_lock); |
295a6e06 | 808 | oethh = (struct ethhdr *)skb->data; |
ef6980b6 | 809 | |
aa9fd9a3 AA |
810 | if (!is_zero_ether_addr(p->eth_src)) |
811 | ether_addr_copy(oethh->h_source, p->eth_src); | |
812 | if (!is_zero_ether_addr(p->eth_dst)) | |
813 | ether_addr_copy(oethh->h_dest, p->eth_dst); | |
814 | oethh->h_proto = htons(p->eth_type); | |
ef6980b6 | 815 | |
a5135bcf | 816 | if (skb_at_tc_ingress(skb)) |
ef6980b6 JHS |
817 | skb_pull(skb, skb->dev->hard_header_len); |
818 | ||
ef6980b6 JHS |
819 | return action; |
820 | } | |
821 | ||
822 | static int tcf_ife_act(struct sk_buff *skb, const struct tc_action *a, | |
823 | struct tcf_result *res) | |
824 | { | |
a85a970a | 825 | struct tcf_ife_info *ife = to_ife(a); |
aa9fd9a3 AA |
826 | struct tcf_ife_params *p; |
827 | int ret; | |
828 | ||
7fd4b288 | 829 | p = rcu_dereference_bh(ife->params); |
aa9fd9a3 AA |
830 | if (p->flags & IFE_ENCODE) { |
831 | ret = tcf_ife_encode(skb, a, res, p); | |
aa9fd9a3 AA |
832 | return ret; |
833 | } | |
ef6980b6 | 834 | |
734534e9 | 835 | return tcf_ife_decode(skb, a, res); |
ef6980b6 JHS |
836 | } |
837 | ||
838 | static int tcf_ife_walker(struct net *net, struct sk_buff *skb, | |
839 | struct netlink_callback *cb, int type, | |
41780105 AA |
840 | const struct tc_action_ops *ops, |
841 | struct netlink_ext_ack *extack) | |
ef6980b6 JHS |
842 | { |
843 | struct tc_action_net *tn = net_generic(net, ife_net_id); | |
844 | ||
b3620145 | 845 | return tcf_generic_walker(tn, skb, cb, type, ops, extack); |
ef6980b6 JHS |
846 | } |
847 | ||
331a9295 AA |
848 | static int tcf_ife_search(struct net *net, struct tc_action **a, u32 index, |
849 | struct netlink_ext_ack *extack) | |
ef6980b6 JHS |
850 | { |
851 | struct tc_action_net *tn = net_generic(net, ife_net_id); | |
852 | ||
65a206c0 | 853 | return tcf_idr_search(tn, a, index); |
ef6980b6 JHS |
854 | } |
855 | ||
856 | static struct tc_action_ops act_ife_ops = { | |
857 | .kind = "ife", | |
858 | .type = TCA_ACT_IFE, | |
859 | .owner = THIS_MODULE, | |
860 | .act = tcf_ife_act, | |
861 | .dump = tcf_ife_dump, | |
862 | .cleanup = tcf_ife_cleanup, | |
863 | .init = tcf_ife_init, | |
864 | .walk = tcf_ife_walker, | |
865 | .lookup = tcf_ife_search, | |
a85a970a | 866 | .size = sizeof(struct tcf_ife_info), |
ef6980b6 JHS |
867 | }; |
868 | ||
869 | static __net_init int ife_init_net(struct net *net) | |
870 | { | |
871 | struct tc_action_net *tn = net_generic(net, ife_net_id); | |
872 | ||
c7e460ce | 873 | return tc_action_net_init(tn, &act_ife_ops); |
ef6980b6 JHS |
874 | } |
875 | ||
039af9c6 | 876 | static void __net_exit ife_exit_net(struct list_head *net_list) |
ef6980b6 | 877 | { |
039af9c6 | 878 | tc_action_net_exit(net_list, ife_net_id); |
ef6980b6 JHS |
879 | } |
880 | ||
881 | static struct pernet_operations ife_net_ops = { | |
882 | .init = ife_init_net, | |
039af9c6 | 883 | .exit_batch = ife_exit_net, |
ef6980b6 JHS |
884 | .id = &ife_net_id, |
885 | .size = sizeof(struct tc_action_net), | |
886 | }; | |
887 | ||
888 | static int __init ife_init_module(void) | |
889 | { | |
890 | return tcf_register_action(&act_ife_ops, &ife_net_ops); | |
891 | } | |
892 | ||
893 | static void __exit ife_cleanup_module(void) | |
894 | { | |
895 | tcf_unregister_action(&act_ife_ops, &ife_net_ops); | |
896 | } | |
897 | ||
898 | module_init(ife_init_module); | |
899 | module_exit(ife_cleanup_module); | |
900 | ||
901 | MODULE_AUTHOR("Jamal Hadi Salim(2015)"); | |
902 | MODULE_DESCRIPTION("Inter-FE LFB action"); | |
903 | MODULE_LICENSE("GPL"); |