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