Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * net/sched/cls_fw.c Classifier mapping ipchains' fwmark to traffic class. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 10 | * |
| 11 | * Changes: |
| 12 | * Karlis Peisenieks <karlis@mt.lv> : 990415 : fw_walk off by one |
| 13 | * Karlis Peisenieks <karlis@mt.lv> : 990415 : fw_delete killed all the filter (and kernel). |
| 14 | * Alex <alex@pilotsoft.com> : 2004xxyy: Added Action extension |
| 15 | * |
| 16 | * JHS: We should remove the CONFIG_NET_CLS_IND from here |
| 17 | * eventually when the meta match extension is made available |
| 18 | * |
| 19 | */ |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/types.h> |
| 24 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/skbuff.h> |
Patrick McHardy | 0ba4805 | 2007-07-02 22:49:07 -0700 | [diff] [blame] | 28 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <net/act_api.h> |
| 30 | #include <net/pkt_cls.h> |
| 31 | |
Eric Dumazet | d37d8ac | 2014-03-17 20:20:49 -0700 | [diff] [blame] | 32 | #define HTSIZE 256 |
Thomas Graf | c5c13fa | 2005-04-24 20:19:54 -0700 | [diff] [blame] | 33 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 34 | struct fw_head { |
Eric Dumazet | d37d8ac | 2014-03-17 20:20:49 -0700 | [diff] [blame] | 35 | u32 mask; |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 36 | struct fw_filter __rcu *ht[HTSIZE]; |
| 37 | struct rcu_head rcu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 40 | struct fw_filter { |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 41 | struct fw_filter __rcu *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | u32 id; |
| 43 | struct tcf_result res; |
| 44 | #ifdef CONFIG_NET_CLS_IND |
WANG Cong | 2519a60 | 2014-01-09 16:14:02 -0800 | [diff] [blame] | 45 | int ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #endif /* CONFIG_NET_CLS_IND */ |
| 47 | struct tcf_exts exts; |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 48 | struct tcf_proto *tp; |
Cong Wang | e071dff | 2017-10-26 18:24:34 -0700 | [diff] [blame^] | 49 | union { |
| 50 | struct work_struct work; |
| 51 | struct rcu_head rcu; |
| 52 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
Eric Dumazet | d37d8ac | 2014-03-17 20:20:49 -0700 | [diff] [blame] | 55 | static u32 fw_hash(u32 handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | { |
Eric Dumazet | d37d8ac | 2014-03-17 20:20:49 -0700 | [diff] [blame] | 57 | handle ^= (handle >> 16); |
| 58 | handle ^= (handle >> 8); |
| 59 | return handle % HTSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Eric Dumazet | dc7f9f6 | 2011-07-05 23:25:42 +0000 | [diff] [blame] | 62 | static int fw_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 63 | struct tcf_result *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 65 | struct fw_head *head = rcu_dereference_bh(tp->root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | struct fw_filter *f; |
| 67 | int r; |
Patrick McHardy | 5c804bf | 2006-12-05 13:46:13 -0800 | [diff] [blame] | 68 | u32 id = skb->mark; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | if (head != NULL) { |
Patrick McHardy | 5c804bf | 2006-12-05 13:46:13 -0800 | [diff] [blame] | 71 | id &= head->mask; |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 72 | |
| 73 | for (f = rcu_dereference_bh(head->ht[fw_hash(id)]); f; |
| 74 | f = rcu_dereference_bh(f->next)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | if (f->id == id) { |
| 76 | *res = f->res; |
| 77 | #ifdef CONFIG_NET_CLS_IND |
WANG Cong | 2519a60 | 2014-01-09 16:14:02 -0800 | [diff] [blame] | 78 | if (!tcf_match_indev(skb, f->ifindex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | continue; |
| 80 | #endif /* CONFIG_NET_CLS_IND */ |
| 81 | r = tcf_exts_exec(skb, &f->exts, res); |
| 82 | if (r < 0) |
| 83 | continue; |
| 84 | |
| 85 | return r; |
| 86 | } |
| 87 | } |
| 88 | } else { |
WANG Cong | d8aecb1 | 2015-09-22 17:01:11 -0700 | [diff] [blame] | 89 | /* Old method: classify the packet using its skb mark. */ |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 90 | if (id && (TC_H_MAJ(id) == 0 || |
| 91 | !(TC_H_MAJ(id ^ tp->q->handle)))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | res->classid = id; |
| 93 | res->class = 0; |
| 94 | return 0; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return -1; |
| 99 | } |
| 100 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 101 | static void *fw_get(struct tcf_proto *tp, u32 handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | { |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 103 | struct fw_head *head = rtnl_dereference(tp->root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | struct fw_filter *f; |
| 105 | |
| 106 | if (head == NULL) |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 107 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 109 | f = rtnl_dereference(head->ht[fw_hash(handle)]); |
| 110 | for (; f; f = rtnl_dereference(f->next)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | if (f->id == handle) |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 112 | return f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | } |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 114 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | static int fw_init(struct tcf_proto *tp) |
| 118 | { |
WANG Cong | d8aecb1 | 2015-09-22 17:01:11 -0700 | [diff] [blame] | 119 | /* We don't allocate fw_head here, because in the old method |
| 120 | * we don't need it at all. |
| 121 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | return 0; |
| 123 | } |
| 124 | |
Cong Wang | e071dff | 2017-10-26 18:24:34 -0700 | [diff] [blame^] | 125 | static void fw_delete_filter_work(struct work_struct *work) |
| 126 | { |
| 127 | struct fw_filter *f = container_of(work, struct fw_filter, work); |
| 128 | |
| 129 | rtnl_lock(); |
| 130 | tcf_exts_destroy(&f->exts); |
| 131 | kfree(f); |
| 132 | rtnl_unlock(); |
| 133 | } |
| 134 | |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 135 | static void fw_delete_filter(struct rcu_head *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 137 | struct fw_filter *f = container_of(head, struct fw_filter, rcu); |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 138 | |
Cong Wang | e071dff | 2017-10-26 18:24:34 -0700 | [diff] [blame^] | 139 | INIT_WORK(&f->work, fw_delete_filter_work); |
| 140 | tcf_queue_work(&f->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | } |
| 142 | |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 143 | static void fw_destroy(struct tcf_proto *tp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 145 | struct fw_head *head = rtnl_dereference(tp->root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | struct fw_filter *f; |
| 147 | int h; |
| 148 | |
| 149 | if (head == NULL) |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 150 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 152 | for (h = 0; h < HTSIZE; h++) { |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 153 | while ((f = rtnl_dereference(head->ht[h])) != NULL) { |
| 154 | RCU_INIT_POINTER(head->ht[h], |
| 155 | rtnl_dereference(f->next)); |
John Fastabend | 18cdb37 | 2014-10-05 21:28:52 -0700 | [diff] [blame] | 156 | tcf_unbind_filter(tp, &f->res); |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 157 | call_rcu(&f->rcu, fw_delete_filter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
| 159 | } |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 160 | kfree_rcu(head, rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } |
| 162 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 163 | static int fw_delete(struct tcf_proto *tp, void *arg, bool *last) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | { |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 165 | struct fw_head *head = rtnl_dereference(tp->root); |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 166 | struct fw_filter *f = arg; |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 167 | struct fw_filter __rcu **fp; |
| 168 | struct fw_filter *pfp; |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 169 | int ret = -EINVAL; |
| 170 | int h; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
| 172 | if (head == NULL || f == NULL) |
| 173 | goto out; |
| 174 | |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 175 | fp = &head->ht[fw_hash(f->id)]; |
| 176 | |
| 177 | for (pfp = rtnl_dereference(*fp); pfp; |
| 178 | fp = &pfp->next, pfp = rtnl_dereference(*fp)) { |
| 179 | if (pfp == f) { |
| 180 | RCU_INIT_POINTER(*fp, rtnl_dereference(f->next)); |
John Fastabend | 18cdb37 | 2014-10-05 21:28:52 -0700 | [diff] [blame] | 181 | tcf_unbind_filter(tp, &f->res); |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 182 | call_rcu(&f->rcu, fw_delete_filter); |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 183 | ret = 0; |
| 184 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 187 | |
| 188 | *last = true; |
| 189 | for (h = 0; h < HTSIZE; h++) { |
| 190 | if (rcu_access_pointer(head->ht[h])) { |
| 191 | *last = false; |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | out: |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 197 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Patrick McHardy | 6fa8c01 | 2008-01-23 20:36:12 -0800 | [diff] [blame] | 200 | static const struct nla_policy fw_policy[TCA_FW_MAX + 1] = { |
| 201 | [TCA_FW_CLASSID] = { .type = NLA_U32 }, |
| 202 | [TCA_FW_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ }, |
| 203 | [TCA_FW_MASK] = { .type = NLA_U32 }, |
| 204 | }; |
| 205 | |
Jiri Pirko | 1e5003a | 2017-08-04 14:29:05 +0200 | [diff] [blame] | 206 | static int fw_set_parms(struct net *net, struct tcf_proto *tp, |
| 207 | struct fw_filter *f, struct nlattr **tb, |
| 208 | struct nlattr **tca, unsigned long base, bool ovr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | { |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 210 | struct fw_head *head = rtnl_dereference(tp->root); |
Patrick McHardy | b4e9b52 | 2006-08-25 16:11:42 -0700 | [diff] [blame] | 211 | u32 mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | int err; |
| 213 | |
Jiri Pirko | 94611bf | 2017-08-04 14:29:07 +0200 | [diff] [blame] | 214 | err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &f->exts, ovr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | if (err < 0) |
| 216 | return err; |
| 217 | |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 218 | if (tb[TCA_FW_CLASSID]) { |
Patrick McHardy | 1587bac | 2008-01-23 20:35:03 -0800 | [diff] [blame] | 219 | f->res.classid = nla_get_u32(tb[TCA_FW_CLASSID]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | tcf_bind_filter(tp, &f->res, base); |
| 221 | } |
| 222 | |
| 223 | #ifdef CONFIG_NET_CLS_IND |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 224 | if (tb[TCA_FW_INDEV]) { |
WANG Cong | 2519a60 | 2014-01-09 16:14:02 -0800 | [diff] [blame] | 225 | int ret; |
| 226 | ret = tcf_change_indev(net, tb[TCA_FW_INDEV]); |
Jiri Pirko | 94611bf | 2017-08-04 14:29:07 +0200 | [diff] [blame] | 227 | if (ret < 0) |
| 228 | return ret; |
WANG Cong | 2519a60 | 2014-01-09 16:14:02 -0800 | [diff] [blame] | 229 | f->ifindex = ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | } |
| 231 | #endif /* CONFIG_NET_CLS_IND */ |
| 232 | |
Wei Yongjun | cb95ec6 | 2013-04-17 16:49:10 +0000 | [diff] [blame] | 233 | err = -EINVAL; |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 234 | if (tb[TCA_FW_MASK]) { |
Patrick McHardy | 1587bac | 2008-01-23 20:35:03 -0800 | [diff] [blame] | 235 | mask = nla_get_u32(tb[TCA_FW_MASK]); |
Patrick McHardy | b4e9b52 | 2006-08-25 16:11:42 -0700 | [diff] [blame] | 236 | if (mask != head->mask) |
Jiri Pirko | 94611bf | 2017-08-04 14:29:07 +0200 | [diff] [blame] | 237 | return err; |
Patrick McHardy | b4e9b52 | 2006-08-25 16:11:42 -0700 | [diff] [blame] | 238 | } else if (head->mask != 0xFFFFFFFF) |
Jiri Pirko | 94611bf | 2017-08-04 14:29:07 +0200 | [diff] [blame] | 239 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
| 241 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 244 | static int fw_change(struct net *net, struct sk_buff *in_skb, |
Eric W. Biederman | af4c664 | 2012-05-25 13:42:45 -0600 | [diff] [blame] | 245 | struct tcf_proto *tp, unsigned long base, |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 246 | u32 handle, struct nlattr **tca, void **arg, |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 247 | bool ovr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | { |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 249 | struct fw_head *head = rtnl_dereference(tp->root); |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 250 | struct fw_filter *f = *arg; |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 251 | struct nlattr *opt = tca[TCA_OPTIONS]; |
| 252 | struct nlattr *tb[TCA_FW_MAX + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | int err; |
| 254 | |
| 255 | if (!opt) |
WANG Cong | d8aecb1 | 2015-09-22 17:01:11 -0700 | [diff] [blame] | 256 | return handle ? -EINVAL : 0; /* Succeed if it is old method. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 258 | err = nla_parse_nested(tb, TCA_FW_MAX, opt, fw_policy, NULL); |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 259 | if (err < 0) |
| 260 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 262 | if (f) { |
| 263 | struct fw_filter *pfp, *fnew; |
| 264 | struct fw_filter __rcu **fp; |
| 265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | if (f->id != handle && handle) |
| 267 | return -EINVAL; |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 268 | |
| 269 | fnew = kzalloc(sizeof(struct fw_filter), GFP_KERNEL); |
| 270 | if (!fnew) |
| 271 | return -ENOBUFS; |
| 272 | |
| 273 | fnew->id = f->id; |
| 274 | fnew->res = f->res; |
| 275 | #ifdef CONFIG_NET_CLS_IND |
| 276 | fnew->ifindex = f->ifindex; |
| 277 | #endif /* CONFIG_NET_CLS_IND */ |
| 278 | fnew->tp = f->tp; |
| 279 | |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 280 | err = tcf_exts_init(&fnew->exts, TCA_FW_ACT, TCA_FW_POLICE); |
| 281 | if (err < 0) { |
| 282 | kfree(fnew); |
| 283 | return err; |
| 284 | } |
John Fastabend | e1f93eb | 2014-09-15 23:31:42 -0700 | [diff] [blame] | 285 | |
Jiri Pirko | 1e5003a | 2017-08-04 14:29:05 +0200 | [diff] [blame] | 286 | err = fw_set_parms(net, tp, fnew, tb, tca, base, ovr); |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 287 | if (err < 0) { |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 288 | tcf_exts_destroy(&fnew->exts); |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 289 | kfree(fnew); |
| 290 | return err; |
| 291 | } |
| 292 | |
| 293 | fp = &head->ht[fw_hash(fnew->id)]; |
| 294 | for (pfp = rtnl_dereference(*fp); pfp; |
| 295 | fp = &pfp->next, pfp = rtnl_dereference(*fp)) |
| 296 | if (pfp == f) |
| 297 | break; |
| 298 | |
| 299 | RCU_INIT_POINTER(fnew->next, rtnl_dereference(pfp->next)); |
| 300 | rcu_assign_pointer(*fp, fnew); |
John Fastabend | 18cdb37 | 2014-10-05 21:28:52 -0700 | [diff] [blame] | 301 | tcf_unbind_filter(tp, &f->res); |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 302 | call_rcu(&f->rcu, fw_delete_filter); |
| 303 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 304 | *arg = fnew; |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 305 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | if (!handle) |
| 309 | return -EINVAL; |
| 310 | |
WANG Cong | d8aecb1 | 2015-09-22 17:01:11 -0700 | [diff] [blame] | 311 | if (!head) { |
| 312 | u32 mask = 0xFFFFFFFF; |
Patrick McHardy | 6fa8c01 | 2008-01-23 20:36:12 -0800 | [diff] [blame] | 313 | if (tb[TCA_FW_MASK]) |
WANG Cong | d8aecb1 | 2015-09-22 17:01:11 -0700 | [diff] [blame] | 314 | mask = nla_get_u32(tb[TCA_FW_MASK]); |
| 315 | |
| 316 | head = kzalloc(sizeof(*head), GFP_KERNEL); |
| 317 | if (!head) |
| 318 | return -ENOBUFS; |
| 319 | head->mask = mask; |
| 320 | |
| 321 | rcu_assign_pointer(tp->root, head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 324 | f = kzalloc(sizeof(struct fw_filter), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | if (f == NULL) |
| 326 | return -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 328 | err = tcf_exts_init(&f->exts, TCA_FW_ACT, TCA_FW_POLICE); |
| 329 | if (err < 0) |
| 330 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | f->id = handle; |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 332 | f->tp = tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
Jiri Pirko | 1e5003a | 2017-08-04 14:29:05 +0200 | [diff] [blame] | 334 | err = fw_set_parms(net, tp, f, tb, tca, base, ovr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | if (err < 0) |
| 336 | goto errout; |
| 337 | |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 338 | RCU_INIT_POINTER(f->next, head->ht[fw_hash(handle)]); |
| 339 | rcu_assign_pointer(head->ht[fw_hash(handle)], f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 341 | *arg = f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | return 0; |
| 343 | |
| 344 | errout: |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 345 | tcf_exts_destroy(&f->exts); |
Jesper Juhl | a51482b | 2005-11-08 09:41:34 -0800 | [diff] [blame] | 346 | kfree(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | return err; |
| 348 | } |
| 349 | |
| 350 | static void fw_walk(struct tcf_proto *tp, struct tcf_walker *arg) |
| 351 | { |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 352 | struct fw_head *head = rtnl_dereference(tp->root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | int h; |
| 354 | |
| 355 | if (head == NULL) |
| 356 | arg->stop = 1; |
| 357 | |
| 358 | if (arg->stop) |
| 359 | return; |
| 360 | |
Thomas Graf | c5c13fa | 2005-04-24 20:19:54 -0700 | [diff] [blame] | 361 | for (h = 0; h < HTSIZE; h++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | struct fw_filter *f; |
| 363 | |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 364 | for (f = rtnl_dereference(head->ht[h]); f; |
| 365 | f = rtnl_dereference(f->next)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | if (arg->count < arg->skip) { |
| 367 | arg->count++; |
| 368 | continue; |
| 369 | } |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 370 | if (arg->fn(tp, f, arg) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | arg->stop = 1; |
| 372 | return; |
| 373 | } |
| 374 | arg->count++; |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 379 | static int fw_dump(struct net *net, struct tcf_proto *tp, void *fh, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | struct sk_buff *skb, struct tcmsg *t) |
| 381 | { |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 382 | struct fw_head *head = rtnl_dereference(tp->root); |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 383 | struct fw_filter *f = fh; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 384 | struct nlattr *nest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | |
| 386 | if (f == NULL) |
| 387 | return skb->len; |
| 388 | |
| 389 | t->tcm_handle = f->id; |
| 390 | |
Jiri Pirko | 6fc6d06 | 2017-08-04 14:29:00 +0200 | [diff] [blame] | 391 | if (!f->res.classid && !tcf_exts_has_actions(&f->exts)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | return skb->len; |
| 393 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 394 | nest = nla_nest_start(skb, TCA_OPTIONS); |
| 395 | if (nest == NULL) |
| 396 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 398 | if (f->res.classid && |
| 399 | nla_put_u32(skb, TCA_FW_CLASSID, f->res.classid)) |
| 400 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | #ifdef CONFIG_NET_CLS_IND |
WANG Cong | 2519a60 | 2014-01-09 16:14:02 -0800 | [diff] [blame] | 402 | if (f->ifindex) { |
| 403 | struct net_device *dev; |
| 404 | dev = __dev_get_by_index(net, f->ifindex); |
| 405 | if (dev && nla_put_string(skb, TCA_FW_INDEV, dev->name)) |
| 406 | goto nla_put_failure; |
| 407 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | #endif /* CONFIG_NET_CLS_IND */ |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 409 | if (head->mask != 0xFFFFFFFF && |
| 410 | nla_put_u32(skb, TCA_FW_MASK, head->mask)) |
| 411 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 413 | if (tcf_exts_dump(skb, &f->exts) < 0) |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 414 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 416 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 418 | if (tcf_exts_dump_stats(skb, &f->exts) < 0) |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 419 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
| 421 | return skb->len; |
| 422 | |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 423 | nla_put_failure: |
Jiri Pirko | 6ea3b44 | 2014-12-09 22:23:29 +0100 | [diff] [blame] | 424 | nla_nest_cancel(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | return -1; |
| 426 | } |
| 427 | |
Cong Wang | 07d79fc | 2017-08-30 14:30:36 -0700 | [diff] [blame] | 428 | static void fw_bind_class(void *fh, u32 classid, unsigned long cl) |
| 429 | { |
| 430 | struct fw_filter *f = fh; |
| 431 | |
| 432 | if (f && f->res.classid == classid) |
| 433 | f->res.class = cl; |
| 434 | } |
| 435 | |
Patrick McHardy | 2eb9d75 | 2008-01-22 22:10:42 -0800 | [diff] [blame] | 436 | static struct tcf_proto_ops cls_fw_ops __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | .kind = "fw", |
| 438 | .classify = fw_classify, |
| 439 | .init = fw_init, |
| 440 | .destroy = fw_destroy, |
| 441 | .get = fw_get, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | .change = fw_change, |
| 443 | .delete = fw_delete, |
| 444 | .walk = fw_walk, |
| 445 | .dump = fw_dump, |
Cong Wang | 07d79fc | 2017-08-30 14:30:36 -0700 | [diff] [blame] | 446 | .bind_class = fw_bind_class, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | .owner = THIS_MODULE, |
| 448 | }; |
| 449 | |
| 450 | static int __init init_fw(void) |
| 451 | { |
| 452 | return register_tcf_proto_ops(&cls_fw_ops); |
| 453 | } |
| 454 | |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 455 | static void __exit exit_fw(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | { |
| 457 | unregister_tcf_proto_ops(&cls_fw_ops); |
| 458 | } |
| 459 | |
| 460 | module_init(init_fw) |
| 461 | module_exit(exit_fw) |
| 462 | MODULE_LICENSE("GPL"); |