act_api.c 23 KB

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