Blame view

net/sched/act_ife.c 20.2 KB
ef6980b6b   Jamal Hadi Salim   introduce IFE action
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  /*
   * net/sched/ife.c	Inter-FE action based on ForCES WG InterFE LFB
   *
   *		Refer to:
   *		draft-ietf-forces-interfelfb-03
   *		and
   *		netdev01 paper:
   *		"Distributing Linux Traffic Control Classifier-Action
   *		Subsystem"
   *		Authors: Jamal Hadi Salim and Damascene M. Joachimpillai
   *
   *		This program is free software; you can redistribute it and/or
   *		modify it under the terms of the GNU General Public License
   *		as published by the Free Software Foundation; either version
   *		2 of the License, or (at your option) any later version.
   *
   * copyright Jamal Hadi Salim (2015)
   *
  */
  
  #include <linux/types.h>
  #include <linux/kernel.h>
  #include <linux/string.h>
  #include <linux/errno.h>
  #include <linux/skbuff.h>
  #include <linux/rtnetlink.h>
  #include <linux/module.h>
  #include <linux/init.h>
  #include <net/net_namespace.h>
  #include <net/netlink.h>
  #include <net/pkt_sched.h>
  #include <uapi/linux/tc_act/tc_ife.h>
  #include <net/tc_act/tc_ife.h>
  #include <linux/etherdevice.h>
  
  #define IFE_TAB_MASK 15
  
  static int ife_net_id;
  static int max_metacnt = IFE_META_MAX + 1;
a85a970af   WANG Cong   net_sched: move t...
40
  static struct tc_action_ops act_ife_ops;
ef6980b6b   Jamal Hadi Salim   introduce IFE action
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  
  static const struct nla_policy ife_policy[TCA_IFE_MAX + 1] = {
  	[TCA_IFE_PARMS] = { .len = sizeof(struct tc_ife)},
  	[TCA_IFE_DMAC] = { .len = ETH_ALEN},
  	[TCA_IFE_SMAC] = { .len = ETH_ALEN},
  	[TCA_IFE_TYPE] = { .type = NLA_U16},
  };
  
  /* Caller takes care of presenting data in network order
  */
  int ife_tlv_meta_encode(void *skbdata, u16 attrtype, u16 dlen, const void *dval)
  {
  	u32 *tlv = (u32 *)(skbdata);
  	u16 totlen = nla_total_size(dlen);	/*alignment + hdr */
  	char *dptr = (char *)tlv + NLA_HDRLEN;
c006da0be   Yotam Gigi   act_ife: Fix fals...
56
  	u32 htlv = attrtype << 16 | (dlen + NLA_HDRLEN);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
57
58
59
60
61
62
63
64
  
  	*tlv = htonl(htlv);
  	memset(dptr, 0, totlen - NLA_HDRLEN);
  	memcpy(dptr, dval, dlen);
  
  	return totlen;
  }
  EXPORT_SYMBOL_GPL(ife_tlv_meta_encode);
6a5d58b67   Jamal Hadi Salim   net sched ife act...
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  int ife_encode_meta_u16(u16 metaval, void *skbdata, struct tcf_meta_info *mi)
  {
  	u16 edata = 0;
  
  	if (mi->metaval)
  		edata = *(u16 *)mi->metaval;
  	else if (metaval)
  		edata = metaval;
  
  	if (!edata) /* will not encode */
  		return 0;
  
  	edata = htons(edata);
  	return ife_tlv_meta_encode(skbdata, mi->metaid, 2, &edata);
  }
  EXPORT_SYMBOL_GPL(ife_encode_meta_u16);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  int ife_get_meta_u32(struct sk_buff *skb, struct tcf_meta_info *mi)
  {
  	if (mi->metaval)
  		return nla_put_u32(skb, mi->metaid, *(u32 *)mi->metaval);
  	else
  		return nla_put(skb, mi->metaid, 0, NULL);
  }
  EXPORT_SYMBOL_GPL(ife_get_meta_u32);
  
  int ife_check_meta_u32(u32 metaval, struct tcf_meta_info *mi)
  {
  	if (metaval || mi->metaval)
  		return 8; /* T+L+V == 2+2+4 */
  
  	return 0;
  }
  EXPORT_SYMBOL_GPL(ife_check_meta_u32);
