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