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. if ((err = tcf_action_dump_old(skb, a, bind, ref)) > 0) {
  396. nla_nest_end(skb, nest);
  397. return err;
  398. }
  399. nla_put_failure:
  400. nlmsg_trim(skb, b);
  401. return -1;
  402. }
  403. EXPORT_SYMBOL(tcf_action_dump_1);
  404. int
  405. tcf_action_dump(struct sk_buff *skb, struct tc_action *act, int bind, int ref)
  406. {
  407. struct tc_action *a;
  408. int err = -EINVAL;
  409. struct nlattr *nest;
  410. while ((a = act) != NULL) {
  411. act = a->next;
  412. nest = nla_nest_start(skb, a->order);
  413. if (nest == NULL)
  414. goto nla_put_failure;
  415. err = tcf_action_dump_1(skb, a, bind, ref);
  416. if (err < 0)
  417. goto errout;
  418. nla_nest_end(skb, nest);
  419. }
  420. return 0;
  421. nla_put_failure:
  422. err = -EINVAL;
  423. errout:
  424. nla_nest_cancel(skb, nest);
  425. return err;
  426. }
  427. struct tc_action *tcf_action_init_1(struct nlattr *nla, struct nlattr *est,
  428. char *name, int ovr, int bind)
  429. {
  430. struct tc_action *a;
  431. struct tc_action_ops *a_o;
  432. char act_name[IFNAMSIZ];
  433. struct nlattr *tb[TCA_ACT_MAX+1];
  434. struct nlattr *kind;
  435. int err;
  436. if (name == NULL) {
  437. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
  438. if (err < 0)
  439. goto err_out;
  440. err = -EINVAL;
  441. kind = tb[TCA_ACT_KIND];
  442. if (kind == NULL)
  443. goto err_out;
  444. if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ)
  445. goto err_out;
  446. } else {
  447. err = -EINVAL;
  448. if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ)
  449. goto err_out;
  450. }
  451. a_o = tc_lookup_action_n(act_name);
  452. if (a_o == NULL) {
  453. #ifdef CONFIG_MODULES
  454. rtnl_unlock();
  455. request_module("act_%s", act_name);
  456. rtnl_lock();
  457. a_o = tc_lookup_action_n(act_name);
  458. /* We dropped the RTNL semaphore in order to
  459. * perform the module load. So, even if we
  460. * succeeded in loading the module we have to
  461. * tell the caller to replay the request. We
  462. * indicate this using -EAGAIN.
  463. */
  464. if (a_o != NULL) {
  465. err = -EAGAIN;
  466. goto err_mod;
  467. }
  468. #endif
  469. err = -ENOENT;
  470. goto err_out;
  471. }
  472. err = -ENOMEM;
  473. a = kzalloc(sizeof(*a), GFP_KERNEL);
  474. if (a == NULL)
  475. goto err_mod;
  476. /* backward compatibility for policer */
  477. if (name == NULL)
  478. err = a_o->init(tb[TCA_ACT_OPTIONS], est, a, ovr, bind);
  479. else
  480. err = a_o->init(nla, est, a, ovr, bind);
  481. if (err < 0)
  482. goto err_free;
  483. /* module count goes up only when brand new policy is created
  484. if it exists and is only bound to in a_o->init() then
  485. ACT_P_CREATED is not returned (a zero is).
  486. */
  487. if (err != ACT_P_CREATED)
  488. module_put(a_o->owner);
  489. a->ops = a_o;
  490. return a;
  491. err_free:
  492. kfree(a);
  493. err_mod:
  494. module_put(a_o->owner);
  495. err_out:
  496. return ERR_PTR(err);
  497. }
  498. struct tc_action *tcf_action_init(struct nlattr *nla, struct nlattr *est,
  499. char *name, int ovr, int bind)
  500. {
  501. struct nlattr *tb[TCA_ACT_MAX_PRIO+1];
  502. struct tc_action *head = NULL, *act, *act_prev = NULL;
  503. int err;
  504. int i;
  505. err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
  506. if (err < 0)
  507. return ERR_PTR(err);
  508. for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
  509. act = tcf_action_init_1(tb[i], est, name, ovr, bind);
  510. if (IS_ERR(act))
  511. goto err;
  512. act->order = i;
  513. if (head == NULL)
  514. head = act;
  515. else
  516. act_prev->next = act;
  517. act_prev = act;
  518. }
  519. return head;
  520. err:
  521. if (head != NULL)
  522. tcf_action_destroy(head, bind);
  523. return act;
  524. }
  525. int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a,
  526. int compat_mode)
  527. {
  528. int err = 0;
  529. struct gnet_dump d;
  530. struct tcf_act_hdr *h = a->priv;
  531. if (h == NULL)
  532. goto errout;
  533. /* compat_mode being true specifies a call that is supposed
  534. * to add additional backward compatibility statistic TLVs.
  535. */
  536. if (compat_mode) {
  537. if (a->type == TCA_OLD_COMPAT)
  538. err = gnet_stats_start_copy_compat(skb, 0,
  539. TCA_STATS, TCA_XSTATS, &h->tcf_lock, &d);
  540. else
  541. return 0;
  542. } else
  543. err = gnet_stats_start_copy(skb, TCA_ACT_STATS,
  544. &h->tcf_lock, &d);
  545. if (err < 0)
  546. goto errout;
  547. if (a->ops != NULL && a->ops->get_stats != NULL)
  548. if (a->ops->get_stats(skb, a) < 0)
  549. goto errout;
  550. if (gnet_stats_copy_basic(&d, &h->tcf_bstats) < 0 ||
  551. gnet_stats_copy_rate_est(&d, &h->tcf_bstats,
  552. &h->tcf_rate_est) < 0 ||
  553. gnet_stats_copy_queue(&d, &h->tcf_qstats) < 0)
  554. goto errout;
  555. if (gnet_stats_finish_copy(&d) < 0)
  556. goto errout;
  557. return 0;
  558. errout:
  559. return -1;
  560. }
  561. static int
  562. tca_get_fill(struct sk_buff *skb, struct tc_action *a, u32 pid, u32 seq,
  563. u16 flags, int event, int bind, int ref)
  564. {
  565. struct tcamsg *t;
  566. struct nlmsghdr *nlh;
  567. unsigned char *b = skb_tail_pointer(skb);
  568. struct nlattr *nest;
  569. nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
  570. t = NLMSG_DATA(nlh);
  571. t->tca_family = AF_UNSPEC;
  572. t->tca__pad1 = 0;
  573. t->tca__pad2 = 0;
  574. nest = nla_nest_start(skb, TCA_ACT_TAB);
  575. if (nest == NULL)
  576. goto nla_put_failure;
  577. if (tcf_action_dump(skb, a, bind, ref) < 0)
  578. goto nla_put_failure;
  579. nla_nest_end(skb, nest);
  580. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  581. return skb->len;
  582. nla_put_failure:
  583. nlmsg_failure:
  584. nlmsg_trim(skb, b);
  585. return -1;
  586. }
  587. static int
  588. act_get_notify(struct net *net, u32 pid, struct nlmsghdr *n,
  589. struct tc_action *a, int event)
  590. {
  591. struct sk_buff *skb;
  592. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  593. if (!skb)
  594. return -ENOBUFS;
  595. if (tca_get_fill(skb, a, pid, n->nlmsg_seq, 0, event, 0, 0) <= 0) {
  596. kfree_skb(skb);
  597. return -EINVAL;
  598. }
  599. return rtnl_unicast(skb, net, pid);
  600. }
  601. static struct tc_action *
  602. tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 pid)
  603. {
  604. struct nlattr *tb[TCA_ACT_MAX+1];
  605. struct tc_action *a;
  606. int index;
  607. int err;
  608. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
  609. if (err < 0)
  610. goto err_out;
  611. err = -EINVAL;
  612. if (tb[TCA_ACT_INDEX] == NULL ||
  613. nla_len(tb[TCA_ACT_INDEX]) < sizeof(index))
  614. goto err_out;
  615. index = nla_get_u32(tb[TCA_ACT_INDEX]);
  616. err = -ENOMEM;
  617. a = kzalloc(sizeof(struct tc_action), GFP_KERNEL);
  618. if (a == NULL)
  619. goto err_out;
  620. err = -EINVAL;
  621. a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
  622. if (a->ops == NULL)
  623. goto err_free;
  624. if (a->ops->lookup == NULL)
  625. goto err_mod;
  626. err = -ENOENT;
  627. if (a->ops->lookup(a, index) == 0)
  628. goto err_mod;
  629. module_put(a->ops->owner);
  630. return a;
  631. err_mod:
  632. module_put(a->ops->owner);
  633. err_free:
  634. kfree(a);
  635. err_out:
  636. return ERR_PTR(err);
  637. }
  638. static void cleanup_a(struct tc_action *act)
  639. {
  640. struct tc_action *a;
  641. for (a = act; a; a = act) {
  642. act = a->next;
  643. kfree(a);
  644. }
  645. }
  646. static struct tc_action *create_a(int i)
  647. {
  648. struct tc_action *act;
  649. act = kzalloc(sizeof(*act), GFP_KERNEL);
  650. if (act == NULL) {
  651. pr_debug("create_a: failed to alloc!\n");
  652. return NULL;
  653. }
  654. act->order = i;
  655. return act;
  656. }
  657. static int tca_action_flush(struct net *net, struct nlattr *nla,
  658. struct nlmsghdr *n, u32 pid)
  659. {
  660. struct sk_buff *skb;
  661. unsigned char *b;
  662. struct nlmsghdr *nlh;
  663. struct tcamsg *t;
  664. struct netlink_callback dcb;
  665. struct nlattr *nest;
  666. struct nlattr *tb[TCA_ACT_MAX+1];
  667. struct nlattr *kind;
  668. struct tc_action *a = create_a(0);
  669. int err = -ENOMEM;
  670. if (a == NULL) {
  671. pr_debug("tca_action_flush: couldnt create tc_action\n");
  672. return err;
  673. }
  674. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  675. if (!skb) {
  676. pr_debug("tca_action_flush: failed skb alloc\n");
  677. kfree(a);
  678. return err;
  679. }
  680. b = skb_tail_pointer(skb);
  681. err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL);
  682. if (err < 0)
  683. goto err_out;
  684. err = -EINVAL;
  685. kind = tb[TCA_ACT_KIND];
  686. a->ops = tc_lookup_action(kind);
  687. if (a->ops == NULL)
  688. goto err_out;
  689. nlh = NLMSG_PUT(skb, pid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t));
  690. t = NLMSG_DATA(nlh);
  691. t->tca_family = AF_UNSPEC;
  692. t->tca__pad1 = 0;
  693. t->tca__pad2 = 0;
  694. nest = nla_nest_start(skb, TCA_ACT_TAB);
  695. if (nest == NULL)
  696. goto nla_put_failure;
  697. err = a->ops->walk(skb, &dcb, RTM_DELACTION, a);
  698. if (err < 0)
  699. goto nla_put_failure;
  700. if (err == 0)
  701. goto noflush_out;
  702. nla_nest_end(skb, nest);
  703. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  704. nlh->nlmsg_flags |= NLM_F_ROOT;
  705. module_put(a->ops->owner);
  706. kfree(a);
  707. err = rtnetlink_send(skb, net, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO);
  708. if (err > 0)
  709. return 0;
  710. return err;
  711. nla_put_failure:
  712. nlmsg_failure:
  713. module_put(a->ops->owner);
  714. err_out:
  715. noflush_out:
  716. kfree_skb(skb);
  717. kfree(a);
  718. return err;
  719. }
  720. static int
  721. tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
  722. u32 pid, int event)
  723. {
  724. int i, ret;
  725. struct nlattr *tb[TCA_ACT_MAX_PRIO+1];
  726. struct tc_action *head = NULL, *act, *act_prev = NULL;
  727. ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL);
  728. if (ret < 0)
  729. return ret;
  730. if (event == RTM_DELACTION && n->nlmsg_flags&NLM_F_ROOT) {
  731. if (tb[1] != NULL)
  732. return tca_action_flush(net, tb[1], n, pid);
  733. else
  734. return -EINVAL;
  735. }
  736. for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) {
  737. act = tcf_action_get_1(tb[i], n, pid);
  738. if (IS_ERR(act)) {
  739. ret = PTR_ERR(act);
  740. goto err;
  741. }
  742. act->order = i;
  743. if (head == NULL)
  744. head = act;
  745. else
  746. act_prev->next = act;
  747. act_prev = act;
  748. }
  749. if (event == RTM_GETACTION)
  750. ret = act_get_notify(net, pid, n, head, event);
  751. else { /* delete */
  752. struct sk_buff *skb;
  753. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  754. if (!skb) {
  755. ret = -ENOBUFS;
  756. goto err;
  757. }
  758. if (tca_get_fill(skb, head, pid, n->nlmsg_seq, 0, event,
  759. 0, 1) <= 0) {
  760. kfree_skb(skb);
  761. ret = -EINVAL;
  762. goto err;
  763. }
  764. /* now do the delete */
  765. tcf_action_destroy(head, 0);
  766. ret = rtnetlink_send(skb, net, pid, RTNLGRP_TC,
  767. n->nlmsg_flags&NLM_F_ECHO);
  768. if (ret > 0)
  769. return 0;
  770. return ret;
  771. }
  772. err:
  773. cleanup_a(head);
  774. return ret;
  775. }
  776. static int tcf_add_notify(struct net *net, struct tc_action *a,
  777. u32 pid, u32 seq, int event, u16 flags)
  778. {
  779. struct tcamsg *t;
  780. struct nlmsghdr *nlh;
  781. struct sk_buff *skb;
  782. struct nlattr *nest;
  783. unsigned char *b;
  784. int err = 0;
  785. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  786. if (!skb)
  787. return -ENOBUFS;
  788. b = skb_tail_pointer(skb);
  789. nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags);
  790. t = NLMSG_DATA(nlh);
  791. t->tca_family = AF_UNSPEC;
  792. t->tca__pad1 = 0;
  793. t->tca__pad2 = 0;
  794. nest = nla_nest_start(skb, TCA_ACT_TAB);
  795. if (nest == NULL)
  796. goto nla_put_failure;
  797. if (tcf_action_dump(skb, a, 0, 0) < 0)
  798. goto nla_put_failure;
  799. nla_nest_end(skb, nest);
  800. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  801. NETLINK_CB(skb).dst_group = RTNLGRP_TC;
  802. err = rtnetlink_send(skb, net, pid, RTNLGRP_TC, flags&NLM_F_ECHO);
  803. if (err > 0)
  804. err = 0;
  805. return err;
  806. nla_put_failure:
  807. nlmsg_failure:
  808. kfree_skb(skb);
  809. return -1;
  810. }
  811. static int
  812. tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n,
  813. u32 pid, int ovr)
  814. {
  815. int ret = 0;
  816. struct tc_action *act;
  817. struct tc_action *a;
  818. u32 seq = n->nlmsg_seq;
  819. act = tcf_action_init(nla, NULL, NULL, ovr, 0);
  820. if (act == NULL)
  821. goto done;
  822. if (IS_ERR(act)) {
  823. ret = PTR_ERR(act);
  824. goto done;
  825. }
  826. /* dump then free all the actions after update; inserted policy
  827. * stays intact
  828. * */
  829. ret = tcf_add_notify(net, act, pid, seq, RTM_NEWACTION, n->nlmsg_flags);
  830. for (a = act; a; a = act) {
  831. act = a->next;
  832. kfree(a);
  833. }
  834. done:
  835. return ret;
  836. }
  837. static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, void *arg)
  838. {
  839. struct net *net = sock_net(skb->sk);
  840. struct nlattr *tca[TCA_ACT_MAX + 1];
  841. u32 pid = skb ? NETLINK_CB(skb).pid : 0;
  842. int ret = 0, ovr = 0;
  843. ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL);
  844. if (ret < 0)
  845. return ret;
  846. if (tca[TCA_ACT_TAB] == NULL) {
  847. pr_notice("tc_ctl_action: received NO action attribs\n");
  848. return -EINVAL;
  849. }
  850. /* n->nlmsg_flags&NLM_F_CREATE
  851. * */
  852. switch (n->nlmsg_type) {
  853. case RTM_NEWACTION:
  854. /* we are going to assume all other flags
  855. * imply create only if it doesnt exist
  856. * Note that CREATE | EXCL implies that
  857. * but since we want avoid ambiguity (eg when flags
  858. * is zero) then just set this
  859. */
  860. if (n->nlmsg_flags&NLM_F_REPLACE)
  861. ovr = 1;
  862. replay:
  863. ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, pid, ovr);
  864. if (ret == -EAGAIN)
  865. goto replay;
  866. break;
  867. case RTM_DELACTION:
  868. ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
  869. pid, RTM_DELACTION);
  870. break;
  871. case RTM_GETACTION:
  872. ret = tca_action_gd(net, tca[TCA_ACT_TAB], n,
  873. pid, RTM_GETACTION);
  874. break;
  875. default:
  876. BUG();
  877. }
  878. return ret;
  879. }
  880. static struct nlattr *
  881. find_dump_kind(const struct nlmsghdr *n)
  882. {
  883. struct nlattr *tb1, *tb2[TCA_ACT_MAX+1];
  884. struct nlattr *tb[TCA_ACT_MAX_PRIO + 1];
  885. struct nlattr *nla[TCAA_MAX + 1];
  886. struct nlattr *kind;
  887. if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0)
  888. return NULL;
  889. tb1 = nla[TCA_ACT_TAB];
  890. if (tb1 == NULL)
  891. return NULL;
  892. if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1),
  893. NLMSG_ALIGN(nla_len(tb1)), NULL) < 0)
  894. return NULL;
  895. if (tb[1] == NULL)
  896. return NULL;
  897. if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]),
  898. nla_len(tb[1]), NULL) < 0)
  899. return NULL;
  900. kind = tb2[TCA_ACT_KIND];
  901. return kind;
  902. }
  903. static int
  904. tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
  905. {
  906. struct nlmsghdr *nlh;
  907. unsigned char *b = skb_tail_pointer(skb);
  908. struct nlattr *nest;
  909. struct tc_action_ops *a_o;
  910. struct tc_action a;
  911. int ret = 0;
  912. struct tcamsg *t = (struct tcamsg *) NLMSG_DATA(cb->nlh);
  913. struct nlattr *kind = find_dump_kind(cb->nlh);
  914. if (kind == NULL) {
  915. pr_info("tc_dump_action: action bad kind\n");
  916. return 0;
  917. }
  918. a_o = tc_lookup_action(kind);
  919. if (a_o == NULL) {
  920. return 0;
  921. }
  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);