cls_api.c 14 KB

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