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
|
/*
Copyright (c) 2012, Matthias Schiffer <mschiffer@universe-factory.net>
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.
*/
#define _GNU_SOURCE
#include "peer.h"
#include "task.h"
static inline void reset_peer(fastd_context *ctx, fastd_peer *peer) {
ctx->conf->protocol->free_peer_state(ctx, peer);
peer->protocol_state = NULL;
int i, deleted = 0;
for (i = 0; i < ctx->n_eth_addr; i++) {
if (ctx->eth_addr[i].peer == peer) {
deleted++;
}
else if (deleted) {
ctx->eth_addr[i-deleted] = ctx->eth_addr[i];
}
}
ctx->n_eth_addr -= deleted;
fastd_task_delete_peer(ctx, peer);
}
static inline void setup_peer(fastd_context *ctx, fastd_peer *peer) {
if (fastd_peer_is_temporary(peer)) {
exit_fatal(ctx, "tried to setup temporary peer");
}
peer->address = peer->config->address;
peer->state = STATE_WAIT;
peer->seen = (struct timespec){0, 0};
if (!fastd_peer_is_floating(peer))
fastd_task_schedule_handshake(ctx, peer, 0);
}
static void delete_peer(fastd_context *ctx, fastd_peer *peer) {
pr_debug(ctx, "deleting peer %P", peer);
fastd_peer **cur_peer;
for (cur_peer = &ctx->peers; *cur_peer; cur_peer = &(*cur_peer)->next) {
if ((*cur_peer) == peer) {
*cur_peer = peer->next;
break;
}
}
free(peer);
}
fastd_peer_config* fastd_peer_config_new(fastd_context *ctx, fastd_config *conf) {
fastd_peer_config *peer = malloc(sizeof(fastd_peer_config));
peer->enabled = true;
memset(&peer->address, 0, sizeof(fastd_peer_address));
peer->name = NULL;
peer->key = NULL;
peer->protocol_config = NULL;
peer->next = conf->peers;
conf->peers = peer;
return peer;
}
void fastd_peer_reset(fastd_context *ctx, fastd_peer *peer) {
pr_debug(ctx, "resetting peer %P", peer);
reset_peer(ctx, peer);
if (fastd_peer_is_temporary(peer))
delete_peer(ctx, peer);
else
setup_peer(ctx, peer);
}
static fastd_peer* add_peer(fastd_context *ctx) {
fastd_peer *peer = malloc(sizeof(fastd_peer));
peer->next = ctx->peers;
peer->last_req_id = 0;
peer->protocol_state = NULL;
ctx->peers = peer;
return peer;
}
fastd_peer* fastd_peer_add(fastd_context *ctx, fastd_peer_config *peer_conf) {
fastd_peer *peer = add_peer(ctx);
peer->config = peer_conf;
peer->state = STATE_WAIT;
setup_peer(ctx, peer);
pr_debug(ctx, "adding peer %P", peer);
return peer;
}
fastd_peer* fastd_peer_add_temp(fastd_context *ctx, const fastd_peer_address *address) {
fastd_peer *peer = add_peer(ctx);
if (!ctx->conf->n_floating)
exit_bug(ctx, "tried to add a temporary peer with no floating remotes defined");
peer->config = NULL;
peer->address = *address;
peer->state = STATE_TEMP;
peer->seen = ctx->now;
pr_debug(ctx, "adding peer %P", peer);
return peer;
}
fastd_peer* fastd_peer_set_established_merge(fastd_context *ctx, fastd_peer *perm_peer, fastd_peer *temp_peer) {
pr_debug(ctx, "merging peer %P into %P", temp_peer, perm_peer);
ctx->conf->protocol->free_peer_state(ctx, perm_peer);
perm_peer->address = temp_peer->address;
perm_peer->state = STATE_ESTABLISHED;
perm_peer->seen = temp_peer->seen;
perm_peer->protocol_state = temp_peer->protocol_state;
temp_peer->protocol_state = NULL;
int i;
for (i = 0; i < ctx->n_eth_addr; i++) {
if (ctx->eth_addr[i].peer == temp_peer) {
ctx->eth_addr[i].peer = perm_peer;
}
}
fastd_peer_reset(ctx, temp_peer);
pr_info(ctx, "Connection with %P established.", perm_peer);
return perm_peer;
}
const fastd_eth_addr* fastd_get_source_address(const fastd_context *ctx, fastd_buffer buffer) {
switch (ctx->conf->mode) {
case MODE_TAP:
return (fastd_eth_addr*)&((struct ethhdr*)buffer.data)->h_source;
default:
exit_bug(ctx, "invalid mode");
}
}
const fastd_eth_addr* fastd_get_dest_address(const fastd_context *ctx, fastd_buffer buffer) {
switch (ctx->conf->mode) {
case MODE_TAP:
return (fastd_eth_addr*)&((struct ethhdr*)buffer.data)->h_dest;
default:
exit_bug(ctx, "invalid mode");
}
}
void fastd_peer_set_established(fastd_context *ctx, fastd_peer *peer) {
fastd_task_delete_peer_handshakes(ctx, peer);
switch(peer->state) {
case STATE_WAIT:
pr_info(ctx, "Connection with %P established.", peer);
peer->state = STATE_ESTABLISHED;
break;
case STATE_TEMP:
exit_bug(ctx, "tried to set a temporary connection to established");
default:
return;
}
}
static inline int fastd_eth_addr_cmp(const fastd_eth_addr *addr1, const fastd_eth_addr *addr2) {
return memcmp(addr1->data, addr2->data, ETH_ALEN);
}
static inline int fastd_peer_eth_addr_cmp(const fastd_peer_eth_addr *addr1, const fastd_peer_eth_addr *addr2) {
return fastd_eth_addr_cmp(&addr1->addr, &addr2->addr);
}
static inline fastd_peer_eth_addr* peer_get_by_addr(fastd_context *ctx, const fastd_eth_addr *addr) {
return bsearch(container_of(addr, fastd_peer_eth_addr, addr), ctx->eth_addr, ctx->n_eth_addr, sizeof(fastd_peer_eth_addr),
(int (*)(const void *, const void *))fastd_peer_eth_addr_cmp);
}
void fastd_peer_eth_addr_add(fastd_context *ctx, fastd_peer *peer, const fastd_eth_addr *addr) {
int min = 0, max = ctx->n_eth_addr;
while (max > min) {
int cur = (min+max)/2;
int cmp = fastd_eth_addr_cmp(addr, &ctx->eth_addr[cur].addr);
if (cmp == 0) {
ctx->eth_addr[cur].peer = peer;
ctx->eth_addr[cur].seen = ctx->now;
return; /* We're done here. */
}
else if (cmp < 0) {
max = cur;
}
else {
min = cur+1;
}
}
ctx->n_eth_addr++;
if (ctx->n_eth_addr > ctx->eth_addr_size) {
if (ctx->eth_addr_size == 0)
ctx->eth_addr_size = 16;
else
ctx->eth_addr_size *= 2;
ctx->eth_addr = realloc(ctx->eth_addr, ctx->eth_addr_size*sizeof(fastd_peer_eth_addr));
}
int i;
for (i = ctx->n_eth_addr-1; i > min; i--)
ctx->eth_addr[i] = ctx->eth_addr[i-1];
ctx->eth_addr[min] = (fastd_peer_eth_addr){ *addr, peer, ctx->now };
pr_debug(ctx, "Learned new MAC address %E on peer %P", addr, peer);
}
void fastd_peer_eth_addr_cleanup(fastd_context *ctx) {
int i, deleted = 0;
for (i = 0; i < ctx->n_eth_addr; i++) {
if (timespec_diff(&ctx->now, &ctx->eth_addr[i].seen) > ctx->conf->eth_addr_stale_time*1000) {
deleted++;
pr_debug(ctx, "MAC address %E not seen for more than %u seconds, removing",
&ctx->eth_addr[i].addr, ctx->conf->eth_addr_stale_time);
}
else if (deleted) {
ctx->eth_addr[i-deleted] = ctx->eth_addr[i];
}
}
ctx->n_eth_addr -= deleted;
}
fastd_peer *fastd_peer_find_by_eth_addr(fastd_context *ctx, const fastd_eth_addr *addr) {
fastd_peer_eth_addr *peer_eth_addr = peer_get_by_addr(ctx, addr);
if (peer_eth_addr)
return peer_eth_addr->peer;
else
return NULL;
}
|