act_api.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  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 <net/net_namespace.h>
  24. #include <net/sock.h>
  25. #include <net/sch_generic.h>
  26. #include <net/act_api.h>
  27. #include <net/netlink.h>
  28. void tcf_hash_destroy(struct tcf_common *p, struct tcf_hashinfo *hinfo)
  29. {
  30. unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
  31. struct tcf_common **p1p;
  32. for (p1p = &hinfo->htab[h]; *p1p; p1p = &(*p1p)->tcfc_next) {
  33. if (*p1p == p) {
  34. write_lock_bh(hinfo->lock);
  35. *p1p = p->tcfc_next;
  36. write_unlock_bh(hinfo->lock);
  37. gen_kill_estimator(&p->tcfc_bstats,
  38. &p->tcfc_rate_est);
  39. /*
  40. * gen_estimator est_timer() might access p->tcfc_lock
  41. * or bstats, wait a RCU grace period before freeing p
  42. */
  43. kfree_rcu(p, tcfc_rcu);
  44. return;
  45. }
  46. }
  47. WARN_ON(1);
  48. }
  49. EXPORT_SYMBOL(tcf_hash_destroy);
  50. int tcf_hash_release(struct tcf_common *p, int bind,
  51. struct tcf_hashinfo *hinfo)
  52. {
  53. int ret = 0;
  54. if (p) {
  55. if (bind)
  56. p->tcfc_bindcnt--;
  57. p->tcfc_refcnt--;
  58. if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) {
  59. tcf_hash_destroy(p, hinfo);
  60. ret = 1;
  61. }
  62. }
  63. return ret;
  64. }
  65. EXPORT_SYMBOL(tcf_hash_release);
  66. static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
  67. struct tc_action *a, struct tcf_hashinfo *hinfo)
  68. {
  69. struct tcf_common *p;
  70. int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
  71. struct nlattr *nest;
  72. read_lock_bh(hinfo->lock);
  73. s_i = cb->args[0];
  74. for (i = 0; i < (hinfo->hmask + 1); i++) {
  75. p = hinfo->htab[tcf_hash(i, hinfo->hmask)];
  76. for (; p; p = p->tcfc_next) {
  77. index++;
  78. if (index < s_i)
  79. continue;
  80. a->priv = p;
  81. a->order = n_i;
  82. nest = nla_nest_start(skb, a->order);
  83. if (nest == NULL)
  84. goto nla_put_failure;
  85. err = tcf_action_dump_1(skb, a, 0, 0);
  86. if (err < 0) {
  87. index--;
  88. nlmsg_trim(skb, nest);
  89. goto done;
  90. }
  91. nla_nest_end(skb, nest);
  92. n_i++;
  93. if (n_i >= TCA_ACT_MAX_PRIO)
  94. goto done;
  95. }
  96. }
  97. done:
  98. read_unlock_bh(hinfo->lock);
  99. if (n_i)
  100. cb->args[0] += n_i;
  101. return n_i;
  102. nla_put_failure:
  103. nla_nest_cancel(skb, nest);
  104. goto done;
  105. }
  106. static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a,
  107. struct tcf_hashinfo *hinfo)
  108. {
  109. struct tcf_common *p, *s_p;
  110. struct nlattr *nest;
  111. int i = 0, n_i = 0;
  112. nest = nla_nest_start(skb, a->order);
  113. if (nest == NULL)
  114. goto nla_put_failure;
  115. if (nla_put_string(skb, TCA_KIND, a->ops->kind))
  116. goto nla_put_failure;
  117. for (i = 0; i < (hinfo->hmask + 1); i++) {
  118. p = hinfo->htab[tcf_hash(i, hinfo->hmask)];
  119. while (p != NULL) {
  120. s_p = p->tcfc_next;
  121. if (ACT_P_DELETED == tcf_hash_release(p, 0, hinfo))
  122. module_put(a->ops->owner);
  123. n_i++;
  124. p = s_p;
  125. }
  126. }
  127. if (nla_put_u32(skb, TCA_FCNT, n_i))
  128. goto nla_put_failure;
  129. nla_nest_end(skb, nest);
  130. return n_i;
  131. nla_put_failure:
  132. nla_nest_cancel(skb, nest);
  133. return -EINVAL;
  134. }
  135. int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
  136. int type, struct tc_action *a)
  137. {
  138. struct tcf_hashinfo *hinfo = a->ops->hinfo;
  139. if (type == RTM_DELACTION) {
  140. return tcf_del_walker(skb, a, hinfo);
  141. } else if (type == RTM_GETACTION) {
  142. return tcf_dump_walker(skb, cb, a, hinfo);
  143. } else {
  144. WARN(1, "tcf_generic_walker: unknown action %d\n", type);
  145. return -EINVAL;
  146. }
  147. }
  148. EXPORT_SYMBOL(tcf_generic_walker);
  149. struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
  150. {
  151. struct tcf_common *p;
  152. read_lock_bh(hinfo->lock);
  153. for (p = hinfo->htab[tcf_hash(index, hinfo->hmask)]; p;
  154. p = p->tcfc_next) {
  155. if (p->tcfc_index == index)
  156. break;
  157. }
  158. read_unlock_bh(hinfo->lock);
  159. return p;
  160. }
  161. EXPORT_SYMBOL(tcf_hash_lookup);
  162. u32 tcf_hash_new_index(u32 *idx_gen, struct tcf_hashinfo *hinfo)
  163. {
  164. u32 val = *idx_gen;
  165. do {
  166. if (++val == 0)
  167. val = 1;
  168. } while (tcf_hash_lookup(val, hinfo));
  169. return (*idx_gen = val);
  170. }
  171. EXPORT_SYMBOL(tcf_hash_new_index);
  172. int tcf_hash_search(struct tc_action *a, u32 index)
  173. {
  174. struct tcf_hashinfo *hinfo = a->ops->hinfo;
  175. struct tcf_common *p = tcf_hash_lookup(index, hinfo);
  176. if (p) {
  177. a->priv = p;
  178. return 1;
  179. }
  180. return 0;
  181. }
  182. EXPORT_SYMBOL(tcf_hash_search);
  183. struct tcf_common *tcf_hash_check(u32 index, struct tc_action *a, int bind,
  184. struct tcf_hashinfo *hinfo)
  185. {
  186. struct tcf_common *p = NULL;
  187. if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
  188. if (bind)
  189. p->tcfc_bindcnt++;
  190. p->tcfc_refcnt++;
  191. a->priv = p;
  192. }
  193. return p;
  194. }
  195. EXPORT_SYMBOL(tcf_hash_check);
  196. struct tcf_common *tcf_hash_create(u32 index, struct nlattr *est,
  197. struct tc_action *a, int size, int bind,
  198. u32 *idx_gen, struct tcf_hashinfo *hinfo)
  199. {
  200. struct tcf_common *p = kzalloc(size, GFP_KERNEL);
  201. if (unlikely(!p))
  202. return ERR_PTR(-ENOMEM);
  203. p->tcfc_refcnt = 1;
  204. if (bind)
  205. p->tcfc_bindcnt = 1;
  206. spin_lock_init(&p->tcfc_lock);
  207. p->tcfc_index = index ? index : tcf_hash_new_index(idx_gen, hinfo);
  208. p->tcfc_tm.install = jiffies;
  209. p->tcfc_tm.lastuse = jiffies;
  210. if (est) {
  211. int err = gen_new_estimator(&p->tcfc_bstats, &p->tcfc_rate_est,
  212. &p->tcfc_lock, est);
  213. if (err) {
  214. kfree(p);
  215. return ERR_PTR(err);
  216. }
  217. }
  218. a->priv = (void *) p;
  219. return p;
  220. }
  221. EXPORT_SYMBOL(tcf_hash_create);
  222. void tcf_hash_insert(struct tcf_common *p, struct tcf_hashinfo *hinfo)
  223. {
  224. unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
  225. write_lock_bh(hinfo->lock);
  226. p->tcfc_next = hinfo->htab[h];
  227. hinfo->htab[h] = p;
  228. write_unlock_bh(hinfo->lock);
  229. }
  230. EXPORT_SYMBOL(tcf_hash_insert);
  231. static struct tc_action_ops *act_base = NULL;
  232. static DEFINE_RWLOCK(act_mod_lock);
  233. int tcf_register_action(struct tc_action_ops *act)
  234. {
  235. struct tc_action_ops *a, **ap;
  236. write_lock(&act_mod_lock);
  237. for (ap = &act_base; (a = *ap) != NULL; ap = &a->next) {
  238. if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
  239. write_unlock(&act_mod_lock);
  240. return -EEXIST;
  241. }
  242. }
  243. act->next = NULL;
  244. *ap = act;
  245. write_unlock(&act_mod_lock);
  246. return 0;
  247. }
  248. EXPORT_SYMBOL(tcf_register_action);
  249. int tcf_unregister_action(struct tc_action_ops *act)
  250. {
  251. struct tc_action_ops *a, **ap;
  252. int err = -ENOENT;
  253. write_lock(&act_mod_lock);
  254. for (ap = &act_base; (a = *ap) != NULL; ap = &a->next)
  255. if (a == act)
  256. break;
  257. if (a) {
  258. *ap = a->next;
  259. a->next = NULL;
  260. err = 0;
  261. }
  262. write_unlock(&act_mod_lock);
  263. return err;
  264. }
  265. EXPORT_SYMBOL(tcf_unregister_action);
  266. /* lookup by name */
  267. static struct tc_action_ops *tc_lookup_action_n(char *kind)
  268. {
  269. struct tc_action_ops *a = NULL;
  270. if (kind) {
  271. read_lock(&act_mod_lock);
  272. for (a = act_base; a; a = a->next) {
  273. if (strcmp(kind, a->kind) == 0) {
  274. if (!try_module_get(a->owner)) {
  275. read_unlock(&act_mod_lock);
  276. return NULL;
  277. }
  278. break;
  279. }
  280. }
  281. read_unlock(&act_mod_lock);
  282. }
  283. return a;
  284. }
  285. /* lookup by nlattr */
  286. static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
  287. {
  288. struct tc_action_ops *a = NULL;
  289. if (kind) {
  290. read_lock(&act_mod_lock);
  291. for (a = act_base; a; a = a->next) {
  292. if (nla_strcmp(kind, a->kind) == 0) {
  293. if (!try_module_get(a->owner)) {
  294. read_unlock(&act_mod_lock);
  295. return NULL;
  296. }
  297. break;
  298. }
  299. }
  300. read_unlock(&act_mod_lock);
  301. }
  302. return a;
  303. }
  304. #if 0
  305. /* lookup by id */
  306. static struct tc_action_ops *tc_lookup_action_id(u32 type)
  307. {
  308. struct tc_action_ops *a = NULL;
  309. if (type) {
  310. read_lock(&act_mod_lock);
  311. for (a = act_base; a; a = a->next) {
  312. if (a->type == type) {
  313. if (!try_module_get(a->owner)) {
  314. read_unlock(&act_mod_lock);
  315. return NULL;
  316. }
  317. break;
  318. }
  319. }
  320. read_unlock(&act_mod_lock);
  321. }
  322. return a;
  323. }
  324. #endif
  325. int tcf_action_exec(struct sk_buff *skb, const struct tc_action *act,
  326. struct tcf_result *res)
  327. {
  328. const struct tc_action *a;
  329. int ret = -1;
  330. if (skb->tc_verd & TC_NCLS) {
  331. skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
  332. ret = TC_ACT_OK;
  333. goto exec_done;
  334. }
  335. while ((a = act) != NULL) {
  336. repeat:
  337. if (a->ops && a->ops->act) {
  338. ret = a->ops->act(skb, a, res);
  339. if (TC_MUNGED & skb->tc_verd) {
  340. /* copied already, allow trampling */
  341. skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
  342. skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
  343. }
  344. if (ret == TC_ACT_REPEAT)
  345. goto repeat; /* we need a ttl - JHS */
  346. if (ret != TC_ACT_PIPE)
  347. goto exec_done;
  348. }
  349. act = a->next;
  350. }
  351. exec_done:
  352. return ret;
  353. }
  354. EXPORT_SYMBOL(tcf_action_exec);
  355. void tcf_action_destroy(struct tc_action *act, int bind)
  356. {
  357. struct tc_action *a;
  358. for (a = act; a; a = act) {
  359. if (a->ops && a->ops->cleanup) {
  360. if (a->ops->cleanup(a, bind) == ACT_P_DELETED)
  361. module_put(a->ops->owner);
  362. act = act->next;
  363. kfree(a);
  364. } else {
  365. /*FIXME: Remove later - catch insertion bugs*/
  366. WARN(1, "tcf_action_destroy: BUG? destroying NULL ops\n");
  367. act = act->next;
  368. kfree(a);
  369. }
  370. }
  371. }
  372. int
  373. tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  374. {
  375. int err = -EINVAL;
  376. if (a->ops == NULL || a->ops->dump == NULL)
  377. return err;
  378. return a->ops->dump(skb, a, bind, ref);
  379. }
  380. int
  381. tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  382. {
  383. int err = -EINVAL;
  384. unsigned char *b = skb_tail_pointer(skb);
  385. struct nlattr *nest;
  386. if (a->ops == NULL || a->ops->dump == NULL)
  387. return err;
  388. if (nla_put_string(skb, TCA_KIND, a->ops->kind))
  389. goto nla_put_failure;
  390. if (tcf_action_copy_stats(skb, a, 0))
  391. goto nla_put_failure;
  392. nest = nla_nest_start(skb, TCA_OPTIONS);
  393. if (nest == NULL)
  394. goto nla_put_failure;
  395. err = tcf_action_dump_old(skb, a, bind, ref);
  396. if (err > 0) {
  397. nla_nest_end(skb, nest);
  398. return err;
  399. }
  400. nla_put_failure:
  401. nlmsg_trim(skb, b);
  402. return -1;
  403. }
  404. EXPORT_SYMBOL(tcf_action_dump_1);
  405. int
  406. tcf_action_dump(struct sk_buff *skb, struct tc_action *act, int bind, int ref)
  407. {
  408. struct tc_action *a;
  409. int err = -EINVAL;
  410. struct nlattr *nest;
  411. while ((a = act) != NULL) {
  412. act = a->next;
  413. nest = nla_nest_start(skb, a->order);
  414. if (nest == NULL)
  415. goto nla_put_failure;
  416. err = tcf_action_dump_1(skb, a, bind, ref);
  417. if (err < 0)
  418. goto errout;
  419. nla_nest_end(skb, nest);
  420. }
  421. return 0;
  422. nla_put_failure:
  423. err = -EINVAL;
  424. errout:
  425. nla_nest_cancel(skb, nest);
  426. return err;
  427. }
  428. struct tc_action *tcf_action_init_1(struct nlattr *nla, struct nlattr *est,
  429. char *name, int ovr, int bind)
  430. {
  431. struct tc_action *a;
  432. struct tc_action_ops *a_o;
  433. char act_name[IFNAMSIZ];
  434. struct nlattr *tb[TCA_ACT_MAX + 1];
  435. struct nlattr *kind;
  436. int err;
  437. if (name == NULL) {
  438. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
  439. if (err < 0)
  440. goto err_out;
  441. err = -EINVAL;
  442. kind = tb[TCA_ACT_KIND];
  443. if (kind == NULL)
  444. goto err_out;
  445. if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
  446. goto err_out;
  447. } else {
  448. err = -EINVAL;
  449. if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
  450. goto err_out;
  451. }
  452. a_o = tc_lookup_action_n(act_name);
  453. if (a_o == NULL) {
  454. #ifdef CONFIG_MODULES
  455. rtnl_unlock();
  456. request_module("act_%s", act_name);
  457. rtnl_lock();
  458. a_o = tc_lookup_action_n(act_name);
  459. /* We dropped the RTNL semaphore in order to
  460. * perform the module load. So, even if we
  461. * succeeded in loading the module we have to
  462. * tell the caller to replay the request. We
  463. * indicate this using -EAGAIN.
  464. */
  465. if (a_o != NULL) {
  466. err = -EAGAIN;
  467. goto err_mod;
  468. }
  469. #endif
  470. err = -ENOENT;
  471. goto err_out;
  472. }
  473. err = -ENOMEM;
  474. a = kzalloc(sizeof(*a), GFP_KERNEL);
  475. if (a == NULL)
  476. goto err_mod;
  477. /* backward compatibility for policer */
  478. if (name == NULL)
  479. err = a_o->init(tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
  480. else
  481. err = a_o->init(nla, est, a, ovr, bind);
  482. if (err < 0)
  483. goto err_free;
  484. /* module count goes up only when brand new policy is created
  485. * if it exists and is only bound to in a_o->init() then
  486. * ACT_P_CREATED is not returned (a zero is).
  487. */
  488. if (err != ACT_P_CREATED)
  489. module_put(a_o->owner);
  490. a->ops = a_o;
  491. return a;
  492. err_free:
  493. kfree(a);
  494. err_mod:
  495. module_put(a_o->owner);
  496. err_out:
  497. return ERR_PTR(err);
  498. }
  499. struct tc_action *tcf_action_init(struct nlattr *nla, struct nlattr *est,
  500. char *name, int ovr, int bind)
  501. {
  502. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  503. struct tc_action *head = NULL, *act, *act_prev = NULL;
  504. int err;
  505. int i;
  506. err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
  507. if (err < 0)
  508. return ERR_PTR(err);
  509. for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
  510. act = tcf_action_init_1(tb[i], est, name, ovr, bind);
  511. if (IS_ERR(act))
  512. goto err;
  513. act->order = i;
  514. if (head == NULL)
  515. head = act;
  516. else
  517. act_prev->next = act;
  518. act_prev = act;
  519. }
  520. return head;
  521. err:
  522. if (head != NULL)
  523. tcf_action_destroy(head, bind);
  524. return act;
  525. }
  526. int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
  527. int compat_mode)
  528. {
  529. int err = 0;
  530. struct gnet_dump d;
  531. struct tcf_act_hdr *h = a->priv;
  532. if (h == NULL)
  533. goto errout;
  534. /* compat_mode being true specifies a call that is supposed
  535. * to add additional backward compatibility statistic TLVs.
  536. */
  537. if (compat_mode) {
  538. if (a->type == TCA_OLD_COMPAT)
  539. err = gnet_stats_start_copy_compat(skb, 0,
  540. TCA_STATS, TCA_XSTATS, &h->tcf_lock, &d);
  541. else
  542. return 0;
  543. } else
  544. err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
  545. &h->tcf_lock, &d);
  546. if (err < 0)
  547. goto errout;
  548. if (a->ops != NULL && a->ops->get_stats != NULL)
  549. if (a->ops->get_stats(skb, a) < 0)
  550. goto errout;
  551. if (gnet_stats_copy_basic(&d, &h->tcf_bstats) < 0 ||
  552. gnet_stats_copy_rate_est(&d, &h->tcf_bstats,
  553. &h->tcf_rate_est) < 0 ||
  554. gnet_stats_copy_queue(&d, &h->tcf_qstats) < 0)
  555. goto errout;
  556. if (gnet_stats_finish_copy(&d) < 0)
  557. goto errout;
  558. return 0;
  559. errout:
  560. return -1;
  561. }
  562. static int
  563. tca_get_fill(struct sk_buff *skb, struct tc_action *a, u32 pid, u32 seq,
  564. u16 flags, int event, int bind, int ref)
  565. {
  566. struct tcamsg *t;
  567. struct nlmsghdr *nlh;
  568. unsigned char *b = skb_tail_pointer(skb);
  569. struct nlattr *nest;
  570. nlh = nlmsg_put(skb, pid, seq, event, sizeof(*t), flags);
  571. if (!nlh)
  572. goto out_nlmsg_trim;
  573. t = nlmsg_data(nlh);
  574. t->tca_family = AF_UNSPEC;
  575. t->tca__pad1 = 0;
  576. t->tca__pad2 = 0;
  577. nest = nla_nest_start(skb, TCA_ACT_TAB);
  578. if (nest == NULL)
  579. goto out_nlmsg_trim;
  580. if (tcf_action_dump(skb, a, bind, ref) < 0)
  581. goto out_nlmsg_trim;
  582. nla_nest_end(skb, nest);
  583. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  584. return skb->len;
  585. out_nlmsg_trim:
  586. nlmsg_trim(skb, b);
  587. return -1;
  588. }
  589. static int
  590. act_get_notify(struct net *net, u32 pid, struct nlmsghdr *n,
  591. struct tc_action *a, int event)
  592. {
  593. struct sk_buff *skb;
  594. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  595. if (!skb)
  596. return -ENOBUFS;
  597. if (tca_get_fill(skb, a, pid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
  598. kfree_skb(skb);
  599. return -EINVAL;
  600. }
  601. return rtnl_unicast(skb, net, pid);
  602. }
  603. static struct tc_action *
  604. tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 pid)
  605. {
  606. struct nlattr *tb[TCA_ACT_MAX + 1];
  607. struct tc_action *a;
  608. int index;
  609. int err;
  610. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
  611. if (err < 0)
  612. goto err_out;
  613. err = -EINVAL;
  614. if (tb[TCA_ACT_INDEX] == NULL ||
  615. nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
  616. goto err_out;
  617. index = nla_get_u32(tb[TCA_ACT_INDEX]);
  618. err = -ENOMEM;
  619. a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
  620. if (a == NULL)
  621. goto err_out;
  622. err = -EINVAL;
  623. a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
  624. if (a->ops == NULL)
  625. goto err_free;
  626. if (a->ops->lookup == NULL)
  627. goto err_mod;
  628. err = -ENOENT;
  629. if (a->ops->lookup(a, index) == 0)
  630. goto err_mod;
  631. module_put(a->ops->owner);
  632. return a;
  633. err_mod:
  634. module_put(a->ops->owner);
  635. err_free:
  636. kfree(a);
  637. err_out:
  638. return ERR_PTR(err);
  639. }
  640. static void cleanup_a(struct tc_action *act)
  641. {
  642. struct tc_action *a;
  643. for (a = act; a; a = act) {
  644. act = a->next;
  645. kfree(a);
  646. }
  647. }
  648. static struct tc_action *create_a(int i)
  649. {
  650. struct tc_action *act;
  651. act = kzalloc(sizeof(*act), GFP_KERNEL);
  652. if (act == NULL) {
  653. pr_debug("create_a: failed to alloc!\n");
  654. return NULL;
  655. }
  656. act->order = i;
  657. return act;
  658. }
  659. static int tca_action_flush(struct net *net, struct nlattr *nla,
  660. struct nlmsghdr *n, u32 pid)
  661. {
  662. struct sk_buff *skb;
  663. unsigned char *b;
  664. struct nlmsghdr *nlh;
  665. struct tcamsg *t;
  666. struct netlink_callback dcb;
  667. struct nlattr *nest;
  668. struct nlattr *tb[TCA_ACT_MAX + 1];
  669. struct nlattr *kind;
  670. struct tc_action *a = create_a(0);
  671. int err = -ENOMEM;
  672. if (a == NULL) {
  673. pr_debug("tca_action_flush: couldnt create tc_action\n");
  674. return err;
  675. }
  676. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  677. if (!skb) {
  678. pr_debug("tca_action_flush: failed skb alloc\n");
  679. kfree(a);
  680. return err;
  681. }
  682. b = skb_tail_pointer(skb);
  683. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
  684. if (err < 0)
  685. goto err_out;
  686. err = -EINVAL;
  687. kind = tb[TCA_ACT_KIND];
  688. a->ops = tc_lookup_action(kind);
  689. if (a->ops == NULL)
  690. goto err_out;
  691. nlh = nlmsg_put(skb, pid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t), 0);
  692. if (!nlh)
  693. goto out_module_put;
  694. t = nlmsg_data(nlh);
  695. t->tca_family = AF_UNSPEC;
  696. t->tca__pad1 = 0;
  697. t->tca__pad2 = 0;
  698. nest = nla_nest_start(skb, TCA_ACT_TAB);
  699. if (nest == NULL)
  700. goto out_module_put;
  701. err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
  702. if (err < 0)
  703. goto out_module_put;
  704. if (err == 0)
  705. goto noflush_out;
  706. nla_nest_end(skb, nest);
  707. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  708. nlh->nlmsg_flags |= NLM_F_ROOT;
  709. module_put(a->ops->owner);
  710. kfree(a);
  711. err = rtnetlink_send(skb, net, pid, RTNLGRP_TC,
  712. n->nlmsg_flags & NLM_F_ECHO);
  713. if (err > 0)
  714. return 0;
  715. return err;
  716. out_module_put:
  717. module_put(a->ops->owner);
  718. err_out:
  719. noflush_out:
  720. kfree_skb(skb);
  721. kfree(a);
  722. return err;
  723. }
  724. static int
  725. tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
  726. u32 pid, int event)
  727. {
  728. int i, ret;
  729. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  730. struct tc_action *head = NULL, *act, *act_prev = NULL;
  731. ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
  732. if (ret < 0)
  733. return ret;
  734. if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
  735. if (tb[1] != NULL)
  736. return tca_action_flush(net, tb[1], n, pid);
  737. else
  738. return -EINVAL;
  739. }
  740. for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
  741. act = tcf_action_get_1(tb[i], n, pid);
  742. if (IS_ERR(act)) {
  743. ret = PTR_ERR(act);
  744. goto err;
  745. }
  746. act->order = i;
  747. if (head == NULL)
  748. head = act;
  749. else
  750. act_prev->next = act;
  751. act_prev = act;
  752. }
  753. if (event == RTM_GETACTION)
  754. ret = act_get_notify(net, pid, n, head, event);
  755. else { /* delete */
  756. struct sk_buff *skb;
  757. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  758. if (!skb) {
  759. ret = -ENOBUFS;
  760. goto err;
  761. }
  762. if (tca_get_fill(skb, head, pid, n->nlmsg_seq, 0, event,
  763. 0, 1) <= 0) {
  764. kfree_skb(skb);
  765. ret = -EINVAL;
  766. goto err;
  767. }
  768. /* now do the delete */
  769. tcf_action_destroy(head, 0);
  770. ret = rtnetlink_send(skb, net, pid, RTNLGRP_TC,
  771. n->nlmsg_flags & NLM_F_ECHO);
  772. if (ret > 0)
  773. return 0;
  774. return ret;
  775. }
  776. err:
  777. cleanup_a(head);
  778. return ret;
  779. }
  780. static int tcf_add_notify(struct net *net, struct tc_action *a,
  781. u32 pid, u32 seq, int event, u16 flags)
  782. {
  783. struct tcamsg *t;
  784. struct nlmsghdr *nlh;
  785. struct sk_buff *skb;
  786. struct nlattr *nest;
  787. unsigned char *b;
  788. int err = 0;
  789. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  790. if (!skb)
  791. return -ENOBUFS;
  792. b = skb_tail_pointer(skb);
  793. nlh = nlmsg_put(skb, pid, seq, event, sizeof(*t), flags);
  794. if (!nlh)
  795. goto out_kfree_skb;
  796. t = nlmsg_data(nlh);
  797. t->tca_family = AF_UNSPEC;
  798. t->tca__pad1 = 0;
  799. t->tca__pad2 = 0;
  800. nest = nla_nest_start(skb, TCA_ACT_TAB);
  801. if (nest == NULL)
  802. goto out_kfree_skb;
  803. if (tcf_action_dump(skb, a, 0, 0) < 0)
  804. goto out_kfree_skb;
  805. nla_nest_end(skb, nest);
  806. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  807. NETLINK_CB(skb).dst_group = RTNLGRP_TC;
  808. err = rtnetlink_send(skb, net, pid, RTNLGRP_TC, flags & NLM_F_ECHO);
  809. if (err > 0)
  810. err = 0;
  811. return err;
  812. out_kfree_skb:
  813. kfree_skb(skb);
  814. return -1;
  815. }
  816. static int
  817. tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
  818. u32 pid, int ovr)
  819. {
  820. int ret = 0;
  821. struct tc_action *act;
  822. struct tc_action *a;
  823. u32 seq = n->nlmsg_seq;
  824. act = tcf_action_init(nla, NULL, NULL, ovr, 0);
  825. if (act == NULL)
  826. goto done;
  827. if (IS_ERR(act)) {
  828. ret = PTR_ERR(act);
  829. goto done;
  830. }
  831. /* dump then free all the actions after update; inserted policy
  832. * stays intact
  833. */
  834. ret = tcf_add_notify(net, act, pid, seq, RTM_NEWACTION, n->nlmsg_flags);
  835. for (a = act; a; a = act) {
  836. act = a->next;
  837. kfree(a);
  838. }
  839. done:
  840. return ret;
  841. }
  842. static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
  843. {
  844. struct net *net = sock_net(skb->sk);
  845. struct nlattr *tca[TCA_ACT_MAX + 1];
  846. u32 pid = skb ? NETLINK_CB(skb).pid : 0;
  847. int ret = 0, ovr = 0;
  848. ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
  849. if (ret < 0)
  850. return ret;
  851. if (tca[TCA_ACT_TAB] == NULL) {
  852. pr_notice("tc_ctl_action: received NO action attribs\n");
  853. return -EINVAL;
  854. }
  855. /* n->nlmsg_flags & NLM_F_CREATE */
  856. switch (n->nlmsg_type) {
  857. case RTM_NEWACTION:
  858. /* we are going to assume all other flags
  859. * imply create only if it doesn't exist
  860. * Note that CREATE | EXCL implies that
  861. * but since we want avoid ambiguity (eg when flags
  862. * is zero) then just set this
  863. */
  864. if (n->nlmsg_flags & NLM_F_REPLACE)
  865. ovr = 1;
  866. replay:
  867. ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, pid, ovr);
  868. if (ret == -EAGAIN)
  869. goto replay;
  870. break;
  871. case RTM_DELACTION:
  872. ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
  873. pid, RTM_DELACTION);
  874. break;
  875. case RTM_GETACTION:
  876. ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
  877. pid, RTM_GETACTION);
  878. break;
  879. default:
  880. BUG();
  881. }
  882. return ret;
  883. }
  884. static struct nlattr *
  885. find_dump_kind(const struct nlmsghdr *n)
  886. {
  887. struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
  888. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  889. struct nlattr *nla[TCAA_MAX + 1];
  890. struct nlattr *kind;
  891. if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
  892. return NULL;
  893. tb1 = nla[TCA_ACT_TAB];
  894. if (tb1 == NULL)
  895. return NULL;
  896. if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
  897. NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
  898. return NULL;
  899. if (tb[1] == NULL)
  900. return NULL;
  901. if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
  902. nla_len(tb[1]), NULL) < 0)
  903. return NULL;
  904. kind = tb2[TCA_ACT_KIND];
  905. return kind;
  906. }
  907. static int
  908. tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
  909. {
  910. struct nlmsghdr *nlh;
  911. unsigned char *b = skb_tail_pointer(skb);
  912. struct nlattr *nest;
  913. struct tc_action_ops *a_o;
  914. struct tc_action a;
  915. int ret = 0;
  916. struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh);
  917. struct nlattr *kind = find_dump_kind(cb->nlh);
  918. if (kind == NULL) {
  919. pr_info("tc_dump_action: action bad kind\n");
  920. return 0;
  921. }
  922. a_o = tc_lookup_action(kind);
  923. if (a_o == NULL)
  924. return 0;
  925. memset(&a, 0, sizeof(struct tc_action));
  926. a.ops = a_o;
  927. if (a_o->walk == NULL) {
  928. WARN(1, "tc_dump_action: %s !capable of dumping table\n",
  929. a_o->kind);
  930. goto out_module_put;
  931. }
  932. nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
  933. cb->nlh->nlmsg_type, sizeof(*t), 0);
  934. if (!nlh)
  935. goto out_module_put;
  936. t = nlmsg_data(nlh);
  937. t->tca_family = AF_UNSPEC;
  938. t->tca__pad1 = 0;
  939. t->tca__pad2 = 0;
  940. nest = nla_nest_start(skb, TCA_ACT_TAB);
  941. if (nest == NULL)
  942. goto out_module_put;
  943. ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
  944. if (ret < 0)
  945. goto out_module_put;
  946. if (ret > 0) {
  947. nla_nest_end(skb, nest);
  948. ret = skb->len;
  949. } else
  950. nla_nest_cancel(skb, nest);
  951. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  952. if (NETLINK_CB(cb->skb).pid && ret)
  953. nlh->nlmsg_flags |= NLM_F_MULTI;
  954. module_put(a_o->owner);
  955. return skb->len;
  956. out_module_put:
  957. module_put(a_o->owner);
  958. nlmsg_trim(skb, b);
  959. return skb->len;
  960. }
  961. static int __init tc_action_init(void)
  962. {
  963. rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL);
  964. rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL);
  965. rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action,
  966. NULL);
  967. return 0;
  968. }
  969. subsys_initcall(tc_action_init);