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) {
45 if (size * sizeof(spinlock_t) > PAGE_SIZE)
46 ilan->locks = vmalloc(size * sizeof(spinlock_t));
49 ilan->locks = kmalloc_array(size, sizeof(spinlock_t),
53 for (i = 0; i < size; i++)
54 spin_lock_init(&ilan->locks[i]);
56 ilan->locks_mask = size - 1;
61 static u32 hashrnd __read_mostly;
62 static __always_inline void __ila_hash_secret_init(void)
64 net_get_random_once(&hashrnd, sizeof(hashrnd));
67 static inline u32 ila_locator_hash(struct ila_locator loc)
69 u32 *v = (u32 *)loc.v32;
71 __ila_hash_secret_init();
72 return jhash_2words(v[0], v[1], hashrnd);
75 static inline spinlock_t *ila_get_lock(struct ila_net *ilan,
76 struct ila_locator loc)
78 return &ilan->locks[ila_locator_hash(loc) & ilan->locks_mask];
81 static inline int ila_cmp_wildcards(struct ila_map *ila,
82 struct ila_addr *iaddr, int ifindex)
84 return (ila->xp.ifindex && ila->xp.ifindex != ifindex);
87 static inline int ila_cmp_params(struct ila_map *ila,
88 struct ila_xlat_params *xp)
90 return (ila->xp.ifindex != xp->ifindex);
93 static int ila_cmpfn(struct rhashtable_compare_arg *arg,
96 const struct ila_map *ila = obj;
98 return (ila->xp.ip.locator_match.v64 != *(__be64 *)arg->key);
101 static inline int ila_order(struct ila_map *ila)
111 static const struct rhashtable_params rht_params = {
113 .head_offset = offsetof(struct ila_map, node),
114 .key_offset = offsetof(struct ila_map, xp.ip.locator_match),
115 .key_len = sizeof(u64), /* identifier */
118 .automatic_shrinking = true,
119 .obj_cmpfn = ila_cmpfn,
122 static struct genl_family ila_nl_family = {
123 .id = GENL_ID_GENERATE,
125 .name = ILA_GENL_NAME,
126 .version = ILA_GENL_VERSION,
127 .maxattr = ILA_ATTR_MAX,
129 .parallel_ops = true,
132 static const struct nla_policy ila_nl_policy[ILA_ATTR_MAX + 1] = {
133 [ILA_ATTR_LOCATOR] = { .type = NLA_U64, },
134 [ILA_ATTR_LOCATOR_MATCH] = { .type = NLA_U64, },
135 [ILA_ATTR_IFINDEX] = { .type = NLA_U32, },
136 [ILA_ATTR_CSUM_MODE] = { .type = NLA_U8, },
139 static int parse_nl_config(struct genl_info *info,
140 struct ila_xlat_params *xp)
142 memset(xp, 0, sizeof(*xp));
144 if (info->attrs[ILA_ATTR_LOCATOR])
145 xp->ip.locator.v64 = (__force __be64)nla_get_u64(
146 info->attrs[ILA_ATTR_LOCATOR]);
148 if (info->attrs[ILA_ATTR_LOCATOR_MATCH])
149 xp->ip.locator_match.v64 = (__force __be64)nla_get_u64(
150 info->attrs[ILA_ATTR_LOCATOR_MATCH]);
152 if (info->attrs[ILA_ATTR_CSUM_MODE])
153 xp->ip.csum_mode = nla_get_u8(info->attrs[ILA_ATTR_CSUM_MODE]);
155 if (info->attrs[ILA_ATTR_IFINDEX])
156 xp->ifindex = nla_get_s32(info->attrs[ILA_ATTR_IFINDEX]);
161 /* Must be called with rcu readlock */
162 static inline struct ila_map *ila_lookup_wildcards(struct ila_addr *iaddr,
164 struct ila_net *ilan)
168 ila = rhashtable_lookup_fast(&ilan->rhash_table, &iaddr->loc,
171 if (!ila_cmp_wildcards(ila, iaddr, ifindex))
173 ila = rcu_access_pointer(ila->next);
179 /* Must be called with rcu readlock */
180 static inline struct ila_map *ila_lookup_by_params(struct ila_xlat_params *xp,
181 struct ila_net *ilan)
185 ila = rhashtable_lookup_fast(&ilan->rhash_table,
186 &xp->ip.locator_match,
189 if (!ila_cmp_params(ila, xp))
191 ila = rcu_access_pointer(ila->next);
197 static inline void ila_release(struct ila_map *ila)
202 static void ila_free_cb(void *ptr, void *arg)
204 struct ila_map *ila = (struct ila_map *)ptr, *next;
206 /* Assume rcu_readlock held */
208 next = rcu_access_pointer(ila->next);
214 static int ila_xlat_addr(struct sk_buff *skb, bool set_csum_neutral);
217 ila_nf_input(void *priv,
219 const struct nf_hook_state *state)
221 ila_xlat_addr(skb, false);
225 static struct nf_hook_ops ila_nf_hook_ops[] __read_mostly = {
227 .hook = ila_nf_input,
229 .hooknum = NF_INET_PRE_ROUTING,
234 static int ila_add_mapping(struct net *net, struct ila_xlat_params *xp)
236 struct ila_net *ilan = net_generic(net, ila_net_id);
237 struct ila_map *ila, *head;
238 spinlock_t *lock = ila_get_lock(ilan, xp->ip.locator_match);
241 if (!ilan->hooks_registered) {
242 /* We defer registering net hooks in the namespace until the
243 * first mapping is added.
245 err = nf_register_net_hooks(net, ila_nf_hook_ops,
246 ARRAY_SIZE(ila_nf_hook_ops));
250 ilan->hooks_registered = true;
253 ila = kzalloc(sizeof(*ila), GFP_KERNEL);
257 ila_init_saved_csum(&xp->ip);
261 order = ila_order(ila);
265 head = rhashtable_lookup_fast(&ilan->rhash_table,
266 &xp->ip.locator_match,
269 /* New entry for the rhash_table */
270 err = rhashtable_lookup_insert_fast(&ilan->rhash_table,
271 &ila->node, rht_params);
273 struct ila_map *tila = head, *prev = NULL;
276 if (!ila_cmp_params(tila, xp)) {
281 if (order > ila_order(tila))
285 tila = rcu_dereference_protected(tila->next,
286 lockdep_is_held(lock));
290 /* Insert in sub list of head */
291 RCU_INIT_POINTER(ila->next, tila);
292 rcu_assign_pointer(prev->next, ila);
294 /* Make this ila new head */
295 RCU_INIT_POINTER(ila->next, head);
296 err = rhashtable_replace_fast(&ilan->rhash_table,
298 &ila->node, rht_params);
313 static int ila_del_mapping(struct net *net, struct ila_xlat_params *xp)
315 struct ila_net *ilan = net_generic(net, ila_net_id);
316 struct ila_map *ila, *head, *prev;
317 spinlock_t *lock = ila_get_lock(ilan, xp->ip.locator_match);
322 head = rhashtable_lookup_fast(&ilan->rhash_table,
323 &xp->ip.locator_match, rht_params);
329 if (ila_cmp_params(ila, xp)) {
331 ila = rcu_dereference_protected(ila->next,
332 lockdep_is_held(lock));
339 /* Not head, just delete from list */
340 rcu_assign_pointer(prev->next, ila->next);
342 /* It is the head. If there is something in the
343 * sublist we need to make a new head.
345 head = rcu_dereference_protected(ila->next,
346 lockdep_is_held(lock));
348 /* Put first entry in the sublist into the
351 err = rhashtable_replace_fast(
352 &ilan->rhash_table, &ila->node,
353 &head->node, rht_params);
357 /* Entry no longer used */
358 err = rhashtable_remove_fast(&ilan->rhash_table,
375 static int ila_nl_cmd_add_mapping(struct sk_buff *skb, struct genl_info *info)
377 struct net *net = genl_info_net(info);
378 struct ila_xlat_params p;
381 err = parse_nl_config(info, &p);
385 return ila_add_mapping(net, &p);
388 static int ila_nl_cmd_del_mapping(struct sk_buff *skb, struct genl_info *info)
390 struct net *net = genl_info_net(info);
391 struct ila_xlat_params xp;
394 err = parse_nl_config(info, &xp);
398 ila_del_mapping(net, &xp);
403 static int ila_fill_info(struct ila_map *ila, struct sk_buff *msg)
405 if (nla_put_u64_64bit(msg, ILA_ATTR_LOCATOR,
406 (__force u64)ila->xp.ip.locator.v64,
408 nla_put_u64_64bit(msg, ILA_ATTR_LOCATOR_MATCH,
409 (__force u64)ila->xp.ip.locator_match.v64,
411 nla_put_s32(msg, ILA_ATTR_IFINDEX, ila->xp.ifindex) ||
412 nla_put_u32(msg, ILA_ATTR_CSUM_MODE, ila->xp.ip.csum_mode))
418 static int ila_dump_info(struct ila_map *ila,
419 u32 portid, u32 seq, u32 flags,
420 struct sk_buff *skb, u8 cmd)
424 hdr = genlmsg_put(skb, portid, seq, &ila_nl_family, flags, cmd);
428 if (ila_fill_info(ila, skb) < 0)
429 goto nla_put_failure;
431 genlmsg_end(skb, hdr);
435 genlmsg_cancel(skb, hdr);
439 static int ila_nl_cmd_get_mapping(struct sk_buff *skb, struct genl_info *info)
441 struct net *net = genl_info_net(info);
442 struct ila_net *ilan = net_generic(net, ila_net_id);
444 struct ila_xlat_params xp;
448 ret = parse_nl_config(info, &xp);
452 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
458 ila = ila_lookup_by_params(&xp, ilan);
460 ret = ila_dump_info(ila,
462 info->snd_seq, 0, msg,
471 return genlmsg_reply(msg, info);
478 struct ila_dump_iter {
479 struct rhashtable_iter rhiter;
482 static int ila_nl_dump_start(struct netlink_callback *cb)
484 struct net *net = sock_net(cb->skb->sk);
485 struct ila_net *ilan = net_generic(net, ila_net_id);
486 struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args;
488 return rhashtable_walk_init(&ilan->rhash_table, &iter->rhiter,
492 static int ila_nl_dump_done(struct netlink_callback *cb)
494 struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args;
496 rhashtable_walk_exit(&iter->rhiter);
501 static int ila_nl_dump(struct sk_buff *skb, struct netlink_callback *cb)
503 struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args;
504 struct rhashtable_iter *rhiter = &iter->rhiter;
508 ret = rhashtable_walk_start(rhiter);
509 if (ret && ret != -EAGAIN)
513 ila = rhashtable_walk_next(rhiter);
516 if (PTR_ERR(ila) == -EAGAIN)
525 ret = ila_dump_info(ila, NETLINK_CB(cb->skb).portid,
526 cb->nlh->nlmsg_seq, NLM_F_MULTI,
531 ila = rcu_access_pointer(ila->next);
538 rhashtable_walk_stop(rhiter);
542 static const struct genl_ops ila_nl_ops[] = {
545 .doit = ila_nl_cmd_add_mapping,
546 .policy = ila_nl_policy,
547 .flags = GENL_ADMIN_PERM,
551 .doit = ila_nl_cmd_del_mapping,
552 .policy = ila_nl_policy,
553 .flags = GENL_ADMIN_PERM,
557 .doit = ila_nl_cmd_get_mapping,
558 .start = ila_nl_dump_start,
559 .dumpit = ila_nl_dump,
560 .done = ila_nl_dump_done,
561 .policy = ila_nl_policy,
565 #define ILA_HASH_TABLE_SIZE 1024
567 static __net_init int ila_init_net(struct net *net)
570 struct ila_net *ilan = net_generic(net, ila_net_id);
572 err = alloc_ila_locks(ilan);
576 rhashtable_init(&ilan->rhash_table, &rht_params);
581 static __net_exit void ila_exit_net(struct net *net)
583 struct ila_net *ilan = net_generic(net, ila_net_id);
585 rhashtable_free_and_destroy(&ilan->rhash_table, ila_free_cb, NULL);
589 if (ilan->hooks_registered)
590 nf_unregister_net_hooks(net, ila_nf_hook_ops,
591 ARRAY_SIZE(ila_nf_hook_ops));
594 static struct pernet_operations ila_net_ops = {
595 .init = ila_init_net,
596 .exit = ila_exit_net,
598 .size = sizeof(struct ila_net),
601 static int ila_xlat_addr(struct sk_buff *skb, bool set_csum_neutral)
604 struct ipv6hdr *ip6h = ipv6_hdr(skb);
605 struct net *net = dev_net(skb->dev);
606 struct ila_net *ilan = net_generic(net, ila_net_id);
607 struct ila_addr *iaddr = ila_a2i(&ip6h->daddr);
609 /* Assumes skb contains a valid IPv6 header that is pulled */
611 if (!ila_addr_is_ila(iaddr)) {
612 /* Type indicates this is not an ILA address */
618 ila = ila_lookup_wildcards(iaddr, skb->dev->ifindex, ilan);
620 ila_update_ipv6_locator(skb, &ila->xp.ip, set_csum_neutral);
627 int ila_xlat_init(void)
631 ret = register_pernet_device(&ila_net_ops);
635 ret = genl_register_family_with_ops(&ila_nl_family,
643 unregister_pernet_device(&ila_net_ops);
648 void ila_xlat_fini(void)
650 genl_unregister_family(&ila_nl_family);
651 unregister_pernet_device(&ila_net_ops);