6a5d58b67   Jamal Hadi Salim   net sched ife act...
98
99
100
101
102
103
104
105
  int ife_check_meta_u16(u16 metaval, struct tcf_meta_info *mi)
  {
  	if (metaval || mi->metaval)
  		return 8; /* T+L+(V) == 2+2+(2+2bytepad) */
  
  	return 0;
  }
  EXPORT_SYMBOL_GPL(ife_check_meta_u16);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  int ife_encode_meta_u32(u32 metaval, void *skbdata, struct tcf_meta_info *mi)
  {
  	u32 edata = metaval;
  
  	if (mi->metaval)
  		edata = *(u32 *)mi->metaval;
  	else if (metaval)
  		edata = metaval;
  
  	if (!edata) /* will not encode */
  		return 0;
  
  	edata = htonl(edata);
  	return ife_tlv_meta_encode(skbdata, mi->metaid, 4, &edata);
  }
  EXPORT_SYMBOL_GPL(ife_encode_meta_u32);
  
  int ife_get_meta_u16(struct sk_buff *skb, struct tcf_meta_info *mi)
  {
  	if (mi->metaval)
  		return nla_put_u16(skb, mi->metaid, *(u16 *)mi->metaval);
  	else
  		return nla_put(skb, mi->metaid, 0, NULL);
  }
  EXPORT_SYMBOL_GPL(ife_get_meta_u16);
067a7cd06   WANG Cong   act_ife: only acq...
131
  int ife_alloc_meta_u32(struct tcf_meta_info *mi, void *metaval, gfp_t gfp)
ef6980b6b   Jamal Hadi Salim   introduce IFE action
132
  {
067a7cd06   WANG Cong   act_ife: only acq...
133
  	mi->metaval = kmemdup(metaval, sizeof(u32), gfp);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
134
135
136
137
138
139
  	if (!mi->metaval)
  		return -ENOMEM;
  
  	return 0;
  }
  EXPORT_SYMBOL_GPL(ife_alloc_meta_u32);
067a7cd06   WANG Cong   act_ife: only acq...
140
  int ife_alloc_meta_u16(struct tcf_meta_info *mi, void *metaval, gfp_t gfp)
ef6980b6b   Jamal Hadi Salim   introduce IFE action
141
  {
067a7cd06   WANG Cong   act_ife: only acq...
142
  	mi->metaval = kmemdup(metaval, sizeof(u16), gfp);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
  	if (!mi->metaval)
  		return -ENOMEM;
  
  	return 0;
  }
  EXPORT_SYMBOL_GPL(ife_alloc_meta_u16);
  
  void ife_release_meta_gen(struct tcf_meta_info *mi)
  {
  	kfree(mi->metaval);
  }
  EXPORT_SYMBOL_GPL(ife_release_meta_gen);
  
  int ife_validate_meta_u32(void *val, int len)
  {
28a10c426   Jamal Hadi Salim   net sched: fix en...
158
  	if (len == sizeof(u32))
ef6980b6b   Jamal Hadi Salim   introduce IFE action
159
160
161
162
163
164
165
166
  		return 0;
  
  	return -EINVAL;
  }
  EXPORT_SYMBOL_GPL(ife_validate_meta_u32);
  
  int ife_validate_meta_u16(void *val, int len)
  {
28a10c426   Jamal Hadi Salim   net sched: fix en...
167
168
  	/* length will not include padding */
  	if (len == sizeof(u16))
ef6980b6b   Jamal Hadi Salim   introduce IFE action
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
  		return 0;
  
  	return -EINVAL;
  }
  EXPORT_SYMBOL_GPL(ife_validate_meta_u16);
  
  static LIST_HEAD(ifeoplist);
  static DEFINE_RWLOCK(ife_mod_lock);
  
  static struct tcf_meta_ops *find_ife_oplist(u16 metaid)
  {
  	struct tcf_meta_ops *o;
  
  	read_lock(&ife_mod_lock);
  	list_for_each_entry(o, &ifeoplist, list) {
  		if (o->metaid == metaid) {
  			if (!try_module_get(o->owner))
  				o = NULL;
  			read_unlock(&ife_mod_lock);
  			return o;
  		}
  	}
  	read_unlock(&ife_mod_lock);
  
  	return NULL;
  }
  
  int register_ife_op(struct tcf_meta_ops *mops)
  {
  	struct tcf_meta_ops *m;
  
  	if (!mops->metaid || !mops->metatype || !mops->name ||
  	    !mops->check_presence || !mops->encode || !mops->decode ||
  	    !mops->get || !mops->alloc)
  		return -EINVAL;
  
  	write_lock(&ife_mod_lock);
  
  	list_for_each_entry(m, &ifeoplist, list) {
  		if (m->metaid == mops->metaid ||
  		    (strcmp(mops->name, m->name) == 0)) {
  			write_unlock(&ife_mod_lock);
  			return -EEXIST;
  		}
  	}
  
  	if (!mops->release)
  		mops->release = ife_release_meta_gen;
  
  	list_add_tail(&mops->list, &ifeoplist);
  	write_unlock(&ife_mod_lock);
  	return 0;
  }
  EXPORT_SYMBOL_GPL(unregister_ife_op);
  
  int unregister_ife_op(struct tcf_meta_ops *mops)
  {
  	struct tcf_meta_ops *m;
  	int err = -ENOENT;
  
  	write_lock(&ife_mod_lock);
  	list_for_each_entry(m, &ifeoplist, list) {
  		if (m->metaid == mops->metaid) {
  			list_del(&mops->list);
  			err = 0;
  			break;
  		}
  	}
  	write_unlock(&ife_mod_lock);
  
  	return err;
  }
  EXPORT_SYMBOL_GPL(register_ife_op);
  
  static int ife_validate_metatype(struct tcf_meta_ops *ops, void *val, int len)
  {
  	int ret = 0;
  	/* XXX: unfortunately cant use nla_policy at this point
  	* because a length of 0 is valid in the case of
  	* "allow". "use" semantics do enforce for proper
  	* length and i couldve use nla_policy but it makes it hard
  	* to use it just for that..
  	*/
  	if (ops->validate)
  		return ops->validate(val, len);
  
  	if (ops->metatype == NLA_U32)
  		ret = ife_validate_meta_u32(val, len);
  	else if (ops->metatype == NLA_U16)
  		ret = ife_validate_meta_u16(val, len);
  
  	return ret;
  }
  
  /* called when adding new meta information
067a7cd06   WANG Cong   act_ife: only acq...
264
   * under ife->tcf_lock for existing action
ef6980b6b   Jamal Hadi Salim   introduce IFE action
265
266
  */
  static int load_metaops_and_vet(struct tcf_ife_info *ife, u32 metaid,
067a7cd06   WANG Cong   act_ife: only acq...
267
  				void *val, int len, bool exists)
