cls_api.c 14 KB

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