ila_xlat.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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 = 0;
  315. rhashtable_walk_enter(&ilan->xlat.rhash_table, &iter);
  316. rhashtable_walk_start(&iter);
  317. for (;;) {
  318. ila = rhashtable_walk_next(&iter);
  319. if (IS_ERR(ila)) {
  320. if (PTR_ERR(ila) == -EAGAIN)
  321. continue;
  322. ret = PTR_ERR(ila);
  323. goto done;
  324. } else if (!ila) {
  325. break;
  326. }
  327. lock = lock_from_ila_map(ilan, ila);
  328. spin_lock(lock);
  329. ret = rhashtable_remove_fast(&ilan->xlat.rhash_table,
  330. &ila->node, rht_params);
  331. if (!ret)
  332. ila_free_node(ila);
  333. spin_unlock(lock);
  334. if (ret)
  335. break;
  336. }
  337. done:
  338. rhashtable_walk_stop(&iter);
  339. rhashtable_walk_exit(&iter);
  340. return ret;
  341. }
  342. static int ila_fill_info(struct ila_map *ila, struct sk_buff *msg)
  343. {
  344. if (nla_put_u64_64bit(msg, ILA_ATTR_LOCATOR,
  345. (__force u64)ila->xp.ip.locator.v64,
  346. ILA_ATTR_PAD) ||
  347. nla_put_u64_64bit(msg, ILA_ATTR_LOCATOR_MATCH,
  348. (__force u64)ila->xp.ip.locator_match.v64,
  349. ILA_ATTR_PAD) ||
  350. nla_put_s32(msg, ILA_ATTR_IFINDEX, ila->xp.ifindex) ||
  351. nla_put_u8(msg, ILA_ATTR_CSUM_MODE, ila->xp.ip.csum_mode) ||
  352. nla_put_u8(msg, ILA_ATTR_IDENT_TYPE, ila->xp.ip.ident_type))
  353. return -1;
  354. return 0;
  355. }
  356. static int ila_dump_info(struct ila_map *ila,
  357. u32 portid, u32 seq, u32 flags,
  358. struct sk_buff *skb, u8 cmd)
  359. {
  360. void *hdr;
  361. hdr = genlmsg_put(skb, portid, seq, &ila_nl_family, flags, cmd);
  362. if (!hdr)
  363. return -ENOMEM;
  364. if (ila_fill_info(ila, skb) < 0)
  365. goto nla_put_failure;
  366. genlmsg_end(skb, hdr);
  367. return 0;
  368. nla_put_failure:
  369. genlmsg_cancel(skb, hdr);
  370. return -EMSGSIZE;
  371. }
  372. int ila_xlat_nl_cmd_get_mapping(struct sk_buff *skb, struct genl_info *info)
  373. {
  374. struct net *net = genl_info_net(info);
  375. struct ila_net *ilan = net_generic(net, ila_net_id);
  376. struct sk_buff *msg;
  377. struct ila_xlat_params xp;
  378. struct ila_map *ila;
  379. int ret;
  380. ret = parse_nl_config(info, &xp);
  381. if (ret)
  382. return ret;
  383. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  384. if (!msg)
  385. return -ENOMEM;
  386. rcu_read_lock();
  387. ret = -ESRCH;
  388. ila = ila_lookup_by_params(&xp, ilan);
  389. if (ila) {
  390. ret = ila_dump_info(ila,
  391. info->snd_portid,
  392. info->snd_seq, 0, msg,
  393. info->genlhdr->cmd);
  394. }
  395. rcu_read_unlock();
  396. if (ret < 0)
  397. goto out_free;
  398. return genlmsg_reply(msg, info);
  399. out_free:
  400. nlmsg_free(msg);
  401. return ret;
  402. }
  403. struct ila_dump_iter {
  404. struct rhashtable_iter rhiter;
  405. int skip;
  406. };
  407. int ila_xlat_nl_dump_start(struct netlink_callback *cb)
  408. {
  409. struct net *net = sock_net(cb->skb->sk);
  410. struct ila_net *ilan = net_generic(net, ila_net_id);
  411. struct ila_dump_iter *iter;
  412. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  413. if (!iter)
  414. return -ENOMEM;
  415. rhashtable_walk_enter(&ilan->xlat.rhash_table, &iter->rhiter);
  416. iter->skip = 0;
  417. cb->args[0] = (long)iter;
  418. return 0;
  419. }
  420. int ila_xlat_nl_dump_done(struct netlink_callback *cb)
  421. {
  422. struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args[0];
  423. rhashtable_walk_exit(&iter->rhiter);
  424. kfree(iter);
  425. return 0;
  426. }
  427. int ila_xlat_nl_dump(struct sk_buff *skb, struct netlink_callback *cb)
  428. {
  429. struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args[0];
  430. struct rhashtable_iter *rhiter = &iter->rhiter;
  431. int skip = iter->skip;
  432. struct ila_map *ila;
  433. int ret;
  434. rhashtable_walk_start(rhiter);
  435. /* Get first entry */
  436. ila = rhashtable_walk_peek(rhiter);
  437. if (ila && !IS_ERR(ila) && skip) {
  438. /* Skip over visited entries */
  439. while (ila && skip) {
  440. /* Skip over any ila entries in this list that we
  441. * have already dumped.
  442. */
  443. ila = rcu_access_pointer(ila->next);
  444. skip--;
  445. }
  446. }
  447. skip = 0;
  448. for (;;) {
  449. if (IS_ERR(ila)) {
  450. ret = PTR_ERR(ila);
  451. if (ret == -EAGAIN) {
  452. /* Table has changed and iter has reset. Return
  453. * -EAGAIN to the application even if we have
  454. * written data to the skb. The application
  455. * needs to deal with this.
  456. */
  457. goto out_ret;
  458. } else {
  459. break;
  460. }
  461. } else if (!ila) {
  462. ret = 0;
  463. break;
  464. }
  465. while (ila) {
  466. ret = ila_dump_info(ila, NETLINK_CB(cb->skb).portid,
  467. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  468. skb, ILA_CMD_GET);
  469. if (ret)
  470. goto out;
  471. skip++;
  472. ila = rcu_access_pointer(ila->next);
  473. }
  474. skip = 0;
  475. ila = rhashtable_walk_next(rhiter);
  476. }
  477. out:
  478. iter->skip = skip;
  479. ret = (skb->len ? : ret);
  480. out_ret:
  481. rhashtable_walk_stop(rhiter);
  482. return ret;
  483. }
  484. int ila_xlat_init_net(struct net *net)
  485. {
  486. struct ila_net *ilan = net_generic(net, ila_net_id);
  487. int err;
  488. err = alloc_ila_locks(ilan);
  489. if (err)
  490. return err;
  491. err = rhashtable_init(&ilan->xlat.rhash_table, &rht_params);
  492. if (err) {
  493. free_bucket_spinlocks(ilan->xlat.locks);
  494. return err;
  495. }
  496. return 0;
  497. }
  498. void ila_xlat_exit_net(struct net *net)
  499. {
  500. struct ila_net *ilan = net_generic(net, ila_net_id);
  501. rhashtable_free_and_destroy(&ilan->xlat.rhash_table, ila_free_cb, NULL);
  502. free_bucket_spinlocks(ilan->xlat.locks);
  503. if (ilan->xlat.hooks_registered)
  504. nf_unregister_net_hooks(net, ila_nf_hook_ops,
  505. ARRAY_SIZE(ila_nf_hook_ops));
  506. }
  507. static int ila_xlat_addr(struct sk_buff *skb, bool sir2ila)
  508. {
  509. struct ila_map *ila;
  510. struct ipv6hdr *ip6h = ipv6_hdr(skb);
  511. struct net *net = dev_net(skb->dev);
  512. struct ila_net *ilan = net_generic(net, ila_net_id);
  513. struct ila_addr *iaddr = ila_a2i(&ip6h->daddr);
  514. /* Assumes skb contains a valid IPv6 header that is pulled */
  515. /* No check here that ILA type in the mapping matches what is in the
  516. * address. We assume that whatever sender gaves us can be translated.
  517. * The checksum mode however is relevant.
  518. */
  519. rcu_read_lock();
  520. ila = ila_lookup_wildcards(iaddr, skb->dev->ifindex, ilan);
  521. if (ila)
  522. ila_update_ipv6_locator(skb, &ila->xp.ip, sir2ila);
  523. rcu_read_unlock();
  524. return 0;
  525. }