act_api.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  1. /*
  2. * net/sched/act_api.c Packet action 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. * Author: Jamal Hadi Salim
  10. *
  11. *
  12. */
  13. #include <linux/types.h>
  14. #include <linux/kernel.h>
  15. #include <linux/string.h>
  16. #include <linux/errno.h>
  17. #include <linux/slab.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/init.h>
  20. #include <linux/kmod.h>
  21. #include <linux/err.h>
  22. #include <linux/module.h>
  23. #include <linux/rhashtable.h>
  24. #include <linux/list.h>
  25. #include <net/net_namespace.h>
  26. #include <net/sock.h>
  27. #include <net/sch_generic.h>
  28. #include <net/pkt_cls.h>
  29. #include <net/act_api.h>
  30. #include <net/netlink.h>
  31. static int tcf_action_goto_chain_init(struct tc_action *a, struct tcf_proto *tp)
  32. {
  33. u32 chain_index = a->tcfa_action & TC_ACT_EXT_VAL_MASK;
  34. if (!tp)
  35. return -EINVAL;
  36. a->goto_chain = tcf_chain_get(tp->chain->block, chain_index, true);
  37. if (!a->goto_chain)
  38. return -ENOMEM;
  39. return 0;
  40. }
  41. static void tcf_action_goto_chain_fini(struct tc_action *a)
  42. {
  43. tcf_chain_put(a->goto_chain);
  44. }
  45. static void tcf_action_goto_chain_exec(const struct tc_action *a,
  46. struct tcf_result *res)
  47. {
  48. const struct tcf_chain *chain = a->goto_chain;
  49. res->goto_tp = rcu_dereference_bh(chain->filter_chain);
  50. }
  51. /* XXX: For standalone actions, we don't need a RCU grace period either, because
  52. * actions are always connected to filters and filters are already destroyed in
  53. * RCU callbacks, so after a RCU grace period actions are already disconnected
  54. * from filters. Readers later can not find us.
  55. */
  56. static void free_tcf(struct tc_action *p)
  57. {
  58. free_percpu(p->cpu_bstats);
  59. free_percpu(p->cpu_qstats);
  60. if (p->act_cookie) {
  61. kfree(p->act_cookie->data);
  62. kfree(p->act_cookie);
  63. }
  64. if (p->goto_chain)
  65. tcf_action_goto_chain_fini(p);
  66. kfree(p);
  67. }
  68. static void tcf_idr_remove(struct tcf_idrinfo *idrinfo, struct tc_action *p)
  69. {
  70. spin_lock(&idrinfo->lock);
  71. idr_remove(&idrinfo->action_idr, p->tcfa_index);
  72. spin_unlock(&idrinfo->lock);
  73. gen_kill_estimator(&p->tcfa_rate_est);
  74. free_tcf(p);
  75. }
  76. int __tcf_idr_release(struct tc_action *p, bool bind, bool strict)
  77. {
  78. int ret = 0;
  79. ASSERT_RTNL();
  80. if (p) {
  81. if (bind)
  82. p->tcfa_bindcnt--;
  83. else if (strict && p->tcfa_bindcnt > 0)
  84. return -EPERM;
  85. p->tcfa_refcnt--;
  86. if (p->tcfa_bindcnt <= 0 && p->tcfa_refcnt <= 0) {
  87. if (p->ops->cleanup)
  88. p->ops->cleanup(p);
  89. tcf_idr_remove(p->idrinfo, p);
  90. ret = ACT_P_DELETED;
  91. }
  92. }
  93. return ret;
  94. }
  95. EXPORT_SYMBOL(__tcf_idr_release);
  96. static size_t tcf_action_shared_attrs_size(const struct tc_action *act)
  97. {
  98. u32 cookie_len = 0;
  99. if (act->act_cookie)
  100. cookie_len = nla_total_size(act->act_cookie->len);
  101. return nla_total_size(0) /* action number nested */
  102. + nla_total_size(IFNAMSIZ) /* TCA_ACT_KIND */
  103. + cookie_len /* TCA_ACT_COOKIE */
  104. + nla_total_size(0) /* TCA_ACT_STATS nested */
  105. /* TCA_STATS_BASIC */
  106. + nla_total_size_64bit(sizeof(struct gnet_stats_basic))
  107. /* TCA_STATS_QUEUE */
  108. + nla_total_size_64bit(sizeof(struct gnet_stats_queue))
  109. + nla_total_size(0) /* TCA_OPTIONS nested */
  110. + nla_total_size(sizeof(struct tcf_t)); /* TCA_GACT_TM */
  111. }
  112. static size_t tcf_action_full_attrs_size(size_t sz)
  113. {
  114. return NLMSG_HDRLEN /* struct nlmsghdr */
  115. + sizeof(struct tcamsg)
  116. + nla_total_size(0) /* TCA_ACT_TAB nested */
  117. + sz;
  118. }
  119. static size_t tcf_action_fill_size(const struct tc_action *act)
  120. {
  121. size_t sz = tcf_action_shared_attrs_size(act);
  122. if (act->ops->get_fill_size)
  123. return act->ops->get_fill_size(act) + sz;
  124. return sz;
  125. }
  126. static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
  127. struct netlink_callback *cb)
  128. {
  129. int err = 0, index = -1, s_i = 0, n_i = 0;
  130. u32 act_flags = cb->args[2];
  131. unsigned long jiffy_since = cb->args[3];
  132. struct nlattr *nest;
  133. struct idr *idr = &idrinfo->action_idr;
  134. struct tc_action *p;
  135. unsigned long id = 1;
  136. spin_lock(&idrinfo->lock);
  137. s_i = cb->args[0];
  138. idr_for_each_entry_ul(idr, p, id) {
  139. index++;
  140. if (index < s_i)
  141. continue;
  142. if (jiffy_since &&
  143. time_after(jiffy_since,
  144. (unsigned long)p->tcfa_tm.lastuse))
  145. continue;
  146. nest = nla_nest_start(skb, n_i);
  147. if (!nest) {
  148. index--;
  149. goto nla_put_failure;
  150. }
  151. err = tcf_action_dump_1(skb, p, 0, 0);
  152. if (err < 0) {
  153. index--;
  154. nlmsg_trim(skb, nest);
  155. goto done;
  156. }
  157. nla_nest_end(skb, nest);
  158. n_i++;
  159. if (!(act_flags & TCA_FLAG_LARGE_DUMP_ON) &&
  160. n_i >= TCA_ACT_MAX_PRIO)
  161. goto done;
  162. }
  163. done:
  164. if (index >= 0)
  165. cb->args[0] = index + 1;
  166. spin_unlock(&idrinfo->lock);
  167. if (n_i) {
  168. if (act_flags & TCA_FLAG_LARGE_DUMP_ON)
  169. cb->args[1] = n_i;
  170. }
  171. return n_i;
  172. nla_put_failure:
  173. nla_nest_cancel(skb, nest);
  174. goto done;
  175. }
  176. static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb,
  177. const struct tc_action_ops *ops)
  178. {
  179. struct nlattr *nest;
  180. int n_i = 0;
  181. int ret = -EINVAL;
  182. struct idr *idr = &idrinfo->action_idr;
  183. struct tc_action *p;
  184. unsigned long id = 1;
  185. nest = nla_nest_start(skb, 0);
  186. if (nest == NULL)
  187. goto nla_put_failure;
  188. if (nla_put_string(skb, TCA_KIND, ops->kind))
  189. goto nla_put_failure;
  190. idr_for_each_entry_ul(idr, p, id) {
  191. ret = __tcf_idr_release(p, false, true);
  192. if (ret == ACT_P_DELETED) {
  193. module_put(ops->owner);
  194. n_i++;
  195. } else if (ret < 0) {
  196. goto nla_put_failure;
  197. }
  198. }
  199. if (nla_put_u32(skb, TCA_FCNT, n_i))
  200. goto nla_put_failure;
  201. nla_nest_end(skb, nest);
  202. return n_i;
  203. nla_put_failure:
  204. nla_nest_cancel(skb, nest);
  205. return ret;
  206. }
  207. int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb,
  208. struct netlink_callback *cb, int type,
  209. const struct tc_action_ops *ops,
  210. struct netlink_ext_ack *extack)
  211. {
  212. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  213. if (type == RTM_DELACTION) {
  214. return tcf_del_walker(idrinfo, skb, ops);
  215. } else if (type == RTM_GETACTION) {
  216. return tcf_dump_walker(idrinfo, skb, cb);
  217. } else {
  218. WARN(1, "tcf_generic_walker: unknown command %d\n", type);
  219. NL_SET_ERR_MSG(extack, "tcf_generic_walker: unknown command");
  220. return -EINVAL;
  221. }
  222. }
  223. EXPORT_SYMBOL(tcf_generic_walker);
  224. static struct tc_action *tcf_idr_lookup(u32 index, struct tcf_idrinfo *idrinfo)
  225. {
  226. struct tc_action *p = NULL;
  227. spin_lock(&idrinfo->lock);
  228. p = idr_find(&idrinfo->action_idr, index);
  229. spin_unlock(&idrinfo->lock);
  230. return p;
  231. }
  232. int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index)
  233. {
  234. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  235. struct tc_action *p = tcf_idr_lookup(index, idrinfo);
  236. if (p) {
  237. *a = p;
  238. return 1;
  239. }
  240. return 0;
  241. }
  242. EXPORT_SYMBOL(tcf_idr_search);
  243. bool tcf_idr_check(struct tc_action_net *tn, u32 index, struct tc_action **a,
  244. int bind)
  245. {
  246. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  247. struct tc_action *p = tcf_idr_lookup(index, idrinfo);
  248. if (index && p) {
  249. if (bind)
  250. p->tcfa_bindcnt++;
  251. p->tcfa_refcnt++;
  252. *a = p;
  253. return true;
  254. }
  255. return false;
  256. }
  257. EXPORT_SYMBOL(tcf_idr_check);
  258. int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est,
  259. struct tc_action **a, const struct tc_action_ops *ops,
  260. int bind, bool cpustats)
  261. {
  262. struct tc_action *p = kzalloc(ops->size, GFP_KERNEL);
  263. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  264. struct idr *idr = &idrinfo->action_idr;
  265. int err = -ENOMEM;
  266. if (unlikely(!p))
  267. return -ENOMEM;
  268. p->tcfa_refcnt = 1;
  269. if (bind)
  270. p->tcfa_bindcnt = 1;
  271. if (cpustats) {
  272. p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu);
  273. if (!p->cpu_bstats)
  274. goto err1;
  275. p->cpu_qstats = alloc_percpu(struct gnet_stats_queue);
  276. if (!p->cpu_qstats)
  277. goto err2;
  278. }
  279. spin_lock_init(&p->tcfa_lock);
  280. idr_preload(GFP_KERNEL);
  281. spin_lock(&idrinfo->lock);
  282. /* user doesn't specify an index */
  283. if (!index) {
  284. index = 1;
  285. err = idr_alloc_u32(idr, NULL, &index, UINT_MAX, GFP_ATOMIC);
  286. } else {
  287. err = idr_alloc_u32(idr, NULL, &index, index, GFP_ATOMIC);
  288. }
  289. spin_unlock(&idrinfo->lock);
  290. idr_preload_end();
  291. if (err)
  292. goto err3;
  293. p->tcfa_index = index;
  294. p->tcfa_tm.install = jiffies;
  295. p->tcfa_tm.lastuse = jiffies;
  296. p->tcfa_tm.firstuse = 0;
  297. if (est) {
  298. err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats,
  299. &p->tcfa_rate_est,
  300. &p->tcfa_lock, NULL, est);
  301. if (err)
  302. goto err4;
  303. }
  304. p->idrinfo = idrinfo;
  305. p->ops = ops;
  306. INIT_LIST_HEAD(&p->list);
  307. *a = p;
  308. return 0;
  309. err4:
  310. idr_remove(idr, index);
  311. err3:
  312. free_percpu(p->cpu_qstats);
  313. err2:
  314. free_percpu(p->cpu_bstats);
  315. err1:
  316. kfree(p);
  317. return err;
  318. }
  319. EXPORT_SYMBOL(tcf_idr_create);
  320. void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a)
  321. {
  322. struct tcf_idrinfo *idrinfo = tn->idrinfo;
  323. spin_lock(&idrinfo->lock);
  324. idr_replace(&idrinfo->action_idr, a, a->tcfa_index);
  325. spin_unlock(&idrinfo->lock);
  326. }
  327. EXPORT_SYMBOL(tcf_idr_insert);
  328. void tcf_idrinfo_destroy(const struct tc_action_ops *ops,
  329. struct tcf_idrinfo *idrinfo)
  330. {
  331. struct idr *idr = &idrinfo->action_idr;
  332. struct tc_action *p;
  333. int ret;
  334. unsigned long id = 1;
  335. idr_for_each_entry_ul(idr, p, id) {
  336. ret = __tcf_idr_release(p, false, true);
  337. if (ret == ACT_P_DELETED)
  338. module_put(ops->owner);
  339. else if (ret < 0)
  340. return;
  341. }
  342. idr_destroy(&idrinfo->action_idr);
  343. }
  344. EXPORT_SYMBOL(tcf_idrinfo_destroy);
  345. static LIST_HEAD(act_base);
  346. static DEFINE_RWLOCK(act_mod_lock);
  347. int tcf_register_action(struct tc_action_ops *act,
  348. struct pernet_operations *ops)
  349. {
  350. struct tc_action_ops *a;
  351. int ret;
  352. if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup)
  353. return -EINVAL;
  354. /* We have to register pernet ops before making the action ops visible,
  355. * otherwise tcf_action_init_1() could get a partially initialized
  356. * netns.
  357. */
  358. ret = register_pernet_subsys(ops);
  359. if (ret)
  360. return ret;
  361. write_lock(&act_mod_lock);
  362. list_for_each_entry(a, &act_base, head) {
  363. if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
  364. write_unlock(&act_mod_lock);
  365. unregister_pernet_subsys(ops);
  366. return -EEXIST;
  367. }
  368. }
  369. list_add_tail(&act->head, &act_base);
  370. write_unlock(&act_mod_lock);
  371. return 0;
  372. }
  373. EXPORT_SYMBOL(tcf_register_action);
  374. int tcf_unregister_action(struct tc_action_ops *act,
  375. struct pernet_operations *ops)
  376. {
  377. struct tc_action_ops *a;
  378. int err = -ENOENT;
  379. write_lock(&act_mod_lock);
  380. list_for_each_entry(a, &act_base, head) {
  381. if (a == act) {
  382. list_del(&act->head);
  383. err = 0;
  384. break;
  385. }
  386. }
  387. write_unlock(&act_mod_lock);
  388. if (!err)
  389. unregister_pernet_subsys(ops);
  390. return err;
  391. }
  392. EXPORT_SYMBOL(tcf_unregister_action);
  393. /* lookup by name */
  394. static struct tc_action_ops *tc_lookup_action_n(char *kind)
  395. {
  396. struct tc_action_ops *a, *res = NULL;
  397. if (kind) {
  398. read_lock(&act_mod_lock);
  399. list_for_each_entry(a, &act_base, head) {
  400. if (strcmp(kind, a->kind) == 0) {
  401. if (try_module_get(a->owner))
  402. res = a;
  403. break;
  404. }
  405. }
  406. read_unlock(&act_mod_lock);
  407. }
  408. return res;
  409. }
  410. /* lookup by nlattr */
  411. static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
  412. {
  413. struct tc_action_ops *a, *res = NULL;
  414. if (kind) {
  415. read_lock(&act_mod_lock);
  416. list_for_each_entry(a, &act_base, head) {
  417. if (nla_strcmp(kind, a->kind) == 0) {
  418. if (try_module_get(a->owner))
  419. res = a;
  420. break;
  421. }
  422. }
  423. read_unlock(&act_mod_lock);
  424. }
  425. return res;
  426. }
  427. /*TCA_ACT_MAX_PRIO is 32, there count upto 32 */
  428. #define TCA_ACT_MAX_PRIO_MASK 0x1FF
  429. int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions,
  430. int nr_actions, struct tcf_result *res)
  431. {
  432. u32 jmp_prgcnt = 0;
  433. u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */
  434. int i;
  435. int ret = TC_ACT_OK;
  436. if (skb_skip_tc_classify(skb))
  437. return TC_ACT_OK;
  438. restart_act_graph:
  439. for (i = 0; i < nr_actions; i++) {
  440. const struct tc_action *a = actions[i];
  441. if (jmp_prgcnt > 0) {
  442. jmp_prgcnt -= 1;
  443. continue;
  444. }
  445. repeat:
  446. ret = a->ops->act(skb, a, res);
  447. if (ret == TC_ACT_REPEAT)
  448. goto repeat; /* we need a ttl - JHS */
  449. if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) {
  450. jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK;
  451. if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) {
  452. /* faulty opcode, stop pipeline */
  453. return TC_ACT_OK;
  454. } else {
  455. jmp_ttl -= 1;
  456. if (jmp_ttl > 0)
  457. goto restart_act_graph;
  458. else /* faulty graph, stop pipeline */
  459. return TC_ACT_OK;
  460. }
  461. } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) {
  462. tcf_action_goto_chain_exec(a, res);
  463. }
  464. if (ret != TC_ACT_PIPE)
  465. break;
  466. }
  467. return ret;
  468. }
  469. EXPORT_SYMBOL(tcf_action_exec);
  470. int tcf_action_destroy(struct list_head *actions, int bind)
  471. {
  472. const struct tc_action_ops *ops;
  473. struct tc_action *a, *tmp;
  474. int ret = 0;
  475. list_for_each_entry_safe(a, tmp, actions, list) {
  476. ops = a->ops;
  477. ret = __tcf_idr_release(a, bind, true);
  478. if (ret == ACT_P_DELETED)
  479. module_put(ops->owner);
  480. else if (ret < 0)
  481. return ret;
  482. }
  483. return ret;
  484. }
  485. int
  486. tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  487. {
  488. return a->ops->dump(skb, a, bind, ref);
  489. }
  490. int
  491. tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  492. {
  493. int err = -EINVAL;
  494. unsigned char *b = skb_tail_pointer(skb);
  495. struct nlattr *nest;
  496. if (nla_put_string(skb, TCA_KIND, a->ops->kind))
  497. goto nla_put_failure;
  498. if (tcf_action_copy_stats(skb, a, 0))
  499. goto nla_put_failure;
  500. if (a->act_cookie) {
  501. if (nla_put(skb, TCA_ACT_COOKIE, a->act_cookie->len,
  502. a->act_cookie->data))
  503. goto nla_put_failure;
  504. }
  505. nest = nla_nest_start(skb, TCA_OPTIONS);
  506. if (nest == NULL)
  507. goto nla_put_failure;
  508. err = tcf_action_dump_old(skb, a, bind, ref);
  509. if (err > 0) {
  510. nla_nest_end(skb, nest);
  511. return err;
  512. }
  513. nla_put_failure:
  514. nlmsg_trim(skb, b);
  515. return -1;
  516. }
  517. EXPORT_SYMBOL(tcf_action_dump_1);
  518. int tcf_action_dump(struct sk_buff *skb, struct list_head *actions,
  519. int bind, int ref)
  520. {
  521. struct tc_action *a;
  522. int err = -EINVAL;
  523. struct nlattr *nest;
  524. list_for_each_entry(a, actions, list) {
  525. nest = nla_nest_start(skb, a->order);
  526. if (nest == NULL)
  527. goto nla_put_failure;
  528. err = tcf_action_dump_1(skb, a, bind, ref);
  529. if (err < 0)
  530. goto errout;
  531. nla_nest_end(skb, nest);
  532. }
  533. return 0;
  534. nla_put_failure:
  535. err = -EINVAL;
  536. errout:
  537. nla_nest_cancel(skb, nest);
  538. return err;
  539. }
  540. static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb)
  541. {
  542. struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL);
  543. if (!c)
  544. return NULL;
  545. c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL);
  546. if (!c->data) {
  547. kfree(c);
  548. return NULL;
  549. }
  550. c->len = nla_len(tb[TCA_ACT_COOKIE]);
  551. return c;
  552. }
  553. struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
  554. struct nlattr *nla, struct nlattr *est,
  555. char *name, int ovr, int bind,
  556. struct netlink_ext_ack *extack)
  557. {
  558. struct tc_action *a;
  559. struct tc_action_ops *a_o;
  560. struct tc_cookie *cookie = NULL;
  561. char act_name[IFNAMSIZ];
  562. struct nlattr *tb[TCA_ACT_MAX + 1];
  563. struct nlattr *kind;
  564. int err;
  565. if (name == NULL) {
  566. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack);
  567. if (err < 0)
  568. goto err_out;
  569. err = -EINVAL;
  570. kind = tb[TCA_ACT_KIND];
  571. if (!kind) {
  572. NL_SET_ERR_MSG(extack, "TC action kind must be specified");
  573. goto err_out;
  574. }
  575. if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ) {
  576. NL_SET_ERR_MSG(extack, "TC action name too long");
  577. goto err_out;
  578. }
  579. if (tb[TCA_ACT_COOKIE]) {
  580. int cklen = nla_len(tb[TCA_ACT_COOKIE]);
  581. if (cklen > TC_COOKIE_MAX_SIZE) {
  582. NL_SET_ERR_MSG(extack, "TC cookie size above the maximum");
  583. goto err_out;
  584. }
  585. cookie = nla_memdup_cookie(tb);
  586. if (!cookie) {
  587. NL_SET_ERR_MSG(extack, "No memory to generate TC cookie");
  588. err = -ENOMEM;
  589. goto err_out;
  590. }
  591. }
  592. } else {
  593. if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ) {
  594. NL_SET_ERR_MSG(extack, "TC action name too long");
  595. err = -EINVAL;
  596. goto err_out;
  597. }
  598. }
  599. a_o = tc_lookup_action_n(act_name);
  600. if (a_o == NULL) {
  601. #ifdef CONFIG_MODULES
  602. rtnl_unlock();
  603. request_module("act_%s", act_name);
  604. rtnl_lock();
  605. a_o = tc_lookup_action_n(act_name);
  606. /* We dropped the RTNL semaphore in order to
  607. * perform the module load. So, even if we
  608. * succeeded in loading the module we have to
  609. * tell the caller to replay the request. We
  610. * indicate this using -EAGAIN.
  611. */
  612. if (a_o != NULL) {
  613. err = -EAGAIN;
  614. goto err_mod;
  615. }
  616. #endif
  617. NL_SET_ERR_MSG(extack, "Failed to load TC action module");
  618. err = -ENOENT;
  619. goto err_out;
  620. }
  621. /* backward compatibility for policer */
  622. if (name == NULL)
  623. err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind,
  624. extack);
  625. else
  626. err = a_o->init(net, nla, est, &a, ovr, bind, extack);
  627. if (err < 0)
  628. goto err_mod;
  629. if (name == NULL && tb[TCA_ACT_COOKIE]) {
  630. if (a->act_cookie) {
  631. kfree(a->act_cookie->data);
  632. kfree(a->act_cookie);
  633. }
  634. a->act_cookie = cookie;
  635. }
  636. /* module count goes up only when brand new policy is created
  637. * if it exists and is only bound to in a_o->init() then
  638. * ACT_P_CREATED is not returned (a zero is).
  639. */
  640. if (err != ACT_P_CREATED)
  641. module_put(a_o->owner);
  642. if (TC_ACT_EXT_CMP(a->tcfa_action, TC_ACT_GOTO_CHAIN)) {
  643. err = tcf_action_goto_chain_init(a, tp);
  644. if (err) {
  645. LIST_HEAD(actions);
  646. list_add_tail(&a->list, &actions);
  647. tcf_action_destroy(&actions, bind);
  648. NL_SET_ERR_MSG(extack, "Failed to init TC action chain");
  649. return ERR_PTR(err);
  650. }
  651. }
  652. return a;
  653. err_mod:
  654. module_put(a_o->owner);
  655. err_out:
  656. if (cookie) {
  657. kfree(cookie->data);
  658. kfree(cookie);
  659. }
  660. return ERR_PTR(err);
  661. }
  662. static void cleanup_a(struct list_head *actions, int ovr)
  663. {
  664. struct tc_action *a;
  665. if (!ovr)
  666. return;
  667. list_for_each_entry(a, actions, list)
  668. a->tcfa_refcnt--;
  669. }
  670. int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla,
  671. struct nlattr *est, char *name, int ovr, int bind,
  672. struct list_head *actions, size_t *attr_size,
  673. struct netlink_ext_ack *extack)
  674. {
  675. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  676. struct tc_action *act;
  677. size_t sz = 0;
  678. int err;
  679. int i;
  680. err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, extack);
  681. if (err < 0)
  682. return err;
  683. for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
  684. act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind,
  685. extack);
  686. if (IS_ERR(act)) {
  687. err = PTR_ERR(act);
  688. goto err;
  689. }
  690. act->order = i;
  691. sz += tcf_action_fill_size(act);
  692. if (ovr)
  693. act->tcfa_refcnt++;
  694. list_add_tail(&act->list, actions);
  695. }
  696. *attr_size = tcf_action_full_attrs_size(sz);
  697. /* Remove the temp refcnt which was necessary to protect against
  698. * destroying an existing action which was being replaced
  699. */
  700. cleanup_a(actions, ovr);
  701. return 0;
  702. err:
  703. tcf_action_destroy(actions, bind);
  704. return err;
  705. }
  706. int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p,
  707. int compat_mode)
  708. {
  709. int err = 0;
  710. struct gnet_dump d;
  711. if (p == NULL)
  712. goto errout;
  713. /* compat_mode being true specifies a call that is supposed
  714. * to add additional backward compatibility statistic TLVs.
  715. */
  716. if (compat_mode) {
  717. if (p->type == TCA_OLD_COMPAT)
  718. err = gnet_stats_start_copy_compat(skb, 0,
  719. TCA_STATS,
  720. TCA_XSTATS,
  721. &p->tcfa_lock, &d,
  722. TCA_PAD);
  723. else
  724. return 0;
  725. } else
  726. err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
  727. &p->tcfa_lock, &d, TCA_ACT_PAD);
  728. if (err < 0)
  729. goto errout;
  730. if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 ||
  731. gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 ||
  732. gnet_stats_copy_queue(&d, p->cpu_qstats,
  733. &p->tcfa_qstats,
  734. p->tcfa_qstats.qlen) < 0)
  735. goto errout;
  736. if (gnet_stats_finish_copy(&d) < 0)
  737. goto errout;
  738. return 0;
  739. errout:
  740. return -1;
  741. }
  742. static int tca_get_fill(struct sk_buff *skb, struct list_head *actions,
  743. u32 portid, u32 seq, u16 flags, int event, int bind,
  744. int ref)
  745. {
  746. struct tcamsg *t;
  747. struct nlmsghdr *nlh;
  748. unsigned char *b = skb_tail_pointer(skb);
  749. struct nlattr *nest;
  750. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags);
  751. if (!nlh)
  752. goto out_nlmsg_trim;
  753. t = nlmsg_data(nlh);
  754. t->tca_family = AF_UNSPEC;
  755. t->tca__pad1 = 0;
  756. t->tca__pad2 = 0;
  757. nest = nla_nest_start(skb, TCA_ACT_TAB);
  758. if (!nest)
  759. goto out_nlmsg_trim;
  760. if (tcf_action_dump(skb, actions, bind, ref) < 0)
  761. goto out_nlmsg_trim;
  762. nla_nest_end(skb, nest);
  763. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  764. return skb->len;
  765. out_nlmsg_trim:
  766. nlmsg_trim(skb, b);
  767. return -1;
  768. }
  769. static int
  770. tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n,
  771. struct list_head *actions, int event,
  772. struct netlink_ext_ack *extack)
  773. {
  774. struct sk_buff *skb;
  775. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  776. if (!skb)
  777. return -ENOBUFS;
  778. if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event,
  779. 0, 0) <= 0) {
  780. NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
  781. kfree_skb(skb);
  782. return -EINVAL;
  783. }
  784. return rtnl_unicast(skb, net, portid);
  785. }
  786. static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla,
  787. struct nlmsghdr *n, u32 portid,
  788. struct netlink_ext_ack *extack)
  789. {
  790. struct nlattr *tb[TCA_ACT_MAX + 1];
  791. const struct tc_action_ops *ops;
  792. struct tc_action *a;
  793. int index;
  794. int err;
  795. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack);
  796. if (err < 0)
  797. goto err_out;
  798. err = -EINVAL;
  799. if (tb[TCA_ACT_INDEX] == NULL ||
  800. nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) {
  801. NL_SET_ERR_MSG(extack, "Invalid TC action index value");
  802. goto err_out;
  803. }
  804. index = nla_get_u32(tb[TCA_ACT_INDEX]);
  805. err = -EINVAL;
  806. ops = tc_lookup_action(tb[TCA_ACT_KIND]);
  807. if (!ops) { /* could happen in batch of actions */
  808. NL_SET_ERR_MSG(extack, "Specified TC action not found");
  809. goto err_out;
  810. }
  811. err = -ENOENT;
  812. if (ops->lookup(net, &a, index, extack) == 0)
  813. goto err_mod;
  814. module_put(ops->owner);
  815. return a;
  816. err_mod:
  817. module_put(ops->owner);
  818. err_out:
  819. return ERR_PTR(err);
  820. }
  821. static int tca_action_flush(struct net *net, struct nlattr *nla,
  822. struct nlmsghdr *n, u32 portid,
  823. struct netlink_ext_ack *extack)
  824. {
  825. struct sk_buff *skb;
  826. unsigned char *b;
  827. struct nlmsghdr *nlh;
  828. struct tcamsg *t;
  829. struct netlink_callback dcb;
  830. struct nlattr *nest;
  831. struct nlattr *tb[TCA_ACT_MAX + 1];
  832. const struct tc_action_ops *ops;
  833. struct nlattr *kind;
  834. int err = -ENOMEM;
  835. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  836. if (!skb)
  837. return err;
  838. b = skb_tail_pointer(skb);
  839. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack);
  840. if (err < 0)
  841. goto err_out;
  842. err = -EINVAL;
  843. kind = tb[TCA_ACT_KIND];
  844. ops = tc_lookup_action(kind);
  845. if (!ops) { /*some idjot trying to flush unknown action */
  846. NL_SET_ERR_MSG(extack, "Cannot flush unknown TC action");
  847. goto err_out;
  848. }
  849. nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION,
  850. sizeof(*t), 0);
  851. if (!nlh) {
  852. NL_SET_ERR_MSG(extack, "Failed to create TC action flush notification");
  853. goto out_module_put;
  854. }
  855. t = nlmsg_data(nlh);
  856. t->tca_family = AF_UNSPEC;
  857. t->tca__pad1 = 0;
  858. t->tca__pad2 = 0;
  859. nest = nla_nest_start(skb, TCA_ACT_TAB);
  860. if (!nest) {
  861. NL_SET_ERR_MSG(extack, "Failed to add new netlink message");
  862. goto out_module_put;
  863. }
  864. err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops, extack);
  865. if (err <= 0) {
  866. nla_nest_cancel(skb, nest);
  867. goto out_module_put;
  868. }
  869. nla_nest_end(skb, nest);
  870. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  871. nlh->nlmsg_flags |= NLM_F_ROOT;
  872. module_put(ops->owner);
  873. err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  874. n->nlmsg_flags & NLM_F_ECHO);
  875. if (err > 0)
  876. return 0;
  877. if (err < 0)
  878. NL_SET_ERR_MSG(extack, "Failed to send TC action flush notification");
  879. return err;
  880. out_module_put:
  881. module_put(ops->owner);
  882. err_out:
  883. kfree_skb(skb);
  884. return err;
  885. }
  886. static int
  887. tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
  888. u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
  889. {
  890. int ret;
  891. struct sk_buff *skb;
  892. skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
  893. GFP_KERNEL);
  894. if (!skb)
  895. return -ENOBUFS;
  896. if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION,
  897. 0, 1) <= 0) {
  898. NL_SET_ERR_MSG(extack, "Failed to fill netlink TC action attributes");
  899. kfree_skb(skb);
  900. return -EINVAL;
  901. }
  902. /* now do the delete */
  903. ret = tcf_action_destroy(actions, 0);
  904. if (ret < 0) {
  905. NL_SET_ERR_MSG(extack, "Failed to delete TC action");
  906. kfree_skb(skb);
  907. return ret;
  908. }
  909. ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  910. n->nlmsg_flags & NLM_F_ECHO);
  911. if (ret > 0)
  912. return 0;
  913. return ret;
  914. }
  915. static int
  916. tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
  917. u32 portid, int event, struct netlink_ext_ack *extack)
  918. {
  919. int i, ret;
  920. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  921. struct tc_action *act;
  922. size_t attr_size = 0;
  923. LIST_HEAD(actions);
  924. ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, extack);
  925. if (ret < 0)
  926. return ret;
  927. if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
  928. if (tb[1])
  929. return tca_action_flush(net, tb[1], n, portid, extack);
  930. NL_SET_ERR_MSG(extack, "Invalid netlink attributes while flushing TC action");
  931. return -EINVAL;
  932. }
  933. for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
  934. act = tcf_action_get_1(net, tb[i], n, portid, extack);
  935. if (IS_ERR(act)) {
  936. ret = PTR_ERR(act);
  937. goto err;
  938. }
  939. act->order = i;
  940. attr_size += tcf_action_fill_size(act);
  941. list_add_tail(&act->list, &actions);
  942. }
  943. attr_size = tcf_action_full_attrs_size(attr_size);
  944. if (event == RTM_GETACTION)
  945. ret = tcf_get_notify(net, portid, n, &actions, event, extack);
  946. else { /* delete */
  947. ret = tcf_del_notify(net, n, &actions, portid, attr_size, extack);
  948. if (ret)
  949. goto err;
  950. return ret;
  951. }
  952. err:
  953. if (event != RTM_GETACTION)
  954. tcf_action_destroy(&actions, 0);
  955. return ret;
  956. }
  957. static int
  958. tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions,
  959. u32 portid, size_t attr_size, struct netlink_ext_ack *extack)
  960. {
  961. struct sk_buff *skb;
  962. int err = 0;
  963. skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size,
  964. GFP_KERNEL);
  965. if (!skb)
  966. return -ENOBUFS;
  967. if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags,
  968. RTM_NEWACTION, 0, 0) <= 0) {
  969. NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action");
  970. kfree_skb(skb);
  971. return -EINVAL;
  972. }
  973. err = rtnetlink_send(skb, net, portid, RTNLGRP_TC,
  974. n->nlmsg_flags & NLM_F_ECHO);
  975. if (err > 0)
  976. err = 0;
  977. return err;
  978. }
  979. static int tcf_action_add(struct net *net, struct nlattr *nla,
  980. struct nlmsghdr *n, u32 portid, int ovr,
  981. struct netlink_ext_ack *extack)
  982. {
  983. size_t attr_size = 0;
  984. int ret = 0;
  985. LIST_HEAD(actions);
  986. ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0, &actions,
  987. &attr_size, extack);
  988. if (ret)
  989. return ret;
  990. return tcf_add_notify(net, n, &actions, portid, attr_size, extack);
  991. }
  992. static u32 tcaa_root_flags_allowed = TCA_FLAG_LARGE_DUMP_ON;
  993. static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = {
  994. [TCA_ROOT_FLAGS] = { .type = NLA_BITFIELD32,
  995. .validation_data = &tcaa_root_flags_allowed },
  996. [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 },
  997. };
  998. static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n,
  999. struct netlink_ext_ack *extack)
  1000. {
  1001. struct net *net = sock_net(skb->sk);
  1002. struct nlattr *tca[TCA_ROOT_MAX + 1];
  1003. u32 portid = skb ? NETLINK_CB(skb).portid : 0;
  1004. int ret = 0, ovr = 0;
  1005. if ((n->nlmsg_type != RTM_GETACTION) &&
  1006. !netlink_capable(skb, CAP_NET_ADMIN))
  1007. return -EPERM;
  1008. ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ROOT_MAX, NULL,
  1009. extack);
  1010. if (ret < 0)
  1011. return ret;
  1012. if (tca[TCA_ACT_TAB] == NULL) {
  1013. NL_SET_ERR_MSG(extack, "Netlink action attributes missing");
  1014. return -EINVAL;
  1015. }
  1016. /* n->nlmsg_flags & NLM_F_CREATE */
  1017. switch (n->nlmsg_type) {
  1018. case RTM_NEWACTION:
  1019. /* we are going to assume all other flags
  1020. * imply create only if it doesn't exist
  1021. * Note that CREATE | EXCL implies that
  1022. * but since we want avoid ambiguity (eg when flags
  1023. * is zero) then just set this
  1024. */
  1025. if (n->nlmsg_flags & NLM_F_REPLACE)
  1026. ovr = 1;
  1027. replay:
  1028. ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr,
  1029. extack);
  1030. if (ret == -EAGAIN)
  1031. goto replay;
  1032. break;
  1033. case RTM_DELACTION:
  1034. ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
  1035. portid, RTM_DELACTION, extack);
  1036. break;
  1037. case RTM_GETACTION:
  1038. ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
  1039. portid, RTM_GETACTION, extack);
  1040. break;
  1041. default:
  1042. BUG();
  1043. }
  1044. return ret;
  1045. }
  1046. static struct nlattr *find_dump_kind(struct nlattr **nla)
  1047. {
  1048. struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
  1049. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  1050. struct nlattr *kind;
  1051. tb1 = nla[TCA_ACT_TAB];
  1052. if (tb1 == NULL)
  1053. return NULL;
  1054. if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
  1055. NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0)
  1056. return NULL;
  1057. if (tb[1] == NULL)
  1058. return NULL;
  1059. if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL, NULL) < 0)
  1060. return NULL;
  1061. kind = tb2[TCA_ACT_KIND];
  1062. return kind;
  1063. }
  1064. static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
  1065. {
  1066. struct net *net = sock_net(skb->sk);
  1067. struct nlmsghdr *nlh;
  1068. unsigned char *b = skb_tail_pointer(skb);
  1069. struct nlattr *nest;
  1070. struct tc_action_ops *a_o;
  1071. int ret = 0;
  1072. struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
  1073. struct nlattr *tb[TCA_ROOT_MAX + 1];
  1074. struct nlattr *count_attr = NULL;
  1075. unsigned long jiffy_since = 0;
  1076. struct nlattr *kind = NULL;
  1077. struct nla_bitfield32 bf;
  1078. u32 msecs_since = 0;
  1079. u32 act_count = 0;
  1080. ret = nlmsg_parse(cb->nlh, sizeof(struct tcamsg), tb, TCA_ROOT_MAX,
  1081. tcaa_policy, NULL);
  1082. if (ret < 0)
  1083. return ret;
  1084. kind = find_dump_kind(tb);
  1085. if (kind == NULL) {
  1086. pr_info("tc_dump_action: action bad kind\n");
  1087. return 0;
  1088. }
  1089. a_o = tc_lookup_action(kind);
  1090. if (a_o == NULL)
  1091. return 0;
  1092. cb->args[2] = 0;
  1093. if (tb[TCA_ROOT_FLAGS]) {
  1094. bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]);
  1095. cb->args[2] = bf.value;
  1096. }
  1097. if (tb[TCA_ROOT_TIME_DELTA]) {
  1098. msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]);
  1099. }
  1100. nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  1101. cb->nlh->nlmsg_type, sizeof(*t), 0);
  1102. if (!nlh)
  1103. goto out_module_put;
  1104. if (msecs_since)
  1105. jiffy_since = jiffies - msecs_to_jiffies(msecs_since);
  1106. t = nlmsg_data(nlh);
  1107. t->tca_family = AF_UNSPEC;
  1108. t->tca__pad1 = 0;
  1109. t->tca__pad2 = 0;
  1110. cb->args[3] = jiffy_since;
  1111. count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32));
  1112. if (!count_attr)
  1113. goto out_module_put;
  1114. nest = nla_nest_start(skb, TCA_ACT_TAB);
  1115. if (nest == NULL)
  1116. goto out_module_put;
  1117. ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o, NULL);
  1118. if (ret < 0)
  1119. goto out_module_put;
  1120. if (ret > 0) {
  1121. nla_nest_end(skb, nest);
  1122. ret = skb->len;
  1123. act_count = cb->args[1];
  1124. memcpy(nla_data(count_attr), &act_count, sizeof(u32));
  1125. cb->args[1] = 0;
  1126. } else
  1127. nlmsg_trim(skb, b);
  1128. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  1129. if (NETLINK_CB(cb->skb).portid && ret)
  1130. nlh->nlmsg_flags |= NLM_F_MULTI;
  1131. module_put(a_o->owner);
  1132. return skb->len;
  1133. out_module_put:
  1134. module_put(a_o->owner);
  1135. nlmsg_trim(skb, b);
  1136. return skb->len;
  1137. }
  1138. struct tcf_action_net {
  1139. struct rhashtable egdev_ht;
  1140. };
  1141. static unsigned int tcf_action_net_id;
  1142. struct tcf_action_egdev_cb {
  1143. struct list_head list;
  1144. tc_setup_cb_t *cb;
  1145. void *cb_priv;
  1146. };
  1147. struct tcf_action_egdev {
  1148. struct rhash_head ht_node;
  1149. const struct net_device *dev;
  1150. unsigned int refcnt;
  1151. struct list_head cb_list;
  1152. };
  1153. static const struct rhashtable_params tcf_action_egdev_ht_params = {
  1154. .key_offset = offsetof(struct tcf_action_egdev, dev),
  1155. .head_offset = offsetof(struct tcf_action_egdev, ht_node),
  1156. .key_len = sizeof(const struct net_device *),
  1157. };
  1158. static struct tcf_action_egdev *
  1159. tcf_action_egdev_lookup(const struct net_device *dev)
  1160. {
  1161. struct net *net = dev_net(dev);
  1162. struct tcf_action_net *tan = net_generic(net, tcf_action_net_id);
  1163. return rhashtable_lookup_fast(&tan->egdev_ht, &dev,
  1164. tcf_action_egdev_ht_params);
  1165. }
  1166. static struct tcf_action_egdev *
  1167. tcf_action_egdev_get(const struct net_device *dev)
  1168. {
  1169. struct tcf_action_egdev *egdev;
  1170. struct tcf_action_net *tan;
  1171. egdev = tcf_action_egdev_lookup(dev);
  1172. if (egdev)
  1173. goto inc_ref;
  1174. egdev = kzalloc(sizeof(*egdev), GFP_KERNEL);
  1175. if (!egdev)
  1176. return NULL;
  1177. INIT_LIST_HEAD(&egdev->cb_list);
  1178. egdev->dev = dev;
  1179. tan = net_generic(dev_net(dev), tcf_action_net_id);
  1180. rhashtable_insert_fast(&tan->egdev_ht, &egdev->ht_node,
  1181. tcf_action_egdev_ht_params);
  1182. inc_ref:
  1183. egdev->refcnt++;
  1184. return egdev;
  1185. }
  1186. static void tcf_action_egdev_put(struct tcf_action_egdev *egdev)
  1187. {
  1188. struct tcf_action_net *tan;
  1189. if (--egdev->refcnt)
  1190. return;
  1191. tan = net_generic(dev_net(egdev->dev), tcf_action_net_id);
  1192. rhashtable_remove_fast(&tan->egdev_ht, &egdev->ht_node,
  1193. tcf_action_egdev_ht_params);
  1194. kfree(egdev);
  1195. }
  1196. static struct tcf_action_egdev_cb *
  1197. tcf_action_egdev_cb_lookup(struct tcf_action_egdev *egdev,
  1198. tc_setup_cb_t *cb, void *cb_priv)
  1199. {
  1200. struct tcf_action_egdev_cb *egdev_cb;
  1201. list_for_each_entry(egdev_cb, &egdev->cb_list, list)
  1202. if (egdev_cb->cb == cb && egdev_cb->cb_priv == cb_priv)
  1203. return egdev_cb;
  1204. return NULL;
  1205. }
  1206. static int tcf_action_egdev_cb_call(struct tcf_action_egdev *egdev,
  1207. enum tc_setup_type type,
  1208. void *type_data, bool err_stop)
  1209. {
  1210. struct tcf_action_egdev_cb *egdev_cb;
  1211. int ok_count = 0;
  1212. int err;
  1213. list_for_each_entry(egdev_cb, &egdev->cb_list, list) {
  1214. err = egdev_cb->cb(type, type_data, egdev_cb->cb_priv);
  1215. if (err) {
  1216. if (err_stop)
  1217. return err;
  1218. } else {
  1219. ok_count++;
  1220. }
  1221. }
  1222. return ok_count;
  1223. }
  1224. static int tcf_action_egdev_cb_add(struct tcf_action_egdev *egdev,
  1225. tc_setup_cb_t *cb, void *cb_priv)
  1226. {
  1227. struct tcf_action_egdev_cb *egdev_cb;
  1228. egdev_cb = tcf_action_egdev_cb_lookup(egdev, cb, cb_priv);
  1229. if (WARN_ON(egdev_cb))
  1230. return -EEXIST;
  1231. egdev_cb = kzalloc(sizeof(*egdev_cb), GFP_KERNEL);
  1232. if (!egdev_cb)
  1233. return -ENOMEM;
  1234. egdev_cb->cb = cb;
  1235. egdev_cb->cb_priv = cb_priv;
  1236. list_add(&egdev_cb->list, &egdev->cb_list);
  1237. return 0;
  1238. }
  1239. static void tcf_action_egdev_cb_del(struct tcf_action_egdev *egdev,
  1240. tc_setup_cb_t *cb, void *cb_priv)
  1241. {
  1242. struct tcf_action_egdev_cb *egdev_cb;
  1243. egdev_cb = tcf_action_egdev_cb_lookup(egdev, cb, cb_priv);
  1244. if (WARN_ON(!egdev_cb))
  1245. return;
  1246. list_del(&egdev_cb->list);
  1247. kfree(egdev_cb);
  1248. }
  1249. static int __tc_setup_cb_egdev_register(const struct net_device *dev,
  1250. tc_setup_cb_t *cb, void *cb_priv)
  1251. {
  1252. struct tcf_action_egdev *egdev = tcf_action_egdev_get(dev);
  1253. int err;
  1254. if (!egdev)
  1255. return -ENOMEM;
  1256. err = tcf_action_egdev_cb_add(egdev, cb, cb_priv);
  1257. if (err)
  1258. goto err_cb_add;
  1259. return 0;
  1260. err_cb_add:
  1261. tcf_action_egdev_put(egdev);
  1262. return err;
  1263. }
  1264. int tc_setup_cb_egdev_register(const struct net_device *dev,
  1265. tc_setup_cb_t *cb, void *cb_priv)
  1266. {
  1267. int err;
  1268. rtnl_lock();
  1269. err = __tc_setup_cb_egdev_register(dev, cb, cb_priv);
  1270. rtnl_unlock();
  1271. return err;
  1272. }
  1273. EXPORT_SYMBOL_GPL(tc_setup_cb_egdev_register);
  1274. static void __tc_setup_cb_egdev_unregister(const struct net_device *dev,
  1275. tc_setup_cb_t *cb, void *cb_priv)
  1276. {
  1277. struct tcf_action_egdev *egdev = tcf_action_egdev_lookup(dev);
  1278. if (WARN_ON(!egdev))
  1279. return;
  1280. tcf_action_egdev_cb_del(egdev, cb, cb_priv);
  1281. tcf_action_egdev_put(egdev);
  1282. }
  1283. void tc_setup_cb_egdev_unregister(const struct net_device *dev,
  1284. tc_setup_cb_t *cb, void *cb_priv)
  1285. {
  1286. rtnl_lock();
  1287. __tc_setup_cb_egdev_unregister(dev, cb, cb_priv);
  1288. rtnl_unlock();
  1289. }
  1290. EXPORT_SYMBOL_GPL(tc_setup_cb_egdev_unregister);
  1291. int tc_setup_cb_egdev_call(const struct net_device *dev,
  1292. enum tc_setup_type type, void *type_data,
  1293. bool err_stop)
  1294. {
  1295. struct tcf_action_egdev *egdev = tcf_action_egdev_lookup(dev);
  1296. if (!egdev)
  1297. return 0;
  1298. return tcf_action_egdev_cb_call(egdev, type, type_data, err_stop);
  1299. }
  1300. EXPORT_SYMBOL_GPL(tc_setup_cb_egdev_call);
  1301. static __net_init int tcf_action_net_init(struct net *net)
  1302. {
  1303. struct tcf_action_net *tan = net_generic(net, tcf_action_net_id);
  1304. return rhashtable_init(&tan->egdev_ht, &tcf_action_egdev_ht_params);
  1305. }
  1306. static void __net_exit tcf_action_net_exit(struct net *net)
  1307. {
  1308. struct tcf_action_net *tan = net_generic(net, tcf_action_net_id);
  1309. rhashtable_destroy(&tan->egdev_ht);
  1310. }
  1311. static struct pernet_operations tcf_action_net_ops = {
  1312. .init = tcf_action_net_init,
  1313. .exit = tcf_action_net_exit,
  1314. .id = &tcf_action_net_id,
  1315. .size = sizeof(struct tcf_action_net),
  1316. };
  1317. static int __init tc_action_init(void)
  1318. {
  1319. int err;
  1320. err = register_pernet_subsys(&tcf_action_net_ops);
  1321. if (err)
  1322. return err;
  1323. rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0);
  1324. rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0);
  1325. rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
  1326. 0);
  1327. return 0;
  1328. }
  1329. subsys_initcall(tc_action_init);