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 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_gw_status(struct amt_dev *amt, enum amt_status status,
606 spin_lock_bh(&amt->lock);
607 __amt_update_gw_status(amt, status, validate);
608 spin_unlock_bh(&amt->lock);
611 static void amt_update_relay_status(struct amt_tunnel_list *tunnel,
612 enum amt_status status, bool validate)
614 spin_lock_bh(&tunnel->lock);
615 __amt_update_relay_status(tunnel, status, validate);
616 spin_unlock_bh(&tunnel->lock);
619 static void amt_send_discovery(struct amt_dev *amt)
621 struct amt_header_discovery *amtd;
622 int hlen, tlen, offset;
633 sock = rcu_dereference(amt->sock);
637 if (!netif_running(amt->stream_dev) || !netif_running(amt->dev))
640 rt = ip_route_output_ports(amt->net, &fl4, sock->sk,
641 amt->discovery_ip, amt->local_ip,
642 amt->gw_port, amt->relay_port,
644 amt->stream_dev->ifindex);
646 amt->dev->stats.tx_errors++;
650 hlen = LL_RESERVED_SPACE(amt->dev);
651 tlen = amt->dev->needed_tailroom;
652 len = hlen + tlen + sizeof(*iph) + sizeof(*udph) + sizeof(*amtd);
653 skb = netdev_alloc_skb_ip_align(amt->dev, len);
656 amt->dev->stats.tx_errors++;
660 skb->priority = TC_PRIO_CONTROL;
661 skb_dst_set(skb, &rt->dst);
663 len = sizeof(*iph) + sizeof(*udph) + sizeof(*amtd);
664 skb_reset_network_header(skb);
666 amtd = skb_pull(skb, sizeof(*iph) + sizeof(*udph));
668 amtd->type = AMT_MSG_DISCOVERY;
670 amtd->nonce = amt->nonce;
671 skb_push(skb, sizeof(*udph));
672 skb_reset_transport_header(skb);
674 udph->source = amt->gw_port;
675 udph->dest = amt->relay_port;
676 udph->len = htons(sizeof(*udph) + sizeof(*amtd));
678 offset = skb_transport_offset(skb);
679 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
680 udph->check = csum_tcpudp_magic(amt->local_ip, amt->discovery_ip,
681 sizeof(*udph) + sizeof(*amtd),
682 IPPROTO_UDP, skb->csum);
684 skb_push(skb, sizeof(*iph));
687 iph->ihl = (sizeof(struct iphdr)) >> 2;
690 iph->ttl = ip4_dst_hoplimit(&rt->dst);
691 iph->daddr = amt->discovery_ip;
692 iph->saddr = amt->local_ip;
693 iph->protocol = IPPROTO_UDP;
694 iph->tot_len = htons(len);
696 skb->ip_summed = CHECKSUM_NONE;
697 ip_select_ident(amt->net, skb, NULL);
699 err = ip_local_out(amt->net, sock->sk, skb);
700 if (unlikely(net_xmit_eval(err)))
701 amt->dev->stats.tx_errors++;
703 spin_lock_bh(&amt->lock);
704 __amt_update_gw_status(amt, AMT_STATUS_SENT_DISCOVERY, true);
705 spin_unlock_bh(&amt->lock);
710 static void amt_send_request(struct amt_dev *amt, bool v6)
712 struct amt_header_request *amtrh;
713 int hlen, tlen, offset;
724 sock = rcu_dereference(amt->sock);
728 if (!netif_running(amt->stream_dev) || !netif_running(amt->dev))
731 rt = ip_route_output_ports(amt->net, &fl4, sock->sk,
732 amt->remote_ip, amt->local_ip,
733 amt->gw_port, amt->relay_port,
735 amt->stream_dev->ifindex);
737 amt->dev->stats.tx_errors++;
741 hlen = LL_RESERVED_SPACE(amt->dev);
742 tlen = amt->dev->needed_tailroom;
743 len = hlen + tlen + sizeof(*iph) + sizeof(*udph) + sizeof(*amtrh);
744 skb = netdev_alloc_skb_ip_align(amt->dev, len);
747 amt->dev->stats.tx_errors++;
751 skb->priority = TC_PRIO_CONTROL;
752 skb_dst_set(skb, &rt->dst);
754 len = sizeof(*iph) + sizeof(*udph) + sizeof(*amtrh);
755 skb_reset_network_header(skb);
757 amtrh = skb_pull(skb, sizeof(*iph) + sizeof(*udph));
759 amtrh->type = AMT_MSG_REQUEST;
760 amtrh->reserved1 = 0;
762 amtrh->reserved2 = 0;
763 amtrh->nonce = amt->nonce;
764 skb_push(skb, sizeof(*udph));
765 skb_reset_transport_header(skb);
767 udph->source = amt->gw_port;
768 udph->dest = amt->relay_port;
769 udph->len = htons(sizeof(*amtrh) + sizeof(*udph));
771 offset = skb_transport_offset(skb);
772 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
773 udph->check = csum_tcpudp_magic(amt->local_ip, amt->remote_ip,
774 sizeof(*udph) + sizeof(*amtrh),
775 IPPROTO_UDP, skb->csum);
777 skb_push(skb, sizeof(*iph));
780 iph->ihl = (sizeof(struct iphdr)) >> 2;
783 iph->ttl = ip4_dst_hoplimit(&rt->dst);
784 iph->daddr = amt->remote_ip;
785 iph->saddr = amt->local_ip;
786 iph->protocol = IPPROTO_UDP;
787 iph->tot_len = htons(len);
789 skb->ip_summed = CHECKSUM_NONE;
790 ip_select_ident(amt->net, skb, NULL);
792 err = ip_local_out(amt->net, sock->sk, skb);
793 if (unlikely(net_xmit_eval(err)))
794 amt->dev->stats.tx_errors++;
800 static void amt_send_igmp_gq(struct amt_dev *amt,
801 struct amt_tunnel_list *tunnel)
805 skb = amt_build_igmp_gq(amt);
809 amt_skb_cb(skb)->tunnel = tunnel;
813 #if IS_ENABLED(CONFIG_IPV6)
814 static struct sk_buff *amt_build_mld_gq(struct amt_dev *amt)
816 u8 ra[AMT_IP6HDR_OPTS] = { IPPROTO_ICMPV6, 0, IPV6_TLV_ROUTERALERT,
817 2, 0, 0, IPV6_TLV_PAD1, IPV6_TLV_PAD1 };
818 int hlen = LL_RESERVED_SPACE(amt->dev);
819 int tlen = amt->dev->needed_tailroom;
820 struct mld2_query *mld2q;
821 void *csum_start = NULL;
822 struct ipv6hdr *ip6h;
827 len = hlen + tlen + sizeof(*ip6h) + sizeof(ra) + sizeof(*mld2q);
828 skb = netdev_alloc_skb_ip_align(amt->dev, len);
832 skb_reserve(skb, hlen);
833 skb_push(skb, sizeof(*eth));
834 skb_reset_mac_header(skb);
836 skb->priority = TC_PRIO_CONTROL;
837 skb->protocol = htons(ETH_P_IPV6);
838 skb_put_zero(skb, sizeof(*ip6h));
839 skb_put_data(skb, ra, sizeof(ra));
840 skb_put_zero(skb, sizeof(*mld2q));
841 skb_pull(skb, sizeof(*eth));
842 skb_reset_network_header(skb);
843 ip6h = ipv6_hdr(skb);
844 ip6h->payload_len = htons(sizeof(ra) + sizeof(*mld2q));
845 ip6h->nexthdr = NEXTHDR_HOP;
847 ip6h->daddr = mld2_all_node;
848 ip6_flow_hdr(ip6h, 0, 0);
850 if (ipv6_dev_get_saddr(amt->net, amt->dev, &ip6h->daddr, 0,
852 amt->dev->stats.tx_errors++;
857 eth->h_proto = htons(ETH_P_IPV6);
858 ether_addr_copy(eth->h_source, amt->dev->dev_addr);
859 ipv6_eth_mc_map(&mld2_all_node, eth->h_dest);
861 skb_pull(skb, sizeof(*ip6h) + sizeof(ra));
862 skb_reset_transport_header(skb);
863 mld2q = (struct mld2_query *)icmp6_hdr(skb);
864 mld2q->mld2q_mrc = htons(1);
865 mld2q->mld2q_type = ICMPV6_MGM_QUERY;
866 mld2q->mld2q_code = 0;
867 mld2q->mld2q_cksum = 0;
868 mld2q->mld2q_resv1 = 0;
869 mld2q->mld2q_resv2 = 0;
870 mld2q->mld2q_suppress = 0;
871 mld2q->mld2q_qrv = amt->qrv;
872 mld2q->mld2q_nsrcs = 0;
873 mld2q->mld2q_qqic = amt->qi;
874 csum_start = (void *)mld2q;
875 mld2q->mld2q_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
878 csum_partial(csum_start,
881 skb->ip_summed = CHECKSUM_NONE;
882 skb_push(skb, sizeof(*eth) + sizeof(*ip6h) + sizeof(ra));
886 static void amt_send_mld_gq(struct amt_dev *amt, struct amt_tunnel_list *tunnel)
890 skb = amt_build_mld_gq(amt);
894 amt_skb_cb(skb)->tunnel = tunnel;
898 static void amt_send_mld_gq(struct amt_dev *amt, struct amt_tunnel_list *tunnel)
903 static bool amt_queue_event(struct amt_dev *amt, enum amt_event event,
908 spin_lock_bh(&amt->lock);
909 if (amt->nr_events >= AMT_MAX_EVENTS) {
910 spin_unlock_bh(&amt->lock);
914 index = (amt->event_idx + amt->nr_events) % AMT_MAX_EVENTS;
915 amt->events[index].event = event;
916 amt->events[index].skb = skb;
918 amt->event_idx %= AMT_MAX_EVENTS;
919 queue_work(amt_wq, &amt->event_wq);
920 spin_unlock_bh(&amt->lock);
925 static void amt_secret_work(struct work_struct *work)
927 struct amt_dev *amt = container_of(to_delayed_work(work),
931 spin_lock_bh(&amt->lock);
932 get_random_bytes(&amt->key, sizeof(siphash_key_t));
933 spin_unlock_bh(&amt->lock);
934 mod_delayed_work(amt_wq, &amt->secret_wq,
935 msecs_to_jiffies(AMT_SECRET_TIMEOUT));
938 static void amt_event_send_discovery(struct amt_dev *amt)
940 spin_lock_bh(&amt->lock);
941 if (amt->status > AMT_STATUS_SENT_DISCOVERY)
943 get_random_bytes(&amt->nonce, sizeof(__be32));
944 spin_unlock_bh(&amt->lock);
946 amt_send_discovery(amt);
947 spin_lock_bh(&amt->lock);
949 mod_delayed_work(amt_wq, &amt->discovery_wq,
950 msecs_to_jiffies(AMT_DISCOVERY_TIMEOUT));
951 spin_unlock_bh(&amt->lock);
954 static void amt_discovery_work(struct work_struct *work)
956 struct amt_dev *amt = container_of(to_delayed_work(work),
960 if (amt_queue_event(amt, AMT_EVENT_SEND_DISCOVERY, NULL))
961 mod_delayed_work(amt_wq, &amt->discovery_wq,
962 msecs_to_jiffies(AMT_DISCOVERY_TIMEOUT));
965 static void amt_event_send_request(struct amt_dev *amt)
969 spin_lock_bh(&amt->lock);
970 if (amt->status < AMT_STATUS_RECEIVED_ADVERTISEMENT)
973 if (amt->req_cnt > AMT_MAX_REQ_COUNT) {
974 netdev_dbg(amt->dev, "Gateway is not ready");
975 amt->qi = AMT_INIT_REQ_TIMEOUT;
979 __amt_update_gw_status(amt, AMT_STATUS_INIT, false);
983 spin_unlock_bh(&amt->lock);
985 amt_send_request(amt, false);
986 amt_send_request(amt, true);
987 spin_lock_bh(&amt->lock);
988 __amt_update_gw_status(amt, AMT_STATUS_SENT_REQUEST, true);
991 exp = min_t(u32, (1 * (1 << amt->req_cnt)), AMT_MAX_REQ_TIMEOUT);
992 mod_delayed_work(amt_wq, &amt->req_wq, msecs_to_jiffies(exp * 1000));
993 spin_unlock_bh(&amt->lock);
996 static void amt_req_work(struct work_struct *work)
998 struct amt_dev *amt = container_of(to_delayed_work(work),
1002 if (amt_queue_event(amt, AMT_EVENT_SEND_REQUEST, NULL))
1003 mod_delayed_work(amt_wq, &amt->req_wq,
1004 msecs_to_jiffies(100));
1007 static bool amt_send_membership_update(struct amt_dev *amt,
1008 struct sk_buff *skb,
1011 struct amt_header_membership_update *amtmu;
1012 struct socket *sock;
1018 sock = rcu_dereference_bh(amt->sock);
1022 err = skb_cow_head(skb, LL_RESERVED_SPACE(amt->dev) + sizeof(*amtmu) +
1023 sizeof(*iph) + sizeof(struct udphdr));
1027 skb_reset_inner_headers(skb);
1028 memset(&fl4, 0, sizeof(struct flowi4));
1029 fl4.flowi4_oif = amt->stream_dev->ifindex;
1030 fl4.daddr = amt->remote_ip;
1031 fl4.saddr = amt->local_ip;
1032 fl4.flowi4_tos = AMT_TOS;
1033 fl4.flowi4_proto = IPPROTO_UDP;
1034 rt = ip_route_output_key(amt->net, &fl4);
1036 netdev_dbg(amt->dev, "no route to %pI4\n", &amt->remote_ip);
1040 amtmu = skb_push(skb, sizeof(*amtmu));
1042 amtmu->type = AMT_MSG_MEMBERSHIP_UPDATE;
1043 amtmu->reserved = 0;
1044 amtmu->nonce = amt->nonce;
1045 amtmu->response_mac = amt->mac;
1048 skb_set_inner_protocol(skb, htons(ETH_P_IP));
1050 skb_set_inner_protocol(skb, htons(ETH_P_IPV6));
1051 udp_tunnel_xmit_skb(rt, sock->sk, skb,
1055 ip4_dst_hoplimit(&rt->dst),
1061 amt_update_gw_status(amt, AMT_STATUS_SENT_UPDATE, true);
1065 static void amt_send_multicast_data(struct amt_dev *amt,
1066 const struct sk_buff *oskb,
1067 struct amt_tunnel_list *tunnel,
1070 struct amt_header_mcast_data *amtmd;
1071 struct socket *sock;
1072 struct sk_buff *skb;
1077 sock = rcu_dereference_bh(amt->sock);
1081 skb = skb_copy_expand(oskb, sizeof(*amtmd) + sizeof(*iph) +
1082 sizeof(struct udphdr), 0, GFP_ATOMIC);
1086 skb_reset_inner_headers(skb);
1087 memset(&fl4, 0, sizeof(struct flowi4));
1088 fl4.flowi4_oif = amt->stream_dev->ifindex;
1089 fl4.daddr = tunnel->ip4;
1090 fl4.saddr = amt->local_ip;
1091 fl4.flowi4_proto = IPPROTO_UDP;
1092 rt = ip_route_output_key(amt->net, &fl4);
1094 netdev_dbg(amt->dev, "no route to %pI4\n", &tunnel->ip4);
1099 amtmd = skb_push(skb, sizeof(*amtmd));
1101 amtmd->reserved = 0;
1102 amtmd->type = AMT_MSG_MULTICAST_DATA;
1105 skb_set_inner_protocol(skb, htons(ETH_P_IP));
1107 skb_set_inner_protocol(skb, htons(ETH_P_IPV6));
1108 udp_tunnel_xmit_skb(rt, sock->sk, skb,
1112 ip4_dst_hoplimit(&rt->dst),
1115 tunnel->source_port,
1120 static bool amt_send_membership_query(struct amt_dev *amt,
1121 struct sk_buff *skb,
1122 struct amt_tunnel_list *tunnel,
1125 struct amt_header_membership_query *amtmq;
1126 struct socket *sock;
1131 sock = rcu_dereference_bh(amt->sock);
1135 err = skb_cow_head(skb, LL_RESERVED_SPACE(amt->dev) + sizeof(*amtmq) +
1136 sizeof(struct iphdr) + sizeof(struct udphdr));
1140 skb_reset_inner_headers(skb);
1141 memset(&fl4, 0, sizeof(struct flowi4));
1142 fl4.flowi4_oif = amt->stream_dev->ifindex;
1143 fl4.daddr = tunnel->ip4;
1144 fl4.saddr = amt->local_ip;
1145 fl4.flowi4_tos = AMT_TOS;
1146 fl4.flowi4_proto = IPPROTO_UDP;
1147 rt = ip_route_output_key(amt->net, &fl4);
1149 netdev_dbg(amt->dev, "no route to %pI4\n", &tunnel->ip4);
1153 amtmq = skb_push(skb, sizeof(*amtmq));
1155 amtmq->type = AMT_MSG_MEMBERSHIP_QUERY;
1156 amtmq->reserved = 0;
1159 amtmq->nonce = tunnel->nonce;
1160 amtmq->response_mac = tunnel->mac;
1163 skb_set_inner_protocol(skb, htons(ETH_P_IP));
1165 skb_set_inner_protocol(skb, htons(ETH_P_IPV6));
1166 udp_tunnel_xmit_skb(rt, sock->sk, skb,
1170 ip4_dst_hoplimit(&rt->dst),
1173 tunnel->source_port,
1176 amt_update_relay_status(tunnel, AMT_STATUS_SENT_QUERY, true);
1180 static netdev_tx_t amt_dev_xmit(struct sk_buff *skb, struct net_device *dev)
1182 struct amt_dev *amt = netdev_priv(dev);
1183 struct amt_tunnel_list *tunnel;
1184 struct amt_group_node *gnode;
1185 union amt_addr group = {0,};
1186 #if IS_ENABLED(CONFIG_IPV6)
1187 struct ipv6hdr *ip6h;
1188 struct mld_msg *mld;
1190 bool report = false;
1199 if (iph->version == 4) {
1200 if (!ipv4_is_multicast(iph->daddr))
1203 if (!ip_mc_check_igmp(skb)) {
1206 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1207 case IGMP_HOST_MEMBERSHIP_REPORT:
1210 case IGMP_HOST_MEMBERSHIP_QUERY:
1220 group.ip4 = iph->daddr;
1221 #if IS_ENABLED(CONFIG_IPV6)
1222 } else if (iph->version == 6) {
1223 ip6h = ipv6_hdr(skb);
1224 if (!ipv6_addr_is_multicast(&ip6h->daddr))
1227 if (!ipv6_mc_check_mld(skb)) {
1228 mld = (struct mld_msg *)skb_transport_header(skb);
1229 switch (mld->mld_type) {
1230 case ICMPV6_MGM_REPORT:
1231 case ICMPV6_MLD2_REPORT:
1234 case ICMPV6_MGM_QUERY:
1244 group.ip6 = ip6h->daddr;
1247 dev->stats.tx_errors++;
1251 if (!pskb_may_pull(skb, sizeof(struct ethhdr)))
1254 skb_pull(skb, sizeof(struct ethhdr));
1256 if (amt->mode == AMT_MODE_GATEWAY) {
1257 /* Gateway only passes IGMP/MLD packets */
1260 if ((!v6 && !amt->ready4) || (v6 && !amt->ready6))
1262 if (amt_send_membership_update(amt, skb, v6))
1265 } else if (amt->mode == AMT_MODE_RELAY) {
1267 tunnel = amt_skb_cb(skb)->tunnel;
1273 /* Do not forward unexpected query */
1274 if (amt_send_membership_query(amt, skb, tunnel, v6))
1281 list_for_each_entry_rcu(tunnel, &amt->tunnel_list, list) {
1282 hash = amt_group_hash(tunnel, &group);
1283 hlist_for_each_entry_rcu(gnode, &tunnel->groups[hash],
1286 if (gnode->group_addr.ip4 == iph->daddr)
1288 #if IS_ENABLED(CONFIG_IPV6)
1290 if (ipv6_addr_equal(&gnode->group_addr.ip6,
1298 amt_send_multicast_data(amt, skb, tunnel, v6);
1303 return NETDEV_TX_OK;
1307 dev->stats.tx_dropped++;
1308 return NETDEV_TX_OK;
1311 static int amt_parse_type(struct sk_buff *skb)
1313 struct amt_header *amth;
1315 if (!pskb_may_pull(skb, sizeof(struct udphdr) +
1316 sizeof(struct amt_header)))
1319 amth = (struct amt_header *)(udp_hdr(skb) + 1);
1321 if (amth->version != 0)
1324 if (amth->type >= __AMT_MSG_MAX || !amth->type)
1329 static void amt_clear_groups(struct amt_tunnel_list *tunnel)
1331 struct amt_dev *amt = tunnel->amt;
1332 struct amt_group_node *gnode;
1333 struct hlist_node *t;
1336 spin_lock_bh(&tunnel->lock);
1338 for (i = 0; i < amt->hash_buckets; i++)
1339 hlist_for_each_entry_safe(gnode, t, &tunnel->groups[i], node)
1340 amt_del_group(amt, gnode);
1342 spin_unlock_bh(&tunnel->lock);
1345 static void amt_tunnel_expire(struct work_struct *work)
1347 struct amt_tunnel_list *tunnel = container_of(to_delayed_work(work),
1348 struct amt_tunnel_list,
1350 struct amt_dev *amt = tunnel->amt;
1352 spin_lock_bh(&amt->lock);
1354 list_del_rcu(&tunnel->list);
1356 amt_clear_groups(tunnel);
1358 spin_unlock_bh(&amt->lock);
1359 kfree_rcu(tunnel, rcu);
1362 static void amt_cleanup_srcs(struct amt_dev *amt,
1363 struct amt_tunnel_list *tunnel,
1364 struct amt_group_node *gnode)
1366 struct amt_source_node *snode;
1367 struct hlist_node *t;
1370 /* Delete old sources */
1371 for (i = 0; i < amt->hash_buckets; i++) {
1372 hlist_for_each_entry_safe(snode, t, &gnode->sources[i], node) {
1373 if (snode->flags == AMT_SOURCE_OLD)
1374 amt_destroy_source(snode);
1378 /* switch from new to old */
1379 for (i = 0; i < amt->hash_buckets; i++) {
1380 hlist_for_each_entry_rcu(snode, &gnode->sources[i], node) {
1381 snode->flags = AMT_SOURCE_OLD;
1383 netdev_dbg(snode->gnode->amt->dev,
1384 "Add source as OLD %pI4 from %pI4\n",
1385 &snode->source_addr.ip4,
1386 &gnode->group_addr.ip4);
1387 #if IS_ENABLED(CONFIG_IPV6)
1389 netdev_dbg(snode->gnode->amt->dev,
1390 "Add source as OLD %pI6 from %pI6\n",
1391 &snode->source_addr.ip6,
1392 &gnode->group_addr.ip6);
1398 static void amt_add_srcs(struct amt_dev *amt, struct amt_tunnel_list *tunnel,
1399 struct amt_group_node *gnode, void *grec,
1402 struct igmpv3_grec *igmp_grec;
1403 struct amt_source_node *snode;
1404 #if IS_ENABLED(CONFIG_IPV6)
1405 struct mld2_grec *mld_grec;
1407 union amt_addr src = {0,};
1413 igmp_grec = (struct igmpv3_grec *)grec;
1414 nsrcs = ntohs(igmp_grec->grec_nsrcs);
1416 #if IS_ENABLED(CONFIG_IPV6)
1417 mld_grec = (struct mld2_grec *)grec;
1418 nsrcs = ntohs(mld_grec->grec_nsrcs);
1423 for (i = 0; i < nsrcs; i++) {
1424 if (tunnel->nr_sources >= amt->max_sources)
1427 src.ip4 = igmp_grec->grec_src[i];
1428 #if IS_ENABLED(CONFIG_IPV6)
1430 memcpy(&src.ip6, &mld_grec->grec_src[i],
1431 sizeof(struct in6_addr));
1433 if (amt_lookup_src(tunnel, gnode, AMT_FILTER_ALL, &src))
1436 snode = amt_alloc_snode(gnode, &src);
1438 hash = amt_source_hash(tunnel, &snode->source_addr);
1439 hlist_add_head_rcu(&snode->node, &gnode->sources[hash]);
1440 tunnel->nr_sources++;
1441 gnode->nr_sources++;
1444 netdev_dbg(snode->gnode->amt->dev,
1445 "Add source as NEW %pI4 from %pI4\n",
1446 &snode->source_addr.ip4,
1447 &gnode->group_addr.ip4);
1448 #if IS_ENABLED(CONFIG_IPV6)
1450 netdev_dbg(snode->gnode->amt->dev,
1451 "Add source as NEW %pI6 from %pI6\n",
1452 &snode->source_addr.ip6,
1453 &gnode->group_addr.ip6);
1459 /* Router State Report Rec'd New Router State
1460 * ------------ ------------ ----------------
1461 * EXCLUDE (X,Y) IS_IN (A) EXCLUDE (X+A,Y-A)
1463 * -----------+-----------+-----------+
1465 * -----------+-----------+-----------+
1467 * -----------+-----------+-----------+
1469 * -----------+-----------+-----------+
1471 * -----------+-----------+-----------+
1473 * a) Received sources are NONE/NEW
1474 * b) All NONE will be deleted by amt_cleanup_srcs().
1475 * c) All OLD will be deleted by amt_cleanup_srcs().
1476 * d) After delete, NEW source will be switched to OLD.
1478 static void amt_lookup_act_srcs(struct amt_tunnel_list *tunnel,
1479 struct amt_group_node *gnode,
1482 enum amt_filter filter,
1486 struct amt_dev *amt = tunnel->amt;
1487 struct amt_source_node *snode;
1488 struct igmpv3_grec *igmp_grec;
1489 #if IS_ENABLED(CONFIG_IPV6)
1490 struct mld2_grec *mld_grec;
1492 union amt_addr src = {0,};
1493 struct hlist_node *t;
1498 igmp_grec = (struct igmpv3_grec *)grec;
1499 nsrcs = ntohs(igmp_grec->grec_nsrcs);
1501 #if IS_ENABLED(CONFIG_IPV6)
1502 mld_grec = (struct mld2_grec *)grec;
1503 nsrcs = ntohs(mld_grec->grec_nsrcs);
1509 memset(&src, 0, sizeof(union amt_addr));
1513 for (i = 0; i < nsrcs; i++) {
1515 src.ip4 = igmp_grec->grec_src[i];
1516 #if IS_ENABLED(CONFIG_IPV6)
1518 memcpy(&src.ip6, &mld_grec->grec_src[i],
1519 sizeof(struct in6_addr));
1521 snode = amt_lookup_src(tunnel, gnode, filter, &src);
1524 amt_act_src(tunnel, gnode, snode, act);
1529 for (i = 0; i < amt->hash_buckets; i++) {
1530 hlist_for_each_entry_safe(snode, t, &gnode->sources[i],
1532 if (amt_status_filter(snode, filter))
1533 amt_act_src(tunnel, gnode, snode, act);
1536 for (i = 0; i < nsrcs; i++) {
1538 src.ip4 = igmp_grec->grec_src[i];
1539 #if IS_ENABLED(CONFIG_IPV6)
1541 memcpy(&src.ip6, &mld_grec->grec_src[i],
1542 sizeof(struct in6_addr));
1544 snode = amt_lookup_src(tunnel, gnode, filter, &src);
1547 amt_act_src(tunnel, gnode, snode, act);
1552 for (i = 0; i < amt->hash_buckets; i++) {
1553 hlist_for_each_entry_safe(snode, t, &gnode->sources[i],
1555 if (!amt_status_filter(snode, filter))
1557 for (j = 0; j < nsrcs; j++) {
1559 src.ip4 = igmp_grec->grec_src[j];
1560 #if IS_ENABLED(CONFIG_IPV6)
1563 &mld_grec->grec_src[j],
1564 sizeof(struct in6_addr));
1566 if (amt_addr_equal(&snode->source_addr,
1570 amt_act_src(tunnel, gnode, snode, act);
1576 case AMT_OPS_SUB_REV:
1578 for (i = 0; i < nsrcs; i++) {
1580 src.ip4 = igmp_grec->grec_src[i];
1581 #if IS_ENABLED(CONFIG_IPV6)
1583 memcpy(&src.ip6, &mld_grec->grec_src[i],
1584 sizeof(struct in6_addr));
1586 snode = amt_lookup_src(tunnel, gnode, AMT_FILTER_ALL,
1589 snode = amt_lookup_src(tunnel, gnode,
1592 amt_act_src(tunnel, gnode, snode, act);
1597 netdev_dbg(amt->dev, "Invalid type\n");
1602 static void amt_mcast_is_in_handler(struct amt_dev *amt,
1603 struct amt_tunnel_list *tunnel,
1604 struct amt_group_node *gnode,
1605 void *grec, void *zero_grec, bool v6)
1607 if (gnode->filter_mode == MCAST_INCLUDE) {
1608 /* Router State Report Rec'd New Router State Actions
1609 * ------------ ------------ ---------------- -------
1610 * INCLUDE (A) IS_IN (B) INCLUDE (A+B) (B)=GMI
1612 /* Update IS_IN (B) as FWD/NEW */
1613 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
1614 AMT_FILTER_NONE_NEW,
1615 AMT_ACT_STATUS_FWD_NEW,
1617 /* Update INCLUDE (A) as NEW */
1618 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
1620 AMT_ACT_STATUS_FWD_NEW,
1623 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
1629 * ------------ ------------ ---------------- -------
1630 * EXCLUDE (X,Y) IS_IN (A) EXCLUDE (X+A,Y-A) (A)=GMI
1632 /* Update (A) in (X, Y) as NONE/NEW */
1633 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
1635 AMT_ACT_STATUS_NONE_NEW,
1637 /* Update FWD/OLD as FWD/NEW */
1638 amt_lookup_act_srcs(tunnel, gnode, zero_grec, AMT_OPS_UNI,
1640 AMT_ACT_STATUS_FWD_NEW,
1642 /* Update IS_IN (A) as FWD/NEW */
1643 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
1644 AMT_FILTER_NONE_NEW,
1645 AMT_ACT_STATUS_FWD_NEW,
1647 /* Update EXCLUDE (, Y-A) as D_FWD_NEW */
1648 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB,
1650 AMT_ACT_STATUS_D_FWD_NEW,
1655 static void amt_mcast_is_ex_handler(struct amt_dev *amt,
1656 struct amt_tunnel_list *tunnel,
1657 struct amt_group_node *gnode,
1658 void *grec, void *zero_grec, bool v6)
1660 if (gnode->filter_mode == MCAST_INCLUDE) {
1661 /* Router State Report Rec'd New Router State Actions
1662 * ------------ ------------ ---------------- -------
1663 * INCLUDE (A) IS_EX (B) EXCLUDE (A*B,B-A) (B-A)=0
1667 /* EXCLUDE(A*B, ) */
1668 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
1670 AMT_ACT_STATUS_FWD_NEW,
1672 /* EXCLUDE(, B-A) */
1673 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV,
1675 AMT_ACT_STATUS_D_FWD_NEW,
1678 amt_lookup_act_srcs(tunnel, gnode, zero_grec, AMT_OPS_UNI,
1679 AMT_FILTER_D_FWD_NEW,
1682 /* Group Timer=GMI */
1683 if (!mod_delayed_work(amt_wq, &gnode->group_timer,
1684 msecs_to_jiffies(amt_gmi(amt))))
1686 gnode->filter_mode = MCAST_EXCLUDE;
1687 /* Delete (A-B) will be worked by amt_cleanup_srcs(). */
1689 /* Router State Report Rec'd New Router State Actions
1690 * ------------ ------------ ---------------- -------
1691 * EXCLUDE (X,Y) IS_EX (A) EXCLUDE (A-Y,Y*A) (A-X-Y)=GMI
1696 /* EXCLUDE (A-Y, ) */
1697 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV,
1699 AMT_ACT_STATUS_FWD_NEW,
1701 /* EXCLUDE (, Y*A ) */
1702 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
1704 AMT_ACT_STATUS_D_FWD_NEW,
1707 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV,
1708 AMT_FILTER_BOTH_NEW,
1711 /* Group Timer=GMI */
1712 if (!mod_delayed_work(amt_wq, &gnode->group_timer,
1713 msecs_to_jiffies(amt_gmi(amt))))
1715 /* Delete (X-A), (Y-A) will be worked by amt_cleanup_srcs(). */
1719 static void amt_mcast_to_in_handler(struct amt_dev *amt,
1720 struct amt_tunnel_list *tunnel,
1721 struct amt_group_node *gnode,
1722 void *grec, void *zero_grec, bool v6)
1724 if (gnode->filter_mode == MCAST_INCLUDE) {
1725 /* Router State Report Rec'd New Router State Actions
1726 * ------------ ------------ ---------------- -------
1727 * INCLUDE (A) TO_IN (B) INCLUDE (A+B) (B)=GMI
1730 /* Update TO_IN (B) sources as FWD/NEW */
1731 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
1732 AMT_FILTER_NONE_NEW,
1733 AMT_ACT_STATUS_FWD_NEW,
1735 /* Update INCLUDE (A) sources as NEW */
1736 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
1738 AMT_ACT_STATUS_FWD_NEW,
1741 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
1746 /* Router State Report Rec'd New Router State Actions
1747 * ------------ ------------ ---------------- -------
1748 * EXCLUDE (X,Y) TO_IN (A) EXCLUDE (X+A,Y-A) (A)=GMI
1752 /* Update TO_IN (A) sources as FWD/NEW */
1753 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
1754 AMT_FILTER_NONE_NEW,
1755 AMT_ACT_STATUS_FWD_NEW,
1757 /* Update EXCLUDE(X,) sources as FWD/NEW */
1758 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
1760 AMT_ACT_STATUS_FWD_NEW,
1763 * (A) are already switched to FWD_NEW.
1764 * So, D_FWD/OLD -> D_FWD/NEW is okay.
1766 amt_lookup_act_srcs(tunnel, gnode, zero_grec, AMT_OPS_UNI,
1768 AMT_ACT_STATUS_D_FWD_NEW,
1771 * Only FWD_NEW will have (A) sources.
1773 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
1780 static void amt_mcast_to_ex_handler(struct amt_dev *amt,
1781 struct amt_tunnel_list *tunnel,
1782 struct amt_group_node *gnode,
1783 void *grec, void *zero_grec, bool v6)
1785 if (gnode->filter_mode == MCAST_INCLUDE) {
1786 /* Router State Report Rec'd New Router State Actions
1787 * ------------ ------------ ---------------- -------
1788 * INCLUDE (A) TO_EX (B) EXCLUDE (A*B,B-A) (B-A)=0
1793 /* EXCLUDE (A*B, ) */
1794 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
1796 AMT_ACT_STATUS_FWD_NEW,
1798 /* EXCLUDE (, B-A) */
1799 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV,
1801 AMT_ACT_STATUS_D_FWD_NEW,
1804 amt_lookup_act_srcs(tunnel, gnode, zero_grec, AMT_OPS_UNI,
1805 AMT_FILTER_D_FWD_NEW,
1808 /* Group Timer=GMI */
1809 if (!mod_delayed_work(amt_wq, &gnode->group_timer,
1810 msecs_to_jiffies(amt_gmi(amt))))
1812 gnode->filter_mode = MCAST_EXCLUDE;
1813 /* Delete (A-B) will be worked by amt_cleanup_srcs(). */
1815 /* Router State Report Rec'd New Router State Actions
1816 * ------------ ------------ ---------------- -------
1817 * EXCLUDE (X,Y) TO_EX (A) EXCLUDE (A-Y,Y*A) (A-X-Y)=Group Timer
1823 /* Update (A-X-Y) as NONE/OLD */
1824 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV,
1828 /* EXCLUDE (A-Y, ) */
1829 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV,
1831 AMT_ACT_STATUS_FWD_NEW,
1833 /* EXCLUDE (, Y*A) */
1834 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
1836 AMT_ACT_STATUS_D_FWD_NEW,
1838 /* Group Timer=GMI */
1839 if (!mod_delayed_work(amt_wq, &gnode->group_timer,
1840 msecs_to_jiffies(amt_gmi(amt))))
1842 /* Delete (X-A), (Y-A) will be worked by amt_cleanup_srcs(). */
1846 static void amt_mcast_allow_handler(struct amt_dev *amt,
1847 struct amt_tunnel_list *tunnel,
1848 struct amt_group_node *gnode,
1849 void *grec, void *zero_grec, bool v6)
1851 if (gnode->filter_mode == MCAST_INCLUDE) {
1852 /* Router State Report Rec'd New Router State Actions
1853 * ------------ ------------ ---------------- -------
1854 * INCLUDE (A) ALLOW (B) INCLUDE (A+B) (B)=GMI
1857 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
1859 AMT_ACT_STATUS_FWD_NEW,
1862 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
1867 /* Router State Report Rec'd New Router State Actions
1868 * ------------ ------------ ---------------- -------
1869 * EXCLUDE (X,Y) ALLOW (A) EXCLUDE (X+A,Y-A) (A)=GMI
1871 /* EXCLUDE (X+A, ) */
1872 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
1874 AMT_ACT_STATUS_FWD_NEW,
1876 /* EXCLUDE (, Y-A) */
1877 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB,
1879 AMT_ACT_STATUS_D_FWD_NEW,
1882 * All (A) source are now FWD/NEW status.
1884 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_INT,
1891 static void amt_mcast_block_handler(struct amt_dev *amt,
1892 struct amt_tunnel_list *tunnel,
1893 struct amt_group_node *gnode,
1894 void *grec, void *zero_grec, bool v6)
1896 if (gnode->filter_mode == MCAST_INCLUDE) {
1897 /* Router State Report Rec'd New Router State Actions
1898 * ------------ ------------ ---------------- -------
1899 * INCLUDE (A) BLOCK (B) INCLUDE (A) Send Q(G,A*B)
1902 amt_lookup_act_srcs(tunnel, gnode, zero_grec, AMT_OPS_UNI,
1904 AMT_ACT_STATUS_FWD_NEW,
1907 /* Router State Report Rec'd New Router State Actions
1908 * ------------ ------------ ---------------- -------
1909 * EXCLUDE (X,Y) BLOCK (A) EXCLUDE (X+(A-Y),Y) (A-X-Y)=Group Timer
1912 /* (A-X-Y)=Group Timer */
1913 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV,
1918 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
1920 AMT_ACT_STATUS_FWD_NEW,
1922 /* EXCLUDE (X+(A-Y) */
1923 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_SUB_REV,
1925 AMT_ACT_STATUS_FWD_NEW,
1928 amt_lookup_act_srcs(tunnel, gnode, grec, AMT_OPS_UNI,
1930 AMT_ACT_STATUS_D_FWD_NEW,
1936 * 7.3.2. In the Presence of Older Version Group Members
1938 * When Group Compatibility Mode is IGMPv2, a router internally
1939 * translates the following IGMPv2 messages for that group to their
1940 * IGMPv3 equivalents:
1942 * IGMPv2 Message IGMPv3 Equivalent
1943 * -------------- -----------------
1944 * Report IS_EX( {} )
1947 static void amt_igmpv2_report_handler(struct amt_dev *amt, struct sk_buff *skb,
1948 struct amt_tunnel_list *tunnel)
1950 struct igmphdr *ih = igmp_hdr(skb);
1951 struct iphdr *iph = ip_hdr(skb);
1952 struct amt_group_node *gnode;
1953 union amt_addr group, host;
1955 memset(&group, 0, sizeof(union amt_addr));
1956 group.ip4 = ih->group;
1957 memset(&host, 0, sizeof(union amt_addr));
1958 host.ip4 = iph->saddr;
1960 gnode = amt_lookup_group(tunnel, &group, &host, false);
1962 gnode = amt_add_group(amt, tunnel, &group, &host, false);
1963 if (!IS_ERR(gnode)) {
1964 gnode->filter_mode = MCAST_EXCLUDE;
1965 if (!mod_delayed_work(amt_wq, &gnode->group_timer,
1966 msecs_to_jiffies(amt_gmi(amt))))
1973 * 7.3.2. In the Presence of Older Version Group Members
1975 * When Group Compatibility Mode is IGMPv2, a router internally
1976 * translates the following IGMPv2 messages for that group to their
1977 * IGMPv3 equivalents:
1979 * IGMPv2 Message IGMPv3 Equivalent
1980 * -------------- -----------------
1981 * Report IS_EX( {} )
1984 static void amt_igmpv2_leave_handler(struct amt_dev *amt, struct sk_buff *skb,
1985 struct amt_tunnel_list *tunnel)
1987 struct igmphdr *ih = igmp_hdr(skb);
1988 struct iphdr *iph = ip_hdr(skb);
1989 struct amt_group_node *gnode;
1990 union amt_addr group, host;
1992 memset(&group, 0, sizeof(union amt_addr));
1993 group.ip4 = ih->group;
1994 memset(&host, 0, sizeof(union amt_addr));
1995 host.ip4 = iph->saddr;
1997 gnode = amt_lookup_group(tunnel, &group, &host, false);
1999 amt_del_group(amt, gnode);
2002 static void amt_igmpv3_report_handler(struct amt_dev *amt, struct sk_buff *skb,
2003 struct amt_tunnel_list *tunnel)
2005 struct igmpv3_report *ihrv3 = igmpv3_report_hdr(skb);
2006 int len = skb_transport_offset(skb) + sizeof(*ihrv3);
2007 void *zero_grec = (void *)&igmpv3_zero_grec;
2008 struct iphdr *iph = ip_hdr(skb);
2009 struct amt_group_node *gnode;
2010 union amt_addr group, host;
2011 struct igmpv3_grec *grec;
2015 for (i = 0; i < ntohs(ihrv3->ngrec); i++) {
2016 len += sizeof(*grec);
2017 if (!ip_mc_may_pull(skb, len))
2020 grec = (void *)(skb->data + len - sizeof(*grec));
2021 nsrcs = ntohs(grec->grec_nsrcs);
2023 len += nsrcs * sizeof(__be32);
2024 if (!ip_mc_may_pull(skb, len))
2027 memset(&group, 0, sizeof(union amt_addr));
2028 group.ip4 = grec->grec_mca;
2029 memset(&host, 0, sizeof(union amt_addr));
2030 host.ip4 = iph->saddr;
2031 gnode = amt_lookup_group(tunnel, &group, &host, false);
2033 gnode = amt_add_group(amt, tunnel, &group, &host,
2039 amt_add_srcs(amt, tunnel, gnode, grec, false);
2040 switch (grec->grec_type) {
2041 case IGMPV3_MODE_IS_INCLUDE:
2042 amt_mcast_is_in_handler(amt, tunnel, gnode, grec,
2045 case IGMPV3_MODE_IS_EXCLUDE:
2046 amt_mcast_is_ex_handler(amt, tunnel, gnode, grec,
2049 case IGMPV3_CHANGE_TO_INCLUDE:
2050 amt_mcast_to_in_handler(amt, tunnel, gnode, grec,
2053 case IGMPV3_CHANGE_TO_EXCLUDE:
2054 amt_mcast_to_ex_handler(amt, tunnel, gnode, grec,
2057 case IGMPV3_ALLOW_NEW_SOURCES:
2058 amt_mcast_allow_handler(amt, tunnel, gnode, grec,
2061 case IGMPV3_BLOCK_OLD_SOURCES:
2062 amt_mcast_block_handler(amt, tunnel, gnode, grec,
2068 amt_cleanup_srcs(amt, tunnel, gnode);
2072 /* caller held tunnel->lock */
2073 static void amt_igmp_report_handler(struct amt_dev *amt, struct sk_buff *skb,
2074 struct amt_tunnel_list *tunnel)
2076 struct igmphdr *ih = igmp_hdr(skb);
2079 case IGMPV3_HOST_MEMBERSHIP_REPORT:
2080 amt_igmpv3_report_handler(amt, skb, tunnel);
2082 case IGMPV2_HOST_MEMBERSHIP_REPORT:
2083 amt_igmpv2_report_handler(amt, skb, tunnel);
2085 case IGMP_HOST_LEAVE_MESSAGE:
2086 amt_igmpv2_leave_handler(amt, skb, tunnel);
2093 #if IS_ENABLED(CONFIG_IPV6)
2095 * 8.3.2. In the Presence of MLDv1 Multicast Address Listeners
2097 * When Multicast Address Compatibility Mode is MLDv2, a router acts
2098 * using the MLDv2 protocol for that multicast address. When Multicast
2099 * Address Compatibility Mode is MLDv1, a router internally translates
2100 * the following MLDv1 messages for that multicast address to their
2101 * MLDv2 equivalents:
2103 * MLDv1 Message MLDv2 Equivalent
2104 * -------------- -----------------
2105 * Report IS_EX( {} )
2108 static void amt_mldv1_report_handler(struct amt_dev *amt, struct sk_buff *skb,
2109 struct amt_tunnel_list *tunnel)
2111 struct mld_msg *mld = (struct mld_msg *)icmp6_hdr(skb);
2112 struct ipv6hdr *ip6h = ipv6_hdr(skb);
2113 struct amt_group_node *gnode;
2114 union amt_addr group, host;
2116 memcpy(&group.ip6, &mld->mld_mca, sizeof(struct in6_addr));
2117 memcpy(&host.ip6, &ip6h->saddr, sizeof(struct in6_addr));
2119 gnode = amt_lookup_group(tunnel, &group, &host, true);
2121 gnode = amt_add_group(amt, tunnel, &group, &host, true);
2122 if (!IS_ERR(gnode)) {
2123 gnode->filter_mode = MCAST_EXCLUDE;
2124 if (!mod_delayed_work(amt_wq, &gnode->group_timer,
2125 msecs_to_jiffies(amt_gmi(amt))))
2132 * 8.3.2. In the Presence of MLDv1 Multicast Address Listeners
2134 * When Multicast Address Compatibility Mode is MLDv2, a router acts
2135 * using the MLDv2 protocol for that multicast address. When Multicast
2136 * Address Compatibility Mode is MLDv1, a router internally translates
2137 * the following MLDv1 messages for that multicast address to their
2138 * MLDv2 equivalents:
2140 * MLDv1 Message MLDv2 Equivalent
2141 * -------------- -----------------
2142 * Report IS_EX( {} )
2145 static void amt_mldv1_leave_handler(struct amt_dev *amt, struct sk_buff *skb,
2146 struct amt_tunnel_list *tunnel)
2148 struct mld_msg *mld = (struct mld_msg *)icmp6_hdr(skb);
2149 struct iphdr *iph = ip_hdr(skb);
2150 struct amt_group_node *gnode;
2151 union amt_addr group, host;
2153 memcpy(&group.ip6, &mld->mld_mca, sizeof(struct in6_addr));
2154 memset(&host, 0, sizeof(union amt_addr));
2155 host.ip4 = iph->saddr;
2157 gnode = amt_lookup_group(tunnel, &group, &host, true);
2159 amt_del_group(amt, gnode);
2164 static void amt_mldv2_report_handler(struct amt_dev *amt, struct sk_buff *skb,
2165 struct amt_tunnel_list *tunnel)
2167 struct mld2_report *mld2r = (struct mld2_report *)icmp6_hdr(skb);
2168 int len = skb_transport_offset(skb) + sizeof(*mld2r);
2169 void *zero_grec = (void *)&mldv2_zero_grec;
2170 struct ipv6hdr *ip6h = ipv6_hdr(skb);
2171 struct amt_group_node *gnode;
2172 union amt_addr group, host;
2173 struct mld2_grec *grec;
2177 for (i = 0; i < ntohs(mld2r->mld2r_ngrec); i++) {
2178 len += sizeof(*grec);
2179 if (!ipv6_mc_may_pull(skb, len))
2182 grec = (void *)(skb->data + len - sizeof(*grec));
2183 nsrcs = ntohs(grec->grec_nsrcs);
2185 len += nsrcs * sizeof(struct in6_addr);
2186 if (!ipv6_mc_may_pull(skb, len))
2189 memset(&group, 0, sizeof(union amt_addr));
2190 group.ip6 = grec->grec_mca;
2191 memset(&host, 0, sizeof(union amt_addr));
2192 host.ip6 = ip6h->saddr;
2193 gnode = amt_lookup_group(tunnel, &group, &host, true);
2195 gnode = amt_add_group(amt, tunnel, &group, &host,
2201 amt_add_srcs(amt, tunnel, gnode, grec, true);
2202 switch (grec->grec_type) {
2203 case MLD2_MODE_IS_INCLUDE:
2204 amt_mcast_is_in_handler(amt, tunnel, gnode, grec,
2207 case MLD2_MODE_IS_EXCLUDE:
2208 amt_mcast_is_ex_handler(amt, tunnel, gnode, grec,
2211 case MLD2_CHANGE_TO_INCLUDE:
2212 amt_mcast_to_in_handler(amt, tunnel, gnode, grec,
2215 case MLD2_CHANGE_TO_EXCLUDE:
2216 amt_mcast_to_ex_handler(amt, tunnel, gnode, grec,
2219 case MLD2_ALLOW_NEW_SOURCES:
2220 amt_mcast_allow_handler(amt, tunnel, gnode, grec,
2223 case MLD2_BLOCK_OLD_SOURCES:
2224 amt_mcast_block_handler(amt, tunnel, gnode, grec,
2230 amt_cleanup_srcs(amt, tunnel, gnode);
2234 /* caller held tunnel->lock */
2235 static void amt_mld_report_handler(struct amt_dev *amt, struct sk_buff *skb,
2236 struct amt_tunnel_list *tunnel)
2238 struct mld_msg *mld = (struct mld_msg *)icmp6_hdr(skb);
2240 switch (mld->mld_type) {
2241 case ICMPV6_MGM_REPORT:
2242 amt_mldv1_report_handler(amt, skb, tunnel);
2244 case ICMPV6_MLD2_REPORT:
2245 amt_mldv2_report_handler(amt, skb, tunnel);
2247 case ICMPV6_MGM_REDUCTION:
2248 amt_mldv1_leave_handler(amt, skb, tunnel);
2256 static bool amt_advertisement_handler(struct amt_dev *amt, struct sk_buff *skb)
2258 struct amt_header_advertisement *amta;
2261 hdr_size = sizeof(*amta) + sizeof(struct udphdr);
2262 if (!pskb_may_pull(skb, hdr_size))
2265 amta = (struct amt_header_advertisement *)(udp_hdr(skb) + 1);
2269 if (amta->reserved || amta->version)
2272 if (ipv4_is_loopback(amta->ip4) || ipv4_is_multicast(amta->ip4) ||
2273 ipv4_is_zeronet(amta->ip4))
2276 amt->remote_ip = amta->ip4;
2277 netdev_dbg(amt->dev, "advertised remote ip = %pI4\n", &amt->remote_ip);
2278 mod_delayed_work(amt_wq, &amt->req_wq, 0);
2280 amt_update_gw_status(amt, AMT_STATUS_RECEIVED_ADVERTISEMENT, true);
2284 static bool amt_multicast_data_handler(struct amt_dev *amt, struct sk_buff *skb)
2286 struct amt_header_mcast_data *amtmd;
2287 int hdr_size, len, err;
2291 hdr_size = sizeof(*amtmd) + sizeof(struct udphdr);
2292 if (!pskb_may_pull(skb, hdr_size))
2295 amtmd = (struct amt_header_mcast_data *)(udp_hdr(skb) + 1);
2296 if (amtmd->reserved || amtmd->version)
2299 if (iptunnel_pull_header(skb, hdr_size, htons(ETH_P_IP), false))
2302 skb_reset_network_header(skb);
2303 skb_push(skb, sizeof(*eth));
2304 skb_reset_mac_header(skb);
2305 skb_pull(skb, sizeof(*eth));
2308 if (!pskb_may_pull(skb, sizeof(*iph)))
2312 if (iph->version == 4) {
2313 if (!ipv4_is_multicast(iph->daddr))
2315 skb->protocol = htons(ETH_P_IP);
2316 eth->h_proto = htons(ETH_P_IP);
2317 ip_eth_mc_map(iph->daddr, eth->h_dest);
2318 #if IS_ENABLED(CONFIG_IPV6)
2319 } else if (iph->version == 6) {
2320 struct ipv6hdr *ip6h;
2322 if (!pskb_may_pull(skb, sizeof(*ip6h)))
2325 ip6h = ipv6_hdr(skb);
2326 if (!ipv6_addr_is_multicast(&ip6h->daddr))
2328 skb->protocol = htons(ETH_P_IPV6);
2329 eth->h_proto = htons(ETH_P_IPV6);
2330 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
2336 skb->pkt_type = PACKET_MULTICAST;
2337 skb->ip_summed = CHECKSUM_NONE;
2339 err = gro_cells_receive(&amt->gro_cells, skb);
2340 if (likely(err == NET_RX_SUCCESS))
2341 dev_sw_netstats_rx_add(amt->dev, len);
2343 amt->dev->stats.rx_dropped++;
2348 static bool amt_membership_query_handler(struct amt_dev *amt,
2349 struct sk_buff *skb)
2351 struct amt_header_membership_query *amtmq;
2352 struct igmpv3_query *ihv3;
2353 struct ethhdr *eth, *oeth;
2357 hdr_size = sizeof(*amtmq) + sizeof(struct udphdr);
2358 if (!pskb_may_pull(skb, hdr_size))
2361 amtmq = (struct amt_header_membership_query *)(udp_hdr(skb) + 1);
2362 if (amtmq->reserved || amtmq->version)
2365 hdr_size -= sizeof(*eth);
2366 if (iptunnel_pull_header(skb, hdr_size, htons(ETH_P_TEB), false))
2369 oeth = eth_hdr(skb);
2370 skb_reset_mac_header(skb);
2371 skb_pull(skb, sizeof(*eth));
2372 skb_reset_network_header(skb);
2374 if (!pskb_may_pull(skb, sizeof(*iph)))
2378 if (iph->version == 4) {
2379 if (!pskb_may_pull(skb, sizeof(*iph) + AMT_IPHDR_OPTS +
2383 if (!ipv4_is_multicast(iph->daddr))
2386 ihv3 = skb_pull(skb, sizeof(*iph) + AMT_IPHDR_OPTS);
2387 skb_reset_transport_header(skb);
2388 skb_push(skb, sizeof(*iph) + AMT_IPHDR_OPTS);
2389 spin_lock_bh(&amt->lock);
2391 amt->mac = amtmq->response_mac;
2393 amt->qi = ihv3->qqic;
2394 spin_unlock_bh(&amt->lock);
2395 skb->protocol = htons(ETH_P_IP);
2396 eth->h_proto = htons(ETH_P_IP);
2397 ip_eth_mc_map(iph->daddr, eth->h_dest);
2398 #if IS_ENABLED(CONFIG_IPV6)
2399 } else if (iph->version == 6) {
2400 struct mld2_query *mld2q;
2401 struct ipv6hdr *ip6h;
2403 if (!pskb_may_pull(skb, sizeof(*ip6h) + AMT_IP6HDR_OPTS +
2407 ip6h = ipv6_hdr(skb);
2408 if (!ipv6_addr_is_multicast(&ip6h->daddr))
2411 mld2q = skb_pull(skb, sizeof(*ip6h) + AMT_IP6HDR_OPTS);
2412 skb_reset_transport_header(skb);
2413 skb_push(skb, sizeof(*ip6h) + AMT_IP6HDR_OPTS);
2414 spin_lock_bh(&amt->lock);
2416 amt->mac = amtmq->response_mac;
2418 amt->qi = mld2q->mld2q_qqic;
2419 spin_unlock_bh(&amt->lock);
2420 skb->protocol = htons(ETH_P_IPV6);
2421 eth->h_proto = htons(ETH_P_IPV6);
2422 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
2428 ether_addr_copy(eth->h_source, oeth->h_source);
2429 skb->pkt_type = PACKET_MULTICAST;
2430 skb->ip_summed = CHECKSUM_NONE;
2433 if (__netif_rx(skb) == NET_RX_SUCCESS) {
2434 amt_update_gw_status(amt, AMT_STATUS_RECEIVED_QUERY, true);
2435 dev_sw_netstats_rx_add(amt->dev, len);
2437 amt->dev->stats.rx_dropped++;
2444 static bool amt_update_handler(struct amt_dev *amt, struct sk_buff *skb)
2446 struct amt_header_membership_update *amtmu;
2447 struct amt_tunnel_list *tunnel;
2454 hdr_size = sizeof(*amtmu) + sizeof(struct udphdr);
2455 if (!pskb_may_pull(skb, hdr_size))
2458 amtmu = (struct amt_header_membership_update *)(udp_hdr(skb) + 1);
2459 if (amtmu->reserved || amtmu->version)
2462 if (iptunnel_pull_header(skb, hdr_size, skb->protocol, false))
2465 skb_reset_network_header(skb);
2467 list_for_each_entry_rcu(tunnel, &amt->tunnel_list, list) {
2468 if (tunnel->ip4 == iph->saddr) {
2469 if ((amtmu->nonce == tunnel->nonce &&
2470 amtmu->response_mac == tunnel->mac)) {
2471 mod_delayed_work(amt_wq, &tunnel->gc_wq,
2472 msecs_to_jiffies(amt_gmi(amt))
2476 netdev_dbg(amt->dev, "Invalid MAC\n");
2485 if (!pskb_may_pull(skb, sizeof(*iph)))
2489 if (iph->version == 4) {
2490 if (ip_mc_check_igmp(skb)) {
2491 netdev_dbg(amt->dev, "Invalid IGMP\n");
2495 spin_lock_bh(&tunnel->lock);
2496 amt_igmp_report_handler(amt, skb, tunnel);
2497 spin_unlock_bh(&tunnel->lock);
2499 skb_push(skb, sizeof(struct ethhdr));
2500 skb_reset_mac_header(skb);
2502 skb->protocol = htons(ETH_P_IP);
2503 eth->h_proto = htons(ETH_P_IP);
2504 ip_eth_mc_map(iph->daddr, eth->h_dest);
2505 #if IS_ENABLED(CONFIG_IPV6)
2506 } else if (iph->version == 6) {
2507 struct ipv6hdr *ip6h = ipv6_hdr(skb);
2509 if (ipv6_mc_check_mld(skb)) {
2510 netdev_dbg(amt->dev, "Invalid MLD\n");
2514 spin_lock_bh(&tunnel->lock);
2515 amt_mld_report_handler(amt, skb, tunnel);
2516 spin_unlock_bh(&tunnel->lock);
2518 skb_push(skb, sizeof(struct ethhdr));
2519 skb_reset_mac_header(skb);
2521 skb->protocol = htons(ETH_P_IPV6);
2522 eth->h_proto = htons(ETH_P_IPV6);
2523 ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest);
2526 netdev_dbg(amt->dev, "Unsupported Protocol\n");
2530 skb_pull(skb, sizeof(struct ethhdr));
2531 skb->pkt_type = PACKET_MULTICAST;
2532 skb->ip_summed = CHECKSUM_NONE;
2534 if (__netif_rx(skb) == NET_RX_SUCCESS) {
2535 amt_update_relay_status(tunnel, AMT_STATUS_RECEIVED_UPDATE,
2537 dev_sw_netstats_rx_add(amt->dev, len);
2539 amt->dev->stats.rx_dropped++;
2545 static void amt_send_advertisement(struct amt_dev *amt, __be32 nonce,
2546 __be32 daddr, __be16 dport)
2548 struct amt_header_advertisement *amta;
2549 int hlen, tlen, offset;
2550 struct socket *sock;
2551 struct udphdr *udph;
2552 struct sk_buff *skb;
2560 sock = rcu_dereference(amt->sock);
2564 if (!netif_running(amt->stream_dev) || !netif_running(amt->dev))
2567 rt = ip_route_output_ports(amt->net, &fl4, sock->sk,
2568 daddr, amt->local_ip,
2569 dport, amt->relay_port,
2571 amt->stream_dev->ifindex);
2573 amt->dev->stats.tx_errors++;
2577 hlen = LL_RESERVED_SPACE(amt->dev);
2578 tlen = amt->dev->needed_tailroom;
2579 len = hlen + tlen + sizeof(*iph) + sizeof(*udph) + sizeof(*amta);
2580 skb = netdev_alloc_skb_ip_align(amt->dev, len);
2583 amt->dev->stats.tx_errors++;
2587 skb->priority = TC_PRIO_CONTROL;
2588 skb_dst_set(skb, &rt->dst);
2590 len = sizeof(*iph) + sizeof(*udph) + sizeof(*amta);
2591 skb_reset_network_header(skb);
2593 amta = skb_pull(skb, sizeof(*iph) + sizeof(*udph));
2595 amta->type = AMT_MSG_ADVERTISEMENT;
2597 amta->nonce = nonce;
2598 amta->ip4 = amt->local_ip;
2599 skb_push(skb, sizeof(*udph));
2600 skb_reset_transport_header(skb);
2601 udph = udp_hdr(skb);
2602 udph->source = amt->relay_port;
2604 udph->len = htons(sizeof(*amta) + sizeof(*udph));
2606 offset = skb_transport_offset(skb);
2607 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
2608 udph->check = csum_tcpudp_magic(amt->local_ip, daddr,
2609 sizeof(*udph) + sizeof(*amta),
2610 IPPROTO_UDP, skb->csum);
2612 skb_push(skb, sizeof(*iph));
2615 iph->ihl = (sizeof(struct iphdr)) >> 2;
2618 iph->ttl = ip4_dst_hoplimit(&rt->dst);
2620 iph->saddr = amt->local_ip;
2621 iph->protocol = IPPROTO_UDP;
2622 iph->tot_len = htons(len);
2624 skb->ip_summed = CHECKSUM_NONE;
2625 ip_select_ident(amt->net, skb, NULL);
2627 err = ip_local_out(amt->net, sock->sk, skb);
2628 if (unlikely(net_xmit_eval(err)))
2629 amt->dev->stats.tx_errors++;
2635 static bool amt_discovery_handler(struct amt_dev *amt, struct sk_buff *skb)
2637 struct amt_header_discovery *amtd;
2638 struct udphdr *udph;
2641 if (!pskb_may_pull(skb, sizeof(*udph) + sizeof(*amtd)))
2645 udph = udp_hdr(skb);
2646 amtd = (struct amt_header_discovery *)(udp_hdr(skb) + 1);
2648 if (amtd->reserved || amtd->version)
2651 amt_send_advertisement(amt, amtd->nonce, iph->saddr, udph->source);
2656 static bool amt_request_handler(struct amt_dev *amt, struct sk_buff *skb)
2658 struct amt_header_request *amtrh;
2659 struct amt_tunnel_list *tunnel;
2660 unsigned long long key;
2661 struct udphdr *udph;
2666 if (!pskb_may_pull(skb, sizeof(*udph) + sizeof(*amtrh)))
2670 udph = udp_hdr(skb);
2671 amtrh = (struct amt_header_request *)(udp_hdr(skb) + 1);
2673 if (amtrh->reserved1 || amtrh->reserved2 || amtrh->version)
2676 list_for_each_entry_rcu(tunnel, &amt->tunnel_list, list)
2677 if (tunnel->ip4 == iph->saddr)
2680 if (amt->nr_tunnels >= amt->max_tunnels) {
2681 icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_HOST_UNREACH, 0);
2685 tunnel = kzalloc(sizeof(*tunnel) +
2686 (sizeof(struct hlist_head) * amt->hash_buckets),
2691 tunnel->source_port = udph->source;
2692 tunnel->ip4 = iph->saddr;
2694 memcpy(&key, &tunnel->key, sizeof(unsigned long long));
2696 spin_lock_init(&tunnel->lock);
2697 for (i = 0; i < amt->hash_buckets; i++)
2698 INIT_HLIST_HEAD(&tunnel->groups[i]);
2700 INIT_DELAYED_WORK(&tunnel->gc_wq, amt_tunnel_expire);
2702 spin_lock_bh(&amt->lock);
2703 list_add_tail_rcu(&tunnel->list, &amt->tunnel_list);
2704 tunnel->key = amt->key;
2705 amt_update_relay_status(tunnel, AMT_STATUS_RECEIVED_REQUEST, true);
2707 mod_delayed_work(amt_wq, &tunnel->gc_wq,
2708 msecs_to_jiffies(amt_gmi(amt)));
2709 spin_unlock_bh(&amt->lock);
2712 tunnel->nonce = amtrh->nonce;
2713 mac = siphash_3u32((__force u32)tunnel->ip4,
2714 (__force u32)tunnel->source_port,
2715 (__force u32)tunnel->nonce,
2717 tunnel->mac = mac >> 16;
2719 if (!netif_running(amt->dev) || !netif_running(amt->stream_dev))
2723 amt_send_igmp_gq(amt, tunnel);
2725 amt_send_mld_gq(amt, tunnel);
2730 static void amt_gw_rcv(struct amt_dev *amt, struct sk_buff *skb)
2732 int type = amt_parse_type(skb);
2738 if (amt->mode == AMT_MODE_GATEWAY) {
2740 case AMT_MSG_ADVERTISEMENT:
2741 err = amt_advertisement_handler(amt, skb);
2743 case AMT_MSG_MEMBERSHIP_QUERY:
2744 err = amt_membership_query_handler(amt, skb);
2749 netdev_dbg(amt->dev, "Invalid type of Gateway\n");
2755 amt->dev->stats.rx_dropped++;
2762 static int amt_rcv(struct sock *sk, struct sk_buff *skb)
2764 struct amt_dev *amt;
2770 amt = rcu_dereference_sk_user_data(sk);
2777 skb->dev = amt->dev;
2779 type = amt_parse_type(skb);
2785 if (amt->mode == AMT_MODE_GATEWAY) {
2787 case AMT_MSG_ADVERTISEMENT:
2788 if (iph->saddr != amt->discovery_ip) {
2789 netdev_dbg(amt->dev, "Invalid Relay IP\n");
2793 if (amt_queue_event(amt, AMT_EVENT_RECEIVE, skb)) {
2794 netdev_dbg(amt->dev, "AMT Event queue full\n");
2799 case AMT_MSG_MULTICAST_DATA:
2800 if (iph->saddr != amt->remote_ip) {
2801 netdev_dbg(amt->dev, "Invalid Relay IP\n");
2805 err = amt_multicast_data_handler(amt, skb);
2810 case AMT_MSG_MEMBERSHIP_QUERY:
2811 if (iph->saddr != amt->remote_ip) {
2812 netdev_dbg(amt->dev, "Invalid Relay IP\n");
2816 if (amt_queue_event(amt, AMT_EVENT_RECEIVE, skb)) {
2817 netdev_dbg(amt->dev, "AMT Event queue full\n");
2824 netdev_dbg(amt->dev, "Invalid type of Gateway\n");
2829 case AMT_MSG_DISCOVERY:
2830 err = amt_discovery_handler(amt, skb);
2832 case AMT_MSG_REQUEST:
2833 err = amt_request_handler(amt, skb);
2835 case AMT_MSG_MEMBERSHIP_UPDATE:
2836 err = amt_update_handler(amt, skb);
2843 netdev_dbg(amt->dev, "Invalid type of relay\n");
2849 amt->dev->stats.rx_dropped++;
2855 rcu_read_unlock_bh();
2859 static void amt_event_work(struct work_struct *work)
2861 struct amt_dev *amt = container_of(work, struct amt_dev, event_wq);
2862 struct sk_buff *skb;
2866 for (i = 0; i < AMT_MAX_EVENTS; i++) {
2867 spin_lock_bh(&amt->lock);
2868 if (amt->nr_events == 0) {
2869 spin_unlock_bh(&amt->lock);
2872 event = amt->events[amt->event_idx].event;
2873 skb = amt->events[amt->event_idx].skb;
2874 amt->events[amt->event_idx].event = AMT_EVENT_NONE;
2875 amt->events[amt->event_idx].skb = NULL;
2878 amt->event_idx %= AMT_MAX_EVENTS;
2879 spin_unlock_bh(&amt->lock);
2882 case AMT_EVENT_RECEIVE:
2883 amt_gw_rcv(amt, skb);
2885 case AMT_EVENT_SEND_DISCOVERY:
2886 amt_event_send_discovery(amt);
2888 case AMT_EVENT_SEND_REQUEST:
2889 amt_event_send_request(amt);
2899 static int amt_err_lookup(struct sock *sk, struct sk_buff *skb)
2901 struct amt_dev *amt;
2905 amt = rcu_dereference_sk_user_data(sk);
2909 if (amt->mode != AMT_MODE_GATEWAY)
2912 type = amt_parse_type(skb);
2916 netdev_dbg(amt->dev, "Received IGMP Unreachable of %s\n",
2919 case AMT_MSG_DISCOVERY:
2921 case AMT_MSG_REQUEST:
2922 case AMT_MSG_MEMBERSHIP_UPDATE:
2923 if (amt->status >= AMT_STATUS_RECEIVED_ADVERTISEMENT)
2924 mod_delayed_work(amt_wq, &amt->req_wq, 0);
2930 rcu_read_unlock_bh();
2933 rcu_read_unlock_bh();
2934 amt->dev->stats.rx_dropped++;
2938 static struct socket *amt_create_sock(struct net *net, __be16 port)
2940 struct udp_port_cfg udp_conf;
2941 struct socket *sock;
2944 memset(&udp_conf, 0, sizeof(udp_conf));
2945 udp_conf.family = AF_INET;
2946 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
2948 udp_conf.local_udp_port = port;
2950 err = udp_sock_create(net, &udp_conf, &sock);
2952 return ERR_PTR(err);
2957 static int amt_socket_create(struct amt_dev *amt)
2959 struct udp_tunnel_sock_cfg tunnel_cfg;
2960 struct socket *sock;
2962 sock = amt_create_sock(amt->net, amt->relay_port);
2964 return PTR_ERR(sock);
2966 /* Mark socket as an encapsulation socket */
2967 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
2968 tunnel_cfg.sk_user_data = amt;
2969 tunnel_cfg.encap_type = 1;
2970 tunnel_cfg.encap_rcv = amt_rcv;
2971 tunnel_cfg.encap_err_lookup = amt_err_lookup;
2972 tunnel_cfg.encap_destroy = NULL;
2973 setup_udp_tunnel_sock(amt->net, sock, &tunnel_cfg);
2975 rcu_assign_pointer(amt->sock, sock);
2979 static int amt_dev_open(struct net_device *dev)
2981 struct amt_dev *amt = netdev_priv(dev);
2984 amt->ready4 = false;
2985 amt->ready6 = false;
2989 err = amt_socket_create(amt);
2995 get_random_bytes(&amt->key, sizeof(siphash_key_t));
2997 amt->status = AMT_STATUS_INIT;
2998 if (amt->mode == AMT_MODE_GATEWAY) {
2999 mod_delayed_work(amt_wq, &amt->discovery_wq, 0);
3000 mod_delayed_work(amt_wq, &amt->req_wq, 0);
3001 } else if (amt->mode == AMT_MODE_RELAY) {
3002 mod_delayed_work(amt_wq, &amt->secret_wq,
3003 msecs_to_jiffies(AMT_SECRET_TIMEOUT));
3008 static int amt_dev_stop(struct net_device *dev)
3010 struct amt_dev *amt = netdev_priv(dev);
3011 struct amt_tunnel_list *tunnel, *tmp;
3012 struct socket *sock;
3013 struct sk_buff *skb;
3016 cancel_delayed_work_sync(&amt->req_wq);
3017 cancel_delayed_work_sync(&amt->discovery_wq);
3018 cancel_delayed_work_sync(&amt->secret_wq);
3021 sock = rtnl_dereference(amt->sock);
3022 RCU_INIT_POINTER(amt->sock, NULL);
3025 udp_tunnel_sock_release(sock);
3027 cancel_work_sync(&amt->event_wq);
3028 for (i = 0; i < AMT_MAX_EVENTS; i++) {
3029 skb = amt->events[i].skb;
3032 amt->events[i].event = AMT_EVENT_NONE;
3033 amt->events[i].skb = NULL;
3036 amt->ready4 = false;
3037 amt->ready6 = false;
3041 list_for_each_entry_safe(tunnel, tmp, &amt->tunnel_list, list) {
3042 list_del_rcu(&tunnel->list);
3044 cancel_delayed_work_sync(&tunnel->gc_wq);
3045 amt_clear_groups(tunnel);
3046 kfree_rcu(tunnel, rcu);
3052 static const struct device_type amt_type = {
3056 static int amt_dev_init(struct net_device *dev)
3058 struct amt_dev *amt = netdev_priv(dev);
3062 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
3066 err = gro_cells_init(&amt->gro_cells, dev);
3068 free_percpu(dev->tstats);
3075 static void amt_dev_uninit(struct net_device *dev)
3077 struct amt_dev *amt = netdev_priv(dev);
3079 gro_cells_destroy(&amt->gro_cells);
3080 free_percpu(dev->tstats);
3083 static const struct net_device_ops amt_netdev_ops = {
3084 .ndo_init = amt_dev_init,
3085 .ndo_uninit = amt_dev_uninit,
3086 .ndo_open = amt_dev_open,
3087 .ndo_stop = amt_dev_stop,
3088 .ndo_start_xmit = amt_dev_xmit,
3089 .ndo_get_stats64 = dev_get_tstats64,
3092 static void amt_link_setup(struct net_device *dev)
3094 dev->netdev_ops = &amt_netdev_ops;
3095 dev->needs_free_netdev = true;
3096 SET_NETDEV_DEVTYPE(dev, &amt_type);
3097 dev->min_mtu = ETH_MIN_MTU;
3098 dev->max_mtu = ETH_MAX_MTU;
3099 dev->type = ARPHRD_NONE;
3100 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
3101 dev->hard_header_len = 0;
3103 dev->priv_flags |= IFF_NO_QUEUE;
3104 dev->features |= NETIF_F_LLTX;
3105 dev->features |= NETIF_F_GSO_SOFTWARE;
3106 dev->features |= NETIF_F_NETNS_LOCAL;
3107 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM;
3108 dev->hw_features |= NETIF_F_FRAGLIST | NETIF_F_RXCSUM;
3109 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
3110 eth_hw_addr_random(dev);
3111 eth_zero_addr(dev->broadcast);
3115 static const struct nla_policy amt_policy[IFLA_AMT_MAX + 1] = {
3116 [IFLA_AMT_MODE] = { .type = NLA_U32 },
3117 [IFLA_AMT_RELAY_PORT] = { .type = NLA_U16 },
3118 [IFLA_AMT_GATEWAY_PORT] = { .type = NLA_U16 },
3119 [IFLA_AMT_LINK] = { .type = NLA_U32 },
3120 [IFLA_AMT_LOCAL_IP] = { .len = sizeof_field(struct iphdr, daddr) },
3121 [IFLA_AMT_REMOTE_IP] = { .len = sizeof_field(struct iphdr, daddr) },
3122 [IFLA_AMT_DISCOVERY_IP] = { .len = sizeof_field(struct iphdr, daddr) },
3123 [IFLA_AMT_MAX_TUNNELS] = { .type = NLA_U32 },
3126 static int amt_validate(struct nlattr *tb[], struct nlattr *data[],
3127 struct netlink_ext_ack *extack)
3132 if (!data[IFLA_AMT_LINK]) {
3133 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_LINK],
3134 "Link attribute is required");
3138 if (!data[IFLA_AMT_MODE]) {
3139 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_MODE],
3140 "Mode attribute is required");
3144 if (nla_get_u32(data[IFLA_AMT_MODE]) > AMT_MODE_MAX) {
3145 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_MODE],
3146 "Mode attribute is not valid");
3150 if (!data[IFLA_AMT_LOCAL_IP]) {
3151 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_DISCOVERY_IP],
3152 "Local attribute is required");
3156 if (!data[IFLA_AMT_DISCOVERY_IP] &&
3157 nla_get_u32(data[IFLA_AMT_MODE]) == AMT_MODE_GATEWAY) {
3158 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_AMT_LOCAL_IP],
3159 "Discovery attribute is required");
3166 static int amt_newlink(struct net *net, struct net_device *dev,
3167 struct nlattr *tb[], struct nlattr *data[],
3168 struct netlink_ext_ack *extack)
3170 struct amt_dev *amt = netdev_priv(dev);
3174 amt->mode = nla_get_u32(data[IFLA_AMT_MODE]);
3176 if (data[IFLA_AMT_MAX_TUNNELS] &&
3177 nla_get_u32(data[IFLA_AMT_MAX_TUNNELS]))
3178 amt->max_tunnels = nla_get_u32(data[IFLA_AMT_MAX_TUNNELS]);
3180 amt->max_tunnels = AMT_MAX_TUNNELS;
3182 spin_lock_init(&amt->lock);
3183 amt->max_groups = AMT_MAX_GROUP;
3184 amt->max_sources = AMT_MAX_SOURCE;
3185 amt->hash_buckets = AMT_HSIZE;
3186 amt->nr_tunnels = 0;
3187 get_random_bytes(&amt->hash_seed, sizeof(amt->hash_seed));
3188 amt->stream_dev = dev_get_by_index(net,
3189 nla_get_u32(data[IFLA_AMT_LINK]));
3190 if (!amt->stream_dev) {
3191 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_LINK],
3192 "Can't find stream device");
3196 if (amt->stream_dev->type != ARPHRD_ETHER) {
3197 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_LINK],
3198 "Invalid stream device type");
3202 amt->local_ip = nla_get_in_addr(data[IFLA_AMT_LOCAL_IP]);
3203 if (ipv4_is_loopback(amt->local_ip) ||
3204 ipv4_is_zeronet(amt->local_ip) ||
3205 ipv4_is_multicast(amt->local_ip)) {
3206 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_LOCAL_IP],
3207 "Invalid Local address");
3211 if (data[IFLA_AMT_RELAY_PORT])
3212 amt->relay_port = nla_get_be16(data[IFLA_AMT_RELAY_PORT]);
3214 amt->relay_port = htons(IANA_AMT_UDP_PORT);
3216 if (data[IFLA_AMT_GATEWAY_PORT])
3217 amt->gw_port = nla_get_be16(data[IFLA_AMT_GATEWAY_PORT]);
3219 amt->gw_port = htons(IANA_AMT_UDP_PORT);
3221 if (!amt->relay_port) {
3222 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_DISCOVERY_IP],
3223 "relay port must not be 0");
3226 if (amt->mode == AMT_MODE_RELAY) {
3227 amt->qrv = READ_ONCE(amt->net->ipv4.sysctl_igmp_qrv);
3229 dev->needed_headroom = amt->stream_dev->needed_headroom +
3231 dev->mtu = amt->stream_dev->mtu - AMT_RELAY_HLEN;
3232 dev->max_mtu = dev->mtu;
3233 dev->min_mtu = ETH_MIN_MTU + AMT_RELAY_HLEN;
3235 if (!data[IFLA_AMT_DISCOVERY_IP]) {
3236 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_DISCOVERY_IP],
3237 "discovery must be set in gateway mode");
3240 if (!amt->gw_port) {
3241 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_DISCOVERY_IP],
3242 "gateway port must not be 0");
3246 amt->discovery_ip = nla_get_in_addr(data[IFLA_AMT_DISCOVERY_IP]);
3247 if (ipv4_is_loopback(amt->discovery_ip) ||
3248 ipv4_is_zeronet(amt->discovery_ip) ||
3249 ipv4_is_multicast(amt->discovery_ip)) {
3250 NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_AMT_DISCOVERY_IP],
3251 "discovery must be unicast");
3255 dev->needed_headroom = amt->stream_dev->needed_headroom +
3257 dev->mtu = amt->stream_dev->mtu - AMT_GW_HLEN;
3258 dev->max_mtu = dev->mtu;
3259 dev->min_mtu = ETH_MIN_MTU + AMT_GW_HLEN;
3261 amt->qi = AMT_INIT_QUERY_INTERVAL;
3263 err = register_netdevice(dev);
3265 netdev_dbg(dev, "failed to register new netdev %d\n", err);
3269 err = netdev_upper_dev_link(amt->stream_dev, dev, extack);
3271 unregister_netdevice(dev);
3275 INIT_DELAYED_WORK(&amt->discovery_wq, amt_discovery_work);
3276 INIT_DELAYED_WORK(&amt->req_wq, amt_req_work);
3277 INIT_DELAYED_WORK(&amt->secret_wq, amt_secret_work);
3278 INIT_WORK(&amt->event_wq, amt_event_work);
3279 INIT_LIST_HEAD(&amt->tunnel_list);
3282 dev_put(amt->stream_dev);
3286 static void amt_dellink(struct net_device *dev, struct list_head *head)
3288 struct amt_dev *amt = netdev_priv(dev);
3290 unregister_netdevice_queue(dev, head);
3291 netdev_upper_dev_unlink(amt->stream_dev, dev);
3292 dev_put(amt->stream_dev);
3295 static size_t amt_get_size(const struct net_device *dev)
3297 return nla_total_size(sizeof(__u32)) + /* IFLA_AMT_MODE */
3298 nla_total_size(sizeof(__u16)) + /* IFLA_AMT_RELAY_PORT */
3299 nla_total_size(sizeof(__u16)) + /* IFLA_AMT_GATEWAY_PORT */
3300 nla_total_size(sizeof(__u32)) + /* IFLA_AMT_LINK */
3301 nla_total_size(sizeof(__u32)) + /* IFLA_MAX_TUNNELS */
3302 nla_total_size(sizeof(struct iphdr)) + /* IFLA_AMT_DISCOVERY_IP */
3303 nla_total_size(sizeof(struct iphdr)) + /* IFLA_AMT_REMOTE_IP */
3304 nla_total_size(sizeof(struct iphdr)); /* IFLA_AMT_LOCAL_IP */
3307 static int amt_fill_info(struct sk_buff *skb, const struct net_device *dev)
3309 struct amt_dev *amt = netdev_priv(dev);
3311 if (nla_put_u32(skb, IFLA_AMT_MODE, amt->mode))
3312 goto nla_put_failure;
3313 if (nla_put_be16(skb, IFLA_AMT_RELAY_PORT, amt->relay_port))
3314 goto nla_put_failure;
3315 if (nla_put_be16(skb, IFLA_AMT_GATEWAY_PORT, amt->gw_port))
3316 goto nla_put_failure;
3317 if (nla_put_u32(skb, IFLA_AMT_LINK, amt->stream_dev->ifindex))
3318 goto nla_put_failure;
3319 if (nla_put_in_addr(skb, IFLA_AMT_LOCAL_IP, amt->local_ip))
3320 goto nla_put_failure;
3321 if (nla_put_in_addr(skb, IFLA_AMT_DISCOVERY_IP, amt->discovery_ip))
3322 goto nla_put_failure;
3324 if (nla_put_in_addr(skb, IFLA_AMT_REMOTE_IP, amt->remote_ip))
3325 goto nla_put_failure;
3326 if (nla_put_u32(skb, IFLA_AMT_MAX_TUNNELS, amt->max_tunnels))
3327 goto nla_put_failure;
3335 static struct rtnl_link_ops amt_link_ops __read_mostly = {
3337 .maxtype = IFLA_AMT_MAX,
3338 .policy = amt_policy,
3339 .priv_size = sizeof(struct amt_dev),
3340 .setup = amt_link_setup,
3341 .validate = amt_validate,
3342 .newlink = amt_newlink,
3343 .dellink = amt_dellink,
3344 .get_size = amt_get_size,
3345 .fill_info = amt_fill_info,
3348 static struct net_device *amt_lookup_upper_dev(struct net_device *dev)
3350 struct net_device *upper_dev;
3351 struct amt_dev *amt;
3353 for_each_netdev(dev_net(dev), upper_dev) {
3354 if (netif_is_amt(upper_dev)) {
3355 amt = netdev_priv(upper_dev);
3356 if (amt->stream_dev == dev)
3364 static int amt_device_event(struct notifier_block *unused,
3365 unsigned long event, void *ptr)
3367 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3368 struct net_device *upper_dev;
3369 struct amt_dev *amt;
3373 upper_dev = amt_lookup_upper_dev(dev);
3376 amt = netdev_priv(upper_dev);
3379 case NETDEV_UNREGISTER:
3380 amt_dellink(amt->dev, &list);
3381 unregister_netdevice_many(&list);
3383 case NETDEV_CHANGEMTU:
3384 if (amt->mode == AMT_MODE_RELAY)
3385 new_mtu = dev->mtu - AMT_RELAY_HLEN;
3387 new_mtu = dev->mtu - AMT_GW_HLEN;
3389 dev_set_mtu(amt->dev, new_mtu);
3396 static struct notifier_block amt_notifier_block __read_mostly = {
3397 .notifier_call = amt_device_event,
3400 static int __init amt_init(void)
3404 err = register_netdevice_notifier(&amt_notifier_block);
3408 err = rtnl_link_register(&amt_link_ops);
3410 goto unregister_notifier;
3412 amt_wq = alloc_workqueue("amt", WQ_UNBOUND, 0);
3415 goto rtnl_unregister;
3418 spin_lock_init(&source_gc_lock);
3419 spin_lock_bh(&source_gc_lock);
3420 INIT_DELAYED_WORK(&source_gc_wq, amt_source_gc_work);
3421 mod_delayed_work(amt_wq, &source_gc_wq,
3422 msecs_to_jiffies(AMT_GC_INTERVAL));
3423 spin_unlock_bh(&source_gc_lock);
3428 rtnl_link_unregister(&amt_link_ops);
3429 unregister_notifier:
3430 unregister_netdevice_notifier(&amt_notifier_block);
3432 pr_err("error loading AMT module loaded\n");
3435 late_initcall(amt_init);
3437 static void __exit amt_fini(void)
3439 rtnl_link_unregister(&amt_link_ops);
3440 unregister_netdevice_notifier(&amt_notifier_block);
3441 cancel_delayed_work_sync(&source_gc_wq);
3442 __amt_source_gc_work();
3443 destroy_workqueue(amt_wq);
3445 module_exit(amt_fini);
3447 MODULE_LICENSE("GPL");
3448 MODULE_AUTHOR("Taehee Yoo <ap420073@gmail.com>");
3449 MODULE_ALIAS_RTNL_LINK("amt");