Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * net/sched/cls_api.c Packet classifier 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 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 10 | * |
| 11 | * Changes: |
| 12 | * |
| 13 | * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support |
| 14 | * |
| 15 | */ |
| 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/module.h> |
| 18 | #include <linux/types.h> |
| 19 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/init.h> |
| 24 | #include <linux/kmod.h> |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 25 | #include <linux/netlink.h> |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 26 | #include <net/net_namespace.h> |
| 27 | #include <net/sock.h> |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 28 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <net/pkt_sched.h> |
| 30 | #include <net/pkt_cls.h> |
| 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | /* The list of all installed classifier types */ |
| 33 | |
Patrick McHardy | 2eb9d75 | 2008-01-22 22:10:42 -0800 | [diff] [blame] | 34 | static struct tcf_proto_ops *tcf_proto_base __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | /* Protects list of registered TC modules. It is pure SMP lock. */ |
| 37 | static DEFINE_RWLOCK(cls_mod_lock); |
| 38 | |
| 39 | /* Find classifier type by string name */ |
| 40 | |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame^] | 41 | static struct tcf_proto_ops *tcf_proto_lookup_ops(struct nlattr *kind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { |
| 43 | struct tcf_proto_ops *t = NULL; |
| 44 | |
| 45 | if (kind) { |
| 46 | read_lock(&cls_mod_lock); |
| 47 | for (t = tcf_proto_base; t; t = t->next) { |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame^] | 48 | if (nla_strcmp(kind, t->kind) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | if (!try_module_get(t->owner)) |
| 50 | t = NULL; |
| 51 | break; |
| 52 | } |
| 53 | } |
| 54 | read_unlock(&cls_mod_lock); |
| 55 | } |
| 56 | return t; |
| 57 | } |
| 58 | |
| 59 | /* Register(unregister) new classifier type */ |
| 60 | |
| 61 | int register_tcf_proto_ops(struct tcf_proto_ops *ops) |
| 62 | { |
| 63 | struct tcf_proto_ops *t, **tp; |
| 64 | int rc = -EEXIST; |
| 65 | |
| 66 | write_lock(&cls_mod_lock); |
| 67 | for (tp = &tcf_proto_base; (t = *tp) != NULL; tp = &t->next) |
| 68 | if (!strcmp(ops->kind, t->kind)) |
| 69 | goto out; |
| 70 | |
| 71 | ops->next = NULL; |
| 72 | *tp = ops; |
| 73 | rc = 0; |
| 74 | out: |
| 75 | write_unlock(&cls_mod_lock); |
| 76 | return rc; |
| 77 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 78 | EXPORT_SYMBOL(register_tcf_proto_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | int unregister_tcf_proto_ops(struct tcf_proto_ops *ops) |
| 81 | { |
| 82 | struct tcf_proto_ops *t, **tp; |
| 83 | int rc = -ENOENT; |
| 84 | |
| 85 | write_lock(&cls_mod_lock); |
| 86 | for (tp = &tcf_proto_base; (t=*tp) != NULL; tp = &t->next) |
| 87 | if (t == ops) |
| 88 | break; |
| 89 | |
| 90 | if (!t) |
| 91 | goto out; |
| 92 | *tp = t->next; |
| 93 | rc = 0; |
| 94 | out: |
| 95 | write_unlock(&cls_mod_lock); |
| 96 | return rc; |
| 97 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 98 | EXPORT_SYMBOL(unregister_tcf_proto_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
| 100 | static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n, |
| 101 | struct tcf_proto *tp, unsigned long fh, int event); |
| 102 | |
| 103 | |
| 104 | /* Select new prio value from the range, managed by kernel. */ |
| 105 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 106 | static inline u32 tcf_auto_prio(struct tcf_proto *tp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 108 | u32 first = TC_H_MAKE(0xC0000000U, 0U); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | if (tp) |
| 111 | first = tp->prio-1; |
| 112 | |
| 113 | return first; |
| 114 | } |
| 115 | |
| 116 | /* Add/change/delete/get a filter node */ |
| 117 | |
| 118 | static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n, void *arg) |
| 119 | { |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 120 | struct net *net = skb->sk->sk_net; |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame^] | 121 | struct nlattr *tca[TCA_MAX + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | struct tcmsg *t; |
| 123 | u32 protocol; |
| 124 | u32 prio; |
| 125 | u32 nprio; |
| 126 | u32 parent; |
| 127 | struct net_device *dev; |
| 128 | struct Qdisc *q; |
| 129 | struct tcf_proto **back, **chain; |
| 130 | struct tcf_proto *tp; |
| 131 | struct tcf_proto_ops *tp_ops; |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 132 | const struct Qdisc_class_ops *cops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | unsigned long cl; |
| 134 | unsigned long fh; |
| 135 | int err; |
| 136 | |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 137 | if (net != &init_net) |
| 138 | return -EINVAL; |
| 139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | replay: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | t = NLMSG_DATA(n); |
| 142 | protocol = TC_H_MIN(t->tcm_info); |
| 143 | prio = TC_H_MAJ(t->tcm_info); |
| 144 | nprio = prio; |
| 145 | parent = t->tcm_parent; |
| 146 | cl = 0; |
| 147 | |
| 148 | if (prio == 0) { |
| 149 | /* If no priority is given, user wants we allocated it. */ |
| 150 | if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE)) |
| 151 | return -ENOENT; |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 152 | prio = TC_H_MAKE(0x80000000U, 0U); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /* Find head of filter chain. */ |
| 156 | |
| 157 | /* Find link */ |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 158 | dev = __dev_get_by_index(&init_net, t->tcm_ifindex); |
| 159 | if (dev == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | return -ENODEV; |
| 161 | |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame^] | 162 | err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL); |
| 163 | if (err < 0) |
| 164 | return err; |
| 165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | /* Find qdisc */ |
| 167 | if (!parent) { |
| 168 | q = dev->qdisc_sleeping; |
| 169 | parent = q->handle; |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 170 | } else { |
| 171 | q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent)); |
| 172 | if (q == NULL) |
| 173 | return -EINVAL; |
| 174 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
| 176 | /* Is it classful? */ |
| 177 | if ((cops = q->ops->cl_ops) == NULL) |
| 178 | return -EINVAL; |
| 179 | |
| 180 | /* Do we search for filter, attached to class? */ |
| 181 | if (TC_H_MIN(parent)) { |
| 182 | cl = cops->get(q, parent); |
| 183 | if (cl == 0) |
| 184 | return -ENOENT; |
| 185 | } |
| 186 | |
| 187 | /* And the last stroke */ |
| 188 | chain = cops->tcf_chain(q, cl); |
| 189 | err = -EINVAL; |
| 190 | if (chain == NULL) |
| 191 | goto errout; |
| 192 | |
| 193 | /* Check the chain for existence of proto-tcf with this priority */ |
| 194 | for (back = chain; (tp=*back) != NULL; back = &tp->next) { |
| 195 | if (tp->prio >= prio) { |
| 196 | if (tp->prio == prio) { |
| 197 | if (!nprio || (tp->protocol != protocol && protocol)) |
| 198 | goto errout; |
| 199 | } else |
| 200 | tp = NULL; |
| 201 | break; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | if (tp == NULL) { |
| 206 | /* Proto-tcf does not exist, create new one */ |
| 207 | |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame^] | 208 | if (tca[TCA_KIND] == NULL || !protocol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | goto errout; |
| 210 | |
| 211 | err = -ENOENT; |
| 212 | if (n->nlmsg_type != RTM_NEWTFILTER || !(n->nlmsg_flags&NLM_F_CREATE)) |
| 213 | goto errout; |
| 214 | |
| 215 | |
| 216 | /* Create new proto tcf */ |
| 217 | |
| 218 | err = -ENOBUFS; |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 219 | tp = kzalloc(sizeof(*tp), GFP_KERNEL); |
| 220 | if (tp == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | goto errout; |
| 222 | err = -EINVAL; |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame^] | 223 | tp_ops = tcf_proto_lookup_ops(tca[TCA_KIND]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | if (tp_ops == NULL) { |
| 225 | #ifdef CONFIG_KMOD |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame^] | 226 | struct nlattr *kind = tca[TCA_KIND]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | char name[IFNAMSIZ]; |
| 228 | |
| 229 | if (kind != NULL && |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame^] | 230 | nla_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | rtnl_unlock(); |
| 232 | request_module("cls_%s", name); |
| 233 | rtnl_lock(); |
| 234 | tp_ops = tcf_proto_lookup_ops(kind); |
| 235 | /* We dropped the RTNL semaphore in order to |
| 236 | * perform the module load. So, even if we |
| 237 | * succeeded in loading the module we have to |
| 238 | * replay the request. We indicate this using |
| 239 | * -EAGAIN. |
| 240 | */ |
| 241 | if (tp_ops != NULL) { |
| 242 | module_put(tp_ops->owner); |
| 243 | err = -EAGAIN; |
| 244 | } |
| 245 | } |
| 246 | #endif |
| 247 | kfree(tp); |
| 248 | goto errout; |
| 249 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | tp->ops = tp_ops; |
| 251 | tp->protocol = protocol; |
| 252 | tp->prio = nprio ? : tcf_auto_prio(*back); |
| 253 | tp->q = q; |
| 254 | tp->classify = tp_ops->classify; |
| 255 | tp->classid = parent; |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 256 | |
| 257 | err = tp_ops->init(tp); |
| 258 | if (err != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | module_put(tp_ops->owner); |
| 260 | kfree(tp); |
| 261 | goto errout; |
| 262 | } |
| 263 | |
| 264 | qdisc_lock_tree(dev); |
| 265 | tp->next = *back; |
| 266 | *back = tp; |
| 267 | qdisc_unlock_tree(dev); |
| 268 | |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame^] | 269 | } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | goto errout; |
| 271 | |
| 272 | fh = tp->ops->get(tp, t->tcm_handle); |
| 273 | |
| 274 | if (fh == 0) { |
| 275 | if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) { |
| 276 | qdisc_lock_tree(dev); |
| 277 | *back = tp->next; |
| 278 | qdisc_unlock_tree(dev); |
| 279 | |
| 280 | tfilter_notify(skb, n, tp, fh, RTM_DELTFILTER); |
| 281 | tcf_destroy(tp); |
| 282 | err = 0; |
| 283 | goto errout; |
| 284 | } |
| 285 | |
| 286 | err = -ENOENT; |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 287 | if (n->nlmsg_type != RTM_NEWTFILTER || |
| 288 | !(n->nlmsg_flags & NLM_F_CREATE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | goto errout; |
| 290 | } else { |
| 291 | switch (n->nlmsg_type) { |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 292 | case RTM_NEWTFILTER: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | err = -EEXIST; |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 294 | if (n->nlmsg_flags & NLM_F_EXCL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | goto errout; |
| 296 | break; |
| 297 | case RTM_DELTFILTER: |
| 298 | err = tp->ops->delete(tp, fh); |
| 299 | if (err == 0) |
| 300 | tfilter_notify(skb, n, tp, fh, RTM_DELTFILTER); |
| 301 | goto errout; |
| 302 | case RTM_GETTFILTER: |
| 303 | err = tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER); |
| 304 | goto errout; |
| 305 | default: |
| 306 | err = -EINVAL; |
| 307 | goto errout; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | err = tp->ops->change(tp, cl, t->tcm_handle, tca, &fh); |
| 312 | if (err == 0) |
| 313 | tfilter_notify(skb, n, tp, fh, RTM_NEWTFILTER); |
| 314 | |
| 315 | errout: |
| 316 | if (cl) |
| 317 | cops->put(q, cl); |
| 318 | if (err == -EAGAIN) |
| 319 | /* Replay the request. */ |
| 320 | goto replay; |
| 321 | return err; |
| 322 | } |
| 323 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 324 | static int tcf_fill_node(struct sk_buff *skb, struct tcf_proto *tp, |
| 325 | unsigned long fh, u32 pid, u32 seq, u16 flags, int event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | { |
| 327 | struct tcmsg *tcm; |
| 328 | struct nlmsghdr *nlh; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 329 | unsigned char *b = skb_tail_pointer(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | |
Jamal Hadi Salim | e431b8c | 2005-06-18 22:55:31 -0700 | [diff] [blame] | 331 | nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*tcm), flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | tcm = NLMSG_DATA(nlh); |
| 333 | tcm->tcm_family = AF_UNSPEC; |
Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 334 | tcm->tcm__pad1 = 0; |
| 335 | tcm->tcm__pad1 = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | tcm->tcm_ifindex = tp->q->dev->ifindex; |
| 337 | tcm->tcm_parent = tp->classid; |
| 338 | tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol); |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame^] | 339 | NLA_PUT(skb, TCA_KIND, IFNAMSIZ, tp->ops->kind); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | tcm->tcm_handle = fh; |
| 341 | if (RTM_DELTFILTER != event) { |
| 342 | tcm->tcm_handle = 0; |
| 343 | if (tp->ops->dump && tp->ops->dump(tp, fh, skb, tcm) < 0) |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame^] | 344 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | } |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 346 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | return skb->len; |
| 348 | |
| 349 | nlmsg_failure: |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame^] | 350 | nla_put_failure: |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 351 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | return -1; |
| 353 | } |
| 354 | |
| 355 | static int tfilter_notify(struct sk_buff *oskb, struct nlmsghdr *n, |
| 356 | struct tcf_proto *tp, unsigned long fh, int event) |
| 357 | { |
| 358 | struct sk_buff *skb; |
| 359 | u32 pid = oskb ? NETLINK_CB(oskb).pid : 0; |
| 360 | |
| 361 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 362 | if (!skb) |
| 363 | return -ENOBUFS; |
| 364 | |
| 365 | if (tcf_fill_node(skb, tp, fh, pid, n->nlmsg_seq, 0, event) <= 0) { |
| 366 | kfree_skb(skb); |
| 367 | return -EINVAL; |
| 368 | } |
| 369 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 370 | return rtnetlink_send(skb, &init_net, pid, RTNLGRP_TC, |
| 371 | n->nlmsg_flags & NLM_F_ECHO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 374 | struct tcf_dump_args { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | struct tcf_walker w; |
| 376 | struct sk_buff *skb; |
| 377 | struct netlink_callback *cb; |
| 378 | }; |
| 379 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 380 | static int tcf_node_dump(struct tcf_proto *tp, unsigned long n, |
| 381 | struct tcf_walker *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | { |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 383 | struct tcf_dump_args *a = (void *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | |
| 385 | return tcf_fill_node(a->skb, tp, n, NETLINK_CB(a->cb->skb).pid, |
| 386 | a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER); |
| 387 | } |
| 388 | |
| 389 | static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb) |
| 390 | { |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 391 | struct net *net = skb->sk->sk_net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | int t; |
| 393 | int s_t; |
| 394 | struct net_device *dev; |
| 395 | struct Qdisc *q; |
| 396 | struct tcf_proto *tp, **chain; |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 397 | struct tcmsg *tcm = (struct tcmsg *)NLMSG_DATA(cb->nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | unsigned long cl = 0; |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 399 | const struct Qdisc_class_ops *cops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | struct tcf_dump_args arg; |
| 401 | |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 402 | if (net != &init_net) |
| 403 | return 0; |
| 404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | if (cb->nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*tcm))) |
| 406 | return skb->len; |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 407 | if ((dev = dev_get_by_index(&init_net, tcm->tcm_ifindex)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | return skb->len; |
| 409 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | if (!tcm->tcm_parent) |
| 411 | q = dev->qdisc_sleeping; |
| 412 | else |
| 413 | q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent)); |
| 414 | if (!q) |
| 415 | goto out; |
| 416 | if ((cops = q->ops->cl_ops) == NULL) |
| 417 | goto errout; |
| 418 | if (TC_H_MIN(tcm->tcm_parent)) { |
| 419 | cl = cops->get(q, tcm->tcm_parent); |
| 420 | if (cl == 0) |
| 421 | goto errout; |
| 422 | } |
| 423 | chain = cops->tcf_chain(q, cl); |
| 424 | if (chain == NULL) |
| 425 | goto errout; |
| 426 | |
| 427 | s_t = cb->args[0]; |
| 428 | |
| 429 | for (tp=*chain, t=0; tp; tp = tp->next, t++) { |
| 430 | if (t < s_t) continue; |
| 431 | if (TC_H_MAJ(tcm->tcm_info) && |
| 432 | TC_H_MAJ(tcm->tcm_info) != tp->prio) |
| 433 | continue; |
| 434 | if (TC_H_MIN(tcm->tcm_info) && |
| 435 | TC_H_MIN(tcm->tcm_info) != tp->protocol) |
| 436 | continue; |
| 437 | if (t > s_t) |
| 438 | memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0])); |
| 439 | if (cb->args[1] == 0) { |
| 440 | if (tcf_fill_node(skb, tp, 0, NETLINK_CB(cb->skb).pid, |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 441 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 442 | RTM_NEWTFILTER) <= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | break; |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | cb->args[1] = 1; |
| 446 | } |
| 447 | if (tp->ops->walk == NULL) |
| 448 | continue; |
| 449 | arg.w.fn = tcf_node_dump; |
| 450 | arg.skb = skb; |
| 451 | arg.cb = cb; |
| 452 | arg.w.stop = 0; |
| 453 | arg.w.skip = cb->args[1]-1; |
| 454 | arg.w.count = 0; |
| 455 | tp->ops->walk(tp, &arg.w); |
| 456 | cb->args[1] = arg.w.count+1; |
| 457 | if (arg.w.stop) |
| 458 | break; |
| 459 | } |
| 460 | |
| 461 | cb->args[0] = t; |
| 462 | |
| 463 | errout: |
| 464 | if (cl) |
| 465 | cops->put(q, cl); |
| 466 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | dev_put(dev); |
| 468 | return skb->len; |
| 469 | } |
| 470 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 471 | void tcf_exts_destroy(struct tcf_proto *tp, struct tcf_exts *exts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | { |
| 473 | #ifdef CONFIG_NET_CLS_ACT |
| 474 | if (exts->action) { |
| 475 | tcf_action_destroy(exts->action, TCA_ACT_UNBIND); |
| 476 | exts->action = NULL; |
| 477 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | #endif |
| 479 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 480 | EXPORT_SYMBOL(tcf_exts_destroy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame^] | 482 | int tcf_exts_validate(struct tcf_proto *tp, struct nlattr **tb, |
| 483 | struct nlattr *rate_tlv, struct tcf_exts *exts, |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 484 | struct tcf_ext_map *map) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | { |
| 486 | memset(exts, 0, sizeof(*exts)); |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 487 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | #ifdef CONFIG_NET_CLS_ACT |
| 489 | { |
| 490 | int err; |
| 491 | struct tc_action *act; |
| 492 | |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame^] | 493 | if (map->police && tb[map->police]) { |
| 494 | act = tcf_action_init_1((struct rtattr *)tb[map->police], |
| 495 | (struct rtattr *)rate_tlv, |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 496 | "police", TCA_ACT_NOREPLACE, |
| 497 | TCA_ACT_BIND, &err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | if (act == NULL) |
| 499 | return err; |
| 500 | |
| 501 | act->type = TCA_OLD_COMPAT; |
| 502 | exts->action = act; |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame^] | 503 | } else if (map->action && tb[map->action]) { |
| 504 | act = tcf_action_init((struct rtattr *)tb[map->action], |
| 505 | (struct rtattr *)rate_tlv, NULL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | TCA_ACT_NOREPLACE, TCA_ACT_BIND, &err); |
| 507 | if (act == NULL) |
| 508 | return err; |
| 509 | |
| 510 | exts->action = act; |
| 511 | } |
| 512 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | #else |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame^] | 514 | if ((map->action && tb[map->action]) || |
| 515 | (map->police && tb[map->police])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | return -EOPNOTSUPP; |
| 517 | #endif |
| 518 | |
| 519 | return 0; |
| 520 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 521 | EXPORT_SYMBOL(tcf_exts_validate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 523 | void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst, |
| 524 | struct tcf_exts *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | { |
| 526 | #ifdef CONFIG_NET_CLS_ACT |
| 527 | if (src->action) { |
| 528 | struct tc_action *act; |
| 529 | tcf_tree_lock(tp); |
| 530 | act = xchg(&dst->action, src->action); |
| 531 | tcf_tree_unlock(tp); |
| 532 | if (act) |
| 533 | tcf_action_destroy(act, TCA_ACT_UNBIND); |
| 534 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | #endif |
| 536 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 537 | EXPORT_SYMBOL(tcf_exts_change); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 539 | int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | struct tcf_ext_map *map) |
| 541 | { |
| 542 | #ifdef CONFIG_NET_CLS_ACT |
| 543 | if (map->action && exts->action) { |
| 544 | /* |
| 545 | * again for backward compatible mode - we want |
| 546 | * to work with both old and new modes of entering |
| 547 | * tc data even if iproute2 was newer - jhs |
| 548 | */ |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame^] | 549 | struct nlattr *p_rta = (struct nlattr *)skb_tail_pointer(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
| 551 | if (exts->action->type != TCA_OLD_COMPAT) { |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame^] | 552 | NLA_PUT(skb, map->action, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | if (tcf_action_dump(skb, exts->action, 0, 0) < 0) |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame^] | 554 | goto nla_put_failure; |
| 555 | p_rta->nla_len = skb_tail_pointer(skb) - (u8 *)p_rta; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | } else if (map->police) { |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame^] | 557 | NLA_PUT(skb, map->police, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | if (tcf_action_dump_old(skb, exts->action, 0, 0) < 0) |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame^] | 559 | goto nla_put_failure; |
| 560 | p_rta->nla_len = skb_tail_pointer(skb) - (u8 *)p_rta; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | } |
| 562 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | #endif |
| 564 | return 0; |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame^] | 565 | nla_put_failure: __attribute__ ((unused)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | return -1; |
| 567 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 568 | EXPORT_SYMBOL(tcf_exts_dump); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 570 | |
| 571 | int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts, |
| 572 | struct tcf_ext_map *map) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | { |
| 574 | #ifdef CONFIG_NET_CLS_ACT |
| 575 | if (exts->action) |
| 576 | if (tcf_action_copy_stats(skb, exts->action, 1) < 0) |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame^] | 577 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | #endif |
| 579 | return 0; |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame^] | 580 | nla_put_failure: __attribute__ ((unused)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | return -1; |
| 582 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 583 | EXPORT_SYMBOL(tcf_exts_dump_stats); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | |
| 585 | static int __init tc_filter_init(void) |
| 586 | { |
Thomas Graf | 82623c0 | 2007-03-22 11:56:22 -0700 | [diff] [blame] | 587 | rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL); |
| 588 | rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL); |
| 589 | rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter, |
| 590 | tc_dump_tfilter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | subsys_initcall(tc_filter_init); |