cls_api.c 13 KB

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