cls_api.c 24 KB

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