cls_api.c 13 KB

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