ef6980b6b   Jamal Hadi Salim   introduce IFE action
268
269
270
271
272
273
274
  {
  	struct tcf_meta_ops *ops = find_ife_oplist(metaid);
  	int ret = 0;
  
  	if (!ops) {
  		ret = -ENOENT;
  #ifdef CONFIG_MODULES
067a7cd06   WANG Cong   act_ife: only acq...
275
276
  		if (exists)
  			spin_unlock_bh(&ife->tcf_lock);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
277
278
279
  		rtnl_unlock();
  		request_module("ifemeta%u", metaid);
  		rtnl_lock();
067a7cd06   WANG Cong   act_ife: only acq...
280
281
  		if (exists)
  			spin_lock_bh(&ife->tcf_lock);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
  		ops = find_ife_oplist(metaid);
  #endif
  	}
  
  	if (ops) {
  		ret = 0;
  		if (len)
  			ret = ife_validate_metatype(ops, val, len);
  
  		module_put(ops->owner);
  	}
  
  	return ret;
  }
  
  /* called when adding new meta information
067a7cd06   WANG Cong   act_ife: only acq...
298
   * under ife->tcf_lock for existing action
ef6980b6b   Jamal Hadi Salim   introduce IFE action
299
300
  */
  static int add_metainfo(struct tcf_ife_info *ife, u32 metaid, void *metaval,
817e9f2c5   WANG Cong   act_ife: acquire ...
301
  			int len, bool atomic)
