cls_api.c 14 KB

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