act_api.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  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 <net/net_namespace.h>
  23. #include <net/sock.h>
  24. #include <net/sch_generic.h>
  25. #include <net/act_api.h>
  26. #include <net/netlink.h>
  27. static void tcf_common_free_rcu(struct rcu_head *head)
  28. {
  29. kfree(container_of(head, struct tcf_common, tcfc_rcu));
  30. }
  31. void tcf_hash_destroy(struct tcf_common *p, struct tcf_hashinfo *hinfo)
  32. {
  33. unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
  34. struct tcf_common **p1p;
  35. for (p1p = &hinfo->htab[h]; *p1p; p1p = &(*p1p)->tcfc_next) {
  36. if (*p1p == p) {
  37. write_lock_bh(hinfo->lock);
  38. *p1p = p->tcfc_next;
  39. write_unlock_bh(hinfo->lock);
  40. gen_kill_estimator(&p->tcfc_bstats,
  41. &p->tcfc_rate_est);
  42. /*
  43. * gen_estimator est_timer() might access p->tcfc_lock
  44. * or bstats, wait a RCU grace period before freeing p
  45. */
  46. call_rcu(&p->tcfc_rcu, tcf_common_free_rcu);
  47. return;
  48. }
  49. }
  50. WARN_ON(1);
  51. }
  52. EXPORT_SYMBOL(tcf_hash_destroy);
  53. int tcf_hash_release(struct tcf_common *p, int bind,
  54. struct tcf_hashinfo *hinfo)
  55. {
  56. int ret = 0;
  57. if (p) {
  58. if (bind)
  59. p->tcfc_bindcnt--;
  60. p->tcfc_refcnt--;
  61. if (p->tcfc_bindcnt <= 0 && p->tcfc_refcnt <= 0) {
  62. tcf_hash_destroy(p, hinfo);
  63. ret = 1;
  64. }
  65. }
  66. return ret;
  67. }
  68. EXPORT_SYMBOL(tcf_hash_release);
  69. static int tcf_dump_walker(struct sk_buff *skb, struct netlink_callback *cb,
  70. struct tc_action *a, struct tcf_hashinfo *hinfo)
  71. {
  72. struct tcf_common *p;
  73. int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
  74. struct nlattr *nest;
  75. read_lock_bh(hinfo->lock);
  76. s_i = cb->args[0];
  77. for (i = 0; i < (hinfo->hmask + 1); i++) {
  78. p = hinfo->htab[tcf_hash(i, hinfo->hmask)];
  79. for (; p; p = p->tcfc_next) {
  80. index++;
  81. if (index < s_i)
  82. continue;
  83. a->priv = p;
  84. a->order = n_i;
  85. nest = nla_nest_start(skb, a->order);
  86. if (nest == NULL)
  87. goto nla_put_failure;
  88. err = tcf_action_dump_1(skb, a, 0, 0);
  89. if (err < 0) {
  90. index--;
  91. nlmsg_trim(skb, nest);
  92. goto done;
  93. }
  94. nla_nest_end(skb, nest);
  95. n_i++;
  96. if (n_i >= TCA_ACT_MAX_PRIO)
  97. goto done;
  98. }
  99. }
  100. done:
  101. read_unlock_bh(hinfo->lock);
  102. if (n_i)
  103. cb->args[0] += n_i;
  104. return n_i;
  105. nla_put_failure:
  106. nla_nest_cancel(skb, nest);
  107. goto done;
  108. }
  109. static int tcf_del_walker(struct sk_buff *skb, struct tc_action *a,
  110. struct tcf_hashinfo *hinfo)
  111. {
  112. struct tcf_common *p, *s_p;
  113. struct nlattr *nest;
  114. int i = 0, n_i = 0;
  115. nest = nla_nest_start(skb, a->order);
  116. if (nest == NULL)
  117. goto nla_put_failure;
  118. NLA_PUT_STRING(skb, TCA_KIND, a->ops->kind);
  119. for (i = 0; i < (hinfo->hmask + 1); i++) {
  120. p = hinfo->htab[tcf_hash(i, hinfo->hmask)];
  121. while (p != NULL) {
  122. s_p = p->tcfc_next;
  123. if (ACT_P_DELETED == tcf_hash_release(p, 0, hinfo))
  124. module_put(a->ops->owner);
  125. n_i++;
  126. p = s_p;
  127. }
  128. }
  129. NLA_PUT_U32(skb, TCA_FCNT, n_i);
  130. nla_nest_end(skb, nest);
  131. return n_i;
  132. nla_put_failure:
  133. nla_nest_cancel(skb, nest);
  134. return -EINVAL;
  135. }
  136. int tcf_generic_walker(struct sk_buff *skb, struct netlink_callback *cb,
  137. int type, struct tc_action *a)
  138. {
  139. struct tcf_hashinfo *hinfo = a->ops->hinfo;
  140. if (type == RTM_DELACTION) {
  141. return tcf_del_walker(skb, a, hinfo);
  142. } else if (type == RTM_GETACTION) {
  143. return tcf_dump_walker(skb, cb, a, hinfo);
  144. } else {
  145. WARN(1, "tcf_generic_walker: unknown action %d\n", type);
  146. return -EINVAL;
  147. }
  148. }
  149. EXPORT_SYMBOL(tcf_generic_walker);
  150. struct tcf_common *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo)
  151. {
  152. struct tcf_common *p;
  153. read_lock_bh(hinfo->lock);
  154. for (p = hinfo->htab[tcf_hash(index, hinfo->hmask)]; p;
  155. p = p->tcfc_next) {
  156. if (p->tcfc_index == index)
  157. break;
  158. }
  159. read_unlock_bh(hinfo->lock);
  160. return p;
  161. }
  162. EXPORT_SYMBOL(tcf_hash_lookup);
  163. u32 tcf_hash_new_index(u32 *idx_gen, struct tcf_hashinfo *hinfo)
  164. {
  165. u32 val = *idx_gen;
  166. do {
  167. if (++val == 0)
  168. val = 1;
  169. } while (tcf_hash_lookup(val, hinfo));
  170. return (*idx_gen = val);
  171. }
  172. EXPORT_SYMBOL(tcf_hash_new_index);
  173. int tcf_hash_search(struct tc_action *a, u32 index)
  174. {
  175. struct tcf_hashinfo *hinfo = a->ops->hinfo;
  176. struct tcf_common *p = tcf_hash_lookup(index, hinfo);
  177. if (p) {
  178. a->priv = p;
  179. return 1;
  180. }
  181. return 0;
  182. }
  183. EXPORT_SYMBOL(tcf_hash_search);
  184. struct tcf_common *tcf_hash_check(u32 index, struct tc_action *a, int bind,
  185. struct tcf_hashinfo *hinfo)
  186. {
  187. struct tcf_common *p = NULL;
  188. if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) {
  189. if (bind)
  190. p->tcfc_bindcnt++;
  191. p->tcfc_refcnt++;
  192. a->priv = p;
  193. }
  194. return p;
  195. }
  196. EXPORT_SYMBOL(tcf_hash_check);
  197. struct tcf_common *tcf_hash_create(u32 index, struct nlattr *est,
  198. struct tc_action *a, int size, int bind,
  199. u32 *idx_gen, struct tcf_hashinfo *hinfo)
  200. {
  201. struct tcf_common *p = kzalloc(size, GFP_KERNEL);
  202. if (unlikely(!p))
  203. return ERR_PTR(-ENOMEM);
  204. p->tcfc_refcnt = 1;
  205. if (bind)
  206. p->tcfc_bindcnt = 1;
  207. spin_lock_init(&p->tcfc_lock);
  208. p->tcfc_index = index ? index : tcf_hash_new_index(idx_gen, hinfo);
  209. p->tcfc_tm.install = jiffies;
  210. p->tcfc_tm.lastuse = jiffies;
  211. if (est) {
  212. int err = gen_new_estimator(&p->tcfc_bstats, &p->tcfc_rate_est,
  213. &p->tcfc_lock, est);
  214. if (err) {
  215. kfree(p);
  216. return ERR_PTR(err);
  217. }
  218. }
  219. a->priv = (void *) p;
  220. return p;
  221. }
  222. EXPORT_SYMBOL(tcf_hash_create);
  223. void tcf_hash_insert(struct tcf_common *p, struct tcf_hashinfo *hinfo)
  224. {
  225. unsigned int h = tcf_hash(p->tcfc_index, hinfo->hmask);
  226. write_lock_bh(hinfo->lock);
  227. p->tcfc_next = hinfo->htab[h];
  228. hinfo->htab[h] = p;
  229. write_unlock_bh(hinfo->lock);
  230. }
  231. EXPORT_SYMBOL(tcf_hash_insert);
  232. static struct tc_action_ops *act_base = NULL;
  233. static DEFINE_RWLOCK(act_mod_lock);
  234. int tcf_register_action(struct tc_action_ops *act)
  235. {
  236. struct tc_action_ops *a, **ap;
  237. write_lock(&act_mod_lock);
  238. for (ap = &act_base; (a = *ap) != NULL; ap = &a->next) {
  239. if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
  240. write_unlock(&act_mod_lock);
  241. return -EEXIST;
  242. }
  243. }
  244. act->next = NULL;
  245. *ap = act;
  246. write_unlock(&act_mod_lock);
  247. return 0;
  248. }
  249. EXPORT_SYMBOL(tcf_register_action);
  250. int tcf_unregister_action(struct tc_action_ops *act)
  251. {
  252. struct tc_action_ops *a, **ap;
  253. int err = -ENOENT;
  254. write_lock(&act_mod_lock);
  255. for (ap = &act_base; (a = *ap) != NULL; ap = &a->next)
  256. if (a == act)
  257. break;
  258. if (a) {
  259. *ap = a->next;
  260. a->next = NULL;
  261. err = 0;
  262. }
  263. write_unlock(&act_mod_lock);
  264. return err;
  265. }
  266. EXPORT_SYMBOL(tcf_unregister_action);
  267. /* lookup by name */
  268. static struct tc_action_ops *tc_lookup_action_n(char *kind)
  269. {
  270. struct tc_action_ops *a = NULL;
  271. if (kind) {
  272. read_lock(&act_mod_lock);
  273. for (a = act_base; a; a = a->next) {
  274. if (strcmp(kind, a->kind) == 0) {
  275. if (!try_module_get(a->owner)) {
  276. read_unlock(&act_mod_lock);
  277. return NULL;
  278. }
  279. break;
  280. }
  281. }
  282. read_unlock(&act_mod_lock);
  283. }
  284. return a;
  285. }
  286. /* lookup by nlattr */
  287. static struct tc_action_ops *tc_lookup_action(struct nlattr *kind)
  288. {
  289. struct tc_action_ops *a = NULL;
  290. if (kind) {
  291. read_lock(&act_mod_lock);
  292. for (a = act_base; a; a = a->next) {
  293. if (nla_strcmp(kind, a->kind) == 0) {
  294. if (!try_module_get(a->owner)) {
  295. read_unlock(&act_mod_lock);
  296. return NULL;
  297. }
  298. break;
  299. }
  300. }
  301. read_unlock(&act_mod_lock);
  302. }
  303. return a;
  304. }
  305. #if 0
  306. /* lookup by id */
  307. static struct tc_action_ops *tc_lookup_action_id(u32 type)
  308. {
  309. struct tc_action_ops *a = NULL;
  310. if (type) {
  311. read_lock(&act_mod_lock);
  312. for (a = act_base; a; a = a->next) {
  313. if (a->type == type) {
  314. if (!try_module_get(a->owner)) {
  315. read_unlock(&act_mod_lock);
  316. return NULL;
  317. }
  318. break;
  319. }
  320. }
  321. read_unlock(&act_mod_lock);
  322. }
  323. return a;
  324. }
  325. #endif
  326. int tcf_action_exec(struct sk_buff *skb, struct tc_action *act,
  327. struct tcf_result *res)
  328. {
  329. struct tc_action *a;
  330. int ret = -1;
  331. if (skb->tc_verd & TC_NCLS) {
  332. skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
  333. ret = TC_ACT_OK;
  334. goto exec_done;
  335. }
  336. while ((a = act) != NULL) {
  337. repeat:
  338. if (a->ops && a->ops->act) {
  339. ret = a->ops->act(skb, a, res);
  340. if (TC_MUNGED & skb->tc_verd) {
  341. /* copied already, allow trampling */
  342. skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
  343. skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd);
  344. }
  345. if (ret == TC_ACT_REPEAT)
  346. goto repeat; /* we need a ttl - JHS */
  347. if (ret != TC_ACT_PIPE)
  348. goto exec_done;
  349. }
  350. act = a->next;
  351. }
  352. exec_done:
  353. return ret;
  354. }
  355. EXPORT_SYMBOL(tcf_action_exec);
  356. void tcf_action_destroy(struct tc_action *act, int bind)
  357. {
  358. struct tc_action *a;
  359. for (a = act; a; a = act) {
  360. if (a->ops && a->ops->cleanup) {
  361. if (a->ops->cleanup(a, bind) == ACT_P_DELETED)
  362. module_put(a->ops->owner);
  363. act = act->next;
  364. kfree(a);
  365. } else {
  366. /*FIXME: Remove later - catch insertion bugs*/
  367. WARN(1, "tcf_action_destroy: BUG? destroying NULL ops\n");
  368. act = act->next;
  369. kfree(a);
  370. }
  371. }
  372. }
  373. int
  374. tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  375. {
  376. int err = -EINVAL;
  377. if (a->ops == NULL || a->ops->dump == NULL)
  378. return err;
  379. return a->ops->dump(skb, a, bind, ref);
  380. }
  381. int
  382. tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
  383. {
  384. int err = -EINVAL;
  385. unsigned char *b = skb_tail_pointer(skb);
  386. struct nlattr *nest;
  387. if (a->ops == NULL || a->ops->dump == NULL)
  388. return err;
  389. NLA_PUT_STRING(skb, TCA_KIND, a->ops->kind);
  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_NEW(skb, pid, seq, event, sizeof(*t), flags);
  571. t = NLMSG_DATA(nlh);
  572. t->tca_family = AF_UNSPEC;
  573. t->tca__pad1 = 0;
  574. t->tca__pad2 = 0;
  575. nest = nla_nest_start(skb, TCA_ACT_TAB);
  576. if (nest == NULL)
  577. goto nla_put_failure;
  578. if (tcf_action_dump(skb, a, bind, ref) < 0)
  579. goto nla_put_failure;
  580. nla_nest_end(skb, nest);
  581. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  582. return skb->len;
  583. nla_put_failure:
  584. nlmsg_failure:
  585. nlmsg_trim(skb, b);
  586. return -1;
  587. }
  588. static int
  589. act_get_notify(struct net *net, u32 pid, struct nlmsghdr *n,
  590. struct tc_action *a, int event)
  591. {
  592. struct sk_buff *skb;
  593. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  594. if (!skb)
  595. return -ENOBUFS;
  596. if (tca_get_fill(skb, a, pid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
  597. kfree_skb(skb);
  598. return -EINVAL;
  599. }
  600. return rtnl_unicast(skb, net, pid);
  601. }
  602. static struct tc_action *
  603. tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 pid)
  604. {
  605. struct nlattr *tb[TCA_ACT_MAX + 1];
  606. struct tc_action *a;
  607. int index;
  608. int err;
  609. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
  610. if (err < 0)
  611. goto err_out;
  612. err = -EINVAL;
  613. if (tb[TCA_ACT_INDEX] == NULL ||
  614. nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
  615. goto err_out;
  616. index = nla_get_u32(tb[TCA_ACT_INDEX]);
  617. err = -ENOMEM;
  618. a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
  619. if (a == NULL)
  620. goto err_out;
  621. err = -EINVAL;
  622. a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
  623. if (a->ops == NULL)
  624. goto err_free;
  625. if (a->ops->lookup == NULL)
  626. goto err_mod;
  627. err = -ENOENT;
  628. if (a->ops->lookup(a, index) == 0)
  629. goto err_mod;
  630. module_put(a->ops->owner);
  631. return a;
  632. err_mod:
  633. module_put(a->ops->owner);
  634. err_free:
  635. kfree(a);
  636. err_out:
  637. return ERR_PTR(err);
  638. }
  639. static void cleanup_a(struct tc_action *act)
  640. {
  641. struct tc_action *a;
  642. for (a = act; a; a = act) {
  643. act = a->next;
  644. kfree(a);
  645. }
  646. }
  647. static struct tc_action *create_a(int i)
  648. {
  649. struct tc_action *act;
  650. act = kzalloc(sizeof(*act), GFP_KERNEL);
  651. if (act == NULL) {
  652. pr_debug("create_a: failed to alloc!\n");
  653. return NULL;
  654. }
  655. act->order = i;
  656. return act;
  657. }
  658. static int tca_action_flush(struct net *net, struct nlattr *nla,
  659. struct nlmsghdr *n, u32 pid)
  660. {
  661. struct sk_buff *skb;
  662. unsigned char *b;
  663. struct nlmsghdr *nlh;
  664. struct tcamsg *t;
  665. struct netlink_callback dcb;
  666. struct nlattr *nest;
  667. struct nlattr *tb[TCA_ACT_MAX + 1];
  668. struct nlattr *kind;
  669. struct tc_action *a = create_a(0);
  670. int err = -ENOMEM;
  671. if (a == NULL) {
  672. pr_debug("tca_action_flush: couldnt create tc_action\n");
  673. return err;
  674. }
  675. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  676. if (!skb) {
  677. pr_debug("tca_action_flush: failed skb alloc\n");
  678. kfree(a);
  679. return err;
  680. }
  681. b = skb_tail_pointer(skb);
  682. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
  683. if (err < 0)
  684. goto err_out;
  685. err = -EINVAL;
  686. kind = tb[TCA_ACT_KIND];
  687. a->ops = tc_lookup_action(kind);
  688. if (a->ops == NULL)
  689. goto err_out;
  690. nlh = NLMSG_PUT(skb, pid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t));
  691. t = NLMSG_DATA(nlh);
  692. t->tca_family = AF_UNSPEC;
  693. t->tca__pad1 = 0;
  694. t->tca__pad2 = 0;
  695. nest = nla_nest_start(skb, TCA_ACT_TAB);
  696. if (nest == NULL)
  697. goto nla_put_failure;
  698. err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
  699. if (err < 0)
  700. goto nla_put_failure;
  701. if (err == 0)
  702. goto noflush_out;
  703. nla_nest_end(skb, nest);
  704. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  705. nlh->nlmsg_flags |= NLM_F_ROOT;
  706. module_put(a->ops->owner);
  707. kfree(a);
  708. err = rtnetlink_send(skb, net, pid, RTNLGRP_TC,
  709. n->nlmsg_flags & NLM_F_ECHO);
  710. if (err > 0)
  711. return 0;
  712. return err;
  713. nla_put_failure:
  714. nlmsg_failure:
  715. module_put(a->ops->owner);
  716. err_out:
  717. noflush_out:
  718. kfree_skb(skb);
  719. kfree(a);
  720. return err;
  721. }
  722. static int
  723. tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
  724. u32 pid, int event)
  725. {
  726. int i, ret;
  727. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  728. struct tc_action *head = NULL, *act, *act_prev = NULL;
  729. ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
  730. if (ret < 0)
  731. return ret;
  732. if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) {
  733. if (tb[1] != NULL)
  734. return tca_action_flush(net, tb[1], n, pid);
  735. else
  736. return -EINVAL;
  737. }
  738. for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
  739. act = tcf_action_get_1(tb[i], n, pid);
  740. if (IS_ERR(act)) {
  741. ret = PTR_ERR(act);
  742. goto err;
  743. }
  744. act->order = i;
  745. if (head == NULL)
  746. head = act;
  747. else
  748. act_prev->next = act;
  749. act_prev = act;
  750. }
  751. if (event == RTM_GETACTION)
  752. ret = act_get_notify(net, pid, n, head, event);
  753. else { /* delete */
  754. struct sk_buff *skb;
  755. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  756. if (!skb) {
  757. ret = -ENOBUFS;
  758. goto err;
  759. }
  760. if (tca_get_fill(skb, head, pid, n->nlmsg_seq, 0, event,
  761. 0, 1) <= 0) {
  762. kfree_skb(skb);
  763. ret = -EINVAL;
  764. goto err;
  765. }
  766. /* now do the delete */
  767. tcf_action_destroy(head, 0);
  768. ret = rtnetlink_send(skb, net, pid, RTNLGRP_TC,
  769. n->nlmsg_flags & NLM_F_ECHO);
  770. if (ret > 0)
  771. return 0;
  772. return ret;
  773. }
  774. err:
  775. cleanup_a(head);
  776. return ret;
  777. }
  778. static int tcf_add_notify(struct net *net, struct tc_action *a,
  779. u32 pid, u32 seq, int event, u16 flags)
  780. {
  781. struct tcamsg *t;
  782. struct nlmsghdr *nlh;
  783. struct sk_buff *skb;
  784. struct nlattr *nest;
  785. unsigned char *b;
  786. int err = 0;
  787. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  788. if (!skb)
  789. return -ENOBUFS;
  790. b = skb_tail_pointer(skb);
  791. nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
  792. t = NLMSG_DATA(nlh);
  793. t->tca_family = AF_UNSPEC;
  794. t->tca__pad1 = 0;
  795. t->tca__pad2 = 0;
  796. nest = nla_nest_start(skb, TCA_ACT_TAB);
  797. if (nest == NULL)
  798. goto nla_put_failure;
  799. if (tcf_action_dump(skb, a, 0, 0) < 0)
  800. goto nla_put_failure;
  801. nla_nest_end(skb, nest);
  802. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  803. NETLINK_CB(skb).dst_group = RTNLGRP_TC;
  804. err = rtnetlink_send(skb, net, pid, RTNLGRP_TC, flags & NLM_F_ECHO);
  805. if (err > 0)
  806. err = 0;
  807. return err;
  808. nla_put_failure:
  809. nlmsg_failure:
  810. kfree_skb(skb);
  811. return -1;
  812. }
  813. static int
  814. tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
  815. u32 pid, int ovr)
  816. {
  817. int ret = 0;
  818. struct tc_action *act;
  819. struct tc_action *a;
  820. u32 seq = n->nlmsg_seq;
  821. act = tcf_action_init(nla, NULL, NULL, ovr, 0);
  822. if (act == NULL)
  823. goto done;
  824. if (IS_ERR(act)) {
  825. ret = PTR_ERR(act);
  826. goto done;
  827. }
  828. /* dump then free all the actions after update; inserted policy
  829. * stays intact
  830. */
  831. ret = tcf_add_notify(net, act, pid, seq, RTM_NEWACTION, n->nlmsg_flags);
  832. for (a = act; a; a = act) {
  833. act = a->next;
  834. kfree(a);
  835. }
  836. done:
  837. return ret;
  838. }
  839. static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
  840. {
  841. struct net *net = sock_net(skb->sk);
  842. struct nlattr *tca[TCA_ACT_MAX + 1];
  843. u32 pid = skb ? NETLINK_CB(skb).pid : 0;
  844. int ret = 0, ovr = 0;
  845. ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
  846. if (ret < 0)
  847. return ret;
  848. if (tca[TCA_ACT_TAB] == NULL) {
  849. pr_notice("tc_ctl_action: received NO action attribs\n");
  850. return -EINVAL;
  851. }
  852. /* n->nlmsg_flags & NLM_F_CREATE */
  853. switch (n->nlmsg_type) {
  854. case RTM_NEWACTION:
  855. /* we are going to assume all other flags
  856. * imply create only if it doesnt exist
  857. * Note that CREATE | EXCL implies that
  858. * but since we want avoid ambiguity (eg when flags
  859. * is zero) then just set this
  860. */
  861. if (n->nlmsg_flags & NLM_F_REPLACE)
  862. ovr = 1;
  863. replay:
  864. ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, pid, ovr);
  865. if (ret == -EAGAIN)
  866. goto replay;
  867. break;
  868. case RTM_DELACTION:
  869. ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
  870. pid, RTM_DELACTION);
  871. break;
  872. case RTM_GETACTION:
  873. ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
  874. pid, RTM_GETACTION);
  875. break;
  876. default:
  877. BUG();
  878. }
  879. return ret;
  880. }
  881. static struct nlattr *
  882. find_dump_kind(const struct nlmsghdr *n)
  883. {
  884. struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1];
  885. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  886. struct nlattr *nla[TCAA_MAX + 1];
  887. struct nlattr *kind;
  888. if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
  889. return NULL;
  890. tb1 = nla[TCA_ACT_TAB];
  891. if (tb1 == NULL)
  892. return NULL;
  893. if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
  894. NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
  895. return NULL;
  896. if (tb[1] == NULL)
  897. return NULL;
  898. if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
  899. nla_len(tb[1]), NULL) < 0)
  900. return NULL;
  901. kind = tb2[TCA_ACT_KIND];
  902. return kind;
  903. }
  904. static int
  905. tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
  906. {
  907. struct nlmsghdr *nlh;
  908. unsigned char *b = skb_tail_pointer(skb);
  909. struct nlattr *nest;
  910. struct tc_action_ops *a_o;
  911. struct tc_action a;
  912. int ret = 0;
  913. struct tcamsg *t = (struct tcamsg *) NLMSG_DATA(cb->nlh);
  914. struct nlattr *kind = find_dump_kind(cb->nlh);
  915. if (kind == NULL) {
  916. pr_info("tc_dump_action: action bad kind\n");
  917. return 0;
  918. }
  919. a_o = tc_lookup_action(kind);
  920. if (a_o == NULL)
  921. return 0;
  922. memset(&a, 0, sizeof(struct tc_action));
  923. a.ops = a_o;
  924. if (a_o->walk == NULL) {
  925. WARN(1, "tc_dump_action: %s !capable of dumping table\n",
  926. a_o->kind);
  927. goto nla_put_failure;
  928. }
  929. nlh = NLMSG_PUT(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq,
  930. cb->nlh->nlmsg_type, sizeof(*t));
  931. t = NLMSG_DATA(nlh);
  932. t->tca_family = AF_UNSPEC;
  933. t->tca__pad1 = 0;
  934. t->tca__pad2 = 0;
  935. nest = nla_nest_start(skb, TCA_ACT_TAB);
  936. if (nest == NULL)
  937. goto nla_put_failure;
  938. ret = a_o->walk(skb, cb, RTM_GETACTION, &a);
  939. if (ret < 0)
  940. goto nla_put_failure;
  941. if (ret > 0) {
  942. nla_nest_end(skb, nest);
  943. ret = skb->len;
  944. } else
  945. nla_nest_cancel(skb, nest);
  946. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  947. if (NETLINK_CB(cb->skb).pid && ret)
  948. nlh->nlmsg_flags |= NLM_F_MULTI;
  949. module_put(a_o->owner);
  950. return skb->len;
  951. nla_put_failure:
  952. nlmsg_failure:
  953. module_put(a_o->owner);
  954. nlmsg_trim(skb, b);
  955. return skb->len;
  956. }
  957. static int __init tc_action_init(void)
  958. {
  959. rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL);
  960. rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL);
  961. rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action);
  962. return 0;
  963. }
  964. subsys_initcall(tc_action_init);