1 /*
2  * Copyright (c)2019 ZeroTier, Inc.
3  *
4  * Use of this software is governed by the Business Source License included
5  * in the LICENSE.TXT file in the project's root directory.
6  *
7  * Change Date: 2025-01-01
8  *
9  * On the date above, in accordance with the Business Source License, use
10  * of this software will be governed by version 2.0 of the Apache License.
11  */
12 /****/
13 
14 #include "Constants.hpp"
15 #include "RuntimeEnvironment.hpp"
16 #include "OutboundMulticast.hpp"
17 #include "Switch.hpp"
18 #include "Network.hpp"
19 #include "Node.hpp"
20 #include "Peer.hpp"
21 #include "Topology.hpp"
22 
23 namespace ZeroTier {
24 
init(const RuntimeEnvironment * RR,uint64_t timestamp,uint64_t nwid,bool disableCompression,unsigned int limit,unsigned int gatherLimit,const MAC & src,const MulticastGroup & dest,unsigned int etherType,const void * payload,unsigned int len)25 void OutboundMulticast::init(
26 	const RuntimeEnvironment *RR,
27 	uint64_t timestamp,
28 	uint64_t nwid,
29 	bool disableCompression,
30 	unsigned int limit,
31 	unsigned int gatherLimit,
32 	const MAC &src,
33 	const MulticastGroup &dest,
34 	unsigned int etherType,
35 	const void *payload,
36 	unsigned int len)
37 {
38 	uint8_t flags = 0;
39 
40 	_timestamp = timestamp;
41 	_nwid = nwid;
42 	if (src) {
43 		_macSrc = src;
44 		flags |= 0x04;
45 	} else {
46 		_macSrc.fromAddress(RR->identity.address(),nwid);
47 	}
48 	_macDest = dest.mac();
49 	_limit = limit;
50 	_frameLen = (len < ZT_MAX_MTU) ? len : ZT_MAX_MTU;
51 	_etherType = etherType;
52 
53 	if (gatherLimit) flags |= 0x02;
54 
55 	_packet.setSource(RR->identity.address());
56 	_packet.setVerb(Packet::VERB_MULTICAST_FRAME);
57 	_packet.append((uint64_t)nwid);
58 	_packet.append(flags);
59 	if (gatherLimit) _packet.append((uint32_t)gatherLimit);
60 	if (src) src.appendTo(_packet);
61 	dest.mac().appendTo(_packet);
62 	_packet.append((uint32_t)dest.adi());
63 	_packet.append((uint16_t)etherType);
64 	_packet.append(payload,_frameLen);
65 	if (!disableCompression)
66 		_packet.compress();
67 
68 	memcpy(_frameData,payload,_frameLen);
69 }
70 
sendOnly(const RuntimeEnvironment * RR,void * tPtr,const Address & toAddr)71 void OutboundMulticast::sendOnly(const RuntimeEnvironment *RR,void *tPtr,const Address &toAddr)
72 {
73 	const SharedPtr<Network> nw(RR->node->network(_nwid));
74 	uint8_t QoSBucket = 255; // Dummy value
75 	if ((nw)&&(nw->filterOutgoingPacket(tPtr,true,RR->identity.address(),toAddr,_macSrc,_macDest,_frameData,_frameLen,_etherType,0,QoSBucket))) {
76 		nw->pushCredentialsIfNeeded(tPtr,toAddr,RR->node->now());
77 		_packet.newInitializationVector();
78 		_packet.setDestination(toAddr);
79 		RR->node->expectReplyTo(_packet.packetId());
80 		_tmp = _packet;
81 		RR->sw->send(tPtr,_tmp,true);
82 	}
83 }
84 
85 } // namespace ZeroTier
86