act_api.c 35 KB

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