2013-07-04 20:56:19 +00:00
|
|
|
/*
|
2015-02-17 21:11:34 +00:00
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
|
|
|
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
|
|
|
* 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/
|
|
|
|
*/
|
|
|
|
|
2013-12-07 00:49:20 +00:00
|
|
|
#ifndef ZT_N_PACKET_HPP
|
|
|
|
#define ZT_N_PACKET_HPP
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2013-09-17 19:53:59 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
|
|
|
|
2014-10-29 00:25:34 +00:00
|
|
|
#include "Constants.hpp"
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "Address.hpp"
|
2013-09-16 17:02:10 +00:00
|
|
|
#include "Poly1305.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "Salsa20.hpp"
|
|
|
|
#include "Utils.hpp"
|
|
|
|
#include "Buffer.hpp"
|
|
|
|
|
|
|
|
#include "../ext/lz4/lz4.h"
|
|
|
|
|
|
|
|
/**
|
2015-10-07 23:11:50 +00:00
|
|
|
* Protocol version -- incremented only for major changes
|
2013-07-11 02:58:43 +00:00
|
|
|
*
|
|
|
|
* 1 - 0.2.0 ... 0.2.5
|
2013-09-20 17:36:14 +00:00
|
|
|
* 2 - 0.3.0 ... 0.4.5
|
2015-10-07 23:11:50 +00:00
|
|
|
* + Added signature and originating peer to multicast frame
|
|
|
|
* + Double size of multicast frame bloom filter
|
2013-10-05 14:19:12 +00:00
|
|
|
* 3 - 0.5.0 ... 0.6.0
|
2015-10-07 23:11:50 +00:00
|
|
|
* + Yet another multicast redesign
|
|
|
|
* + New crypto completely changes key agreement cipher
|
2015-10-07 17:30:47 +00:00
|
|
|
* 4 - 0.6.0 ... 1.0.6
|
2015-10-07 23:11:50 +00:00
|
|
|
* + New identity format based on hashcash design
|
2015-10-07 17:30:47 +00:00
|
|
|
* 5 - 1.0.6 ... CURRENT
|
2015-10-07 23:11:50 +00:00
|
|
|
* + Supports circuit test, proof of work, and echo
|
|
|
|
* + Otherwise backward compatible with 4
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2015-10-07 17:30:47 +00:00
|
|
|
#define ZT_PROTO_VERSION 5
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2014-09-30 23:28:25 +00:00
|
|
|
/**
|
|
|
|
* Minimum supported protocol version
|
|
|
|
*/
|
|
|
|
#define ZT_PROTO_VERSION_MIN 4
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* Maximum hop count allowed by packet structure (3 bits, 0-7)
|
2015-07-13 17:03:04 +00:00
|
|
|
*
|
2015-07-06 19:34:35 +00:00
|
|
|
* This is a protocol constant. It's the maximum allowed by the length
|
|
|
|
* of the hop counter -- three bits. See node/Constants.hpp for the
|
|
|
|
* pragmatic forwarding limit, which is typically lower.
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
|
|
|
#define ZT_PROTO_MAX_HOPS 7
|
|
|
|
|
2014-10-02 17:54:34 +00:00
|
|
|
/**
|
2015-07-06 19:34:35 +00:00
|
|
|
* Cipher suite: Curve25519/Poly1305/Salsa20/12/NOCRYPT
|
2014-10-02 17:54:34 +00:00
|
|
|
*
|
|
|
|
* This specifies Poly1305 MAC using a 32-bit key derived from the first
|
|
|
|
* 32 bytes of a Salsa20/12 keystream as in the Salsa20/12 cipher suite,
|
|
|
|
* but the payload is not encrypted. This is currently only used to send
|
|
|
|
* HELLO since that's the public key specification packet and must be
|
|
|
|
* sent in the clear. Key agreement is performed using Curve25519 elliptic
|
|
|
|
* curve Diffie-Hellman.
|
|
|
|
*/
|
2014-10-02 18:13:53 +00:00
|
|
|
#define ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_NONE 0
|
2014-10-02 17:54:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cipher suite: Curve25519/Poly1305/Salsa20/12
|
|
|
|
*
|
|
|
|
* This specifies Poly1305 using the first 32 bytes of a Salsa20/12 key
|
|
|
|
* stream as its one-time-use key followed by payload encryption with
|
|
|
|
* the remaining Salsa20/12 key stream. Key agreement is performed using
|
|
|
|
* Curve25519 elliptic curve Diffie-Hellman.
|
|
|
|
*/
|
2014-10-02 18:13:53 +00:00
|
|
|
#define ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012 1
|
2014-10-02 17:54:34 +00:00
|
|
|
|
2015-05-15 00:41:05 +00:00
|
|
|
/**
|
|
|
|
* Cipher suite: PFS negotiated ephemeral cipher suite and authentication
|
|
|
|
*
|
|
|
|
* This message is encrypted with the latest negotiated ephemeral (PFS)
|
|
|
|
* key pair and cipher suite. If authentication fails, VERB_SET_EPHEMERAL_KEY
|
2015-07-06 19:34:35 +00:00
|
|
|
* may be sent to renegotiate ephemeral keys.
|
2015-05-15 00:41:05 +00:00
|
|
|
*/
|
|
|
|
#define ZT_PROTO_CIPHER_SUITE__EPHEMERAL 7
|
|
|
|
|
2014-10-02 17:54:34 +00:00
|
|
|
/**
|
2015-05-14 01:53:37 +00:00
|
|
|
* DEPRECATED payload encrypted flag, will be removed for re-use soon.
|
2014-10-02 17:54:34 +00:00
|
|
|
*
|
2015-05-14 01:53:37 +00:00
|
|
|
* This has been replaced by the two-bit cipher suite selection field where
|
2015-07-06 19:34:35 +00:00
|
|
|
* a value of 0 indicates unencrypted (but authenticated) messages.
|
2014-10-02 17:54:34 +00:00
|
|
|
*/
|
2015-05-14 01:53:37 +00:00
|
|
|
#define ZT_PROTO_FLAG_ENCRYPTED 0x80
|
2014-10-02 17:54:34 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* Header flag indicating that a packet is fragmented
|
|
|
|
*
|
|
|
|
* If this flag is set, the receiver knows to expect more than one fragment.
|
|
|
|
* See Packet::Fragment for details.
|
|
|
|
*/
|
|
|
|
#define ZT_PROTO_FLAG_FRAGMENTED 0x40
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verb flag indicating payload is compressed with LZ4
|
|
|
|
*/
|
|
|
|
#define ZT_PROTO_VERB_FLAG_COMPRESSED 0x80
|
|
|
|
|
2013-10-18 21:39:48 +00:00
|
|
|
/**
|
|
|
|
* Rounds used for Salsa20 encryption in ZT
|
2015-07-06 19:34:35 +00:00
|
|
|
*
|
|
|
|
* Discussion:
|
|
|
|
*
|
|
|
|
* DJB (Salsa20's designer) designed Salsa20 with a significant margin of 20
|
|
|
|
* rounds, but has said repeatedly that 12 is likely sufficient. So far (as of
|
|
|
|
* July 2015) there are no published attacks against 12 rounds, let alone 20.
|
|
|
|
*
|
|
|
|
* In cryptography, a "break" means something different from what it means in
|
|
|
|
* common discussion. If a cipher is 256 bits strong and someone finds a way
|
|
|
|
* to reduce key search to 254 bits, this constitues a "break" in the academic
|
|
|
|
* literature. 254 bits is still far beyond what can be leveraged to accomplish
|
|
|
|
* a "break" as most people would understand it -- the actual decryption and
|
|
|
|
* reading of traffic.
|
|
|
|
*
|
|
|
|
* Nevertheless, "attacks only get better" as cryptographers like to say. As
|
|
|
|
* a result, they recommend not using anything that's shown any weakness even
|
|
|
|
* if that weakness is so far only meaningful to academics. It may be a sign
|
|
|
|
* of a deeper problem.
|
|
|
|
*
|
|
|
|
* So why choose a lower round count?
|
|
|
|
*
|
|
|
|
* Turns out the speed difference is nontrivial. On a Macbook Pro (Core i3) 20
|
|
|
|
* rounds of SSE-optimized Salsa20 achieves ~508mb/sec/core, while 12 rounds
|
|
|
|
* hits ~832mb/sec/core. ZeroTier is designed for multiple objectives:
|
|
|
|
* security, simplicity, and performance. In this case a deference was made
|
|
|
|
* for performance.
|
|
|
|
*
|
|
|
|
* Meta discussion:
|
|
|
|
*
|
|
|
|
* The cipher is not the thing you should be paranoid about.
|
|
|
|
*
|
|
|
|
* I'll qualify that. If the cipher is known to be weak, like RC4, or has a
|
|
|
|
* key size that is too small, like DES, then yes you should worry about
|
|
|
|
* the cipher.
|
|
|
|
*
|
|
|
|
* But if the cipher is strong and your adversary is anyone other than the
|
|
|
|
* intelligence apparatus of a major superpower, you are fine in that
|
|
|
|
* department.
|
|
|
|
*
|
|
|
|
* Go ahead. Search for the last ten vulnerabilities discovered in SSL. Not
|
|
|
|
* a single one involved the breaking of a cipher. Now broaden your search.
|
|
|
|
* Look for issues with SSH, IPSec, etc. The only cipher-related issues you
|
|
|
|
* will find might involve the use of RC4 or MD5, algorithms with known
|
|
|
|
* issues or small key/digest sizes. But even weak ciphers are difficult to
|
|
|
|
* exploit in the real world -- you usually need a lot of data and a lot of
|
|
|
|
* compute time. No, virtually EVERY security vulnerability you will find
|
|
|
|
* involves a problem with the IMPLEMENTATION not with the cipher.
|
|
|
|
*
|
|
|
|
* A flaw in ZeroTier's protocol or code is incredibly, unbelievably
|
|
|
|
* more likely than a flaw in Salsa20 or any other cipher or cryptographic
|
|
|
|
* primitive it uses. We're talking odds of dying in a car wreck vs. odds of
|
|
|
|
* being personally impacted on the head by a meteorite. Nobody without a
|
|
|
|
* billion dollar budget is going to break into your network by actually
|
|
|
|
* cracking Salsa20/12 (or even /8) in the field.
|
|
|
|
*
|
|
|
|
* So stop worrying about the cipher unless you are, say, the Kremlin and your
|
|
|
|
* adversary is the NSA and the GCHQ. In that case... well that's above my
|
|
|
|
* pay grade. I'll just say defense in depth.
|
2013-10-18 21:39:48 +00:00
|
|
|
*/
|
|
|
|
#define ZT_PROTO_SALSA20_ROUNDS 12
|
|
|
|
|
2015-07-06 19:34:35 +00:00
|
|
|
// Field indexes in packet header
|
2013-07-04 20:56:19 +00:00
|
|
|
#define ZT_PACKET_IDX_IV 0
|
|
|
|
#define ZT_PACKET_IDX_DEST 8
|
|
|
|
#define ZT_PACKET_IDX_SOURCE 13
|
|
|
|
#define ZT_PACKET_IDX_FLAGS 18
|
2013-09-16 17:02:10 +00:00
|
|
|
#define ZT_PACKET_IDX_MAC 19
|
2013-07-04 20:56:19 +00:00
|
|
|
#define ZT_PACKET_IDX_VERB 27
|
|
|
|
#define ZT_PACKET_IDX_PAYLOAD 28
|
|
|
|
|
|
|
|
/**
|
2013-09-26 21:45:19 +00:00
|
|
|
* Packet buffer size (can be changed)
|
2015-07-06 19:34:35 +00:00
|
|
|
*
|
|
|
|
* The current value is big enough for ZT_MAX_PACKET_FRAGMENTS, the pragmatic
|
|
|
|
* packet fragment limit, times the default UDP MTU. Most packets won't be
|
|
|
|
* this big.
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2013-09-26 21:45:19 +00:00
|
|
|
#define ZT_PROTO_MAX_PACKET_LENGTH (ZT_MAX_PACKET_FRAGMENTS * ZT_UDP_DEFAULT_PAYLOAD_MTU)
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
2015-07-06 19:34:35 +00:00
|
|
|
* Minimum viable packet length (a.k.a. header length)
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
|
|
|
#define ZT_PROTO_MIN_PACKET_LENGTH ZT_PACKET_IDX_PAYLOAD
|
|
|
|
|
2015-07-06 19:34:35 +00:00
|
|
|
// Indexes of fields in fragment header
|
2013-07-04 20:56:19 +00:00
|
|
|
#define ZT_PACKET_FRAGMENT_IDX_PACKET_ID 0
|
|
|
|
#define ZT_PACKET_FRAGMENT_IDX_DEST 8
|
|
|
|
#define ZT_PACKET_FRAGMENT_IDX_FRAGMENT_INDICATOR 13
|
|
|
|
#define ZT_PACKET_FRAGMENT_IDX_FRAGMENT_NO 14
|
|
|
|
#define ZT_PACKET_FRAGMENT_IDX_HOPS 15
|
|
|
|
#define ZT_PACKET_FRAGMENT_IDX_PAYLOAD 16
|
|
|
|
|
|
|
|
/**
|
2015-07-06 19:34:35 +00:00
|
|
|
* Magic number found at ZT_PACKET_FRAGMENT_IDX_FRAGMENT_INDICATOR
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
|
|
|
#define ZT_PACKET_FRAGMENT_INDICATOR ZT_ADDRESS_RESERVED_PREFIX
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Minimum viable fragment length
|
|
|
|
*/
|
|
|
|
#define ZT_PROTO_MIN_FRAGMENT_LENGTH ZT_PACKET_FRAGMENT_IDX_PAYLOAD
|
|
|
|
|
2015-05-15 00:41:05 +00:00
|
|
|
// Ephemeral key record flags
|
2015-07-06 19:34:35 +00:00
|
|
|
#define ZT_PROTO_EPHEMERAL_KEY_FLAG_FIPS 0x01 // future use
|
2015-05-15 00:41:05 +00:00
|
|
|
|
|
|
|
// Ephemeral key record symmetric cipher types
|
|
|
|
#define ZT_PROTO_EPHEMERAL_KEY_SYMMETRIC_CIPHER_SALSA2012_POLY1305 0x01
|
|
|
|
#define ZT_PROTO_EPHEMERAL_KEY_SYMMETRIC_CIPHER_AES256_GCM 0x02
|
|
|
|
|
|
|
|
// Ephemeral key record public key types
|
|
|
|
#define ZT_PROTO_EPHEMERAL_KEY_PK_C25519 0x01
|
|
|
|
#define ZT_PROTO_EPHEMERAL_KEY_PK_NISTP256 0x02
|
|
|
|
|
2013-08-03 16:53:46 +00:00
|
|
|
// Field incides for parsing verbs -------------------------------------------
|
2013-08-02 21:17:34 +00:00
|
|
|
|
2014-09-26 21:18:25 +00:00
|
|
|
// Some verbs have variable-length fields. Those aren't fully defined here
|
|
|
|
// yet-- instead they are parsed using relative indexes in IncomingPacket.
|
|
|
|
// See their respective handler functions.
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
#define ZT_PROTO_VERB_HELLO_IDX_PROTOCOL_VERSION (ZT_PACKET_IDX_PAYLOAD)
|
|
|
|
#define ZT_PROTO_VERB_HELLO_IDX_MAJOR_VERSION (ZT_PROTO_VERB_HELLO_IDX_PROTOCOL_VERSION + 1)
|
|
|
|
#define ZT_PROTO_VERB_HELLO_IDX_MINOR_VERSION (ZT_PROTO_VERB_HELLO_IDX_MAJOR_VERSION + 1)
|
|
|
|
#define ZT_PROTO_VERB_HELLO_IDX_REVISION (ZT_PROTO_VERB_HELLO_IDX_MINOR_VERSION + 1)
|
|
|
|
#define ZT_PROTO_VERB_HELLO_IDX_TIMESTAMP (ZT_PROTO_VERB_HELLO_IDX_REVISION + 2)
|
|
|
|
#define ZT_PROTO_VERB_HELLO_IDX_IDENTITY (ZT_PROTO_VERB_HELLO_IDX_TIMESTAMP + 8)
|
2013-08-02 21:17:34 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
#define ZT_PROTO_VERB_ERROR_IDX_IN_RE_VERB (ZT_PACKET_IDX_PAYLOAD)
|
|
|
|
#define ZT_PROTO_VERB_ERROR_IDX_IN_RE_PACKET_ID (ZT_PROTO_VERB_ERROR_IDX_IN_RE_VERB + 1)
|
|
|
|
#define ZT_PROTO_VERB_ERROR_IDX_ERROR_CODE (ZT_PROTO_VERB_ERROR_IDX_IN_RE_PACKET_ID + 8)
|
|
|
|
#define ZT_PROTO_VERB_ERROR_IDX_PAYLOAD (ZT_PROTO_VERB_ERROR_IDX_ERROR_CODE + 1)
|
2013-08-02 21:17:34 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
#define ZT_PROTO_VERB_OK_IDX_IN_RE_VERB (ZT_PACKET_IDX_PAYLOAD)
|
|
|
|
#define ZT_PROTO_VERB_OK_IDX_IN_RE_PACKET_ID (ZT_PROTO_VERB_OK_IDX_IN_RE_VERB + 1)
|
|
|
|
#define ZT_PROTO_VERB_OK_IDX_PAYLOAD (ZT_PROTO_VERB_OK_IDX_IN_RE_PACKET_ID + 8)
|
2013-08-02 21:17:34 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
#define ZT_PROTO_VERB_WHOIS_IDX_ZTADDRESS (ZT_PACKET_IDX_PAYLOAD)
|
2013-08-02 21:17:34 +00:00
|
|
|
|
2013-10-17 10:41:52 +00:00
|
|
|
#define ZT_PROTO_VERB_RENDEZVOUS_IDX_FLAGS (ZT_PACKET_IDX_PAYLOAD)
|
|
|
|
#define ZT_PROTO_VERB_RENDEZVOUS_IDX_ZTADDRESS (ZT_PROTO_VERB_RENDEZVOUS_IDX_FLAGS + 1)
|
2013-07-04 20:56:19 +00:00
|
|
|
#define ZT_PROTO_VERB_RENDEZVOUS_IDX_PORT (ZT_PROTO_VERB_RENDEZVOUS_IDX_ZTADDRESS + 5)
|
|
|
|
#define ZT_PROTO_VERB_RENDEZVOUS_IDX_ADDRLEN (ZT_PROTO_VERB_RENDEZVOUS_IDX_PORT + 2)
|
|
|
|
#define ZT_PROTO_VERB_RENDEZVOUS_IDX_ADDRESS (ZT_PROTO_VERB_RENDEZVOUS_IDX_ADDRLEN + 1)
|
2013-08-02 21:17:34 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
#define ZT_PROTO_VERB_FRAME_IDX_NETWORK_ID (ZT_PACKET_IDX_PAYLOAD)
|
|
|
|
#define ZT_PROTO_VERB_FRAME_IDX_ETHERTYPE (ZT_PROTO_VERB_FRAME_IDX_NETWORK_ID + 8)
|
|
|
|
#define ZT_PROTO_VERB_FRAME_IDX_PAYLOAD (ZT_PROTO_VERB_FRAME_IDX_ETHERTYPE + 2)
|
2013-08-02 21:17:34 +00:00
|
|
|
|
2014-06-11 04:41:34 +00:00
|
|
|
#define ZT_PROTO_VERB_EXT_FRAME_IDX_NETWORK_ID (ZT_PACKET_IDX_PAYLOAD)
|
|
|
|
#define ZT_PROTO_VERB_EXT_FRAME_LEN_NETWORK_ID 8
|
2014-06-12 18:40:30 +00:00
|
|
|
#define ZT_PROTO_VERB_EXT_FRAME_IDX_FLAGS (ZT_PROTO_VERB_EXT_FRAME_IDX_NETWORK_ID + ZT_PROTO_VERB_EXT_FRAME_LEN_NETWORK_ID)
|
|
|
|
#define ZT_PROTO_VERB_EXT_FRAME_LEN_FLAGS 1
|
2014-09-30 23:28:25 +00:00
|
|
|
#define ZT_PROTO_VERB_EXT_FRAME_IDX_COM (ZT_PROTO_VERB_EXT_FRAME_IDX_FLAGS + ZT_PROTO_VERB_EXT_FRAME_LEN_FLAGS)
|
2014-06-12 18:40:30 +00:00
|
|
|
#define ZT_PROTO_VERB_EXT_FRAME_IDX_TO (ZT_PROTO_VERB_EXT_FRAME_IDX_FLAGS + ZT_PROTO_VERB_EXT_FRAME_LEN_FLAGS)
|
2014-06-11 04:41:34 +00:00
|
|
|
#define ZT_PROTO_VERB_EXT_FRAME_LEN_TO 6
|
|
|
|
#define ZT_PROTO_VERB_EXT_FRAME_IDX_FROM (ZT_PROTO_VERB_EXT_FRAME_IDX_TO + ZT_PROTO_VERB_EXT_FRAME_LEN_TO)
|
|
|
|
#define ZT_PROTO_VERB_EXT_FRAME_LEN_FROM 6
|
|
|
|
#define ZT_PROTO_VERB_EXT_FRAME_IDX_ETHERTYPE (ZT_PROTO_VERB_EXT_FRAME_IDX_FROM + ZT_PROTO_VERB_EXT_FRAME_LEN_FROM)
|
|
|
|
#define ZT_PROTO_VERB_EXT_FRAME_LEN_ETHERTYPE 2
|
2014-06-21 16:25:10 +00:00
|
|
|
#define ZT_PROTO_VERB_EXT_FRAME_IDX_PAYLOAD (ZT_PROTO_VERB_EXT_FRAME_IDX_ETHERTYPE + ZT_PROTO_VERB_EXT_FRAME_LEN_ETHERTYPE)
|
2014-06-11 04:41:34 +00:00
|
|
|
|
2013-08-02 21:17:34 +00:00
|
|
|
#define ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_NETWORK_ID (ZT_PACKET_IDX_PAYLOAD)
|
|
|
|
#define ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT_LEN (ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_NETWORK_ID + 8)
|
|
|
|
#define ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT (ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST_IDX_DICT_LEN + 2)
|
|
|
|
|
2014-09-26 21:18:25 +00:00
|
|
|
#define ZT_PROTO_VERB_MULTICAST_GATHER_IDX_NETWORK_ID (ZT_PACKET_IDX_PAYLOAD)
|
|
|
|
#define ZT_PROTO_VERB_MULTICAST_GATHER_IDX_FLAGS (ZT_PROTO_VERB_MULTICAST_GATHER_IDX_NETWORK_ID + 8)
|
|
|
|
#define ZT_PROTO_VERB_MULTICAST_GATHER_IDX_MAC (ZT_PROTO_VERB_MULTICAST_GATHER_IDX_FLAGS + 1)
|
|
|
|
#define ZT_PROTO_VERB_MULTICAST_GATHER_IDX_ADI (ZT_PROTO_VERB_MULTICAST_GATHER_IDX_MAC + 6)
|
|
|
|
#define ZT_PROTO_VERB_MULTICAST_GATHER_IDX_GATHER_LIMIT (ZT_PROTO_VERB_MULTICAST_GATHER_IDX_ADI + 4)
|
|
|
|
|
2014-10-09 19:42:25 +00:00
|
|
|
// Note: COM, GATHER_LIMIT, and SOURCE_MAC are optional, and so are specified without size
|
|
|
|
#define ZT_PROTO_VERB_MULTICAST_FRAME_IDX_NETWORK_ID (ZT_PACKET_IDX_PAYLOAD)
|
2014-09-26 21:18:25 +00:00
|
|
|
#define ZT_PROTO_VERB_MULTICAST_FRAME_IDX_FLAGS (ZT_PROTO_VERB_MULTICAST_FRAME_IDX_NETWORK_ID + 8)
|
2014-10-09 19:42:25 +00:00
|
|
|
#define ZT_PROTO_VERB_MULTICAST_FRAME_IDX_COM (ZT_PROTO_VERB_MULTICAST_FRAME_IDX_FLAGS + 1)
|
2014-09-26 21:18:25 +00:00
|
|
|
#define ZT_PROTO_VERB_MULTICAST_FRAME_IDX_GATHER_LIMIT (ZT_PROTO_VERB_MULTICAST_FRAME_IDX_FLAGS + 1)
|
2014-10-09 19:42:25 +00:00
|
|
|
#define ZT_PROTO_VERB_MULTICAST_FRAME_IDX_SOURCE_MAC (ZT_PROTO_VERB_MULTICAST_FRAME_IDX_FLAGS + 1)
|
|
|
|
#define ZT_PROTO_VERB_MULTICAST_FRAME_IDX_DEST_MAC (ZT_PROTO_VERB_MULTICAST_FRAME_IDX_FLAGS + 1)
|
|
|
|
#define ZT_PROTO_VERB_MULTICAST_FRAME_IDX_DEST_ADI (ZT_PROTO_VERB_MULTICAST_FRAME_IDX_DEST_MAC + 6)
|
|
|
|
#define ZT_PROTO_VERB_MULTICAST_FRAME_IDX_ETHERTYPE (ZT_PROTO_VERB_MULTICAST_FRAME_IDX_DEST_ADI + 4)
|
2014-09-30 23:28:25 +00:00
|
|
|
#define ZT_PROTO_VERB_MULTICAST_FRAME_IDX_FRAME (ZT_PROTO_VERB_MULTICAST_FRAME_IDX_ETHERTYPE + 2)
|
2014-09-26 21:18:25 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
#define ZT_PROTO_VERB_HELLO__OK__IDX_TIMESTAMP (ZT_PROTO_VERB_OK_IDX_PAYLOAD)
|
2013-09-20 17:36:14 +00:00
|
|
|
#define ZT_PROTO_VERB_HELLO__OK__IDX_PROTOCOL_VERSION (ZT_PROTO_VERB_HELLO__OK__IDX_TIMESTAMP + 8)
|
|
|
|
#define ZT_PROTO_VERB_HELLO__OK__IDX_MAJOR_VERSION (ZT_PROTO_VERB_HELLO__OK__IDX_PROTOCOL_VERSION + 1)
|
|
|
|
#define ZT_PROTO_VERB_HELLO__OK__IDX_MINOR_VERSION (ZT_PROTO_VERB_HELLO__OK__IDX_MAJOR_VERSION + 1)
|
|
|
|
#define ZT_PROTO_VERB_HELLO__OK__IDX_REVISION (ZT_PROTO_VERB_HELLO__OK__IDX_MINOR_VERSION + 1)
|
2013-08-03 16:53:46 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
#define ZT_PROTO_VERB_WHOIS__OK__IDX_IDENTITY (ZT_PROTO_VERB_OK_IDX_PAYLOAD)
|
2013-08-03 16:53:46 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
#define ZT_PROTO_VERB_WHOIS__ERROR__IDX_ZTADDRESS (ZT_PROTO_VERB_ERROR_IDX_PAYLOAD)
|
|
|
|
|
2013-08-03 16:53:46 +00:00
|
|
|
#define ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST__OK__IDX_NETWORK_ID (ZT_PROTO_VERB_OK_IDX_PAYLOAD)
|
|
|
|
#define ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST__OK__IDX_DICT_LEN (ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST__OK__IDX_NETWORK_ID + 8)
|
|
|
|
#define ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST__OK__IDX_DICT (ZT_PROTO_VERB_NETWORK_CONFIG_REQUEST__OK__IDX_DICT_LEN + 2)
|
|
|
|
|
2014-10-02 20:50:37 +00:00
|
|
|
#define ZT_PROTO_VERB_MULTICAST_GATHER__OK__IDX_NETWORK_ID (ZT_PROTO_VERB_OK_IDX_PAYLOAD)
|
|
|
|
#define ZT_PROTO_VERB_MULTICAST_GATHER__OK__IDX_MAC (ZT_PROTO_VERB_MULTICAST_GATHER__OK__IDX_NETWORK_ID + 8)
|
|
|
|
#define ZT_PROTO_VERB_MULTICAST_GATHER__OK__IDX_ADI (ZT_PROTO_VERB_MULTICAST_GATHER__OK__IDX_MAC + 6)
|
|
|
|
#define ZT_PROTO_VERB_MULTICAST_GATHER__OK__IDX_GATHER_RESULTS (ZT_PROTO_VERB_MULTICAST_GATHER__OK__IDX_ADI + 4)
|
|
|
|
|
|
|
|
#define ZT_PROTO_VERB_MULTICAST_FRAME__OK__IDX_NETWORK_ID (ZT_PROTO_VERB_OK_IDX_PAYLOAD)
|
|
|
|
#define ZT_PROTO_VERB_MULTICAST_FRAME__OK__IDX_MAC (ZT_PROTO_VERB_MULTICAST_FRAME__OK__IDX_NETWORK_ID + 8)
|
|
|
|
#define ZT_PROTO_VERB_MULTICAST_FRAME__OK__IDX_ADI (ZT_PROTO_VERB_MULTICAST_FRAME__OK__IDX_MAC + 6)
|
|
|
|
#define ZT_PROTO_VERB_MULTICAST_FRAME__OK__IDX_FLAGS (ZT_PROTO_VERB_MULTICAST_FRAME__OK__IDX_ADI + 4)
|
2014-10-29 23:24:19 +00:00
|
|
|
#define ZT_PROTO_VERB_MULTICAST_FRAME__OK__IDX_COM_AND_GATHER_RESULTS (ZT_PROTO_VERB_MULTICAST_FRAME__OK__IDX_FLAGS + 1)
|
2014-10-02 20:50:37 +00:00
|
|
|
|
2013-08-03 16:53:46 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ZeroTier packet
|
2015-07-13 17:03:04 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* Packet format:
|
2015-10-08 16:05:25 +00:00
|
|
|
* <[8] 64-bit random packet ID and crypto initialization vector>
|
2013-07-04 20:56:19 +00:00
|
|
|
* <[5] destination ZT address>
|
|
|
|
* <[5] source ZT address>
|
2014-10-02 17:54:34 +00:00
|
|
|
* <[1] flags/cipher (top 5 bits) and ZT hop count (last 3 bits)>
|
2015-10-08 16:05:25 +00:00
|
|
|
* <[8] 64-bit MAC>
|
2013-07-04 20:56:19 +00:00
|
|
|
* [... -- begin encryption envelope -- ...]
|
2014-10-02 17:54:34 +00:00
|
|
|
* <[1] encrypted flags (top 3 bits) and verb (last 5 bits)>
|
2013-07-04 20:56:19 +00:00
|
|
|
* [... verb-specific payload ...]
|
2015-07-13 17:03:04 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* Packets smaller than 28 bytes are invalid and silently discarded.
|
|
|
|
*
|
2015-05-15 00:41:05 +00:00
|
|
|
* The flags/cipher/hops bit field is: FFCCCHHH where C is a 3-bit cipher
|
|
|
|
* selection allowing up to 7 cipher suites, F is outside-envelope flags,
|
2015-05-14 01:53:37 +00:00
|
|
|
* and H is hop count.
|
2014-10-02 17:54:34 +00:00
|
|
|
*
|
|
|
|
* The three-bit hop count is the only part of a packet that is mutable in
|
|
|
|
* transit without invalidating the MAC. All other bits in the packet are
|
|
|
|
* immutable. This is because intermediate nodes can increment the hop
|
|
|
|
* count up to 7 (protocol max).
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
2015-10-07 23:25:08 +00:00
|
|
|
* A hop count of 7 also indicates that receiving peers should not attempt
|
|
|
|
* to learn direct paths from this packet. (Right now direct paths are only
|
|
|
|
* learned from direct packets anyway.)
|
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* http://tonyarcieri.com/all-the-crypto-code-youve-ever-written-is-probably-broken
|
|
|
|
*
|
|
|
|
* For unencrypted packets, MAC is computed on plaintext. Only HELLO is ever
|
|
|
|
* sent in the clear, as it's the "here is my public key" message.
|
|
|
|
*/
|
|
|
|
class Packet : public Buffer<ZT_PROTO_MAX_PACKET_LENGTH>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* A packet fragment
|
2015-07-13 17:03:04 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* Fragments are sent if a packet is larger than UDP MTU. The first fragment
|
|
|
|
* is sent with its normal header with the fragmented flag set. Remaining
|
|
|
|
* fragments are sent this way.
|
2015-07-13 17:03:04 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* The fragmented bit indicates that there is at least one fragment. Fragments
|
|
|
|
* themselves contain the total, so the receiver must "learn" this from the
|
|
|
|
* first fragment it receives.
|
2015-07-13 17:03:04 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* Fragments are sent with the following format:
|
|
|
|
* <[8] packet ID of packet whose fragment this belongs to>
|
|
|
|
* <[5] destination ZT address>
|
|
|
|
* <[1] 0xff, a reserved address, signals that this isn't a normal packet>
|
|
|
|
* <[1] total fragments (most significant 4 bits), fragment no (LS 4 bits)>
|
2014-10-02 17:54:34 +00:00
|
|
|
* <[1] ZT hop count (top 5 bits unused and must be zero)>
|
2013-07-04 20:56:19 +00:00
|
|
|
* <[...] fragment data>
|
|
|
|
*
|
|
|
|
* The protocol supports a maximum of 16 fragments. If a fragment is received
|
|
|
|
* before its main packet header, it should be cached for a brief period of
|
|
|
|
* time to see if its parent arrives. Loss of any fragment constitutes packet
|
|
|
|
* loss; there is no retransmission mechanism. The receiver must wait for full
|
|
|
|
* receipt to authenticate and decrypt; there is no per-fragment MAC. (But if
|
|
|
|
* fragments are corrupt, the MAC will fail for the whole assembled packet.)
|
|
|
|
*/
|
|
|
|
class Fragment : public Buffer<ZT_PROTO_MAX_PACKET_LENGTH>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Fragment() :
|
|
|
|
Buffer<ZT_PROTO_MAX_PACKET_LENGTH>()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<unsigned int C2>
|
|
|
|
Fragment(const Buffer<C2> &b)
|
|
|
|
throw(std::out_of_range) :
|
|
|
|
Buffer<ZT_PROTO_MAX_PACKET_LENGTH>(b)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-04-08 02:31:11 +00:00
|
|
|
Fragment(const void *data,unsigned int len) :
|
|
|
|
Buffer<ZT_PROTO_MAX_PACKET_LENGTH>(data,len)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* Initialize from a packet
|
2015-07-13 17:03:04 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param p Original assembled packet
|
|
|
|
* @param fragStart Start of fragment (raw index in packet data)
|
|
|
|
* @param fragLen Length of fragment in bytes
|
|
|
|
* @param fragNo Which fragment (>= 1, since 0 is Packet with end chopped off)
|
|
|
|
* @param fragTotal Total number of fragments (including 0)
|
|
|
|
* @throws std::out_of_range Packet size would exceed buffer
|
|
|
|
*/
|
|
|
|
Fragment(const Packet &p,unsigned int fragStart,unsigned int fragLen,unsigned int fragNo,unsigned int fragTotal)
|
|
|
|
throw(std::out_of_range)
|
|
|
|
{
|
|
|
|
init(p,fragStart,fragLen,fragNo,fragTotal);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize from a packet
|
2015-07-13 17:03:04 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param p Original assembled packet
|
|
|
|
* @param fragStart Start of fragment (raw index in packet data)
|
|
|
|
* @param fragLen Length of fragment in bytes
|
|
|
|
* @param fragNo Which fragment (>= 1, since 0 is Packet with end chopped off)
|
|
|
|
* @param fragTotal Total number of fragments (including 0)
|
|
|
|
* @throws std::out_of_range Packet size would exceed buffer
|
|
|
|
*/
|
|
|
|
inline void init(const Packet &p,unsigned int fragStart,unsigned int fragLen,unsigned int fragNo,unsigned int fragTotal)
|
|
|
|
throw(std::out_of_range)
|
|
|
|
{
|
|
|
|
if ((fragStart + fragLen) > p.size())
|
|
|
|
throw std::out_of_range("Packet::Fragment: tried to construct fragment of packet past its length");
|
|
|
|
setSize(fragLen + ZT_PROTO_MIN_FRAGMENT_LENGTH);
|
|
|
|
|
|
|
|
// NOTE: this copies both the IV/packet ID and the destination address.
|
2014-10-29 00:25:34 +00:00
|
|
|
memcpy(field(ZT_PACKET_FRAGMENT_IDX_PACKET_ID,13),p.field(ZT_PACKET_IDX_IV,13),13);
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-07-09 18:06:55 +00:00
|
|
|
(*this)[ZT_PACKET_FRAGMENT_IDX_FRAGMENT_INDICATOR] = ZT_PACKET_FRAGMENT_INDICATOR;
|
|
|
|
(*this)[ZT_PACKET_FRAGMENT_IDX_FRAGMENT_NO] = (char)(((fragTotal & 0xf) << 4) | (fragNo & 0xf));
|
|
|
|
(*this)[ZT_PACKET_FRAGMENT_IDX_HOPS] = 0;
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2014-10-29 00:25:34 +00:00
|
|
|
memcpy(field(ZT_PACKET_FRAGMENT_IDX_PAYLOAD,fragLen),p.field(fragStart,fragLen),fragLen);
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get this fragment's destination
|
2015-07-13 17:03:04 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* @return Destination ZT address
|
|
|
|
*/
|
2013-07-29 17:56:20 +00:00
|
|
|
inline Address destination() const { return Address(field(ZT_PACKET_FRAGMENT_IDX_DEST,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return True if fragment is of a valid length
|
|
|
|
*/
|
2013-07-09 18:06:55 +00:00
|
|
|
inline bool lengthValid() const { return (size() >= ZT_PACKET_FRAGMENT_IDX_PAYLOAD); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ID of packet this is a fragment of
|
|
|
|
*/
|
|
|
|
inline uint64_t packetId() const { return at<uint64_t>(ZT_PACKET_FRAGMENT_IDX_PACKET_ID); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Total number of fragments in packet
|
|
|
|
*/
|
2013-07-09 18:06:55 +00:00
|
|
|
inline unsigned int totalFragments() const { return (((unsigned int)((*this)[ZT_PACKET_FRAGMENT_IDX_FRAGMENT_NO]) >> 4) & 0xf); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Fragment number of this fragment
|
|
|
|
*/
|
2013-07-09 18:06:55 +00:00
|
|
|
inline unsigned int fragmentNumber() const { return ((unsigned int)((*this)[ZT_PACKET_FRAGMENT_IDX_FRAGMENT_NO]) & 0xf); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Fragment ZT hop count
|
|
|
|
*/
|
2013-07-09 18:06:55 +00:00
|
|
|
inline unsigned int hops() const { return (unsigned int)((*this)[ZT_PACKET_FRAGMENT_IDX_HOPS]); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Increment this packet's hop count
|
|
|
|
*/
|
|
|
|
inline void incrementHops()
|
|
|
|
{
|
2013-07-09 18:06:55 +00:00
|
|
|
(*this)[ZT_PACKET_FRAGMENT_IDX_HOPS] = (((*this)[ZT_PACKET_FRAGMENT_IDX_HOPS]) + 1) & ZT_PROTO_MAX_HOPS;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-07-09 18:06:55 +00:00
|
|
|
* @return Length of payload in bytes
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2013-07-09 18:06:55 +00:00
|
|
|
inline unsigned int payloadLength() const { return ((size() > ZT_PACKET_FRAGMENT_IDX_PAYLOAD) ? (size() - ZT_PACKET_FRAGMENT_IDX_PAYLOAD) : 0); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
2013-07-09 18:06:55 +00:00
|
|
|
* @return Raw packet payload
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2013-07-09 18:06:55 +00:00
|
|
|
inline const unsigned char *payload() const
|
|
|
|
{
|
|
|
|
return field(ZT_PACKET_FRAGMENT_IDX_PAYLOAD,size() - ZT_PACKET_FRAGMENT_IDX_PAYLOAD);
|
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ZeroTier protocol verbs
|
|
|
|
*/
|
|
|
|
enum Verb /* Max value: 32 (5 bits) */
|
|
|
|
{
|
2015-10-07 20:35:46 +00:00
|
|
|
/**
|
|
|
|
* No operation (ignored, no reply)
|
|
|
|
*/
|
2013-07-04 20:56:19 +00:00
|
|
|
VERB_NOP = 0,
|
|
|
|
|
2015-10-07 20:35:46 +00:00
|
|
|
/**
|
|
|
|
* Announcement of a node's existence:
|
2013-07-06 17:34:35 +00:00
|
|
|
* <[1] protocol version>
|
2013-07-04 20:56:19 +00:00
|
|
|
* <[1] software major version>
|
|
|
|
* <[1] software minor version>
|
|
|
|
* <[2] software revision>
|
2013-07-06 17:34:35 +00:00
|
|
|
* <[8] timestamp (ms since epoch)>
|
|
|
|
* <[...] binary serialized identity (see Identity)>
|
2015-04-07 03:17:21 +00:00
|
|
|
* <[1] destination address type>
|
|
|
|
* [<[...] destination address>]
|
|
|
|
*
|
|
|
|
* This is the only message that ever must be sent in the clear, since it
|
|
|
|
* is used to push an identity to a new peer.
|
|
|
|
*
|
|
|
|
* The destination address is the wire address to which this packet is
|
|
|
|
* being sent, and in OK is *also* the destination address of the OK
|
|
|
|
* packet. This can be used by the receiver to detect NAT, learn its real
|
|
|
|
* external address if behind NAT, and detect changes to its external
|
|
|
|
* address that require re-establishing connectivity.
|
|
|
|
*
|
|
|
|
* Destination address types and formats (not all of these are used now):
|
2015-09-27 18:37:39 +00:00
|
|
|
* 0x00 - None -- no destination address data present
|
|
|
|
* 0x01 - Ethernet address -- format: <[6] Ethernet MAC>
|
|
|
|
* 0x04 - 6-byte IPv4 UDP address/port -- format: <[4] IP>, <[2] port>
|
|
|
|
* 0x06 - 18-byte IPv6 UDP address/port -- format: <[16] IP>, <[2] port>
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
|
|
|
* OK payload:
|
2013-07-06 17:34:35 +00:00
|
|
|
* <[8] timestamp (echoed from original HELLO)>
|
2013-09-13 18:41:20 +00:00
|
|
|
* <[1] protocol version (of responder)>
|
|
|
|
* <[1] software major version (of responder)>
|
|
|
|
* <[1] software minor version (of responder)>
|
|
|
|
* <[2] software revision (of responder)>
|
2015-04-07 03:17:21 +00:00
|
|
|
* <[1] destination address type (for this OK, not copied from HELLO)>
|
|
|
|
* [<[...] destination address>]
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
|
|
|
* ERROR has no payload.
|
|
|
|
*/
|
|
|
|
VERB_HELLO = 1,
|
|
|
|
|
2015-10-07 20:35:46 +00:00
|
|
|
/**
|
|
|
|
* Error response:
|
2013-07-06 17:34:35 +00:00
|
|
|
* <[1] in-re verb>
|
|
|
|
* <[8] in-re packet ID>
|
|
|
|
* <[1] error code>
|
|
|
|
* <[...] error-dependent payload>
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
|
|
|
VERB_ERROR = 2,
|
|
|
|
|
2015-10-07 20:35:46 +00:00
|
|
|
/**
|
|
|
|
* Success response:
|
2013-07-06 17:34:35 +00:00
|
|
|
* <[1] in-re verb>
|
|
|
|
* <[8] in-re packet ID>
|
|
|
|
* <[...] request-specific payload>
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
|
|
|
VERB_OK = 3,
|
|
|
|
|
2015-10-07 20:35:46 +00:00
|
|
|
/**
|
|
|
|
* Query an identity by address:
|
2013-07-06 17:34:35 +00:00
|
|
|
* <[5] address to look up>
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
|
|
|
* OK response payload:
|
2013-07-06 17:34:35 +00:00
|
|
|
* <[...] binary serialized identity>
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
2013-10-17 10:41:52 +00:00
|
|
|
* ERROR response payload:
|
|
|
|
* <[5] address>
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
|
|
|
VERB_WHOIS = 4,
|
|
|
|
|
2015-10-07 20:35:46 +00:00
|
|
|
/**
|
|
|
|
* Meet another node at a given protocol address:
|
2013-10-17 10:41:52 +00:00
|
|
|
* <[1] flags (unused, currently 0)>
|
2013-07-04 20:56:19 +00:00
|
|
|
* <[5] ZeroTier address of peer that might be found at this address>
|
|
|
|
* <[2] 16-bit protocol address port>
|
|
|
|
* <[1] protocol address length (4 for IPv4, 16 for IPv6)>
|
|
|
|
* <[...] protocol address (network byte order)>
|
|
|
|
*
|
|
|
|
* This is sent by a relaying node to initiate NAT traversal between two
|
|
|
|
* peers that are communicating by way of indirect relay. The relay will
|
|
|
|
* send this to both peers at the same time on a periodic basis, telling
|
|
|
|
* each where it might find the other on the network.
|
|
|
|
*
|
2013-12-31 19:03:45 +00:00
|
|
|
* Upon receipt a peer sends HELLO to establish a direct link.
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
|
|
|
* Nodes should implement rate control, limiting the rate at which they
|
|
|
|
* respond to these packets to prevent their use in DDOS attacks. Nodes
|
|
|
|
* may also ignore these messages if a peer is not known or is not being
|
|
|
|
* actively communicated with.
|
|
|
|
*
|
|
|
|
* No OK or ERROR is generated.
|
|
|
|
*/
|
|
|
|
VERB_RENDEZVOUS = 5,
|
|
|
|
|
2015-10-07 20:35:46 +00:00
|
|
|
/**
|
|
|
|
* ZT-to-ZT unicast ethernet frame (shortened EXT_FRAME):
|
2013-07-04 20:56:19 +00:00
|
|
|
* <[8] 64-bit network ID>
|
2013-07-06 17:34:35 +00:00
|
|
|
* <[2] 16-bit ethertype>
|
|
|
|
* <[...] ethernet payload>
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
|
|
|
* MAC addresses are derived from the packet's source and destination
|
2014-09-30 15:38:03 +00:00
|
|
|
* ZeroTier addresses. This is a shortened EXT_FRAME that elides full
|
|
|
|
* Ethernet framing and other optional flags and features when they
|
|
|
|
* are not necessary.
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
2013-09-27 20:03:13 +00:00
|
|
|
* ERROR may be generated if a membership certificate is needed for a
|
|
|
|
* closed network. Payload will be network ID.
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
|
|
|
VERB_FRAME = 6,
|
|
|
|
|
2015-10-07 20:35:46 +00:00
|
|
|
/**
|
|
|
|
* Full Ethernet frame with MAC addressing and optional fields:
|
2014-06-10 22:25:15 +00:00
|
|
|
* <[8] 64-bit network ID>
|
2014-09-30 15:38:03 +00:00
|
|
|
* <[1] flags>
|
|
|
|
* [<[...] certificate of network membership>]
|
2014-06-10 22:25:15 +00:00
|
|
|
* <[6] destination MAC or all zero for destination node>
|
|
|
|
* <[6] source MAC or all zero for node of origin>
|
|
|
|
* <[2] 16-bit ethertype>
|
|
|
|
* <[...] ethernet payload>
|
|
|
|
*
|
2014-09-30 15:38:03 +00:00
|
|
|
* Flags:
|
|
|
|
* 0x01 - Certificate of network membership is attached
|
|
|
|
*
|
|
|
|
* An extended frame carries full MAC addressing, making them a
|
|
|
|
* superset of VERB_FRAME. They're used for bridging or when we
|
|
|
|
* want to attach a certificate since FRAME does not support that.
|
2014-06-10 22:25:15 +00:00
|
|
|
*
|
2014-09-30 23:28:25 +00:00
|
|
|
* Multicast frames may not be sent as EXT_FRAME.
|
|
|
|
*
|
2014-06-10 22:25:15 +00:00
|
|
|
* ERROR may be generated if a membership certificate is needed for a
|
|
|
|
* closed network. Payload will be network ID.
|
|
|
|
*/
|
|
|
|
VERB_EXT_FRAME = 7,
|
2013-07-11 02:58:43 +00:00
|
|
|
|
2015-10-07 23:11:50 +00:00
|
|
|
/**
|
|
|
|
* ECHO request (a.k.a. ping):
|
|
|
|
* <[...] arbitrary payload to be echoed back>
|
|
|
|
*
|
|
|
|
* This generates OK with a copy of the transmitted payload. No ERROR
|
|
|
|
* is generated. Response to ECHO requests is optional.
|
2015-10-07 23:20:54 +00:00
|
|
|
*
|
|
|
|
* Note that fragmented ECHO packets may not work.
|
2015-10-07 23:11:50 +00:00
|
|
|
*/
|
|
|
|
VERB_ECHO = 8,
|
2013-09-26 21:45:19 +00:00
|
|
|
|
2015-10-07 20:35:46 +00:00
|
|
|
/**
|
|
|
|
* Announce interest in multicast group(s):
|
2013-09-26 21:45:19 +00:00
|
|
|
* <[8] 64-bit network ID>
|
|
|
|
* <[6] multicast Ethernet address>
|
|
|
|
* <[4] multicast additional distinguishing information (ADI)>
|
|
|
|
* [... additional tuples of network/address/adi ...]
|
|
|
|
*
|
|
|
|
* LIKEs are sent to peers with whom you have a direct peer to peer
|
2015-06-19 17:23:25 +00:00
|
|
|
* connection, and always including root servers.
|
2013-09-26 21:45:19 +00:00
|
|
|
*
|
|
|
|
* OK/ERROR are not generated.
|
2013-07-11 02:58:43 +00:00
|
|
|
*/
|
2013-09-26 21:45:19 +00:00
|
|
|
VERB_MULTICAST_LIKE = 9,
|
2013-07-27 20:20:08 +00:00
|
|
|
|
2015-10-07 20:35:46 +00:00
|
|
|
/**
|
|
|
|
* Network member certificate replication/push:
|
2013-10-07 20:13:52 +00:00
|
|
|
* <[...] serialized certificate of membership>
|
2013-10-17 10:41:52 +00:00
|
|
|
* [ ... additional certificates may follow ...]
|
2013-07-27 20:20:08 +00:00
|
|
|
*
|
2015-07-07 17:00:34 +00:00
|
|
|
* This is sent in response to ERROR_NEED_MEMBERSHIP_CERTIFICATE and may
|
|
|
|
* be pushed at any other time to keep exchanged certificates up to date.
|
|
|
|
*
|
|
|
|
* OK/ERROR are not generated.
|
2013-07-27 20:20:08 +00:00
|
|
|
*/
|
2013-07-29 20:18:29 +00:00
|
|
|
VERB_NETWORK_MEMBERSHIP_CERTIFICATE = 10,
|
2013-07-29 17:56:20 +00:00
|
|
|
|
2015-10-07 20:35:46 +00:00
|
|
|
/**
|
|
|
|
* Network configuration request:
|
2013-07-29 17:56:20 +00:00
|
|
|
* <[8] 64-bit network ID>
|
2013-07-31 13:27:55 +00:00
|
|
|
* <[2] 16-bit length of request meta-data dictionary>
|
|
|
|
* <[...] string-serialized request meta-data>
|
2015-04-02 02:09:18 +00:00
|
|
|
* [<[8] 64-bit revision of netconf we currently have>]
|
2013-07-29 17:56:20 +00:00
|
|
|
*
|
|
|
|
* This message requests network configuration from a node capable of
|
2015-04-02 02:09:18 +00:00
|
|
|
* providing it. If the optional revision is included, a response is
|
2015-01-06 00:19:56 +00:00
|
|
|
* only generated if there is a newer network configuration available.
|
2013-07-29 17:56:20 +00:00
|
|
|
*
|
|
|
|
* OK response payload:
|
|
|
|
* <[8] 64-bit network ID>
|
2013-07-31 13:27:55 +00:00
|
|
|
* <[2] 16-bit length of network configuration dictionary>
|
2013-07-29 17:56:20 +00:00
|
|
|
* <[...] network configuration dictionary>
|
|
|
|
*
|
|
|
|
* OK returns a Dictionary (string serialized) containing the network's
|
|
|
|
* configuration and IP address assignment information for the querying
|
2013-07-31 13:27:55 +00:00
|
|
|
* node. It also contains a membership certificate that the querying
|
|
|
|
* node can push to other peers to demonstrate its right to speak on
|
|
|
|
* a given network.
|
2013-07-29 17:56:20 +00:00
|
|
|
*
|
2015-01-06 00:19:56 +00:00
|
|
|
* When a new network configuration is received, another config request
|
2015-04-02 02:09:18 +00:00
|
|
|
* should be sent with the new netconf's revision. This confirms receipt
|
2015-01-06 00:19:56 +00:00
|
|
|
* and also causes any subsequent changes to rapidly propagate as this
|
|
|
|
* cycle will repeat until there are no changes. This is optional but
|
|
|
|
* recommended behavior.
|
|
|
|
*
|
2013-10-17 10:41:52 +00:00
|
|
|
* ERROR response payload:
|
|
|
|
* <[8] 64-bit network ID>
|
2013-10-27 11:26:50 +00:00
|
|
|
*
|
2015-01-06 00:19:56 +00:00
|
|
|
* UNSUPPORTED_OPERATION is returned if this service is not supported,
|
|
|
|
* and OBJ_NOT_FOUND if the queried network ID was not found.
|
2013-07-29 17:56:20 +00:00
|
|
|
*/
|
|
|
|
VERB_NETWORK_CONFIG_REQUEST = 11,
|
|
|
|
|
2015-10-07 20:35:46 +00:00
|
|
|
/**
|
|
|
|
* Network configuration refresh request:
|
2015-01-06 00:19:56 +00:00
|
|
|
* <[...] array of 64-bit network IDs>
|
|
|
|
*
|
2015-07-06 19:34:35 +00:00
|
|
|
* This can be sent by the network controller to inform a node that it
|
|
|
|
* should now make a NETWORK_CONFIG_REQUEST.
|
2015-01-06 00:19:56 +00:00
|
|
|
*
|
|
|
|
* It does not generate an OK or ERROR message, and is treated only as
|
|
|
|
* a hint to refresh now.
|
|
|
|
*/
|
2014-09-19 01:28:14 +00:00
|
|
|
VERB_NETWORK_CONFIG_REFRESH = 12,
|
|
|
|
|
2015-10-07 20:35:46 +00:00
|
|
|
/**
|
|
|
|
* Request endpoints for multicast distribution:
|
2014-09-19 01:28:14 +00:00
|
|
|
* <[8] 64-bit network ID>
|
2014-10-10 00:58:31 +00:00
|
|
|
* <[1] flags>
|
2014-09-19 01:28:14 +00:00
|
|
|
* <[6] MAC address of multicast group being queried>
|
|
|
|
* <[4] 32-bit ADI for multicast group being queried>
|
2014-10-10 00:58:31 +00:00
|
|
|
* <[4] 32-bit requested max number of multicast peers>
|
|
|
|
* [<[...] network certificate of membership>]
|
|
|
|
*
|
|
|
|
* Flags:
|
|
|
|
* 0x01 - Network certificate of membership is attached
|
2014-09-19 01:28:14 +00:00
|
|
|
*
|
|
|
|
* This message asks a peer for additional known endpoints that have
|
|
|
|
* LIKEd a given multicast group. It's sent when the sender wishes
|
|
|
|
* to send multicast but does not have the desired number of recipient
|
2014-09-22 20:18:24 +00:00
|
|
|
* peers.
|
2014-09-19 01:28:14 +00:00
|
|
|
*
|
|
|
|
* OK response payload:
|
|
|
|
* <[8] 64-bit network ID>
|
|
|
|
* <[6] MAC address of multicast group being queried>
|
|
|
|
* <[4] 32-bit ADI for multicast group being queried>
|
2014-09-30 23:28:25 +00:00
|
|
|
* [begin gather results -- these same fields can be in OK(MULTICAST_FRAME)]
|
2014-09-22 20:18:24 +00:00
|
|
|
* <[4] 32-bit total number of known members in this multicast group>
|
2014-09-19 01:28:14 +00:00
|
|
|
* <[2] 16-bit number of members enumerated in this packet>
|
|
|
|
* <[...] series of 5-byte ZeroTier addresses of enumerated members>
|
|
|
|
*
|
2014-09-25 22:57:43 +00:00
|
|
|
* If no endpoints are known, OK and ERROR are both optional. It's okay
|
|
|
|
* to return nothing in that case since gathering is "lazy."
|
|
|
|
*
|
2014-09-19 01:28:14 +00:00
|
|
|
* ERROR response payload:
|
|
|
|
* <[8] 64-bit network ID>
|
|
|
|
* <[6] MAC address of multicast group being queried>
|
|
|
|
* <[4] 32-bit ADI for multicast group being queried>
|
|
|
|
*
|
|
|
|
* ERRORs are optional and are only generated if permission is denied,
|
|
|
|
* certificate of membership is out of date, etc.
|
|
|
|
*/
|
2014-09-22 20:18:24 +00:00
|
|
|
VERB_MULTICAST_GATHER = 13,
|
2014-09-19 01:28:14 +00:00
|
|
|
|
2015-10-07 20:35:46 +00:00
|
|
|
/**
|
|
|
|
* Multicast frame:
|
2014-09-24 20:45:58 +00:00
|
|
|
* <[8] 64-bit network ID>
|
2014-09-26 21:18:25 +00:00
|
|
|
* <[1] flags>
|
2014-10-09 19:42:25 +00:00
|
|
|
* [<[...] network certificate of membership>]
|
|
|
|
* [<[4] 32-bit implicit gather limit>]
|
|
|
|
* [<[6] source MAC>]
|
|
|
|
* <[6] destination MAC (multicast address)>
|
|
|
|
* <[4] 32-bit multicast ADI (multicast address extension)>
|
2014-09-19 01:28:14 +00:00
|
|
|
* <[2] 16-bit ethertype>
|
|
|
|
* <[...] ethernet payload>
|
|
|
|
*
|
2014-09-26 21:18:25 +00:00
|
|
|
* Flags:
|
|
|
|
* 0x01 - Network certificate of membership is attached
|
2014-10-09 19:42:25 +00:00
|
|
|
* 0x02 - Implicit gather limit field is present
|
|
|
|
* 0x04 - Source MAC is specified -- otherwise it's computed from sender
|
2014-09-26 21:18:25 +00:00
|
|
|
*
|
2014-10-09 19:42:25 +00:00
|
|
|
* OK and ERROR responses are optional. OK may be generated if there are
|
|
|
|
* implicit gather results or if the recipient wants to send its own
|
|
|
|
* updated certificate of network membership to the sender. ERROR may be
|
|
|
|
* generated if a certificate is needed or if multicasts to this group
|
|
|
|
* are no longer wanted (multicast unsubscribe).
|
2014-09-30 23:28:25 +00:00
|
|
|
*
|
2014-09-25 22:57:43 +00:00
|
|
|
* OK response payload:
|
2014-09-30 23:28:25 +00:00
|
|
|
* <[8] 64-bit network ID>
|
|
|
|
* <[6] MAC address of multicast group>
|
|
|
|
* <[4] 32-bit ADI for multicast group>
|
|
|
|
* <[1] flags>
|
2014-10-09 19:42:25 +00:00
|
|
|
* [<[...] network certficate of membership>]
|
2014-09-30 23:28:25 +00:00
|
|
|
* [<[...] implicit gather results if flag 0x01 is set>]
|
|
|
|
*
|
2014-10-09 19:42:25 +00:00
|
|
|
* OK flags (same bits as request flags):
|
|
|
|
* 0x01 - OK includes certificate of network membership
|
|
|
|
* 0x02 - OK includes implicit gather results
|
2014-09-19 01:28:14 +00:00
|
|
|
*
|
|
|
|
* ERROR response payload:
|
2014-09-30 23:28:25 +00:00
|
|
|
* <[8] 64-bit network ID>
|
2014-09-19 01:28:14 +00:00
|
|
|
* <[6] multicast group MAC>
|
|
|
|
* <[4] 32-bit multicast group ADI>
|
|
|
|
*/
|
2015-05-15 00:41:05 +00:00
|
|
|
VERB_MULTICAST_FRAME = 14,
|
|
|
|
|
2015-10-07 20:35:46 +00:00
|
|
|
/**
|
|
|
|
* Ephemeral (PFS) key push: (UNFINISHED, NOT IMPLEMENTED YET)
|
2015-06-13 16:08:00 +00:00
|
|
|
* <[2] flags (unused and reserved, must be 0)>
|
|
|
|
* <[2] length of padding / extra field section>
|
|
|
|
* <[...] padding / extra field section>
|
2015-05-15 00:41:05 +00:00
|
|
|
* <[8] 64-bit PFS key set ID sender holds for recipient (0==none)>
|
|
|
|
* <[8] 64-bit PFS key set ID of this key set>
|
|
|
|
* [... begin PFS key record ...]
|
|
|
|
* <[1] flags>
|
|
|
|
* <[1] symmetric cipher ID>
|
|
|
|
* <[1] public key type ID>
|
|
|
|
* <[2] public key length in bytes>
|
|
|
|
* <[...] public key>
|
|
|
|
* [... additional records may follow up to max packet length ...]
|
|
|
|
*
|
|
|
|
* This message is sent to negotiate an ephemeral key. If the recipient's
|
|
|
|
* current key pair for the sender does not match the one the sender
|
|
|
|
* claims to have on file, it must respond with its own SET_EPHEMERAL_KEY.
|
|
|
|
*
|
|
|
|
* PFS key IDs are random and must not be zero, since zero indicates that
|
|
|
|
* the sender does not have an ephemeral key on file for the recipient.
|
|
|
|
*
|
|
|
|
* One or more records may be sent. If multiple records are present,
|
|
|
|
* the first record with common symmetric cipher, public key type,
|
|
|
|
* and relevant flags must be used.
|
|
|
|
*
|
2015-06-13 16:08:00 +00:00
|
|
|
* The padding section may be filled with an arbitrary amount of random
|
|
|
|
* or empty payload. This may be used as a countermeasure to prevent PFS
|
|
|
|
* key pushes from being recognized by packet size vs. other packets in
|
|
|
|
* the stream. This also provides potential space for additional fields
|
|
|
|
* that might be indicated in the future by flags.
|
|
|
|
*
|
2015-05-15 00:41:05 +00:00
|
|
|
* Flags (all unspecified flags must be zero):
|
|
|
|
* 0x01 - FIPS mode, only use record if FIPS compliant crypto in use
|
|
|
|
*
|
|
|
|
* Symmetric cipher IDs:
|
|
|
|
* 0x01 - Salsa20/12 with Poly1305 authentication (ZT default)
|
|
|
|
* 0x02 - AES256-GCM combined crypto and authentication
|
|
|
|
*
|
|
|
|
* Public key types:
|
2015-05-15 16:04:39 +00:00
|
|
|
* 0x01 - Curve25519 ECDH with SHA-512 KDF
|
|
|
|
* 0x02 - NIST P-256 ECDH with SHA-512 KDF
|
2015-05-15 00:41:05 +00:00
|
|
|
*
|
|
|
|
* Once both peers have a PFS key, they will attempt to send PFS key
|
|
|
|
* encrypted messages with the PFS flag set using the negotiated
|
|
|
|
* cipher/auth type.
|
|
|
|
*
|
|
|
|
* Note: most of these features such as FIPS and other cipher suites are
|
|
|
|
* not implemented yet. They're just specified in the protocol for future
|
|
|
|
* use to support e.g. FIPS requirements.
|
2015-05-15 16:04:39 +00:00
|
|
|
*
|
|
|
|
* OK response payload:
|
|
|
|
* <[8] PFS key set ID of received key set>
|
|
|
|
* <[1] index in record list of chosen key record>
|
2015-05-15 00:41:05 +00:00
|
|
|
*/
|
2015-06-13 16:08:00 +00:00
|
|
|
VERB_SET_EPHEMERAL_KEY = 15,
|
|
|
|
|
2015-10-07 20:35:46 +00:00
|
|
|
/**
|
|
|
|
* Push of potential endpoints for direct communication:
|
2015-07-06 19:34:35 +00:00
|
|
|
* <[2] 16-bit number of paths>
|
|
|
|
* <[...] paths>
|
|
|
|
*
|
|
|
|
* Path record format:
|
2015-06-13 16:08:00 +00:00
|
|
|
* <[1] flags>
|
2015-07-06 21:08:13 +00:00
|
|
|
* <[2] length of extended path characteristics or 0 for none>
|
|
|
|
* <[...] extended path characteristics>
|
2015-07-06 19:34:35 +00:00
|
|
|
* <[1] address type>
|
2015-07-06 22:28:48 +00:00
|
|
|
* <[1] address length in bytes>
|
2015-07-06 19:34:35 +00:00
|
|
|
* <[...] address>
|
|
|
|
*
|
|
|
|
* Path record flags:
|
2015-07-06 21:08:13 +00:00
|
|
|
* 0x01 - Forget this path if it is currently known
|
|
|
|
* 0x02 - Blacklist this path, do not use
|
2015-07-13 17:03:04 +00:00
|
|
|
* 0x04 - Disable encryption (trust: privacy)
|
|
|
|
* 0x08 - Disable encryption and authentication (trust: ultimate)
|
2015-06-13 16:08:00 +00:00
|
|
|
*
|
|
|
|
* Address types and addresses are of the same format as the destination
|
|
|
|
* address type and address in HELLO.
|
|
|
|
*
|
2015-06-29 23:01:01 +00:00
|
|
|
* The receiver may, upon receiving a push, attempt to establish a
|
2015-07-06 19:34:35 +00:00
|
|
|
* direct link to one or more of the indicated addresses. It is the
|
|
|
|
* responsibility of the sender to limit which peers it pushes direct
|
2015-07-06 21:08:13 +00:00
|
|
|
* paths to to those with whom it has a trust relationship. The receiver
|
|
|
|
* must obey any restrictions provided such as exclusivity or blacklists.
|
|
|
|
* OK responses to this message are optional.
|
2015-06-13 16:08:00 +00:00
|
|
|
*
|
2015-07-06 21:08:13 +00:00
|
|
|
* Note that a direct path push does not imply that learned paths can't
|
|
|
|
* be used unless they are blacklisted explicitly or unless flag 0x01
|
|
|
|
* is set.
|
|
|
|
*
|
|
|
|
* Only a subset of this functionality is currently implemented: basic
|
2015-07-13 17:03:04 +00:00
|
|
|
* path pushing and learning. Blacklisting and trust are not fully
|
|
|
|
* implemented yet (encryption is still always used).
|
2015-07-06 21:08:13 +00:00
|
|
|
*
|
2015-07-13 17:03:04 +00:00
|
|
|
* OK and ERROR are not generated.
|
2015-06-13 16:08:00 +00:00
|
|
|
*/
|
2015-09-27 18:37:39 +00:00
|
|
|
VERB_PUSH_DIRECT_PATHS = 16,
|
|
|
|
|
2015-10-07 20:35:46 +00:00
|
|
|
/**
|
|
|
|
* Source-routed circuit test message:
|
2015-09-27 18:37:39 +00:00
|
|
|
* <[5] address of originator of circuit test>
|
|
|
|
* <[2] 16-bit flags>
|
|
|
|
* <[8] 64-bit timestamp>
|
|
|
|
* <[8] 64-bit test ID (arbitrary, set by tester)>
|
2015-10-06 18:47:16 +00:00
|
|
|
* <[2] 16-bit originator credential length (includes type)>
|
|
|
|
* [[1] originator credential type (for authorizing test)]
|
|
|
|
* [[...] originator credential]
|
2015-09-27 18:37:39 +00:00
|
|
|
* <[2] 16-bit length of additional fields>
|
2015-10-06 18:47:16 +00:00
|
|
|
* [[...] additional fields]
|
2015-09-30 20:59:05 +00:00
|
|
|
* [ ... end of signed portion of request ... ]
|
2015-09-27 18:37:39 +00:00
|
|
|
* <[2] 16-bit length of signature of request>
|
|
|
|
* <[...] signature of request by originator>
|
2015-10-06 18:47:16 +00:00
|
|
|
* <[2] 16-bit previous hop credential length (including type)>
|
|
|
|
* [[1] previous hop credential type]
|
|
|
|
* [[...] previous hop credential]
|
2015-09-27 18:37:39 +00:00
|
|
|
* <[...] next hop(s) in path>
|
|
|
|
*
|
|
|
|
* Flags:
|
2015-10-06 18:47:16 +00:00
|
|
|
* 0x01 - Report back to originator at middle hops
|
2015-09-27 18:37:39 +00:00
|
|
|
* 0x02 - Report back to originator at last hop
|
|
|
|
*
|
|
|
|
* Originator credential types:
|
|
|
|
* 0x01 - 64-bit network ID for which originator is controller
|
|
|
|
*
|
|
|
|
* Previous hop credential types:
|
|
|
|
* 0x01 - Certificate of network membership
|
|
|
|
*
|
|
|
|
* Path record format:
|
2015-10-06 18:47:16 +00:00
|
|
|
* <[1] 8-bit flags (unused, must be zero)>
|
2015-09-27 18:37:39 +00:00
|
|
|
* <[1] 8-bit breadth (number of next hops)>
|
|
|
|
* <[...] one or more ZeroTier addresses of next hops>
|
|
|
|
*
|
|
|
|
* The circuit test allows a device to send a message that will traverse
|
|
|
|
* the network along a specified path, with each hop optionally reporting
|
|
|
|
* back to the tester via VERB_CIRCUIT_TEST_REPORT.
|
|
|
|
*
|
|
|
|
* Each circuit test packet includes a digital signature by the originator
|
|
|
|
* of the request, as well as a credential by which that originator claims
|
|
|
|
* authorization to perform the test. Currently this signature is ed25519,
|
|
|
|
* but in the future flags might be used to indicate an alternative
|
|
|
|
* algorithm. For example, the originator might be a network controller.
|
|
|
|
* In this case the test might be authorized if the recipient is a member
|
|
|
|
* of a network controlled by it, and if the previous hop(s) are also
|
|
|
|
* members. Each hop may include its certificate of network membership.
|
|
|
|
*
|
|
|
|
* Circuit test paths consist of a series of records. When a node receives
|
|
|
|
* an authorized circuit test, it:
|
|
|
|
*
|
|
|
|
* (1) Reports back to circuit tester as flags indicate
|
|
|
|
* (2) Reads and removes the next hop from the packet's path
|
|
|
|
* (3) Sends the packet along to next hop(s), if any.
|
|
|
|
*
|
|
|
|
* It is perfectly legal for a path to contain the same hop more than
|
|
|
|
* once. In fact, this can be a very useful test to determine if a hop
|
|
|
|
* can be reached bidirectionally and if so what that connectivity looks
|
|
|
|
* like.
|
|
|
|
*
|
|
|
|
* The breadth field in source-routed path records allows a hop to forward
|
|
|
|
* to more than one recipient, allowing the tester to specify different
|
|
|
|
* forms of graph traversal in a test.
|
|
|
|
*
|
|
|
|
* There is no hard limit to the number of hops in a test, but it is
|
|
|
|
* practically limited by the maximum size of a (possibly fragmented)
|
|
|
|
* ZeroTier packet.
|
|
|
|
*
|
|
|
|
* Support for circuit tests is optional. If they are not supported, the
|
|
|
|
* node should respond with an UNSUPPORTED_OPERATION error. If a circuit
|
|
|
|
* test request is not authorized, it may be ignored or reported as
|
|
|
|
* an INVALID_REQUEST. No OK messages are generated, but TEST_REPORT
|
|
|
|
* messages may be sent (see below).
|
|
|
|
*
|
|
|
|
* ERROR packet format:
|
|
|
|
* <[8] 64-bit timestamp (echoed from original>
|
|
|
|
* <[8] 64-bit test ID (echoed from original)>
|
|
|
|
*/
|
|
|
|
VERB_CIRCUIT_TEST = 17,
|
|
|
|
|
2015-10-07 20:35:46 +00:00
|
|
|
/**
|
|
|
|
* Circuit test hop report:
|
2015-09-27 18:37:39 +00:00
|
|
|
* <[8] 64-bit timestamp (from original test)>
|
|
|
|
* <[8] 64-bit test ID (from original test)>
|
|
|
|
* <[8] 64-bit reporter timestamp (reporter's clock, 0 if unspec)>
|
|
|
|
* <[1] 8-bit vendor ID (set to 0, currently unused)>
|
|
|
|
* <[1] 8-bit reporter protocol version>
|
|
|
|
* <[1] 8-bit reporter major version>
|
|
|
|
* <[1] 8-bit reporter minor version>
|
|
|
|
* <[2] 16-bit reporter revision>
|
|
|
|
* <[2] 16-bit reporter OS/platform>
|
|
|
|
* <[2] 16-bit reporter architecture>
|
|
|
|
* <[2] 16-bit error code (set to 0, currently unused)>
|
2015-10-06 18:47:16 +00:00
|
|
|
* <[8] 64-bit report flags (set to 0, currently unused)>
|
2015-09-27 18:37:39 +00:00
|
|
|
* <[8] 64-bit source packet ID>
|
2015-10-06 18:47:16 +00:00
|
|
|
* <[1] 8-bit source packet hop count (ZeroTier hop count)>
|
|
|
|
* <[...] local wire address on which packet was received>
|
|
|
|
* <[...] remote wire address from which packet was received>
|
2015-09-27 18:37:39 +00:00
|
|
|
* <[2] 16-bit length of additional fields>
|
|
|
|
* <[...] additional fields>
|
2015-10-06 18:47:16 +00:00
|
|
|
* <[1] 8-bit number of next hops (breadth)>
|
2015-09-27 18:37:39 +00:00
|
|
|
* <[...] next hop information>
|
|
|
|
*
|
|
|
|
* Next hop information record format:
|
|
|
|
* <[5] ZeroTier address of next hop>
|
2015-10-06 18:47:16 +00:00
|
|
|
* <[...] current best direct path address, if any, 0 if none>
|
2015-09-27 18:37:39 +00:00
|
|
|
*
|
|
|
|
* Circuit test reports can be sent by hops in a circuit test to report
|
|
|
|
* back results. They should include information about the sender as well
|
|
|
|
* as about the paths to which next hops are being sent.
|
|
|
|
*
|
|
|
|
* If a test report is received and no circuit test was sent, it should be
|
|
|
|
* ignored. This message generates no OK or ERROR response.
|
|
|
|
*/
|
2015-10-07 20:35:46 +00:00
|
|
|
VERB_CIRCUIT_TEST_REPORT = 18,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Request proof of work:
|
|
|
|
* <[1] 8-bit proof of work type>
|
|
|
|
* <[1] 8-bit proof of work difficulty>
|
|
|
|
* <[2] 16-bit length of proof of work challenge>
|
|
|
|
* <[...] proof of work challenge>
|
|
|
|
*
|
|
|
|
* This requests that a peer perform a proof of work calucation. It can be
|
|
|
|
* sent by highly trusted peers (e.g. root servers, network controllers)
|
|
|
|
* under suspected denial of service conditions in an attempt to filter
|
|
|
|
* out "non-serious" peers and remain responsive to those proving their
|
|
|
|
* intent to actually communicate.
|
|
|
|
*
|
|
|
|
* If the peer obliges to perform the work, it does so and responds with
|
|
|
|
* an OK containing the result. Otherwise it may ignore the message or
|
|
|
|
* response with an ERROR_INVALID_REQUEST or ERROR_UNSUPPORTED_OPERATION.
|
|
|
|
*
|
|
|
|
* Proof of work type IDs:
|
|
|
|
* 0x01 - Salsa20/12+SHA512 hashcash function
|
|
|
|
*
|
|
|
|
* Salsa20/12+SHA512 is based on the following composite hash function:
|
|
|
|
*
|
|
|
|
* (1) Compute SHA512(candidate)
|
|
|
|
* (2) Use the first 256 bits of the result of #1 as a key to encrypt
|
|
|
|
* 131072 zero bytes with Salsa20/12 (with a zero IV).
|
|
|
|
* (3) Compute SHA512(the result of step #2)
|
|
|
|
* (4) Accept this candiate if the first [difficulty] bits of the result
|
|
|
|
* from step #3 are zero. Otherwise generate a new candidate and try
|
|
|
|
* again.
|
|
|
|
*
|
|
|
|
* This is performed repeatedly on candidates generated by appending the
|
|
|
|
* supplied challenge to an arbitrary nonce until a valid candidate
|
|
|
|
* is found. This chosen prepended nonce is then returned as the result
|
|
|
|
* in OK.
|
|
|
|
*
|
|
|
|
* OK payload:
|
|
|
|
* <[2] 16-bit length of result>
|
|
|
|
* <[...] computed proof of work>
|
|
|
|
*
|
|
|
|
* ERROR has no payload.
|
|
|
|
*/
|
|
|
|
VERB_REQUEST_PROOF_OF_WORK = 19
|
2015-09-27 18:37:39 +00:00
|
|
|
};
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* Error codes for VERB_ERROR
|
|
|
|
*/
|
|
|
|
enum ErrorCode
|
|
|
|
{
|
|
|
|
/* No error, not actually used in transit */
|
|
|
|
ERROR_NONE = 0,
|
|
|
|
|
|
|
|
/* Invalid request */
|
|
|
|
ERROR_INVALID_REQUEST = 1,
|
|
|
|
|
|
|
|
/* Bad/unsupported protocol version */
|
|
|
|
ERROR_BAD_PROTOCOL_VERSION = 2,
|
|
|
|
|
|
|
|
/* Unknown object queried (e.g. with WHOIS) */
|
2013-08-13 01:25:36 +00:00
|
|
|
ERROR_OBJ_NOT_FOUND = 3,
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/* HELLO pushed an identity whose address is already claimed */
|
|
|
|
ERROR_IDENTITY_COLLISION = 4,
|
|
|
|
|
|
|
|
/* Verb or use case not supported/enabled by this node */
|
2013-10-05 14:19:12 +00:00
|
|
|
ERROR_UNSUPPORTED_OPERATION = 5,
|
2013-07-27 20:20:08 +00:00
|
|
|
|
|
|
|
/* Message to private network rejected -- no unexpired certificate on file */
|
2013-10-16 21:47:26 +00:00
|
|
|
ERROR_NEED_MEMBERSHIP_CERTIFICATE = 6,
|
|
|
|
|
|
|
|
/* Tried to join network, but you're not a member */
|
2014-09-19 01:28:14 +00:00
|
|
|
ERROR_NETWORK_ACCESS_DENIED_ = 7, /* extra _ to avoid Windows name conflict */
|
|
|
|
|
|
|
|
/* Multicasts to this group are not wanted */
|
|
|
|
ERROR_UNWANTED_MULTICAST = 8
|
2013-07-04 20:56:19 +00:00
|
|
|
};
|
|
|
|
|
2015-09-30 21:48:07 +00:00
|
|
|
#ifdef ZT_TRACE
|
2013-07-04 20:56:19 +00:00
|
|
|
static const char *verbString(Verb v)
|
|
|
|
throw();
|
|
|
|
static const char *errorString(ErrorCode e)
|
|
|
|
throw();
|
2015-09-30 21:48:07 +00:00
|
|
|
#endif
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
template<unsigned int C2>
|
2015-04-08 02:31:11 +00:00
|
|
|
Packet(const Buffer<C2> &b) :
|
2013-07-04 20:56:19 +00:00
|
|
|
Buffer<ZT_PROTO_MAX_PACKET_LENGTH>(b)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-04-08 02:31:11 +00:00
|
|
|
Packet(const void *data,unsigned int len) :
|
|
|
|
Buffer<ZT_PROTO_MAX_PACKET_LENGTH>(data,len)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* Construct a new empty packet with a unique random packet ID
|
2015-07-13 17:03:04 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* Flags and hops will be zero. Other fields and data region are undefined.
|
|
|
|
* Use the header access methods (setDestination() and friends) to fill out
|
|
|
|
* the header. Payload should be appended; initial size is header size.
|
|
|
|
*/
|
|
|
|
Packet() :
|
|
|
|
Buffer<ZT_PROTO_MAX_PACKET_LENGTH>(ZT_PROTO_MIN_PACKET_LENGTH)
|
|
|
|
{
|
2013-07-09 18:06:55 +00:00
|
|
|
Utils::getSecureRandom(field(ZT_PACKET_IDX_IV,8),8);
|
2015-05-15 00:41:05 +00:00
|
|
|
(*this)[ZT_PACKET_IDX_FLAGS] = 0; // zero flags, cipher ID, and hops
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2014-09-22 20:18:24 +00:00
|
|
|
/**
|
|
|
|
* Make a copy of a packet with a new initialization vector and destination address
|
|
|
|
*
|
|
|
|
* This can be used to take one draft prototype packet and quickly make copies to
|
|
|
|
* encrypt for different destinations.
|
|
|
|
*
|
|
|
|
* @param prototype Prototype packet
|
|
|
|
* @param dest Destination ZeroTier address for new packet
|
|
|
|
*/
|
|
|
|
Packet(const Packet &prototype,const Address &dest) :
|
|
|
|
Buffer<ZT_PROTO_MAX_PACKET_LENGTH>(prototype)
|
|
|
|
{
|
|
|
|
Utils::getSecureRandom(field(ZT_PACKET_IDX_IV,8),8);
|
|
|
|
setDestination(dest);
|
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* Construct a new empty packet with a unique random packet ID
|
2015-07-13 17:03:04 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param dest Destination ZT address
|
|
|
|
* @param source Source ZT address
|
|
|
|
* @param v Verb
|
|
|
|
*/
|
|
|
|
Packet(const Address &dest,const Address &source,const Verb v) :
|
|
|
|
Buffer<ZT_PROTO_MAX_PACKET_LENGTH>(ZT_PROTO_MIN_PACKET_LENGTH)
|
|
|
|
{
|
2013-07-09 18:06:55 +00:00
|
|
|
Utils::getSecureRandom(field(ZT_PACKET_IDX_IV,8),8);
|
2013-07-04 20:56:19 +00:00
|
|
|
setDestination(dest);
|
|
|
|
setSource(source);
|
2013-07-09 18:06:55 +00:00
|
|
|
(*this)[ZT_PACKET_IDX_FLAGS] = 0; // zero flags and hops
|
2013-07-04 20:56:19 +00:00
|
|
|
setVerb(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset this packet structure for reuse in place
|
2015-07-13 17:03:04 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param dest Destination ZT address
|
|
|
|
* @param source Source ZT address
|
|
|
|
* @param v Verb
|
|
|
|
*/
|
|
|
|
inline void reset(const Address &dest,const Address &source,const Verb v)
|
|
|
|
{
|
|
|
|
setSize(ZT_PROTO_MIN_PACKET_LENGTH);
|
2013-07-09 18:06:55 +00:00
|
|
|
Utils::getSecureRandom(field(ZT_PACKET_IDX_IV,8),8);
|
2013-07-04 20:56:19 +00:00
|
|
|
setDestination(dest);
|
|
|
|
setSource(source);
|
2015-05-15 00:41:05 +00:00
|
|
|
(*this)[ZT_PACKET_IDX_FLAGS] = 0; // zero flags, cipher ID, and hops
|
2013-07-04 20:56:19 +00:00
|
|
|
setVerb(v);
|
|
|
|
}
|
|
|
|
|
2013-07-11 20:19:06 +00:00
|
|
|
/**
|
|
|
|
* Generate a new IV / packet ID in place
|
|
|
|
*
|
|
|
|
* This can be used to re-use a packet buffer multiple times to send
|
|
|
|
* technically different but otherwise identical copies of the same
|
|
|
|
* packet.
|
|
|
|
*/
|
2015-05-15 00:41:05 +00:00
|
|
|
inline void newInitializationVector() { Utils::getSecureRandom(field(ZT_PACKET_IDX_IV,8),8); }
|
2013-07-11 20:19:06 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* Set this packet's destination
|
2015-07-13 17:03:04 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param dest ZeroTier address of destination
|
|
|
|
*/
|
2015-05-15 00:41:05 +00:00
|
|
|
inline void setDestination(const Address &dest) { dest.copyTo(field(ZT_PACKET_IDX_DEST,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set this packet's source
|
2015-07-13 17:03:04 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param source ZeroTier address of source
|
|
|
|
*/
|
2015-05-15 00:41:05 +00:00
|
|
|
inline void setSource(const Address &source) { source.copyTo(field(ZT_PACKET_IDX_SOURCE,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get this packet's destination
|
2015-07-13 17:03:04 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* @return Destination ZT address
|
|
|
|
*/
|
2013-07-29 17:56:20 +00:00
|
|
|
inline Address destination() const { return Address(field(ZT_PACKET_IDX_DEST,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get this packet's source
|
2015-07-13 17:03:04 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* @return Source ZT address
|
|
|
|
*/
|
2013-07-29 17:56:20 +00:00
|
|
|
inline Address source() const { return Address(field(ZT_PACKET_IDX_SOURCE,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return True if packet is of valid length
|
|
|
|
*/
|
2013-07-09 18:06:55 +00:00
|
|
|
inline bool lengthValid() const { return (size() >= ZT_PROTO_MIN_PACKET_LENGTH); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return True if packet is fragmented (expect fragments)
|
|
|
|
*/
|
2013-08-13 01:25:36 +00:00
|
|
|
inline bool fragmented() const { return (((unsigned char)(*this)[ZT_PACKET_IDX_FLAGS] & ZT_PROTO_FLAG_FRAGMENTED) != 0); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set this packet's fragmented flag
|
|
|
|
*
|
|
|
|
* @param f Fragmented flag value
|
|
|
|
*/
|
|
|
|
inline void setFragmented(bool f)
|
|
|
|
{
|
|
|
|
if (f)
|
2013-07-09 18:06:55 +00:00
|
|
|
(*this)[ZT_PACKET_IDX_FLAGS] |= (char)ZT_PROTO_FLAG_FRAGMENTED;
|
|
|
|
else (*this)[ZT_PACKET_IDX_FLAGS] &= (char)(~ZT_PROTO_FLAG_FRAGMENTED);
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return True if compressed (result only valid if unencrypted)
|
|
|
|
*/
|
2013-08-13 01:25:36 +00:00
|
|
|
inline bool compressed() const { return (((unsigned char)(*this)[ZT_PACKET_IDX_VERB] & ZT_PROTO_VERB_FLAG_COMPRESSED) != 0); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ZeroTier forwarding hops (0 to 7)
|
|
|
|
*/
|
2013-07-09 18:06:55 +00:00
|
|
|
inline unsigned int hops() const { return ((unsigned int)(*this)[ZT_PACKET_IDX_FLAGS] & 0x07); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Increment this packet's hop count
|
|
|
|
*/
|
|
|
|
inline void incrementHops()
|
|
|
|
{
|
2014-10-02 17:54:34 +00:00
|
|
|
unsigned char &b = (*this)[ZT_PACKET_IDX_FLAGS];
|
|
|
|
b = (b & 0xf8) | ((b + 1) & 0x07);
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2014-10-02 17:54:34 +00:00
|
|
|
/**
|
|
|
|
* @return Cipher suite selector: 0 - 7 (see #defines)
|
|
|
|
*/
|
2014-10-02 18:08:59 +00:00
|
|
|
inline unsigned int cipher() const
|
|
|
|
{
|
2015-05-14 01:53:37 +00:00
|
|
|
// Note: this uses the new cipher spec field, which is incompatible with <1.0.0 peers
|
2015-05-15 00:41:05 +00:00
|
|
|
return (((unsigned int)(*this)[ZT_PACKET_IDX_FLAGS] & 0x38) >> 3);
|
2014-10-02 18:08:59 +00:00
|
|
|
}
|
2014-10-02 17:54:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set this packet's cipher suite
|
|
|
|
*/
|
|
|
|
inline void setCipher(unsigned int c)
|
|
|
|
{
|
|
|
|
unsigned char &b = (*this)[ZT_PACKET_IDX_FLAGS];
|
2015-05-15 00:41:05 +00:00
|
|
|
b = (b & 0xc7) | (unsigned char)((c << 3) & 0x38); // bits: FFCCCHHH
|
2015-05-14 01:53:37 +00:00
|
|
|
// DEPRECATED "encrypted" flag -- used by pre-1.0.3 peers
|
2014-10-02 18:08:59 +00:00
|
|
|
if (c == ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012)
|
2015-05-14 01:53:37 +00:00
|
|
|
b |= ZT_PROTO_FLAG_ENCRYPTED;
|
|
|
|
else b &= (~ZT_PROTO_FLAG_ENCRYPTED);
|
2014-10-02 17:54:34 +00:00
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* Get this packet's unique ID (the IV field interpreted as uint64_t)
|
2015-07-13 17:03:04 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* @return Packet ID
|
|
|
|
*/
|
|
|
|
inline uint64_t packetId() const { return at<uint64_t>(ZT_PACKET_IDX_IV); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set packet verb
|
2015-07-13 17:03:04 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* This also has the side-effect of clearing any verb flags, such as
|
|
|
|
* compressed, and so must only be done during packet composition.
|
2015-07-13 17:03:04 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param v New packet verb
|
|
|
|
*/
|
2013-07-09 18:06:55 +00:00
|
|
|
inline void setVerb(Verb v) { (*this)[ZT_PACKET_IDX_VERB] = (char)v; }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Packet verb (not including flag bits)
|
|
|
|
*/
|
2013-07-09 18:06:55 +00:00
|
|
|
inline Verb verb() const { return (Verb)((*this)[ZT_PACKET_IDX_VERB] & 0x1f); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Length of packet payload
|
|
|
|
*/
|
2013-07-09 18:06:55 +00:00
|
|
|
inline unsigned int payloadLength() const { return ((size() < ZT_PROTO_MIN_PACKET_LENGTH) ? 0 : (size() - ZT_PROTO_MIN_PACKET_LENGTH)); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
2013-07-09 18:06:55 +00:00
|
|
|
* @return Raw packet payload
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2015-05-05 01:39:53 +00:00
|
|
|
inline const unsigned char *payload() const { return field(ZT_PACKET_IDX_PAYLOAD,size() - ZT_PACKET_IDX_PAYLOAD); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
2013-09-24 21:35:05 +00:00
|
|
|
* Armor packet for transport
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
2013-09-24 21:35:05 +00:00
|
|
|
* @param key 32-byte key
|
|
|
|
* @param encryptPayload If true, encrypt packet payload, else just MAC
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2015-05-05 01:39:53 +00:00
|
|
|
void armor(const void *key,bool encryptPayload);
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
2013-09-24 21:35:05 +00:00
|
|
|
* Verify and (if encrypted) decrypt packet
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
2013-09-24 21:35:05 +00:00
|
|
|
* @param key 32-byte key
|
|
|
|
* @return False if packet is invalid or failed MAC authenticity check
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2015-05-05 01:39:53 +00:00
|
|
|
bool dearmor(const void *key);
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt to compress payload if not already (must be unencrypted)
|
2015-07-13 17:03:04 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* This requires that the payload at least contain the verb byte already
|
|
|
|
* set. The compressed flag in the verb is set if compression successfully
|
|
|
|
* results in a size reduction. If no size reduction occurs, compression
|
|
|
|
* is not done and the flag is left cleared.
|
2015-07-13 17:03:04 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* @return True if compression occurred
|
|
|
|
*/
|
2015-05-05 01:39:53 +00:00
|
|
|
bool compress();
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt to decompress payload if it is compressed (must be unencrypted)
|
2015-07-13 17:03:04 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* If payload is compressed, it is decompressed and the compressed verb
|
|
|
|
* flag is cleared. Otherwise nothing is done and true is returned.
|
2015-07-13 17:03:04 +00:00
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* @return True if data is now decompressed and valid, false on error
|
|
|
|
*/
|
2015-05-05 01:39:53 +00:00
|
|
|
bool uncompress();
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
private:
|
2013-09-25 14:55:27 +00:00
|
|
|
static const unsigned char ZERO_KEY[32];
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
2013-09-16 17:02:10 +00:00
|
|
|
* Deterministically mangle a 256-bit crypto key based on packet
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
2014-10-02 17:54:34 +00:00
|
|
|
* This uses extra data from the packet to mangle the secret, giving us an
|
|
|
|
* effective IV that is somewhat more than 64 bits. This is "free" for
|
|
|
|
* Salsa20 since it has negligible key setup time so using a different
|
|
|
|
* key each time is fine.
|
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param in Input key (32 bytes)
|
|
|
|
* @param out Output buffer (32 bytes)
|
|
|
|
*/
|
2014-10-02 17:54:34 +00:00
|
|
|
inline void _salsa20MangleKey(const unsigned char *in,unsigned char *out) const
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2014-10-02 17:54:34 +00:00
|
|
|
const unsigned char *d = (const unsigned char *)data();
|
|
|
|
|
2013-09-24 21:35:05 +00:00
|
|
|
// IV and source/destination addresses. Using the addresses divides the
|
|
|
|
// key space into two halves-- A->B and B->A (since order will change).
|
2013-07-04 20:56:19 +00:00
|
|
|
for(unsigned int i=0;i<18;++i) // 8 + (ZT_ADDRESS_LENGTH * 2) == 18
|
2014-10-02 17:54:34 +00:00
|
|
|
out[i] = in[i] ^ d[i];
|
2013-09-16 17:02:10 +00:00
|
|
|
|
|
|
|
// Flags, but with hop count masked off. Hop count is altered by forwarding
|
|
|
|
// nodes. It's one of the only parts of a packet modifiable by people
|
|
|
|
// without the key.
|
2014-10-02 17:54:34 +00:00
|
|
|
out[18] = in[18] ^ (d[ZT_PACKET_IDX_FLAGS] & 0xf8);
|
2013-09-16 17:02:10 +00:00
|
|
|
|
|
|
|
// Raw packet size in bytes -- thus each packet size defines a new
|
|
|
|
// key space.
|
2013-07-09 18:06:55 +00:00
|
|
|
out[19] = in[19] ^ (unsigned char)(size() & 0xff);
|
|
|
|
out[20] = in[20] ^ (unsigned char)((size() >> 8) & 0xff); // little endian
|
2013-09-16 17:02:10 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
// Rest of raw key is used unchanged
|
|
|
|
for(unsigned int i=21;i<32;++i)
|
|
|
|
out[i] = in[i];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|