cls_api.c 24 KB

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