cls_api.c 14 KB

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