cls_api.c 23 KB

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