act_api.c 40 KB

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