cls_api.c 14 KB

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