cls_route.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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 __rcu *table[256 + 1];
  38. struct rcu_head rcu;
  39. };
  40. struct route4_bucket {
  41. /* 16 FROM buckets + 16 IIF buckets + 1 wildcard bucket */
  42. struct route4_filter __rcu *ht[16 + 16 + 1];
  43. struct rcu_head rcu;
  44. };
  45. struct route4_filter {
  46. struct route4_filter __rcu *next;
  47. u32 id;
  48. int iif;
  49. struct tcf_result res;
  50. struct tcf_exts exts;
  51. u32 handle;
  52. struct route4_bucket *bkt;
  53. struct tcf_proto *tp;
  54. struct rcu_head rcu;
  55. };
  56. #define ROUTE4_FAILURE ((struct route4_filter *)(-1L))
  57. static inline int route4_fastmap_hash(u32 id, int iif)
  58. {
  59. return id & 0xF;
  60. }
  61. static DEFINE_SPINLOCK(fastmap_lock);
  62. static void
  63. route4_reset_fastmap(struct route4_head *head)
  64. {
  65. spin_lock_bh(&fastmap_lock);
  66. memset(head->fastmap, 0, sizeof(head->fastmap));
  67. spin_unlock_bh(&fastmap_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. /* fastmap updates must look atomic to aling id, iff, filter */
  75. spin_lock_bh(&fastmap_lock);
  76. head->fastmap[h].id = id;
  77. head->fastmap[h].iif = iif;
  78. head->fastmap[h].filter = f;
  79. spin_unlock_bh(&fastmap_lock);
  80. }
  81. static inline int route4_hash_to(u32 id)
  82. {
  83. return id & 0xFF;
  84. }
  85. static inline int route4_hash_from(u32 id)
  86. {
  87. return (id >> 16) & 0xF;
  88. }
  89. static inline int route4_hash_iif(int iif)
  90. {
  91. return 16 + ((iif >> 16) & 0xF);
  92. }
  93. static inline int route4_hash_wild(void)
  94. {
  95. return 32;
  96. }
  97. #define ROUTE4_APPLY_RESULT() \
  98. { \
  99. *res = f->res; \
  100. if (tcf_exts_is_available(&f->exts)) { \
  101. int r = tcf_exts_exec(skb, &f->exts, res); \
  102. if (r < 0) { \
  103. dont_cache = 1; \
  104. continue; \
  105. } \
  106. return r; \
  107. } else if (!dont_cache) \
  108. route4_set_fastmap(head, id, iif, f); \
  109. return 0; \
  110. }
  111. static int route4_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  112. struct tcf_result *res)
  113. {
  114. struct route4_head *head = rcu_dereference_bh(tp->root);
  115. struct dst_entry *dst;
  116. struct route4_bucket *b;
  117. struct route4_filter *f;
  118. u32 id, h;
  119. int iif, dont_cache = 0;
  120. dst = skb_dst(skb);
  121. if (!dst)
  122. goto failure;
  123. id = dst->tclassid;
  124. if (head == NULL)
  125. goto old_method;
  126. iif = inet_iif(skb);
  127. h = route4_fastmap_hash(id, iif);
  128. spin_lock(&fastmap_lock);
  129. if (id == head->fastmap[h].id &&
  130. iif == head->fastmap[h].iif &&
  131. (f = head->fastmap[h].filter) != NULL) {
  132. if (f == ROUTE4_FAILURE) {
  133. spin_unlock(&fastmap_lock);
  134. goto failure;
  135. }
  136. *res = f->res;
  137. spin_unlock(&fastmap_lock);
  138. return 0;
  139. }
  140. spin_unlock(&fastmap_lock);
  141. h = route4_hash_to(id);
  142. restart:
  143. b = rcu_dereference_bh(head->table[h]);
  144. if (b) {
  145. for (f = rcu_dereference_bh(b->ht[route4_hash_from(id)]);
  146. f;
  147. f = rcu_dereference_bh(f->next))
  148. if (f->id == id)
  149. ROUTE4_APPLY_RESULT();
  150. for (f = rcu_dereference_bh(b->ht[route4_hash_iif(iif)]);
  151. f;
  152. f = rcu_dereference_bh(f->next))
  153. if (f->iif == iif)
  154. ROUTE4_APPLY_RESULT();
  155. for (f = rcu_dereference_bh(b->ht[route4_hash_wild()]);
  156. f;
  157. f = rcu_dereference_bh(f->next))
  158. ROUTE4_APPLY_RESULT();
  159. }
  160. if (h < 256) {
  161. h = 256;
  162. id &= ~0xFFFF;
  163. goto restart;
  164. }
  165. if (!dont_cache)
  166. route4_set_fastmap(head, id, iif, ROUTE4_FAILURE);
  167. failure:
  168. return -1;
  169. old_method:
  170. if (id && (TC_H_MAJ(id) == 0 ||
  171. !(TC_H_MAJ(id^tp->q->handle)))) {
  172. res->classid = id;
  173. res->class = 0;
  174. return 0;
  175. }
  176. return -1;
  177. }
  178. static inline u32 to_hash(u32 id)
  179. {
  180. u32 h = id & 0xFF;
  181. if (id & 0x8000)
  182. h += 256;
  183. return h;
  184. }
  185. static inline u32 from_hash(u32 id)
  186. {
  187. id &= 0xFFFF;
  188. if (id == 0xFFFF)
  189. return 32;
  190. if (!(id & 0x8000)) {
  191. if (id > 255)
  192. return 256;
  193. return id & 0xF;
  194. }
  195. return 16 + (id & 0xF);
  196. }
  197. static unsigned long route4_get(struct tcf_proto *tp, u32 handle)
  198. {
  199. struct route4_head *head = rtnl_dereference(tp->root);
  200. struct route4_bucket *b;
  201. struct route4_filter *f;
  202. unsigned int h1, h2;
  203. if (!head)
  204. return 0;
  205. h1 = to_hash(handle);
  206. if (h1 > 256)
  207. return 0;
  208. h2 = from_hash(handle >> 16);
  209. if (h2 > 32)
  210. return 0;
  211. b = rtnl_dereference(head->table[h1]);
  212. if (b) {
  213. for (f = rtnl_dereference(b->ht[h2]);
  214. f;
  215. f = rtnl_dereference(f->next))
  216. if (f->handle == handle)
  217. return (unsigned long)f;
  218. }
  219. return 0;
  220. }
  221. static int route4_init(struct tcf_proto *tp)
  222. {
  223. return 0;
  224. }
  225. static void
  226. route4_delete_filter(struct rcu_head *head)
  227. {
  228. struct route4_filter *f = container_of(head, struct route4_filter, rcu);
  229. tcf_exts_destroy(&f->exts);
  230. kfree(f);
  231. }
  232. static void route4_destroy(struct tcf_proto *tp)
  233. {
  234. struct route4_head *head = rtnl_dereference(tp->root);
  235. int h1, h2;
  236. if (head == NULL)
  237. return;
  238. for (h1 = 0; h1 <= 256; h1++) {
  239. struct route4_bucket *b;
  240. b = rtnl_dereference(head->table[h1]);
  241. if (b) {
  242. for (h2 = 0; h2 <= 32; h2++) {
  243. struct route4_filter *f;
  244. while ((f = rtnl_dereference(b->ht[h2])) != NULL) {
  245. struct route4_filter *next;
  246. next = rtnl_dereference(f->next);
  247. RCU_INIT_POINTER(b->ht[h2], next);
  248. tcf_unbind_filter(tp, &f->res);
  249. call_rcu(&f->rcu, route4_delete_filter);
  250. }
  251. }
  252. RCU_INIT_POINTER(head->table[h1], NULL);
  253. kfree_rcu(b, rcu);
  254. }
  255. }
  256. RCU_INIT_POINTER(tp->root, NULL);
  257. kfree_rcu(head, rcu);
  258. }
  259. static int route4_delete(struct tcf_proto *tp, unsigned long arg)
  260. {
  261. struct route4_head *head = rtnl_dereference(tp->root);
  262. struct route4_filter *f = (struct route4_filter *)arg;
  263. struct route4_filter __rcu **fp;
  264. struct route4_filter *nf;
  265. struct route4_bucket *b;
  266. unsigned int h = 0;
  267. int i;
  268. if (!head || !f)
  269. return -EINVAL;
  270. h = f->handle;
  271. b = f->bkt;
  272. fp = &b->ht[from_hash(h >> 16)];
  273. for (nf = rtnl_dereference(*fp); nf;
  274. fp = &nf->next, nf = rtnl_dereference(*fp)) {
  275. if (nf == f) {
  276. /* unlink it */
  277. RCU_INIT_POINTER(*fp, rtnl_dereference(f->next));
  278. /* Remove any fastmap lookups that might ref filter
  279. * notice we unlink'd the filter so we can't get it
  280. * back in the fastmap.
  281. */
  282. route4_reset_fastmap(head);
  283. /* Delete it */
  284. tcf_unbind_filter(tp, &f->res);
  285. call_rcu(&f->rcu, route4_delete_filter);
  286. /* Strip RTNL protected tree */
  287. for (i = 0; i <= 32; i++) {
  288. struct route4_filter *rt;
  289. rt = rtnl_dereference(b->ht[i]);
  290. if (rt)
  291. return 0;
  292. }
  293. /* OK, session has no flows */
  294. RCU_INIT_POINTER(head->table[to_hash(h)], NULL);
  295. kfree_rcu(b, rcu);
  296. return 0;
  297. }
  298. }
  299. return 0;
  300. }
  301. static const struct nla_policy route4_policy[TCA_ROUTE4_MAX + 1] = {
  302. [TCA_ROUTE4_CLASSID] = { .type = NLA_U32 },
  303. [TCA_ROUTE4_TO] = { .type = NLA_U32 },
  304. [TCA_ROUTE4_FROM] = { .type = NLA_U32 },
  305. [TCA_ROUTE4_IIF] = { .type = NLA_U32 },
  306. };
  307. static int route4_set_parms(struct net *net, struct tcf_proto *tp,
  308. unsigned long base, struct route4_filter *f,
  309. u32 handle, struct route4_head *head,
  310. struct nlattr **tb, struct nlattr *est, int new,
  311. bool ovr)
  312. {
  313. int err;
  314. u32 id = 0, to = 0, nhandle = 0x8000;
  315. struct route4_filter *fp;
  316. unsigned int h1;
  317. struct route4_bucket *b;
  318. struct tcf_exts e;
  319. tcf_exts_init(&e, TCA_ROUTE4_ACT, TCA_ROUTE4_POLICE);
  320. err = tcf_exts_validate(net, tp, tb, est, &e, ovr);
  321. if (err < 0)
  322. return err;
  323. err = -EINVAL;
  324. if (tb[TCA_ROUTE4_TO]) {
  325. if (new && handle & 0x8000)
  326. goto errout;
  327. to = nla_get_u32(tb[TCA_ROUTE4_TO]);
  328. if (to > 0xFF)
  329. goto errout;
  330. nhandle = to;
  331. }
  332. if (tb[TCA_ROUTE4_FROM]) {
  333. if (tb[TCA_ROUTE4_IIF])
  334. goto errout;
  335. id = nla_get_u32(tb[TCA_ROUTE4_FROM]);
  336. if (id > 0xFF)
  337. goto errout;
  338. nhandle |= id << 16;
  339. } else if (tb[TCA_ROUTE4_IIF]) {
  340. id = nla_get_u32(tb[TCA_ROUTE4_IIF]);
  341. if (id > 0x7FFF)
  342. goto errout;
  343. nhandle |= (id | 0x8000) << 16;
  344. } else
  345. nhandle |= 0xFFFF << 16;
  346. if (handle && new) {
  347. nhandle |= handle & 0x7F00;
  348. if (nhandle != handle)
  349. goto errout;
  350. }
  351. h1 = to_hash(nhandle);
  352. b = rtnl_dereference(head->table[h1]);
  353. if (!b) {
  354. err = -ENOBUFS;
  355. b = kzalloc(sizeof(struct route4_bucket), GFP_KERNEL);
  356. if (b == NULL)
  357. goto errout;
  358. rcu_assign_pointer(head->table[h1], b);
  359. } else {
  360. unsigned int h2 = from_hash(nhandle >> 16);
  361. err = -EEXIST;
  362. for (fp = rtnl_dereference(b->ht[h2]);
  363. fp;
  364. fp = rtnl_dereference(fp->next))
  365. if (fp->handle == f->handle)
  366. goto errout;
  367. }
  368. if (tb[TCA_ROUTE4_TO])
  369. f->id = to;
  370. if (tb[TCA_ROUTE4_FROM])
  371. f->id = to | id<<16;
  372. else if (tb[TCA_ROUTE4_IIF])
  373. f->iif = id;
  374. f->handle = nhandle;
  375. f->bkt = b;
  376. f->tp = tp;
  377. if (tb[TCA_ROUTE4_CLASSID]) {
  378. f->res.classid = nla_get_u32(tb[TCA_ROUTE4_CLASSID]);
  379. tcf_bind_filter(tp, &f->res, base);
  380. }
  381. tcf_exts_change(tp, &f->exts, &e);
  382. return 0;
  383. errout:
  384. tcf_exts_destroy(&e);
  385. return err;
  386. }
  387. static int route4_change(struct net *net, struct sk_buff *in_skb,
  388. struct tcf_proto *tp, unsigned long base,
  389. u32 handle,
  390. struct nlattr **tca,
  391. unsigned long *arg, bool ovr)
  392. {
  393. struct route4_head *head = rtnl_dereference(tp->root);
  394. struct route4_filter __rcu **fp;
  395. struct route4_filter *fold, *f1, *pfp, *f = NULL;
  396. struct route4_bucket *b;
  397. struct nlattr *opt = tca[TCA_OPTIONS];
  398. struct nlattr *tb[TCA_ROUTE4_MAX + 1];
  399. unsigned int h, th;
  400. int err;
  401. bool new = true;
  402. if (opt == NULL)
  403. return handle ? -EINVAL : 0;
  404. err = nla_parse_nested(tb, TCA_ROUTE4_MAX, opt, route4_policy);
  405. if (err < 0)
  406. return err;
  407. fold = (struct route4_filter *)*arg;
  408. if (fold && handle && fold->handle != handle)
  409. return -EINVAL;
  410. err = -ENOBUFS;
  411. if (head == NULL) {
  412. head = kzalloc(sizeof(struct route4_head), GFP_KERNEL);
  413. if (head == NULL)
  414. goto errout;
  415. rcu_assign_pointer(tp->root, head);
  416. }
  417. f = kzalloc(sizeof(struct route4_filter), GFP_KERNEL);
  418. if (!f)
  419. goto errout;
  420. tcf_exts_init(&f->exts, TCA_ROUTE4_ACT, TCA_ROUTE4_POLICE);
  421. if (fold) {
  422. f->id = fold->id;
  423. f->iif = fold->iif;
  424. f->res = fold->res;
  425. f->handle = fold->handle;
  426. f->tp = fold->tp;
  427. f->bkt = fold->bkt;
  428. new = false;
  429. }
  430. err = route4_set_parms(net, tp, base, f, handle, head, tb,
  431. tca[TCA_RATE], new, ovr);
  432. if (err < 0)
  433. goto errout;
  434. h = from_hash(f->handle >> 16);
  435. fp = &f->bkt->ht[h];
  436. for (pfp = rtnl_dereference(*fp);
  437. (f1 = rtnl_dereference(*fp)) != NULL;
  438. fp = &f1->next)
  439. if (f->handle < f1->handle)
  440. break;
  441. netif_keep_dst(qdisc_dev(tp->q));
  442. rcu_assign_pointer(f->next, f1);
  443. rcu_assign_pointer(*fp, f);
  444. if (fold && fold->handle && f->handle != fold->handle) {
  445. th = to_hash(fold->handle);
  446. h = from_hash(fold->handle >> 16);
  447. b = rtnl_dereference(head->table[th]);
  448. if (b) {
  449. fp = &b->ht[h];
  450. for (pfp = rtnl_dereference(*fp); pfp;
  451. fp = &pfp->next, pfp = rtnl_dereference(*fp)) {
  452. if (pfp == f) {
  453. *fp = f->next;
  454. break;
  455. }
  456. }
  457. }
  458. }
  459. route4_reset_fastmap(head);
  460. *arg = (unsigned long)f;
  461. if (fold) {
  462. tcf_unbind_filter(tp, &fold->res);
  463. call_rcu(&fold->rcu, route4_delete_filter);
  464. }
  465. return 0;
  466. errout:
  467. kfree(f);
  468. return err;
  469. }
  470. static void route4_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  471. {
  472. struct route4_head *head = rtnl_dereference(tp->root);
  473. unsigned int h, h1;
  474. if (head == NULL)
  475. arg->stop = 1;
  476. if (arg->stop)
  477. return;
  478. for (h = 0; h <= 256; h++) {
  479. struct route4_bucket *b = rtnl_dereference(head->table[h]);
  480. if (b) {
  481. for (h1 = 0; h1 <= 32; h1++) {
  482. struct route4_filter *f;
  483. for (f = rtnl_dereference(b->ht[h1]);
  484. f;
  485. f = rtnl_dereference(f->next)) {
  486. if (arg->count < arg->skip) {
  487. arg->count++;
  488. continue;
  489. }
  490. if (arg->fn(tp, (unsigned long)f, arg) < 0) {
  491. arg->stop = 1;
  492. return;
  493. }
  494. arg->count++;
  495. }
  496. }
  497. }
  498. }
  499. }
  500. static int route4_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
  501. struct sk_buff *skb, struct tcmsg *t)
  502. {
  503. struct route4_filter *f = (struct route4_filter *)fh;
  504. struct nlattr *nest;
  505. u32 id;
  506. if (f == NULL)
  507. return skb->len;
  508. t->tcm_handle = f->handle;
  509. nest = nla_nest_start(skb, TCA_OPTIONS);
  510. if (nest == NULL)
  511. goto nla_put_failure;
  512. if (!(f->handle & 0x8000)) {
  513. id = f->id & 0xFF;
  514. if (nla_put_u32(skb, TCA_ROUTE4_TO, id))
  515. goto nla_put_failure;
  516. }
  517. if (f->handle & 0x80000000) {
  518. if ((f->handle >> 16) != 0xFFFF &&
  519. nla_put_u32(skb, TCA_ROUTE4_IIF, f->iif))
  520. goto nla_put_failure;
  521. } else {
  522. id = f->id >> 16;
  523. if (nla_put_u32(skb, TCA_ROUTE4_FROM, id))
  524. goto nla_put_failure;
  525. }
  526. if (f->res.classid &&
  527. nla_put_u32(skb, TCA_ROUTE4_CLASSID, f->res.classid))
  528. goto nla_put_failure;
  529. if (tcf_exts_dump(skb, &f->exts) < 0)
  530. goto nla_put_failure;
  531. nla_nest_end(skb, nest);
  532. if (tcf_exts_dump_stats(skb, &f->exts) < 0)
  533. goto nla_put_failure;
  534. return skb->len;
  535. nla_put_failure:
  536. nla_nest_cancel(skb, nest);
  537. return -1;
  538. }
  539. static struct tcf_proto_ops cls_route4_ops __read_mostly = {
  540. .kind = "route",
  541. .classify = route4_classify,
  542. .init = route4_init,
  543. .destroy = route4_destroy,
  544. .get = route4_get,
  545. .change = route4_change,
  546. .delete = route4_delete,
  547. .walk = route4_walk,
  548. .dump = route4_dump,
  549. .owner = THIS_MODULE,
  550. };
  551. static int __init init_route4(void)
  552. {
  553. return register_tcf_proto_ops(&cls_route4_ops);
  554. }
  555. static void __exit exit_route4(void)
  556. {
  557. unregister_tcf_proto_ops(&cls_route4_ops);
  558. }
  559. module_init(init_route4)
  560. module_exit(exit_route4)
  561. MODULE_LICENSE("GPL");