cls_api.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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. if ((n->nlmsg_type != RTM_GETTFILTER) && !capable(CAP_NET_ADMIN))
  120. return -EPERM;
  121. replay:
  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. err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL);
  141. if (err < 0)
  142. return err;
  143. /* Find qdisc */
  144. if (!parent) {
  145. q = dev->qdisc;
  146. parent = q->handle;
  147. } else {
  148. q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
  149. if (q == NULL)
  150. return -EINVAL;
  151. }
  152. /* Is it classful? */
  153. cops = q->ops->cl_ops;
  154. if (!cops)
  155. return -EINVAL;
  156. if (cops->tcf_chain == NULL)
  157. return -EOPNOTSUPP;
  158. /* Do we search for filter, attached to class? */
  159. if (TC_H_MIN(parent)) {
  160. cl = cops->get(q, parent);
  161. if (cl == 0)
  162. return -ENOENT;
  163. }
  164. /* And the last stroke */
  165. chain = cops->tcf_chain(q, cl);
  166. err = -EINVAL;
  167. if (chain == NULL)
  168. goto errout;
  169. /* Check the chain for existence of proto-tcf with this priority */
  170. for (back = chain; (tp = *back) != NULL; 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. root_lock = qdisc_root_sleeping_lock(q);
  182. if (tp == NULL) {
  183. /* Proto-tcf does not exist, create new one */
  184. if (tca[TCA_KIND] == NULL || !protocol)
  185. goto errout;
  186. err = -ENOENT;
  187. if (n->nlmsg_type != RTM_NEWTFILTER ||
  188. !(n->nlmsg_flags & NLM_F_CREATE))
  189. goto errout;
  190. /* Create new proto tcf */
  191. err = -ENOBUFS;
  192. tp = kzalloc(sizeof(*tp), GFP_KERNEL);
  193. if (tp == NULL)
  194. goto errout;
  195. err = -ENOENT;
  196. tp_ops = tcf_proto_lookup_ops(tca[TCA_KIND]);
  197. if (tp_ops == NULL) {
  198. #ifdef CONFIG_MODULES
  199. struct nlattr *kind = tca[TCA_KIND];
  200. char name[IFNAMSIZ];
  201. if (kind != NULL &&
  202. nla_strlcpy(name, kind, IFNAMSIZ) < IFNAMSIZ) {
  203. rtnl_unlock();
  204. request_module("cls_%s", name);
  205. rtnl_lock();
  206. tp_ops = tcf_proto_lookup_ops(kind);
  207. /* We dropped the RTNL semaphore in order to
  208. * perform the module load. So, even if we
  209. * succeeded in loading the module we have to
  210. * replay the request. We indicate this using
  211. * -EAGAIN.
  212. */
  213. if (tp_ops != NULL) {
  214. module_put(tp_ops->owner);
  215. err = -EAGAIN;
  216. }
  217. }
  218. #endif
  219. kfree(tp);
  220. goto errout;
  221. }
  222. tp->ops = tp_ops;
  223. tp->protocol = protocol;
  224. tp->prio = nprio ? : TC_H_MAJ(tcf_auto_prio(*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. spin_lock_bh(root_lock);
  241. *back = tp->next;
  242. spin_unlock_bh(root_lock);
  243. tfilter_notify(net, skb, n, tp, fh, RTM_DELTFILTER);
  244. tcf_destroy(tp);
  245. err = 0;
  246. goto errout;
  247. }
  248. err = -ENOENT;
  249. if (n->nlmsg_type != RTM_NEWTFILTER ||
  250. !(n->nlmsg_flags & NLM_F_CREATE))
  251. goto errout;
  252. } else {
  253. switch (n->nlmsg_type) {
  254. case RTM_NEWTFILTER:
  255. err = -EEXIST;
  256. if (n->nlmsg_flags & NLM_F_EXCL) {
  257. if (tp_created)
  258. tcf_destroy(tp);
  259. goto errout;
  260. }
  261. break;
  262. case RTM_DELTFILTER:
  263. err = tp->ops->delete(tp, fh);
  264. if (err == 0)
  265. tfilter_notify(net, skb, n, tp, fh, RTM_DELTFILTER);
  266. goto errout;
  267. case RTM_GETTFILTER:
  268. err = tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER);
  269. goto errout;
  270. default:
  271. err = -EINVAL;
  272. goto errout;
  273. }
  274. }
  275. err = tp->ops->change(skb, tp, cl, t->tcm_handle, tca, &fh);
  276. if (err == 0) {
  277. if (tp_created) {
  278. spin_lock_bh(root_lock);
  279. tp->next = *back;
  280. *back = tp;
  281. spin_unlock_bh(root_lock);
  282. }
  283. tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER);
  284. } else {
  285. if (tp_created)
  286. tcf_destroy(tp);
  287. }
  288. errout:
  289. if (cl)
  290. cops->put(q, cl);
  291. if (err == -EAGAIN)
  292. /* Replay the request. */
  293. goto replay;
  294. return err;
  295. }
  296. static int tcf_fill_node(struct sk_buff *skb, struct tcf_proto *tp,
  297. unsigned long fh, u32 portid, u32 seq, u16 flags, int event)
  298. {
  299. struct tcmsg *tcm;
  300. struct nlmsghdr *nlh;
  301. unsigned char *b = skb_tail_pointer(skb);
  302. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
  303. if (!nlh)
  304. goto out_nlmsg_trim;
  305. tcm = nlmsg_data(nlh);
  306. tcm->tcm_family = AF_UNSPEC;
  307. tcm->tcm__pad1 = 0;
  308. tcm->tcm__pad2 = 0;
  309. tcm->tcm_ifindex = qdisc_dev(tp->q)->ifindex;
  310. tcm->tcm_parent = tp->classid;
  311. tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
  312. if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
  313. goto nla_put_failure;
  314. tcm->tcm_handle = fh;
  315. if (RTM_DELTFILTER != event) {
  316. tcm->tcm_handle = 0;
  317. if (tp->ops->dump && tp->ops->dump(tp, fh, skb, tcm) < 0)
  318. goto nla_put_failure;
  319. }
  320. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  321. return skb->len;
  322. out_nlmsg_trim:
  323. nla_put_failure:
  324. nlmsg_trim(skb, b);
  325. return -1;
  326. }
  327. static int tfilter_notify(struct net *net, struct sk_buff *oskb,
  328. struct nlmsghdr *n, struct tcf_proto *tp,
  329. unsigned long fh, int event)
  330. {
  331. struct sk_buff *skb;
  332. u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
  333. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  334. if (!skb)
  335. return -ENOBUFS;
  336. if (tcf_fill_node(skb, tp, fh, portid, n->nlmsg_seq, 0, event) <= 0) {
  337. kfree_skb(skb);
  338. return -EINVAL;
  339. }
  340. return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  341. n->nlmsg_flags & NLM_F_ECHO);
  342. }
  343. struct tcf_dump_args {
  344. struct tcf_walker w;
  345. struct sk_buff *skb;
  346. struct netlink_callback *cb;
  347. };
  348. static int tcf_node_dump(struct tcf_proto *tp, unsigned long n,
  349. struct tcf_walker *arg)
  350. {
  351. struct tcf_dump_args *a = (void *)arg;
  352. return tcf_fill_node(a->skb, tp, n, NETLINK_CB(a->cb->skb).portid,
  353. a->cb->nlh->nlmsg_seq, NLM_F_MULTI, RTM_NEWTFILTER);
  354. }
  355. /* called with RTNL */
  356. static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
  357. {
  358. struct net *net = sock_net(skb->sk);
  359. int t;
  360. int s_t;
  361. struct net_device *dev;
  362. struct Qdisc *q;
  363. struct tcf_proto *tp, **chain;
  364. struct tcmsg *tcm = nlmsg_data(cb->nlh);
  365. unsigned long cl = 0;
  366. const struct Qdisc_class_ops *cops;
  367. struct tcf_dump_args arg;
  368. if (cb->nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*tcm)))
  369. return skb->len;
  370. dev = __dev_get_by_index(net, tcm->tcm_ifindex);
  371. if (!dev)
  372. return skb->len;
  373. if (!tcm->tcm_parent)
  374. q = dev->qdisc;
  375. else
  376. q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
  377. if (!q)
  378. goto out;
  379. cops = q->ops->cl_ops;
  380. if (!cops)
  381. goto errout;
  382. if (cops->tcf_chain == NULL)
  383. goto errout;
  384. if (TC_H_MIN(tcm->tcm_parent)) {
  385. cl = cops->get(q, tcm->tcm_parent);
  386. if (cl == 0)
  387. goto errout;
  388. }
  389. chain = cops->tcf_chain(q, cl);
  390. if (chain == NULL)
  391. goto errout;
  392. s_t = cb->args[0];
  393. for (tp = *chain, t = 0; tp; tp = tp->next, t++) {
  394. if (t < s_t)
  395. continue;
  396. if (TC_H_MAJ(tcm->tcm_info) &&
  397. TC_H_MAJ(tcm->tcm_info) != tp->prio)
  398. continue;
  399. if (TC_H_MIN(tcm->tcm_info) &&
  400. TC_H_MIN(tcm->tcm_info) != tp->protocol)
  401. continue;
  402. if (t > s_t)
  403. memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
  404. if (cb->args[1] == 0) {
  405. if (tcf_fill_node(skb, tp, 0, NETLINK_CB(cb->skb).portid,
  406. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  407. RTM_NEWTFILTER) <= 0)
  408. break;
  409. cb->args[1] = 1;
  410. }
  411. if (tp->ops->walk == NULL)
  412. continue;
  413. arg.w.fn = tcf_node_dump;
  414. arg.skb = skb;
  415. arg.cb = cb;
  416. arg.w.stop = 0;
  417. arg.w.skip = cb->args[1] - 1;
  418. arg.w.count = 0;
  419. tp->ops->walk(tp, &arg.w);
  420. cb->args[1] = arg.w.count + 1;
  421. if (arg.w.stop)
  422. break;
  423. }
  424. cb->args[0] = t;
  425. errout:
  426. if (cl)
  427. cops->put(q, cl);
  428. out:
  429. return skb->len;
  430. }
  431. void tcf_exts_destroy(struct tcf_proto *tp, struct tcf_exts *exts)
  432. {
  433. #ifdef CONFIG_NET_CLS_ACT
  434. if (exts->action) {
  435. tcf_action_destroy(exts->action, TCA_ACT_UNBIND);
  436. exts->action = NULL;
  437. }
  438. #endif
  439. }
  440. EXPORT_SYMBOL(tcf_exts_destroy);
  441. int tcf_exts_validate(struct tcf_proto *tp, struct nlattr **tb,
  442. struct nlattr *rate_tlv, struct tcf_exts *exts,
  443. const struct tcf_ext_map *map)
  444. {
  445. memset(exts, 0, sizeof(*exts));
  446. #ifdef CONFIG_NET_CLS_ACT
  447. {
  448. struct tc_action *act;
  449. if (map->police && tb[map->police]) {
  450. act = tcf_action_init_1(tb[map->police], rate_tlv,
  451. "police", TCA_ACT_NOREPLACE,
  452. TCA_ACT_BIND);
  453. if (IS_ERR(act))
  454. return PTR_ERR(act);
  455. act->type = TCA_OLD_COMPAT;
  456. exts->action = act;
  457. } else if (map->action && tb[map->action]) {
  458. act = tcf_action_init(tb[map->action], rate_tlv, NULL,
  459. TCA_ACT_NOREPLACE, 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);