… | |
… | |
16 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
16 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 | */ |
17 | */ |
18 | |
18 | |
19 | #include "config.h" |
19 | #include "config.h" |
20 | |
20 | |
|
|
21 | #include <list> |
|
|
22 | |
21 | #include <cstdlib> |
23 | #include <cstdlib> |
22 | #include <cstring> |
24 | #include <cstring> |
23 | #include <cstdio> |
25 | #include <cstdio> |
24 | |
26 | |
25 | #include <sys/types.h> |
27 | #include <sys/types.h> |
… | |
… | |
60 | |
62 | |
61 | static const rsachallenge & |
63 | static const rsachallenge & |
62 | challenge_bytes () |
64 | challenge_bytes () |
63 | { |
65 | { |
64 | static rsachallenge challenge; |
66 | static rsachallenge challenge; |
65 | static time_t challenge_ttl; // time this challenge needs to be recreated |
67 | static double challenge_ttl; // time this challenge needs to be recreated |
66 | |
68 | |
67 | if (now > challenge_ttl) |
69 | if (NOW > challenge_ttl) |
68 | { |
70 | { |
69 | RAND_bytes ((unsigned char *)&challenge, sizeof (challenge)); |
71 | RAND_bytes ((unsigned char *)&challenge, sizeof (challenge)); |
70 | challenge_ttl = now + CHALLENGE_TTL; |
72 | challenge_ttl = NOW + CHALLENGE_TTL; |
71 | } |
73 | } |
72 | |
74 | |
73 | return challenge; |
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 | } |
74 | } |
167 | } |
75 | |
168 | |
76 | // run a script. yes, it's a template function. yes, c++ |
169 | // run a script. yes, it's a template function. yes, c++ |
77 | // is not a functional language. yes, this suxx. |
170 | // is not a functional language. yes, this suxx. |
78 | template<class owner> |
171 | template<class owner> |
… | |
… | |
112 | struct crypto_ctx |
205 | struct crypto_ctx |
113 | { |
206 | { |
114 | EVP_CIPHER_CTX cctx; |
207 | EVP_CIPHER_CTX cctx; |
115 | HMAC_CTX hctx; |
208 | HMAC_CTX hctx; |
116 | |
209 | |
117 | crypto_ctx (rsachallenge &challenge, int enc); |
210 | crypto_ctx (const rsachallenge &challenge, int enc); |
118 | ~crypto_ctx (); |
211 | ~crypto_ctx (); |
119 | }; |
212 | }; |
120 | |
213 | |
121 | crypto_ctx::crypto_ctx (rsachallenge &challenge, int enc) |
214 | crypto_ctx::crypto_ctx (const rsachallenge &challenge, int enc) |
122 | { |
215 | { |
123 | EVP_CIPHER_CTX_init (&cctx); |
216 | EVP_CIPHER_CTX_init (&cctx); |
124 | EVP_CipherInit_ex (&cctx, CIPHER, 0, &challenge[CHG_CIPHER_KEY], 0, enc); |
217 | EVP_CipherInit_ex (&cctx, CIPHER, 0, &challenge[CHG_CIPHER_KEY], 0, enc); |
125 | HMAC_CTX_init (&hctx); |
218 | HMAC_CTX_init (&hctx); |
126 | HMAC_Init_ex (&hctx, &challenge[CHG_HMAC_KEY], HMAC_KEYLEN, DIGEST, 0); |
219 | HMAC_Init_ex (&hctx, &challenge[CHG_HMAC_KEY], HMAC_KEYLEN, DIGEST, 0); |
… | |
… | |
128 | |
221 | |
129 | crypto_ctx::~crypto_ctx () |
222 | crypto_ctx::~crypto_ctx () |
130 | { |
223 | { |
131 | EVP_CIPHER_CTX_cleanup (&cctx); |
224 | EVP_CIPHER_CTX_cleanup (&cctx); |
132 | HMAC_CTX_cleanup (&hctx); |
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; |
133 | } |
313 | } |
134 | |
314 | |
135 | ///////////////////////////////////////////////////////////////////////////// |
315 | ///////////////////////////////////////////////////////////////////////////// |
136 | |
316 | |
137 | static void next_wakeup (time_t next) |
317 | static void next_wakeup (time_t next) |
… | |
… | |
200 | |
380 | |
201 | unsigned int src () |
381 | unsigned int src () |
202 | { |
382 | { |
203 | return src1 | ((srcdst >> 4) << 8); |
383 | return src1 | ((srcdst >> 4) << 8); |
204 | } |
384 | } |
|
|
385 | |
205 | unsigned int dst () |
386 | unsigned int dst () |
206 | { |
387 | { |
207 | return dst1 | ((srcdst & 0xf) << 8); |
388 | return dst1 | ((srcdst & 0xf) << 8); |
208 | } |
389 | } |
|
|
390 | |
209 | ptype typ () |
391 | ptype typ () |
210 | { |
392 | { |
211 | return (ptype) type; |
393 | return (ptype) type; |
212 | } |
394 | } |
213 | }; |
395 | }; |
… | |
… | |
252 | u32 cl; |
434 | u32 cl; |
253 | |
435 | |
254 | cl = lzf_compress (d, l, cdata + 2, (l - 2) & ~7); |
436 | cl = lzf_compress (d, l, cdata + 2, (l - 2) & ~7); |
255 | if (cl) |
437 | if (cl) |
256 | { |
438 | { |
257 | //printf ("compressed packet, %d => %d\n", l, cl);//D |
|
|
258 | type = PT_DATA_COMPRESSED; |
439 | type = PT_DATA_COMPRESSED; |
259 | d = cdata; |
440 | d = cdata; |
260 | l = cl + 2; |
441 | l = cl + 2; |
261 | |
442 | |
262 | d[0] = cl >> 8; |
443 | d[0] = cl >> 8; |
… | |
… | |
264 | } |
445 | } |
265 | #endif |
446 | #endif |
266 | |
447 | |
267 | EVP_EncryptInit_ex (cctx, 0, 0, 0, 0); |
448 | EVP_EncryptInit_ex (cctx, 0, 0, 0, 0); |
268 | |
449 | |
|
|
450 | struct { |
269 | #if RAND_SIZE |
451 | #if RAND_SIZE |
270 | struct { |
|
|
271 | u8 rnd[RAND_SIZE]; |
452 | u8 rnd[RAND_SIZE]; |
|
|
453 | #endif |
272 | u32 seqno; |
454 | u32 seqno; |
273 | } datahdr; |
455 | } datahdr; |
274 | |
456 | |
275 | datahdr.seqno = seqno; |
457 | datahdr.seqno = ntohl (seqno); |
|
|
458 | #if RAND_SIZE |
276 | RAND_pseudo_bytes ((unsigned char *) datahdr.rnd, RAND_SIZE); |
459 | RAND_pseudo_bytes ((unsigned char *) datahdr.rnd, RAND_SIZE); |
|
|
460 | #endif |
277 | |
461 | |
278 | EVP_EncryptUpdate (cctx, |
462 | EVP_EncryptUpdate (cctx, |
279 | (unsigned char *) data + outl, &outl2, |
463 | (unsigned char *) data + outl, &outl2, |
280 | (unsigned char *) &datahdr, DATAHDR); |
464 | (unsigned char *) &datahdr, DATAHDR); |
281 | outl += outl2; |
465 | outl += outl2; |
282 | #else |
|
|
283 | EVP_EncryptUpdate (cctx, |
|
|
284 | (unsigned char *) data + outl, &outl2, |
|
|
285 | (unsigned char *) &seqno, DATAHDR); |
|
|
286 | outl += outl2; |
|
|
287 | #endif |
|
|
288 | |
466 | |
289 | EVP_EncryptUpdate (cctx, |
467 | EVP_EncryptUpdate (cctx, |
290 | (unsigned char *) data + outl, &outl2, |
468 | (unsigned char *) data + outl, &outl2, |
291 | (unsigned char *) d, l); |
469 | (unsigned char *) d, l); |
292 | outl += outl2; |
470 | outl += outl2; |
… | |
… | |
328 | outl += outl2; |
506 | outl += outl2; |
329 | |
507 | |
330 | EVP_DecryptFinal_ex (cctx, (unsigned char *)d + outl, &outl2); |
508 | EVP_DecryptFinal_ex (cctx, (unsigned char *)d + outl, &outl2); |
331 | outl += outl2; |
509 | outl += outl2; |
332 | |
510 | |
333 | seqno = *(u32 *)(d + RAND_SIZE); |
511 | seqno = ntohl (*(u32 *)(d + RAND_SIZE)); |
334 | |
512 | |
335 | id2mac (dst () ? dst() : THISNODE->id, p->dst); |
513 | id2mac (dst () ? dst() : THISNODE->id, p->dst); |
336 | id2mac (src (), p->src); |
514 | id2mac (src (), p->src); |
337 | |
515 | |
338 | #if ENABLE_COMPRESSION |
516 | #if ENABLE_COMPRESSION |
339 | if (type == PT_DATA_COMPRESSED) |
517 | if (type == PT_DATA_COMPRESSED) |
340 | { |
518 | { |
341 | u32 cl = (d[DATAHDR] << 8) | d[DATAHDR + 1]; |
519 | u32 cl = (d[DATAHDR] << 8) | d[DATAHDR + 1]; |
342 | p->len = lzf_decompress (d + DATAHDR + 2, cl, &(*p)[6 + 6], MAX_MTU) + 6 + 6; |
520 | p->len = lzf_decompress (d + DATAHDR + 2, cl, &(*p)[6 + 6], MAX_MTU) + 6 + 6; |
343 | //printf ("decompressxed %d(%d) => %d\n", cl, len - data_hdr_size (), p->len);//D |
|
|
344 | } |
521 | } |
345 | else |
522 | else |
346 | p->len = outl + (6 + 6 - DATAHDR); |
523 | p->len = outl + (6 + 6 - DATAHDR); |
347 | #endif |
524 | #endif |
348 | |
525 | |
… | |
… | |
369 | u32 digest_nid; |
546 | u32 digest_nid; |
370 | |
547 | |
371 | const u8 curflags () const |
548 | const u8 curflags () const |
372 | { |
549 | { |
373 | return 0x80 |
550 | return 0x80 |
374 | | (ENABLE_COMPRESSION ? 0x01 : 0x00) |
551 | | (ENABLE_COMPRESSION ? 0x01 : 0x00); |
375 | | (ENABLE_TRUST ? 0x02 : 0x00); |
|
|
376 | } |
552 | } |
377 | |
553 | |
378 | void setup (ptype type, int dst) |
554 | void setup (ptype type, int dst) |
379 | { |
555 | { |
380 | prot_major = PROTOCOL_MAJOR; |
556 | prot_major = PROTOCOL_MAJOR; |
… | |
… | |
491 | void |
667 | void |
492 | connection::send_reset (SOCKADDR *dsa) |
668 | connection::send_reset (SOCKADDR *dsa) |
493 | { |
669 | { |
494 | static net_rate_limiter limiter(1); |
670 | static net_rate_limiter limiter(1); |
495 | |
671 | |
496 | if (limiter.can (dsa)) |
672 | if (limiter.can (dsa) && connectmode != conf_node::C_DISABLED) |
497 | { |
673 | { |
498 | config_packet *pkt = new config_packet; |
674 | config_packet *pkt = new config_packet; |
499 | |
675 | |
500 | pkt->setup (vpn_packet::PT_RESET, conf->id); |
676 | pkt->setup (vpn_packet::PT_RESET, conf->id); |
501 | vpn->send_vpn_packet (pkt, dsa, IPTOS_MINCOST); |
677 | vpn->send_vpn_packet (pkt, dsa, IPTOS_MINCOST); |
… | |
… | |
503 | delete pkt; |
679 | delete pkt; |
504 | } |
680 | } |
505 | } |
681 | } |
506 | |
682 | |
507 | static rsachallenge * |
683 | static rsachallenge * |
508 | gen_challenge (SOCKADDR *sa) |
684 | gen_challenge (u32 seqrand, SOCKADDR *sa) |
509 | { |
685 | { |
510 | static rsachallenge k; |
686 | static rsachallenge k; |
511 | |
687 | |
512 | memcpy (&k, &challenge_bytes (), sizeof (k)); |
688 | memcpy (&k, &challenge_bytes (), sizeof (k)); |
513 | RAND_bytes ((unsigned char *)&k[CHG_SEQNO], sizeof (u32)); |
689 | *(u32 *)&k[CHG_SEQNO] ^= seqrand; |
514 | xor_sa (k, sa); |
690 | xor_sa (k, sa); |
515 | |
691 | |
516 | return &k; |
692 | return &k; |
517 | } |
693 | } |
518 | |
694 | |
519 | void |
695 | void |
520 | connection::send_auth (auth_subtype subtype, SOCKADDR *sa, rsachallenge *k) |
696 | connection::send_auth (auth_subtype subtype, SOCKADDR *sa, const rsachallenge *k) |
521 | { |
697 | { |
522 | static net_rate_limiter limiter(2); |
698 | static net_rate_limiter limiter(0.2); |
523 | |
699 | |
524 | if (subtype != AUTH_INIT || limiter.can (sa)) |
700 | if (subtype != AUTH_INIT || limiter.can (sa)) |
525 | { |
701 | { |
|
|
702 | if (!k) |
|
|
703 | k = gen_challenge (seqrand, sa); |
|
|
704 | |
526 | auth_packet *pkt = new auth_packet (conf->id, subtype); |
705 | auth_packet *pkt = new auth_packet (conf->id, subtype); |
527 | |
706 | |
528 | //printf ("send auth_packet subtype %d\n", subtype);//D |
707 | memcpy (pkt->challenge, rsa_cache.public_encrypt (conf->rsa_key, *k), sizeof (rsaencrdata)); |
529 | |
|
|
530 | if (!k) |
|
|
531 | k = gen_challenge (sa); |
|
|
532 | |
|
|
533 | #if ENABLE_TRUST |
|
|
534 | if (0 > RSA_public_encrypt (sizeof (*k), |
|
|
535 | (unsigned char *)k, (unsigned char *)&pkt->challenge, |
|
|
536 | conf->rsa_key, RSA_PKCS1_OAEP_PADDING)) |
|
|
537 | fatal ("RSA_public_encrypt error"); |
|
|
538 | #else |
|
|
539 | # error untrusted mode not yet implemented: programemr does not know how to |
|
|
540 | rsaencrdata enc; |
|
|
541 | |
|
|
542 | if (0 > RSA_private_encrypt (sizeof (*k), |
|
|
543 | (unsigned char *)k, (unsigned char *)&enc, |
|
|
544 | ::conf.rsa_key, RSA_PKCS1_OAEP_PADDING)) |
|
|
545 | fatal ("RSA_private_encrypt error"); |
|
|
546 | |
|
|
547 | if (0 > RSA_public_encrypt (sizeof (enc), |
|
|
548 | (unsigned char *)enc, (unsigned char *)&pkt->challenge, |
|
|
549 | conf->rsa_key, RSA_NO_PADDING)) |
|
|
550 | fatal ("RSA_public_encrypt error"); |
|
|
551 | #endif |
|
|
552 | |
708 | |
553 | slog (L_TRACE, ">>%d PT_AUTH(%d) [%s]", conf->id, subtype, (const char *)sockinfo (sa)); |
709 | slog (L_TRACE, ">>%d PT_AUTH(%d) [%s]", conf->id, subtype, (const char *)sockinfo (sa)); |
554 | |
710 | |
555 | vpn->send_vpn_packet (pkt, sa, IPTOS_RELIABILITY); |
711 | vpn->send_vpn_packet (pkt, sa, IPTOS_RELIABILITY); |
556 | |
712 | |
557 | delete pkt; |
713 | delete pkt; |
558 | } |
714 | } |
559 | } |
715 | } |
560 | |
716 | |
561 | void |
717 | void |
562 | connection::establish_connection () |
718 | connection::establish_connection_cb (tstamp &ts) |
563 | { |
719 | { |
564 | if (!ictx && conf != THISNODE && connectmode != conf_node::C_NEVER) |
720 | if (ictx || conf == THISNODE || connectmode == conf_node::C_NEVER) |
|
|
721 | ts = TSTAMP_CANCEL; |
|
|
722 | else if (ts <= NOW) |
565 | { |
723 | { |
566 | if (now >= next_retry) |
724 | double retry_int = double (retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2)) * 0.25; |
567 | { |
|
|
568 | int retry_int = retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2); |
|
|
569 | |
725 | |
570 | if (retry_cnt < (17 << 2) | 3) |
726 | if (retry_int < 3600 * 8) |
571 | retry_cnt++; |
727 | retry_cnt++; |
572 | |
728 | |
573 | if (connectmode == conf_node::C_ONDEMAND |
729 | ts = NOW + retry_int; |
574 | && retry_int > ::conf.keepalive) |
|
|
575 | retry_int = ::conf.keepalive; |
|
|
576 | |
730 | |
577 | next_retry = now + retry_int; |
|
|
578 | next_wakeup (next_retry); |
|
|
579 | |
|
|
580 | if (conf->hostname) |
731 | if (conf->hostname) |
581 | { |
732 | { |
582 | reset_dstaddr (); |
733 | reset_dstaddr (); |
583 | if (sa.sin_addr.s_addr) |
734 | if (sa.sin_addr.s_addr) |
584 | if (retry_cnt < 4) |
735 | if (retry_cnt < 4) |
585 | send_auth (AUTH_INIT, &sa); |
736 | send_auth (AUTH_INIT, &sa); |
586 | else |
737 | else |
587 | send_ping (&sa, 0); |
738 | send_ping (&sa, 0); |
588 | } |
739 | } |
589 | else |
740 | else |
590 | vpn->connect_request (conf->id); |
741 | vpn->connect_request (conf->id); |
591 | } |
|
|
592 | } |
742 | } |
593 | } |
743 | } |
594 | |
744 | |
595 | void |
745 | void |
596 | connection::reset_connection () |
746 | connection::reset_connection () |
… | |
… | |
601 | |
751 | |
602 | if (::conf.script_node_down) |
752 | if (::conf.script_node_down) |
603 | run_script (this, &connection::script_node_down, false); |
753 | run_script (this, &connection::script_node_down, false); |
604 | } |
754 | } |
605 | |
755 | |
606 | delete ictx; |
756 | delete ictx; ictx = 0; |
607 | ictx = 0; |
757 | delete octx; octx = 0; |
608 | |
758 | |
609 | delete octx; |
759 | RAND_bytes ((unsigned char *)&seqrand, sizeof (u32)); |
610 | octx = 0; |
|
|
611 | |
760 | |
612 | sa.sin_port = 0; |
761 | sa.sin_port = 0; |
613 | sa.sin_addr.s_addr = 0; |
762 | sa.sin_addr.s_addr = 0; |
614 | |
763 | |
615 | next_retry = 0; |
|
|
616 | next_rekey = 0; |
|
|
617 | last_activity = 0; |
764 | last_activity = 0; |
|
|
765 | |
|
|
766 | rekey.reset (); |
|
|
767 | keepalive.reset (); |
|
|
768 | establish_connection.reset (); |
618 | } |
769 | } |
619 | |
770 | |
620 | void |
771 | void |
621 | connection::shutdown () |
772 | connection::shutdown () |
622 | { |
773 | { |
… | |
… | |
625 | |
776 | |
626 | reset_connection (); |
777 | reset_connection (); |
627 | } |
778 | } |
628 | |
779 | |
629 | void |
780 | void |
630 | connection::rekey () |
781 | connection::rekey_cb (tstamp &ts) |
631 | { |
782 | { |
|
|
783 | ts = TSTAMP_CANCEL; |
|
|
784 | |
632 | reset_connection (); |
785 | reset_connection (); |
633 | establish_connection (); |
786 | establish_connection (); |
634 | } |
787 | } |
635 | |
788 | |
636 | void |
789 | void |
… | |
… | |
641 | |
794 | |
642 | if (conf->inherit_tos |
795 | if (conf->inherit_tos |
643 | && (*pkt)[12] == 0x08 && (*pkt)[13] == 0x00 // IP |
796 | && (*pkt)[12] == 0x08 && (*pkt)[13] == 0x00 // IP |
644 | && ((*pkt)[14] & 0xf0) == 0x40) // IPv4 |
797 | && ((*pkt)[14] & 0xf0) == 0x40) // IPv4 |
645 | tos = (*pkt)[15] & IPTOS_TOS_MASK; |
798 | tos = (*pkt)[15] & IPTOS_TOS_MASK; |
646 | printf ("%d %02x %02x %02x %02x = %02x\n", (int)conf->inherit_tos, (*pkt)[12],(*pkt)[13],(*pkt)[14],(*pkt)[15], tos); |
|
|
647 | |
799 | |
648 | p->setup (this, broadcast ? 0 : conf->id, &((*pkt)[6 + 6]), pkt->len - 6 - 6, ++oseqno); // skip 2 macs |
800 | p->setup (this, broadcast ? 0 : conf->id, &((*pkt)[6 + 6]), pkt->len - 6 - 6, ++oseqno); // skip 2 macs |
649 | vpn->send_vpn_packet (p, &sa, tos); |
801 | vpn->send_vpn_packet (p, &sa, tos); |
650 | |
802 | |
651 | delete p; |
803 | delete p; |
… | |
… | |
669 | } |
821 | } |
670 | |
822 | |
671 | void |
823 | void |
672 | connection::recv_vpn_packet (vpn_packet *pkt, SOCKADDR *ssa) |
824 | connection::recv_vpn_packet (vpn_packet *pkt, SOCKADDR *ssa) |
673 | { |
825 | { |
674 | last_activity = now; |
826 | last_activity = NOW; |
675 | |
827 | |
676 | slog (L_NOISE, "<<%d received packet type %d from %d to %d", |
828 | slog (L_NOISE, "<<%d received packet type %d from %d to %d", |
677 | conf->id, pkt->typ (), pkt->src (), pkt->dst ()); |
829 | conf->id, pkt->typ (), pkt->src (), pkt->dst ()); |
678 | |
830 | |
679 | switch (pkt->typ ()) |
831 | switch (pkt->typ ()) |
… | |
… | |
687 | // so reset the retry counter and establish a conenction |
839 | // so reset the retry counter and establish a conenction |
688 | // when we receive a pong. |
840 | // when we receive a pong. |
689 | if (!ictx && !octx) |
841 | if (!ictx && !octx) |
690 | { |
842 | { |
691 | retry_cnt = 0; |
843 | retry_cnt = 0; |
692 | next_retry = 0; |
844 | establish_connection.at = 0; |
693 | establish_connection (); |
845 | establish_connection (); |
694 | } |
846 | } |
695 | |
847 | |
696 | break; |
848 | break; |
697 | |
849 | |
698 | case vpn_packet::PT_RESET: |
850 | case vpn_packet::PT_RESET: |
699 | { |
851 | { |
700 | reset_connection (); |
852 | reset_connection (); |
701 | |
853 | |
702 | config_packet *p = (config_packet *) pkt; |
854 | config_packet *p = (config_packet *) pkt; |
703 | if (p->chk_config ()) |
855 | if (!p->chk_config ()) |
|
|
856 | { |
|
|
857 | slog (L_WARN, _("protocol mismatch, disabling node '%s'"), conf->nodename); |
|
|
858 | connectmode = conf_node::C_DISABLED; |
|
|
859 | } |
704 | if (connectmode == conf_node::C_ALWAYS) |
860 | else if (connectmode == conf_node::C_ALWAYS) |
705 | establish_connection (); |
861 | establish_connection (); |
706 | |
|
|
707 | //D slog the protocol mismatch? |
|
|
708 | } |
862 | } |
709 | break; |
863 | break; |
710 | |
864 | |
711 | case vpn_packet::PT_AUTH: |
865 | case vpn_packet::PT_AUTH: |
712 | { |
866 | { |
… | |
… | |
722 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
876 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
723 | |
877 | |
724 | if (p->subtype == AUTH_INIT) |
878 | if (p->subtype == AUTH_INIT) |
725 | send_auth (AUTH_INITREPLY, ssa); |
879 | send_auth (AUTH_INITREPLY, ssa); |
726 | |
880 | |
727 | rsachallenge k; |
881 | const rsachallenge *k = rsa_cache.private_decrypt (::conf.rsa_key, p->challenge); |
728 | |
882 | |
729 | #if ENABLE_TRUST |
|
|
730 | if (0 > RSA_private_decrypt (sizeof (rsaencrdata), |
|
|
731 | (unsigned char *)&p->challenge, (unsigned char *)&k, |
|
|
732 | ::conf.rsa_key, RSA_PKCS1_OAEP_PADDING)) |
|
|
733 | // continued below |
|
|
734 | #else |
|
|
735 | rsaencrdata j; |
|
|
736 | |
883 | if (!k) |
737 | if (0 > RSA_private_decrypt (sizeof (rsaencrdata), |
|
|
738 | (unsigned char *)&p->challenge, (unsigned char *)&j, |
|
|
739 | ::conf.rsa_key, RSA_NO_PADDING)) |
|
|
740 | fatal ("RSA_private_decrypt error"); |
|
|
741 | |
|
|
742 | if (0 > RSA_public_decrypt (sizeof (k), |
|
|
743 | (unsigned char *)&j, (unsigned char *)&k, |
|
|
744 | conf->rsa_key, RSA_PKCS1_OAEP_PADDING)) |
|
|
745 | // continued below |
|
|
746 | #endif |
|
|
747 | { |
884 | { |
748 | slog (L_ERR, _("challenge from %s (%s) illegal or corrupted"), |
885 | slog (L_ERR, _("challenge from %s (%s) illegal or corrupted"), |
749 | conf->nodename, (const char *)sockinfo (ssa)); |
886 | conf->nodename, (const char *)sockinfo (ssa)); |
750 | break; |
887 | break; |
751 | } |
888 | } |
752 | |
889 | |
753 | retry_cnt = 0; |
890 | retry_cnt = 0; |
754 | next_retry = now + 8; |
891 | establish_connection.set (NOW + 8); //? ;) |
|
|
892 | keepalive.reset (); |
|
|
893 | rekey.reset (); |
755 | |
894 | |
756 | switch (p->subtype) |
895 | switch (p->subtype) |
757 | { |
896 | { |
758 | case AUTH_INIT: |
897 | case AUTH_INIT: |
759 | case AUTH_INITREPLY: |
898 | case AUTH_INITREPLY: |
760 | delete ictx; |
899 | delete ictx; |
761 | ictx = 0; |
900 | ictx = 0; |
762 | |
901 | |
763 | delete octx; |
902 | delete octx; |
764 | |
903 | |
765 | octx = new crypto_ctx (k, 1); |
904 | octx = new crypto_ctx (*k, 1); |
766 | oseqno = *(u32 *)&k[CHG_SEQNO] & 0x7fffffff; |
905 | oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff; |
767 | |
906 | |
768 | send_auth (AUTH_REPLY, ssa, &k); |
907 | send_auth (AUTH_REPLY, ssa, k); |
769 | break; |
908 | break; |
770 | |
909 | |
771 | case AUTH_REPLY: |
910 | case AUTH_REPLY: |
772 | |
911 | |
773 | if (!memcmp ((u8 *)gen_challenge (ssa) + sizeof (u32), (u8 *)&k + sizeof (u32), |
912 | if (!memcmp ((u8 *)gen_challenge (seqrand, ssa), (u8 *)k, sizeof (rsachallenge))) |
774 | sizeof (rsachallenge) - sizeof (u32))) |
|
|
775 | { |
913 | { |
776 | delete ictx; |
914 | delete ictx; |
777 | |
915 | |
778 | ictx = new crypto_ctx (k, 0); |
916 | ictx = new crypto_ctx (*k, 0); |
779 | iseqno = *(u32 *)&k[CHG_SEQNO] & 0x7fffffff; // at least 2**31 sequence numbers are valid |
917 | iseqno.reset (ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff); // at least 2**31 sequence numbers are valid |
780 | ismask = 0xffffffff; // initially, all lower sequence numbers are invalid |
|
|
781 | |
918 | |
782 | sa = *ssa; |
919 | sa = *ssa; |
783 | |
920 | |
784 | next_rekey = now + ::conf.rekey; |
921 | rekey.set (NOW + ::conf.rekey); |
785 | next_wakeup (next_rekey); |
922 | keepalive.set (NOW + ::conf.keepalive); |
786 | |
923 | |
787 | // send queued packets |
924 | // send queued packets |
788 | while (tap_packet *p = queue.get ()) |
925 | while (tap_packet *p = queue.get ()) |
789 | { |
926 | { |
790 | send_data_packet (p); |
927 | send_data_packet (p); |
… | |
… | |
835 | else |
972 | else |
836 | { |
973 | { |
837 | u32 seqno; |
974 | u32 seqno; |
838 | tap_packet *d = p->unpack (this, seqno); |
975 | tap_packet *d = p->unpack (this, seqno); |
839 | |
976 | |
840 | if (seqno <= iseqno - 32) |
977 | if (iseqno.recv_ok (seqno)) |
841 | slog (L_ERR, _("received duplicate or outdated packet (received %08lx, expected %08lx)\n" |
|
|
842 | "possible replay attack, or just massive packet reordering"), seqno, iseqno + 1);//D |
|
|
843 | else if (seqno > iseqno + 32) |
|
|
844 | slog (L_ERR, _("received duplicate or out-of-sync packet (received %08lx, expected %08lx)\n" |
|
|
845 | "possible replay attack, or just massive packet loss"), seqno, iseqno + 1);//D |
|
|
846 | else |
|
|
847 | { |
978 | { |
848 | if (seqno > iseqno) |
|
|
849 | { |
|
|
850 | ismask <<= seqno - iseqno; |
|
|
851 | iseqno = seqno; |
|
|
852 | } |
|
|
853 | |
|
|
854 | u32 mask = 1 << (iseqno - seqno); |
|
|
855 | |
|
|
856 | //printf ("received seqno %08lx, iseqno %08lx, mask %08lx is %08lx\n", seqno, iseqno, mask, ismask); |
|
|
857 | if (ismask & mask) |
|
|
858 | slog (L_ERR, _("received duplicate packet (received %08lx, expected %08lx)\n" |
|
|
859 | "possible replay attack, or just packet duplication"), seqno, iseqno + 1);//D |
|
|
860 | else |
|
|
861 | { |
|
|
862 | ismask |= mask; |
|
|
863 | |
|
|
864 | vpn->tap->send (d); |
979 | vpn->tap->send (d); |
865 | |
980 | |
866 | if (p->dst () == 0) // re-broadcast |
981 | if (p->dst () == 0) // re-broadcast |
867 | for (vpn::conns_vector::iterator i = vpn->conns.begin (); i != vpn->conns.end (); ++i) |
982 | for (vpn::conns_vector::iterator i = vpn->conns.begin (); i != vpn->conns.end (); ++i) |
868 | { |
983 | { |
869 | connection *c = *i; |
984 | connection *c = *i; |
870 | |
985 | |
871 | if (c->conf != THISNODE && c->conf != conf) |
986 | if (c->conf != THISNODE && c->conf != conf) |
872 | c->inject_data_packet (d); |
987 | c->inject_data_packet (d); |
873 | } |
|
|
874 | |
|
|
875 | delete d; |
|
|
876 | |
|
|
877 | break; |
|
|
878 | } |
988 | } |
|
|
989 | |
|
|
990 | delete d; |
|
|
991 | |
|
|
992 | break; |
879 | } |
993 | } |
880 | } |
994 | } |
881 | } |
995 | } |
882 | else |
996 | else |
883 | slog (L_ERR, _("received data packet from unknown source %s"), (const char *)sockinfo (ssa));//D |
997 | slog (L_ERR, _("received data packet from unknown source %s"), (const char *)sockinfo (ssa));//D |
… | |
… | |
954 | send_reset (ssa); |
1068 | send_reset (ssa); |
955 | break; |
1069 | break; |
956 | } |
1070 | } |
957 | } |
1071 | } |
958 | |
1072 | |
959 | void connection::timer () |
1073 | void connection::keepalive_cb (tstamp &ts) |
960 | { |
1074 | { |
961 | if (conf != THISNODE) |
1075 | if (NOW >= last_activity + ::conf.keepalive + 30) |
962 | { |
1076 | { |
963 | if (now >= next_retry && connectmode == conf_node::C_ALWAYS) |
1077 | reset_connection (); |
964 | establish_connection (); |
1078 | establish_connection (); |
965 | |
1079 | } |
966 | if (ictx && octx) |
|
|
967 | { |
|
|
968 | if (now >= next_rekey) |
|
|
969 | rekey (); |
|
|
970 | else if (now >= last_activity + ::conf.keepalive + 30) |
|
|
971 | { |
|
|
972 | reset_connection (); |
|
|
973 | establish_connection (); |
|
|
974 | } |
|
|
975 | else if (now >= last_activity + ::conf.keepalive) |
1080 | else if (NOW < last_activity + ::conf.keepalive) |
|
|
1081 | ts = last_activity + ::conf.keepalive; |
976 | if (conf->connectmode != conf_node::C_ONDEMAND |
1082 | else if (conf->connectmode != conf_node::C_ONDEMAND |
977 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1083 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
|
|
1084 | { |
978 | send_ping (&sa); |
1085 | send_ping (&sa); |
979 | else |
1086 | ts = NOW + 5; |
|
|
1087 | } |
|
|
1088 | else |
980 | reset_connection (); |
1089 | reset_connection (); |
981 | |
1090 | |
982 | } |
|
|
983 | } |
|
|
984 | } |
1091 | } |
985 | |
1092 | |
986 | void connection::connect_request (int id) |
1093 | void connection::connect_request (int id) |
987 | { |
1094 | { |
988 | connect_req_packet *p = new connect_req_packet (conf->id, id); |
1095 | connect_req_packet *p = new connect_req_packet (conf->id, id); |
… | |
… | |
1025 | putenv ("STATE=down"); |
1132 | putenv ("STATE=down"); |
1026 | |
1133 | |
1027 | return ::conf.script_node_up ? ::conf.script_node_down : "node-down"; |
1134 | return ::conf.script_node_up ? ::conf.script_node_down : "node-down"; |
1028 | } |
1135 | } |
1029 | |
1136 | |
|
|
1137 | connection::connection(struct vpn *vpn_) |
|
|
1138 | : vpn(vpn_) |
|
|
1139 | , rekey (this, &connection::rekey_cb) |
|
|
1140 | , keepalive (this, &connection::keepalive_cb) |
|
|
1141 | , establish_connection (this, &connection::establish_connection_cb) |
|
|
1142 | { |
|
|
1143 | octx = ictx = 0; |
|
|
1144 | retry_cnt = 0; |
|
|
1145 | |
|
|
1146 | connectmode = conf_node::C_ALWAYS; // initial setting |
|
|
1147 | reset_connection (); |
|
|
1148 | } |
|
|
1149 | |
|
|
1150 | connection::~connection () |
|
|
1151 | { |
|
|
1152 | shutdown (); |
|
|
1153 | } |
|
|
1154 | |
1030 | ///////////////////////////////////////////////////////////////////////////// |
1155 | ///////////////////////////////////////////////////////////////////////////// |
1031 | |
|
|
1032 | vpn::vpn (void) |
|
|
1033 | {} |
|
|
1034 | |
1156 | |
1035 | const char *vpn::script_if_up () |
1157 | const char *vpn::script_if_up () |
1036 | { |
1158 | { |
1037 | // the tunnel device mtu should be the physical mtu - overhead |
1159 | // the tunnel device mtu should be the physical mtu - overhead |
1038 | // the tricky part is rounding to the cipher key blocksize |
1160 | // the tricky part is rounding to the cipher key blocksize |
… | |
… | |
1089 | { |
1211 | { |
1090 | int oval = 1; |
1212 | int oval = 1; |
1091 | setsockopt (socket_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
1213 | setsockopt (socket_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
1092 | } |
1214 | } |
1093 | |
1215 | |
|
|
1216 | udp_ev_watcher.start (socket_fd, POLLIN); |
|
|
1217 | |
1094 | tap = new tap_device (); |
1218 | tap = new tap_device (); |
1095 | if (!tap) //D this, of course, never catches |
1219 | if (!tap) //D this, of course, never catches |
1096 | { |
1220 | { |
1097 | slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
1221 | slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
1098 | exit (1); |
1222 | exit (1); |
1099 | } |
1223 | } |
1100 | |
1224 | |
1101 | run_script (this, &vpn::script_if_up, true); |
1225 | run_script (this, &vpn::script_if_up, true); |
|
|
1226 | |
|
|
1227 | vpn_ev_watcher.start (tap->fd, POLLIN); |
|
|
1228 | |
|
|
1229 | reconnect_all (); |
1102 | |
1230 | |
1103 | return 0; |
1231 | return 0; |
1104 | } |
1232 | } |
1105 | |
1233 | |
1106 | void |
1234 | void |
… | |
… | |
1171 | // if ((*i)->conf->routerprio) |
1299 | // if ((*i)->conf->routerprio) |
1172 | // (*i)->establish_connection (); |
1300 | // (*i)->establish_connection (); |
1173 | } |
1301 | } |
1174 | |
1302 | |
1175 | void |
1303 | void |
1176 | vpn::main_loop () |
1304 | vpn::udp_ev (short revents) |
1177 | { |
1305 | { |
1178 | struct pollfd pollfd[2]; |
1306 | if (revents & (POLLIN | POLLERR)) |
1179 | |
|
|
1180 | pollfd[0].fd = tap->fd; |
|
|
1181 | pollfd[0].events = POLLIN; |
|
|
1182 | pollfd[1].fd = socket_fd; |
|
|
1183 | pollfd[1].events = POLLIN; |
|
|
1184 | |
|
|
1185 | events = 0; |
|
|
1186 | now = time (0); |
|
|
1187 | next_timecheck = now + 1; |
|
|
1188 | |
|
|
1189 | reconnect_all (); |
|
|
1190 | |
|
|
1191 | for (;;) |
|
|
1192 | { |
1307 | { |
1193 | int npoll = poll (pollfd, 2, (next_timecheck - now) * 1000); |
1308 | vpn_packet *pkt = new vpn_packet; |
1194 | |
1309 | struct sockaddr_in sa; |
1195 | now = time (0); |
1310 | socklen_t sa_len = sizeof (sa); |
|
|
1311 | int len; |
1196 | |
1312 | |
|
|
1313 | len = recvfrom (socket_fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
|
|
1314 | |
1197 | if (npoll > 0) |
1315 | if (len > 0) |
|
|
1316 | { |
|
|
1317 | pkt->len = len; |
|
|
1318 | |
|
|
1319 | unsigned int src = pkt->src (); |
|
|
1320 | unsigned int dst = pkt->dst (); |
|
|
1321 | |
|
|
1322 | slog (L_NOISE, _("<<?/%s received possible vpn packet type %d from %d to %d, length %d"), |
|
|
1323 | (const char *)sockinfo (sa), pkt->typ (), pkt->src (), pkt->dst (), pkt->len); |
|
|
1324 | |
|
|
1325 | if (dst > conns.size () || pkt->typ () >= vpn_packet::PT_MAX) |
|
|
1326 | slog (L_WARN, _("<<? received CORRUPTED packet type %d from %d to %d"), |
|
|
1327 | pkt->typ (), pkt->src (), pkt->dst ()); |
|
|
1328 | else if (dst == 0 && !THISNODE->routerprio) |
|
|
1329 | slog (L_WARN, _("<<%d received broadcast, but we are no router"), dst); |
|
|
1330 | else if (dst != 0 && dst != THISNODE->id) |
|
|
1331 | slog (L_WARN, |
|
|
1332 | _("received frame for node %d ('%s') from %s, but this is node %d ('%s')"), |
|
|
1333 | dst, conns[dst - 1]->conf->nodename, |
|
|
1334 | (const char *)sockinfo (sa), |
|
|
1335 | THISNODE->id, THISNODE->nodename); |
|
|
1336 | else if (src == 0 || src > conns.size ()) |
|
|
1337 | slog (L_WARN, _("received frame from unknown node %d (%s)"), src, (const char *)sockinfo (sa)); |
|
|
1338 | else |
|
|
1339 | conns[src - 1]->recv_vpn_packet (pkt, &sa); |
1198 | { |
1340 | } |
1199 | if (pollfd[1].revents) |
1341 | else |
|
|
1342 | { |
|
|
1343 | // probably ECONNRESET or somesuch |
|
|
1344 | slog (L_DEBUG, _("%s: %s"), (const char *)sockinfo(sa), strerror (errno)); |
|
|
1345 | } |
|
|
1346 | |
|
|
1347 | delete pkt; |
|
|
1348 | } |
|
|
1349 | else if (revents & POLLHUP) |
|
|
1350 | { |
|
|
1351 | // this cannot ;) happen on udp sockets |
|
|
1352 | slog (L_ERR, _("FATAL: POLLHUP on socket fd, terminating.")); |
|
|
1353 | exit (1); |
|
|
1354 | } |
|
|
1355 | else |
|
|
1356 | { |
|
|
1357 | slog (L_ERR, |
|
|
1358 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
|
|
1359 | revents); |
|
|
1360 | exit (1); |
|
|
1361 | } |
|
|
1362 | } |
|
|
1363 | |
|
|
1364 | void |
|
|
1365 | vpn::vpn_ev (short revents) |
|
|
1366 | { |
|
|
1367 | if (revents & POLLIN) |
|
|
1368 | { |
|
|
1369 | /* process data */ |
|
|
1370 | tap_packet *pkt; |
|
|
1371 | |
|
|
1372 | pkt = tap->recv (); |
|
|
1373 | |
|
|
1374 | int dst = mac2id (pkt->dst); |
|
|
1375 | int src = mac2id (pkt->src); |
|
|
1376 | |
|
|
1377 | if (src != THISNODE->id) |
|
|
1378 | { |
|
|
1379 | slog (L_ERR, _("FATAL: tap packet not originating on current node received, terminating.")); |
|
|
1380 | exit (1); |
|
|
1381 | } |
|
|
1382 | |
|
|
1383 | if (dst == THISNODE->id) |
|
|
1384 | { |
|
|
1385 | slog (L_ERR, _("FATAL: tap packet destined for current node received, terminating.")); |
|
|
1386 | exit (1); |
|
|
1387 | } |
|
|
1388 | |
|
|
1389 | if (dst > conns.size ()) |
|
|
1390 | slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); |
|
|
1391 | else |
|
|
1392 | { |
|
|
1393 | if (dst) |
1200 | { |
1394 | { |
1201 | if (pollfd[1].revents & (POLLIN | POLLERR)) |
1395 | // unicast |
1202 | { |
|
|
1203 | vpn_packet *pkt = new vpn_packet; |
|
|
1204 | struct sockaddr_in sa; |
|
|
1205 | socklen_t sa_len = sizeof (sa); |
|
|
1206 | int len; |
|
|
1207 | |
|
|
1208 | len = recvfrom (socket_fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
|
|
1209 | |
|
|
1210 | if (len > 0) |
|
|
1211 | { |
|
|
1212 | pkt->len = len; |
|
|
1213 | |
|
|
1214 | unsigned int src = pkt->src (); |
|
|
1215 | unsigned int dst = pkt->dst (); |
|
|
1216 | |
|
|
1217 | slog (L_NOISE, _("<<?/%s received possible vpn packet type %d from %d to %d, length %d"), |
|
|
1218 | (const char *)sockinfo (sa), pkt->typ (), pkt->src (), pkt->dst (), pkt->len); |
|
|
1219 | |
|
|
1220 | if (dst > conns.size () || pkt->typ () >= vpn_packet::PT_MAX) |
|
|
1221 | slog (L_WARN, _("<<? received CORRUPTED packet type %d from %d to %d"), |
|
|
1222 | pkt->typ (), pkt->src (), pkt->dst ()); |
|
|
1223 | else if (dst == 0 && !THISNODE->routerprio) |
|
|
1224 | slog (L_WARN, _("<<%d received broadcast, but we are no router"), dst); |
|
|
1225 | else if (dst != 0 && dst != THISNODE->id) |
1396 | if (dst != THISNODE->id) |
1226 | slog (L_WARN, |
|
|
1227 | _("received frame for node %d ('%s') from %s, but this is node %d ('%s')"), |
|
|
1228 | dst, conns[dst - 1]->conf->nodename, |
|
|
1229 | (const char *)sockinfo (sa), |
|
|
1230 | THISNODE->id, THISNODE->nodename); |
|
|
1231 | else if (src == 0 || src > conns.size ()) |
|
|
1232 | slog (L_WARN, _("received frame from unknown node %d (%s)"), src, (const char *)sockinfo (sa)); |
|
|
1233 | else |
|
|
1234 | conns[src - 1]->recv_vpn_packet (pkt, &sa); |
1397 | conns[dst - 1]->inject_data_packet (pkt); |
1235 | } |
|
|
1236 | else |
|
|
1237 | { |
|
|
1238 | // probably ECONNRESET or somesuch |
|
|
1239 | slog (L_DEBUG, _("%s: %s"), (const char *)sockinfo(sa), strerror (errno)); |
|
|
1240 | } |
|
|
1241 | |
|
|
1242 | delete pkt; |
|
|
1243 | } |
1398 | } |
1244 | else if (pollfd[1].revents & POLLHUP) |
1399 | else |
1245 | { |
1400 | { |
1246 | // this cannot ;) happen on udp sockets |
1401 | // broadcast, first check router, then self, then english |
1247 | slog (L_ERR, _("FATAL: POLLHUP on socket fd, terminating.")); |
1402 | connection *router = find_router (); |
1248 | exit (1); |
1403 | |
1249 | } |
1404 | if (router) |
|
|
1405 | router->inject_data_packet (pkt, true); |
1250 | else |
1406 | else |
1251 | { |
1407 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
1252 | slog (L_ERR, |
1408 | if ((*c)->conf != THISNODE) |
1253 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
1409 | (*c)->inject_data_packet (pkt); |
1254 | pollfd[1].revents); |
|
|
1255 | exit (1); |
|
|
1256 | } |
|
|
1257 | } |
1410 | } |
|
|
1411 | } |
1258 | |
1412 | |
1259 | // I use else here to give vpn_packets absolute priority |
1413 | delete pkt; |
1260 | else if (pollfd[0].revents) |
1414 | } |
1261 | { |
|
|
1262 | if (pollfd[0].revents & POLLIN) |
|
|
1263 | { |
|
|
1264 | /* process data */ |
|
|
1265 | tap_packet *pkt; |
|
|
1266 | |
|
|
1267 | pkt = tap->recv (); |
|
|
1268 | |
|
|
1269 | int dst = mac2id (pkt->dst); |
|
|
1270 | int src = mac2id (pkt->src); |
|
|
1271 | |
|
|
1272 | if (src != THISNODE->id) |
|
|
1273 | { |
|
|
1274 | slog (L_ERR, _("FATAL: tap packet not originating on current node received, terminating.")); |
|
|
1275 | exit (1); |
|
|
1276 | } |
|
|
1277 | |
|
|
1278 | if (dst == THISNODE->id) |
|
|
1279 | { |
|
|
1280 | slog (L_ERR, _("FATAL: tap packet destined for current node received, terminating.")); |
|
|
1281 | exit (1); |
|
|
1282 | } |
|
|
1283 | |
|
|
1284 | if (dst > conns.size ()) |
|
|
1285 | slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); |
|
|
1286 | else |
|
|
1287 | { |
|
|
1288 | if (dst) |
|
|
1289 | { |
|
|
1290 | // unicast |
|
|
1291 | if (dst != THISNODE->id) |
|
|
1292 | conns[dst - 1]->inject_data_packet (pkt); |
|
|
1293 | } |
|
|
1294 | else |
|
|
1295 | { |
|
|
1296 | // broadcast, first check router, then self, then english |
|
|
1297 | connection *router = find_router (); |
|
|
1298 | |
|
|
1299 | if (router) |
|
|
1300 | router->inject_data_packet (pkt, true); |
|
|
1301 | else |
|
|
1302 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
|
|
1303 | if ((*c)->conf != THISNODE) |
|
|
1304 | (*c)->inject_data_packet (pkt); |
|
|
1305 | } |
|
|
1306 | } |
|
|
1307 | |
|
|
1308 | delete pkt; |
|
|
1309 | } |
|
|
1310 | else if (pollfd[0].revents & (POLLHUP | POLLERR)) |
1415 | else if (revents & (POLLHUP | POLLERR)) |
1311 | { |
1416 | { |
1312 | slog (L_ERR, _("FATAL: POLLHUP or POLLERR on network device fd, terminating.")); |
1417 | slog (L_ERR, _("FATAL: POLLHUP or POLLERR on network device fd, terminating.")); |
1313 | exit (1); |
1418 | exit (1); |
1314 | } |
1419 | } |
1315 | else |
1420 | else |
1316 | abort (); |
1421 | abort (); |
1317 | } |
1422 | } |
1318 | } |
|
|
1319 | |
1423 | |
|
|
1424 | void |
|
|
1425 | vpn::event_cb (tstamp &ts) |
|
|
1426 | { |
1320 | if (events) |
1427 | if (events) |
1321 | { |
1428 | { |
1322 | if (events & EVENT_SHUTDOWN) |
1429 | if (events & EVENT_SHUTDOWN) |
1323 | { |
1430 | { |
1324 | shutdown_all (); |
1431 | shutdown_all (); |
1325 | |
1432 | |
1326 | remove_pid (pidfilename); |
1433 | remove_pid (pidfilename); |
1327 | |
1434 | |
1328 | slog (L_INFO, _("vped terminating")); |
1435 | slog (L_INFO, _("vped terminating")); |
1329 | |
1436 | |
1330 | exit (0); |
1437 | exit (0); |
1331 | } |
1438 | } |
1332 | |
1439 | |
1333 | if (events & EVENT_RECONNECT) |
1440 | if (events & EVENT_RECONNECT) |
1334 | reconnect_all (); |
1441 | reconnect_all (); |
1335 | |
1442 | |
1336 | events = 0; |
1443 | events = 0; |
1337 | } |
|
|
1338 | |
|
|
1339 | // very very very dumb and crude and inefficient timer handling, or maybe not? |
|
|
1340 | if (now >= next_timecheck) |
|
|
1341 | { |
|
|
1342 | next_timecheck = now + TIMER_GRANULARITY; |
|
|
1343 | |
|
|
1344 | for (conns_vector::iterator c = conns.begin (); |
|
|
1345 | c != conns.end (); ++c) |
|
|
1346 | (*c)->timer (); |
|
|
1347 | } |
|
|
1348 | } |
1444 | } |
|
|
1445 | |
|
|
1446 | ts = TSTAMP_CANCEL; |
|
|
1447 | } |
|
|
1448 | |
|
|
1449 | vpn::vpn (void) |
|
|
1450 | : udp_ev_watcher (this, &vpn::udp_ev) |
|
|
1451 | , vpn_ev_watcher (this, &vpn::vpn_ev) |
|
|
1452 | , event (this, &vpn::event_cb) |
|
|
1453 | { |
1349 | } |
1454 | } |
1350 | |
1455 | |
1351 | vpn::~vpn () |
1456 | vpn::~vpn () |
1352 | {} |
1457 | { |
|
|
1458 | } |
1353 | |
1459 | |