ef6980b6b   Jamal Hadi Salim   introduce IFE action
302
303
304
305
306
307
308
  {
  	struct tcf_meta_info *mi = NULL;
  	struct tcf_meta_ops *ops = find_ife_oplist(metaid);
  	int ret = 0;
  
  	if (!ops)
  		return -ENOENT;
817e9f2c5   WANG Cong   act_ife: acquire ...
309
  	mi = kzalloc(sizeof(*mi), atomic ? GFP_ATOMIC : GFP_KERNEL);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
310
311
312
313
314
315
316
317
318
  	if (!mi) {
  		/*put back what find_ife_oplist took */
  		module_put(ops->owner);
  		return -ENOMEM;
  	}
  
  	mi->metaid = metaid;
  	mi->ops = ops;
  	if (len > 0) {
817e9f2c5   WANG Cong   act_ife: acquire ...
319
  		ret = ops->alloc(mi, metaval, atomic ? GFP_ATOMIC : GFP_KERNEL);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
  		if (ret != 0) {
  			kfree(mi);
  			module_put(ops->owner);
  			return ret;
  		}
  	}
  
  	list_add_tail(&mi->metalist, &ife->metalist);
  
  	return ret;
  }
  
  static int use_all_metadata(struct tcf_ife_info *ife)
  {
  	struct tcf_meta_ops *o;
  	int rc = 0;
  	int installed = 0;
817e9f2c5   WANG Cong   act_ife: acquire ...
337
  	read_lock(&ife_mod_lock);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
338
  	list_for_each_entry(o, &ifeoplist, list) {
817e9f2c5   WANG Cong   act_ife: acquire ...
339
  		rc = add_metainfo(ife, o->metaid, NULL, 0, true);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
340
341
342
  		if (rc == 0)
  			installed += 1;
  	}
817e9f2c5   WANG Cong   act_ife: acquire ...
343
  	read_unlock(&ife_mod_lock);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
  
  	if (installed)
  		return 0;
  	else
  		return -EINVAL;
  }
  
  static int dump_metalist(struct sk_buff *skb, struct tcf_ife_info *ife)
  {
  	struct tcf_meta_info *e;
  	struct nlattr *nest;
  	unsigned char *b = skb_tail_pointer(skb);
  	int total_encoded = 0;
  
  	/*can only happen on decode */
  	if (list_empty(&ife->metalist))
  		return 0;
  
  	nest = nla_nest_start(skb, TCA_IFE_METALST);
  	if (!nest)
  		goto out_nlmsg_trim;
  
  	list_for_each_entry(e, &ife->metalist, metalist) {
  		if (!e->ops->get(skb, e))
  			total_encoded += 1;
  	}
  
  	if (!total_encoded)
  		goto out_nlmsg_trim;
  
  	nla_nest_end(skb, nest);
  
  	return 0;
  
  out_nlmsg_trim:
  	nlmsg_trim(skb, b);
  	return -1;
  }
  
  /* under ife->tcf_lock */
  static void _tcf_ife_cleanup(struct tc_action *a, int bind)
  {
a85a970af   WANG Cong   net_sched: move t...
386
  	struct tcf_ife_info *ife = to_ife(a);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
  	struct tcf_meta_info *e, *n;
  
  	list_for_each_entry_safe(e, n, &ife->metalist, metalist) {
  		module_put(e->ops->owner);
  		list_del(&e->metalist);
  		if (e->metaval) {
  			if (e->ops->release)
  				e->ops->release(e);
  			else
  				kfree(e->metaval);
  		}
  		kfree(e);
  	}
  }
  
  static void tcf_ife_cleanup(struct tc_action *a, int bind)
  {
a85a970af   WANG Cong   net_sched: move t...
404
  	struct tcf_ife_info *ife = to_ife(a);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
405
406
407
408
409
  
  	spin_lock_bh(&ife->tcf_lock);
  	_tcf_ife_cleanup(a, bind);
  	spin_unlock_bh(&ife->tcf_lock);
  }
067a7cd06   WANG Cong   act_ife: only acq...
410
411
412
  /* under ife->tcf_lock for existing action */
  static int populate_metalist(struct tcf_ife_info *ife, struct nlattr **tb,
  			     bool exists)
