cls_api.c 14 KB

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