cls_route.c 14 KB

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