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