act_api.c 40 KB

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