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> | |
35 | ||
36 | #define IFE_TAB_MASK 15 | |
37 | ||
38 | static int ife_net_id; | |
39 | static int max_metacnt = IFE_META_MAX + 1; | |
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 | ||
48 | /* Caller takes care of presenting data in network order | |
49 | */ | |
50 | int ife_tlv_meta_encode(void *skbdata, u16 attrtype, u16 dlen, const void *dval) | |
51 | { | |
52 | u32 *tlv = (u32 *)(skbdata); | |
53 | u16 totlen = nla_total_size(dlen); /*alignment + hdr */ | |
54 | char *dptr = (char *)tlv + NLA_HDRLEN; | |
55 | u32 htlv = attrtype << 16 | totlen; | |
56 | ||
57 | *tlv = htonl(htlv); | |
58 | memset(dptr, 0, totlen - NLA_HDRLEN); | |
59 | memcpy(dptr, dval, dlen); | |
60 | ||
61 | return totlen; | |
62 | } | |
63 | EXPORT_SYMBOL_GPL(ife_tlv_meta_encode); | |
64 | ||
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 | ||
83 | int ife_encode_meta_u32(u32 metaval, void *skbdata, struct tcf_meta_info *mi) | |
84 | { | |
85 | u32 edata = metaval; | |
86 | ||
87 | if (mi->metaval) | |
88 | edata = *(u32 *)mi->metaval; | |
89 | else if (metaval) | |
90 | edata = metaval; | |
91 | ||
92 | if (!edata) /* will not encode */ | |
93 | return 0; | |
94 | ||
95 | edata = htonl(edata); | |
96 | return ife_tlv_meta_encode(skbdata, mi->metaid, 4, &edata); | |
97 | } | |
98 | EXPORT_SYMBOL_GPL(ife_encode_meta_u32); | |
99 | ||
100 | int ife_get_meta_u16(struct sk_buff *skb, struct tcf_meta_info *mi) | |
101 | { | |
102 | if (mi->metaval) | |
103 | return nla_put_u16(skb, mi->metaid, *(u16 *)mi->metaval); | |
104 | else | |
105 | return nla_put(skb, mi->metaid, 0, NULL); | |
106 | } | |
107 | EXPORT_SYMBOL_GPL(ife_get_meta_u16); | |
108 | ||
067a7cd0 | 109 | int ife_alloc_meta_u32(struct tcf_meta_info *mi, void *metaval, gfp_t gfp) |
ef6980b6 | 110 | { |
067a7cd0 | 111 | mi->metaval = kmemdup(metaval, sizeof(u32), gfp); |
ef6980b6 JHS |
112 | if (!mi->metaval) |
113 | return -ENOMEM; | |
114 | ||
115 | return 0; | |
116 | } | |
117 | EXPORT_SYMBOL_GPL(ife_alloc_meta_u32); | |
118 | ||
067a7cd0 | 119 | int ife_alloc_meta_u16(struct tcf_meta_info *mi, void *metaval, gfp_t gfp) |
ef6980b6 | 120 | { |
067a7cd0 | 121 | mi->metaval = kmemdup(metaval, sizeof(u16), gfp); |
ef6980b6 JHS |
122 | if (!mi->metaval) |
123 | return -ENOMEM; | |
124 | ||
125 | return 0; | |
126 | } | |
127 | EXPORT_SYMBOL_GPL(ife_alloc_meta_u16); | |
128 | ||
129 | void ife_release_meta_gen(struct tcf_meta_info *mi) | |
130 | { | |
131 | kfree(mi->metaval); | |
132 | } | |
133 | EXPORT_SYMBOL_GPL(ife_release_meta_gen); | |
134 | ||
135 | int ife_validate_meta_u32(void *val, int len) | |
136 | { | |
137 | if (len == 4) | |
138 | return 0; | |
139 | ||
140 | return -EINVAL; | |
141 | } | |
142 | EXPORT_SYMBOL_GPL(ife_validate_meta_u32); | |
143 | ||
144 | int ife_validate_meta_u16(void *val, int len) | |
145 | { | |
146 | /* length will include padding */ | |
147 | if (len == NLA_ALIGN(2)) | |
148 | return 0; | |
149 | ||
150 | return -EINVAL; | |
151 | } | |
152 | EXPORT_SYMBOL_GPL(ife_validate_meta_u16); | |
153 | ||
154 | static LIST_HEAD(ifeoplist); | |
155 | static DEFINE_RWLOCK(ife_mod_lock); | |
156 | ||
157 | static struct tcf_meta_ops *find_ife_oplist(u16 metaid) | |
158 | { | |
159 | struct tcf_meta_ops *o; | |
160 | ||
161 | read_lock(&ife_mod_lock); | |
162 | list_for_each_entry(o, &ifeoplist, list) { | |
163 | if (o->metaid == metaid) { | |
164 | if (!try_module_get(o->owner)) | |
165 | o = NULL; | |
166 | read_unlock(&ife_mod_lock); | |
167 | return o; | |
168 | } | |
169 | } | |
170 | read_unlock(&ife_mod_lock); | |
171 | ||
172 | return NULL; | |
173 | } | |
174 | ||
175 | int register_ife_op(struct tcf_meta_ops *mops) | |
176 | { | |
177 | struct tcf_meta_ops *m; | |
178 | ||
179 | if (!mops->metaid || !mops->metatype || !mops->name || | |
180 | !mops->check_presence || !mops->encode || !mops->decode || | |
181 | !mops->get || !mops->alloc) | |
182 | return -EINVAL; | |
183 | ||
184 | write_lock(&ife_mod_lock); | |
185 | ||
186 | list_for_each_entry(m, &ifeoplist, list) { | |
187 | if (m->metaid == mops->metaid || | |
188 | (strcmp(mops->name, m->name) == 0)) { | |
189 | write_unlock(&ife_mod_lock); | |
190 | return -EEXIST; | |
191 | } | |
192 | } | |
193 | ||
194 | if (!mops->release) | |
195 | mops->release = ife_release_meta_gen; | |
196 | ||
197 | list_add_tail(&mops->list, &ifeoplist); | |
198 | write_unlock(&ife_mod_lock); | |
199 | return 0; | |
200 | } | |
201 | EXPORT_SYMBOL_GPL(unregister_ife_op); | |
202 | ||
203 | int unregister_ife_op(struct tcf_meta_ops *mops) | |
204 | { | |
205 | struct tcf_meta_ops *m; | |
206 | int err = -ENOENT; | |
207 | ||
208 | write_lock(&ife_mod_lock); | |
209 | list_for_each_entry(m, &ifeoplist, list) { | |
210 | if (m->metaid == mops->metaid) { | |
211 | list_del(&mops->list); | |
212 | err = 0; | |
213 | break; | |
214 | } | |
215 | } | |
216 | write_unlock(&ife_mod_lock); | |
217 | ||
218 | return err; | |
219 | } | |
220 | EXPORT_SYMBOL_GPL(register_ife_op); | |
221 | ||
222 | static int ife_validate_metatype(struct tcf_meta_ops *ops, void *val, int len) | |
223 | { | |
224 | int ret = 0; | |
225 | /* XXX: unfortunately cant use nla_policy at this point | |
226 | * because a length of 0 is valid in the case of | |
227 | * "allow". "use" semantics do enforce for proper | |
228 | * length and i couldve use nla_policy but it makes it hard | |
229 | * to use it just for that.. | |
230 | */ | |
231 | if (ops->validate) | |
232 | return ops->validate(val, len); | |
233 | ||
234 | if (ops->metatype == NLA_U32) | |
235 | ret = ife_validate_meta_u32(val, len); | |
236 | else if (ops->metatype == NLA_U16) | |
237 | ret = ife_validate_meta_u16(val, len); | |
238 | ||
239 | return ret; | |
240 | } | |
241 | ||
242 | /* called when adding new meta information | |
067a7cd0 | 243 | * under ife->tcf_lock for existing action |
ef6980b6 JHS |
244 | */ |
245 | static int load_metaops_and_vet(struct tcf_ife_info *ife, u32 metaid, | |
067a7cd0 | 246 | void *val, int len, bool exists) |
ef6980b6 JHS |
247 | { |
248 | struct tcf_meta_ops *ops = find_ife_oplist(metaid); | |
249 | int ret = 0; | |
250 | ||
251 | if (!ops) { | |
252 | ret = -ENOENT; | |
253 | #ifdef CONFIG_MODULES | |
067a7cd0 WC |
254 | if (exists) |
255 | spin_unlock_bh(&ife->tcf_lock); | |
ef6980b6 JHS |
256 | rtnl_unlock(); |
257 | request_module("ifemeta%u", metaid); | |
258 | rtnl_lock(); | |
067a7cd0 WC |
259 | if (exists) |
260 | spin_lock_bh(&ife->tcf_lock); | |
ef6980b6 JHS |
261 | ops = find_ife_oplist(metaid); |
262 | #endif | |
263 | } | |
264 | ||
265 | if (ops) { | |
266 | ret = 0; | |
267 | if (len) | |
268 | ret = ife_validate_metatype(ops, val, len); | |
269 | ||
270 | module_put(ops->owner); | |
271 | } | |
272 | ||
273 | return ret; | |
274 | } | |
275 | ||
276 | /* called when adding new meta information | |
067a7cd0 | 277 | * under ife->tcf_lock for existing action |
ef6980b6 JHS |
278 | */ |
279 | static int add_metainfo(struct tcf_ife_info *ife, u32 metaid, void *metaval, | |
817e9f2c | 280 | int len, bool atomic) |
ef6980b6 JHS |
281 | { |
282 | struct tcf_meta_info *mi = NULL; | |
283 | struct tcf_meta_ops *ops = find_ife_oplist(metaid); | |
284 | int ret = 0; | |
285 | ||
286 | if (!ops) | |
287 | return -ENOENT; | |
288 | ||
817e9f2c | 289 | mi = kzalloc(sizeof(*mi), atomic ? GFP_ATOMIC : GFP_KERNEL); |
ef6980b6 JHS |
290 | if (!mi) { |
291 | /*put back what find_ife_oplist took */ | |
292 | module_put(ops->owner); | |
293 | return -ENOMEM; | |
294 | } | |
295 | ||
296 | mi->metaid = metaid; | |
297 | mi->ops = ops; | |
298 | if (len > 0) { | |
817e9f2c | 299 | ret = ops->alloc(mi, metaval, atomic ? GFP_ATOMIC : GFP_KERNEL); |
ef6980b6 JHS |
300 | if (ret != 0) { |
301 | kfree(mi); | |
302 | module_put(ops->owner); | |
303 | return ret; | |
304 | } | |
305 | } | |
306 | ||
307 | list_add_tail(&mi->metalist, &ife->metalist); | |
308 | ||
309 | return ret; | |
310 | } | |
311 | ||
312 | static int use_all_metadata(struct tcf_ife_info *ife) | |
313 | { | |
314 | struct tcf_meta_ops *o; | |
315 | int rc = 0; | |
316 | int installed = 0; | |
317 | ||
817e9f2c | 318 | read_lock(&ife_mod_lock); |
ef6980b6 | 319 | list_for_each_entry(o, &ifeoplist, list) { |
817e9f2c | 320 | rc = add_metainfo(ife, o->metaid, NULL, 0, true); |
ef6980b6 JHS |
321 | if (rc == 0) |
322 | installed += 1; | |
323 | } | |
817e9f2c | 324 | read_unlock(&ife_mod_lock); |
ef6980b6 JHS |
325 | |
326 | if (installed) | |
327 | return 0; | |
328 | else | |
329 | return -EINVAL; | |
330 | } | |
331 | ||
332 | static int dump_metalist(struct sk_buff *skb, struct tcf_ife_info *ife) | |
333 | { | |
334 | struct tcf_meta_info *e; | |
335 | struct nlattr *nest; | |
336 | unsigned char *b = skb_tail_pointer(skb); | |
337 | int total_encoded = 0; | |
338 | ||
339 | /*can only happen on decode */ | |
340 | if (list_empty(&ife->metalist)) | |
341 | return 0; | |
342 | ||
343 | nest = nla_nest_start(skb, TCA_IFE_METALST); | |
344 | if (!nest) | |
345 | goto out_nlmsg_trim; | |
346 | ||
347 | list_for_each_entry(e, &ife->metalist, metalist) { | |
348 | if (!e->ops->get(skb, e)) | |
349 | total_encoded += 1; | |
350 | } | |
351 | ||
352 | if (!total_encoded) | |
353 | goto out_nlmsg_trim; | |
354 | ||
355 | nla_nest_end(skb, nest); | |
356 | ||
357 | return 0; | |
358 | ||
359 | out_nlmsg_trim: | |
360 | nlmsg_trim(skb, b); | |
361 | return -1; | |
362 | } | |
363 | ||
364 | /* under ife->tcf_lock */ | |
365 | static void _tcf_ife_cleanup(struct tc_action *a, int bind) | |
366 | { | |
367 | struct tcf_ife_info *ife = a->priv; | |
368 | struct tcf_meta_info *e, *n; | |
369 | ||
370 | list_for_each_entry_safe(e, n, &ife->metalist, metalist) { | |
371 | module_put(e->ops->owner); | |
372 | list_del(&e->metalist); | |
373 | if (e->metaval) { | |
374 | if (e->ops->release) | |
375 | e->ops->release(e); | |
376 | else | |
377 | kfree(e->metaval); | |
378 | } | |
379 | kfree(e); | |
380 | } | |
381 | } | |
382 | ||
383 | static void tcf_ife_cleanup(struct tc_action *a, int bind) | |
384 | { | |
385 | struct tcf_ife_info *ife = a->priv; | |
386 | ||
387 | spin_lock_bh(&ife->tcf_lock); | |
388 | _tcf_ife_cleanup(a, bind); | |
389 | spin_unlock_bh(&ife->tcf_lock); | |
390 | } | |
391 | ||
067a7cd0 WC |
392 | /* under ife->tcf_lock for existing action */ |
393 | static int populate_metalist(struct tcf_ife_info *ife, struct nlattr **tb, | |
394 | bool exists) | |
ef6980b6 JHS |
395 | { |
396 | int len = 0; | |
397 | int rc = 0; | |
398 | int i = 0; | |
399 | void *val; | |
400 | ||
401 | for (i = 1; i < max_metacnt; i++) { | |
402 | if (tb[i]) { | |
403 | val = nla_data(tb[i]); | |
404 | len = nla_len(tb[i]); | |
405 | ||
067a7cd0 | 406 | rc = load_metaops_and_vet(ife, i, val, len, exists); |
ef6980b6 JHS |
407 | if (rc != 0) |
408 | return rc; | |
409 | ||
067a7cd0 | 410 | rc = add_metainfo(ife, i, val, len, exists); |
ef6980b6 JHS |
411 | if (rc) |
412 | return rc; | |
413 | } | |
414 | } | |
415 | ||
416 | return rc; | |
417 | } | |
418 | ||
419 | static int tcf_ife_init(struct net *net, struct nlattr *nla, | |
420 | struct nlattr *est, struct tc_action *a, | |
421 | int ovr, int bind) | |
422 | { | |
423 | struct tc_action_net *tn = net_generic(net, ife_net_id); | |
424 | struct nlattr *tb[TCA_IFE_MAX + 1]; | |
425 | struct nlattr *tb2[IFE_META_MAX + 1]; | |
426 | struct tcf_ife_info *ife; | |
427 | struct tc_ife *parm; | |
428 | u16 ife_type = 0; | |
429 | u8 *daddr = NULL; | |
430 | u8 *saddr = NULL; | |
b2313077 WC |
431 | bool exists = false; |
432 | int ret = 0; | |
ef6980b6 JHS |
433 | int err; |
434 | ||
435 | err = nla_parse_nested(tb, TCA_IFE_MAX, nla, ife_policy); | |
436 | if (err < 0) | |
437 | return err; | |
438 | ||
439 | if (!tb[TCA_IFE_PARMS]) | |
440 | return -EINVAL; | |
441 | ||
442 | parm = nla_data(tb[TCA_IFE_PARMS]); | |
443 | ||
4e8c8615 JHS |
444 | exists = tcf_hash_check(tn, parm->index, a, bind); |
445 | if (exists && bind) | |
446 | return 0; | |
447 | ||
ef6980b6 JHS |
448 | if (parm->flags & IFE_ENCODE) { |
449 | /* Until we get issued the ethertype, we cant have | |
450 | * a default.. | |
451 | **/ | |
452 | if (!tb[TCA_IFE_TYPE]) { | |
4e8c8615 JHS |
453 | if (exists) |
454 | tcf_hash_release(a, bind); | |
ef6980b6 JHS |
455 | pr_info("You MUST pass etherype for encoding\n"); |
456 | return -EINVAL; | |
457 | } | |
458 | } | |
459 | ||
4e8c8615 | 460 | if (!exists) { |
ef6980b6 JHS |
461 | ret = tcf_hash_create(tn, parm->index, est, a, sizeof(*ife), |
462 | bind, false); | |
463 | if (ret) | |
464 | return ret; | |
465 | ret = ACT_P_CREATED; | |
466 | } else { | |
ef6980b6 JHS |
467 | tcf_hash_release(a, bind); |
468 | if (!ovr) | |
469 | return -EEXIST; | |
470 | } | |
471 | ||
472 | ife = to_ife(a); | |
473 | ife->flags = parm->flags; | |
474 | ||
475 | if (parm->flags & IFE_ENCODE) { | |
476 | ife_type = nla_get_u16(tb[TCA_IFE_TYPE]); | |
477 | if (tb[TCA_IFE_DMAC]) | |
478 | daddr = nla_data(tb[TCA_IFE_DMAC]); | |
479 | if (tb[TCA_IFE_SMAC]) | |
480 | saddr = nla_data(tb[TCA_IFE_SMAC]); | |
481 | } | |
482 | ||
067a7cd0 WC |
483 | if (exists) |
484 | spin_lock_bh(&ife->tcf_lock); | |
ef6980b6 JHS |
485 | ife->tcf_action = parm->action; |
486 | ||
487 | if (parm->flags & IFE_ENCODE) { | |
488 | if (daddr) | |
489 | ether_addr_copy(ife->eth_dst, daddr); | |
490 | else | |
491 | eth_zero_addr(ife->eth_dst); | |
492 | ||
493 | if (saddr) | |
494 | ether_addr_copy(ife->eth_src, saddr); | |
495 | else | |
496 | eth_zero_addr(ife->eth_src); | |
497 | ||
498 | ife->eth_type = ife_type; | |
499 | } | |
500 | ||
501 | if (ret == ACT_P_CREATED) | |
502 | INIT_LIST_HEAD(&ife->metalist); | |
503 | ||
504 | if (tb[TCA_IFE_METALST]) { | |
505 | err = nla_parse_nested(tb2, IFE_META_MAX, tb[TCA_IFE_METALST], | |
506 | NULL); | |
507 | if (err) { | |
508 | metadata_parse_err: | |
4e8c8615 JHS |
509 | if (exists) |
510 | tcf_hash_release(a, bind); | |
ef6980b6 JHS |
511 | if (ret == ACT_P_CREATED) |
512 | _tcf_ife_cleanup(a, bind); | |
513 | ||
067a7cd0 WC |
514 | if (exists) |
515 | spin_unlock_bh(&ife->tcf_lock); | |
ef6980b6 JHS |
516 | return err; |
517 | } | |
518 | ||
067a7cd0 | 519 | err = populate_metalist(ife, tb2, exists); |
ef6980b6 JHS |
520 | if (err) |
521 | goto metadata_parse_err; | |
522 | ||
523 | } else { | |
524 | /* if no passed metadata allow list or passed allow-all | |
525 | * then here we process by adding as many supported metadatum | |
526 | * as we can. You better have at least one else we are | |
527 | * going to bail out | |
528 | */ | |
529 | err = use_all_metadata(ife); | |
530 | if (err) { | |
531 | if (ret == ACT_P_CREATED) | |
532 | _tcf_ife_cleanup(a, bind); | |
533 | ||
067a7cd0 WC |
534 | if (exists) |
535 | spin_unlock_bh(&ife->tcf_lock); | |
ef6980b6 JHS |
536 | return err; |
537 | } | |
538 | } | |
539 | ||
067a7cd0 WC |
540 | if (exists) |
541 | spin_unlock_bh(&ife->tcf_lock); | |
ef6980b6 JHS |
542 | |
543 | if (ret == ACT_P_CREATED) | |
544 | tcf_hash_insert(tn, a); | |
545 | ||
546 | return ret; | |
547 | } | |
548 | ||
549 | static int tcf_ife_dump(struct sk_buff *skb, struct tc_action *a, int bind, | |
550 | int ref) | |
551 | { | |
552 | unsigned char *b = skb_tail_pointer(skb); | |
553 | struct tcf_ife_info *ife = a->priv; | |
554 | struct tc_ife opt = { | |
555 | .index = ife->tcf_index, | |
556 | .refcnt = ife->tcf_refcnt - ref, | |
557 | .bindcnt = ife->tcf_bindcnt - bind, | |
558 | .action = ife->tcf_action, | |
559 | .flags = ife->flags, | |
560 | }; | |
561 | struct tcf_t t; | |
562 | ||
563 | if (nla_put(skb, TCA_IFE_PARMS, sizeof(opt), &opt)) | |
564 | goto nla_put_failure; | |
565 | ||
48d8ee16 | 566 | tcf_tm_dump(&t, &ife->tcf_tm); |
9854518e | 567 | if (nla_put_64bit(skb, TCA_IFE_TM, sizeof(t), &t, TCA_IFE_PAD)) |
ef6980b6 JHS |
568 | goto nla_put_failure; |
569 | ||
570 | if (!is_zero_ether_addr(ife->eth_dst)) { | |
571 | if (nla_put(skb, TCA_IFE_DMAC, ETH_ALEN, ife->eth_dst)) | |
572 | goto nla_put_failure; | |
573 | } | |
574 | ||
575 | if (!is_zero_ether_addr(ife->eth_src)) { | |
576 | if (nla_put(skb, TCA_IFE_SMAC, ETH_ALEN, ife->eth_src)) | |
577 | goto nla_put_failure; | |
578 | } | |
579 | ||
580 | if (nla_put(skb, TCA_IFE_TYPE, 2, &ife->eth_type)) | |
581 | goto nla_put_failure; | |
582 | ||
583 | if (dump_metalist(skb, ife)) { | |
584 | /*ignore failure to dump metalist */ | |
585 | pr_info("Failed to dump metalist\n"); | |
586 | } | |
587 | ||
588 | return skb->len; | |
589 | ||
590 | nla_put_failure: | |
591 | nlmsg_trim(skb, b); | |
592 | return -1; | |
593 | } | |
594 | ||
595 | int find_decode_metaid(struct sk_buff *skb, struct tcf_ife_info *ife, | |
596 | u16 metaid, u16 mlen, void *mdata) | |
597 | { | |
598 | struct tcf_meta_info *e; | |
599 | ||
600 | /* XXX: use hash to speed up */ | |
601 | list_for_each_entry(e, &ife->metalist, metalist) { | |
602 | if (metaid == e->metaid) { | |
603 | if (e->ops) { | |
604 | /* We check for decode presence already */ | |
605 | return e->ops->decode(skb, mdata, mlen); | |
606 | } | |
607 | } | |
608 | } | |
609 | ||
610 | return 0; | |
611 | } | |
612 | ||
613 | struct ifeheadr { | |
614 | __be16 metalen; | |
615 | u8 tlv_data[]; | |
616 | }; | |
617 | ||
618 | struct meta_tlvhdr { | |
619 | __be16 type; | |
620 | __be16 len; | |
621 | }; | |
622 | ||
623 | static int tcf_ife_decode(struct sk_buff *skb, const struct tc_action *a, | |
624 | struct tcf_result *res) | |
625 | { | |
626 | struct tcf_ife_info *ife = a->priv; | |
627 | int action = ife->tcf_action; | |
628 | struct ifeheadr *ifehdr = (struct ifeheadr *)skb->data; | |
629 | u16 ifehdrln = ifehdr->metalen; | |
630 | struct meta_tlvhdr *tlv = (struct meta_tlvhdr *)(ifehdr->tlv_data); | |
631 | ||
632 | spin_lock(&ife->tcf_lock); | |
633 | bstats_update(&ife->tcf_bstats, skb); | |
9c4a4e48 | 634 | tcf_lastuse_update(&ife->tcf_tm); |
ef6980b6 JHS |
635 | spin_unlock(&ife->tcf_lock); |
636 | ||
637 | ifehdrln = ntohs(ifehdrln); | |
638 | if (unlikely(!pskb_may_pull(skb, ifehdrln))) { | |
639 | spin_lock(&ife->tcf_lock); | |
640 | ife->tcf_qstats.drops++; | |
641 | spin_unlock(&ife->tcf_lock); | |
642 | return TC_ACT_SHOT; | |
643 | } | |
644 | ||
645 | skb_set_mac_header(skb, ifehdrln); | |
646 | __skb_pull(skb, ifehdrln); | |
647 | skb->protocol = eth_type_trans(skb, skb->dev); | |
648 | ifehdrln -= IFE_METAHDRLEN; | |
649 | ||
650 | while (ifehdrln > 0) { | |
651 | u8 *tlvdata = (u8 *)tlv; | |
652 | u16 mtype = tlv->type; | |
653 | u16 mlen = tlv->len; | |
654 | ||
655 | mtype = ntohs(mtype); | |
656 | mlen = ntohs(mlen); | |
657 | ||
658 | if (find_decode_metaid(skb, ife, mtype, (mlen - 4), | |
659 | (void *)(tlvdata + 4))) { | |
660 | /* abuse overlimits to count when we receive metadata | |
661 | * but dont have an ops for it | |
662 | */ | |
663 | pr_info_ratelimited("Unknown metaid %d alnlen %d\n", | |
664 | mtype, mlen); | |
665 | ife->tcf_qstats.overlimits++; | |
666 | } | |
667 | ||
668 | tlvdata += mlen; | |
669 | ifehdrln -= mlen; | |
670 | tlv = (struct meta_tlvhdr *)tlvdata; | |
671 | } | |
672 | ||
673 | skb_reset_network_header(skb); | |
674 | return action; | |
675 | } | |
676 | ||
677 | /*XXX: check if we can do this at install time instead of current | |
678 | * send data path | |
679 | **/ | |
680 | static int ife_get_sz(struct sk_buff *skb, struct tcf_ife_info *ife) | |
681 | { | |
682 | struct tcf_meta_info *e, *n; | |
683 | int tot_run_sz = 0, run_sz = 0; | |
684 | ||
685 | list_for_each_entry_safe(e, n, &ife->metalist, metalist) { | |
686 | if (e->ops->check_presence) { | |
687 | run_sz = e->ops->check_presence(skb, e); | |
688 | tot_run_sz += run_sz; | |
689 | } | |
690 | } | |
691 | ||
692 | return tot_run_sz; | |
693 | } | |
694 | ||
695 | static int tcf_ife_encode(struct sk_buff *skb, const struct tc_action *a, | |
696 | struct tcf_result *res) | |
697 | { | |
698 | struct tcf_ife_info *ife = a->priv; | |
699 | int action = ife->tcf_action; | |
700 | struct ethhdr *oethh; /* outer ether header */ | |
701 | struct ethhdr *iethh; /* inner eth header */ | |
702 | struct tcf_meta_info *e; | |
703 | /* | |
704 | OUTERHDR:TOTMETALEN:{TLVHDR:Metadatum:TLVHDR..}:ORIGDATA | |
705 | where ORIGDATA = original ethernet header ... | |
706 | */ | |
707 | u16 metalen = ife_get_sz(skb, ife); | |
708 | int hdrm = metalen + skb->dev->hard_header_len + IFE_METAHDRLEN; | |
709 | unsigned int skboff = skb->dev->hard_header_len; | |
710 | u32 at = G_TC_AT(skb->tc_verd); | |
711 | int new_len = skb->len + hdrm; | |
712 | bool exceed_mtu = false; | |
713 | int err; | |
714 | ||
715 | if (at & AT_EGRESS) { | |
716 | if (new_len > skb->dev->mtu) | |
717 | exceed_mtu = true; | |
718 | } | |
719 | ||
720 | spin_lock(&ife->tcf_lock); | |
721 | bstats_update(&ife->tcf_bstats, skb); | |
9c4a4e48 | 722 | tcf_lastuse_update(&ife->tcf_tm); |
ef6980b6 JHS |
723 | |
724 | if (!metalen) { /* no metadata to send */ | |
725 | /* abuse overlimits to count when we allow packet | |
726 | * with no metadata | |
727 | */ | |
728 | ife->tcf_qstats.overlimits++; | |
729 | spin_unlock(&ife->tcf_lock); | |
730 | return action; | |
731 | } | |
732 | /* could be stupid policy setup or mtu config | |
733 | * so lets be conservative.. */ | |
734 | if ((action == TC_ACT_SHOT) || exceed_mtu) { | |
735 | ife->tcf_qstats.drops++; | |
736 | spin_unlock(&ife->tcf_lock); | |
737 | return TC_ACT_SHOT; | |
738 | } | |
739 | ||
740 | iethh = eth_hdr(skb); | |
741 | ||
742 | err = skb_cow_head(skb, hdrm); | |
743 | if (unlikely(err)) { | |
744 | ife->tcf_qstats.drops++; | |
745 | spin_unlock(&ife->tcf_lock); | |
746 | return TC_ACT_SHOT; | |
747 | } | |
748 | ||
749 | if (!(at & AT_EGRESS)) | |
750 | skb_push(skb, skb->dev->hard_header_len); | |
751 | ||
752 | __skb_push(skb, hdrm); | |
753 | memcpy(skb->data, iethh, skb->mac_len); | |
754 | skb_reset_mac_header(skb); | |
755 | oethh = eth_hdr(skb); | |
756 | ||
757 | /*total metadata length */ | |
758 | metalen += IFE_METAHDRLEN; | |
759 | metalen = htons(metalen); | |
760 | memcpy((skb->data + skboff), &metalen, IFE_METAHDRLEN); | |
761 | skboff += IFE_METAHDRLEN; | |
762 | ||
763 | /* XXX: we dont have a clever way of telling encode to | |
764 | * not repeat some of the computations that are done by | |
765 | * ops->presence_check... | |
766 | */ | |
767 | list_for_each_entry(e, &ife->metalist, metalist) { | |
768 | if (e->ops->encode) { | |
769 | err = e->ops->encode(skb, (void *)(skb->data + skboff), | |
770 | e); | |
771 | } | |
772 | if (err < 0) { | |
773 | /* too corrupt to keep around if overwritten */ | |
774 | ife->tcf_qstats.drops++; | |
775 | spin_unlock(&ife->tcf_lock); | |
776 | return TC_ACT_SHOT; | |
777 | } | |
778 | skboff += err; | |
779 | } | |
780 | ||
781 | if (!is_zero_ether_addr(ife->eth_src)) | |
782 | ether_addr_copy(oethh->h_source, ife->eth_src); | |
783 | else | |
784 | ether_addr_copy(oethh->h_source, iethh->h_source); | |
785 | if (!is_zero_ether_addr(ife->eth_dst)) | |
786 | ether_addr_copy(oethh->h_dest, ife->eth_dst); | |
787 | else | |
788 | ether_addr_copy(oethh->h_dest, iethh->h_dest); | |
789 | oethh->h_proto = htons(ife->eth_type); | |
790 | ||
791 | if (!(at & AT_EGRESS)) | |
792 | skb_pull(skb, skb->dev->hard_header_len); | |
793 | ||
794 | spin_unlock(&ife->tcf_lock); | |
795 | ||
796 | return action; | |
797 | } | |
798 | ||
799 | static int tcf_ife_act(struct sk_buff *skb, const struct tc_action *a, | |
800 | struct tcf_result *res) | |
801 | { | |
802 | struct tcf_ife_info *ife = a->priv; | |
803 | ||
804 | if (ife->flags & IFE_ENCODE) | |
805 | return tcf_ife_encode(skb, a, res); | |
806 | ||
807 | if (!(ife->flags & IFE_ENCODE)) | |
808 | return tcf_ife_decode(skb, a, res); | |
809 | ||
810 | pr_info_ratelimited("unknown failure(policy neither de/encode\n"); | |
811 | spin_lock(&ife->tcf_lock); | |
812 | bstats_update(&ife->tcf_bstats, skb); | |
9c4a4e48 | 813 | tcf_lastuse_update(&ife->tcf_tm); |
ef6980b6 JHS |
814 | ife->tcf_qstats.drops++; |
815 | spin_unlock(&ife->tcf_lock); | |
816 | ||
817 | return TC_ACT_SHOT; | |
818 | } | |
819 | ||
820 | static int tcf_ife_walker(struct net *net, struct sk_buff *skb, | |
821 | struct netlink_callback *cb, int type, | |
822 | struct tc_action *a) | |
823 | { | |
824 | struct tc_action_net *tn = net_generic(net, ife_net_id); | |
825 | ||
826 | return tcf_generic_walker(tn, skb, cb, type, a); | |
827 | } | |
828 | ||
829 | static int tcf_ife_search(struct net *net, struct tc_action *a, u32 index) | |
830 | { | |
831 | struct tc_action_net *tn = net_generic(net, ife_net_id); | |
832 | ||
833 | return tcf_hash_search(tn, a, index); | |
834 | } | |
835 | ||
836 | static struct tc_action_ops act_ife_ops = { | |
837 | .kind = "ife", | |
838 | .type = TCA_ACT_IFE, | |
839 | .owner = THIS_MODULE, | |
840 | .act = tcf_ife_act, | |
841 | .dump = tcf_ife_dump, | |
842 | .cleanup = tcf_ife_cleanup, | |
843 | .init = tcf_ife_init, | |
844 | .walk = tcf_ife_walker, | |
845 | .lookup = tcf_ife_search, | |
846 | }; | |
847 | ||
848 | static __net_init int ife_init_net(struct net *net) | |
849 | { | |
850 | struct tc_action_net *tn = net_generic(net, ife_net_id); | |
851 | ||
852 | return tc_action_net_init(tn, &act_ife_ops, IFE_TAB_MASK); | |
853 | } | |
854 | ||
855 | static void __net_exit ife_exit_net(struct net *net) | |
856 | { | |
857 | struct tc_action_net *tn = net_generic(net, ife_net_id); | |
858 | ||
859 | tc_action_net_exit(tn); | |
860 | } | |
861 | ||
862 | static struct pernet_operations ife_net_ops = { | |
863 | .init = ife_init_net, | |
864 | .exit = ife_exit_net, | |
865 | .id = &ife_net_id, | |
866 | .size = sizeof(struct tc_action_net), | |
867 | }; | |
868 | ||
869 | static int __init ife_init_module(void) | |
870 | { | |
871 | return tcf_register_action(&act_ife_ops, &ife_net_ops); | |
872 | } | |
873 | ||
874 | static void __exit ife_cleanup_module(void) | |
875 | { | |
876 | tcf_unregister_action(&act_ife_ops, &ife_net_ops); | |
877 | } | |
878 | ||
879 | module_init(ife_init_module); | |
880 | module_exit(ife_cleanup_module); | |
881 | ||
882 | MODULE_AUTHOR("Jamal Hadi Salim(2015)"); | |
883 | MODULE_DESCRIPTION("Inter-FE LFB action"); | |
884 | MODULE_LICENSE("GPL"); |