]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/sched/cls_flower.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/ide
[mirror_ubuntu-artful-kernel.git] / net / sched / cls_flower.c
1 /*
2 * net/sched/cls_flower.c Flower classifier
3 *
4 * Copyright (c) 2015 Jiri Pirko <jiri@resnulli.us>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/rhashtable.h>
16 #include <linux/workqueue.h>
17
18 #include <linux/if_ether.h>
19 #include <linux/in6.h>
20 #include <linux/ip.h>
21
22 #include <net/sch_generic.h>
23 #include <net/pkt_cls.h>
24 #include <net/ip.h>
25 #include <net/flow_dissector.h>
26
27 #include <net/dst.h>
28 #include <net/dst_metadata.h>
29
30 struct fl_flow_key {
31 int indev_ifindex;
32 struct flow_dissector_key_control control;
33 struct flow_dissector_key_control enc_control;
34 struct flow_dissector_key_basic basic;
35 struct flow_dissector_key_eth_addrs eth;
36 struct flow_dissector_key_vlan vlan;
37 union {
38 struct flow_dissector_key_ipv4_addrs ipv4;
39 struct flow_dissector_key_ipv6_addrs ipv6;
40 };
41 struct flow_dissector_key_ports tp;
42 struct flow_dissector_key_icmp icmp;
43 struct flow_dissector_key_arp arp;
44 struct flow_dissector_key_keyid enc_key_id;
45 union {
46 struct flow_dissector_key_ipv4_addrs enc_ipv4;
47 struct flow_dissector_key_ipv6_addrs enc_ipv6;
48 };
49 struct flow_dissector_key_ports enc_tp;
50 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
51
52 struct fl_flow_mask_range {
53 unsigned short int start;
54 unsigned short int end;
55 };
56
57 struct fl_flow_mask {
58 struct fl_flow_key key;
59 struct fl_flow_mask_range range;
60 struct rcu_head rcu;
61 };
62
63 struct cls_fl_head {
64 struct rhashtable ht;
65 struct fl_flow_mask mask;
66 struct flow_dissector dissector;
67 u32 hgen;
68 bool mask_assigned;
69 struct list_head filters;
70 struct rhashtable_params ht_params;
71 union {
72 struct work_struct work;
73 struct rcu_head rcu;
74 };
75 };
76
77 struct cls_fl_filter {
78 struct rhash_head ht_node;
79 struct fl_flow_key mkey;
80 struct tcf_exts exts;
81 struct tcf_result res;
82 struct fl_flow_key key;
83 struct list_head list;
84 u32 handle;
85 u32 flags;
86 struct rcu_head rcu;
87 struct tc_to_netdev tc;
88 struct net_device *hw_dev;
89 };
90
91 static unsigned short int fl_mask_range(const struct fl_flow_mask *mask)
92 {
93 return mask->range.end - mask->range.start;
94 }
95
96 static void fl_mask_update_range(struct fl_flow_mask *mask)
97 {
98 const u8 *bytes = (const u8 *) &mask->key;
99 size_t size = sizeof(mask->key);
100 size_t i, first = 0, last = size - 1;
101
102 for (i = 0; i < sizeof(mask->key); i++) {
103 if (bytes[i]) {
104 if (!first && i)
105 first = i;
106 last = i;
107 }
108 }
109 mask->range.start = rounddown(first, sizeof(long));
110 mask->range.end = roundup(last + 1, sizeof(long));
111 }
112
113 static void *fl_key_get_start(struct fl_flow_key *key,
114 const struct fl_flow_mask *mask)
115 {
116 return (u8 *) key + mask->range.start;
117 }
118
119 static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key,
120 struct fl_flow_mask *mask)
121 {
122 const long *lkey = fl_key_get_start(key, mask);
123 const long *lmask = fl_key_get_start(&mask->key, mask);
124 long *lmkey = fl_key_get_start(mkey, mask);
125 int i;
126
127 for (i = 0; i < fl_mask_range(mask); i += sizeof(long))
128 *lmkey++ = *lkey++ & *lmask++;
129 }
130
131 static void fl_clear_masked_range(struct fl_flow_key *key,
132 struct fl_flow_mask *mask)
133 {
134 memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask));
135 }
136
137 static struct cls_fl_filter *fl_lookup(struct cls_fl_head *head,
138 struct fl_flow_key *mkey)
139 {
140 return rhashtable_lookup_fast(&head->ht,
141 fl_key_get_start(mkey, &head->mask),
142 head->ht_params);
143 }
144
145 static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp,
146 struct tcf_result *res)
147 {
148 struct cls_fl_head *head = rcu_dereference_bh(tp->root);
149 struct cls_fl_filter *f;
150 struct fl_flow_key skb_key;
151 struct fl_flow_key skb_mkey;
152 struct ip_tunnel_info *info;
153
154 if (!atomic_read(&head->ht.nelems))
155 return -1;
156
157 fl_clear_masked_range(&skb_key, &head->mask);
158
159 info = skb_tunnel_info(skb);
160 if (info) {
161 struct ip_tunnel_key *key = &info->key;
162
163 switch (ip_tunnel_info_af(info)) {
164 case AF_INET:
165 skb_key.enc_control.addr_type =
166 FLOW_DISSECTOR_KEY_IPV4_ADDRS;
167 skb_key.enc_ipv4.src = key->u.ipv4.src;
168 skb_key.enc_ipv4.dst = key->u.ipv4.dst;
169 break;
170 case AF_INET6:
171 skb_key.enc_control.addr_type =
172 FLOW_DISSECTOR_KEY_IPV6_ADDRS;
173 skb_key.enc_ipv6.src = key->u.ipv6.src;
174 skb_key.enc_ipv6.dst = key->u.ipv6.dst;
175 break;
176 }
177
178 skb_key.enc_key_id.keyid = tunnel_id_to_key32(key->tun_id);
179 skb_key.enc_tp.src = key->tp_src;
180 skb_key.enc_tp.dst = key->tp_dst;
181 }
182
183 skb_key.indev_ifindex = skb->skb_iif;
184 /* skb_flow_dissect() does not set n_proto in case an unknown protocol,
185 * so do it rather here.
186 */
187 skb_key.basic.n_proto = skb->protocol;
188 skb_flow_dissect(skb, &head->dissector, &skb_key, 0);
189
190 fl_set_masked_key(&skb_mkey, &skb_key, &head->mask);
191
192 f = fl_lookup(head, &skb_mkey);
193 if (f && !tc_skip_sw(f->flags)) {
194 *res = f->res;
195 return tcf_exts_exec(skb, &f->exts, res);
196 }
197 return -1;
198 }
199
200 static int fl_init(struct tcf_proto *tp)
201 {
202 struct cls_fl_head *head;
203
204 head = kzalloc(sizeof(*head), GFP_KERNEL);
205 if (!head)
206 return -ENOBUFS;
207
208 INIT_LIST_HEAD_RCU(&head->filters);
209 rcu_assign_pointer(tp->root, head);
210
211 return 0;
212 }
213
214 static void fl_destroy_filter(struct rcu_head *head)
215 {
216 struct cls_fl_filter *f = container_of(head, struct cls_fl_filter, rcu);
217
218 tcf_exts_destroy(&f->exts);
219 kfree(f);
220 }
221
222 static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f)
223 {
224 struct tc_cls_flower_offload offload = {0};
225 struct net_device *dev = f->hw_dev;
226 struct tc_to_netdev *tc = &f->tc;
227
228 if (!tc_can_offload(dev, tp))
229 return;
230
231 offload.command = TC_CLSFLOWER_DESTROY;
232 offload.prio = tp->prio;
233 offload.cookie = (unsigned long)f;
234
235 tc->type = TC_SETUP_CLSFLOWER;
236 tc->cls_flower = &offload;
237
238 dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->protocol, tc);
239 }
240
241 static int fl_hw_replace_filter(struct tcf_proto *tp,
242 struct flow_dissector *dissector,
243 struct fl_flow_key *mask,
244 struct cls_fl_filter *f)
245 {
246 struct net_device *dev = tp->q->dev_queue->dev;
247 struct tc_cls_flower_offload offload = {0};
248 struct tc_to_netdev *tc = &f->tc;
249 int err;
250
251 if (!tc_can_offload(dev, tp)) {
252 if (tcf_exts_get_dev(dev, &f->exts, &f->hw_dev) ||
253 (f->hw_dev && !tc_can_offload(f->hw_dev, tp))) {
254 f->hw_dev = dev;
255 return tc_skip_sw(f->flags) ? -EINVAL : 0;
256 }
257 dev = f->hw_dev;
258 tc->egress_dev = true;
259 } else {
260 f->hw_dev = dev;
261 }
262
263 offload.command = TC_CLSFLOWER_REPLACE;
264 offload.prio = tp->prio;
265 offload.cookie = (unsigned long)f;
266 offload.dissector = dissector;
267 offload.mask = mask;
268 offload.key = &f->mkey;
269 offload.exts = &f->exts;
270
271 tc->type = TC_SETUP_CLSFLOWER;
272 tc->cls_flower = &offload;
273
274 err = dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->protocol,
275 tc);
276 if (!err)
277 f->flags |= TCA_CLS_FLAGS_IN_HW;
278
279 if (tc_skip_sw(f->flags))
280 return err;
281 return 0;
282 }
283
284 static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f)
285 {
286 struct tc_cls_flower_offload offload = {0};
287 struct net_device *dev = f->hw_dev;
288 struct tc_to_netdev *tc = &f->tc;
289
290 if (!tc_can_offload(dev, tp))
291 return;
292
293 offload.command = TC_CLSFLOWER_STATS;
294 offload.prio = tp->prio;
295 offload.cookie = (unsigned long)f;
296 offload.exts = &f->exts;
297
298 tc->type = TC_SETUP_CLSFLOWER;
299 tc->cls_flower = &offload;
300
301 dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->protocol, tc);
302 }
303
304 static void __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f)
305 {
306 list_del_rcu(&f->list);
307 if (!tc_skip_hw(f->flags))
308 fl_hw_destroy_filter(tp, f);
309 tcf_unbind_filter(tp, &f->res);
310 call_rcu(&f->rcu, fl_destroy_filter);
311 }
312
313 static void fl_destroy_sleepable(struct work_struct *work)
314 {
315 struct cls_fl_head *head = container_of(work, struct cls_fl_head,
316 work);
317 if (head->mask_assigned)
318 rhashtable_destroy(&head->ht);
319 kfree(head);
320 module_put(THIS_MODULE);
321 }
322
323 static void fl_destroy_rcu(struct rcu_head *rcu)
324 {
325 struct cls_fl_head *head = container_of(rcu, struct cls_fl_head, rcu);
326
327 INIT_WORK(&head->work, fl_destroy_sleepable);
328 schedule_work(&head->work);
329 }
330
331 static bool fl_destroy(struct tcf_proto *tp, bool force)
332 {
333 struct cls_fl_head *head = rtnl_dereference(tp->root);
334 struct cls_fl_filter *f, *next;
335
336 if (!force && !list_empty(&head->filters))
337 return false;
338
339 list_for_each_entry_safe(f, next, &head->filters, list)
340 __fl_delete(tp, f);
341
342 __module_get(THIS_MODULE);
343 call_rcu(&head->rcu, fl_destroy_rcu);
344
345 return true;
346 }
347
348 static unsigned long fl_get(struct tcf_proto *tp, u32 handle)
349 {
350 struct cls_fl_head *head = rtnl_dereference(tp->root);
351 struct cls_fl_filter *f;
352
353 list_for_each_entry(f, &head->filters, list)
354 if (f->handle == handle)
355 return (unsigned long) f;
356 return 0;
357 }
358
359 static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
360 [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC },
361 [TCA_FLOWER_CLASSID] = { .type = NLA_U32 },
362 [TCA_FLOWER_INDEV] = { .type = NLA_STRING,
363 .len = IFNAMSIZ },
364 [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN },
365 [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN },
366 [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN },
367 [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN },
368 [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 },
369 [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 },
370 [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 },
371 [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 },
372 [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 },
373 [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 },
374 [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
375 [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
376 [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) },
377 [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
378 [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 },
379 [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 },
380 [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 },
381 [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 },
382 [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 },
383 [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 },
384 [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 },
385 [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
386 [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
387 [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
388 [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
389 [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
390 [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
391 [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
392 [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
393 [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
394 [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 },
395 [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 },
396 [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 },
397 [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 },
398 [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 },
399 [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 },
400 [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 },
401 [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 },
402 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 },
403 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 },
404 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 },
405 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 },
406 [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 },
407 [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 },
408 [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 },
409 [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 },
410 [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 },
411 [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 },
412 [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 },
413 [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 },
414 [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 },
415 [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 },
416 [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 },
417 [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 },
418 [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 },
419 [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 },
420 [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 },
421 [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 },
422 [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN },
423 [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN },
424 [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN },
425 [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN },
426 };
427
428 static void fl_set_key_val(struct nlattr **tb,
429 void *val, int val_type,
430 void *mask, int mask_type, int len)
431 {
432 if (!tb[val_type])
433 return;
434 memcpy(val, nla_data(tb[val_type]), len);
435 if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
436 memset(mask, 0xff, len);
437 else
438 memcpy(mask, nla_data(tb[mask_type]), len);
439 }
440
441 static void fl_set_key_vlan(struct nlattr **tb,
442 struct flow_dissector_key_vlan *key_val,
443 struct flow_dissector_key_vlan *key_mask)
444 {
445 #define VLAN_PRIORITY_MASK 0x7
446
447 if (tb[TCA_FLOWER_KEY_VLAN_ID]) {
448 key_val->vlan_id =
449 nla_get_u16(tb[TCA_FLOWER_KEY_VLAN_ID]) & VLAN_VID_MASK;
450 key_mask->vlan_id = VLAN_VID_MASK;
451 }
452 if (tb[TCA_FLOWER_KEY_VLAN_PRIO]) {
453 key_val->vlan_priority =
454 nla_get_u8(tb[TCA_FLOWER_KEY_VLAN_PRIO]) &
455 VLAN_PRIORITY_MASK;
456 key_mask->vlan_priority = VLAN_PRIORITY_MASK;
457 }
458 }
459
460 static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
461 u32 *dissector_key, u32 *dissector_mask,
462 u32 flower_flag_bit, u32 dissector_flag_bit)
463 {
464 if (flower_mask & flower_flag_bit) {
465 *dissector_mask |= dissector_flag_bit;
466 if (flower_key & flower_flag_bit)
467 *dissector_key |= dissector_flag_bit;
468 }
469 }
470
471 static int fl_set_key_flags(struct nlattr **tb,
472 u32 *flags_key, u32 *flags_mask)
473 {
474 u32 key, mask;
475
476 /* mask is mandatory for flags */
477 if (!tb[TCA_FLOWER_KEY_FLAGS_MASK])
478 return -EINVAL;
479
480 key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS]));
481 mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
482
483 *flags_key = 0;
484 *flags_mask = 0;
485
486 fl_set_key_flag(key, mask, flags_key, flags_mask,
487 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
488
489 return 0;
490 }
491
492 static int fl_set_key(struct net *net, struct nlattr **tb,
493 struct fl_flow_key *key, struct fl_flow_key *mask)
494 {
495 __be16 ethertype;
496 int ret = 0;
497 #ifdef CONFIG_NET_CLS_IND
498 if (tb[TCA_FLOWER_INDEV]) {
499 int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV]);
500 if (err < 0)
501 return err;
502 key->indev_ifindex = err;
503 mask->indev_ifindex = 0xffffffff;
504 }
505 #endif
506
507 fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
508 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
509 sizeof(key->eth.dst));
510 fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
511 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
512 sizeof(key->eth.src));
513
514 if (tb[TCA_FLOWER_KEY_ETH_TYPE]) {
515 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]);
516
517 if (ethertype == htons(ETH_P_8021Q)) {
518 fl_set_key_vlan(tb, &key->vlan, &mask->vlan);
519 fl_set_key_val(tb, &key->basic.n_proto,
520 TCA_FLOWER_KEY_VLAN_ETH_TYPE,
521 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
522 sizeof(key->basic.n_proto));
523 } else {
524 key->basic.n_proto = ethertype;
525 mask->basic.n_proto = cpu_to_be16(~0);
526 }
527 }
528
529 if (key->basic.n_proto == htons(ETH_P_IP) ||
530 key->basic.n_proto == htons(ETH_P_IPV6)) {
531 fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
532 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
533 sizeof(key->basic.ip_proto));
534 }
535
536 if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
537 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
538 mask->control.addr_type = ~0;
539 fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
540 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
541 sizeof(key->ipv4.src));
542 fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
543 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
544 sizeof(key->ipv4.dst));
545 } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
546 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
547 mask->control.addr_type = ~0;
548 fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
549 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
550 sizeof(key->ipv6.src));
551 fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
552 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
553 sizeof(key->ipv6.dst));
554 }
555
556 if (key->basic.ip_proto == IPPROTO_TCP) {
557 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
558 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
559 sizeof(key->tp.src));
560 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
561 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
562 sizeof(key->tp.dst));
563 } else if (key->basic.ip_proto == IPPROTO_UDP) {
564 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
565 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
566 sizeof(key->tp.src));
567 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
568 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
569 sizeof(key->tp.dst));
570 } else if (key->basic.ip_proto == IPPROTO_SCTP) {
571 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
572 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
573 sizeof(key->tp.src));
574 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
575 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
576 sizeof(key->tp.dst));
577 } else if (key->basic.n_proto == htons(ETH_P_IP) &&
578 key->basic.ip_proto == IPPROTO_ICMP) {
579 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
580 &mask->icmp.type,
581 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
582 sizeof(key->icmp.type));
583 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
584 &mask->icmp.code,
585 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
586 sizeof(key->icmp.code));
587 } else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
588 key->basic.ip_proto == IPPROTO_ICMPV6) {
589 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
590 &mask->icmp.type,
591 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
592 sizeof(key->icmp.type));
593 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
594 &mask->icmp.code,
595 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
596 sizeof(key->icmp.code));
597 } else if (key->basic.n_proto == htons(ETH_P_ARP) ||
598 key->basic.n_proto == htons(ETH_P_RARP)) {
599 fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
600 &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
601 sizeof(key->arp.sip));
602 fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
603 &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
604 sizeof(key->arp.tip));
605 fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
606 &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
607 sizeof(key->arp.op));
608 fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
609 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
610 sizeof(key->arp.sha));
611 fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
612 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
613 sizeof(key->arp.tha));
614 }
615
616 if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
617 tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
618 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
619 mask->enc_control.addr_type = ~0;
620 fl_set_key_val(tb, &key->enc_ipv4.src,
621 TCA_FLOWER_KEY_ENC_IPV4_SRC,
622 &mask->enc_ipv4.src,
623 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
624 sizeof(key->enc_ipv4.src));
625 fl_set_key_val(tb, &key->enc_ipv4.dst,
626 TCA_FLOWER_KEY_ENC_IPV4_DST,
627 &mask->enc_ipv4.dst,
628 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
629 sizeof(key->enc_ipv4.dst));
630 }
631
632 if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
633 tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
634 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
635 mask->enc_control.addr_type = ~0;
636 fl_set_key_val(tb, &key->enc_ipv6.src,
637 TCA_FLOWER_KEY_ENC_IPV6_SRC,
638 &mask->enc_ipv6.src,
639 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
640 sizeof(key->enc_ipv6.src));
641 fl_set_key_val(tb, &key->enc_ipv6.dst,
642 TCA_FLOWER_KEY_ENC_IPV6_DST,
643 &mask->enc_ipv6.dst,
644 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
645 sizeof(key->enc_ipv6.dst));
646 }
647
648 fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
649 &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
650 sizeof(key->enc_key_id.keyid));
651
652 fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
653 &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
654 sizeof(key->enc_tp.src));
655
656 fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
657 &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
658 sizeof(key->enc_tp.dst));
659
660 if (tb[TCA_FLOWER_KEY_FLAGS])
661 ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags);
662
663 return ret;
664 }
665
666 static bool fl_mask_eq(struct fl_flow_mask *mask1,
667 struct fl_flow_mask *mask2)
668 {
669 const long *lmask1 = fl_key_get_start(&mask1->key, mask1);
670 const long *lmask2 = fl_key_get_start(&mask2->key, mask2);
671
672 return !memcmp(&mask1->range, &mask2->range, sizeof(mask1->range)) &&
673 !memcmp(lmask1, lmask2, fl_mask_range(mask1));
674 }
675
676 static const struct rhashtable_params fl_ht_params = {
677 .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
678 .head_offset = offsetof(struct cls_fl_filter, ht_node),
679 .automatic_shrinking = true,
680 };
681
682 static int fl_init_hashtable(struct cls_fl_head *head,
683 struct fl_flow_mask *mask)
684 {
685 head->ht_params = fl_ht_params;
686 head->ht_params.key_len = fl_mask_range(mask);
687 head->ht_params.key_offset += mask->range.start;
688
689 return rhashtable_init(&head->ht, &head->ht_params);
690 }
691
692 #define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
693 #define FL_KEY_MEMBER_SIZE(member) (sizeof(((struct fl_flow_key *) 0)->member))
694
695 #define FL_KEY_IS_MASKED(mask, member) \
696 memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \
697 0, FL_KEY_MEMBER_SIZE(member)) \
698
699 #define FL_KEY_SET(keys, cnt, id, member) \
700 do { \
701 keys[cnt].key_id = id; \
702 keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \
703 cnt++; \
704 } while(0);
705
706 #define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \
707 do { \
708 if (FL_KEY_IS_MASKED(mask, member)) \
709 FL_KEY_SET(keys, cnt, id, member); \
710 } while(0);
711
712 static void fl_init_dissector(struct cls_fl_head *head,
713 struct fl_flow_mask *mask)
714 {
715 struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
716 size_t cnt = 0;
717
718 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
719 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
720 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
721 FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
722 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
723 FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
724 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
725 FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
726 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
727 FLOW_DISSECTOR_KEY_PORTS, tp);
728 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
729 FLOW_DISSECTOR_KEY_ICMP, icmp);
730 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
731 FLOW_DISSECTOR_KEY_ARP, arp);
732 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
733 FLOW_DISSECTOR_KEY_VLAN, vlan);
734 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
735 FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
736 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
737 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
738 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
739 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
740 if (FL_KEY_IS_MASKED(&mask->key, enc_ipv4) ||
741 FL_KEY_IS_MASKED(&mask->key, enc_ipv6))
742 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
743 enc_control);
744 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
745 FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
746
747 skb_flow_dissector_init(&head->dissector, keys, cnt);
748 }
749
750 static int fl_check_assign_mask(struct cls_fl_head *head,
751 struct fl_flow_mask *mask)
752 {
753 int err;
754
755 if (head->mask_assigned) {
756 if (!fl_mask_eq(&head->mask, mask))
757 return -EINVAL;
758 else
759 return 0;
760 }
761
762 /* Mask is not assigned yet. So assign it and init hashtable
763 * according to that.
764 */
765 err = fl_init_hashtable(head, mask);
766 if (err)
767 return err;
768 memcpy(&head->mask, mask, sizeof(head->mask));
769 head->mask_assigned = true;
770
771 fl_init_dissector(head, mask);
772
773 return 0;
774 }
775
776 static int fl_set_parms(struct net *net, struct tcf_proto *tp,
777 struct cls_fl_filter *f, struct fl_flow_mask *mask,
778 unsigned long base, struct nlattr **tb,
779 struct nlattr *est, bool ovr)
780 {
781 struct tcf_exts e;
782 int err;
783
784 err = tcf_exts_init(&e, TCA_FLOWER_ACT, 0);
785 if (err < 0)
786 return err;
787 err = tcf_exts_validate(net, tp, tb, est, &e, ovr);
788 if (err < 0)
789 goto errout;
790
791 if (tb[TCA_FLOWER_CLASSID]) {
792 f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
793 tcf_bind_filter(tp, &f->res, base);
794 }
795
796 err = fl_set_key(net, tb, &f->key, &mask->key);
797 if (err)
798 goto errout;
799
800 fl_mask_update_range(mask);
801 fl_set_masked_key(&f->mkey, &f->key, mask);
802
803 tcf_exts_change(tp, &f->exts, &e);
804
805 return 0;
806 errout:
807 tcf_exts_destroy(&e);
808 return err;
809 }
810
811 static u32 fl_grab_new_handle(struct tcf_proto *tp,
812 struct cls_fl_head *head)
813 {
814 unsigned int i = 0x80000000;
815 u32 handle;
816
817 do {
818 if (++head->hgen == 0x7FFFFFFF)
819 head->hgen = 1;
820 } while (--i > 0 && fl_get(tp, head->hgen));
821
822 if (unlikely(i == 0)) {
823 pr_err("Insufficient number of handles\n");
824 handle = 0;
825 } else {
826 handle = head->hgen;
827 }
828
829 return handle;
830 }
831
832 static int fl_change(struct net *net, struct sk_buff *in_skb,
833 struct tcf_proto *tp, unsigned long base,
834 u32 handle, struct nlattr **tca,
835 unsigned long *arg, bool ovr)
836 {
837 struct cls_fl_head *head = rtnl_dereference(tp->root);
838 struct cls_fl_filter *fold = (struct cls_fl_filter *) *arg;
839 struct cls_fl_filter *fnew;
840 struct nlattr **tb;
841 struct fl_flow_mask mask = {};
842 int err;
843
844 if (!tca[TCA_OPTIONS])
845 return -EINVAL;
846
847 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
848 if (!tb)
849 return -ENOBUFS;
850
851 err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS], fl_policy);
852 if (err < 0)
853 goto errout_tb;
854
855 if (fold && handle && fold->handle != handle) {
856 err = -EINVAL;
857 goto errout_tb;
858 }
859
860 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
861 if (!fnew) {
862 err = -ENOBUFS;
863 goto errout_tb;
864 }
865
866 err = tcf_exts_init(&fnew->exts, TCA_FLOWER_ACT, 0);
867 if (err < 0)
868 goto errout;
869
870 if (!handle) {
871 handle = fl_grab_new_handle(tp, head);
872 if (!handle) {
873 err = -EINVAL;
874 goto errout;
875 }
876 }
877 fnew->handle = handle;
878
879 if (tb[TCA_FLOWER_FLAGS]) {
880 fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
881
882 if (!tc_flags_valid(fnew->flags)) {
883 err = -EINVAL;
884 goto errout;
885 }
886 }
887
888 err = fl_set_parms(net, tp, fnew, &mask, base, tb, tca[TCA_RATE], ovr);
889 if (err)
890 goto errout;
891
892 err = fl_check_assign_mask(head, &mask);
893 if (err)
894 goto errout;
895
896 if (!tc_skip_sw(fnew->flags)) {
897 if (!fold && fl_lookup(head, &fnew->mkey)) {
898 err = -EEXIST;
899 goto errout;
900 }
901
902 err = rhashtable_insert_fast(&head->ht, &fnew->ht_node,
903 head->ht_params);
904 if (err)
905 goto errout;
906 }
907
908 if (!tc_skip_hw(fnew->flags)) {
909 err = fl_hw_replace_filter(tp,
910 &head->dissector,
911 &mask.key,
912 fnew);
913 if (err)
914 goto errout;
915 }
916
917 if (!tc_in_hw(fnew->flags))
918 fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
919
920 if (fold) {
921 if (!tc_skip_sw(fold->flags))
922 rhashtable_remove_fast(&head->ht, &fold->ht_node,
923 head->ht_params);
924 if (!tc_skip_hw(fold->flags))
925 fl_hw_destroy_filter(tp, fold);
926 }
927
928 *arg = (unsigned long) fnew;
929
930 if (fold) {
931 list_replace_rcu(&fold->list, &fnew->list);
932 tcf_unbind_filter(tp, &fold->res);
933 call_rcu(&fold->rcu, fl_destroy_filter);
934 } else {
935 list_add_tail_rcu(&fnew->list, &head->filters);
936 }
937
938 kfree(tb);
939 return 0;
940
941 errout:
942 tcf_exts_destroy(&fnew->exts);
943 kfree(fnew);
944 errout_tb:
945 kfree(tb);
946 return err;
947 }
948
949 static int fl_delete(struct tcf_proto *tp, unsigned long arg)
950 {
951 struct cls_fl_head *head = rtnl_dereference(tp->root);
952 struct cls_fl_filter *f = (struct cls_fl_filter *) arg;
953
954 if (!tc_skip_sw(f->flags))
955 rhashtable_remove_fast(&head->ht, &f->ht_node,
956 head->ht_params);
957 __fl_delete(tp, f);
958 return 0;
959 }
960
961 static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg)
962 {
963 struct cls_fl_head *head = rtnl_dereference(tp->root);
964 struct cls_fl_filter *f;
965
966 list_for_each_entry_rcu(f, &head->filters, list) {
967 if (arg->count < arg->skip)
968 goto skip;
969 if (arg->fn(tp, (unsigned long) f, arg) < 0) {
970 arg->stop = 1;
971 break;
972 }
973 skip:
974 arg->count++;
975 }
976 }
977
978 static int fl_dump_key_val(struct sk_buff *skb,
979 void *val, int val_type,
980 void *mask, int mask_type, int len)
981 {
982 int err;
983
984 if (!memchr_inv(mask, 0, len))
985 return 0;
986 err = nla_put(skb, val_type, len, val);
987 if (err)
988 return err;
989 if (mask_type != TCA_FLOWER_UNSPEC) {
990 err = nla_put(skb, mask_type, len, mask);
991 if (err)
992 return err;
993 }
994 return 0;
995 }
996
997 static int fl_dump_key_vlan(struct sk_buff *skb,
998 struct flow_dissector_key_vlan *vlan_key,
999 struct flow_dissector_key_vlan *vlan_mask)
1000 {
1001 int err;
1002
1003 if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
1004 return 0;
1005 if (vlan_mask->vlan_id) {
1006 err = nla_put_u16(skb, TCA_FLOWER_KEY_VLAN_ID,
1007 vlan_key->vlan_id);
1008 if (err)
1009 return err;
1010 }
1011 if (vlan_mask->vlan_priority) {
1012 err = nla_put_u8(skb, TCA_FLOWER_KEY_VLAN_PRIO,
1013 vlan_key->vlan_priority);
1014 if (err)
1015 return err;
1016 }
1017 return 0;
1018 }
1019
1020 static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
1021 u32 *flower_key, u32 *flower_mask,
1022 u32 flower_flag_bit, u32 dissector_flag_bit)
1023 {
1024 if (dissector_mask & dissector_flag_bit) {
1025 *flower_mask |= flower_flag_bit;
1026 if (dissector_key & dissector_flag_bit)
1027 *flower_key |= flower_flag_bit;
1028 }
1029 }
1030
1031 static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask)
1032 {
1033 u32 key, mask;
1034 __be32 _key, _mask;
1035 int err;
1036
1037 if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
1038 return 0;
1039
1040 key = 0;
1041 mask = 0;
1042
1043 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
1044 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
1045
1046 _key = cpu_to_be32(key);
1047 _mask = cpu_to_be32(mask);
1048
1049 err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key);
1050 if (err)
1051 return err;
1052
1053 return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask);
1054 }
1055
1056 static int fl_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
1057 struct sk_buff *skb, struct tcmsg *t)
1058 {
1059 struct cls_fl_head *head = rtnl_dereference(tp->root);
1060 struct cls_fl_filter *f = (struct cls_fl_filter *) fh;
1061 struct nlattr *nest;
1062 struct fl_flow_key *key, *mask;
1063
1064 if (!f)
1065 return skb->len;
1066
1067 t->tcm_handle = f->handle;
1068
1069 nest = nla_nest_start(skb, TCA_OPTIONS);
1070 if (!nest)
1071 goto nla_put_failure;
1072
1073 if (f->res.classid &&
1074 nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
1075 goto nla_put_failure;
1076
1077 key = &f->key;
1078 mask = &head->mask.key;
1079
1080 if (mask->indev_ifindex) {
1081 struct net_device *dev;
1082
1083 dev = __dev_get_by_index(net, key->indev_ifindex);
1084 if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
1085 goto nla_put_failure;
1086 }
1087
1088 if (!tc_skip_hw(f->flags))
1089 fl_hw_update_stats(tp, f);
1090
1091 if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
1092 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
1093 sizeof(key->eth.dst)) ||
1094 fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
1095 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
1096 sizeof(key->eth.src)) ||
1097 fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
1098 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
1099 sizeof(key->basic.n_proto)))
1100 goto nla_put_failure;
1101
1102 if (fl_dump_key_vlan(skb, &key->vlan, &mask->vlan))
1103 goto nla_put_failure;
1104
1105 if ((key->basic.n_proto == htons(ETH_P_IP) ||
1106 key->basic.n_proto == htons(ETH_P_IPV6)) &&
1107 fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
1108 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
1109 sizeof(key->basic.ip_proto)))
1110 goto nla_put_failure;
1111
1112 if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
1113 (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1114 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1115 sizeof(key->ipv4.src)) ||
1116 fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
1117 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
1118 sizeof(key->ipv4.dst))))
1119 goto nla_put_failure;
1120 else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
1121 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
1122 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
1123 sizeof(key->ipv6.src)) ||
1124 fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
1125 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
1126 sizeof(key->ipv6.dst))))
1127 goto nla_put_failure;
1128
1129 if (key->basic.ip_proto == IPPROTO_TCP &&
1130 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
1131 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
1132 sizeof(key->tp.src)) ||
1133 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
1134 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
1135 sizeof(key->tp.dst))))
1136 goto nla_put_failure;
1137 else if (key->basic.ip_proto == IPPROTO_UDP &&
1138 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
1139 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
1140 sizeof(key->tp.src)) ||
1141 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
1142 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
1143 sizeof(key->tp.dst))))
1144 goto nla_put_failure;
1145 else if (key->basic.ip_proto == IPPROTO_SCTP &&
1146 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
1147 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
1148 sizeof(key->tp.src)) ||
1149 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
1150 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
1151 sizeof(key->tp.dst))))
1152 goto nla_put_failure;
1153 else if (key->basic.n_proto == htons(ETH_P_IP) &&
1154 key->basic.ip_proto == IPPROTO_ICMP &&
1155 (fl_dump_key_val(skb, &key->icmp.type,
1156 TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
1157 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
1158 sizeof(key->icmp.type)) ||
1159 fl_dump_key_val(skb, &key->icmp.code,
1160 TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
1161 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
1162 sizeof(key->icmp.code))))
1163 goto nla_put_failure;
1164 else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
1165 key->basic.ip_proto == IPPROTO_ICMPV6 &&
1166 (fl_dump_key_val(skb, &key->icmp.type,
1167 TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
1168 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
1169 sizeof(key->icmp.type)) ||
1170 fl_dump_key_val(skb, &key->icmp.code,
1171 TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
1172 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
1173 sizeof(key->icmp.code))))
1174 goto nla_put_failure;
1175 else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
1176 key->basic.n_proto == htons(ETH_P_RARP)) &&
1177 (fl_dump_key_val(skb, &key->arp.sip,
1178 TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
1179 TCA_FLOWER_KEY_ARP_SIP_MASK,
1180 sizeof(key->arp.sip)) ||
1181 fl_dump_key_val(skb, &key->arp.tip,
1182 TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
1183 TCA_FLOWER_KEY_ARP_TIP_MASK,
1184 sizeof(key->arp.tip)) ||
1185 fl_dump_key_val(skb, &key->arp.op,
1186 TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
1187 TCA_FLOWER_KEY_ARP_OP_MASK,
1188 sizeof(key->arp.op)) ||
1189 fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
1190 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
1191 sizeof(key->arp.sha)) ||
1192 fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
1193 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
1194 sizeof(key->arp.tha))))
1195 goto nla_put_failure;
1196
1197 if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
1198 (fl_dump_key_val(skb, &key->enc_ipv4.src,
1199 TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
1200 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
1201 sizeof(key->enc_ipv4.src)) ||
1202 fl_dump_key_val(skb, &key->enc_ipv4.dst,
1203 TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
1204 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
1205 sizeof(key->enc_ipv4.dst))))
1206 goto nla_put_failure;
1207 else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
1208 (fl_dump_key_val(skb, &key->enc_ipv6.src,
1209 TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
1210 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
1211 sizeof(key->enc_ipv6.src)) ||
1212 fl_dump_key_val(skb, &key->enc_ipv6.dst,
1213 TCA_FLOWER_KEY_ENC_IPV6_DST,
1214 &mask->enc_ipv6.dst,
1215 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
1216 sizeof(key->enc_ipv6.dst))))
1217 goto nla_put_failure;
1218
1219 if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
1220 &mask->enc_key_id, TCA_FLOWER_UNSPEC,
1221 sizeof(key->enc_key_id)) ||
1222 fl_dump_key_val(skb, &key->enc_tp.src,
1223 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
1224 &mask->enc_tp.src,
1225 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
1226 sizeof(key->enc_tp.src)) ||
1227 fl_dump_key_val(skb, &key->enc_tp.dst,
1228 TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
1229 &mask->enc_tp.dst,
1230 TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
1231 sizeof(key->enc_tp.dst)))
1232 goto nla_put_failure;
1233
1234 if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
1235 goto nla_put_failure;
1236
1237 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
1238 goto nla_put_failure;
1239
1240 if (tcf_exts_dump(skb, &f->exts))
1241 goto nla_put_failure;
1242
1243 nla_nest_end(skb, nest);
1244
1245 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
1246 goto nla_put_failure;
1247
1248 return skb->len;
1249
1250 nla_put_failure:
1251 nla_nest_cancel(skb, nest);
1252 return -1;
1253 }
1254
1255 static struct tcf_proto_ops cls_fl_ops __read_mostly = {
1256 .kind = "flower",
1257 .classify = fl_classify,
1258 .init = fl_init,
1259 .destroy = fl_destroy,
1260 .get = fl_get,
1261 .change = fl_change,
1262 .delete = fl_delete,
1263 .walk = fl_walk,
1264 .dump = fl_dump,
1265 .owner = THIS_MODULE,
1266 };
1267
1268 static int __init cls_fl_init(void)
1269 {
1270 return register_tcf_proto_ops(&cls_fl_ops);
1271 }
1272
1273 static void __exit cls_fl_exit(void)
1274 {
1275 unregister_tcf_proto_ops(&cls_fl_ops);
1276 }
1277
1278 module_init(cls_fl_init);
1279 module_exit(cls_fl_exit);
1280
1281 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
1282 MODULE_DESCRIPTION("Flower classifier");
1283 MODULE_LICENSE("GPL v2");