ila_xlat.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/jhash.h>
  3. #include <linux/netfilter.h>
  4. #include <linux/rcupdate.h>
  5. #include <linux/rhashtable.h>
  6. #include <linux/vmalloc.h>
  7. #include <net/genetlink.h>
  8. #include <net/ila.h>
  9. #include <net/netns/generic.h>
  10. #include <uapi/linux/genetlink.h>
  11. #include "ila.h"
  12. struct ila_xlat_params {
  13. struct ila_params ip;
  14. int ifindex;
  15. };
  16. struct ila_map {
  17. struct ila_xlat_params xp;
  18. struct rhash_head node;
  19. struct ila_map __rcu *next;
  20. struct rcu_head rcu;
  21. };
  22. #define MAX_LOCKS 1024
  23. #define LOCKS_PER_CPU 10
  24. static int alloc_ila_locks(struct ila_net *ilan)
  25. {
  26. return alloc_bucket_spinlocks(&ilan->xlat.locks, &ilan->xlat.locks_mask,
  27. MAX_LOCKS, LOCKS_PER_CPU,
  28. GFP_KERNEL);
  29. }
  30. static u32 hashrnd __read_mostly;
  31. static __always_inline void __ila_hash_secret_init(void)
  32. {
  33. net_get_random_once(&hashrnd, sizeof(hashrnd));
  34. }
  35. static inline u32 ila_locator_hash(struct ila_locator loc)
  36. {
  37. u32 *v = (u32 *)loc.v32;
  38. __ila_hash_secret_init();
  39. return jhash_2words(v[0], v[1], hashrnd);
  40. }
  41. static inline spinlock_t *ila_get_lock(struct ila_net *ilan,
  42. struct ila_locator loc)
  43. {
  44. return &ilan->xlat.locks[ila_locator_hash(loc) & ilan->xlat.locks_mask];
  45. }
  46. static inline int ila_cmp_wildcards(struct ila_map *ila,
  47. struct ila_addr *iaddr, int ifindex)
  48. {
  49. return (ila->xp.ifindex && ila->xp.ifindex != ifindex);
  50. }
  51. static inline int ila_cmp_params(struct ila_map *ila,
  52. struct ila_xlat_params *xp)
  53. {
  54. return (ila->xp.ifindex != xp->ifindex);
  55. }
  56. static int ila_cmpfn(struct rhashtable_compare_arg *arg,
  57. const void *obj)
  58. {
  59. const struct ila_map *ila = obj;
  60. return (ila->xp.ip.locator_match.v64 != *(__be64 *)arg->key);
  61. }
  62. static inline int ila_order(struct ila_map *ila)
  63. {
  64. int score = 0;
  65. if (ila->xp.ifindex)
  66. score += 1 << 1;
  67. return score;
  68. }
  69. static const struct rhashtable_params rht_params = {
  70. .nelem_hint = 1024,
  71. .head_offset = offsetof(struct ila_map, node),
  72. .key_offset = offsetof(struct ila_map, xp.ip.locator_match),
  73. .key_len = sizeof(u64), /* identifier */
  74. .max_size = 1048576,
  75. .min_size = 256,
  76. .automatic_shrinking = true,
  77. .obj_cmpfn = ila_cmpfn,
  78. };
  79. static int parse_nl_config(struct genl_info *info,
  80. struct ila_xlat_params *xp)
  81. {
  82. memset(xp, 0, sizeof(*xp));
  83. if (info->attrs[ILA_ATTR_LOCATOR])
  84. xp->ip.locator.v64 = (__force __be64)nla_get_u64(
  85. info->attrs[ILA_ATTR_LOCATOR]);
  86. if (info->attrs[ILA_ATTR_LOCATOR_MATCH])
  87. xp->ip.locator_match.v64 = (__force __be64)nla_get_u64(
  88. info->attrs[ILA_ATTR_LOCATOR_MATCH]);
  89. if (info->attrs[ILA_ATTR_CSUM_MODE])
  90. xp->ip.csum_mode = nla_get_u8(info->attrs[ILA_ATTR_CSUM_MODE]);
  91. else
  92. xp->ip.csum_mode = ILA_CSUM_NO_ACTION;
  93. if (info->attrs[ILA_ATTR_IDENT_TYPE])
  94. xp->ip.ident_type = nla_get_u8(
  95. info->attrs[ILA_ATTR_IDENT_TYPE]);
  96. else
  97. xp->ip.ident_type = ILA_ATYPE_USE_FORMAT;
  98. if (info->attrs[ILA_ATTR_IFINDEX])
  99. xp->ifindex = nla_get_s32(info->attrs[ILA_ATTR_IFINDEX]);
  100. return 0;
  101. }
  102. /* Must be called with rcu readlock */
  103. static inline struct ila_map *ila_lookup_wildcards(struct ila_addr *iaddr,
  104. int ifindex,
  105. struct ila_net *ilan)
  106. {
  107. struct ila_map *ila;
  108. ila = rhashtable_lookup_fast(&ilan->xlat.rhash_table, &iaddr->loc,
  109. rht_params);
  110. while (ila) {
  111. if (!ila_cmp_wildcards(ila, iaddr, ifindex))
  112. return ila;
  113. ila = rcu_access_pointer(ila->next);
  114. }
  115. return NULL;
  116. }
  117. /* Must be called with rcu readlock */
  118. static inline struct ila_map *ila_lookup_by_params(struct ila_xlat_params *xp,
  119. struct ila_net *ilan)
  120. {
  121. struct ila_map *ila;
  122. ila = rhashtable_lookup_fast(&ilan->xlat.rhash_table,
  123. &xp->ip.locator_match,
  124. rht_params);
  125. while (ila) {
  126. if (!ila_cmp_params(ila, xp))
  127. return ila;
  128. ila = rcu_access_pointer(ila->next);
  129. }
  130. return NULL;
  131. }
  132. static inline void ila_release(struct ila_map *ila)
  133. {
  134. kfree_rcu(ila, rcu);
  135. }
  136. static void ila_free_node(struct ila_map *ila)
  137. {
  138. struct ila_map *next;
  139. /* Assume rcu_readlock held */
  140. while (ila) {
  141. next = rcu_access_pointer(ila->next);
  142. ila_release(ila);
  143. ila = next;
  144. }
  145. }
  146. static void ila_free_cb(void *ptr, void *arg)
  147. {
  148. ila_free_node((struct ila_map *)ptr);
  149. }
  150. static int ila_xlat_addr(struct sk_buff *skb, bool sir2ila);
  151. static unsigned int
  152. ila_nf_input(void *priv,
  153. struct sk_buff *skb,
  154. const struct nf_hook_state *state)
  155. {
  156. ila_xlat_addr(skb, false);
  157. return NF_ACCEPT;
  158. }
  159. static const struct nf_hook_ops ila_nf_hook_ops[] = {
  160. {
  161. .hook = ila_nf_input,
  162. .pf = NFPROTO_IPV6,
  163. .hooknum = NF_INET_PRE_ROUTING,
  164. .priority = -1,
  165. },
  166. };
  167. static int ila_add_mapping(struct net *net, struct ila_xlat_params *xp)
  168. {
  169. struct ila_net *ilan = net_generic(net, ila_net_id);
  170. struct ila_map *ila, *head;
  171. spinlock_t *lock = ila_get_lock(ilan, xp->ip.locator_match);
  172. int err = 0, order;
  173. if (!ilan->xlat.hooks_registered) {
  174. /* We defer registering net hooks in the namespace until the
  175. * first mapping is added.
  176. */
  177. err = nf_register_net_hooks(net, ila_nf_hook_ops,
  178. ARRAY_SIZE(ila_nf_hook_ops));
  179. if (err)
  180. return err;
  181. ilan->xlat.hooks_registered = true;
  182. }
  183. ila = kzalloc(sizeof(*ila), GFP_KERNEL);
  184. if (!ila)
  185. return -ENOMEM;
  186. ila_init_saved_csum(&xp->ip);
  187. ila->xp = *xp;
  188. order = ila_order(ila);
  189. spin_lock(lock);
  190. head = rhashtable_lookup_fast(&ilan->xlat.rhash_table,
  191. &xp->ip.locator_match,
  192. rht_params);
  193. if (!head) {
  194. /* New entry for the rhash_table */
  195. err = rhashtable_lookup_insert_fast(&ilan->xlat.rhash_table,
  196. &ila->node, rht_params);
  197. } else {
  198. struct ila_map *tila = head, *prev = NULL;
  199. do {
  200. if (!ila_cmp_params(tila, xp)) {
  201. err = -EEXIST;
  202. goto out;
  203. }
  204. if (order > ila_order(tila))
  205. break;
  206. prev = tila;
  207. tila = rcu_dereference_protected(tila->next,
  208. lockdep_is_held(lock));
  209. } while (tila);
  210. if (prev) {
  211. /* Insert in sub list of head */
  212. RCU_INIT_POINTER(ila->next, tila);
  213. rcu_assign_pointer(prev->next, ila);
  214. } else {
  215. /* Make this ila new head */
  216. RCU_INIT_POINTER(ila->next, head);
  217. err = rhashtable_replace_fast(&ilan->xlat.rhash_table,
  218. &head->node,
  219. &ila->node, rht_params);
  220. if (err)
  221. goto out;
  222. }
  223. }
  224. out:
  225. spin_unlock(lock);
  226. if (err)
  227. kfree(ila);
  228. return err;
  229. }
  230. static int ila_del_mapping(struct net *net, struct ila_xlat_params *xp)
  231. {
  232. struct ila_net *ilan = net_generic(net, ila_net_id);
  233. struct ila_map *ila, *head, *prev;
  234. spinlock_t *lock = ila_get_lock(ilan, xp->ip.locator_match);
  235. int err = -ENOENT;
  236. spin_lock(lock);
  237. head = rhashtable_lookup_fast(&ilan->xlat.rhash_table,
  238. &xp->ip.locator_match, rht_params);
  239. ila = head;
  240. prev = NULL;
  241. while (ila) {
  242. if (ila_cmp_params(ila, xp)) {
  243. prev = ila;
  244. ila = rcu_dereference_protected(ila->next,
  245. lockdep_is_held(lock));
  246. continue;
  247. }
  248. err = 0;
  249. if (prev) {
  250. /* Not head, just delete from list */
  251. rcu_assign_pointer(prev->next, ila->next);
  252. } else {
  253. /* It is the head. If there is something in the
  254. * sublist we need to make a new head.
  255. */
  256. head = rcu_dereference_protected(ila->next,
  257. lockdep_is_held(lock));
  258. if (head) {
  259. /* Put first entry in the sublist into the
  260. * table
  261. */
  262. err = rhashtable_replace_fast(
  263. &ilan->xlat.rhash_table, &ila->node,
  264. &head->node, rht_params);
  265. if (err)
  266. goto out;
  267. } else {
  268. /* Entry no longer used */
  269. err = rhashtable_remove_fast(
  270. &ilan->xlat.rhash_table,
  271. &ila->node, rht_params);
  272. }
  273. }
  274. ila_release(ila);
  275. break;
  276. }
  277. out:
  278. spin_unlock(lock);
  279. return err;
  280. }
  281. int ila_xlat_nl_cmd_add_mapping(struct sk_buff *skb, struct genl_info *info)
  282. {
  283. struct net *net = genl_info_net(info);
  284. struct ila_xlat_params p;
  285. int err;
  286. err = parse_nl_config(info, &p);
  287. if (err)
  288. return err;
  289. return ila_add_mapping(net, &p);
  290. }
  291. int ila_xlat_nl_cmd_del_mapping(struct sk_buff *skb, struct genl_info *info)
  292. {
  293. struct net *net = genl_info_net(info);
  294. struct ila_xlat_params xp;
  295. int err;
  296. err = parse_nl_config(info, &xp);
  297. if (err)
  298. return err;
  299. ila_del_mapping(net, &xp);
  300. return 0;
  301. }
  302. static inline spinlock_t *lock_from_ila_map(struct ila_net *ilan,
  303. struct ila_map *ila)
  304. {
  305. return ila_get_lock(ilan, ila->xp.ip.locator_match);
  306. }
  307. int ila_xlat_nl_cmd_flush(struct sk_buff *skb, struct genl_info *info)
  308. {
  309. struct net *net = genl_info_net(info);
  310. struct ila_net *ilan = net_generic(net, ila_net_id);
  311. struct rhashtable_iter iter;
  312. struct ila_map *ila;
  313. spinlock_t *lock;
  314. int ret;
  315. ret = rhashtable_walk_init(&ilan->xlat.rhash_table, &iter, GFP_KERNEL);
  316. if (ret)
  317. goto done;
  318. rhashtable_walk_start(&iter);
  319. for (;;) {
  320. ila = rhashtable_walk_next(&iter);
  321. if (IS_ERR(ila)) {
  322. if (PTR_ERR(ila) == -EAGAIN)
  323. continue;
  324. ret = PTR_ERR(ila);
  325. goto done;
  326. } else if (!ila) {
  327. break;
  328. }
  329. lock = lock_from_ila_map(ilan, ila);
  330. spin_lock(lock);
  331. ret = rhashtable_remove_fast(&ilan->xlat.rhash_table,
  332. &ila->node, rht_params);
  333. if (!ret)
  334. ila_free_node(ila);
  335. spin_unlock(lock);
  336. if (ret)
  337. break;
  338. }
  339. done:
  340. rhashtable_walk_stop(&iter);
  341. rhashtable_walk_exit(&iter);
  342. return ret;
  343. }
  344. static int ila_fill_info(struct ila_map *ila, struct sk_buff *msg)
  345. {
  346. if (nla_put_u64_64bit(msg, ILA_ATTR_LOCATOR,
  347. (__force u64)ila->xp.ip.locator.v64,
  348. ILA_ATTR_PAD) ||
  349. nla_put_u64_64bit(msg, ILA_ATTR_LOCATOR_MATCH,
  350. (__force u64)ila->xp.ip.locator_match.v64,
  351. ILA_ATTR_PAD) ||
  352. nla_put_s32(msg, ILA_ATTR_IFINDEX, ila->xp.ifindex) ||
  353. nla_put_u8(msg, ILA_ATTR_CSUM_MODE, ila->xp.ip.csum_mode) ||
  354. nla_put_u8(msg, ILA_ATTR_IDENT_TYPE, ila->xp.ip.ident_type))
  355. return -1;
  356. return 0;
  357. }
  358. static int ila_dump_info(struct ila_map *ila,
  359. u32 portid, u32 seq, u32 flags,
  360. struct sk_buff *skb, u8 cmd)
  361. {
  362. void *hdr;
  363. hdr = genlmsg_put(skb, portid, seq, &ila_nl_family, flags, cmd);
  364. if (!hdr)
  365. return -ENOMEM;
  366. if (ila_fill_info(ila, skb) < 0)
  367. goto nla_put_failure;
  368. genlmsg_end(skb, hdr);
  369. return 0;
  370. nla_put_failure:
  371. genlmsg_cancel(skb, hdr);
  372. return -EMSGSIZE;
  373. }
  374. int ila_xlat_nl_cmd_get_mapping(struct sk_buff *skb, struct genl_info *info)
  375. {
  376. struct net *net = genl_info_net(info);
  377. struct ila_net *ilan = net_generic(net, ila_net_id);
  378. struct sk_buff *msg;
  379. struct ila_xlat_params xp;
  380. struct ila_map *ila;
  381. int ret;
  382. ret = parse_nl_config(info, &xp);
  383. if (ret)
  384. return ret;
  385. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  386. if (!msg)
  387. return -ENOMEM;
  388. rcu_read_lock();
  389. ila = ila_lookup_by_params(&xp, ilan);
  390. if (ila) {
  391. ret = ila_dump_info(ila,
  392. info->snd_portid,
  393. info->snd_seq, 0, msg,
  394. info->genlhdr->cmd);
  395. }
  396. rcu_read_unlock();
  397. if (ret < 0)
  398. goto out_free;
  399. return genlmsg_reply(msg, info);
  400. out_free:
  401. nlmsg_free(msg);
  402. return ret;
  403. }
  404. struct ila_dump_iter {
  405. struct rhashtable_iter rhiter;
  406. int skip;
  407. };
  408. int ila_xlat_nl_dump_start(struct netlink_callback *cb)
  409. {
  410. struct net *net = sock_net(cb->skb->sk);
  411. struct ila_net *ilan = net_generic(net, ila_net_id);
  412. struct ila_dump_iter *iter;
  413. int ret;
  414. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  415. if (!iter)
  416. return -ENOMEM;
  417. ret = rhashtable_walk_init(&ilan->xlat.rhash_table, &iter->rhiter,
  418. GFP_KERNEL);
  419. if (ret) {
  420. kfree(iter);
  421. return ret;
  422. }
  423. iter->skip = 0;
  424. cb->args[0] = (long)iter;
  425. return ret;
  426. }
  427. int ila_xlat_nl_dump_done(struct netlink_callback *cb)
  428. {
  429. struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args[0];
  430. rhashtable_walk_exit(&iter->rhiter);
  431. kfree(iter);
  432. return 0;
  433. }
  434. int ila_xlat_nl_dump(struct sk_buff *skb, struct netlink_callback *cb)
  435. {
  436. struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args[0];
  437. struct rhashtable_iter *rhiter = &iter->rhiter;
  438. int skip = iter->skip;
  439. struct ila_map *ila;
  440. int ret;
  441. rhashtable_walk_start(rhiter);
  442. /* Get first entry */
  443. ila = rhashtable_walk_peek(rhiter);
  444. if (ila && !IS_ERR(ila) && skip) {
  445. /* Skip over visited entries */
  446. while (ila && skip) {
  447. /* Skip over any ila entries in this list that we
  448. * have already dumped.
  449. */
  450. ila = rcu_access_pointer(ila->next);
  451. skip--;
  452. }
  453. }
  454. skip = 0;
  455. for (;;) {
  456. if (IS_ERR(ila)) {
  457. ret = PTR_ERR(ila);
  458. if (ret == -EAGAIN) {
  459. /* Table has changed and iter has reset. Return
  460. * -EAGAIN to the application even if we have
  461. * written data to the skb. The application
  462. * needs to deal with this.
  463. */
  464. goto out_ret;
  465. } else {
  466. break;
  467. }
  468. } else if (!ila) {
  469. ret = 0;
  470. break;
  471. }
  472. while (ila) {
  473. ret = ila_dump_info(ila, NETLINK_CB(cb->skb).portid,
  474. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  475. skb, ILA_CMD_GET);
  476. if (ret)
  477. goto out;
  478. skip++;
  479. ila = rcu_access_pointer(ila->next);
  480. }
  481. skip = 0;
  482. ila = rhashtable_walk_next(rhiter);
  483. }
  484. out:
  485. iter->skip = skip;
  486. ret = (skb->len ? : ret);
  487. out_ret:
  488. rhashtable_walk_stop(rhiter);
  489. return ret;
  490. }
  491. #define ILA_HASH_TABLE_SIZE 1024
  492. int ila_xlat_init_net(struct net *net)
  493. {
  494. struct ila_net *ilan = net_generic(net, ila_net_id);
  495. int err;
  496. err = alloc_ila_locks(ilan);
  497. if (err)
  498. return err;
  499. rhashtable_init(&ilan->xlat.rhash_table, &rht_params);
  500. return 0;
  501. }
  502. void ila_xlat_exit_net(struct net *net)
  503. {
  504. struct ila_net *ilan = net_generic(net, ila_net_id);
  505. rhashtable_free_and_destroy(&ilan->xlat.rhash_table, ila_free_cb, NULL);
  506. free_bucket_spinlocks(ilan->xlat.locks);
  507. if (ilan->xlat.hooks_registered)
  508. nf_unregister_net_hooks(net, ila_nf_hook_ops,
  509. ARRAY_SIZE(ila_nf_hook_ops));
  510. }
  511. static int ila_xlat_addr(struct sk_buff *skb, bool sir2ila)
  512. {
  513. struct ila_map *ila;
  514. struct ipv6hdr *ip6h = ipv6_hdr(skb);
  515. struct net *net = dev_net(skb->dev);
  516. struct ila_net *ilan = net_generic(net, ila_net_id);
  517. struct ila_addr *iaddr = ila_a2i(&ip6h->daddr);
  518. /* Assumes skb contains a valid IPv6 header that is pulled */
  519. /* No check here that ILA type in the mapping matches what is in the
  520. * address. We assume that whatever sender gaves us can be translated.
  521. * The checksum mode however is relevant.
  522. */
  523. rcu_read_lock();
  524. ila = ila_lookup_wildcards(iaddr, skb->dev->ifindex, ilan);
  525. if (ila)
  526. ila_update_ipv6_locator(skb, &ila->xp.ip, sir2ila);
  527. rcu_read_unlock();
  528. return 0;
  529. }