summaryrefslogtreecommitdiff
path: root/node/Multicaster.cpp
blob: f3c9038e2f7c5cdf18e228076f1c77abb54149df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
 * ZeroTier One - Global Peer to Peer Ethernet
 * Copyright (C) 2011-2015  ZeroTier Networks
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * --
 *
 * ZeroTier may be used and distributed under the terms of the GPLv3, which
 * are available at: http://www.gnu.org/licenses/gpl-3.0.html
 *
 * If you would like to embed ZeroTier into a commercial application or
 * redistribute it in a modified binary form, please contact ZeroTier Networks
 * LLC. Start here: http://www.zerotier.com/
 */

#include <algorithm>

#include "Constants.hpp"
#include "RuntimeEnvironment.hpp"
#include "SharedPtr.hpp"
#include "Multicaster.hpp"
#include "Topology.hpp"
#include "Switch.hpp"
#include "Packet.hpp"
#include "Peer.hpp"
#include "CMWC4096.hpp"
#include "C25519.hpp"
#include "NodeConfig.hpp"
#include "CertificateOfMembership.hpp"
#include "Logger.hpp"

namespace ZeroTier {

Multicaster::Multicaster(const RuntimeEnvironment *renv) :
	RR(renv)
{
}

Multicaster::~Multicaster()
{
}

void Multicaster::addMultiple(uint64_t now,uint64_t nwid,const MulticastGroup &mg,const void *addresses,unsigned int count,unsigned int totalKnown)
{
	const unsigned char *p = (const unsigned char *)addresses;
	const unsigned char *e = p + (5 * count);
	Mutex::Lock _l(_groups_m);
	MulticastGroupStatus &gs = _groups[std::pair<uint64_t,MulticastGroup>(nwid,mg)];
	while (p != e) {
		_add(now,nwid,mg,gs,Address(p,5));
		p += 5;
	}
}

unsigned int Multicaster::gather(const Address &queryingPeer,uint64_t nwid,const MulticastGroup &mg,Packet &appendTo,unsigned int limit) const
{
	unsigned char *p;
	unsigned int added = 0,i,k,rptr,totalKnown = 0;
	uint64_t a,picked[(ZT_PROTO_MAX_PACKET_LENGTH / 5) + 2];

	if (!limit)
		return 0;
	else if (limit > 0xffff)
		limit = 0xffff;

	const unsigned int totalAt = appendTo.size();
	appendTo.addSize(4); // sizeof(uint32_t)
	const unsigned int addedAt = appendTo.size();
	appendTo.addSize(2); // sizeof(uint16_t)

	{ // Return myself if I am a member of this group
		SharedPtr<Network> network(RR->nc->network(nwid));
		if ((network)&&(network->subscribedToMulticastGroup(mg))) {
			RR->identity.address().appendTo(appendTo);
			++totalKnown;
			++added;
		}
	}

	Mutex::Lock _l(_groups_m);

	std::map< std::pair<uint64_t,MulticastGroup>,MulticastGroupStatus >::const_iterator gs(_groups.find(std::pair<uint64_t,MulticastGroup>(nwid,mg)));
	if ((gs != _groups.end())&&(!gs->second.members.empty())) {
		totalKnown += (unsigned int)gs->second.members.size();

		// Members are returned in random order so that repeated gather queries
		// will return different subsets of a large multicast group.
		k = 0;
		while ((added < limit)&&(k < gs->second.members.size())&&((appendTo.size() + ZT_ADDRESS_LENGTH) <= ZT_UDP_DEFAULT_PAYLOAD_MTU)) {
			rptr = (unsigned int)RR->prng->next32();

restart_member_scan:
			a = gs->second.members[rptr % (unsigned int)gs->second.members.size()].address.toInt();
			for(i=0;i<k;++i) {
				if (picked[i] == a) {
					++rptr;
					goto restart_member_scan;
				}
			}
			picked[k++] = a;

			if (queryingPeer.toInt() != a) { // do not return the peer that is making the request as a result
				p = (unsigned char *)appendTo.appendField(ZT_ADDRESS_LENGTH);
				*(p++) = (unsigned char)((a >> 32) & 0xff);
				*(p++) = (unsigned char)((a >> 24) & 0xff);
				*(p++) = (unsigned char)((a >> 16) & 0xff);
				*(p++) = (unsigned char)((a >> 8) & 0xff);
				*p = (unsigned char)(a & 0xff);
				++added;
			}
		}
	}

	appendTo.setAt(totalAt,(uint32_t)totalKnown);
	appendTo.setAt(addedAt,(uint16_t)added);

	//TRACE("..MC Multicaster::gather() attached %u of %u peers for %.16llx/%s (2)",n,(unsigned int)(gs->second.members.size() - skipped),nwid,mg.toString().c_str());

	return added;
}

std::vector<Address> Multicaster::getMembers(uint64_t nwid,const MulticastGroup &mg,unsigned int limit) const
{
	std::vector<Address> ls;
	Mutex::Lock _l(_groups_m);
	std::map< std::pair<uint64_t,MulticastGroup>,MulticastGroupStatus >::const_iterator gs(_groups.find(std::pair<uint64_t,MulticastGroup>(nwid,mg)));
	if (gs == _groups.end())
		return ls;
	for(std::vector<MulticastGroupMember>::const_reverse_iterator m(gs->second.members.rbegin());m!=gs->second.members.rend();++m) {
		ls.push_back(m->address);
		if (ls.size() >= limit)
			break;
	}
	return ls;
}

void Multicaster::send(
	const CertificateOfMembership *com,
	unsigned int limit,
	uint64_t now,
	uint64_t nwid,
	const std::vector<Address> &alwaysSendTo,
	const MulticastGroup &mg,
	const MAC &src,
	unsigned int etherType,
	const void *data,
	unsigned int len)
{
	unsigned long idxbuf[8194];
	unsigned long *indexes = idxbuf;

	Mutex::Lock _l(_groups_m);
	MulticastGroupStatus &gs = _groups[std::pair<uint64_t,MulticastGroup>(nwid,mg)];

	if (!gs.members.empty()) {
		// Allocate a memory buffer if group is monstrous
		if (gs.members.size() > (sizeof(idxbuf) / sizeof(unsigned long)))
			indexes = new unsigned long[gs.members.size()];

		// Generate a random permutation of member indexes
		for(unsigned long i=0;i<gs.members.size();++i)
			indexes[i] = i;
		for(unsigned long i=(unsigned long)gs.members.size()-1;i>0;--i) {
			unsigned long j = RR->prng->next32() % (i + 1);
			unsigned long tmp = indexes[j];
			indexes[j] = indexes[i];
			indexes[i] = tmp;
		}
	}

	if (gs.members.size() >= limit) {
		// Skip queue if we already have enough members to complete the send operation
		OutboundMulticast out;

		out.init(
			RR,
			now,
			nwid,
			com,
			limit,
			1, // we'll still gather a little from peers to keep multicast list fresh
			src,
			mg,
			etherType,
			data,
			len);

		unsigned int count = 0;

		for(std::vector<Address>::const_iterator ast(alwaysSendTo.begin());ast!=alwaysSendTo.end();++ast) {
			out.sendOnly(RR,*ast);
			if (++count >= limit)
				break;
		}

		unsigned long idx = 0;
		while ((count < limit)&&(idx < gs.members.size())) {
			Address ma(gs.members[indexes[idx++]].address);
			if (std::find(alwaysSendTo.begin(),alwaysSendTo.end(),ma) == alwaysSendTo.end()) {
				out.sendOnly(RR,ma);
				++count;
			}
		}
	} else {
		unsigned int gatherLimit = (limit - (unsigned int)gs.members.size()) + 1;

		if ((now - gs.lastExplicitGather) >= ZT_MULTICAST_EXPLICIT_GATHER_DELAY) {
			gs.lastExplicitGather = now;
			SharedPtr<Peer> sn(RR->topology->getBestSupernode());
			if (sn) {
				TRACE(">>MC upstream GATHER up to %u for group %.16llx/%s",gatherLimit,nwid,mg.toString().c_str());

				Packet outp(sn->address(),RR->identity.address(),Packet::VERB_MULTICAST_GATHER);
				outp.append(nwid);
				outp.append((uint8_t)0);
				mg.mac().appendTo(outp);
				outp.append((uint32_t)mg.adi());
				outp.append((uint32_t)gatherLimit);
				outp.armor(sn->key(),true);
				sn->send(RR,outp.data(),outp.size(),now);
			}
			gatherLimit = 0;
		}

		gs.txQueue.push_back(OutboundMulticast());
		OutboundMulticast &out = gs.txQueue.back();

		out.init(
			RR,
			now,
			nwid,
			com,
			limit,
			gatherLimit,
			src,
			mg,
			etherType,
			data,
			len);

		unsigned int count = 0;

		for(std::vector<Address>::const_iterator ast(alwaysSendTo.begin());ast!=alwaysSendTo.end();++ast) {
			out.sendAndLog(RR,*ast);
			if (++count >= limit)
				break;
		}

		unsigned long idx = 0;
		while ((count < limit)&&(idx < gs.members.size())) {
			Address ma(gs.members[indexes[idx++]].address);
			if (std::find(alwaysSendTo.begin(),alwaysSendTo.end(),ma) == alwaysSendTo.end()) {
				out.sendAndLog(RR,ma);
				++count;
			}
		}
	}

	// Free allocated memory buffer if any
	if (indexes != idxbuf)
		delete [] indexes;

#ifdef ZT_SUPPORT_LEGACY_MULTICAST
	// This sends a P5 multicast up to our supernode, who then
	// redistributes it manually down to all <1.0.0 peers for
	// legacy support. These peers don't support the new multicast
	// frame type, so even if they receive it they will ignore it.
	{
		SharedPtr<Peer> sn(RR->topology->getBestSupernode());
		if (sn) {
			uint32_t rn = RR->prng->next32();
			Packet outp(sn->address(),RR->identity.address(),Packet::VERB_P5_MULTICAST_FRAME);

			outp.append((uint16_t)0xffff); // do not forward
			outp.append((unsigned char)0,320 + 1024); // empty queue and bloom filter

			outp.append((unsigned char)((com) ? ZT_PROTO_VERB_P5_MULTICAST_FRAME_FLAGS_HAS_MEMBERSHIP_CERTIFICATE : 0));
			outp.append((uint64_t)nwid);
			outp.append((uint16_t)0);
			outp.append((unsigned char)0);
			outp.append((unsigned char)0);
			RR->identity.address().appendTo(outp);
			outp.append((const void *)&rn,3); // random multicast ID
			if (src)
				src.appendTo(outp);
			else MAC(RR->identity.address(),nwid).appendTo(outp);
			mg.mac().appendTo(outp);
			outp.append((uint32_t)mg.adi());
			outp.append((uint16_t)etherType);
			outp.append((uint16_t)len);
			outp.append(data,len);
			unsigned int signedPortionLen = outp.size() - ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX__START_OF_SIGNED_PORTION;

			C25519::Signature sig(RR->identity.sign(outp.field(ZT_PROTO_VERB_P5_MULTICAST_FRAME_IDX__START_OF_SIGNED_PORTION,signedPortionLen),signedPortionLen));

			outp.append((uint16_t)sig.size());
			outp.append(sig.data,(unsigned int)sig.size());

			if (com) com->serialize(outp);

			outp.compress();
			outp.armor(sn->key(),true);
			sn->send(RR,outp.data(),outp.size(),now);
		}
	}
#endif // ZT_SUPPORT_LEGACY_MULTICAST
}

void Multicaster::clean(uint64_t now)
{
	Mutex::Lock _l(_groups_m);
	for(std::map< std::pair<uint64_t,MulticastGroup>,MulticastGroupStatus >::iterator mm(_groups.begin());mm!=_groups.end();) {
		for(std::list<OutboundMulticast>::iterator tx(mm->second.txQueue.begin());tx!=mm->second.txQueue.end();) {
			if ((tx->expired(now))||(tx->atLimit()))
				mm->second.txQueue.erase(tx++);
			else ++tx;
		}

		unsigned long count = 0;
		{
			std::vector<MulticastGroupMember>::iterator reader(mm->second.members.begin());
			std::vector<MulticastGroupMember>::iterator writer(reader);
			while (reader != mm->second.members.end()) {
				if ((now - reader->timestamp) < ZT_MULTICAST_LIKE_EXPIRE) {
					*writer = *reader;
					++writer;
					++count;
				}
				++reader;
			}
		}

		if (count) {
			mm->second.members.resize(count);
			++mm;
		} else if (mm->second.txQueue.empty()) {
			_groups.erase(mm++);
		} else {
			mm->second.members.clear();
			++mm;
		}
	}
}

void Multicaster::_add(uint64_t now,uint64_t nwid,const MulticastGroup &mg,MulticastGroupStatus &gs,const Address &member)
{
	// assumes _groups_m is locked

	// Do not add self -- even if someone else returns it
	if (member == RR->identity.address())
		return;

	for(std::vector<MulticastGroupMember>::iterator m(gs.members.begin());m!=gs.members.end();++m) {
		if (m->address == member) {
			m->timestamp = now;
			return;
		}
	}

	gs.members.push_back(MulticastGroupMember(member,now));

	//TRACE("..MC %s joined multicast group %.16llx/%s via %s",member.toString().c_str(),nwid,mg.toString().c_str(),((learnedFrom) ? learnedFrom.toString().c_str() : "(direct)"));

	for(std::list<OutboundMulticast>::iterator tx(gs.txQueue.begin());tx!=gs.txQueue.end();) {
		if (tx->atLimit()) {
			gs.txQueue.erase(tx++);
		} else {
			tx->sendIfNew(RR,member);
			if (tx->atLimit())
				gs.txQueue.erase(tx++);
			else ++tx;
		}
	}
}

} // namespace ZeroTier