cls_route.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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_work rwork;
  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 route4_filter *f)
  219. {
  220. tcf_exts_destroy(&f->exts);
  221. tcf_exts_put_net(&f->exts);
  222. kfree(f);
  223. }
  224. static void route4_delete_filter_work(struct work_struct *work)
  225. {
  226. struct route4_filter *f = container_of(to_rcu_work(work),
  227. struct route4_filter,
  228. rwork);
  229. rtnl_lock();
  230. __route4_delete_filter(f);
  231. rtnl_unlock();
  232. }
  233. static void route4_queue_work(struct route4_filter *f)
  234. {
  235. tcf_queue_work(&f->rwork, route4_delete_filter_work);
  236. }
  237. static void route4_destroy(struct tcf_proto *tp, struct netlink_ext_ack *extack)
  238. {
  239. struct route4_head *head = rtnl_dereference(tp->root);
  240. int h1, h2;
  241. if (head == NULL)
  242. return;
  243. for (h1 = 0; h1 <= 256; h1++) {
  244. struct route4_bucket *b;
  245. b = rtnl_dereference(head->table[h1]);
  246. if (b) {
  247. for (h2 = 0; h2 <= 32; h2++) {
  248. struct route4_filter *f;
  249. while ((f = rtnl_dereference(b->ht[h2])) != NULL) {
  250. struct route4_filter *next;
  251. next = rtnl_dereference(f->next);
  252. RCU_INIT_POINTER(b->ht[h2], next);
  253. tcf_unbind_filter(tp, &f->res);
  254. if (tcf_exts_get_net(&f->exts))
  255. route4_queue_work(f);
  256. else
  257. __route4_delete_filter(f);
  258. }
  259. }
  260. RCU_INIT_POINTER(head->table[h1], NULL);
  261. kfree_rcu(b, rcu);
  262. }
  263. }
  264. kfree_rcu(head, rcu);
  265. }
  266. static int route4_delete(struct tcf_proto *tp, void *arg, bool *last,
  267. struct netlink_ext_ack *extack)
  268. {
  269. struct route4_head *head = rtnl_dereference(tp->root);
  270. struct route4_filter *f = arg;
  271. struct route4_filter __rcu **fp;
  272. struct route4_filter *nf;
  273. struct route4_bucket *b;
  274. unsigned int h = 0;
  275. int i, h1;
  276. if (!head || !f)
  277. return -EINVAL;
  278. h = f->handle;
  279. b = f->bkt;
  280. fp = &b->ht[from_hash(h >> 16)];
  281. for (nf = rtnl_dereference(*fp); nf;
  282. fp = &nf->next, nf = rtnl_dereference(*fp)) {
  283. if (nf == f) {
  284. /* unlink it */
  285. RCU_INIT_POINTER(*fp, rtnl_dereference(f->next));
  286. /* Remove any fastmap lookups that might ref filter
  287. * notice we unlink'd the filter so we can't get it
  288. * back in the fastmap.
  289. */
  290. route4_reset_fastmap(head);
  291. /* Delete it */
  292. tcf_unbind_filter(tp, &f->res);
  293. tcf_exts_get_net(&f->exts);
  294. tcf_queue_work(&f->rwork, route4_delete_filter_work);
  295. /* Strip RTNL protected tree */
  296. for (i = 0; i <= 32; i++) {
  297. struct route4_filter *rt;
  298. rt = rtnl_dereference(b->ht[i]);
  299. if (rt)
  300. goto out;
  301. }
  302. /* OK, session has no flows */
  303. RCU_INIT_POINTER(head->table[to_hash(h)], NULL);
  304. kfree_rcu(b, rcu);
  305. break;
  306. }
  307. }
  308. out:
  309. *last = true;
  310. for (h1 = 0; h1 <= 256; h1++) {
  311. if (rcu_access_pointer(head->table[h1])) {
  312. *last = false;
  313. break;
  314. }
  315. }
  316. return 0;
  317. }
  318. static const struct nla_policy route4_policy[TCA_ROUTE4_MAX + 1] = {
  319. [TCA_ROUTE4_CLASSID] = { .type = NLA_U32 },
  320. [TCA_ROUTE4_TO] = { .type = NLA_U32 },
  321. [TCA_ROUTE4_FROM] = { .type = NLA_U32 },
  322. [TCA_ROUTE4_IIF] = { .type = NLA_U32 },
  323. };
  324. static int route4_set_parms(struct net *net, struct tcf_proto *tp,
  325. unsigned long base, struct route4_filter *f,
  326. u32 handle, struct route4_head *head,
  327. struct nlattr **tb, struct nlattr *est, int new,
  328. bool ovr, struct netlink_ext_ack *extack)
  329. {
  330. u32 id = 0, to = 0, nhandle = 0x8000;
  331. struct route4_filter *fp;
  332. unsigned int h1;
  333. struct route4_bucket *b;
  334. int err;
  335. err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, extack);
  336. if (err < 0)
  337. return err;
  338. if (tb[TCA_ROUTE4_TO]) {
  339. if (new && handle & 0x8000)
  340. return -EINVAL;
  341. to = nla_get_u32(tb[TCA_ROUTE4_TO]);
  342. if (to > 0xFF)
  343. return -EINVAL;
  344. nhandle = to;
  345. }
  346. if (tb[TCA_ROUTE4_FROM]) {
  347. if (tb[TCA_ROUTE4_IIF])
  348. return -EINVAL;
  349. id = nla_get_u32(tb[TCA_ROUTE4_FROM]);
  350. if (id > 0xFF)
  351. return -EINVAL;
  352. nhandle |= id << 16;
  353. } else if (tb[TCA_ROUTE4_IIF]) {
  354. id = nla_get_u32(tb[TCA_ROUTE4_IIF]);
  355. if (id > 0x7FFF)
  356. return -EINVAL;
  357. nhandle |= (id | 0x8000) << 16;
  358. } else
  359. nhandle |= 0xFFFF << 16;
  360. if (handle && new) {
  361. nhandle |= handle & 0x7F00;
  362. if (nhandle != handle)
  363. return -EINVAL;
  364. }
  365. h1 = to_hash(nhandle);
  366. b = rtnl_dereference(head->table[h1]);
  367. if (!b) {
  368. b = kzalloc(sizeof(struct route4_bucket), GFP_KERNEL);
  369. if (b == NULL)
  370. return -ENOBUFS;
  371. rcu_assign_pointer(head->table[h1], b);
  372. } else {
  373. unsigned int h2 = from_hash(nhandle >> 16);
  374. for (fp = rtnl_dereference(b->ht[h2]);
  375. fp;
  376. fp = rtnl_dereference(fp->next))
  377. if (fp->handle == f->handle)
  378. return -EEXIST;
  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. return 0;
  394. }
  395. static int route4_change(struct net *net, struct sk_buff *in_skb,
  396. struct tcf_proto *tp, unsigned long base, u32 handle,
  397. struct nlattr **tca, void **arg, bool ovr,
  398. struct netlink_ext_ack *extack)
  399. {
  400. struct route4_head *head = rtnl_dereference(tp->root);
  401. struct route4_filter __rcu **fp;
  402. struct route4_filter *fold, *f1, *pfp, *f = NULL;
  403. struct route4_bucket *b;
  404. struct nlattr *opt = tca[TCA_OPTIONS];
  405. struct nlattr *tb[TCA_ROUTE4_MAX + 1];
  406. unsigned int h, th;
  407. int err;
  408. bool new = true;
  409. if (opt == NULL)
  410. return handle ? -EINVAL : 0;
  411. err = nla_parse_nested(tb, TCA_ROUTE4_MAX, opt, route4_policy, NULL);
  412. if (err < 0)
  413. return err;
  414. fold = *arg;
  415. if (fold && handle && fold->handle != handle)
  416. return -EINVAL;
  417. err = -ENOBUFS;
  418. f = kzalloc(sizeof(struct route4_filter), GFP_KERNEL);
  419. if (!f)
  420. goto errout;
  421. err = tcf_exts_init(&f->exts, TCA_ROUTE4_ACT, TCA_ROUTE4_POLICE);
  422. if (err < 0)
  423. goto errout;
  424. if (fold) {
  425. f->id = fold->id;
  426. f->iif = fold->iif;
  427. f->res = fold->res;
  428. f->handle = fold->handle;
  429. f->tp = fold->tp;
  430. f->bkt = fold->bkt;
  431. new = false;
  432. }
  433. err = route4_set_parms(net, tp, base, f, handle, head, tb,
  434. tca[TCA_RATE], new, ovr, extack);
  435. if (err < 0)
  436. goto errout;
  437. h = from_hash(f->handle >> 16);
  438. fp = &f->bkt->ht[h];
  439. for (pfp = rtnl_dereference(*fp);
  440. (f1 = rtnl_dereference(*fp)) != NULL;
  441. fp = &f1->next)
  442. if (f->handle < f1->handle)
  443. break;
  444. tcf_block_netif_keep_dst(tp->chain->block);
  445. rcu_assign_pointer(f->next, f1);
  446. rcu_assign_pointer(*fp, f);
  447. if (fold && fold->handle && f->handle != fold->handle) {
  448. th = to_hash(fold->handle);
  449. h = from_hash(fold->handle >> 16);
  450. b = rtnl_dereference(head->table[th]);
  451. if (b) {
  452. fp = &b->ht[h];
  453. for (pfp = rtnl_dereference(*fp); pfp;
  454. fp = &pfp->next, pfp = rtnl_dereference(*fp)) {
  455. if (pfp == fold) {
  456. rcu_assign_pointer(*fp, fold->next);
  457. break;
  458. }
  459. }
  460. }
  461. }
  462. route4_reset_fastmap(head);
  463. *arg = f;
  464. if (fold) {
  465. tcf_unbind_filter(tp, &fold->res);
  466. tcf_exts_get_net(&fold->exts);
  467. tcf_queue_work(&fold->rwork, route4_delete_filter_work);
  468. }
  469. return 0;
  470. errout:
  471. if (f)
  472. tcf_exts_destroy(&f->exts);
  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, 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, void *fh,
  507. struct sk_buff *skb, struct tcmsg *t)
  508. {
  509. struct route4_filter *f = 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 void route4_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
  546. unsigned long base)
  547. {
  548. struct route4_filter *f = fh;
  549. if (f && f->res.classid == classid) {
  550. if (cl)
  551. __tcf_bind_filter(q, &f->res, base);
  552. else
  553. __tcf_unbind_filter(q, &f->res);
  554. }
  555. }
  556. static struct tcf_proto_ops cls_route4_ops __read_mostly = {
  557. .kind = "route",
  558. .classify = route4_classify,
  559. .init = route4_init,
  560. .destroy = route4_destroy,
  561. .get = route4_get,
  562. .change = route4_change,
  563. .delete = route4_delete,
  564. .walk = route4_walk,
  565. .dump = route4_dump,
  566. .bind_class = route4_bind_class,
  567. .owner = THIS_MODULE,
  568. };
  569. static int __init init_route4(void)
  570. {
  571. return register_tcf_proto_ops(&cls_route4_ops);
  572. }
  573. static void __exit exit_route4(void)
  574. {
  575. unregister_tcf_proto_ops(&cls_route4_ops);
  576. }
  577. module_init(init_route4)
  578. module_exit(exit_route4)
  579. MODULE_LICENSE("GPL");