cls_api.c 24 KB

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