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