cls_api.c 14 KB

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