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>
9 #include <net/netns/generic.h>
10 #include <uapi/linux/genetlink.h>
13 struct ila_xlat_params {
19 struct ila_xlat_params xp;
20 struct rhash_head node;
21 struct ila_map __rcu *next;
25 static unsigned int ila_net_id;
28 struct rhashtable rhash_table;
29 spinlock_t *locks; /* Bucket locks for entry manipulation */
30 unsigned int locks_mask;
31 bool hooks_registered;
34 #define LOCKS_PER_CPU 10
36 static int alloc_ila_locks(struct ila_net *ilan)
39 unsigned int nr_pcpus = num_possible_cpus();
41 nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL);
42 size = roundup_pow_of_two(nr_pcpus * LOCKS_PER_CPU);
44 if (sizeof(spinlock_t) != 0) {
45 ilan->locks = kvmalloc_array(size, sizeof(spinlock_t),
49 for (i = 0; i < size; i++)
50 spin_lock_init(&ilan->locks[i]);
52 ilan->locks_mask = size - 1;
57 static u32 hashrnd __read_mostly;
58 static __always_inline void __ila_hash_secret_init(void)
60 net_get_random_once(&hashrnd, sizeof(hashrnd));
63 static inline u32 ila_locator_hash(struct ila_locator loc)
65 u32 *v = (u32 *)loc.v32;
67 __ila_hash_secret_init();
68 return jhash_2words(v[0], v[1], hashrnd);
71 static inline spinlock_t *ila_get_lock(struct ila_net *ilan,
72 struct ila_locator loc)
74 return &ilan->locks[ila_locator_hash(loc) & ilan->locks_mask];
77 static inline int ila_cmp_wildcards(struct ila_map *ila,
78 struct ila_addr *iaddr, int ifindex)
80 return (ila->xp.ifindex && ila->xp.ifindex != ifindex);
83 static inline int ila_cmp_params(struct ila_map *ila,
84 struct ila_xlat_params *xp)
86 return (ila->xp.ifindex != xp->ifindex);
89 static int ila_cmpfn(struct rhashtable_compare_arg *arg,
92 const struct ila_map *ila = obj;
94 return (ila->xp.ip.locator_match.v64 != *(__be64 *)arg->key);
97 static inline int ila_order(struct ila_map *ila)
107 static const struct rhashtable_params rht_params = {
109 .head_offset = offsetof(struct ila_map, node),
110 .key_offset = offsetof(struct ila_map, xp.ip.locator_match),
111 .key_len = sizeof(u64), /* identifier */
114 .automatic_shrinking = true,
115 .obj_cmpfn = ila_cmpfn,
118 static struct genl_family ila_nl_family;
120 static const struct nla_policy ila_nl_policy[ILA_ATTR_MAX + 1] = {
121 [ILA_ATTR_LOCATOR] = { .type = NLA_U64, },
122 [ILA_ATTR_LOCATOR_MATCH] = { .type = NLA_U64, },
123 [ILA_ATTR_IFINDEX] = { .type = NLA_U32, },
124 [ILA_ATTR_CSUM_MODE] = { .type = NLA_U8, },
125 [ILA_ATTR_IDENT_TYPE] = { .type = NLA_U8, },
128 static int parse_nl_config(struct genl_info *info,
129 struct ila_xlat_params *xp)
131 memset(xp, 0, sizeof(*xp));
133 if (info->attrs[ILA_ATTR_LOCATOR])
134 xp->ip.locator.v64 = (__force __be64)nla_get_u64(
135 info->attrs[ILA_ATTR_LOCATOR]);
137 if (info->attrs[ILA_ATTR_LOCATOR_MATCH])
138 xp->ip.locator_match.v64 = (__force __be64)nla_get_u64(
139 info->attrs[ILA_ATTR_LOCATOR_MATCH]);
141 if (info->attrs[ILA_ATTR_CSUM_MODE])
142 xp->ip.csum_mode = nla_get_u8(info->attrs[ILA_ATTR_CSUM_MODE]);
144 xp->ip.csum_mode = ILA_CSUM_NO_ACTION;
146 if (info->attrs[ILA_ATTR_IDENT_TYPE])
147 xp->ip.ident_type = nla_get_u8(
148 info->attrs[ILA_ATTR_IDENT_TYPE]);
150 xp->ip.ident_type = ILA_ATYPE_USE_FORMAT;
152 if (info->attrs[ILA_ATTR_IFINDEX])
153 xp->ifindex = nla_get_s32(info->attrs[ILA_ATTR_IFINDEX]);
158 /* Must be called with rcu readlock */
159 static inline struct ila_map *ila_lookup_wildcards(struct ila_addr *iaddr,
161 struct ila_net *ilan)
165 ila = rhashtable_lookup_fast(&ilan->rhash_table, &iaddr->loc,
168 if (!ila_cmp_wildcards(ila, iaddr, ifindex))
170 ila = rcu_access_pointer(ila->next);
176 /* Must be called with rcu readlock */
177 static inline struct ila_map *ila_lookup_by_params(struct ila_xlat_params *xp,
178 struct ila_net *ilan)
182 ila = rhashtable_lookup_fast(&ilan->rhash_table,
183 &xp->ip.locator_match,
186 if (!ila_cmp_params(ila, xp))
188 ila = rcu_access_pointer(ila->next);
194 static inline void ila_release(struct ila_map *ila)
199 static void ila_free_cb(void *ptr, void *arg)
201 struct ila_map *ila = (struct ila_map *)ptr, *next;
203 /* Assume rcu_readlock held */
205 next = rcu_access_pointer(ila->next);
211 static int ila_xlat_addr(struct sk_buff *skb, bool sir2ila);
214 ila_nf_input(void *priv,
216 const struct nf_hook_state *state)
218 ila_xlat_addr(skb, false);
222 static const struct nf_hook_ops ila_nf_hook_ops[] = {
224 .hook = ila_nf_input,
226 .hooknum = NF_INET_PRE_ROUTING,
231 static int ila_add_mapping(struct net *net, struct ila_xlat_params *xp)
233 struct ila_net *ilan = net_generic(net, ila_net_id);
234 struct ila_map *ila, *head;
235 spinlock_t *lock = ila_get_lock(ilan, xp->ip.locator_match);
238 if (!ilan->hooks_registered) {
239 /* We defer registering net hooks in the namespace until the
240 * first mapping is added.
242 err = nf_register_net_hooks(net, ila_nf_hook_ops,
243 ARRAY_SIZE(ila_nf_hook_ops));
247 ilan->hooks_registered = true;
250 ila = kzalloc(sizeof(*ila), GFP_KERNEL);
254 ila_init_saved_csum(&xp->ip);
258 order = ila_order(ila);
262 head = rhashtable_lookup_fast(&ilan->rhash_table,
263 &xp->ip.locator_match,
266 /* New entry for the rhash_table */
267 err = rhashtable_lookup_insert_fast(&ilan->rhash_table,
268 &ila->node, rht_params);
270 struct ila_map *tila = head, *prev = NULL;
273 if (!ila_cmp_params(tila, xp)) {
278 if (order > ila_order(tila))
282 tila = rcu_dereference_protected(tila->next,
283 lockdep_is_held(lock));
287 /* Insert in sub list of head */
288 RCU_INIT_POINTER(ila->next, tila);
289 rcu_assign_pointer(prev->next, ila);
291 /* Make this ila new head */
292 RCU_INIT_POINTER(ila->next, head);
293 err = rhashtable_replace_fast(&ilan->rhash_table,
295 &ila->node, rht_params);
310 static int ila_del_mapping(struct net *net, struct ila_xlat_params *xp)
312 struct ila_net *ilan = net_generic(net, ila_net_id);
313 struct ila_map *ila, *head, *prev;
314 spinlock_t *lock = ila_get_lock(ilan, xp->ip.locator_match);
319 head = rhashtable_lookup_fast(&ilan->rhash_table,
320 &xp->ip.locator_match, rht_params);
326 if (ila_cmp_params(ila, xp)) {
328 ila = rcu_dereference_protected(ila->next,
329 lockdep_is_held(lock));
336 /* Not head, just delete from list */
337 rcu_assign_pointer(prev->next, ila->next);
339 /* It is the head. If there is something in the
340 * sublist we need to make a new head.
342 head = rcu_dereference_protected(ila->next,
343 lockdep_is_held(lock));
345 /* Put first entry in the sublist into the
348 err = rhashtable_replace_fast(
349 &ilan->rhash_table, &ila->node,
350 &head->node, rht_params);
354 /* Entry no longer used */
355 err = rhashtable_remove_fast(&ilan->rhash_table,
372 static int ila_nl_cmd_add_mapping(struct sk_buff *skb, struct genl_info *info)
374 struct net *net = genl_info_net(info);
375 struct ila_xlat_params p;
378 err = parse_nl_config(info, &p);
382 return ila_add_mapping(net, &p);
385 static int ila_nl_cmd_del_mapping(struct sk_buff *skb, struct genl_info *info)
387 struct net *net = genl_info_net(info);
388 struct ila_xlat_params xp;
391 err = parse_nl_config(info, &xp);
395 ila_del_mapping(net, &xp);
400 static int ila_fill_info(struct ila_map *ila, struct sk_buff *msg)
402 if (nla_put_u64_64bit(msg, ILA_ATTR_LOCATOR,
403 (__force u64)ila->xp.ip.locator.v64,
405 nla_put_u64_64bit(msg, ILA_ATTR_LOCATOR_MATCH,
406 (__force u64)ila->xp.ip.locator_match.v64,
408 nla_put_s32(msg, ILA_ATTR_IFINDEX, ila->xp.ifindex) ||
409 nla_put_u8(msg, ILA_ATTR_CSUM_MODE, ila->xp.ip.csum_mode) ||
410 nla_put_u8(msg, ILA_ATTR_IDENT_TYPE, ila->xp.ip.ident_type))
416 static int ila_dump_info(struct ila_map *ila,
417 u32 portid, u32 seq, u32 flags,
418 struct sk_buff *skb, u8 cmd)
422 hdr = genlmsg_put(skb, portid, seq, &ila_nl_family, flags, cmd);
426 if (ila_fill_info(ila, skb) < 0)
427 goto nla_put_failure;
429 genlmsg_end(skb, hdr);
433 genlmsg_cancel(skb, hdr);
437 static int ila_nl_cmd_get_mapping(struct sk_buff *skb, struct genl_info *info)
439 struct net *net = genl_info_net(info);
440 struct ila_net *ilan = net_generic(net, ila_net_id);
442 struct ila_xlat_params xp;
446 ret = parse_nl_config(info, &xp);
450 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
456 ila = ila_lookup_by_params(&xp, ilan);
458 ret = ila_dump_info(ila,
460 info->snd_seq, 0, msg,
469 return genlmsg_reply(msg, info);
476 struct ila_dump_iter {
477 struct rhashtable_iter rhiter;
480 static int ila_nl_dump_start(struct netlink_callback *cb)
482 struct net *net = sock_net(cb->skb->sk);
483 struct ila_net *ilan = net_generic(net, ila_net_id);
484 struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args[0];
487 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
491 cb->args[0] = (long)iter;
494 return rhashtable_walk_init(&ilan->rhash_table, &iter->rhiter,
498 static int ila_nl_dump_done(struct netlink_callback *cb)
500 struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args[0];
502 rhashtable_walk_exit(&iter->rhiter);
509 static int ila_nl_dump(struct sk_buff *skb, struct netlink_callback *cb)
511 struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args[0];
512 struct rhashtable_iter *rhiter = &iter->rhiter;
516 rhashtable_walk_start(rhiter);
519 ila = rhashtable_walk_next(rhiter);
522 if (PTR_ERR(ila) == -EAGAIN)
531 ret = ila_dump_info(ila, NETLINK_CB(cb->skb).portid,
532 cb->nlh->nlmsg_seq, NLM_F_MULTI,
537 ila = rcu_access_pointer(ila->next);
544 rhashtable_walk_stop(rhiter);
548 static const struct genl_ops ila_nl_ops[] = {
551 .doit = ila_nl_cmd_add_mapping,
552 .policy = ila_nl_policy,
553 .flags = GENL_ADMIN_PERM,
557 .doit = ila_nl_cmd_del_mapping,
558 .policy = ila_nl_policy,
559 .flags = GENL_ADMIN_PERM,
563 .doit = ila_nl_cmd_get_mapping,
564 .start = ila_nl_dump_start,
565 .dumpit = ila_nl_dump,
566 .done = ila_nl_dump_done,
567 .policy = ila_nl_policy,
571 static struct genl_family ila_nl_family __ro_after_init = {
573 .name = ILA_GENL_NAME,
574 .version = ILA_GENL_VERSION,
575 .maxattr = ILA_ATTR_MAX,
577 .parallel_ops = true,
578 .module = THIS_MODULE,
580 .n_ops = ARRAY_SIZE(ila_nl_ops),
583 #define ILA_HASH_TABLE_SIZE 1024
585 static __net_init int ila_init_net(struct net *net)
588 struct ila_net *ilan = net_generic(net, ila_net_id);
590 err = alloc_ila_locks(ilan);
594 rhashtable_init(&ilan->rhash_table, &rht_params);
599 static __net_exit void ila_exit_net(struct net *net)
601 struct ila_net *ilan = net_generic(net, ila_net_id);
603 rhashtable_free_and_destroy(&ilan->rhash_table, ila_free_cb, NULL);
607 if (ilan->hooks_registered)
608 nf_unregister_net_hooks(net, ila_nf_hook_ops,
609 ARRAY_SIZE(ila_nf_hook_ops));
612 static struct pernet_operations ila_net_ops = {
613 .init = ila_init_net,
614 .exit = ila_exit_net,
616 .size = sizeof(struct ila_net),
619 static int ila_xlat_addr(struct sk_buff *skb, bool sir2ila)
622 struct ipv6hdr *ip6h = ipv6_hdr(skb);
623 struct net *net = dev_net(skb->dev);
624 struct ila_net *ilan = net_generic(net, ila_net_id);
625 struct ila_addr *iaddr = ila_a2i(&ip6h->daddr);
627 /* Assumes skb contains a valid IPv6 header that is pulled */
629 /* No check here that ILA type in the mapping matches what is in the
630 * address. We assume that whatever sender gaves us can be translated.
631 * The checksum mode however is relevant.
636 ila = ila_lookup_wildcards(iaddr, skb->dev->ifindex, ilan);
638 ila_update_ipv6_locator(skb, &ila->xp.ip, sir2ila);
645 int __init ila_xlat_init(void)
649 ret = register_pernet_device(&ila_net_ops);
653 ret = genl_register_family(&ila_nl_family);
660 unregister_pernet_device(&ila_net_ops);
665 void ila_xlat_fini(void)
667 genl_unregister_family(&ila_nl_family);
668 unregister_pernet_device(&ila_net_ops);