… | |
… | |
58 | |
58 | |
59 | static time_t next_timecheck; |
59 | static time_t next_timecheck; |
60 | |
60 | |
61 | #define MAGIC "vped\xbd\xc6\xdb\x82" // 8 bytes of magic |
61 | #define MAGIC "vped\xbd\xc6\xdb\x82" // 8 bytes of magic |
62 | |
62 | |
63 | static const rsachallenge & |
63 | struct crypto_ctx |
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 | { |
64 | { |
71 | RAND_bytes ((unsigned char *)&challenge, sizeof (challenge)); |
65 | EVP_CIPHER_CTX cctx; |
72 | challenge_ttl = NOW + CHALLENGE_TTL; |
66 | HMAC_CTX hctx; |
73 | } |
|
|
74 | |
67 | |
75 | return challenge; |
68 | crypto_ctx (const rsachallenge &challenge, int enc); |
76 | } |
69 | ~crypto_ctx (); |
|
|
70 | }; |
77 | |
71 | |
78 | // caching of rsa operations really helps slow computers |
72 | crypto_ctx::crypto_ctx (const rsachallenge &challenge, int enc) |
|
|
73 | { |
|
|
74 | EVP_CIPHER_CTX_init (&cctx); |
|
|
75 | EVP_CipherInit_ex (&cctx, CIPHER, 0, &challenge[CHG_CIPHER_KEY], 0, enc); |
|
|
76 | HMAC_CTX_init (&hctx); |
|
|
77 | HMAC_Init_ex (&hctx, &challenge[CHG_HMAC_KEY], HMAC_KEYLEN, DIGEST, 0); |
|
|
78 | } |
|
|
79 | |
|
|
80 | crypto_ctx::~crypto_ctx () |
|
|
81 | { |
|
|
82 | EVP_CIPHER_CTX_cleanup (&cctx); |
|
|
83 | HMAC_CTX_cleanup (&hctx); |
|
|
84 | } |
|
|
85 | |
|
|
86 | static void |
|
|
87 | rsa_hash (const rsaid &id, const rsachallenge &chg, rsaresponse &h) |
|
|
88 | { |
|
|
89 | EVP_MD_CTX ctx; |
|
|
90 | |
|
|
91 | EVP_MD_CTX_init (&ctx); |
|
|
92 | EVP_DigestInit (&ctx, RSA_HASH); |
|
|
93 | EVP_DigestUpdate(&ctx, &chg, sizeof chg); |
|
|
94 | EVP_DigestUpdate(&ctx, &id, sizeof id); |
|
|
95 | EVP_DigestFinal (&ctx, (unsigned char *)&h, 0); |
|
|
96 | EVP_MD_CTX_cleanup (&ctx); |
|
|
97 | } |
|
|
98 | |
79 | struct rsa_entry { |
99 | struct rsa_entry { |
80 | tstamp expire; |
100 | tstamp expire; |
|
|
101 | rsaid id; |
81 | rsachallenge chg; |
102 | rsachallenge chg; |
82 | RSA *key; // which key |
|
|
83 | rsaencrdata encr; |
|
|
84 | |
|
|
85 | rsa_entry () |
|
|
86 | { |
|
|
87 | expire = NOW + CHALLENGE_TTL; |
|
|
88 | } |
|
|
89 | }; |
103 | }; |
90 | |
104 | |
91 | struct rsa_cache : list<rsa_entry> |
105 | struct rsa_cache : list<rsa_entry> |
92 | { |
106 | { |
93 | void cleaner_cb (tstamp &ts); time_watcher cleaner; |
107 | void cleaner_cb (tstamp &ts); time_watcher cleaner; |
94 | |
108 | |
95 | const rsaencrdata *public_encrypt (RSA *key, const rsachallenge &chg) |
109 | bool find (const rsaid &id, rsachallenge &chg) |
96 | { |
110 | { |
97 | for (iterator i = begin (); i != end (); ++i) |
111 | for (iterator i = begin (); i != end (); ++i) |
98 | { |
112 | { |
99 | if (i->key == key && !memcmp (&chg, &i->chg, sizeof chg)) |
113 | if (!memcmp (&id, &i->id, sizeof id) && i->expire > NOW) |
100 | return &i->encr; |
114 | { |
|
|
115 | memcpy (&chg, &i->chg, sizeof chg); |
|
|
116 | |
|
|
117 | erase (i); |
|
|
118 | return true; |
|
|
119 | } |
101 | } |
120 | } |
102 | |
121 | |
103 | if (cleaner.at < NOW) |
122 | if (cleaner.at < NOW) |
104 | cleaner.start (NOW + CHALLENGE_TTL); |
123 | cleaner.start (NOW + RSA_TTL); |
105 | |
124 | |
106 | resize (size () + 1); |
125 | return false; |
107 | rsa_entry *e = &(*rbegin ()); |
126 | } |
108 | |
127 | |
109 | e->key = key; |
128 | void gen (rsaid &id, rsachallenge &chg) |
|
|
129 | { |
|
|
130 | rsa_entry e; |
|
|
131 | |
|
|
132 | RAND_bytes ((unsigned char *)&id, sizeof id); |
|
|
133 | RAND_bytes ((unsigned char *)&chg, sizeof chg); |
|
|
134 | |
|
|
135 | e.expire = NOW + RSA_TTL; |
|
|
136 | e.id = id; |
110 | memcpy (&e->chg, &chg, sizeof chg); |
137 | memcpy (&e.chg, &chg, sizeof chg); |
111 | |
138 | |
112 | if (0 > RSA_public_encrypt (sizeof chg, |
139 | push_back (e); |
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 | |
140 | |
126 | if (cleaner.at < NOW) |
141 | if (cleaner.at < NOW) |
127 | cleaner.start (NOW + CHALLENGE_TTL); |
142 | cleaner.start (NOW + RSA_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 | } |
143 | } |
145 | |
144 | |
146 | rsa_cache () |
145 | rsa_cache () |
147 | : cleaner (this, &rsa_cache::cleaner_cb) |
146 | : cleaner (this, &rsa_cache::cleaner_cb) |
148 | { } |
147 | { } |
… | |
… | |
153 | { |
152 | { |
154 | if (empty ()) |
153 | if (empty ()) |
155 | ts = TSTAMP_CANCEL; |
154 | ts = TSTAMP_CANCEL; |
156 | else |
155 | else |
157 | { |
156 | { |
158 | ts = NOW + 3; |
157 | ts = NOW + RSA_TTL; |
|
|
158 | |
159 | for (iterator i = begin (); i != end (); ) |
159 | for (iterator i = begin (); i != end (); ) |
160 | { |
|
|
161 | if (i->expire >= NOW) |
160 | if (i->expire <= NOW) |
162 | i = erase (i); |
161 | i = erase (i); |
163 | else |
162 | else |
164 | ++i; |
163 | ++i; |
165 | } |
|
|
166 | } |
164 | } |
167 | } |
165 | } |
168 | |
166 | |
169 | // run a script. yes, it's a template function. yes, c++ |
167 | typedef callback<const char *, int> run_script_cb; |
170 | // is not a functional language. yes, this suxx. |
168 | |
171 | template<class owner> |
169 | // run a shell script (or actually an external program). |
172 | static void |
170 | static void |
173 | run_script (owner *obj, const char *(owner::*setup)(), bool wait) |
171 | run_script (const run_script_cb &cb, bool wait) |
174 | { |
172 | { |
175 | int pid; |
173 | int pid; |
176 | |
174 | |
177 | if ((pid = fork ()) == 0) |
175 | if ((pid = fork ()) == 0) |
178 | { |
176 | { |
179 | char *filename; |
177 | char *filename; |
180 | asprintf (&filename, "%s/%s", confbase, (obj->*setup) ()); |
178 | asprintf (&filename, "%s/%s", confbase, cb(0)); |
181 | execl (filename, filename, (char *) 0); |
179 | execl (filename, filename, (char *) 0); |
182 | exit (255); |
180 | exit (255); |
183 | } |
181 | } |
184 | else if (pid > 0) |
182 | else if (pid > 0) |
185 | { |
183 | { |
… | |
… | |
189 | /* TODO: check status */ |
187 | /* TODO: check status */ |
190 | } |
188 | } |
191 | } |
189 | } |
192 | } |
190 | } |
193 | |
191 | |
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 | ////////////////////////////////////////////////////////////////////////////// |
192 | ////////////////////////////////////////////////////////////////////////////// |
229 | |
193 | |
230 | void pkt_queue::put (tap_packet *p) |
194 | void pkt_queue::put (tap_packet *p) |
231 | { |
195 | { |
232 | if (queue[i]) |
196 | if (queue[i]) |
… | |
… | |
264 | { |
228 | { |
265 | for (i = QUEUEDEPTH; --i > 0; ) |
229 | for (i = QUEUEDEPTH; --i > 0; ) |
266 | delete queue[i]; |
230 | delete queue[i]; |
267 | } |
231 | } |
268 | |
232 | |
|
|
233 | struct net_rateinfo { |
|
|
234 | u32 host; |
|
|
235 | double pcnt, diff; |
|
|
236 | tstamp last; |
|
|
237 | }; |
|
|
238 | |
269 | // only do action once every x seconds per host. |
239 | // only do action once every x seconds per host whole allowing bursts. |
270 | // currently this is quite a slow implementation, |
240 | // this implementation ("splay list" ;) is inefficient, |
271 | // but suffices for normal operation. |
241 | // but low on resources. |
272 | struct u32_rate_limiter : private map<u32, tstamp> |
242 | struct net_rate_limiter : private list<net_rateinfo> |
273 | { |
243 | { |
274 | tstamp every; |
244 | static const double ALPHA = 1. - 1. / 90.; // allow bursts |
|
|
245 | static const double CUTOFF = 20.; // one event every CUTOFF seconds |
|
|
246 | static const double EXPIRE = CUTOFF * 30.; // expire entries after this time |
275 | |
247 | |
276 | bool can (u32 host); |
248 | 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); } |
249 | bool can (SOCKADDR *sa) { return can((u32)sa->sin_addr.s_addr); } |
287 | bool can (sockinfo &si) { return u32_rate_limiter::can((u32)si.host); } |
250 | bool can (sockinfo &si) { return can((u32)si.host); } |
|
|
251 | }; |
288 | |
252 | |
289 | net_rate_limiter (tstamp every) : u32_rate_limiter (every) {} |
253 | net_rate_limiter auth_rate_limiter, reset_rate_limiter; |
290 | }; |
|
|
291 | |
254 | |
292 | bool u32_rate_limiter::can (u32 host) |
255 | bool net_rate_limiter::can (u32 host) |
293 | { |
256 | { |
294 | iterator i; |
257 | iterator i; |
295 | |
258 | |
296 | for (i = begin (); i != end (); ) |
259 | for (i = begin (); i != end (); ) |
297 | if (i->second <= NOW) |
260 | if (i->host == host) |
298 | { |
261 | break; |
|
|
262 | else if (i->last < NOW - EXPIRE) |
299 | erase (i); |
263 | i = erase (i); |
300 | i = begin (); |
|
|
301 | } |
|
|
302 | else |
264 | else |
303 | ++i; |
265 | i++; |
304 | |
266 | |
305 | i = find (host); |
|
|
306 | |
|
|
307 | if (i != end ()) |
267 | if (i == end ()) |
308 | return false; |
268 | { |
|
|
269 | net_rateinfo ri; |
309 | |
270 | |
310 | insert (value_type (host, NOW + every)); |
271 | ri.host = host; |
|
|
272 | ri.pcnt = 1.; |
|
|
273 | ri.diff = CUTOFF * (1. / (1. - ALPHA)); |
|
|
274 | ri.last = NOW; |
311 | |
275 | |
|
|
276 | push_front (ri); |
|
|
277 | |
312 | return true; |
278 | return true; |
|
|
279 | } |
|
|
280 | else |
|
|
281 | { |
|
|
282 | net_rateinfo ri (*i); |
|
|
283 | erase (i); |
|
|
284 | |
|
|
285 | ri.pcnt = ri.pcnt * ALPHA; |
|
|
286 | ri.diff = ri.diff * ALPHA + (NOW - ri.last); |
|
|
287 | |
|
|
288 | ri.last = NOW; |
|
|
289 | |
|
|
290 | bool send = ri.diff / ri.pcnt > CUTOFF; |
|
|
291 | |
|
|
292 | if (send) |
|
|
293 | ri.pcnt++; |
|
|
294 | |
|
|
295 | push_front (ri); |
|
|
296 | |
|
|
297 | return send; |
|
|
298 | } |
313 | } |
299 | } |
314 | |
300 | |
315 | ///////////////////////////////////////////////////////////////////////////// |
301 | ///////////////////////////////////////////////////////////////////////////// |
316 | |
302 | |
317 | static void next_wakeup (time_t next) |
303 | static void next_wakeup (time_t next) |
… | |
… | |
321 | } |
307 | } |
322 | |
308 | |
323 | static unsigned char hmac_digest[EVP_MAX_MD_SIZE]; |
309 | static unsigned char hmac_digest[EVP_MAX_MD_SIZE]; |
324 | |
310 | |
325 | struct hmac_packet:net_packet |
311 | struct hmac_packet:net_packet |
|
|
312 | { |
|
|
313 | u8 hmac[HMACLENGTH]; // each and every packet has a hmac field, but that is not (yet) checked everywhere |
|
|
314 | |
|
|
315 | void hmac_set (crypto_ctx * ctx); |
|
|
316 | bool hmac_chk (crypto_ctx * ctx); |
|
|
317 | |
|
|
318 | private: |
|
|
319 | void hmac_gen (crypto_ctx * ctx) |
326 | { |
320 | { |
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; |
321 | unsigned int xlen; |
336 | HMAC_CTX *hctx = &ctx->hctx; |
322 | HMAC_CTX *hctx = &ctx->hctx; |
337 | |
323 | |
338 | HMAC_Init_ex (hctx, 0, 0, 0, 0); |
324 | HMAC_Init_ex (hctx, 0, 0, 0, 0); |
339 | HMAC_Update (hctx, ((unsigned char *) this) + sizeof (hmac_packet), |
325 | HMAC_Update (hctx, ((unsigned char *) this) + sizeof (hmac_packet), |
340 | len - sizeof (hmac_packet)); |
326 | len - sizeof (hmac_packet)); |
341 | HMAC_Final (hctx, (unsigned char *) &hmac_digest, &xlen); |
327 | HMAC_Final (hctx, (unsigned char *) &hmac_digest, &xlen); |
342 | } |
|
|
343 | }; |
328 | } |
|
|
329 | }; |
344 | |
330 | |
345 | void |
331 | void |
346 | hmac_packet::hmac_set (crypto_ctx * ctx) |
332 | hmac_packet::hmac_set (crypto_ctx * ctx) |
347 | { |
333 | { |
348 | hmac_gen (ctx); |
334 | hmac_gen (ctx); |
… | |
… | |
364 | { |
350 | { |
365 | PT_RESET = 0, |
351 | PT_RESET = 0, |
366 | PT_DATA_UNCOMPRESSED, |
352 | PT_DATA_UNCOMPRESSED, |
367 | PT_DATA_COMPRESSED, |
353 | PT_DATA_COMPRESSED, |
368 | PT_PING, PT_PONG, // wasting namespace space? ;) |
354 | PT_PING, PT_PONG, // wasting namespace space? ;) |
369 | PT_AUTH, // authentification |
355 | PT_AUTH_REQ, // authentification request |
|
|
356 | PT_AUTH_RES, // authentification response |
370 | PT_CONNECT_REQ, // want other host to contact me |
357 | PT_CONNECT_REQ, // want other host to contact me |
371 | PT_CONNECT_INFO, // request connection to some node |
358 | PT_CONNECT_INFO, // request connection to some node |
372 | PT_REKEY, // rekeying (not yet implemented) |
|
|
373 | PT_MAX |
359 | PT_MAX |
374 | }; |
360 | }; |
375 | |
361 | |
376 | u8 type; |
362 | u8 type; |
377 | u8 srcdst, src1, dst1; |
363 | u8 srcdst, src1, dst1; |
… | |
… | |
445 | } |
431 | } |
446 | #endif |
432 | #endif |
447 | |
433 | |
448 | EVP_EncryptInit_ex (cctx, 0, 0, 0, 0); |
434 | EVP_EncryptInit_ex (cctx, 0, 0, 0, 0); |
449 | |
435 | |
|
|
436 | struct { |
450 | #if RAND_SIZE |
437 | #if RAND_SIZE |
451 | struct { |
|
|
452 | u8 rnd[RAND_SIZE]; |
438 | u8 rnd[RAND_SIZE]; |
|
|
439 | #endif |
453 | u32 seqno; |
440 | u32 seqno; |
454 | } datahdr; |
441 | } datahdr; |
455 | |
442 | |
456 | datahdr.seqno = seqno; |
443 | datahdr.seqno = ntohl (seqno); |
|
|
444 | #if RAND_SIZE |
457 | RAND_pseudo_bytes ((unsigned char *) datahdr.rnd, RAND_SIZE); |
445 | RAND_pseudo_bytes ((unsigned char *) datahdr.rnd, RAND_SIZE); |
|
|
446 | #endif |
458 | |
447 | |
459 | EVP_EncryptUpdate (cctx, |
448 | EVP_EncryptUpdate (cctx, |
460 | (unsigned char *) data + outl, &outl2, |
449 | (unsigned char *) data + outl, &outl2, |
461 | (unsigned char *) &datahdr, DATAHDR); |
450 | (unsigned char *) &datahdr, DATAHDR); |
462 | outl += outl2; |
451 | 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 | |
452 | |
470 | EVP_EncryptUpdate (cctx, |
453 | EVP_EncryptUpdate (cctx, |
471 | (unsigned char *) data + outl, &outl2, |
454 | (unsigned char *) data + outl, &outl2, |
472 | (unsigned char *) d, l); |
455 | (unsigned char *) d, l); |
473 | outl += outl2; |
456 | outl += outl2; |
… | |
… | |
509 | outl += outl2; |
492 | outl += outl2; |
510 | |
493 | |
511 | EVP_DecryptFinal_ex (cctx, (unsigned char *)d + outl, &outl2); |
494 | EVP_DecryptFinal_ex (cctx, (unsigned char *)d + outl, &outl2); |
512 | outl += outl2; |
495 | outl += outl2; |
513 | |
496 | |
514 | seqno = *(u32 *)(d + RAND_SIZE); |
497 | seqno = ntohl (*(u32 *)(d + RAND_SIZE)); |
515 | |
498 | |
516 | id2mac (dst () ? dst() : THISNODE->id, p->dst); |
499 | id2mac (dst () ? dst() : THISNODE->id, p->dst); |
517 | id2mac (src (), p->src); |
500 | id2mac (src (), p->src); |
518 | |
501 | |
519 | #if ENABLE_COMPRESSION |
502 | #if ENABLE_COMPRESSION |
520 | if (type == PT_DATA_COMPRESSED) |
503 | if (type == PT_DATA_COMPRESSED) |
521 | { |
504 | { |
522 | u32 cl = (d[DATAHDR] << 8) | d[DATAHDR + 1]; |
505 | u32 cl = (d[DATAHDR] << 8) | d[DATAHDR + 1]; |
|
|
506 | |
523 | p->len = lzf_decompress (d + DATAHDR + 2, cl, &(*p)[6 + 6], MAX_MTU) + 6 + 6; |
507 | p->len = lzf_decompress (d + DATAHDR + 2, cl < MAX_MTU ? cl : 0, |
|
|
508 | &(*p)[6 + 6], MAX_MTU) |
|
|
509 | + 6 + 6; |
524 | } |
510 | } |
525 | else |
511 | else |
526 | p->len = outl + (6 + 6 - DATAHDR); |
512 | p->len = outl + (6 + 6 - DATAHDR); |
527 | #endif |
513 | #endif |
528 | |
514 | |
529 | return p; |
515 | return p; |
530 | } |
516 | } |
531 | |
517 | |
532 | struct ping_packet : vpn_packet |
518 | struct ping_packet : vpn_packet |
|
|
519 | { |
|
|
520 | void setup (int dst, ptype type) |
533 | { |
521 | { |
534 | void setup (int dst, ptype type) |
|
|
535 | { |
|
|
536 | set_hdr (type, dst); |
522 | set_hdr (type, dst); |
537 | len = sizeof (*this) - sizeof (net_packet); |
523 | len = sizeof (*this) - sizeof (net_packet); |
538 | } |
|
|
539 | }; |
524 | } |
|
|
525 | }; |
540 | |
526 | |
541 | struct config_packet : vpn_packet |
527 | struct config_packet : vpn_packet |
|
|
528 | { |
|
|
529 | // actually, hmaclen cannot be checked because the hmac |
|
|
530 | // field comes before this data, so peers with other |
|
|
531 | // hmacs simply will not work. |
|
|
532 | u8 prot_major, prot_minor, randsize, hmaclen; |
|
|
533 | u8 flags, challengelen, pad2, pad3; |
|
|
534 | u32 cipher_nid, digest_nid, hmac_nid; |
|
|
535 | |
|
|
536 | const u8 curflags () const |
542 | { |
537 | { |
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 |
538 | return 0x80 |
554 | | 0x02 |
|
|
555 | #if PROTOCOL_MAJOR != 2 |
|
|
556 | #error hi |
|
|
557 | #endif |
|
|
558 | | (ENABLE_COMPRESSION ? 0x01 : 0x00); |
539 | | (ENABLE_COMPRESSION ? 0x01 : 0x00); |
559 | } |
540 | } |
560 | |
541 | |
561 | void setup (ptype type, int dst) |
542 | 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 | { |
543 | { |
591 | char magic[8]; |
544 | prot_major = PROTOCOL_MAJOR; |
592 | u8 subtype; |
545 | prot_minor = PROTOCOL_MINOR; |
593 | u8 pad1, pad2; |
546 | randsize = RAND_SIZE; |
594 | rsaencrdata challenge; |
547 | hmaclen = HMACLENGTH; |
|
|
548 | flags = curflags (); |
|
|
549 | challengelen = sizeof (rsachallenge); |
595 | |
550 | |
596 | auth_packet (int dst, auth_subtype stype) |
551 | cipher_nid = htonl (EVP_CIPHER_nid (CIPHER)); |
597 | { |
552 | digest_nid = htonl (EVP_MD_type (RSA_HASH)); |
598 | config_packet::setup (PT_AUTH, dst); |
553 | hmac_nid = htonl (EVP_MD_type (DIGEST)); |
599 | subtype = stype; |
554 | |
600 | len = sizeof (*this) - sizeof (net_packet); |
555 | len = sizeof (*this) - sizeof (net_packet); |
|
|
556 | set_hdr (type, dst); |
|
|
557 | } |
|
|
558 | |
|
|
559 | bool chk_config () |
|
|
560 | { |
|
|
561 | return prot_major == PROTOCOL_MAJOR |
|
|
562 | && randsize == RAND_SIZE |
|
|
563 | && hmaclen == HMACLENGTH |
|
|
564 | && flags == curflags () |
|
|
565 | && challengelen == sizeof (rsachallenge) |
|
|
566 | && cipher_nid == htonl (EVP_CIPHER_nid (CIPHER)) |
|
|
567 | && digest_nid == htonl (EVP_MD_type (RSA_HASH)) |
|
|
568 | && hmac_nid == htonl (EVP_MD_type (DIGEST)); |
|
|
569 | } |
|
|
570 | }; |
|
|
571 | |
|
|
572 | struct auth_req_packet : config_packet |
|
|
573 | { |
|
|
574 | char magic[8]; |
|
|
575 | u8 initiate; // false if this is just an automatic reply |
|
|
576 | u8 pad1, pad2, pad3; |
|
|
577 | rsaid id; |
|
|
578 | rsaencrdata encr; |
|
|
579 | |
|
|
580 | auth_req_packet (int dst, bool initiate_) |
|
|
581 | { |
|
|
582 | config_packet::setup (PT_AUTH_REQ, dst); |
|
|
583 | initiate = !!initiate_; |
601 | strncpy (magic, MAGIC, 8); |
584 | strncpy (magic, MAGIC, 8); |
602 | } |
585 | len = sizeof (*this) - sizeof (net_packet); |
603 | }; |
586 | } |
|
|
587 | }; |
|
|
588 | |
|
|
589 | struct auth_res_packet : config_packet |
|
|
590 | { |
|
|
591 | rsaid id; |
|
|
592 | u8 pad1, pad2, pad3; |
|
|
593 | u8 response_len; // encrypted length |
|
|
594 | rsaresponse response; |
|
|
595 | |
|
|
596 | auth_res_packet (int dst) |
|
|
597 | { |
|
|
598 | config_packet::setup (PT_AUTH_RES, dst); |
|
|
599 | len = sizeof (*this) - sizeof (net_packet); |
|
|
600 | } |
|
|
601 | }; |
604 | |
602 | |
605 | struct connect_req_packet : vpn_packet |
603 | struct connect_req_packet : vpn_packet |
|
|
604 | { |
|
|
605 | u8 id; |
|
|
606 | u8 pad1, pad2, pad3; |
|
|
607 | |
|
|
608 | connect_req_packet (int dst, int id_) |
606 | { |
609 | { |
607 | u8 id; |
610 | id = 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); |
611 | set_hdr (PT_CONNECT_REQ, dst); |
614 | len = sizeof (*this) - sizeof (net_packet); |
612 | len = sizeof (*this) - sizeof (net_packet); |
615 | } |
|
|
616 | }; |
613 | } |
|
|
614 | }; |
617 | |
615 | |
618 | struct connect_info_packet : vpn_packet |
616 | struct connect_info_packet : vpn_packet |
|
|
617 | { |
|
|
618 | u8 id; |
|
|
619 | u8 pad1, pad2, pad3; |
|
|
620 | sockinfo si; |
|
|
621 | |
|
|
622 | connect_info_packet (int dst, int id_, sockinfo &si_) |
619 | { |
623 | { |
620 | u8 id; |
624 | id = id_; |
621 | u8 pad1, pad2, pad3; |
625 | si = si_; |
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); |
626 | set_hdr (PT_CONNECT_INFO, dst); |
629 | len = sizeof (*this) - sizeof (net_packet); |
627 | len = sizeof (*this) - sizeof (net_packet); |
630 | } |
|
|
631 | }; |
628 | } |
|
|
629 | }; |
632 | |
630 | |
633 | ///////////////////////////////////////////////////////////////////////////// |
631 | ///////////////////////////////////////////////////////////////////////////// |
634 | |
632 | |
635 | void |
633 | void |
636 | fill_sa (SOCKADDR *sa, conf_node *conf) |
634 | fill_sa (SOCKADDR *sa, conf_node *conf) |
… | |
… | |
672 | } |
670 | } |
673 | |
671 | |
674 | void |
672 | void |
675 | connection::send_reset (SOCKADDR *dsa) |
673 | connection::send_reset (SOCKADDR *dsa) |
676 | { |
674 | { |
677 | static net_rate_limiter limiter(1); |
|
|
678 | |
|
|
679 | if (limiter.can (dsa) && connectmode != conf_node::C_DISABLED) |
675 | if (reset_rate_limiter.can (dsa) && connectmode != conf_node::C_DISABLED) |
680 | { |
676 | { |
681 | config_packet *pkt = new config_packet; |
677 | config_packet *pkt = new config_packet; |
682 | |
678 | |
683 | pkt->setup (vpn_packet::PT_RESET, conf->id); |
679 | pkt->setup (vpn_packet::PT_RESET, conf->id); |
684 | vpn->send_vpn_packet (pkt, dsa, IPTOS_MINCOST); |
680 | vpn->send_vpn_packet (pkt, dsa, IPTOS_MINCOST); |
685 | |
681 | |
686 | delete pkt; |
682 | delete pkt; |
687 | } |
683 | } |
688 | } |
684 | } |
689 | |
685 | |
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 |
686 | void |
703 | connection::send_auth (auth_subtype subtype, SOCKADDR *sa, const rsachallenge *k) |
687 | connection::send_auth_request (SOCKADDR *sa, bool initiate) |
704 | { |
688 | { |
705 | static net_rate_limiter limiter(0.2); |
689 | if (!initiate || auth_rate_limiter.can (sa)) |
706 | |
|
|
707 | if (subtype != AUTH_INIT || limiter.can (sa)) |
|
|
708 | { |
690 | { |
709 | if (!k) |
|
|
710 | k = gen_challenge (seqrand, sa); |
|
|
711 | |
|
|
712 | auth_packet *pkt = new auth_packet (conf->id, subtype); |
691 | auth_req_packet *pkt = new auth_req_packet (conf->id, initiate); |
713 | |
692 | |
714 | memcpy (pkt->challenge, rsa_cache.public_encrypt (conf->rsa_key, *k), sizeof (rsaencrdata)); |
693 | rsachallenge chg; |
715 | |
694 | |
|
|
695 | rsa_cache.gen (pkt->id, chg); |
|
|
696 | |
|
|
697 | if (0 > RSA_public_encrypt (sizeof chg, |
|
|
698 | (unsigned char *)&chg, (unsigned char *)&pkt->encr, |
|
|
699 | conf->rsa_key, RSA_PKCS1_OAEP_PADDING)) |
|
|
700 | fatal ("RSA_public_encrypt error"); |
|
|
701 | |
716 | slog (L_TRACE, ">>%d PT_AUTH(%d) [%s]", conf->id, subtype, (const char *)sockinfo (sa)); |
702 | slog (L_TRACE, ">>%d PT_AUTH_REQ [%s]", conf->id, (const char *)sockinfo (sa)); |
717 | |
703 | |
718 | vpn->send_vpn_packet (pkt, sa, IPTOS_RELIABILITY); |
704 | vpn->send_vpn_packet (pkt, sa, IPTOS_RELIABILITY); // rsa is very very costly |
719 | |
705 | |
720 | delete pkt; |
706 | delete pkt; |
721 | } |
707 | } |
722 | } |
708 | } |
723 | |
709 | |
724 | void |
710 | void |
|
|
711 | connection::send_auth_response (SOCKADDR *sa, const rsaid &id, const rsachallenge &chg) |
|
|
712 | { |
|
|
713 | auth_res_packet *pkt = new auth_res_packet (conf->id); |
|
|
714 | |
|
|
715 | pkt->id = id; |
|
|
716 | |
|
|
717 | rsa_hash (id, chg, pkt->response); |
|
|
718 | |
|
|
719 | pkt->hmac_set (octx); |
|
|
720 | |
|
|
721 | slog (L_TRACE, ">>%d PT_AUTH_RES [%s]", conf->id, (const char *)sockinfo (sa)); |
|
|
722 | |
|
|
723 | vpn->send_vpn_packet (pkt, sa, IPTOS_RELIABILITY); // rsa is very very costly |
|
|
724 | |
|
|
725 | delete pkt; |
|
|
726 | } |
|
|
727 | |
|
|
728 | void |
725 | connection::establish_connection_cb (tstamp &ts) |
729 | connection::establish_connection_cb (tstamp &ts) |
726 | { |
730 | { |
727 | if (ictx || conf == THISNODE || connectmode == conf_node::C_NEVER) |
731 | if (ictx || conf == THISNODE |
|
|
732 | || connectmode == conf_node::C_NEVER |
|
|
733 | || connectmode == conf_node::C_DISABLED) |
728 | ts = TSTAMP_CANCEL; |
734 | ts = TSTAMP_CANCEL; |
729 | else if (ts <= NOW) |
735 | else if (ts <= NOW) |
730 | { |
736 | { |
731 | double retry_int = double (retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2)) * 0.25; |
737 | double retry_int = double (retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2)) * 0.6; |
732 | |
738 | |
733 | if (retry_int < 3600 * 8) |
739 | if (retry_int < 3600 * 8) |
734 | retry_cnt++; |
740 | retry_cnt++; |
735 | |
|
|
736 | if (connectmode == conf_node::C_ONDEMAND |
|
|
737 | && retry_int > ::conf.keepalive) |
|
|
738 | retry_int = ::conf.keepalive; |
|
|
739 | |
741 | |
740 | ts = NOW + retry_int; |
742 | ts = NOW + retry_int; |
741 | |
743 | |
742 | if (conf->hostname) |
744 | if (conf->hostname) |
743 | { |
745 | { |
744 | reset_dstaddr (); |
746 | reset_dstaddr (); |
745 | if (sa.sin_addr.s_addr) |
747 | if (sa.sin_addr.s_addr) |
746 | if (retry_cnt < 4) |
748 | if (retry_cnt < 4) |
747 | send_auth (AUTH_INIT, &sa); |
749 | send_auth_request (&sa, true); |
748 | else |
750 | else if (auth_rate_limiter.can (&sa)) |
749 | send_ping (&sa, 0); |
751 | send_ping (&sa, 0); |
750 | } |
752 | } |
751 | else |
753 | else |
752 | vpn->connect_request (conf->id); |
754 | vpn->connect_request (conf->id); |
753 | } |
755 | } |
… | |
… | |
756 | void |
758 | void |
757 | connection::reset_connection () |
759 | connection::reset_connection () |
758 | { |
760 | { |
759 | if (ictx && octx) |
761 | if (ictx && octx) |
760 | { |
762 | { |
761 | slog (L_INFO, _("connection to %d (%s) lost"), conf->id, conf->nodename); |
763 | slog (L_INFO, _("%s(%s): connection lost"), |
|
|
764 | conf->nodename, (const char *)sockinfo (sa)); |
762 | |
765 | |
763 | if (::conf.script_node_down) |
766 | if (::conf.script_node_down) |
764 | run_script (this, &connection::script_node_down, false); |
767 | run_script (run_script_cb (this, &connection::script_node_down), false); |
765 | } |
768 | } |
766 | |
769 | |
767 | delete ictx; ictx = 0; |
770 | delete ictx; ictx = 0; |
768 | delete octx; octx = 0; |
771 | delete octx; octx = 0; |
769 | |
|
|
770 | RAND_bytes ((unsigned char *)&seqrand, sizeof (u32)); |
|
|
771 | |
772 | |
772 | sa.sin_port = 0; |
773 | sa.sin_port = 0; |
773 | sa.sin_addr.s_addr = 0; |
774 | sa.sin_addr.s_addr = 0; |
774 | |
775 | |
775 | last_activity = 0; |
776 | last_activity = 0; |
… | |
… | |
840 | conf->id, pkt->typ (), pkt->src (), pkt->dst ()); |
841 | conf->id, pkt->typ (), pkt->src (), pkt->dst ()); |
841 | |
842 | |
842 | switch (pkt->typ ()) |
843 | switch (pkt->typ ()) |
843 | { |
844 | { |
844 | case vpn_packet::PT_PING: |
845 | 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, |
846 | // we send pings instead of auth packets after some retries, |
850 | // so reset the retry counter and establish a conenction |
847 | // so reset the retry counter and establish a conenction |
851 | // when we receive a pong. |
848 | // when we receive a pong. |
852 | if (!ictx && !octx) |
849 | if (!ictx && !octx) |
853 | { |
850 | { |
854 | retry_cnt = 0; |
851 | retry_cnt = 0; |
855 | establish_connection.at = 0; |
852 | establish_connection.at = 0; |
856 | establish_connection (); |
853 | establish_connection (); |
857 | } |
854 | } |
|
|
855 | else |
|
|
856 | send_ping (ssa, 1); // pong |
858 | |
857 | |
|
|
858 | break; |
|
|
859 | |
|
|
860 | case vpn_packet::PT_PONG: |
859 | break; |
861 | break; |
860 | |
862 | |
861 | case vpn_packet::PT_RESET: |
863 | case vpn_packet::PT_RESET: |
862 | { |
864 | { |
863 | reset_connection (); |
865 | reset_connection (); |
864 | |
866 | |
865 | config_packet *p = (config_packet *) pkt; |
867 | config_packet *p = (config_packet *) pkt; |
|
|
868 | |
866 | if (!p->chk_config ()) |
869 | if (!p->chk_config ()) |
867 | { |
870 | { |
868 | slog (L_WARN, _("protocol mismatch, disabling node '%s'"), conf->nodename); |
871 | slog (L_WARN, _("%s(%s): protocol mismatch, disabling node"), |
|
|
872 | conf->nodename, (const char *)sockinfo (ssa)); |
869 | connectmode = conf_node::C_DISABLED; |
873 | connectmode = conf_node::C_DISABLED; |
870 | } |
874 | } |
871 | else if (connectmode == conf_node::C_ALWAYS) |
875 | else if (connectmode == conf_node::C_ALWAYS) |
872 | establish_connection (); |
876 | establish_connection (); |
873 | } |
877 | } |
874 | break; |
878 | break; |
875 | |
879 | |
876 | case vpn_packet::PT_AUTH: |
880 | case vpn_packet::PT_AUTH_REQ: |
|
|
881 | if (auth_rate_limiter.can (ssa)) |
|
|
882 | { |
|
|
883 | auth_req_packet *p = (auth_req_packet *) pkt; |
|
|
884 | |
|
|
885 | slog (L_TRACE, "<<%d PT_AUTH_REQ(%d)", conf->id, p->initiate); |
|
|
886 | |
|
|
887 | if (p->chk_config () && !strncmp (p->magic, MAGIC, 8)) |
|
|
888 | { |
|
|
889 | if (p->prot_minor != PROTOCOL_MINOR) |
|
|
890 | slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), |
|
|
891 | conf->nodename, (const char *)sockinfo (ssa), |
|
|
892 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
|
|
893 | |
|
|
894 | if (p->initiate) |
|
|
895 | send_auth_request (ssa, false); |
|
|
896 | |
|
|
897 | rsachallenge k; |
|
|
898 | |
|
|
899 | if (0 > RSA_private_decrypt (sizeof (p->encr), |
|
|
900 | (unsigned char *)&p->encr, (unsigned char *)&k, |
|
|
901 | ::conf.rsa_key, RSA_PKCS1_OAEP_PADDING)) |
|
|
902 | slog (L_ERR, _("%s(%s): challenge illegal or corrupted"), |
|
|
903 | conf->nodename, (const char *)sockinfo (ssa)); |
|
|
904 | else |
|
|
905 | { |
|
|
906 | retry_cnt = 0; |
|
|
907 | establish_connection.set (NOW + 8); //? ;) |
|
|
908 | keepalive.reset (); |
|
|
909 | rekey.reset (); |
|
|
910 | |
|
|
911 | delete ictx; |
|
|
912 | ictx = 0; |
|
|
913 | |
|
|
914 | delete octx; |
|
|
915 | |
|
|
916 | octx = new crypto_ctx (k, 1); |
|
|
917 | oseqno = ntohl (*(u32 *)&k[CHG_SEQNO]) & 0x7fffffff; |
|
|
918 | |
|
|
919 | send_auth_response (ssa, p->id, k); |
|
|
920 | |
|
|
921 | break; |
|
|
922 | } |
|
|
923 | } |
|
|
924 | |
|
|
925 | send_reset (ssa); |
|
|
926 | } |
|
|
927 | |
|
|
928 | break; |
|
|
929 | |
|
|
930 | case vpn_packet::PT_AUTH_RES: |
877 | { |
931 | { |
878 | auth_packet *p = (auth_packet *) pkt; |
932 | auth_res_packet *p = (auth_res_packet *) pkt; |
879 | |
933 | |
880 | slog (L_TRACE, "<<%d PT_AUTH(%d)", conf->id, p->subtype); |
934 | slog (L_TRACE, "<<%d PT_AUTH_RES", conf->id); |
881 | |
935 | |
882 | if (p->chk_config () |
936 | if (p->chk_config ()) |
883 | && !strncmp (p->magic, MAGIC, 8)) |
|
|
884 | { |
937 | { |
885 | if (p->prot_minor != PROTOCOL_MINOR) |
938 | if (p->prot_minor != PROTOCOL_MINOR) |
886 | slog (L_INFO, _("protocol minor version mismatch: ours is %d, %s's is %d."), |
939 | slog (L_INFO, _("%s(%s): protocol minor version mismatch: ours is %d, %s's is %d."), |
|
|
940 | conf->nodename, (const char *)sockinfo (ssa), |
887 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
941 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
888 | |
942 | |
889 | if (p->subtype == AUTH_INIT) |
943 | rsachallenge chg; |
890 | send_auth (AUTH_INITREPLY, ssa); |
|
|
891 | |
944 | |
892 | const rsachallenge *k = rsa_cache.private_decrypt (::conf.rsa_key, p->challenge); |
945 | if (!rsa_cache.find (p->id, chg)) |
893 | |
946 | slog (L_ERR, _("%s(%s): unrequested auth response"), |
894 | if (!k) |
947 | conf->nodename, (const char *)sockinfo (ssa)); |
|
|
948 | else |
895 | { |
949 | { |
896 | slog (L_ERR, _("challenge from %s (%s) illegal or corrupted"), |
950 | crypto_ctx *cctx = new crypto_ctx (chg, 0); |
|
|
951 | |
|
|
952 | if (!p->hmac_chk (cctx)) |
|
|
953 | slog (L_ERR, _("%s(%s): hmac authentication error on auth response, received invalid packet\n" |
|
|
954 | "could be an attack, or just corruption or an synchronization error"), |
897 | conf->nodename, (const char *)sockinfo (ssa)); |
955 | conf->nodename, (const char *)sockinfo (ssa)); |
898 | break; |
956 | else |
899 | } |
|
|
900 | |
|
|
901 | retry_cnt = 0; |
|
|
902 | establish_connection.set (NOW + 8); //? ;) |
|
|
903 | keepalive.reset (); |
|
|
904 | rekey.reset (); |
|
|
905 | |
|
|
906 | switch (p->subtype) |
|
|
907 | { |
|
|
908 | case AUTH_INIT: |
|
|
909 | case AUTH_INITREPLY: |
|
|
910 | delete ictx; |
|
|
911 | ictx = 0; |
|
|
912 | |
|
|
913 | delete octx; |
|
|
914 | |
|
|
915 | octx = new crypto_ctx (*k, 1); |
|
|
916 | oseqno = *(u32 *)&k[CHG_SEQNO] & 0x7fffffff; |
|
|
917 | |
|
|
918 | send_auth (AUTH_REPLY, ssa, k); |
|
|
919 | break; |
|
|
920 | |
|
|
921 | case AUTH_REPLY: |
|
|
922 | |
|
|
923 | if (!memcmp ((u8 *)gen_challenge (seqrand, ssa), (u8 *)k, sizeof (rsachallenge))) |
|
|
924 | { |
957 | { |
925 | delete ictx; |
|
|
926 | |
|
|
927 | ictx = new crypto_ctx (*k, 0); |
|
|
928 | iseqno.reset (*(u32 *)&k[CHG_SEQNO] & 0x7fffffff); // at least 2**31 sequence numbers are valid |
|
|
929 | |
|
|
930 | sa = *ssa; |
958 | rsaresponse h; |
931 | |
959 | |
932 | rekey.set (NOW + ::conf.rekey); |
960 | rsa_hash (p->id, chg, h); |
933 | keepalive.set (NOW + ::conf.keepalive); |
|
|
934 | |
961 | |
935 | // send queued packets |
962 | if (!memcmp ((u8 *)&h, (u8 *)p->response, sizeof h)) |
936 | while (tap_packet *p = queue.get ()) |
|
|
937 | { |
963 | { |
|
|
964 | prot_minor = p->prot_minor; |
|
|
965 | |
|
|
966 | delete ictx; ictx = cctx; |
|
|
967 | |
|
|
968 | iseqno.reset (ntohl (*(u32 *)&chg[CHG_SEQNO]) & 0x7fffffff); // at least 2**31 sequence numbers are valid |
|
|
969 | |
|
|
970 | sa = *ssa; |
|
|
971 | |
|
|
972 | rekey.set (NOW + ::conf.rekey); |
|
|
973 | keepalive.set (NOW + ::conf.keepalive); |
|
|
974 | |
|
|
975 | // send queued packets |
|
|
976 | while (tap_packet *p = queue.get ()) |
|
|
977 | { |
938 | send_data_packet (p); |
978 | send_data_packet (p); |
939 | delete p; |
979 | delete p; |
|
|
980 | } |
|
|
981 | |
|
|
982 | connectmode = conf->connectmode; |
|
|
983 | |
|
|
984 | slog (L_INFO, _("%s(%s): connection established, protocol version %d.%d"), |
|
|
985 | conf->nodename, (const char *)sockinfo (ssa), |
|
|
986 | p->prot_major, p->prot_minor); |
|
|
987 | |
|
|
988 | if (::conf.script_node_up) |
|
|
989 | run_script (run_script_cb (this, &connection::script_node_up), false); |
|
|
990 | |
|
|
991 | break; |
940 | } |
992 | } |
941 | |
993 | else |
942 | connectmode = conf->connectmode; |
994 | slog (L_ERR, _("%s(%s): sent and received challenge do not match"), |
943 | |
|
|
944 | slog (L_INFO, _("connection to %d (%s %s) established"), |
|
|
945 | conf->id, conf->nodename, (const char *)sockinfo (ssa)); |
995 | conf->nodename, (const char *)sockinfo (ssa)); |
946 | |
|
|
947 | if (::conf.script_node_up) |
|
|
948 | run_script (this, &connection::script_node_up, false); |
|
|
949 | } |
996 | } |
|
|
997 | |
950 | else |
998 | delete cctx; |
951 | slog (L_ERR, _("sent and received challenge do not match with (%s %s))"), |
|
|
952 | conf->nodename, (const char *)sockinfo (ssa)); |
|
|
953 | |
|
|
954 | break; |
|
|
955 | default: |
|
|
956 | slog (L_ERR, _("authentification illegal subtype error (%s %s)"), |
|
|
957 | conf->nodename, (const char *)sockinfo (ssa)); |
|
|
958 | break; |
|
|
959 | } |
999 | } |
960 | } |
1000 | } |
961 | else |
|
|
962 | send_reset (ssa); |
|
|
963 | |
|
|
964 | break; |
|
|
965 | } |
1001 | } |
|
|
1002 | |
|
|
1003 | send_reset (ssa); |
|
|
1004 | break; |
966 | |
1005 | |
967 | case vpn_packet::PT_DATA_COMPRESSED: |
1006 | case vpn_packet::PT_DATA_COMPRESSED: |
968 | #if !ENABLE_COMPRESSION |
1007 | #if !ENABLE_COMPRESSION |
969 | send_reset (ssa); |
1008 | send_reset (ssa); |
970 | break; |
1009 | break; |
971 | #endif |
1010 | #endif |
|
|
1011 | |
972 | case vpn_packet::PT_DATA_UNCOMPRESSED: |
1012 | case vpn_packet::PT_DATA_UNCOMPRESSED: |
973 | |
1013 | |
974 | if (ictx && octx) |
1014 | if (ictx && octx) |
975 | { |
1015 | { |
976 | vpndata_packet *p = (vpndata_packet *)pkt; |
1016 | vpndata_packet *p = (vpndata_packet *)pkt; |
977 | |
1017 | |
978 | if (*ssa == sa) |
1018 | if (*ssa == sa) |
979 | { |
1019 | { |
980 | if (!p->hmac_chk (ictx)) |
1020 | if (!p->hmac_chk (ictx)) |
981 | slog (L_ERR, _("hmac authentication error, received invalid packet\n" |
1021 | slog (L_ERR, _("%s(%s): hmac authentication error, received invalid packet\n" |
982 | "could be an attack, or just corruption or an synchronization error")); |
1022 | "could be an attack, or just corruption or an synchronization error"), |
|
|
1023 | conf->nodename, (const char *)sockinfo (ssa)); |
983 | else |
1024 | else |
984 | { |
1025 | { |
985 | u32 seqno; |
1026 | u32 seqno; |
986 | tap_packet *d = p->unpack (this, seqno); |
1027 | tap_packet *d = p->unpack (this, seqno); |
987 | |
1028 | |
… | |
… | |
1069 | connection *c = vpn->conns[p->id - 1]; |
1110 | connection *c = vpn->conns[p->id - 1]; |
1070 | |
1111 | |
1071 | slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", |
1112 | slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", |
1072 | conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx); |
1113 | conf->id, p->id, (const char *)p->si, !c->ictx && !c->octx); |
1073 | |
1114 | |
1074 | c->send_auth (AUTH_INIT, p->si.sa ()); |
1115 | c->send_auth_request (p->si.sa (), true); |
1075 | } |
1116 | } |
|
|
1117 | |
1076 | break; |
1118 | break; |
1077 | |
1119 | |
1078 | default: |
1120 | default: |
1079 | send_reset (ssa); |
1121 | send_reset (ssa); |
1080 | break; |
1122 | break; |
|
|
1123 | |
1081 | } |
1124 | } |
1082 | } |
1125 | } |
1083 | |
1126 | |
1084 | void connection::keepalive_cb (tstamp &ts) |
1127 | void connection::keepalive_cb (tstamp &ts) |
1085 | { |
1128 | { |
… | |
… | |
1112 | delete p; |
1155 | delete p; |
1113 | } |
1156 | } |
1114 | |
1157 | |
1115 | void connection::script_node () |
1158 | void connection::script_node () |
1116 | { |
1159 | { |
1117 | vpn->script_if_up (); |
1160 | vpn->script_if_up (0); |
1118 | |
1161 | |
1119 | char *env; |
1162 | char *env; |
1120 | asprintf (&env, "DESTID=%d", conf->id); |
1163 | asprintf (&env, "DESTID=%d", conf->id); |
1121 | putenv (env); |
1164 | putenv (env); |
1122 | asprintf (&env, "DESTNODE=%s", conf->nodename); |
1165 | asprintf (&env, "DESTNODE=%s", conf->nodename); |
… | |
… | |
1125 | putenv (env); |
1168 | putenv (env); |
1126 | asprintf (&env, "DESTPORT=%d", ntohs (sa.sin_port)); |
1169 | asprintf (&env, "DESTPORT=%d", ntohs (sa.sin_port)); |
1127 | putenv (env); |
1170 | putenv (env); |
1128 | } |
1171 | } |
1129 | |
1172 | |
1130 | const char *connection::script_node_up () |
1173 | const char *connection::script_node_up (int) |
1131 | { |
1174 | { |
1132 | script_node (); |
1175 | script_node (); |
1133 | |
1176 | |
1134 | putenv ("STATE=up"); |
1177 | putenv ("STATE=up"); |
1135 | |
1178 | |
1136 | return ::conf.script_node_up ? ::conf.script_node_up : "node-up"; |
1179 | return ::conf.script_node_up ? ::conf.script_node_up : "node-up"; |
1137 | } |
1180 | } |
1138 | |
1181 | |
1139 | const char *connection::script_node_down () |
1182 | const char *connection::script_node_down (int) |
1140 | { |
1183 | { |
1141 | script_node (); |
1184 | script_node (); |
1142 | |
1185 | |
1143 | putenv ("STATE=down"); |
1186 | putenv ("STATE=down"); |
1144 | |
1187 | |
… | |
… | |
1163 | shutdown (); |
1206 | shutdown (); |
1164 | } |
1207 | } |
1165 | |
1208 | |
1166 | ///////////////////////////////////////////////////////////////////////////// |
1209 | ///////////////////////////////////////////////////////////////////////////// |
1167 | |
1210 | |
1168 | const char *vpn::script_if_up () |
1211 | const char *vpn::script_if_up (int) |
1169 | { |
1212 | { |
1170 | // the tunnel device mtu should be the physical mtu - overhead |
1213 | // the tunnel device mtu should be the physical mtu - overhead |
1171 | // the tricky part is rounding to the cipher key blocksize |
1214 | // the tricky part is rounding to the cipher key blocksize |
1172 | int mtu = conf.mtu - ETH_OVERHEAD - VPE_OVERHEAD - UDP_OVERHEAD; |
1215 | int mtu = conf.mtu - ETH_OVERHEAD - VPE_OVERHEAD - MAX_OVERHEAD; |
1173 | mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion |
1216 | mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion |
1174 | mtu -= mtu % EVP_CIPHER_block_size (CIPHER); // round |
1217 | mtu -= mtu % EVP_CIPHER_block_size (CIPHER); // round |
1175 | mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again |
1218 | mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again |
1176 | |
1219 | |
1177 | char *env; |
1220 | char *env; |
… | |
… | |
1231 | { |
1274 | { |
1232 | slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
1275 | slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
1233 | exit (1); |
1276 | exit (1); |
1234 | } |
1277 | } |
1235 | |
1278 | |
1236 | run_script (this, &vpn::script_if_up, true); |
1279 | run_script (run_script_cb (this, &vpn::script_if_up), true); |
1237 | |
1280 | |
1238 | vpn_ev_watcher.start (tap->fd, POLLIN); |
1281 | vpn_ev_watcher.start (tap->fd, POLLIN); |
1239 | |
1282 | |
1240 | reconnect_all (); |
1283 | reconnect_all (); |
1241 | |
1284 | |