Tom Herbert | 7f00fea | 2015-12-15 15:41:38 -0800 | [diff] [blame] | 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> |
| 7 | #include <net/ila.h> |
| 8 | #include <net/netns/generic.h> |
| 9 | #include <uapi/linux/genetlink.h> |
| 10 | #include "ila.h" |
| 11 | |
| 12 | struct ila_xlat_params { |
| 13 | struct ila_params ip; |
| 14 | __be64 identifier; |
| 15 | int ifindex; |
| 16 | unsigned int dir; |
| 17 | }; |
| 18 | |
| 19 | struct ila_map { |
| 20 | struct ila_xlat_params p; |
| 21 | struct rhash_head node; |
| 22 | struct ila_map __rcu *next; |
| 23 | struct rcu_head rcu; |
| 24 | }; |
| 25 | |
| 26 | static unsigned int ila_net_id; |
| 27 | |
| 28 | struct ila_net { |
| 29 | struct rhashtable rhash_table; |
| 30 | spinlock_t *locks; /* Bucket locks for entry manipulation */ |
| 31 | unsigned int locks_mask; |
| 32 | bool hooks_registered; |
| 33 | }; |
| 34 | |
| 35 | #define LOCKS_PER_CPU 10 |
| 36 | |
| 37 | static int alloc_ila_locks(struct ila_net *ilan) |
| 38 | { |
| 39 | unsigned int i, size; |
| 40 | unsigned int nr_pcpus = num_possible_cpus(); |
| 41 | |
| 42 | nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL); |
| 43 | size = roundup_pow_of_two(nr_pcpus * LOCKS_PER_CPU); |
| 44 | |
| 45 | if (sizeof(spinlock_t) != 0) { |
| 46 | #ifdef CONFIG_NUMA |
| 47 | if (size * sizeof(spinlock_t) > PAGE_SIZE) |
| 48 | ilan->locks = vmalloc(size * sizeof(spinlock_t)); |
| 49 | else |
| 50 | #endif |
| 51 | ilan->locks = kmalloc_array(size, sizeof(spinlock_t), |
| 52 | GFP_KERNEL); |
| 53 | if (!ilan->locks) |
| 54 | return -ENOMEM; |
| 55 | for (i = 0; i < size; i++) |
| 56 | spin_lock_init(&ilan->locks[i]); |
| 57 | } |
| 58 | ilan->locks_mask = size - 1; |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static u32 hashrnd __read_mostly; |
| 64 | static __always_inline void __ila_hash_secret_init(void) |
| 65 | { |
| 66 | net_get_random_once(&hashrnd, sizeof(hashrnd)); |
| 67 | } |
| 68 | |
| 69 | static inline u32 ila_identifier_hash(__be64 identifier) |
| 70 | { |
| 71 | u32 *v = (u32 *)&identifier; |
| 72 | |
| 73 | return jhash_2words(v[0], v[1], hashrnd); |
| 74 | } |
| 75 | |
| 76 | static inline spinlock_t *ila_get_lock(struct ila_net *ilan, __be64 identifier) |
| 77 | { |
| 78 | return &ilan->locks[ila_identifier_hash(identifier) & ilan->locks_mask]; |
| 79 | } |
| 80 | |
| 81 | static inline int ila_cmp_wildcards(struct ila_map *ila, __be64 loc, |
| 82 | int ifindex, unsigned int dir) |
| 83 | { |
| 84 | return (ila->p.ip.locator_match && ila->p.ip.locator_match != loc) || |
| 85 | (ila->p.ifindex && ila->p.ifindex != ifindex) || |
| 86 | !(ila->p.dir & dir); |
| 87 | } |
| 88 | |
| 89 | static inline int ila_cmp_params(struct ila_map *ila, struct ila_xlat_params *p) |
| 90 | { |
| 91 | return (ila->p.ip.locator_match != p->ip.locator_match) || |
| 92 | (ila->p.ifindex != p->ifindex) || |
| 93 | (ila->p.dir != p->dir); |
| 94 | } |
| 95 | |
| 96 | static int ila_cmpfn(struct rhashtable_compare_arg *arg, |
| 97 | const void *obj) |
| 98 | { |
| 99 | const struct ila_map *ila = obj; |
| 100 | |
| 101 | return (ila->p.identifier != *(__be64 *)arg->key); |
| 102 | } |
| 103 | |
| 104 | static inline int ila_order(struct ila_map *ila) |
| 105 | { |
| 106 | int score = 0; |
| 107 | |
| 108 | if (ila->p.ip.locator_match) |
| 109 | score += 1 << 0; |
| 110 | |
| 111 | if (ila->p.ifindex) |
| 112 | score += 1 << 1; |
| 113 | |
| 114 | return score; |
| 115 | } |
| 116 | |
| 117 | static const struct rhashtable_params rht_params = { |
| 118 | .nelem_hint = 1024, |
| 119 | .head_offset = offsetof(struct ila_map, node), |
| 120 | .key_offset = offsetof(struct ila_map, p.identifier), |
| 121 | .key_len = sizeof(u64), /* identifier */ |
| 122 | .max_size = 1048576, |
| 123 | .min_size = 256, |
| 124 | .automatic_shrinking = true, |
| 125 | .obj_cmpfn = ila_cmpfn, |
| 126 | }; |
| 127 | |
| 128 | static struct genl_family ila_nl_family = { |
| 129 | .id = GENL_ID_GENERATE, |
| 130 | .hdrsize = 0, |
| 131 | .name = ILA_GENL_NAME, |
| 132 | .version = ILA_GENL_VERSION, |
| 133 | .maxattr = ILA_ATTR_MAX, |
| 134 | .netnsok = true, |
| 135 | .parallel_ops = true, |
| 136 | }; |
| 137 | |
| 138 | static struct nla_policy ila_nl_policy[ILA_ATTR_MAX + 1] = { |
| 139 | [ILA_ATTR_IDENTIFIER] = { .type = NLA_U64, }, |
| 140 | [ILA_ATTR_LOCATOR] = { .type = NLA_U64, }, |
| 141 | [ILA_ATTR_LOCATOR_MATCH] = { .type = NLA_U64, }, |
| 142 | [ILA_ATTR_IFINDEX] = { .type = NLA_U32, }, |
| 143 | [ILA_ATTR_DIR] = { .type = NLA_U32, }, |
| 144 | }; |
| 145 | |
| 146 | static int parse_nl_config(struct genl_info *info, |
| 147 | struct ila_xlat_params *p) |
| 148 | { |
| 149 | memset(p, 0, sizeof(*p)); |
| 150 | |
| 151 | if (info->attrs[ILA_ATTR_IDENTIFIER]) |
| 152 | p->identifier = (__force __be64)nla_get_u64( |
| 153 | info->attrs[ILA_ATTR_IDENTIFIER]); |
| 154 | |
| 155 | if (info->attrs[ILA_ATTR_LOCATOR]) |
| 156 | p->ip.locator = (__force __be64)nla_get_u64( |
| 157 | info->attrs[ILA_ATTR_LOCATOR]); |
| 158 | |
| 159 | if (info->attrs[ILA_ATTR_LOCATOR_MATCH]) |
| 160 | p->ip.locator_match = (__force __be64)nla_get_u64( |
| 161 | info->attrs[ILA_ATTR_LOCATOR_MATCH]); |
| 162 | |
| 163 | if (info->attrs[ILA_ATTR_IFINDEX]) |
| 164 | p->ifindex = nla_get_s32(info->attrs[ILA_ATTR_IFINDEX]); |
| 165 | |
| 166 | if (info->attrs[ILA_ATTR_DIR]) |
| 167 | p->dir = nla_get_u32(info->attrs[ILA_ATTR_DIR]); |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | /* Must be called with rcu readlock */ |
| 173 | static inline struct ila_map *ila_lookup_wildcards(__be64 id, __be64 loc, |
| 174 | int ifindex, |
| 175 | unsigned int dir, |
| 176 | struct ila_net *ilan) |
| 177 | { |
| 178 | struct ila_map *ila; |
| 179 | |
| 180 | ila = rhashtable_lookup_fast(&ilan->rhash_table, &id, rht_params); |
| 181 | while (ila) { |
| 182 | if (!ila_cmp_wildcards(ila, loc, ifindex, dir)) |
| 183 | return ila; |
| 184 | ila = rcu_access_pointer(ila->next); |
| 185 | } |
| 186 | |
| 187 | return NULL; |
| 188 | } |
| 189 | |
| 190 | /* Must be called with rcu readlock */ |
| 191 | static inline struct ila_map *ila_lookup_by_params(struct ila_xlat_params *p, |
| 192 | struct ila_net *ilan) |
| 193 | { |
| 194 | struct ila_map *ila; |
| 195 | |
| 196 | ila = rhashtable_lookup_fast(&ilan->rhash_table, &p->identifier, |
| 197 | rht_params); |
| 198 | while (ila) { |
| 199 | if (!ila_cmp_params(ila, p)) |
| 200 | return ila; |
| 201 | ila = rcu_access_pointer(ila->next); |
| 202 | } |
| 203 | |
| 204 | return NULL; |
| 205 | } |
| 206 | |
| 207 | static inline void ila_release(struct ila_map *ila) |
| 208 | { |
| 209 | kfree_rcu(ila, rcu); |
| 210 | } |
| 211 | |
| 212 | static void ila_free_cb(void *ptr, void *arg) |
| 213 | { |
| 214 | struct ila_map *ila = (struct ila_map *)ptr, *next; |
| 215 | |
| 216 | /* Assume rcu_readlock held */ |
| 217 | while (ila) { |
| 218 | next = rcu_access_pointer(ila->next); |
| 219 | ila_release(ila); |
| 220 | ila = next; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | static int ila_xlat_addr(struct sk_buff *skb, int dir); |
| 225 | |
| 226 | static unsigned int |
| 227 | ila_nf_input(void *priv, |
| 228 | struct sk_buff *skb, |
| 229 | const struct nf_hook_state *state) |
| 230 | { |
| 231 | ila_xlat_addr(skb, ILA_DIR_IN); |
| 232 | return NF_ACCEPT; |
| 233 | } |
| 234 | |
| 235 | static struct nf_hook_ops ila_nf_hook_ops[] __read_mostly = { |
| 236 | { |
| 237 | .hook = ila_nf_input, |
| 238 | .pf = NFPROTO_IPV6, |
| 239 | .hooknum = NF_INET_PRE_ROUTING, |
| 240 | .priority = -1, |
| 241 | }, |
| 242 | }; |
| 243 | |
| 244 | static int ila_add_mapping(struct net *net, struct ila_xlat_params *p) |
| 245 | { |
| 246 | struct ila_net *ilan = net_generic(net, ila_net_id); |
| 247 | struct ila_map *ila, *head; |
| 248 | spinlock_t *lock = ila_get_lock(ilan, p->identifier); |
| 249 | int err = 0, order; |
| 250 | |
| 251 | if (!ilan->hooks_registered) { |
| 252 | /* We defer registering net hooks in the namespace until the |
| 253 | * first mapping is added. |
| 254 | */ |
| 255 | err = nf_register_net_hooks(net, ila_nf_hook_ops, |
| 256 | ARRAY_SIZE(ila_nf_hook_ops)); |
| 257 | if (err) |
| 258 | return err; |
| 259 | |
| 260 | ilan->hooks_registered = true; |
| 261 | } |
| 262 | |
| 263 | ila = kzalloc(sizeof(*ila), GFP_KERNEL); |
| 264 | if (!ila) |
| 265 | return -ENOMEM; |
| 266 | |
| 267 | ila->p = *p; |
| 268 | |
| 269 | if (p->ip.locator_match) { |
| 270 | /* Precompute checksum difference for translation since we |
| 271 | * know both the old identifier and the new one. |
| 272 | */ |
| 273 | ila->p.ip.csum_diff = compute_csum_diff8( |
| 274 | (__be32 *)&p->ip.locator_match, |
| 275 | (__be32 *)&p->ip.locator); |
| 276 | } |
| 277 | |
| 278 | order = ila_order(ila); |
| 279 | |
| 280 | spin_lock(lock); |
| 281 | |
| 282 | head = rhashtable_lookup_fast(&ilan->rhash_table, &p->identifier, |
| 283 | rht_params); |
| 284 | if (!head) { |
| 285 | /* New entry for the rhash_table */ |
| 286 | err = rhashtable_lookup_insert_fast(&ilan->rhash_table, |
| 287 | &ila->node, rht_params); |
| 288 | } else { |
| 289 | struct ila_map *tila = head, *prev = NULL; |
| 290 | |
| 291 | do { |
| 292 | if (!ila_cmp_params(tila, p)) { |
| 293 | err = -EEXIST; |
| 294 | goto out; |
| 295 | } |
| 296 | |
| 297 | if (order > ila_order(tila)) |
| 298 | break; |
| 299 | |
| 300 | prev = tila; |
| 301 | tila = rcu_dereference_protected(tila->next, |
| 302 | lockdep_is_held(lock)); |
| 303 | } while (tila); |
| 304 | |
| 305 | if (prev) { |
| 306 | /* Insert in sub list of head */ |
| 307 | RCU_INIT_POINTER(ila->next, tila); |
| 308 | rcu_assign_pointer(prev->next, ila); |
| 309 | } else { |
| 310 | /* Make this ila new head */ |
| 311 | RCU_INIT_POINTER(ila->next, head); |
| 312 | err = rhashtable_replace_fast(&ilan->rhash_table, |
| 313 | &head->node, |
| 314 | &ila->node, rht_params); |
| 315 | if (err) |
| 316 | goto out; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | out: |
| 321 | spin_unlock(lock); |
| 322 | |
| 323 | if (err) |
| 324 | kfree(ila); |
| 325 | |
| 326 | return err; |
| 327 | } |
| 328 | |
| 329 | static int ila_del_mapping(struct net *net, struct ila_xlat_params *p) |
| 330 | { |
| 331 | struct ila_net *ilan = net_generic(net, ila_net_id); |
| 332 | struct ila_map *ila, *head, *prev; |
| 333 | spinlock_t *lock = ila_get_lock(ilan, p->identifier); |
| 334 | int err = -ENOENT; |
| 335 | |
| 336 | spin_lock(lock); |
| 337 | |
| 338 | head = rhashtable_lookup_fast(&ilan->rhash_table, |
| 339 | &p->identifier, rht_params); |
| 340 | ila = head; |
| 341 | |
| 342 | prev = NULL; |
| 343 | |
| 344 | while (ila) { |
| 345 | if (ila_cmp_params(ila, p)) { |
| 346 | prev = ila; |
| 347 | ila = rcu_dereference_protected(ila->next, |
| 348 | lockdep_is_held(lock)); |
| 349 | continue; |
| 350 | } |
| 351 | |
| 352 | err = 0; |
| 353 | |
| 354 | if (prev) { |
| 355 | /* Not head, just delete from list */ |
| 356 | rcu_assign_pointer(prev->next, ila->next); |
| 357 | } else { |
| 358 | /* It is the head. If there is something in the |
| 359 | * sublist we need to make a new head. |
| 360 | */ |
| 361 | head = rcu_dereference_protected(ila->next, |
| 362 | lockdep_is_held(lock)); |
| 363 | if (head) { |
| 364 | /* Put first entry in the sublist into the |
| 365 | * table |
| 366 | */ |
| 367 | err = rhashtable_replace_fast( |
| 368 | &ilan->rhash_table, &ila->node, |
| 369 | &head->node, rht_params); |
| 370 | if (err) |
| 371 | goto out; |
| 372 | } else { |
| 373 | /* Entry no longer used */ |
| 374 | err = rhashtable_remove_fast(&ilan->rhash_table, |
| 375 | &ila->node, |
| 376 | rht_params); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | ila_release(ila); |
| 381 | |
| 382 | break; |
| 383 | } |
| 384 | |
| 385 | out: |
| 386 | spin_unlock(lock); |
| 387 | |
| 388 | return err; |
| 389 | } |
| 390 | |
| 391 | static int ila_nl_cmd_add_mapping(struct sk_buff *skb, struct genl_info *info) |
| 392 | { |
| 393 | struct net *net = genl_info_net(info); |
| 394 | struct ila_xlat_params p; |
| 395 | int err; |
| 396 | |
| 397 | err = parse_nl_config(info, &p); |
| 398 | if (err) |
| 399 | return err; |
| 400 | |
| 401 | return ila_add_mapping(net, &p); |
| 402 | } |
| 403 | |
| 404 | static int ila_nl_cmd_del_mapping(struct sk_buff *skb, struct genl_info *info) |
| 405 | { |
| 406 | struct net *net = genl_info_net(info); |
| 407 | struct ila_xlat_params p; |
| 408 | int err; |
| 409 | |
| 410 | err = parse_nl_config(info, &p); |
| 411 | if (err) |
| 412 | return err; |
| 413 | |
| 414 | ila_del_mapping(net, &p); |
| 415 | |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | static int ila_fill_info(struct ila_map *ila, struct sk_buff *msg) |
| 420 | { |
Nicolas Dichtel | f13a82d | 2016-04-25 10:25:16 +0200 | [diff] [blame^] | 421 | if (nla_put_u64_64bit(msg, ILA_ATTR_IDENTIFIER, |
| 422 | (__force u64)ila->p.identifier, |
| 423 | ILA_ATTR_PAD) || |
| 424 | nla_put_u64_64bit(msg, ILA_ATTR_LOCATOR, |
| 425 | (__force u64)ila->p.ip.locator, |
| 426 | ILA_ATTR_PAD) || |
| 427 | nla_put_u64_64bit(msg, ILA_ATTR_LOCATOR_MATCH, |
| 428 | (__force u64)ila->p.ip.locator_match, |
| 429 | ILA_ATTR_PAD) || |
Tom Herbert | 7f00fea | 2015-12-15 15:41:38 -0800 | [diff] [blame] | 430 | nla_put_s32(msg, ILA_ATTR_IFINDEX, ila->p.ifindex) || |
| 431 | nla_put_u32(msg, ILA_ATTR_DIR, ila->p.dir)) |
| 432 | return -1; |
| 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | static int ila_dump_info(struct ila_map *ila, |
| 438 | u32 portid, u32 seq, u32 flags, |
| 439 | struct sk_buff *skb, u8 cmd) |
| 440 | { |
| 441 | void *hdr; |
| 442 | |
| 443 | hdr = genlmsg_put(skb, portid, seq, &ila_nl_family, flags, cmd); |
| 444 | if (!hdr) |
| 445 | return -ENOMEM; |
| 446 | |
| 447 | if (ila_fill_info(ila, skb) < 0) |
| 448 | goto nla_put_failure; |
| 449 | |
| 450 | genlmsg_end(skb, hdr); |
| 451 | return 0; |
| 452 | |
| 453 | nla_put_failure: |
| 454 | genlmsg_cancel(skb, hdr); |
| 455 | return -EMSGSIZE; |
| 456 | } |
| 457 | |
| 458 | static int ila_nl_cmd_get_mapping(struct sk_buff *skb, struct genl_info *info) |
| 459 | { |
| 460 | struct net *net = genl_info_net(info); |
| 461 | struct ila_net *ilan = net_generic(net, ila_net_id); |
| 462 | struct sk_buff *msg; |
| 463 | struct ila_xlat_params p; |
| 464 | struct ila_map *ila; |
| 465 | int ret; |
| 466 | |
| 467 | ret = parse_nl_config(info, &p); |
| 468 | if (ret) |
| 469 | return ret; |
| 470 | |
| 471 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 472 | if (!msg) |
| 473 | return -ENOMEM; |
| 474 | |
| 475 | rcu_read_lock(); |
| 476 | |
| 477 | ila = ila_lookup_by_params(&p, ilan); |
| 478 | if (ila) { |
| 479 | ret = ila_dump_info(ila, |
| 480 | info->snd_portid, |
| 481 | info->snd_seq, 0, msg, |
| 482 | info->genlhdr->cmd); |
| 483 | } |
| 484 | |
| 485 | rcu_read_unlock(); |
| 486 | |
| 487 | if (ret < 0) |
| 488 | goto out_free; |
| 489 | |
| 490 | return genlmsg_reply(msg, info); |
| 491 | |
| 492 | out_free: |
| 493 | nlmsg_free(msg); |
| 494 | return ret; |
| 495 | } |
| 496 | |
| 497 | struct ila_dump_iter { |
| 498 | struct rhashtable_iter rhiter; |
| 499 | }; |
| 500 | |
| 501 | static int ila_nl_dump_start(struct netlink_callback *cb) |
| 502 | { |
| 503 | struct net *net = sock_net(cb->skb->sk); |
| 504 | struct ila_net *ilan = net_generic(net, ila_net_id); |
| 505 | struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args; |
| 506 | |
Bob Copeland | 8f6fd83 | 2016-03-02 10:09:19 -0500 | [diff] [blame] | 507 | return rhashtable_walk_init(&ilan->rhash_table, &iter->rhiter, |
| 508 | GFP_KERNEL); |
Tom Herbert | 7f00fea | 2015-12-15 15:41:38 -0800 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | static int ila_nl_dump_done(struct netlink_callback *cb) |
| 512 | { |
| 513 | struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args; |
| 514 | |
| 515 | rhashtable_walk_exit(&iter->rhiter); |
| 516 | |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | static int ila_nl_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 521 | { |
| 522 | struct ila_dump_iter *iter = (struct ila_dump_iter *)cb->args; |
| 523 | struct rhashtable_iter *rhiter = &iter->rhiter; |
| 524 | struct ila_map *ila; |
| 525 | int ret; |
| 526 | |
| 527 | ret = rhashtable_walk_start(rhiter); |
| 528 | if (ret && ret != -EAGAIN) |
| 529 | goto done; |
| 530 | |
| 531 | for (;;) { |
| 532 | ila = rhashtable_walk_next(rhiter); |
| 533 | |
| 534 | if (IS_ERR(ila)) { |
| 535 | if (PTR_ERR(ila) == -EAGAIN) |
| 536 | continue; |
| 537 | ret = PTR_ERR(ila); |
| 538 | goto done; |
| 539 | } else if (!ila) { |
| 540 | break; |
| 541 | } |
| 542 | |
| 543 | while (ila) { |
| 544 | ret = ila_dump_info(ila, NETLINK_CB(cb->skb).portid, |
| 545 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 546 | skb, ILA_CMD_GET); |
| 547 | if (ret) |
| 548 | goto done; |
| 549 | |
| 550 | ila = rcu_access_pointer(ila->next); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | ret = skb->len; |
| 555 | |
| 556 | done: |
| 557 | rhashtable_walk_stop(rhiter); |
| 558 | return ret; |
| 559 | } |
| 560 | |
| 561 | static const struct genl_ops ila_nl_ops[] = { |
| 562 | { |
| 563 | .cmd = ILA_CMD_ADD, |
| 564 | .doit = ila_nl_cmd_add_mapping, |
| 565 | .policy = ila_nl_policy, |
| 566 | .flags = GENL_ADMIN_PERM, |
| 567 | }, |
| 568 | { |
| 569 | .cmd = ILA_CMD_DEL, |
| 570 | .doit = ila_nl_cmd_del_mapping, |
| 571 | .policy = ila_nl_policy, |
| 572 | .flags = GENL_ADMIN_PERM, |
| 573 | }, |
| 574 | { |
| 575 | .cmd = ILA_CMD_GET, |
| 576 | .doit = ila_nl_cmd_get_mapping, |
| 577 | .start = ila_nl_dump_start, |
| 578 | .dumpit = ila_nl_dump, |
| 579 | .done = ila_nl_dump_done, |
| 580 | .policy = ila_nl_policy, |
| 581 | }, |
| 582 | }; |
| 583 | |
| 584 | #define ILA_HASH_TABLE_SIZE 1024 |
| 585 | |
| 586 | static __net_init int ila_init_net(struct net *net) |
| 587 | { |
| 588 | int err; |
| 589 | struct ila_net *ilan = net_generic(net, ila_net_id); |
| 590 | |
| 591 | err = alloc_ila_locks(ilan); |
| 592 | if (err) |
| 593 | return err; |
| 594 | |
| 595 | rhashtable_init(&ilan->rhash_table, &rht_params); |
| 596 | |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | static __net_exit void ila_exit_net(struct net *net) |
| 601 | { |
| 602 | struct ila_net *ilan = net_generic(net, ila_net_id); |
| 603 | |
| 604 | rhashtable_free_and_destroy(&ilan->rhash_table, ila_free_cb, NULL); |
| 605 | |
| 606 | kvfree(ilan->locks); |
| 607 | |
| 608 | if (ilan->hooks_registered) |
| 609 | nf_unregister_net_hooks(net, ila_nf_hook_ops, |
| 610 | ARRAY_SIZE(ila_nf_hook_ops)); |
| 611 | } |
| 612 | |
| 613 | static struct pernet_operations ila_net_ops = { |
| 614 | .init = ila_init_net, |
| 615 | .exit = ila_exit_net, |
| 616 | .id = &ila_net_id, |
| 617 | .size = sizeof(struct ila_net), |
| 618 | }; |
| 619 | |
| 620 | static int ila_xlat_addr(struct sk_buff *skb, int dir) |
| 621 | { |
| 622 | struct ila_map *ila; |
| 623 | struct ipv6hdr *ip6h = ipv6_hdr(skb); |
| 624 | struct net *net = dev_net(skb->dev); |
| 625 | struct ila_net *ilan = net_generic(net, ila_net_id); |
| 626 | __be64 identifier, locator_match; |
| 627 | size_t nhoff; |
| 628 | |
| 629 | /* Assumes skb contains a valid IPv6 header that is pulled */ |
| 630 | |
| 631 | identifier = *(__be64 *)&ip6h->daddr.in6_u.u6_addr8[8]; |
| 632 | locator_match = *(__be64 *)&ip6h->daddr.in6_u.u6_addr8[0]; |
| 633 | nhoff = sizeof(struct ipv6hdr); |
| 634 | |
| 635 | rcu_read_lock(); |
| 636 | |
| 637 | ila = ila_lookup_wildcards(identifier, locator_match, |
| 638 | skb->dev->ifindex, dir, ilan); |
| 639 | if (ila) |
| 640 | update_ipv6_locator(skb, &ila->p.ip); |
| 641 | |
| 642 | rcu_read_unlock(); |
| 643 | |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | int ila_xlat_incoming(struct sk_buff *skb) |
| 648 | { |
| 649 | return ila_xlat_addr(skb, ILA_DIR_IN); |
| 650 | } |
| 651 | EXPORT_SYMBOL(ila_xlat_incoming); |
| 652 | |
| 653 | int ila_xlat_outgoing(struct sk_buff *skb) |
| 654 | { |
| 655 | return ila_xlat_addr(skb, ILA_DIR_OUT); |
| 656 | } |
| 657 | EXPORT_SYMBOL(ila_xlat_outgoing); |
| 658 | |
| 659 | int ila_xlat_init(void) |
| 660 | { |
| 661 | int ret; |
| 662 | |
| 663 | ret = register_pernet_device(&ila_net_ops); |
| 664 | if (ret) |
| 665 | goto exit; |
| 666 | |
| 667 | ret = genl_register_family_with_ops(&ila_nl_family, |
| 668 | ila_nl_ops); |
| 669 | if (ret < 0) |
| 670 | goto unregister; |
| 671 | |
| 672 | return 0; |
| 673 | |
| 674 | unregister: |
| 675 | unregister_pernet_device(&ila_net_ops); |
| 676 | exit: |
| 677 | return ret; |
| 678 | } |
| 679 | |
| 680 | void ila_xlat_fini(void) |
| 681 | { |
| 682 | genl_unregister_family(&ila_nl_family); |
| 683 | unregister_pernet_device(&ila_net_ops); |
| 684 | } |