ef6980b6b   Jamal Hadi Salim   introduce IFE action
413
414
415
416
417
418
419
420
421
422
  {
  	int len = 0;
  	int rc = 0;
  	int i = 0;
  	void *val;
  
  	for (i = 1; i < max_metacnt; i++) {
  		if (tb[i]) {
  			val = nla_data(tb[i]);
  			len = nla_len(tb[i]);
067a7cd06   WANG Cong   act_ife: only acq...
423
  			rc = load_metaops_and_vet(ife, i, val, len, exists);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
424
425
  			if (rc != 0)
  				return rc;
067a7cd06   WANG Cong   act_ife: only acq...
426
  			rc = add_metainfo(ife, i, val, len, exists);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
427
428
429
430
431
432
433
434
435
  			if (rc)
  				return rc;
  		}
  	}
  
  	return rc;
  }
  
  static int tcf_ife_init(struct net *net, struct nlattr *nla,
a85a970af   WANG Cong   net_sched: move t...
436
  			struct nlattr *est, struct tc_action **a,
ef6980b6b   Jamal Hadi Salim   introduce IFE action
437
438
439
440
441
442
443
444
445
446
  			int ovr, int bind)
  {
  	struct tc_action_net *tn = net_generic(net, ife_net_id);
  	struct nlattr *tb[TCA_IFE_MAX + 1];
  	struct nlattr *tb2[IFE_META_MAX + 1];
  	struct tcf_ife_info *ife;
  	struct tc_ife *parm;
  	u16 ife_type = 0;
  	u8 *daddr = NULL;
  	u8 *saddr = NULL;
b2313077e   WANG Cong   net_sched: make t...
447
448
  	bool exists = false;
  	int ret = 0;
ef6980b6b   Jamal Hadi Salim   introduce IFE action
449
450
451
452
453
454
455
456
457
458
  	int err;
  
  	err = nla_parse_nested(tb, TCA_IFE_MAX, nla, ife_policy);
  	if (err < 0)
  		return err;
  
  	if (!tb[TCA_IFE_PARMS])
  		return -EINVAL;
  
  	parm = nla_data(tb[TCA_IFE_PARMS]);
4e8c86155   Jamal Hadi Salim   net sched: ife ac...
459
460
461
  	exists = tcf_hash_check(tn, parm->index, a, bind);
  	if (exists && bind)
  		return 0;
ef6980b6b   Jamal Hadi Salim   introduce IFE action
462
463
464
465
466
  	if (parm->flags & IFE_ENCODE) {
  		/* Until we get issued the ethertype, we cant have
  		 * a default..
  		**/
  		if (!tb[TCA_IFE_TYPE]) {
4e8c86155   Jamal Hadi Salim   net sched: ife ac...
467
  			if (exists)
a85a970af   WANG Cong   net_sched: move t...
468
  				tcf_hash_release(*a, bind);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
469
470
471
472
473
  			pr_info("You MUST pass etherype for encoding
  ");
  			return -EINVAL;
  		}
  	}
4e8c86155   Jamal Hadi Salim   net sched: ife ac...
474
  	if (!exists) {
a85a970af   WANG Cong   net_sched: move t...
475
  		ret = tcf_hash_create(tn, parm->index, est, a, &act_ife_ops,
ef6980b6b   Jamal Hadi Salim   introduce IFE action
476
477
478
479
480
  				      bind, false);
  		if (ret)
  			return ret;
  		ret = ACT_P_CREATED;
  	} else {
a85a970af   WANG Cong   net_sched: move t...
481
  		tcf_hash_release(*a, bind);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
482
483
484
  		if (!ovr)
  			return -EEXIST;
  	}
a85a970af   WANG Cong   net_sched: move t...
485
  	ife = to_ife(*a);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
486
487
488
489
490
491
492
493
494
  	ife->flags = parm->flags;
  
  	if (parm->flags & IFE_ENCODE) {
  		ife_type = nla_get_u16(tb[TCA_IFE_TYPE]);
  		if (tb[TCA_IFE_DMAC])
  			daddr = nla_data(tb[TCA_IFE_DMAC]);
  		if (tb[TCA_IFE_SMAC])
  			saddr = nla_data(tb[TCA_IFE_SMAC]);
  	}
067a7cd06   WANG Cong   act_ife: only acq...
495
496
  	if (exists)
  		spin_lock_bh(&ife->tcf_lock);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
  	ife->tcf_action = parm->action;
  
  	if (parm->flags & IFE_ENCODE) {
  		if (daddr)
  			ether_addr_copy(ife->eth_dst, daddr);
  		else
  			eth_zero_addr(ife->eth_dst);
  
  		if (saddr)
  			ether_addr_copy(ife->eth_src, saddr);
  		else
  			eth_zero_addr(ife->eth_src);
  
  		ife->eth_type = ife_type;
  	}
  
  	if (ret == ACT_P_CREATED)
  		INIT_LIST_HEAD(&ife->metalist);
  
  	if (tb[TCA_IFE_METALST]) {
  		err = nla_parse_nested(tb2, IFE_META_MAX, tb[TCA_IFE_METALST],
  				       NULL);
  		if (err) {
  metadata_parse_err:
4e8c86155   Jamal Hadi Salim   net sched: ife ac...
521
  			if (exists)
a85a970af   WANG Cong   net_sched: move t...
522
  				tcf_hash_release(*a, bind);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
523
  			if (ret == ACT_P_CREATED)
a85a970af   WANG Cong   net_sched: move t...
524
  				_tcf_ife_cleanup(*a, bind);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
525

067a7cd06   WANG Cong   act_ife: only acq...
526
527
  			if (exists)
  				spin_unlock_bh(&ife->tcf_lock);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
528
529
  			return err;
  		}
067a7cd06   WANG Cong   act_ife: only acq...
530
  		err = populate_metalist(ife, tb2, exists);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
531
532
533
534
535
536
537
538
539
540
541
542
  		if (err)
  			goto metadata_parse_err;
  
  	} else {
  		/* if no passed metadata allow list or passed allow-all
  		 * then here we process by adding as many supported metadatum
  		 * as we can. You better have at least one else we are
  		 * going to bail out
  		 */
  		err = use_all_metadata(ife);
  		if (err) {
  			if (ret == ACT_P_CREATED)
a85a970af   WANG Cong   net_sched: move t...
543
  				_tcf_ife_cleanup(*a, bind);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
544

067a7cd06   WANG Cong   act_ife: only acq...
545
546
  			if (exists)
  				spin_unlock_bh(&ife->tcf_lock);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
547
548
549
  			return err;
  		}
  	}
067a7cd06   WANG Cong   act_ife: only acq...
550
551
  	if (exists)
  		spin_unlock_bh(&ife->tcf_lock);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
552
553
  
  	if (ret == ACT_P_CREATED)
a85a970af   WANG Cong   net_sched: move t...
554
  		tcf_hash_insert(tn, *a);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
555
556
557
558
559
560
561
562
  
  	return ret;
  }
  
  static int tcf_ife_dump(struct sk_buff *skb, struct tc_action *a, int bind,
  			int ref)
  {
  	unsigned char *b = skb_tail_pointer(skb);
a85a970af   WANG Cong   net_sched: move t...
563
  	struct tcf_ife_info *ife = to_ife(a);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
564
565
566
567
568
569
570
571
572
573
574
  	struct tc_ife opt = {
  		.index = ife->tcf_index,
  		.refcnt = ife->tcf_refcnt - ref,
  		.bindcnt = ife->tcf_bindcnt - bind,
  		.action = ife->tcf_action,
  		.flags = ife->flags,
  	};
  	struct tcf_t t;
  
  	if (nla_put(skb, TCA_IFE_PARMS, sizeof(opt), &opt))
  		goto nla_put_failure;
48d8ee169   Jamal Hadi Salim   net sched actions...
575
  	tcf_tm_dump(&t, &ife->tcf_tm);
9854518ea   Nicolas Dichtel   sched: align nlat...
576
  	if (nla_put_64bit(skb, TCA_IFE_TM, sizeof(t), &t, TCA_IFE_PAD))
ef6980b6b   Jamal Hadi Salim   introduce IFE action
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
  		goto nla_put_failure;
  
  	if (!is_zero_ether_addr(ife->eth_dst)) {
  		if (nla_put(skb, TCA_IFE_DMAC, ETH_ALEN, ife->eth_dst))
  			goto nla_put_failure;
  	}
  
  	if (!is_zero_ether_addr(ife->eth_src)) {
  		if (nla_put(skb, TCA_IFE_SMAC, ETH_ALEN, ife->eth_src))
  			goto nla_put_failure;
  	}
  
  	if (nla_put(skb, TCA_IFE_TYPE, 2, &ife->eth_type))
  		goto nla_put_failure;
  
  	if (dump_metalist(skb, ife)) {
  		/*ignore failure to dump metalist */
  		pr_info("Failed to dump metalist
  ");
  	}
  
  	return skb->len;
  
  nla_put_failure:
  	nlmsg_trim(skb, b);
  	return -1;
  }
  
  int find_decode_metaid(struct sk_buff *skb, struct tcf_ife_info *ife,
  		       u16 metaid, u16 mlen, void *mdata)
  {
  	struct tcf_meta_info *e;
  
  	/* XXX: use hash to speed up */
  	list_for_each_entry(e, &ife->metalist, metalist) {
  		if (metaid == e->metaid) {
  			if (e->ops) {
  				/* We check for decode presence already */
  				return e->ops->decode(skb, mdata, mlen);
  			}
  		}
  	}
  
  	return 0;
  }
  
  struct ifeheadr {
  	__be16 metalen;
  	u8 tlv_data[];
  };
  
  struct meta_tlvhdr {
  	__be16 type;
  	__be16 len;
  };
  
  static int tcf_ife_decode(struct sk_buff *skb, const struct tc_action *a,
  			  struct tcf_result *res)
  {
a85a970af   WANG Cong   net_sched: move t...
636
  	struct tcf_ife_info *ife = to_ife(a);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
637
638
  	int action = ife->tcf_action;
  	struct ifeheadr *ifehdr = (struct ifeheadr *)skb->data;
c006da0be   Yotam Gigi   act_ife: Fix fals...
639
  	int ifehdrln = (int)ifehdr->metalen;
ef6980b6b   Jamal Hadi Salim   introduce IFE action
640
641
642
643
  	struct meta_tlvhdr *tlv = (struct meta_tlvhdr *)(ifehdr->tlv_data);
  
  	spin_lock(&ife->tcf_lock);
  	bstats_update(&ife->tcf_bstats, skb);
9c4a4e488   Jamal Hadi Salim   net sched: action...
644
  	tcf_lastuse_update(&ife->tcf_tm);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
  	spin_unlock(&ife->tcf_lock);
  
  	ifehdrln = ntohs(ifehdrln);
  	if (unlikely(!pskb_may_pull(skb, ifehdrln))) {
  		spin_lock(&ife->tcf_lock);
  		ife->tcf_qstats.drops++;
  		spin_unlock(&ife->tcf_lock);
  		return TC_ACT_SHOT;
  	}
  
  	skb_set_mac_header(skb, ifehdrln);
  	__skb_pull(skb, ifehdrln);
  	skb->protocol = eth_type_trans(skb, skb->dev);
  	ifehdrln -= IFE_METAHDRLEN;
  
  	while (ifehdrln > 0) {
  		u8 *tlvdata = (u8 *)tlv;
  		u16 mtype = tlv->type;
  		u16 mlen = tlv->len;
28a10c426   Jamal Hadi Salim   net sched: fix en...
664
  		u16 alen;
ef6980b6b   Jamal Hadi Salim   introduce IFE action
665
666
667
  
  		mtype = ntohs(mtype);
  		mlen = ntohs(mlen);
28a10c426   Jamal Hadi Salim   net sched: fix en...
668
  		alen = NLA_ALIGN(mlen);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
669

28a10c426   Jamal Hadi Salim   net sched: fix en...
670
671
  		if (find_decode_metaid(skb, ife, mtype, (mlen - NLA_HDRLEN),
  				       (void *)(tlvdata + NLA_HDRLEN))) {
ef6980b6b   Jamal Hadi Salim   introduce IFE action
672
673
674
675
676
677
678
679
  			/* abuse overlimits to count when we receive metadata
  			 * but dont have an ops for it
  			 */
  			pr_info_ratelimited("Unknown metaid %d alnlen %d
  ",
  					    mtype, mlen);
  			ife->tcf_qstats.overlimits++;
  		}
28a10c426   Jamal Hadi Salim   net sched: fix en...
680
681
  		tlvdata += alen;
  		ifehdrln -= alen;
ef6980b6b   Jamal Hadi Salim   introduce IFE action
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
  		tlv = (struct meta_tlvhdr *)tlvdata;
  	}
  
  	skb_reset_network_header(skb);
  	return action;
  }
  
  /*XXX: check if we can do this at install time instead of current
   * send data path
  **/
  static int ife_get_sz(struct sk_buff *skb, struct tcf_ife_info *ife)
  {
  	struct tcf_meta_info *e, *n;
  	int tot_run_sz = 0, run_sz = 0;
  
  	list_for_each_entry_safe(e, n, &ife->metalist, metalist) {
  		if (e->ops->check_presence) {
  			run_sz = e->ops->check_presence(skb, e);
  			tot_run_sz += run_sz;
  		}
  	}
  
  	return tot_run_sz;
  }
  
  static int tcf_ife_encode(struct sk_buff *skb, const struct tc_action *a,
  			  struct tcf_result *res)
  {
a85a970af   WANG Cong   net_sched: move t...
710
  	struct tcf_ife_info *ife = to_ife(a);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
  	int action = ife->tcf_action;
  	struct ethhdr *oethh;	/* outer ether header */
  	struct ethhdr *iethh;	/* inner eth header */
  	struct tcf_meta_info *e;
  	/*
  	   OUTERHDR:TOTMETALEN:{TLVHDR:Metadatum:TLVHDR..}:ORIGDATA
  	   where ORIGDATA = original ethernet header ...
  	 */
  	u16 metalen = ife_get_sz(skb, ife);
  	int hdrm = metalen + skb->dev->hard_header_len + IFE_METAHDRLEN;
  	unsigned int skboff = skb->dev->hard_header_len;
  	u32 at = G_TC_AT(skb->tc_verd);
  	int new_len = skb->len + hdrm;
  	bool exceed_mtu = false;
  	int err;
  
  	if (at & AT_EGRESS) {
  		if (new_len > skb->dev->mtu)
  			exceed_mtu = true;
  	}
  
  	spin_lock(&ife->tcf_lock);
  	bstats_update(&ife->tcf_bstats, skb);
9c4a4e488   Jamal Hadi Salim   net sched: action...
734
  	tcf_lastuse_update(&ife->tcf_tm);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
  
  	if (!metalen) {		/* no metadata to send */
  		/* abuse overlimits to count when we allow packet
  		 * with no metadata
  		 */
  		ife->tcf_qstats.overlimits++;
  		spin_unlock(&ife->tcf_lock);
  		return action;
  	}
  	/* could be stupid policy setup or mtu config
  	 * so lets be conservative.. */
  	if ((action == TC_ACT_SHOT) || exceed_mtu) {
  		ife->tcf_qstats.drops++;
  		spin_unlock(&ife->tcf_lock);
  		return TC_ACT_SHOT;
  	}
ef6980b6b   Jamal Hadi Salim   introduce IFE action
751
752
753
754
755
756
757
758
759
  	err = skb_cow_head(skb, hdrm);
  	if (unlikely(err)) {
  		ife->tcf_qstats.drops++;
  		spin_unlock(&ife->tcf_lock);
  		return TC_ACT_SHOT;
  	}
  
  	if (!(at & AT_EGRESS))
  		skb_push(skb, skb->dev->hard_header_len);
4b1d488a2   Yotam Gigi   act_ife: Fix exte...
760
  	iethh = (struct ethhdr *)skb->data;
ef6980b6b   Jamal Hadi Salim   introduce IFE action
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
  	__skb_push(skb, hdrm);
  	memcpy(skb->data, iethh, skb->mac_len);
  	skb_reset_mac_header(skb);
  	oethh = eth_hdr(skb);
  
  	/*total metadata length */
  	metalen += IFE_METAHDRLEN;
  	metalen = htons(metalen);
  	memcpy((skb->data + skboff), &metalen, IFE_METAHDRLEN);
  	skboff += IFE_METAHDRLEN;
  
  	/* XXX: we dont have a clever way of telling encode to
  	 * not repeat some of the computations that are done by
  	 * ops->presence_check...
  	 */
  	list_for_each_entry(e, &ife->metalist, metalist) {
  		if (e->ops->encode) {
  			err = e->ops->encode(skb, (void *)(skb->data + skboff),
  					     e);
  		}
  		if (err < 0) {
  			/* too corrupt to keep around if overwritten */
  			ife->tcf_qstats.drops++;
  			spin_unlock(&ife->tcf_lock);
  			return TC_ACT_SHOT;
  		}
  		skboff += err;
  	}
  
  	if (!is_zero_ether_addr(ife->eth_src))
  		ether_addr_copy(oethh->h_source, ife->eth_src);
  	else
  		ether_addr_copy(oethh->h_source, iethh->h_source);
  	if (!is_zero_ether_addr(ife->eth_dst))
  		ether_addr_copy(oethh->h_dest, ife->eth_dst);
  	else
  		ether_addr_copy(oethh->h_dest, iethh->h_dest);
  	oethh->h_proto = htons(ife->eth_type);
  
  	if (!(at & AT_EGRESS))
  		skb_pull(skb, skb->dev->hard_header_len);
  
  	spin_unlock(&ife->tcf_lock);
  
  	return action;
  }
  
  static int tcf_ife_act(struct sk_buff *skb, const struct tc_action *a,
  		       struct tcf_result *res)
  {
a85a970af   WANG Cong   net_sched: move t...
811
  	struct tcf_ife_info *ife = to_ife(a);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
812
813
814
815
816
817
818
819
820
821
822
  
  	if (ife->flags & IFE_ENCODE)
  		return tcf_ife_encode(skb, a, res);
  
  	if (!(ife->flags & IFE_ENCODE))
  		return tcf_ife_decode(skb, a, res);
  
  	pr_info_ratelimited("unknown failure(policy neither de/encode
  ");
  	spin_lock(&ife->tcf_lock);
  	bstats_update(&ife->tcf_bstats, skb);
9c4a4e488   Jamal Hadi Salim   net sched: action...
823
  	tcf_lastuse_update(&ife->tcf_tm);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
824
825
826
827
828
829
830
831
  	ife->tcf_qstats.drops++;
  	spin_unlock(&ife->tcf_lock);
  
  	return TC_ACT_SHOT;
  }
  
  static int tcf_ife_walker(struct net *net, struct sk_buff *skb,
  			  struct netlink_callback *cb, int type,
a85a970af   WANG Cong   net_sched: move t...
832
  			  const struct tc_action_ops *ops)
ef6980b6b   Jamal Hadi Salim   introduce IFE action
833
834
  {
  	struct tc_action_net *tn = net_generic(net, ife_net_id);
a85a970af   WANG Cong   net_sched: move t...
835
  	return tcf_generic_walker(tn, skb, cb, type, ops);
ef6980b6b   Jamal Hadi Salim   introduce IFE action
836
  }
a85a970af   WANG Cong   net_sched: move t...
837
  static int tcf_ife_search(struct net *net, struct tc_action **a, u32 index)
ef6980b6b   Jamal Hadi Salim   introduce IFE action
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
  {
  	struct tc_action_net *tn = net_generic(net, ife_net_id);
  
  	return tcf_hash_search(tn, a, index);
  }
  
  static struct tc_action_ops act_ife_ops = {
  	.kind = "ife",
  	.type = TCA_ACT_IFE,
  	.owner = THIS_MODULE,
  	.act = tcf_ife_act,
  	.dump = tcf_ife_dump,
  	.cleanup = tcf_ife_cleanup,
  	.init = tcf_ife_init,
  	.walk = tcf_ife_walker,
  	.lookup = tcf_ife_search,
a85a970af   WANG Cong   net_sched: move t...
854
  	.size =	sizeof(struct tcf_ife_info),
ef6980b6b   Jamal Hadi Salim   introduce IFE action
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
  };
  
  static __net_init int ife_init_net(struct net *net)
  {
  	struct tc_action_net *tn = net_generic(net, ife_net_id);
  
  	return tc_action_net_init(tn, &act_ife_ops, IFE_TAB_MASK);
  }
  
  static void __net_exit ife_exit_net(struct net *net)
  {
  	struct tc_action_net *tn = net_generic(net, ife_net_id);
  
  	tc_action_net_exit(tn);
  }
  
  static struct pernet_operations ife_net_ops = {
  	.init = ife_init_net,
  	.exit = ife_exit_net,
  	.id   = &ife_net_id,
  	.size = sizeof(struct tc_action_net),
  };
  
  static int __init ife_init_module(void)
  {
  	return tcf_register_action(&act_ife_ops, &ife_net_ops);
  }
  
  static void __exit ife_cleanup_module(void)
  {
  	tcf_unregister_action(&act_ife_ops, &ife_net_ops);
  }
  
  module_init(ife_init_module);
  module_exit(ife_cleanup_module);
  
  MODULE_AUTHOR("Jamal Hadi Salim(2015)");
  MODULE_DESCRIPTION("Inter-FE LFB action");
  MODULE_LICENSE("GPL");