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; |
Paolo Abeni | 44a5cd4 | 2018-06-21 20:02:16 +0200 | [diff] [blame] | 69 | struct rcu_work rwork; |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 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; |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 76 | struct rcu_work rwork; |
Chris Mi | c15ab23 | 2017-08-30 02:31:58 -0400 | [diff] [blame] | 77 | struct idr handle_idr; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | struct cls_fl_filter { |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 81 | struct fl_flow_mask *mask; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 82 | struct rhash_head ht_node; |
| 83 | struct fl_flow_key mkey; |
| 84 | struct tcf_exts exts; |
| 85 | struct tcf_result res; |
| 86 | struct fl_flow_key key; |
| 87 | struct list_head list; |
| 88 | u32 handle; |
Amir Vadai | e69985c | 2016-06-05 17:11:18 +0300 | [diff] [blame] | 89 | u32 flags; |
John Hurley | 31533cb | 2018-06-25 14:30:06 -0700 | [diff] [blame] | 90 | unsigned int in_hw_count; |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 91 | struct rcu_work rwork; |
Hadar Hen Zion | 7091d8c | 2016-12-01 14:06:37 +0200 | [diff] [blame] | 92 | struct net_device *hw_dev; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 93 | }; |
| 94 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 95 | static const struct rhashtable_params mask_ht_params = { |
| 96 | .key_offset = offsetof(struct fl_flow_mask, key), |
| 97 | .key_len = sizeof(struct fl_flow_key), |
| 98 | .head_offset = offsetof(struct fl_flow_mask, ht_node), |
| 99 | .automatic_shrinking = true, |
| 100 | }; |
| 101 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 102 | static unsigned short int fl_mask_range(const struct fl_flow_mask *mask) |
| 103 | { |
| 104 | return mask->range.end - mask->range.start; |
| 105 | } |
| 106 | |
| 107 | static void fl_mask_update_range(struct fl_flow_mask *mask) |
| 108 | { |
| 109 | const u8 *bytes = (const u8 *) &mask->key; |
| 110 | size_t size = sizeof(mask->key); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 111 | size_t i, first = 0, last; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 112 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 113 | for (i = 0; i < size; i++) { |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 114 | if (bytes[i]) { |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 115 | first = i; |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | last = first; |
| 120 | for (i = size - 1; i != first; i--) { |
| 121 | if (bytes[i]) { |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 122 | last = i; |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 123 | break; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | mask->range.start = rounddown(first, sizeof(long)); |
| 127 | mask->range.end = roundup(last + 1, sizeof(long)); |
| 128 | } |
| 129 | |
| 130 | static void *fl_key_get_start(struct fl_flow_key *key, |
| 131 | const struct fl_flow_mask *mask) |
| 132 | { |
| 133 | return (u8 *) key + mask->range.start; |
| 134 | } |
| 135 | |
| 136 | static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key, |
| 137 | struct fl_flow_mask *mask) |
| 138 | { |
| 139 | const long *lkey = fl_key_get_start(key, mask); |
| 140 | const long *lmask = fl_key_get_start(&mask->key, mask); |
| 141 | long *lmkey = fl_key_get_start(mkey, mask); |
| 142 | int i; |
| 143 | |
| 144 | for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) |
| 145 | *lmkey++ = *lkey++ & *lmask++; |
| 146 | } |
| 147 | |
| 148 | static void fl_clear_masked_range(struct fl_flow_key *key, |
| 149 | struct fl_flow_mask *mask) |
| 150 | { |
| 151 | memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask)); |
| 152 | } |
| 153 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 154 | static struct cls_fl_filter *fl_lookup(struct fl_flow_mask *mask, |
Paul Blakey | a3308d8 | 2017-01-16 10:45:13 +0200 | [diff] [blame] | 155 | struct fl_flow_key *mkey) |
| 156 | { |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 157 | return rhashtable_lookup_fast(&mask->ht, fl_key_get_start(mkey, mask), |
| 158 | mask->filter_ht_params); |
Paul Blakey | a3308d8 | 2017-01-16 10:45:13 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 161 | static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
| 162 | struct tcf_result *res) |
| 163 | { |
| 164 | struct cls_fl_head *head = rcu_dereference_bh(tp->root); |
| 165 | struct cls_fl_filter *f; |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 166 | struct fl_flow_mask *mask; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 167 | struct fl_flow_key skb_key; |
| 168 | struct fl_flow_key skb_mkey; |
| 169 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 170 | list_for_each_entry_rcu(mask, &head->masks, list) { |
| 171 | fl_clear_masked_range(&skb_key, mask); |
Amir Vadai | e69985c | 2016-06-05 17:11:18 +0300 | [diff] [blame] | 172 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 173 | skb_key.indev_ifindex = skb->skb_iif; |
| 174 | /* skb_flow_dissect() does not set n_proto in case an unknown |
| 175 | * protocol, so do it rather here. |
| 176 | */ |
| 177 | skb_key.basic.n_proto = skb->protocol; |
| 178 | skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key); |
| 179 | skb_flow_dissect(skb, &mask->dissector, &skb_key, 0); |
Amir Vadai | bc3103f | 2016-09-08 16:23:47 +0300 | [diff] [blame] | 180 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 181 | fl_set_masked_key(&skb_mkey, &skb_key, mask); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 182 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 183 | f = fl_lookup(mask, &skb_mkey); |
| 184 | if (f && !tc_skip_sw(f->flags)) { |
| 185 | *res = f->res; |
| 186 | return tcf_exts_exec(skb, &f->exts, res); |
| 187 | } |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 188 | } |
| 189 | return -1; |
| 190 | } |
| 191 | |
| 192 | static int fl_init(struct tcf_proto *tp) |
| 193 | { |
| 194 | struct cls_fl_head *head; |
| 195 | |
| 196 | head = kzalloc(sizeof(*head), GFP_KERNEL); |
| 197 | if (!head) |
| 198 | return -ENOBUFS; |
| 199 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 200 | INIT_LIST_HEAD_RCU(&head->masks); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 201 | rcu_assign_pointer(tp->root, head); |
Chris Mi | c15ab23 | 2017-08-30 02:31:58 -0400 | [diff] [blame] | 202 | idr_init(&head->handle_idr); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 203 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 204 | return rhashtable_init(&head->ht, &mask_ht_params); |
| 205 | } |
| 206 | |
Paolo Abeni | 44a5cd4 | 2018-06-21 20:02:16 +0200 | [diff] [blame] | 207 | static void fl_mask_free(struct fl_flow_mask *mask) |
| 208 | { |
| 209 | rhashtable_destroy(&mask->ht); |
| 210 | kfree(mask); |
| 211 | } |
| 212 | |
| 213 | static void fl_mask_free_work(struct work_struct *work) |
| 214 | { |
| 215 | struct fl_flow_mask *mask = container_of(to_rcu_work(work), |
| 216 | struct fl_flow_mask, rwork); |
| 217 | |
| 218 | fl_mask_free(mask); |
| 219 | } |
| 220 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 221 | static bool fl_mask_put(struct cls_fl_head *head, struct fl_flow_mask *mask, |
| 222 | bool async) |
| 223 | { |
| 224 | if (!list_empty(&mask->filters)) |
| 225 | return false; |
| 226 | |
| 227 | rhashtable_remove_fast(&head->ht, &mask->ht_node, mask_ht_params); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 228 | list_del_rcu(&mask->list); |
| 229 | if (async) |
Paolo Abeni | 44a5cd4 | 2018-06-21 20:02:16 +0200 | [diff] [blame] | 230 | tcf_queue_work(&mask->rwork, fl_mask_free_work); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 231 | else |
Paolo Abeni | 44a5cd4 | 2018-06-21 20:02:16 +0200 | [diff] [blame] | 232 | fl_mask_free(mask); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 233 | |
| 234 | return true; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 235 | } |
| 236 | |
Cong Wang | 0dadc11 | 2017-11-06 13:47:24 -0800 | [diff] [blame] | 237 | static void __fl_destroy_filter(struct cls_fl_filter *f) |
| 238 | { |
| 239 | tcf_exts_destroy(&f->exts); |
| 240 | tcf_exts_put_net(&f->exts); |
| 241 | kfree(f); |
| 242 | } |
| 243 | |
Cong Wang | 0552c8a | 2017-10-26 18:24:33 -0700 | [diff] [blame] | 244 | static void fl_destroy_filter_work(struct work_struct *work) |
| 245 | { |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 246 | struct cls_fl_filter *f = container_of(to_rcu_work(work), |
| 247 | struct cls_fl_filter, rwork); |
Cong Wang | 0552c8a | 2017-10-26 18:24:33 -0700 | [diff] [blame] | 248 | |
| 249 | rtnl_lock(); |
Cong Wang | 0dadc11 | 2017-11-06 13:47:24 -0800 | [diff] [blame] | 250 | __fl_destroy_filter(f); |
Cong Wang | 0552c8a | 2017-10-26 18:24:33 -0700 | [diff] [blame] | 251 | rtnl_unlock(); |
| 252 | } |
| 253 | |
Jakub Kicinski | 1b0f803 | 2018-01-24 12:54:21 -0800 | [diff] [blame] | 254 | static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f, |
| 255 | struct netlink_ext_ack *extack) |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 256 | { |
Jiri Pirko | de4784c | 2017-08-07 10:15:32 +0200 | [diff] [blame] | 257 | struct tc_cls_flower_offload cls_flower = {}; |
Jiri Pirko | 208c0f4 | 2017-10-19 15:50:32 +0200 | [diff] [blame] | 258 | struct tcf_block *block = tp->chain->block; |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 259 | |
Jakub Kicinski | 1b0f803 | 2018-01-24 12:54:21 -0800 | [diff] [blame] | 260 | tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack); |
Jiri Pirko | de4784c | 2017-08-07 10:15:32 +0200 | [diff] [blame] | 261 | cls_flower.command = TC_CLSFLOWER_DESTROY; |
| 262 | cls_flower.cookie = (unsigned long) f; |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 263 | |
Jiri Pirko | 208c0f4 | 2017-10-19 15:50:32 +0200 | [diff] [blame] | 264 | tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER, |
Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 265 | &cls_flower, false); |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 266 | tcf_block_offload_dec(block, &f->flags); |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 267 | } |
| 268 | |
Amir Vadai | e8eb36c | 2016-06-13 12:06:39 +0300 | [diff] [blame] | 269 | static int fl_hw_replace_filter(struct tcf_proto *tp, |
Quentin Monnet | 4100203 | 2018-01-19 17:44:43 -0800 | [diff] [blame] | 270 | struct cls_fl_filter *f, |
| 271 | struct netlink_ext_ack *extack) |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 272 | { |
Jiri Pirko | de4784c | 2017-08-07 10:15:32 +0200 | [diff] [blame] | 273 | struct tc_cls_flower_offload cls_flower = {}; |
Jiri Pirko | 208c0f4 | 2017-10-19 15:50:32 +0200 | [diff] [blame] | 274 | struct tcf_block *block = tp->chain->block; |
Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 275 | bool skip_sw = tc_skip_sw(f->flags); |
Amir Vadai | e8eb36c | 2016-06-13 12:06:39 +0300 | [diff] [blame] | 276 | int err; |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 277 | |
Jakub Kicinski | ea20594 | 2018-01-24 12:54:20 -0800 | [diff] [blame] | 278 | tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack); |
Jiri Pirko | de4784c | 2017-08-07 10:15:32 +0200 | [diff] [blame] | 279 | cls_flower.command = TC_CLSFLOWER_REPLACE; |
| 280 | cls_flower.cookie = (unsigned long) f; |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 281 | cls_flower.dissector = &f->mask->dissector; |
| 282 | cls_flower.mask = &f->mask->key; |
Jiri Pirko | de4784c | 2017-08-07 10:15:32 +0200 | [diff] [blame] | 283 | cls_flower.key = &f->mkey; |
| 284 | cls_flower.exts = &f->exts; |
Amritha Nambiar | 384c181 | 2017-10-27 02:35:34 -0700 | [diff] [blame] | 285 | cls_flower.classid = f->res.classid; |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 286 | |
Jiri Pirko | 208c0f4 | 2017-10-19 15:50:32 +0200 | [diff] [blame] | 287 | err = tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER, |
Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 288 | &cls_flower, skip_sw); |
| 289 | if (err < 0) { |
Jakub Kicinski | 1b0f803 | 2018-01-24 12:54:21 -0800 | [diff] [blame] | 290 | fl_hw_destroy_filter(tp, f, NULL); |
Amir Vadai | e8eb36c | 2016-06-13 12:06:39 +0300 | [diff] [blame] | 291 | return err; |
Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 292 | } else if (err > 0) { |
John Hurley | 31533cb | 2018-06-25 14:30:06 -0700 | [diff] [blame] | 293 | f->in_hw_count = err; |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 294 | tcf_block_offload_inc(block, &f->flags); |
Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW)) |
| 298 | return -EINVAL; |
| 299 | |
Amir Vadai | e8eb36c | 2016-06-13 12:06:39 +0300 | [diff] [blame] | 300 | return 0; |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 301 | } |
| 302 | |
Amir Vadai | 10cbc68 | 2016-05-13 12:55:37 +0000 | [diff] [blame] | 303 | static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f) |
| 304 | { |
Jiri Pirko | de4784c | 2017-08-07 10:15:32 +0200 | [diff] [blame] | 305 | struct tc_cls_flower_offload cls_flower = {}; |
Jiri Pirko | 208c0f4 | 2017-10-19 15:50:32 +0200 | [diff] [blame] | 306 | struct tcf_block *block = tp->chain->block; |
Amir Vadai | 10cbc68 | 2016-05-13 12:55:37 +0000 | [diff] [blame] | 307 | |
Jakub Kicinski | ea20594 | 2018-01-24 12:54:20 -0800 | [diff] [blame] | 308 | tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, NULL); |
Jiri Pirko | de4784c | 2017-08-07 10:15:32 +0200 | [diff] [blame] | 309 | cls_flower.command = TC_CLSFLOWER_STATS; |
| 310 | cls_flower.cookie = (unsigned long) f; |
| 311 | cls_flower.exts = &f->exts; |
Amritha Nambiar | 384c181 | 2017-10-27 02:35:34 -0700 | [diff] [blame] | 312 | cls_flower.classid = f->res.classid; |
Amir Vadai | 10cbc68 | 2016-05-13 12:55:37 +0000 | [diff] [blame] | 313 | |
Jiri Pirko | 208c0f4 | 2017-10-19 15:50:32 +0200 | [diff] [blame] | 314 | tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER, |
Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 315 | &cls_flower, false); |
Amir Vadai | 10cbc68 | 2016-05-13 12:55:37 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 318 | 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] | 319 | struct netlink_ext_ack *extack) |
Roi Dayan | 13fa876 | 2016-11-01 16:08:29 +0200 | [diff] [blame] | 320 | { |
Chris Mi | c15ab23 | 2017-08-30 02:31:58 -0400 | [diff] [blame] | 321 | struct cls_fl_head *head = rtnl_dereference(tp->root); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 322 | bool async = tcf_exts_get_net(&f->exts); |
| 323 | bool last; |
Chris Mi | c15ab23 | 2017-08-30 02:31:58 -0400 | [diff] [blame] | 324 | |
Matthew Wilcox | 9c16094 | 2017-11-28 09:48:43 -0500 | [diff] [blame] | 325 | idr_remove(&head->handle_idr, f->handle); |
Roi Dayan | 13fa876 | 2016-11-01 16:08:29 +0200 | [diff] [blame] | 326 | list_del_rcu(&f->list); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 327 | last = fl_mask_put(head, f->mask, async); |
Hadar Hen Zion | 7968521 | 2016-12-01 14:06:34 +0200 | [diff] [blame] | 328 | if (!tc_skip_hw(f->flags)) |
Jakub Kicinski | 1b0f803 | 2018-01-24 12:54:21 -0800 | [diff] [blame] | 329 | fl_hw_destroy_filter(tp, f, extack); |
Roi Dayan | 13fa876 | 2016-11-01 16:08:29 +0200 | [diff] [blame] | 330 | tcf_unbind_filter(tp, &f->res); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 331 | if (async) |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 332 | tcf_queue_work(&f->rwork, fl_destroy_filter_work); |
Cong Wang | 0dadc11 | 2017-11-06 13:47:24 -0800 | [diff] [blame] | 333 | else |
| 334 | __fl_destroy_filter(f); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 335 | |
| 336 | return last; |
Roi Dayan | 13fa876 | 2016-11-01 16:08:29 +0200 | [diff] [blame] | 337 | } |
| 338 | |
Daniel Borkmann | d936377 | 2016-11-27 01:18:01 +0100 | [diff] [blame] | 339 | static void fl_destroy_sleepable(struct work_struct *work) |
| 340 | { |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 341 | struct cls_fl_head *head = container_of(to_rcu_work(work), |
| 342 | struct cls_fl_head, |
| 343 | rwork); |
Paul Blakey | de9dc65 | 2018-06-03 10:06:13 +0300 | [diff] [blame] | 344 | |
| 345 | rhashtable_destroy(&head->ht); |
Daniel Borkmann | d936377 | 2016-11-27 01:18:01 +0100 | [diff] [blame] | 346 | kfree(head); |
| 347 | module_put(THIS_MODULE); |
| 348 | } |
| 349 | |
Jakub Kicinski | 715df5e | 2018-01-24 12:54:13 -0800 | [diff] [blame] | 350 | 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] | 351 | { |
| 352 | struct cls_fl_head *head = rtnl_dereference(tp->root); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 353 | struct fl_flow_mask *mask, *next_mask; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 354 | struct cls_fl_filter *f, *next; |
| 355 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 356 | list_for_each_entry_safe(mask, next_mask, &head->masks, list) { |
| 357 | list_for_each_entry_safe(f, next, &mask->filters, list) { |
| 358 | if (__fl_delete(tp, f, extack)) |
| 359 | break; |
| 360 | } |
| 361 | } |
Chris Mi | c15ab23 | 2017-08-30 02:31:58 -0400 | [diff] [blame] | 362 | idr_destroy(&head->handle_idr); |
Daniel Borkmann | d936377 | 2016-11-27 01:18:01 +0100 | [diff] [blame] | 363 | |
| 364 | __module_get(THIS_MODULE); |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 365 | tcf_queue_work(&head->rwork, fl_destroy_sleepable); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 366 | } |
| 367 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 368 | static void *fl_get(struct tcf_proto *tp, u32 handle) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 369 | { |
| 370 | struct cls_fl_head *head = rtnl_dereference(tp->root); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 371 | |
Matthew Wilcox | 322d884 | 2017-11-28 10:01:24 -0500 | [diff] [blame] | 372 | return idr_find(&head->handle_idr, handle); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = { |
| 376 | [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC }, |
| 377 | [TCA_FLOWER_CLASSID] = { .type = NLA_U32 }, |
| 378 | [TCA_FLOWER_INDEV] = { .type = NLA_STRING, |
| 379 | .len = IFNAMSIZ }, |
| 380 | [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN }, |
| 381 | [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN }, |
| 382 | [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN }, |
| 383 | [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN }, |
| 384 | [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 }, |
| 385 | [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 }, |
| 386 | [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 }, |
| 387 | [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 }, |
| 388 | [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 }, |
| 389 | [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 }, |
| 390 | [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) }, |
| 391 | [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) }, |
| 392 | [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) }, |
| 393 | [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) }, |
| 394 | [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 }, |
| 395 | [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 }, |
Jamal Hadi Salim | b175c3a | 2015-06-25 06:55:27 -0400 | [diff] [blame] | 396 | [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 }, |
| 397 | [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 }, |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 398 | [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 }, |
| 399 | [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 }, |
| 400 | [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 }, |
Amir Vadai | bc3103f | 2016-09-08 16:23:47 +0300 | [diff] [blame] | 401 | [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 }, |
| 402 | [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 }, |
| 403 | [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 }, |
| 404 | [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 }, |
| 405 | [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 }, |
| 406 | [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) }, |
| 407 | [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) }, |
| 408 | [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) }, |
| 409 | [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) }, |
Or Gerlitz | aa72d70 | 2016-09-15 15:28:22 +0300 | [diff] [blame] | 410 | [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 }, |
| 411 | [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 }, |
| 412 | [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 }, |
| 413 | [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 }, |
Simon Horman | 5976c5f | 2016-11-03 13:24:21 +0100 | [diff] [blame] | 414 | [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 }, |
| 415 | [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 }, |
| 416 | [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 }, |
| 417 | [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 }, |
Hadar Hen Zion | f4d997f | 2016-11-07 15:14:39 +0200 | [diff] [blame] | 418 | [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 }, |
| 419 | [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 }, |
| 420 | [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 }, |
| 421 | [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 }, |
Or Gerlitz | faa3ffc | 2016-12-07 14:03:10 +0200 | [diff] [blame] | 422 | [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 }, |
| 423 | [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 }, |
Simon Horman | 7b68488 | 2016-12-07 13:48:28 +0100 | [diff] [blame] | 424 | [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 }, |
| 425 | [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 }, |
| 426 | [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 }, |
| 427 | [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 }, |
| 428 | [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 }, |
| 429 | [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 }, |
| 430 | [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 }, |
| 431 | [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 }, |
Simon Horman | 99d3132 | 2017-01-11 14:05:43 +0100 | [diff] [blame] | 432 | [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 }, |
| 433 | [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 }, |
| 434 | [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 }, |
| 435 | [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 }, |
| 436 | [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 }, |
| 437 | [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 }, |
| 438 | [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN }, |
| 439 | [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN }, |
| 440 | [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN }, |
| 441 | [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN }, |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 442 | [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 }, |
| 443 | [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 }, |
| 444 | [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 }, |
| 445 | [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 }, |
Jiri Pirko | fdfc7dd | 2017-05-23 18:40:45 +0200 | [diff] [blame] | 446 | [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 }, |
| 447 | [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 }, |
Or Gerlitz | 4d80cc0 | 2017-06-01 21:37:38 +0300 | [diff] [blame] | 448 | [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 }, |
| 449 | [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 }, |
| 450 | [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 }, |
| 451 | [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 }, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 452 | }; |
| 453 | |
| 454 | static void fl_set_key_val(struct nlattr **tb, |
| 455 | void *val, int val_type, |
| 456 | void *mask, int mask_type, int len) |
| 457 | { |
| 458 | if (!tb[val_type]) |
| 459 | return; |
| 460 | memcpy(val, nla_data(tb[val_type]), len); |
| 461 | if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type]) |
| 462 | memset(mask, 0xff, len); |
| 463 | else |
| 464 | memcpy(mask, nla_data(tb[mask_type]), len); |
| 465 | } |
| 466 | |
Benjamin LaHaise | 1a7fca6 | 2017-05-01 09:58:40 -0400 | [diff] [blame] | 467 | static int fl_set_key_mpls(struct nlattr **tb, |
| 468 | struct flow_dissector_key_mpls *key_val, |
| 469 | struct flow_dissector_key_mpls *key_mask) |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 470 | { |
| 471 | if (tb[TCA_FLOWER_KEY_MPLS_TTL]) { |
| 472 | key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]); |
| 473 | key_mask->mpls_ttl = MPLS_TTL_MASK; |
| 474 | } |
| 475 | if (tb[TCA_FLOWER_KEY_MPLS_BOS]) { |
Benjamin LaHaise | 1a7fca6 | 2017-05-01 09:58:40 -0400 | [diff] [blame] | 476 | u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]); |
| 477 | |
| 478 | if (bos & ~MPLS_BOS_MASK) |
| 479 | return -EINVAL; |
| 480 | key_val->mpls_bos = bos; |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 481 | key_mask->mpls_bos = MPLS_BOS_MASK; |
| 482 | } |
| 483 | if (tb[TCA_FLOWER_KEY_MPLS_TC]) { |
Benjamin LaHaise | 1a7fca6 | 2017-05-01 09:58:40 -0400 | [diff] [blame] | 484 | u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]); |
| 485 | |
| 486 | if (tc & ~MPLS_TC_MASK) |
| 487 | return -EINVAL; |
| 488 | key_val->mpls_tc = tc; |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 489 | key_mask->mpls_tc = MPLS_TC_MASK; |
| 490 | } |
| 491 | if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) { |
Benjamin LaHaise | 1a7fca6 | 2017-05-01 09:58:40 -0400 | [diff] [blame] | 492 | u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]); |
| 493 | |
| 494 | if (label & ~MPLS_LABEL_MASK) |
| 495 | return -EINVAL; |
| 496 | key_val->mpls_label = label; |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 497 | key_mask->mpls_label = MPLS_LABEL_MASK; |
| 498 | } |
Benjamin LaHaise | 1a7fca6 | 2017-05-01 09:58:40 -0400 | [diff] [blame] | 499 | return 0; |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 500 | } |
| 501 | |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 502 | static void fl_set_key_vlan(struct nlattr **tb, |
Jianbo Liu | aaab083 | 2018-07-06 05:38:13 +0000 | [diff] [blame] | 503 | __be16 ethertype, |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 504 | struct flow_dissector_key_vlan *key_val, |
| 505 | struct flow_dissector_key_vlan *key_mask) |
| 506 | { |
| 507 | #define VLAN_PRIORITY_MASK 0x7 |
| 508 | |
| 509 | if (tb[TCA_FLOWER_KEY_VLAN_ID]) { |
| 510 | key_val->vlan_id = |
| 511 | nla_get_u16(tb[TCA_FLOWER_KEY_VLAN_ID]) & VLAN_VID_MASK; |
| 512 | key_mask->vlan_id = VLAN_VID_MASK; |
| 513 | } |
| 514 | if (tb[TCA_FLOWER_KEY_VLAN_PRIO]) { |
| 515 | key_val->vlan_priority = |
| 516 | nla_get_u8(tb[TCA_FLOWER_KEY_VLAN_PRIO]) & |
| 517 | VLAN_PRIORITY_MASK; |
| 518 | key_mask->vlan_priority = VLAN_PRIORITY_MASK; |
| 519 | } |
Jianbo Liu | aaab083 | 2018-07-06 05:38:13 +0000 | [diff] [blame] | 520 | key_val->vlan_tpid = ethertype; |
| 521 | key_mask->vlan_tpid = cpu_to_be16(~0); |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 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 | |
Jianbo Liu | aaab083 | 2018-07-06 05:38:13 +0000 | [diff] [blame] | 598 | if (eth_type_vlan(ethertype)) { |
| 599 | fl_set_key_vlan(tb, ethertype, &key->vlan, &mask->vlan); |
Arnd Bergmann | 0b498a5 | 2016-08-26 17:25:45 +0200 | [diff] [blame] | 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; |
Paul Blakey | f6521c5 | 2018-06-03 10:06:14 +0300 | [diff] [blame] | 898 | } else if (fold && fold->mask != fnew->mask) { |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 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); |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 1039 | tcf_queue_work(&fold->rwork, fl_destroy_filter_work); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 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: |
Paul Blakey | 8258d2d | 2018-05-30 11:29:15 +0300 | [diff] [blame] | 1051 | if (!fold) |
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 | |
John Hurley | 31533cb | 2018-06-25 14:30:06 -0700 | [diff] [blame] | 1095 | static int fl_reoffload(struct tcf_proto *tp, bool add, tc_setup_cb_t *cb, |
| 1096 | void *cb_priv, struct netlink_ext_ack *extack) |
| 1097 | { |
| 1098 | struct cls_fl_head *head = rtnl_dereference(tp->root); |
| 1099 | struct tc_cls_flower_offload cls_flower = {}; |
| 1100 | struct tcf_block *block = tp->chain->block; |
| 1101 | struct fl_flow_mask *mask; |
| 1102 | struct cls_fl_filter *f; |
| 1103 | int err; |
| 1104 | |
| 1105 | list_for_each_entry(mask, &head->masks, list) { |
| 1106 | list_for_each_entry(f, &mask->filters, list) { |
| 1107 | if (tc_skip_hw(f->flags)) |
| 1108 | continue; |
| 1109 | |
| 1110 | tc_cls_common_offload_init(&cls_flower.common, tp, |
| 1111 | f->flags, extack); |
| 1112 | cls_flower.command = add ? |
| 1113 | TC_CLSFLOWER_REPLACE : TC_CLSFLOWER_DESTROY; |
| 1114 | cls_flower.cookie = (unsigned long)f; |
| 1115 | cls_flower.dissector = &mask->dissector; |
| 1116 | cls_flower.mask = &f->mkey; |
| 1117 | cls_flower.key = &f->key; |
| 1118 | cls_flower.exts = &f->exts; |
| 1119 | cls_flower.classid = f->res.classid; |
| 1120 | |
| 1121 | err = cb(TC_SETUP_CLSFLOWER, &cls_flower, cb_priv); |
| 1122 | if (err) { |
| 1123 | if (add && tc_skip_sw(f->flags)) |
| 1124 | return err; |
| 1125 | continue; |
| 1126 | } |
| 1127 | |
| 1128 | tc_cls_offload_cnt_update(block, &f->in_hw_count, |
| 1129 | &f->flags, add); |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | return 0; |
| 1134 | } |
| 1135 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1136 | static int fl_dump_key_val(struct sk_buff *skb, |
| 1137 | void *val, int val_type, |
| 1138 | void *mask, int mask_type, int len) |
| 1139 | { |
| 1140 | int err; |
| 1141 | |
| 1142 | if (!memchr_inv(mask, 0, len)) |
| 1143 | return 0; |
| 1144 | err = nla_put(skb, val_type, len, val); |
| 1145 | if (err) |
| 1146 | return err; |
| 1147 | if (mask_type != TCA_FLOWER_UNSPEC) { |
| 1148 | err = nla_put(skb, mask_type, len, mask); |
| 1149 | if (err) |
| 1150 | return err; |
| 1151 | } |
| 1152 | return 0; |
| 1153 | } |
| 1154 | |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 1155 | static int fl_dump_key_mpls(struct sk_buff *skb, |
| 1156 | struct flow_dissector_key_mpls *mpls_key, |
| 1157 | struct flow_dissector_key_mpls *mpls_mask) |
| 1158 | { |
| 1159 | int err; |
| 1160 | |
| 1161 | if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask))) |
| 1162 | return 0; |
| 1163 | if (mpls_mask->mpls_ttl) { |
| 1164 | err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL, |
| 1165 | mpls_key->mpls_ttl); |
| 1166 | if (err) |
| 1167 | return err; |
| 1168 | } |
| 1169 | if (mpls_mask->mpls_tc) { |
| 1170 | err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC, |
| 1171 | mpls_key->mpls_tc); |
| 1172 | if (err) |
| 1173 | return err; |
| 1174 | } |
| 1175 | if (mpls_mask->mpls_label) { |
| 1176 | err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL, |
| 1177 | mpls_key->mpls_label); |
| 1178 | if (err) |
| 1179 | return err; |
| 1180 | } |
| 1181 | if (mpls_mask->mpls_bos) { |
| 1182 | err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS, |
| 1183 | mpls_key->mpls_bos); |
| 1184 | if (err) |
| 1185 | return err; |
| 1186 | } |
| 1187 | return 0; |
| 1188 | } |
| 1189 | |
Or Gerlitz | 4d80cc0 | 2017-06-01 21:37:38 +0300 | [diff] [blame] | 1190 | static int fl_dump_key_ip(struct sk_buff *skb, |
| 1191 | struct flow_dissector_key_ip *key, |
| 1192 | struct flow_dissector_key_ip *mask) |
| 1193 | { |
| 1194 | if (fl_dump_key_val(skb, &key->tos, TCA_FLOWER_KEY_IP_TOS, &mask->tos, |
| 1195 | TCA_FLOWER_KEY_IP_TOS_MASK, sizeof(key->tos)) || |
| 1196 | fl_dump_key_val(skb, &key->ttl, TCA_FLOWER_KEY_IP_TTL, &mask->ttl, |
| 1197 | TCA_FLOWER_KEY_IP_TTL_MASK, sizeof(key->ttl))) |
| 1198 | return -1; |
| 1199 | |
| 1200 | return 0; |
| 1201 | } |
| 1202 | |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 1203 | static int fl_dump_key_vlan(struct sk_buff *skb, |
| 1204 | struct flow_dissector_key_vlan *vlan_key, |
| 1205 | struct flow_dissector_key_vlan *vlan_mask) |
| 1206 | { |
| 1207 | int err; |
| 1208 | |
| 1209 | if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask))) |
| 1210 | return 0; |
| 1211 | if (vlan_mask->vlan_id) { |
| 1212 | err = nla_put_u16(skb, TCA_FLOWER_KEY_VLAN_ID, |
| 1213 | vlan_key->vlan_id); |
| 1214 | if (err) |
| 1215 | return err; |
| 1216 | } |
| 1217 | if (vlan_mask->vlan_priority) { |
| 1218 | err = nla_put_u8(skb, TCA_FLOWER_KEY_VLAN_PRIO, |
| 1219 | vlan_key->vlan_priority); |
| 1220 | if (err) |
| 1221 | return err; |
| 1222 | } |
| 1223 | return 0; |
| 1224 | } |
| 1225 | |
Or Gerlitz | faa3ffc | 2016-12-07 14:03:10 +0200 | [diff] [blame] | 1226 | static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask, |
| 1227 | u32 *flower_key, u32 *flower_mask, |
| 1228 | u32 flower_flag_bit, u32 dissector_flag_bit) |
| 1229 | { |
| 1230 | if (dissector_mask & dissector_flag_bit) { |
| 1231 | *flower_mask |= flower_flag_bit; |
| 1232 | if (dissector_key & dissector_flag_bit) |
| 1233 | *flower_key |= flower_flag_bit; |
| 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask) |
| 1238 | { |
| 1239 | u32 key, mask; |
| 1240 | __be32 _key, _mask; |
| 1241 | int err; |
| 1242 | |
| 1243 | if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask))) |
| 1244 | return 0; |
| 1245 | |
| 1246 | key = 0; |
| 1247 | mask = 0; |
| 1248 | |
| 1249 | fl_get_key_flag(flags_key, flags_mask, &key, &mask, |
| 1250 | TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT); |
Pieter Jansen van Vuuren | 459d153 | 2018-03-06 18:11:14 +0100 | [diff] [blame] | 1251 | fl_get_key_flag(flags_key, flags_mask, &key, &mask, |
| 1252 | TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST, |
| 1253 | FLOW_DIS_FIRST_FRAG); |
Or Gerlitz | faa3ffc | 2016-12-07 14:03:10 +0200 | [diff] [blame] | 1254 | |
| 1255 | _key = cpu_to_be32(key); |
| 1256 | _mask = cpu_to_be32(mask); |
| 1257 | |
| 1258 | err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key); |
| 1259 | if (err) |
| 1260 | return err; |
| 1261 | |
| 1262 | return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask); |
| 1263 | } |
| 1264 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 1265 | 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] | 1266 | struct sk_buff *skb, struct tcmsg *t) |
| 1267 | { |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 1268 | struct cls_fl_filter *f = fh; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1269 | struct nlattr *nest; |
| 1270 | struct fl_flow_key *key, *mask; |
| 1271 | |
| 1272 | if (!f) |
| 1273 | return skb->len; |
| 1274 | |
| 1275 | t->tcm_handle = f->handle; |
| 1276 | |
| 1277 | nest = nla_nest_start(skb, TCA_OPTIONS); |
| 1278 | if (!nest) |
| 1279 | goto nla_put_failure; |
| 1280 | |
| 1281 | if (f->res.classid && |
| 1282 | nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid)) |
| 1283 | goto nla_put_failure; |
| 1284 | |
| 1285 | key = &f->key; |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1286 | mask = &f->mask->key; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1287 | |
| 1288 | if (mask->indev_ifindex) { |
| 1289 | struct net_device *dev; |
| 1290 | |
| 1291 | dev = __dev_get_by_index(net, key->indev_ifindex); |
| 1292 | if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name)) |
| 1293 | goto nla_put_failure; |
| 1294 | } |
| 1295 | |
Hadar Hen Zion | 7968521 | 2016-12-01 14:06:34 +0200 | [diff] [blame] | 1296 | if (!tc_skip_hw(f->flags)) |
| 1297 | fl_hw_update_stats(tp, f); |
Amir Vadai | 10cbc68 | 2016-05-13 12:55:37 +0000 | [diff] [blame] | 1298 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1299 | if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST, |
| 1300 | mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK, |
| 1301 | sizeof(key->eth.dst)) || |
| 1302 | fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC, |
| 1303 | mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK, |
| 1304 | sizeof(key->eth.src)) || |
| 1305 | fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE, |
| 1306 | &mask->basic.n_proto, TCA_FLOWER_UNSPEC, |
| 1307 | sizeof(key->basic.n_proto))) |
| 1308 | goto nla_put_failure; |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 1309 | |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 1310 | if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls)) |
| 1311 | goto nla_put_failure; |
| 1312 | |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 1313 | if (fl_dump_key_vlan(skb, &key->vlan, &mask->vlan)) |
| 1314 | goto nla_put_failure; |
| 1315 | |
Jianbo Liu | d306951 | 2018-07-06 05:38:15 +0000 | [diff] [blame^] | 1316 | if (mask->vlan.vlan_tpid && |
| 1317 | nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE, key->basic.n_proto)) |
| 1318 | goto nla_put_failure; |
| 1319 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1320 | if ((key->basic.n_proto == htons(ETH_P_IP) || |
| 1321 | key->basic.n_proto == htons(ETH_P_IPV6)) && |
Or Gerlitz | 4d80cc0 | 2017-06-01 21:37:38 +0300 | [diff] [blame] | 1322 | (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] | 1323 | &mask->basic.ip_proto, TCA_FLOWER_UNSPEC, |
Or Gerlitz | 4d80cc0 | 2017-06-01 21:37:38 +0300 | [diff] [blame] | 1324 | sizeof(key->basic.ip_proto)) || |
| 1325 | fl_dump_key_ip(skb, &key->ip, &mask->ip))) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1326 | goto nla_put_failure; |
| 1327 | |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 1328 | if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS && |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1329 | (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC, |
| 1330 | &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK, |
| 1331 | sizeof(key->ipv4.src)) || |
| 1332 | fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST, |
| 1333 | &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK, |
| 1334 | sizeof(key->ipv4.dst)))) |
| 1335 | goto nla_put_failure; |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 1336 | else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS && |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1337 | (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC, |
| 1338 | &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK, |
| 1339 | sizeof(key->ipv6.src)) || |
| 1340 | fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST, |
| 1341 | &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK, |
| 1342 | sizeof(key->ipv6.dst)))) |
| 1343 | goto nla_put_failure; |
| 1344 | |
| 1345 | if (key->basic.ip_proto == IPPROTO_TCP && |
| 1346 | (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] | 1347 | &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1348 | sizeof(key->tp.src)) || |
| 1349 | 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] | 1350 | &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK, |
Jiri Pirko | fdfc7dd | 2017-05-23 18:40:45 +0200 | [diff] [blame] | 1351 | sizeof(key->tp.dst)) || |
| 1352 | fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS, |
| 1353 | &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK, |
| 1354 | sizeof(key->tcp.flags)))) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1355 | goto nla_put_failure; |
| 1356 | else if (key->basic.ip_proto == IPPROTO_UDP && |
| 1357 | (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] | 1358 | &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1359 | sizeof(key->tp.src)) || |
| 1360 | 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] | 1361 | &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1362 | sizeof(key->tp.dst)))) |
| 1363 | goto nla_put_failure; |
Simon Horman | 5976c5f | 2016-11-03 13:24:21 +0100 | [diff] [blame] | 1364 | else if (key->basic.ip_proto == IPPROTO_SCTP && |
| 1365 | (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC, |
| 1366 | &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK, |
| 1367 | sizeof(key->tp.src)) || |
| 1368 | fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST, |
| 1369 | &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK, |
| 1370 | sizeof(key->tp.dst)))) |
| 1371 | goto nla_put_failure; |
Simon Horman | 7b68488 | 2016-12-07 13:48:28 +0100 | [diff] [blame] | 1372 | else if (key->basic.n_proto == htons(ETH_P_IP) && |
| 1373 | key->basic.ip_proto == IPPROTO_ICMP && |
| 1374 | (fl_dump_key_val(skb, &key->icmp.type, |
| 1375 | TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type, |
| 1376 | TCA_FLOWER_KEY_ICMPV4_TYPE_MASK, |
| 1377 | sizeof(key->icmp.type)) || |
| 1378 | fl_dump_key_val(skb, &key->icmp.code, |
| 1379 | TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code, |
| 1380 | TCA_FLOWER_KEY_ICMPV4_CODE_MASK, |
| 1381 | sizeof(key->icmp.code)))) |
| 1382 | goto nla_put_failure; |
| 1383 | else if (key->basic.n_proto == htons(ETH_P_IPV6) && |
| 1384 | key->basic.ip_proto == IPPROTO_ICMPV6 && |
| 1385 | (fl_dump_key_val(skb, &key->icmp.type, |
| 1386 | TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type, |
| 1387 | TCA_FLOWER_KEY_ICMPV6_TYPE_MASK, |
| 1388 | sizeof(key->icmp.type)) || |
| 1389 | fl_dump_key_val(skb, &key->icmp.code, |
| 1390 | TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code, |
| 1391 | TCA_FLOWER_KEY_ICMPV6_CODE_MASK, |
| 1392 | sizeof(key->icmp.code)))) |
| 1393 | goto nla_put_failure; |
Simon Horman | 99d3132 | 2017-01-11 14:05:43 +0100 | [diff] [blame] | 1394 | else if ((key->basic.n_proto == htons(ETH_P_ARP) || |
| 1395 | key->basic.n_proto == htons(ETH_P_RARP)) && |
| 1396 | (fl_dump_key_val(skb, &key->arp.sip, |
| 1397 | TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip, |
| 1398 | TCA_FLOWER_KEY_ARP_SIP_MASK, |
| 1399 | sizeof(key->arp.sip)) || |
| 1400 | fl_dump_key_val(skb, &key->arp.tip, |
| 1401 | TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip, |
| 1402 | TCA_FLOWER_KEY_ARP_TIP_MASK, |
| 1403 | sizeof(key->arp.tip)) || |
| 1404 | fl_dump_key_val(skb, &key->arp.op, |
| 1405 | TCA_FLOWER_KEY_ARP_OP, &mask->arp.op, |
| 1406 | TCA_FLOWER_KEY_ARP_OP_MASK, |
| 1407 | sizeof(key->arp.op)) || |
| 1408 | fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA, |
| 1409 | mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK, |
| 1410 | sizeof(key->arp.sha)) || |
| 1411 | fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA, |
| 1412 | mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK, |
| 1413 | sizeof(key->arp.tha)))) |
| 1414 | goto nla_put_failure; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1415 | |
Amir Vadai | bc3103f | 2016-09-08 16:23:47 +0300 | [diff] [blame] | 1416 | if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS && |
| 1417 | (fl_dump_key_val(skb, &key->enc_ipv4.src, |
| 1418 | TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src, |
| 1419 | TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK, |
| 1420 | sizeof(key->enc_ipv4.src)) || |
| 1421 | fl_dump_key_val(skb, &key->enc_ipv4.dst, |
| 1422 | TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst, |
| 1423 | TCA_FLOWER_KEY_ENC_IPV4_DST_MASK, |
| 1424 | sizeof(key->enc_ipv4.dst)))) |
| 1425 | goto nla_put_failure; |
| 1426 | else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS && |
| 1427 | (fl_dump_key_val(skb, &key->enc_ipv6.src, |
| 1428 | TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src, |
| 1429 | TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK, |
| 1430 | sizeof(key->enc_ipv6.src)) || |
| 1431 | fl_dump_key_val(skb, &key->enc_ipv6.dst, |
| 1432 | TCA_FLOWER_KEY_ENC_IPV6_DST, |
| 1433 | &mask->enc_ipv6.dst, |
| 1434 | TCA_FLOWER_KEY_ENC_IPV6_DST_MASK, |
| 1435 | sizeof(key->enc_ipv6.dst)))) |
| 1436 | goto nla_put_failure; |
| 1437 | |
| 1438 | 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] | 1439 | &mask->enc_key_id, TCA_FLOWER_UNSPEC, |
Hadar Hen Zion | f4d997f | 2016-11-07 15:14:39 +0200 | [diff] [blame] | 1440 | sizeof(key->enc_key_id)) || |
| 1441 | fl_dump_key_val(skb, &key->enc_tp.src, |
| 1442 | TCA_FLOWER_KEY_ENC_UDP_SRC_PORT, |
| 1443 | &mask->enc_tp.src, |
| 1444 | TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK, |
| 1445 | sizeof(key->enc_tp.src)) || |
| 1446 | fl_dump_key_val(skb, &key->enc_tp.dst, |
| 1447 | TCA_FLOWER_KEY_ENC_UDP_DST_PORT, |
| 1448 | &mask->enc_tp.dst, |
| 1449 | TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK, |
| 1450 | sizeof(key->enc_tp.dst))) |
Amir Vadai | bc3103f | 2016-09-08 16:23:47 +0300 | [diff] [blame] | 1451 | goto nla_put_failure; |
| 1452 | |
Or Gerlitz | faa3ffc | 2016-12-07 14:03:10 +0200 | [diff] [blame] | 1453 | if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags)) |
| 1454 | goto nla_put_failure; |
| 1455 | |
Or Gerlitz | 749e672 | 2017-02-16 10:31:10 +0200 | [diff] [blame] | 1456 | if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags)) |
| 1457 | goto nla_put_failure; |
Amir Vadai | e69985c | 2016-06-05 17:11:18 +0300 | [diff] [blame] | 1458 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1459 | if (tcf_exts_dump(skb, &f->exts)) |
| 1460 | goto nla_put_failure; |
| 1461 | |
| 1462 | nla_nest_end(skb, nest); |
| 1463 | |
| 1464 | if (tcf_exts_dump_stats(skb, &f->exts) < 0) |
| 1465 | goto nla_put_failure; |
| 1466 | |
| 1467 | return skb->len; |
| 1468 | |
| 1469 | nla_put_failure: |
| 1470 | nla_nest_cancel(skb, nest); |
| 1471 | return -1; |
| 1472 | } |
| 1473 | |
Cong Wang | 07d79fc | 2017-08-30 14:30:36 -0700 | [diff] [blame] | 1474 | static void fl_bind_class(void *fh, u32 classid, unsigned long cl) |
| 1475 | { |
| 1476 | struct cls_fl_filter *f = fh; |
| 1477 | |
| 1478 | if (f && f->res.classid == classid) |
| 1479 | f->res.class = cl; |
| 1480 | } |
| 1481 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1482 | static struct tcf_proto_ops cls_fl_ops __read_mostly = { |
| 1483 | .kind = "flower", |
| 1484 | .classify = fl_classify, |
| 1485 | .init = fl_init, |
| 1486 | .destroy = fl_destroy, |
| 1487 | .get = fl_get, |
| 1488 | .change = fl_change, |
| 1489 | .delete = fl_delete, |
| 1490 | .walk = fl_walk, |
John Hurley | 31533cb | 2018-06-25 14:30:06 -0700 | [diff] [blame] | 1491 | .reoffload = fl_reoffload, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1492 | .dump = fl_dump, |
Cong Wang | 07d79fc | 2017-08-30 14:30:36 -0700 | [diff] [blame] | 1493 | .bind_class = fl_bind_class, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1494 | .owner = THIS_MODULE, |
| 1495 | }; |
| 1496 | |
| 1497 | static int __init cls_fl_init(void) |
| 1498 | { |
| 1499 | return register_tcf_proto_ops(&cls_fl_ops); |
| 1500 | } |
| 1501 | |
| 1502 | static void __exit cls_fl_exit(void) |
| 1503 | { |
| 1504 | unregister_tcf_proto_ops(&cls_fl_ops); |
| 1505 | } |
| 1506 | |
| 1507 | module_init(cls_fl_init); |
| 1508 | module_exit(cls_fl_exit); |
| 1509 | |
| 1510 | MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>"); |
| 1511 | MODULE_DESCRIPTION("Flower classifier"); |
| 1512 | MODULE_LICENSE("GPL v2"); |