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