cls_api.c 13 KB

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