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 udp_set_len_short(udph, 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 udp_set_len_short(udph, 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 = ip_hdr(skb)->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 = ipv6_hdr(skb)->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 == group.ip4) 1285 goto found; 1286 #if IS_ENABLED(CONFIG_IPV6) 1287 } else { 1288 if (ipv6_addr_equal(&gnode->group_addr.ip6, 1289 &group.ip6)) 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 amt_group_node *gnode; 2007 union amt_addr group, host; 2008 struct igmpv3_grec *grec; 2009 __be32 saddr; 2010 u16 nsrcs; 2011 u16 ngrec; 2012 int i; 2013 2014 saddr = ip_hdr(skb)->saddr; 2015 ngrec = ntohs(ihrv3->ngrec); 2016 2017 for (i = 0; i < ngrec; i++) { 2018 len += sizeof(*grec); 2019 if (!ip_mc_may_pull(skb, len)) 2020 break; 2021 2022 grec = (void *)(skb->data + len - sizeof(*grec)); 2023 nsrcs = ntohs(grec->grec_nsrcs); 2024 2025 len += nsrcs * sizeof(__be32); 2026 if (!ip_mc_may_pull(skb, len)) 2027 break; 2028 2029 grec = (void *)(skb->data + len - sizeof(*grec) - 2030 nsrcs * sizeof(__be32)); 2031 2032 memset(&group, 0, sizeof(union amt_addr)); 2033 group.ip4 = grec->grec_mca; 2034 memset(&host, 0, sizeof(union amt_addr)); 2035 host.ip4 = saddr; 2036 gnode = amt_lookup_group(tunnel, &group, &host, false); 2037 if (!gnode) { 2038 gnode = amt_add_group(amt, tunnel, &group, &host, 2039 false); 2040 if (IS_ERR(gnode)) 2041 continue; 2042 } 2043 2044 amt_add_srcs(amt, tunnel, gnode, grec, false); 2045 switch (grec->grec_type) { 2046 case IGMPV3_MODE_IS_INCLUDE: 2047 amt_mcast_is_in_handler(amt, tunnel, gnode, grec, 2048 zero_grec, false); 2049 break; 2050 case IGMPV3_MODE_IS_EXCLUDE: 2051 amt_mcast_is_ex_handler(amt, tunnel, gnode, grec, 2052 zero_grec, false); 2053 break; 2054 case IGMPV3_CHANGE_TO_INCLUDE: 2055 amt_mcast_to_in_handler(amt, tunnel, gnode, grec, 2056 zero_grec, false); 2057 break; 2058 case IGMPV3_CHANGE_TO_EXCLUDE: 2059 amt_mcast_to_ex_handler(amt, tunnel, gnode, grec, 2060 zero_grec, false); 2061 break; 2062 case IGMPV3_ALLOW_NEW_SOURCES: 2063 amt_mcast_allow_handler(amt, tunnel, gnode, grec, 2064 zero_grec, false); 2065 break; 2066 case IGMPV3_BLOCK_OLD_SOURCES: 2067 amt_mcast_block_handler(amt, tunnel, gnode, grec, 2068 zero_grec, false); 2069 break; 2070 default: 2071 break; 2072 } 2073 amt_cleanup_srcs(amt, tunnel, gnode); 2074 } 2075 } 2076 2077 /* caller held tunnel->lock */ 2078 static void amt_igmp_report_handler(struct amt_dev *amt, struct sk_buff *skb, 2079 struct amt_tunnel_list *tunnel) 2080 { 2081 struct igmphdr *ih = igmp_hdr(skb); 2082 2083 switch (ih->type) { 2084 case IGMPV3_HOST_MEMBERSHIP_REPORT: 2085 amt_igmpv3_report_handler(amt, skb, tunnel); 2086 break; 2087 case IGMPV2_HOST_MEMBERSHIP_REPORT: 2088 amt_igmpv2_report_handler(amt, skb, tunnel); 2089 break; 2090 case IGMP_HOST_LEAVE_MESSAGE: 2091 amt_igmpv2_leave_handler(amt, skb, tunnel); 2092 break; 2093 default: 2094 break; 2095 } 2096 } 2097 2098 #if IS_ENABLED(CONFIG_IPV6) 2099 /* RFC 3810 2100 * 8.3.2. In the Presence of MLDv1 Multicast Address Listeners 2101 * 2102 * When Multicast Address Compatibility Mode is MLDv2, a router acts 2103 * using the MLDv2 protocol for that multicast address. When Multicast 2104 * Address Compatibility Mode is MLDv1, a router internally translates 2105 * the following MLDv1 messages for that multicast address to their 2106 * MLDv2 equivalents: 2107 * 2108 * MLDv1 Message MLDv2 Equivalent 2109 * -------------- ----------------- 2110 * Report IS_EX( {} ) 2111 * Done TO_IN( {} ) 2112 */ 2113 static void amt_mldv1_report_handler(struct amt_dev *amt, struct sk_buff *skb, 2114 struct amt_tunnel_list *tunnel) 2115 { 2116 struct mld_msg *mld = (struct mld_msg *)icmp6_hdr(skb); 2117 struct ipv6hdr *ip6h = ipv6_hdr(skb); 2118 struct amt_group_node *gnode; 2119 union amt_addr group, host; 2120 2121 memcpy(&group.ip6, &mld->mld_mca, sizeof(struct in6_addr)); 2122 memcpy(&host.ip6, &ip6h->saddr, sizeof(struct in6_addr)); 2123 2124 gnode = amt_lookup_group(tunnel, &group, &host, true); 2125 if (!gnode) { 2126 gnode = amt_add_group(amt, tunnel, &group, &host, true); 2127 if (!IS_ERR(gnode)) { 2128 gnode->filter_mode = MCAST_EXCLUDE; 2129 if (!mod_delayed_work(amt_wq, &gnode->group_timer, 2130 msecs_to_jiffies(amt_gmi(amt)))) 2131 dev_hold(amt->dev); 2132 } 2133 } 2134 } 2135 2136 /* RFC 3810 2137 * 8.3.2. In the Presence of MLDv1 Multicast Address Listeners 2138 * 2139 * When Multicast Address Compatibility Mode is MLDv2, a router acts 2140 * using the MLDv2 protocol for that multicast address. When Multicast 2141 * Address Compatibility Mode is MLDv1, a router internally translates 2142 * the following MLDv1 messages for that multicast address to their 2143 * MLDv2 equivalents: 2144 * 2145 * MLDv1 Message MLDv2 Equivalent 2146 * -------------- ----------------- 2147 * Report IS_EX( {} ) 2148 * Done TO_IN( {} ) 2149 */ 2150 static void amt_mldv1_leave_handler(struct amt_dev *amt, struct sk_buff *skb, 2151 struct amt_tunnel_list *tunnel) 2152 { 2153 struct mld_msg *mld = (struct mld_msg *)icmp6_hdr(skb); 2154 struct iphdr *iph = ip_hdr(skb); 2155 struct amt_group_node *gnode; 2156 union amt_addr group, host; 2157 2158 memcpy(&group.ip6, &mld->mld_mca, sizeof(struct in6_addr)); 2159 memset(&host, 0, sizeof(union amt_addr)); 2160 host.ip4 = iph->saddr; 2161 2162 gnode = amt_lookup_group(tunnel, &group, &host, true); 2163 if (gnode) { 2164 amt_del_group(amt, gnode); 2165 return; 2166 } 2167 } 2168 2169 static void amt_mldv2_report_handler(struct amt_dev *amt, struct sk_buff *skb, 2170 struct amt_tunnel_list *tunnel) 2171 { 2172 struct mld2_report *mld2r = (struct mld2_report *)icmp6_hdr(skb); 2173 int len = skb_transport_offset(skb) + sizeof(*mld2r); 2174 void *zero_grec = (void *)&mldv2_zero_grec; 2175 struct amt_group_node *gnode; 2176 union amt_addr group, host; 2177 struct mld2_grec *grec; 2178 struct in6_addr saddr; 2179 u16 nsrcs; 2180 u16 ngrec; 2181 int i; 2182 2183 saddr = ipv6_hdr(skb)->saddr; 2184 ngrec = ntohs(mld2r->mld2r_ngrec); 2185 2186 for (i = 0; i < ngrec; i++) { 2187 len += sizeof(*grec); 2188 if (!ipv6_mc_may_pull(skb, len)) 2189 break; 2190 2191 grec = (void *)(skb->data + len - sizeof(*grec)); 2192 nsrcs = ntohs(grec->grec_nsrcs); 2193 2194 len += nsrcs * sizeof(struct in6_addr); 2195 if (!ipv6_mc_may_pull(skb, len)) 2196 break; 2197 2198 grec = (void *)(skb->data + len - sizeof(*grec) - 2199 nsrcs * sizeof(struct in6_addr)); 2200 2201 memset(&group, 0, sizeof(union amt_addr)); 2202 group.ip6 = grec->grec_mca; 2203 memset(&host, 0, sizeof(union amt_addr)); 2204 host.ip6 = saddr; 2205 gnode = amt_lookup_group(tunnel, &group, &host, true); 2206 if (!gnode) { 2207 gnode = amt_add_group(amt, tunnel, &group, &host, 2208 ETH_P_IPV6); 2209 if (IS_ERR(gnode)) 2210 continue; 2211 } 2212 2213 amt_add_srcs(amt, tunnel, gnode, grec, true); 2214 switch (grec->grec_type) { 2215 case MLD2_MODE_IS_INCLUDE: 2216 amt_mcast_is_in_handler(amt, tunnel, gnode, grec, 2217 zero_grec, true); 2218 break; 2219 case MLD2_MODE_IS_EXCLUDE: 2220 amt_mcast_is_ex_handler(amt, tunnel, gnode, grec, 2221 zero_grec, true); 2222 break; 2223 case MLD2_CHANGE_TO_INCLUDE: 2224 amt_mcast_to_in_handler(amt, tunnel, gnode, grec, 2225 zero_grec, true); 2226 break; 2227 case MLD2_CHANGE_TO_EXCLUDE: 2228 amt_mcast_to_ex_handler(amt, tunnel, gnode, grec, 2229 zero_grec, true); 2230 break; 2231 case MLD2_ALLOW_NEW_SOURCES: 2232 amt_mcast_allow_handler(amt, tunnel, gnode, grec, 2233 zero_grec, true); 2234 break; 2235 case MLD2_BLOCK_OLD_SOURCES: 2236 amt_mcast_block_handler(amt, tunnel, gnode, grec, 2237 zero_grec, true); 2238 break; 2239 default: 2240 break; 2241 } 2242 amt_cleanup_srcs(amt, tunnel, gnode); 2243 } 2244 } 2245 2246 /* caller held tunnel->lock */ 2247 static void amt_mld_report_handler(struct amt_dev *amt, struct sk_buff *skb, 2248 struct amt_tunnel_list *tunnel) 2249 { 2250 struct mld_msg *mld = (struct mld_msg *)icmp6_hdr(skb); 2251 2252 switch (mld->mld_type) { 2253 case ICMPV6_MGM_REPORT: 2254 amt_mldv1_report_handler(amt, skb, tunnel); 2255 break; 2256 case ICMPV6_MLD2_REPORT: 2257 amt_mldv2_report_handler(amt, skb, tunnel); 2258 break; 2259 case ICMPV6_MGM_REDUCTION: 2260 amt_mldv1_leave_handler(amt, skb, tunnel); 2261 break; 2262 default: 2263 break; 2264 } 2265 } 2266 #endif 2267 2268 static bool amt_advertisement_handler(struct amt_dev *amt, struct sk_buff *skb) 2269 { 2270 struct amt_header_advertisement *amta; 2271 int hdr_size; 2272 2273 hdr_size = sizeof(*amta) + sizeof(struct udphdr); 2274 if (!pskb_may_pull(skb, hdr_size)) 2275 return true; 2276 2277 amta = (struct amt_header_advertisement *)(udp_hdr(skb) + 1); 2278 if (!amta->ip4) 2279 return true; 2280 2281 if (amta->reserved || amta->version) 2282 return true; 2283 2284 if (ipv4_is_loopback(amta->ip4) || ipv4_is_multicast(amta->ip4) || 2285 ipv4_is_zeronet(amta->ip4)) 2286 return true; 2287 2288 if (amt->status != AMT_STATUS_SENT_DISCOVERY || 2289 amt->nonce != amta->nonce) 2290 return true; 2291 2292 WRITE_ONCE(amt->remote_ip, amta->ip4); 2293 netdev_dbg(amt->dev, "advertised remote ip = %pI4\n", &amta->ip4); 2294 mod_delayed_work(amt_wq, &amt->req_wq, 0); 2295 2296 amt_update_gw_status(amt, AMT_STATUS_RECEIVED_ADVERTISEMENT, true); 2297 return false; 2298 } 2299 2300 static bool amt_multicast_data_handler(struct amt_dev *amt, struct sk_buff *skb) 2301 { 2302 struct amt_header_mcast_data *amtmd; 2303 int hdr_size, len, err; 2304 struct ethhdr *eth; 2305 struct iphdr *iph; 2306 2307 if (READ_ONCE(amt->status) != AMT_STATUS_SENT_UPDATE) 2308 return true; 2309 2310 hdr_size = sizeof(*amtmd) + sizeof(struct udphdr); 2311 if (!pskb_may_pull(skb, hdr_size)) 2312 return true; 2313 2314 amtmd = (struct amt_header_mcast_data *)(udp_hdr(skb) + 1); 2315 if (amtmd->reserved || amtmd->version) 2316 return true; 2317 2318 if (iptunnel_pull_header(skb, hdr_size, htons(ETH_P_IP), false)) 2319 return true; 2320 2321 skb_reset_network_header(skb); 2322 skb_push(skb, sizeof(*eth)); 2323 skb_reset_mac_header(skb); 2324 skb_pull(skb, sizeof(*eth)); 2325 2326 if (skb_cow_head(skb, 0)) 2327 return true; 2328 2329 if (!pskb_may_pull(skb, sizeof(*iph))) 2330 return true; 2331 iph = ip_hdr(skb); 2332 2333 if (iph->version == 4) { 2334 if (!ipv4_is_multicast(iph->daddr)) 2335 return true; 2336 skb->protocol = htons(ETH_P_IP); 2337 eth = eth_hdr(skb); 2338 eth->h_proto = htons(ETH_P_IP); 2339 ip_eth_mc_map(iph->daddr, eth->h_dest); 2340 #if IS_ENABLED(CONFIG_IPV6) 2341 } else if (iph->version == 6) { 2342 struct ipv6hdr *ip6h; 2343 2344 if (!pskb_may_pull(skb, sizeof(*ip6h))) 2345 return true; 2346 2347 ip6h = ipv6_hdr(skb); 2348 if (!ipv6_addr_is_multicast(&ip6h->daddr)) 2349 return true; 2350 skb->protocol = htons(ETH_P_IPV6); 2351 eth = eth_hdr(skb); 2352 eth->h_proto = htons(ETH_P_IPV6); 2353 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest); 2354 #endif 2355 } else { 2356 return true; 2357 } 2358 2359 skb->pkt_type = PACKET_MULTICAST; 2360 skb->ip_summed = CHECKSUM_NONE; 2361 len = skb->len; 2362 err = gro_cells_receive(&amt->gro_cells, skb); 2363 if (likely(err == NET_RX_SUCCESS)) 2364 dev_sw_netstats_rx_add(amt->dev, len); 2365 else 2366 amt->dev->stats.rx_dropped++; 2367 2368 return false; 2369 } 2370 2371 static bool amt_membership_query_handler(struct amt_dev *amt, 2372 struct sk_buff *skb) 2373 { 2374 struct amt_header_membership_query *amtmq; 2375 struct ethhdr *eth, *oeth; 2376 struct igmpv3_query *ihv3; 2377 u8 h_source[ETH_ALEN]; 2378 struct iphdr *iph; 2379 int hdr_size, len; 2380 u64 response_mac; 2381 2382 hdr_size = sizeof(*amtmq) + sizeof(struct udphdr); 2383 if (!pskb_may_pull(skb, hdr_size)) 2384 return true; 2385 2386 amtmq = (struct amt_header_membership_query *)(udp_hdr(skb) + 1); 2387 if (amtmq->reserved || amtmq->version) 2388 return true; 2389 2390 if (amtmq->nonce != amt->nonce) 2391 return true; 2392 2393 response_mac = amtmq->response_mac; 2394 2395 hdr_size -= sizeof(*eth); 2396 if (iptunnel_pull_header(skb, hdr_size, htons(ETH_P_TEB), false)) 2397 return true; 2398 2399 oeth = eth_hdr(skb); 2400 skb_reset_mac_header(skb); 2401 skb_pull(skb, sizeof(*eth)); 2402 skb_reset_network_header(skb); 2403 eth = eth_hdr(skb); 2404 ether_addr_copy(h_source, oeth->h_source); 2405 if (skb_cow_head(skb, 0)) 2406 return true; 2407 if (!pskb_may_pull(skb, sizeof(*iph))) 2408 return true; 2409 2410 iph = ip_hdr(skb); 2411 if (iph->version == 4) { 2412 if (READ_ONCE(amt->ready4)) 2413 return true; 2414 2415 if (!pskb_may_pull(skb, sizeof(*iph) + AMT_IPHDR_OPTS + 2416 sizeof(*ihv3))) 2417 return true; 2418 2419 iph = ip_hdr(skb); 2420 if (!ipv4_is_multicast(iph->daddr)) 2421 return true; 2422 2423 ihv3 = skb_pull(skb, sizeof(*iph) + AMT_IPHDR_OPTS); 2424 skb_reset_transport_header(skb); 2425 skb_push(skb, sizeof(*iph) + AMT_IPHDR_OPTS); 2426 WRITE_ONCE(amt->ready4, true); 2427 amt->mac = response_mac; 2428 amt->req_cnt = 0; 2429 amt->qi = ihv3->qqic; 2430 skb->protocol = htons(ETH_P_IP); 2431 eth = eth_hdr(skb); 2432 eth->h_proto = htons(ETH_P_IP); 2433 ip_eth_mc_map(iph->daddr, eth->h_dest); 2434 #if IS_ENABLED(CONFIG_IPV6) 2435 } else if (iph->version == 6) { 2436 struct mld2_query *mld2q; 2437 struct ipv6hdr *ip6h; 2438 2439 if (READ_ONCE(amt->ready6)) 2440 return true; 2441 2442 if (!pskb_may_pull(skb, sizeof(*ip6h) + AMT_IP6HDR_OPTS + 2443 sizeof(*mld2q))) 2444 return true; 2445 2446 ip6h = ipv6_hdr(skb); 2447 if (!ipv6_addr_is_multicast(&ip6h->daddr)) 2448 return true; 2449 2450 mld2q = skb_pull(skb, sizeof(*ip6h) + AMT_IP6HDR_OPTS); 2451 skb_reset_transport_header(skb); 2452 skb_push(skb, sizeof(*ip6h) + AMT_IP6HDR_OPTS); 2453 WRITE_ONCE(amt->ready6, true); 2454 amt->mac = response_mac; 2455 amt->req_cnt = 0; 2456 amt->qi = mld2q->mld2q_qqic; 2457 skb->protocol = htons(ETH_P_IPV6); 2458 eth = eth_hdr(skb); 2459 eth->h_proto = htons(ETH_P_IPV6); 2460 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest); 2461 #endif 2462 } else { 2463 return true; 2464 } 2465 2466 ether_addr_copy(eth->h_source, h_source); 2467 skb->pkt_type = PACKET_MULTICAST; 2468 skb->ip_summed = CHECKSUM_NONE; 2469 len = skb->len; 2470 local_bh_disable(); 2471 if (__netif_rx(skb) == NET_RX_SUCCESS) { 2472 amt_update_gw_status(amt, AMT_STATUS_RECEIVED_QUERY, true); 2473 dev_sw_netstats_rx_add(amt->dev, len); 2474 } else { 2475 amt->dev->stats.rx_dropped++; 2476 } 2477 local_bh_enable(); 2478 2479 return false; 2480 } 2481 2482 static bool amt_update_handler(struct amt_dev *amt, struct sk_buff *skb) 2483 { 2484 struct amt_header_membership_update *amtmu; 2485 struct amt_tunnel_list *tunnel; 2486 struct ethhdr *eth; 2487 struct iphdr *iph; 2488 int len, hdr_size; 2489 u64 response_mac; 2490 __be32 saddr; 2491 __be32 nonce; 2492 2493 saddr = ip_hdr(skb)->saddr; 2494 2495 hdr_size = sizeof(*amtmu) + sizeof(struct udphdr); 2496 if (!pskb_may_pull(skb, hdr_size)) 2497 return true; 2498 2499 amtmu = (struct amt_header_membership_update *)(udp_hdr(skb) + 1); 2500 if (amtmu->reserved || amtmu->version) 2501 return true; 2502 2503 nonce = amtmu->nonce; 2504 response_mac = amtmu->response_mac; 2505 2506 if (iptunnel_pull_header(skb, hdr_size, skb->protocol, false)) 2507 return true; 2508 2509 skb_reset_network_header(skb); 2510 2511 list_for_each_entry_rcu(tunnel, &amt->tunnel_list, list) { 2512 if (tunnel->ip4 == saddr) { 2513 if ((nonce == tunnel->nonce && 2514 response_mac == tunnel->mac)) { 2515 mod_delayed_work(amt_wq, &tunnel->gc_wq, 2516 msecs_to_jiffies(amt_gmi(amt)) 2517 * 3); 2518 goto report; 2519 } else { 2520 netdev_dbg(amt->dev, "Invalid MAC\n"); 2521 return true; 2522 } 2523 } 2524 } 2525 2526 return true; 2527 2528 report: 2529 if (!pskb_may_pull(skb, sizeof(*iph))) 2530 return true; 2531 2532 if (skb_cow_head(skb, 0)) 2533 return true; 2534 2535 iph = ip_hdr(skb); 2536 if (iph->version == 4) { 2537 if (ip_mc_check_igmp(skb)) { 2538 netdev_dbg(amt->dev, "Invalid IGMP\n"); 2539 return true; 2540 } 2541 2542 spin_lock_bh(&tunnel->lock); 2543 amt_igmp_report_handler(amt, skb, tunnel); 2544 spin_unlock_bh(&tunnel->lock); 2545 2546 skb_push(skb, sizeof(struct ethhdr)); 2547 skb_reset_mac_header(skb); 2548 eth = eth_hdr(skb); 2549 skb->protocol = htons(ETH_P_IP); 2550 eth->h_proto = htons(ETH_P_IP); 2551 iph = ip_hdr(skb); 2552 ip_eth_mc_map(iph->daddr, eth->h_dest); 2553 #if IS_ENABLED(CONFIG_IPV6) 2554 } else if (iph->version == 6) { 2555 struct ipv6hdr *ip6h = ipv6_hdr(skb); 2556 2557 if (ipv6_mc_check_mld(skb)) { 2558 netdev_dbg(amt->dev, "Invalid MLD\n"); 2559 return true; 2560 } 2561 2562 spin_lock_bh(&tunnel->lock); 2563 amt_mld_report_handler(amt, skb, tunnel); 2564 spin_unlock_bh(&tunnel->lock); 2565 2566 skb_push(skb, sizeof(struct ethhdr)); 2567 skb_reset_mac_header(skb); 2568 eth = eth_hdr(skb); 2569 skb->protocol = htons(ETH_P_IPV6); 2570 eth->h_proto = htons(ETH_P_IPV6); 2571 ip6h = ipv6_hdr(skb); 2572 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest); 2573 #endif 2574 } else { 2575 netdev_dbg(amt->dev, "Unsupported Protocol\n"); 2576 return true; 2577 } 2578 2579 skb_pull(skb, sizeof(struct ethhdr)); 2580 skb->pkt_type = PACKET_MULTICAST; 2581 skb->ip_summed = CHECKSUM_NONE; 2582 len = skb->len; 2583 if (__netif_rx(skb) == NET_RX_SUCCESS) { 2584 amt_update_relay_status(tunnel, AMT_STATUS_RECEIVED_UPDATE, 2585 true); 2586 dev_sw_netstats_rx_add(amt->dev, len); 2587 } else { 2588 amt->dev->stats.rx_dropped++; 2589 } 2590 2591 return false; 2592 } 2593 2594 static void amt_send_advertisement(struct amt_dev *amt, __be32 nonce, 2595 __be32 daddr, __be16 dport) 2596 { 2597 struct amt_header_advertisement *amta; 2598 int hlen, tlen, offset; 2599 struct udphdr *udph; 2600 struct sk_buff *skb; 2601 struct iphdr *iph; 2602 struct rtable *rt; 2603 struct flowi4 fl4; 2604 struct sock *sk; 2605 u32 len; 2606 int err; 2607 2608 rcu_read_lock(); 2609 sk = rcu_dereference(amt->sk); 2610 if (!sk) 2611 goto out; 2612 2613 if (!netif_running(amt->stream_dev) || !netif_running(amt->dev)) 2614 goto out; 2615 2616 rt = ip_route_output_ports(amt->net, &fl4, sk, 2617 daddr, amt->local_ip, 2618 dport, amt->relay_port, 2619 IPPROTO_UDP, 0, 2620 amt->stream_dev->ifindex); 2621 if (IS_ERR(rt)) { 2622 amt->dev->stats.tx_errors++; 2623 goto out; 2624 } 2625 2626 hlen = LL_RESERVED_SPACE(amt->dev); 2627 tlen = amt->dev->needed_tailroom; 2628 len = hlen + tlen + sizeof(*iph) + sizeof(*udph) + sizeof(*amta); 2629 skb = netdev_alloc_skb_ip_align(amt->dev, len); 2630 if (!skb) { 2631 ip_rt_put(rt); 2632 amt->dev->stats.tx_errors++; 2633 goto out; 2634 } 2635 2636 skb->priority = TC_PRIO_CONTROL; 2637 skb_dst_set(skb, &rt->dst); 2638 2639 len = sizeof(*iph) + sizeof(*udph) + sizeof(*amta); 2640 skb_reset_network_header(skb); 2641 skb_put(skb, len); 2642 amta = skb_pull(skb, sizeof(*iph) + sizeof(*udph)); 2643 amta->version = 0; 2644 amta->type = AMT_MSG_ADVERTISEMENT; 2645 amta->reserved = 0; 2646 amta->nonce = nonce; 2647 amta->ip4 = amt->local_ip; 2648 skb_push(skb, sizeof(*udph)); 2649 skb_reset_transport_header(skb); 2650 udph = udp_hdr(skb); 2651 udph->source = amt->relay_port; 2652 udph->dest = dport; 2653 udp_set_len_short(udph, sizeof(*amta) + sizeof(*udph)); 2654 udph->check = 0; 2655 offset = skb_transport_offset(skb); 2656 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0); 2657 udph->check = csum_tcpudp_magic(amt->local_ip, daddr, 2658 sizeof(*udph) + sizeof(*amta), 2659 IPPROTO_UDP, skb->csum); 2660 2661 skb_push(skb, sizeof(*iph)); 2662 iph = ip_hdr(skb); 2663 iph->version = 4; 2664 iph->ihl = (sizeof(struct iphdr)) >> 2; 2665 iph->tos = AMT_TOS; 2666 iph->frag_off = 0; 2667 iph->ttl = ip4_dst_hoplimit(&rt->dst); 2668 iph->daddr = daddr; 2669 iph->saddr = amt->local_ip; 2670 iph->protocol = IPPROTO_UDP; 2671 iph->tot_len = htons(len); 2672 2673 skb->ip_summed = CHECKSUM_NONE; 2674 ip_select_ident(amt->net, skb, NULL); 2675 ip_send_check(iph); 2676 err = ip_local_out(amt->net, sk, skb); 2677 if (unlikely(net_xmit_eval(err))) 2678 amt->dev->stats.tx_errors++; 2679 2680 out: 2681 rcu_read_unlock(); 2682 } 2683 2684 static bool amt_discovery_handler(struct amt_dev *amt, struct sk_buff *skb) 2685 { 2686 struct amt_header_discovery *amtd; 2687 struct udphdr *udph; 2688 struct iphdr *iph; 2689 2690 if (!pskb_may_pull(skb, sizeof(*udph) + sizeof(*amtd))) 2691 return true; 2692 2693 iph = ip_hdr(skb); 2694 udph = udp_hdr(skb); 2695 amtd = (struct amt_header_discovery *)(udp_hdr(skb) + 1); 2696 2697 if (amtd->reserved || amtd->version) 2698 return true; 2699 2700 amt_send_advertisement(amt, amtd->nonce, iph->saddr, udph->source); 2701 2702 return false; 2703 } 2704 2705 static bool amt_request_handler(struct amt_dev *amt, struct sk_buff *skb) 2706 { 2707 struct amt_header_request *amtrh; 2708 struct amt_tunnel_list *tunnel; 2709 unsigned long long key; 2710 struct udphdr *udph; 2711 struct iphdr *iph; 2712 u64 mac; 2713 int i; 2714 2715 if (!pskb_may_pull(skb, sizeof(*udph) + sizeof(*amtrh))) 2716 return true; 2717 2718 iph = ip_hdr(skb); 2719 udph = udp_hdr(skb); 2720 amtrh = (struct amt_header_request *)(udp_hdr(skb) + 1); 2721 2722 if (amtrh->reserved1 || amtrh->reserved2 || amtrh->version) 2723 return true; 2724 2725 list_for_each_entry_rcu(tunnel, &amt->tunnel_list, list) 2726 if (tunnel->ip4 == iph->saddr) 2727 goto send; 2728 2729 spin_lock_bh(&amt->lock); 2730 if (amt->nr_tunnels >= amt->max_tunnels) { 2731 spin_unlock_bh(&amt->lock); 2732 icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0); 2733 return true; 2734 } 2735 2736 tunnel = kzalloc(sizeof(*tunnel) + 2737 (sizeof(struct hlist_head) * amt->hash_buckets), 2738 GFP_ATOMIC); 2739 if (!tunnel) { 2740 spin_unlock_bh(&amt->lock); 2741 return true; 2742 } 2743 2744 tunnel->source_port = udph->source; 2745 tunnel->ip4 = iph->saddr; 2746 2747 memcpy(&key, &tunnel->key, sizeof(unsigned long long)); 2748 tunnel->amt = amt; 2749 spin_lock_init(&tunnel->lock); 2750 for (i = 0; i < amt->hash_buckets; i++) 2751 INIT_HLIST_HEAD(&tunnel->groups[i]); 2752 2753 INIT_DELAYED_WORK(&tunnel->gc_wq, amt_tunnel_expire); 2754 2755 list_add_tail_rcu(&tunnel->list, &amt->tunnel_list); 2756 tunnel->key = amt->key; 2757 __amt_update_relay_status(tunnel, AMT_STATUS_RECEIVED_REQUEST, true); 2758 amt->nr_tunnels++; 2759 mod_delayed_work(amt_wq, &tunnel->gc_wq, 2760 msecs_to_jiffies(amt_gmi(amt))); 2761 spin_unlock_bh(&amt->lock); 2762 2763 send: 2764 tunnel->nonce = amtrh->nonce; 2765 mac = siphash_3u32((__force u32)tunnel->ip4, 2766 (__force u32)tunnel->source_port, 2767 (__force u32)tunnel->nonce, 2768 &tunnel->key); 2769 tunnel->mac = mac >> 16; 2770 2771 if (!netif_running(amt->dev) || !netif_running(amt->stream_dev)) 2772 return true; 2773 2774 if (!amtrh->p) 2775 amt_send_igmp_gq(amt, tunnel); 2776 else 2777 amt_send_mld_gq(amt, tunnel); 2778 2779 return false; 2780 } 2781 2782 static void amt_gw_rcv(struct amt_dev *amt, struct sk_buff *skb) 2783 { 2784 int type = amt_parse_type(skb); 2785 int err = 1; 2786 2787 if (type == -1) 2788 goto drop; 2789 2790 if (amt->mode == AMT_MODE_GATEWAY) { 2791 switch (type) { 2792 case AMT_MSG_ADVERTISEMENT: 2793 err = amt_advertisement_handler(amt, skb); 2794 break; 2795 case AMT_MSG_MEMBERSHIP_QUERY: 2796 err = amt_membership_query_handler(amt, skb); 2797 if (!err) 2798 return; 2799 break; 2800 default: 2801 netdev_dbg(amt->dev, "Invalid type of Gateway\n"); 2802 break; 2803 } 2804 } 2805 drop: 2806 if (err) { 2807 amt->dev->stats.rx_dropped++; 2808 kfree_skb(skb); 2809 } else { 2810 consume_skb(skb); 2811 } 2812 } 2813 2814 static int amt_rcv(struct sock *sk, struct sk_buff *skb) 2815 { 2816 struct amt_dev *amt; 2817 __be32 remote_ip; 2818 __be32 saddr; 2819 int type; 2820 bool err; 2821 2822 rcu_read_lock_bh(); 2823 amt = rcu_dereference_sk_user_data(sk); 2824 if (!amt) { 2825 err = true; 2826 kfree_skb(skb); 2827 goto out; 2828 } 2829 remote_ip = READ_ONCE(amt->remote_ip); 2830 2831 skb->dev = amt->dev; 2832 saddr = ip_hdr(skb)->saddr; 2833 type = amt_parse_type(skb); 2834 if (type == -1) { 2835 err = true; 2836 goto drop; 2837 } 2838 2839 if (amt->mode == AMT_MODE_GATEWAY) { 2840 switch (type) { 2841 case AMT_MSG_ADVERTISEMENT: 2842 if (saddr != amt->discovery_ip) { 2843 netdev_dbg(amt->dev, "Invalid Relay IP\n"); 2844 err = true; 2845 goto drop; 2846 } 2847 if (amt_queue_event(amt, AMT_EVENT_RECEIVE, skb)) { 2848 netdev_dbg(amt->dev, "AMT Event queue full\n"); 2849 err = true; 2850 goto drop; 2851 } 2852 goto out; 2853 case AMT_MSG_MULTICAST_DATA: 2854 if (saddr != remote_ip) { 2855 netdev_dbg(amt->dev, "Invalid Relay IP\n"); 2856 err = true; 2857 goto drop; 2858 } 2859 err = amt_multicast_data_handler(amt, skb); 2860 if (err) 2861 goto drop; 2862 else 2863 goto out; 2864 case AMT_MSG_MEMBERSHIP_QUERY: 2865 if (saddr != remote_ip) { 2866 netdev_dbg(amt->dev, "Invalid Relay IP\n"); 2867 err = true; 2868 goto drop; 2869 } 2870 if (amt_queue_event(amt, AMT_EVENT_RECEIVE, skb)) { 2871 netdev_dbg(amt->dev, "AMT Event queue full\n"); 2872 err = true; 2873 goto drop; 2874 } 2875 goto out; 2876 default: 2877 err = true; 2878 netdev_dbg(amt->dev, "Invalid type of Gateway\n"); 2879 break; 2880 } 2881 } else { 2882 switch (type) { 2883 case AMT_MSG_DISCOVERY: 2884 err = amt_discovery_handler(amt, skb); 2885 break; 2886 case AMT_MSG_REQUEST: 2887 err = amt_request_handler(amt, skb); 2888 break; 2889 case AMT_MSG_MEMBERSHIP_UPDATE: 2890 err = amt_update_handler(amt, skb); 2891 if (err) 2892 goto drop; 2893 else 2894 goto out; 2895 default: 2896 err = true; 2897 netdev_dbg(amt->dev, "Invalid type of relay\n"); 2898 break; 2899 } 2900 } 2901 drop: 2902 if (err) { 2903 amt->dev->stats.rx_dropped++; 2904 kfree_skb(skb); 2905 } else { 2906 consume_skb(skb); 2907 } 2908 out: 2909 rcu_read_unlock_bh(); 2910 return 0; 2911 } 2912 2913 static void amt_event_work(struct work_struct *work) 2914 { 2915 struct amt_dev *amt = container_of(work, struct amt_dev, event_wq); 2916 struct sk_buff *skb; 2917 u8 event; 2918 int i; 2919 2920 for (i = 0; i < AMT_MAX_EVENTS; i++) { 2921 spin_lock_bh(&amt->lock); 2922 if (amt->nr_events == 0) { 2923 spin_unlock_bh(&amt->lock); 2924 return; 2925 } 2926 event = amt->events[amt->event_idx].event; 2927 skb = amt->events[amt->event_idx].skb; 2928 amt->events[amt->event_idx].event = AMT_EVENT_NONE; 2929 amt->events[amt->event_idx].skb = NULL; 2930 amt->nr_events--; 2931 amt->event_idx++; 2932 amt->event_idx %= AMT_MAX_EVENTS; 2933 spin_unlock_bh(&amt->lock); 2934 2935 switch (event) { 2936 case AMT_EVENT_RECEIVE: 2937 amt_gw_rcv(amt, skb); 2938 break; 2939 case AMT_EVENT_SEND_DISCOVERY: 2940 amt_event_send_discovery(amt); 2941 break; 2942 case AMT_EVENT_SEND_REQUEST: 2943 amt_event_send_request(amt); 2944 break; 2945 default: 2946 kfree_skb(skb); 2947 break; 2948 } 2949 } 2950 } 2951 2952 static int amt_err_lookup(struct sock *sk, struct sk_buff *skb) 2953 { 2954 struct amt_dev *amt; 2955 int type; 2956 2957 rcu_read_lock_bh(); 2958 amt = rcu_dereference_sk_user_data(sk); 2959 if (!amt) 2960 goto out; 2961 2962 if (amt->mode != AMT_MODE_GATEWAY) 2963 goto drop; 2964 2965 type = amt_parse_type(skb); 2966 if (type == -1) 2967 goto drop; 2968 2969 netdev_dbg(amt->dev, "Received IGMP Unreachable of %s\n", 2970 type_str[type]); 2971 switch (type) { 2972 case AMT_MSG_DISCOVERY: 2973 break; 2974 case AMT_MSG_REQUEST: 2975 case AMT_MSG_MEMBERSHIP_UPDATE: 2976 if (READ_ONCE(amt->status) >= AMT_STATUS_RECEIVED_ADVERTISEMENT) 2977 mod_delayed_work(amt_wq, &amt->req_wq, 0); 2978 break; 2979 default: 2980 goto drop; 2981 } 2982 out: 2983 rcu_read_unlock_bh(); 2984 return 0; 2985 drop: 2986 rcu_read_unlock_bh(); 2987 amt->dev->stats.rx_dropped++; 2988 return 0; 2989 } 2990 2991 static struct sock *amt_create_sock(struct net *net, __be16 port) 2992 { 2993 struct udp_port_cfg udp_conf; 2994 struct socket *sock; 2995 int err; 2996 2997 memset(&udp_conf, 0, sizeof(udp_conf)); 2998 udp_conf.family = AF_INET; 2999 udp_conf.local_ip.s_addr = htonl(INADDR_ANY); 3000 3001 udp_conf.local_udp_port = port; 3002 3003 err = udp_sock_create(net, &udp_conf, &sock); 3004 if (err < 0) 3005 return ERR_PTR(err); 3006 3007 return sock->sk; 3008 } 3009 3010 static int amt_socket_create(struct amt_dev *amt) 3011 { 3012 struct udp_tunnel_sock_cfg tunnel_cfg; 3013 struct sock *sk; 3014 3015 sk = amt_create_sock(amt->net, amt->relay_port); 3016 if (IS_ERR(sk)) 3017 return PTR_ERR(sk); 3018 3019 /* Mark socket as an encapsulation socket */ 3020 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg)); 3021 tunnel_cfg.sk_user_data = amt; 3022 tunnel_cfg.encap_type = 1; 3023 tunnel_cfg.encap_rcv = amt_rcv; 3024 tunnel_cfg.encap_err_lookup = amt_err_lookup; 3025 tunnel_cfg.encap_destroy = NULL; 3026 setup_udp_tunnel_sock(amt->net, sk, &tunnel_cfg); 3027 3028 rcu_assign_pointer(amt->sk, sk); 3029 return 0; 3030 } 3031 3032 static int amt_dev_open(struct net_device *dev) 3033 { 3034 struct amt_dev *amt = netdev_priv(dev); 3035 int err; 3036 3037 amt->ready4 = false; 3038 amt->ready6 = false; 3039 amt->event_idx = 0; 3040 amt->nr_events = 0; 3041 3042 enable_delayed_work(&amt->discovery_wq); 3043 enable_delayed_work(&amt->req_wq); 3044 3045 err = amt_socket_create(amt); 3046 if (err) { 3047 disable_delayed_work(&amt->req_wq); 3048 disable_delayed_work(&amt->discovery_wq); 3049 return err; 3050 } 3051 3052 amt->req_cnt = 0; 3053 WRITE_ONCE(amt->remote_ip, 0); 3054 amt->nonce = 0; 3055 get_random_bytes(&amt->key, sizeof(siphash_key_t)); 3056 3057 amt->status = AMT_STATUS_INIT; 3058 if (amt->mode == AMT_MODE_GATEWAY) { 3059 mod_delayed_work(amt_wq, &amt->discovery_wq, 0); 3060 mod_delayed_work(amt_wq, &amt->req_wq, 0); 3061 } else if (amt->mode == AMT_MODE_RELAY) { 3062 mod_delayed_work(amt_wq, &amt->secret_wq, 3063 msecs_to_jiffies(AMT_SECRET_TIMEOUT)); 3064 } 3065 return err; 3066 } 3067 3068 static int amt_dev_stop(struct net_device *dev) 3069 { 3070 struct amt_dev *amt = netdev_priv(dev); 3071 struct amt_tunnel_list *tunnel, *tmp; 3072 struct sk_buff *skb; 3073 struct sock *sk; 3074 int i; 3075 3076 disable_delayed_work_sync(&amt->req_wq); 3077 disable_delayed_work_sync(&amt->discovery_wq); 3078 cancel_delayed_work_sync(&amt->secret_wq); 3079 3080 /* shutdown */ 3081 sk = rtnl_dereference(amt->sk); 3082 RCU_INIT_POINTER(amt->sk, NULL); 3083 synchronize_net(); 3084 if (sk) 3085 udp_tunnel_sock_release(sk); 3086 3087 cancel_work_sync(&amt->event_wq); 3088 for (i = 0; i < AMT_MAX_EVENTS; i++) { 3089 skb = amt->events[i].skb; 3090 kfree_skb(skb); 3091 amt->events[i].event = AMT_EVENT_NONE; 3092 amt->events[i].skb = NULL; 3093 } 3094 3095 amt->ready4 = false; 3096 amt->ready6 = false; 3097 amt->req_cnt = 0; 3098 WRITE_ONCE(amt->remote_ip, 0); 3099 3100 list_for_each_entry_safe(tunnel, tmp, &amt->tunnel_list, list) { 3101 list_del_rcu(&tunnel->list); 3102 amt->nr_tunnels--; 3103 cancel_delayed_work_sync(&tunnel->gc_wq); 3104 amt_clear_groups(tunnel); 3105 kfree_rcu(tunnel, rcu); 3106 } 3107 3108 return 0; 3109 } 3110 3111 static const struct device_type amt_type = { 3112 .name = "amt", 3113 }; 3114 3115 static int amt_dev_init(struct net_device *dev) 3116 { 3117 struct amt_dev *amt = netdev_priv(dev); 3118 int err; 3119 3120 amt->dev = dev; 3121 3122 err = gro_cells_init(&amt->gro_cells, dev); 3123 if (err) 3124 return err; 3125 3126 return 0; 3127 } 3128 3129 static void amt_dev_uninit(struct net_device *dev) 3130 { 3131 struct amt_dev *amt = netdev_priv(dev); 3132 3133 gro_cells_destroy(&amt->gro_cells); 3134 } 3135 3136 static const struct net_device_ops amt_netdev_ops = { 3137 .ndo_init = amt_dev_init, 3138 .ndo_uninit = amt_dev_uninit, 3139 .ndo_open = amt_dev_open, 3140 .ndo_stop = amt_dev_stop, 3141 .ndo_start_xmit = amt_dev_xmit, 3142 }; 3143 3144 static void amt_link_setup(struct net_device *dev) 3145 { 3146 dev->netdev_ops = &amt_netdev_ops; 3147 dev->needs_free_netdev = true; 3148 SET_NETDEV_DEVTYPE(dev, &amt_type); 3149 dev->min_mtu = ETH_MIN_MTU; 3150 dev->max_mtu = ETH_MAX_MTU; 3151 dev->type = ARPHRD_NONE; 3152 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; 3153 dev->hard_header_len = 0; 3154 dev->addr_len = 0; 3155 dev->priv_flags |= IFF_NO_QUEUE; 3156 dev->lltx = true; 3157 dev->netns_immutable = true; 3158 dev->features |= NETIF_F_GSO_SOFTWARE; 3159 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM; 3160 dev->hw_features |= NETIF_F_FRAGLIST | NETIF_F_RXCSUM; 3161 dev->hw_features |= NETIF_F_GSO_SOFTWARE; 3162 dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS; 3163 eth_hw_addr_random(dev); 3164 eth_zero_addr(dev->broadcast); 3165 ether_setup(dev); 3166 } 3167 3168 static const struct nla_policy amt_policy[IFLA_AMT_MAX + 1] = { 3169 [IFLA_AMT_MODE] = { .type = NLA_U32 }, 3170 [IFLA_AMT_RELAY_PORT] = { .type = NLA_U16 }, 3171 [IFLA_AMT_GATEWAY_PORT] = { .type = NLA_U16 }, 3172 [IFLA_AMT_LINK] = { .type = NLA_U32 }, 3173 [IFLA_AMT_LOCAL_IP] = { .len = sizeof_field(struct iphdr, daddr) }, 3174 [IFLA_AMT_REMOTE_IP] = { .len = sizeof_field(struct iphdr, daddr) }, 3175 [IFLA_AMT_DISCOVERY_IP] = { .len = sizeof_field(struct iphdr, daddr) }, 3176 [IFLA_AMT_MAX_TUNNELS] = { .type = NLA_U32 }, 3177 }; 3178 3179 static int amt_validate(struct nlattr *tb[], struct nlattr *data[], 3180 struct netlink_ext_ack *extack) 3181 { 3182 if (!data) 3183 return -EINVAL; 3184 3185 if (!data[IFLA_AMT_LINK]) { 3186 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_LINK], 3187 "Link attribute is required"); 3188 return -EINVAL; 3189 } 3190 3191 if (!data[IFLA_AMT_MODE]) { 3192 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_MODE], 3193 "Mode attribute is required"); 3194 return -EINVAL; 3195 } 3196 3197 if (nla_get_u32(data[IFLA_AMT_MODE]) > AMT_MODE_MAX) { 3198 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_MODE], 3199 "Mode attribute is not valid"); 3200 return -EINVAL; 3201 } 3202 3203 if (!data[IFLA_AMT_LOCAL_IP]) { 3204 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_DISCOVERY_IP], 3205 "Local attribute is required"); 3206 return -EINVAL; 3207 } 3208 3209 if (!data[IFLA_AMT_DISCOVERY_IP] && 3210 nla_get_u32(data[IFLA_AMT_MODE]) == AMT_MODE_GATEWAY) { 3211 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_LOCAL_IP], 3212 "Discovery attribute is required"); 3213 return -EINVAL; 3214 } 3215 3216 return 0; 3217 } 3218 3219 static int amt_newlink(struct net_device *dev, 3220 struct rtnl_newlink_params *params, 3221 struct netlink_ext_ack *extack) 3222 { 3223 struct net *link_net = rtnl_newlink_link_net(params); 3224 struct amt_dev *amt = netdev_priv(dev); 3225 struct nlattr **data = params->data; 3226 struct nlattr **tb = params->tb; 3227 int err = -EINVAL; 3228 3229 if (!net_eq(link_net, dev_net(dev))) 3230 return err; 3231 3232 amt->net = link_net; 3233 amt->mode = nla_get_u32(data[IFLA_AMT_MODE]); 3234 3235 if (data[IFLA_AMT_MAX_TUNNELS] && 3236 nla_get_u32(data[IFLA_AMT_MAX_TUNNELS])) 3237 amt->max_tunnels = nla_get_u32(data[IFLA_AMT_MAX_TUNNELS]); 3238 else 3239 amt->max_tunnels = AMT_MAX_TUNNELS; 3240 3241 spin_lock_init(&amt->lock); 3242 amt->max_groups = AMT_MAX_GROUP; 3243 amt->max_sources = AMT_MAX_SOURCE; 3244 amt->hash_buckets = AMT_HSIZE; 3245 amt->nr_tunnels = 0; 3246 get_random_bytes(&amt->hash_seed, sizeof(amt->hash_seed)); 3247 amt->stream_dev = dev_get_by_index(link_net, 3248 nla_get_u32(data[IFLA_AMT_LINK])); 3249 if (!amt->stream_dev) { 3250 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_LINK], 3251 "Can't find stream device"); 3252 return -ENODEV; 3253 } 3254 3255 if (amt->stream_dev->type != ARPHRD_ETHER) { 3256 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_LINK], 3257 "Invalid stream device type"); 3258 goto err; 3259 } 3260 3261 amt->local_ip = nla_get_in_addr(data[IFLA_AMT_LOCAL_IP]); 3262 if (ipv4_is_loopback(amt->local_ip) || 3263 ipv4_is_zeronet(amt->local_ip) || 3264 ipv4_is_multicast(amt->local_ip)) { 3265 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_LOCAL_IP], 3266 "Invalid Local address"); 3267 goto err; 3268 } 3269 3270 amt->relay_port = nla_get_be16_default(data[IFLA_AMT_RELAY_PORT], 3271 htons(IANA_AMT_UDP_PORT)); 3272 3273 amt->gw_port = nla_get_be16_default(data[IFLA_AMT_GATEWAY_PORT], 3274 htons(IANA_AMT_UDP_PORT)); 3275 3276 if (!amt->relay_port) { 3277 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_DISCOVERY_IP], 3278 "relay port must not be 0"); 3279 goto err; 3280 } 3281 if (amt->mode == AMT_MODE_RELAY) { 3282 amt->qrv = READ_ONCE(amt->net->ipv4.sysctl_igmp_qrv); 3283 amt->qri = 10; 3284 dev->needed_headroom = amt->stream_dev->needed_headroom + 3285 AMT_RELAY_HLEN; 3286 dev->mtu = amt->stream_dev->mtu - AMT_RELAY_HLEN; 3287 dev->max_mtu = dev->mtu; 3288 dev->min_mtu = ETH_MIN_MTU + AMT_RELAY_HLEN; 3289 } else { 3290 if (!data[IFLA_AMT_DISCOVERY_IP]) { 3291 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_DISCOVERY_IP], 3292 "discovery must be set in gateway mode"); 3293 goto err; 3294 } 3295 if (!amt->gw_port) { 3296 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_DISCOVERY_IP], 3297 "gateway port must not be 0"); 3298 goto err; 3299 } 3300 WRITE_ONCE(amt->remote_ip, 0); 3301 amt->discovery_ip = nla_get_in_addr(data[IFLA_AMT_DISCOVERY_IP]); 3302 if (ipv4_is_loopback(amt->discovery_ip) || 3303 ipv4_is_zeronet(amt->discovery_ip) || 3304 ipv4_is_multicast(amt->discovery_ip)) { 3305 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_DISCOVERY_IP], 3306 "discovery must be unicast"); 3307 goto err; 3308 } 3309 3310 dev->needed_headroom = amt->stream_dev->needed_headroom + 3311 AMT_GW_HLEN; 3312 dev->mtu = amt->stream_dev->mtu - AMT_GW_HLEN; 3313 dev->max_mtu = dev->mtu; 3314 dev->min_mtu = ETH_MIN_MTU + AMT_GW_HLEN; 3315 } 3316 amt->qi = AMT_INIT_QUERY_INTERVAL; 3317 3318 err = register_netdevice(dev); 3319 if (err < 0) { 3320 netdev_dbg(dev, "failed to register new netdev %d\n", err); 3321 goto err; 3322 } 3323 3324 err = netdev_upper_dev_link(amt->stream_dev, dev, extack); 3325 if (err < 0) { 3326 unregister_netdevice(dev); 3327 goto err; 3328 } 3329 3330 INIT_DELAYED_WORK(&amt->discovery_wq, amt_discovery_work); 3331 INIT_DELAYED_WORK(&amt->req_wq, amt_req_work); 3332 INIT_DELAYED_WORK(&amt->secret_wq, amt_secret_work); 3333 INIT_WORK(&amt->event_wq, amt_event_work); 3334 disable_delayed_work(&amt->req_wq); 3335 disable_delayed_work(&amt->discovery_wq); 3336 INIT_LIST_HEAD(&amt->tunnel_list); 3337 return 0; 3338 err: 3339 dev_put(amt->stream_dev); 3340 return err; 3341 } 3342 3343 static void amt_dellink(struct net_device *dev, struct list_head *head) 3344 { 3345 struct amt_dev *amt = netdev_priv(dev); 3346 3347 unregister_netdevice_queue(dev, head); 3348 netdev_upper_dev_unlink(amt->stream_dev, dev); 3349 dev_put(amt->stream_dev); 3350 } 3351 3352 static size_t amt_get_size(const struct net_device *dev) 3353 { 3354 return nla_total_size(sizeof(__u32)) + /* IFLA_AMT_MODE */ 3355 nla_total_size(sizeof(__u16)) + /* IFLA_AMT_RELAY_PORT */ 3356 nla_total_size(sizeof(__u16)) + /* IFLA_AMT_GATEWAY_PORT */ 3357 nla_total_size(sizeof(__u32)) + /* IFLA_AMT_LINK */ 3358 nla_total_size(sizeof(__u32)) + /* IFLA_MAX_TUNNELS */ 3359 nla_total_size(sizeof(__be32)) + /* IFLA_AMT_DISCOVERY_IP */ 3360 nla_total_size(sizeof(__be32)) + /* IFLA_AMT_REMOTE_IP */ 3361 nla_total_size(sizeof(__be32)); /* IFLA_AMT_LOCAL_IP */ 3362 } 3363 3364 static int amt_fill_info(struct sk_buff *skb, const struct net_device *dev) 3365 { 3366 const struct amt_dev *amt = netdev_priv(dev); 3367 __be32 remote_ip; 3368 3369 rcu_read_lock(); 3370 if (nla_put_u32(skb, IFLA_AMT_MODE, amt->mode)) 3371 goto nla_put_failure; 3372 if (nla_put_be16(skb, IFLA_AMT_RELAY_PORT, amt->relay_port)) 3373 goto nla_put_failure; 3374 if (nla_put_be16(skb, IFLA_AMT_GATEWAY_PORT, amt->gw_port)) 3375 goto nla_put_failure; 3376 if (nla_put_u32(skb, IFLA_AMT_LINK, amt->stream_dev->ifindex)) 3377 goto nla_put_failure; 3378 if (nla_put_in_addr(skb, IFLA_AMT_LOCAL_IP, amt->local_ip)) 3379 goto nla_put_failure; 3380 if (nla_put_in_addr(skb, IFLA_AMT_DISCOVERY_IP, amt->discovery_ip)) 3381 goto nla_put_failure; 3382 3383 remote_ip = READ_ONCE(amt->remote_ip); 3384 if (remote_ip) 3385 if (nla_put_in_addr(skb, IFLA_AMT_REMOTE_IP, remote_ip)) 3386 goto nla_put_failure; 3387 if (nla_put_u32(skb, IFLA_AMT_MAX_TUNNELS, amt->max_tunnels)) 3388 goto nla_put_failure; 3389 3390 rcu_read_unlock(); 3391 return 0; 3392 3393 nla_put_failure: 3394 rcu_read_unlock(); 3395 return -EMSGSIZE; 3396 } 3397 3398 static struct rtnl_link_ops amt_link_ops __read_mostly = { 3399 .kind = "amt", 3400 .maxtype = IFLA_AMT_MAX, 3401 .policy = amt_policy, 3402 .priv_size = sizeof(struct amt_dev), 3403 .setup = amt_link_setup, 3404 .validate = amt_validate, 3405 .newlink = amt_newlink, 3406 .dellink = amt_dellink, 3407 .get_size = amt_get_size, 3408 .fill_info = amt_fill_info, 3409 }; 3410 3411 static struct net_device *amt_lookup_upper_dev(struct net_device *dev) 3412 { 3413 struct net_device *upper_dev; 3414 struct amt_dev *amt; 3415 3416 for_each_netdev(dev_net(dev), upper_dev) { 3417 if (netif_is_amt(upper_dev)) { 3418 amt = netdev_priv(upper_dev); 3419 if (amt->stream_dev == dev) 3420 return upper_dev; 3421 } 3422 } 3423 3424 return NULL; 3425 } 3426 3427 static int amt_device_event(struct notifier_block *unused, 3428 unsigned long event, void *ptr) 3429 { 3430 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 3431 struct net_device *upper_dev; 3432 struct amt_dev *amt; 3433 LIST_HEAD(list); 3434 int new_mtu; 3435 3436 upper_dev = amt_lookup_upper_dev(dev); 3437 if (!upper_dev) 3438 return NOTIFY_DONE; 3439 amt = netdev_priv(upper_dev); 3440 3441 switch (event) { 3442 case NETDEV_UNREGISTER: 3443 amt_dellink(amt->dev, &list); 3444 unregister_netdevice_many(&list); 3445 break; 3446 case NETDEV_CHANGEMTU: 3447 if (amt->mode == AMT_MODE_RELAY) 3448 new_mtu = dev->mtu - AMT_RELAY_HLEN; 3449 else 3450 new_mtu = dev->mtu - AMT_GW_HLEN; 3451 3452 dev_set_mtu(amt->dev, new_mtu); 3453 break; 3454 } 3455 3456 return NOTIFY_DONE; 3457 } 3458 3459 static struct notifier_block amt_notifier_block __read_mostly = { 3460 .notifier_call = amt_device_event, 3461 }; 3462 3463 static int __init amt_init(void) 3464 { 3465 int err; 3466 3467 err = register_netdevice_notifier(&amt_notifier_block); 3468 if (err < 0) 3469 goto err; 3470 3471 err = rtnl_link_register(&amt_link_ops); 3472 if (err < 0) 3473 goto unregister_notifier; 3474 3475 amt_wq = alloc_workqueue("amt", WQ_UNBOUND, 0); 3476 if (!amt_wq) { 3477 err = -ENOMEM; 3478 goto rtnl_unregister; 3479 } 3480 3481 spin_lock_init(&source_gc_lock); 3482 spin_lock_bh(&source_gc_lock); 3483 INIT_DELAYED_WORK(&source_gc_wq, amt_source_gc_work); 3484 mod_delayed_work(amt_wq, &source_gc_wq, 3485 msecs_to_jiffies(AMT_GC_INTERVAL)); 3486 spin_unlock_bh(&source_gc_lock); 3487 3488 return 0; 3489 3490 rtnl_unregister: 3491 rtnl_link_unregister(&amt_link_ops); 3492 unregister_notifier: 3493 unregister_netdevice_notifier(&amt_notifier_block); 3494 err: 3495 pr_err("error loading AMT module loaded\n"); 3496 return err; 3497 } 3498 late_initcall(amt_init); 3499 3500 static void __exit amt_fini(void) 3501 { 3502 rtnl_link_unregister(&amt_link_ops); 3503 unregister_netdevice_notifier(&amt_notifier_block); 3504 cancel_delayed_work_sync(&source_gc_wq); 3505 __amt_source_gc_work(); 3506 destroy_workqueue(amt_wq); 3507 } 3508 module_exit(amt_fini); 3509 3510 MODULE_LICENSE("GPL"); 3511 MODULE_DESCRIPTION("Driver for Automatic Multicast Tunneling (AMT)"); 3512 MODULE_AUTHOR("Taehee Yoo <ap420073@gmail.com>"); 3513 MODULE_ALIAS_RTNL_LINK("amt"); 3514