cls_route.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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 inline int route4_fastmap_hash(u32 id, int iif)
  54. {
  55. return id & 0xF;
  56. }
  57. static void
  58. route4_reset_fastmap(struct Qdisc *q, struct route4_head *head, u32 id)
  59. {
  60. spinlock_t *root_lock = qdisc_root_sleeping_lock(q);
  61. spin_lock_bh(root_lock);
  62. memset(head->fastmap, 0, sizeof(head->fastmap));
  63. spin_unlock_bh(root_lock);
  64. }
  65. static void
  66. route4_set_fastmap(struct route4_head *head, u32 id, int iif,
  67. struct route4_filter *f)
  68. {
  69. int h = route4_fastmap_hash(id, iif);
  70. head->fastmap[h].id = id;
  71. head->fastmap[h].iif = iif;
  72. head->fastmap[h].filter = f;
  73. }
  74. static inline int route4_hash_to(u32 id)
  75. {
  76. return id & 0xFF;
  77. }
  78. static inline int route4_hash_from(u32 id)
  79. {
  80. return (id >> 16) & 0xF;
  81. }
  82. static inline int route4_hash_iif(int iif)
  83. {
  84. return 16 + ((iif >> 16) & 0xF);
  85. }
  86. static inline int route4_hash_wild(void)
  87. {
  88. return 32;
  89. }
  90. #define ROUTE4_APPLY_RESULT() \
  91. { \
  92. *res = f->res; \
  93. if (tcf_exts_is_available(&f->exts)) { \
  94. int r = tcf_exts_exec(skb, &f->exts, res); \
  95. if (r < 0) { \
  96. dont_cache = 1; \
  97. continue; \
  98. } \
  99. return r; \
  100. } else if (!dont_cache) \
  101. route4_set_fastmap(head, id, iif, f); \
  102. return 0; \
  103. }
  104. static int route4_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  105. struct tcf_result *res)
  106. {
  107. struct route4_head *head = tp->root;
  108. struct dst_entry *dst;
  109. struct route4_bucket *b;
  110. struct route4_filter *f;
  111. u32 id, h;
  112. int iif, dont_cache = 0;
  113. dst = skb_dst(skb);
  114. if (!dst)
  115. goto failure;
  116. id = dst->tclassid;
  117. if (head == NULL)
  118. goto old_method;
  119. iif = inet_iif(skb);
  120. h = route4_fastmap_hash(id, iif);
  121. if (id == head->fastmap[h].id &&
  122. iif == head->fastmap[h].iif &&
  123. (f = head->fastmap[h].filter) != NULL) {
  124. if (f == ROUTE4_FAILURE)
  125. goto failure;
  126. *res = f->res;
  127. return 0;
  128. }
  129. h = route4_hash_to(id);
  130. restart:
  131. b = head->table[h];
  132. if (b) {
  133. for (f = b->ht[route4_hash_from(id)]; f; f = f->next)
  134. if (f->id == id)
  135. ROUTE4_APPLY_RESULT();
  136. for (f = b->ht[route4_hash_iif(iif)]; f; f = f->next)
  137. if (f->iif == iif)
  138. ROUTE4_APPLY_RESULT();
  139. for (f = b->ht[route4_hash_wild()]; f; f = f->next)
  140. ROUTE4_APPLY_RESULT();
  141. }
  142. if (h < 256) {
  143. h = 256;
  144. id &= ~0xFFFF;
  145. goto restart;
  146. }
  147. if (!dont_cache)
  148. route4_set_fastmap(head, id, iif, ROUTE4_FAILURE);
  149. failure:
  150. return -1;
  151. old_method:
  152. if (id && (TC_H_MAJ(id) == 0 ||
  153. !(TC_H_MAJ(id^tp->q->handle)))) {
  154. res->classid = id;
  155. res->class = 0;
  156. return 0;
  157. }
  158. return -1;
  159. }
  160. static inline u32 to_hash(u32 id)
  161. {
  162. u32 h = id & 0xFF;
  163. if (id & 0x8000)
  164. h += 256;
  165. return h;
  166. }
  167. static inline u32 from_hash(u32 id)
  168. {
  169. id &= 0xFFFF;
  170. if (id == 0xFFFF)
  171. return 32;
  172. if (!(id & 0x8000)) {
  173. if (id > 255)
  174. return 256;
  175. return id & 0xF;
  176. }
  177. return 16 + (id & 0xF);
  178. }
  179. static unsigned long route4_get(struct tcf_proto *tp, u32 handle)
  180. {
  181. struct route4_head *head = tp->root;
  182. struct route4_bucket *b;
  183. struct route4_filter *f;
  184. unsigned int h1, h2;
  185. if (!head)
  186. return 0;
  187. h1 = to_hash(handle);
  188. if (h1 > 256)
  189. return 0;
  190. h2 = from_hash(handle >> 16);
  191. if (h2 > 32)
  192. return 0;
  193. b = head->table[h1];
  194. if (b) {
  195. for (f = b->ht[h2]; f; f = f->next)
  196. if (f->handle == handle)
  197. return (unsigned long)f;
  198. }
  199. return 0;
  200. }
  201. static void route4_put(struct tcf_proto *tp, unsigned long f)
  202. {
  203. }
  204. static int route4_init(struct tcf_proto *tp)
  205. {
  206. return 0;
  207. }
  208. static void
  209. route4_delete_filter(struct tcf_proto *tp, struct route4_filter *f)
  210. {
  211. tcf_unbind_filter(tp, &f->res);
  212. tcf_exts_destroy(tp, &f->exts);
  213. kfree(f);
  214. }
  215. static void route4_destroy(struct tcf_proto *tp)
  216. {
  217. struct route4_head *head = tp->root;
  218. int h1, h2;
  219. if (head == NULL)
  220. return;
  221. for (h1 = 0; h1 <= 256; h1++) {
  222. struct route4_bucket *b;
  223. b = head->table[h1];
  224. if (b) {
  225. for (h2 = 0; h2 <= 32; h2++) {
  226. struct route4_filter *f;
  227. while ((f = b->ht[h2]) != NULL) {
  228. b->ht[h2] = f->next;
  229. route4_delete_filter(tp, f);
  230. }
  231. }
  232. kfree(b);
  233. }
  234. }
  235. kfree(head);
  236. }
  237. static int route4_delete(struct tcf_proto *tp, unsigned long arg)
  238. {
  239. struct route4_head *head = tp->root;
  240. struct route4_filter **fp, *f = (struct route4_filter *)arg;
  241. unsigned int h = 0;
  242. struct route4_bucket *b;
  243. int i;
  244. if (!head || !f)
  245. return -EINVAL;
  246. h = f->handle;
  247. b = f->bkt;
  248. for (fp = &b->ht[from_hash(h >> 16)]; *fp; fp = &(*fp)->next) {
  249. if (*fp == f) {
  250. tcf_tree_lock(tp);
  251. *fp = f->next;
  252. tcf_tree_unlock(tp);
  253. route4_reset_fastmap(tp->q, head, f->id);
  254. route4_delete_filter(tp, f);
  255. /* Strip tree */
  256. for (i = 0; i <= 32; i++)
  257. if (b->ht[i])
  258. return 0;
  259. /* OK, session has no flows */
  260. tcf_tree_lock(tp);
  261. head->table[to_hash(h)] = NULL;
  262. tcf_tree_unlock(tp);
  263. kfree(b);
  264. return 0;
  265. }
  266. }
  267. return 0;
  268. }
  269. static const struct nla_policy route4_policy[TCA_ROUTE4_MAX + 1] = {
  270. [TCA_ROUTE4_CLASSID] = { .type = NLA_U32 },
  271. [TCA_ROUTE4_TO] = { .type = NLA_U32 },
  272. [TCA_ROUTE4_FROM] = { .type = NLA_U32 },
  273. [TCA_ROUTE4_IIF] = { .type = NLA_U32 },
  274. };
  275. static int route4_set_parms(struct net *net, struct tcf_proto *tp,
  276. unsigned long base, struct route4_filter *f,
  277. u32 handle, struct route4_head *head,
  278. struct nlattr **tb, struct nlattr *est, int new)
  279. {
  280. int err;
  281. u32 id = 0, to = 0, nhandle = 0x8000;
  282. struct route4_filter *fp;
  283. unsigned int h1;
  284. struct route4_bucket *b;
  285. struct tcf_exts e;
  286. tcf_exts_init(&e, TCA_ROUTE4_ACT, TCA_ROUTE4_POLICE);
  287. err = tcf_exts_validate(net, tp, tb, est, &e);
  288. if (err < 0)
  289. return err;
  290. err = -EINVAL;
  291. if (tb[TCA_ROUTE4_TO]) {
  292. if (new && handle & 0x8000)
  293. goto errout;
  294. to = nla_get_u32(tb[TCA_ROUTE4_TO]);
  295. if (to > 0xFF)
  296. goto errout;
  297. nhandle = to;
  298. }
  299. if (tb[TCA_ROUTE4_FROM]) {
  300. if (tb[TCA_ROUTE4_IIF])
  301. goto errout;
  302. id = nla_get_u32(tb[TCA_ROUTE4_FROM]);
  303. if (id > 0xFF)
  304. goto errout;
  305. nhandle |= id << 16;
  306. } else if (tb[TCA_ROUTE4_IIF]) {
  307. id = nla_get_u32(tb[TCA_ROUTE4_IIF]);
  308. if (id > 0x7FFF)
  309. goto errout;
  310. nhandle |= (id | 0x8000) << 16;
  311. } else
  312. nhandle |= 0xFFFF << 16;
  313. if (handle && new) {
  314. nhandle |= handle & 0x7F00;
  315. if (nhandle != handle)
  316. goto errout;
  317. }
  318. h1 = to_hash(nhandle);
  319. b = head->table[h1];
  320. if (!b) {
  321. err = -ENOBUFS;
  322. b = kzalloc(sizeof(struct route4_bucket), GFP_KERNEL);
  323. if (b == NULL)
  324. goto errout;
  325. tcf_tree_lock(tp);
  326. head->table[h1] = b;
  327. tcf_tree_unlock(tp);
  328. } else {
  329. unsigned int h2 = from_hash(nhandle >> 16);
  330. err = -EEXIST;
  331. for (fp = b->ht[h2]; fp; fp = fp->next)
  332. if (fp->handle == f->handle)
  333. goto errout;
  334. }
  335. tcf_tree_lock(tp);
  336. if (tb[TCA_ROUTE4_TO])
  337. f->id = to;
  338. if (tb[TCA_ROUTE4_FROM])
  339. f->id = to | id<<16;
  340. else if (tb[TCA_ROUTE4_IIF])
  341. f->iif = id;
  342. f->handle = nhandle;
  343. f->bkt = b;
  344. tcf_tree_unlock(tp);
  345. if (tb[TCA_ROUTE4_CLASSID]) {
  346. f->res.classid = nla_get_u32(tb[TCA_ROUTE4_CLASSID]);
  347. tcf_bind_filter(tp, &f->res, base);
  348. }
  349. tcf_exts_change(tp, &f->exts, &e);
  350. return 0;
  351. errout:
  352. tcf_exts_destroy(tp, &e);
  353. return err;
  354. }
  355. static int route4_change(struct net *net, struct sk_buff *in_skb,
  356. struct tcf_proto *tp, unsigned long base,
  357. u32 handle,
  358. struct nlattr **tca,
  359. unsigned long *arg)
  360. {
  361. struct route4_head *head = tp->root;
  362. struct route4_filter *f, *f1, **fp;
  363. struct route4_bucket *b;
  364. struct nlattr *opt = tca[TCA_OPTIONS];
  365. struct nlattr *tb[TCA_ROUTE4_MAX + 1];
  366. unsigned int h, th;
  367. u32 old_handle = 0;
  368. int err;
  369. if (opt == NULL)
  370. return handle ? -EINVAL : 0;
  371. err = nla_parse_nested(tb, TCA_ROUTE4_MAX, opt, route4_policy);
  372. if (err < 0)
  373. return err;
  374. f = (struct route4_filter *)*arg;
  375. if (f) {
  376. if (f->handle != handle && handle)
  377. return -EINVAL;
  378. if (f->bkt)
  379. old_handle = f->handle;
  380. err = route4_set_parms(net, tp, base, f, handle, head, tb,
  381. tca[TCA_RATE], 0);
  382. if (err < 0)
  383. return err;
  384. goto reinsert;
  385. }
  386. err = -ENOBUFS;
  387. if (head == NULL) {
  388. head = kzalloc(sizeof(struct route4_head), GFP_KERNEL);
  389. if (head == NULL)
  390. goto errout;
  391. tcf_tree_lock(tp);
  392. tp->root = head;
  393. tcf_tree_unlock(tp);
  394. }
  395. f = kzalloc(sizeof(struct route4_filter), GFP_KERNEL);
  396. if (f == NULL)
  397. goto errout;
  398. tcf_exts_init(&f->exts, TCA_ROUTE4_ACT, TCA_ROUTE4_POLICE);
  399. err = route4_set_parms(net, 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 net *net, 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. if (nla_put_u32(skb, TCA_ROUTE4_TO, id))
  476. goto nla_put_failure;
  477. }
  478. if (f->handle & 0x80000000) {
  479. if ((f->handle >> 16) != 0xFFFF &&
  480. nla_put_u32(skb, TCA_ROUTE4_IIF, f->iif))
  481. goto nla_put_failure;
  482. } else {
  483. id = f->id >> 16;
  484. if (nla_put_u32(skb, TCA_ROUTE4_FROM, id))
  485. goto nla_put_failure;
  486. }
  487. if (f->res.classid &&
  488. nla_put_u32(skb, TCA_ROUTE4_CLASSID, f->res.classid))
  489. goto nla_put_failure;
  490. if (tcf_exts_dump(skb, &f->exts) < 0)
  491. goto nla_put_failure;
  492. nla_nest_end(skb, nest);
  493. if (tcf_exts_dump_stats(skb, &f->exts) < 0)
  494. goto nla_put_failure;
  495. return skb->len;
  496. nla_put_failure:
  497. nlmsg_trim(skb, b);
  498. return -1;
  499. }
  500. static struct tcf_proto_ops cls_route4_ops __read_mostly = {
  501. .kind = "route",
  502. .classify = route4_classify,
  503. .init = route4_init,
  504. .destroy = route4_destroy,
  505. .get = route4_get,
  506. .put = route4_put,
  507. .change = route4_change,
  508. .delete = route4_delete,
  509. .walk = route4_walk,
  510. .dump = route4_dump,
  511. .owner = THIS_MODULE,
  512. };
  513. static int __init init_route4(void)
  514. {
  515. return register_tcf_proto_ops(&cls_route4_ops);
  516. }
  517. static void __exit exit_route4(void)
  518. {
  519. unregister_tcf_proto_ops(&cls_route4_ops);
  520. }
  521. module_init(init_route4)
  522. module_exit(exit_route4)
  523. MODULE_LICENSE("GPL");