Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * net/sched/act_api.c Packet action API. |
| 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 | * Author: Jamal Hadi Salim |
| 10 | * |
| 11 | * |
| 12 | */ |
| 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/types.h> |
| 15 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/errno.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/init.h> |
| 21 | #include <linux/kmod.h> |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 22 | #include <linux/err.h> |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 23 | #include <linux/module.h> |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 24 | #include <net/net_namespace.h> |
| 25 | #include <net/sock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <net/sch_generic.h> |
| 27 | #include <net/act_api.h> |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 28 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 30 | static void free_tcf(struct rcu_head *head) |
| 31 | { |
| 32 | struct tcf_common *p = container_of(head, struct tcf_common, tcfc_rcu); |
| 33 | |
| 34 | free_percpu(p->cpu_bstats); |
| 35 | free_percpu(p->cpu_qstats); |
| 36 | kfree(p); |
| 37 | } |
| 38 | |
Alexei Starovoitov | 3c64562 | 2015-08-25 20:06:31 -0700 | [diff] [blame] | 39 | static void tcf_hash_destroy(struct tc_action *a) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 40 | { |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 41 | struct tcf_common *p = a->priv; |
| 42 | struct tcf_hashinfo *hinfo = a->ops->hinfo; |
| 43 | |
WANG Cong | 89819dc | 2013-12-15 20:15:09 -0800 | [diff] [blame] | 44 | spin_lock_bh(&hinfo->lock); |
| 45 | hlist_del(&p->tcfc_head); |
| 46 | spin_unlock_bh(&hinfo->lock); |
| 47 | gen_kill_estimator(&p->tcfc_bstats, |
| 48 | &p->tcfc_rate_est); |
| 49 | /* |
| 50 | * gen_estimator est_timer() might access p->tcfc_lock |
| 51 | * or bstats, wait a RCU grace period before freeing p |
| 52 | */ |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 53 | call_rcu(&p->tcfc_rcu, free_tcf); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 54 | } |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 55 | |
Daniel Borkmann | 28e6b67 | 2015-07-29 23:35:25 +0200 | [diff] [blame] | 56 | int __tcf_hash_release(struct tc_action *a, bool bind, bool strict) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 57 | { |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 58 | struct tcf_common *p = a->priv; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 59 | int ret = 0; |
| 60 | |
| 61 | if (p) { |
| 62 | if (bind) |
| 63 | p->tcfc_bindcnt--; |
Daniel Borkmann | 28e6b67 | 2015-07-29 23:35:25 +0200 | [diff] [blame] | 64 | else if (strict && p->tcfc_bindcnt > 0) |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 65 | return -EPERM; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 66 | |
| 67 | p->tcfc_refcnt--; |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 68 | if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) { |
WANG Cong | a5b5c95 | 2014-02-11 17:07:32 -0800 | [diff] [blame] | 69 | if (a->ops->cleanup) |
| 70 | a->ops->cleanup(a, bind); |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 71 | tcf_hash_destroy(a); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 72 | ret = 1; |
| 73 | } |
| 74 | } |
Daniel Borkmann | 28e6b67 | 2015-07-29 23:35:25 +0200 | [diff] [blame] | 75 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 76 | return ret; |
| 77 | } |
Daniel Borkmann | 28e6b67 | 2015-07-29 23:35:25 +0200 | [diff] [blame] | 78 | EXPORT_SYMBOL(__tcf_hash_release); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 79 | |
| 80 | static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb, |
WANG Cong | c779f7a | 2014-01-17 11:37:02 -0800 | [diff] [blame] | 81 | struct tc_action *a) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 82 | { |
WANG Cong | c779f7a | 2014-01-17 11:37:02 -0800 | [diff] [blame] | 83 | struct tcf_hashinfo *hinfo = a->ops->hinfo; |
WANG Cong | 89819dc | 2013-12-15 20:15:09 -0800 | [diff] [blame] | 84 | struct hlist_head *head; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 85 | struct tcf_common *p; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 86 | int err = 0, index = -1, i = 0, s_i = 0, n_i = 0; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 87 | struct nlattr *nest; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 88 | |
WANG Cong | 89819dc | 2013-12-15 20:15:09 -0800 | [diff] [blame] | 89 | spin_lock_bh(&hinfo->lock); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 90 | |
| 91 | s_i = cb->args[0]; |
| 92 | |
| 93 | for (i = 0; i < (hinfo->hmask + 1); i++) { |
WANG Cong | 89819dc | 2013-12-15 20:15:09 -0800 | [diff] [blame] | 94 | head = &hinfo->htab[tcf_hash(i, hinfo->hmask)]; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 95 | |
WANG Cong | 89819dc | 2013-12-15 20:15:09 -0800 | [diff] [blame] | 96 | hlist_for_each_entry_rcu(p, head, tcfc_head) { |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 97 | index++; |
| 98 | if (index < s_i) |
| 99 | continue; |
| 100 | a->priv = p; |
| 101 | a->order = n_i; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 102 | |
| 103 | nest = nla_nest_start(skb, a->order); |
| 104 | if (nest == NULL) |
| 105 | goto nla_put_failure; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 106 | err = tcf_action_dump_1(skb, a, 0, 0); |
| 107 | if (err < 0) { |
| 108 | index--; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 109 | nlmsg_trim(skb, nest); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 110 | goto done; |
| 111 | } |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 112 | nla_nest_end(skb, nest); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 113 | n_i++; |
| 114 | if (n_i >= TCA_ACT_MAX_PRIO) |
| 115 | goto done; |
| 116 | } |
| 117 | } |
| 118 | done: |
WANG Cong | 89819dc | 2013-12-15 20:15:09 -0800 | [diff] [blame] | 119 | spin_unlock_bh(&hinfo->lock); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 120 | if (n_i) |
| 121 | cb->args[0] += n_i; |
| 122 | return n_i; |
| 123 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 124 | nla_put_failure: |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 125 | nla_nest_cancel(skb, nest); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 126 | goto done; |
| 127 | } |
| 128 | |
WANG Cong | c779f7a | 2014-01-17 11:37:02 -0800 | [diff] [blame] | 129 | static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 130 | { |
WANG Cong | c779f7a | 2014-01-17 11:37:02 -0800 | [diff] [blame] | 131 | struct tcf_hashinfo *hinfo = a->ops->hinfo; |
WANG Cong | 89819dc | 2013-12-15 20:15:09 -0800 | [diff] [blame] | 132 | struct hlist_head *head; |
| 133 | struct hlist_node *n; |
| 134 | struct tcf_common *p; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 135 | struct nlattr *nest; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 136 | int i = 0, n_i = 0; |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 137 | int ret = -EINVAL; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 138 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 139 | nest = nla_nest_start(skb, a->order); |
| 140 | if (nest == NULL) |
| 141 | goto nla_put_failure; |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 142 | if (nla_put_string(skb, TCA_KIND, a->ops->kind)) |
| 143 | goto nla_put_failure; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 144 | for (i = 0; i < (hinfo->hmask + 1); i++) { |
WANG Cong | 89819dc | 2013-12-15 20:15:09 -0800 | [diff] [blame] | 145 | head = &hinfo->htab[tcf_hash(i, hinfo->hmask)]; |
| 146 | hlist_for_each_entry_safe(p, n, head, tcfc_head) { |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 147 | a->priv = p; |
Daniel Borkmann | 28e6b67 | 2015-07-29 23:35:25 +0200 | [diff] [blame] | 148 | ret = __tcf_hash_release(a, false, true); |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 149 | if (ret == ACT_P_DELETED) { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 150 | module_put(a->ops->owner); |
Jamal Hadi Salim | 805c1f4 | 2013-12-23 08:02:13 -0500 | [diff] [blame] | 151 | n_i++; |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 152 | } else if (ret < 0) |
| 153 | goto nla_put_failure; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 156 | if (nla_put_u32(skb, TCA_FCNT, n_i)) |
| 157 | goto nla_put_failure; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 158 | nla_nest_end(skb, nest); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 159 | |
| 160 | return n_i; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 161 | nla_put_failure: |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 162 | nla_nest_cancel(skb, nest); |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 163 | return ret; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 164 | } |
| 165 | |
stephen hemminger | 9c75f40 | 2013-12-31 11:54:00 -0800 | [diff] [blame] | 166 | static int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb, |
| 167 | int type, struct tc_action *a) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 168 | { |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 169 | if (type == RTM_DELACTION) { |
WANG Cong | c779f7a | 2014-01-17 11:37:02 -0800 | [diff] [blame] | 170 | return tcf_del_walker(skb, a); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 171 | } else if (type == RTM_GETACTION) { |
WANG Cong | c779f7a | 2014-01-17 11:37:02 -0800 | [diff] [blame] | 172 | return tcf_dump_walker(skb, cb, a); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 173 | } else { |
stephen hemminger | 6ff9c36 | 2010-05-12 06:37:05 +0000 | [diff] [blame] | 174 | WARN(1, "tcf_generic_walker: unknown action %d\n", type); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 175 | return -EINVAL; |
| 176 | } |
| 177 | } |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 178 | |
WANG Cong | 6e6a50c | 2014-01-17 11:37:03 -0800 | [diff] [blame] | 179 | static struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 180 | { |
WANG Cong | 89819dc | 2013-12-15 20:15:09 -0800 | [diff] [blame] | 181 | struct tcf_common *p = NULL; |
| 182 | struct hlist_head *head; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 183 | |
WANG Cong | 89819dc | 2013-12-15 20:15:09 -0800 | [diff] [blame] | 184 | spin_lock_bh(&hinfo->lock); |
| 185 | head = &hinfo->htab[tcf_hash(index, hinfo->hmask)]; |
| 186 | hlist_for_each_entry_rcu(p, head, tcfc_head) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 187 | if (p->tcfc_index == index) |
| 188 | break; |
WANG Cong | 89819dc | 2013-12-15 20:15:09 -0800 | [diff] [blame] | 189 | spin_unlock_bh(&hinfo->lock); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 190 | |
| 191 | return p; |
| 192 | } |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 193 | |
WANG Cong | ddafd34 | 2014-01-09 16:13:59 -0800 | [diff] [blame] | 194 | u32 tcf_hash_new_index(struct tcf_hashinfo *hinfo) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 195 | { |
WANG Cong | ddafd34 | 2014-01-09 16:13:59 -0800 | [diff] [blame] | 196 | u32 val = hinfo->index; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 197 | |
| 198 | do { |
| 199 | if (++val == 0) |
| 200 | val = 1; |
| 201 | } while (tcf_hash_lookup(val, hinfo)); |
| 202 | |
WANG Cong | ddafd34 | 2014-01-09 16:13:59 -0800 | [diff] [blame] | 203 | hinfo->index = val; |
Yang Yingliang | 17569fa | 2013-12-10 20:55:29 +0800 | [diff] [blame] | 204 | return val; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 205 | } |
| 206 | EXPORT_SYMBOL(tcf_hash_new_index); |
| 207 | |
WANG Cong | 6e6a50c | 2014-01-17 11:37:03 -0800 | [diff] [blame] | 208 | int tcf_hash_search(struct tc_action *a, u32 index) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 209 | { |
| 210 | struct tcf_hashinfo *hinfo = a->ops->hinfo; |
| 211 | struct tcf_common *p = tcf_hash_lookup(index, hinfo); |
| 212 | |
| 213 | if (p) { |
| 214 | a->priv = p; |
| 215 | return 1; |
| 216 | } |
| 217 | return 0; |
| 218 | } |
WANG Cong | 6e6a50c | 2014-01-17 11:37:03 -0800 | [diff] [blame] | 219 | EXPORT_SYMBOL(tcf_hash_search); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 220 | |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 221 | int tcf_hash_check(u32 index, struct tc_action *a, int bind) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 222 | { |
WANG Cong | c779f7a | 2014-01-17 11:37:02 -0800 | [diff] [blame] | 223 | struct tcf_hashinfo *hinfo = a->ops->hinfo; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 224 | struct tcf_common *p = NULL; |
| 225 | if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) { |
Jamal Hadi Salim | 76aab2c | 2008-08-07 20:37:22 -0700 | [diff] [blame] | 226 | if (bind) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 227 | p->tcfc_bindcnt++; |
Jamal Hadi Salim | 76aab2c | 2008-08-07 20:37:22 -0700 | [diff] [blame] | 228 | p->tcfc_refcnt++; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 229 | a->priv = p; |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 230 | return 1; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 231 | } |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 232 | return 0; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 233 | } |
| 234 | EXPORT_SYMBOL(tcf_hash_check); |
| 235 | |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 236 | void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est) |
| 237 | { |
| 238 | struct tcf_common *pc = a->priv; |
| 239 | if (est) |
| 240 | gen_kill_estimator(&pc->tcfc_bstats, |
| 241 | &pc->tcfc_rate_est); |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 242 | call_rcu(&pc->tcfc_rcu, free_tcf); |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 243 | } |
| 244 | EXPORT_SYMBOL(tcf_hash_cleanup); |
| 245 | |
| 246 | int tcf_hash_create(u32 index, struct nlattr *est, struct tc_action *a, |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 247 | int size, int bind, bool cpustats) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 248 | { |
WANG Cong | c779f7a | 2014-01-17 11:37:02 -0800 | [diff] [blame] | 249 | struct tcf_hashinfo *hinfo = a->ops->hinfo; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 250 | struct tcf_common *p = kzalloc(size, GFP_KERNEL); |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 251 | int err = -ENOMEM; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 252 | |
| 253 | if (unlikely(!p)) |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 254 | return -ENOMEM; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 255 | p->tcfc_refcnt = 1; |
| 256 | if (bind) |
| 257 | p->tcfc_bindcnt = 1; |
| 258 | |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 259 | if (cpustats) { |
| 260 | p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu); |
| 261 | if (!p->cpu_bstats) { |
| 262 | err1: |
| 263 | kfree(p); |
| 264 | return err; |
| 265 | } |
| 266 | p->cpu_qstats = alloc_percpu(struct gnet_stats_queue); |
| 267 | if (!p->cpu_qstats) { |
| 268 | err2: |
| 269 | free_percpu(p->cpu_bstats); |
| 270 | goto err1; |
| 271 | } |
| 272 | } |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 273 | spin_lock_init(&p->tcfc_lock); |
WANG Cong | 89819dc | 2013-12-15 20:15:09 -0800 | [diff] [blame] | 274 | INIT_HLIST_NODE(&p->tcfc_head); |
WANG Cong | ddafd34 | 2014-01-09 16:13:59 -0800 | [diff] [blame] | 275 | p->tcfc_index = index ? index : tcf_hash_new_index(hinfo); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 276 | p->tcfc_tm.install = jiffies; |
| 277 | p->tcfc_tm.lastuse = jiffies; |
Stephen Hemminger | 0e991ec | 2008-11-25 21:12:32 -0800 | [diff] [blame] | 278 | if (est) { |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 279 | err = gen_new_estimator(&p->tcfc_bstats, p->cpu_bstats, |
| 280 | &p->tcfc_rate_est, |
| 281 | &p->tcfc_lock, est); |
Stephen Hemminger | 0e991ec | 2008-11-25 21:12:32 -0800 | [diff] [blame] | 282 | if (err) { |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 283 | free_percpu(p->cpu_qstats); |
| 284 | goto err2; |
Stephen Hemminger | 0e991ec | 2008-11-25 21:12:32 -0800 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 288 | a->priv = (void *) p; |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 289 | return 0; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 290 | } |
| 291 | EXPORT_SYMBOL(tcf_hash_create); |
| 292 | |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 293 | void tcf_hash_insert(struct tc_action *a) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 294 | { |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 295 | struct tcf_common *p = a->priv; |
| 296 | struct tcf_hashinfo *hinfo = a->ops->hinfo; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 297 | unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask); |
| 298 | |
WANG Cong | 89819dc | 2013-12-15 20:15:09 -0800 | [diff] [blame] | 299 | spin_lock_bh(&hinfo->lock); |
| 300 | hlist_add_head(&p->tcfc_head, &hinfo->htab[h]); |
| 301 | spin_unlock_bh(&hinfo->lock); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 302 | } |
| 303 | EXPORT_SYMBOL(tcf_hash_insert); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame] | 305 | static LIST_HEAD(act_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | static DEFINE_RWLOCK(act_mod_lock); |
| 307 | |
WANG Cong | 4f1e9d8 | 2014-02-11 17:07:33 -0800 | [diff] [blame] | 308 | int tcf_register_action(struct tc_action_ops *act, unsigned int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | { |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame] | 310 | struct tc_action_ops *a; |
WANG Cong | 4f1e9d8 | 2014-02-11 17:07:33 -0800 | [diff] [blame] | 311 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | |
WANG Cong | a5b5c95 | 2014-02-11 17:07:32 -0800 | [diff] [blame] | 313 | /* Must supply act, dump and init */ |
| 314 | if (!act->act || !act->dump || !act->init) |
Jamal Hadi Salim | 76c82d7 | 2013-12-04 09:26:52 -0500 | [diff] [blame] | 315 | return -EINVAL; |
| 316 | |
Jamal Hadi Salim | 382ca8a | 2013-12-04 09:26:55 -0500 | [diff] [blame] | 317 | /* Supply defaults */ |
Jamal Hadi Salim | 63ef617 | 2013-12-04 09:26:53 -0500 | [diff] [blame] | 318 | if (!act->lookup) |
| 319 | act->lookup = tcf_hash_search; |
Jamal Hadi Salim | 382ca8a | 2013-12-04 09:26:55 -0500 | [diff] [blame] | 320 | if (!act->walk) |
| 321 | act->walk = tcf_generic_walker; |
Jamal Hadi Salim | 63ef617 | 2013-12-04 09:26:53 -0500 | [diff] [blame] | 322 | |
WANG Cong | 4f1e9d8 | 2014-02-11 17:07:33 -0800 | [diff] [blame] | 323 | act->hinfo = kmalloc(sizeof(struct tcf_hashinfo), GFP_KERNEL); |
| 324 | if (!act->hinfo) |
| 325 | return -ENOMEM; |
| 326 | err = tcf_hashinfo_init(act->hinfo, mask); |
| 327 | if (err) { |
| 328 | kfree(act->hinfo); |
| 329 | return err; |
| 330 | } |
| 331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | write_lock(&act_mod_lock); |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame] | 333 | list_for_each_entry(a, &act_base, head) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) { |
| 335 | write_unlock(&act_mod_lock); |
WANG Cong | 4f1e9d8 | 2014-02-11 17:07:33 -0800 | [diff] [blame] | 336 | tcf_hashinfo_destroy(act->hinfo); |
| 337 | kfree(act->hinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | return -EEXIST; |
| 339 | } |
| 340 | } |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame] | 341 | list_add_tail(&act->head, &act_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | write_unlock(&act_mod_lock); |
| 343 | return 0; |
| 344 | } |
Patrick McHardy | 62e3ba1 | 2008-01-22 22:10:23 -0800 | [diff] [blame] | 345 | EXPORT_SYMBOL(tcf_register_action); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
| 347 | int tcf_unregister_action(struct tc_action_ops *act) |
| 348 | { |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame] | 349 | struct tc_action_ops *a; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | int err = -ENOENT; |
| 351 | |
| 352 | write_lock(&act_mod_lock); |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 353 | list_for_each_entry(a, &act_base, head) { |
| 354 | if (a == act) { |
| 355 | list_del(&act->head); |
WANG Cong | 4f1e9d8 | 2014-02-11 17:07:33 -0800 | [diff] [blame] | 356 | tcf_hashinfo_destroy(act->hinfo); |
| 357 | kfree(act->hinfo); |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 358 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | break; |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 360 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | } |
| 362 | write_unlock(&act_mod_lock); |
| 363 | return err; |
| 364 | } |
Patrick McHardy | 62e3ba1 | 2008-01-22 22:10:23 -0800 | [diff] [blame] | 365 | EXPORT_SYMBOL(tcf_unregister_action); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | |
| 367 | /* lookup by name */ |
| 368 | static struct tc_action_ops *tc_lookup_action_n(char *kind) |
| 369 | { |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 370 | struct tc_action_ops *a, *res = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
| 372 | if (kind) { |
| 373 | read_lock(&act_mod_lock); |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame] | 374 | list_for_each_entry(a, &act_base, head) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | if (strcmp(kind, a->kind) == 0) { |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 376 | if (try_module_get(a->owner)) |
| 377 | res = a; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | break; |
| 379 | } |
| 380 | } |
| 381 | read_unlock(&act_mod_lock); |
| 382 | } |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 383 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 386 | /* lookup by nlattr */ |
| 387 | static struct tc_action_ops *tc_lookup_action(struct nlattr *kind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | { |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 389 | struct tc_action_ops *a, *res = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
| 391 | if (kind) { |
| 392 | read_lock(&act_mod_lock); |
WANG Cong | 1f747c2 | 2013-12-15 20:15:10 -0800 | [diff] [blame] | 393 | list_for_each_entry(a, &act_base, head) { |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 394 | if (nla_strcmp(kind, a->kind) == 0) { |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 395 | if (try_module_get(a->owner)) |
| 396 | res = a; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | break; |
| 398 | } |
| 399 | } |
| 400 | read_unlock(&act_mod_lock); |
| 401 | } |
Eric Dumazet | a792866 | 2013-12-20 12:32:32 -0800 | [diff] [blame] | 402 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | } |
| 404 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 405 | int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions, |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 406 | struct tcf_result *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | { |
Eric Dumazet | dc7f9f6 | 2011-07-05 23:25:42 +0000 | [diff] [blame] | 408 | const struct tc_action *a; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | int ret = -1; |
| 410 | |
| 411 | if (skb->tc_verd & TC_NCLS) { |
| 412 | skb->tc_verd = CLR_TC_NCLS(skb->tc_verd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | ret = TC_ACT_OK; |
| 414 | goto exec_done; |
| 415 | } |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 416 | list_for_each_entry(a, actions, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | repeat: |
Jamal Hadi Salim | 63acd68 | 2013-12-23 08:02:12 -0500 | [diff] [blame] | 418 | ret = a->ops->act(skb, a, res); |
Jamal Hadi Salim | 63acd68 | 2013-12-23 08:02:12 -0500 | [diff] [blame] | 419 | if (ret == TC_ACT_REPEAT) |
| 420 | goto repeat; /* we need a ttl - JHS */ |
| 421 | if (ret != TC_ACT_PIPE) |
| 422 | goto exec_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | } |
| 424 | exec_done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | return ret; |
| 426 | } |
Patrick McHardy | 62e3ba1 | 2008-01-22 22:10:23 -0800 | [diff] [blame] | 427 | EXPORT_SYMBOL(tcf_action_exec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 429 | int tcf_action_destroy(struct list_head *actions, int bind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | { |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 431 | struct tc_action *a, *tmp; |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 432 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 434 | list_for_each_entry_safe(a, tmp, actions, list) { |
Daniel Borkmann | 28e6b67 | 2015-07-29 23:35:25 +0200 | [diff] [blame] | 435 | ret = __tcf_hash_release(a, bind, true); |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 436 | if (ret == ACT_P_DELETED) |
Jamal Hadi Salim | 63acd68 | 2013-12-23 08:02:12 -0500 | [diff] [blame] | 437 | module_put(a->ops->owner); |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 438 | else if (ret < 0) |
| 439 | return ret; |
Jamal Hadi Salim | 63acd68 | 2013-12-23 08:02:12 -0500 | [diff] [blame] | 440 | list_del(&a->list); |
| 441 | kfree(a); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | } |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 443 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | int |
| 447 | tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref) |
| 448 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | return a->ops->dump(skb, a, bind, ref); |
| 450 | } |
| 451 | |
| 452 | int |
| 453 | tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref) |
| 454 | { |
| 455 | int err = -EINVAL; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 456 | unsigned char *b = skb_tail_pointer(skb); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 457 | struct nlattr *nest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 459 | if (nla_put_string(skb, TCA_KIND, a->ops->kind)) |
| 460 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | if (tcf_action_copy_stats(skb, a, 0)) |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 462 | goto nla_put_failure; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 463 | nest = nla_nest_start(skb, TCA_OPTIONS); |
| 464 | if (nest == NULL) |
| 465 | goto nla_put_failure; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 466 | err = tcf_action_dump_old(skb, a, bind, ref); |
| 467 | if (err > 0) { |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 468 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | return err; |
| 470 | } |
| 471 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 472 | nla_put_failure: |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 473 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | return -1; |
| 475 | } |
Patrick McHardy | 62e3ba1 | 2008-01-22 22:10:23 -0800 | [diff] [blame] | 476 | EXPORT_SYMBOL(tcf_action_dump_1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | |
| 478 | int |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 479 | tcf_action_dump(struct sk_buff *skb, struct list_head *actions, int bind, int ref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | { |
| 481 | struct tc_action *a; |
| 482 | int err = -EINVAL; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 483 | struct nlattr *nest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 485 | list_for_each_entry(a, actions, list) { |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 486 | nest = nla_nest_start(skb, a->order); |
| 487 | if (nest == NULL) |
| 488 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | err = tcf_action_dump_1(skb, a, bind, ref); |
| 490 | if (err < 0) |
Thomas Graf | 4fe683f | 2006-07-05 20:47:28 -0700 | [diff] [blame] | 491 | goto errout; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 492 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | return 0; |
| 496 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 497 | nla_put_failure: |
Thomas Graf | 4fe683f | 2006-07-05 20:47:28 -0700 | [diff] [blame] | 498 | err = -EINVAL; |
| 499 | errout: |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 500 | nla_nest_cancel(skb, nest); |
Thomas Graf | 4fe683f | 2006-07-05 20:47:28 -0700 | [diff] [blame] | 501 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 504 | struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla, |
| 505 | struct nlattr *est, char *name, int ovr, |
| 506 | int bind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | { |
| 508 | struct tc_action *a; |
| 509 | struct tc_action_ops *a_o; |
| 510 | char act_name[IFNAMSIZ]; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 511 | struct nlattr *tb[TCA_ACT_MAX + 1]; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 512 | struct nlattr *kind; |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 513 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | if (name == NULL) { |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 516 | err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL); |
| 517 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | goto err_out; |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 519 | err = -EINVAL; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 520 | kind = tb[TCA_ACT_KIND]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | if (kind == NULL) |
| 522 | goto err_out; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 523 | if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | goto err_out; |
| 525 | } else { |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 526 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ) |
| 528 | goto err_out; |
| 529 | } |
| 530 | |
| 531 | a_o = tc_lookup_action_n(act_name); |
| 532 | if (a_o == NULL) { |
Johannes Berg | 95a5afc | 2008-10-16 15:24:51 -0700 | [diff] [blame] | 533 | #ifdef CONFIG_MODULES |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | rtnl_unlock(); |
Patrick McHardy | 4bba392 | 2006-01-08 22:22:14 -0800 | [diff] [blame] | 535 | request_module("act_%s", act_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | rtnl_lock(); |
| 537 | |
| 538 | a_o = tc_lookup_action_n(act_name); |
| 539 | |
| 540 | /* We dropped the RTNL semaphore in order to |
| 541 | * perform the module load. So, even if we |
| 542 | * succeeded in loading the module we have to |
| 543 | * tell the caller to replay the request. We |
| 544 | * indicate this using -EAGAIN. |
| 545 | */ |
| 546 | if (a_o != NULL) { |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 547 | err = -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | goto err_mod; |
| 549 | } |
| 550 | #endif |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 551 | err = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | goto err_out; |
| 553 | } |
| 554 | |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 555 | err = -ENOMEM; |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 556 | a = kzalloc(sizeof(*a), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | if (a == NULL) |
| 558 | goto err_mod; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | |
WANG Cong | c779f7a | 2014-01-17 11:37:02 -0800 | [diff] [blame] | 560 | a->ops = a_o; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 561 | INIT_LIST_HEAD(&a->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | /* backward compatibility for policer */ |
| 563 | if (name == NULL) |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 564 | err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, a, ovr, bind); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | else |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 566 | err = a_o->init(net, nla, est, a, ovr, bind); |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 567 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | goto err_free; |
| 569 | |
| 570 | /* module count goes up only when brand new policy is created |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 571 | * if it exists and is only bound to in a_o->init() then |
| 572 | * ACT_P_CREATED is not returned (a zero is). |
| 573 | */ |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 574 | if (err != ACT_P_CREATED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | module_put(a_o->owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | return a; |
| 578 | |
| 579 | err_free: |
| 580 | kfree(a); |
| 581 | err_mod: |
| 582 | module_put(a_o->owner); |
| 583 | err_out: |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 584 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | } |
| 586 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 587 | int tcf_action_init(struct net *net, struct nlattr *nla, |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 588 | struct nlattr *est, char *name, int ovr, |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 589 | int bind, struct list_head *actions) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 591 | struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 592 | struct tc_action *act; |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 593 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | int i; |
| 595 | |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 596 | err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL); |
| 597 | if (err < 0) |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 598 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 600 | for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) { |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 601 | act = tcf_action_init_1(net, tb[i], est, name, ovr, bind); |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 602 | if (IS_ERR(act)) { |
| 603 | err = PTR_ERR(act); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | goto err; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 605 | } |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 606 | act->order = i; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 607 | list_add_tail(&act->list, actions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | } |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 609 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | |
| 611 | err: |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 612 | tcf_action_destroy(actions, bind); |
| 613 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a, |
| 617 | int compat_mode) |
| 618 | { |
| 619 | int err = 0; |
| 620 | struct gnet_dump d; |
WANG Cong | 7eb8896 | 2014-01-09 16:14:05 -0800 | [diff] [blame] | 621 | struct tcf_common *p = a->priv; |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 622 | |
WANG Cong | 7eb8896 | 2014-01-09 16:14:05 -0800 | [diff] [blame] | 623 | if (p == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | goto errout; |
| 625 | |
| 626 | /* compat_mode being true specifies a call that is supposed |
Dirk Hohndel | 06fe9fb | 2009-09-28 21:43:57 -0400 | [diff] [blame] | 627 | * to add additional backward compatibility statistic TLVs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | */ |
| 629 | if (compat_mode) { |
| 630 | if (a->type == TCA_OLD_COMPAT) |
| 631 | err = gnet_stats_start_copy_compat(skb, 0, |
WANG Cong | 7eb8896 | 2014-01-09 16:14:05 -0800 | [diff] [blame] | 632 | TCA_STATS, TCA_XSTATS, &p->tcfc_lock, &d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | else |
| 634 | return 0; |
| 635 | } else |
| 636 | err = gnet_stats_start_copy(skb, TCA_ACT_STATS, |
WANG Cong | 7eb8896 | 2014-01-09 16:14:05 -0800 | [diff] [blame] | 637 | &p->tcfc_lock, &d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
| 639 | if (err < 0) |
| 640 | goto errout; |
| 641 | |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 642 | if (gnet_stats_copy_basic(&d, p->cpu_bstats, &p->tcfc_bstats) < 0 || |
WANG Cong | 7eb8896 | 2014-01-09 16:14:05 -0800 | [diff] [blame] | 643 | gnet_stats_copy_rate_est(&d, &p->tcfc_bstats, |
| 644 | &p->tcfc_rate_est) < 0 || |
Eric Dumazet | 519c818 | 2015-07-06 05:18:04 -0700 | [diff] [blame] | 645 | gnet_stats_copy_queue(&d, p->cpu_qstats, |
John Fastabend | 6401585 | 2014-09-28 11:53:57 -0700 | [diff] [blame] | 646 | &p->tcfc_qstats, |
| 647 | p->tcfc_qstats.qlen) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | goto errout; |
| 649 | |
| 650 | if (gnet_stats_finish_copy(&d) < 0) |
| 651 | goto errout; |
| 652 | |
| 653 | return 0; |
| 654 | |
| 655 | errout: |
| 656 | return -1; |
| 657 | } |
| 658 | |
| 659 | static int |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 660 | tca_get_fill(struct sk_buff *skb, struct list_head *actions, u32 portid, u32 seq, |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 661 | u16 flags, int event, int bind, int ref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | { |
| 663 | struct tcamsg *t; |
| 664 | struct nlmsghdr *nlh; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 665 | unsigned char *b = skb_tail_pointer(skb); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 666 | struct nlattr *nest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 668 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags); |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 669 | if (!nlh) |
| 670 | goto out_nlmsg_trim; |
| 671 | t = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | t->tca_family = AF_UNSPEC; |
Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 673 | t->tca__pad1 = 0; |
| 674 | t->tca__pad2 = 0; |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 675 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 676 | nest = nla_nest_start(skb, TCA_ACT_TAB); |
| 677 | if (nest == NULL) |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 678 | goto out_nlmsg_trim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 680 | if (tcf_action_dump(skb, actions, bind, ref) < 0) |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 681 | goto out_nlmsg_trim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 683 | nla_nest_end(skb, nest); |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 684 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 685 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | return skb->len; |
| 687 | |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 688 | out_nlmsg_trim: |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 689 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | return -1; |
| 691 | } |
| 692 | |
| 693 | static int |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 694 | act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n, |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 695 | struct list_head *actions, int event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | { |
| 697 | struct sk_buff *skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | |
| 699 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 700 | if (!skb) |
| 701 | return -ENOBUFS; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 702 | if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, 0, 0) <= 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | kfree_skb(skb); |
| 704 | return -EINVAL; |
| 705 | } |
Thomas Graf | 2942e90 | 2006-08-15 00:30:25 -0700 | [diff] [blame] | 706 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 707 | return rtnl_unicast(skb, net, portid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | } |
| 709 | |
WANG Cong | 03701d6 | 2014-02-11 17:07:35 -0800 | [diff] [blame] | 710 | static struct tc_action *create_a(int i) |
| 711 | { |
| 712 | struct tc_action *act; |
| 713 | |
| 714 | act = kzalloc(sizeof(*act), GFP_KERNEL); |
| 715 | if (act == NULL) { |
| 716 | pr_debug("create_a: failed to alloc!\n"); |
| 717 | return NULL; |
| 718 | } |
| 719 | act->order = i; |
| 720 | INIT_LIST_HEAD(&act->list); |
| 721 | return act; |
| 722 | } |
| 723 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | static struct tc_action * |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 725 | tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 portid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 727 | struct nlattr *tb[TCA_ACT_MAX + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | struct tc_action *a; |
| 729 | int index; |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 730 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 732 | err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL); |
| 733 | if (err < 0) |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 734 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 736 | err = -EINVAL; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 737 | if (tb[TCA_ACT_INDEX] == NULL || |
| 738 | nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 739 | goto err_out; |
Patrick McHardy | 1587bac | 2008-01-23 20:35:03 -0800 | [diff] [blame] | 740 | index = nla_get_u32(tb[TCA_ACT_INDEX]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 742 | err = -ENOMEM; |
WANG Cong | 03701d6 | 2014-02-11 17:07:35 -0800 | [diff] [blame] | 743 | a = create_a(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | if (a == NULL) |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 745 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 747 | err = -EINVAL; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 748 | a->ops = tc_lookup_action(tb[TCA_ACT_KIND]); |
Jamal Hadi Salim | 63acd68 | 2013-12-23 08:02:12 -0500 | [diff] [blame] | 749 | if (a->ops == NULL) /* could happen in batch of actions */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | goto err_free; |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 751 | err = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | if (a->ops->lookup(a, index) == 0) |
| 753 | goto err_mod; |
| 754 | |
| 755 | module_put(a->ops->owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | return a; |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 757 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | err_mod: |
| 759 | module_put(a->ops->owner); |
| 760 | err_free: |
| 761 | kfree(a); |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 762 | err_out: |
| 763 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | } |
| 765 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 766 | static void cleanup_a(struct list_head *actions) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | { |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 768 | struct tc_action *a, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 770 | list_for_each_entry_safe(a, tmp, actions, list) { |
| 771 | list_del(&a->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | kfree(a); |
| 773 | } |
| 774 | } |
| 775 | |
Tom Goff | 7316ae8 | 2010-03-19 15:40:13 +0000 | [diff] [blame] | 776 | static int tca_action_flush(struct net *net, struct nlattr *nla, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 777 | struct nlmsghdr *n, u32 portid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | { |
| 779 | struct sk_buff *skb; |
| 780 | unsigned char *b; |
| 781 | struct nlmsghdr *nlh; |
| 782 | struct tcamsg *t; |
| 783 | struct netlink_callback dcb; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 784 | struct nlattr *nest; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 785 | struct nlattr *tb[TCA_ACT_MAX + 1]; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 786 | struct nlattr *kind; |
WANG Cong | 03701d6 | 2014-02-11 17:07:35 -0800 | [diff] [blame] | 787 | struct tc_action a; |
Jamal Hadi Salim | 3672387 | 2008-08-13 02:41:45 -0700 | [diff] [blame] | 788 | int err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 791 | if (!skb) { |
stephen hemminger | 6ff9c36 | 2010-05-12 06:37:05 +0000 | [diff] [blame] | 792 | pr_debug("tca_action_flush: failed skb alloc\n"); |
Jamal Hadi Salim | 3672387 | 2008-08-13 02:41:45 -0700 | [diff] [blame] | 793 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | } |
| 795 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 796 | b = skb_tail_pointer(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 798 | err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL); |
| 799 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | goto err_out; |
| 801 | |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 802 | err = -EINVAL; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 803 | kind = tb[TCA_ACT_KIND]; |
WANG Cong | 03701d6 | 2014-02-11 17:07:35 -0800 | [diff] [blame] | 804 | memset(&a, 0, sizeof(struct tc_action)); |
| 805 | INIT_LIST_HEAD(&a.list); |
| 806 | a.ops = tc_lookup_action(kind); |
| 807 | if (a.ops == NULL) /*some idjot trying to flush unknown action */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | goto err_out; |
| 809 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 810 | nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t), 0); |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 811 | if (!nlh) |
| 812 | goto out_module_put; |
| 813 | t = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | t->tca_family = AF_UNSPEC; |
Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 815 | t->tca__pad1 = 0; |
| 816 | t->tca__pad2 = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 818 | nest = nla_nest_start(skb, TCA_ACT_TAB); |
| 819 | if (nest == NULL) |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 820 | goto out_module_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | |
WANG Cong | 03701d6 | 2014-02-11 17:07:35 -0800 | [diff] [blame] | 822 | err = a.ops->walk(skb, &dcb, RTM_DELACTION, &a); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | if (err < 0) |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 824 | goto out_module_put; |
Jamal Hadi Salim | f97017c | 2008-08-13 02:41:22 -0700 | [diff] [blame] | 825 | if (err == 0) |
| 826 | goto noflush_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 828 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 830 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | nlh->nlmsg_flags |= NLM_F_ROOT; |
WANG Cong | 03701d6 | 2014-02-11 17:07:35 -0800 | [diff] [blame] | 832 | module_put(a.ops->owner); |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 833 | err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 834 | n->nlmsg_flags & NLM_F_ECHO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | if (err > 0) |
| 836 | return 0; |
| 837 | |
| 838 | return err; |
| 839 | |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 840 | out_module_put: |
WANG Cong | 03701d6 | 2014-02-11 17:07:35 -0800 | [diff] [blame] | 841 | module_put(a.ops->owner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | err_out: |
Jamal Hadi Salim | f97017c | 2008-08-13 02:41:22 -0700 | [diff] [blame] | 843 | noflush_out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | return err; |
| 846 | } |
| 847 | |
| 848 | static int |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 849 | tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions, |
| 850 | u32 portid) |
| 851 | { |
| 852 | int ret; |
| 853 | struct sk_buff *skb; |
| 854 | |
| 855 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 856 | if (!skb) |
| 857 | return -ENOBUFS; |
| 858 | |
| 859 | if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION, |
| 860 | 0, 1) <= 0) { |
| 861 | kfree_skb(skb); |
| 862 | return -EINVAL; |
| 863 | } |
| 864 | |
| 865 | /* now do the delete */ |
WANG Cong | 55334a5 | 2014-02-11 17:07:34 -0800 | [diff] [blame] | 866 | ret = tcf_action_destroy(actions, 0); |
| 867 | if (ret < 0) { |
| 868 | kfree_skb(skb); |
| 869 | return ret; |
| 870 | } |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 871 | |
| 872 | ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
| 873 | n->nlmsg_flags & NLM_F_ECHO); |
| 874 | if (ret > 0) |
| 875 | return 0; |
| 876 | return ret; |
| 877 | } |
| 878 | |
| 879 | static int |
Tom Goff | 7316ae8 | 2010-03-19 15:40:13 +0000 | [diff] [blame] | 880 | tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 881 | u32 portid, int event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | { |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 883 | int i, ret; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 884 | struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 885 | struct tc_action *act; |
| 886 | LIST_HEAD(actions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 888 | ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL); |
| 889 | if (ret < 0) |
| 890 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 892 | if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) { |
Jamal Hadi Salim | f97017c | 2008-08-13 02:41:22 -0700 | [diff] [blame] | 893 | if (tb[1] != NULL) |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 894 | return tca_action_flush(net, tb[1], n, portid); |
Jamal Hadi Salim | f97017c | 2008-08-13 02:41:22 -0700 | [diff] [blame] | 895 | else |
| 896 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | } |
| 898 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 899 | for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 900 | act = tcf_action_get_1(tb[i], n, portid); |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 901 | if (IS_ERR(act)) { |
| 902 | ret = PTR_ERR(act); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | goto err; |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 904 | } |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 905 | act->order = i; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 906 | list_add_tail(&act->list, &actions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | if (event == RTM_GETACTION) |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 910 | ret = act_get_notify(net, portid, n, &actions, event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | else { /* delete */ |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 912 | ret = tcf_del_notify(net, n, &actions, portid); |
| 913 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | return ret; |
| 916 | } |
| 917 | err: |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 918 | cleanup_a(&actions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | return ret; |
| 920 | } |
| 921 | |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 922 | static int |
| 923 | tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions, |
| 924 | u32 portid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | struct sk_buff *skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | int err = 0; |
| 928 | |
| 929 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 930 | if (!skb) |
| 931 | return -ENOBUFS; |
| 932 | |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 933 | if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags, |
| 934 | RTM_NEWACTION, 0, 0) <= 0) { |
| 935 | kfree_skb(skb); |
| 936 | return -EINVAL; |
| 937 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 939 | err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
| 940 | n->nlmsg_flags & NLM_F_ECHO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | if (err > 0) |
| 942 | err = 0; |
| 943 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | } |
| 945 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | static int |
Tom Goff | 7316ae8 | 2010-03-19 15:40:13 +0000 | [diff] [blame] | 947 | tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 948 | u32 portid, int ovr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | { |
| 950 | int ret = 0; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 951 | LIST_HEAD(actions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 953 | ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions); |
| 954 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | goto done; |
| 956 | |
| 957 | /* dump then free all the actions after update; inserted policy |
| 958 | * stays intact |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 959 | */ |
WANG Cong | a56e195 | 2014-01-09 16:14:00 -0800 | [diff] [blame] | 960 | ret = tcf_add_notify(net, n, &actions, portid); |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 961 | cleanup_a(&actions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | done: |
| 963 | return ret; |
| 964 | } |
| 965 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 966 | static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 968 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 969 | struct nlattr *tca[TCA_ACT_MAX + 1]; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 970 | u32 portid = skb ? NETLINK_CB(skb).portid : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | int ret = 0, ovr = 0; |
| 972 | |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 973 | if ((n->nlmsg_type != RTM_GETACTION) && !netlink_capable(skb, CAP_NET_ADMIN)) |
Eric W. Biederman | dfc47ef | 2012-11-16 03:03:00 +0000 | [diff] [blame] | 974 | return -EPERM; |
| 975 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 976 | ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL); |
| 977 | if (ret < 0) |
| 978 | return ret; |
| 979 | |
| 980 | if (tca[TCA_ACT_TAB] == NULL) { |
stephen hemminger | 6ff9c36 | 2010-05-12 06:37:05 +0000 | [diff] [blame] | 981 | pr_notice("tc_ctl_action: received NO action attribs\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | return -EINVAL; |
| 983 | } |
| 984 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 985 | /* n->nlmsg_flags & NLM_F_CREATE */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | switch (n->nlmsg_type) { |
| 987 | case RTM_NEWACTION: |
| 988 | /* we are going to assume all other flags |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 989 | * imply create only if it doesn't exist |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | * Note that CREATE | EXCL implies that |
| 991 | * but since we want avoid ambiguity (eg when flags |
| 992 | * is zero) then just set this |
| 993 | */ |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 994 | if (n->nlmsg_flags & NLM_F_REPLACE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | ovr = 1; |
| 996 | replay: |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 997 | ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | if (ret == -EAGAIN) |
| 999 | goto replay; |
| 1000 | break; |
| 1001 | case RTM_DELACTION: |
Tom Goff | 7316ae8 | 2010-03-19 15:40:13 +0000 | [diff] [blame] | 1002 | ret = tca_action_gd(net, tca[TCA_ACT_TAB], n, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1003 | portid, RTM_DELACTION); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | break; |
| 1005 | case RTM_GETACTION: |
Tom Goff | 7316ae8 | 2010-03-19 15:40:13 +0000 | [diff] [blame] | 1006 | ret = tca_action_gd(net, tca[TCA_ACT_TAB], n, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1007 | portid, RTM_GETACTION); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | break; |
| 1009 | default: |
| 1010 | BUG(); |
| 1011 | } |
| 1012 | |
| 1013 | return ret; |
| 1014 | } |
| 1015 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1016 | static struct nlattr * |
Patrick McHardy | 3a6c2b4 | 2009-08-25 16:07:40 +0200 | [diff] [blame] | 1017 | find_dump_kind(const struct nlmsghdr *n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1019 | struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1]; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1020 | struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; |
| 1021 | struct nlattr *nla[TCAA_MAX + 1]; |
| 1022 | struct nlattr *kind; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | |
Patrick McHardy | c96c947 | 2008-01-23 20:32:58 -0800 | [diff] [blame] | 1024 | if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | return NULL; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1026 | tb1 = nla[TCA_ACT_TAB]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | if (tb1 == NULL) |
| 1028 | return NULL; |
| 1029 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1030 | if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1), |
| 1031 | NLMSG_ALIGN(nla_len(tb1)), NULL) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | |
Patrick McHardy | 6d834e0 | 2008-01-23 20:32:42 -0800 | [diff] [blame] | 1034 | if (tb[1] == NULL) |
| 1035 | return NULL; |
| 1036 | if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]), |
| 1037 | nla_len(tb[1]), NULL) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | return NULL; |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1039 | kind = tb2[TCA_ACT_KIND]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | |
Thomas Graf | 26dab89 | 2006-07-05 20:45:06 -0700 | [diff] [blame] | 1041 | return kind; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | static int |
| 1045 | tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb) |
| 1046 | { |
| 1047 | struct nlmsghdr *nlh; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1048 | unsigned char *b = skb_tail_pointer(skb); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1049 | struct nlattr *nest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | struct tc_action_ops *a_o; |
| 1051 | struct tc_action a; |
| 1052 | int ret = 0; |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1053 | struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh); |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 1054 | struct nlattr *kind = find_dump_kind(cb->nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | |
| 1056 | if (kind == NULL) { |
stephen hemminger | 6ff9c36 | 2010-05-12 06:37:05 +0000 | [diff] [blame] | 1057 | pr_info("tc_dump_action: action bad kind\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | return 0; |
| 1059 | } |
| 1060 | |
Thomas Graf | 26dab89 | 2006-07-05 20:45:06 -0700 | [diff] [blame] | 1061 | a_o = tc_lookup_action(kind); |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 1062 | if (a_o == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | |
| 1065 | memset(&a, 0, sizeof(struct tc_action)); |
| 1066 | a.ops = a_o; |
| 1067 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1068 | nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1069 | cb->nlh->nlmsg_type, sizeof(*t), 0); |
| 1070 | if (!nlh) |
| 1071 | goto out_module_put; |
| 1072 | t = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | t->tca_family = AF_UNSPEC; |
Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 1074 | t->tca__pad1 = 0; |
| 1075 | t->tca__pad2 = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1077 | nest = nla_nest_start(skb, TCA_ACT_TAB); |
| 1078 | if (nest == NULL) |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1079 | goto out_module_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | |
| 1081 | ret = a_o->walk(skb, cb, RTM_GETACTION, &a); |
| 1082 | if (ret < 0) |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1083 | goto out_module_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | |
| 1085 | if (ret > 0) { |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1086 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | ret = skb->len; |
| 1088 | } else |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1089 | nla_nest_cancel(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1091 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1092 | if (NETLINK_CB(cb->skb).portid && ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | nlh->nlmsg_flags |= NLM_F_MULTI; |
| 1094 | module_put(a_o->owner); |
| 1095 | return skb->len; |
| 1096 | |
David S. Miller | 8b00a53 | 2012-06-26 21:39:32 -0700 | [diff] [blame] | 1097 | out_module_put: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | module_put(a_o->owner); |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 1099 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | return skb->len; |
| 1101 | } |
| 1102 | |
| 1103 | static int __init tc_action_init(void) |
| 1104 | { |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 1105 | rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL); |
| 1106 | rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL); |
| 1107 | rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action, |
| 1108 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | return 0; |
| 1111 | } |
| 1112 | |
| 1113 | subsys_initcall(tc_action_init); |