cls_route.c 14 KB

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