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