cls_route.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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 = inet_iif(skb);
  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 sk_buff *in_skb,
  358. struct tcf_proto *tp, unsigned long base,
  359. u32 handle,
  360. struct nlattr **tca,
  361. unsigned long *arg)
  362. {
  363. struct route4_head *head = tp->root;
  364. struct route4_filter *f, *f1, **fp;
  365. struct route4_bucket *b;
  366. struct nlattr *opt = tca[TCA_OPTIONS];
  367. struct nlattr *tb[TCA_ROUTE4_MAX + 1];
  368. unsigned int h, th;
  369. u32 old_handle = 0;
  370. int err;
  371. if (opt == NULL)
  372. return handle ? -EINVAL : 0;
  373. err = nla_parse_nested(tb, TCA_ROUTE4_MAX, opt, route4_policy);
  374. if (err < 0)
  375. return err;
  376. f = (struct route4_filter *)*arg;
  377. if (f) {
  378. if (f->handle != handle && handle)
  379. return -EINVAL;
  380. if (f->bkt)
  381. old_handle = f->handle;
  382. err = route4_set_parms(tp, base, f, handle, head, tb,
  383. tca[TCA_RATE], 0);
  384. if (err < 0)
  385. return err;
  386. goto reinsert;
  387. }
  388. err = -ENOBUFS;
  389. if (head == NULL) {
  390. head = kzalloc(sizeof(struct route4_head), GFP_KERNEL);
  391. if (head == NULL)
  392. goto errout;
  393. tcf_tree_lock(tp);
  394. tp->root = head;
  395. tcf_tree_unlock(tp);
  396. }
  397. f = kzalloc(sizeof(struct route4_filter), GFP_KERNEL);
  398. if (f == NULL)
  399. goto errout;
  400. err = route4_set_parms(tp, base, f, handle, head, tb,
  401. tca[TCA_RATE], 1);
  402. if (err < 0)
  403. goto errout;
  404. reinsert:
  405. h = from_hash(f->handle >> 16);
  406. for (fp = &f->bkt->ht[h]; (f1 = *fp) != NULL; fp = &f1->next)
  407. if (f->handle < f1->handle)
  408. break;
  409. f->next = f1;
  410. tcf_tree_lock(tp);
  411. *fp = f;
  412. if (old_handle && f->handle != old_handle) {
  413. th = to_hash(old_handle);
  414. h = from_hash(old_handle >> 16);
  415. b = head->table[th];
  416. if (b) {
  417. for (fp = &b->ht[h]; *fp; fp = &(*fp)->next) {
  418. if (*fp == f) {
  419. *fp = f->next;
  420. break;
  421. }
  422. }
  423. }
  424. }
  425. tcf_tree_unlock(tp);
  426. route4_reset_fastmap(tp->q, head, f->id);
  427. *arg = (unsigned long)f;
  428. return 0;
  429. errout:
  430. kfree(f);
  431. return err;
  432. }
  433. static void route4_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  434. {
  435. struct route4_head *head = tp->root;
  436. unsigned int h, h1;
  437. if (head == NULL)
  438. arg->stop = 1;
  439. if (arg->stop)
  440. return;
  441. for (h = 0; h <= 256; h++) {
  442. struct route4_bucket *b = head->table[h];
  443. if (b) {
  444. for (h1 = 0; h1 <= 32; h1++) {
  445. struct route4_filter *f;
  446. for (f = b->ht[h1]; f; f = f->next) {
  447. if (arg->count < arg->skip) {
  448. arg->count++;
  449. continue;
  450. }
  451. if (arg->fn(tp, (unsigned long)f, arg) < 0) {
  452. arg->stop = 1;
  453. return;
  454. }
  455. arg->count++;
  456. }
  457. }
  458. }
  459. }
  460. }
  461. static int route4_dump(struct tcf_proto *tp, unsigned long fh,
  462. struct sk_buff *skb, struct tcmsg *t)
  463. {
  464. struct route4_filter *f = (struct route4_filter *)fh;
  465. unsigned char *b = skb_tail_pointer(skb);
  466. struct nlattr *nest;
  467. u32 id;
  468. if (f == NULL)
  469. return skb->len;
  470. t->tcm_handle = f->handle;
  471. nest = nla_nest_start(skb, TCA_OPTIONS);
  472. if (nest == NULL)
  473. goto nla_put_failure;
  474. if (!(f->handle & 0x8000)) {
  475. id = f->id & 0xFF;
  476. if (nla_put_u32(skb, TCA_ROUTE4_TO, id))
  477. goto nla_put_failure;
  478. }
  479. if (f->handle & 0x80000000) {
  480. if ((f->handle >> 16) != 0xFFFF &&
  481. nla_put_u32(skb, TCA_ROUTE4_IIF, f->iif))
  482. goto nla_put_failure;
  483. } else {
  484. id = f->id >> 16;
  485. if (nla_put_u32(skb, TCA_ROUTE4_FROM, id))
  486. goto nla_put_failure;
  487. }
  488. if (f->res.classid &&
  489. nla_put_u32(skb, TCA_ROUTE4_CLASSID, f->res.classid))
  490. goto nla_put_failure;
  491. if (tcf_exts_dump(skb, &f->exts, &route_ext_map) < 0)
  492. goto nla_put_failure;
  493. nla_nest_end(skb, nest);
  494. if (tcf_exts_dump_stats(skb, &f->exts, &route_ext_map) < 0)
  495. goto nla_put_failure;
  496. return skb->len;
  497. nla_put_failure:
  498. nlmsg_trim(skb, b);
  499. return -1;
  500. }
  501. static struct tcf_proto_ops cls_route4_ops __read_mostly = {
  502. .kind = "route",
  503. .classify = route4_classify,
  504. .init = route4_init,
  505. .destroy = route4_destroy,
  506. .get = route4_get,
  507. .put = route4_put,
  508. .change = route4_change,
  509. .delete = route4_delete,
  510. .walk = route4_walk,
  511. .dump = route4_dump,
  512. .owner = THIS_MODULE,
  513. };
  514. static int __init init_route4(void)
  515. {
  516. return register_tcf_proto_ops(&cls_route4_ops);
  517. }
  518. static void __exit exit_route4(void)
  519. {
  520. unregister_tcf_proto_ops(&cls_route4_ops);
  521. }
  522. module_init(init_route4)
  523. module_exit(exit_route4)
  524. MODULE_LICENSE("GPL");