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