act_api.c 23 KB

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