cls_route.c 14 KB

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