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