act_api.c 23 KB

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