1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Copyright (c) 2021 Taehee Yoo <ap420073@gmail.com> */
4 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6 #include <linux/module.h>
7 #include <linux/skbuff.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/sch_generic.h>
15 #include <net/net_namespace.h>
18 #include <net/udp_tunnel.h>
22 #include <uapi/linux/amt.h>
23 #include <linux/security.h>
24 #include <net/gro_cells.h>
26 #include <net/if_inet6.h>
27 #include <net/ndisc.h>
28 #include <net/addrconf.h>
29 #include <net/ip6_route.h>
30 #include <net/inet_common.h>
31 #include <net/ip6_checksum.h>
33 static struct workqueue_struct *amt_wq;
35 static HLIST_HEAD(source_gc_list);
36 /* Lock for source_gc_list */
37 static spinlock_t source_gc_lock;
38 static struct delayed_work source_gc_wq;
39 static char *status_str[] = {
41 "AMT_STATUS_SENT_DISCOVERY",
42 "AMT_STATUS_RECEIVED_DISCOVERY",
43 "AMT_STATUS_SENT_ADVERTISEMENT",
44 "AMT_STATUS_RECEIVED_ADVERTISEMENT",
45 "AMT_STATUS_SENT_REQUEST",
46 "AMT_STATUS_RECEIVED_REQUEST",
47 "AMT_STATUS_SENT_QUERY",
48 "AMT_STATUS_RECEIVED_QUERY",
49 "AMT_STATUS_SENT_UPDATE",
50 "AMT_STATUS_RECEIVED_UPDATE",
53 static char *type_str[] = {
54 "", /* Type 0 is not defined */
56 "AMT_MSG_ADVERTISEMENT",
58 "AMT_MSG_MEMBERSHIP_QUERY",
59 "AMT_MSG_MEMBERSHIP_UPDATE",
60 "AMT_MSG_MULTICAST_DATA",
64 static char *action_str[] = {
68 "AMT_ACT_STATUS_FWD_NEW",
69 "AMT_ACT_STATUS_D_FWD_NEW",
70 "AMT_ACT_STATUS_NONE_NEW",
73 static struct igmpv3_grec igmpv3_zero_grec;
75 #if IS_ENABLED(CONFIG_IPV6)
76 #define MLD2_ALL_NODE_INIT { { { 0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01 } } }
77 static struct in6_addr mld2_all_node = MLD2_ALL_NODE_INIT;
78 static struct mld2_grec mldv2_zero_grec;
81 static struct amt_skb_cb *amt_skb_cb(struct sk_buff *skb)
83 BUILD_BUG_ON(sizeof(struct amt_skb_cb) + sizeof(struct qdisc_skb_cb) >
84 sizeof_field(struct sk_buff, cb));
86 return (struct amt_skb_cb *)((void *)skb->cb +
87 sizeof(struct qdisc_skb_cb));
90 static void __amt_source_gc_work(void)
92 struct amt_source_node *snode;
93 struct hlist_head gc_list;
96 spin_lock_bh(&source_gc_lock);
97 hlist_move_list(&source_gc_list, &gc_list);
98 spin_unlock_bh(&source_gc_lock);
100 hlist_for_each_entry_safe(snode, t, &gc_list, node) {
101 hlist_del_rcu(&snode->node);
102 kfree_rcu(snode, rcu);
106 static void amt_source_gc_work(struct work_struct *work)
108 __amt_source_gc_work();
110 spin_lock_bh(&source_gc_lock);
111 mod_delayed_work(amt_wq, &source_gc_wq,
112 msecs_to_jiffies(AMT_GC_INTERVAL));
113 spin_unlock_bh(&source_gc_lock);
116 static bool amt_addr_equal(union amt_addr *a, union amt_addr *b)
118 return !memcmp(a, b, sizeof(union amt_addr));
121 static u32 amt_source_hash(struct amt_tunnel_list *tunnel, union amt_addr *src)
123 u32 hash = jhash(src, sizeof(*src), tunnel->amt->hash_seed);
125 return reciprocal_scale(hash, tunnel->amt->hash_buckets);
128 static bool amt_status_filter(struct amt_source_node *snode,
129 enum amt_filter filter)
135 if (snode->status == AMT_SOURCE_STATUS_FWD &&
136 snode->flags == AMT_SOURCE_OLD)
139 case AMT_FILTER_D_FWD:
140 if (snode->status == AMT_SOURCE_STATUS_D_FWD &&
141 snode->flags == AMT_SOURCE_OLD)
144 case AMT_FILTER_FWD_NEW:
145 if (snode->status == AMT_SOURCE_STATUS_FWD &&
146 snode->flags == AMT_SOURCE_NEW)
149 case AMT_FILTER_D_FWD_NEW:
150 if (snode->status == AMT_SOURCE_STATUS_D_FWD &&
151 snode->flags == AMT_SOURCE_NEW)
157 case AMT_FILTER_NONE_NEW:
158 if (snode->status == AMT_SOURCE_STATUS_NONE &&
159 snode->flags == AMT_SOURCE_NEW)
162 case AMT_FILTER_BOTH:
163 if ((snode->status == AMT_SOURCE_STATUS_D_FWD ||
164 snode->status == AMT_SOURCE_STATUS_FWD) &&
165 snode->flags == AMT_SOURCE_OLD)
168 case AMT_FILTER_BOTH_NEW:
169 if ((snode->status == AMT_SOURCE_STATUS_D_FWD ||
170 snode->status == AMT_SOURCE_STATUS_FWD) &&
171 snode->flags == AMT_SOURCE_NEW)
182 static struct amt_source_node *amt_lookup_src(struct amt_tunnel_list *tunnel,
183 struct amt_group_node *gnode,
184 enum amt_filter filter,
187 u32 hash = amt_source_hash(tunnel, src);
188 struct amt_source_node *snode;
190 hlist_for_each_entry_rcu(snode, &gnode->sources[hash], node)
191 if (amt_status_filter(snode, filter) &&
192 amt_addr_equal(&snode->source_addr, src))
198 static u32 amt_group_hash(struct amt_tunnel_list *tunnel, union amt_addr *group)
200 u32 hash = jhash(group, sizeof(*group), tunnel->amt->hash_seed);
202 return reciprocal_scale(hash, tunnel->amt->hash_buckets);
205 static struct amt_group_node *amt_lookup_group(struct amt_tunnel_list *tunnel,
206 union amt_addr *group,
207 union amt_addr *host,
210 u32 hash = amt_group_hash(tunnel, group);
211 struct amt_group_node *gnode;
213 hlist_for_each_entry_rcu(gnode, &tunnel->groups[hash], node) {
214 if (amt_addr_equal(&gnode->group_addr, group) &&
215 amt_addr_equal(&gnode->host_addr, host) &&
223 static void amt_destroy_source(struct amt_source_node *snode)
225 struct amt_group_node *gnode = snode->gnode;
226 struct amt_tunnel_list *tunnel;
228 tunnel = gnode->tunnel_list;
231 netdev_dbg(snode->gnode->amt->dev,
232 "Delete source %pI4 from %pI4\n",
233 &snode->source_addr.ip4,
234 &gnode->group_addr.ip4);
235 #if IS_ENABLED(CONFIG_IPV6)
237 netdev_dbg(snode->gnode->amt->dev,
238 "Delete source %pI6 from %pI6\n",
239 &snode->source_addr.ip6,
240 &gnode->group_addr.ip6);
244 cancel_delayed_work(&snode->source_timer);
245 hlist_del_init_rcu(&snode->node);
246 tunnel->nr_sources--;
248 spin_lock_bh(&source_gc_lock);
249 hlist_add_head_rcu(&snode->node, &source_gc_list);
250 spin_unlock_bh(&source_gc_lock);
253 static void amt_del_group(struct amt_dev *amt, struct amt_group_node *gnode)
255 struct amt_source_node *snode;
256 struct hlist_node *t;
259 if (cancel_delayed_work(&gnode->group_timer))
261 hlist_del_rcu(&gnode->node);
262 gnode->tunnel_list->nr_groups--;
265 netdev_dbg(amt->dev, "Leave group %pI4\n",
266 &gnode->group_addr.ip4);
267 #if IS_ENABLED(CONFIG_IPV6)
269 netdev_dbg(amt->dev, "Leave group %pI6\n",
270 &gnode->group_addr.ip6);
272 for (i = 0; i < amt->hash_buckets; i++)
273 hlist_for_each_entry_safe(snode, t, &gnode->sources[i], node)
274 amt_destroy_source(snode);
276 /* tunnel->lock was acquired outside of amt_del_group()
277 * But rcu_read_lock() was acquired too so It's safe.
279 kfree_rcu(gnode, rcu);
282 /* If a source timer expires with a router filter-mode for the group of
283 * INCLUDE, the router concludes that traffic from this particular
284 * source is no longer desired on the attached network, and deletes the
285 * associated source record.
287 static void amt_source_work(struct work_struct *work)
289 struct amt_source_node *snode = container_of(to_delayed_work(work),
290 struct amt_source_node,
292 struct amt_group_node *gnode = snode->gnode;
293 struct amt_dev *amt = gnode->amt;
294 struct amt_tunnel_list *tunnel;
296 tunnel = gnode->tunnel_list;
297 spin_lock_bh(&tunnel->lock);
299 if (gnode->filter_mode == MCAST_INCLUDE) {
300 amt_destroy_source(snode);
301 if (!gnode->nr_sources)
302 amt_del_group(amt, gnode);
304 /* When a router filter-mode for a group is EXCLUDE,
305 * source records are only deleted when the group timer expires
307 snode->status = AMT_SOURCE_STATUS_D_FWD;
310 spin_unlock_bh(&tunnel->lock);
313 static void amt_act_src(struct amt_tunnel_list *tunnel,
314 struct amt_group_node *gnode,
315 struct amt_source_node *snode,
318 struct amt_dev *amt = tunnel->amt;
322 mod_delayed_work(amt_wq, &snode->source_timer,
323 msecs_to_jiffies(amt_gmi(amt)));
325 case AMT_ACT_GMI_ZERO:
326 cancel_delayed_work(&snode->source_timer);
329 mod_delayed_work(amt_wq, &snode->source_timer,
330 gnode->group_timer.timer.expires);
332 case AMT_ACT_STATUS_FWD_NEW:
333 snode->status = AMT_SOURCE_STATUS_FWD;
334 snode->flags = AMT_SOURCE_NEW;
336 case AMT_ACT_STATUS_D_FWD_NEW:
337 snode->status = AMT_SOURCE_STATUS_D_FWD;
338 snode->flags = AMT_SOURCE_NEW;
340 case AMT_ACT_STATUS_NONE_NEW:
341 cancel_delayed_work(&snode->source_timer);
342 snode->status = AMT_SOURCE_STATUS_NONE;
343 snode->flags = AMT_SOURCE_NEW;
351 netdev_dbg(amt->dev, "Source %pI4 from %pI4 Acted %s\n",
352 &snode->source_addr.ip4,
353 &gnode->group_addr.ip4,
355 #if IS_ENABLED(CONFIG_IPV6)
357 netdev_dbg(amt->dev, "Source %pI6 from %pI6 Acted %s\n",
358 &snode->source_addr.ip6,
359 &gnode->group_addr.ip6,
364 static struct amt_source_node *amt_alloc_snode(struct amt_group_node *gnode,
367 struct amt_source_node *snode;
369 snode = kzalloc(sizeof(*snode), GFP_ATOMIC);
373 memcpy(&snode->source_addr, src, sizeof(union amt_addr));
374 snode->gnode = gnode;
375 snode->status = AMT_SOURCE_STATUS_NONE;
376 snode->flags = AMT_SOURCE_NEW;
377 INIT_HLIST_NODE(&snode->node);
378 INIT_DELAYED_WORK(&snode->source_timer, amt_source_work);
383 /* RFC 3810 - 7.2.2. Definition of Filter Timers
385 * Router Mode Filter Timer Actions/Comments
386 * ----------- ----------------- ----------------
388 * INCLUDE Not Used All listeners in
391 * EXCLUDE Timer > 0 At least one listener
394 * EXCLUDE Timer == 0 No more listeners in
395 * EXCLUDE mode for the
397 * If the Requested List
400 * Record. If not, switch
401 * to INCLUDE filter mode;
404 * moved to the Include
405 * List, and the Exclude
408 static void amt_group_work(struct work_struct *work)
410 struct amt_group_node *gnode = container_of(to_delayed_work(work),
411 struct amt_group_node,
413 struct amt_tunnel_list *tunnel = gnode->tunnel_list;
414 struct amt_dev *amt = gnode->amt;
415 struct amt_source_node *snode;
416 bool delete_group = true;
417 struct hlist_node *t;
420 buckets = amt->hash_buckets;
422 spin_lock_bh(&tunnel->lock);
423 if (gnode->filter_mode == MCAST_INCLUDE) {
425 spin_unlock_bh(&tunnel->lock);
430 for (i = 0; i < buckets; i++) {
431 hlist_for_each_entry_safe(snode, t,
432 &gnode->sources[i], node) {
433 if (!delayed_work_pending(&snode->source_timer) ||
434 snode->status == AMT_SOURCE_STATUS_D_FWD) {
435 amt_destroy_source(snode);
437 delete_group = false;
438 snode->status = AMT_SOURCE_STATUS_FWD;
443 amt_del_group(amt, gnode);
445 gnode->filter_mode = MCAST_INCLUDE;
447 spin_unlock_bh(&tunnel->lock);
452 /* Non-existant group is created as INCLUDE {empty}:
454 * RFC 3376 - 5.1. Action on Change of Interface State
456 * If no interface state existed for that multicast address before
457 * the change (i.e., the change consisted of creating a new
458 * per-interface record), or if no state exists after the change
459 * (i.e., the change consisted of deleting a per-interface record),
460 * then the "non-existent" state is considered to have a filter mode
461 * of INCLUDE and an empty source list.
463 static struct amt_group_node *amt_add_group(struct amt_dev *amt,
464 struct amt_tunnel_list *tunnel,
465 union amt_addr *group,
466 union amt_addr *host,
469 struct amt_group_node *gnode;
473 if (tunnel->nr_groups >= amt->max_groups)
474 return ERR_PTR(-ENOSPC);
476 gnode = kzalloc(sizeof(*gnode) +
477 (sizeof(struct hlist_head) * amt->hash_buckets),
479 if (unlikely(!gnode))
480 return ERR_PTR(-ENOMEM);
483 gnode->group_addr = *group;
484 gnode->host_addr = *host;
486 gnode->tunnel_list = tunnel;
487 gnode->filter_mode = MCAST_INCLUDE;
488 INIT_HLIST_NODE(&gnode->node);
489 INIT_DELAYED_WORK(&gnode->group_timer, amt_group_work);
490 for (i = 0; i < amt->hash_buckets; i++)
491 INIT_HLIST_HEAD(&gnode->sources[i]);
493 hash = amt_group_hash(tunnel, group);
494 hlist_add_head_rcu(&gnode->node, &tunnel->groups[hash]);
498 netdev_dbg(amt->dev, "Join group %pI4\n",
499 &gnode->group_addr.ip4);
500 #if IS_ENABLED(CONFIG_IPV6)
502 netdev_dbg(amt->dev, "Join group %pI6\n",
503 &gnode->group_addr.ip6);
509 static struct sk_buff *amt_build_igmp_gq(struct amt_dev *amt)
511 u8 ra[AMT_IPHDR_OPTS] = { IPOPT_RA, 4, 0, 0 };
512 int hlen = LL_RESERVED_SPACE(amt->dev);
513 int tlen = amt->dev->needed_tailroom;
514 struct igmpv3_query *ihv3;
515 void *csum_start = NULL;
516 __sum16 *csum = NULL;
523 len = hlen + tlen + sizeof(*iph) + AMT_IPHDR_OPTS + sizeof(*ihv3);
524 skb = netdev_alloc_skb_ip_align(amt->dev, len);
528 skb_reserve(skb, hlen);
529 skb_push(skb, sizeof(*eth));
530 skb->protocol = htons(ETH_P_IP);
531 skb_reset_mac_header(skb);
532 skb->priority = TC_PRIO_CONTROL;
533 skb_put(skb, sizeof(*iph));
534 skb_put_data(skb, ra, sizeof(ra));
535 skb_put(skb, sizeof(*ihv3));
536 skb_pull(skb, sizeof(*eth));
537 skb_reset_network_header(skb);
541 iph->ihl = (sizeof(struct iphdr) + AMT_IPHDR_OPTS) >> 2;
543 iph->tot_len = htons(sizeof(*iph) + AMT_IPHDR_OPTS + sizeof(*ihv3));
544 iph->frag_off = htons(IP_DF);
547 iph->protocol = IPPROTO_IGMP;
548 iph->daddr = htonl(INADDR_ALLHOSTS_GROUP);
549 iph->saddr = htonl(INADDR_ANY);
553 ether_addr_copy(eth->h_source, amt->dev->dev_addr);
554 ip_eth_mc_map(htonl(INADDR_ALLHOSTS_GROUP), eth->h_dest);
555 eth->h_proto = htons(ETH_P_IP);
557 ihv3 = skb_pull(skb, sizeof(*iph) + AMT_IPHDR_OPTS);
558 skb_reset_transport_header(skb);
559 ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY;
562 ihv3->qqic = amt->qi;
565 ihv3->suppress = false;
566 ihv3->qrv = READ_ONCE(amt->net->ipv4.sysctl_igmp_qrv);
569 csum_start = (void *)ihv3;
570 *csum = ip_compute_csum(csum_start, sizeof(*ihv3));
571 offset = skb_transport_offset(skb);
572 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
573 skb->ip_summed = CHECKSUM_NONE;
575 skb_push(skb, sizeof(*eth) + sizeof(*iph) + AMT_IPHDR_OPTS);
580 static void amt_update_gw_status(struct amt_dev *amt, enum amt_status status,
583 if (validate && amt->status >= status)
585 netdev_dbg(amt->dev, "Update GW status %s -> %s",
586 status_str[amt->status], status_str[status]);
587 WRITE_ONCE(amt->status, status);
590 static void __amt_update_relay_status(struct amt_tunnel_list *tunnel,
591 enum amt_status status,
594 if (validate && tunnel->status >= status)
596 netdev_dbg(tunnel->amt->dev,
597 "Update Tunnel(IP = %pI4, PORT = %u) status %s -> %s",
598 &tunnel->ip4, ntohs(tunnel->source_port),
599 status_str[tunnel->status], status_str[status]);
600 tunnel->status = status;
603 static void amt_update_relay_status(struct amt_tunnel_list *tunnel,
604 enum amt_status status, bool validate)
606 spin_lock_bh(&tunnel->lock);
607 __amt_update_relay_status(tunnel, status, validate);
608 spin_unlock_bh(&tunnel->lock);
611 static void amt_send_discovery(struct amt_dev *amt)
613 struct amt_header_discovery *amtd;
614 int hlen, tlen, offset;
625 sock = rcu_dereference(amt->sock);
629 if (!netif_running(amt->stream_dev) || !netif_running(amt->dev))
632 rt = ip_route_output_ports(amt->net, &fl4, sock->sk,
633 amt->discovery_ip, amt->local_ip,
634 amt->gw_port, amt->relay_port,
636 amt->stream_dev->ifindex);
638 amt->dev->stats.tx_errors++;
642 hlen = LL_RESERVED_SPACE(amt->dev);
643 tlen = amt->dev->needed_tailroom;
644 len = hlen + tlen + sizeof(*iph) + sizeof(*udph) + sizeof(*amtd);
645 skb = netdev_alloc_skb_ip_align(amt->dev, len);
648 amt->dev->stats.tx_errors++;
652 skb->priority = TC_PRIO_CONTROL;
653 skb_dst_set(skb, &rt->dst);
655 len = sizeof(*iph) + sizeof(*udph) + sizeof(*amtd);
656 skb_reset_network_header(skb);
658 amtd = skb_pull(skb, sizeof(*iph) + sizeof(*udph));
660 amtd->type = AMT_MSG_DISCOVERY;
662 amtd->nonce = amt->nonce;
663 skb_push(skb, sizeof(*udph));
664 skb_reset_transport_header(skb);
666 udph->source = amt->gw_port;
667 udph->dest = amt->relay_port;
668 udph->len = htons(sizeof(*udph) + sizeof(*amtd));
670 offset = skb_transport_offset(skb);
671 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
672 udph->check = csum_tcpudp_magic(amt->local_ip, amt->discovery_ip,
673 sizeof(*udph) + sizeof(*amtd),
674 IPPROTO_UDP, skb->csum);
676 skb_push(skb, sizeof(*iph));
679 iph->ihl = (sizeof(struct iphdr)) >> 2;
682 iph->ttl = ip4_dst_hoplimit(&rt->dst);
683 iph->daddr = amt->discovery_ip;
684 iph->saddr = amt->local_ip;
685 iph->protocol = IPPROTO_UDP;
686 iph->tot_len = htons(len);
688 skb->ip_summed = CHECKSUM_NONE;
689 ip_select_ident(amt->net, skb, NULL);
691 err = ip_local_out(amt->net, sock->sk, skb);
692 if (unlikely(net_xmit_eval(err)))
693 amt->dev->stats.tx_errors++;
695 amt_update_gw_status(amt, AMT_STATUS_SENT_DISCOVERY, true);
700 static void amt_send_request(struct amt_dev *amt, bool v6)
702 struct amt_header_request *amtrh;
703 int hlen, tlen, offset;
714 sock = rcu_dereference(amt->sock);
718 if (!netif_running(amt->stream_dev) || !netif_running(amt->dev))
721 rt = ip_route_output_ports(amt->net, &fl4, sock->sk,
722 amt->remote_ip, amt->local_ip,
723 amt->gw_port, amt->relay_port,
725 amt->stream_dev->ifindex);
727 amt->dev->stats.tx_errors++;
731 hlen = LL_RESERVED_SPACE(amt->dev);
732 tlen = amt->dev->needed_tailroom;
733 len = hlen + tlen + sizeof(*iph) + sizeof(*udph) + sizeof(*amtrh);
734 skb = netdev_alloc_skb_ip_align(amt->dev, len);
737 amt->dev->stats.tx_errors++;
741 skb->priority = TC_PRIO_CONTROL;
742 skb_dst_set(skb, &rt->dst);
744 len = sizeof(*iph) + sizeof(*udph) + sizeof(*amtrh);
745 skb_reset_network_header(skb);
747 amtrh = skb_pull(skb, sizeof(*iph) + sizeof(*udph));
749 amtrh->type = AMT_MSG_REQUEST;
750 amtrh->reserved1 = 0;
752 amtrh->reserved2 = 0;
753 amtrh->nonce = amt->nonce;
754 skb_push(skb, sizeof(*udph));
755 skb_reset_transport_header(skb);
757 udph->source = amt->gw_port;
758 udph->dest = amt->relay_port;
759 udph->len = htons(sizeof(*amtrh) + sizeof(*udph));
761 offset = skb_transport_offset(skb);
762 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
763 udph->check = csum_tcpudp_magic(amt->local_ip, amt->remote_ip,
764 sizeof(*udph) + sizeof(*amtrh),
765 IPPROTO_UDP, skb->csum);
767 skb_push(skb, sizeof(*iph));
770 iph->ihl = (sizeof(struct iphdr)) >> 2;
773 iph->ttl = ip4_dst_hoplimit(&rt->dst);
774 iph->daddr = amt->remote_ip;
775 iph->saddr = amt->local_ip;
776 iph->protocol = IPPROTO_UDP;
777 iph->tot_len = htons(len);
779 skb->ip_summed = CHECKSUM_NONE;
780 ip_select_ident(amt->net, skb, NULL);
782 err = ip_local_out(amt->net, sock->sk, skb);
783 if (unlikely(net_xmit_eval(err)))
784 amt->dev->stats.tx_errors++;
790 static void amt_send_igmp_gq(struct amt_dev *amt,
791 struct amt_tunnel_list *tunnel)
795 skb = amt_build_igmp_gq(amt);
799 amt_skb_cb(skb)->tunnel = tunnel;
803 #if IS_ENABLED(CONFIG_IPV6)
804 static struct sk_buff *amt_build_mld_gq(struct amt_dev *amt)
806 u8 ra[AMT_IP6HDR_OPTS] = { IPPROTO_ICMPV6, 0, IPV6_TLV_ROUTERALERT,
807 2, 0, 0, IPV6_TLV_PAD1, IPV6_TLV_PAD1 };
808 int hlen = LL_RESERVED_SPACE(amt->dev);
809 int tlen = amt->dev->needed_tailroom;
810 struct mld2_query *mld2q;
811 void *csum_start = NULL;
812 struct ipv6hdr *ip6h;
817 len = hlen + tlen + sizeof(*ip6h) + sizeof(ra) + sizeof(*mld2q);
818 skb = netdev_alloc_skb_ip_align(amt->dev, len);
822 skb_reserve(skb, hlen);
823 skb_push(skb, sizeof(*eth));
824 skb_reset_mac_header(skb);
826 skb->priority = TC_PRIO_CONTROL;
827 skb->protocol = htons(ETH_P_IPV6);
828 skb_put_zero(skb, sizeof(*ip6h));
829 skb_put_data(skb, ra, sizeof(ra));
830 skb_put_zero(skb, sizeof(*mld2q));
831 skb_pull(skb, sizeof(*eth));
832 skb_reset_network_header(skb);
833 ip6h = ipv6_hdr(skb);
834 ip6h->payload_len = htons(sizeof(ra) + sizeof(*mld2q));
835 ip6h->nexthdr = NEXTHDR_HOP;
837 ip6h->daddr = mld2_all_node;
838 ip6_flow_hdr(ip6h, 0, 0);
840 if (ipv6_dev_get_saddr(amt->net, amt->dev, &ip6h->daddr, 0,
842 amt->dev->stats.tx_errors++;
847 eth->h_proto = htons(ETH_P_IPV6);
848 ether_addr_copy(eth->h_source, amt->dev->dev_addr);
849 ipv6_eth_mc_map(&mld2_all_node, eth->h_dest);
851 skb_pull(skb, sizeof(*ip6h) + sizeof(ra));
852 skb_reset_transport_header(skb);
853 mld2q = (struct mld2_query *)icmp6_hdr(skb);
854 mld2q->mld2q_mrc = htons(1);
855 mld2q->mld2q_type = ICMPV6_MGM_QUERY;
856 mld2q->mld2q_code = 0;
857 mld2q->mld2q_cksum = 0;
858 mld2q->mld2q_resv1 = 0;
859 mld2q->mld2q_resv2 = 0;
860 mld2q->mld2q_suppress = 0;
861 mld2q->mld2q_qrv = amt->qrv;
862 mld2q->mld2q_nsrcs = 0;
863 mld2q->mld2q_qqic = amt->qi;
864 csum_start = (void *)mld2q;
865 mld2q->mld2q_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
868 csum_partial(csum_start,
871 skb->ip_summed = CHECKSUM_NONE;
872 skb_push(skb, sizeof(*eth) + sizeof(*ip6h) + sizeof(ra));
876 static void amt_send_mld_gq(struct amt_dev *amt, struct amt_tunnel_list *tunnel)
880 skb = amt_build_mld_gq(amt);
884 amt_skb_cb(skb)->tunnel = tunnel;
888 static void amt_send_mld_gq(struct amt_dev *amt, struct amt_tunnel_list *tunnel)
893 static bool amt_queue_event(struct amt_dev *amt, enum amt_event event,
898 spin_lock_bh(&amt->lock);
899 if (amt->nr_events >= AMT_MAX_EVENTS) {
900 spin_unlock_bh(&amt->lock);
904 index = (amt->event_idx + amt->nr_events) % AMT_MAX_EVENTS;
905 amt->events[index].event = event;
906 amt->events[index].skb = skb;
908 amt->event_idx %= AMT_MAX_EVENTS;
909 queue_work(amt_wq, &amt->event_wq);
910 spin_unlock_bh(&amt->lock);
915 static void amt_secret_work(struct work_struct *work)
917 struct amt_dev *amt = container_of(to_delayed_work(work),
921 spin_lock_bh(&amt->lock);
922 get_random_bytes(&amt->key, sizeof(siphash_key_t));
923 spin_unlock_bh(&amt->lock);
924 mod_delayed_work(amt_wq, &amt->secret_wq,
925 msecs_to_jiffies(AMT_SECRET_TIMEOUT));
928 static void amt_event_send_discovery(struct amt_dev *amt)
930 if (amt->status > AMT_STATUS_SENT_DISCOVERY)
932 get_random_bytes(&amt->nonce, sizeof(__be32));
934 amt_send_discovery(amt);
936 mod_delayed_work(amt_wq, &amt->discovery_wq,
937 msecs_to_jiffies(AMT_DISCOVERY_TIMEOUT));
940 static void amt_discovery_work(struct work_struct *work)
942 struct amt_dev *amt = container_of(to_delayed_work(work),
946 if (amt_queue_event(amt, AMT_EVENT_SEND_DISCOVERY, NULL))
947 mod_delayed_work(amt_wq, &amt->discovery_wq,
948 msecs_to_jiffies(AMT_DISCOVERY_TIMEOUT));
951 static void amt_event_send_request(struct amt_dev *amt)
955 if (amt->status < AMT_STATUS_RECEIVED_ADVERTISEMENT)
958 if (amt->req_cnt > AMT_MAX_REQ_COUNT) {
959 netdev_dbg(amt->dev, "Gateway is not ready");
960 amt->qi = AMT_INIT_REQ_TIMEOUT;
961 WRITE_ONCE(amt->ready4, false);
962 WRITE_ONCE(amt->ready6, false);
964 amt_update_gw_status(amt, AMT_STATUS_INIT, false);
971 get_random_bytes(&amt->nonce, sizeof(__be32));
973 amt_send_request(amt, false);
974 amt_send_request(amt, true);
975 amt_update_gw_status(amt, AMT_STATUS_SENT_REQUEST, true);
978 exp = min_t(u32, (1 * (1 << amt->req_cnt)), AMT_MAX_REQ_TIMEOUT);
979 mod_delayed_work(amt_wq, &amt->req_wq, msecs_to_jiffies(exp * 1000));
982 static void amt_req_work(struct work_struct *work)
984 struct amt_dev *amt = container_of(to_delayed_work(work),
988 if (amt_queue_event(amt, AMT_EVENT_SEND_REQUEST, NULL))
989 mod_delayed_work(amt_wq, &amt->req_wq,
990 msecs_to_jiffies(100));
993 static bool amt_send_membership_update(struct amt_dev *amt,
997 struct amt_header_membership_update *amtmu;
1004 sock = rcu_dereference_bh(amt->sock);
1008 err = skb_cow_head(skb, LL_RESERVED_SPACE(amt->dev) + sizeof(*amtmu) +
1009 sizeof(*iph) + sizeof(struct udphdr));
1013 skb_reset_inner_headers(skb);
1014 memset(&fl4, 0, sizeof(struct flowi4));
1015 fl4.flowi4_oif = amt->stream_dev->ifindex;
1016 fl4.daddr = amt->remote_ip;
1017 fl4.saddr = amt->local_ip;
1018 fl4.flowi4_tos = AMT_TOS;
1019 fl4.flowi4_proto = IPPROTO_UDP;
1020 rt = ip_route_output_key(amt->net, &fl4);
1022 netdev_dbg(amt->dev, "no route to %pI4\n", &amt->remote_ip);
1026 amtmu = skb_push(skb, sizeof(*amtmu));
1028 amtmu->type = AMT_MSG_MEMBERSHIP_UPDATE;
1029 amtmu->reserved = 0;
1030 amtmu->nonce = amt->nonce;
1031 amtmu->response_mac = amt->mac;
1034 skb_set_inner_protocol(skb, htons(ETH_P_IP));
1036 skb_set_inner_protocol(skb, htons(ETH_P_IPV6));
1037 udp_tunnel_xmit_skb(rt, sock->sk, skb,
1041 ip4_dst_hoplimit(&rt->dst),
1047 amt_update_gw_status(amt, AMT_STATUS_SENT_UPDATE, true);
1051 static void amt_send_multicast_data(struct amt_dev *amt,
1052 const struct sk_buff *oskb,
1053 struct amt_tunnel_list *tunnel,
1056 struct amt_header_mcast_data *amtmd;
1057 struct socket *sock;
1058 struct sk_buff *skb;
1063 sock = rcu_dereference_bh(amt->sock);
1067 skb = skb_copy_expand(oskb, sizeof(*amtmd) + sizeof(*iph) +
1068 sizeof(struct udphdr), 0, GFP_ATOMIC);
1072 skb_reset_inner_headers(skb);
1073 memset(&fl4, 0, sizeof(struct flowi4));
1074 fl4.flowi4_oif = amt->stream_dev->ifindex;
1075 fl4.daddr = tunnel->ip4;
1076 fl4.saddr = amt->local_ip;
1077 fl4.flowi4_proto = IPPROTO_UDP;
1078 rt = ip_route_output_key(amt->net, &fl4);
1080 netdev_dbg(amt->dev, "no route to %pI4\n", &tunnel->ip4);
1085 amtmd = skb_push(skb, sizeof(*amtmd));
1087 amtmd->reserved = 0;
1088 amtmd->type = AMT_MSG_MULTICAST_DATA;
1091 skb_set_inner_protocol(skb, htons(ETH_P_IP));
1093 skb_set_inner_protocol(skb, htons(ETH_P_IPV6));
1094 udp_tunnel_xmit_skb(rt, sock->sk, skb,
1098 ip4_dst_hoplimit(&rt->dst),
1101 tunnel->source_port,
1106 static bool amt_send_membership_query(struct amt_dev *amt,
1107 struct sk_buff *skb,
1108 struct amt_tunnel_list *tunnel,
1111 struct amt_header_membership_query *amtmq;
1112 struct socket *sock;
1117 sock = rcu_dereference_bh(amt->sock);
1121 err = skb_cow_head(skb, LL_RESERVED_SPACE(amt->dev) + sizeof(*amtmq) +
1122 sizeof(struct iphdr) + sizeof(struct udphdr));
1126 skb_reset_inner_headers(skb);
1127 memset(&fl4, 0, sizeof(struct flowi4));
1128 fl4.flowi4_oif = amt->stream_dev->ifindex;
1129 fl4.daddr = tunnel->ip4;
1130 fl4.saddr = amt->local_ip;
1131 fl4.flowi4_tos = AMT_TOS;
1132 fl4.flowi4_proto = IPPROTO_UDP;
1133 rt = ip_route_output_key(amt->net, &fl4);
1135 netdev_dbg(amt->dev, "no route to %pI4\n", &tunnel->ip4);
1139 amtmq = skb_push(skb, sizeof(*amtmq));
1141 amtmq->type = AMT_MSG_MEMBERSHIP_QUERY;
1142 amtmq->reserved = 0;
1145 amtmq->nonce = tunnel->nonce;
1146 amtmq->response_mac = tunnel->mac;
1149 skb_set_inner_protocol(skb, htons(ETH_P_IP));
1151 skb_set_inner_protocol(skb, htons(ETH_P_IPV6));
1152 udp_tunnel_xmit_skb(rt, sock->sk, skb,
1156 ip4_dst_hoplimit(&rt->dst),
1159 tunnel->source_port,
1162 amt_update_relay_status(tunnel, AMT_STATUS_SENT_QUERY, true);
1166 static netdev_tx_t amt_dev_xmit(struct sk_buff *skb, struct net_device *dev)
1168 struct amt_dev *amt = netdev_priv(dev);
1169 struct amt_tunnel_list *tunnel;
1170 struct amt_group_node *gnode;
1171 union amt_addr group = {0,};
1172 #if IS_ENABLED(CONFIG_IPV6)
1173 struct ipv6hdr *ip6h;
1174 struct mld_msg *mld;
1176 bool report = false;
1185 if (iph->version == 4) {
1186 if (!ipv4_is_multicast(iph->daddr))
1189 if (!ip_mc_check_igmp(skb)) {
1192 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1193 case IGMP_HOST_MEMBERSHIP_REPORT:
1196 case IGMP_HOST_MEMBERSHIP_QUERY:
1206 group.ip4 = iph->daddr;
1207 #if IS_ENABLED(CONFIG_IPV6)
1208 } else if (iph->version == 6) {
1209 ip6h = ipv6_hdr(skb);
1210 if (!ipv6_addr_is_multicast(&ip6h->daddr))
1213 if (!ipv6_mc_check_mld(skb)) {
1214 mld = (struct mld_msg *)skb_transport_header(skb);
1215 switch (mld->mld_type) {
1216 case ICMPV6_MGM_REPORT:
1217 case ICMPV6_MLD2_REPORT:
1220 case ICMPV6_MGM_QUERY:
1230 group.ip6 = ip6h->daddr;
1233 dev->stats.tx_errors++;
1237 if (!pskb_may_pull(skb, sizeof(struct ethhdr)))
1240 skb_pull(skb, sizeof(struct ethhdr));
1242 if (amt->mode == AMT_MODE_GATEWAY) {
1243 /* Gateway only passes IGMP/MLD packets */
1246 if ((!v6 && !READ_ONCE(amt->ready4)) ||
1247 (v6 && !READ_ONCE(amt->ready6)))
1249 if (amt_send_membership_update(amt, skb, v6))
1252 } else if (amt->mode == AMT_MODE_RELAY) {
1254 tunnel = amt_skb_cb(skb)->tunnel;
1260 /* Do not forward unexpected query */
1261 if (amt_send_membership_query(amt, skb, tunnel, v6))
1268 list_for_each_entry_rcu(tunnel, &amt->tunnel_list, list) {
1269 hash = amt_group_hash(tunnel, &group);
1270 hlist_for_each_entry_rcu(gnode, &tunnel->groups[hash],
1273 if (gnode->group_addr.ip4 == iph->daddr)
1275 #if IS_ENABLED(CONFIG_IPV6)
1277 if (ipv6_addr_equal(&gnode->group_addr.ip6,
1285 amt_send_multicast_data(amt, skb, tunnel, v6);
1290 return NETDEV_TX_OK;
1294 dev->stats.tx_dropped++;
1295 return NETDEV_TX_OK;
1298 static int amt_parse_type(struct sk_buff *skb)
1300 struct amt_header *amth;
1302 if (!pskb_may_pull(skb, sizeof(struct udphdr) +
1303 sizeof(struct amt_header)))
1306 amth = (struct amt_header *)(udp_hdr(skb) + 1);
1308 if (amth->version != 0)
1311 if (amth->type >= __AMT_MSG_MAX || !amth->type)
1316 static void amt_clear_groups(struct amt_tunnel_list *tunnel)
1318 struct amt_dev *amt = tunnel->amt;
1319 struct amt_group_node *gnode;
1320 struct hlist_node *t;
1323 spin_lock_bh(&tunnel->lock);
1325 for (i = 0; i < amt->hash_buckets; i++)
1326 hlist_for_each_entry_safe(gnode, t, &tunnel->groups[i], node)
1327 amt_del_group(amt, gnode);
1329 spin_unlock_bh(&tunnel->lock);
1332 static void amt_tunnel_expire(struct work_struct *work)
1334 struct amt_tunnel_list *tunnel = container_of(to_delayed_work(work),
1335 struct amt_tunnel_list,
1337 struct amt_dev *amt = tunnel->amt;
1339 spin_lock_bh(&amt->lock);
1341 list_del_rcu(&tunnel->list);
1343 amt_clear_groups(tunnel);
1345 spin_unlock_bh(&amt->lock);
1346 kfree_rcu(tunnel, rcu);
1349 static void amt_cleanup_srcs(struct amt_dev *amt,
1350 struct amt_tunnel_list *tunnel,
1351 struct amt_group_node *gnode)
1353 struct amt_source_node *snode;
1354 struct hlist_node *t;
1357 /* Delete old sources */
1358 for (i = 0; i < amt->hash_buckets; i++) {
1359 hlist_for_each_entry_safe(snode, t, &gnode->sources[i], node) {
1360 if (snode->flags == AMT_SOURCE_OLD)
1361 amt_destroy_source(snode);
1365 /* switch from new to old */
1366 for (i = 0; i < amt->hash_buckets; i++) {
1367 hlist_for_each_entry_rcu(snode, &gnode->sources[i], node) {
1368 snode->flags = AMT_SOURCE_OLD;
1370 netdev_dbg(snode->gnode->amt->dev,
1371 "Add source as OLD %pI4 from %pI4\n",
1372 &snode->source_addr.ip4,
1373 &gnode->group_addr.ip4);
1374 #if IS_ENABLED(CONFIG_IPV6)
1376 netdev_dbg(snode->gnode->amt->dev,
1377 "Add source as OLD %pI6 from %pI6\n",
1378 &snode->source_addr.ip6,
1379 &gnode->group_addr.ip6);
1385 static void amt_add_srcs(struct amt_dev *amt, struct amt_tunnel_list *tunnel,
1386 struct amt_group_node *gnode, void *grec,
1389 struct igmpv3_grec *igmp_grec;
1390 struct amt_source_node *snode;
1391 #if IS_ENABLED(CONFIG_IPV6)
1392 struct mld2_grec *mld_grec;
1394 union amt_addr src = {0,};
1400 igmp_grec = (struct igmpv3_grec *)grec;
1401 nsrcs = ntohs(igmp_grec->grec_nsrcs);
1403 #if IS_ENABLED(CONFIG_IPV6)
1404 mld_grec = (struct mld2_grec *)grec;
1405 nsrcs = ntohs(mld_grec->grec_nsrcs);
1410 for (i = 0; i < nsrcs; i++) {
1411 if (tunnel->nr_sources >= amt->max_sources)
1414 src.ip4 = igmp_grec->grec_src[i];
1415 #if IS_ENABLED(CONFIG_IPV6)
1417 memcpy(&src.ip6, &mld_grec->grec_src[i],
1418 sizeof(struct in6_addr));
1420 if (amt_lookup_src(tunnel, gnode, AMT_FILTER_ALL, &src))
1423 snode = amt_alloc_snode(gnode, &src);
1425 hash = amt_source_hash(tunnel, &snode->source_addr);
1426 hlist_add_head_rcu(&snode->node, &gnode->sources[hash]);
1427 tunnel->nr_sources++;
1428 gnode->nr_sources++;
1431 netdev_dbg(snode->gnode->amt->dev,
1432 "Add source as NEW %pI4 from %pI4\n",
1433 &snode->source_addr.ip4,
1434 &gnode->group_addr.ip4);
1435 #if IS_ENABLED(CONFIG_IPV6)
1437 netdev_dbg(snode->gnode->amt->dev,
1438 "Add source as NEW %pI6 from %pI6\n",
1439 &snode->source_addr.ip6,
1440 &gnode->group_addr.ip6);
1446 /* Router State Report Rec'd New Router State
1447 * ------------ ------------ ----------------
1448 * EXCLUDE (X,Y) IS_IN (A) EXCLUDE (X+A,Y-A)
1450 * -----------+-----------+-----------+
1452 * -----------+-----------+-----------+
1454 * -----------+-----------+-----------+
1456 * -----------+-----------+-----------+
1458 * -----------+-----------+-----------+
1460 * a) Received sources are NONE/NEW
1461 * b) All NONE will be deleted by amt_cleanup_srcs().
1462 * c) All OLD will be deleted by amt_cleanup_srcs().
1463 * d) After delete, NEW source will be switched to OLD.
1465 static void amt_lookup_act_srcs(struct amt_tunnel_list *tunnel,
1466 struct amt_group_node *gnode,
1469 enum amt_filter filter,
1473 struct amt_dev *amt = tunnel->amt;
1474 struct amt_source_node *snode;
1475 struct igmpv3_grec *igmp_grec;
1476 #if IS_ENABLED(CONFIG_IPV6)
1477 struct mld2_grec *mld_grec;
1479 union amt_addr src = {0,};
1480 struct hlist_node *t;
1485 igmp_grec = (struct igmpv3_grec *)grec;
1486 nsrcs = ntohs(igmp_grec->grec_nsrcs);
1488 #if IS_ENABLED(CONFIG_IPV6)
1489 mld_grec = (struct mld2_grec *)grec;
1490 nsrcs = ntohs(mld_grec->grec_nsrcs);
1496 memset(&src, 0, sizeof(union amt_addr));
1500 for (i = 0; i < nsrcs; i++) {
1502 src.ip4 = igmp_grec->grec_src[i];
1503 #if IS_ENABLED(CONFIG_IPV6)
1505 memcpy(&src.ip6, &mld_grec->grec_src[i],
1506 sizeof(struct in6_addr));
1508 snode = amt_lookup_src(tunnel, gnode, filter, &src);
1511 amt_act_src(tunnel, gnode, snode, act);
1516 for (i = 0; i < amt->hash_buckets; i++) {
1517 hlist_for_each_entry_safe(snode, t, &gnode->sources[i],
1519 if (amt_status_filter(snode, filter))
1520 amt_act_src(tunnel, gnode, snode, act);
1523 for (i = 0; i < nsrcs; i++) {
1525 src.ip4 = igmp_grec->grec_src[i];
1526 #if IS_ENABLED(CONFIG_IPV6)
1528 memcpy(&src.ip6, &mld_grec->grec_src[i],
1529 sizeof(struct in6_addr));
1531 snode = amt_lookup_src(tunnel, gnode, filter, &src);
1534 amt_act_src(tunnel, gnode, snode, act);
1539 for (i = 0; i < amt->hash_buckets; i++) {
1540 hlist_for_each_entry_safe(snode, t, &gnode->sources[i],
1542 if (!amt_status_filter(snode, filter))
1544 for (j = 0; j < nsrcs; j++) {
1546 src.ip4 = igmp_grec->grec_src[j];
1547 #if IS_ENABLED(CONFIG_IPV6)
1550 &mld_grec->grec_src[j],
1551 sizeof(struct in6_addr));
1553 if (amt_addr_equal(&snode->source_addr,
1557 amt_act_src(tunnel, gnode, snode, act);
1563 case AMT_OPS_SUB_REV:
1565 for (i = 0; i < nsrcs; i++) {
1567 src.ip4 = igmp_grec->grec_src[i];
1568 #if IS_ENABLED(CONFIG_IPV6)
1570 memcpy(&src.ip6, &mld_grec->grec_src[i],
1571 sizeof(struct in6_addr));
1573 snode = amt_lookup_src(tunnel, gnode, AMT_FILTER_ALL,
1576 snode = amt_lookup_src(tunnel, gnode,
1579 amt_act_src(tunnel, gnode, snode, act);
1584 netdev_dbg(amt->dev, "Invalid type\n");
1589 static void amt_mcast_is_in_handler(struct amt_dev *amt,
1590 struct amt_tunnel_list *tunnel,
1591 struct amt_group_node *gnode,
1592 void *grec, void *zero_grec, bool v6)
1594 if (gnode->filter_mode == MCAST_INCLUDE) {
1595 /* Router State Report Rec'd New Router State Actions
1596 * ------------ ------------ ---------------- -------
1597 * INCLUDE (A) IS_IN (B) INCLUDE (A+B) (B)=GMI
1599 /* Update IS_IN (B) as FWD/NEW */
1600 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
1601 AMT_FILTER_NONE_NEW,
1602 AMT_ACT_STATUS_FWD_NEW,
1604 /* Update INCLUDE (A) as NEW */
1605 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
1607 AMT_ACT_STATUS_FWD_NEW,
1610 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
1616 * ------------ ------------ ---------------- -------
1617 * EXCLUDE (X,Y) IS_IN (A) EXCLUDE (X+A,Y-A) (A)=GMI
1619 /* Update (A) in (X, Y) as NONE/NEW */
1620 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
1622 AMT_ACT_STATUS_NONE_NEW,
1624 /* Update FWD/OLD as FWD/NEW */
1625 amt_lookup_act_srcs(tunnel, gnode, zero_grec, AMT_OPS_UNI,
1627 AMT_ACT_STATUS_FWD_NEW,
1629 /* Update IS_IN (A) as FWD/NEW */
1630 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
1631 AMT_FILTER_NONE_NEW,
1632 AMT_ACT_STATUS_FWD_NEW,
1634 /* Update EXCLUDE (, Y-A) as D_FWD_NEW */
1635 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB,
1637 AMT_ACT_STATUS_D_FWD_NEW,
1642 static void amt_mcast_is_ex_handler(struct amt_dev *amt,
1643 struct amt_tunnel_list *tunnel,
1644 struct amt_group_node *gnode,
1645 void *grec, void *zero_grec, bool v6)
1647 if (gnode->filter_mode == MCAST_INCLUDE) {
1648 /* Router State Report Rec'd New Router State Actions
1649 * ------------ ------------ ---------------- -------
1650 * INCLUDE (A) IS_EX (B) EXCLUDE (A*B,B-A) (B-A)=0
1654 /* EXCLUDE(A*B, ) */
1655 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
1657 AMT_ACT_STATUS_FWD_NEW,
1659 /* EXCLUDE(, B-A) */
1660 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV,
1662 AMT_ACT_STATUS_D_FWD_NEW,
1665 amt_lookup_act_srcs(tunnel, gnode, zero_grec, AMT_OPS_UNI,
1666 AMT_FILTER_D_FWD_NEW,
1669 /* Group Timer=GMI */
1670 if (!mod_delayed_work(amt_wq, &gnode->group_timer,
1671 msecs_to_jiffies(amt_gmi(amt))))
1673 gnode->filter_mode = MCAST_EXCLUDE;
1674 /* Delete (A-B) will be worked by amt_cleanup_srcs(). */
1676 /* Router State Report Rec'd New Router State Actions
1677 * ------------ ------------ ---------------- -------
1678 * EXCLUDE (X,Y) IS_EX (A) EXCLUDE (A-Y,Y*A) (A-X-Y)=GMI
1683 /* EXCLUDE (A-Y, ) */
1684 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV,
1686 AMT_ACT_STATUS_FWD_NEW,
1688 /* EXCLUDE (, Y*A ) */
1689 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
1691 AMT_ACT_STATUS_D_FWD_NEW,
1694 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV,
1695 AMT_FILTER_BOTH_NEW,
1698 /* Group Timer=GMI */
1699 if (!mod_delayed_work(amt_wq, &gnode->group_timer,
1700 msecs_to_jiffies(amt_gmi(amt))))
1702 /* Delete (X-A), (Y-A) will be worked by amt_cleanup_srcs(). */
1706 static void amt_mcast_to_in_handler(struct amt_dev *amt,
1707 struct amt_tunnel_list *tunnel,
1708 struct amt_group_node *gnode,
1709 void *grec, void *zero_grec, bool v6)
1711 if (gnode->filter_mode == MCAST_INCLUDE) {
1712 /* Router State Report Rec'd New Router State Actions
1713 * ------------ ------------ ---------------- -------
1714 * INCLUDE (A) TO_IN (B) INCLUDE (A+B) (B)=GMI
1717 /* Update TO_IN (B) sources as FWD/NEW */
1718 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
1719 AMT_FILTER_NONE_NEW,
1720 AMT_ACT_STATUS_FWD_NEW,
1722 /* Update INCLUDE (A) sources as NEW */
1723 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
1725 AMT_ACT_STATUS_FWD_NEW,
1728 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
1733 /* Router State Report Rec'd New Router State Actions
1734 * ------------ ------------ ---------------- -------
1735 * EXCLUDE (X,Y) TO_IN (A) EXCLUDE (X+A,Y-A) (A)=GMI
1739 /* Update TO_IN (A) sources as FWD/NEW */
1740 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
1741 AMT_FILTER_NONE_NEW,
1742 AMT_ACT_STATUS_FWD_NEW,
1744 /* Update EXCLUDE(X,) sources as FWD/NEW */
1745 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
1747 AMT_ACT_STATUS_FWD_NEW,
1750 * (A) are already switched to FWD_NEW.
1751 * So, D_FWD/OLD -> D_FWD/NEW is okay.
1753 amt_lookup_act_srcs(tunnel, gnode, zero_grec, AMT_OPS_UNI,
1755 AMT_ACT_STATUS_D_FWD_NEW,
1758 * Only FWD_NEW will have (A) sources.
1760 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
1767 static void amt_mcast_to_ex_handler(struct amt_dev *amt,
1768 struct amt_tunnel_list *tunnel,
1769 struct amt_group_node *gnode,
1770 void *grec, void *zero_grec, bool v6)
1772 if (gnode->filter_mode == MCAST_INCLUDE) {
1773 /* Router State Report Rec'd New Router State Actions
1774 * ------------ ------------ ---------------- -------
1775 * INCLUDE (A) TO_EX (B) EXCLUDE (A*B,B-A) (B-A)=0
1780 /* EXCLUDE (A*B, ) */
1781 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
1783 AMT_ACT_STATUS_FWD_NEW,
1785 /* EXCLUDE (, B-A) */
1786 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV,
1788 AMT_ACT_STATUS_D_FWD_NEW,
1791 amt_lookup_act_srcs(tunnel, gnode, zero_grec, AMT_OPS_UNI,
1792 AMT_FILTER_D_FWD_NEW,
1795 /* Group Timer=GMI */
1796 if (!mod_delayed_work(amt_wq, &gnode->group_timer,
1797 msecs_to_jiffies(amt_gmi(amt))))
1799 gnode->filter_mode = MCAST_EXCLUDE;
1800 /* Delete (A-B) will be worked by amt_cleanup_srcs(). */
1802 /* Router State Report Rec'd New Router State Actions
1803 * ------------ ------------ ---------------- -------
1804 * EXCLUDE (X,Y) TO_EX (A) EXCLUDE (A-Y,Y*A) (A-X-Y)=Group Timer
1810 /* Update (A-X-Y) as NONE/OLD */
1811 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV,
1815 /* EXCLUDE (A-Y, ) */
1816 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV,
1818 AMT_ACT_STATUS_FWD_NEW,
1820 /* EXCLUDE (, Y*A) */
1821 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
1823 AMT_ACT_STATUS_D_FWD_NEW,
1825 /* Group Timer=GMI */
1826 if (!mod_delayed_work(amt_wq, &gnode->group_timer,
1827 msecs_to_jiffies(amt_gmi(amt))))
1829 /* Delete (X-A), (Y-A) will be worked by amt_cleanup_srcs(). */
1833 static void amt_mcast_allow_handler(struct amt_dev *amt,
1834 struct amt_tunnel_list *tunnel,
1835 struct amt_group_node *gnode,
1836 void *grec, void *zero_grec, bool v6)
1838 if (gnode->filter_mode == MCAST_INCLUDE) {
1839 /* Router State Report Rec'd New Router State Actions
1840 * ------------ ------------ ---------------- -------
1841 * INCLUDE (A) ALLOW (B) INCLUDE (A+B) (B)=GMI
1844 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
1846 AMT_ACT_STATUS_FWD_NEW,
1849 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
1854 /* Router State Report Rec'd New Router State Actions
1855 * ------------ ------------ ---------------- -------
1856 * EXCLUDE (X,Y) ALLOW (A) EXCLUDE (X+A,Y-A) (A)=GMI
1858 /* EXCLUDE (X+A, ) */
1859 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
1861 AMT_ACT_STATUS_FWD_NEW,
1863 /* EXCLUDE (, Y-A) */
1864 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB,
1866 AMT_ACT_STATUS_D_FWD_NEW,
1869 * All (A) source are now FWD/NEW status.
1871 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
1878 static void amt_mcast_block_handler(struct amt_dev *amt,
1879 struct amt_tunnel_list *tunnel,
1880 struct amt_group_node *gnode,
1881 void *grec, void *zero_grec, bool v6)
1883 if (gnode->filter_mode == MCAST_INCLUDE) {
1884 /* Router State Report Rec'd New Router State Actions
1885 * ------------ ------------ ---------------- -------
1886 * INCLUDE (A) BLOCK (B) INCLUDE (A) Send Q(G,A*B)
1889 amt_lookup_act_srcs(tunnel, gnode, zero_grec, AMT_OPS_UNI,
1891 AMT_ACT_STATUS_FWD_NEW,
1894 /* Router State Report Rec'd New Router State Actions
1895 * ------------ ------------ ---------------- -------
1896 * EXCLUDE (X,Y) BLOCK (A) EXCLUDE (X+(A-Y),Y) (A-X-Y)=Group Timer
1899 /* (A-X-Y)=Group Timer */
1900 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV,
1905 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
1907 AMT_ACT_STATUS_FWD_NEW,
1909 /* EXCLUDE (X+(A-Y) */
1910 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV,
1912 AMT_ACT_STATUS_FWD_NEW,
1915 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
1917 AMT_ACT_STATUS_D_FWD_NEW,
1923 * 7.3.2. In the Presence of Older Version Group Members
1925 * When Group Compatibility Mode is IGMPv2, a router internally
1926 * translates the following IGMPv2 messages for that group to their
1927 * IGMPv3 equivalents:
1929 * IGMPv2 Message IGMPv3 Equivalent
1930 * -------------- -----------------
1931 * Report IS_EX( {} )
1934 static void amt_igmpv2_report_handler(struct amt_dev *amt, struct sk_buff *skb,
1935 struct amt_tunnel_list *tunnel)
1937 struct igmphdr *ih = igmp_hdr(skb);
1938 struct iphdr *iph = ip_hdr(skb);
1939 struct amt_group_node *gnode;
1940 union amt_addr group, host;
1942 memset(&group, 0, sizeof(union amt_addr));
1943 group.ip4 = ih->group;
1944 memset(&host, 0, sizeof(union amt_addr));
1945 host.ip4 = iph->saddr;
1947 gnode = amt_lookup_group(tunnel, &group, &host, false);
1949 gnode = amt_add_group(amt, tunnel, &group, &host, false);
1950 if (!IS_ERR(gnode)) {
1951 gnode->filter_mode = MCAST_EXCLUDE;
1952 if (!mod_delayed_work(amt_wq, &gnode->group_timer,
1953 msecs_to_jiffies(amt_gmi(amt))))
1960 * 7.3.2. In the Presence of Older Version Group Members
1962 * When Group Compatibility Mode is IGMPv2, a router internally
1963 * translates the following IGMPv2 messages for that group to their
1964 * IGMPv3 equivalents:
1966 * IGMPv2 Message IGMPv3 Equivalent
1967 * -------------- -----------------
1968 * Report IS_EX( {} )
1971 static void amt_igmpv2_leave_handler(struct amt_dev *amt, struct sk_buff *skb,
1972 struct amt_tunnel_list *tunnel)
1974 struct igmphdr *ih = igmp_hdr(skb);
1975 struct iphdr *iph = ip_hdr(skb);
1976 struct amt_group_node *gnode;
1977 union amt_addr group, host;
1979 memset(&group, 0, sizeof(union amt_addr));
1980 group.ip4 = ih->group;
1981 memset(&host, 0, sizeof(union amt_addr));
1982 host.ip4 = iph->saddr;
1984 gnode = amt_lookup_group(tunnel, &group, &host, false);
1986 amt_del_group(amt, gnode);
1989 static void amt_igmpv3_report_handler(struct amt_dev *amt, struct sk_buff *skb,
1990 struct amt_tunnel_list *tunnel)
1992 struct igmpv3_report *ihrv3 = igmpv3_report_hdr(skb);
1993 int len = skb_transport_offset(skb) + sizeof(*ihrv3);
1994 void *zero_grec = (void *)&igmpv3_zero_grec;
1995 struct iphdr *iph = ip_hdr(skb);
1996 struct amt_group_node *gnode;
1997 union amt_addr group, host;
1998 struct igmpv3_grec *grec;
2002 for (i = 0; i < ntohs(ihrv3->ngrec); i++) {
2003 len += sizeof(*grec);
2004 if (!ip_mc_may_pull(skb, len))
2007 grec = (void *)(skb->data + len - sizeof(*grec));
2008 nsrcs = ntohs(grec->grec_nsrcs);
2010 len += nsrcs * sizeof(__be32);
2011 if (!ip_mc_may_pull(skb, len))
2014 memset(&group, 0, sizeof(union amt_addr));
2015 group.ip4 = grec->grec_mca;
2016 memset(&host, 0, sizeof(union amt_addr));
2017 host.ip4 = iph->saddr;
2018 gnode = amt_lookup_group(tunnel, &group, &host, false);
2020 gnode = amt_add_group(amt, tunnel, &group, &host,
2026 amt_add_srcs(amt, tunnel, gnode, grec, false);
2027 switch (grec->grec_type) {
2028 case IGMPV3_MODE_IS_INCLUDE:
2029 amt_mcast_is_in_handler(amt, tunnel, gnode, grec,
2032 case IGMPV3_MODE_IS_EXCLUDE:
2033 amt_mcast_is_ex_handler(amt, tunnel, gnode, grec,
2036 case IGMPV3_CHANGE_TO_INCLUDE:
2037 amt_mcast_to_in_handler(amt, tunnel, gnode, grec,
2040 case IGMPV3_CHANGE_TO_EXCLUDE:
2041 amt_mcast_to_ex_handler(amt, tunnel, gnode, grec,
2044 case IGMPV3_ALLOW_NEW_SOURCES:
2045 amt_mcast_allow_handler(amt, tunnel, gnode, grec,
2048 case IGMPV3_BLOCK_OLD_SOURCES:
2049 amt_mcast_block_handler(amt, tunnel, gnode, grec,
2055 amt_cleanup_srcs(amt, tunnel, gnode);
2059 /* caller held tunnel->lock */
2060 static void amt_igmp_report_handler(struct amt_dev *amt, struct sk_buff *skb,
2061 struct amt_tunnel_list *tunnel)
2063 struct igmphdr *ih = igmp_hdr(skb);
2066 case IGMPV3_HOST_MEMBERSHIP_REPORT:
2067 amt_igmpv3_report_handler(amt, skb, tunnel);
2069 case IGMPV2_HOST_MEMBERSHIP_REPORT:
2070 amt_igmpv2_report_handler(amt, skb, tunnel);
2072 case IGMP_HOST_LEAVE_MESSAGE:
2073 amt_igmpv2_leave_handler(amt, skb, tunnel);
2080 #if IS_ENABLED(CONFIG_IPV6)
2082 * 8.3.2. In the Presence of MLDv1 Multicast Address Listeners
2084 * When Multicast Address Compatibility Mode is MLDv2, a router acts
2085 * using the MLDv2 protocol for that multicast address. When Multicast
2086 * Address Compatibility Mode is MLDv1, a router internally translates
2087 * the following MLDv1 messages for that multicast address to their
2088 * MLDv2 equivalents:
2090 * MLDv1 Message MLDv2 Equivalent
2091 * -------------- -----------------
2092 * Report IS_EX( {} )
2095 static void amt_mldv1_report_handler(struct amt_dev *amt, struct sk_buff *skb,
2096 struct amt_tunnel_list *tunnel)
2098 struct mld_msg *mld = (struct mld_msg *)icmp6_hdr(skb);
2099 struct ipv6hdr *ip6h = ipv6_hdr(skb);
2100 struct amt_group_node *gnode;
2101 union amt_addr group, host;
2103 memcpy(&group.ip6, &mld->mld_mca, sizeof(struct in6_addr));
2104 memcpy(&host.ip6, &ip6h->saddr, sizeof(struct in6_addr));
2106 gnode = amt_lookup_group(tunnel, &group, &host, true);
2108 gnode = amt_add_group(amt, tunnel, &group, &host, true);
2109 if (!IS_ERR(gnode)) {
2110 gnode->filter_mode = MCAST_EXCLUDE;
2111 if (!mod_delayed_work(amt_wq, &gnode->group_timer,
2112 msecs_to_jiffies(amt_gmi(amt))))
2119 * 8.3.2. In the Presence of MLDv1 Multicast Address Listeners
2121 * When Multicast Address Compatibility Mode is MLDv2, a router acts
2122 * using the MLDv2 protocol for that multicast address. When Multicast
2123 * Address Compatibility Mode is MLDv1, a router internally translates
2124 * the following MLDv1 messages for that multicast address to their
2125 * MLDv2 equivalents:
2127 * MLDv1 Message MLDv2 Equivalent
2128 * -------------- -----------------
2129 * Report IS_EX( {} )
2132 static void amt_mldv1_leave_handler(struct amt_dev *amt, struct sk_buff *skb,
2133 struct amt_tunnel_list *tunnel)
2135 struct mld_msg *mld = (struct mld_msg *)icmp6_hdr(skb);
2136 struct iphdr *iph = ip_hdr(skb);
2137 struct amt_group_node *gnode;
2138 union amt_addr group, host;
2140 memcpy(&group.ip6, &mld->mld_mca, sizeof(struct in6_addr));
2141 memset(&host, 0, sizeof(union amt_addr));
2142 host.ip4 = iph->saddr;
2144 gnode = amt_lookup_group(tunnel, &group, &host, true);
2146 amt_del_group(amt, gnode);
2151 static void amt_mldv2_report_handler(struct amt_dev *amt, struct sk_buff *skb,
2152 struct amt_tunnel_list *tunnel)
2154 struct mld2_report *mld2r = (struct mld2_report *)icmp6_hdr(skb);
2155 int len = skb_transport_offset(skb) + sizeof(*mld2r);
2156 void *zero_grec = (void *)&mldv2_zero_grec;
2157 struct ipv6hdr *ip6h = ipv6_hdr(skb);
2158 struct amt_group_node *gnode;
2159 union amt_addr group, host;
2160 struct mld2_grec *grec;
2164 for (i = 0; i < ntohs(mld2r->mld2r_ngrec); i++) {
2165 len += sizeof(*grec);
2166 if (!ipv6_mc_may_pull(skb, len))
2169 grec = (void *)(skb->data + len - sizeof(*grec));
2170 nsrcs = ntohs(grec->grec_nsrcs);
2172 len += nsrcs * sizeof(struct in6_addr);
2173 if (!ipv6_mc_may_pull(skb, len))
2176 memset(&group, 0, sizeof(union amt_addr));
2177 group.ip6 = grec->grec_mca;
2178 memset(&host, 0, sizeof(union amt_addr));
2179 host.ip6 = ip6h->saddr;
2180 gnode = amt_lookup_group(tunnel, &group, &host, true);
2182 gnode = amt_add_group(amt, tunnel, &group, &host,
2188 amt_add_srcs(amt, tunnel, gnode, grec, true);
2189 switch (grec->grec_type) {
2190 case MLD2_MODE_IS_INCLUDE:
2191 amt_mcast_is_in_handler(amt, tunnel, gnode, grec,
2194 case MLD2_MODE_IS_EXCLUDE:
2195 amt_mcast_is_ex_handler(amt, tunnel, gnode, grec,
2198 case MLD2_CHANGE_TO_INCLUDE:
2199 amt_mcast_to_in_handler(amt, tunnel, gnode, grec,
2202 case MLD2_CHANGE_TO_EXCLUDE:
2203 amt_mcast_to_ex_handler(amt, tunnel, gnode, grec,
2206 case MLD2_ALLOW_NEW_SOURCES:
2207 amt_mcast_allow_handler(amt, tunnel, gnode, grec,
2210 case MLD2_BLOCK_OLD_SOURCES:
2211 amt_mcast_block_handler(amt, tunnel, gnode, grec,
2217 amt_cleanup_srcs(amt, tunnel, gnode);
2221 /* caller held tunnel->lock */
2222 static void amt_mld_report_handler(struct amt_dev *amt, struct sk_buff *skb,
2223 struct amt_tunnel_list *tunnel)
2225 struct mld_msg *mld = (struct mld_msg *)icmp6_hdr(skb);
2227 switch (mld->mld_type) {
2228 case ICMPV6_MGM_REPORT:
2229 amt_mldv1_report_handler(amt, skb, tunnel);
2231 case ICMPV6_MLD2_REPORT:
2232 amt_mldv2_report_handler(amt, skb, tunnel);
2234 case ICMPV6_MGM_REDUCTION:
2235 amt_mldv1_leave_handler(amt, skb, tunnel);
2243 static bool amt_advertisement_handler(struct amt_dev *amt, struct sk_buff *skb)
2245 struct amt_header_advertisement *amta;
2248 hdr_size = sizeof(*amta) + sizeof(struct udphdr);
2249 if (!pskb_may_pull(skb, hdr_size))
2252 amta = (struct amt_header_advertisement *)(udp_hdr(skb) + 1);
2256 if (amta->reserved || amta->version)
2259 if (ipv4_is_loopback(amta->ip4) || ipv4_is_multicast(amta->ip4) ||
2260 ipv4_is_zeronet(amta->ip4))
2263 if (amt->status != AMT_STATUS_SENT_DISCOVERY ||
2264 amt->nonce != amta->nonce)
2267 amt->remote_ip = amta->ip4;
2268 netdev_dbg(amt->dev, "advertised remote ip = %pI4\n", &amt->remote_ip);
2269 mod_delayed_work(amt_wq, &amt->req_wq, 0);
2271 amt_update_gw_status(amt, AMT_STATUS_RECEIVED_ADVERTISEMENT, true);
2275 static bool amt_multicast_data_handler(struct amt_dev *amt, struct sk_buff *skb)
2277 struct amt_header_mcast_data *amtmd;
2278 int hdr_size, len, err;
2282 hdr_size = sizeof(*amtmd) + sizeof(struct udphdr);
2283 if (!pskb_may_pull(skb, hdr_size))
2286 amtmd = (struct amt_header_mcast_data *)(udp_hdr(skb) + 1);
2287 if (amtmd->reserved || amtmd->version)
2290 if (iptunnel_pull_header(skb, hdr_size, htons(ETH_P_IP), false))
2293 skb_reset_network_header(skb);
2294 skb_push(skb, sizeof(*eth));
2295 skb_reset_mac_header(skb);
2296 skb_pull(skb, sizeof(*eth));
2299 if (!pskb_may_pull(skb, sizeof(*iph)))
2303 if (iph->version == 4) {
2304 if (!ipv4_is_multicast(iph->daddr))
2306 skb->protocol = htons(ETH_P_IP);
2307 eth->h_proto = htons(ETH_P_IP);
2308 ip_eth_mc_map(iph->daddr, eth->h_dest);
2309 #if IS_ENABLED(CONFIG_IPV6)
2310 } else if (iph->version == 6) {
2311 struct ipv6hdr *ip6h;
2313 if (!pskb_may_pull(skb, sizeof(*ip6h)))
2316 ip6h = ipv6_hdr(skb);
2317 if (!ipv6_addr_is_multicast(&ip6h->daddr))
2319 skb->protocol = htons(ETH_P_IPV6);
2320 eth->h_proto = htons(ETH_P_IPV6);
2321 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
2327 skb->pkt_type = PACKET_MULTICAST;
2328 skb->ip_summed = CHECKSUM_NONE;
2330 err = gro_cells_receive(&amt->gro_cells, skb);
2331 if (likely(err == NET_RX_SUCCESS))
2332 dev_sw_netstats_rx_add(amt->dev, len);
2334 amt->dev->stats.rx_dropped++;
2339 static bool amt_membership_query_handler(struct amt_dev *amt,
2340 struct sk_buff *skb)
2342 struct amt_header_membership_query *amtmq;
2343 struct igmpv3_query *ihv3;
2344 struct ethhdr *eth, *oeth;
2348 hdr_size = sizeof(*amtmq) + sizeof(struct udphdr);
2349 if (!pskb_may_pull(skb, hdr_size))
2352 amtmq = (struct amt_header_membership_query *)(udp_hdr(skb) + 1);
2353 if (amtmq->reserved || amtmq->version)
2356 hdr_size -= sizeof(*eth);
2357 if (iptunnel_pull_header(skb, hdr_size, htons(ETH_P_TEB), false))
2360 oeth = eth_hdr(skb);
2361 skb_reset_mac_header(skb);
2362 skb_pull(skb, sizeof(*eth));
2363 skb_reset_network_header(skb);
2365 if (!pskb_may_pull(skb, sizeof(*iph)))
2369 if (iph->version == 4) {
2370 if (!pskb_may_pull(skb, sizeof(*iph) + AMT_IPHDR_OPTS +
2374 if (!ipv4_is_multicast(iph->daddr))
2377 ihv3 = skb_pull(skb, sizeof(*iph) + AMT_IPHDR_OPTS);
2378 skb_reset_transport_header(skb);
2379 skb_push(skb, sizeof(*iph) + AMT_IPHDR_OPTS);
2380 WRITE_ONCE(amt->ready4, true);
2381 amt->mac = amtmq->response_mac;
2383 amt->qi = ihv3->qqic;
2384 skb->protocol = htons(ETH_P_IP);
2385 eth->h_proto = htons(ETH_P_IP);
2386 ip_eth_mc_map(iph->daddr, eth->h_dest);
2387 #if IS_ENABLED(CONFIG_IPV6)
2388 } else if (iph->version == 6) {
2389 struct mld2_query *mld2q;
2390 struct ipv6hdr *ip6h;
2392 if (!pskb_may_pull(skb, sizeof(*ip6h) + AMT_IP6HDR_OPTS +
2396 ip6h = ipv6_hdr(skb);
2397 if (!ipv6_addr_is_multicast(&ip6h->daddr))
2400 mld2q = skb_pull(skb, sizeof(*ip6h) + AMT_IP6HDR_OPTS);
2401 skb_reset_transport_header(skb);
2402 skb_push(skb, sizeof(*ip6h) + AMT_IP6HDR_OPTS);
2403 WRITE_ONCE(amt->ready6, true);
2404 amt->mac = amtmq->response_mac;
2406 amt->qi = mld2q->mld2q_qqic;
2407 skb->protocol = htons(ETH_P_IPV6);
2408 eth->h_proto = htons(ETH_P_IPV6);
2409 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
2415 ether_addr_copy(eth->h_source, oeth->h_source);
2416 skb->pkt_type = PACKET_MULTICAST;
2417 skb->ip_summed = CHECKSUM_NONE;
2420 if (__netif_rx(skb) == NET_RX_SUCCESS) {
2421 amt_update_gw_status(amt, AMT_STATUS_RECEIVED_QUERY, true);
2422 dev_sw_netstats_rx_add(amt->dev, len);
2424 amt->dev->stats.rx_dropped++;
2431 static bool amt_update_handler(struct amt_dev *amt, struct sk_buff *skb)
2433 struct amt_header_membership_update *amtmu;
2434 struct amt_tunnel_list *tunnel;
2441 hdr_size = sizeof(*amtmu) + sizeof(struct udphdr);
2442 if (!pskb_may_pull(skb, hdr_size))
2445 amtmu = (struct amt_header_membership_update *)(udp_hdr(skb) + 1);
2446 if (amtmu->reserved || amtmu->version)
2449 if (iptunnel_pull_header(skb, hdr_size, skb->protocol, false))
2452 skb_reset_network_header(skb);
2454 list_for_each_entry_rcu(tunnel, &amt->tunnel_list, list) {
2455 if (tunnel->ip4 == iph->saddr) {
2456 if ((amtmu->nonce == tunnel->nonce &&
2457 amtmu->response_mac == tunnel->mac)) {
2458 mod_delayed_work(amt_wq, &tunnel->gc_wq,
2459 msecs_to_jiffies(amt_gmi(amt))
2463 netdev_dbg(amt->dev, "Invalid MAC\n");
2472 if (!pskb_may_pull(skb, sizeof(*iph)))
2476 if (iph->version == 4) {
2477 if (ip_mc_check_igmp(skb)) {
2478 netdev_dbg(amt->dev, "Invalid IGMP\n");
2482 spin_lock_bh(&tunnel->lock);
2483 amt_igmp_report_handler(amt, skb, tunnel);
2484 spin_unlock_bh(&tunnel->lock);
2486 skb_push(skb, sizeof(struct ethhdr));
2487 skb_reset_mac_header(skb);
2489 skb->protocol = htons(ETH_P_IP);
2490 eth->h_proto = htons(ETH_P_IP);
2491 ip_eth_mc_map(iph->daddr, eth->h_dest);
2492 #if IS_ENABLED(CONFIG_IPV6)
2493 } else if (iph->version == 6) {
2494 struct ipv6hdr *ip6h = ipv6_hdr(skb);
2496 if (ipv6_mc_check_mld(skb)) {
2497 netdev_dbg(amt->dev, "Invalid MLD\n");
2501 spin_lock_bh(&tunnel->lock);
2502 amt_mld_report_handler(amt, skb, tunnel);
2503 spin_unlock_bh(&tunnel->lock);
2505 skb_push(skb, sizeof(struct ethhdr));
2506 skb_reset_mac_header(skb);
2508 skb->protocol = htons(ETH_P_IPV6);
2509 eth->h_proto = htons(ETH_P_IPV6);
2510 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
2513 netdev_dbg(amt->dev, "Unsupported Protocol\n");
2517 skb_pull(skb, sizeof(struct ethhdr));
2518 skb->pkt_type = PACKET_MULTICAST;
2519 skb->ip_summed = CHECKSUM_NONE;
2521 if (__netif_rx(skb) == NET_RX_SUCCESS) {
2522 amt_update_relay_status(tunnel, AMT_STATUS_RECEIVED_UPDATE,
2524 dev_sw_netstats_rx_add(amt->dev, len);
2526 amt->dev->stats.rx_dropped++;
2532 static void amt_send_advertisement(struct amt_dev *amt, __be32 nonce,
2533 __be32 daddr, __be16 dport)
2535 struct amt_header_advertisement *amta;
2536 int hlen, tlen, offset;
2537 struct socket *sock;
2538 struct udphdr *udph;
2539 struct sk_buff *skb;
2547 sock = rcu_dereference(amt->sock);
2551 if (!netif_running(amt->stream_dev) || !netif_running(amt->dev))
2554 rt = ip_route_output_ports(amt->net, &fl4, sock->sk,
2555 daddr, amt->local_ip,
2556 dport, amt->relay_port,
2558 amt->stream_dev->ifindex);
2560 amt->dev->stats.tx_errors++;
2564 hlen = LL_RESERVED_SPACE(amt->dev);
2565 tlen = amt->dev->needed_tailroom;
2566 len = hlen + tlen + sizeof(*iph) + sizeof(*udph) + sizeof(*amta);
2567 skb = netdev_alloc_skb_ip_align(amt->dev, len);
2570 amt->dev->stats.tx_errors++;
2574 skb->priority = TC_PRIO_CONTROL;
2575 skb_dst_set(skb, &rt->dst);
2577 len = sizeof(*iph) + sizeof(*udph) + sizeof(*amta);
2578 skb_reset_network_header(skb);
2580 amta = skb_pull(skb, sizeof(*iph) + sizeof(*udph));
2582 amta->type = AMT_MSG_ADVERTISEMENT;
2584 amta->nonce = nonce;
2585 amta->ip4 = amt->local_ip;
2586 skb_push(skb, sizeof(*udph));
2587 skb_reset_transport_header(skb);
2588 udph = udp_hdr(skb);
2589 udph->source = amt->relay_port;
2591 udph->len = htons(sizeof(*amta) + sizeof(*udph));
2593 offset = skb_transport_offset(skb);
2594 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
2595 udph->check = csum_tcpudp_magic(amt->local_ip, daddr,
2596 sizeof(*udph) + sizeof(*amta),
2597 IPPROTO_UDP, skb->csum);
2599 skb_push(skb, sizeof(*iph));
2602 iph->ihl = (sizeof(struct iphdr)) >> 2;
2605 iph->ttl = ip4_dst_hoplimit(&rt->dst);
2607 iph->saddr = amt->local_ip;
2608 iph->protocol = IPPROTO_UDP;
2609 iph->tot_len = htons(len);
2611 skb->ip_summed = CHECKSUM_NONE;
2612 ip_select_ident(amt->net, skb, NULL);
2614 err = ip_local_out(amt->net, sock->sk, skb);
2615 if (unlikely(net_xmit_eval(err)))
2616 amt->dev->stats.tx_errors++;
2622 static bool amt_discovery_handler(struct amt_dev *amt, struct sk_buff *skb)
2624 struct amt_header_discovery *amtd;
2625 struct udphdr *udph;
2628 if (!pskb_may_pull(skb, sizeof(*udph) + sizeof(*amtd)))
2632 udph = udp_hdr(skb);
2633 amtd = (struct amt_header_discovery *)(udp_hdr(skb) + 1);
2635 if (amtd->reserved || amtd->version)
2638 amt_send_advertisement(amt, amtd->nonce, iph->saddr, udph->source);
2643 static bool amt_request_handler(struct amt_dev *amt, struct sk_buff *skb)
2645 struct amt_header_request *amtrh;
2646 struct amt_tunnel_list *tunnel;
2647 unsigned long long key;
2648 struct udphdr *udph;
2653 if (!pskb_may_pull(skb, sizeof(*udph) + sizeof(*amtrh)))
2657 udph = udp_hdr(skb);
2658 amtrh = (struct amt_header_request *)(udp_hdr(skb) + 1);
2660 if (amtrh->reserved1 || amtrh->reserved2 || amtrh->version)
2663 list_for_each_entry_rcu(tunnel, &amt->tunnel_list, list)
2664 if (tunnel->ip4 == iph->saddr)
2667 if (amt->nr_tunnels >= amt->max_tunnels) {
2668 icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0);
2672 tunnel = kzalloc(sizeof(*tunnel) +
2673 (sizeof(struct hlist_head) * amt->hash_buckets),
2678 tunnel->source_port = udph->source;
2679 tunnel->ip4 = iph->saddr;
2681 memcpy(&key, &tunnel->key, sizeof(unsigned long long));
2683 spin_lock_init(&tunnel->lock);
2684 for (i = 0; i < amt->hash_buckets; i++)
2685 INIT_HLIST_HEAD(&tunnel->groups[i]);
2687 INIT_DELAYED_WORK(&tunnel->gc_wq, amt_tunnel_expire);
2689 spin_lock_bh(&amt->lock);
2690 list_add_tail_rcu(&tunnel->list, &amt->tunnel_list);
2691 tunnel->key = amt->key;
2692 amt_update_relay_status(tunnel, AMT_STATUS_RECEIVED_REQUEST, true);
2694 mod_delayed_work(amt_wq, &tunnel->gc_wq,
2695 msecs_to_jiffies(amt_gmi(amt)));
2696 spin_unlock_bh(&amt->lock);
2699 tunnel->nonce = amtrh->nonce;
2700 mac = siphash_3u32((__force u32)tunnel->ip4,
2701 (__force u32)tunnel->source_port,
2702 (__force u32)tunnel->nonce,
2704 tunnel->mac = mac >> 16;
2706 if (!netif_running(amt->dev) || !netif_running(amt->stream_dev))
2710 amt_send_igmp_gq(amt, tunnel);
2712 amt_send_mld_gq(amt, tunnel);
2717 static void amt_gw_rcv(struct amt_dev *amt, struct sk_buff *skb)
2719 int type = amt_parse_type(skb);
2725 if (amt->mode == AMT_MODE_GATEWAY) {
2727 case AMT_MSG_ADVERTISEMENT:
2728 err = amt_advertisement_handler(amt, skb);
2730 case AMT_MSG_MEMBERSHIP_QUERY:
2731 err = amt_membership_query_handler(amt, skb);
2736 netdev_dbg(amt->dev, "Invalid type of Gateway\n");
2742 amt->dev->stats.rx_dropped++;
2749 static int amt_rcv(struct sock *sk, struct sk_buff *skb)
2751 struct amt_dev *amt;
2757 amt = rcu_dereference_sk_user_data(sk);
2764 skb->dev = amt->dev;
2766 type = amt_parse_type(skb);
2772 if (amt->mode == AMT_MODE_GATEWAY) {
2774 case AMT_MSG_ADVERTISEMENT:
2775 if (iph->saddr != amt->discovery_ip) {
2776 netdev_dbg(amt->dev, "Invalid Relay IP\n");
2780 if (amt_queue_event(amt, AMT_EVENT_RECEIVE, skb)) {
2781 netdev_dbg(amt->dev, "AMT Event queue full\n");
2786 case AMT_MSG_MULTICAST_DATA:
2787 if (iph->saddr != amt->remote_ip) {
2788 netdev_dbg(amt->dev, "Invalid Relay IP\n");
2792 err = amt_multicast_data_handler(amt, skb);
2797 case AMT_MSG_MEMBERSHIP_QUERY:
2798 if (iph->saddr != amt->remote_ip) {
2799 netdev_dbg(amt->dev, "Invalid Relay IP\n");
2803 if (amt_queue_event(amt, AMT_EVENT_RECEIVE, skb)) {
2804 netdev_dbg(amt->dev, "AMT Event queue full\n");
2811 netdev_dbg(amt->dev, "Invalid type of Gateway\n");
2816 case AMT_MSG_DISCOVERY:
2817 err = amt_discovery_handler(amt, skb);
2819 case AMT_MSG_REQUEST:
2820 err = amt_request_handler(amt, skb);
2822 case AMT_MSG_MEMBERSHIP_UPDATE:
2823 err = amt_update_handler(amt, skb);
2830 netdev_dbg(amt->dev, "Invalid type of relay\n");
2836 amt->dev->stats.rx_dropped++;
2842 rcu_read_unlock_bh();
2846 static void amt_event_work(struct work_struct *work)
2848 struct amt_dev *amt = container_of(work, struct amt_dev, event_wq);
2849 struct sk_buff *skb;
2853 for (i = 0; i < AMT_MAX_EVENTS; i++) {
2854 spin_lock_bh(&amt->lock);
2855 if (amt->nr_events == 0) {
2856 spin_unlock_bh(&amt->lock);
2859 event = amt->events[amt->event_idx].event;
2860 skb = amt->events[amt->event_idx].skb;
2861 amt->events[amt->event_idx].event = AMT_EVENT_NONE;
2862 amt->events[amt->event_idx].skb = NULL;
2865 amt->event_idx %= AMT_MAX_EVENTS;
2866 spin_unlock_bh(&amt->lock);
2869 case AMT_EVENT_RECEIVE:
2870 amt_gw_rcv(amt, skb);
2872 case AMT_EVENT_SEND_DISCOVERY:
2873 amt_event_send_discovery(amt);
2875 case AMT_EVENT_SEND_REQUEST:
2876 amt_event_send_request(amt);
2886 static int amt_err_lookup(struct sock *sk, struct sk_buff *skb)
2888 struct amt_dev *amt;
2892 amt = rcu_dereference_sk_user_data(sk);
2896 if (amt->mode != AMT_MODE_GATEWAY)
2899 type = amt_parse_type(skb);
2903 netdev_dbg(amt->dev, "Received IGMP Unreachable of %s\n",
2906 case AMT_MSG_DISCOVERY:
2908 case AMT_MSG_REQUEST:
2909 case AMT_MSG_MEMBERSHIP_UPDATE:
2910 if (READ_ONCE(amt->status) >= AMT_STATUS_RECEIVED_ADVERTISEMENT)
2911 mod_delayed_work(amt_wq, &amt->req_wq, 0);
2917 rcu_read_unlock_bh();
2920 rcu_read_unlock_bh();
2921 amt->dev->stats.rx_dropped++;
2925 static struct socket *amt_create_sock(struct net *net, __be16 port)
2927 struct udp_port_cfg udp_conf;
2928 struct socket *sock;
2931 memset(&udp_conf, 0, sizeof(udp_conf));
2932 udp_conf.family = AF_INET;
2933 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
2935 udp_conf.local_udp_port = port;
2937 err = udp_sock_create(net, &udp_conf, &sock);
2939 return ERR_PTR(err);
2944 static int amt_socket_create(struct amt_dev *amt)
2946 struct udp_tunnel_sock_cfg tunnel_cfg;
2947 struct socket *sock;
2949 sock = amt_create_sock(amt->net, amt->relay_port);
2951 return PTR_ERR(sock);
2953 /* Mark socket as an encapsulation socket */
2954 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
2955 tunnel_cfg.sk_user_data = amt;
2956 tunnel_cfg.encap_type = 1;
2957 tunnel_cfg.encap_rcv = amt_rcv;
2958 tunnel_cfg.encap_err_lookup = amt_err_lookup;
2959 tunnel_cfg.encap_destroy = NULL;
2960 setup_udp_tunnel_sock(amt->net, sock, &tunnel_cfg);
2962 rcu_assign_pointer(amt->sock, sock);
2966 static int amt_dev_open(struct net_device *dev)
2968 struct amt_dev *amt = netdev_priv(dev);
2971 amt->ready4 = false;
2972 amt->ready6 = false;
2976 err = amt_socket_create(amt);
2983 get_random_bytes(&amt->key, sizeof(siphash_key_t));
2985 amt->status = AMT_STATUS_INIT;
2986 if (amt->mode == AMT_MODE_GATEWAY) {
2987 mod_delayed_work(amt_wq, &amt->discovery_wq, 0);
2988 mod_delayed_work(amt_wq, &amt->req_wq, 0);
2989 } else if (amt->mode == AMT_MODE_RELAY) {
2990 mod_delayed_work(amt_wq, &amt->secret_wq,
2991 msecs_to_jiffies(AMT_SECRET_TIMEOUT));
2996 static int amt_dev_stop(struct net_device *dev)
2998 struct amt_dev *amt = netdev_priv(dev);
2999 struct amt_tunnel_list *tunnel, *tmp;
3000 struct socket *sock;
3001 struct sk_buff *skb;
3004 cancel_delayed_work_sync(&amt->req_wq);
3005 cancel_delayed_work_sync(&amt->discovery_wq);
3006 cancel_delayed_work_sync(&amt->secret_wq);
3009 sock = rtnl_dereference(amt->sock);
3010 RCU_INIT_POINTER(amt->sock, NULL);
3013 udp_tunnel_sock_release(sock);
3015 cancel_work_sync(&amt->event_wq);
3016 for (i = 0; i < AMT_MAX_EVENTS; i++) {
3017 skb = amt->events[i].skb;
3020 amt->events[i].event = AMT_EVENT_NONE;
3021 amt->events[i].skb = NULL;
3024 amt->ready4 = false;
3025 amt->ready6 = false;
3029 list_for_each_entry_safe(tunnel, tmp, &amt->tunnel_list, list) {
3030 list_del_rcu(&tunnel->list);
3032 cancel_delayed_work_sync(&tunnel->gc_wq);
3033 amt_clear_groups(tunnel);
3034 kfree_rcu(tunnel, rcu);
3040 static const struct device_type amt_type = {
3044 static int amt_dev_init(struct net_device *dev)
3046 struct amt_dev *amt = netdev_priv(dev);
3050 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
3054 err = gro_cells_init(&amt->gro_cells, dev);
3056 free_percpu(dev->tstats);
3063 static void amt_dev_uninit(struct net_device *dev)
3065 struct amt_dev *amt = netdev_priv(dev);
3067 gro_cells_destroy(&amt->gro_cells);
3068 free_percpu(dev->tstats);
3071 static const struct net_device_ops amt_netdev_ops = {
3072 .ndo_init = amt_dev_init,
3073 .ndo_uninit = amt_dev_uninit,
3074 .ndo_open = amt_dev_open,
3075 .ndo_stop = amt_dev_stop,
3076 .ndo_start_xmit = amt_dev_xmit,
3077 .ndo_get_stats64 = dev_get_tstats64,
3080 static void amt_link_setup(struct net_device *dev)
3082 dev->netdev_ops = &amt_netdev_ops;
3083 dev->needs_free_netdev = true;
3084 SET_NETDEV_DEVTYPE(dev, &amt_type);
3085 dev->min_mtu = ETH_MIN_MTU;
3086 dev->max_mtu = ETH_MAX_MTU;
3087 dev->type = ARPHRD_NONE;
3088 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
3089 dev->hard_header_len = 0;
3091 dev->priv_flags |= IFF_NO_QUEUE;
3092 dev->features |= NETIF_F_LLTX;
3093 dev->features |= NETIF_F_GSO_SOFTWARE;
3094 dev->features |= NETIF_F_NETNS_LOCAL;
3095 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM;
3096 dev->hw_features |= NETIF_F_FRAGLIST | NETIF_F_RXCSUM;
3097 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
3098 eth_hw_addr_random(dev);
3099 eth_zero_addr(dev->broadcast);
3103 static const struct nla_policy amt_policy[IFLA_AMT_MAX + 1] = {
3104 [IFLA_AMT_MODE] = { .type = NLA_U32 },
3105 [IFLA_AMT_RELAY_PORT] = { .type = NLA_U16 },
3106 [IFLA_AMT_GATEWAY_PORT] = { .type = NLA_U16 },
3107 [IFLA_AMT_LINK] = { .type = NLA_U32 },
3108 [IFLA_AMT_LOCAL_IP] = { .len = sizeof_field(struct iphdr, daddr) },
3109 [IFLA_AMT_REMOTE_IP] = { .len = sizeof_field(struct iphdr, daddr) },
3110 [IFLA_AMT_DISCOVERY_IP] = { .len = sizeof_field(struct iphdr, daddr) },
3111 [IFLA_AMT_MAX_TUNNELS] = { .type = NLA_U32 },
3114 static int amt_validate(struct nlattr *tb[], struct nlattr *data[],
3115 struct netlink_ext_ack *extack)
3120 if (!data[IFLA_AMT_LINK]) {
3121 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_LINK],
3122 "Link attribute is required");
3126 if (!data[IFLA_AMT_MODE]) {
3127 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_MODE],
3128 "Mode attribute is required");
3132 if (nla_get_u32(data[IFLA_AMT_MODE]) > AMT_MODE_MAX) {
3133 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_MODE],
3134 "Mode attribute is not valid");
3138 if (!data[IFLA_AMT_LOCAL_IP]) {
3139 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_DISCOVERY_IP],
3140 "Local attribute is required");
3144 if (!data[IFLA_AMT_DISCOVERY_IP] &&
3145 nla_get_u32(data[IFLA_AMT_MODE]) == AMT_MODE_GATEWAY) {
3146 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_LOCAL_IP],
3147 "Discovery attribute is required");
3154 static int amt_newlink(struct net *net, struct net_device *dev,
3155 struct nlattr *tb[], struct nlattr *data[],
3156 struct netlink_ext_ack *extack)
3158 struct amt_dev *amt = netdev_priv(dev);
3162 amt->mode = nla_get_u32(data[IFLA_AMT_MODE]);
3164 if (data[IFLA_AMT_MAX_TUNNELS] &&
3165 nla_get_u32(data[IFLA_AMT_MAX_TUNNELS]))
3166 amt->max_tunnels = nla_get_u32(data[IFLA_AMT_MAX_TUNNELS]);
3168 amt->max_tunnels = AMT_MAX_TUNNELS;
3170 spin_lock_init(&amt->lock);
3171 amt->max_groups = AMT_MAX_GROUP;
3172 amt->max_sources = AMT_MAX_SOURCE;
3173 amt->hash_buckets = AMT_HSIZE;
3174 amt->nr_tunnels = 0;
3175 get_random_bytes(&amt->hash_seed, sizeof(amt->hash_seed));
3176 amt->stream_dev = dev_get_by_index(net,
3177 nla_get_u32(data[IFLA_AMT_LINK]));
3178 if (!amt->stream_dev) {
3179 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_LINK],
3180 "Can't find stream device");
3184 if (amt->stream_dev->type != ARPHRD_ETHER) {
3185 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_LINK],
3186 "Invalid stream device type");
3190 amt->local_ip = nla_get_in_addr(data[IFLA_AMT_LOCAL_IP]);
3191 if (ipv4_is_loopback(amt->local_ip) ||
3192 ipv4_is_zeronet(amt->local_ip) ||
3193 ipv4_is_multicast(amt->local_ip)) {
3194 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_LOCAL_IP],
3195 "Invalid Local address");
3199 if (data[IFLA_AMT_RELAY_PORT])
3200 amt->relay_port = nla_get_be16(data[IFLA_AMT_RELAY_PORT]);
3202 amt->relay_port = htons(IANA_AMT_UDP_PORT);
3204 if (data[IFLA_AMT_GATEWAY_PORT])
3205 amt->gw_port = nla_get_be16(data[IFLA_AMT_GATEWAY_PORT]);
3207 amt->gw_port = htons(IANA_AMT_UDP_PORT);
3209 if (!amt->relay_port) {
3210 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_DISCOVERY_IP],
3211 "relay port must not be 0");
3214 if (amt->mode == AMT_MODE_RELAY) {
3215 amt->qrv = READ_ONCE(amt->net->ipv4.sysctl_igmp_qrv);
3217 dev->needed_headroom = amt->stream_dev->needed_headroom +
3219 dev->mtu = amt->stream_dev->mtu - AMT_RELAY_HLEN;
3220 dev->max_mtu = dev->mtu;
3221 dev->min_mtu = ETH_MIN_MTU + AMT_RELAY_HLEN;
3223 if (!data[IFLA_AMT_DISCOVERY_IP]) {
3224 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_DISCOVERY_IP],
3225 "discovery must be set in gateway mode");
3228 if (!amt->gw_port) {
3229 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_DISCOVERY_IP],
3230 "gateway port must not be 0");
3234 amt->discovery_ip = nla_get_in_addr(data[IFLA_AMT_DISCOVERY_IP]);
3235 if (ipv4_is_loopback(amt->discovery_ip) ||
3236 ipv4_is_zeronet(amt->discovery_ip) ||
3237 ipv4_is_multicast(amt->discovery_ip)) {
3238 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_DISCOVERY_IP],
3239 "discovery must be unicast");
3243 dev->needed_headroom = amt->stream_dev->needed_headroom +
3245 dev->mtu = amt->stream_dev->mtu - AMT_GW_HLEN;
3246 dev->max_mtu = dev->mtu;
3247 dev->min_mtu = ETH_MIN_MTU + AMT_GW_HLEN;
3249 amt->qi = AMT_INIT_QUERY_INTERVAL;
3251 err = register_netdevice(dev);
3253 netdev_dbg(dev, "failed to register new netdev %d\n", err);
3257 err = netdev_upper_dev_link(amt->stream_dev, dev, extack);
3259 unregister_netdevice(dev);
3263 INIT_DELAYED_WORK(&amt->discovery_wq, amt_discovery_work);
3264 INIT_DELAYED_WORK(&amt->req_wq, amt_req_work);
3265 INIT_DELAYED_WORK(&amt->secret_wq, amt_secret_work);
3266 INIT_WORK(&amt->event_wq, amt_event_work);
3267 INIT_LIST_HEAD(&amt->tunnel_list);
3270 dev_put(amt->stream_dev);
3274 static void amt_dellink(struct net_device *dev, struct list_head *head)
3276 struct amt_dev *amt = netdev_priv(dev);
3278 unregister_netdevice_queue(dev, head);
3279 netdev_upper_dev_unlink(amt->stream_dev, dev);
3280 dev_put(amt->stream_dev);
3283 static size_t amt_get_size(const struct net_device *dev)
3285 return nla_total_size(sizeof(__u32)) + /* IFLA_AMT_MODE */
3286 nla_total_size(sizeof(__u16)) + /* IFLA_AMT_RELAY_PORT */
3287 nla_total_size(sizeof(__u16)) + /* IFLA_AMT_GATEWAY_PORT */
3288 nla_total_size(sizeof(__u32)) + /* IFLA_AMT_LINK */
3289 nla_total_size(sizeof(__u32)) + /* IFLA_MAX_TUNNELS */
3290 nla_total_size(sizeof(struct iphdr)) + /* IFLA_AMT_DISCOVERY_IP */
3291 nla_total_size(sizeof(struct iphdr)) + /* IFLA_AMT_REMOTE_IP */
3292 nla_total_size(sizeof(struct iphdr)); /* IFLA_AMT_LOCAL_IP */
3295 static int amt_fill_info(struct sk_buff *skb, const struct net_device *dev)
3297 struct amt_dev *amt = netdev_priv(dev);
3299 if (nla_put_u32(skb, IFLA_AMT_MODE, amt->mode))
3300 goto nla_put_failure;
3301 if (nla_put_be16(skb, IFLA_AMT_RELAY_PORT, amt->relay_port))
3302 goto nla_put_failure;
3303 if (nla_put_be16(skb, IFLA_AMT_GATEWAY_PORT, amt->gw_port))
3304 goto nla_put_failure;
3305 if (nla_put_u32(skb, IFLA_AMT_LINK, amt->stream_dev->ifindex))
3306 goto nla_put_failure;
3307 if (nla_put_in_addr(skb, IFLA_AMT_LOCAL_IP, amt->local_ip))
3308 goto nla_put_failure;
3309 if (nla_put_in_addr(skb, IFLA_AMT_DISCOVERY_IP, amt->discovery_ip))
3310 goto nla_put_failure;
3312 if (nla_put_in_addr(skb, IFLA_AMT_REMOTE_IP, amt->remote_ip))
3313 goto nla_put_failure;
3314 if (nla_put_u32(skb, IFLA_AMT_MAX_TUNNELS, amt->max_tunnels))
3315 goto nla_put_failure;
3323 static struct rtnl_link_ops amt_link_ops __read_mostly = {
3325 .maxtype = IFLA_AMT_MAX,
3326 .policy = amt_policy,
3327 .priv_size = sizeof(struct amt_dev),
3328 .setup = amt_link_setup,
3329 .validate = amt_validate,
3330 .newlink = amt_newlink,
3331 .dellink = amt_dellink,
3332 .get_size = amt_get_size,
3333 .fill_info = amt_fill_info,
3336 static struct net_device *amt_lookup_upper_dev(struct net_device *dev)
3338 struct net_device *upper_dev;
3339 struct amt_dev *amt;
3341 for_each_netdev(dev_net(dev), upper_dev) {
3342 if (netif_is_amt(upper_dev)) {
3343 amt = netdev_priv(upper_dev);
3344 if (amt->stream_dev == dev)
3352 static int amt_device_event(struct notifier_block *unused,
3353 unsigned long event, void *ptr)
3355 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3356 struct net_device *upper_dev;
3357 struct amt_dev *amt;
3361 upper_dev = amt_lookup_upper_dev(dev);
3364 amt = netdev_priv(upper_dev);
3367 case NETDEV_UNREGISTER:
3368 amt_dellink(amt->dev, &list);
3369 unregister_netdevice_many(&list);
3371 case NETDEV_CHANGEMTU:
3372 if (amt->mode == AMT_MODE_RELAY)
3373 new_mtu = dev->mtu - AMT_RELAY_HLEN;
3375 new_mtu = dev->mtu - AMT_GW_HLEN;
3377 dev_set_mtu(amt->dev, new_mtu);
3384 static struct notifier_block amt_notifier_block __read_mostly = {
3385 .notifier_call = amt_device_event,
3388 static int __init amt_init(void)
3392 err = register_netdevice_notifier(&amt_notifier_block);
3396 err = rtnl_link_register(&amt_link_ops);
3398 goto unregister_notifier;
3400 amt_wq = alloc_workqueue("amt", WQ_UNBOUND, 0);
3403 goto rtnl_unregister;
3406 spin_lock_init(&source_gc_lock);
3407 spin_lock_bh(&source_gc_lock);
3408 INIT_DELAYED_WORK(&source_gc_wq, amt_source_gc_work);
3409 mod_delayed_work(amt_wq, &source_gc_wq,
3410 msecs_to_jiffies(AMT_GC_INTERVAL));
3411 spin_unlock_bh(&source_gc_lock);
3416 rtnl_link_unregister(&amt_link_ops);
3417 unregister_notifier:
3418 unregister_netdevice_notifier(&amt_notifier_block);
3420 pr_err("error loading AMT module loaded\n");
3423 late_initcall(amt_init);
3425 static void __exit amt_fini(void)
3427 rtnl_link_unregister(&amt_link_ops);
3428 unregister_netdevice_notifier(&amt_notifier_block);
3429 cancel_delayed_work_sync(&source_gc_wq);
3430 __amt_source_gc_work();
3431 destroy_workqueue(amt_wq);
3433 module_exit(amt_fini);
3435 MODULE_LICENSE("GPL");
3436 MODULE_AUTHOR("Taehee Yoo <ap420073@gmail.com>");
3437 MODULE_ALIAS_RTNL_LINK("amt");