cls_route.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:13k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*
  2.  * net/sched/cls_route.c ROUTE4 classifier.
  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.  * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  10.  */
  11. #include <linux/module.h>
  12. #include <linux/config.h>
  13. #include <asm/uaccess.h>
  14. #include <asm/system.h>
  15. #include <asm/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/if_ether.h>
  27. #include <linux/inet.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/etherdevice.h>
  30. #include <linux/notifier.h>
  31. #include <net/ip.h>
  32. #include <net/route.h>
  33. #include <linux/skbuff.h>
  34. #include <net/sock.h>
  35. #include <net/pkt_sched.h>
  36. /*
  37.    1. For now we assume that route tags < 256.
  38.       It allows to use direct table lookups, instead of hash tables.
  39.    2. For now we assume that "from TAG" and "fromdev DEV" statements
  40.       are mutually  exclusive.
  41.    3. "to TAG from ANY" has higher priority, than "to ANY from XXX"
  42.  */
  43. struct route4_fastmap
  44. {
  45. struct route4_filter *filter;
  46. u32 id;
  47. int iif;
  48. };
  49. struct route4_head
  50. {
  51. struct route4_fastmap fastmap[16];
  52. struct route4_bucket *table[256+1];
  53. };
  54. struct route4_bucket
  55. {
  56. struct route4_filter *ht[16+16+1];
  57. };
  58. struct route4_filter
  59. {
  60. struct route4_filter *next;
  61. u32 id;
  62. int iif;
  63. struct tcf_result res;
  64. #ifdef CONFIG_NET_CLS_POLICE
  65. struct tcf_police *police;
  66. #endif
  67. u32 handle;
  68. struct route4_bucket *bkt;
  69. };
  70. #define ROUTE4_FAILURE ((struct route4_filter*)(-1L))
  71. static __inline__ int route4_fastmap_hash(u32 id, int iif)
  72. {
  73. return id&0xF;
  74. }
  75. static void route4_reset_fastmap(struct net_device *dev, struct route4_head *head, u32 id)
  76. {
  77. spin_lock_bh(&dev->queue_lock);
  78. memset(head->fastmap, 0, sizeof(head->fastmap));
  79. spin_unlock_bh(&dev->queue_lock);
  80. }
  81. static void __inline__
  82. route4_set_fastmap(struct route4_head *head, u32 id, int iif,
  83.    struct route4_filter *f)
  84. {
  85. int h = route4_fastmap_hash(id, iif);
  86. head->fastmap[h].id = id;
  87. head->fastmap[h].iif = iif;
  88. head->fastmap[h].filter = f;
  89. }
  90. static __inline__ int route4_hash_to(u32 id)
  91. {
  92. return id&0xFF;
  93. }
  94. static __inline__ int route4_hash_from(u32 id)
  95. {
  96. return (id>>16)&0xF;
  97. }
  98. static __inline__ int route4_hash_iif(int iif)
  99. {
  100. return 16 + ((iif>>16)&0xF);
  101. }
  102. static __inline__ int route4_hash_wild(void)
  103. {
  104. return 32;
  105. }
  106. #ifdef CONFIG_NET_CLS_POLICE
  107. #define IF_ROUTE_POLICE 
  108. if (f->police) { 
  109. int pol_res = tcf_police(skb, f->police); 
  110. if (pol_res >= 0) return pol_res; 
  111. dont_cache = 1; 
  112. continue; 
  113. if (!dont_cache)
  114. #else
  115. #define IF_ROUTE_POLICE
  116. #endif
  117. static int route4_classify(struct sk_buff *skb, struct tcf_proto *tp,
  118.    struct tcf_result *res)
  119. {
  120. struct route4_head *head = (struct route4_head*)tp->root;
  121. struct dst_entry *dst;
  122. struct route4_bucket *b;
  123. struct route4_filter *f;
  124. #ifdef CONFIG_NET_CLS_POLICE
  125. int dont_cache = 0;
  126. #endif
  127. u32 id, h;
  128. int iif;
  129. if ((dst = skb->dst) == NULL)
  130. goto failure;
  131. id = dst->tclassid;
  132. if (head == NULL)
  133. goto old_method;
  134. iif = ((struct rtable*)dst)->key.iif;
  135. h = route4_fastmap_hash(id, iif);
  136. if (id == head->fastmap[h].id &&
  137.     iif == head->fastmap[h].iif &&
  138.     (f = head->fastmap[h].filter) != NULL) {
  139. if (f == ROUTE4_FAILURE)
  140. goto failure;
  141. *res = f->res;
  142. return 0;
  143. }
  144. h = route4_hash_to(id);
  145. restart:
  146. if ((b = head->table[h]) != NULL) {
  147. f = b->ht[route4_hash_from(id)];
  148. for ( ; f; f = f->next) {
  149. if (f->id == id) {
  150. *res = f->res;
  151. IF_ROUTE_POLICE route4_set_fastmap(head, id, iif, f);
  152. return 0;
  153. }
  154. }
  155. for (f = b->ht[route4_hash_iif(iif)]; f; f = f->next) {
  156. if (f->iif == iif) {
  157. *res = f->res;
  158. IF_ROUTE_POLICE route4_set_fastmap(head, id, iif, f);
  159. return 0;
  160. }
  161. }
  162. for (f = b->ht[route4_hash_wild()]; f; f = f->next) {
  163. *res = f->res;
  164. IF_ROUTE_POLICE route4_set_fastmap(head, id, iif, f);
  165. return 0;
  166. }
  167. }
  168. if (h < 256) {
  169. h = 256;
  170. id &= ~0xFFFF;
  171. goto restart;
  172. }
  173. #ifdef CONFIG_NET_CLS_POLICE
  174. if (!dont_cache)
  175. #endif
  176. route4_set_fastmap(head, id, iif, ROUTE4_FAILURE);
  177. failure:
  178. return -1;
  179. old_method:
  180. if (id && (TC_H_MAJ(id) == 0 ||
  181.    !(TC_H_MAJ(id^tp->q->handle)))) {
  182. res->classid = id;
  183. res->class = 0;
  184. return 0;
  185. }
  186. return -1;
  187. }
  188. static u32 to_hash(u32 id)
  189. {
  190. u32 h = id&0xFF;
  191. if (id&0x8000)
  192. h += 256;
  193. return h;
  194. }
  195. static u32 from_hash(u32 id)
  196. {
  197. id &= 0xFFFF;
  198. if (id == 0xFFFF)
  199. return 32;
  200. if (!(id & 0x8000)) {
  201. if (id > 255)
  202. return 256;
  203. return id&0xF;
  204. }
  205. return 16 + (id&0xF);
  206. }
  207. static unsigned long route4_get(struct tcf_proto *tp, u32 handle)
  208. {
  209. struct route4_head *head = (struct route4_head*)tp->root;
  210. struct route4_bucket *b;
  211. struct route4_filter *f;
  212. unsigned h1, h2;
  213. if (!head)
  214. return 0;
  215. h1 = to_hash(handle);
  216. if (h1 > 256)
  217. return 0;
  218. h2 = from_hash(handle>>16);
  219. if (h2 > 32)
  220. return 0;
  221. if ((b = head->table[h1]) != NULL) {
  222. for (f = b->ht[h2]; f; f = f->next)
  223. if (f->handle == handle)
  224. return (unsigned long)f;
  225. }
  226. return 0;
  227. }
  228. static void route4_put(struct tcf_proto *tp, unsigned long f)
  229. {
  230. }
  231. static int route4_init(struct tcf_proto *tp)
  232. {
  233. MOD_INC_USE_COUNT;
  234. return 0;
  235. }
  236. static void route4_destroy(struct tcf_proto *tp)
  237. {
  238. struct route4_head *head = xchg(&tp->root, NULL);
  239. int h1, h2;
  240. if (head == NULL) {
  241. MOD_DEC_USE_COUNT;
  242. return;
  243. }
  244. for (h1=0; h1<=256; h1++) {
  245. struct route4_bucket *b;
  246. if ((b = head->table[h1]) != NULL) {
  247. for (h2=0; h2<=32; h2++) {
  248. struct route4_filter *f;
  249. while ((f = b->ht[h2]) != NULL) {
  250. unsigned long cl;
  251. b->ht[h2] = f->next;
  252. if ((cl = __cls_set_class(&f->res.class, 0)) != 0)
  253. tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
  254. #ifdef CONFIG_NET_CLS_POLICE
  255. tcf_police_release(f->police);
  256. #endif
  257. kfree(f);
  258. }
  259. }
  260. kfree(b);
  261. }
  262. }
  263. kfree(head);
  264. MOD_DEC_USE_COUNT;
  265. }
  266. static int route4_delete(struct tcf_proto *tp, unsigned long arg)
  267. {
  268. struct route4_head *head = (struct route4_head*)tp->root;
  269. struct route4_filter **fp, *f = (struct route4_filter*)arg;
  270. unsigned h = 0;
  271. struct route4_bucket *b;
  272. int i;
  273. if (!head || !f)
  274. return -EINVAL;
  275. h = f->handle;
  276. b = f->bkt;
  277. for (fp = &b->ht[from_hash(h>>16)]; *fp; fp = &(*fp)->next) {
  278. if (*fp == f) {
  279. unsigned long cl;
  280. tcf_tree_lock(tp);
  281. *fp = f->next;
  282. tcf_tree_unlock(tp);
  283. route4_reset_fastmap(tp->q->dev, head, f->id);
  284. if ((cl = cls_set_class(tp, &f->res.class, 0)) != 0)
  285. tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
  286. #ifdef CONFIG_NET_CLS_POLICE
  287. tcf_police_release(f->police);
  288. #endif
  289. kfree(f);
  290. /* Strip tree */
  291. for (i=0; i<=32; i++)
  292. if (b->ht[i])
  293. return 0;
  294. /* OK, session has no flows */
  295. tcf_tree_lock(tp);
  296. head->table[to_hash(h)] = NULL;
  297. tcf_tree_unlock(tp);
  298. kfree(b);
  299. return 0;
  300. }
  301. }
  302. return 0;
  303. }
  304. static int route4_change(struct tcf_proto *tp, unsigned long base,
  305.        u32 handle,
  306.        struct rtattr **tca,
  307.        unsigned long *arg)
  308. {
  309. struct route4_head *head = tp->root;
  310. struct route4_filter *f, *f1, **ins_f;
  311. struct route4_bucket *b;
  312. struct rtattr *opt = tca[TCA_OPTIONS-1];
  313. struct rtattr *tb[TCA_ROUTE4_MAX];
  314. unsigned h1, h2;
  315. int err;
  316. if (opt == NULL)
  317. return handle ? -EINVAL : 0;
  318. if (rtattr_parse(tb, TCA_ROUTE4_MAX, RTA_DATA(opt), RTA_PAYLOAD(opt)) < 0)
  319. return -EINVAL;
  320. if ((f = (struct route4_filter*)*arg) != NULL) {
  321. /* Node exists: adjust only classid */
  322. if (f->handle != handle && handle)
  323. return -EINVAL;
  324. if (tb[TCA_ROUTE4_CLASSID-1]) {
  325. unsigned long cl;
  326. f->res.classid = *(u32*)RTA_DATA(tb[TCA_ROUTE4_CLASSID-1]);
  327. cl = cls_set_class(tp, &f->res.class, tp->q->ops->cl_ops->bind_tcf(tp->q, base, f->res.classid));
  328. if (cl)
  329. tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
  330. }
  331. #ifdef CONFIG_NET_CLS_POLICE
  332. if (tb[TCA_ROUTE4_POLICE-1]) {
  333. struct tcf_police *police = tcf_police_locate(tb[TCA_ROUTE4_POLICE-1], tca[TCA_RATE-1]);
  334. tcf_tree_lock(tp);
  335. police = xchg(&f->police, police);
  336. tcf_tree_unlock(tp);
  337. tcf_police_release(police);
  338. }
  339. #endif
  340. return 0;
  341. }
  342. /* Now more serious part... */
  343. if (head == NULL) {
  344. head = kmalloc(sizeof(struct route4_head), GFP_KERNEL);
  345. if (head == NULL)
  346. return -ENOBUFS;
  347. memset(head, 0, sizeof(struct route4_head));
  348. tcf_tree_lock(tp);
  349. tp->root = head;
  350. tcf_tree_unlock(tp);
  351. }
  352. f = kmalloc(sizeof(struct route4_filter), GFP_KERNEL);
  353. if (f == NULL)
  354. return -ENOBUFS;
  355. memset(f, 0, sizeof(*f));
  356. err = -EINVAL;
  357. f->handle = 0x8000;
  358. if (tb[TCA_ROUTE4_TO-1]) {
  359. if (handle&0x8000)
  360. goto errout;
  361. if (RTA_PAYLOAD(tb[TCA_ROUTE4_TO-1]) < 4)
  362. goto errout;
  363. f->id = *(u32*)RTA_DATA(tb[TCA_ROUTE4_TO-1]);
  364. if (f->id > 0xFF)
  365. goto errout;
  366. f->handle = f->id;
  367. }
  368. if (tb[TCA_ROUTE4_FROM-1]) {
  369. u32 sid;
  370. if (tb[TCA_ROUTE4_IIF-1])
  371. goto errout;
  372. if (RTA_PAYLOAD(tb[TCA_ROUTE4_FROM-1]) < 4)
  373. goto errout;
  374. sid = (*(u32*)RTA_DATA(tb[TCA_ROUTE4_FROM-1]));
  375. if (sid > 0xFF)
  376. goto errout;
  377. f->handle |= sid<<16;
  378. f->id |= sid<<16;
  379. } else if (tb[TCA_ROUTE4_IIF-1]) {
  380. if (RTA_PAYLOAD(tb[TCA_ROUTE4_IIF-1]) < 4)
  381. goto errout;
  382. f->iif = *(u32*)RTA_DATA(tb[TCA_ROUTE4_IIF-1]);
  383. if (f->iif > 0x7FFF)
  384. goto errout;
  385. f->handle |= (f->iif|0x8000)<<16;
  386. } else
  387. f->handle |= 0xFFFF<<16;
  388. if (handle) {
  389. f->handle |= handle&0x7F00;
  390. if (f->handle != handle)
  391. goto errout;
  392. }
  393. if (tb[TCA_ROUTE4_CLASSID-1]) {
  394. if (RTA_PAYLOAD(tb[TCA_ROUTE4_CLASSID-1]) < 4)
  395. goto errout;
  396. f->res.classid = *(u32*)RTA_DATA(tb[TCA_ROUTE4_CLASSID-1]);
  397. }
  398. h1 = to_hash(f->handle);
  399. if ((b = head->table[h1]) == NULL) {
  400. err = -ENOBUFS;
  401. b = kmalloc(sizeof(struct route4_bucket), GFP_KERNEL);
  402. if (b == NULL)
  403. goto errout;
  404. memset(b, 0, sizeof(*b));
  405. tcf_tree_lock(tp);
  406. head->table[h1] = b;
  407. tcf_tree_unlock(tp);
  408. }
  409. f->bkt = b;
  410. err = -EEXIST;
  411. h2 = from_hash(f->handle>>16);
  412. for (ins_f = &b->ht[h2]; (f1=*ins_f) != NULL; ins_f = &f1->next) {
  413. if (f->handle < f1->handle)
  414. break;
  415. if (f1->handle == f->handle)
  416. goto errout;
  417. }
  418. cls_set_class(tp, &f->res.class, tp->q->ops->cl_ops->bind_tcf(tp->q, base, f->res.classid));
  419. #ifdef CONFIG_NET_CLS_POLICE
  420. if (tb[TCA_ROUTE4_POLICE-1])
  421. f->police = tcf_police_locate(tb[TCA_ROUTE4_POLICE-1], tca[TCA_RATE-1]);
  422. #endif
  423. f->next = f1;
  424. tcf_tree_lock(tp);
  425. *ins_f = f;
  426. tcf_tree_unlock(tp);
  427. route4_reset_fastmap(tp->q->dev, head, f->id);
  428. *arg = (unsigned long)f;
  429. return 0;
  430. errout:
  431. if (f)
  432. kfree(f);
  433. return err;
  434. }
  435. static void route4_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  436. {
  437. struct route4_head *head = tp->root;
  438. unsigned h, h1;
  439. if (head == NULL)
  440. arg->stop = 1;
  441. if (arg->stop)
  442. return;
  443. for (h = 0; h <= 256; h++) {
  444. struct route4_bucket *b = head->table[h];
  445. if (b) {
  446. for (h1 = 0; h1 <= 32; h1++) {
  447. struct route4_filter *f;
  448. for (f = b->ht[h1]; f; f = f->next) {
  449. if (arg->count < arg->skip) {
  450. arg->count++;
  451. continue;
  452. }
  453. if (arg->fn(tp, (unsigned long)f, arg) < 0) {
  454. arg->stop = 1;
  455. break;
  456. }
  457. arg->count++;
  458. }
  459. }
  460. }
  461. }
  462. }
  463. static int route4_dump(struct tcf_proto *tp, unsigned long fh,
  464.        struct sk_buff *skb, struct tcmsg *t)
  465. {
  466. struct route4_filter *f = (struct route4_filter*)fh;
  467. unsigned char  *b = skb->tail;
  468. struct rtattr *rta;
  469. u32 id;
  470. if (f == NULL)
  471. return skb->len;
  472. t->tcm_handle = f->handle;
  473. rta = (struct rtattr*)b;
  474. RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
  475. if (!(f->handle&0x8000)) {
  476. id = f->id&0xFF;
  477. RTA_PUT(skb, TCA_ROUTE4_TO, sizeof(id), &id);
  478. }
  479. if (f->handle&0x80000000) {
  480. if ((f->handle>>16) != 0xFFFF)
  481. RTA_PUT(skb, TCA_ROUTE4_IIF, sizeof(f->iif), &f->iif);
  482. } else {
  483. id = f->id>>16;
  484. RTA_PUT(skb, TCA_ROUTE4_FROM, sizeof(id), &id);
  485. }
  486. if (f->res.classid)
  487. RTA_PUT(skb, TCA_ROUTE4_CLASSID, 4, &f->res.classid);
  488. #ifdef CONFIG_NET_CLS_POLICE
  489. if (f->police) {
  490. struct rtattr * p_rta = (struct rtattr*)skb->tail;
  491. RTA_PUT(skb, TCA_ROUTE4_POLICE, 0, NULL);
  492. if (tcf_police_dump(skb, f->police) < 0)
  493. goto rtattr_failure;
  494. p_rta->rta_len = skb->tail - (u8*)p_rta;
  495. }
  496. #endif
  497. rta->rta_len = skb->tail - b;
  498. #ifdef CONFIG_NET_CLS_POLICE
  499. if (f->police) {
  500. if (qdisc_copy_stats(skb, &f->police->stats))
  501. goto rtattr_failure;
  502. }
  503. #endif
  504. return skb->len;
  505. rtattr_failure:
  506. skb_trim(skb, b - skb->data);
  507. return -1;
  508. }
  509. struct tcf_proto_ops cls_route4_ops = {
  510. NULL,
  511. "route",
  512. route4_classify,
  513. route4_init,
  514. route4_destroy,
  515. route4_get,
  516. route4_put,
  517. route4_change,
  518. route4_delete,
  519. route4_walk,
  520. route4_dump
  521. };
  522. #ifdef MODULE
  523. int init_module(void)
  524. {
  525. return register_tcf_proto_ops(&cls_route4_ops);
  526. }
  527. void cleanup_module(void)
  528. {
  529. unregister_tcf_proto_ops(&cls_route4_ops);
  530. }
  531. #endif
  532. MODULE_LICENSE("GPL");