1 | /* |
1 | /* |
2 | connection.C -- manage a single connection |
2 | connection.C -- manage a single connection |
|
|
3 | Copyright (C) 2003 Marc Lehmann <pcg@goof.com> |
3 | |
4 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by |
6 | it under the terms of the GNU General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or |
7 | the Free Software Foundation; either version 2 of the License, or |
7 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
478 | } |
479 | } |
479 | |
480 | |
480 | bool config_packet::chk_config () const |
481 | bool config_packet::chk_config () const |
481 | { |
482 | { |
482 | if (prot_major != PROTOCOL_MAJOR) |
483 | if (prot_major != PROTOCOL_MAJOR) |
483 | slog (L_WARN, _("major version mismatch (%d <=> %d)"), prot_major, PROTOCOL_MAJOR); |
484 | slog (L_WARN, _("major version mismatch (remote %d <=> local %d)"), prot_major, PROTOCOL_MAJOR); |
484 | else if (randsize != RAND_SIZE) |
485 | else if (randsize != RAND_SIZE) |
485 | slog (L_WARN, _("rand size mismatch (%d <=> %d)"), randsize, RAND_SIZE); |
486 | slog (L_WARN, _("rand size mismatch (remote %d <=> local %d)"), randsize, RAND_SIZE); |
486 | else if (hmaclen != HMACLENGTH) |
487 | else if (hmaclen != HMACLENGTH) |
487 | slog (L_WARN, _("hmac length mismatch (%d <=> %d)"), hmaclen, HMACLENGTH); |
488 | slog (L_WARN, _("hmac length mismatch (remote %d <=> local %d)"), hmaclen, HMACLENGTH); |
488 | else if (flags != curflags ()) |
489 | else if (flags != curflags ()) |
489 | slog (L_WARN, _("flag mismatch (%x <=> %x)"), flags, curflags ()); |
490 | slog (L_WARN, _("flag mismatch (remote %x <=> local %x)"), flags, curflags ()); |
490 | else if (challengelen != sizeof (rsachallenge)) |
491 | else if (challengelen != sizeof (rsachallenge)) |
491 | slog (L_WARN, _("challenge length mismatch (%d <=> %d)"), challengelen, sizeof (rsachallenge)); |
492 | slog (L_WARN, _("challenge length mismatch (remote %d <=> local %d)"), challengelen, sizeof (rsachallenge)); |
492 | else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER))) |
493 | else if (cipher_nid != htonl (EVP_CIPHER_nid (CIPHER))) |
493 | slog (L_WARN, _("cipher mismatch (%x <=> %x)"), ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER)); |
494 | slog (L_WARN, _("cipher mismatch (remote %x <=> local %x)"), ntohl (cipher_nid), EVP_CIPHER_nid (CIPHER)); |
494 | else if (digest_nid != htonl (EVP_MD_type (RSA_HASH))) |
495 | else if (digest_nid != htonl (EVP_MD_type (RSA_HASH))) |
495 | slog (L_WARN, _("digest mismatch (%x <=> %x)"), ntohl (digest_nid), EVP_MD_type (RSA_HASH)); |
496 | slog (L_WARN, _("digest mismatch (remote %x <=> local %x)"), ntohl (digest_nid), EVP_MD_type (RSA_HASH)); |
496 | else if (hmac_nid != htonl (EVP_MD_type (DIGEST))) |
497 | else if (hmac_nid != htonl (EVP_MD_type (DIGEST))) |
497 | slog (L_WARN, _("hmac mismatch (%x <=> %x)"), ntohl (hmac_nid), EVP_MD_type (DIGEST)); |
498 | slog (L_WARN, _("hmac mismatch (remote %x <=> local %x)"), ntohl (hmac_nid), EVP_MD_type (DIGEST)); |
498 | else |
499 | else |
499 | return true; |
500 | return true; |
500 | |
501 | |
501 | return false; |
502 | return false; |
502 | } |
503 | } |
… | |
… | |
800 | reset_connection (); |
801 | reset_connection (); |
801 | establish_connection (); |
802 | establish_connection (); |
802 | } |
803 | } |
803 | |
804 | |
804 | void |
805 | void |
805 | connection::send_data_packet (tap_packet *pkt, bool broadcast) |
806 | connection::send_data_packet (tap_packet *pkt) |
806 | { |
807 | { |
807 | vpndata_packet *p = new vpndata_packet; |
808 | vpndata_packet *p = new vpndata_packet; |
808 | int tos = 0; |
809 | int tos = 0; |
809 | |
810 | |
810 | // I am not hilarious about peeking into packets, but so be it. |
811 | // I am not hilarious about peeking into packets, but so be it. |
811 | if (conf->inherit_tos |
812 | if (conf->inherit_tos && pkt->is_ipv4 ()) |
812 | && (*pkt)[12] == 0x08 && (*pkt)[13] == 0x00 // IP |
|
|
813 | && ((*pkt)[14] & 0xf0) == 0x40) // IPv4 |
|
|
814 | tos = (*pkt)[15] & IPTOS_TOS_MASK; |
813 | tos = (*pkt)[15] & IPTOS_TOS_MASK; |
815 | |
814 | |
816 | p->setup (this, broadcast ? 0 : conf->id, &((*pkt)[6 + 6]), pkt->len - 6 - 6, ++oseqno); // skip 2 macs |
815 | p->setup (this, conf->id, &((*pkt)[6 + 6]), pkt->len - 6 - 6, ++oseqno); // skip 2 macs |
817 | send_vpn_packet (p, si, tos); |
816 | send_vpn_packet (p, si, tos); |
818 | |
817 | |
819 | delete p; |
818 | delete p; |
820 | |
819 | |
821 | if (oseqno > MAX_SEQNO) |
820 | if (oseqno > MAX_SEQNO) |
822 | rekey (); |
821 | rekey (); |
823 | } |
822 | } |
824 | |
823 | |
825 | void |
824 | void |
826 | connection::inject_data_packet (tap_packet *pkt, bool broadcast) |
825 | connection::inject_data_packet (tap_packet *pkt, bool broadcast/*TODO DDD*/) |
827 | { |
826 | { |
828 | if (ictx && octx) |
827 | if (ictx && octx) |
829 | send_data_packet (pkt, broadcast); |
828 | send_data_packet (pkt); |
830 | else |
829 | else |
831 | { |
830 | { |
832 | if (!broadcast)//DDDD |
831 | if (!broadcast)//DDDD |
833 | data_queue.put (new tap_packet (*pkt)); |
832 | data_queue.put (new tap_packet (*pkt)); |
834 | |
833 | |
… | |
… | |
912 | rsachallenge k; |
911 | rsachallenge k; |
913 | |
912 | |
914 | if (0 > RSA_private_decrypt (sizeof (p->encr), |
913 | if (0 > RSA_private_decrypt (sizeof (p->encr), |
915 | (unsigned char *)&p->encr, (unsigned char *)&k, |
914 | (unsigned char *)&p->encr, (unsigned char *)&k, |
916 | ::conf.rsa_key, RSA_PKCS1_OAEP_PADDING)) |
915 | ::conf.rsa_key, RSA_PKCS1_OAEP_PADDING)) |
|
|
916 | { |
917 | slog (L_ERR, _("%s(%s): challenge illegal or corrupted (%s). mismatched key or config file?"), |
917 | slog (L_ERR, _("%s(%s): challenge illegal or corrupted (%s). mismatched key or config file?"), |
918 | conf->nodename, (const char *)rsi, ERR_error_string (ERR_get_error (), 0)); |
918 | conf->nodename, (const char *)rsi, ERR_error_string (ERR_get_error (), 0)); |
|
|
919 | break; |
|
|
920 | } |
919 | else |
921 | else |
920 | { |
922 | { |
921 | delete octx; |
923 | delete octx; |
922 | |
924 | |
923 | octx = new crypto_ctx (k, 1); |
925 | octx = new crypto_ctx (k, 1); |
… | |
… | |
1037 | |
1039 | |
1038 | if (iseqno.recv_ok (seqno)) |
1040 | if (iseqno.recv_ok (seqno)) |
1039 | { |
1041 | { |
1040 | vpn->tap->send (d); |
1042 | vpn->tap->send (d); |
1041 | |
1043 | |
1042 | if (p->dst () == 0) // re-broadcast |
|
|
1043 | for (vpn::conns_vector::iterator i = vpn->conns.begin (); i != vpn->conns.end (); ++i) |
|
|
1044 | { |
|
|
1045 | connection *c = *i; |
|
|
1046 | |
|
|
1047 | if (c->conf != THISNODE && c->conf != conf) |
|
|
1048 | c->inject_data_packet (d); |
|
|
1049 | } |
|
|
1050 | |
|
|
1051 | if (si != rsi) |
1044 | if (si != rsi) |
1052 | { |
1045 | { |
1053 | // fast re-sync on connection changes, useful especially for tcp/ip |
1046 | // fast re-sync on connection changes, useful especially for tcp/ip |
1054 | si = rsi; |
1047 | si = rsi; |
1055 | |
1048 | |