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