act_api.c 23 KB

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