cls_api.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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/err.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/init.h>
  24. #include <linux/kmod.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 LIST_HEAD(tcf_proto_base);
  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 const struct tcf_proto_ops *tcf_proto_lookup_ops(const char *kind)
  38. {
  39. const struct tcf_proto_ops *t, *res = NULL;
  40. if (kind) {
  41. read_lock(&cls_mod_lock);
  42. list_for_each_entry(t, &tcf_proto_base, head) {
  43. if (strcmp(kind, t->kind) == 0) {
  44. if (try_module_get(t->owner))
  45. res = t;
  46. break;
  47. }
  48. }
  49. read_unlock(&cls_mod_lock);
  50. }
  51. return res;
  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;
  57. int rc = -EEXIST;
  58. write_lock(&cls_mod_lock);
  59. list_for_each_entry(t, &tcf_proto_base, head)
  60. if (!strcmp(ops->kind, t->kind))
  61. goto out;
  62. list_add_tail(&ops->head, &tcf_proto_base);
  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;
  72. int rc = -ENOENT;
  73. /* Wait for outstanding call_rcu()s, if any, from a
  74. * tcf_proto_ops's destroy() handler.
  75. */
  76. rcu_barrier();
  77. write_lock(&cls_mod_lock);
  78. list_for_each_entry(t, &tcf_proto_base, head) {
  79. if (t == ops) {
  80. list_del(&t->head);
  81. rc = 0;
  82. break;
  83. }
  84. }
  85. write_unlock(&cls_mod_lock);
  86. return rc;
  87. }
  88. EXPORT_SYMBOL(unregister_tcf_proto_ops);
  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 TC_H_MAJ(first);
  96. }
  97. static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol,
  98. u32 prio, u32 parent, struct Qdisc *q,
  99. struct tcf_chain *chain)
  100. {
  101. struct tcf_proto *tp;
  102. int err;
  103. tp = kzalloc(sizeof(*tp), GFP_KERNEL);
  104. if (!tp)
  105. return ERR_PTR(-ENOBUFS);
  106. err = -ENOENT;
  107. tp->ops = tcf_proto_lookup_ops(kind);
  108. if (!tp->ops) {
  109. #ifdef CONFIG_MODULES
  110. rtnl_unlock();
  111. request_module("cls_%s", kind);
  112. rtnl_lock();
  113. tp->ops = tcf_proto_lookup_ops(kind);
  114. /* We dropped the RTNL semaphore in order to perform
  115. * the module load. So, even if we succeeded in loading
  116. * the module we have to replay the request. We indicate
  117. * this using -EAGAIN.
  118. */
  119. if (tp->ops) {
  120. module_put(tp->ops->owner);
  121. err = -EAGAIN;
  122. } else {
  123. err = -ENOENT;
  124. }
  125. goto errout;
  126. #endif
  127. }
  128. tp->classify = tp->ops->classify;
  129. tp->protocol = protocol;
  130. tp->prio = prio;
  131. tp->classid = parent;
  132. tp->q = q;
  133. tp->chain = chain;
  134. err = tp->ops->init(tp);
  135. if (err) {
  136. module_put(tp->ops->owner);
  137. goto errout;
  138. }
  139. return tp;
  140. errout:
  141. kfree(tp);
  142. return ERR_PTR(err);
  143. }
  144. static void tcf_proto_destroy(struct tcf_proto *tp)
  145. {
  146. tp->ops->destroy(tp);
  147. module_put(tp->ops->owner);
  148. kfree_rcu(tp, rcu);
  149. }
  150. static struct tcf_chain *tcf_chain_create(struct tcf_block *block,
  151. u32 chain_index)
  152. {
  153. struct tcf_chain *chain;
  154. chain = kzalloc(sizeof(*chain), GFP_KERNEL);
  155. if (!chain)
  156. return NULL;
  157. list_add_tail(&chain->list, &block->chain_list);
  158. chain->block = block;
  159. chain->index = chain_index;
  160. chain->refcnt = 1;
  161. return chain;
  162. }
  163. static void tcf_chain_flush(struct tcf_chain *chain)
  164. {
  165. struct tcf_proto *tp;
  166. if (*chain->p_filter_chain)
  167. RCU_INIT_POINTER(*chain->p_filter_chain, NULL);
  168. while ((tp = rtnl_dereference(chain->filter_chain)) != NULL) {
  169. RCU_INIT_POINTER(chain->filter_chain, tp->next);
  170. tcf_proto_destroy(tp);
  171. }
  172. }
  173. static void tcf_chain_destroy(struct tcf_chain *chain)
  174. {
  175. list_del(&chain->list);
  176. tcf_chain_flush(chain);
  177. kfree(chain);
  178. }
  179. struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
  180. bool create)
  181. {
  182. struct tcf_chain *chain;
  183. list_for_each_entry(chain, &block->chain_list, list) {
  184. if (chain->index == chain_index) {
  185. chain->refcnt++;
  186. return chain;
  187. }
  188. }
  189. if (create)
  190. return tcf_chain_create(block, chain_index);
  191. else
  192. return NULL;
  193. }
  194. EXPORT_SYMBOL(tcf_chain_get);
  195. void tcf_chain_put(struct tcf_chain *chain)
  196. {
  197. /* Destroy unused chain, with exception of chain 0, which is the
  198. * default one and has to be always present.
  199. */
  200. if (--chain->refcnt == 0 && !chain->filter_chain && chain->index != 0)
  201. tcf_chain_destroy(chain);
  202. }
  203. EXPORT_SYMBOL(tcf_chain_put);
  204. static void
  205. tcf_chain_filter_chain_ptr_set(struct tcf_chain *chain,
  206. struct tcf_proto __rcu **p_filter_chain)
  207. {
  208. chain->p_filter_chain = p_filter_chain;
  209. }
  210. int tcf_block_get(struct tcf_block **p_block,
  211. struct tcf_proto __rcu **p_filter_chain)
  212. {
  213. struct tcf_block *block = kzalloc(sizeof(*block), GFP_KERNEL);
  214. struct tcf_chain *chain;
  215. int err;
  216. if (!block)
  217. return -ENOMEM;
  218. INIT_LIST_HEAD(&block->chain_list);
  219. /* Create chain 0 by default, it has to be always present. */
  220. chain = tcf_chain_create(block, 0);
  221. if (!chain) {
  222. err = -ENOMEM;
  223. goto err_chain_create;
  224. }
  225. tcf_chain_filter_chain_ptr_set(chain, p_filter_chain);
  226. *p_block = block;
  227. return 0;
  228. err_chain_create:
  229. kfree(block);
  230. return err;
  231. }
  232. EXPORT_SYMBOL(tcf_block_get);
  233. void tcf_block_put(struct tcf_block *block)
  234. {
  235. struct tcf_chain *chain, *tmp;
  236. if (!block)
  237. return;
  238. list_for_each_entry_safe(chain, tmp, &block->chain_list, list)
  239. tcf_chain_destroy(chain);
  240. kfree(block);
  241. }
  242. EXPORT_SYMBOL(tcf_block_put);
  243. /* Main classifier routine: scans classifier chain attached
  244. * to this qdisc, (optionally) tests for protocol and asks
  245. * specific classifiers.
  246. */
  247. int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  248. struct tcf_result *res, bool compat_mode)
  249. {
  250. __be16 protocol = tc_skb_protocol(skb);
  251. #ifdef CONFIG_NET_CLS_ACT
  252. const int max_reclassify_loop = 4;
  253. const struct tcf_proto *orig_tp = tp;
  254. const struct tcf_proto *first_tp;
  255. int limit = 0;
  256. reclassify:
  257. #endif
  258. for (; tp; tp = rcu_dereference_bh(tp->next)) {
  259. int err;
  260. if (tp->protocol != protocol &&
  261. tp->protocol != htons(ETH_P_ALL))
  262. continue;
  263. err = tp->classify(skb, tp, res);
  264. #ifdef CONFIG_NET_CLS_ACT
  265. if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) {
  266. first_tp = orig_tp;
  267. goto reset;
  268. } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) {
  269. first_tp = res->goto_tp;
  270. goto reset;
  271. }
  272. #endif
  273. if (err >= 0)
  274. return err;
  275. }
  276. return TC_ACT_UNSPEC; /* signal: continue lookup */
  277. #ifdef CONFIG_NET_CLS_ACT
  278. reset:
  279. if (unlikely(limit++ >= max_reclassify_loop)) {
  280. net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n",
  281. tp->q->ops->id, tp->prio & 0xffff,
  282. ntohs(tp->protocol));
  283. return TC_ACT_SHOT;
  284. }
  285. tp = first_tp;
  286. protocol = tc_skb_protocol(skb);
  287. goto reclassify;
  288. #endif
  289. }
  290. EXPORT_SYMBOL(tcf_classify);
  291. struct tcf_chain_info {
  292. struct tcf_proto __rcu **pprev;
  293. struct tcf_proto __rcu *next;
  294. };
  295. static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info)
  296. {
  297. return rtnl_dereference(*chain_info->pprev);
  298. }
  299. static void tcf_chain_tp_insert(struct tcf_chain *chain,
  300. struct tcf_chain_info *chain_info,
  301. struct tcf_proto *tp)
  302. {
  303. if (chain->p_filter_chain &&
  304. *chain_info->pprev == chain->filter_chain)
  305. rcu_assign_pointer(*chain->p_filter_chain, tp);
  306. RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info));
  307. rcu_assign_pointer(*chain_info->pprev, tp);
  308. }
  309. static void tcf_chain_tp_remove(struct tcf_chain *chain,
  310. struct tcf_chain_info *chain_info,
  311. struct tcf_proto *tp)
  312. {
  313. struct tcf_proto *next = rtnl_dereference(chain_info->next);
  314. if (chain->p_filter_chain && tp == chain->filter_chain)
  315. RCU_INIT_POINTER(*chain->p_filter_chain, next);
  316. RCU_INIT_POINTER(*chain_info->pprev, next);
  317. }
  318. static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
  319. struct tcf_chain_info *chain_info,
  320. u32 protocol, u32 prio,
  321. bool prio_allocate)
  322. {
  323. struct tcf_proto **pprev;
  324. struct tcf_proto *tp;
  325. /* Check the chain for existence of proto-tcf with this priority */
  326. for (pprev = &chain->filter_chain;
  327. (tp = rtnl_dereference(*pprev)); pprev = &tp->next) {
  328. if (tp->prio >= prio) {
  329. if (tp->prio == prio) {
  330. if (prio_allocate ||
  331. (tp->protocol != protocol && protocol))
  332. return ERR_PTR(-EINVAL);
  333. } else {
  334. tp = NULL;
  335. }
  336. break;
  337. }
  338. }
  339. chain_info->pprev = pprev;
  340. chain_info->next = tp ? tp->next : NULL;
  341. return tp;
  342. }
  343. static int tcf_fill_node(struct net *net, struct sk_buff *skb,
  344. struct tcf_proto *tp, void *fh, u32 portid,
  345. u32 seq, u16 flags, int event)
  346. {
  347. struct tcmsg *tcm;
  348. struct nlmsghdr *nlh;
  349. unsigned char *b = skb_tail_pointer(skb);
  350. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags);
  351. if (!nlh)
  352. goto out_nlmsg_trim;
  353. tcm = nlmsg_data(nlh);
  354. tcm->tcm_family = AF_UNSPEC;
  355. tcm->tcm__pad1 = 0;
  356. tcm->tcm__pad2 = 0;
  357. tcm->tcm_ifindex = qdisc_dev(tp->q)->ifindex;
  358. tcm->tcm_parent = tp->classid;
  359. tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol);
  360. if (nla_put_string(skb, TCA_KIND, tp->ops->kind))
  361. goto nla_put_failure;
  362. if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index))
  363. goto nla_put_failure;
  364. if (!fh) {
  365. tcm->tcm_handle = 0;
  366. } else {
  367. if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0)
  368. goto nla_put_failure;
  369. }
  370. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  371. return skb->len;
  372. out_nlmsg_trim:
  373. nla_put_failure:
  374. nlmsg_trim(skb, b);
  375. return -1;
  376. }
  377. static int tfilter_notify(struct net *net, struct sk_buff *oskb,
  378. struct nlmsghdr *n, struct tcf_proto *tp,
  379. void *fh, int event, bool unicast)
  380. {
  381. struct sk_buff *skb;
  382. u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
  383. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  384. if (!skb)
  385. return -ENOBUFS;
  386. if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq,
  387. n->nlmsg_flags, event) <= 0) {
  388. kfree_skb(skb);
  389. return -EINVAL;
  390. }
  391. if (unicast)
  392. return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
  393. return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  394. n->nlmsg_flags & NLM_F_ECHO);
  395. }
  396. static int tfilter_del_notify(struct net *net, struct sk_buff *oskb,
  397. struct nlmsghdr *n, struct tcf_proto *tp,
  398. void *fh, bool unicast, bool *last)
  399. {
  400. struct sk_buff *skb;
  401. u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
  402. int err;
  403. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  404. if (!skb)
  405. return -ENOBUFS;
  406. if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq,
  407. n->nlmsg_flags, RTM_DELTFILTER) <= 0) {
  408. kfree_skb(skb);
  409. return -EINVAL;
  410. }
  411. err = tp->ops->delete(tp, fh, last);
  412. if (err) {
  413. kfree_skb(skb);
  414. return err;
  415. }
  416. if (unicast)
  417. return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT);
  418. return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  419. n->nlmsg_flags & NLM_F_ECHO);
  420. }
  421. static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb,
  422. struct nlmsghdr *n,
  423. struct tcf_chain *chain, int event)
  424. {
  425. struct tcf_proto *tp;
  426. for (tp = rtnl_dereference(chain->filter_chain);
  427. tp; tp = rtnl_dereference(tp->next))
  428. tfilter_notify(net, oskb, n, tp, 0, event, false);
  429. }
  430. /* Add/change/delete/get a filter node */
  431. static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n,
  432. struct netlink_ext_ack *extack)
  433. {
  434. struct net *net = sock_net(skb->sk);
  435. struct nlattr *tca[TCA_MAX + 1];
  436. struct tcmsg *t;
  437. u32 protocol;
  438. u32 prio;
  439. bool prio_allocate;
  440. u32 parent;
  441. u32 chain_index;
  442. struct net_device *dev;
  443. struct Qdisc *q;
  444. struct tcf_chain_info chain_info;
  445. struct tcf_chain *chain = NULL;
  446. struct tcf_block *block;
  447. struct tcf_proto *tp;
  448. const struct Qdisc_class_ops *cops;
  449. unsigned long cl;
  450. void *fh;
  451. int err;
  452. int tp_created;
  453. if ((n->nlmsg_type != RTM_GETTFILTER) &&
  454. !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
  455. return -EPERM;
  456. replay:
  457. tp_created = 0;
  458. err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack);
  459. if (err < 0)
  460. return err;
  461. t = nlmsg_data(n);
  462. protocol = TC_H_MIN(t->tcm_info);
  463. prio = TC_H_MAJ(t->tcm_info);
  464. prio_allocate = false;
  465. parent = t->tcm_parent;
  466. cl = 0;
  467. if (prio == 0) {
  468. switch (n->nlmsg_type) {
  469. case RTM_DELTFILTER:
  470. if (protocol || t->tcm_handle || tca[TCA_KIND])
  471. return -ENOENT;
  472. break;
  473. case RTM_NEWTFILTER:
  474. /* If no priority is provided by the user,
  475. * we allocate one.
  476. */
  477. if (n->nlmsg_flags & NLM_F_CREATE) {
  478. prio = TC_H_MAKE(0x80000000U, 0U);
  479. prio_allocate = true;
  480. break;
  481. }
  482. /* fall-through */
  483. default:
  484. return -ENOENT;
  485. }
  486. }
  487. /* Find head of filter chain. */
  488. /* Find link */
  489. dev = __dev_get_by_index(net, t->tcm_ifindex);
  490. if (dev == NULL)
  491. return -ENODEV;
  492. /* Find qdisc */
  493. if (!parent) {
  494. q = dev->qdisc;
  495. parent = q->handle;
  496. } else {
  497. q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent));
  498. if (q == NULL)
  499. return -EINVAL;
  500. }
  501. /* Is it classful? */
  502. cops = q->ops->cl_ops;
  503. if (!cops)
  504. return -EINVAL;
  505. if (!cops->tcf_block)
  506. return -EOPNOTSUPP;
  507. /* Do we search for filter, attached to class? */
  508. if (TC_H_MIN(parent)) {
  509. cl = cops->get(q, parent);
  510. if (cl == 0)
  511. return -ENOENT;
  512. }
  513. /* And the last stroke */
  514. block = cops->tcf_block(q, cl);
  515. if (!block) {
  516. err = -EINVAL;
  517. goto errout;
  518. }
  519. chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0;
  520. if (chain_index > TC_ACT_EXT_VAL_MASK) {
  521. err = -EINVAL;
  522. goto errout;
  523. }
  524. chain = tcf_chain_get(block, chain_index,
  525. n->nlmsg_type == RTM_NEWTFILTER);
  526. if (!chain) {
  527. err = n->nlmsg_type == RTM_NEWTFILTER ? -ENOMEM : -EINVAL;
  528. goto errout;
  529. }
  530. if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) {
  531. tfilter_notify_chain(net, skb, n, chain, RTM_DELTFILTER);
  532. tcf_chain_flush(chain);
  533. err = 0;
  534. goto errout;
  535. }
  536. tp = tcf_chain_tp_find(chain, &chain_info, protocol,
  537. prio, prio_allocate);
  538. if (IS_ERR(tp)) {
  539. err = PTR_ERR(tp);
  540. goto errout;
  541. }
  542. if (tp == NULL) {
  543. /* Proto-tcf does not exist, create new one */
  544. if (tca[TCA_KIND] == NULL || !protocol) {
  545. err = -EINVAL;
  546. goto errout;
  547. }
  548. if (n->nlmsg_type != RTM_NEWTFILTER ||
  549. !(n->nlmsg_flags & NLM_F_CREATE)) {
  550. err = -ENOENT;
  551. goto errout;
  552. }
  553. if (prio_allocate)
  554. prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info));
  555. tp = tcf_proto_create(nla_data(tca[TCA_KIND]),
  556. protocol, prio, parent, q, chain);
  557. if (IS_ERR(tp)) {
  558. err = PTR_ERR(tp);
  559. goto errout;
  560. }
  561. tp_created = 1;
  562. } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
  563. err = -EINVAL;
  564. goto errout;
  565. }
  566. fh = tp->ops->get(tp, t->tcm_handle);
  567. if (!fh) {
  568. if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) {
  569. tcf_chain_tp_remove(chain, &chain_info, tp);
  570. tfilter_notify(net, skb, n, tp, fh,
  571. RTM_DELTFILTER, false);
  572. tcf_proto_destroy(tp);
  573. err = 0;
  574. goto errout;
  575. }
  576. if (n->nlmsg_type != RTM_NEWTFILTER ||
  577. !(n->nlmsg_flags & NLM_F_CREATE)) {
  578. err = -ENOENT;
  579. goto errout;
  580. }
  581. } else {
  582. bool last;
  583. switch (n->nlmsg_type) {
  584. case RTM_NEWTFILTER:
  585. if (n->nlmsg_flags & NLM_F_EXCL) {
  586. if (tp_created)
  587. tcf_proto_destroy(tp);
  588. err = -EEXIST;
  589. goto errout;
  590. }
  591. break;
  592. case RTM_DELTFILTER:
  593. err = tfilter_del_notify(net, skb, n, tp, fh, false,
  594. &last);
  595. if (err)
  596. goto errout;
  597. if (last) {
  598. tcf_chain_tp_remove(chain, &chain_info, tp);
  599. tcf_proto_destroy(tp);
  600. }
  601. goto errout;
  602. case RTM_GETTFILTER:
  603. err = tfilter_notify(net, skb, n, tp, fh,
  604. RTM_NEWTFILTER, true);
  605. goto errout;
  606. default:
  607. err = -EINVAL;
  608. goto errout;
  609. }
  610. }
  611. err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh,
  612. n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE);
  613. if (err == 0) {
  614. if (tp_created)
  615. tcf_chain_tp_insert(chain, &chain_info, tp);
  616. tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER, false);
  617. } else {
  618. if (tp_created)
  619. tcf_proto_destroy(tp);
  620. }
  621. errout:
  622. if (chain)
  623. tcf_chain_put(chain);
  624. if (cl)
  625. cops->put(q, cl);
  626. if (err == -EAGAIN)
  627. /* Replay the request. */
  628. goto replay;
  629. return err;
  630. }
  631. struct tcf_dump_args {
  632. struct tcf_walker w;
  633. struct sk_buff *skb;
  634. struct netlink_callback *cb;
  635. };
  636. static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg)
  637. {
  638. struct tcf_dump_args *a = (void *)arg;
  639. struct net *net = sock_net(a->skb->sk);
  640. return tcf_fill_node(net, a->skb, tp, n, NETLINK_CB(a->cb->skb).portid,
  641. a->cb->nlh->nlmsg_seq, NLM_F_MULTI,
  642. RTM_NEWTFILTER);
  643. }
  644. static bool tcf_chain_dump(struct tcf_chain *chain, struct sk_buff *skb,
  645. struct netlink_callback *cb,
  646. long index_start, long *p_index)
  647. {
  648. struct net *net = sock_net(skb->sk);
  649. struct tcmsg *tcm = nlmsg_data(cb->nlh);
  650. struct tcf_dump_args arg;
  651. struct tcf_proto *tp;
  652. for (tp = rtnl_dereference(chain->filter_chain);
  653. tp; tp = rtnl_dereference(tp->next), (*p_index)++) {
  654. if (*p_index < index_start)
  655. continue;
  656. if (TC_H_MAJ(tcm->tcm_info) &&
  657. TC_H_MAJ(tcm->tcm_info) != tp->prio)
  658. continue;
  659. if (TC_H_MIN(tcm->tcm_info) &&
  660. TC_H_MIN(tcm->tcm_info) != tp->protocol)
  661. continue;
  662. if (*p_index > index_start)
  663. memset(&cb->args[1], 0,
  664. sizeof(cb->args) - sizeof(cb->args[0]));
  665. if (cb->args[1] == 0) {
  666. if (tcf_fill_node(net, skb, tp, 0,
  667. NETLINK_CB(cb->skb).portid,
  668. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  669. RTM_NEWTFILTER) <= 0)
  670. return false;
  671. cb->args[1] = 1;
  672. }
  673. if (!tp->ops->walk)
  674. continue;
  675. arg.w.fn = tcf_node_dump;
  676. arg.skb = skb;
  677. arg.cb = cb;
  678. arg.w.stop = 0;
  679. arg.w.skip = cb->args[1] - 1;
  680. arg.w.count = 0;
  681. tp->ops->walk(tp, &arg.w);
  682. cb->args[1] = arg.w.count + 1;
  683. if (arg.w.stop)
  684. return false;
  685. }
  686. return true;
  687. }
  688. /* called with RTNL */
  689. static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb)
  690. {
  691. struct net *net = sock_net(skb->sk);
  692. struct nlattr *tca[TCA_MAX + 1];
  693. struct net_device *dev;
  694. struct Qdisc *q;
  695. struct tcf_block *block;
  696. struct tcf_chain *chain;
  697. struct tcmsg *tcm = nlmsg_data(cb->nlh);
  698. unsigned long cl = 0;
  699. const struct Qdisc_class_ops *cops;
  700. long index_start;
  701. long index;
  702. int err;
  703. if (nlmsg_len(cb->nlh) < sizeof(*tcm))
  704. return skb->len;
  705. err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL);
  706. if (err)
  707. return err;
  708. dev = __dev_get_by_index(net, tcm->tcm_ifindex);
  709. if (!dev)
  710. return skb->len;
  711. if (!tcm->tcm_parent)
  712. q = dev->qdisc;
  713. else
  714. q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent));
  715. if (!q)
  716. goto out;
  717. cops = q->ops->cl_ops;
  718. if (!cops)
  719. goto errout;
  720. if (!cops->tcf_block)
  721. goto errout;
  722. if (TC_H_MIN(tcm->tcm_parent)) {
  723. cl = cops->get(q, tcm->tcm_parent);
  724. if (cl == 0)
  725. goto errout;
  726. }
  727. block = cops->tcf_block(q, cl);
  728. if (!block)
  729. goto errout;
  730. index_start = cb->args[0];
  731. index = 0;
  732. list_for_each_entry(chain, &block->chain_list, list) {
  733. if (tca[TCA_CHAIN] &&
  734. nla_get_u32(tca[TCA_CHAIN]) != chain->index)
  735. continue;
  736. if (!tcf_chain_dump(chain, skb, cb, index_start, &index))
  737. break;
  738. }
  739. cb->args[0] = index;
  740. errout:
  741. if (cl)
  742. cops->put(q, cl);
  743. out:
  744. return skb->len;
  745. }
  746. void tcf_exts_destroy(struct tcf_exts *exts)
  747. {
  748. #ifdef CONFIG_NET_CLS_ACT
  749. LIST_HEAD(actions);
  750. tcf_exts_to_list(exts, &actions);
  751. tcf_action_destroy(&actions, TCA_ACT_UNBIND);
  752. kfree(exts->actions);
  753. exts->nr_actions = 0;
  754. #endif
  755. }
  756. EXPORT_SYMBOL(tcf_exts_destroy);
  757. int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
  758. struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr)
  759. {
  760. #ifdef CONFIG_NET_CLS_ACT
  761. {
  762. struct tc_action *act;
  763. if (exts->police && tb[exts->police]) {
  764. act = tcf_action_init_1(net, tp, tb[exts->police],
  765. rate_tlv, "police", ovr,
  766. TCA_ACT_BIND);
  767. if (IS_ERR(act))
  768. return PTR_ERR(act);
  769. act->type = exts->type = TCA_OLD_COMPAT;
  770. exts->actions[0] = act;
  771. exts->nr_actions = 1;
  772. } else if (exts->action && tb[exts->action]) {
  773. LIST_HEAD(actions);
  774. int err, i = 0;
  775. err = tcf_action_init(net, tp, tb[exts->action],
  776. rate_tlv, NULL, ovr, TCA_ACT_BIND,
  777. &actions);
  778. if (err)
  779. return err;
  780. list_for_each_entry(act, &actions, list)
  781. exts->actions[i++] = act;
  782. exts->nr_actions = i;
  783. }
  784. }
  785. #else
  786. if ((exts->action && tb[exts->action]) ||
  787. (exts->police && tb[exts->police]))
  788. return -EOPNOTSUPP;
  789. #endif
  790. return 0;
  791. }
  792. EXPORT_SYMBOL(tcf_exts_validate);
  793. void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src)
  794. {
  795. #ifdef CONFIG_NET_CLS_ACT
  796. struct tcf_exts old = *dst;
  797. *dst = *src;
  798. tcf_exts_destroy(&old);
  799. #endif
  800. }
  801. EXPORT_SYMBOL(tcf_exts_change);
  802. #ifdef CONFIG_NET_CLS_ACT
  803. static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts)
  804. {
  805. if (exts->nr_actions == 0)
  806. return NULL;
  807. else
  808. return exts->actions[0];
  809. }
  810. #endif
  811. int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts)
  812. {
  813. #ifdef CONFIG_NET_CLS_ACT
  814. struct nlattr *nest;
  815. if (exts->action && tcf_exts_has_actions(exts)) {
  816. /*
  817. * again for backward compatible mode - we want
  818. * to work with both old and new modes of entering
  819. * tc data even if iproute2 was newer - jhs
  820. */
  821. if (exts->type != TCA_OLD_COMPAT) {
  822. LIST_HEAD(actions);
  823. nest = nla_nest_start(skb, exts->action);
  824. if (nest == NULL)
  825. goto nla_put_failure;
  826. tcf_exts_to_list(exts, &actions);
  827. if (tcf_action_dump(skb, &actions, 0, 0) < 0)
  828. goto nla_put_failure;
  829. nla_nest_end(skb, nest);
  830. } else if (exts->police) {
  831. struct tc_action *act = tcf_exts_first_act(exts);
  832. nest = nla_nest_start(skb, exts->police);
  833. if (nest == NULL || !act)
  834. goto nla_put_failure;
  835. if (tcf_action_dump_old(skb, act, 0, 0) < 0)
  836. goto nla_put_failure;
  837. nla_nest_end(skb, nest);
  838. }
  839. }
  840. return 0;
  841. nla_put_failure:
  842. nla_nest_cancel(skb, nest);
  843. return -1;
  844. #else
  845. return 0;
  846. #endif
  847. }
  848. EXPORT_SYMBOL(tcf_exts_dump);
  849. int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts)
  850. {
  851. #ifdef CONFIG_NET_CLS_ACT
  852. struct tc_action *a = tcf_exts_first_act(exts);
  853. if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0)
  854. return -1;
  855. #endif
  856. return 0;
  857. }
  858. EXPORT_SYMBOL(tcf_exts_dump_stats);
  859. int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts,
  860. struct net_device **hw_dev)
  861. {
  862. #ifdef CONFIG_NET_CLS_ACT
  863. const struct tc_action *a;
  864. LIST_HEAD(actions);
  865. if (!tcf_exts_has_actions(exts))
  866. return -EINVAL;
  867. tcf_exts_to_list(exts, &actions);
  868. list_for_each_entry(a, &actions, list) {
  869. if (a->ops->get_dev) {
  870. a->ops->get_dev(a, dev_net(dev), hw_dev);
  871. break;
  872. }
  873. }
  874. if (*hw_dev)
  875. return 0;
  876. #endif
  877. return -EOPNOTSUPP;
  878. }
  879. EXPORT_SYMBOL(tcf_exts_get_dev);
  880. static int __init tc_filter_init(void)
  881. {
  882. rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, 0);
  883. rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, 0);
  884. rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter,
  885. tc_dump_tfilter, 0);
  886. return 0;
  887. }
  888. subsys_initcall(tc_filter_init);