cls_api.c 13 KB

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