cls_api.c 14 KB

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