… | |
… | |
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> |
… | |
… | |
56 | |
58 | |
57 | static time_t next_timecheck; |
59 | static time_t next_timecheck; |
58 | |
60 | |
59 | #define MAGIC "vped\xbd\xc6\xdb\x82" // 8 bytes of magic |
61 | #define MAGIC "vped\xbd\xc6\xdb\x82" // 8 bytes of magic |
60 | |
62 | |
61 | static const rsachallenge & |
63 | struct crypto_ctx |
62 | challenge_bytes () |
|
|
63 | { |
|
|
64 | static rsachallenge challenge; |
|
|
65 | static time_t challenge_ttl; // time this challenge needs to be recreated |
|
|
66 | |
|
|
67 | if (now > challenge_ttl) |
|
|
68 | { |
64 | { |
69 | RAND_bytes ((unsigned char *)&challenge, sizeof (challenge)); |
65 | EVP_CIPHER_CTX cctx; |
70 | challenge_ttl = now + CHALLENGE_TTL; |
66 | HMAC_CTX hctx; |
71 | } |
|
|
72 | |
67 | |
73 | return challenge; |
68 | crypto_ctx (const rsachallenge &challenge, int enc); |
74 | } |
69 | ~crypto_ctx (); |
|
|
70 | }; |
75 | |
71 | |
76 | // run a script. yes, it's a template function. yes, c++ |
72 | crypto_ctx::crypto_ctx (const rsachallenge &challenge, int enc) |
77 | // is not a functional language. yes, this suxx. |
73 | { |
78 | template<class owner> |
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 | |
79 | static void |
86 | static void |
80 | run_script (owner *obj, const char *(owner::*setup)(), bool wait) |
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 | |
|
|
98 | struct rsa_entry { |
|
|
99 | tstamp expire; |
|
|
100 | rsaid id; |
|
|
101 | rsachallenge chg; |
|
|
102 | }; |
|
|
103 | |
|
|
104 | struct rsa_cache : list<rsa_entry> |
|
|
105 | { |
|
|
106 | void cleaner_cb (tstamp &ts); time_watcher cleaner; |
|
|
107 | |
|
|
108 | bool find (const rsaid &id, rsachallenge &chg) |
|
|
109 | { |
|
|
110 | for (iterator i = begin (); i != end (); ++i) |
|
|
111 | { |
|
|
112 | if (!memcmp (&id, &i->id, sizeof id) && i->expire > NOW) |
|
|
113 | { |
|
|
114 | memcpy (&chg, &i->chg, sizeof chg); |
|
|
115 | |
|
|
116 | erase (i); |
|
|
117 | return true; |
|
|
118 | } |
|
|
119 | } |
|
|
120 | |
|
|
121 | if (cleaner.at < NOW) |
|
|
122 | cleaner.start (NOW + RSA_TTL); |
|
|
123 | |
|
|
124 | return false; |
|
|
125 | } |
|
|
126 | |
|
|
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; |
|
|
136 | memcpy (&e.chg, &chg, sizeof chg); |
|
|
137 | |
|
|
138 | push_back (e); |
|
|
139 | |
|
|
140 | if (cleaner.at < NOW) |
|
|
141 | cleaner.start (NOW + RSA_TTL); |
|
|
142 | } |
|
|
143 | |
|
|
144 | rsa_cache () |
|
|
145 | : cleaner (this, &rsa_cache::cleaner_cb) |
|
|
146 | { } |
|
|
147 | |
|
|
148 | } rsa_cache; |
|
|
149 | |
|
|
150 | void rsa_cache::cleaner_cb (tstamp &ts) |
|
|
151 | { |
|
|
152 | if (empty ()) |
|
|
153 | ts = TSTAMP_CANCEL; |
|
|
154 | else |
|
|
155 | { |
|
|
156 | ts = NOW + RSA_TTL; |
|
|
157 | |
|
|
158 | for (iterator i = begin (); i != end (); ) |
|
|
159 | if (i->expire <= NOW) |
|
|
160 | i = erase (i); |
|
|
161 | else |
|
|
162 | ++i; |
|
|
163 | } |
|
|
164 | } |
|
|
165 | |
|
|
166 | typedef callback<const char *, int> run_script_cb; |
|
|
167 | |
|
|
168 | // run a shell script (or actually an external program). |
|
|
169 | static void |
|
|
170 | run_script (const run_script_cb &cb, bool wait) |
81 | { |
171 | { |
82 | int pid; |
172 | int pid; |
83 | |
173 | |
84 | if ((pid = fork ()) == 0) |
174 | if ((pid = fork ()) == 0) |
85 | { |
175 | { |
86 | char *filename; |
176 | char *filename; |
87 | asprintf (&filename, "%s/%s", confbase, (obj->*setup) ()); |
177 | asprintf (&filename, "%s/%s", confbase, cb(0)); |
88 | execl (filename, filename, (char *) 0); |
178 | execl (filename, filename, (char *) 0); |
89 | exit (255); |
179 | exit (255); |
90 | } |
180 | } |
91 | else if (pid > 0) |
181 | else if (pid > 0) |
92 | { |
182 | { |
… | |
… | |
96 | /* TODO: check status */ |
186 | /* TODO: check status */ |
97 | } |
187 | } |
98 | } |
188 | } |
99 | } |
189 | } |
100 | |
190 | |
101 | // xor the socket address into the challenge to ensure different challenges |
191 | ////////////////////////////////////////////////////////////////////////////// |
102 | // per host. we could rely on the OAEP padding, but this doesn't hurt. |
|
|
103 | void |
|
|
104 | xor_sa (rsachallenge &k, SOCKADDR *sa) |
|
|
105 | { |
|
|
106 | ((u32 *) k)[(CHG_CIPHER_KEY + 0) / 4] ^= sa->sin_addr.s_addr; |
|
|
107 | ((u16 *) k)[(CHG_CIPHER_KEY + 4) / 2] ^= sa->sin_port; |
|
|
108 | ((u32 *) k)[(CHG_HMAC_KEY + 0) / 4] ^= sa->sin_addr.s_addr; |
|
|
109 | ((u16 *) k)[(CHG_HMAC_KEY + 4) / 2] ^= sa->sin_port; |
|
|
110 | } |
|
|
111 | |
192 | |
112 | struct crypto_ctx |
193 | void pkt_queue::put (tap_packet *p) |
|
|
194 | { |
|
|
195 | if (queue[i]) |
113 | { |
196 | { |
114 | EVP_CIPHER_CTX cctx; |
197 | delete queue[i]; |
115 | HMAC_CTX hctx; |
198 | j = (j + 1) % QUEUEDEPTH; |
|
|
199 | } |
116 | |
200 | |
117 | crypto_ctx (rsachallenge &challenge, int enc); |
201 | queue[i] = p; |
118 | ~crypto_ctx (); |
|
|
119 | }; |
|
|
120 | |
202 | |
121 | crypto_ctx::crypto_ctx (rsachallenge &challenge, int enc) |
203 | i = (i + 1) % QUEUEDEPTH; |
122 | { |
|
|
123 | EVP_CIPHER_CTX_init (&cctx); |
|
|
124 | EVP_CipherInit_ex (&cctx, CIPHER, 0, &challenge[CHG_CIPHER_KEY], 0, enc); |
|
|
125 | HMAC_CTX_init (&hctx); |
|
|
126 | HMAC_Init_ex (&hctx, &challenge[CHG_HMAC_KEY], HMAC_KEYLEN, DIGEST, 0); |
|
|
127 | } |
204 | } |
128 | |
205 | |
129 | crypto_ctx::~crypto_ctx () |
206 | tap_packet *pkt_queue::get () |
130 | { |
207 | { |
131 | EVP_CIPHER_CTX_cleanup (&cctx); |
208 | tap_packet *p = queue[j]; |
132 | HMAC_CTX_cleanup (&hctx); |
209 | |
|
|
210 | if (p) |
|
|
211 | { |
|
|
212 | queue[j] = 0; |
|
|
213 | j = (j + 1) % QUEUEDEPTH; |
|
|
214 | } |
|
|
215 | |
|
|
216 | return p; |
|
|
217 | } |
|
|
218 | |
|
|
219 | pkt_queue::pkt_queue () |
|
|
220 | { |
|
|
221 | memset (queue, 0, sizeof (queue)); |
|
|
222 | i = 0; |
|
|
223 | j = 0; |
|
|
224 | } |
|
|
225 | |
|
|
226 | pkt_queue::~pkt_queue () |
|
|
227 | { |
|
|
228 | for (i = QUEUEDEPTH; --i > 0; ) |
|
|
229 | delete queue[i]; |
|
|
230 | } |
|
|
231 | |
|
|
232 | struct net_rateinfo { |
|
|
233 | u32 host; |
|
|
234 | double pcnt, diff; |
|
|
235 | tstamp last; |
|
|
236 | }; |
|
|
237 | |
|
|
238 | // only do action once every x seconds per host whole allowing bursts. |
|
|
239 | // this implementation ("splay list" ;) is inefficient, |
|
|
240 | // but low on resources. |
|
|
241 | struct net_rate_limiter : private list<net_rateinfo> |
|
|
242 | { |
|
|
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 |
|
|
246 | |
|
|
247 | bool can (u32 host); |
|
|
248 | bool can (SOCKADDR *sa) { return can((u32)sa->sin_addr.s_addr); } |
|
|
249 | bool can (sockinfo &si) { return can((u32)si.host); } |
|
|
250 | }; |
|
|
251 | |
|
|
252 | net_rate_limiter auth_rate_limiter, reset_rate_limiter; |
|
|
253 | |
|
|
254 | bool net_rate_limiter::can (u32 host) |
|
|
255 | { |
|
|
256 | iterator i; |
|
|
257 | |
|
|
258 | for (i = begin (); i != end (); ) |
|
|
259 | if (i->host == host) |
|
|
260 | break; |
|
|
261 | else if (i->last < NOW - EXPIRE) |
|
|
262 | i = erase (i); |
|
|
263 | else |
|
|
264 | i++; |
|
|
265 | |
|
|
266 | if (i == end ()) |
|
|
267 | { |
|
|
268 | net_rateinfo ri; |
|
|
269 | |
|
|
270 | ri.host = host; |
|
|
271 | ri.pcnt = 1.; |
|
|
272 | ri.diff = CUTOFF * (1. / (1. - ALPHA)); |
|
|
273 | ri.last = NOW; |
|
|
274 | |
|
|
275 | push_front (ri); |
|
|
276 | |
|
|
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 | } |
133 | } |
298 | } |
134 | |
299 | |
135 | ///////////////////////////////////////////////////////////////////////////// |
300 | ///////////////////////////////////////////////////////////////////////////// |
136 | |
301 | |
137 | static void next_wakeup (time_t next) |
302 | static void next_wakeup (time_t next) |
… | |
… | |
141 | } |
306 | } |
142 | |
307 | |
143 | static unsigned char hmac_digest[EVP_MAX_MD_SIZE]; |
308 | static unsigned char hmac_digest[EVP_MAX_MD_SIZE]; |
144 | |
309 | |
145 | 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) |
146 | { |
319 | { |
147 | u8 hmac[HMACLENGTH]; // each and every packet has a hmac field, but that is not (yet) checked everywhere |
|
|
148 | |
|
|
149 | void hmac_set (crypto_ctx * ctx); |
|
|
150 | bool hmac_chk (crypto_ctx * ctx); |
|
|
151 | |
|
|
152 | private: |
|
|
153 | void hmac_gen (crypto_ctx * ctx) |
|
|
154 | { |
|
|
155 | unsigned int xlen; |
320 | unsigned int xlen; |
156 | HMAC_CTX *hctx = &ctx->hctx; |
321 | HMAC_CTX *hctx = &ctx->hctx; |
157 | |
322 | |
158 | HMAC_Init_ex (hctx, 0, 0, 0, 0); |
323 | HMAC_Init_ex (hctx, 0, 0, 0, 0); |
159 | HMAC_Update (hctx, ((unsigned char *) this) + sizeof (hmac_packet), |
324 | HMAC_Update (hctx, ((unsigned char *) this) + sizeof (hmac_packet), |
160 | len - sizeof (hmac_packet)); |
325 | len - sizeof (hmac_packet)); |
161 | HMAC_Final (hctx, (unsigned char *) &hmac_digest, &xlen); |
326 | HMAC_Final (hctx, (unsigned char *) &hmac_digest, &xlen); |
162 | } |
|
|
163 | }; |
327 | } |
|
|
328 | }; |
164 | |
329 | |
165 | void |
330 | void |
166 | hmac_packet::hmac_set (crypto_ctx * ctx) |
331 | hmac_packet::hmac_set (crypto_ctx * ctx) |
167 | { |
332 | { |
168 | hmac_gen (ctx); |
333 | hmac_gen (ctx); |
… | |
… | |
184 | { |
349 | { |
185 | PT_RESET = 0, |
350 | PT_RESET = 0, |
186 | PT_DATA_UNCOMPRESSED, |
351 | PT_DATA_UNCOMPRESSED, |
187 | PT_DATA_COMPRESSED, |
352 | PT_DATA_COMPRESSED, |
188 | PT_PING, PT_PONG, // wasting namespace space? ;) |
353 | PT_PING, PT_PONG, // wasting namespace space? ;) |
189 | PT_AUTH, // authentification |
354 | PT_AUTH_REQ, // authentification request |
|
|
355 | PT_AUTH_RES, // authentification response |
190 | PT_CONNECT_REQ, // want other host to contact me |
356 | PT_CONNECT_REQ, // want other host to contact me |
191 | PT_CONNECT_INFO, // request connection to some node |
357 | PT_CONNECT_INFO, // request connection to some node |
192 | PT_REKEY, // rekeying (not yet implemented) |
|
|
193 | PT_MAX |
358 | PT_MAX |
194 | }; |
359 | }; |
195 | |
360 | |
196 | u8 type; |
361 | u8 type; |
197 | u8 srcdst, src1, dst1; |
362 | u8 srcdst, src1, dst1; |
… | |
… | |
200 | |
365 | |
201 | unsigned int src () |
366 | unsigned int src () |
202 | { |
367 | { |
203 | return src1 | ((srcdst >> 4) << 8); |
368 | return src1 | ((srcdst >> 4) << 8); |
204 | } |
369 | } |
|
|
370 | |
205 | unsigned int dst () |
371 | unsigned int dst () |
206 | { |
372 | { |
207 | return dst1 | ((srcdst & 0xf) << 8); |
373 | return dst1 | ((srcdst & 0xf) << 8); |
208 | } |
374 | } |
|
|
375 | |
209 | ptype typ () |
376 | ptype typ () |
210 | { |
377 | { |
211 | return (ptype) type; |
378 | return (ptype) type; |
212 | } |
379 | } |
213 | }; |
380 | }; |
… | |
… | |
252 | u32 cl; |
419 | u32 cl; |
253 | |
420 | |
254 | cl = lzf_compress (d, l, cdata + 2, (l - 2) & ~7); |
421 | cl = lzf_compress (d, l, cdata + 2, (l - 2) & ~7); |
255 | if (cl) |
422 | if (cl) |
256 | { |
423 | { |
257 | //printf ("compressed packet, %d => %d\n", l, cl);//D |
|
|
258 | type = PT_DATA_COMPRESSED; |
424 | type = PT_DATA_COMPRESSED; |
259 | d = cdata; |
425 | d = cdata; |
260 | l = cl + 2; |
426 | l = cl + 2; |
261 | |
427 | |
262 | d[0] = cl >> 8; |
428 | d[0] = cl >> 8; |
… | |
… | |
264 | } |
430 | } |
265 | #endif |
431 | #endif |
266 | |
432 | |
267 | EVP_EncryptInit_ex (cctx, 0, 0, 0, 0); |
433 | EVP_EncryptInit_ex (cctx, 0, 0, 0, 0); |
268 | |
434 | |
|
|
435 | struct { |
269 | #if RAND_SIZE |
436 | #if RAND_SIZE |
270 | struct { |
|
|
271 | u8 rnd[RAND_SIZE]; |
437 | u8 rnd[RAND_SIZE]; |
|
|
438 | #endif |
272 | u32 seqno; |
439 | u32 seqno; |
273 | } datahdr; |
440 | } datahdr; |
274 | |
441 | |
275 | datahdr.seqno = seqno; |
442 | datahdr.seqno = ntohl (seqno); |
|
|
443 | #if RAND_SIZE |
276 | RAND_pseudo_bytes ((unsigned char *) datahdr.rnd, RAND_SIZE); |
444 | RAND_pseudo_bytes ((unsigned char *) datahdr.rnd, RAND_SIZE); |
|
|
445 | #endif |
277 | |
446 | |
278 | EVP_EncryptUpdate (cctx, |
447 | EVP_EncryptUpdate (cctx, |
279 | (unsigned char *) data + outl, &outl2, |
448 | (unsigned char *) data + outl, &outl2, |
280 | (unsigned char *) &datahdr, DATAHDR); |
449 | (unsigned char *) &datahdr, DATAHDR); |
281 | outl += outl2; |
450 | 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 | |
451 | |
289 | EVP_EncryptUpdate (cctx, |
452 | EVP_EncryptUpdate (cctx, |
290 | (unsigned char *) data + outl, &outl2, |
453 | (unsigned char *) data + outl, &outl2, |
291 | (unsigned char *) d, l); |
454 | (unsigned char *) d, l); |
292 | outl += outl2; |
455 | outl += outl2; |
… | |
… | |
328 | outl += outl2; |
491 | outl += outl2; |
329 | |
492 | |
330 | EVP_DecryptFinal_ex (cctx, (unsigned char *)d + outl, &outl2); |
493 | EVP_DecryptFinal_ex (cctx, (unsigned char *)d + outl, &outl2); |
331 | outl += outl2; |
494 | outl += outl2; |
332 | |
495 | |
333 | seqno = *(u32 *)(d + RAND_SIZE); |
496 | seqno = ntohl (*(u32 *)(d + RAND_SIZE)); |
334 | |
497 | |
335 | id2mac (dst () ? dst() : THISNODE->id, p->dst); |
498 | id2mac (dst () ? dst() : THISNODE->id, p->dst); |
336 | id2mac (src (), p->src); |
499 | id2mac (src (), p->src); |
337 | |
500 | |
338 | #if ENABLE_COMPRESSION |
501 | #if ENABLE_COMPRESSION |
339 | if (type == PT_DATA_COMPRESSED) |
502 | if (type == PT_DATA_COMPRESSED) |
340 | { |
503 | { |
341 | u32 cl = (d[DATAHDR] << 8) | d[DATAHDR + 1]; |
504 | u32 cl = (d[DATAHDR] << 8) | d[DATAHDR + 1]; |
342 | p->len = lzf_decompress (d + DATAHDR + 2, cl, &(*p)[6 + 6], MAX_MTU) + 6 + 6; |
505 | 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 | } |
506 | } |
345 | else |
507 | else |
346 | p->len = outl + (6 + 6 - DATAHDR); |
508 | p->len = outl + (6 + 6 - DATAHDR); |
347 | #endif |
509 | #endif |
348 | |
510 | |
349 | return p; |
511 | return p; |
350 | } |
512 | } |
351 | |
513 | |
352 | struct ping_packet : vpn_packet |
514 | struct ping_packet : vpn_packet |
|
|
515 | { |
|
|
516 | void setup (int dst, ptype type) |
353 | { |
517 | { |
354 | void setup (int dst, ptype type) |
|
|
355 | { |
|
|
356 | set_hdr (type, dst); |
518 | set_hdr (type, dst); |
357 | len = sizeof (*this) - sizeof (net_packet); |
519 | len = sizeof (*this) - sizeof (net_packet); |
358 | } |
|
|
359 | }; |
520 | } |
|
|
521 | }; |
360 | |
522 | |
361 | 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 |
362 | { |
533 | { |
363 | // actually, hmaclen cannot be checked because the hmac |
|
|
364 | // field comes before this data, so peers with other |
|
|
365 | // hmacs simply will not work. |
|
|
366 | u8 prot_major, prot_minor, randsize, hmaclen; |
|
|
367 | u8 flags, challengelen, pad2, pad3; |
|
|
368 | u32 cipher_nid; |
|
|
369 | u32 digest_nid; |
|
|
370 | |
|
|
371 | const u8 curflags () const |
|
|
372 | { |
|
|
373 | return 0x80 |
534 | return 0x80 |
374 | | (ENABLE_COMPRESSION ? 0x01 : 0x00) |
535 | | (ENABLE_COMPRESSION ? 0x01 : 0x00); |
375 | | (ENABLE_TRUST ? 0x02 : 0x00); |
|
|
376 | } |
536 | } |
377 | |
537 | |
378 | void setup (ptype type, int dst) |
538 | void setup (ptype type, int dst) |
379 | { |
|
|
380 | prot_major = PROTOCOL_MAJOR; |
|
|
381 | prot_minor = PROTOCOL_MINOR; |
|
|
382 | randsize = RAND_SIZE; |
|
|
383 | hmaclen = HMACLENGTH; |
|
|
384 | flags = curflags (); |
|
|
385 | challengelen = sizeof (rsachallenge); |
|
|
386 | |
|
|
387 | cipher_nid = htonl (EVP_CIPHER_nid (CIPHER)); |
|
|
388 | digest_nid = htonl (EVP_MD_type (DIGEST)); |
|
|
389 | |
|
|
390 | len = sizeof (*this) - sizeof (net_packet); |
|
|
391 | set_hdr (type, dst); |
|
|
392 | } |
|
|
393 | |
|
|
394 | bool chk_config () |
|
|
395 | { |
|
|
396 | return prot_major == PROTOCOL_MAJOR |
|
|
397 | && randsize == RAND_SIZE |
|
|
398 | && hmaclen == HMACLENGTH |
|
|
399 | && flags == curflags () |
|
|
400 | && challengelen == sizeof (rsachallenge) |
|
|
401 | && cipher_nid == htonl (EVP_CIPHER_nid (CIPHER)) |
|
|
402 | && digest_nid == htonl (EVP_MD_type (DIGEST)); |
|
|
403 | } |
|
|
404 | }; |
|
|
405 | |
|
|
406 | struct auth_packet : config_packet |
|
|
407 | { |
539 | { |
408 | char magic[8]; |
540 | prot_major = PROTOCOL_MAJOR; |
409 | u8 subtype; |
541 | prot_minor = PROTOCOL_MINOR; |
410 | u8 pad1, pad2; |
542 | randsize = RAND_SIZE; |
411 | rsaencrdata challenge; |
543 | hmaclen = HMACLENGTH; |
|
|
544 | flags = curflags (); |
|
|
545 | challengelen = sizeof (rsachallenge); |
412 | |
546 | |
413 | auth_packet (int dst, auth_subtype stype) |
547 | cipher_nid = htonl (EVP_CIPHER_nid (CIPHER)); |
414 | { |
548 | digest_nid = htonl (EVP_MD_type (RSA_HASH)); |
415 | config_packet::setup (PT_AUTH, dst); |
549 | hmac_nid = htonl (EVP_MD_type (DIGEST)); |
416 | subtype = stype; |
550 | |
417 | 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_; |
418 | strncpy (magic, MAGIC, 8); |
580 | strncpy (magic, MAGIC, 8); |
419 | } |
581 | len = sizeof (*this) - sizeof (net_packet); |
420 | }; |
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 | }; |
421 | |
596 | |
422 | 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_) |
423 | { |
603 | { |
424 | u8 id; |
604 | id = id_; |
425 | u8 pad1, pad2, pad3; |
|
|
426 | |
|
|
427 | connect_req_packet (int dst, int id) |
|
|
428 | { |
|
|
429 | this->id = id; |
|
|
430 | set_hdr (PT_CONNECT_REQ, dst); |
605 | set_hdr (PT_CONNECT_REQ, dst); |
431 | len = sizeof (*this) - sizeof (net_packet); |
606 | len = sizeof (*this) - sizeof (net_packet); |
432 | } |
|
|
433 | }; |
607 | } |
|
|
608 | }; |
434 | |
609 | |
435 | 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_) |
436 | { |
617 | { |
437 | u8 id; |
618 | id = id_; |
438 | u8 pad1, pad2, pad3; |
619 | si = si_; |
439 | sockinfo si; |
|
|
440 | |
|
|
441 | connect_info_packet (int dst, int id, sockinfo &si) |
|
|
442 | { |
|
|
443 | this->id = id; |
|
|
444 | this->si = si; |
|
|
445 | set_hdr (PT_CONNECT_INFO, dst); |
620 | set_hdr (PT_CONNECT_INFO, dst); |
446 | len = sizeof (*this) - sizeof (net_packet); |
621 | len = sizeof (*this) - sizeof (net_packet); |
447 | } |
|
|
448 | }; |
622 | } |
|
|
623 | }; |
449 | |
624 | |
450 | ///////////////////////////////////////////////////////////////////////////// |
625 | ///////////////////////////////////////////////////////////////////////////// |
451 | |
626 | |
452 | void |
627 | void |
453 | fill_sa (SOCKADDR *sa, conf_node *conf) |
628 | fill_sa (SOCKADDR *sa, conf_node *conf) |
… | |
… | |
489 | } |
664 | } |
490 | |
665 | |
491 | void |
666 | void |
492 | connection::send_reset (SOCKADDR *dsa) |
667 | connection::send_reset (SOCKADDR *dsa) |
493 | { |
668 | { |
494 | static net_rate_limiter limiter(1); |
669 | if (reset_rate_limiter.can (dsa) && connectmode != conf_node::C_DISABLED) |
495 | |
|
|
496 | if (limiter.can (dsa)) |
|
|
497 | { |
670 | { |
498 | config_packet *pkt = new config_packet; |
671 | config_packet *pkt = new config_packet; |
499 | |
672 | |
500 | pkt->setup (vpn_packet::PT_RESET, conf->id); |
673 | pkt->setup (vpn_packet::PT_RESET, conf->id); |
501 | vpn->send_vpn_packet (pkt, dsa, IPTOS_MINCOST); |
674 | vpn->send_vpn_packet (pkt, dsa, IPTOS_MINCOST); |
502 | |
675 | |
503 | delete pkt; |
676 | delete pkt; |
504 | } |
677 | } |
505 | } |
678 | } |
506 | |
679 | |
507 | static rsachallenge * |
|
|
508 | gen_challenge (SOCKADDR *sa) |
|
|
509 | { |
|
|
510 | static rsachallenge k; |
|
|
511 | |
|
|
512 | memcpy (&k, &challenge_bytes (), sizeof (k)); |
|
|
513 | RAND_bytes ((unsigned char *)&k[CHG_SEQNO], sizeof (u32)); |
|
|
514 | xor_sa (k, sa); |
|
|
515 | |
|
|
516 | return &k; |
|
|
517 | } |
|
|
518 | |
|
|
519 | void |
680 | void |
520 | connection::send_auth (auth_subtype subtype, SOCKADDR *sa, rsachallenge *k) |
681 | connection::send_auth_request (SOCKADDR *sa, bool initiate) |
521 | { |
682 | { |
522 | static net_rate_limiter limiter(2); |
683 | if (auth_rate_limiter.can (sa)) |
523 | |
|
|
524 | if (subtype != AUTH_INIT || limiter.can (sa)) |
|
|
525 | { |
684 | { |
526 | auth_packet *pkt = new auth_packet (conf->id, subtype); |
685 | auth_req_packet *pkt = new auth_req_packet (conf->id, initiate); |
527 | |
686 | |
528 | //printf ("send auth_packet subtype %d\n", subtype);//D |
687 | rsachallenge chg; |
529 | |
688 | |
530 | if (!k) |
689 | rsa_cache.gen (pkt->id, chg); |
531 | k = gen_challenge (sa); |
|
|
532 | |
690 | |
533 | #if ENABLE_TRUST |
|
|
534 | if (0 > RSA_public_encrypt (sizeof (*k), |
691 | if (0 > RSA_public_encrypt (sizeof chg, |
535 | (unsigned char *)k, (unsigned char *)&pkt->challenge, |
692 | (unsigned char *)&chg, (unsigned char *)&pkt->encr, |
536 | conf->rsa_key, RSA_PKCS1_OAEP_PADDING)) |
693 | conf->rsa_key, RSA_PKCS1_OAEP_PADDING)) |
537 | fatal ("RSA_public_encrypt error"); |
694 | fatal ("RSA_public_encrypt error"); |
538 | #else |
|
|
539 | # error untrusted mode not yet implemented: programemr does not know how to |
|
|
540 | rsaencrdata enc; |
|
|
541 | |
695 | |
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 | |
|
|
553 | 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)); |
554 | |
697 | |
555 | vpn->send_vpn_packet (pkt, sa, IPTOS_RELIABILITY); |
698 | vpn->send_vpn_packet (pkt, sa, IPTOS_RELIABILITY); // rsa is very very costly |
556 | |
699 | |
557 | delete pkt; |
700 | delete pkt; |
558 | } |
701 | } |
559 | } |
702 | } |
560 | |
703 | |
561 | 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 |
562 | connection::establish_connection () |
723 | connection::establish_connection_cb (tstamp &ts) |
563 | { |
724 | { |
564 | 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) |
|
|
728 | ts = TSTAMP_CANCEL; |
|
|
729 | else if (ts <= NOW) |
565 | { |
730 | { |
566 | if (now >= next_retry) |
731 | double retry_int = double (retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2)) * 0.6; |
|
|
732 | |
|
|
733 | if (retry_int < 3600 * 8) |
|
|
734 | retry_cnt++; |
|
|
735 | |
|
|
736 | ts = NOW + retry_int; |
|
|
737 | |
|
|
738 | if (conf->hostname) |
567 | { |
739 | { |
568 | int retry_int = retry_cnt & 3 ? (retry_cnt & 3) : 1 << (retry_cnt >> 2); |
|
|
569 | |
|
|
570 | if (retry_cnt < (17 << 2) | 3) |
|
|
571 | retry_cnt++; |
|
|
572 | |
|
|
573 | if (connectmode == conf_node::C_ONDEMAND |
|
|
574 | && retry_int > ::conf.keepalive) |
|
|
575 | retry_int = ::conf.keepalive; |
|
|
576 | |
|
|
577 | next_retry = now + retry_int; |
|
|
578 | next_wakeup (next_retry); |
|
|
579 | |
|
|
580 | if (conf->hostname) |
|
|
581 | { |
|
|
582 | reset_dstaddr (); |
740 | reset_dstaddr (); |
583 | if (sa.sin_addr.s_addr) |
741 | if (sa.sin_addr.s_addr) |
584 | if (retry_cnt < 4) |
742 | if (retry_cnt < 4) |
585 | send_auth (AUTH_INIT, &sa); |
743 | send_auth_request (&sa, true); |
586 | else |
744 | else if (auth_rate_limiter.can (&sa)) |
587 | send_ping (&sa, 0); |
745 | send_ping (&sa, 0); |
588 | } |
|
|
589 | else |
|
|
590 | vpn->connect_request (conf->id); |
|
|
591 | } |
746 | } |
|
|
747 | else |
|
|
748 | vpn->connect_request (conf->id); |
592 | } |
749 | } |
593 | } |
750 | } |
594 | |
751 | |
595 | void |
752 | void |
596 | connection::reset_connection () |
753 | connection::reset_connection () |
… | |
… | |
598 | if (ictx && octx) |
755 | if (ictx && octx) |
599 | { |
756 | { |
600 | 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); |
601 | |
758 | |
602 | if (::conf.script_node_down) |
759 | if (::conf.script_node_down) |
603 | run_script (this, &connection::script_node_down, false); |
760 | run_script (run_script_cb (this, &connection::script_node_down), false); |
604 | } |
761 | } |
605 | |
762 | |
606 | delete ictx; |
763 | delete ictx; ictx = 0; |
607 | ictx = 0; |
764 | delete octx; octx = 0; |
608 | |
|
|
609 | delete octx; |
|
|
610 | octx = 0; |
|
|
611 | |
765 | |
612 | sa.sin_port = 0; |
766 | sa.sin_port = 0; |
613 | sa.sin_addr.s_addr = 0; |
767 | sa.sin_addr.s_addr = 0; |
614 | |
768 | |
615 | next_retry = 0; |
|
|
616 | next_rekey = 0; |
|
|
617 | last_activity = 0; |
769 | last_activity = 0; |
|
|
770 | |
|
|
771 | rekey.reset (); |
|
|
772 | keepalive.reset (); |
|
|
773 | establish_connection.reset (); |
618 | } |
774 | } |
619 | |
775 | |
620 | void |
776 | void |
621 | connection::shutdown () |
777 | connection::shutdown () |
622 | { |
778 | { |
… | |
… | |
625 | |
781 | |
626 | reset_connection (); |
782 | reset_connection (); |
627 | } |
783 | } |
628 | |
784 | |
629 | void |
785 | void |
630 | connection::rekey () |
786 | connection::rekey_cb (tstamp &ts) |
631 | { |
787 | { |
|
|
788 | ts = TSTAMP_CANCEL; |
|
|
789 | |
632 | reset_connection (); |
790 | reset_connection (); |
633 | establish_connection (); |
791 | establish_connection (); |
634 | } |
792 | } |
635 | |
793 | |
636 | void |
794 | void |
… | |
… | |
668 | } |
826 | } |
669 | |
827 | |
670 | void |
828 | void |
671 | connection::recv_vpn_packet (vpn_packet *pkt, SOCKADDR *ssa) |
829 | connection::recv_vpn_packet (vpn_packet *pkt, SOCKADDR *ssa) |
672 | { |
830 | { |
673 | last_activity = now; |
831 | last_activity = NOW; |
674 | |
832 | |
675 | slog (L_NOISE, "<<%d received packet type %d from %d to %d", |
833 | slog (L_NOISE, "<<%d received packet type %d from %d to %d", |
676 | conf->id, pkt->typ (), pkt->src (), pkt->dst ()); |
834 | conf->id, pkt->typ (), pkt->src (), pkt->dst ()); |
677 | |
835 | |
678 | switch (pkt->typ ()) |
836 | switch (pkt->typ ()) |
679 | { |
837 | { |
680 | case vpn_packet::PT_PING: |
838 | 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, |
839 | // we send pings instead of auth packets after some retries, |
686 | // so reset the retry counter and establish a conenction |
840 | // so reset the retry counter and establish a conenction |
687 | // when we receive a pong. |
841 | // when we receive a pong. |
688 | if (!ictx && !octx) |
842 | if (!ictx && !octx) |
689 | { |
843 | { |
690 | retry_cnt = 0; |
844 | retry_cnt = 0; |
691 | next_retry = 0; |
845 | establish_connection.at = 0; |
692 | establish_connection (); |
846 | establish_connection (); |
693 | } |
847 | } |
|
|
848 | else |
|
|
849 | send_ping (ssa, 1); // pong |
694 | |
850 | |
|
|
851 | break; |
|
|
852 | |
|
|
853 | case vpn_packet::PT_PONG: |
695 | break; |
854 | break; |
696 | |
855 | |
697 | case vpn_packet::PT_RESET: |
856 | case vpn_packet::PT_RESET: |
698 | { |
857 | { |
699 | reset_connection (); |
858 | reset_connection (); |
700 | |
859 | |
701 | config_packet *p = (config_packet *) pkt; |
860 | config_packet *p = (config_packet *) pkt; |
|
|
861 | |
702 | if (p->chk_config ()) |
862 | if (!p->chk_config ()) |
|
|
863 | { |
|
|
864 | slog (L_WARN, _("protocol mismatch, disabling node '%s'"), conf->nodename); |
|
|
865 | connectmode = conf_node::C_DISABLED; |
|
|
866 | } |
703 | if (connectmode == conf_node::C_ALWAYS) |
867 | else if (connectmode == conf_node::C_ALWAYS) |
704 | establish_connection (); |
868 | establish_connection (); |
705 | |
|
|
706 | //D slog the protocol mismatch? |
|
|
707 | } |
869 | } |
708 | break; |
870 | break; |
709 | |
871 | |
710 | case vpn_packet::PT_AUTH: |
872 | case vpn_packet::PT_AUTH_REQ: |
711 | { |
873 | { |
712 | auth_packet *p = (auth_packet *) pkt; |
874 | auth_req_packet *p = (auth_req_packet *) pkt; |
713 | |
875 | |
714 | slog (L_TRACE, "<<%d PT_AUTH(%d)", conf->id, p->subtype); |
876 | slog (L_TRACE, "<<%d PT_AUTH_REQ(%d)", conf->id, p->initiate); |
715 | |
877 | |
716 | if (p->chk_config () |
|
|
717 | && !strncmp (p->magic, MAGIC, 8)) |
878 | if (p->chk_config () && !strncmp (p->magic, MAGIC, 8)) |
718 | { |
879 | { |
719 | if (p->prot_minor != PROTOCOL_MINOR) |
880 | if (p->prot_minor != PROTOCOL_MINOR) |
720 | 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."), |
721 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
882 | PROTOCOL_MINOR, conf->nodename, p->prot_minor); |
722 | |
883 | |
723 | if (p->subtype == AUTH_INIT) |
884 | if (p->initiate) |
724 | send_auth (AUTH_INITREPLY, ssa); |
885 | send_auth_request (ssa, false); |
725 | |
886 | |
726 | rsachallenge k; |
887 | rsachallenge k; |
727 | |
888 | |
728 | #if ENABLE_TRUST |
|
|
729 | if (0 > RSA_private_decrypt (sizeof (rsaencrdata), |
889 | if (0 > RSA_private_decrypt (sizeof (p->encr), |
730 | (unsigned char *)&p->challenge, (unsigned char *)&k, |
890 | (unsigned char *)&p->encr, (unsigned char *)&k, |
731 | ::conf.rsa_key, RSA_PKCS1_OAEP_PADDING)) |
891 | ::conf.rsa_key, RSA_PKCS1_OAEP_PADDING)) |
732 | // continued below |
892 | slog (L_ERR, _("challenge from %s (%s) illegal or corrupted"), |
733 | #else |
893 | conf->nodename, (const char *)sockinfo (ssa)); |
734 | rsaencrdata j; |
|
|
735 | |
894 | else |
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 | { |
895 | { |
747 | slog (L_ERR, _("challenge from %s (%s) illegal or corrupted"), |
896 | retry_cnt = 0; |
748 | 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 | |
749 | break; |
911 | break; |
750 | } |
912 | } |
|
|
913 | } |
|
|
914 | } |
751 | |
915 | |
752 | retry_cnt = 0; |
916 | send_reset (ssa); |
753 | next_retry = now + 8; |
917 | break; |
754 | |
918 | |
755 | 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 |
756 | { |
937 | { |
757 | case AUTH_INIT: |
938 | rsaresponse h; |
758 | case AUTH_INITREPLY: |
|
|
759 | delete ictx; |
|
|
760 | ictx = 0; |
|
|
761 | |
939 | |
762 | delete octx; |
940 | rsa_hash (chg, h); |
763 | |
941 | |
764 | octx = new crypto_ctx (k, 1); |
942 | if (!memcmp ((u8 *)&h, (u8 *)p->response, sizeof h)) |
765 | oseqno = *(u32 *)&k[CHG_SEQNO] & 0x7fffffff; |
|
|
766 | |
|
|
767 | send_auth (AUTH_REPLY, ssa, &k); |
|
|
768 | break; |
|
|
769 | |
|
|
770 | case AUTH_REPLY: |
|
|
771 | |
|
|
772 | if (!memcmp ((u8 *)gen_challenge (ssa) + sizeof (u32), (u8 *)&k + sizeof (u32), |
|
|
773 | sizeof (rsachallenge) - sizeof (u32))) |
|
|
774 | { |
943 | { |
775 | delete ictx; |
944 | delete ictx; |
776 | |
945 | |
777 | ictx = new crypto_ctx (k, 0); |
946 | ictx = new crypto_ctx (chg, 0); |
778 | iseqno.reset (*(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 |
779 | |
948 | |
780 | sa = *ssa; |
949 | sa = *ssa; |
781 | |
950 | |
782 | next_rekey = now + ::conf.rekey; |
951 | rekey.set (NOW + ::conf.rekey); |
783 | next_wakeup (next_rekey); |
952 | keepalive.set (NOW + ::conf.keepalive); |
784 | |
953 | |
785 | // send queued packets |
954 | // send queued packets |
786 | while (tap_packet *p = queue.get ()) |
955 | while (tap_packet *p = queue.get ()) |
787 | { |
956 | { |
788 | send_data_packet (p); |
957 | send_data_packet (p); |
… | |
… | |
793 | |
962 | |
794 | slog (L_INFO, _("connection to %d (%s %s) established"), |
963 | slog (L_INFO, _("connection to %d (%s %s) established"), |
795 | conf->id, conf->nodename, (const char *)sockinfo (ssa)); |
964 | conf->id, conf->nodename, (const char *)sockinfo (ssa)); |
796 | |
965 | |
797 | if (::conf.script_node_up) |
966 | if (::conf.script_node_up) |
798 | run_script (this, &connection::script_node_up, false); |
967 | run_script (run_script_cb (this, &connection::script_node_up), false); |
|
|
968 | |
|
|
969 | break; |
799 | } |
970 | } |
800 | else |
971 | else |
801 | 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))"), |
802 | conf->nodename, (const char *)sockinfo (ssa)); |
973 | conf->nodename, (const char *)sockinfo (ssa)); |
803 | |
|
|
804 | break; |
|
|
805 | default: |
|
|
806 | slog (L_ERR, _("authentification illegal subtype error (%s %s)"), |
|
|
807 | conf->nodename, (const char *)sockinfo (ssa)); |
|
|
808 | break; |
|
|
809 | } |
974 | } |
810 | } |
975 | } |
811 | else |
|
|
812 | send_reset (ssa); |
|
|
813 | |
|
|
814 | break; |
|
|
815 | } |
976 | } |
|
|
977 | |
|
|
978 | send_reset (ssa); |
|
|
979 | break; |
816 | |
980 | |
817 | case vpn_packet::PT_DATA_COMPRESSED: |
981 | case vpn_packet::PT_DATA_COMPRESSED: |
818 | #if !ENABLE_COMPRESSION |
982 | #if !ENABLE_COMPRESSION |
819 | send_reset (ssa); |
983 | send_reset (ssa); |
820 | break; |
984 | break; |
821 | #endif |
985 | #endif |
|
|
986 | |
822 | case vpn_packet::PT_DATA_UNCOMPRESSED: |
987 | case vpn_packet::PT_DATA_UNCOMPRESSED: |
823 | |
988 | |
824 | if (ictx && octx) |
989 | if (ictx && octx) |
825 | { |
990 | { |
826 | vpndata_packet *p = (vpndata_packet *)pkt; |
991 | vpndata_packet *p = (vpndata_packet *)pkt; |
… | |
… | |
919 | connection *c = vpn->conns[p->id - 1]; |
1084 | connection *c = vpn->conns[p->id - 1]; |
920 | |
1085 | |
921 | slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", |
1086 | slog (L_TRACE, "<<%d PT_CONNECT_INFO(%d,%s) (%d)", |
922 | 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); |
923 | |
1088 | |
924 | c->send_auth (AUTH_INIT, p->si.sa ()); |
1089 | c->send_auth_request (p->si.sa (), true); |
925 | } |
1090 | } |
|
|
1091 | |
926 | break; |
1092 | break; |
927 | |
1093 | |
928 | default: |
1094 | default: |
929 | send_reset (ssa); |
1095 | send_reset (ssa); |
930 | break; |
1096 | break; |
931 | } |
|
|
932 | } |
|
|
933 | |
1097 | |
934 | void connection::timer () |
|
|
935 | { |
|
|
936 | if (conf != THISNODE) |
|
|
937 | { |
1098 | } |
938 | if (now >= next_retry && connectmode == conf_node::C_ALWAYS) |
1099 | } |
|
|
1100 | |
|
|
1101 | void connection::keepalive_cb (tstamp &ts) |
|
|
1102 | { |
|
|
1103 | if (NOW >= last_activity + ::conf.keepalive + 30) |
|
|
1104 | { |
|
|
1105 | reset_connection (); |
939 | establish_connection (); |
1106 | establish_connection (); |
940 | |
1107 | } |
941 | if (ictx && octx) |
|
|
942 | { |
|
|
943 | if (now >= next_rekey) |
|
|
944 | rekey (); |
|
|
945 | else if (now >= last_activity + ::conf.keepalive + 30) |
|
|
946 | { |
|
|
947 | reset_connection (); |
|
|
948 | establish_connection (); |
|
|
949 | } |
|
|
950 | else if (now >= last_activity + ::conf.keepalive) |
1108 | else if (NOW < last_activity + ::conf.keepalive) |
|
|
1109 | ts = last_activity + ::conf.keepalive; |
951 | if (conf->connectmode != conf_node::C_ONDEMAND |
1110 | else if (conf->connectmode != conf_node::C_ONDEMAND |
952 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
1111 | || THISNODE->connectmode != conf_node::C_ONDEMAND) |
|
|
1112 | { |
953 | send_ping (&sa); |
1113 | send_ping (&sa); |
954 | else |
1114 | ts = NOW + 5; |
|
|
1115 | } |
|
|
1116 | else |
955 | reset_connection (); |
1117 | reset_connection (); |
956 | |
1118 | |
957 | } |
|
|
958 | } |
|
|
959 | } |
1119 | } |
960 | |
1120 | |
961 | void connection::connect_request (int id) |
1121 | void connection::connect_request (int id) |
962 | { |
1122 | { |
963 | connect_req_packet *p = new connect_req_packet (conf->id, id); |
1123 | connect_req_packet *p = new connect_req_packet (conf->id, id); |
… | |
… | |
969 | delete p; |
1129 | delete p; |
970 | } |
1130 | } |
971 | |
1131 | |
972 | void connection::script_node () |
1132 | void connection::script_node () |
973 | { |
1133 | { |
974 | vpn->script_if_up (); |
1134 | vpn->script_if_up (0); |
975 | |
1135 | |
976 | char *env; |
1136 | char *env; |
977 | asprintf (&env, "DESTID=%d", conf->id); |
1137 | asprintf (&env, "DESTID=%d", conf->id); |
978 | putenv (env); |
1138 | putenv (env); |
979 | asprintf (&env, "DESTNODE=%s", conf->nodename); |
1139 | asprintf (&env, "DESTNODE=%s", conf->nodename); |
… | |
… | |
982 | putenv (env); |
1142 | putenv (env); |
983 | asprintf (&env, "DESTPORT=%d", ntohs (sa.sin_port)); |
1143 | asprintf (&env, "DESTPORT=%d", ntohs (sa.sin_port)); |
984 | putenv (env); |
1144 | putenv (env); |
985 | } |
1145 | } |
986 | |
1146 | |
987 | const char *connection::script_node_up () |
1147 | const char *connection::script_node_up (int) |
988 | { |
1148 | { |
989 | script_node (); |
1149 | script_node (); |
990 | |
1150 | |
991 | putenv ("STATE=up"); |
1151 | putenv ("STATE=up"); |
992 | |
1152 | |
993 | return ::conf.script_node_up ? ::conf.script_node_up : "node-up"; |
1153 | return ::conf.script_node_up ? ::conf.script_node_up : "node-up"; |
994 | } |
1154 | } |
995 | |
1155 | |
996 | const char *connection::script_node_down () |
1156 | const char *connection::script_node_down (int) |
997 | { |
1157 | { |
998 | script_node (); |
1158 | script_node (); |
999 | |
1159 | |
1000 | putenv ("STATE=down"); |
1160 | putenv ("STATE=down"); |
1001 | |
1161 | |
1002 | return ::conf.script_node_up ? ::conf.script_node_down : "node-down"; |
1162 | return ::conf.script_node_up ? ::conf.script_node_down : "node-down"; |
1003 | } |
1163 | } |
1004 | |
1164 | |
|
|
1165 | connection::connection(struct vpn *vpn_) |
|
|
1166 | : vpn(vpn_) |
|
|
1167 | , rekey (this, &connection::rekey_cb) |
|
|
1168 | , keepalive (this, &connection::keepalive_cb) |
|
|
1169 | , establish_connection (this, &connection::establish_connection_cb) |
|
|
1170 | { |
|
|
1171 | octx = ictx = 0; |
|
|
1172 | retry_cnt = 0; |
|
|
1173 | |
|
|
1174 | connectmode = conf_node::C_ALWAYS; // initial setting |
|
|
1175 | reset_connection (); |
|
|
1176 | } |
|
|
1177 | |
|
|
1178 | connection::~connection () |
|
|
1179 | { |
|
|
1180 | shutdown (); |
|
|
1181 | } |
|
|
1182 | |
1005 | ///////////////////////////////////////////////////////////////////////////// |
1183 | ///////////////////////////////////////////////////////////////////////////// |
1006 | |
1184 | |
1007 | vpn::vpn (void) |
|
|
1008 | {} |
|
|
1009 | |
|
|
1010 | const char *vpn::script_if_up () |
1185 | const char *vpn::script_if_up (int) |
1011 | { |
1186 | { |
1012 | // the tunnel device mtu should be the physical mtu - overhead |
1187 | // the tunnel device mtu should be the physical mtu - overhead |
1013 | // the tricky part is rounding to the cipher key blocksize |
1188 | // the tricky part is rounding to the cipher key blocksize |
1014 | int mtu = conf.mtu - ETH_OVERHEAD - VPE_OVERHEAD - UDP_OVERHEAD; |
1189 | int mtu = conf.mtu - ETH_OVERHEAD - VPE_OVERHEAD - MAX_OVERHEAD; |
1015 | mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion |
1190 | mtu += ETH_OVERHEAD - 6 - 6; // now we have the data portion |
1016 | mtu -= mtu % EVP_CIPHER_block_size (CIPHER); // round |
1191 | mtu -= mtu % EVP_CIPHER_block_size (CIPHER); // round |
1017 | mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again |
1192 | mtu -= ETH_OVERHEAD - 6 - 6; // and get interface mtu again |
1018 | |
1193 | |
1019 | char *env; |
1194 | char *env; |
… | |
… | |
1064 | { |
1239 | { |
1065 | int oval = 1; |
1240 | int oval = 1; |
1066 | setsockopt (socket_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
1241 | setsockopt (socket_fd, SOL_SOCKET, SO_REUSEADDR, &oval, sizeof oval); |
1067 | } |
1242 | } |
1068 | |
1243 | |
|
|
1244 | udp_ev_watcher.start (socket_fd, POLLIN); |
|
|
1245 | |
1069 | tap = new tap_device (); |
1246 | tap = new tap_device (); |
1070 | if (!tap) //D this, of course, never catches |
1247 | if (!tap) //D this, of course, never catches |
1071 | { |
1248 | { |
1072 | slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
1249 | slog (L_ERR, _("cannot create network interface '%s'"), conf.ifname); |
1073 | exit (1); |
1250 | exit (1); |
1074 | } |
1251 | } |
1075 | |
1252 | |
1076 | run_script (this, &vpn::script_if_up, true); |
1253 | run_script (run_script_cb (this, &vpn::script_if_up), true); |
|
|
1254 | |
|
|
1255 | vpn_ev_watcher.start (tap->fd, POLLIN); |
|
|
1256 | |
|
|
1257 | reconnect_all (); |
1077 | |
1258 | |
1078 | return 0; |
1259 | return 0; |
1079 | } |
1260 | } |
1080 | |
1261 | |
1081 | void |
1262 | void |
… | |
… | |
1146 | // if ((*i)->conf->routerprio) |
1327 | // if ((*i)->conf->routerprio) |
1147 | // (*i)->establish_connection (); |
1328 | // (*i)->establish_connection (); |
1148 | } |
1329 | } |
1149 | |
1330 | |
1150 | void |
1331 | void |
1151 | vpn::main_loop () |
1332 | vpn::udp_ev (short revents) |
1152 | { |
1333 | { |
1153 | struct pollfd pollfd[2]; |
1334 | if (revents & (POLLIN | POLLERR)) |
1154 | |
|
|
1155 | pollfd[0].fd = tap->fd; |
|
|
1156 | pollfd[0].events = POLLIN; |
|
|
1157 | pollfd[1].fd = socket_fd; |
|
|
1158 | pollfd[1].events = POLLIN; |
|
|
1159 | |
|
|
1160 | events = 0; |
|
|
1161 | now = time (0); |
|
|
1162 | next_timecheck = now + 1; |
|
|
1163 | |
|
|
1164 | reconnect_all (); |
|
|
1165 | |
|
|
1166 | for (;;) |
|
|
1167 | { |
1335 | { |
1168 | int npoll = poll (pollfd, 2, (next_timecheck - now) * 1000); |
1336 | vpn_packet *pkt = new vpn_packet; |
1169 | |
1337 | struct sockaddr_in sa; |
1170 | now = time (0); |
1338 | socklen_t sa_len = sizeof (sa); |
|
|
1339 | int len; |
1171 | |
1340 | |
|
|
1341 | len = recvfrom (socket_fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
|
|
1342 | |
1172 | if (npoll > 0) |
1343 | if (len > 0) |
1173 | { |
1344 | { |
1174 | if (pollfd[1].revents) |
1345 | pkt->len = len; |
|
|
1346 | |
|
|
1347 | unsigned int src = pkt->src (); |
|
|
1348 | unsigned int dst = pkt->dst (); |
|
|
1349 | |
|
|
1350 | slog (L_NOISE, _("<<?/%s received possible vpn packet type %d from %d to %d, length %d"), |
|
|
1351 | (const char *)sockinfo (sa), pkt->typ (), pkt->src (), pkt->dst (), pkt->len); |
|
|
1352 | |
|
|
1353 | if (dst > conns.size () || pkt->typ () >= vpn_packet::PT_MAX) |
|
|
1354 | slog (L_WARN, _("<<? received CORRUPTED packet type %d from %d to %d"), |
|
|
1355 | pkt->typ (), pkt->src (), pkt->dst ()); |
|
|
1356 | else if (dst == 0 && !THISNODE->routerprio) |
|
|
1357 | slog (L_WARN, _("<<%d received broadcast, but we are no router"), dst); |
|
|
1358 | else if (dst != 0 && dst != THISNODE->id) |
|
|
1359 | slog (L_WARN, |
|
|
1360 | _("received frame for node %d ('%s') from %s, but this is node %d ('%s')"), |
|
|
1361 | dst, conns[dst - 1]->conf->nodename, |
|
|
1362 | (const char *)sockinfo (sa), |
|
|
1363 | THISNODE->id, THISNODE->nodename); |
|
|
1364 | else if (src == 0 || src > conns.size ()) |
|
|
1365 | slog (L_WARN, _("received frame from unknown node %d (%s)"), src, (const char *)sockinfo (sa)); |
|
|
1366 | else |
|
|
1367 | conns[src - 1]->recv_vpn_packet (pkt, &sa); |
|
|
1368 | } |
|
|
1369 | else |
|
|
1370 | { |
|
|
1371 | // probably ECONNRESET or somesuch |
|
|
1372 | slog (L_DEBUG, _("%s: %s"), (const char *)sockinfo(sa), strerror (errno)); |
|
|
1373 | } |
|
|
1374 | |
|
|
1375 | delete pkt; |
|
|
1376 | } |
|
|
1377 | else if (revents & POLLHUP) |
|
|
1378 | { |
|
|
1379 | // this cannot ;) happen on udp sockets |
|
|
1380 | slog (L_ERR, _("FATAL: POLLHUP on socket fd, terminating.")); |
|
|
1381 | exit (1); |
|
|
1382 | } |
|
|
1383 | else |
|
|
1384 | { |
|
|
1385 | slog (L_ERR, |
|
|
1386 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
|
|
1387 | revents); |
|
|
1388 | exit (1); |
|
|
1389 | } |
|
|
1390 | } |
|
|
1391 | |
|
|
1392 | void |
|
|
1393 | vpn::vpn_ev (short revents) |
|
|
1394 | { |
|
|
1395 | if (revents & POLLIN) |
|
|
1396 | { |
|
|
1397 | /* process data */ |
|
|
1398 | tap_packet *pkt; |
|
|
1399 | |
|
|
1400 | pkt = tap->recv (); |
|
|
1401 | |
|
|
1402 | int dst = mac2id (pkt->dst); |
|
|
1403 | int src = mac2id (pkt->src); |
|
|
1404 | |
|
|
1405 | if (src != THISNODE->id) |
|
|
1406 | { |
|
|
1407 | slog (L_ERR, _("FATAL: tap packet not originating on current node received, terminating.")); |
|
|
1408 | exit (1); |
|
|
1409 | } |
|
|
1410 | |
|
|
1411 | if (dst == THISNODE->id) |
|
|
1412 | { |
|
|
1413 | slog (L_ERR, _("FATAL: tap packet destined for current node received, terminating.")); |
|
|
1414 | exit (1); |
|
|
1415 | } |
|
|
1416 | |
|
|
1417 | if (dst > conns.size ()) |
|
|
1418 | slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); |
|
|
1419 | else |
|
|
1420 | { |
|
|
1421 | if (dst) |
1175 | { |
1422 | { |
1176 | if (pollfd[1].revents & (POLLIN | POLLERR)) |
1423 | // unicast |
1177 | { |
|
|
1178 | vpn_packet *pkt = new vpn_packet; |
|
|
1179 | struct sockaddr_in sa; |
|
|
1180 | socklen_t sa_len = sizeof (sa); |
|
|
1181 | int len; |
|
|
1182 | |
|
|
1183 | len = recvfrom (socket_fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
|
|
1184 | |
|
|
1185 | if (len > 0) |
|
|
1186 | { |
|
|
1187 | pkt->len = len; |
|
|
1188 | |
|
|
1189 | unsigned int src = pkt->src (); |
|
|
1190 | unsigned int dst = pkt->dst (); |
|
|
1191 | |
|
|
1192 | slog (L_NOISE, _("<<?/%s received possible vpn packet type %d from %d to %d, length %d"), |
|
|
1193 | (const char *)sockinfo (sa), pkt->typ (), pkt->src (), pkt->dst (), pkt->len); |
|
|
1194 | |
|
|
1195 | if (dst > conns.size () || pkt->typ () >= vpn_packet::PT_MAX) |
|
|
1196 | slog (L_WARN, _("<<? received CORRUPTED packet type %d from %d to %d"), |
|
|
1197 | pkt->typ (), pkt->src (), pkt->dst ()); |
|
|
1198 | else if (dst == 0 && !THISNODE->routerprio) |
|
|
1199 | slog (L_WARN, _("<<%d received broadcast, but we are no router"), dst); |
|
|
1200 | else if (dst != 0 && dst != THISNODE->id) |
1424 | if (dst != THISNODE->id) |
1201 | slog (L_WARN, |
|
|
1202 | _("received frame for node %d ('%s') from %s, but this is node %d ('%s')"), |
|
|
1203 | dst, conns[dst - 1]->conf->nodename, |
|
|
1204 | (const char *)sockinfo (sa), |
|
|
1205 | THISNODE->id, THISNODE->nodename); |
|
|
1206 | else if (src == 0 || src > conns.size ()) |
|
|
1207 | slog (L_WARN, _("received frame from unknown node %d (%s)"), src, (const char *)sockinfo (sa)); |
|
|
1208 | else |
|
|
1209 | conns[src - 1]->recv_vpn_packet (pkt, &sa); |
1425 | conns[dst - 1]->inject_data_packet (pkt); |
1210 | } |
|
|
1211 | else |
|
|
1212 | { |
|
|
1213 | // probably ECONNRESET or somesuch |
|
|
1214 | slog (L_DEBUG, _("%s: %s"), (const char *)sockinfo(sa), strerror (errno)); |
|
|
1215 | } |
|
|
1216 | |
|
|
1217 | delete pkt; |
|
|
1218 | } |
1426 | } |
1219 | else if (pollfd[1].revents & POLLHUP) |
1427 | else |
1220 | { |
1428 | { |
1221 | // this cannot ;) happen on udp sockets |
1429 | // broadcast, first check router, then self, then english |
1222 | slog (L_ERR, _("FATAL: POLLHUP on socket fd, terminating.")); |
1430 | connection *router = find_router (); |
1223 | exit (1); |
1431 | |
1224 | } |
1432 | if (router) |
|
|
1433 | router->inject_data_packet (pkt, true); |
1225 | else |
1434 | else |
1226 | { |
|
|
1227 | slog (L_ERR, |
|
|
1228 | _("FATAL: unknown revents %08x in socket, terminating\n"), |
|
|
1229 | pollfd[1].revents); |
|
|
1230 | exit (1); |
|
|
1231 | } |
|
|
1232 | } |
|
|
1233 | |
|
|
1234 | // I use else here to give vpn_packets absolute priority |
|
|
1235 | else if (pollfd[0].revents) |
|
|
1236 | { |
|
|
1237 | if (pollfd[0].revents & POLLIN) |
|
|
1238 | { |
|
|
1239 | /* process data */ |
|
|
1240 | tap_packet *pkt; |
|
|
1241 | |
|
|
1242 | pkt = tap->recv (); |
|
|
1243 | |
|
|
1244 | int dst = mac2id (pkt->dst); |
|
|
1245 | int src = mac2id (pkt->src); |
|
|
1246 | |
|
|
1247 | if (src != THISNODE->id) |
|
|
1248 | { |
|
|
1249 | slog (L_ERR, _("FATAL: tap packet not originating on current node received, terminating.")); |
|
|
1250 | exit (1); |
|
|
1251 | } |
|
|
1252 | |
|
|
1253 | if (dst == THISNODE->id) |
|
|
1254 | { |
|
|
1255 | slog (L_ERR, _("FATAL: tap packet destined for current node received, terminating.")); |
|
|
1256 | exit (1); |
|
|
1257 | } |
|
|
1258 | |
|
|
1259 | if (dst > conns.size ()) |
|
|
1260 | slog (L_ERR, _("tap packet for unknown node %d received, ignoring."), dst); |
|
|
1261 | else |
|
|
1262 | { |
|
|
1263 | if (dst) |
|
|
1264 | { |
|
|
1265 | // unicast |
|
|
1266 | if (dst != THISNODE->id) |
|
|
1267 | conns[dst - 1]->inject_data_packet (pkt); |
|
|
1268 | } |
|
|
1269 | else |
|
|
1270 | { |
|
|
1271 | // broadcast, first check router, then self, then english |
|
|
1272 | connection *router = find_router (); |
|
|
1273 | |
|
|
1274 | if (router) |
|
|
1275 | router->inject_data_packet (pkt, true); |
|
|
1276 | else |
|
|
1277 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
1435 | for (conns_vector::iterator c = conns.begin (); c != conns.end (); ++c) |
1278 | if ((*c)->conf != THISNODE) |
1436 | if ((*c)->conf != THISNODE) |
1279 | (*c)->inject_data_packet (pkt); |
1437 | (*c)->inject_data_packet (pkt); |
1280 | } |
|
|
1281 | } |
|
|
1282 | |
|
|
1283 | delete pkt; |
|
|
1284 | } |
|
|
1285 | else if (pollfd[0].revents & (POLLHUP | POLLERR)) |
|
|
1286 | { |
|
|
1287 | slog (L_ERR, _("FATAL: POLLHUP or POLLERR on network device fd, terminating.")); |
|
|
1288 | exit (1); |
|
|
1289 | } |
|
|
1290 | else |
|
|
1291 | abort (); |
|
|
1292 | } |
1438 | } |
1293 | } |
1439 | } |
1294 | |
1440 | |
|
|
1441 | delete pkt; |
|
|
1442 | } |
|
|
1443 | else if (revents & (POLLHUP | POLLERR)) |
|
|
1444 | { |
|
|
1445 | slog (L_ERR, _("FATAL: POLLHUP or POLLERR on network device fd, terminating.")); |
|
|
1446 | exit (1); |
|
|
1447 | } |
|
|
1448 | else |
|
|
1449 | abort (); |
|
|
1450 | } |
|
|
1451 | |
|
|
1452 | void |
|
|
1453 | vpn::event_cb (tstamp &ts) |
|
|
1454 | { |
1295 | if (events) |
1455 | if (events) |
|
|
1456 | { |
|
|
1457 | if (events & EVENT_SHUTDOWN) |
1296 | { |
1458 | { |
1297 | if (events & EVENT_SHUTDOWN) |
|
|
1298 | { |
|
|
1299 | shutdown_all (); |
1459 | shutdown_all (); |
1300 | |
1460 | |
1301 | remove_pid (pidfilename); |
1461 | remove_pid (pidfilename); |
1302 | |
1462 | |
1303 | slog (L_INFO, _("vped terminating")); |
1463 | slog (L_INFO, _("vped terminating")); |
1304 | |
1464 | |
1305 | exit (0); |
1465 | exit (0); |
1306 | } |
|
|
1307 | |
|
|
1308 | if (events & EVENT_RECONNECT) |
|
|
1309 | reconnect_all (); |
|
|
1310 | |
|
|
1311 | events = 0; |
|
|
1312 | } |
1466 | } |
1313 | |
1467 | |
1314 | // very very very dumb and crude and inefficient timer handling, or maybe not? |
1468 | if (events & EVENT_RECONNECT) |
1315 | if (now >= next_timecheck) |
1469 | reconnect_all (); |
1316 | { |
|
|
1317 | next_timecheck = now + TIMER_GRANULARITY; |
|
|
1318 | |
1470 | |
1319 | for (conns_vector::iterator c = conns.begin (); |
1471 | events = 0; |
1320 | c != conns.end (); ++c) |
|
|
1321 | (*c)->timer (); |
|
|
1322 | } |
|
|
1323 | } |
1472 | } |
|
|
1473 | |
|
|
1474 | ts = TSTAMP_CANCEL; |
|
|
1475 | } |
|
|
1476 | |
|
|
1477 | vpn::vpn (void) |
|
|
1478 | : udp_ev_watcher (this, &vpn::udp_ev) |
|
|
1479 | , vpn_ev_watcher (this, &vpn::vpn_ev) |
|
|
1480 | , event (this, &vpn::event_cb) |
|
|
1481 | { |
1324 | } |
1482 | } |
1325 | |
1483 | |
1326 | vpn::~vpn () |
1484 | vpn::~vpn () |
1327 | {} |
1485 | { |
|
|
1486 | } |
1328 | |
1487 | |