cls_api.c 14 KB

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