cls_route.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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/slab.h>
  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 <net/dst.h>
  19. #include <net/route.h>
  20. #include <net/netlink.h>
  21. #include <net/act_api.h>
  22. #include <net/pkt_cls.h>
  23. /*
  24. * 1. For now we assume that route tags < 256.
  25. * It allows to use direct table lookups, instead of hash tables.
  26. * 2. For now we assume that "from TAG" and "fromdev DEV" statements
  27. * are mutually exclusive.
  28. * 3. "to TAG from ANY" has higher priority, than "to ANY from XXX"
  29. */
  30. struct route4_fastmap {
  31. struct route4_filter *filter;
  32. u32 id;
  33. int iif;
  34. };
  35. struct route4_head {
  36. struct route4_fastmap fastmap[16];
  37. struct route4_bucket *table[256 + 1];
  38. };
  39. struct route4_bucket {
  40. /* 16 FROM buckets + 16 IIF buckets + 1 wildcard bucket */
  41. struct route4_filter *ht[16 + 16 + 1];
  42. };
  43. struct route4_filter {
  44. struct route4_filter *next;
  45. u32 id;
  46. int iif;
  47. struct tcf_result res;
  48. struct tcf_exts exts;
  49. u32 handle;
  50. struct route4_bucket *bkt;
  51. };
  52. #define ROUTE4_FAILURE ((struct route4_filter *)(-1L))
  53. static const struct tcf_ext_map route_ext_map = {
  54. .police = TCA_ROUTE4_POLICE,
  55. .action = TCA_ROUTE4_ACT
  56. };
  57. static inline int route4_fastmap_hash(u32 id, int iif)
  58. {
  59. return id & 0xF;
  60. }
  61. static void
  62. route4_reset_fastmap(struct Qdisc *q, struct route4_head *head, u32 id)
  63. {
  64. spinlock_t *root_lock = qdisc_root_sleeping_lock(q);
  65. spin_lock_bh(root_lock);
  66. memset(head->fastmap, 0, sizeof(head->fastmap));
  67. spin_unlock_bh(root_lock);
  68. }
  69. static void
  70. route4_set_fastmap(struct route4_head *head, u32 id, int iif,
  71. struct route4_filter *f)
  72. {
  73. int h = route4_fastmap_hash(id, iif);
  74. head->fastmap[h].id = id;
  75. head->fastmap[h].iif = iif;
  76. head->fastmap[h].filter = f;
  77. }
  78. static inline int route4_hash_to(u32 id)
  79. {
  80. return id & 0xFF;
  81. }
  82. static inline int route4_hash_from(u32 id)
  83. {
  84. return (id >> 16) & 0xF;
  85. }
  86. static inline int route4_hash_iif(int iif)
  87. {
  88. return 16 + ((iif >> 16) & 0xF);
  89. }
  90. static inline int route4_hash_wild(void)
  91. {
  92. return 32;
  93. }
  94. #define ROUTE4_APPLY_RESULT() \
  95. { \
  96. *res = f->res; \
  97. if (tcf_exts_is_available(&f->exts)) { \
  98. int r = tcf_exts_exec(skb, &f->exts, res); \
  99. if (r < 0) { \
  100. dont_cache = 1; \
  101. continue; \
  102. } \
  103. return r; \
  104. } else if (!dont_cache) \
  105. route4_set_fastmap(head, id, iif, f); \
  106. return 0; \
  107. }
  108. static int route4_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  109. struct tcf_result *res)
  110. {
  111. struct route4_head *head = (struct route4_head *)tp->root;
  112. struct dst_entry *dst;
  113. struct route4_bucket *b;
  114. struct route4_filter *f;
  115. u32 id, h;
  116. int iif, dont_cache = 0;
  117. dst = skb_dst(skb);
  118. if (!dst)
  119. goto failure;
  120. id = dst->tclassid;
  121. if (head == NULL)
  122. goto old_method;
  123. iif = ((struct rtable *)dst)->rt_iif;
  124. h = route4_fastmap_hash(id, iif);
  125. if (id == head->fastmap[h].id &&
  126. iif == head->fastmap[h].iif &&
  127. (f = head->fastmap[h].filter) != NULL) {
  128. if (f == ROUTE4_FAILURE)
  129. goto failure;
  130. *res = f->res;
  131. return 0;
  132. }
  133. h = route4_hash_to(id);
  134. restart:
  135. b = head->table[h];
  136. if (b) {
  137. for (f = b->ht[route4_hash_from(id)]; f; f = f->next)
  138. if (f->id == id)
  139. ROUTE4_APPLY_RESULT();
  140. for (f = b->ht[route4_hash_iif(iif)]; f; f = f->next)
  141. if (f->iif == iif)
  142. ROUTE4_APPLY_RESULT();
  143. for (f = b->ht[route4_hash_wild()]; f; f = f->next)
  144. ROUTE4_APPLY_RESULT();
  145. }
  146. if (h < 256) {
  147. h = 256;
  148. id &= ~0xFFFF;
  149. goto restart;
  150. }
  151. if (!dont_cache)
  152. route4_set_fastmap(head, id, iif, ROUTE4_FAILURE);
  153. failure:
  154. return -1;
  155. old_method:
  156. if (id && (TC_H_MAJ(id) == 0 ||
  157. !(TC_H_MAJ(id^tp->q->handle)))) {
  158. res->classid = id;
  159. res->class = 0;
  160. return 0;
  161. }
  162. return -1;
  163. }
  164. static inline u32 to_hash(u32 id)
  165. {
  166. u32 h = id & 0xFF;
  167. if (id & 0x8000)
  168. h += 256;
  169. return h;
  170. }
  171. static inline u32 from_hash(u32 id)
  172. {
  173. id &= 0xFFFF;
  174. if (id == 0xFFFF)
  175. return 32;
  176. if (!(id & 0x8000)) {
  177. if (id > 255)
  178. return 256;
  179. return id & 0xF;
  180. }
  181. return 16 + (id & 0xF);
  182. }
  183. static unsigned long route4_get(struct tcf_proto *tp, u32 handle)
  184. {
  185. struct route4_head *head = (struct route4_head *)tp->root;
  186. struct route4_bucket *b;
  187. struct route4_filter *f;
  188. unsigned int h1, h2;
  189. if (!head)
  190. return 0;
  191. h1 = to_hash(handle);
  192. if (h1 > 256)
  193. return 0;
  194. h2 = from_hash(handle >> 16);
  195. if (h2 > 32)
  196. return 0;
  197. b = head->table[h1];
  198. if (b) {
  199. for (f = b->ht[h2]; f; f = f->next)
  200. if (f->handle == handle)
  201. return (unsigned long)f;
  202. }
  203. return 0;
  204. }
  205. static void route4_put(struct tcf_proto *tp, unsigned long f)
  206. {
  207. }
  208. static int route4_init(struct tcf_proto *tp)
  209. {
  210. return 0;
  211. }
  212. static void
  213. route4_delete_filter(struct tcf_proto *tp, struct route4_filter *f)
  214. {
  215. tcf_unbind_filter(tp, &f->res);
  216. tcf_exts_destroy(tp, &f->exts);
  217. kfree(f);
  218. }
  219. static void route4_destroy(struct tcf_proto *tp)
  220. {
  221. struct route4_head *head = tp->root;
  222. int h1, h2;
  223. if (head == NULL)
  224. return;
  225. for (h1 = 0; h1 <= 256; h1++) {
  226. struct route4_bucket *b;
  227. b = head->table[h1];
  228. if (b) {
  229. for (h2 = 0; h2 <= 32; h2++) {
  230. struct route4_filter *f;
  231. while ((f = b->ht[h2]) != NULL) {
  232. b->ht[h2] = f->next;
  233. route4_delete_filter(tp, f);
  234. }
  235. }
  236. kfree(b);
  237. }
  238. }
  239. kfree(head);
  240. }
  241. static int route4_delete(struct tcf_proto *tp, unsigned long arg)
  242. {
  243. struct route4_head *head = (struct route4_head *)tp->root;
  244. struct route4_filter **fp, *f = (struct route4_filter *)arg;
  245. unsigned int h = 0;
  246. struct route4_bucket *b;
  247. int i;
  248. if (!head || !f)
  249. return -EINVAL;
  250. h = f->handle;
  251. b = f->bkt;
  252. for (fp = &b->ht[from_hash(h >> 16)]; *fp; fp = &(*fp)->next) {
  253. if (*fp == f) {
  254. tcf_tree_lock(tp);
  255. *fp = f->next;
  256. tcf_tree_unlock(tp);
  257. route4_reset_fastmap(tp->q, head, f->id);
  258. route4_delete_filter(tp, f);
  259. /* Strip tree */
  260. for (i = 0; i <= 32; i++)
  261. if (b->ht[i])
  262. return 0;
  263. /* OK, session has no flows */
  264. tcf_tree_lock(tp);
  265. head->table[to_hash(h)] = NULL;
  266. tcf_tree_unlock(tp);
  267. kfree(b);
  268. return 0;
  269. }
  270. }
  271. return 0;
  272. }
  273. static const struct nla_policy route4_policy[TCA_ROUTE4_MAX + 1] = {
  274. [TCA_ROUTE4_CLASSID] = { .type = NLA_U32 },
  275. [TCA_ROUTE4_TO] = { .type = NLA_U32 },
  276. [TCA_ROUTE4_FROM] = { .type = NLA_U32 },
  277. [TCA_ROUTE4_IIF] = { .type = NLA_U32 },
  278. };
  279. static int route4_set_parms(struct tcf_proto *tp, unsigned long base,
  280. struct route4_filter *f, u32 handle, struct route4_head *head,
  281. struct nlattr **tb, struct nlattr *est, int new)
  282. {
  283. int err;
  284. u32 id = 0, to = 0, nhandle = 0x8000;
  285. struct route4_filter *fp;
  286. unsigned int h1;
  287. struct route4_bucket *b;
  288. struct tcf_exts e;
  289. err = tcf_exts_validate(tp, tb, est, &e, &route_ext_map);
  290. if (err < 0)
  291. return err;
  292. err = -EINVAL;
  293. if (tb[TCA_ROUTE4_TO]) {
  294. if (new && handle & 0x8000)
  295. goto errout;
  296. to = nla_get_u32(tb[TCA_ROUTE4_TO]);
  297. if (to > 0xFF)
  298. goto errout;
  299. nhandle = to;
  300. }
  301. if (tb[TCA_ROUTE4_FROM]) {
  302. if (tb[TCA_ROUTE4_IIF])
  303. goto errout;
  304. id = nla_get_u32(tb[TCA_ROUTE4_FROM]);
  305. if (id > 0xFF)
  306. goto errout;
  307. nhandle |= id << 16;
  308. } else if (tb[TCA_ROUTE4_IIF]) {
  309. id = nla_get_u32(tb[TCA_ROUTE4_IIF]);
  310. if (id > 0x7FFF)
  311. goto errout;
  312. nhandle |= (id | 0x8000) << 16;
  313. } else
  314. nhandle |= 0xFFFF << 16;
  315. if (handle && new) {
  316. nhandle |= handle & 0x7F00;
  317. if (nhandle != handle)
  318. goto errout;
  319. }
  320. h1 = to_hash(nhandle);
  321. b = head->table[h1];
  322. if (!b) {
  323. err = -ENOBUFS;
  324. b = kzalloc(sizeof(struct route4_bucket), GFP_KERNEL);
  325. if (b == NULL)
  326. goto errout;
  327. tcf_tree_lock(tp);
  328. head->table[h1] = b;
  329. tcf_tree_unlock(tp);
  330. } else {
  331. unsigned int h2 = from_hash(nhandle >> 16);
  332. err = -EEXIST;
  333. for (fp = b->ht[h2]; fp; fp = fp->next)
  334. if (fp->handle == f->handle)
  335. goto errout;
  336. }
  337. tcf_tree_lock(tp);
  338. if (tb[TCA_ROUTE4_TO])
  339. f->id = to;
  340. if (tb[TCA_ROUTE4_FROM])
  341. f->id = to | id<<16;
  342. else if (tb[TCA_ROUTE4_IIF])
  343. f->iif = id;
  344. f->handle = nhandle;
  345. f->bkt = b;
  346. tcf_tree_unlock(tp);
  347. if (tb[TCA_ROUTE4_CLASSID]) {
  348. f->res.classid = nla_get_u32(tb[TCA_ROUTE4_CLASSID]);
  349. tcf_bind_filter(tp, &f->res, base);
  350. }
  351. tcf_exts_change(tp, &f->exts, &e);
  352. return 0;
  353. errout:
  354. tcf_exts_destroy(tp, &e);
  355. return err;
  356. }
  357. static int route4_change(struct tcf_proto *tp, unsigned long base,
  358. u32 handle,
  359. struct nlattr **tca,
  360. unsigned long *arg)
  361. {
  362. struct route4_head *head = tp->root;
  363. struct route4_filter *f, *f1, **fp;
  364. struct route4_bucket *b;
  365. struct nlattr *opt = tca[TCA_OPTIONS];
  366. struct nlattr *tb[TCA_ROUTE4_MAX + 1];
  367. unsigned int h, th;
  368. u32 old_handle = 0;
  369. int err;
  370. if (opt == NULL)
  371. return handle ? -EINVAL : 0;
  372. err = nla_parse_nested(tb, TCA_ROUTE4_MAX, opt, route4_policy);
  373. if (err < 0)
  374. return err;
  375. f = (struct route4_filter *)*arg;
  376. if (f) {
  377. if (f->handle != handle && handle)
  378. return -EINVAL;
  379. if (f->bkt)
  380. old_handle = f->handle;
  381. err = route4_set_parms(tp, base, f, handle, head, tb,
  382. tca[TCA_RATE], 0);
  383. if (err < 0)
  384. return err;
  385. goto reinsert;
  386. }
  387. err = -ENOBUFS;
  388. if (head == NULL) {
  389. head = kzalloc(sizeof(struct route4_head), GFP_KERNEL);
  390. if (head == NULL)
  391. goto errout;
  392. tcf_tree_lock(tp);
  393. tp->root = head;
  394. tcf_tree_unlock(tp);
  395. }
  396. f = kzalloc(sizeof(struct route4_filter), GFP_KERNEL);
  397. if (f == NULL)
  398. goto errout;
  399. err = route4_set_parms(tp, base, f, handle, head, tb,
  400. tca[TCA_RATE], 1);
  401. if (err < 0)
  402. goto errout;
  403. reinsert:
  404. h = from_hash(f->handle >> 16);
  405. for (fp = &f->bkt->ht[h]; (f1 = *fp) != NULL; fp = &f1->next)
  406. if (f->handle < f1->handle)
  407. break;
  408. f->next = f1;
  409. tcf_tree_lock(tp);
  410. *fp = f;
  411. if (old_handle && f->handle != old_handle) {
  412. th = to_hash(old_handle);
  413. h = from_hash(old_handle >> 16);
  414. b = head->table[th];
  415. if (b) {
  416. for (fp = &b->ht[h]; *fp; fp = &(*fp)->next) {
  417. if (*fp == f) {
  418. *fp = f->next;
  419. break;
  420. }
  421. }
  422. }
  423. }
  424. tcf_tree_unlock(tp);
  425. route4_reset_fastmap(tp->q, head, f->id);
  426. *arg = (unsigned long)f;
  427. return 0;
  428. errout:
  429. kfree(f);
  430. return err;
  431. }
  432. static void route4_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  433. {
  434. struct route4_head *head = tp->root;
  435. unsigned int h, h1;
  436. if (head == NULL)
  437. arg->stop = 1;
  438. if (arg->stop)
  439. return;
  440. for (h = 0; h <= 256; h++) {
  441. struct route4_bucket *b = head->table[h];
  442. if (b) {
  443. for (h1 = 0; h1 <= 32; h1++) {
  444. struct route4_filter *f;
  445. for (f = b->ht[h1]; f; f = f->next) {
  446. if (arg->count < arg->skip) {
  447. arg->count++;
  448. continue;
  449. }
  450. if (arg->fn(tp, (unsigned long)f, arg) < 0) {
  451. arg->stop = 1;
  452. return;
  453. }
  454. arg->count++;
  455. }
  456. }
  457. }
  458. }
  459. }
  460. static int route4_dump(struct tcf_proto *tp, unsigned long fh,
  461. struct sk_buff *skb, struct tcmsg *t)
  462. {
  463. struct route4_filter *f = (struct route4_filter *)fh;
  464. unsigned char *b = skb_tail_pointer(skb);
  465. struct nlattr *nest;
  466. u32 id;
  467. if (f == NULL)
  468. return skb->len;
  469. t->tcm_handle = f->handle;
  470. nest = nla_nest_start(skb, TCA_OPTIONS);
  471. if (nest == NULL)
  472. goto nla_put_failure;
  473. if (!(f->handle & 0x8000)) {
  474. id = f->id & 0xFF;
  475. NLA_PUT_U32(skb, TCA_ROUTE4_TO, id);
  476. }
  477. if (f->handle & 0x80000000) {
  478. if ((f->handle >> 16) != 0xFFFF)
  479. NLA_PUT_U32(skb, TCA_ROUTE4_IIF, f->iif);
  480. } else {
  481. id = f->id >> 16;
  482. NLA_PUT_U32(skb, TCA_ROUTE4_FROM, id);
  483. }
  484. if (f->res.classid)
  485. NLA_PUT_U32(skb, TCA_ROUTE4_CLASSID, f->res.classid);
  486. if (tcf_exts_dump(skb, &f->exts, &route_ext_map) < 0)
  487. goto nla_put_failure;
  488. nla_nest_end(skb, nest);
  489. if (tcf_exts_dump_stats(skb, &f->exts, &route_ext_map) < 0)
  490. goto nla_put_failure;
  491. return skb->len;
  492. nla_put_failure:
  493. nlmsg_trim(skb, b);
  494. return -1;
  495. }
  496. static struct tcf_proto_ops cls_route4_ops __read_mostly = {
  497. .kind = "route",
  498. .classify = route4_classify,
  499. .init = route4_init,
  500. .destroy = route4_destroy,
  501. .get = route4_get,
  502. .put = route4_put,
  503. .change = route4_change,
  504. .delete = route4_delete,
  505. .walk = route4_walk,
  506. .dump = route4_dump,
  507. .owner = THIS_MODULE,
  508. };
  509. static int __init init_route4(void)
  510. {
  511. return register_tcf_proto_ops(&cls_route4_ops);
  512. }
  513. static void __exit exit_route4(void)
  514. {
  515. unregister_tcf_proto_ops(&cls_route4_ops);
  516. }
  517. module_init(init_route4)
  518. module_exit(exit_route4)
  519. MODULE_LICENSE("GPL");