act_api.c 23 KB

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