1 #include <linux/jhash.h>
2 #include <linux/netfilter.h>
3 #include <linux/rcupdate.h>
4 #include <linux/rhashtable.h>
5 #include <linux/vmalloc.h>
6 #include <net/genetlink.h>
8 #include <net/netns/generic.h>
9 #include <uapi/linux/genetlink.h>
12 struct ila_xlat_params {
18 struct ila_xlat_params xp;
19 struct rhash_head node;
20 struct ila_map __rcu *next;
24 static unsigned int ila_net_id;
27 struct rhashtable rhash_table;
28 spinlock_t *locks; /* Bucket locks for entry manipulation */
29 unsigned int locks_mask;
30 bool hooks_registered;
33 #define LOCKS_PER_CPU 10
35 static int alloc_ila_locks(struct ila_net *ilan)
38 unsigned int nr_pcpus = num_possible_cpus();
40 nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL);
41 size = roundup_pow_of_two(nr_pcpus * LOCKS_PER_CPU);
43 if (sizeof(spinlock_t) != 0) {
44 ilan->locks = kvmalloc(size * sizeof(spinlock_t), GFP_KERNEL);
47 for (i = 0; i < size; i++)
48 spin_lock_init(&ilan->locks[i]);
50 ilan->locks_mask = size - 1;
55 static u32 hashrnd __read_mostly;
56 static __always_inline void __ila_hash_secret_init(void)
58 net_get_random_once(&hashrnd, sizeof(hashrnd));
61 static inline u32 ila_locator_hash(struct ila_locator loc)
63 u32 *v = (u32 *)loc.v32;
65 return jhash_2words(v[0], v[1], hashrnd);
68 static inline spinlock_t *ila_get_lock(struct ila_net *ilan,
69 struct ila_locator loc)
71 return &ilan->locks[ila_locator_hash(loc) & ilan->locks_mask];
74 static inline int ila_cmp_wildcards(struct ila_map *ila,
75 struct ila_addr *iaddr, int ifindex)
77 return (ila->xp.ifindex && ila->xp.ifindex != ifindex);
80 static inline int ila_cmp_params(struct ila_map *ila,
81 struct ila_xlat_params *xp)
83 return (ila->xp.ifindex != xp->ifindex);
86 static int ila_cmpfn(struct rhashtable_compare_arg *arg,
89 const struct ila_map *ila = obj;
91 return (ila->xp.ip.locator_match.v64 != *(__be64 *)arg->key);
94 static inline int ila_order(struct ila_map *ila)
104 static const struct rhashtable_params rht_params = {
106 .head_offset = offsetof(struct ila_map, node),
107 .key_offset = offsetof(struct ila_map, xp.ip.locator_match),
108 .key_len = sizeof(u64), /* identifier */
111 .automatic_shrinking = true,
112 .obj_cmpfn = ila_cmpfn,
115 static struct genl_family ila_nl_family;
117 static const struct nla_policy ila_nl_policy[ILA_ATTR_MAX + 1] = {
118 [ILA_ATTR_LOCATOR] = { .type = NLA_U64, },
119 [ILA_ATTR_LOCATOR_MATCH] = { .type = NLA_U64, },
120 [ILA_ATTR_IFINDEX] = { .type = NLA_U32, },
121 [ILA_ATTR_CSUM_MODE] = { .type = NLA_U8, },
124 static int parse_nl_config(struct genl_info *info,
125 struct ila_xlat_params *xp)
127 memset(xp, 0, sizeof(*xp));
129 if (info->attrs[ILA_ATTR_LOCATOR])
130 xp->ip.locator.v64 = (__force __be64)nla_get_u64(
131 info->attrs[ILA_ATTR_LOCATOR]);
133 if (info->attrs[ILA_ATTR_LOCATOR_MATCH])
134 xp->ip.locator_match.v64 = (__force __be64)nla_get_u64(
135 info->attrs[ILA_ATTR_LOCATOR_MATCH]);
137 if (info->attrs[ILA_ATTR_CSUM_MODE])
138 xp->ip.csum_mode = nla_get_u8(info->attrs[ILA_ATTR_CSUM_MODE]);
140 if (info->attrs[ILA_ATTR_IFINDEX])
141 xp->ifindex = nla_get_s32(info->attrs[ILA_ATTR_IFINDEX]);
146 /* Must be called with rcu readlock */
147 static inline struct ila_map *ila_lookup_wildcards(struct ila_addr *iaddr,
149 struct ila_net *ilan)
153 ila = rhashtable_lookup_fast(&ilan->rhash_table, &iaddr->loc,
156 if (!ila_cmp_wildcards(ila, iaddr, ifindex))
158 ila = rcu_access_pointer(ila->next);
164 /* Must be called with rcu readlock */
165 static inline struct ila_map *ila_lookup_by_params(struct ila_xlat_params *xp,
166 struct ila_net *ilan)
170 ila = rhashtable_lookup_fast(&ilan->rhash_table,
171 &xp->ip.locator_match,
174 if (!ila_cmp_params(ila, xp))
176 ila = rcu_access_pointer(ila->next);
182 static inline void ila_release(struct ila_map *ila)
187 static void ila_free_cb(void *ptr, void *arg)
189 struct ila_map *ila = (struct ila_map *)ptr, *next;
191 /* Assume rcu_readlock held */
193 next = rcu_access_pointer(ila->next);
199 static int ila_xlat_addr(struct sk_buff *skb, bool set_csum_neutral);
202 ila_nf_input(void *priv,
204 const struct nf_hook_state *state)
206 ila_xlat_addr(skb, false);
210 static struct nf_hook_ops ila_nf_hook_ops[] __read_mostly = {
212 .hook = ila_nf_input,
214 .hooknum = NF_INET_PRE_ROUTING,
219 static int ila_add_mapping(struct net *net, struct ila_xlat_params *xp)
221 struct ila_net *ilan = net_generic(net, ila_net_id);
222 struct ila_map *ila, *head;
223 spinlock_t *lock = ila_get_lock(ilan, xp->ip.locator_match);
226 if (!ilan->hooks_registered) {
227 /* We defer registering net hooks in the namespace until the
228 * first mapping is added.
230 err = nf_register_net_hooks(net, ila_nf_hook_ops,
231 ARRAY_SIZE(ila_nf_hook_ops));
235 ilan->hooks_registered = true;
238 ila = kzalloc(sizeof(*ila), GFP_KERNEL);
242 ila_init_saved_csum(&xp->ip);
246 order = ila_order(ila);
250 head = rhashtable_lookup_fast(&ilan->rhash_table,
251 &xp->ip.locator_match,
254 /* New entry for the rhash_table */
255 err = rhashtable_lookup_insert_fast(&ilan->rhash_table,
256 &ila->node, rht_params);
258 struct ila_map *tila = head, *prev = NULL;
261 if (!ila_cmp_params(tila, xp)) {
266 if (order > ila_order(tila))
270 tila = rcu_dereference_protected(tila->next,
271 lockdep_is_held(lock));
275 /* Insert in sub list of head */
276 RCU_INIT_POINTER(ila->next, tila);
277 rcu_assign_pointer(prev->next, ila);
279 /* Make this ila new head */
280 RCU_INIT_POINTER(ila->next, head);
281 err = rhashtable_replace_fast(&ilan->rhash_table,
283 &ila->node, rht_params);
298 static int ila_del_mapping(struct net *net, struct ila_xlat_params *xp)
300 struct ila_net *ilan = net_generic(net, ila_net_id);
301 struct ila_map *ila, *head, *prev;
302 spinlock_t *lock = ila_get_lock(ilan, xp->ip.locator_match);
307 head = rhashtable_lookup_fast(&ilan->rhash_table,
308 &xp->ip.locator_match, rht_params);
314 if (ila_cmp_params(ila, xp)) {
316 ila = rcu_dereference_protected(ila->next,
317 lockdep_is_held(lock));
324 /* Not head, just delete from list */
325 rcu_assign_pointer(prev->next, ila->next);
327 /* It is the head. If there is something in the
328 * sublist we need to make a new head.
330 head = rcu_dereference_protected(ila->next,
331 lockdep_is_held(lock));
333 /* Put first entry in the sublist into the
336 err = rhashtable_replace_fast(
337 &ilan->rhash_table, &ila->node,
338 &head->node, rht_params);
342 /* Entry no longer used */
343 err = rhashtable_remove_fast(&ilan->rhash_table,
360 static int ila_nl_cmd_add_mapping(struct sk_buff *skb, struct genl_info *info)
362 struct net *net = genl_info_net(info);
363 struct ila_xlat_params p;
366 err = parse_nl_config(info, &p);
370 return ila_add_mapping(net, &p);
373 static int ila_nl_cmd_del_mapping(struct sk_buff *skb, struct genl_info *info)
375 struct net *net = genl_info_net(info);
376 struct ila_xlat_params xp;
379 err = parse_nl_config(info, &xp);
383 ila_del_mapping(net, &xp);
388 static int ila_fill_info(struct ila_map *ila, struct sk_buff *msg)
390 if (nla_put_u64_64bit(msg, ILA_ATTR_LOCATOR,
391 (__force u64)ila->xp.ip.locator.v64,
393 nla_put_u64_64bit(msg, ILA_ATTR_LOCATOR_MATCH,
394 (__force u64)ila->xp.ip.locator_match.v64,
396 nla_put_s32(msg, ILA_ATTR_IFINDEX, ila->xp.ifindex) ||
397 nla_put_u32(msg, ILA_ATTR_CSUM_MODE, ila->xp.ip.csum_mode))
403 static int ila_dump_info(struct ila_map *ila,
404 u32 portid, u32 seq, u32 flags,
405 struct sk_buff *skb, u8 cmd)
409 hdr = genlmsg_put(skb, portid, seq, &ila_nl_family, flags, cmd);
413 if (ila_fill_info(ila, skb) < 0)
414 goto nla_put_failure;
416 genlmsg_end(skb, hdr);
420 genlmsg_cancel(skb, hdr);
424 static int ila_nl_cmd_get_mapping(struct sk_buff *skb, struct genl_info *info)
426 struct net *net = genl_info_net(info);
427 struct ila_net *ilan = net_generic(net, ila_net_id);
429 struct ila_xlat_params xp;
433 ret = parse_nl_config(info, &xp);
437 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
443 ila = ila_lookup_by_params(&xp, ilan);
445 ret = ila_dump_info(ila,
447 info->snd_seq, 0, msg,
456 return genlmsg_reply(msg, info);
463 struct ila_dump_iter {
464 struct rhashtable_iter rhiter;
467 static int ila_nl_dump_start(struct netlink_callback *cb)
469 struct net *net = sock_net(cb->skb->sk);
470 struct ila_net *ilan = net_generic(net, ila_net_id);
471 struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args[0];
474 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
478 cb->args[0] = (long)iter;
481 return rhashtable_walk_init(&ilan->rhash_table, &iter->rhiter,
485 static int ila_nl_dump_done(struct netlink_callback *cb)
487 struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args[0];
489 rhashtable_walk_exit(&iter->rhiter);
496 static int ila_nl_dump(struct sk_buff *skb, struct netlink_callback *cb)
498 struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args[0];
499 struct rhashtable_iter *rhiter = &iter->rhiter;
503 ret = rhashtable_walk_start(rhiter);
504 if (ret && ret != -EAGAIN)
508 ila = rhashtable_walk_next(rhiter);
511 if (PTR_ERR(ila) == -EAGAIN)
520 ret = ila_dump_info(ila, NETLINK_CB(cb->skb).portid,
521 cb->nlh->nlmsg_seq, NLM_F_MULTI,
526 ila = rcu_access_pointer(ila->next);
533 rhashtable_walk_stop(rhiter);
537 static const struct genl_ops ila_nl_ops[] = {
540 .doit = ila_nl_cmd_add_mapping,
541 .policy = ila_nl_policy,
542 .flags = GENL_ADMIN_PERM,
546 .doit = ila_nl_cmd_del_mapping,
547 .policy = ila_nl_policy,
548 .flags = GENL_ADMIN_PERM,
552 .doit = ila_nl_cmd_get_mapping,
553 .start = ila_nl_dump_start,
554 .dumpit = ila_nl_dump,
555 .done = ila_nl_dump_done,
556 .policy = ila_nl_policy,
560 static struct genl_family ila_nl_family __ro_after_init = {
562 .name = ILA_GENL_NAME,
563 .version = ILA_GENL_VERSION,
564 .maxattr = ILA_ATTR_MAX,
566 .parallel_ops = true,
567 .module = THIS_MODULE,
569 .n_ops = ARRAY_SIZE(ila_nl_ops),
572 #define ILA_HASH_TABLE_SIZE 1024
574 static __net_init int ila_init_net(struct net *net)
577 struct ila_net *ilan = net_generic(net, ila_net_id);
579 err = alloc_ila_locks(ilan);
583 rhashtable_init(&ilan->rhash_table, &rht_params);
588 static __net_exit void ila_exit_net(struct net *net)
590 struct ila_net *ilan = net_generic(net, ila_net_id);
592 rhashtable_free_and_destroy(&ilan->rhash_table, ila_free_cb, NULL);
596 if (ilan->hooks_registered)
597 nf_unregister_net_hooks(net, ila_nf_hook_ops,
598 ARRAY_SIZE(ila_nf_hook_ops));
601 static struct pernet_operations ila_net_ops = {
602 .init = ila_init_net,
603 .exit = ila_exit_net,
605 .size = sizeof(struct ila_net),
608 static int ila_xlat_addr(struct sk_buff *skb, bool set_csum_neutral)
611 struct ipv6hdr *ip6h = ipv6_hdr(skb);
612 struct net *net = dev_net(skb->dev);
613 struct ila_net *ilan = net_generic(net, ila_net_id);
614 struct ila_addr *iaddr = ila_a2i(&ip6h->daddr);
616 /* Assumes skb contains a valid IPv6 header that is pulled */
618 if (!ila_addr_is_ila(iaddr)) {
619 /* Type indicates this is not an ILA address */
625 ila = ila_lookup_wildcards(iaddr, skb->dev->ifindex, ilan);
627 ila_update_ipv6_locator(skb, &ila->xp.ip, set_csum_neutral);
634 int __init ila_xlat_init(void)
638 ret = register_pernet_device(&ila_net_ops);
642 ret = genl_register_family(&ila_nl_family);
649 unregister_pernet_device(&ila_net_ops);
654 void ila_xlat_fini(void)
656 genl_unregister_family(&ila_nl_family);
657 unregister_pernet_device(&ila_net_ops);