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