cls_route.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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_has_actions(&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 void *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 NULL;
  196. h2 = from_hash(handle >> 16);
  197. if (h2 > 32)
  198. return NULL;
  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 f;
  206. }
  207. return NULL;
  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, void *arg, bool *last)
  251. {
  252. struct route4_head *head = rtnl_dereference(tp->root);
  253. struct route4_filter *f = 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. int err;
  317. err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr);
  318. if (err < 0)
  319. return err;
  320. if (tb[TCA_ROUTE4_TO]) {
  321. if (new && handle & 0x8000)
  322. return -EINVAL;
  323. to = nla_get_u32(tb[TCA_ROUTE4_TO]);
  324. if (to > 0xFF)
  325. return -EINVAL;
  326. nhandle = to;
  327. }
  328. if (tb[TCA_ROUTE4_FROM]) {
  329. if (tb[TCA_ROUTE4_IIF])
  330. return -EINVAL;
  331. id = nla_get_u32(tb[TCA_ROUTE4_FROM]);
  332. if (id > 0xFF)
  333. return -EINVAL;
  334. nhandle |= id << 16;
  335. } else if (tb[TCA_ROUTE4_IIF]) {
  336. id = nla_get_u32(tb[TCA_ROUTE4_IIF]);
  337. if (id > 0x7FFF)
  338. return -EINVAL;
  339. nhandle |= (id | 0x8000) << 16;
  340. } else
  341. nhandle |= 0xFFFF << 16;
  342. if (handle && new) {
  343. nhandle |= handle & 0x7F00;
  344. if (nhandle != handle)
  345. return -EINVAL;
  346. }
  347. h1 = to_hash(nhandle);
  348. b = rtnl_dereference(head->table[h1]);
  349. if (!b) {
  350. b = kzalloc(sizeof(struct route4_bucket), GFP_KERNEL);
  351. if (b == NULL)
  352. return -ENOBUFS;
  353. rcu_assign_pointer(head->table[h1], b);
  354. } else {
  355. unsigned int h2 = from_hash(nhandle >> 16);
  356. for (fp = rtnl_dereference(b->ht[h2]);
  357. fp;
  358. fp = rtnl_dereference(fp->next))
  359. if (fp->handle == f->handle)
  360. return -EEXIST;
  361. }
  362. if (tb[TCA_ROUTE4_TO])
  363. f->id = to;
  364. if (tb[TCA_ROUTE4_FROM])
  365. f->id = to | id<<16;
  366. else if (tb[TCA_ROUTE4_IIF])
  367. f->iif = id;
  368. f->handle = nhandle;
  369. f->bkt = b;
  370. f->tp = tp;
  371. if (tb[TCA_ROUTE4_CLASSID]) {
  372. f->res.classid = nla_get_u32(tb[TCA_ROUTE4_CLASSID]);
  373. tcf_bind_filter(tp, &f->res, base);
  374. }
  375. return 0;
  376. }
  377. static int route4_change(struct net *net, struct sk_buff *in_skb,
  378. struct tcf_proto *tp, unsigned long base, u32 handle,
  379. struct nlattr **tca, void **arg, bool ovr)
  380. {
  381. struct route4_head *head = rtnl_dereference(tp->root);
  382. struct route4_filter __rcu **fp;
  383. struct route4_filter *fold, *f1, *pfp, *f = NULL;
  384. struct route4_bucket *b;
  385. struct nlattr *opt = tca[TCA_OPTIONS];
  386. struct nlattr *tb[TCA_ROUTE4_MAX + 1];
  387. unsigned int h, th;
  388. int err;
  389. bool new = true;
  390. if (opt == NULL)
  391. return handle ? -EINVAL : 0;
  392. err = nla_parse_nested(tb, TCA_ROUTE4_MAX, opt, route4_policy, NULL);
  393. if (err < 0)
  394. return err;
  395. fold = *arg;
  396. if (fold && handle && fold->handle != handle)
  397. return -EINVAL;
  398. err = -ENOBUFS;
  399. f = kzalloc(sizeof(struct route4_filter), GFP_KERNEL);
  400. if (!f)
  401. goto errout;
  402. err = tcf_exts_init(&f->exts, TCA_ROUTE4_ACT, TCA_ROUTE4_POLICE);
  403. if (err < 0)
  404. goto errout;
  405. if (fold) {
  406. f->id = fold->id;
  407. f->iif = fold->iif;
  408. f->res = fold->res;
  409. f->handle = fold->handle;
  410. f->tp = fold->tp;
  411. f->bkt = fold->bkt;
  412. new = false;
  413. }
  414. err = route4_set_parms(net, tp, base, f, handle, head, tb,
  415. tca[TCA_RATE], new, ovr);
  416. if (err < 0)
  417. goto errout;
  418. h = from_hash(f->handle >> 16);
  419. fp = &f->bkt->ht[h];
  420. for (pfp = rtnl_dereference(*fp);
  421. (f1 = rtnl_dereference(*fp)) != NULL;
  422. fp = &f1->next)
  423. if (f->handle < f1->handle)
  424. break;
  425. netif_keep_dst(qdisc_dev(tp->q));
  426. rcu_assign_pointer(f->next, f1);
  427. rcu_assign_pointer(*fp, f);
  428. if (fold && fold->handle && f->handle != fold->handle) {
  429. th = to_hash(fold->handle);
  430. h = from_hash(fold->handle >> 16);
  431. b = rtnl_dereference(head->table[th]);
  432. if (b) {
  433. fp = &b->ht[h];
  434. for (pfp = rtnl_dereference(*fp); pfp;
  435. fp = &pfp->next, pfp = rtnl_dereference(*fp)) {
  436. if (pfp == f) {
  437. *fp = f->next;
  438. break;
  439. }
  440. }
  441. }
  442. }
  443. route4_reset_fastmap(head);
  444. *arg = f;
  445. if (fold) {
  446. tcf_unbind_filter(tp, &fold->res);
  447. call_rcu(&fold->rcu, route4_delete_filter);
  448. }
  449. return 0;
  450. errout:
  451. if (f)
  452. tcf_exts_destroy(&f->exts);
  453. kfree(f);
  454. return err;
  455. }
  456. static void route4_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  457. {
  458. struct route4_head *head = rtnl_dereference(tp->root);
  459. unsigned int h, h1;
  460. if (head == NULL)
  461. arg->stop = 1;
  462. if (arg->stop)
  463. return;
  464. for (h = 0; h <= 256; h++) {
  465. struct route4_bucket *b = rtnl_dereference(head->table[h]);
  466. if (b) {
  467. for (h1 = 0; h1 <= 32; h1++) {
  468. struct route4_filter *f;
  469. for (f = rtnl_dereference(b->ht[h1]);
  470. f;
  471. f = rtnl_dereference(f->next)) {
  472. if (arg->count < arg->skip) {
  473. arg->count++;
  474. continue;
  475. }
  476. if (arg->fn(tp, f, arg) < 0) {
  477. arg->stop = 1;
  478. return;
  479. }
  480. arg->count++;
  481. }
  482. }
  483. }
  484. }
  485. }
  486. static int route4_dump(struct net *net, struct tcf_proto *tp, void *fh,
  487. struct sk_buff *skb, struct tcmsg *t)
  488. {
  489. struct route4_filter *f = fh;
  490. struct nlattr *nest;
  491. u32 id;
  492. if (f == NULL)
  493. return skb->len;
  494. t->tcm_handle = f->handle;
  495. nest = nla_nest_start(skb, TCA_OPTIONS);
  496. if (nest == NULL)
  497. goto nla_put_failure;
  498. if (!(f->handle & 0x8000)) {
  499. id = f->id & 0xFF;
  500. if (nla_put_u32(skb, TCA_ROUTE4_TO, id))
  501. goto nla_put_failure;
  502. }
  503. if (f->handle & 0x80000000) {
  504. if ((f->handle >> 16) != 0xFFFF &&
  505. nla_put_u32(skb, TCA_ROUTE4_IIF, f->iif))
  506. goto nla_put_failure;
  507. } else {
  508. id = f->id >> 16;
  509. if (nla_put_u32(skb, TCA_ROUTE4_FROM, id))
  510. goto nla_put_failure;
  511. }
  512. if (f->res.classid &&
  513. nla_put_u32(skb, TCA_ROUTE4_CLASSID, f->res.classid))
  514. goto nla_put_failure;
  515. if (tcf_exts_dump(skb, &f->exts) < 0)
  516. goto nla_put_failure;
  517. nla_nest_end(skb, nest);
  518. if (tcf_exts_dump_stats(skb, &f->exts) < 0)
  519. goto nla_put_failure;
  520. return skb->len;
  521. nla_put_failure:
  522. nla_nest_cancel(skb, nest);
  523. return -1;
  524. }
  525. static struct tcf_proto_ops cls_route4_ops __read_mostly = {
  526. .kind = "route",
  527. .classify = route4_classify,
  528. .init = route4_init,
  529. .destroy = route4_destroy,
  530. .get = route4_get,
  531. .change = route4_change,
  532. .delete = route4_delete,
  533. .walk = route4_walk,
  534. .dump = route4_dump,
  535. .owner = THIS_MODULE,
  536. };
  537. static int __init init_route4(void)
  538. {
  539. return register_tcf_proto_ops(&cls_route4_ops);
  540. }
  541. static void __exit exit_route4(void)
  542. {
  543. unregister_tcf_proto_ops(&cls_route4_ops);
  544. }
  545. module_init(init_route4)
  546. module_exit(exit_route4)
  547. MODULE_LICENSE("GPL");