cls_route.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/sched/cls_route.c ROUTE4 classifier.
  4. *
  5. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/slab.h>
  9. #include <linux/types.h>
  10. #include <linux/kernel.h>
  11. #include <linux/string.h>
  12. #include <linux/errno.h>
  13. #include <linux/skbuff.h>
  14. #include <net/dst.h>
  15. #include <net/route.h>
  16. #include <net/netlink.h>
  17. #include <net/act_api.h>
  18. #include <net/pkt_cls.h>
  19. #include <net/tc_wrapper.h>
  20. /*
  21. * 1. For now we assume that route tags < 256.
  22. * It allows to use direct table lookups, instead of hash tables.
  23. * 2. For now we assume that "from TAG" and "fromdev DEV" statements
  24. * are mutually exclusive.
  25. * 3. "to TAG from ANY" has higher priority, than "to ANY from XXX"
  26. */
  27. struct route4_fastmap {
  28. struct route4_filter *filter;
  29. u32 id;
  30. int iif;
  31. };
  32. struct route4_head {
  33. struct route4_fastmap fastmap[16];
  34. struct route4_bucket __rcu *table[256 + 1];
  35. struct rcu_head rcu;
  36. };
  37. struct route4_bucket {
  38. /* 16 FROM buckets + 16 IIF buckets + 1 wildcard bucket */
  39. struct route4_filter __rcu *ht[16 + 16 + 1];
  40. struct rcu_head rcu;
  41. };
  42. struct route4_filter {
  43. struct route4_filter __rcu *next;
  44. u32 id;
  45. int iif;
  46. struct tcf_result res;
  47. struct tcf_exts exts;
  48. u32 handle;
  49. struct route4_bucket *bkt;
  50. struct tcf_proto *tp;
  51. struct rcu_work rwork;
  52. };
  53. #define ROUTE4_FAILURE ((struct route4_filter *)(-1L))
  54. static inline int route4_fastmap_hash(u32 id, int iif)
  55. {
  56. return id & 0xF;
  57. }
  58. static DEFINE_SPINLOCK(fastmap_lock);
  59. static void
  60. route4_reset_fastmap(struct route4_head *head)
  61. {
  62. spin_lock_bh(&fastmap_lock);
  63. memset(head->fastmap, 0, sizeof(head->fastmap));
  64. spin_unlock_bh(&fastmap_lock);
  65. }
  66. static void
  67. route4_set_fastmap(struct route4_head *head, u32 id, int iif,
  68. struct route4_filter *f)
  69. {
  70. int h = route4_fastmap_hash(id, iif);
  71. /* fastmap updates must look atomic to aling id, iff, filter */
  72. spin_lock_bh(&fastmap_lock);
  73. head->fastmap[h].id = id;
  74. head->fastmap[h].iif = iif;
  75. head->fastmap[h].filter = f;
  76. spin_unlock_bh(&fastmap_lock);
  77. }
  78. static inline int route4_hash_to(u32 id)
  79. {
  80. return id & 0xFF;
  81. }
  82. static inline int route4_hash_from(u32 id)
  83. {
  84. return (id >> 16) & 0xF;
  85. }
  86. static inline int route4_hash_iif(int iif)
  87. {
  88. return 16 + ((iif >> 16) & 0xF);
  89. }
  90. static inline int route4_hash_wild(void)
  91. {
  92. return 32;
  93. }
  94. #define ROUTE4_APPLY_RESULT() \
  95. { \
  96. *res = f->res; \
  97. if (tcf_exts_has_actions(&f->exts)) { \
  98. int r = tcf_exts_exec(skb, &f->exts, res); \
  99. if (r < 0) { \
  100. dont_cache = 1; \
  101. continue; \
  102. } \
  103. return r; \
  104. } else if (!dont_cache) \
  105. route4_set_fastmap(head, id, iif, f); \
  106. return 0; \
  107. }
  108. TC_INDIRECT_SCOPE int route4_classify(struct sk_buff *skb,
  109. const struct tcf_proto *tp,
  110. struct tcf_result *res)
  111. {
  112. struct route4_head *head = rcu_dereference_bh(tp->root);
  113. struct dst_entry *dst;
  114. struct route4_bucket *b;
  115. struct route4_filter *f;
  116. u32 id, h;
  117. int iif, dont_cache = 0;
  118. dst = skb_dst(skb);
  119. if (!dst)
  120. goto failure;
  121. id = dst->tclassid;
  122. iif = inet_iif(skb);
  123. h = route4_fastmap_hash(id, iif);
  124. spin_lock(&fastmap_lock);
  125. if (id == head->fastmap[h].id &&
  126. iif == head->fastmap[h].iif &&
  127. (f = head->fastmap[h].filter) != NULL) {
  128. if (f == ROUTE4_FAILURE) {
  129. spin_unlock(&fastmap_lock);
  130. goto failure;
  131. }
  132. *res = f->res;
  133. spin_unlock(&fastmap_lock);
  134. return 0;
  135. }
  136. spin_unlock(&fastmap_lock);
  137. h = route4_hash_to(id);
  138. restart:
  139. b = rcu_dereference_bh(head->table[h]);
  140. if (b) {
  141. for (f = rcu_dereference_bh(b->ht[route4_hash_from(id)]);
  142. f;
  143. f = rcu_dereference_bh(f->next))
  144. if (f->id == id)
  145. ROUTE4_APPLY_RESULT();
  146. for (f = rcu_dereference_bh(b->ht[route4_hash_iif(iif)]);
  147. f;
  148. f = rcu_dereference_bh(f->next))
  149. if (f->iif == iif)
  150. ROUTE4_APPLY_RESULT();
  151. for (f = rcu_dereference_bh(b->ht[route4_hash_wild()]);
  152. f;
  153. f = rcu_dereference_bh(f->next))
  154. ROUTE4_APPLY_RESULT();
  155. }
  156. if (h < 256) {
  157. h = 256;
  158. id &= ~0xFFFF;
  159. goto restart;
  160. }
  161. if (!dont_cache)
  162. route4_set_fastmap(head, id, iif, ROUTE4_FAILURE);
  163. failure:
  164. return -1;
  165. }
  166. static inline u32 to_hash(u32 id)
  167. {
  168. u32 h = id & 0xFF;
  169. if (id & 0x8000)
  170. h += 256;
  171. return h;
  172. }
  173. static inline u32 from_hash(u32 id)
  174. {
  175. id &= 0xFFFF;
  176. if (id == 0xFFFF)
  177. return 32;
  178. if (!(id & 0x8000)) {
  179. if (id > 255)
  180. return 256;
  181. return id & 0xF;
  182. }
  183. return 16 + (id & 0xF);
  184. }
  185. static void *route4_get(struct tcf_proto *tp, u32 handle)
  186. {
  187. struct route4_head *head = rtnl_dereference(tp->root);
  188. struct route4_bucket *b;
  189. struct route4_filter *f;
  190. unsigned int h1, h2;
  191. h1 = to_hash(handle);
  192. if (h1 > 256)
  193. return NULL;
  194. h2 = from_hash(handle >> 16);
  195. if (h2 > 32)
  196. return NULL;
  197. b = rtnl_dereference(head->table[h1]);
  198. if (b) {
  199. for (f = rtnl_dereference(b->ht[h2]);
  200. f;
  201. f = rtnl_dereference(f->next))
  202. if (f->handle == handle)
  203. return f;
  204. }
  205. return NULL;
  206. }
  207. static int route4_init(struct tcf_proto *tp)
  208. {
  209. struct route4_head *head;
  210. head = kzalloc(sizeof(struct route4_head), GFP_KERNEL);
  211. if (head == NULL)
  212. return -ENOBUFS;
  213. rcu_assign_pointer(tp->root, head);
  214. return 0;
  215. }
  216. static void __route4_delete_filter(struct route4_filter *f)
  217. {
  218. tcf_exts_destroy(&f->exts);
  219. tcf_exts_put_net(&f->exts);
  220. kfree(f);
  221. }
  222. static void route4_delete_filter_work(struct work_struct *work)
  223. {
  224. struct route4_filter *f = container_of(to_rcu_work(work),
  225. struct route4_filter,
  226. rwork);
  227. rtnl_lock();
  228. __route4_delete_filter(f);
  229. rtnl_unlock();
  230. }
  231. static void route4_queue_work(struct route4_filter *f)
  232. {
  233. tcf_queue_work(&f->rwork, route4_delete_filter_work);
  234. }
  235. static void route4_destroy(struct tcf_proto *tp, bool rtnl_held,
  236. struct netlink_ext_ack *extack)
  237. {
  238. struct route4_head *head = rtnl_dereference(tp->root);
  239. int h1, h2;
  240. if (head == NULL)
  241. return;
  242. for (h1 = 0; h1 <= 256; h1++) {
  243. struct route4_bucket *b;
  244. b = rtnl_dereference(head->table[h1]);
  245. if (b) {
  246. for (h2 = 0; h2 <= 32; h2++) {
  247. struct route4_filter *f;
  248. while ((f = rtnl_dereference(b->ht[h2])) != NULL) {
  249. struct route4_filter *next;
  250. next = rtnl_dereference(f->next);
  251. RCU_INIT_POINTER(b->ht[h2], next);
  252. tcf_unbind_filter(tp, &f->res);
  253. if (tcf_exts_get_net(&f->exts))
  254. route4_queue_work(f);
  255. else
  256. __route4_delete_filter(f);
  257. }
  258. }
  259. RCU_INIT_POINTER(head->table[h1], NULL);
  260. kfree_rcu(b, rcu);
  261. }
  262. }
  263. kfree_rcu(head, rcu);
  264. }
  265. static int route4_delete(struct tcf_proto *tp, void *arg, bool *last,
  266. bool rtnl_held, struct netlink_ext_ack *extack)
  267. {
  268. struct route4_head *head = rtnl_dereference(tp->root);
  269. struct route4_filter *f = arg;
  270. struct route4_filter __rcu **fp;
  271. struct route4_filter *nf;
  272. struct route4_bucket *b;
  273. unsigned int h = 0;
  274. int i, h1;
  275. if (!head || !f)
  276. return -EINVAL;
  277. h = f->handle;
  278. b = f->bkt;
  279. fp = &b->ht[from_hash(h >> 16)];
  280. for (nf = rtnl_dereference(*fp); nf;
  281. fp = &nf->next, nf = rtnl_dereference(*fp)) {
  282. if (nf == f) {
  283. /* unlink it */
  284. RCU_INIT_POINTER(*fp, rtnl_dereference(f->next));
  285. /* Remove any fastmap lookups that might ref filter
  286. * notice we unlink'd the filter so we can't get it
  287. * back in the fastmap.
  288. */
  289. route4_reset_fastmap(head);
  290. /* Delete it */
  291. tcf_unbind_filter(tp, &f->res);
  292. tcf_exts_get_net(&f->exts);
  293. tcf_queue_work(&f->rwork, route4_delete_filter_work);
  294. /* Strip RTNL protected tree */
  295. for (i = 0; i <= 32; i++) {
  296. struct route4_filter *rt;
  297. rt = rtnl_dereference(b->ht[i]);
  298. if (rt)
  299. goto out;
  300. }
  301. /* OK, session has no flows */
  302. RCU_INIT_POINTER(head->table[to_hash(h)], NULL);
  303. kfree_rcu(b, rcu);
  304. break;
  305. }
  306. }
  307. out:
  308. *last = true;
  309. for (h1 = 0; h1 <= 256; h1++) {
  310. if (rcu_access_pointer(head->table[h1])) {
  311. *last = false;
  312. break;
  313. }
  314. }
  315. return 0;
  316. }
  317. static const struct nla_policy route4_policy[TCA_ROUTE4_MAX + 1] = {
  318. [TCA_ROUTE4_CLASSID] = { .type = NLA_U32 },
  319. [TCA_ROUTE4_TO] = NLA_POLICY_MAX(NLA_U32, 0xFF),
  320. [TCA_ROUTE4_FROM] = NLA_POLICY_MAX(NLA_U32, 0xFF),
  321. [TCA_ROUTE4_IIF] = NLA_POLICY_MAX(NLA_U32, 0x7FFF),
  322. };
  323. static int route4_set_parms(struct net *net, struct tcf_proto *tp,
  324. unsigned long base, struct route4_filter *f,
  325. u32 handle, struct route4_head *head,
  326. struct nlattr **tb, struct nlattr *est, int new,
  327. u32 flags, struct netlink_ext_ack *extack)
  328. {
  329. u32 id = 0, to = 0, nhandle = 0x8000;
  330. struct route4_filter *fp;
  331. unsigned int h1;
  332. struct route4_bucket *b;
  333. int err;
  334. err = tcf_exts_validate(net, tp, tb, est, &f->exts, flags, extack);
  335. if (err < 0)
  336. return err;
  337. if (tb[TCA_ROUTE4_TO]) {
  338. if (new && handle & 0x8000) {
  339. NL_SET_ERR_MSG(extack, "Invalid handle");
  340. return -EINVAL;
  341. }
  342. to = nla_get_u32(tb[TCA_ROUTE4_TO]);
  343. nhandle = to;
  344. }
  345. if (tb[TCA_ROUTE4_FROM] && tb[TCA_ROUTE4_IIF]) {
  346. NL_SET_ERR_MSG_ATTR(extack, tb[TCA_ROUTE4_FROM],
  347. "'from' and 'fromif' are mutually exclusive");
  348. return -EINVAL;
  349. }
  350. if (tb[TCA_ROUTE4_FROM]) {
  351. id = nla_get_u32(tb[TCA_ROUTE4_FROM]);
  352. nhandle |= id << 16;
  353. } else if (tb[TCA_ROUTE4_IIF]) {
  354. id = nla_get_u32(tb[TCA_ROUTE4_IIF]);
  355. nhandle |= (id | 0x8000) << 16;
  356. } else
  357. nhandle |= 0xFFFF << 16;
  358. if (handle && new) {
  359. nhandle |= handle & 0x7F00;
  360. if (nhandle != handle) {
  361. NL_SET_ERR_MSG_FMT(extack,
  362. "Handle mismatch constructed: %x (expected: %x)",
  363. handle, nhandle);
  364. return -EINVAL;
  365. }
  366. }
  367. if (!nhandle) {
  368. NL_SET_ERR_MSG(extack, "Replacing with handle of 0 is invalid");
  369. return -EINVAL;
  370. }
  371. h1 = to_hash(nhandle);
  372. b = rtnl_dereference(head->table[h1]);
  373. if (!b) {
  374. b = kzalloc(sizeof(struct route4_bucket), GFP_KERNEL);
  375. if (b == NULL)
  376. return -ENOBUFS;
  377. rcu_assign_pointer(head->table[h1], b);
  378. } else {
  379. unsigned int h2 = from_hash(nhandle >> 16);
  380. for (fp = rtnl_dereference(b->ht[h2]);
  381. fp;
  382. fp = rtnl_dereference(fp->next))
  383. if (fp->handle == f->handle)
  384. return -EEXIST;
  385. }
  386. if (tb[TCA_ROUTE4_TO])
  387. f->id = to;
  388. if (tb[TCA_ROUTE4_FROM])
  389. f->id = to | id<<16;
  390. else if (tb[TCA_ROUTE4_IIF])
  391. f->iif = id;
  392. f->handle = nhandle;
  393. f->bkt = b;
  394. f->tp = tp;
  395. if (tb[TCA_ROUTE4_CLASSID]) {
  396. f->res.classid = nla_get_u32(tb[TCA_ROUTE4_CLASSID]);
  397. tcf_bind_filter(tp, &f->res, base);
  398. }
  399. return 0;
  400. }
  401. static int route4_change(struct net *net, struct sk_buff *in_skb,
  402. struct tcf_proto *tp, unsigned long base, u32 handle,
  403. struct nlattr **tca, void **arg, u32 flags,
  404. struct netlink_ext_ack *extack)
  405. {
  406. struct route4_head *head = rtnl_dereference(tp->root);
  407. struct route4_filter __rcu **fp;
  408. struct route4_filter *fold, *f1, *pfp, *f = NULL;
  409. struct route4_bucket *b;
  410. struct nlattr *tb[TCA_ROUTE4_MAX + 1];
  411. unsigned int h, th;
  412. int err;
  413. bool new = true;
  414. if (!handle) {
  415. NL_SET_ERR_MSG(extack, "Creating with handle of 0 is invalid");
  416. return -EINVAL;
  417. }
  418. if (NL_REQ_ATTR_CHECK(extack, NULL, tca, TCA_OPTIONS)) {
  419. NL_SET_ERR_MSG_MOD(extack, "Missing options");
  420. return -EINVAL;
  421. }
  422. err = nla_parse_nested_deprecated(tb, TCA_ROUTE4_MAX, tca[TCA_OPTIONS],
  423. route4_policy, NULL);
  424. if (err < 0)
  425. return err;
  426. fold = *arg;
  427. if (fold && fold->handle != handle)
  428. return -EINVAL;
  429. err = -ENOBUFS;
  430. f = kzalloc(sizeof(struct route4_filter), GFP_KERNEL);
  431. if (!f)
  432. goto errout;
  433. err = tcf_exts_init(&f->exts, net, TCA_ROUTE4_ACT, TCA_ROUTE4_POLICE);
  434. if (err < 0)
  435. goto errout;
  436. if (fold) {
  437. f->id = fold->id;
  438. f->iif = fold->iif;
  439. f->handle = fold->handle;
  440. f->tp = fold->tp;
  441. f->bkt = fold->bkt;
  442. new = false;
  443. }
  444. err = route4_set_parms(net, tp, base, f, handle, head, tb,
  445. tca[TCA_RATE], new, flags, extack);
  446. if (err < 0)
  447. goto errout;
  448. h = from_hash(f->handle >> 16);
  449. fp = &f->bkt->ht[h];
  450. for (pfp = rtnl_dereference(*fp);
  451. (f1 = rtnl_dereference(*fp)) != NULL;
  452. fp = &f1->next)
  453. if (f->handle < f1->handle)
  454. break;
  455. tcf_block_netif_keep_dst(tp->chain->block);
  456. rcu_assign_pointer(f->next, f1);
  457. rcu_assign_pointer(*fp, f);
  458. if (fold) {
  459. th = to_hash(fold->handle);
  460. h = from_hash(fold->handle >> 16);
  461. b = rtnl_dereference(head->table[th]);
  462. if (b) {
  463. fp = &b->ht[h];
  464. for (pfp = rtnl_dereference(*fp); pfp;
  465. fp = &pfp->next, pfp = rtnl_dereference(*fp)) {
  466. if (pfp == fold) {
  467. rcu_assign_pointer(*fp, fold->next);
  468. break;
  469. }
  470. }
  471. }
  472. }
  473. route4_reset_fastmap(head);
  474. *arg = f;
  475. if (fold) {
  476. tcf_unbind_filter(tp, &fold->res);
  477. tcf_exts_get_net(&fold->exts);
  478. tcf_queue_work(&fold->rwork, route4_delete_filter_work);
  479. }
  480. return 0;
  481. errout:
  482. if (f)
  483. tcf_exts_destroy(&f->exts);
  484. kfree(f);
  485. return err;
  486. }
  487. static void route4_walk(struct tcf_proto *tp, struct tcf_walker *arg,
  488. bool rtnl_held)
  489. {
  490. struct route4_head *head = rtnl_dereference(tp->root);
  491. unsigned int h, h1;
  492. if (head == NULL || arg->stop)
  493. return;
  494. for (h = 0; h <= 256; h++) {
  495. struct route4_bucket *b = rtnl_dereference(head->table[h]);
  496. if (b) {
  497. for (h1 = 0; h1 <= 32; h1++) {
  498. struct route4_filter *f;
  499. for (f = rtnl_dereference(b->ht[h1]);
  500. f;
  501. f = rtnl_dereference(f->next)) {
  502. if (!tc_cls_stats_dump(tp, arg, f))
  503. return;
  504. }
  505. }
  506. }
  507. }
  508. }
  509. static int route4_dump(struct net *net, struct tcf_proto *tp, void *fh,
  510. struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
  511. {
  512. struct route4_filter *f = 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_noflag(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 void route4_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
  549. unsigned long base)
  550. {
  551. struct route4_filter *f = fh;
  552. tc_cls_bind_class(classid, cl, q, &f->res, base);
  553. }
  554. static struct tcf_proto_ops cls_route4_ops __read_mostly = {
  555. .kind = "route",
  556. .classify = route4_classify,
  557. .init = route4_init,
  558. .destroy = route4_destroy,
  559. .get = route4_get,
  560. .change = route4_change,
  561. .delete = route4_delete,
  562. .walk = route4_walk,
  563. .dump = route4_dump,
  564. .bind_class = route4_bind_class,
  565. .owner = THIS_MODULE,
  566. };
  567. MODULE_ALIAS_NET_CLS("route");
  568. static int __init init_route4(void)
  569. {
  570. return register_tcf_proto_ops(&cls_route4_ops);
  571. }
  572. static void __exit exit_route4(void)
  573. {
  574. unregister_tcf_proto_ops(&cls_route4_ops);
  575. }
  576. module_init(init_route4)
  577. module_exit(exit_route4)
  578. MODULE_DESCRIPTION("Routing table realm based TC classifier");
  579. MODULE_LICENSE("GPL");