… | |
… | |
32 | #include <arpa/inet.h> |
32 | #include <arpa/inet.h> |
33 | #include <errno.h> |
33 | #include <errno.h> |
34 | #include <time.h> |
34 | #include <time.h> |
35 | #include <unistd.h> |
35 | #include <unistd.h> |
36 | |
36 | |
37 | #include <openssl/rand.h> |
|
|
38 | #include <openssl/hmac.h> |
|
|
39 | #include <openssl/evp.h> |
|
|
40 | #include <openssl/rsa.h> |
|
|
41 | #include <openssl/err.h> |
|
|
42 | |
|
|
43 | extern "C" { |
|
|
44 | # include "lzf/lzf.h" |
|
|
45 | } |
|
|
46 | |
|
|
47 | #include "gettext.h" |
|
|
48 | #include "pidfile.h" |
37 | #include "pidfile.h" |
49 | |
38 | |
50 | #include "conf.h" |
39 | #include "connection.h" |
51 | #include "slog.h" |
40 | #include "util.h" |
52 | #include "device.h" |
|
|
53 | #include "protocol.h" |
41 | #include "protocol.h" |
54 | |
42 | |
55 | #if !HAVE_RAND_PSEUDO_BYTES |
|
|
56 | # define RAND_pseudo_bytes RAND_bytes |
|
|
57 | #endif |
|
|
58 | |
|
|
59 | static time_t next_timecheck; |
|
|
60 | |
|
|
61 | #define MAGIC "vped\xbd\xc6\xdb\x82" // 8 bytes of magic |
|
|
62 | |
|
|
63 | static const rsachallenge & |
|
|
64 | challenge_bytes () |
|
|
65 | { |
|
|
66 | static rsachallenge challenge; |
|
|
67 | static double challenge_ttl; // time this challenge needs to be recreated |
|
|
68 | |
|
|
69 | if (NOW > challenge_ttl) |
|
|
70 | { |
|
|
71 | RAND_bytes ((unsigned char *)&challenge, sizeof (challenge)); |
|
|
72 | challenge_ttl = NOW + CHALLENGE_TTL; |
|
|
73 | } |
|
|
74 | |
|
|
75 | return challenge; |
|
|
76 | } |
|
|
77 | |
|
|
78 | // caching of rsa operations really helps slow computers |
|
|
79 | struct rsa_entry { |
|
|
80 | tstamp expire; |
|
|
81 | rsachallenge chg; |
|
|
82 | RSA *key; // which key |
|
|
83 | rsaencrdata encr; |
|
|
84 | |
|
|
85 | rsa_entry () |
|
|
86 | { |
|
|
87 | expire = NOW + CHALLENGE_TTL; |
|
|
88 | } |
|
|
89 | }; |
|
|
90 | |
|
|
91 | struct rsa_cache : list<rsa_entry> |
|
|
92 | { |
|
|
93 | void cleaner_cb (tstamp &ts); time_watcher cleaner; |
|
|
94 | |
|
|
95 | const rsaencrdata *public_encrypt (RSA *key, const rsachallenge &chg) |
|
|
96 | { |
|
|
97 | for (iterator i = begin (); i != end (); ++i) |
|
|
98 | { |
|
|
99 | if (i->key == key && !memcmp (&chg, &i->chg, sizeof chg)) |
|
|
100 | return &i->encr; |
|
|
101 | } |
|
|
102 | |
|
|
103 | if (cleaner.at < NOW) |
|
|
104 | cleaner.start (NOW + CHALLENGE_TTL); |
|
|
105 | |
|
|
106 | resize (size () + 1); |
|
|
107 | rsa_entry *e = &(*rbegin ()); |
|
|
108 | |
|
|
109 | e->key = key; |
|
|
110 | memcpy (&e->chg, &chg, sizeof chg); |
|
|
111 | |
|
|
112 | if (0 > RSA_public_encrypt (sizeof chg, |
|
|
113 | (unsigned char *)&chg, (unsigned char *)&e->encr, |
|
|
114 | key, RSA_PKCS1_OAEP_PADDING)) |
|
|
115 | fatal ("RSA_public_encrypt error"); |
|
|
116 | |
|
|
117 | return &e->encr; |
|
|
118 | } |
|
|
119 | |
|
|
120 | const rsachallenge *private_decrypt (RSA *key, const rsaencrdata &encr) |
|
|
121 | { |
|
|
122 | for (iterator i = begin (); i != end (); ++i) |
|
|
123 | if (i->key == key && !memcmp (&encr, &i->encr, sizeof encr)) |
|
|
124 | return &i->chg; |
|
|
125 | |
|
|
126 | if (cleaner.at < NOW) |
|
|
127 | cleaner.start (NOW + CHALLENGE_TTL); |
|
|
128 | |
|
|
129 | resize (size () + 1); |
|
|
130 | rsa_entry *e = &(*rbegin ()); |
|
|
131 | |
|
|
132 | e->key = key; |
|
|
133 | memcpy (&e->encr, &encr, sizeof encr); |
|
|
134 | |
|
|
135 | if (0 > RSA_private_decrypt (sizeof encr, |
|
|
136 | (unsigned char *)&encr, (unsigned char *)&e->chg, |
|
|
137 | key, RSA_PKCS1_OAEP_PADDING)) |
|
|
138 | { |
|
|
139 | pop_back (); |
|
|
140 | return 0; |
|
|
141 | } |
|
|
142 | |
|
|
143 | return &e->chg; |
|
|
144 | } |
|
|
145 | |
|
|
146 | rsa_cache () |
|
|
147 | : cleaner (this, &rsa_cache::cleaner_cb) |
|
|
148 | { } |
|
|
149 | |
|
|
150 | } rsa_cache; |
|
|
151 | |
|
|
152 | void rsa_cache::cleaner_cb (tstamp &ts) |
|
|
153 | { |
|
|
154 | if (empty ()) |
|
|
155 | ts = TSTAMP_CANCEL; |
|
|
156 | else |
|
|
157 | { |
|
|
158 | ts = NOW + 3; |
|
|
159 | for (iterator i = begin (); i != end (); ) |
|
|
160 | { |
|
|
161 | if (i->expire >= NOW) |
|
|
162 | i = erase (i); |
|
|
163 | else |
|
|
164 | ++i; |
|
|
165 | } |
|
|
166 | } |
|
|
167 | } |
|
|
168 | |
|
|
169 | // run a script. yes, it's a template function. yes, c++ |
|
|
170 | // is not a functional language. yes, this suxx. |
|
|
171 | template<class owner> |
|
|
172 | static void |
|
|
173 | run_script (owner *obj, const char *(owner::*setup)(), bool wait) |
|
|
174 | { |
|
|
175 | int pid; |
|
|
176 | |
|
|
177 | if ((pid = fork ()) == 0) |
|
|
178 | { |
|
|
179 | char *filename; |
|
|
180 | asprintf (&filename, "%s/%s", confbase, (obj->*setup) ()); |
|
|
181 | execl (filename, filename, (char *) 0); |
|
|
182 | exit (255); |
|
|
183 | } |
|
|
184 | else if (pid > 0) |
|
|
185 | { |
|
|
186 | if (wait) |
|
|
187 | { |
|
|
188 | waitpid (pid, 0, 0); |
|
|
189 | /* TODO: check status */ |
|
|
190 | } |
|
|
191 | } |
|
|
192 | } |
|
|
193 | |
|
|
194 | // xor the socket address into the challenge to ensure different challenges |
|
|
195 | // per host. we could rely on the OAEP padding, but this doesn't hurt. |
|
|
196 | void |
|
|
197 | xor_sa (rsachallenge &k, SOCKADDR *sa) |
|
|
198 | { |
|
|
199 | ((u32 *) k)[(CHG_CIPHER_KEY + 0) / 4] ^= sa->sin_addr.s_addr; |
|
|
200 | ((u16 *) k)[(CHG_CIPHER_KEY + 4) / 2] ^= sa->sin_port; |
|
|
201 | ((u32 *) k)[(CHG_HMAC_KEY + 0) / 4] ^= sa->sin_addr.s_addr; |
|
|
202 | ((u16 *) k)[(CHG_HMAC_KEY + 4) / 2] ^= sa->sin_port; |
|
|
203 | } |
|
|
204 | |
|
|
205 | struct crypto_ctx |
|
|
206 | { |
|
|
207 | EVP_CIPHER_CTX cctx; |
|
|
208 | HMAC_CTX hctx; |
|
|
209 | |
|
|
210 | crypto_ctx (const rsachallenge &challenge, int enc); |
|
|
211 | ~crypto_ctx (); |
|
|
212 | }; |
|
|
213 | |
|
|
214 | crypto_ctx::crypto_ctx (const rsachallenge &challenge, int enc) |
|
|
215 | { |
|
|
216 | EVP_CIPHER_CTX_init (&cctx); |
|
|
217 | EVP_CipherInit_ex (&cctx, CIPHER, 0, &challenge[CHG_CIPHER_KEY], 0, enc); |
|
|
218 | HMAC_CTX_init (&hctx); |
|
|
219 | HMAC_Init_ex (&hctx, &challenge[CHG_HMAC_KEY], HMAC_KEYLEN, DIGEST, 0); |
|
|
220 | } |
|
|
221 | |
|
|
222 | crypto_ctx::~crypto_ctx () |
|
|
223 | { |
|
|
224 | EVP_CIPHER_CTX_cleanup (&cctx); |
|
|
225 | HMAC_CTX_cleanup (&hctx); |
|
|
226 | } |
|
|
227 | |
|
|
228 | ////////////////////////////////////////////////////////////////////////////// |
|
|
229 | |
|
|
230 | void pkt_queue::put (tap_packet *p) |
|
|
231 | { |
|
|
232 | if (queue[i]) |
|
|
233 | { |
|
|
234 | delete queue[i]; |
|
|
235 | j = (j + 1) % QUEUEDEPTH; |
|
|
236 | } |
|
|
237 | |
|
|
238 | queue[i] = p; |
|
|
239 | |
|
|
240 | i = (i + 1) % QUEUEDEPTH; |
|
|
241 | } |
|
|
242 | |
|
|
243 | tap_packet *pkt_queue::get () |
|
|
244 | { |
|
|
245 | tap_packet *p = queue[j]; |
|
|
246 | |
|
|
247 | if (p) |
|
|
248 | { |
|
|
249 | queue[j] = 0; |
|
|
250 | j = (j + 1) % QUEUEDEPTH; |
|
|
251 | } |
|
|
252 | |
|
|
253 | return p; |
|
|
254 | } |
|
|
255 | |
|
|
256 | pkt_queue::pkt_queue () |
|
|
257 | { |
|
|
258 | memset (queue, 0, sizeof (queue)); |
|
|
259 | i = 0; |
|
|
260 | j = 0; |
|
|
261 | } |
|
|
262 | |
|
|
263 | pkt_queue::~pkt_queue () |
|
|
264 | { |
|
|
265 | for (i = QUEUEDEPTH; --i > 0; ) |
|
|
266 | delete queue[i]; |
|
|
267 | } |
|
|
268 | |
|
|
269 | // only do action once every x seconds per host. |
|
|
270 | // currently this is quite a slow implementation, |
|
|
271 | // but suffices for normal operation. |
|
|
272 | struct u32_rate_limiter : private map<u32, tstamp> |
|
|
273 | { |
|
|
274 | tstamp every; |
|
|
275 | |
|
|
276 | bool can (u32 host); |
|
|
277 | |
|
|
278 | u32_rate_limiter (tstamp every = 1) |
|
|
279 | { |
|
|
280 | this->every = every; |
|
|
281 | } |
|
|
282 | }; |
|
|
283 | |
|
|
284 | struct net_rate_limiter : u32_rate_limiter |
|
|
285 | { |
|
|
286 | bool can (SOCKADDR *sa) { return u32_rate_limiter::can((u32)sa->sin_addr.s_addr); } |
|
|
287 | bool can (sockinfo &si) { return u32_rate_limiter::can((u32)si.host); } |
|
|
288 | |
|
|
289 | net_rate_limiter (tstamp every) : u32_rate_limiter (every) {} |
|
|
290 | }; |
|
|
291 | |
|
|
292 | bool u32_rate_limiter::can (u32 host) |
|
|
293 | { |
|
|
294 | iterator i; |
|
|
295 | |
|
|
296 | for (i = begin (); i != end (); ) |
|
|
297 | if (i->second <= NOW) |
|
|
298 | { |
|
|
299 | erase (i); |
|
|
300 | i = begin (); |
|
|
301 | } |
|
|
302 | else |
|
|
303 | ++i; |
|
|
304 | |
|
|
305 | i = find (host); |
|
|
306 | |
|
|
307 | if (i != end ()) |
|
|
308 | return false; |
|
|
309 | |
|
|
310 | insert (value_type (host, NOW + every)); |
|
|
311 | |
|
|
312 | return true; |
|
|
313 | } |
|
|
314 | |
|
|
315 | ///////////////////////////////////////////////////////////////////////////// |
43 | ///////////////////////////////////////////////////////////////////////////// |
316 | |
44 | |
317 | static void next_wakeup (time_t next) |
|
|
318 | { |
|
|
319 | if (next_timecheck > next) |
|
|
320 | next_timecheck = next; |
|
|
321 | } |
|
|
322 | |
|
|
323 | static unsigned char hmac_digest[EVP_MAX_MD_SIZE]; |
|
|
324 | |
|
|
325 | struct hmac_packet:net_packet |
|
|
326 | { |
|
|
327 | u8 hmac[HMACLENGTH]; // each and every packet has a hmac field, but that is not (yet) checked everywhere |
|
|
328 | |
|
|
329 | void hmac_set (crypto_ctx * ctx); |
|
|
330 | bool hmac_chk (crypto_ctx * ctx); |
|
|
331 | |
|
|
332 | private: |
|
|
333 | void hmac_gen (crypto_ctx * ctx) |
|
|
334 | { |
|
|
335 | unsigned int xlen; |
|
|
336 | HMAC_CTX *hctx = &ctx->hctx; |
|
|
337 | |
|
|
338 | HMAC_Init_ex (hctx, 0, 0, 0, 0); |
|
|
339 | HMAC_Update (hctx, ((unsigned char *) this) + sizeof (hmac_packet), |
|
|
340 | len - sizeof (hmac_packet)); |
|
|
341 | HMAC_Final (hctx, (unsigned char *) &hmac_digest, &xlen); |
|
|
342 | } |
|
|
343 | }; |
|
|
344 | |
|
|
345 | void |
|
|
346 | hmac_packet::hmac_set (crypto_ctx * ctx) |
|
|
347 | { |
|
|
348 | hmac_gen (ctx); |
|
|
349 | |
|
|
350 | memcpy (hmac, hmac_digest, HMACLENGTH); |
|
|
351 | } |
|
|
352 | |
|
|
353 | bool |
|
|
354 | hmac_packet::hmac_chk (crypto_ctx * ctx) |
|
|
355 | { |
|
|
356 | hmac_gen (ctx); |
|
|
357 | |
|
|
358 | return !memcmp (hmac, hmac_digest, HMACLENGTH); |
|
|
359 | } |
|
|
360 | |
|
|
361 | struct vpn_packet : hmac_packet |
|
|
362 | { |
|
|
363 | enum ptype |
|
|
364 | { |
|
|
365 | PT_RESET = 0, |
|
|
366 | PT_DATA_UNCOMPRESSED, |
|
|
367 | PT_DATA_COMPRESSED, |
|
|
368 | PT_PING, PT_PONG, // wasting namespace space? ;) |
|
|
369 | PT_AUTH, // authentification |
|
|
370 | PT_CONNECT_REQ, // want other host to contact me |
|
|
371 | PT_CONNECT_INFO, // request connection to some node |
|
|
372 | PT_REKEY, // rekeying (not yet implemented) |
|
|
373 | PT_MAX |
|
|
374 | }; |
|
|
375 | |
|
|
376 | u8 type; |
|
|
377 | u8 srcdst, src1, dst1; |
|
|
378 | |
|
|
379 | void set_hdr (ptype type, unsigned int dst); |
|
|
380 | |
|
|
381 | unsigned int src () |
|
|
382 | { |
|
|
383 | return src1 | ((srcdst >> 4) << 8); |
|
|
384 | } |
|
|
385 | |
|
|
386 | unsigned int dst () |
|
|
387 | { |
|
|
388 | return dst1 | ((srcdst & 0xf) << 8); |
|
|
389 | } |
|
|
390 | |
|
|
391 | ptype typ () |
|
|
392 | { |
|
|
393 | return (ptype) type; |
|
|
394 | } |
|
|
395 | }; |
|
|
396 | |
|
|
397 | void vpn_packet::set_hdr (ptype type, unsigned int dst) |
|
|
398 | { |
|
|
399 | this->type = type; |
|
|
400 | |
|
|
401 | int src = THISNODE->id; |
|
|
402 | |
|
|
403 | src1 = src; |
|
|
404 | srcdst = ((src >> 8) << 4) | (dst >> 8); |
|
|
405 | dst1 = dst; |
|
|
406 | } |
|
|
407 | |
|
|
408 | #define MAXVPNDATA (MAX_MTU - 6 - 6) |
|
|
409 | #define DATAHDR (sizeof (u32) + RAND_SIZE) |
|
|
410 | |
|
|
411 | struct vpndata_packet:vpn_packet |
|
|
412 | { |
|
|
413 | u8 data[MAXVPNDATA + DATAHDR]; // seqno |
|
|
414 | |
|
|
415 | void setup (connection *conn, int dst, u8 *d, u32 len, u32 seqno); |
|
|
416 | tap_packet *unpack (connection *conn, u32 &seqno); |
|
|
417 | private: |
|
|
418 | |
|
|
419 | const u32 data_hdr_size () const |
|
|
420 | { |
|
|
421 | return sizeof (vpndata_packet) - sizeof (net_packet) - MAXVPNDATA - DATAHDR; |
|
|
422 | } |
|
|
423 | }; |
|
|
424 | |
|
|
425 | void |
|
|
426 | vpndata_packet::setup (connection *conn, int dst, u8 *d, u32 l, u32 seqno) |
|
|
427 | { |
|
|
428 | EVP_CIPHER_CTX *cctx = &conn->octx->cctx; |
|
|
429 | int outl = 0, outl2; |
|
|
430 | ptype type = PT_DATA_UNCOMPRESSED; |
|
|
431 | |
|
|
432 | #if ENABLE_COMPRESSION |
|
|
433 | u8 cdata[MAX_MTU]; |
|
|
434 | u32 cl; |
|
|
435 | |
|
|
436 | cl = lzf_compress (d, l, cdata + 2, (l - 2) & ~7); |
|
|
437 | if (cl) |
|
|
438 | { |
|
|
439 | type = PT_DATA_COMPRESSED; |
|
|
440 | d = cdata; |
|
|
441 | l = cl + 2; |
|
|
442 | |
|
|
443 | d[0] = cl >> 8; |
|
|
444 | d[1] = cl; |
|
|
445 | } |
|
|
446 | #endif |
|
|
447 | |
|
|
448 | EVP_EncryptInit_ex (cctx, 0, 0, 0, 0); |
|
|
449 | |
|
|
450 | #if RAND_SIZE |
|
|
451 | struct { |
|
|
452 | u8 rnd[RAND_SIZE]; |
|
|
453 | u32 seqno; |
|
|
454 | } datahdr; |
|
|
455 | |
|
|
456 | datahdr.seqno = seqno; |
|
|
457 | RAND_pseudo_bytes ((unsigned char *) datahdr.rnd, RAND_SIZE); |
|
|
458 | |
|
|
459 | EVP_EncryptUpdate (cctx, |
|
|
460 | (unsigned char *) data + outl, &outl2, |
|
|
461 | (unsigned char *) &datahdr, DATAHDR); |
|
|
462 | outl += outl2; |
|
|
463 | #else |
|
|
464 | EVP_EncryptUpdate (cctx, |
|
|
465 | (unsigned char *) data + outl, &outl2, |
|
|
466 | (unsigned char *) &seqno, DATAHDR); |
|
|
467 | outl += outl2; |
|
|
468 | #endif |
|
|
469 | |
|
|
470 | EVP_EncryptUpdate (cctx, |
|
|
471 | (unsigned char *) data + outl, &outl2, |
|
|
472 | (unsigned char *) d, l); |
|
|
473 | outl += outl2; |
|
|
474 | |
|
|
475 | EVP_EncryptFinal_ex (cctx, (unsigned char *) data + outl, &outl2); |
|
|
476 | outl += outl2; |
|
|
477 | |
|
|
478 | len = outl + data_hdr_size (); |
|
|
479 | |
|
|
480 | set_hdr (type, dst); |
|
|
481 | |
|
|
482 | hmac_set (conn->octx); |
|
|
483 | } |
|
|
484 | |
|
|
485 | tap_packet * |
|
|
486 | vpndata_packet::unpack (connection *conn, u32 &seqno) |
|
|
487 | { |
|
|
488 | EVP_CIPHER_CTX *cctx = &conn->ictx->cctx; |
|
|
489 | int outl = 0, outl2; |
|
|
490 | tap_packet *p = new tap_packet; |
|
|
491 | u8 *d; |
|
|
492 | u32 l = len - data_hdr_size (); |
|
|
493 | |
|
|
494 | EVP_DecryptInit_ex (cctx, 0, 0, 0, 0); |
|
|
495 | |
|
|
496 | #if ENABLE_COMPRESSION |
|
|
497 | u8 cdata[MAX_MTU]; |
|
|
498 | |
|
|
499 | if (type == PT_DATA_COMPRESSED) |
|
|
500 | d = cdata; |
|
|
501 | else |
|
|
502 | #endif |
|
|
503 | d = &(*p)[6 + 6 - DATAHDR]; |
|
|
504 | |
|
|
505 | /* this overwrites part of the src mac, but we fix that later */ |
|
|
506 | EVP_DecryptUpdate (cctx, |
|
|
507 | d, &outl2, |
|
|
508 | (unsigned char *)&data, len - data_hdr_size ()); |
|
|
509 | outl += outl2; |
|
|
510 | |
|
|
511 | EVP_DecryptFinal_ex (cctx, (unsigned char *)d + outl, &outl2); |
|
|
512 | outl += outl2; |
|
|
513 | |
|
|
514 | seqno = *(u32 *)(d + RAND_SIZE); |
|
|
515 | |
|
|
516 | id2mac (dst () ? dst() : THISNODE->id, p->dst); |
|
|
517 | id2mac (src (), p->src); |
|
|
518 | |
|
|
519 | #if ENABLE_COMPRESSION |
|
|
520 | if (type == PT_DATA_COMPRESSED) |
|
|
521 | { |
|
|
522 | u32 cl = (d[DATAHDR] << 8) | d[DATAHDR + 1]; |
|
|
523 | p->len = lzf_decompress (d + DATAHDR + 2, cl, &(*p)[6 + 6], MAX_MTU) + 6 + 6; |
|
|
524 | } |
|
|
525 | else |
|
|
526 | p->len = outl + (6 + 6 - DATAHDR); |
|
|
527 | #endif |
|
|
528 | |
|
|
529 | return p; |
|
|
530 | } |
|
|
531 | |
|
|
532 | struct ping_packet : vpn_packet |
|
|
533 | { |
|
|
534 | void setup (int dst, ptype type) |
|
|
535 | { |
|
|
536 | set_hdr (type, dst); |
|
|
537 | len = sizeof (*this) - sizeof (net_packet); |
|
|
538 | } |
|
|
539 | }; |
|
|
540 | |
|
|
541 | struct config_packet : vpn_packet |
|
|
542 | { |
|
|
543 | // actually, hmaclen cannot be checked because the hmac |
|
|
544 | // field comes before this data, so peers with other |
|
|
545 | // hmacs simply will not work. |
|
|
546 | u8 prot_major, prot_minor, randsize, hmaclen; |
|
|
547 | u8 flags, challengelen, pad2, pad3; |
|
|
548 | u32 cipher_nid; |
|
|
549 | u32 digest_nid; |
|
|
550 | |
|
|
551 | const u8 curflags () const |
|
|
552 | { |
|
|
553 | return 0x80 |
|
|
554 | | 0x02 |
|
|
555 | #if PROTOCOL_MAJOR != 2 |
|
|
556 | #error hi |
|
|
557 | #endif |
|
|
558 | | (ENABLE_COMPRESSION ? 0x01 : 0x00); |
|
|
559 | } |
|
|
560 | |
|
|
561 | void setup (ptype type, int dst) |
|
|
562 | { |
|
|
563 | prot_major = PROTOCOL_MAJOR; |
|
|
564 | prot_minor = PROTOCOL_MINOR; |
|
|
565 | randsize = RAND_SIZE; |
|
|
566 | hmaclen = HMACLENGTH; |
|
|
567 | flags = curflags (); |
|
|
568 | challengelen = sizeof (rsachallenge); |
|
|
569 | |
|
|
570 | cipher_nid = htonl (EVP_CIPHER_nid (CIPHER)); |
|
|
571 | digest_nid = htonl (EVP_MD_type (DIGEST)); |
|
|
572 | |
|
|
573 | len = sizeof (*this) - sizeof (net_packet); |
|
|
574 | set_hdr (type, dst); |
|
|
575 | } |
|
|
576 | |
|
|
577 | bool chk_config () |
|
|
578 | { |
|
|
579 | return prot_major == PROTOCOL_MAJOR |
|
|
580 | && randsize == RAND_SIZE |
|
|
581 | && hmaclen == HMACLENGTH |
|
|
582 | && flags == curflags () |
|
|
583 | && challengelen == sizeof (rsachallenge) |
|
|
584 | && cipher_nid == htonl (EVP_CIPHER_nid (CIPHER)) |
|
|
585 | && digest_nid == htonl (EVP_MD_type (DIGEST)); |
|
|
586 | } |
|
|
587 | }; |
|
|
588 | |
|
|
589 | struct auth_packet : config_packet |
|
|
590 | { |
|
|
591 | char magic[8]; |
|
|
592 | u8 subtype; |
|
|
593 | u8 pad1, pad2; |
|
|
594 | rsaencrdata challenge; |
|
|
595 | |
|
|
596 | auth_packet (int dst, auth_subtype stype) |
|
|
597 | { |
|
|
598 | config_packet::setup (PT_AUTH, dst); |
|
|
599 | subtype = stype; |
|
|
600 | len = sizeof (*this) - sizeof (net_packet); |
|
|
601 | strncpy (magic, MAGIC, 8); |
|
|
602 | } |
|
|
603 | }; |
|
|
604 | |
|
|
605 | struct connect_req_packet : vpn_packet |
|
|
606 | { |
|
|
607 | u8 id; |
|
|
608 | u8 pad1, pad2, pad3; |
|
|
609 | |
|
|
610 | connect_req_packet (int dst, int id) |
|
|
611 | { |
|
|
612 | this->id = id; |
|
|
613 | set_hdr (PT_CONNECT_REQ, dst); |
|
|
614 | len = sizeof (*this) - sizeof (net_packet); |
|
|
615 | } |
|
|
616 | }; |
|
|
617 | |
|
|
618 | struct connect_info_packet : vpn_packet |
|
|
619 | { |
|
|
620 | u8 id; |
|
|
621 | u8 pad1, pad2, pad3; |
|
|
622 | sockinfo si; |
|
|
623 | |
|
|
624 | connect_info_packet (int dst, int id, sockinfo &si) |
|
|
625 | { |
|
|
626 | this->id = id; |
|
|
627 | this->si = si; |
|
|
628 | set_hdr (PT_CONNECT_INFO, dst); |
|
|
629 | len = sizeof (*this) - sizeof (net_packet); |
|
|
630 | } |
|
|
631 | }; |
|
|
632 | |
|
|
633 | ///////////////////////////////////////////////////////////////////////////// |
|
|
634 | |
|
|
635 | void |
|
|
636 | fill_sa (SOCKADDR *sa, conf_node *conf) |
|
|
637 | { |
|
|
638 | sa->sin_family = AF_INET; |
|
|
639 | sa->sin_port = htons (conf->port); |
|
|
640 | sa->sin_addr.s_addr = 0; |
|
|
641 | |
|
|
642 | if (conf->hostname) |
|
|
643 | { |
|
|
644 | struct hostent *he = gethostbyname (conf->hostname); |
|
|
645 | |
|
|
646 | if (he |
|
|
647 | && he->h_addrtype == AF_INET && he->h_length == 4 && he->h_addr_list[0]) |
|
|
648 | { |
|
|
649 | //sa->sin_family = he->h_addrtype; |
|
|
650 | memcpy (&sa->sin_addr, he->h_addr_list[0], 4); |
|
|
651 | } |
|
|
652 | else |
|
|
653 | slog (L_NOTICE, _("unable to resolve host '%s'"), conf->hostname); |
|
|
654 | } |
|
|
655 | } |
|
|
656 | |
|
|
657 | void |
|
|
658 | connection::reset_dstaddr () |
|
|
659 | { |
|
|
660 | fill_sa (&sa, conf); |
|
|
661 | } |
|
|
662 | |
|
|
663 | void |
|
|
664 | connection::send_ping (SOCKADDR *dsa, u8 pong) |
|
|
665 | { |
|
|
666 | ping_packet *pkt = new ping_packet; |
|
|
667 | |
|
|
668 | pkt->setup (conf->id, pong ? ping_packet::PT_PONG : ping_packet::PT_PING); |
|
|
669 | vpn->send_vpn_packet (pkt, dsa, IPTOS_LOWDELAY); |
|
|
670 | |
|
|
671 | delete pkt; |
|
|
672 | } |
|
|
673 | |
|
|
674 | void |
|
|
675 | connection::send_reset (SOCKADDR *dsa) |
|
|
676 | { |
|
|
677 | static net_rate_limiter limiter(1); |
|
|
678 | |
|
|
679 | if (limiter.can (dsa) && connectmode != conf_node::C_DISABLED) |
|
|
680 | { |
|
|
681 | config_packet *pkt = new config_packet; |
|
|
682 | |
|
|
683 | pkt->setup (vpn_packet::PT_RESET, conf->id); |
|
|
684 | vpn->send_vpn_packet (pkt, dsa, IPTOS_MINCOST); |
|
|
685 | |
|
|
686 | delete pkt; |
|
|
687 | } |
|
|
688 | } |
|
|
689 | |
|
|
690 | static rsachallenge * |
|
|
691 | gen_challenge (u32 seqrand, SOCKADDR *sa) |
|
|
692 | { |
|
|
693 | static rsachallenge k; |
|
|
694 | |
|
|
695 | memcpy (&k, &challenge_bytes (), sizeof (k)); |
|
|
696 | *(u32 *)&k[CHG_SEQNO] ^= seqrand; |
|
|
697 | xor_sa (k, sa); |
|
|
698 | |
|
|
699 | return &k; |
|
|
700 | } |
|
|
701 | |
|
|
702 | void |
|
|
703 | connection::send_auth (auth_subtype subtype, SOCKADDR *sa, const rsachallenge *k) |
|
|
704 | { |
|
|
705 | static net_rate_limiter limiter(0.2); |
|
|
706 | |
|
|
707 | if (subtype != AUTH_INIT || limiter.can (sa)) |
|
|
708 | { |
|
|
709 | if (!k) |
|
|
710 | k = gen_challenge (seqrand, sa); |
|
|
711 | |
|
|
712 | auth_packet *pkt = new auth_packet (conf->id, subtype); |
|
|
713 | |
|
|
714 | memcpy (pkt->challenge, rsa_cache.public_encrypt (conf->rsa_key, *k), sizeof (rsaencrdata)); |
|
|
715 | |
|
|
716 | slog (L_TRACE, ">>%d PT_AUTH(%d) [%s]", conf->id, subtype, (const char *)sockinfo (sa)); |
|
|
717 | |
|
|
718 | vpn->send_vpn_packet (pkt, sa, IPTOS_RELIABILITY); |
|
|
719 | |
|
|
720 | delete pkt; |
|
|
721 | } |
|
|
722 | } |
|
|
723 | |
|
|
724 | void |
|
|
725 | connection::establish_connection_cb (tstamp &ts) |
|
|
726 | { |
|
|
727 | if (ictx || conf == THISNODE || connectmode == conf_node::C_NEVER) |
|
|
728 | ts = TSTAMP_CANCEL; |
|
|
729 | else if (ts <= NOW) |
|
|
730 | { |
|
|
731 | double retry_int = double (retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2)) * 0.25; |
|
|
732 | |
|
|
733 | if (retry_int < 3600 * 8) |
|
|
734 | retry_cnt++; |
|
|
735 | |
|
|
736 | if (connectmode == conf_node::C_ONDEMAND |
|
|
737 | && retry_int > ::conf.keepalive) |
|
|
738 | retry_int = ::conf.keepalive; |
|
|
739 | |
|
|
740 | ts = NOW + retry_int; |
|
|
741 | |
|
|
742 | if (conf->hostname) |
|
|
743 | { |
|
|
744 | reset_dstaddr (); |
|
|
745 | if (sa.sin_addr.s_addr) |
|
|
746 | if (retry_cnt < 4) |
|
|
747 | send_auth (AUTH_INIT, &sa); |
|
|
748 | else |
|
|
749 | send_ping (&sa, 0); |
|
|
750 | } |
|
|
751 | else |
|
|
752 | vpn->connect_request (conf->id); |
|
|
753 | } |
|
|
754 | } |
|
|
755 | |
|
|
756 | void |
|
|
757 | connection::reset_connection () |
|
|
758 | { |
|
|
759 | if (ictx && octx) |
|
|
760 | { |
|
|
761 | slog (L_INFO, _("connection to %d (%s) lost"), conf->id, conf->nodename); |
|
|
762 | |
|
|
763 | if (::conf.script_node_down) |
|
|
764 | run_script (this, &connection::script_node_down, false); |
|
|
765 | } |
|
|
766 | |
|
|
767 | delete ictx; ictx = 0; |
|
|
768 | delete octx; octx = 0; |
|
|
769 | |
|
|
770 | RAND_bytes ((unsigned char *)&seqrand, sizeof (u32)); |
|
|
771 | |
|
|
772 | sa.sin_port = 0; |
|
|
773 | sa.sin_addr.s_addr = 0; |
|
|
774 | |
|
|
775 | last_activity = 0; |
|
|
776 | |
|
|
777 | rekey.reset (); |
|
|
778 | keepalive.reset (); |
|
|
779 | establish_connection.reset (); |
|
|
780 | } |
|
|
781 | |
|
|
782 | void |
|
|
783 | connection::shutdown () |
|
|
784 | { |
|
|
785 | if (ictx && octx) |
|
|
786 | send_reset (&sa); |
|
|
787 | |
|
|
788 | reset_connection (); |
|
|
789 | } |
|
|
790 | |
|
|
791 | void |
|
|
792 | connection::rekey_cb (tstamp &ts) |
|
|
793 | { |
|
|
794 | ts = TSTAMP_CANCEL; |
|
|
795 | |
|
|
796 | reset_connection (); |
|
|
797 | establish_connection (); |
|
|
798 | } |
|
|
799 | |
|
|
800 | void |
|
|
801 | connection::send_data_packet (tap_packet * pkt, bool broadcast) |
|
|
802 | { |
|
|
803 | vpndata_packet *p = new vpndata_packet; |
|
|
804 | int tos = 0; |
|
|
805 | |
|
|
806 | if (conf->inherit_tos |
|
|
807 | && (*pkt)[12] == 0x08 && (*pkt)[13] == 0x00 // IP |
|
|
808 | && ((*pkt)[14] & 0xf0) == 0x40) // IPv4 |
|
|
809 | tos = (*pkt)[15] & IPTOS_TOS_MASK; |
|
|
810 | |
|
|
811 | p->setup (this, broadcast ? 0 : conf->id, &((*pkt)[6 + 6]), pkt->len - 6 - 6, ++oseqno); // skip 2 macs |
|
|
812 | vpn->send_vpn_packet (p, &sa, tos); |
|
|
813 | |
|
|
814 | delete p; |
|
|
815 | |
|
|
816 | if (oseqno > MAX_SEQNO) |
|
|
817 | rekey (); |
|
|
818 | } |
|
|
819 | |
|
|
820 | void |
|
|
821 | connection::inject_data_packet (tap_packet *pkt, bool broadcast) |
|
|
822 | { |
|
|
823 | if (ictx && octx) |
|
|
824 | send_data_packet (pkt, broadcast); |
|
|
825 | else |
|
|
826 | { |
|
|
827 | if (!broadcast)//DDDD |
|
|
828 | queue.put (new tap_packet (*pkt)); |
|
|
829 | |
|
|
830 | establish_connection (); |
|
|
831 | } |
|
|
832 | } |
|
|
833 | |
|
|
834 | void |
|
|
835 | connection::recv_vpn_packet (vpn_packet *pkt, SOCKADDR *ssa) |
|
|
836 | { |
|
|
837 | last_activity = NOW; |
|
|
838 | |
|
|
839 | slog (L_NOISE, "<<%d received packet type %d from %d to %d", |
|
|
840 | conf->id, pkt->typ (), pkt->src (), pkt->dst ()); |
|
|
841 | |
|
|
842 | switch (pkt->typ ()) |
|
|
843 | { |
|
|
844 | case vpn_packet::PT_PING: |
|
|
845 | send_ping (ssa, 1); // pong |
|
|
846 | break; |
|
|
847 | |
|
|
848 | case vpn_packet::PT_PONG: |
|
|
849 | // we send pings instead of auth packets after some retries, |
|
|
850 | // so reset the retry counter and establish a conenction |
|
|
851 | // when we receive a pong. |
|
|
852 | if (!ictx && !octx) |
|
|
853 | { |
|
|
854 | retry_cnt = 0; |
|
|
855 | establish_connection.at = 0; |
|
|
856 | establish_connection (); |
|
|
857 | } |
|
|
858 | |
|
|
859 | break; |
|
|
860 | |
|
|
861 | case vpn_packet::PT_RESET: |
|
|
862 | { |
|
|
863 | reset_connection (); |
|
|
864 | |
|
|
865 | config_packet *p = (config_packet *) pkt; |
|
|
866 | if (!p->chk_config ()) |
|
|
867 | { |
|
|
868 | slog (L_WARN, _("protocol mismatch, disabling node '%s'"), conf->nodename); |
|
|
869 | connectmode = conf_node::C_DISABLED; |
|
|
870 | } |
|
|
871 | else if (connectmode == conf_node::C_ALWAYS) |
|
|
872 | establish_connection (); |
|
|
873 | } |
|
|
874 | break; |
|
|
875 | |
|
|
876 | case vpn_packet::PT_AUTH: |
|
|
877 | { |
|
|
878 | auth_packet *p = (auth_packet *) pkt; |
|
|
879 | |
|
|
880 | slog (L_TRACE, "<<%d PT_AUTH(%d)", conf->id, p->subtype); |
|
|
881 | |
|
|
882 | if (p->chk_config () |
|
|
883 | && !strncmp (p->magic, MAGIC, 8)) |
|
|
884 | { |
|
|
885 | if (p->prot_minor != PROTOCOL_MINOR) |
|
|
886 | slog (L_INFO, _("protocol minor version mismatch: ours is %d, %s's is %d."), |
|
|
887 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
|
|
888 | |
|
|
889 | if (p->subtype == AUTH_INIT) |
|
|
890 | send_auth (AUTH_INITREPLY, ssa); |
|
|
891 | |
|
|
892 | const rsachallenge *k = rsa_cache.private_decrypt (::conf.rsa_key, p->challenge); |
|
|
893 | |
|
|
894 | if (!k) |
|
|
895 | { |
|
|
896 | slog (L_ERR, _("challenge from %s (%s) illegal or corrupted, disabling node"), |
|
|
897 | conf->nodename, (const char *)sockinfo (ssa)); |
|
|
898 | connectmode = conf_node::C_DISABLED; |
|
|
899 | break; |
|
|
900 | } |
|
|
901 | |
|
|
902 | retry_cnt = 0; |
|
|
903 | establish_connection.set (NOW + 8); //? ;) |
|
|
904 | keepalive.reset (); |
|
|
905 | rekey.reset (); |
|
|
906 | |
|
|
907 | switch (p->subtype) |
|
|
908 | { |
|
|
909 | case AUTH_INIT: |
|
|
910 | case AUTH_INITREPLY: |
|
|
911 | delete ictx; |
|
|
912 | ictx = 0; |
|
|
913 | |
|
|
914 | delete octx; |
|
|
915 | |
|
|
916 | octx = new crypto_ctx (*k, 1); |
|
|
917 | oseqno = *(u32 *)&k[CHG_SEQNO] & 0x7fffffff; |
|
|
918 | |
|
|
919 | send_auth (AUTH_REPLY, ssa, k); |
|
|
920 | break; |
|
|
921 | |
|
|
922 | case AUTH_REPLY: |
|
|
923 | |
|
|
924 | if (!memcmp ((u8 *)gen_challenge (seqrand, ssa), (u8 *)k, sizeof (rsachallenge))) |
|
|
925 | { |
|
|
926 | delete ictx; |
|
|
927 | |
|
|
928 | ictx = new crypto_ctx (*k, 0); |
|
|
929 | iseqno.reset (*(u32 *)&k[CHG_SEQNO] & 0x7fffffff); // at least 2**31 sequence numbers are valid |
|
|
930 | |
|
|
931 | sa = *ssa; |
|
|
932 | |
|
|
933 | rekey.set (NOW + ::conf.rekey); |
|
|
934 | keepalive.set (NOW + ::conf.keepalive); |
|
|
935 | |
|
|
936 | // send queued packets |
|
|
937 | while (tap_packet *p = queue.get ()) |
|
|
938 | { |
|
|
939 | send_data_packet (p); |
|
|
940 | delete p; |
|
|
941 | } |
|
|
942 | |
|
|
943 | connectmode = conf->connectmode; |
|
|
944 | |
|
|
945 | slog (L_INFO, _("connection to %d (%s %s) established"), |
|
|
946 | conf->id, conf->nodename, (const char *)sockinfo (ssa)); |
|
|
947 | |
|
|
948 | if (::conf.script_node_up) |
|
|
949 | run_script (this, &connection::script_node_up, false); |
|
|
950 | } |
|
|
951 | else |
|
|
952 | slog (L_ERR, _("sent and received challenge do not match with (%s %s))"), |
|
|
953 | conf->nodename, (const char *)sockinfo (ssa)); |
|
|
954 | |
|
|
955 | break; |
|
|
956 | default: |
|
|
957 | slog (L_ERR, _("authentification illegal subtype error (%s %s)"), |
|
|
958 | conf->nodename, (const char *)sockinfo (ssa)); |
|
|
959 | break; |
|
|
960 | } |
|
|
961 | } |
|
|
962 | else |
|
|
963 | send_reset (ssa); |
|
|
964 | |
|
|
965 | break; |
|
|
966 | } |
|
|
967 | |
|
|
968 | case vpn_packet::PT_DATA_COMPRESSED: |
|
|
969 | #if !ENABLE_COMPRESSION |
|
|
970 | send_reset (ssa); |
|
|
971 | break; |
|
|
972 | #endif |
|
|
973 | case vpn_packet::PT_DATA_UNCOMPRESSED: |
|
|
974 | |
|
|
975 | if (ictx && octx) |
|
|
976 | { |
|
|
977 | vpndata_packet *p = (vpndata_packet *)pkt; |
|
|
978 | |
|
|
979 | if (*ssa == sa) |
|
|
980 | { |
|
|
981 | if (!p->hmac_chk (ictx)) |
|
|
982 | slog (L_ERR, _("hmac authentication error, received invalid packet\n" |
|
|
983 | "could be an attack, or just corruption or an synchronization error")); |
|
|
984 | else |
|
|
985 | { |
|
|
986 | u32 seqno; |
|
|
987 | tap_packet *d = p->unpack (this, seqno); |
|
|
988 | |
|
|
989 | if (iseqno.recv_ok (seqno)) |
|
|
990 | { |
|
|
991 | vpn->tap->send (d); |
|
|
992 | |
|
|
993 | if (p->dst () == 0) // re-broadcast |
|
|
994 | for (vpn::conns_vector::iterator i = vpn->conns.begin (); i != vpn->conns.end (); ++i) |
|
|
995 | { |
|
|
996 | connection *c = *i; |
|
|
997 | |
|
|
998 | if (c->conf != THISNODE && c->conf != conf) |
|
|
999 | c->inject_data_packet (d); |
|
|
1000 | } |
|
|
1001 | |
|
|
1002 | delete d; |
|
|
1003 | |
|
|
1004 | break; |
|
|
1005 | } |
|
|
1006 | } |
|
|
1007 | } |
|
|
1008 | else |
|
|
1009 | slog (L_ERR, _("received data packet from unknown source %s"), (const char *)sockinfo (ssa));//D |
|
|
1010 | } |
|
|
1011 | |
|
|
1012 | send_reset (ssa); |
|
|
1013 | break; |
|
|
1014 | |
|
|
1015 | case vpn_packet::PT_CONNECT_REQ: |
|
|
1016 | if (ictx && octx && *ssa == sa && pkt->hmac_chk (ictx)) |
|
|
1017 | { |
|
|
1018 | connect_req_packet *p = (connect_req_packet *) pkt; |
|
|
1019 | |
|
|
1020 | assert (p->id > 0 && p->id <= vpn->conns.size ()); // hmac-auth does not mean we accept anything |
|
|
1021 | |
|
|
1022 | connection *c = vpn->conns[p->id - 1]; |
|
|
1023 | |
|
|
1024 | slog (L_TRACE, "<<%d PT_CONNECT_REQ(%d) [%d]\n", |
|
|
1025 | conf->id, p->id, c->ictx && c->octx); |
|
|
1026 | |
|
|
1027 | if (c->ictx && c->octx) |
|
|
1028 | { |
|
|
1029 | // send connect_info packets to both sides, in case one is |
|
|
1030 | // behind a nat firewall (or both ;) |
|
|
1031 | { |
|
|
1032 | sockinfo si(sa); |
|
|
1033 | |
|
|
1034 | slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)\n", |
|
|
1035 | c->conf->id, conf->id, (const char *)si); |
|
|
1036 | |
|
|
1037 | connect_info_packet *r = new connect_info_packet (c->conf->id, conf->id, si); |
|
|
1038 | |
|
|
1039 | r->hmac_set (c->octx); |
|
|
1040 | vpn->send_vpn_packet (r, &c->sa); |
|
|
1041 | |
|
|
1042 | delete r; |
|
|
1043 | } |
|
|
1044 | |
|
|
1045 | { |
|
|
1046 | sockinfo si(c->sa); |
|
|
1047 | |
|
|
1048 | slog (L_TRACE, ">>%d PT_CONNECT_INFO(%d,%s)\n", |
|
|
1049 | conf->id, c->conf->id, (const char *)si); |
|
|
1050 | |
|
|
1051 | connect_info_packet *r = new connect_info_packet (conf->id, c->conf->id, si); |
|
|
1052 | |
|
|
1053 | r->hmac_set (octx); |
|
|
1054 | vpn->send_vpn_packet (r, &sa); |
|
|
1055 | |
|
|
1056 | delete r; |
|
|
1057 | } |
|
|
1058 | } |
|
|
1059 | } |
|
|
1060 | |
|
|
1061 | break; |
|
|
1062 | |
|
|
1063 | case vpn_packet::PT_CONNECT_INFO: |
|
|
1064 | if (ictx && octx && *ssa == sa && pkt->hmac_chk (ictx)) |
|
|
1065 | { |
|
|
1066 | connect_info_packet *p = (connect_info_packet *) pkt; |
|
|
1067 | |
|
|
1068 | assert (p->id > 0 && p->id <= vpn->conns.size ()); // hmac-auth does not mean we accept anything |
|
|
1069 | |
|
|
1070 | connection *c = vpn->conns[p->id - 1]; |
|
|
1071 | |
|
|
1072 | slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", |
|
|
1073 | conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx); |
|
|
1074 | |
|
|
1075 | c->send_auth (AUTH_INIT, p->si.sa ()); |
|
|
1076 | } |
|
|
1077 | break; |
|
|
1078 | |
|
|
1079 | default: |
|
|
1080 | send_reset (ssa); |
|
|
1081 | break; |
|
|
1082 | } |
|
|
1083 | } |
|
|
1084 | |
|
|
1085 | void connection::keepalive_cb (tstamp &ts) |
|
|
1086 | { |
|
|
1087 | if (NOW >= last_activity + ::conf.keepalive + 30) |
|
|
1088 | { |
|
|
1089 | reset_connection (); |
|
|
1090 | establish_connection (); |
|
|
1091 | } |
|
|
1092 | else if (NOW < last_activity + ::conf.keepalive) |
|
|
1093 | ts = last_activity + ::conf.keepalive; |
|
|
1094 | else if (conf->connectmode != conf_node::C_ONDEMAND |
|
|
1095 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
|
|
1096 | { |
|
|
1097 | send_ping (&sa); |
|
|
1098 | ts = NOW + 5; |
|
|
1099 | } |
|
|
1100 | else |
|
|
1101 | reset_connection (); |
|
|
1102 | |
|
|
1103 | } |
|
|
1104 | |
|
|
1105 | void connection::connect_request (int id) |
|
|
1106 | { |
|
|
1107 | connect_req_packet *p = new connect_req_packet (conf->id, id); |
|
|
1108 | |
|
|
1109 | slog (L_TRACE, ">>%d PT_CONNECT_REQ(%d)", id, conf->id); |
|
|
1110 | p->hmac_set (octx); |
|
|
1111 | vpn->send_vpn_packet (p, &sa); |
|
|
1112 | |
|
|
1113 | delete p; |
|
|
1114 | } |
|
|
1115 | |
|
|
1116 | void connection::script_node () |
|
|
1117 | { |
|
|
1118 | vpn->script_if_up (); |
|
|
1119 | |
|
|
1120 | char *env; |
|
|
1121 | asprintf (&env, "DESTID=%d", conf->id); |
|
|
1122 | putenv (env); |
|
|
1123 | asprintf (&env, "DESTNODE=%s", conf->nodename); |
|
|
1124 | putenv (env); |
|
|
1125 | asprintf (&env, "DESTIP=%s", inet_ntoa (sa.sin_addr)); |
|
|
1126 | putenv (env); |
|
|
1127 | asprintf (&env, "DESTPORT=%d", ntohs (sa.sin_port)); |
|
|
1128 | putenv (env); |
|
|
1129 | } |
|
|
1130 | |
|
|
1131 | const char *connection::script_node_up () |
|
|
1132 | { |
|
|
1133 | script_node (); |
|
|
1134 | |
|
|
1135 | putenv ("STATE=up"); |
|
|
1136 | |
|
|
1137 | return ::conf.script_node_up ? ::conf.script_node_up : "node-up"; |
|
|
1138 | } |
|
|
1139 | |
|
|
1140 | const char *connection::script_node_down () |
|
|
1141 | { |
|
|
1142 | script_node (); |
|
|
1143 | |
|
|
1144 | putenv ("STATE=down"); |
|
|
1145 | |
|
|
1146 | return ::conf.script_node_up ? ::conf.script_node_down : "node-down"; |
|
|
1147 | } |
|
|
1148 | |
|
|
1149 | connection::connection(struct vpn *vpn_) |
|
|
1150 | : vpn(vpn_) |
|
|
1151 | , rekey (this, &connection::rekey_cb) |
|
|
1152 | , keepalive (this, &connection::keepalive_cb) |
|
|
1153 | , establish_connection (this, &connection::establish_connection_cb) |
|
|
1154 | { |
|
|
1155 | octx = ictx = 0; |
|
|
1156 | retry_cnt = 0; |
|
|
1157 | |
|
|
1158 | connectmode = conf_node::C_ALWAYS; // initial setting |
|
|
1159 | reset_connection (); |
|
|
1160 | } |
|
|
1161 | |
|
|
1162 | connection::~connection () |
|
|
1163 | { |
|
|
1164 | shutdown (); |
|
|
1165 | } |
|
|
1166 | |
|
|
1167 | ///////////////////////////////////////////////////////////////////////////// |
|
|
1168 | |
|
|
1169 | const char *vpn::script_if_up () |
45 | const char *vpn::script_if_up (int) |
1170 | { |
46 | { |
1171 | // the tunnel device mtu should be the physical mtu - overhead |
47 | // the tunnel device mtu should be the physical mtu - overhead |
1172 | // the tricky part is rounding to the cipher key blocksize |
48 | // the tricky part is rounding to the cipher key blocksize |
1173 | int mtu = conf.mtu - ETH_OVERHEAD - VPE_OVERHEAD - UDP_OVERHEAD; |
49 | int mtu = conf.mtu - ETH_OVERHEAD - VPE_OVERHEAD - MAX_OVERHEAD; |
1174 | mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion |
50 | mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion |
1175 | mtu -= mtu % EVP_CIPHER_block_size (CIPHER); // round |
51 | mtu -= mtu % EVP_CIPHER_block_size (CIPHER); // round |
1176 | mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again |
52 | mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again |
1177 | |
53 | |
1178 | char *env; |
54 | char *env; |
… | |
… | |
1193 | |
69 | |
1194 | return ::conf.script_if_up ? ::conf.script_if_up : "if-up"; |
70 | return ::conf.script_if_up ? ::conf.script_if_up : "if-up"; |
1195 | } |
71 | } |
1196 | |
72 | |
1197 | int |
73 | int |
1198 | vpn::setup (void) |
74 | vpn::setup () |
1199 | { |
75 | { |
1200 | struct sockaddr_in sa; |
76 | sockinfo si; |
1201 | |
77 | |
|
|
78 | si.set (THISNODE); |
|
|
79 | |
|
|
80 | udpv4_fd = -1; |
|
|
81 | |
|
|
82 | if (THISNODE->protocols & PROT_UDPv4) |
|
|
83 | { |
1202 | socket_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
84 | udpv4_fd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
1203 | if (socket_fd < 0) |
85 | |
|
|
86 | if (udpv4_fd < 0) |
1204 | return -1; |
87 | return -1; |
1205 | |
88 | |
1206 | fill_sa (&sa, THISNODE); |
89 | if (bind (udpv4_fd, si.sav4 (), si.salenv4 ())) |
1207 | |
90 | { |
1208 | if (bind (socket_fd, (sockaddr *)&sa, sizeof (sa))) |
|
|
1209 | { |
|
|
1210 | slog (L_ERR, _("can't bind to %s: %s"), (const char *)sockinfo(sa), strerror (errno)); |
91 | slog (L_ERR, _("can't bind udpv4 to %s: %s"), (const char *)si, strerror (errno)); |
1211 | exit (1); |
92 | exit (1); |
1212 | } |
93 | } |
1213 | |
94 | |
1214 | #ifdef IP_MTU_DISCOVER |
95 | #ifdef IP_MTU_DISCOVER |
1215 | // this I really consider a linux bug. I am neither connected |
96 | // this I really consider a linux bug. I am neither connected |
1216 | // nor do I fragment myself. Linux still sets DF and doesn't |
97 | // nor do I fragment myself. Linux still sets DF and doesn't |
1217 | // fragment for me sometimes. |
98 | // fragment for me sometimes. |
1218 | { |
99 | { |
1219 | int oval = IP_PMTUDISC_DONT; |
100 | int oval = IP_PMTUDISC_DONT; |
1220 | setsockopt (socket_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
101 | setsockopt (udpv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
1221 | } |
102 | } |
1222 | #endif |
103 | #endif |
1223 | { |
104 | |
|
|
105 | // standard daemon practise... |
|
|
106 | { |
1224 | int oval = 1; |
107 | int oval = 1; |
1225 | setsockopt (socket_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
108 | setsockopt (udpv4_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
1226 | } |
109 | } |
1227 | |
110 | |
1228 | udp_ev_watcher.start (socket_fd, POLLIN); |
111 | udpv4_ev_watcher.start (udpv4_fd, POLLIN); |
|
|
112 | } |
|
|
113 | |
|
|
114 | ipv4_fd = -1; |
|
|
115 | if (THISNODE->protocols & PROT_IPv4) |
|
|
116 | { |
|
|
117 | ipv4_fd = socket (PF_INET, SOCK_RAW, ::conf.ip_proto); |
|
|
118 | |
|
|
119 | if (ipv4_fd < 0) |
|
|
120 | return -1; |
|
|
121 | |
|
|
122 | if (bind (ipv4_fd, si.sav4 (), si.salenv4 ())) |
|
|
123 | { |
|
|
124 | slog (L_ERR, _("can't bind ipv4 socket to %s: %s"), (const char *)si, strerror (errno)); |
|
|
125 | exit (1); |
|
|
126 | } |
|
|
127 | |
|
|
128 | #ifdef IP_MTU_DISCOVER |
|
|
129 | // this I really consider a linux bug. I am neither connected |
|
|
130 | // nor do I fragment myself. Linux still sets DF and doesn't |
|
|
131 | // fragment for me sometimes. |
|
|
132 | { |
|
|
133 | int oval = IP_PMTUDISC_DONT; |
|
|
134 | setsockopt (ipv4_fd, SOL_IP, IP_MTU_DISCOVER, &oval, sizeof oval); |
|
|
135 | } |
|
|
136 | #endif |
|
|
137 | |
|
|
138 | ipv4_ev_watcher.start (ipv4_fd, POLLIN); |
|
|
139 | } |
1229 | |
140 | |
1230 | tap = new tap_device (); |
141 | tap = new tap_device (); |
1231 | if (!tap) //D this, of course, never catches |
142 | if (!tap) //D this, of course, never catches |
1232 | { |
143 | { |
1233 | slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
144 | slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
1234 | exit (1); |
145 | exit (1); |
1235 | } |
146 | } |
1236 | |
147 | |
1237 | run_script (this, &vpn::script_if_up, true); |
148 | run_script (run_script_cb (this, &vpn::script_if_up), true); |
1238 | |
149 | |
1239 | vpn_ev_watcher.start (tap->fd, POLLIN); |
150 | tap_ev_watcher.start (tap->fd, POLLIN); |
1240 | |
151 | |
1241 | reconnect_all (); |
152 | reconnect_all (); |
1242 | |
153 | |
1243 | return 0; |
154 | return 0; |
1244 | } |
155 | } |
1245 | |
156 | |
1246 | void |
157 | void |
1247 | vpn::send_vpn_packet (vpn_packet *pkt, SOCKADDR *sa, int tos) |
158 | vpn::send_ipv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
1248 | { |
159 | { |
1249 | setsockopt (socket_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
160 | setsockopt (ipv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
1250 | sendto (socket_fd, &((*pkt)[0]), pkt->len, 0, (sockaddr *)sa, sizeof (*sa)); |
161 | sendto (ipv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
1251 | } |
162 | } |
1252 | |
163 | |
1253 | void |
164 | void |
1254 | vpn::shutdown_all () |
165 | vpn::send_udpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
1255 | { |
166 | { |
1256 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
167 | setsockopt (udpv4_fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
1257 | (*c)->shutdown (); |
168 | sendto (udpv4_fd, &((*pkt)[0]), pkt->len, 0, si.sav4 (), si.salenv4 ()); |
1258 | } |
169 | } |
1259 | |
170 | |
1260 | void |
171 | void |
1261 | vpn::reconnect_all () |
172 | vpn::recv_vpn_packet (vpn_packet *pkt, const sockinfo &rsi) |
1262 | { |
173 | { |
1263 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
174 | unsigned int src = pkt->src (); |
1264 | delete *c; |
175 | unsigned int dst = pkt->dst (); |
1265 | |
176 | |
1266 | conns.clear (); |
177 | slog (L_NOISE, _("<<?/%s received possible vpn packet type %d from %d to %d, length %d"), |
|
|
178 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst (), pkt->len); |
1267 | |
179 | |
1268 | for (configuration::node_vector::iterator i = conf.nodes.begin (); |
180 | if (src == 0 || src > conns.size () |
1269 | i != conf.nodes.end (); ++i) |
181 | || dst > conns.size () |
1270 | { |
182 | || pkt->typ () >= vpn_packet::PT_MAX) |
1271 | connection *conn = new connection (this); |
183 | slog (L_WARN, _("(%s): received corrupted packet type %d (src %d, dst %d)"), |
1272 | |
184 | (const char *)rsi, pkt->typ (), pkt->src (), pkt->dst ()); |
1273 | conn->conf = *i; |
185 | else |
1274 | conns.push_back (conn); |
|
|
1275 | |
|
|
1276 | conn->establish_connection (); |
|
|
1277 | } |
186 | { |
1278 | } |
187 | connection *c = conns[src - 1]; |
1279 | |
188 | |
1280 | connection *vpn::find_router () |
189 | if (dst == 0 && !THISNODE->routerprio) |
1281 | { |
190 | slog (L_WARN, _("%s(%s): received broadcast, but we are no router"), |
1282 | u32 prio = 0; |
191 | c->conf->nodename, (const char *)rsi); |
1283 | connection *router = 0; |
192 | else if (dst != 0 && dst != THISNODE->id) |
1284 | |
193 | // FORWARDING NEEDED ;) |
1285 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
194 | slog (L_WARN, |
|
|
195 | _("received frame for node %d ('%s') from %s, but this is node %d ('%s')"), |
|
|
196 | dst, conns[dst - 1]->conf->nodename, |
|
|
197 | (const char *)rsi, |
|
|
198 | THISNODE->id, THISNODE->nodename); |
|
|
199 | else |
|
|
200 | c->recv_vpn_packet (pkt, rsi); |
1286 | { |
201 | } |
1287 | connection *c = *i; |
|
|
1288 | |
|
|
1289 | if (c->conf->routerprio > prio |
|
|
1290 | && c->connectmode == conf_node::C_ALWAYS |
|
|
1291 | && c->conf != THISNODE |
|
|
1292 | && c->ictx && c->octx) |
|
|
1293 | { |
|
|
1294 | prio = c->conf->routerprio; |
|
|
1295 | router = c; |
|
|
1296 | } |
|
|
1297 | } |
|
|
1298 | |
|
|
1299 | return router; |
|
|
1300 | } |
202 | } |
1301 | |
203 | |
1302 | void vpn::connect_request (int id) |
|
|
1303 | { |
|
|
1304 | connection *c = find_router (); |
|
|
1305 | |
|
|
1306 | if (c) |
|
|
1307 | c->connect_request (id); |
|
|
1308 | //else // does not work, because all others must connect to the same router |
|
|
1309 | // // no router found, aggressively connect to all routers |
|
|
1310 | // for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
|
|
1311 | // if ((*i)->conf->routerprio) |
|
|
1312 | // (*i)->establish_connection (); |
|
|
1313 | } |
|
|
1314 | |
|
|
1315 | void |
204 | void |
1316 | vpn::udp_ev (short revents) |
205 | vpn::udpv4_ev (short revents) |
1317 | { |
206 | { |
1318 | if (revents & (POLLIN | POLLERR)) |
207 | if (revents & (POLLIN | POLLERR)) |
1319 | { |
208 | { |
1320 | vpn_packet *pkt = new vpn_packet; |
209 | vpn_packet *pkt = new vpn_packet; |
1321 | struct sockaddr_in sa; |
210 | struct sockaddr_in sa; |
1322 | socklen_t sa_len = sizeof (sa); |
211 | socklen_t sa_len = sizeof (sa); |
1323 | int len; |
212 | int len; |
1324 | |
213 | |
1325 | len = recvfrom (socket_fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
214 | len = recvfrom (udpv4_fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
|
|
215 | |
|
|
216 | sockinfo si(sa); |
1326 | |
217 | |
1327 | if (len > 0) |
218 | if (len > 0) |
1328 | { |
219 | { |
1329 | pkt->len = len; |
220 | pkt->len = len; |
1330 | |
221 | |
1331 | unsigned int src = pkt->src (); |
|
|
1332 | unsigned int dst = pkt->dst (); |
|
|
1333 | |
|
|
1334 | slog (L_NOISE, _("<<?/%s received possible vpn packet type %d from %d to %d, length %d"), |
|
|
1335 | (const char *)sockinfo (sa), pkt->typ (), pkt->src (), pkt->dst (), pkt->len); |
|
|
1336 | |
|
|
1337 | if (dst > conns.size () || pkt->typ () >= vpn_packet::PT_MAX) |
|
|
1338 | slog (L_WARN, _("<<? received CORRUPTED packet type %d from %d to %d"), |
|
|
1339 | pkt->typ (), pkt->src (), pkt->dst ()); |
|
|
1340 | else if (dst == 0 && !THISNODE->routerprio) |
|
|
1341 | slog (L_WARN, _("<<%d received broadcast, but we are no router"), dst); |
|
|
1342 | else if (dst != 0 && dst != THISNODE->id) |
|
|
1343 | slog (L_WARN, |
|
|
1344 | _("received frame for node %d ('%s') from %s, but this is node %d ('%s')"), |
|
|
1345 | dst, conns[dst - 1]->conf->nodename, |
|
|
1346 | (const char *)sockinfo (sa), |
|
|
1347 | THISNODE->id, THISNODE->nodename); |
|
|
1348 | else if (src == 0 || src > conns.size ()) |
|
|
1349 | slog (L_WARN, _("received frame from unknown node %d (%s)"), src, (const char *)sockinfo (sa)); |
|
|
1350 | else |
|
|
1351 | conns[src - 1]->recv_vpn_packet (pkt, &sa); |
222 | recv_vpn_packet (pkt, si); |
1352 | } |
223 | } |
1353 | else |
224 | else |
1354 | { |
225 | { |
1355 | // probably ECONNRESET or somesuch |
226 | // probably ECONNRESET or somesuch |
1356 | slog (L_DEBUG, _("%s: %s"), (const char *)sockinfo(sa), strerror (errno)); |
227 | slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
1357 | } |
228 | } |
1358 | |
229 | |
1359 | delete pkt; |
230 | delete pkt; |
1360 | } |
231 | } |
1361 | else if (revents & POLLHUP) |
232 | else if (revents & POLLHUP) |
1362 | { |
233 | { |
1363 | // this cannot ;) happen on udp sockets |
234 | // this cannot ;) happen on udp sockets |
1364 | slog (L_ERR, _("FATAL: POLLHUP on socket fd, terminating.")); |
235 | slog (L_ERR, _("FATAL: POLLHUP on udp v4 fd, terminating.")); |
1365 | exit (1); |
236 | exit (1); |
1366 | } |
237 | } |
1367 | else |
238 | else |
1368 | { |
239 | { |
1369 | slog (L_ERR, |
240 | slog (L_ERR, |
… | |
… | |
1372 | exit (1); |
243 | exit (1); |
1373 | } |
244 | } |
1374 | } |
245 | } |
1375 | |
246 | |
1376 | void |
247 | void |
|
|
248 | vpn::ipv4_ev (short revents) |
|
|
249 | { |
|
|
250 | if (revents & (POLLIN | POLLERR)) |
|
|
251 | { |
|
|
252 | vpn_packet *pkt = new vpn_packet; |
|
|
253 | struct sockaddr_in sa; |
|
|
254 | socklen_t sa_len = sizeof (sa); |
|
|
255 | int len; |
|
|
256 | |
|
|
257 | len = recvfrom (ipv4_fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
|
|
258 | |
|
|
259 | sockinfo si(sa, PROT_IPv4); |
|
|
260 | |
|
|
261 | if (len > 0) |
|
|
262 | { |
|
|
263 | pkt->len = len; |
|
|
264 | |
|
|
265 | // raw sockets deliver the ipv4, but don't expect it on sends |
|
|
266 | // this is slow, but... |
|
|
267 | pkt->skip_hdr (IP_OVERHEAD); |
|
|
268 | |
|
|
269 | recv_vpn_packet (pkt, si); |
|
|
270 | } |
|
|
271 | else |
|
|
272 | { |
|
|
273 | // probably ECONNRESET or somesuch |
|
|
274 | slog (L_DEBUG, _("%s: %s"), (const char *)si, strerror (errno)); |
|
|
275 | } |
|
|
276 | |
|
|
277 | delete pkt; |
|
|
278 | } |
|
|
279 | else if (revents & POLLHUP) |
|
|
280 | { |
|
|
281 | // this cannot ;) happen on udp sockets |
|
|
282 | slog (L_ERR, _("FATAL: POLLHUP on ipv4 fd, terminating.")); |
|
|
283 | exit (1); |
|
|
284 | } |
|
|
285 | else |
|
|
286 | { |
|
|
287 | slog (L_ERR, |
|
|
288 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
|
|
289 | revents); |
|
|
290 | exit (1); |
|
|
291 | } |
|
|
292 | } |
|
|
293 | |
|
|
294 | void |
1377 | vpn::vpn_ev (short revents) |
295 | vpn::tap_ev (short revents) |
1378 | { |
296 | { |
1379 | if (revents & POLLIN) |
297 | if (revents & POLLIN) |
1380 | { |
298 | { |
1381 | /* process data */ |
299 | /* process data */ |
1382 | tap_packet *pkt; |
300 | tap_packet *pkt; |
… | |
… | |
1438 | { |
356 | { |
1439 | if (events) |
357 | if (events) |
1440 | { |
358 | { |
1441 | if (events & EVENT_SHUTDOWN) |
359 | if (events & EVENT_SHUTDOWN) |
1442 | { |
360 | { |
|
|
361 | slog (L_INFO, _("preparing shutdown...")); |
|
|
362 | |
1443 | shutdown_all (); |
363 | shutdown_all (); |
1444 | |
364 | |
1445 | remove_pid (pidfilename); |
365 | remove_pid (pidfilename); |
1446 | |
366 | |
1447 | slog (L_INFO, _("vped terminating")); |
367 | slog (L_INFO, _("terminating")); |
1448 | |
368 | |
1449 | exit (0); |
369 | exit (0); |
1450 | } |
370 | } |
1451 | |
371 | |
1452 | if (events & EVENT_RECONNECT) |
372 | if (events & EVENT_RECONNECT) |
|
|
373 | { |
|
|
374 | slog (L_INFO, _("forced reconnect")); |
|
|
375 | |
1453 | reconnect_all (); |
376 | reconnect_all (); |
|
|
377 | } |
1454 | |
378 | |
1455 | events = 0; |
379 | events = 0; |
1456 | } |
380 | } |
1457 | |
381 | |
1458 | ts = TSTAMP_CANCEL; |
382 | ts = TSTAMP_CANCEL; |
1459 | } |
383 | } |
1460 | |
384 | |
|
|
385 | void |
|
|
386 | vpn::shutdown_all () |
|
|
387 | { |
|
|
388 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
|
|
389 | (*c)->shutdown (); |
|
|
390 | } |
|
|
391 | |
|
|
392 | void |
|
|
393 | vpn::reconnect_all () |
|
|
394 | { |
|
|
395 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
|
|
396 | delete *c; |
|
|
397 | |
|
|
398 | conns.clear (); |
|
|
399 | |
|
|
400 | connection_init (); |
|
|
401 | |
|
|
402 | for (configuration::node_vector::iterator i = conf.nodes.begin (); |
|
|
403 | i != conf.nodes.end (); ++i) |
|
|
404 | { |
|
|
405 | connection *conn = new connection (this); |
|
|
406 | |
|
|
407 | conn->conf = *i; |
|
|
408 | conns.push_back (conn); |
|
|
409 | |
|
|
410 | conn->establish_connection (); |
|
|
411 | } |
|
|
412 | } |
|
|
413 | |
|
|
414 | connection *vpn::find_router () |
|
|
415 | { |
|
|
416 | u32 prio = 0; |
|
|
417 | connection *router = 0; |
|
|
418 | |
|
|
419 | for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
|
|
420 | { |
|
|
421 | connection *c = *i; |
|
|
422 | |
|
|
423 | if (c->conf->routerprio > prio |
|
|
424 | && c->connectmode == conf_node::C_ALWAYS |
|
|
425 | && c->conf != THISNODE |
|
|
426 | && c->ictx && c->octx) |
|
|
427 | { |
|
|
428 | prio = c->conf->routerprio; |
|
|
429 | router = c; |
|
|
430 | } |
|
|
431 | } |
|
|
432 | |
|
|
433 | return router; |
|
|
434 | } |
|
|
435 | |
|
|
436 | void vpn::connect_request (int id) |
|
|
437 | { |
|
|
438 | connection *c = find_router (); |
|
|
439 | |
|
|
440 | if (c) |
|
|
441 | c->connect_request (id); |
|
|
442 | //else // does not work, because all others must connect to the same router |
|
|
443 | // // no router found, aggressively connect to all routers |
|
|
444 | // for (conns_vector::iterator i = conns.begin (); i != conns.end (); ++i) |
|
|
445 | // if ((*i)->conf->routerprio) |
|
|
446 | // (*i)->establish_connection (); |
|
|
447 | } |
|
|
448 | |
|
|
449 | void |
|
|
450 | connection::dump_status () |
|
|
451 | { |
|
|
452 | slog (L_NOTICE, _("node %s (id %d)"), conf->nodename, conf->id); |
|
|
453 | slog (L_NOTICE, _(" connectmode %d (%d) / sockaddr %s / minor %d"), |
|
|
454 | connectmode, conf->connectmode, (const char *)si, (int)prot_minor); |
|
|
455 | slog (L_NOTICE, _(" ictx/octx %08lx/%08lx / oseqno %d / retry_cnt %d"), |
|
|
456 | (long)ictx, (long)octx, (int)oseqno, (int)retry_cnt); |
|
|
457 | slog (L_NOTICE, _(" establish_conn %ld / rekey %ld / keepalive %ld"), |
|
|
458 | (long)(establish_connection.at), (long)(rekey.at), (long)(keepalive.at)); |
|
|
459 | } |
|
|
460 | |
|
|
461 | void |
|
|
462 | vpn::dump_status () |
|
|
463 | { |
|
|
464 | slog (L_NOTICE, _("BEGIN status dump (%ld)"), (long)NOW); |
|
|
465 | |
|
|
466 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
|
|
467 | (*c)->dump_status (); |
|
|
468 | |
|
|
469 | slog (L_NOTICE, _("END status dump")); |
|
|
470 | } |
|
|
471 | |
1461 | vpn::vpn (void) |
472 | vpn::vpn (void) |
1462 | : udp_ev_watcher (this, &vpn::udp_ev) |
473 | : udpv4_ev_watcher(this, &vpn::udpv4_ev) |
|
|
474 | , ipv4_ev_watcher(this, &vpn::ipv4_ev) |
1463 | , vpn_ev_watcher (this, &vpn::vpn_ev) |
475 | , tap_ev_watcher(this, &vpn::tap_ev) |
1464 | , event (this, &vpn::event_cb) |
476 | , event(this, &vpn::event_cb) |
1465 | { |
477 | { |
1466 | } |
478 | } |
1467 | |
479 | |
1468 | vpn::~vpn () |
480 | vpn::~vpn () |
1469 | { |
481 | { |