cls_route.c 14 KB

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