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