1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* Copyright (c) 2021 Taehee Yoo <ap420073@gmail.com> */ 3 4 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 5 6 #include <linux/module.h> 7 #include <linux/skbuff.h> 8 #include <linux/udp.h> 9 #include <linux/jhash.h> 10 #include <linux/if_tunnel.h> 11 #include <linux/net.h> 12 #include <linux/igmp.h> 13 #include <linux/workqueue.h> 14 #include <net/flow.h> 15 #include <net/pkt_sched.h> 16 #include <net/net_namespace.h> 17 #include <net/ip.h> 18 #include <net/udp.h> 19 #include <net/udp_tunnel.h> 20 #include <net/icmp.h> 21 #include <net/mld.h> 22 #include <net/amt.h> 23 #include <uapi/linux/amt.h> 24 #include <linux/security.h> 25 #include <net/gro_cells.h> 26 #include <net/ipv6.h> 27 #include <net/if_inet6.h> 28 #include <net/ndisc.h> 29 #include <net/addrconf.h> 30 #include <net/ip6_route.h> 31 #include <net/inet_common.h> 32 #include <net/inet_dscp.h> 33 #include <net/ip6_checksum.h> 34 35 static struct workqueue_struct *amt_wq; 36 37 static HLIST_HEAD(source_gc_list); 38 /* Lock for source_gc_list */ 39 static spinlock_t source_gc_lock; 40 static struct delayed_work source_gc_wq; 41 static char *status_str[] = { 42 "AMT_STATUS_INIT", 43 "AMT_STATUS_SENT_DISCOVERY", 44 "AMT_STATUS_RECEIVED_DISCOVERY", 45 "AMT_STATUS_SENT_ADVERTISEMENT", 46 "AMT_STATUS_RECEIVED_ADVERTISEMENT", 47 "AMT_STATUS_SENT_REQUEST", 48 "AMT_STATUS_RECEIVED_REQUEST", 49 "AMT_STATUS_SENT_QUERY", 50 "AMT_STATUS_RECEIVED_QUERY", 51 "AMT_STATUS_SENT_UPDATE", 52 "AMT_STATUS_RECEIVED_UPDATE", 53 }; 54 55 static char *type_str[] = { 56 "", /* Type 0 is not defined */ 57 "AMT_MSG_DISCOVERY", 58 "AMT_MSG_ADVERTISEMENT", 59 "AMT_MSG_REQUEST", 60 "AMT_MSG_MEMBERSHIP_QUERY", 61 "AMT_MSG_MEMBERSHIP_UPDATE", 62 "AMT_MSG_MULTICAST_DATA", 63 "AMT_MSG_TEARDOWN", 64 }; 65 66 static char *action_str[] = { 67 "AMT_ACT_GMI", 68 "AMT_ACT_GMI_ZERO", 69 "AMT_ACT_GT", 70 "AMT_ACT_STATUS_FWD_NEW", 71 "AMT_ACT_STATUS_D_FWD_NEW", 72 "AMT_ACT_STATUS_NONE_NEW", 73 }; 74 75 static struct igmpv3_grec igmpv3_zero_grec; 76 77 #if IS_ENABLED(CONFIG_IPV6) 78 #define MLD2_ALL_NODE_INIT { { { 0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01 } } } 79 static struct in6_addr mld2_all_node = MLD2_ALL_NODE_INIT; 80 static struct mld2_grec mldv2_zero_grec; 81 #endif 82 83 static struct amt_skb_cb *amt_skb_cb(struct sk_buff *skb) 84 { 85 BUILD_BUG_ON(sizeof(struct amt_skb_cb) + sizeof(struct tc_skb_cb) > 86 sizeof_field(struct sk_buff, cb)); 87 88 return (struct amt_skb_cb *)((void *)skb->cb + 89 sizeof(struct tc_skb_cb)); 90 } 91 92 static void __amt_source_gc_work(void) 93 { 94 struct amt_source_node *snode; 95 struct hlist_head gc_list; 96 struct hlist_node *t; 97 98 spin_lock_bh(&source_gc_lock); 99 hlist_move_list(&source_gc_list, &gc_list); 100 spin_unlock_bh(&source_gc_lock); 101 102 hlist_for_each_entry_safe(snode, t, &gc_list, node) { 103 hlist_del_rcu(&snode->node); 104 kfree_rcu(snode, rcu); 105 } 106 } 107 108 static void amt_source_gc_work(struct work_struct *work) 109 { 110 __amt_source_gc_work(); 111 112 spin_lock_bh(&source_gc_lock); 113 mod_delayed_work(amt_wq, &source_gc_wq, 114 msecs_to_jiffies(AMT_GC_INTERVAL)); 115 spin_unlock_bh(&source_gc_lock); 116 } 117 118 static bool amt_addr_equal(union amt_addr *a, union amt_addr *b) 119 { 120 return !memcmp(a, b, sizeof(union amt_addr)); 121 } 122 123 static u32 amt_source_hash(struct amt_tunnel_list *tunnel, union amt_addr *src) 124 { 125 u32 hash = jhash(src, sizeof(*src), tunnel->amt->hash_seed); 126 127 return reciprocal_scale(hash, tunnel->amt->hash_buckets); 128 } 129 130 static bool amt_status_filter(struct amt_source_node *snode, 131 enum amt_filter filter) 132 { 133 bool rc = false; 134 135 switch (filter) { 136 case AMT_FILTER_FWD: 137 if (snode->status == AMT_SOURCE_STATUS_FWD && 138 snode->flags == AMT_SOURCE_OLD) 139 rc = true; 140 break; 141 case AMT_FILTER_D_FWD: 142 if (snode->status == AMT_SOURCE_STATUS_D_FWD && 143 snode->flags == AMT_SOURCE_OLD) 144 rc = true; 145 break; 146 case AMT_FILTER_FWD_NEW: 147 if (snode->status == AMT_SOURCE_STATUS_FWD && 148 snode->flags == AMT_SOURCE_NEW) 149 rc = true; 150 break; 151 case AMT_FILTER_D_FWD_NEW: 152 if (snode->status == AMT_SOURCE_STATUS_D_FWD && 153 snode->flags == AMT_SOURCE_NEW) 154 rc = true; 155 break; 156 case AMT_FILTER_ALL: 157 rc = true; 158 break; 159 case AMT_FILTER_NONE_NEW: 160 if (snode->status == AMT_SOURCE_STATUS_NONE && 161 snode->flags == AMT_SOURCE_NEW) 162 rc = true; 163 break; 164 case AMT_FILTER_BOTH: 165 if ((snode->status == AMT_SOURCE_STATUS_D_FWD || 166 snode->status == AMT_SOURCE_STATUS_FWD) && 167 snode->flags == AMT_SOURCE_OLD) 168 rc = true; 169 break; 170 case AMT_FILTER_BOTH_NEW: 171 if ((snode->status == AMT_SOURCE_STATUS_D_FWD || 172 snode->status == AMT_SOURCE_STATUS_FWD) && 173 snode->flags == AMT_SOURCE_NEW) 174 rc = true; 175 break; 176 default: 177 WARN_ON_ONCE(1); 178 break; 179 } 180 181 return rc; 182 } 183 184 static struct amt_source_node *amt_lookup_src(struct amt_tunnel_list *tunnel, 185 struct amt_group_node *gnode, 186 enum amt_filter filter, 187 union amt_addr *src) 188 { 189 u32 hash = amt_source_hash(tunnel, src); 190 struct amt_source_node *snode; 191 192 hlist_for_each_entry_rcu(snode, &gnode->sources[hash], node) 193 if (amt_status_filter(snode, filter) && 194 amt_addr_equal(&snode->source_addr, src)) 195 return snode; 196 197 return NULL; 198 } 199 200 static u32 amt_group_hash(struct amt_tunnel_list *tunnel, union amt_addr *group) 201 { 202 u32 hash = jhash(group, sizeof(*group), tunnel->amt->hash_seed); 203 204 return reciprocal_scale(hash, tunnel->amt->hash_buckets); 205 } 206 207 static struct amt_group_node *amt_lookup_group(struct amt_tunnel_list *tunnel, 208 union amt_addr *group, 209 union amt_addr *host, 210 bool v6) 211 { 212 u32 hash = amt_group_hash(tunnel, group); 213 struct amt_group_node *gnode; 214 215 hlist_for_each_entry_rcu(gnode, &tunnel->groups[hash], node) { 216 if (amt_addr_equal(&gnode->group_addr, group) && 217 amt_addr_equal(&gnode->host_addr, host) && 218 gnode->v6 == v6) 219 return gnode; 220 } 221 222 return NULL; 223 } 224 225 static void amt_destroy_source(struct amt_source_node *snode) 226 { 227 struct amt_group_node *gnode = snode->gnode; 228 struct amt_tunnel_list *tunnel; 229 230 tunnel = gnode->tunnel_list; 231 232 if (!gnode->v6) { 233 netdev_dbg(snode->gnode->amt->dev, 234 "Delete source %pI4 from %pI4\n", 235 &snode->source_addr.ip4, 236 &gnode->group_addr.ip4); 237 #if IS_ENABLED(CONFIG_IPV6) 238 } else { 239 netdev_dbg(snode->gnode->amt->dev, 240 "Delete source %pI6 from %pI6\n", 241 &snode->source_addr.ip6, 242 &gnode->group_addr.ip6); 243 #endif 244 } 245 246 cancel_delayed_work(&snode->source_timer); 247 hlist_del_init_rcu(&snode->node); 248 tunnel->nr_sources--; 249 gnode->nr_sources--; 250 spin_lock_bh(&source_gc_lock); 251 hlist_add_head_rcu(&snode->node, &source_gc_list); 252 spin_unlock_bh(&source_gc_lock); 253 } 254 255 static void amt_del_group(struct amt_dev *amt, struct amt_group_node *gnode) 256 { 257 struct amt_source_node *snode; 258 struct hlist_node *t; 259 int i; 260 261 if (cancel_delayed_work(&gnode->group_timer)) 262 dev_put(amt->dev); 263 hlist_del_rcu(&gnode->node); 264 gnode->tunnel_list->nr_groups--; 265 266 if (!gnode->v6) 267 netdev_dbg(amt->dev, "Leave group %pI4\n", 268 &gnode->group_addr.ip4); 269 #if IS_ENABLED(CONFIG_IPV6) 270 else 271 netdev_dbg(amt->dev, "Leave group %pI6\n", 272 &gnode->group_addr.ip6); 273 #endif 274 for (i = 0; i < amt->hash_buckets; i++) 275 hlist_for_each_entry_safe(snode, t, &gnode->sources[i], node) 276 amt_destroy_source(snode); 277 278 /* tunnel->lock was acquired outside of amt_del_group() 279 * But rcu_read_lock() was acquired too so It's safe. 280 */ 281 kfree_rcu(gnode, rcu); 282 } 283 284 /* If a source timer expires with a router filter-mode for the group of 285 * INCLUDE, the router concludes that traffic from this particular 286 * source is no longer desired on the attached network, and deletes the 287 * associated source record. 288 */ 289 static void amt_source_work(struct work_struct *work) 290 { 291 struct amt_source_node *snode = container_of(to_delayed_work(work), 292 struct amt_source_node, 293 source_timer); 294 struct amt_group_node *gnode = snode->gnode; 295 struct amt_dev *amt = gnode->amt; 296 struct amt_tunnel_list *tunnel; 297 298 tunnel = gnode->tunnel_list; 299 spin_lock_bh(&tunnel->lock); 300 rcu_read_lock(); 301 if (gnode->filter_mode == MCAST_INCLUDE) { 302 amt_destroy_source(snode); 303 if (!gnode->nr_sources) 304 amt_del_group(amt, gnode); 305 } else { 306 /* When a router filter-mode for a group is EXCLUDE, 307 * source records are only deleted when the group timer expires 308 */ 309 snode->status = AMT_SOURCE_STATUS_D_FWD; 310 } 311 rcu_read_unlock(); 312 spin_unlock_bh(&tunnel->lock); 313 } 314 315 static void amt_act_src(struct amt_tunnel_list *tunnel, 316 struct amt_group_node *gnode, 317 struct amt_source_node *snode, 318 enum amt_act act) 319 { 320 struct amt_dev *amt = tunnel->amt; 321 322 switch (act) { 323 case AMT_ACT_GMI: 324 mod_delayed_work(amt_wq, &snode->source_timer, 325 msecs_to_jiffies(amt_gmi(amt))); 326 break; 327 case AMT_ACT_GMI_ZERO: 328 cancel_delayed_work(&snode->source_timer); 329 break; 330 case AMT_ACT_GT: 331 mod_delayed_work(amt_wq, &snode->source_timer, 332 gnode->group_timer.timer.expires); 333 break; 334 case AMT_ACT_STATUS_FWD_NEW: 335 snode->status = AMT_SOURCE_STATUS_FWD; 336 snode->flags = AMT_SOURCE_NEW; 337 break; 338 case AMT_ACT_STATUS_D_FWD_NEW: 339 snode->status = AMT_SOURCE_STATUS_D_FWD; 340 snode->flags = AMT_SOURCE_NEW; 341 break; 342 case AMT_ACT_STATUS_NONE_NEW: 343 cancel_delayed_work(&snode->source_timer); 344 snode->status = AMT_SOURCE_STATUS_NONE; 345 snode->flags = AMT_SOURCE_NEW; 346 break; 347 default: 348 WARN_ON_ONCE(1); 349 return; 350 } 351 352 if (!gnode->v6) 353 netdev_dbg(amt->dev, "Source %pI4 from %pI4 Acted %s\n", 354 &snode->source_addr.ip4, 355 &gnode->group_addr.ip4, 356 action_str[act]); 357 #if IS_ENABLED(CONFIG_IPV6) 358 else 359 netdev_dbg(amt->dev, "Source %pI6 from %pI6 Acted %s\n", 360 &snode->source_addr.ip6, 361 &gnode->group_addr.ip6, 362 action_str[act]); 363 #endif 364 } 365 366 static struct amt_source_node *amt_alloc_snode(struct amt_group_node *gnode, 367 union amt_addr *src) 368 { 369 struct amt_source_node *snode; 370 371 snode = kzalloc_obj(*snode, GFP_ATOMIC); 372 if (!snode) 373 return NULL; 374 375 memcpy(&snode->source_addr, src, sizeof(union amt_addr)); 376 snode->gnode = gnode; 377 snode->status = AMT_SOURCE_STATUS_NONE; 378 snode->flags = AMT_SOURCE_NEW; 379 INIT_HLIST_NODE(&snode->node); 380 INIT_DELAYED_WORK(&snode->source_timer, amt_source_work); 381 382 return snode; 383 } 384 385 /* RFC 3810 - 7.2.2. Definition of Filter Timers 386 * 387 * Router Mode Filter Timer Actions/Comments 388 * ----------- ----------------- ---------------- 389 * 390 * INCLUDE Not Used All listeners in 391 * INCLUDE mode. 392 * 393 * EXCLUDE Timer > 0 At least one listener 394 * in EXCLUDE mode. 395 * 396 * EXCLUDE Timer == 0 No more listeners in 397 * EXCLUDE mode for the 398 * multicast address. 399 * If the Requested List 400 * is empty, delete 401 * Multicast Address 402 * Record. If not, switch 403 * to INCLUDE filter mode; 404 * the sources in the 405 * Requested List are 406 * moved to the Include 407 * List, and the Exclude 408 * List is deleted. 409 */ 410 static void amt_group_work(struct work_struct *work) 411 { 412 struct amt_group_node *gnode = container_of(to_delayed_work(work), 413 struct amt_group_node, 414 group_timer); 415 struct amt_tunnel_list *tunnel = gnode->tunnel_list; 416 struct amt_dev *amt = gnode->amt; 417 struct amt_source_node *snode; 418 bool delete_group = true; 419 struct hlist_node *t; 420 int i, buckets; 421 422 buckets = amt->hash_buckets; 423 424 spin_lock_bh(&tunnel->lock); 425 if (gnode->filter_mode == MCAST_INCLUDE) { 426 /* Not Used */ 427 spin_unlock_bh(&tunnel->lock); 428 goto out; 429 } 430 431 rcu_read_lock(); 432 for (i = 0; i < buckets; i++) { 433 hlist_for_each_entry_safe(snode, t, 434 &gnode->sources[i], node) { 435 if (!delayed_work_pending(&snode->source_timer) || 436 snode->status == AMT_SOURCE_STATUS_D_FWD) { 437 amt_destroy_source(snode); 438 } else { 439 delete_group = false; 440 snode->status = AMT_SOURCE_STATUS_FWD; 441 } 442 } 443 } 444 if (delete_group) 445 amt_del_group(amt, gnode); 446 else 447 gnode->filter_mode = MCAST_INCLUDE; 448 rcu_read_unlock(); 449 spin_unlock_bh(&tunnel->lock); 450 out: 451 dev_put(amt->dev); 452 } 453 454 /* Non-existent group is created as INCLUDE {empty}: 455 * 456 * RFC 3376 - 5.1. Action on Change of Interface State 457 * 458 * If no interface state existed for that multicast address before 459 * the change (i.e., the change consisted of creating a new 460 * per-interface record), or if no state exists after the change 461 * (i.e., the change consisted of deleting a per-interface record), 462 * then the "non-existent" state is considered to have a filter mode 463 * of INCLUDE and an empty source list. 464 */ 465 static struct amt_group_node *amt_add_group(struct amt_dev *amt, 466 struct amt_tunnel_list *tunnel, 467 union amt_addr *group, 468 union amt_addr *host, 469 bool v6) 470 { 471 struct amt_group_node *gnode; 472 u32 hash; 473 int i; 474 475 if (tunnel->nr_groups >= amt->max_groups) 476 return ERR_PTR(-ENOSPC); 477 478 gnode = kzalloc(sizeof(*gnode) + 479 (sizeof(struct hlist_head) * amt->hash_buckets), 480 GFP_ATOMIC); 481 if (unlikely(!gnode)) 482 return ERR_PTR(-ENOMEM); 483 484 gnode->amt = amt; 485 gnode->group_addr = *group; 486 gnode->host_addr = *host; 487 gnode->v6 = v6; 488 gnode->tunnel_list = tunnel; 489 gnode->filter_mode = MCAST_INCLUDE; 490 INIT_HLIST_NODE(&gnode->node); 491 INIT_DELAYED_WORK(&gnode->group_timer, amt_group_work); 492 for (i = 0; i < amt->hash_buckets; i++) 493 INIT_HLIST_HEAD(&gnode->sources[i]); 494 495 hash = amt_group_hash(tunnel, group); 496 hlist_add_head_rcu(&gnode->node, &tunnel->groups[hash]); 497 tunnel->nr_groups++; 498 499 if (!gnode->v6) 500 netdev_dbg(amt->dev, "Join group %pI4\n", 501 &gnode->group_addr.ip4); 502 #if IS_ENABLED(CONFIG_IPV6) 503 else 504 netdev_dbg(amt->dev, "Join group %pI6\n", 505 &gnode->group_addr.ip6); 506 #endif 507 508 return gnode; 509 } 510 511 static struct sk_buff *amt_build_igmp_gq(struct amt_dev *amt) 512 { 513 u8 ra[AMT_IPHDR_OPTS] = { IPOPT_RA, 4, 0, 0 }; 514 int hlen = LL_RESERVED_SPACE(amt->dev); 515 int tlen = amt->dev->needed_tailroom; 516 struct igmpv3_query *ihv3; 517 void *csum_start = NULL; 518 __sum16 *csum = NULL; 519 struct sk_buff *skb; 520 struct ethhdr *eth; 521 struct iphdr *iph; 522 unsigned int len; 523 int offset; 524 525 len = hlen + tlen + sizeof(*iph) + AMT_IPHDR_OPTS + sizeof(*ihv3); 526 skb = netdev_alloc_skb_ip_align(amt->dev, len); 527 if (!skb) 528 return NULL; 529 530 skb_reserve(skb, hlen); 531 skb_push(skb, sizeof(*eth)); 532 skb->protocol = htons(ETH_P_IP); 533 skb_reset_mac_header(skb); 534 skb->priority = TC_PRIO_CONTROL; 535 skb_put(skb, sizeof(*iph)); 536 skb_put_data(skb, ra, sizeof(ra)); 537 skb_put(skb, sizeof(*ihv3)); 538 skb_pull(skb, sizeof(*eth)); 539 skb_reset_network_header(skb); 540 541 iph = ip_hdr(skb); 542 iph->version = 4; 543 iph->ihl = (sizeof(struct iphdr) + AMT_IPHDR_OPTS) >> 2; 544 iph->tos = AMT_TOS; 545 iph->tot_len = htons(sizeof(*iph) + AMT_IPHDR_OPTS + sizeof(*ihv3)); 546 iph->frag_off = htons(IP_DF); 547 iph->ttl = 1; 548 iph->id = 0; 549 iph->protocol = IPPROTO_IGMP; 550 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP); 551 iph->saddr = htonl(INADDR_ANY); 552 ip_send_check(iph); 553 554 eth = eth_hdr(skb); 555 ether_addr_copy(eth->h_source, amt->dev->dev_addr); 556 ip_eth_mc_map(htonl(INADDR_ALLHOSTS_GROUP), eth->h_dest); 557 eth->h_proto = htons(ETH_P_IP); 558 559 ihv3 = skb_pull(skb, sizeof(*iph) + AMT_IPHDR_OPTS); 560 skb_reset_transport_header(skb); 561 ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY; 562 ihv3->code = 1; 563 ihv3->group = 0; 564 ihv3->qqic = amt->qi; 565 ihv3->nsrcs = 0; 566 ihv3->resv = 0; 567 ihv3->suppress = false; 568 ihv3->qrv = READ_ONCE(amt->net->ipv4.sysctl_igmp_qrv); 569 ihv3->csum = 0; 570 csum = &ihv3->csum; 571 csum_start = (void *)ihv3; 572 *csum = ip_compute_csum(csum_start, sizeof(*ihv3)); 573 offset = skb_transport_offset(skb); 574 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0); 575 skb->ip_summed = CHECKSUM_NONE; 576 577 skb_push(skb, sizeof(*eth) + sizeof(*iph) + AMT_IPHDR_OPTS); 578 579 return skb; 580 } 581 582 static void amt_update_gw_status(struct amt_dev *amt, enum amt_status status, 583 bool validate) 584 { 585 if (validate && amt->status >= status) 586 return; 587 netdev_dbg(amt->dev, "Update GW status %s -> %s", 588 status_str[amt->status], status_str[status]); 589 WRITE_ONCE(amt->status, status); 590 } 591 592 static void __amt_update_relay_status(struct amt_tunnel_list *tunnel, 593 enum amt_status status, 594 bool validate) 595 { 596 if (validate && tunnel->status >= status) 597 return; 598 netdev_dbg(tunnel->amt->dev, 599 "Update Tunnel(IP = %pI4, PORT = %u) status %s -> %s", 600 &tunnel->ip4, ntohs(tunnel->source_port), 601 status_str[tunnel->status], status_str[status]); 602 tunnel->status = status; 603 } 604 605 static void amt_update_relay_status(struct amt_tunnel_list *tunnel, 606 enum amt_status status, bool validate) 607 { 608 spin_lock_bh(&tunnel->lock); 609 __amt_update_relay_status(tunnel, status, validate); 610 spin_unlock_bh(&tunnel->lock); 611 } 612 613 static void amt_send_discovery(struct amt_dev *amt) 614 { 615 struct amt_header_discovery *amtd; 616 int hlen, tlen, offset; 617 struct udphdr *udph; 618 struct sk_buff *skb; 619 struct iphdr *iph; 620 struct rtable *rt; 621 struct flowi4 fl4; 622 struct sock *sk; 623 u32 len; 624 int err; 625 626 rcu_read_lock(); 627 sk = rcu_dereference(amt->sk); 628 if (!sk) 629 goto out; 630 631 if (!netif_running(amt->stream_dev) || !netif_running(amt->dev)) 632 goto out; 633 634 rt = ip_route_output_ports(amt->net, &fl4, sk, 635 amt->discovery_ip, amt->local_ip, 636 amt->gw_port, amt->relay_port, 637 IPPROTO_UDP, 0, 638 amt->stream_dev->ifindex); 639 if (IS_ERR(rt)) { 640 amt->dev->stats.tx_errors++; 641 goto out; 642 } 643 644 hlen = LL_RESERVED_SPACE(amt->dev); 645 tlen = amt->dev->needed_tailroom; 646 len = hlen + tlen + sizeof(*iph) + sizeof(*udph) + sizeof(*amtd); 647 skb = netdev_alloc_skb_ip_align(amt->dev, len); 648 if (!skb) { 649 ip_rt_put(rt); 650 amt->dev->stats.tx_errors++; 651 goto out; 652 } 653 654 skb->priority = TC_PRIO_CONTROL; 655 skb_dst_set(skb, &rt->dst); 656 657 len = sizeof(*iph) + sizeof(*udph) + sizeof(*amtd); 658 skb_reset_network_header(skb); 659 skb_put(skb, len); 660 amtd = skb_pull(skb, sizeof(*iph) + sizeof(*udph)); 661 amtd->version = 0; 662 amtd->type = AMT_MSG_DISCOVERY; 663 amtd->reserved = 0; 664 amtd->nonce = amt->nonce; 665 skb_push(skb, sizeof(*udph)); 666 skb_reset_transport_header(skb); 667 udph = udp_hdr(skb); 668 udph->source = amt->gw_port; 669 udph->dest = amt->relay_port; 670 udph->len = htons(sizeof(*udph) + sizeof(*amtd)); 671 udph->check = 0; 672 offset = skb_transport_offset(skb); 673 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0); 674 udph->check = csum_tcpudp_magic(amt->local_ip, amt->discovery_ip, 675 sizeof(*udph) + sizeof(*amtd), 676 IPPROTO_UDP, skb->csum); 677 678 skb_push(skb, sizeof(*iph)); 679 iph = ip_hdr(skb); 680 iph->version = 4; 681 iph->ihl = (sizeof(struct iphdr)) >> 2; 682 iph->tos = AMT_TOS; 683 iph->frag_off = 0; 684 iph->ttl = ip4_dst_hoplimit(&rt->dst); 685 iph->daddr = amt->discovery_ip; 686 iph->saddr = amt->local_ip; 687 iph->protocol = IPPROTO_UDP; 688 iph->tot_len = htons(len); 689 690 skb->ip_summed = CHECKSUM_NONE; 691 ip_select_ident(amt->net, skb, NULL); 692 ip_send_check(iph); 693 err = ip_local_out(amt->net, sk, skb); 694 if (unlikely(net_xmit_eval(err))) 695 amt->dev->stats.tx_errors++; 696 697 amt_update_gw_status(amt, AMT_STATUS_SENT_DISCOVERY, true); 698 out: 699 rcu_read_unlock(); 700 } 701 702 static void amt_send_request(struct amt_dev *amt, bool v6) 703 { 704 struct amt_header_request *amtrh; 705 int hlen, tlen, offset; 706 struct udphdr *udph; 707 struct sk_buff *skb; 708 struct iphdr *iph; 709 struct rtable *rt; 710 struct flowi4 fl4; 711 __be32 remote_ip; 712 struct sock *sk; 713 u32 len; 714 int err; 715 716 rcu_read_lock(); 717 remote_ip = READ_ONCE(amt->remote_ip); 718 sk = rcu_dereference(amt->sk); 719 if (!sk) 720 goto out; 721 722 if (!netif_running(amt->stream_dev) || !netif_running(amt->dev)) 723 goto out; 724 725 rt = ip_route_output_ports(amt->net, &fl4, sk, 726 remote_ip, amt->local_ip, 727 amt->gw_port, amt->relay_port, 728 IPPROTO_UDP, 0, 729 amt->stream_dev->ifindex); 730 if (IS_ERR(rt)) { 731 amt->dev->stats.tx_errors++; 732 goto out; 733 } 734 735 hlen = LL_RESERVED_SPACE(amt->dev); 736 tlen = amt->dev->needed_tailroom; 737 len = hlen + tlen + sizeof(*iph) + sizeof(*udph) + sizeof(*amtrh); 738 skb = netdev_alloc_skb_ip_align(amt->dev, len); 739 if (!skb) { 740 ip_rt_put(rt); 741 amt->dev->stats.tx_errors++; 742 goto out; 743 } 744 745 skb->priority = TC_PRIO_CONTROL; 746 skb_dst_set(skb, &rt->dst); 747 748 len = sizeof(*iph) + sizeof(*udph) + sizeof(*amtrh); 749 skb_reset_network_header(skb); 750 skb_put(skb, len); 751 amtrh = skb_pull(skb, sizeof(*iph) + sizeof(*udph)); 752 amtrh->version = 0; 753 amtrh->type = AMT_MSG_REQUEST; 754 amtrh->reserved1 = 0; 755 amtrh->p = v6; 756 amtrh->reserved2 = 0; 757 amtrh->nonce = amt->nonce; 758 skb_push(skb, sizeof(*udph)); 759 skb_reset_transport_header(skb); 760 udph = udp_hdr(skb); 761 udph->source = amt->gw_port; 762 udph->dest = amt->relay_port; 763 udph->len = htons(sizeof(*amtrh) + sizeof(*udph)); 764 udph->check = 0; 765 offset = skb_transport_offset(skb); 766 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0); 767 udph->check = csum_tcpudp_magic(amt->local_ip, remote_ip, 768 sizeof(*udph) + sizeof(*amtrh), 769 IPPROTO_UDP, skb->csum); 770 771 skb_push(skb, sizeof(*iph)); 772 iph = ip_hdr(skb); 773 iph->version = 4; 774 iph->ihl = (sizeof(struct iphdr)) >> 2; 775 iph->tos = AMT_TOS; 776 iph->frag_off = 0; 777 iph->ttl = ip4_dst_hoplimit(&rt->dst); 778 iph->daddr = remote_ip; 779 iph->saddr = amt->local_ip; 780 iph->protocol = IPPROTO_UDP; 781 iph->tot_len = htons(len); 782 783 skb->ip_summed = CHECKSUM_NONE; 784 ip_select_ident(amt->net, skb, NULL); 785 ip_send_check(iph); 786 err = ip_local_out(amt->net, sk, skb); 787 if (unlikely(net_xmit_eval(err))) 788 amt->dev->stats.tx_errors++; 789 790 out: 791 rcu_read_unlock(); 792 } 793 794 static void amt_send_igmp_gq(struct amt_dev *amt, 795 struct amt_tunnel_list *tunnel) 796 { 797 struct sk_buff *skb; 798 799 skb = amt_build_igmp_gq(amt); 800 if (!skb) 801 return; 802 803 amt_skb_cb(skb)->tunnel = tunnel; 804 dev_queue_xmit(skb); 805 } 806 807 #if IS_ENABLED(CONFIG_IPV6) 808 static struct sk_buff *amt_build_mld_gq(struct amt_dev *amt) 809 { 810 u8 ra[AMT_IP6HDR_OPTS] = { IPPROTO_ICMPV6, 0, IPV6_TLV_ROUTERALERT, 811 2, 0, 0, IPV6_TLV_PAD1, IPV6_TLV_PAD1 }; 812 int hlen = LL_RESERVED_SPACE(amt->dev); 813 int tlen = amt->dev->needed_tailroom; 814 struct mld2_query *mld2q; 815 void *csum_start = NULL; 816 struct ipv6hdr *ip6h; 817 struct sk_buff *skb; 818 struct ethhdr *eth; 819 u32 len; 820 821 len = hlen + tlen + sizeof(*ip6h) + sizeof(ra) + sizeof(*mld2q); 822 skb = netdev_alloc_skb_ip_align(amt->dev, len); 823 if (!skb) 824 return NULL; 825 826 skb_reserve(skb, hlen); 827 skb_push(skb, sizeof(*eth)); 828 skb_reset_mac_header(skb); 829 eth = eth_hdr(skb); 830 skb->priority = TC_PRIO_CONTROL; 831 skb->protocol = htons(ETH_P_IPV6); 832 skb_put_zero(skb, sizeof(*ip6h)); 833 skb_put_data(skb, ra, sizeof(ra)); 834 skb_put_zero(skb, sizeof(*mld2q)); 835 skb_pull(skb, sizeof(*eth)); 836 skb_reset_network_header(skb); 837 ip6h = ipv6_hdr(skb); 838 ip6h->payload_len = htons(sizeof(ra) + sizeof(*mld2q)); 839 ip6h->nexthdr = NEXTHDR_HOP; 840 ip6h->hop_limit = 1; 841 ip6h->daddr = mld2_all_node; 842 ip6_flow_hdr(ip6h, 0, 0); 843 844 if (ipv6_dev_get_saddr(amt->net, amt->dev, &ip6h->daddr, 0, 845 &ip6h->saddr)) { 846 amt->dev->stats.tx_errors++; 847 kfree_skb(skb); 848 return NULL; 849 } 850 851 eth->h_proto = htons(ETH_P_IPV6); 852 ether_addr_copy(eth->h_source, amt->dev->dev_addr); 853 ipv6_eth_mc_map(&mld2_all_node, eth->h_dest); 854 855 skb_pull(skb, sizeof(*ip6h) + sizeof(ra)); 856 skb_reset_transport_header(skb); 857 mld2q = (struct mld2_query *)icmp6_hdr(skb); 858 mld2q->mld2q_mrc = htons(1); 859 mld2q->mld2q_type = ICMPV6_MGM_QUERY; 860 mld2q->mld2q_code = 0; 861 mld2q->mld2q_cksum = 0; 862 mld2q->mld2q_resv1 = 0; 863 mld2q->mld2q_resv2 = 0; 864 mld2q->mld2q_suppress = 0; 865 mld2q->mld2q_qrv = amt->qrv; 866 mld2q->mld2q_nsrcs = 0; 867 mld2q->mld2q_qqic = amt->qi; 868 csum_start = (void *)mld2q; 869 mld2q->mld2q_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, 870 sizeof(*mld2q), 871 IPPROTO_ICMPV6, 872 csum_partial(csum_start, 873 sizeof(*mld2q), 0)); 874 875 skb->ip_summed = CHECKSUM_NONE; 876 skb_push(skb, sizeof(*eth) + sizeof(*ip6h) + sizeof(ra)); 877 return skb; 878 } 879 880 static void amt_send_mld_gq(struct amt_dev *amt, struct amt_tunnel_list *tunnel) 881 { 882 struct sk_buff *skb; 883 884 skb = amt_build_mld_gq(amt); 885 if (!skb) 886 return; 887 888 amt_skb_cb(skb)->tunnel = tunnel; 889 dev_queue_xmit(skb); 890 } 891 #else 892 static void amt_send_mld_gq(struct amt_dev *amt, struct amt_tunnel_list *tunnel) 893 { 894 } 895 #endif 896 897 static bool amt_queue_event(struct amt_dev *amt, enum amt_event event, 898 struct sk_buff *skb) 899 { 900 int index; 901 902 spin_lock_bh(&amt->lock); 903 if (amt->nr_events >= AMT_MAX_EVENTS) { 904 spin_unlock_bh(&amt->lock); 905 return 1; 906 } 907 908 index = (amt->event_idx + amt->nr_events) % AMT_MAX_EVENTS; 909 amt->events[index].event = event; 910 amt->events[index].skb = skb; 911 amt->nr_events++; 912 amt->event_idx %= AMT_MAX_EVENTS; 913 queue_work(amt_wq, &amt->event_wq); 914 spin_unlock_bh(&amt->lock); 915 916 return 0; 917 } 918 919 static void amt_secret_work(struct work_struct *work) 920 { 921 struct amt_dev *amt = container_of(to_delayed_work(work), 922 struct amt_dev, 923 secret_wq); 924 925 spin_lock_bh(&amt->lock); 926 get_random_bytes(&amt->key, sizeof(siphash_key_t)); 927 spin_unlock_bh(&amt->lock); 928 mod_delayed_work(amt_wq, &amt->secret_wq, 929 msecs_to_jiffies(AMT_SECRET_TIMEOUT)); 930 } 931 932 static void amt_event_send_discovery(struct amt_dev *amt) 933 { 934 if (amt->status > AMT_STATUS_SENT_DISCOVERY) 935 goto out; 936 get_random_bytes(&amt->nonce, sizeof(__be32)); 937 938 amt_send_discovery(amt); 939 out: 940 mod_delayed_work(amt_wq, &amt->discovery_wq, 941 msecs_to_jiffies(AMT_DISCOVERY_TIMEOUT)); 942 } 943 944 static void amt_discovery_work(struct work_struct *work) 945 { 946 struct amt_dev *amt = container_of(to_delayed_work(work), 947 struct amt_dev, 948 discovery_wq); 949 950 if (amt_queue_event(amt, AMT_EVENT_SEND_DISCOVERY, NULL)) 951 mod_delayed_work(amt_wq, &amt->discovery_wq, 952 msecs_to_jiffies(AMT_DISCOVERY_TIMEOUT)); 953 } 954 955 static void amt_event_send_request(struct amt_dev *amt) 956 { 957 u32 exp; 958 959 if (amt->status < AMT_STATUS_RECEIVED_ADVERTISEMENT) 960 goto out; 961 962 if (amt->req_cnt > AMT_MAX_REQ_COUNT) { 963 netdev_dbg(amt->dev, "Gateway is not ready"); 964 amt->qi = AMT_INIT_REQ_TIMEOUT; 965 WRITE_ONCE(amt->ready4, false); 966 WRITE_ONCE(amt->ready6, false); 967 WRITE_ONCE(amt->remote_ip, 0); 968 amt_update_gw_status(amt, AMT_STATUS_INIT, false); 969 amt->req_cnt = 0; 970 amt->nonce = 0; 971 goto out; 972 } 973 974 if (!amt->req_cnt) { 975 WRITE_ONCE(amt->ready4, false); 976 WRITE_ONCE(amt->ready6, false); 977 get_random_bytes(&amt->nonce, sizeof(__be32)); 978 } 979 980 amt_send_request(amt, false); 981 amt_send_request(amt, true); 982 amt_update_gw_status(amt, AMT_STATUS_SENT_REQUEST, true); 983 amt->req_cnt++; 984 out: 985 exp = min_t(u32, (1 * (1 << amt->req_cnt)), AMT_MAX_REQ_TIMEOUT); 986 mod_delayed_work(amt_wq, &amt->req_wq, secs_to_jiffies(exp)); 987 } 988 989 static void amt_req_work(struct work_struct *work) 990 { 991 struct amt_dev *amt = container_of(to_delayed_work(work), 992 struct amt_dev, 993 req_wq); 994 995 if (amt_queue_event(amt, AMT_EVENT_SEND_REQUEST, NULL)) 996 mod_delayed_work(amt_wq, &amt->req_wq, 997 msecs_to_jiffies(100)); 998 } 999 1000 static bool amt_send_membership_update(struct amt_dev *amt, 1001 struct sk_buff *skb, 1002 bool v6) 1003 { 1004 __be32 remote_ip = READ_ONCE(amt->remote_ip); 1005 struct amt_header_membership_update *amtmu; 1006 struct iphdr *iph; 1007 struct flowi4 fl4; 1008 struct rtable *rt; 1009 struct sock *sk; 1010 int err; 1011 1012 sk = rcu_dereference_bh(amt->sk); 1013 if (!sk) 1014 return true; 1015 1016 err = skb_cow_head(skb, LL_RESERVED_SPACE(amt->dev) + sizeof(*amtmu) + 1017 sizeof(*iph) + sizeof(struct udphdr)); 1018 if (err) 1019 return true; 1020 1021 skb_reset_inner_headers(skb); 1022 memset(&fl4, 0, sizeof(struct flowi4)); 1023 fl4.flowi4_oif = amt->stream_dev->ifindex; 1024 fl4.daddr = remote_ip; 1025 fl4.saddr = amt->local_ip; 1026 fl4.flowi4_dscp = inet_dsfield_to_dscp(AMT_TOS); 1027 fl4.flowi4_proto = IPPROTO_UDP; 1028 rt = ip_route_output_key(amt->net, &fl4); 1029 if (IS_ERR(rt)) { 1030 netdev_dbg(amt->dev, "no route to %pI4\n", &remote_ip); 1031 return true; 1032 } 1033 1034 amtmu = skb_push(skb, sizeof(*amtmu)); 1035 amtmu->version = 0; 1036 amtmu->type = AMT_MSG_MEMBERSHIP_UPDATE; 1037 amtmu->reserved = 0; 1038 amtmu->nonce = amt->nonce; 1039 amtmu->response_mac = amt->mac; 1040 1041 if (!v6) 1042 skb_set_inner_protocol(skb, htons(ETH_P_IP)); 1043 else 1044 skb_set_inner_protocol(skb, htons(ETH_P_IPV6)); 1045 udp_tunnel_xmit_skb(rt, sk, skb, 1046 fl4.saddr, 1047 fl4.daddr, 1048 AMT_TOS, 1049 ip4_dst_hoplimit(&rt->dst), 1050 0, 1051 amt->gw_port, 1052 amt->relay_port, 1053 false, 1054 false, 1055 0); 1056 amt_update_gw_status(amt, AMT_STATUS_SENT_UPDATE, true); 1057 return false; 1058 } 1059 1060 static void amt_send_multicast_data(struct amt_dev *amt, 1061 const struct sk_buff *oskb, 1062 struct amt_tunnel_list *tunnel, 1063 bool v6) 1064 { 1065 struct amt_header_mcast_data *amtmd; 1066 struct sk_buff *skb; 1067 struct iphdr *iph; 1068 struct flowi4 fl4; 1069 struct rtable *rt; 1070 struct sock *sk; 1071 1072 sk = rcu_dereference_bh(amt->sk); 1073 if (!sk) 1074 return; 1075 1076 skb = skb_copy_expand(oskb, sizeof(*amtmd) + sizeof(*iph) + 1077 sizeof(struct udphdr), 0, GFP_ATOMIC); 1078 if (!skb) 1079 return; 1080 1081 skb_reset_inner_headers(skb); 1082 memset(&fl4, 0, sizeof(struct flowi4)); 1083 fl4.flowi4_oif = amt->stream_dev->ifindex; 1084 fl4.daddr = tunnel->ip4; 1085 fl4.saddr = amt->local_ip; 1086 fl4.flowi4_proto = IPPROTO_UDP; 1087 rt = ip_route_output_key(amt->net, &fl4); 1088 if (IS_ERR(rt)) { 1089 netdev_dbg(amt->dev, "no route to %pI4\n", &tunnel->ip4); 1090 kfree_skb(skb); 1091 return; 1092 } 1093 1094 amtmd = skb_push(skb, sizeof(*amtmd)); 1095 amtmd->version = 0; 1096 amtmd->reserved = 0; 1097 amtmd->type = AMT_MSG_MULTICAST_DATA; 1098 1099 if (!v6) 1100 skb_set_inner_protocol(skb, htons(ETH_P_IP)); 1101 else 1102 skb_set_inner_protocol(skb, htons(ETH_P_IPV6)); 1103 udp_tunnel_xmit_skb(rt, sk, skb, 1104 fl4.saddr, 1105 fl4.daddr, 1106 AMT_TOS, 1107 ip4_dst_hoplimit(&rt->dst), 1108 0, 1109 amt->relay_port, 1110 tunnel->source_port, 1111 false, 1112 false, 1113 0); 1114 } 1115 1116 static bool amt_send_membership_query(struct amt_dev *amt, 1117 struct sk_buff *skb, 1118 struct amt_tunnel_list *tunnel, 1119 bool v6) 1120 { 1121 struct amt_header_membership_query *amtmq; 1122 struct rtable *rt; 1123 struct flowi4 fl4; 1124 struct sock *sk; 1125 int err; 1126 1127 sk = rcu_dereference_bh(amt->sk); 1128 if (!sk) 1129 return true; 1130 1131 err = skb_cow_head(skb, LL_RESERVED_SPACE(amt->dev) + sizeof(*amtmq) + 1132 sizeof(struct iphdr) + sizeof(struct udphdr)); 1133 if (err) 1134 return true; 1135 1136 skb_reset_inner_headers(skb); 1137 memset(&fl4, 0, sizeof(struct flowi4)); 1138 fl4.flowi4_oif = amt->stream_dev->ifindex; 1139 fl4.daddr = tunnel->ip4; 1140 fl4.saddr = amt->local_ip; 1141 fl4.flowi4_dscp = inet_dsfield_to_dscp(AMT_TOS); 1142 fl4.flowi4_proto = IPPROTO_UDP; 1143 rt = ip_route_output_key(amt->net, &fl4); 1144 if (IS_ERR(rt)) { 1145 netdev_dbg(amt->dev, "no route to %pI4\n", &tunnel->ip4); 1146 return true; 1147 } 1148 1149 amtmq = skb_push(skb, sizeof(*amtmq)); 1150 amtmq->version = 0; 1151 amtmq->type = AMT_MSG_MEMBERSHIP_QUERY; 1152 amtmq->reserved = 0; 1153 amtmq->l = 0; 1154 amtmq->g = 0; 1155 amtmq->nonce = tunnel->nonce; 1156 amtmq->response_mac = tunnel->mac; 1157 1158 if (!v6) 1159 skb_set_inner_protocol(skb, htons(ETH_P_IP)); 1160 else 1161 skb_set_inner_protocol(skb, htons(ETH_P_IPV6)); 1162 udp_tunnel_xmit_skb(rt, sk, skb, 1163 fl4.saddr, 1164 fl4.daddr, 1165 AMT_TOS, 1166 ip4_dst_hoplimit(&rt->dst), 1167 0, 1168 amt->relay_port, 1169 tunnel->source_port, 1170 false, 1171 false, 1172 0); 1173 amt_update_relay_status(tunnel, AMT_STATUS_SENT_QUERY, true); 1174 return false; 1175 } 1176 1177 static netdev_tx_t amt_dev_xmit(struct sk_buff *skb, struct net_device *dev) 1178 { 1179 struct amt_dev *amt = netdev_priv(dev); 1180 struct amt_tunnel_list *tunnel; 1181 struct amt_group_node *gnode; 1182 union amt_addr group = {0,}; 1183 #if IS_ENABLED(CONFIG_IPV6) 1184 struct ipv6hdr *ip6h; 1185 struct mld_msg *mld; 1186 #endif 1187 bool report = false; 1188 struct igmphdr *ih; 1189 bool query = false; 1190 struct iphdr *iph; 1191 bool data = false; 1192 bool v6 = false; 1193 u32 hash; 1194 1195 iph = ip_hdr(skb); 1196 if (iph->version == 4) { 1197 if (!ipv4_is_multicast(iph->daddr)) 1198 goto free; 1199 1200 if (!ip_mc_check_igmp(skb)) { 1201 ih = igmp_hdr(skb); 1202 switch (ih->type) { 1203 case IGMPV3_HOST_MEMBERSHIP_REPORT: 1204 case IGMP_HOST_MEMBERSHIP_REPORT: 1205 report = true; 1206 break; 1207 case IGMP_HOST_MEMBERSHIP_QUERY: 1208 query = true; 1209 break; 1210 default: 1211 goto free; 1212 } 1213 } else { 1214 data = true; 1215 } 1216 v6 = false; 1217 group.ip4 = iph->daddr; 1218 #if IS_ENABLED(CONFIG_IPV6) 1219 } else if (iph->version == 6) { 1220 ip6h = ipv6_hdr(skb); 1221 if (!ipv6_addr_is_multicast(&ip6h->daddr)) 1222 goto free; 1223 1224 if (!ipv6_mc_check_mld(skb)) { 1225 mld = (struct mld_msg *)skb_transport_header(skb); 1226 switch (mld->mld_type) { 1227 case ICMPV6_MGM_REPORT: 1228 case ICMPV6_MLD2_REPORT: 1229 report = true; 1230 break; 1231 case ICMPV6_MGM_QUERY: 1232 query = true; 1233 break; 1234 default: 1235 goto free; 1236 } 1237 } else { 1238 data = true; 1239 } 1240 v6 = true; 1241 group.ip6 = ip6h->daddr; 1242 #endif 1243 } else { 1244 dev->stats.tx_errors++; 1245 goto free; 1246 } 1247 1248 if (!pskb_may_pull(skb, sizeof(struct ethhdr))) 1249 goto free; 1250 1251 skb_pull(skb, sizeof(struct ethhdr)); 1252 1253 if (amt->mode == AMT_MODE_GATEWAY) { 1254 /* Gateway only passes IGMP/MLD packets */ 1255 if (!report) 1256 goto free; 1257 if ((!v6 && !READ_ONCE(amt->ready4)) || 1258 (v6 && !READ_ONCE(amt->ready6))) 1259 goto free; 1260 if (amt_send_membership_update(amt, skb, v6)) 1261 goto free; 1262 goto unlock; 1263 } else if (amt->mode == AMT_MODE_RELAY) { 1264 if (query) { 1265 tunnel = amt_skb_cb(skb)->tunnel; 1266 if (!tunnel) { 1267 WARN_ON(1); 1268 goto free; 1269 } 1270 1271 /* Do not forward unexpected query */ 1272 if (amt_send_membership_query(amt, skb, tunnel, v6)) 1273 goto free; 1274 goto unlock; 1275 } 1276 1277 if (!data) 1278 goto free; 1279 list_for_each_entry_rcu(tunnel, &amt->tunnel_list, list) { 1280 hash = amt_group_hash(tunnel, &group); 1281 hlist_for_each_entry_rcu(gnode, &tunnel->groups[hash], 1282 node) { 1283 if (!v6) { 1284 if (gnode->group_addr.ip4 == iph->daddr) 1285 goto found; 1286 #if IS_ENABLED(CONFIG_IPV6) 1287 } else { 1288 if (ipv6_addr_equal(&gnode->group_addr.ip6, 1289 &ip6h->daddr)) 1290 goto found; 1291 #endif 1292 } 1293 } 1294 continue; 1295 found: 1296 amt_send_multicast_data(amt, skb, tunnel, v6); 1297 } 1298 } 1299 1300 dev_kfree_skb(skb); 1301 return NETDEV_TX_OK; 1302 free: 1303 dev_kfree_skb(skb); 1304 unlock: 1305 dev->stats.tx_dropped++; 1306 return NETDEV_TX_OK; 1307 } 1308 1309 static int amt_parse_type(struct sk_buff *skb) 1310 { 1311 struct amt_header *amth; 1312 1313 if (!pskb_may_pull(skb, sizeof(struct udphdr) + 1314 sizeof(struct amt_header))) 1315 return -1; 1316 1317 amth = (struct amt_header *)(udp_hdr(skb) + 1); 1318 1319 if (amth->version != 0) 1320 return -1; 1321 1322 if (amth->type >= __AMT_MSG_MAX || !amth->type) 1323 return -1; 1324 return amth->type; 1325 } 1326 1327 static void amt_clear_groups(struct amt_tunnel_list *tunnel) 1328 { 1329 struct amt_dev *amt = tunnel->amt; 1330 struct amt_group_node *gnode; 1331 struct hlist_node *t; 1332 int i; 1333 1334 spin_lock_bh(&tunnel->lock); 1335 rcu_read_lock(); 1336 for (i = 0; i < amt->hash_buckets; i++) 1337 hlist_for_each_entry_safe(gnode, t, &tunnel->groups[i], node) 1338 amt_del_group(amt, gnode); 1339 rcu_read_unlock(); 1340 spin_unlock_bh(&tunnel->lock); 1341 } 1342 1343 static void amt_tunnel_expire(struct work_struct *work) 1344 { 1345 struct amt_tunnel_list *tunnel = container_of(to_delayed_work(work), 1346 struct amt_tunnel_list, 1347 gc_wq); 1348 struct amt_dev *amt = tunnel->amt; 1349 1350 spin_lock_bh(&amt->lock); 1351 rcu_read_lock(); 1352 list_del_rcu(&tunnel->list); 1353 amt->nr_tunnels--; 1354 amt_clear_groups(tunnel); 1355 rcu_read_unlock(); 1356 spin_unlock_bh(&amt->lock); 1357 kfree_rcu(tunnel, rcu); 1358 } 1359 1360 static void amt_cleanup_srcs(struct amt_dev *amt, 1361 struct amt_tunnel_list *tunnel, 1362 struct amt_group_node *gnode) 1363 { 1364 struct amt_source_node *snode; 1365 struct hlist_node *t; 1366 int i; 1367 1368 /* Delete old sources */ 1369 for (i = 0; i < amt->hash_buckets; i++) { 1370 hlist_for_each_entry_safe(snode, t, &gnode->sources[i], node) { 1371 if (snode->flags == AMT_SOURCE_OLD) 1372 amt_destroy_source(snode); 1373 } 1374 } 1375 1376 /* switch from new to old */ 1377 for (i = 0; i < amt->hash_buckets; i++) { 1378 hlist_for_each_entry_rcu(snode, &gnode->sources[i], node) { 1379 snode->flags = AMT_SOURCE_OLD; 1380 if (!gnode->v6) 1381 netdev_dbg(snode->gnode->amt->dev, 1382 "Add source as OLD %pI4 from %pI4\n", 1383 &snode->source_addr.ip4, 1384 &gnode->group_addr.ip4); 1385 #if IS_ENABLED(CONFIG_IPV6) 1386 else 1387 netdev_dbg(snode->gnode->amt->dev, 1388 "Add source as OLD %pI6 from %pI6\n", 1389 &snode->source_addr.ip6, 1390 &gnode->group_addr.ip6); 1391 #endif 1392 } 1393 } 1394 } 1395 1396 static void amt_add_srcs(struct amt_dev *amt, struct amt_tunnel_list *tunnel, 1397 struct amt_group_node *gnode, void *grec, 1398 bool v6) 1399 { 1400 struct igmpv3_grec *igmp_grec; 1401 struct amt_source_node *snode; 1402 #if IS_ENABLED(CONFIG_IPV6) 1403 struct mld2_grec *mld_grec; 1404 #endif 1405 union amt_addr src = {0,}; 1406 u16 nsrcs; 1407 u32 hash; 1408 int i; 1409 1410 if (!v6) { 1411 igmp_grec = grec; 1412 nsrcs = ntohs(igmp_grec->grec_nsrcs); 1413 } else { 1414 #if IS_ENABLED(CONFIG_IPV6) 1415 mld_grec = grec; 1416 nsrcs = ntohs(mld_grec->grec_nsrcs); 1417 #else 1418 return; 1419 #endif 1420 } 1421 for (i = 0; i < nsrcs; i++) { 1422 if (tunnel->nr_sources >= amt->max_sources) 1423 return; 1424 if (!v6) 1425 src.ip4 = igmp_grec->grec_src[i]; 1426 #if IS_ENABLED(CONFIG_IPV6) 1427 else 1428 memcpy(&src.ip6, &mld_grec->grec_src[i], 1429 sizeof(struct in6_addr)); 1430 #endif 1431 if (amt_lookup_src(tunnel, gnode, AMT_FILTER_ALL, &src)) 1432 continue; 1433 1434 snode = amt_alloc_snode(gnode, &src); 1435 if (snode) { 1436 hash = amt_source_hash(tunnel, &snode->source_addr); 1437 hlist_add_head_rcu(&snode->node, &gnode->sources[hash]); 1438 tunnel->nr_sources++; 1439 gnode->nr_sources++; 1440 1441 if (!gnode->v6) 1442 netdev_dbg(snode->gnode->amt->dev, 1443 "Add source as NEW %pI4 from %pI4\n", 1444 &snode->source_addr.ip4, 1445 &gnode->group_addr.ip4); 1446 #if IS_ENABLED(CONFIG_IPV6) 1447 else 1448 netdev_dbg(snode->gnode->amt->dev, 1449 "Add source as NEW %pI6 from %pI6\n", 1450 &snode->source_addr.ip6, 1451 &gnode->group_addr.ip6); 1452 #endif 1453 } 1454 } 1455 } 1456 1457 /* Router State Report Rec'd New Router State 1458 * ------------ ------------ ---------------- 1459 * EXCLUDE (X,Y) IS_IN (A) EXCLUDE (X+A,Y-A) 1460 * 1461 * -----------+-----------+-----------+ 1462 * | OLD | NEW | 1463 * -----------+-----------+-----------+ 1464 * FWD | X | X+A | 1465 * -----------+-----------+-----------+ 1466 * D_FWD | Y | Y-A | 1467 * -----------+-----------+-----------+ 1468 * NONE | | A | 1469 * -----------+-----------+-----------+ 1470 * 1471 * a) Received sources are NONE/NEW 1472 * b) All NONE will be deleted by amt_cleanup_srcs(). 1473 * c) All OLD will be deleted by amt_cleanup_srcs(). 1474 * d) After delete, NEW source will be switched to OLD. 1475 */ 1476 static void amt_lookup_act_srcs(struct amt_tunnel_list *tunnel, 1477 struct amt_group_node *gnode, 1478 void *grec, 1479 enum amt_ops ops, 1480 enum amt_filter filter, 1481 enum amt_act act, 1482 bool v6) 1483 { 1484 struct amt_dev *amt = tunnel->amt; 1485 struct amt_source_node *snode; 1486 struct igmpv3_grec *igmp_grec; 1487 #if IS_ENABLED(CONFIG_IPV6) 1488 struct mld2_grec *mld_grec; 1489 #endif 1490 union amt_addr src = {0,}; 1491 struct hlist_node *t; 1492 u16 nsrcs; 1493 int i, j; 1494 1495 if (!v6) { 1496 igmp_grec = grec; 1497 nsrcs = ntohs(igmp_grec->grec_nsrcs); 1498 } else { 1499 #if IS_ENABLED(CONFIG_IPV6) 1500 mld_grec = grec; 1501 nsrcs = ntohs(mld_grec->grec_nsrcs); 1502 #else 1503 return; 1504 #endif 1505 } 1506 1507 memset(&src, 0, sizeof(union amt_addr)); 1508 switch (ops) { 1509 case AMT_OPS_INT: 1510 /* A*B */ 1511 for (i = 0; i < nsrcs; i++) { 1512 if (!v6) 1513 src.ip4 = igmp_grec->grec_src[i]; 1514 #if IS_ENABLED(CONFIG_IPV6) 1515 else 1516 memcpy(&src.ip6, &mld_grec->grec_src[i], 1517 sizeof(struct in6_addr)); 1518 #endif 1519 snode = amt_lookup_src(tunnel, gnode, filter, &src); 1520 if (!snode) 1521 continue; 1522 amt_act_src(tunnel, gnode, snode, act); 1523 } 1524 break; 1525 case AMT_OPS_UNI: 1526 /* A+B */ 1527 for (i = 0; i < amt->hash_buckets; i++) { 1528 hlist_for_each_entry_safe(snode, t, &gnode->sources[i], 1529 node) { 1530 if (amt_status_filter(snode, filter)) 1531 amt_act_src(tunnel, gnode, snode, act); 1532 } 1533 } 1534 for (i = 0; i < nsrcs; i++) { 1535 if (!v6) 1536 src.ip4 = igmp_grec->grec_src[i]; 1537 #if IS_ENABLED(CONFIG_IPV6) 1538 else 1539 memcpy(&src.ip6, &mld_grec->grec_src[i], 1540 sizeof(struct in6_addr)); 1541 #endif 1542 snode = amt_lookup_src(tunnel, gnode, filter, &src); 1543 if (!snode) 1544 continue; 1545 amt_act_src(tunnel, gnode, snode, act); 1546 } 1547 break; 1548 case AMT_OPS_SUB: 1549 /* A-B */ 1550 for (i = 0; i < amt->hash_buckets; i++) { 1551 hlist_for_each_entry_safe(snode, t, &gnode->sources[i], 1552 node) { 1553 if (!amt_status_filter(snode, filter)) 1554 continue; 1555 for (j = 0; j < nsrcs; j++) { 1556 if (!v6) 1557 src.ip4 = igmp_grec->grec_src[j]; 1558 #if IS_ENABLED(CONFIG_IPV6) 1559 else 1560 memcpy(&src.ip6, 1561 &mld_grec->grec_src[j], 1562 sizeof(struct in6_addr)); 1563 #endif 1564 if (amt_addr_equal(&snode->source_addr, 1565 &src)) 1566 goto out_sub; 1567 } 1568 amt_act_src(tunnel, gnode, snode, act); 1569 continue; 1570 out_sub:; 1571 } 1572 } 1573 break; 1574 case AMT_OPS_SUB_REV: 1575 /* B-A */ 1576 for (i = 0; i < nsrcs; i++) { 1577 if (!v6) 1578 src.ip4 = igmp_grec->grec_src[i]; 1579 #if IS_ENABLED(CONFIG_IPV6) 1580 else 1581 memcpy(&src.ip6, &mld_grec->grec_src[i], 1582 sizeof(struct in6_addr)); 1583 #endif 1584 snode = amt_lookup_src(tunnel, gnode, AMT_FILTER_ALL, 1585 &src); 1586 if (!snode) { 1587 snode = amt_lookup_src(tunnel, gnode, 1588 filter, &src); 1589 if (snode) 1590 amt_act_src(tunnel, gnode, snode, act); 1591 } 1592 } 1593 break; 1594 default: 1595 netdev_dbg(amt->dev, "Invalid type\n"); 1596 return; 1597 } 1598 } 1599 1600 static void amt_mcast_is_in_handler(struct amt_dev *amt, 1601 struct amt_tunnel_list *tunnel, 1602 struct amt_group_node *gnode, 1603 void *grec, void *zero_grec, bool v6) 1604 { 1605 if (gnode->filter_mode == MCAST_INCLUDE) { 1606 /* Router State Report Rec'd New Router State Actions 1607 * ------------ ------------ ---------------- ------- 1608 * INCLUDE (A) IS_IN (B) INCLUDE (A+B) (B)=GMI 1609 */ 1610 /* Update IS_IN (B) as FWD/NEW */ 1611 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI, 1612 AMT_FILTER_NONE_NEW, 1613 AMT_ACT_STATUS_FWD_NEW, 1614 v6); 1615 /* Update INCLUDE (A) as NEW */ 1616 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI, 1617 AMT_FILTER_FWD, 1618 AMT_ACT_STATUS_FWD_NEW, 1619 v6); 1620 /* (B)=GMI */ 1621 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT, 1622 AMT_FILTER_FWD_NEW, 1623 AMT_ACT_GMI, 1624 v6); 1625 } else { 1626 /* State Actions 1627 * ------------ ------------ ---------------- ------- 1628 * EXCLUDE (X,Y) IS_IN (A) EXCLUDE (X+A,Y-A) (A)=GMI 1629 */ 1630 /* Update (A) in (X, Y) as NONE/NEW */ 1631 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT, 1632 AMT_FILTER_BOTH, 1633 AMT_ACT_STATUS_NONE_NEW, 1634 v6); 1635 /* Update FWD/OLD as FWD/NEW */ 1636 amt_lookup_act_srcs(tunnel, gnode, zero_grec, AMT_OPS_UNI, 1637 AMT_FILTER_FWD, 1638 AMT_ACT_STATUS_FWD_NEW, 1639 v6); 1640 /* Update IS_IN (A) as FWD/NEW */ 1641 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT, 1642 AMT_FILTER_NONE_NEW, 1643 AMT_ACT_STATUS_FWD_NEW, 1644 v6); 1645 /* Update EXCLUDE (, Y-A) as D_FWD_NEW */ 1646 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB, 1647 AMT_FILTER_D_FWD, 1648 AMT_ACT_STATUS_D_FWD_NEW, 1649 v6); 1650 } 1651 } 1652 1653 static void amt_mcast_is_ex_handler(struct amt_dev *amt, 1654 struct amt_tunnel_list *tunnel, 1655 struct amt_group_node *gnode, 1656 void *grec, void *zero_grec, bool v6) 1657 { 1658 if (gnode->filter_mode == MCAST_INCLUDE) { 1659 /* Router State Report Rec'd New Router State Actions 1660 * ------------ ------------ ---------------- ------- 1661 * INCLUDE (A) IS_EX (B) EXCLUDE (A*B,B-A) (B-A)=0 1662 * Delete (A-B) 1663 * Group Timer=GMI 1664 */ 1665 /* EXCLUDE(A*B, ) */ 1666 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT, 1667 AMT_FILTER_FWD, 1668 AMT_ACT_STATUS_FWD_NEW, 1669 v6); 1670 /* EXCLUDE(, B-A) */ 1671 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV, 1672 AMT_FILTER_FWD, 1673 AMT_ACT_STATUS_D_FWD_NEW, 1674 v6); 1675 /* (B-A)=0 */ 1676 amt_lookup_act_srcs(tunnel, gnode, zero_grec, AMT_OPS_UNI, 1677 AMT_FILTER_D_FWD_NEW, 1678 AMT_ACT_GMI_ZERO, 1679 v6); 1680 /* Group Timer=GMI */ 1681 if (!mod_delayed_work(amt_wq, &gnode->group_timer, 1682 msecs_to_jiffies(amt_gmi(amt)))) 1683 dev_hold(amt->dev); 1684 gnode->filter_mode = MCAST_EXCLUDE; 1685 /* Delete (A-B) will be worked by amt_cleanup_srcs(). */ 1686 } else { 1687 /* Router State Report Rec'd New Router State Actions 1688 * ------------ ------------ ---------------- ------- 1689 * EXCLUDE (X,Y) IS_EX (A) EXCLUDE (A-Y,Y*A) (A-X-Y)=GMI 1690 * Delete (X-A) 1691 * Delete (Y-A) 1692 * Group Timer=GMI 1693 */ 1694 /* EXCLUDE (A-Y, ) */ 1695 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV, 1696 AMT_FILTER_D_FWD, 1697 AMT_ACT_STATUS_FWD_NEW, 1698 v6); 1699 /* EXCLUDE (, Y*A ) */ 1700 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT, 1701 AMT_FILTER_D_FWD, 1702 AMT_ACT_STATUS_D_FWD_NEW, 1703 v6); 1704 /* (A-X-Y)=GMI */ 1705 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV, 1706 AMT_FILTER_BOTH_NEW, 1707 AMT_ACT_GMI, 1708 v6); 1709 /* Group Timer=GMI */ 1710 if (!mod_delayed_work(amt_wq, &gnode->group_timer, 1711 msecs_to_jiffies(amt_gmi(amt)))) 1712 dev_hold(amt->dev); 1713 /* Delete (X-A), (Y-A) will be worked by amt_cleanup_srcs(). */ 1714 } 1715 } 1716 1717 static void amt_mcast_to_in_handler(struct amt_dev *amt, 1718 struct amt_tunnel_list *tunnel, 1719 struct amt_group_node *gnode, 1720 void *grec, void *zero_grec, bool v6) 1721 { 1722 if (gnode->filter_mode == MCAST_INCLUDE) { 1723 /* Router State Report Rec'd New Router State Actions 1724 * ------------ ------------ ---------------- ------- 1725 * INCLUDE (A) TO_IN (B) INCLUDE (A+B) (B)=GMI 1726 * Send Q(G,A-B) 1727 */ 1728 /* Update TO_IN (B) sources as FWD/NEW */ 1729 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI, 1730 AMT_FILTER_NONE_NEW, 1731 AMT_ACT_STATUS_FWD_NEW, 1732 v6); 1733 /* Update INCLUDE (A) sources as NEW */ 1734 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI, 1735 AMT_FILTER_FWD, 1736 AMT_ACT_STATUS_FWD_NEW, 1737 v6); 1738 /* (B)=GMI */ 1739 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT, 1740 AMT_FILTER_FWD_NEW, 1741 AMT_ACT_GMI, 1742 v6); 1743 } else { 1744 /* Router State Report Rec'd New Router State Actions 1745 * ------------ ------------ ---------------- ------- 1746 * EXCLUDE (X,Y) TO_IN (A) EXCLUDE (X+A,Y-A) (A)=GMI 1747 * Send Q(G,X-A) 1748 * Send Q(G) 1749 */ 1750 /* Update TO_IN (A) sources as FWD/NEW */ 1751 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI, 1752 AMT_FILTER_NONE_NEW, 1753 AMT_ACT_STATUS_FWD_NEW, 1754 v6); 1755 /* Update EXCLUDE(X,) sources as FWD/NEW */ 1756 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI, 1757 AMT_FILTER_FWD, 1758 AMT_ACT_STATUS_FWD_NEW, 1759 v6); 1760 /* EXCLUDE (, Y-A) 1761 * (A) are already switched to FWD_NEW. 1762 * So, D_FWD/OLD -> D_FWD/NEW is okay. 1763 */ 1764 amt_lookup_act_srcs(tunnel, gnode, zero_grec, AMT_OPS_UNI, 1765 AMT_FILTER_D_FWD, 1766 AMT_ACT_STATUS_D_FWD_NEW, 1767 v6); 1768 /* (A)=GMI 1769 * Only FWD_NEW will have (A) sources. 1770 */ 1771 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT, 1772 AMT_FILTER_FWD_NEW, 1773 AMT_ACT_GMI, 1774 v6); 1775 } 1776 } 1777 1778 static void amt_mcast_to_ex_handler(struct amt_dev *amt, 1779 struct amt_tunnel_list *tunnel, 1780 struct amt_group_node *gnode, 1781 void *grec, void *zero_grec, bool v6) 1782 { 1783 if (gnode->filter_mode == MCAST_INCLUDE) { 1784 /* Router State Report Rec'd New Router State Actions 1785 * ------------ ------------ ---------------- ------- 1786 * INCLUDE (A) TO_EX (B) EXCLUDE (A*B,B-A) (B-A)=0 1787 * Delete (A-B) 1788 * Send Q(G,A*B) 1789 * Group Timer=GMI 1790 */ 1791 /* EXCLUDE (A*B, ) */ 1792 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT, 1793 AMT_FILTER_FWD, 1794 AMT_ACT_STATUS_FWD_NEW, 1795 v6); 1796 /* EXCLUDE (, B-A) */ 1797 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV, 1798 AMT_FILTER_FWD, 1799 AMT_ACT_STATUS_D_FWD_NEW, 1800 v6); 1801 /* (B-A)=0 */ 1802 amt_lookup_act_srcs(tunnel, gnode, zero_grec, AMT_OPS_UNI, 1803 AMT_FILTER_D_FWD_NEW, 1804 AMT_ACT_GMI_ZERO, 1805 v6); 1806 /* Group Timer=GMI */ 1807 if (!mod_delayed_work(amt_wq, &gnode->group_timer, 1808 msecs_to_jiffies(amt_gmi(amt)))) 1809 dev_hold(amt->dev); 1810 gnode->filter_mode = MCAST_EXCLUDE; 1811 /* Delete (A-B) will be worked by amt_cleanup_srcs(). */ 1812 } else { 1813 /* Router State Report Rec'd New Router State Actions 1814 * ------------ ------------ ---------------- ------- 1815 * EXCLUDE (X,Y) TO_EX (A) EXCLUDE (A-Y,Y*A) (A-X-Y)=Group Timer 1816 * Delete (X-A) 1817 * Delete (Y-A) 1818 * Send Q(G,A-Y) 1819 * Group Timer=GMI 1820 */ 1821 /* Update (A-X-Y) as NONE/OLD */ 1822 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV, 1823 AMT_FILTER_BOTH, 1824 AMT_ACT_GT, 1825 v6); 1826 /* EXCLUDE (A-Y, ) */ 1827 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV, 1828 AMT_FILTER_D_FWD, 1829 AMT_ACT_STATUS_FWD_NEW, 1830 v6); 1831 /* EXCLUDE (, Y*A) */ 1832 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT, 1833 AMT_FILTER_D_FWD, 1834 AMT_ACT_STATUS_D_FWD_NEW, 1835 v6); 1836 /* Group Timer=GMI */ 1837 if (!mod_delayed_work(amt_wq, &gnode->group_timer, 1838 msecs_to_jiffies(amt_gmi(amt)))) 1839 dev_hold(amt->dev); 1840 /* Delete (X-A), (Y-A) will be worked by amt_cleanup_srcs(). */ 1841 } 1842 } 1843 1844 static void amt_mcast_allow_handler(struct amt_dev *amt, 1845 struct amt_tunnel_list *tunnel, 1846 struct amt_group_node *gnode, 1847 void *grec, void *zero_grec, bool v6) 1848 { 1849 if (gnode->filter_mode == MCAST_INCLUDE) { 1850 /* Router State Report Rec'd New Router State Actions 1851 * ------------ ------------ ---------------- ------- 1852 * INCLUDE (A) ALLOW (B) INCLUDE (A+B) (B)=GMI 1853 */ 1854 /* INCLUDE (A+B) */ 1855 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI, 1856 AMT_FILTER_FWD, 1857 AMT_ACT_STATUS_FWD_NEW, 1858 v6); 1859 /* (B)=GMI */ 1860 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT, 1861 AMT_FILTER_FWD_NEW, 1862 AMT_ACT_GMI, 1863 v6); 1864 } else { 1865 /* Router State Report Rec'd New Router State Actions 1866 * ------------ ------------ ---------------- ------- 1867 * EXCLUDE (X,Y) ALLOW (A) EXCLUDE (X+A,Y-A) (A)=GMI 1868 */ 1869 /* EXCLUDE (X+A, ) */ 1870 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI, 1871 AMT_FILTER_FWD, 1872 AMT_ACT_STATUS_FWD_NEW, 1873 v6); 1874 /* EXCLUDE (, Y-A) */ 1875 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB, 1876 AMT_FILTER_D_FWD, 1877 AMT_ACT_STATUS_D_FWD_NEW, 1878 v6); 1879 /* (A)=GMI 1880 * All (A) source are now FWD/NEW status. 1881 */ 1882 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT, 1883 AMT_FILTER_FWD_NEW, 1884 AMT_ACT_GMI, 1885 v6); 1886 } 1887 } 1888 1889 static void amt_mcast_block_handler(struct amt_dev *amt, 1890 struct amt_tunnel_list *tunnel, 1891 struct amt_group_node *gnode, 1892 void *grec, void *zero_grec, bool v6) 1893 { 1894 if (gnode->filter_mode == MCAST_INCLUDE) { 1895 /* Router State Report Rec'd New Router State Actions 1896 * ------------ ------------ ---------------- ------- 1897 * INCLUDE (A) BLOCK (B) INCLUDE (A) Send Q(G,A*B) 1898 */ 1899 /* INCLUDE (A) */ 1900 amt_lookup_act_srcs(tunnel, gnode, zero_grec, AMT_OPS_UNI, 1901 AMT_FILTER_FWD, 1902 AMT_ACT_STATUS_FWD_NEW, 1903 v6); 1904 } else { 1905 /* Router State Report Rec'd New Router State Actions 1906 * ------------ ------------ ---------------- ------- 1907 * EXCLUDE (X,Y) BLOCK (A) EXCLUDE (X+(A-Y),Y) (A-X-Y)=Group Timer 1908 * Send Q(G,A-Y) 1909 */ 1910 /* (A-X-Y)=Group Timer */ 1911 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV, 1912 AMT_FILTER_BOTH, 1913 AMT_ACT_GT, 1914 v6); 1915 /* EXCLUDE (X, ) */ 1916 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI, 1917 AMT_FILTER_FWD, 1918 AMT_ACT_STATUS_FWD_NEW, 1919 v6); 1920 /* EXCLUDE (X+(A-Y) */ 1921 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV, 1922 AMT_FILTER_D_FWD, 1923 AMT_ACT_STATUS_FWD_NEW, 1924 v6); 1925 /* EXCLUDE (, Y) */ 1926 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI, 1927 AMT_FILTER_D_FWD, 1928 AMT_ACT_STATUS_D_FWD_NEW, 1929 v6); 1930 } 1931 } 1932 1933 /* RFC 3376 1934 * 7.3.2. In the Presence of Older Version Group Members 1935 * 1936 * When Group Compatibility Mode is IGMPv2, a router internally 1937 * translates the following IGMPv2 messages for that group to their 1938 * IGMPv3 equivalents: 1939 * 1940 * IGMPv2 Message IGMPv3 Equivalent 1941 * -------------- ----------------- 1942 * Report IS_EX( {} ) 1943 * Leave TO_IN( {} ) 1944 */ 1945 static void amt_igmpv2_report_handler(struct amt_dev *amt, struct sk_buff *skb, 1946 struct amt_tunnel_list *tunnel) 1947 { 1948 struct igmphdr *ih = igmp_hdr(skb); 1949 struct iphdr *iph = ip_hdr(skb); 1950 struct amt_group_node *gnode; 1951 union amt_addr group, host; 1952 1953 memset(&group, 0, sizeof(union amt_addr)); 1954 group.ip4 = ih->group; 1955 memset(&host, 0, sizeof(union amt_addr)); 1956 host.ip4 = iph->saddr; 1957 1958 gnode = amt_lookup_group(tunnel, &group, &host, false); 1959 if (!gnode) { 1960 gnode = amt_add_group(amt, tunnel, &group, &host, false); 1961 if (!IS_ERR(gnode)) { 1962 gnode->filter_mode = MCAST_EXCLUDE; 1963 if (!mod_delayed_work(amt_wq, &gnode->group_timer, 1964 msecs_to_jiffies(amt_gmi(amt)))) 1965 dev_hold(amt->dev); 1966 } 1967 } 1968 } 1969 1970 /* RFC 3376 1971 * 7.3.2. In the Presence of Older Version Group Members 1972 * 1973 * When Group Compatibility Mode is IGMPv2, a router internally 1974 * translates the following IGMPv2 messages for that group to their 1975 * IGMPv3 equivalents: 1976 * 1977 * IGMPv2 Message IGMPv3 Equivalent 1978 * -------------- ----------------- 1979 * Report IS_EX( {} ) 1980 * Leave TO_IN( {} ) 1981 */ 1982 static void amt_igmpv2_leave_handler(struct amt_dev *amt, struct sk_buff *skb, 1983 struct amt_tunnel_list *tunnel) 1984 { 1985 struct igmphdr *ih = igmp_hdr(skb); 1986 struct iphdr *iph = ip_hdr(skb); 1987 struct amt_group_node *gnode; 1988 union amt_addr group, host; 1989 1990 memset(&group, 0, sizeof(union amt_addr)); 1991 group.ip4 = ih->group; 1992 memset(&host, 0, sizeof(union amt_addr)); 1993 host.ip4 = iph->saddr; 1994 1995 gnode = amt_lookup_group(tunnel, &group, &host, false); 1996 if (gnode) 1997 amt_del_group(amt, gnode); 1998 } 1999 2000 static void amt_igmpv3_report_handler(struct amt_dev *amt, struct sk_buff *skb, 2001 struct amt_tunnel_list *tunnel) 2002 { 2003 struct igmpv3_report *ihrv3 = igmpv3_report_hdr(skb); 2004 int len = skb_transport_offset(skb) + sizeof(*ihrv3); 2005 void *zero_grec = (void *)&igmpv3_zero_grec; 2006 struct iphdr *iph = ip_hdr(skb); 2007 struct amt_group_node *gnode; 2008 union amt_addr group, host; 2009 struct igmpv3_grec *grec; 2010 u16 nsrcs; 2011 int i; 2012 2013 for (i = 0; i < ntohs(ihrv3->ngrec); i++) { 2014 len += sizeof(*grec); 2015 if (!ip_mc_may_pull(skb, len)) 2016 break; 2017 2018 grec = (void *)(skb->data + len - sizeof(*grec)); 2019 nsrcs = ntohs(grec->grec_nsrcs); 2020 2021 len += nsrcs * sizeof(__be32); 2022 if (!ip_mc_may_pull(skb, len)) 2023 break; 2024 2025 memset(&group, 0, sizeof(union amt_addr)); 2026 group.ip4 = grec->grec_mca; 2027 memset(&host, 0, sizeof(union amt_addr)); 2028 host.ip4 = iph->saddr; 2029 gnode = amt_lookup_group(tunnel, &group, &host, false); 2030 if (!gnode) { 2031 gnode = amt_add_group(amt, tunnel, &group, &host, 2032 false); 2033 if (IS_ERR(gnode)) 2034 continue; 2035 } 2036 2037 amt_add_srcs(amt, tunnel, gnode, grec, false); 2038 switch (grec->grec_type) { 2039 case IGMPV3_MODE_IS_INCLUDE: 2040 amt_mcast_is_in_handler(amt, tunnel, gnode, grec, 2041 zero_grec, false); 2042 break; 2043 case IGMPV3_MODE_IS_EXCLUDE: 2044 amt_mcast_is_ex_handler(amt, tunnel, gnode, grec, 2045 zero_grec, false); 2046 break; 2047 case IGMPV3_CHANGE_TO_INCLUDE: 2048 amt_mcast_to_in_handler(amt, tunnel, gnode, grec, 2049 zero_grec, false); 2050 break; 2051 case IGMPV3_CHANGE_TO_EXCLUDE: 2052 amt_mcast_to_ex_handler(amt, tunnel, gnode, grec, 2053 zero_grec, false); 2054 break; 2055 case IGMPV3_ALLOW_NEW_SOURCES: 2056 amt_mcast_allow_handler(amt, tunnel, gnode, grec, 2057 zero_grec, false); 2058 break; 2059 case IGMPV3_BLOCK_OLD_SOURCES: 2060 amt_mcast_block_handler(amt, tunnel, gnode, grec, 2061 zero_grec, false); 2062 break; 2063 default: 2064 break; 2065 } 2066 amt_cleanup_srcs(amt, tunnel, gnode); 2067 } 2068 } 2069 2070 /* caller held tunnel->lock */ 2071 static void amt_igmp_report_handler(struct amt_dev *amt, struct sk_buff *skb, 2072 struct amt_tunnel_list *tunnel) 2073 { 2074 struct igmphdr *ih = igmp_hdr(skb); 2075 2076 switch (ih->type) { 2077 case IGMPV3_HOST_MEMBERSHIP_REPORT: 2078 amt_igmpv3_report_handler(amt, skb, tunnel); 2079 break; 2080 case IGMPV2_HOST_MEMBERSHIP_REPORT: 2081 amt_igmpv2_report_handler(amt, skb, tunnel); 2082 break; 2083 case IGMP_HOST_LEAVE_MESSAGE: 2084 amt_igmpv2_leave_handler(amt, skb, tunnel); 2085 break; 2086 default: 2087 break; 2088 } 2089 } 2090 2091 #if IS_ENABLED(CONFIG_IPV6) 2092 /* RFC 3810 2093 * 8.3.2. In the Presence of MLDv1 Multicast Address Listeners 2094 * 2095 * When Multicast Address Compatibility Mode is MLDv2, a router acts 2096 * using the MLDv2 protocol for that multicast address. When Multicast 2097 * Address Compatibility Mode is MLDv1, a router internally translates 2098 * the following MLDv1 messages for that multicast address to their 2099 * MLDv2 equivalents: 2100 * 2101 * MLDv1 Message MLDv2 Equivalent 2102 * -------------- ----------------- 2103 * Report IS_EX( {} ) 2104 * Done TO_IN( {} ) 2105 */ 2106 static void amt_mldv1_report_handler(struct amt_dev *amt, struct sk_buff *skb, 2107 struct amt_tunnel_list *tunnel) 2108 { 2109 struct mld_msg *mld = (struct mld_msg *)icmp6_hdr(skb); 2110 struct ipv6hdr *ip6h = ipv6_hdr(skb); 2111 struct amt_group_node *gnode; 2112 union amt_addr group, host; 2113 2114 memcpy(&group.ip6, &mld->mld_mca, sizeof(struct in6_addr)); 2115 memcpy(&host.ip6, &ip6h->saddr, sizeof(struct in6_addr)); 2116 2117 gnode = amt_lookup_group(tunnel, &group, &host, true); 2118 if (!gnode) { 2119 gnode = amt_add_group(amt, tunnel, &group, &host, true); 2120 if (!IS_ERR(gnode)) { 2121 gnode->filter_mode = MCAST_EXCLUDE; 2122 if (!mod_delayed_work(amt_wq, &gnode->group_timer, 2123 msecs_to_jiffies(amt_gmi(amt)))) 2124 dev_hold(amt->dev); 2125 } 2126 } 2127 } 2128 2129 /* RFC 3810 2130 * 8.3.2. In the Presence of MLDv1 Multicast Address Listeners 2131 * 2132 * When Multicast Address Compatibility Mode is MLDv2, a router acts 2133 * using the MLDv2 protocol for that multicast address. When Multicast 2134 * Address Compatibility Mode is MLDv1, a router internally translates 2135 * the following MLDv1 messages for that multicast address to their 2136 * MLDv2 equivalents: 2137 * 2138 * MLDv1 Message MLDv2 Equivalent 2139 * -------------- ----------------- 2140 * Report IS_EX( {} ) 2141 * Done TO_IN( {} ) 2142 */ 2143 static void amt_mldv1_leave_handler(struct amt_dev *amt, struct sk_buff *skb, 2144 struct amt_tunnel_list *tunnel) 2145 { 2146 struct mld_msg *mld = (struct mld_msg *)icmp6_hdr(skb); 2147 struct iphdr *iph = ip_hdr(skb); 2148 struct amt_group_node *gnode; 2149 union amt_addr group, host; 2150 2151 memcpy(&group.ip6, &mld->mld_mca, sizeof(struct in6_addr)); 2152 memset(&host, 0, sizeof(union amt_addr)); 2153 host.ip4 = iph->saddr; 2154 2155 gnode = amt_lookup_group(tunnel, &group, &host, true); 2156 if (gnode) { 2157 amt_del_group(amt, gnode); 2158 return; 2159 } 2160 } 2161 2162 static void amt_mldv2_report_handler(struct amt_dev *amt, struct sk_buff *skb, 2163 struct amt_tunnel_list *tunnel) 2164 { 2165 struct mld2_report *mld2r = (struct mld2_report *)icmp6_hdr(skb); 2166 int len = skb_transport_offset(skb) + sizeof(*mld2r); 2167 void *zero_grec = (void *)&mldv2_zero_grec; 2168 struct ipv6hdr *ip6h = ipv6_hdr(skb); 2169 struct amt_group_node *gnode; 2170 union amt_addr group, host; 2171 struct mld2_grec *grec; 2172 u16 nsrcs; 2173 int i; 2174 2175 for (i = 0; i < ntohs(mld2r->mld2r_ngrec); i++) { 2176 len += sizeof(*grec); 2177 if (!ipv6_mc_may_pull(skb, len)) 2178 break; 2179 2180 grec = (void *)(skb->data + len - sizeof(*grec)); 2181 nsrcs = ntohs(grec->grec_nsrcs); 2182 2183 len += nsrcs * sizeof(struct in6_addr); 2184 if (!ipv6_mc_may_pull(skb, len)) 2185 break; 2186 2187 memset(&group, 0, sizeof(union amt_addr)); 2188 group.ip6 = grec->grec_mca; 2189 memset(&host, 0, sizeof(union amt_addr)); 2190 host.ip6 = ip6h->saddr; 2191 gnode = amt_lookup_group(tunnel, &group, &host, true); 2192 if (!gnode) { 2193 gnode = amt_add_group(amt, tunnel, &group, &host, 2194 ETH_P_IPV6); 2195 if (IS_ERR(gnode)) 2196 continue; 2197 } 2198 2199 amt_add_srcs(amt, tunnel, gnode, grec, true); 2200 switch (grec->grec_type) { 2201 case MLD2_MODE_IS_INCLUDE: 2202 amt_mcast_is_in_handler(amt, tunnel, gnode, grec, 2203 zero_grec, true); 2204 break; 2205 case MLD2_MODE_IS_EXCLUDE: 2206 amt_mcast_is_ex_handler(amt, tunnel, gnode, grec, 2207 zero_grec, true); 2208 break; 2209 case MLD2_CHANGE_TO_INCLUDE: 2210 amt_mcast_to_in_handler(amt, tunnel, gnode, grec, 2211 zero_grec, true); 2212 break; 2213 case MLD2_CHANGE_TO_EXCLUDE: 2214 amt_mcast_to_ex_handler(amt, tunnel, gnode, grec, 2215 zero_grec, true); 2216 break; 2217 case MLD2_ALLOW_NEW_SOURCES: 2218 amt_mcast_allow_handler(amt, tunnel, gnode, grec, 2219 zero_grec, true); 2220 break; 2221 case MLD2_BLOCK_OLD_SOURCES: 2222 amt_mcast_block_handler(amt, tunnel, gnode, grec, 2223 zero_grec, true); 2224 break; 2225 default: 2226 break; 2227 } 2228 amt_cleanup_srcs(amt, tunnel, gnode); 2229 } 2230 } 2231 2232 /* caller held tunnel->lock */ 2233 static void amt_mld_report_handler(struct amt_dev *amt, struct sk_buff *skb, 2234 struct amt_tunnel_list *tunnel) 2235 { 2236 struct mld_msg *mld = (struct mld_msg *)icmp6_hdr(skb); 2237 2238 switch (mld->mld_type) { 2239 case ICMPV6_MGM_REPORT: 2240 amt_mldv1_report_handler(amt, skb, tunnel); 2241 break; 2242 case ICMPV6_MLD2_REPORT: 2243 amt_mldv2_report_handler(amt, skb, tunnel); 2244 break; 2245 case ICMPV6_MGM_REDUCTION: 2246 amt_mldv1_leave_handler(amt, skb, tunnel); 2247 break; 2248 default: 2249 break; 2250 } 2251 } 2252 #endif 2253 2254 static bool amt_advertisement_handler(struct amt_dev *amt, struct sk_buff *skb) 2255 { 2256 struct amt_header_advertisement *amta; 2257 int hdr_size; 2258 2259 hdr_size = sizeof(*amta) + sizeof(struct udphdr); 2260 if (!pskb_may_pull(skb, hdr_size)) 2261 return true; 2262 2263 amta = (struct amt_header_advertisement *)(udp_hdr(skb) + 1); 2264 if (!amta->ip4) 2265 return true; 2266 2267 if (amta->reserved || amta->version) 2268 return true; 2269 2270 if (ipv4_is_loopback(amta->ip4) || ipv4_is_multicast(amta->ip4) || 2271 ipv4_is_zeronet(amta->ip4)) 2272 return true; 2273 2274 if (amt->status != AMT_STATUS_SENT_DISCOVERY || 2275 amt->nonce != amta->nonce) 2276 return true; 2277 2278 WRITE_ONCE(amt->remote_ip, amta->ip4); 2279 netdev_dbg(amt->dev, "advertised remote ip = %pI4\n", &amta->ip4); 2280 mod_delayed_work(amt_wq, &amt->req_wq, 0); 2281 2282 amt_update_gw_status(amt, AMT_STATUS_RECEIVED_ADVERTISEMENT, true); 2283 return false; 2284 } 2285 2286 static bool amt_multicast_data_handler(struct amt_dev *amt, struct sk_buff *skb) 2287 { 2288 struct amt_header_mcast_data *amtmd; 2289 int hdr_size, len, err; 2290 struct ethhdr *eth; 2291 struct iphdr *iph; 2292 2293 if (READ_ONCE(amt->status) != AMT_STATUS_SENT_UPDATE) 2294 return true; 2295 2296 hdr_size = sizeof(*amtmd) + sizeof(struct udphdr); 2297 if (!pskb_may_pull(skb, hdr_size)) 2298 return true; 2299 2300 amtmd = (struct amt_header_mcast_data *)(udp_hdr(skb) + 1); 2301 if (amtmd->reserved || amtmd->version) 2302 return true; 2303 2304 if (iptunnel_pull_header(skb, hdr_size, htons(ETH_P_IP), false)) 2305 return true; 2306 2307 skb_reset_network_header(skb); 2308 skb_push(skb, sizeof(*eth)); 2309 skb_reset_mac_header(skb); 2310 skb_pull(skb, sizeof(*eth)); 2311 eth = eth_hdr(skb); 2312 2313 if (!pskb_may_pull(skb, sizeof(*iph))) 2314 return true; 2315 iph = ip_hdr(skb); 2316 2317 if (iph->version == 4) { 2318 if (!ipv4_is_multicast(iph->daddr)) 2319 return true; 2320 skb->protocol = htons(ETH_P_IP); 2321 eth->h_proto = htons(ETH_P_IP); 2322 ip_eth_mc_map(iph->daddr, eth->h_dest); 2323 #if IS_ENABLED(CONFIG_IPV6) 2324 } else if (iph->version == 6) { 2325 struct ipv6hdr *ip6h; 2326 2327 if (!pskb_may_pull(skb, sizeof(*ip6h))) 2328 return true; 2329 2330 ip6h = ipv6_hdr(skb); 2331 if (!ipv6_addr_is_multicast(&ip6h->daddr)) 2332 return true; 2333 skb->protocol = htons(ETH_P_IPV6); 2334 eth->h_proto = htons(ETH_P_IPV6); 2335 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest); 2336 #endif 2337 } else { 2338 return true; 2339 } 2340 2341 skb->pkt_type = PACKET_MULTICAST; 2342 skb->ip_summed = CHECKSUM_NONE; 2343 len = skb->len; 2344 err = gro_cells_receive(&amt->gro_cells, skb); 2345 if (likely(err == NET_RX_SUCCESS)) 2346 dev_sw_netstats_rx_add(amt->dev, len); 2347 else 2348 amt->dev->stats.rx_dropped++; 2349 2350 return false; 2351 } 2352 2353 static bool amt_membership_query_handler(struct amt_dev *amt, 2354 struct sk_buff *skb) 2355 { 2356 struct amt_header_membership_query *amtmq; 2357 struct igmpv3_query *ihv3; 2358 struct ethhdr *eth, *oeth; 2359 struct iphdr *iph; 2360 int hdr_size, len; 2361 2362 hdr_size = sizeof(*amtmq) + sizeof(struct udphdr); 2363 if (!pskb_may_pull(skb, hdr_size)) 2364 return true; 2365 2366 amtmq = (struct amt_header_membership_query *)(udp_hdr(skb) + 1); 2367 if (amtmq->reserved || amtmq->version) 2368 return true; 2369 2370 if (amtmq->nonce != amt->nonce) 2371 return true; 2372 2373 hdr_size -= sizeof(*eth); 2374 if (iptunnel_pull_header(skb, hdr_size, htons(ETH_P_TEB), false)) 2375 return true; 2376 2377 oeth = eth_hdr(skb); 2378 skb_reset_mac_header(skb); 2379 skb_pull(skb, sizeof(*eth)); 2380 skb_reset_network_header(skb); 2381 eth = eth_hdr(skb); 2382 if (!pskb_may_pull(skb, sizeof(*iph))) 2383 return true; 2384 2385 iph = ip_hdr(skb); 2386 if (iph->version == 4) { 2387 if (READ_ONCE(amt->ready4)) 2388 return true; 2389 2390 if (!pskb_may_pull(skb, sizeof(*iph) + AMT_IPHDR_OPTS + 2391 sizeof(*ihv3))) 2392 return true; 2393 2394 if (!ipv4_is_multicast(iph->daddr)) 2395 return true; 2396 2397 ihv3 = skb_pull(skb, sizeof(*iph) + AMT_IPHDR_OPTS); 2398 skb_reset_transport_header(skb); 2399 skb_push(skb, sizeof(*iph) + AMT_IPHDR_OPTS); 2400 WRITE_ONCE(amt->ready4, true); 2401 amt->mac = amtmq->response_mac; 2402 amt->req_cnt = 0; 2403 amt->qi = ihv3->qqic; 2404 skb->protocol = htons(ETH_P_IP); 2405 eth->h_proto = htons(ETH_P_IP); 2406 ip_eth_mc_map(iph->daddr, eth->h_dest); 2407 #if IS_ENABLED(CONFIG_IPV6) 2408 } else if (iph->version == 6) { 2409 struct mld2_query *mld2q; 2410 struct ipv6hdr *ip6h; 2411 2412 if (READ_ONCE(amt->ready6)) 2413 return true; 2414 2415 if (!pskb_may_pull(skb, sizeof(*ip6h) + AMT_IP6HDR_OPTS + 2416 sizeof(*mld2q))) 2417 return true; 2418 2419 ip6h = ipv6_hdr(skb); 2420 if (!ipv6_addr_is_multicast(&ip6h->daddr)) 2421 return true; 2422 2423 mld2q = skb_pull(skb, sizeof(*ip6h) + AMT_IP6HDR_OPTS); 2424 skb_reset_transport_header(skb); 2425 skb_push(skb, sizeof(*ip6h) + AMT_IP6HDR_OPTS); 2426 WRITE_ONCE(amt->ready6, true); 2427 amt->mac = amtmq->response_mac; 2428 amt->req_cnt = 0; 2429 amt->qi = mld2q->mld2q_qqic; 2430 skb->protocol = htons(ETH_P_IPV6); 2431 eth->h_proto = htons(ETH_P_IPV6); 2432 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest); 2433 #endif 2434 } else { 2435 return true; 2436 } 2437 2438 ether_addr_copy(eth->h_source, oeth->h_source); 2439 skb->pkt_type = PACKET_MULTICAST; 2440 skb->ip_summed = CHECKSUM_NONE; 2441 len = skb->len; 2442 local_bh_disable(); 2443 if (__netif_rx(skb) == NET_RX_SUCCESS) { 2444 amt_update_gw_status(amt, AMT_STATUS_RECEIVED_QUERY, true); 2445 dev_sw_netstats_rx_add(amt->dev, len); 2446 } else { 2447 amt->dev->stats.rx_dropped++; 2448 } 2449 local_bh_enable(); 2450 2451 return false; 2452 } 2453 2454 static bool amt_update_handler(struct amt_dev *amt, struct sk_buff *skb) 2455 { 2456 struct amt_header_membership_update *amtmu; 2457 struct amt_tunnel_list *tunnel; 2458 struct ethhdr *eth; 2459 struct iphdr *iph; 2460 int len, hdr_size; 2461 2462 iph = ip_hdr(skb); 2463 2464 hdr_size = sizeof(*amtmu) + sizeof(struct udphdr); 2465 if (!pskb_may_pull(skb, hdr_size)) 2466 return true; 2467 2468 amtmu = (struct amt_header_membership_update *)(udp_hdr(skb) + 1); 2469 if (amtmu->reserved || amtmu->version) 2470 return true; 2471 2472 if (iptunnel_pull_header(skb, hdr_size, skb->protocol, false)) 2473 return true; 2474 2475 skb_reset_network_header(skb); 2476 2477 list_for_each_entry_rcu(tunnel, &amt->tunnel_list, list) { 2478 if (tunnel->ip4 == iph->saddr) { 2479 if ((amtmu->nonce == tunnel->nonce && 2480 amtmu->response_mac == tunnel->mac)) { 2481 mod_delayed_work(amt_wq, &tunnel->gc_wq, 2482 msecs_to_jiffies(amt_gmi(amt)) 2483 * 3); 2484 goto report; 2485 } else { 2486 netdev_dbg(amt->dev, "Invalid MAC\n"); 2487 return true; 2488 } 2489 } 2490 } 2491 2492 return true; 2493 2494 report: 2495 if (!pskb_may_pull(skb, sizeof(*iph))) 2496 return true; 2497 2498 iph = ip_hdr(skb); 2499 if (iph->version == 4) { 2500 if (ip_mc_check_igmp(skb)) { 2501 netdev_dbg(amt->dev, "Invalid IGMP\n"); 2502 return true; 2503 } 2504 2505 spin_lock_bh(&tunnel->lock); 2506 amt_igmp_report_handler(amt, skb, tunnel); 2507 spin_unlock_bh(&tunnel->lock); 2508 2509 skb_push(skb, sizeof(struct ethhdr)); 2510 skb_reset_mac_header(skb); 2511 eth = eth_hdr(skb); 2512 skb->protocol = htons(ETH_P_IP); 2513 eth->h_proto = htons(ETH_P_IP); 2514 ip_eth_mc_map(iph->daddr, eth->h_dest); 2515 #if IS_ENABLED(CONFIG_IPV6) 2516 } else if (iph->version == 6) { 2517 struct ipv6hdr *ip6h = ipv6_hdr(skb); 2518 2519 if (ipv6_mc_check_mld(skb)) { 2520 netdev_dbg(amt->dev, "Invalid MLD\n"); 2521 return true; 2522 } 2523 2524 spin_lock_bh(&tunnel->lock); 2525 amt_mld_report_handler(amt, skb, tunnel); 2526 spin_unlock_bh(&tunnel->lock); 2527 2528 skb_push(skb, sizeof(struct ethhdr)); 2529 skb_reset_mac_header(skb); 2530 eth = eth_hdr(skb); 2531 skb->protocol = htons(ETH_P_IPV6); 2532 eth->h_proto = htons(ETH_P_IPV6); 2533 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest); 2534 #endif 2535 } else { 2536 netdev_dbg(amt->dev, "Unsupported Protocol\n"); 2537 return true; 2538 } 2539 2540 skb_pull(skb, sizeof(struct ethhdr)); 2541 skb->pkt_type = PACKET_MULTICAST; 2542 skb->ip_summed = CHECKSUM_NONE; 2543 len = skb->len; 2544 if (__netif_rx(skb) == NET_RX_SUCCESS) { 2545 amt_update_relay_status(tunnel, AMT_STATUS_RECEIVED_UPDATE, 2546 true); 2547 dev_sw_netstats_rx_add(amt->dev, len); 2548 } else { 2549 amt->dev->stats.rx_dropped++; 2550 } 2551 2552 return false; 2553 } 2554 2555 static void amt_send_advertisement(struct amt_dev *amt, __be32 nonce, 2556 __be32 daddr, __be16 dport) 2557 { 2558 struct amt_header_advertisement *amta; 2559 int hlen, tlen, offset; 2560 struct udphdr *udph; 2561 struct sk_buff *skb; 2562 struct iphdr *iph; 2563 struct rtable *rt; 2564 struct flowi4 fl4; 2565 struct sock *sk; 2566 u32 len; 2567 int err; 2568 2569 rcu_read_lock(); 2570 sk = rcu_dereference(amt->sk); 2571 if (!sk) 2572 goto out; 2573 2574 if (!netif_running(amt->stream_dev) || !netif_running(amt->dev)) 2575 goto out; 2576 2577 rt = ip_route_output_ports(amt->net, &fl4, sk, 2578 daddr, amt->local_ip, 2579 dport, amt->relay_port, 2580 IPPROTO_UDP, 0, 2581 amt->stream_dev->ifindex); 2582 if (IS_ERR(rt)) { 2583 amt->dev->stats.tx_errors++; 2584 goto out; 2585 } 2586 2587 hlen = LL_RESERVED_SPACE(amt->dev); 2588 tlen = amt->dev->needed_tailroom; 2589 len = hlen + tlen + sizeof(*iph) + sizeof(*udph) + sizeof(*amta); 2590 skb = netdev_alloc_skb_ip_align(amt->dev, len); 2591 if (!skb) { 2592 ip_rt_put(rt); 2593 amt->dev->stats.tx_errors++; 2594 goto out; 2595 } 2596 2597 skb->priority = TC_PRIO_CONTROL; 2598 skb_dst_set(skb, &rt->dst); 2599 2600 len = sizeof(*iph) + sizeof(*udph) + sizeof(*amta); 2601 skb_reset_network_header(skb); 2602 skb_put(skb, len); 2603 amta = skb_pull(skb, sizeof(*iph) + sizeof(*udph)); 2604 amta->version = 0; 2605 amta->type = AMT_MSG_ADVERTISEMENT; 2606 amta->reserved = 0; 2607 amta->nonce = nonce; 2608 amta->ip4 = amt->local_ip; 2609 skb_push(skb, sizeof(*udph)); 2610 skb_reset_transport_header(skb); 2611 udph = udp_hdr(skb); 2612 udph->source = amt->relay_port; 2613 udph->dest = dport; 2614 udph->len = htons(sizeof(*amta) + sizeof(*udph)); 2615 udph->check = 0; 2616 offset = skb_transport_offset(skb); 2617 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0); 2618 udph->check = csum_tcpudp_magic(amt->local_ip, daddr, 2619 sizeof(*udph) + sizeof(*amta), 2620 IPPROTO_UDP, skb->csum); 2621 2622 skb_push(skb, sizeof(*iph)); 2623 iph = ip_hdr(skb); 2624 iph->version = 4; 2625 iph->ihl = (sizeof(struct iphdr)) >> 2; 2626 iph->tos = AMT_TOS; 2627 iph->frag_off = 0; 2628 iph->ttl = ip4_dst_hoplimit(&rt->dst); 2629 iph->daddr = daddr; 2630 iph->saddr = amt->local_ip; 2631 iph->protocol = IPPROTO_UDP; 2632 iph->tot_len = htons(len); 2633 2634 skb->ip_summed = CHECKSUM_NONE; 2635 ip_select_ident(amt->net, skb, NULL); 2636 ip_send_check(iph); 2637 err = ip_local_out(amt->net, sk, skb); 2638 if (unlikely(net_xmit_eval(err))) 2639 amt->dev->stats.tx_errors++; 2640 2641 out: 2642 rcu_read_unlock(); 2643 } 2644 2645 static bool amt_discovery_handler(struct amt_dev *amt, struct sk_buff *skb) 2646 { 2647 struct amt_header_discovery *amtd; 2648 struct udphdr *udph; 2649 struct iphdr *iph; 2650 2651 if (!pskb_may_pull(skb, sizeof(*udph) + sizeof(*amtd))) 2652 return true; 2653 2654 iph = ip_hdr(skb); 2655 udph = udp_hdr(skb); 2656 amtd = (struct amt_header_discovery *)(udp_hdr(skb) + 1); 2657 2658 if (amtd->reserved || amtd->version) 2659 return true; 2660 2661 amt_send_advertisement(amt, amtd->nonce, iph->saddr, udph->source); 2662 2663 return false; 2664 } 2665 2666 static bool amt_request_handler(struct amt_dev *amt, struct sk_buff *skb) 2667 { 2668 struct amt_header_request *amtrh; 2669 struct amt_tunnel_list *tunnel; 2670 unsigned long long key; 2671 struct udphdr *udph; 2672 struct iphdr *iph; 2673 u64 mac; 2674 int i; 2675 2676 if (!pskb_may_pull(skb, sizeof(*udph) + sizeof(*amtrh))) 2677 return true; 2678 2679 iph = ip_hdr(skb); 2680 udph = udp_hdr(skb); 2681 amtrh = (struct amt_header_request *)(udp_hdr(skb) + 1); 2682 2683 if (amtrh->reserved1 || amtrh->reserved2 || amtrh->version) 2684 return true; 2685 2686 list_for_each_entry_rcu(tunnel, &amt->tunnel_list, list) 2687 if (tunnel->ip4 == iph->saddr) 2688 goto send; 2689 2690 spin_lock_bh(&amt->lock); 2691 if (amt->nr_tunnels >= amt->max_tunnels) { 2692 spin_unlock_bh(&amt->lock); 2693 icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0); 2694 return true; 2695 } 2696 2697 tunnel = kzalloc(sizeof(*tunnel) + 2698 (sizeof(struct hlist_head) * amt->hash_buckets), 2699 GFP_ATOMIC); 2700 if (!tunnel) { 2701 spin_unlock_bh(&amt->lock); 2702 return true; 2703 } 2704 2705 tunnel->source_port = udph->source; 2706 tunnel->ip4 = iph->saddr; 2707 2708 memcpy(&key, &tunnel->key, sizeof(unsigned long long)); 2709 tunnel->amt = amt; 2710 spin_lock_init(&tunnel->lock); 2711 for (i = 0; i < amt->hash_buckets; i++) 2712 INIT_HLIST_HEAD(&tunnel->groups[i]); 2713 2714 INIT_DELAYED_WORK(&tunnel->gc_wq, amt_tunnel_expire); 2715 2716 list_add_tail_rcu(&tunnel->list, &amt->tunnel_list); 2717 tunnel->key = amt->key; 2718 __amt_update_relay_status(tunnel, AMT_STATUS_RECEIVED_REQUEST, true); 2719 amt->nr_tunnels++; 2720 mod_delayed_work(amt_wq, &tunnel->gc_wq, 2721 msecs_to_jiffies(amt_gmi(amt))); 2722 spin_unlock_bh(&amt->lock); 2723 2724 send: 2725 tunnel->nonce = amtrh->nonce; 2726 mac = siphash_3u32((__force u32)tunnel->ip4, 2727 (__force u32)tunnel->source_port, 2728 (__force u32)tunnel->nonce, 2729 &tunnel->key); 2730 tunnel->mac = mac >> 16; 2731 2732 if (!netif_running(amt->dev) || !netif_running(amt->stream_dev)) 2733 return true; 2734 2735 if (!amtrh->p) 2736 amt_send_igmp_gq(amt, tunnel); 2737 else 2738 amt_send_mld_gq(amt, tunnel); 2739 2740 return false; 2741 } 2742 2743 static void amt_gw_rcv(struct amt_dev *amt, struct sk_buff *skb) 2744 { 2745 int type = amt_parse_type(skb); 2746 int err = 1; 2747 2748 if (type == -1) 2749 goto drop; 2750 2751 if (amt->mode == AMT_MODE_GATEWAY) { 2752 switch (type) { 2753 case AMT_MSG_ADVERTISEMENT: 2754 err = amt_advertisement_handler(amt, skb); 2755 break; 2756 case AMT_MSG_MEMBERSHIP_QUERY: 2757 err = amt_membership_query_handler(amt, skb); 2758 if (!err) 2759 return; 2760 break; 2761 default: 2762 netdev_dbg(amt->dev, "Invalid type of Gateway\n"); 2763 break; 2764 } 2765 } 2766 drop: 2767 if (err) { 2768 amt->dev->stats.rx_dropped++; 2769 kfree_skb(skb); 2770 } else { 2771 consume_skb(skb); 2772 } 2773 } 2774 2775 static int amt_rcv(struct sock *sk, struct sk_buff *skb) 2776 { 2777 struct amt_dev *amt; 2778 struct iphdr *iph; 2779 __be32 remote_ip; 2780 int type; 2781 bool err; 2782 2783 rcu_read_lock_bh(); 2784 amt = rcu_dereference_sk_user_data(sk); 2785 if (!amt) { 2786 err = true; 2787 kfree_skb(skb); 2788 goto out; 2789 } 2790 remote_ip = READ_ONCE(amt->remote_ip); 2791 2792 skb->dev = amt->dev; 2793 iph = ip_hdr(skb); 2794 type = amt_parse_type(skb); 2795 if (type == -1) { 2796 err = true; 2797 goto drop; 2798 } 2799 2800 if (amt->mode == AMT_MODE_GATEWAY) { 2801 switch (type) { 2802 case AMT_MSG_ADVERTISEMENT: 2803 if (iph->saddr != amt->discovery_ip) { 2804 netdev_dbg(amt->dev, "Invalid Relay IP\n"); 2805 err = true; 2806 goto drop; 2807 } 2808 if (amt_queue_event(amt, AMT_EVENT_RECEIVE, skb)) { 2809 netdev_dbg(amt->dev, "AMT Event queue full\n"); 2810 err = true; 2811 goto drop; 2812 } 2813 goto out; 2814 case AMT_MSG_MULTICAST_DATA: 2815 if (iph->saddr != remote_ip) { 2816 netdev_dbg(amt->dev, "Invalid Relay IP\n"); 2817 err = true; 2818 goto drop; 2819 } 2820 err = amt_multicast_data_handler(amt, skb); 2821 if (err) 2822 goto drop; 2823 else 2824 goto out; 2825 case AMT_MSG_MEMBERSHIP_QUERY: 2826 if (iph->saddr != remote_ip) { 2827 netdev_dbg(amt->dev, "Invalid Relay IP\n"); 2828 err = true; 2829 goto drop; 2830 } 2831 if (amt_queue_event(amt, AMT_EVENT_RECEIVE, skb)) { 2832 netdev_dbg(amt->dev, "AMT Event queue full\n"); 2833 err = true; 2834 goto drop; 2835 } 2836 goto out; 2837 default: 2838 err = true; 2839 netdev_dbg(amt->dev, "Invalid type of Gateway\n"); 2840 break; 2841 } 2842 } else { 2843 switch (type) { 2844 case AMT_MSG_DISCOVERY: 2845 err = amt_discovery_handler(amt, skb); 2846 break; 2847 case AMT_MSG_REQUEST: 2848 err = amt_request_handler(amt, skb); 2849 break; 2850 case AMT_MSG_MEMBERSHIP_UPDATE: 2851 err = amt_update_handler(amt, skb); 2852 if (err) 2853 goto drop; 2854 else 2855 goto out; 2856 default: 2857 err = true; 2858 netdev_dbg(amt->dev, "Invalid type of relay\n"); 2859 break; 2860 } 2861 } 2862 drop: 2863 if (err) { 2864 amt->dev->stats.rx_dropped++; 2865 kfree_skb(skb); 2866 } else { 2867 consume_skb(skb); 2868 } 2869 out: 2870 rcu_read_unlock_bh(); 2871 return 0; 2872 } 2873 2874 static void amt_event_work(struct work_struct *work) 2875 { 2876 struct amt_dev *amt = container_of(work, struct amt_dev, event_wq); 2877 struct sk_buff *skb; 2878 u8 event; 2879 int i; 2880 2881 for (i = 0; i < AMT_MAX_EVENTS; i++) { 2882 spin_lock_bh(&amt->lock); 2883 if (amt->nr_events == 0) { 2884 spin_unlock_bh(&amt->lock); 2885 return; 2886 } 2887 event = amt->events[amt->event_idx].event; 2888 skb = amt->events[amt->event_idx].skb; 2889 amt->events[amt->event_idx].event = AMT_EVENT_NONE; 2890 amt->events[amt->event_idx].skb = NULL; 2891 amt->nr_events--; 2892 amt->event_idx++; 2893 amt->event_idx %= AMT_MAX_EVENTS; 2894 spin_unlock_bh(&amt->lock); 2895 2896 switch (event) { 2897 case AMT_EVENT_RECEIVE: 2898 amt_gw_rcv(amt, skb); 2899 break; 2900 case AMT_EVENT_SEND_DISCOVERY: 2901 amt_event_send_discovery(amt); 2902 break; 2903 case AMT_EVENT_SEND_REQUEST: 2904 amt_event_send_request(amt); 2905 break; 2906 default: 2907 kfree_skb(skb); 2908 break; 2909 } 2910 } 2911 } 2912 2913 static int amt_err_lookup(struct sock *sk, struct sk_buff *skb) 2914 { 2915 struct amt_dev *amt; 2916 int type; 2917 2918 rcu_read_lock_bh(); 2919 amt = rcu_dereference_sk_user_data(sk); 2920 if (!amt) 2921 goto out; 2922 2923 if (amt->mode != AMT_MODE_GATEWAY) 2924 goto drop; 2925 2926 type = amt_parse_type(skb); 2927 if (type == -1) 2928 goto drop; 2929 2930 netdev_dbg(amt->dev, "Received IGMP Unreachable of %s\n", 2931 type_str[type]); 2932 switch (type) { 2933 case AMT_MSG_DISCOVERY: 2934 break; 2935 case AMT_MSG_REQUEST: 2936 case AMT_MSG_MEMBERSHIP_UPDATE: 2937 if (READ_ONCE(amt->status) >= AMT_STATUS_RECEIVED_ADVERTISEMENT) 2938 mod_delayed_work(amt_wq, &amt->req_wq, 0); 2939 break; 2940 default: 2941 goto drop; 2942 } 2943 out: 2944 rcu_read_unlock_bh(); 2945 return 0; 2946 drop: 2947 rcu_read_unlock_bh(); 2948 amt->dev->stats.rx_dropped++; 2949 return 0; 2950 } 2951 2952 static struct sock *amt_create_sock(struct net *net, __be16 port) 2953 { 2954 struct udp_port_cfg udp_conf; 2955 struct socket *sock; 2956 int err; 2957 2958 memset(&udp_conf, 0, sizeof(udp_conf)); 2959 udp_conf.family = AF_INET; 2960 udp_conf.local_ip.s_addr = htonl(INADDR_ANY); 2961 2962 udp_conf.local_udp_port = port; 2963 2964 err = udp_sock_create(net, &udp_conf, &sock); 2965 if (err < 0) 2966 return ERR_PTR(err); 2967 2968 return sock->sk; 2969 } 2970 2971 static int amt_socket_create(struct amt_dev *amt) 2972 { 2973 struct udp_tunnel_sock_cfg tunnel_cfg; 2974 struct sock *sk; 2975 2976 sk = amt_create_sock(amt->net, amt->relay_port); 2977 if (IS_ERR(sk)) 2978 return PTR_ERR(sk); 2979 2980 /* Mark socket as an encapsulation socket */ 2981 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg)); 2982 tunnel_cfg.sk_user_data = amt; 2983 tunnel_cfg.encap_type = 1; 2984 tunnel_cfg.encap_rcv = amt_rcv; 2985 tunnel_cfg.encap_err_lookup = amt_err_lookup; 2986 tunnel_cfg.encap_destroy = NULL; 2987 setup_udp_tunnel_sock(amt->net, sk, &tunnel_cfg); 2988 2989 rcu_assign_pointer(amt->sk, sk); 2990 return 0; 2991 } 2992 2993 static int amt_dev_open(struct net_device *dev) 2994 { 2995 struct amt_dev *amt = netdev_priv(dev); 2996 int err; 2997 2998 amt->ready4 = false; 2999 amt->ready6 = false; 3000 amt->event_idx = 0; 3001 amt->nr_events = 0; 3002 3003 err = amt_socket_create(amt); 3004 if (err) 3005 return err; 3006 3007 amt->req_cnt = 0; 3008 WRITE_ONCE(amt->remote_ip, 0); 3009 amt->nonce = 0; 3010 get_random_bytes(&amt->key, sizeof(siphash_key_t)); 3011 3012 amt->status = AMT_STATUS_INIT; 3013 if (amt->mode == AMT_MODE_GATEWAY) { 3014 mod_delayed_work(amt_wq, &amt->discovery_wq, 0); 3015 mod_delayed_work(amt_wq, &amt->req_wq, 0); 3016 } else if (amt->mode == AMT_MODE_RELAY) { 3017 mod_delayed_work(amt_wq, &amt->secret_wq, 3018 msecs_to_jiffies(AMT_SECRET_TIMEOUT)); 3019 } 3020 return err; 3021 } 3022 3023 static int amt_dev_stop(struct net_device *dev) 3024 { 3025 struct amt_dev *amt = netdev_priv(dev); 3026 struct amt_tunnel_list *tunnel, *tmp; 3027 struct sk_buff *skb; 3028 struct sock *sk; 3029 int i; 3030 3031 cancel_delayed_work_sync(&amt->req_wq); 3032 cancel_delayed_work_sync(&amt->discovery_wq); 3033 cancel_delayed_work_sync(&amt->secret_wq); 3034 3035 /* shutdown */ 3036 sk = rtnl_dereference(amt->sk); 3037 RCU_INIT_POINTER(amt->sk, NULL); 3038 synchronize_net(); 3039 if (sk) 3040 udp_tunnel_sock_release(sk); 3041 3042 cancel_work_sync(&amt->event_wq); 3043 for (i = 0; i < AMT_MAX_EVENTS; i++) { 3044 skb = amt->events[i].skb; 3045 kfree_skb(skb); 3046 amt->events[i].event = AMT_EVENT_NONE; 3047 amt->events[i].skb = NULL; 3048 } 3049 3050 amt->ready4 = false; 3051 amt->ready6 = false; 3052 amt->req_cnt = 0; 3053 WRITE_ONCE(amt->remote_ip, 0); 3054 3055 list_for_each_entry_safe(tunnel, tmp, &amt->tunnel_list, list) { 3056 list_del_rcu(&tunnel->list); 3057 amt->nr_tunnels--; 3058 cancel_delayed_work_sync(&tunnel->gc_wq); 3059 amt_clear_groups(tunnel); 3060 kfree_rcu(tunnel, rcu); 3061 } 3062 3063 return 0; 3064 } 3065 3066 static const struct device_type amt_type = { 3067 .name = "amt", 3068 }; 3069 3070 static int amt_dev_init(struct net_device *dev) 3071 { 3072 struct amt_dev *amt = netdev_priv(dev); 3073 int err; 3074 3075 amt->dev = dev; 3076 3077 err = gro_cells_init(&amt->gro_cells, dev); 3078 if (err) 3079 return err; 3080 3081 return 0; 3082 } 3083 3084 static void amt_dev_uninit(struct net_device *dev) 3085 { 3086 struct amt_dev *amt = netdev_priv(dev); 3087 3088 gro_cells_destroy(&amt->gro_cells); 3089 } 3090 3091 static const struct net_device_ops amt_netdev_ops = { 3092 .ndo_init = amt_dev_init, 3093 .ndo_uninit = amt_dev_uninit, 3094 .ndo_open = amt_dev_open, 3095 .ndo_stop = amt_dev_stop, 3096 .ndo_start_xmit = amt_dev_xmit, 3097 }; 3098 3099 static void amt_link_setup(struct net_device *dev) 3100 { 3101 dev->netdev_ops = &amt_netdev_ops; 3102 dev->needs_free_netdev = true; 3103 SET_NETDEV_DEVTYPE(dev, &amt_type); 3104 dev->min_mtu = ETH_MIN_MTU; 3105 dev->max_mtu = ETH_MAX_MTU; 3106 dev->type = ARPHRD_NONE; 3107 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; 3108 dev->hard_header_len = 0; 3109 dev->addr_len = 0; 3110 dev->priv_flags |= IFF_NO_QUEUE; 3111 dev->lltx = true; 3112 dev->netns_immutable = true; 3113 dev->features |= NETIF_F_GSO_SOFTWARE; 3114 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM; 3115 dev->hw_features |= NETIF_F_FRAGLIST | NETIF_F_RXCSUM; 3116 dev->hw_features |= NETIF_F_GSO_SOFTWARE; 3117 dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS; 3118 eth_hw_addr_random(dev); 3119 eth_zero_addr(dev->broadcast); 3120 ether_setup(dev); 3121 } 3122 3123 static const struct nla_policy amt_policy[IFLA_AMT_MAX + 1] = { 3124 [IFLA_AMT_MODE] = { .type = NLA_U32 }, 3125 [IFLA_AMT_RELAY_PORT] = { .type = NLA_U16 }, 3126 [IFLA_AMT_GATEWAY_PORT] = { .type = NLA_U16 }, 3127 [IFLA_AMT_LINK] = { .type = NLA_U32 }, 3128 [IFLA_AMT_LOCAL_IP] = { .len = sizeof_field(struct iphdr, daddr) }, 3129 [IFLA_AMT_REMOTE_IP] = { .len = sizeof_field(struct iphdr, daddr) }, 3130 [IFLA_AMT_DISCOVERY_IP] = { .len = sizeof_field(struct iphdr, daddr) }, 3131 [IFLA_AMT_MAX_TUNNELS] = { .type = NLA_U32 }, 3132 }; 3133 3134 static int amt_validate(struct nlattr *tb[], struct nlattr *data[], 3135 struct netlink_ext_ack *extack) 3136 { 3137 if (!data) 3138 return -EINVAL; 3139 3140 if (!data[IFLA_AMT_LINK]) { 3141 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_LINK], 3142 "Link attribute is required"); 3143 return -EINVAL; 3144 } 3145 3146 if (!data[IFLA_AMT_MODE]) { 3147 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_MODE], 3148 "Mode attribute is required"); 3149 return -EINVAL; 3150 } 3151 3152 if (nla_get_u32(data[IFLA_AMT_MODE]) > AMT_MODE_MAX) { 3153 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_MODE], 3154 "Mode attribute is not valid"); 3155 return -EINVAL; 3156 } 3157 3158 if (!data[IFLA_AMT_LOCAL_IP]) { 3159 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_DISCOVERY_IP], 3160 "Local attribute is required"); 3161 return -EINVAL; 3162 } 3163 3164 if (!data[IFLA_AMT_DISCOVERY_IP] && 3165 nla_get_u32(data[IFLA_AMT_MODE]) == AMT_MODE_GATEWAY) { 3166 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_LOCAL_IP], 3167 "Discovery attribute is required"); 3168 return -EINVAL; 3169 } 3170 3171 return 0; 3172 } 3173 3174 static int amt_newlink(struct net_device *dev, 3175 struct rtnl_newlink_params *params, 3176 struct netlink_ext_ack *extack) 3177 { 3178 struct net *link_net = rtnl_newlink_link_net(params); 3179 struct amt_dev *amt = netdev_priv(dev); 3180 struct nlattr **data = params->data; 3181 struct nlattr **tb = params->tb; 3182 int err = -EINVAL; 3183 3184 amt->net = link_net; 3185 amt->mode = nla_get_u32(data[IFLA_AMT_MODE]); 3186 3187 if (data[IFLA_AMT_MAX_TUNNELS] && 3188 nla_get_u32(data[IFLA_AMT_MAX_TUNNELS])) 3189 amt->max_tunnels = nla_get_u32(data[IFLA_AMT_MAX_TUNNELS]); 3190 else 3191 amt->max_tunnels = AMT_MAX_TUNNELS; 3192 3193 spin_lock_init(&amt->lock); 3194 amt->max_groups = AMT_MAX_GROUP; 3195 amt->max_sources = AMT_MAX_SOURCE; 3196 amt->hash_buckets = AMT_HSIZE; 3197 amt->nr_tunnels = 0; 3198 get_random_bytes(&amt->hash_seed, sizeof(amt->hash_seed)); 3199 amt->stream_dev = dev_get_by_index(link_net, 3200 nla_get_u32(data[IFLA_AMT_LINK])); 3201 if (!amt->stream_dev) { 3202 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_LINK], 3203 "Can't find stream device"); 3204 return -ENODEV; 3205 } 3206 3207 if (amt->stream_dev->type != ARPHRD_ETHER) { 3208 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_LINK], 3209 "Invalid stream device type"); 3210 goto err; 3211 } 3212 3213 amt->local_ip = nla_get_in_addr(data[IFLA_AMT_LOCAL_IP]); 3214 if (ipv4_is_loopback(amt->local_ip) || 3215 ipv4_is_zeronet(amt->local_ip) || 3216 ipv4_is_multicast(amt->local_ip)) { 3217 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_LOCAL_IP], 3218 "Invalid Local address"); 3219 goto err; 3220 } 3221 3222 amt->relay_port = nla_get_be16_default(data[IFLA_AMT_RELAY_PORT], 3223 htons(IANA_AMT_UDP_PORT)); 3224 3225 amt->gw_port = nla_get_be16_default(data[IFLA_AMT_GATEWAY_PORT], 3226 htons(IANA_AMT_UDP_PORT)); 3227 3228 if (!amt->relay_port) { 3229 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_DISCOVERY_IP], 3230 "relay port must not be 0"); 3231 goto err; 3232 } 3233 if (amt->mode == AMT_MODE_RELAY) { 3234 amt->qrv = READ_ONCE(amt->net->ipv4.sysctl_igmp_qrv); 3235 amt->qri = 10; 3236 dev->needed_headroom = amt->stream_dev->needed_headroom + 3237 AMT_RELAY_HLEN; 3238 dev->mtu = amt->stream_dev->mtu - AMT_RELAY_HLEN; 3239 dev->max_mtu = dev->mtu; 3240 dev->min_mtu = ETH_MIN_MTU + AMT_RELAY_HLEN; 3241 } else { 3242 if (!data[IFLA_AMT_DISCOVERY_IP]) { 3243 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_DISCOVERY_IP], 3244 "discovery must be set in gateway mode"); 3245 goto err; 3246 } 3247 if (!amt->gw_port) { 3248 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_DISCOVERY_IP], 3249 "gateway port must not be 0"); 3250 goto err; 3251 } 3252 WRITE_ONCE(amt->remote_ip, 0); 3253 amt->discovery_ip = nla_get_in_addr(data[IFLA_AMT_DISCOVERY_IP]); 3254 if (ipv4_is_loopback(amt->discovery_ip) || 3255 ipv4_is_zeronet(amt->discovery_ip) || 3256 ipv4_is_multicast(amt->discovery_ip)) { 3257 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_DISCOVERY_IP], 3258 "discovery must be unicast"); 3259 goto err; 3260 } 3261 3262 dev->needed_headroom = amt->stream_dev->needed_headroom + 3263 AMT_GW_HLEN; 3264 dev->mtu = amt->stream_dev->mtu - AMT_GW_HLEN; 3265 dev->max_mtu = dev->mtu; 3266 dev->min_mtu = ETH_MIN_MTU + AMT_GW_HLEN; 3267 } 3268 amt->qi = AMT_INIT_QUERY_INTERVAL; 3269 3270 err = register_netdevice(dev); 3271 if (err < 0) { 3272 netdev_dbg(dev, "failed to register new netdev %d\n", err); 3273 goto err; 3274 } 3275 3276 err = netdev_upper_dev_link(amt->stream_dev, dev, extack); 3277 if (err < 0) { 3278 unregister_netdevice(dev); 3279 goto err; 3280 } 3281 3282 INIT_DELAYED_WORK(&amt->discovery_wq, amt_discovery_work); 3283 INIT_DELAYED_WORK(&amt->req_wq, amt_req_work); 3284 INIT_DELAYED_WORK(&amt->secret_wq, amt_secret_work); 3285 INIT_WORK(&amt->event_wq, amt_event_work); 3286 INIT_LIST_HEAD(&amt->tunnel_list); 3287 return 0; 3288 err: 3289 dev_put(amt->stream_dev); 3290 return err; 3291 } 3292 3293 static void amt_dellink(struct net_device *dev, struct list_head *head) 3294 { 3295 struct amt_dev *amt = netdev_priv(dev); 3296 3297 unregister_netdevice_queue(dev, head); 3298 netdev_upper_dev_unlink(amt->stream_dev, dev); 3299 dev_put(amt->stream_dev); 3300 } 3301 3302 static size_t amt_get_size(const struct net_device *dev) 3303 { 3304 return nla_total_size(sizeof(__u32)) + /* IFLA_AMT_MODE */ 3305 nla_total_size(sizeof(__u16)) + /* IFLA_AMT_RELAY_PORT */ 3306 nla_total_size(sizeof(__u16)) + /* IFLA_AMT_GATEWAY_PORT */ 3307 nla_total_size(sizeof(__u32)) + /* IFLA_AMT_LINK */ 3308 nla_total_size(sizeof(__u32)) + /* IFLA_MAX_TUNNELS */ 3309 nla_total_size(sizeof(__be32)) + /* IFLA_AMT_DISCOVERY_IP */ 3310 nla_total_size(sizeof(__be32)) + /* IFLA_AMT_REMOTE_IP */ 3311 nla_total_size(sizeof(__be32)); /* IFLA_AMT_LOCAL_IP */ 3312 } 3313 3314 static int amt_fill_info(struct sk_buff *skb, const struct net_device *dev) 3315 { 3316 const struct amt_dev *amt = netdev_priv(dev); 3317 __be32 remote_ip; 3318 3319 rcu_read_lock(); 3320 if (nla_put_u32(skb, IFLA_AMT_MODE, amt->mode)) 3321 goto nla_put_failure; 3322 if (nla_put_be16(skb, IFLA_AMT_RELAY_PORT, amt->relay_port)) 3323 goto nla_put_failure; 3324 if (nla_put_be16(skb, IFLA_AMT_GATEWAY_PORT, amt->gw_port)) 3325 goto nla_put_failure; 3326 if (nla_put_u32(skb, IFLA_AMT_LINK, amt->stream_dev->ifindex)) 3327 goto nla_put_failure; 3328 if (nla_put_in_addr(skb, IFLA_AMT_LOCAL_IP, amt->local_ip)) 3329 goto nla_put_failure; 3330 if (nla_put_in_addr(skb, IFLA_AMT_DISCOVERY_IP, amt->discovery_ip)) 3331 goto nla_put_failure; 3332 3333 remote_ip = READ_ONCE(amt->remote_ip); 3334 if (remote_ip) 3335 if (nla_put_in_addr(skb, IFLA_AMT_REMOTE_IP, remote_ip)) 3336 goto nla_put_failure; 3337 if (nla_put_u32(skb, IFLA_AMT_MAX_TUNNELS, amt->max_tunnels)) 3338 goto nla_put_failure; 3339 3340 rcu_read_unlock(); 3341 return 0; 3342 3343 nla_put_failure: 3344 rcu_read_unlock(); 3345 return -EMSGSIZE; 3346 } 3347 3348 static struct rtnl_link_ops amt_link_ops __read_mostly = { 3349 .kind = "amt", 3350 .maxtype = IFLA_AMT_MAX, 3351 .policy = amt_policy, 3352 .priv_size = sizeof(struct amt_dev), 3353 .setup = amt_link_setup, 3354 .validate = amt_validate, 3355 .newlink = amt_newlink, 3356 .dellink = amt_dellink, 3357 .get_size = amt_get_size, 3358 .fill_info = amt_fill_info, 3359 }; 3360 3361 static struct net_device *amt_lookup_upper_dev(struct net_device *dev) 3362 { 3363 struct net_device *upper_dev; 3364 struct amt_dev *amt; 3365 3366 for_each_netdev(dev_net(dev), upper_dev) { 3367 if (netif_is_amt(upper_dev)) { 3368 amt = netdev_priv(upper_dev); 3369 if (amt->stream_dev == dev) 3370 return upper_dev; 3371 } 3372 } 3373 3374 return NULL; 3375 } 3376 3377 static int amt_device_event(struct notifier_block *unused, 3378 unsigned long event, void *ptr) 3379 { 3380 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 3381 struct net_device *upper_dev; 3382 struct amt_dev *amt; 3383 LIST_HEAD(list); 3384 int new_mtu; 3385 3386 upper_dev = amt_lookup_upper_dev(dev); 3387 if (!upper_dev) 3388 return NOTIFY_DONE; 3389 amt = netdev_priv(upper_dev); 3390 3391 switch (event) { 3392 case NETDEV_UNREGISTER: 3393 amt_dellink(amt->dev, &list); 3394 unregister_netdevice_many(&list); 3395 break; 3396 case NETDEV_CHANGEMTU: 3397 if (amt->mode == AMT_MODE_RELAY) 3398 new_mtu = dev->mtu - AMT_RELAY_HLEN; 3399 else 3400 new_mtu = dev->mtu - AMT_GW_HLEN; 3401 3402 dev_set_mtu(amt->dev, new_mtu); 3403 break; 3404 } 3405 3406 return NOTIFY_DONE; 3407 } 3408 3409 static struct notifier_block amt_notifier_block __read_mostly = { 3410 .notifier_call = amt_device_event, 3411 }; 3412 3413 static int __init amt_init(void) 3414 { 3415 int err; 3416 3417 err = register_netdevice_notifier(&amt_notifier_block); 3418 if (err < 0) 3419 goto err; 3420 3421 err = rtnl_link_register(&amt_link_ops); 3422 if (err < 0) 3423 goto unregister_notifier; 3424 3425 amt_wq = alloc_workqueue("amt", WQ_UNBOUND, 0); 3426 if (!amt_wq) { 3427 err = -ENOMEM; 3428 goto rtnl_unregister; 3429 } 3430 3431 spin_lock_init(&source_gc_lock); 3432 spin_lock_bh(&source_gc_lock); 3433 INIT_DELAYED_WORK(&source_gc_wq, amt_source_gc_work); 3434 mod_delayed_work(amt_wq, &source_gc_wq, 3435 msecs_to_jiffies(AMT_GC_INTERVAL)); 3436 spin_unlock_bh(&source_gc_lock); 3437 3438 return 0; 3439 3440 rtnl_unregister: 3441 rtnl_link_unregister(&amt_link_ops); 3442 unregister_notifier: 3443 unregister_netdevice_notifier(&amt_notifier_block); 3444 err: 3445 pr_err("error loading AMT module loaded\n"); 3446 return err; 3447 } 3448 late_initcall(amt_init); 3449 3450 static void __exit amt_fini(void) 3451 { 3452 rtnl_link_unregister(&amt_link_ops); 3453 unregister_netdevice_notifier(&amt_notifier_block); 3454 cancel_delayed_work_sync(&source_gc_wq); 3455 __amt_source_gc_work(); 3456 destroy_workqueue(amt_wq); 3457 } 3458 module_exit(amt_fini); 3459 3460 MODULE_LICENSE("GPL"); 3461 MODULE_DESCRIPTION("Driver for Automatic Multicast Tunneling (AMT)"); 3462 MODULE_AUTHOR("Taehee Yoo <ap420073@gmail.com>"); 3463 MODULE_ALIAS_RTNL_LINK("amt"); 3464