summaryrefslogtreecommitdiffstats
path: root/src/handshake.c
blob: 6220ddfbd2cc0c697ad0dbf4069ae6a7e85102ae (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
/*
  Copyright (c) 2012, Matthias Schiffer <mschiffer@universe-factory.net>
  Partly based on QuickTun Copyright (c) 2010, Ivo Smits <Ivo@UCIS.nl>.
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#include "handshake.h"
#include "packet.h"
#include "peer.h"
#include "task.h"

#include <string.h>


static const char const *RECORD_TYPES[RECORD_MAX] = {
	"reply code",
	"error detail",
	"flags",
	"mode",
	"protocol name",
};

static const char const *REPLY_TYPES[REPLY_MAX] = {
	"success",
	"mandatory field missing",
	"unacceptable value",
};


static inline void handshake_add(fastd_context *ctx, fastd_buffer *buffer, fastd_handshake_record_type type, size_t len, const void *data) {
	if ((uint8_t*)buffer->data + buffer->len + 2 + len > (uint8_t*)buffer->base + buffer->base_len)
		exit_bug(ctx, "not enough buffer allocated for handshake");

	uint8_t *dst = (uint8_t*)buffer->data + buffer->len;

	dst[0] = type;
	dst[1] = len;
	memcpy(dst+2, data, len);

	buffer->len += 2 + len;
}

void fastd_handshake_send(fastd_context *ctx, fastd_peer *peer) {
	size_t protocol_len = strlen(ctx->conf->protocol->name);
	fastd_buffer buffer = fastd_buffer_alloc(sizeof(fastd_packet), 0,
						 2+1 +           /* mode */
						 2+protocol_len  /* protocol name */
						 );
	fastd_packet *request = buffer.data;

	request->reply = 0;
	request->cp = 0;
	request->req_id = ++peer->last_req_id;
	request->rsv = 0;

	uint8_t mode = ctx->conf->mode;
	handshake_add(ctx, &buffer, RECORD_MODE, 1, &mode);

	handshake_add(ctx, &buffer, RECORD_PROTOCOL_NAME, protocol_len, ctx->conf->protocol->name);

	fastd_task_put_send_handshake(ctx, peer, buffer);
}



void fastd_handshake_handle(fastd_context *ctx, fastd_peer *peer, fastd_buffer buffer) {
	if (buffer.len < sizeof(fastd_packet)) {
		pr_warn(ctx, "received an invalid handshake from %P", peer);
		goto end_free;
	}

	fastd_packet *packet = buffer.base;

	size_t lengths[RECORD_MAX];
	void *records[RECORD_MAX] = { 0 };

	uint8_t *ptr = packet->tlv_data;
	while (true) {
		if (ptr+2 > (uint8_t*)buffer.data + buffer.len)
			break;

		uint8_t type = ptr[0];
		uint8_t len = ptr[1];

		if (ptr+2+len > (uint8_t*)buffer.data + buffer.len)
			break;

		lengths[type] = len;
		records[type] = ptr+2;

		ptr += 2+len;
	}

	if (!packet->reply) {
		fastd_buffer reply_buffer;
		fastd_packet *reply;

		uint8_t reply_code = REPLY_SUCCESS;
		uint8_t error_detail = 0;

		if (!records[RECORD_MODE]) {
			reply_code = REPLY_MANDATORY_MISSING;
			error_detail = RECORD_MODE;
			goto send_reply;
		}

		if (lengths[RECORD_MODE] != 1 || *(uint8_t*)records[RECORD_MODE] != ctx->conf->mode) {
			reply_code = REPLY_UNACCEPTABLE_VALUE;
			error_detail = RECORD_MODE;
			goto send_reply;
		}

		if (!records[RECORD_PROTOCOL_NAME]) {
			reply_code = REPLY_MANDATORY_MISSING;
			error_detail = RECORD_PROTOCOL_NAME;
			goto send_reply;
		}

		if (lengths[RECORD_PROTOCOL_NAME] != strlen(ctx->conf->protocol->name)
		    || strncmp((char*)records[RECORD_PROTOCOL_NAME], ctx->conf->protocol->name, lengths[RECORD_PROTOCOL_NAME])) {
			reply_code = REPLY_UNACCEPTABLE_VALUE;
			error_detail = RECORD_PROTOCOL_NAME;
			goto send_reply;
		}

	send_reply:
		reply_buffer = fastd_buffer_alloc(sizeof(fastd_packet), 0, 6 /* enough space for reply code and error detail */);
		reply = reply_buffer.data;

		reply->reply = 1;
		reply->cp = packet->cp;
		reply->req_id = packet->req_id;
		reply->rsv = 0;

		handshake_add(ctx, &reply_buffer, RECORD_REPLY_CODE, 1, &reply_code);

		if (reply_code)
			handshake_add(ctx, &reply_buffer, RECORD_ERROR_DETAIL, 1, &error_detail);

		fastd_task_put_send_handshake(ctx, peer, reply_buffer);
	}
	else {
		if (!packet->cp) {
			if (packet->req_id != peer->last_req_id) {
				pr_warn(ctx, "received handshake reply with request ID %u from %P while %u was expected", packet->req_id, peer, peer->last_req_id);
				goto end_free;
			}
		}
		else {
			goto end_free; /* TODO */
		}

		if (!records[RECORD_REPLY_CODE] || lengths[RECORD_REPLY_CODE] != 1) {
			pr_warn(ctx, "received handshake reply without reply code from %P", peer);
			goto end_free;
		}

		uint8_t reply_code = *(uint8_t*)records[RECORD_REPLY_CODE];
		uint8_t error_detail;
		const char *error_field_str;

		switch (reply_code) {
		case REPLY_SUCCESS:
			pr_info(ctx, "Handshake with %P successful.", peer);
			fastd_peer_set_established(ctx, peer);
			ctx->conf->protocol->init(ctx, peer);
			break;

		default:
			if (reply_code >= REPLY_MAX) {
				pr_warn(ctx, "Handshake with %P failed with unknown code %i", peer, reply_code);
				break;
			}

			if (!records[RECORD_ERROR_DETAIL] || lengths[RECORD_ERROR_DETAIL] != 1) {
				pr_warn(ctx, "Handshake with %P failed with code %s", peer, REPLY_TYPES[reply_code]);
				break;
			}

			error_detail = *(uint8_t*)records[RECORD_ERROR_DETAIL];
			if (error_detail >= RECORD_MAX)
				error_field_str = "<unknown>";
			else
				error_field_str = RECORD_TYPES[error_detail];

			switch (reply_code) {
			case REPLY_MANDATORY_MISSING:
				pr_warn(ctx, "Handshake with %P failed: mandatory field `%s' missing", peer, error_field_str);
				break;

			case REPLY_UNACCEPTABLE_VALUE:
				pr_warn(ctx, "Handshake with %P failed: unacceptable value for field `%s'", peer, error_field_str);
				break;

			default: /* just to silence the warning */
				break;
			}
		}
	}

 end_free:
	fastd_buffer_free(buffer);
}