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