… | |
… | |
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 tstamp 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 | struct net_rateinfo { |
|
|
270 | u32 host; |
|
|
271 | double pcnt, diff; |
|
|
272 | tstamp last; |
|
|
273 | }; |
|
|
274 | |
|
|
275 | // only do action once every x seconds per host whole allowing bursts. |
|
|
276 | // this implementation ("splay list" ;) is inefficient, |
|
|
277 | // but low on resources. |
|
|
278 | struct net_rate_limiter : private list<net_rateinfo> |
|
|
279 | { |
|
|
280 | static const double ALPHA = 1. - 1. / 90.; // allow bursts |
|
|
281 | static const double CUTOFF = 20.; // one event every CUTOFF seconds |
|
|
282 | static const double EXPIRE = CUTOFF * 30.; // expire entries after this time |
|
|
283 | |
|
|
284 | bool can (u32 host); |
|
|
285 | bool can (SOCKADDR *sa) { return can((u32)sa->sin_addr.s_addr); } |
|
|
286 | bool can (sockinfo &si) { return can((u32)si.host); } |
|
|
287 | }; |
|
|
288 | |
|
|
289 | net_rate_limiter auth_rate_limiter, reset_rate_limiter; |
|
|
290 | |
|
|
291 | bool net_rate_limiter::can (u32 host) |
|
|
292 | { |
|
|
293 | iterator i; |
|
|
294 | |
|
|
295 | for (i = begin (); i != end (); ) |
|
|
296 | if (i->host == host) |
|
|
297 | break; |
|
|
298 | else if (i->last < NOW - EXPIRE) |
|
|
299 | i = erase (i); |
|
|
300 | else |
|
|
301 | i++; |
|
|
302 | |
|
|
303 | if (i == end ()) |
|
|
304 | { |
|
|
305 | net_rateinfo ri; |
|
|
306 | |
|
|
307 | ri.host = host; |
|
|
308 | ri.pcnt = 1.; |
|
|
309 | ri.diff = CUTOFF * (1. / (1. - ALPHA)); |
|
|
310 | ri.last = NOW; |
|
|
311 | |
|
|
312 | push_front (ri); |
|
|
313 | |
|
|
314 | return true; |
|
|
315 | } |
|
|
316 | else |
|
|
317 | { |
|
|
318 | net_rateinfo ri (*i); |
|
|
319 | erase (i); |
|
|
320 | |
|
|
321 | ri.pcnt = ri.pcnt * ALPHA; |
|
|
322 | ri.diff = ri.diff * ALPHA + (NOW - ri.last); |
|
|
323 | |
|
|
324 | ri.last = NOW; |
|
|
325 | |
|
|
326 | bool send = ri.diff / ri.pcnt > CUTOFF; |
|
|
327 | |
|
|
328 | if (send) |
|
|
329 | ri.pcnt++; |
|
|
330 | |
|
|
331 | //printf ("RATE %d %f,%f = %f > %f\n", !!send, ri.pcnt, ri.diff, ri.diff / ri.pcnt, CUTOFF); |
|
|
332 | |
|
|
333 | push_front (ri); |
|
|
334 | |
|
|
335 | return send; |
|
|
336 | } |
133 | } |
337 | } |
134 | |
338 | |
135 | ///////////////////////////////////////////////////////////////////////////// |
339 | ///////////////////////////////////////////////////////////////////////////// |
136 | |
340 | |
137 | static void next_wakeup (time_t next) |
341 | static void next_wakeup (time_t next) |
… | |
… | |
200 | |
404 | |
201 | unsigned int src () |
405 | unsigned int src () |
202 | { |
406 | { |
203 | return src1 | ((srcdst >> 4) << 8); |
407 | return src1 | ((srcdst >> 4) << 8); |
204 | } |
408 | } |
|
|
409 | |
205 | unsigned int dst () |
410 | unsigned int dst () |
206 | { |
411 | { |
207 | return dst1 | ((srcdst & 0xf) << 8); |
412 | return dst1 | ((srcdst & 0xf) << 8); |
208 | } |
413 | } |
|
|
414 | |
209 | ptype typ () |
415 | ptype typ () |
210 | { |
416 | { |
211 | return (ptype) type; |
417 | return (ptype) type; |
212 | } |
418 | } |
213 | }; |
419 | }; |
… | |
… | |
252 | u32 cl; |
458 | u32 cl; |
253 | |
459 | |
254 | cl = lzf_compress (d, l, cdata + 2, (l - 2) & ~7); |
460 | cl = lzf_compress (d, l, cdata + 2, (l - 2) & ~7); |
255 | if (cl) |
461 | if (cl) |
256 | { |
462 | { |
257 | //printf ("compressed packet, %d => %d\n", l, cl);//D |
|
|
258 | type = PT_DATA_COMPRESSED; |
463 | type = PT_DATA_COMPRESSED; |
259 | d = cdata; |
464 | d = cdata; |
260 | l = cl + 2; |
465 | l = cl + 2; |
261 | |
466 | |
262 | d[0] = cl >> 8; |
467 | d[0] = cl >> 8; |
… | |
… | |
264 | } |
469 | } |
265 | #endif |
470 | #endif |
266 | |
471 | |
267 | EVP_EncryptInit_ex (cctx, 0, 0, 0, 0); |
472 | EVP_EncryptInit_ex (cctx, 0, 0, 0, 0); |
268 | |
473 | |
|
|
474 | struct { |
269 | #if RAND_SIZE |
475 | #if RAND_SIZE |
270 | struct { |
|
|
271 | u8 rnd[RAND_SIZE]; |
476 | u8 rnd[RAND_SIZE]; |
|
|
477 | #endif |
272 | u32 seqno; |
478 | u32 seqno; |
273 | } datahdr; |
479 | } datahdr; |
274 | |
480 | |
275 | datahdr.seqno = seqno; |
481 | datahdr.seqno = ntohl (seqno); |
|
|
482 | #if RAND_SIZE |
276 | RAND_pseudo_bytes ((unsigned char *) datahdr.rnd, RAND_SIZE); |
483 | RAND_pseudo_bytes ((unsigned char *) datahdr.rnd, RAND_SIZE); |
|
|
484 | #endif |
277 | |
485 | |
278 | EVP_EncryptUpdate (cctx, |
486 | EVP_EncryptUpdate (cctx, |
279 | (unsigned char *) data + outl, &outl2, |
487 | (unsigned char *) data + outl, &outl2, |
280 | (unsigned char *) &datahdr, DATAHDR); |
488 | (unsigned char *) &datahdr, DATAHDR); |
281 | outl += outl2; |
489 | 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 | |
490 | |
289 | EVP_EncryptUpdate (cctx, |
491 | EVP_EncryptUpdate (cctx, |
290 | (unsigned char *) data + outl, &outl2, |
492 | (unsigned char *) data + outl, &outl2, |
291 | (unsigned char *) d, l); |
493 | (unsigned char *) d, l); |
292 | outl += outl2; |
494 | outl += outl2; |
… | |
… | |
328 | outl += outl2; |
530 | outl += outl2; |
329 | |
531 | |
330 | EVP_DecryptFinal_ex (cctx, (unsigned char *)d + outl, &outl2); |
532 | EVP_DecryptFinal_ex (cctx, (unsigned char *)d + outl, &outl2); |
331 | outl += outl2; |
533 | outl += outl2; |
332 | |
534 | |
333 | seqno = *(u32 *)(d + RAND_SIZE); |
535 | seqno = ntohl (*(u32 *)(d + RAND_SIZE)); |
334 | |
536 | |
335 | id2mac (dst () ? dst() : THISNODE->id, p->dst); |
537 | id2mac (dst () ? dst() : THISNODE->id, p->dst); |
336 | id2mac (src (), p->src); |
538 | id2mac (src (), p->src); |
337 | |
539 | |
338 | #if ENABLE_COMPRESSION |
540 | #if ENABLE_COMPRESSION |
339 | if (type == PT_DATA_COMPRESSED) |
541 | if (type == PT_DATA_COMPRESSED) |
340 | { |
542 | { |
341 | u32 cl = (d[DATAHDR] << 8) | d[DATAHDR + 1]; |
543 | u32 cl = (d[DATAHDR] << 8) | d[DATAHDR + 1]; |
342 | p->len = lzf_decompress (d + DATAHDR + 2, cl, &(*p)[6 + 6], MAX_MTU) + 6 + 6; |
544 | 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 | } |
545 | } |
345 | else |
546 | else |
346 | p->len = outl + (6 + 6 - DATAHDR); |
547 | p->len = outl + (6 + 6 - DATAHDR); |
347 | #endif |
548 | #endif |
348 | |
549 | |
… | |
… | |
369 | u32 digest_nid; |
570 | u32 digest_nid; |
370 | |
571 | |
371 | const u8 curflags () const |
572 | const u8 curflags () const |
372 | { |
573 | { |
373 | return 0x80 |
574 | return 0x80 |
374 | | (ENABLE_COMPRESSION ? 0x01 : 0x00) |
575 | | (ENABLE_COMPRESSION ? 0x01 : 0x00); |
375 | | (ENABLE_TRUST ? 0x02 : 0x00); |
|
|
376 | } |
576 | } |
377 | |
577 | |
378 | void setup (ptype type, int dst) |
578 | void setup (ptype type, int dst) |
379 | { |
579 | { |
380 | prot_major = PROTOCOL_MAJOR; |
580 | prot_major = PROTOCOL_MAJOR; |
… | |
… | |
489 | } |
689 | } |
490 | |
690 | |
491 | void |
691 | void |
492 | connection::send_reset (SOCKADDR *dsa) |
692 | connection::send_reset (SOCKADDR *dsa) |
493 | { |
693 | { |
494 | static net_rate_limiter limiter(1); |
694 | if (reset_rate_limiter.can (dsa) && connectmode != conf_node::C_DISABLED) |
495 | |
|
|
496 | if (limiter.can (dsa)) |
|
|
497 | { |
695 | { |
498 | config_packet *pkt = new config_packet; |
696 | config_packet *pkt = new config_packet; |
499 | |
697 | |
500 | pkt->setup (vpn_packet::PT_RESET, conf->id); |
698 | pkt->setup (vpn_packet::PT_RESET, conf->id); |
501 | vpn->send_vpn_packet (pkt, dsa, IPTOS_MINCOST); |
699 | vpn->send_vpn_packet (pkt, dsa, IPTOS_MINCOST); |
… | |
… | |
503 | delete pkt; |
701 | delete pkt; |
504 | } |
702 | } |
505 | } |
703 | } |
506 | |
704 | |
507 | static rsachallenge * |
705 | static rsachallenge * |
508 | gen_challenge (SOCKADDR *sa) |
706 | gen_challenge (u32 seqrand, SOCKADDR *sa) |
509 | { |
707 | { |
510 | static rsachallenge k; |
708 | static rsachallenge k; |
511 | |
709 | |
512 | memcpy (&k, &challenge_bytes (), sizeof (k)); |
710 | memcpy (&k, &challenge_bytes (), sizeof (k)); |
513 | RAND_bytes ((unsigned char *)&k[CHG_SEQNO], sizeof (u32)); |
711 | *(u32 *)&k[CHG_SEQNO] ^= seqrand; |
514 | xor_sa (k, sa); |
712 | xor_sa (k, sa); |
515 | |
713 | |
516 | return &k; |
714 | return &k; |
517 | } |
715 | } |
518 | |
716 | |
519 | void |
717 | void |
520 | connection::send_auth (auth_subtype subtype, SOCKADDR *sa, rsachallenge *k) |
718 | connection::send_auth (auth_subtype subtype, SOCKADDR *sa, const rsachallenge *k) |
521 | { |
719 | { |
522 | static net_rate_limiter limiter(2); |
|
|
523 | |
|
|
524 | if (subtype != AUTH_INIT || limiter.can (sa)) |
720 | if (subtype == AUTH_REPLY || auth_rate_limiter.can (sa)) |
525 | { |
721 | { |
|
|
722 | if (!k) |
|
|
723 | k = gen_challenge (seqrand, sa); |
|
|
724 | |
526 | auth_packet *pkt = new auth_packet (conf->id, subtype); |
725 | auth_packet *pkt = new auth_packet (conf->id, subtype); |
527 | |
726 | |
528 | //printf ("send auth_packet subtype %d\n", subtype);//D |
727 | 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 | |
728 | |
553 | slog (L_TRACE, ">>%d PT_AUTH(%d) [%s]", conf->id, subtype, (const char *)sockinfo (sa)); |
729 | slog (L_TRACE, ">>%d PT_AUTH(%d) [%s]", conf->id, subtype, (const char *)sockinfo (sa)); |
554 | |
730 | |
555 | vpn->send_vpn_packet (pkt, sa, IPTOS_RELIABILITY); |
731 | vpn->send_vpn_packet (pkt, sa, IPTOS_RELIABILITY); |
556 | |
732 | |
557 | delete pkt; |
733 | delete pkt; |
558 | } |
734 | } |
559 | } |
735 | } |
560 | |
736 | |
561 | void |
737 | void |
562 | connection::establish_connection () |
738 | connection::establish_connection_cb (tstamp &ts) |
563 | { |
739 | { |
564 | if (!ictx && conf != THISNODE && connectmode != conf_node::C_NEVER) |
740 | if (ictx || conf == THISNODE || connectmode == conf_node::C_NEVER) |
|
|
741 | ts = TSTAMP_CANCEL; |
|
|
742 | else if (ts <= NOW) |
565 | { |
743 | { |
566 | if (now >= next_retry) |
744 | 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 | |
745 | |
570 | if (retry_cnt < (17 << 2) | 3) |
746 | if (retry_int < 3600 * 8) |
571 | retry_cnt++; |
747 | retry_cnt++; |
572 | |
748 | |
573 | if (connectmode == conf_node::C_ONDEMAND |
749 | ts = NOW + retry_int; |
574 | && retry_int > ::conf.keepalive) |
|
|
575 | retry_int = ::conf.keepalive; |
|
|
576 | |
750 | |
577 | next_retry = now + retry_int; |
|
|
578 | next_wakeup (next_retry); |
|
|
579 | |
|
|
580 | if (conf->hostname) |
751 | if (conf->hostname) |
581 | { |
752 | { |
582 | reset_dstaddr (); |
753 | reset_dstaddr (); |
583 | if (sa.sin_addr.s_addr) |
754 | if (sa.sin_addr.s_addr) |
584 | if (retry_cnt < 4) |
755 | if (retry_cnt < 4) |
585 | send_auth (AUTH_INIT, &sa); |
756 | send_auth (AUTH_INIT, &sa); |
586 | else |
757 | else if (auth_rate_limiter.can (&sa)) |
587 | send_ping (&sa, 0); |
758 | send_ping (&sa, 0); |
588 | } |
759 | } |
589 | else |
760 | else |
590 | vpn->connect_request (conf->id); |
761 | vpn->connect_request (conf->id); |
591 | } |
|
|
592 | } |
762 | } |
593 | } |
763 | } |
594 | |
764 | |
595 | void |
765 | void |
596 | connection::reset_connection () |
766 | connection::reset_connection () |
… | |
… | |
601 | |
771 | |
602 | if (::conf.script_node_down) |
772 | if (::conf.script_node_down) |
603 | run_script (this, &connection::script_node_down, false); |
773 | run_script (this, &connection::script_node_down, false); |
604 | } |
774 | } |
605 | |
775 | |
606 | delete ictx; |
776 | delete ictx; ictx = 0; |
607 | ictx = 0; |
777 | delete octx; octx = 0; |
608 | |
778 | |
609 | delete octx; |
779 | RAND_bytes ((unsigned char *)&seqrand, sizeof (u32)); |
610 | octx = 0; |
|
|
611 | |
780 | |
612 | sa.sin_port = 0; |
781 | sa.sin_port = 0; |
613 | sa.sin_addr.s_addr = 0; |
782 | sa.sin_addr.s_addr = 0; |
614 | |
783 | |
615 | next_retry = 0; |
|
|
616 | next_rekey = 0; |
|
|
617 | last_activity = 0; |
784 | last_activity = 0; |
|
|
785 | |
|
|
786 | rekey.reset (); |
|
|
787 | keepalive.reset (); |
|
|
788 | establish_connection.reset (); |
618 | } |
789 | } |
619 | |
790 | |
620 | void |
791 | void |
621 | connection::shutdown () |
792 | connection::shutdown () |
622 | { |
793 | { |
… | |
… | |
625 | |
796 | |
626 | reset_connection (); |
797 | reset_connection (); |
627 | } |
798 | } |
628 | |
799 | |
629 | void |
800 | void |
630 | connection::rekey () |
801 | connection::rekey_cb (tstamp &ts) |
631 | { |
802 | { |
|
|
803 | ts = TSTAMP_CANCEL; |
|
|
804 | |
632 | reset_connection (); |
805 | reset_connection (); |
633 | establish_connection (); |
806 | establish_connection (); |
634 | } |
807 | } |
635 | |
808 | |
636 | void |
809 | void |
… | |
… | |
668 | } |
841 | } |
669 | |
842 | |
670 | void |
843 | void |
671 | connection::recv_vpn_packet (vpn_packet *pkt, SOCKADDR *ssa) |
844 | connection::recv_vpn_packet (vpn_packet *pkt, SOCKADDR *ssa) |
672 | { |
845 | { |
673 | last_activity = now; |
846 | last_activity = NOW; |
674 | |
847 | |
675 | slog (L_NOISE, "<<%d received packet type %d from %d to %d", |
848 | slog (L_NOISE, "<<%d received packet type %d from %d to %d", |
676 | conf->id, pkt->typ (), pkt->src (), pkt->dst ()); |
849 | conf->id, pkt->typ (), pkt->src (), pkt->dst ()); |
677 | |
850 | |
678 | switch (pkt->typ ()) |
851 | switch (pkt->typ ()) |
679 | { |
852 | { |
680 | case vpn_packet::PT_PING: |
853 | case vpn_packet::PT_PING: |
681 | send_ping (ssa, 1); // pong |
|
|
682 | break; |
|
|
683 | |
|
|
684 | case vpn_packet::PT_PONG: |
|
|
685 | // we send pings instead of auth packets after some retries, |
854 | // we send pings instead of auth packets after some retries, |
686 | // so reset the retry counter and establish a conenction |
855 | // so reset the retry counter and establish a conenction |
687 | // when we receive a pong. |
856 | // when we receive a pong. |
688 | if (!ictx && !octx) |
857 | if (!ictx && !octx) |
689 | { |
858 | { |
690 | retry_cnt = 0; |
859 | retry_cnt = 0; |
691 | next_retry = 0; |
860 | establish_connection.at = 0; |
692 | establish_connection (); |
861 | establish_connection (); |
693 | } |
862 | } |
|
|
863 | else |
|
|
864 | send_ping (ssa, 1); // pong |
694 | |
865 | |
|
|
866 | break; |
|
|
867 | |
|
|
868 | case vpn_packet::PT_PONG: |
695 | break; |
869 | break; |
696 | |
870 | |
697 | case vpn_packet::PT_RESET: |
871 | case vpn_packet::PT_RESET: |
698 | { |
872 | { |
699 | reset_connection (); |
873 | reset_connection (); |
700 | |
874 | |
701 | config_packet *p = (config_packet *) pkt; |
875 | config_packet *p = (config_packet *) pkt; |
702 | if (p->chk_config ()) |
876 | if (!p->chk_config ()) |
|
|
877 | { |
|
|
878 | slog (L_WARN, _("protocol mismatch, disabling node '%s'"), conf->nodename); |
|
|
879 | connectmode = conf_node::C_DISABLED; |
|
|
880 | } |
703 | if (connectmode == conf_node::C_ALWAYS) |
881 | else if (connectmode == conf_node::C_ALWAYS) |
704 | establish_connection (); |
882 | establish_connection (); |
705 | |
|
|
706 | //D slog the protocol mismatch? |
|
|
707 | } |
883 | } |
708 | break; |
884 | break; |
709 | |
885 | |
710 | case vpn_packet::PT_AUTH: |
886 | case vpn_packet::PT_AUTH: |
711 | { |
887 | { |
… | |
… | |
721 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
897 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
722 | |
898 | |
723 | if (p->subtype == AUTH_INIT) |
899 | if (p->subtype == AUTH_INIT) |
724 | send_auth (AUTH_INITREPLY, ssa); |
900 | send_auth (AUTH_INITREPLY, ssa); |
725 | |
901 | |
726 | rsachallenge k; |
902 | const rsachallenge *k = rsa_cache.private_decrypt (::conf.rsa_key, p->challenge); |
727 | |
903 | |
728 | #if ENABLE_TRUST |
|
|
729 | if (0 > RSA_private_decrypt (sizeof (rsaencrdata), |
|
|
730 | (unsigned char *)&p->challenge, (unsigned char *)&k, |
|
|
731 | ::conf.rsa_key, RSA_PKCS1_OAEP_PADDING)) |
|
|
732 | // continued below |
|
|
733 | #else |
|
|
734 | rsaencrdata j; |
|
|
735 | |
904 | if (!k) |
736 | if (0 > RSA_private_decrypt (sizeof (rsaencrdata), |
|
|
737 | (unsigned char *)&p->challenge, (unsigned char *)&j, |
|
|
738 | ::conf.rsa_key, RSA_NO_PADDING)) |
|
|
739 | fatal ("RSA_private_decrypt error"); |
|
|
740 | |
|
|
741 | if (0 > RSA_public_decrypt (sizeof (k), |
|
|
742 | (unsigned char *)&j, (unsigned char *)&k, |
|
|
743 | conf->rsa_key, RSA_PKCS1_OAEP_PADDING)) |
|
|
744 | // continued below |
|
|
745 | #endif |
|
|
746 | { |
905 | { |
747 | slog (L_ERR, _("challenge from %s (%s) illegal or corrupted"), |
906 | slog (L_ERR, _("challenge from %s (%s) illegal or corrupted"), |
748 | conf->nodename, (const char *)sockinfo (ssa)); |
907 | conf->nodename, (const char *)sockinfo (ssa)); |
|
|
908 | send_reset (ssa); |
749 | break; |
909 | break; |
750 | } |
910 | } |
751 | |
911 | |
752 | retry_cnt = 0; |
912 | retry_cnt = 0; |
753 | next_retry = now + 8; |
913 | establish_connection.set (NOW + 8); //? ;) |
|
|
914 | keepalive.reset (); |
|
|
915 | rekey.reset (); |
754 | |
916 | |
755 | switch (p->subtype) |
917 | switch (p->subtype) |
756 | { |
918 | { |
757 | case AUTH_INIT: |
919 | case AUTH_INIT: |
758 | case AUTH_INITREPLY: |
920 | case AUTH_INITREPLY: |
759 | delete ictx; |
921 | delete ictx; |
760 | ictx = 0; |
922 | ictx = 0; |
761 | |
923 | |
762 | delete octx; |
924 | delete octx; |
763 | |
925 | |
764 | octx = new crypto_ctx (k, 1); |
926 | octx = new crypto_ctx (*k, 1); |
765 | oseqno = *(u32 *)&k[CHG_SEQNO] & 0x7fffffff; |
927 | oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff; |
766 | |
928 | |
767 | send_auth (AUTH_REPLY, ssa, &k); |
929 | send_auth (AUTH_REPLY, ssa, k); |
768 | break; |
930 | break; |
769 | |
931 | |
770 | case AUTH_REPLY: |
932 | case AUTH_REPLY: |
771 | |
933 | |
772 | if (!memcmp ((u8 *)gen_challenge (ssa) + sizeof (u32), (u8 *)&k + sizeof (u32), |
934 | if (!memcmp ((u8 *)gen_challenge (seqrand, ssa), (u8 *)k, sizeof (rsachallenge))) |
773 | sizeof (rsachallenge) - sizeof (u32))) |
|
|
774 | { |
935 | { |
775 | delete ictx; |
936 | delete ictx; |
776 | |
937 | |
777 | ictx = new crypto_ctx (k, 0); |
938 | ictx = new crypto_ctx (*k, 0); |
778 | iseqno = *(u32 *)&k[CHG_SEQNO] & 0x7fffffff; // at least 2**31 sequence numbers are valid |
939 | iseqno.reset (ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff); // at least 2**31 sequence numbers are valid |
779 | ismask = 0xffffffff; // initially, all lower sequence numbers are invalid |
|
|
780 | |
940 | |
781 | sa = *ssa; |
941 | sa = *ssa; |
782 | |
942 | |
783 | next_rekey = now + ::conf.rekey; |
943 | rekey.set (NOW + ::conf.rekey); |
784 | next_wakeup (next_rekey); |
944 | keepalive.set (NOW + ::conf.keepalive); |
785 | |
945 | |
786 | // send queued packets |
946 | // send queued packets |
787 | while (tap_packet *p = queue.get ()) |
947 | while (tap_packet *p = queue.get ()) |
788 | { |
948 | { |
789 | send_data_packet (p); |
949 | send_data_packet (p); |
… | |
… | |
834 | else |
994 | else |
835 | { |
995 | { |
836 | u32 seqno; |
996 | u32 seqno; |
837 | tap_packet *d = p->unpack (this, seqno); |
997 | tap_packet *d = p->unpack (this, seqno); |
838 | |
998 | |
839 | if (seqno <= iseqno - 32) |
999 | if (iseqno.recv_ok (seqno)) |
840 | slog (L_ERR, _("received duplicate or outdated packet (received %08lx, expected %08lx)\n" |
|
|
841 | "possible replay attack, or just massive packet reordering"), seqno, iseqno + 1);//D |
|
|
842 | else if (seqno > iseqno + 32) |
|
|
843 | slog (L_ERR, _("received duplicate or out-of-sync packet (received %08lx, expected %08lx)\n" |
|
|
844 | "possible replay attack, or just massive packet loss"), seqno, iseqno + 1);//D |
|
|
845 | else |
|
|
846 | { |
1000 | { |
847 | if (seqno > iseqno) |
|
|
848 | { |
|
|
849 | ismask <<= seqno - iseqno; |
|
|
850 | iseqno = seqno; |
|
|
851 | } |
|
|
852 | |
|
|
853 | u32 mask = 1 << (iseqno - seqno); |
|
|
854 | |
|
|
855 | //printf ("received seqno %08lx, iseqno %08lx, mask %08lx is %08lx\n", seqno, iseqno, mask, ismask); |
|
|
856 | if (ismask & mask) |
|
|
857 | slog (L_ERR, _("received duplicate packet (received %08lx, expected %08lx)\n" |
|
|
858 | "possible replay attack, or just packet duplication"), seqno, iseqno + 1);//D |
|
|
859 | else |
|
|
860 | { |
|
|
861 | ismask |= mask; |
|
|
862 | |
|
|
863 | vpn->tap->send (d); |
1001 | vpn->tap->send (d); |
864 | |
1002 | |
865 | if (p->dst () == 0) // re-broadcast |
1003 | if (p->dst () == 0) // re-broadcast |
866 | for (vpn::conns_vector::iterator i = vpn->conns.begin (); i != vpn->conns.end (); ++i) |
1004 | for (vpn::conns_vector::iterator i = vpn->conns.begin (); i != vpn->conns.end (); ++i) |
867 | { |
1005 | { |
868 | connection *c = *i; |
1006 | connection *c = *i; |
869 | |
1007 | |
870 | if (c->conf != THISNODE && c->conf != conf) |
1008 | if (c->conf != THISNODE && c->conf != conf) |
871 | c->inject_data_packet (d); |
1009 | c->inject_data_packet (d); |
872 | } |
|
|
873 | |
|
|
874 | delete d; |
|
|
875 | |
|
|
876 | break; |
|
|
877 | } |
1010 | } |
|
|
1011 | |
|
|
1012 | delete d; |
|
|
1013 | |
|
|
1014 | break; |
878 | } |
1015 | } |
879 | } |
1016 | } |
880 | } |
1017 | } |
881 | else |
1018 | else |
882 | slog (L_ERR, _("received data packet from unknown source %s"), (const char *)sockinfo (ssa));//D |
1019 | slog (L_ERR, _("received data packet from unknown source %s"), (const char *)sockinfo (ssa));//D |
… | |
… | |
953 | send_reset (ssa); |
1090 | send_reset (ssa); |
954 | break; |
1091 | break; |
955 | } |
1092 | } |
956 | } |
1093 | } |
957 | |
1094 | |
958 | void connection::timer () |
1095 | void connection::keepalive_cb (tstamp &ts) |
959 | { |
1096 | { |
960 | if (conf != THISNODE) |
1097 | if (NOW >= last_activity + ::conf.keepalive + 30) |
961 | { |
1098 | { |
962 | if (now >= next_retry && connectmode == conf_node::C_ALWAYS) |
1099 | reset_connection (); |
963 | establish_connection (); |
1100 | establish_connection (); |
964 | |
1101 | } |
965 | if (ictx && octx) |
|
|
966 | { |
|
|
967 | if (now >= next_rekey) |
|
|
968 | rekey (); |
|
|
969 | else if (now >= last_activity + ::conf.keepalive + 30) |
|
|
970 | { |
|
|
971 | reset_connection (); |
|
|
972 | establish_connection (); |
|
|
973 | } |
|
|
974 | else if (now >= last_activity + ::conf.keepalive) |
1102 | else if (NOW < last_activity + ::conf.keepalive) |
|
|
1103 | ts = last_activity + ::conf.keepalive; |
975 | if (conf->connectmode != conf_node::C_ONDEMAND |
1104 | else if (conf->connectmode != conf_node::C_ONDEMAND |
976 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1105 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
|
|
1106 | { |
977 | send_ping (&sa); |
1107 | send_ping (&sa); |
978 | else |
1108 | ts = NOW + 5; |
|
|
1109 | } |
|
|
1110 | else |
979 | reset_connection (); |
1111 | reset_connection (); |
980 | |
1112 | |
981 | } |
|
|
982 | } |
|
|
983 | } |
1113 | } |
984 | |
1114 | |
985 | void connection::connect_request (int id) |
1115 | void connection::connect_request (int id) |
986 | { |
1116 | { |
987 | connect_req_packet *p = new connect_req_packet (conf->id, id); |
1117 | connect_req_packet *p = new connect_req_packet (conf->id, id); |
… | |
… | |
1024 | putenv ("STATE=down"); |
1154 | putenv ("STATE=down"); |
1025 | |
1155 | |
1026 | return ::conf.script_node_up ? ::conf.script_node_down : "node-down"; |
1156 | return ::conf.script_node_up ? ::conf.script_node_down : "node-down"; |
1027 | } |
1157 | } |
1028 | |
1158 | |
|
|
1159 | connection::connection(struct vpn *vpn_) |
|
|
1160 | : vpn(vpn_) |
|
|
1161 | , rekey (this, &connection::rekey_cb) |
|
|
1162 | , keepalive (this, &connection::keepalive_cb) |
|
|
1163 | , establish_connection (this, &connection::establish_connection_cb) |
|
|
1164 | { |
|
|
1165 | octx = ictx = 0; |
|
|
1166 | retry_cnt = 0; |
|
|
1167 | |
|
|
1168 | connectmode = conf_node::C_ALWAYS; // initial setting |
|
|
1169 | reset_connection (); |
|
|
1170 | } |
|
|
1171 | |
|
|
1172 | connection::~connection () |
|
|
1173 | { |
|
|
1174 | shutdown (); |
|
|
1175 | } |
|
|
1176 | |
1029 | ///////////////////////////////////////////////////////////////////////////// |
1177 | ///////////////////////////////////////////////////////////////////////////// |
1030 | |
|
|
1031 | vpn::vpn (void) |
|
|
1032 | {} |
|
|
1033 | |
1178 | |
1034 | const char *vpn::script_if_up () |
1179 | const char *vpn::script_if_up () |
1035 | { |
1180 | { |
1036 | // the tunnel device mtu should be the physical mtu - overhead |
1181 | // the tunnel device mtu should be the physical mtu - overhead |
1037 | // the tricky part is rounding to the cipher key blocksize |
1182 | // the tricky part is rounding to the cipher key blocksize |
… | |
… | |
1088 | { |
1233 | { |
1089 | int oval = 1; |
1234 | int oval = 1; |
1090 | setsockopt (socket_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
1235 | setsockopt (socket_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
1091 | } |
1236 | } |
1092 | |
1237 | |
|
|
1238 | udp_ev_watcher.start (socket_fd, POLLIN); |
|
|
1239 | |
1093 | tap = new tap_device (); |
1240 | tap = new tap_device (); |
1094 | if (!tap) //D this, of course, never catches |
1241 | if (!tap) //D this, of course, never catches |
1095 | { |
1242 | { |
1096 | slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
1243 | slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
1097 | exit (1); |
1244 | exit (1); |
1098 | } |
1245 | } |
1099 | |
1246 | |
1100 | run_script (this, &vpn::script_if_up, true); |
1247 | run_script (this, &vpn::script_if_up, true); |
|
|
1248 | |
|
|
1249 | vpn_ev_watcher.start (tap->fd, POLLIN); |
|
|
1250 | |
|
|
1251 | reconnect_all (); |
1101 | |
1252 | |
1102 | return 0; |
1253 | return 0; |
1103 | } |
1254 | } |
1104 | |
1255 | |
1105 | void |
1256 | void |
… | |
… | |
1170 | // if ((*i)->conf->routerprio) |
1321 | // if ((*i)->conf->routerprio) |
1171 | // (*i)->establish_connection (); |
1322 | // (*i)->establish_connection (); |
1172 | } |
1323 | } |
1173 | |
1324 | |
1174 | void |
1325 | void |
1175 | vpn::main_loop () |
1326 | vpn::udp_ev (short revents) |
1176 | { |
1327 | { |
1177 | struct pollfd pollfd[2]; |
1328 | if (revents & (POLLIN | POLLERR)) |
1178 | |
|
|
1179 | pollfd[0].fd = tap->fd; |
|
|
1180 | pollfd[0].events = POLLIN; |
|
|
1181 | pollfd[1].fd = socket_fd; |
|
|
1182 | pollfd[1].events = POLLIN; |
|
|
1183 | |
|
|
1184 | events = 0; |
|
|
1185 | now = time (0); |
|
|
1186 | next_timecheck = now + 1; |
|
|
1187 | |
|
|
1188 | reconnect_all (); |
|
|
1189 | |
|
|
1190 | for (;;) |
|
|
1191 | { |
1329 | { |
1192 | int npoll = poll (pollfd, 2, (next_timecheck - now) * 1000); |
1330 | vpn_packet *pkt = new vpn_packet; |
1193 | |
1331 | struct sockaddr_in sa; |
1194 | now = time (0); |
1332 | socklen_t sa_len = sizeof (sa); |
|
|
1333 | int len; |
1195 | |
1334 | |
|
|
1335 | len = recvfrom (socket_fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
|
|
1336 | |
1196 | if (npoll > 0) |
1337 | if (len > 0) |
|
|
1338 | { |
|
|
1339 | pkt->len = len; |
|
|
1340 | |
|
|
1341 | unsigned int src = pkt->src (); |
|
|
1342 | unsigned int dst = pkt->dst (); |
|
|
1343 | |
|
|
1344 | slog (L_NOISE, _("<<?/%s received possible vpn packet type %d from %d to %d, length %d"), |
|
|
1345 | (const char *)sockinfo (sa), pkt->typ (), pkt->src (), pkt->dst (), pkt->len); |
|
|
1346 | |
|
|
1347 | if (dst > conns.size () || pkt->typ () >= vpn_packet::PT_MAX) |
|
|
1348 | slog (L_WARN, _("<<? received CORRUPTED packet type %d from %d to %d"), |
|
|
1349 | pkt->typ (), pkt->src (), pkt->dst ()); |
|
|
1350 | else if (dst == 0 && !THISNODE->routerprio) |
|
|
1351 | slog (L_WARN, _("<<%d received broadcast, but we are no router"), dst); |
|
|
1352 | else if (dst != 0 && dst != THISNODE->id) |
|
|
1353 | slog (L_WARN, |
|
|
1354 | _("received frame for node %d ('%s') from %s, but this is node %d ('%s')"), |
|
|
1355 | dst, conns[dst - 1]->conf->nodename, |
|
|
1356 | (const char *)sockinfo (sa), |
|
|
1357 | THISNODE->id, THISNODE->nodename); |
|
|
1358 | else if (src == 0 || src > conns.size ()) |
|
|
1359 | slog (L_WARN, _("received frame from unknown node %d (%s)"), src, (const char *)sockinfo (sa)); |
|
|
1360 | else |
|
|
1361 | conns[src - 1]->recv_vpn_packet (pkt, &sa); |
1197 | { |
1362 | } |
1198 | if (pollfd[1].revents) |
1363 | else |
|
|
1364 | { |
|
|
1365 | // probably ECONNRESET or somesuch |
|
|
1366 | slog (L_DEBUG, _("%s: %s"), (const char *)sockinfo(sa), strerror (errno)); |
|
|
1367 | } |
|
|
1368 | |
|
|
1369 | delete pkt; |
|
|
1370 | } |
|
|
1371 | else if (revents & POLLHUP) |
|
|
1372 | { |
|
|
1373 | // this cannot ;) happen on udp sockets |
|
|
1374 | slog (L_ERR, _("FATAL: POLLHUP on socket fd, terminating.")); |
|
|
1375 | exit (1); |
|
|
1376 | } |
|
|
1377 | else |
|
|
1378 | { |
|
|
1379 | slog (L_ERR, |
|
|
1380 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
|
|
1381 | revents); |
|
|
1382 | exit (1); |
|
|
1383 | } |
|
|
1384 | } |
|
|
1385 | |
|
|
1386 | void |
|
|
1387 | vpn::vpn_ev (short revents) |
|
|
1388 | { |
|
|
1389 | if (revents & POLLIN) |
|
|
1390 | { |
|
|
1391 | /* process data */ |
|
|
1392 | tap_packet *pkt; |
|
|
1393 | |
|
|
1394 | pkt = tap->recv (); |
|
|
1395 | |
|
|
1396 | int dst = mac2id (pkt->dst); |
|
|
1397 | int src = mac2id (pkt->src); |
|
|
1398 | |
|
|
1399 | if (src != THISNODE->id) |
|
|
1400 | { |
|
|
1401 | slog (L_ERR, _("FATAL: tap packet not originating on current node received, terminating.")); |
|
|
1402 | exit (1); |
|
|
1403 | } |
|
|
1404 | |
|
|
1405 | if (dst == THISNODE->id) |
|
|
1406 | { |
|
|
1407 | slog (L_ERR, _("FATAL: tap packet destined for current node received, terminating.")); |
|
|
1408 | exit (1); |
|
|
1409 | } |
|
|
1410 | |
|
|
1411 | if (dst > conns.size ()) |
|
|
1412 | slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); |
|
|
1413 | else |
|
|
1414 | { |
|
|
1415 | if (dst) |
1199 | { |
1416 | { |
1200 | if (pollfd[1].revents & (POLLIN | POLLERR)) |
1417 | // unicast |
1201 | { |
|
|
1202 | vpn_packet *pkt = new vpn_packet; |
|
|
1203 | struct sockaddr_in sa; |
|
|
1204 | socklen_t sa_len = sizeof (sa); |
|
|
1205 | int len; |
|
|
1206 | |
|
|
1207 | len = recvfrom (socket_fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
|
|
1208 | |
|
|
1209 | if (len > 0) |
|
|
1210 | { |
|
|
1211 | pkt->len = len; |
|
|
1212 | |
|
|
1213 | unsigned int src = pkt->src (); |
|
|
1214 | unsigned int dst = pkt->dst (); |
|
|
1215 | |
|
|
1216 | slog (L_NOISE, _("<<?/%s received possible vpn packet type %d from %d to %d, length %d"), |
|
|
1217 | (const char *)sockinfo (sa), pkt->typ (), pkt->src (), pkt->dst (), pkt->len); |
|
|
1218 | |
|
|
1219 | if (dst > conns.size () || pkt->typ () >= vpn_packet::PT_MAX) |
|
|
1220 | slog (L_WARN, _("<<? received CORRUPTED packet type %d from %d to %d"), |
|
|
1221 | pkt->typ (), pkt->src (), pkt->dst ()); |
|
|
1222 | else if (dst == 0 && !THISNODE->routerprio) |
|
|
1223 | slog (L_WARN, _("<<%d received broadcast, but we are no router"), dst); |
|
|
1224 | else if (dst != 0 && dst != THISNODE->id) |
1418 | if (dst != THISNODE->id) |
1225 | slog (L_WARN, |
|
|
1226 | _("received frame for node %d ('%s') from %s, but this is node %d ('%s')"), |
|
|
1227 | dst, conns[dst - 1]->conf->nodename, |
|
|
1228 | (const char *)sockinfo (sa), |
|
|
1229 | THISNODE->id, THISNODE->nodename); |
|
|
1230 | else if (src == 0 || src > conns.size ()) |
|
|
1231 | slog (L_WARN, _("received frame from unknown node %d (%s)"), src, (const char *)sockinfo (sa)); |
|
|
1232 | else |
|
|
1233 | conns[src - 1]->recv_vpn_packet (pkt, &sa); |
1419 | conns[dst - 1]->inject_data_packet (pkt); |
1234 | } |
|
|
1235 | else |
|
|
1236 | { |
|
|
1237 | // probably ECONNRESET or somesuch |
|
|
1238 | slog (L_DEBUG, _("%s: %s"), (const char *)sockinfo(sa), strerror (errno)); |
|
|
1239 | } |
|
|
1240 | |
|
|
1241 | delete pkt; |
|
|
1242 | } |
1420 | } |
1243 | else if (pollfd[1].revents & POLLHUP) |
1421 | else |
1244 | { |
1422 | { |
1245 | // this cannot ;) happen on udp sockets |
1423 | // broadcast, first check router, then self, then english |
1246 | slog (L_ERR, _("FATAL: POLLHUP on socket fd, terminating.")); |
1424 | connection *router = find_router (); |
1247 | exit (1); |
1425 | |
1248 | } |
1426 | if (router) |
|
|
1427 | router->inject_data_packet (pkt, true); |
1249 | else |
1428 | else |
1250 | { |
1429 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
1251 | slog (L_ERR, |
1430 | if ((*c)->conf != THISNODE) |
1252 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
1431 | (*c)->inject_data_packet (pkt); |
1253 | pollfd[1].revents); |
|
|
1254 | exit (1); |
|
|
1255 | } |
|
|
1256 | } |
1432 | } |
|
|
1433 | } |
1257 | |
1434 | |
1258 | // I use else here to give vpn_packets absolute priority |
1435 | delete pkt; |
1259 | else if (pollfd[0].revents) |
1436 | } |
1260 | { |
|
|
1261 | if (pollfd[0].revents & POLLIN) |
|
|
1262 | { |
|
|
1263 | /* process data */ |
|
|
1264 | tap_packet *pkt; |
|
|
1265 | |
|
|
1266 | pkt = tap->recv (); |
|
|
1267 | |
|
|
1268 | int dst = mac2id (pkt->dst); |
|
|
1269 | int src = mac2id (pkt->src); |
|
|
1270 | |
|
|
1271 | if (src != THISNODE->id) |
|
|
1272 | { |
|
|
1273 | slog (L_ERR, _("FATAL: tap packet not originating on current node received, terminating.")); |
|
|
1274 | exit (1); |
|
|
1275 | } |
|
|
1276 | |
|
|
1277 | if (dst == THISNODE->id) |
|
|
1278 | { |
|
|
1279 | slog (L_ERR, _("FATAL: tap packet destined for current node received, terminating.")); |
|
|
1280 | exit (1); |
|
|
1281 | } |
|
|
1282 | |
|
|
1283 | if (dst > conns.size ()) |
|
|
1284 | slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); |
|
|
1285 | else |
|
|
1286 | { |
|
|
1287 | if (dst) |
|
|
1288 | { |
|
|
1289 | // unicast |
|
|
1290 | if (dst != THISNODE->id) |
|
|
1291 | conns[dst - 1]->inject_data_packet (pkt); |
|
|
1292 | } |
|
|
1293 | else |
|
|
1294 | { |
|
|
1295 | // broadcast, first check router, then self, then english |
|
|
1296 | connection *router = find_router (); |
|
|
1297 | |
|
|
1298 | if (router) |
|
|
1299 | router->inject_data_packet (pkt, true); |
|
|
1300 | else |
|
|
1301 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
|
|
1302 | if ((*c)->conf != THISNODE) |
|
|
1303 | (*c)->inject_data_packet (pkt); |
|
|
1304 | } |
|
|
1305 | } |
|
|
1306 | |
|
|
1307 | delete pkt; |
|
|
1308 | } |
|
|
1309 | else if (pollfd[0].revents & (POLLHUP | POLLERR)) |
1437 | else if (revents & (POLLHUP | POLLERR)) |
1310 | { |
1438 | { |
1311 | slog (L_ERR, _("FATAL: POLLHUP or POLLERR on network device fd, terminating.")); |
1439 | slog (L_ERR, _("FATAL: POLLHUP or POLLERR on network device fd, terminating.")); |
1312 | exit (1); |
1440 | exit (1); |
1313 | } |
1441 | } |
1314 | else |
1442 | else |
1315 | abort (); |
1443 | abort (); |
1316 | } |
1444 | } |
1317 | } |
|
|
1318 | |
1445 | |
|
|
1446 | void |
|
|
1447 | vpn::event_cb (tstamp &ts) |
|
|
1448 | { |
1319 | if (events) |
1449 | if (events) |
1320 | { |
1450 | { |
1321 | if (events & EVENT_SHUTDOWN) |
1451 | if (events & EVENT_SHUTDOWN) |
1322 | { |
1452 | { |
1323 | shutdown_all (); |
1453 | shutdown_all (); |
1324 | |
1454 | |
1325 | remove_pid (pidfilename); |
1455 | remove_pid (pidfilename); |
1326 | |
1456 | |
1327 | slog (L_INFO, _("vped terminating")); |
1457 | slog (L_INFO, _("vped terminating")); |
1328 | |
1458 | |
1329 | exit (0); |
1459 | exit (0); |
1330 | } |
1460 | } |
1331 | |
1461 | |
1332 | if (events & EVENT_RECONNECT) |
1462 | if (events & EVENT_RECONNECT) |
1333 | reconnect_all (); |
1463 | reconnect_all (); |
1334 | |
1464 | |
1335 | events = 0; |
1465 | events = 0; |
1336 | } |
|
|
1337 | |
|
|
1338 | // very very very dumb and crude and inefficient timer handling, or maybe not? |
|
|
1339 | if (now >= next_timecheck) |
|
|
1340 | { |
|
|
1341 | next_timecheck = now + TIMER_GRANULARITY; |
|
|
1342 | |
|
|
1343 | for (conns_vector::iterator c = conns.begin (); |
|
|
1344 | c != conns.end (); ++c) |
|
|
1345 | (*c)->timer (); |
|
|
1346 | } |
|
|
1347 | } |
1466 | } |
|
|
1467 | |
|
|
1468 | ts = TSTAMP_CANCEL; |
|
|
1469 | } |
|
|
1470 | |
|
|
1471 | #include <sys/time.h>//D |
|
|
1472 | vpn::vpn (void) |
|
|
1473 | : udp_ev_watcher (this, &vpn::udp_ev) |
|
|
1474 | , vpn_ev_watcher (this, &vpn::vpn_ev) |
|
|
1475 | , event (this, &vpn::event_cb) |
|
|
1476 | { |
1348 | } |
1477 | } |
1349 | |
1478 | |
1350 | vpn::~vpn () |
1479 | vpn::~vpn () |
1351 | {} |
1480 | { |
|
|
1481 | } |
1352 | |
1482 | |