cls_route.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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. bool ovr)
  280. {
  281. int err;
  282. u32 id = 0, to = 0, nhandle = 0x8000;
  283. struct route4_filter *fp;
  284. unsigned int h1;
  285. struct route4_bucket *b;
  286. struct tcf_exts e;
  287. tcf_exts_init(&e, TCA_ROUTE4_ACT, TCA_ROUTE4_POLICE);
  288. err = tcf_exts_validate(net, tp, tb, est, &e, ovr);
  289. if (err < 0)
  290. return err;
  291. err = -EINVAL;
  292. if (tb[TCA_ROUTE4_TO]) {
  293. if (new && handle & 0x8000)
  294. goto errout;
  295. to = nla_get_u32(tb[TCA_ROUTE4_TO]);
  296. if (to > 0xFF)
  297. goto errout;
  298. nhandle = to;
  299. }
  300. if (tb[TCA_ROUTE4_FROM]) {
  301. if (tb[TCA_ROUTE4_IIF])
  302. goto errout;
  303. id = nla_get_u32(tb[TCA_ROUTE4_FROM]);
  304. if (id > 0xFF)
  305. goto errout;
  306. nhandle |= id << 16;
  307. } else if (tb[TCA_ROUTE4_IIF]) {
  308. id = nla_get_u32(tb[TCA_ROUTE4_IIF]);
  309. if (id > 0x7FFF)
  310. goto errout;
  311. nhandle |= (id | 0x8000) << 16;
  312. } else
  313. nhandle |= 0xFFFF << 16;
  314. if (handle && new) {
  315. nhandle |= handle & 0x7F00;
  316. if (nhandle != handle)
  317. goto errout;
  318. }
  319. h1 = to_hash(nhandle);
  320. b = head->table[h1];
  321. if (!b) {
  322. err = -ENOBUFS;
  323. b = kzalloc(sizeof(struct route4_bucket), GFP_KERNEL);
  324. if (b == NULL)
  325. goto errout;
  326. tcf_tree_lock(tp);
  327. head->table[h1] = b;
  328. tcf_tree_unlock(tp);
  329. } else {
  330. unsigned int h2 = from_hash(nhandle >> 16);
  331. err = -EEXIST;
  332. for (fp = b->ht[h2]; fp; fp = fp->next)
  333. if (fp->handle == f->handle)
  334. goto errout;
  335. }
  336. tcf_tree_lock(tp);
  337. if (tb[TCA_ROUTE4_TO])
  338. f->id = to;
  339. if (tb[TCA_ROUTE4_FROM])
  340. f->id = to | id<<16;
  341. else if (tb[TCA_ROUTE4_IIF])
  342. f->iif = id;
  343. f->handle = nhandle;
  344. f->bkt = b;
  345. tcf_tree_unlock(tp);
  346. if (tb[TCA_ROUTE4_CLASSID]) {
  347. f->res.classid = nla_get_u32(tb[TCA_ROUTE4_CLASSID]);
  348. tcf_bind_filter(tp, &f->res, base);
  349. }
  350. tcf_exts_change(tp, &f->exts, &e);
  351. return 0;
  352. errout:
  353. tcf_exts_destroy(tp, &e);
  354. return err;
  355. }
  356. static int route4_change(struct net *net, struct sk_buff *in_skb,
  357. struct tcf_proto *tp, unsigned long base,
  358. u32 handle,
  359. struct nlattr **tca,
  360. unsigned long *arg, bool ovr)
  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(net, tp, base, f, handle, head, tb,
  382. tca[TCA_RATE], 0, ovr);
  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. tcf_exts_init(&f->exts, TCA_ROUTE4_ACT, TCA_ROUTE4_POLICE);
  400. err = route4_set_parms(net, tp, base, f, handle, head, tb,
  401. tca[TCA_RATE], 1, ovr);
  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 net *net, 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) < 0)
  492. goto nla_put_failure;
  493. nla_nest_end(skb, nest);
  494. if (tcf_exts_dump_stats(skb, &f->exts) < 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");