… | |
… | |
25 | |
25 | |
26 | // dns processing is EXTREMELY ugly. For obvious(?) reasons. |
26 | // dns processing is EXTREMELY ugly. For obvious(?) reasons. |
27 | // it's a hack, use only in emergency situations please. |
27 | // it's a hack, use only in emergency situations please. |
28 | |
28 | |
29 | #include <cstring> |
29 | #include <cstring> |
|
|
30 | #include <cassert> |
30 | |
31 | |
31 | #include <sys/types.h> |
32 | #include <sys/types.h> |
32 | #include <sys/socket.h> |
33 | #include <sys/socket.h> |
33 | #include <sys/wait.h> |
34 | #include <sys/wait.h> |
34 | #include <sys/uio.h> |
35 | #include <sys/uio.h> |
… | |
… | |
356 | return true; |
357 | return true; |
357 | } |
358 | } |
358 | |
359 | |
359 | vpn_packet *byte_stream::get () |
360 | vpn_packet *byte_stream::get () |
360 | { |
361 | { |
|
|
362 | unsigned int len; |
|
|
363 | |
|
|
364 | for (;;) |
|
|
365 | { |
361 | unsigned int len = (data [0] << 8) | data [1]; |
366 | len = (data [0] << 8) | data [1]; |
362 | |
367 | |
363 | if (len > MAXSIZE && fill >= 2) |
368 | if (len <= MAXSIZE || fill < 2) |
364 | assert (len <= MAXSIZE || fill < 2); // TODO handle this gracefully, connection reset |
369 | break; |
365 | |
370 | |
|
|
371 | // TODO: handle this better than skipping, e.g. by reset |
|
|
372 | slog (L_DEBUG, _("DNS: corrupted packet stream skipping a byte...")); |
|
|
373 | remove (1); |
|
|
374 | } |
|
|
375 | |
366 | if (fill < len + 2) |
376 | if (fill < len + 2) |
367 | return 0; |
377 | return 0; |
368 | |
378 | |
369 | vpn_packet *pkt = new vpn_packet; |
379 | vpn_packet *pkt = new vpn_packet; |
370 | |
380 | |
… | |
… | |
498 | return data - orig; |
508 | return data - orig; |
499 | } |
509 | } |
500 | |
510 | |
501 | ///////////////////////////////////////////////////////////////////////////// |
511 | ///////////////////////////////////////////////////////////////////////////// |
502 | |
512 | |
|
|
513 | static u16 dns_id = 0; // TODO: should be per-vpn |
|
|
514 | |
|
|
515 | static u16 next_id () |
|
|
516 | { |
|
|
517 | if (!dns_id) |
|
|
518 | dns_id = time (0); |
|
|
519 | |
|
|
520 | // the simplest lsfr with periodicity 65535 i could find |
|
|
521 | dns_id = (dns_id << 1) |
|
|
522 | | (((dns_id >> 1) |
|
|
523 | ^ (dns_id >> 2) |
|
|
524 | ^ (dns_id >> 4) |
|
|
525 | ^ (dns_id >> 15)) & 1); |
|
|
526 | |
|
|
527 | return dns_id; |
|
|
528 | } |
|
|
529 | |
|
|
530 | struct dns_rcv; |
|
|
531 | struct dns_snd; |
|
|
532 | |
|
|
533 | struct dns_connection |
|
|
534 | { |
|
|
535 | connection *c; |
|
|
536 | struct vpn *vpn; |
|
|
537 | |
|
|
538 | dns_cfg cfg; |
|
|
539 | |
|
|
540 | bool established; |
|
|
541 | |
|
|
542 | tstamp last_received; |
|
|
543 | tstamp last_sent; |
|
|
544 | double last_latency; |
|
|
545 | double poll_interval, send_interval; |
|
|
546 | |
|
|
547 | vector<dns_rcv *> rcvpq; |
|
|
548 | |
|
|
549 | byte_stream rcvdq; int rcvseq; |
|
|
550 | byte_stream snddq; int sndseq; |
|
|
551 | |
|
|
552 | void time_cb (time_watcher &w); time_watcher tw; |
|
|
553 | void receive_rep (dns_rcv *r); |
|
|
554 | |
|
|
555 | dns_connection (connection *c); |
|
|
556 | ~dns_connection (); |
|
|
557 | }; |
|
|
558 | |
503 | struct dns_snd |
559 | struct dns_snd |
504 | { |
560 | { |
505 | dns_packet *pkt; |
561 | dns_packet *pkt; |
506 | tstamp timeout, sent; |
562 | tstamp timeout, sent; |
507 | int retry; |
563 | int retry; |
508 | struct dns_connection *dns; |
564 | struct dns_connection *dns; |
509 | int seqno; |
565 | int seqno; |
510 | bool stdhdr; |
566 | bool stdhdr; |
511 | |
567 | |
512 | void gen_stream_req (int seqno, byte_stream &stream); |
568 | void gen_stream_req (int seqno, byte_stream &stream); |
513 | void gen_syn_req (const dns_cfg &cfg); |
569 | void gen_syn_req (); |
514 | |
570 | |
515 | dns_snd (dns_connection *dns); |
571 | dns_snd (dns_connection *dns); |
516 | ~dns_snd (); |
572 | ~dns_snd (); |
517 | }; |
573 | }; |
518 | |
|
|
519 | static u16 dns_id = 12098; // TODO: should be per-vpn |
|
|
520 | |
|
|
521 | static u16 next_id () |
|
|
522 | { |
|
|
523 | // the simplest lsfr with periodicity 65535 i could find |
|
|
524 | dns_id = (dns_id << 1) |
|
|
525 | | (((dns_id >> 1) |
|
|
526 | ^ (dns_id >> 2) |
|
|
527 | ^ (dns_id >> 4) |
|
|
528 | ^ (dns_id >> 15)) & 1); |
|
|
529 | |
|
|
530 | return dns_id; |
|
|
531 | } |
|
|
532 | |
574 | |
533 | dns_snd::dns_snd (dns_connection *dns) |
575 | dns_snd::dns_snd (dns_connection *dns) |
534 | : dns (dns) |
576 | : dns (dns) |
535 | { |
577 | { |
536 | timeout = 0; |
578 | timeout = 0; |
… | |
… | |
581 | |
623 | |
582 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
624 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
583 | pkt->qdcount = htons (1); |
625 | pkt->qdcount = htons (1); |
584 | |
626 | |
585 | int offs = 6*2; |
627 | int offs = 6*2; |
586 | int dlen = MAX_DOMAIN_SIZE - (strlen (THISNODE->domain) + 2); |
628 | int dlen = MAX_DOMAIN_SIZE - (strlen (dns->c->conf->domain) + 2); |
587 | // MAX_DOMAIN_SIZE is technically 255, but bind doesn't compress responses well, |
629 | // MAX_DOMAIN_SIZE is technically 255, but bind doesn't compress responses well, |
588 | // so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra |
630 | // so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra |
589 | |
631 | |
590 | char enc[256], *encp = enc; |
632 | char enc[256], *encp = enc; |
591 | encode_header (enc, THISNODE->id, seqno); |
633 | encode_header (enc, THISNODE->id, seqno); |
… | |
… | |
609 | encp += lbllen; |
651 | encp += lbllen; |
610 | |
652 | |
611 | enclen -= lbllen; |
653 | enclen -= lbllen; |
612 | } |
654 | } |
613 | |
655 | |
614 | append_domain (*pkt, offs, THISNODE->domain); |
656 | append_domain (*pkt, offs, dns->c->conf->domain); |
615 | |
657 | |
616 | (*pkt)[offs++] = 0; |
658 | (*pkt)[offs++] = 0; |
617 | (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; |
659 | (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; |
618 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
660 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
619 | |
661 | |
620 | pkt->len = offs; |
662 | pkt->len = offs; |
621 | } |
663 | } |
622 | |
664 | |
623 | void dns_snd::gen_syn_req (const dns_cfg &cfg) |
665 | void dns_snd::gen_syn_req () |
624 | { |
666 | { |
625 | timeout = NOW + INITIAL_SYN_TIMEOUT; |
667 | timeout = NOW + INITIAL_SYN_TIMEOUT; |
|
|
668 | |
|
|
669 | printf ("send syn\n");//D |
626 | |
670 | |
627 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
671 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
628 | pkt->qdcount = htons (1); |
672 | pkt->qdcount = htons (1); |
629 | |
673 | |
630 | int offs = 6*2; |
674 | int offs = 6 * 2; |
631 | |
675 | |
632 | int elen = cdc26.encode ((char *)pkt->at (offs + 1), (u8 *)&cfg, sizeof (dns_cfg)); |
676 | int elen = cdc26.encode ((char *)pkt->at (offs + 1), (u8 *)&dns->cfg, sizeof (dns_cfg)); |
633 | |
677 | |
634 | assert (elen <= MAX_LBL_SIZE); |
678 | assert (elen <= MAX_LBL_SIZE); |
635 | |
679 | |
636 | (*pkt)[offs] = elen; |
680 | (*pkt)[offs] = elen; |
637 | offs += elen + 1; |
681 | offs += elen + 1; |
638 | append_domain (*pkt, offs, THISNODE->domain); |
682 | append_domain (*pkt, offs, dns->c->conf->domain); |
639 | |
683 | |
640 | (*pkt)[offs++] = 0; |
684 | (*pkt)[offs++] = 0; |
641 | (*pkt)[offs++] = RR_TYPE_A >> 8; (*pkt)[offs++] = RR_TYPE_A; |
685 | (*pkt)[offs++] = RR_TYPE_A >> 8; (*pkt)[offs++] = RR_TYPE_A; |
642 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
686 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
643 | |
687 | |
… | |
… | |
666 | delete pkt; |
710 | delete pkt; |
667 | } |
711 | } |
668 | |
712 | |
669 | ///////////////////////////////////////////////////////////////////////////// |
713 | ///////////////////////////////////////////////////////////////////////////// |
670 | |
714 | |
671 | struct dns_connection |
|
|
672 | { |
|
|
673 | connection *c; |
|
|
674 | struct vpn *vpn; |
|
|
675 | |
|
|
676 | dns_cfg cfg; |
|
|
677 | |
|
|
678 | bool established; |
|
|
679 | |
|
|
680 | tstamp last_received; |
|
|
681 | tstamp last_sent; |
|
|
682 | double last_latency; |
|
|
683 | double poll_interval, send_interval; |
|
|
684 | |
|
|
685 | vector<dns_rcv *> rcvpq; |
|
|
686 | |
|
|
687 | byte_stream rcvdq; int rcvseq; |
|
|
688 | byte_stream snddq; int sndseq; |
|
|
689 | |
|
|
690 | void time_cb (time_watcher &w); time_watcher tw; |
|
|
691 | void receive_rep (dns_rcv *r); |
|
|
692 | |
|
|
693 | dns_connection (connection *c); |
|
|
694 | ~dns_connection (); |
|
|
695 | }; |
|
|
696 | |
|
|
697 | dns_connection::dns_connection (connection *c) |
715 | dns_connection::dns_connection (connection *c) |
698 | : c (c) |
716 | : c (c) |
699 | , rcvdq (MAX_BACKLOG * 2) |
717 | , rcvdq (MAX_BACKLOG * 2) |
700 | , snddq (MAX_BACKLOG * 2) |
718 | , snddq (MAX_BACKLOG * 2) |
701 | , tw (this, &dns_connection::time_cb) |
719 | , tw (this, &dns_connection::time_cb) |
… | |
… | |
765 | } |
783 | } |
766 | |
784 | |
767 | while (vpn_packet *pkt = rcvdq.get ()) |
785 | while (vpn_packet *pkt = rcvdq.get ()) |
768 | { |
786 | { |
769 | sockinfo si; |
787 | sockinfo si; |
770 | si.host = 0; si.port = 0; si.prot = PROT_DNSv4; |
788 | si.host = 0x01010101; si.port = htons (c->conf->id); si.prot = PROT_DNSv4; |
771 | |
789 | |
772 | vpn->recv_vpn_packet (pkt, si); |
790 | vpn->recv_vpn_packet (pkt, si); |
773 | |
791 | |
774 | delete pkt; |
792 | delete pkt; |
775 | } |
793 | } |
… | |
… | |
857 | // new packet, queue |
875 | // new packet, queue |
858 | rcv = new dns_rcv (seqno, data, datalen); |
876 | rcv = new dns_rcv (seqno, data, datalen); |
859 | dns->receive_rep (rcv); |
877 | dns->receive_rep (rcv); |
860 | } |
878 | } |
861 | |
879 | |
|
|
880 | { |
862 | pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section |
881 | pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section |
863 | |
882 | |
864 | int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A; |
883 | int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A; |
865 | pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; // type |
884 | pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; // type |
866 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
885 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
867 | pkt [offs++] = 0; pkt [offs++] = 0; |
886 | pkt [offs++] = 0; pkt [offs++] = 0; |
868 | pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL |
887 | pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL |
869 | |
888 | |
870 | int rdlen_offs = offs += 2; |
889 | int rdlen_offs = offs += 2; |
871 | |
890 | |
872 | int dlen = (dns ? ntohs (dns->cfg.max_size) : MAX_PKT_SIZE) - offs; |
891 | int dlen = (dns ? ntohs (dns->cfg.max_size) : MAX_PKT_SIZE) - offs; |
873 | // bind doesn't compress well, so reduce further by one label length |
892 | // bind doesn't compress well, so reduce further by one label length |
874 | dlen -= qlen; |
893 | dlen -= qlen; |
875 | |
894 | |
876 | if (dns) |
895 | if (dns) |
877 | { |
896 | { |
878 | // only put data into in-order sequence packets, if |
897 | // only put data into in-order sequence packets, if |
879 | // we receive out-of-order packets we generate empty |
898 | // we receive out-of-order packets we generate empty |
880 | // replies |
899 | // replies |
881 | while (dlen > 1 && !dns->snddq.empty () && in_seq) |
900 | while (dlen > 1 && !dns->snddq.empty () && in_seq) |
882 | { |
901 | { |
883 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
902 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
884 | |
903 | |
885 | if (txtlen > dns->snddq.size ()) |
904 | if (txtlen > dns->snddq.size ()) |
886 | txtlen = dns->snddq.size (); |
905 | txtlen = dns->snddq.size (); |
887 | |
906 | |
888 | pkt[offs++] = txtlen; |
907 | pkt[offs++] = txtlen; |
889 | memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); |
908 | memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); |
890 | offs += txtlen; |
909 | offs += txtlen; |
891 | dns->snddq.remove (txtlen); |
910 | dns->snddq.remove (txtlen); |
892 | |
911 | |
893 | dlen -= txtlen + 1; |
912 | dlen -= txtlen + 1; |
894 | } |
913 | } |
895 | |
914 | |
896 | // avoid empty TXT rdata |
915 | // avoid empty TXT rdata |
897 | if (offs == rdlen_offs) |
916 | if (offs == rdlen_offs) |
898 | pkt[offs++] = 0; |
917 | pkt[offs++] = 0; |
899 | |
918 | |
900 | slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ()); |
919 | slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ()); |
901 | } |
920 | } |
902 | else |
921 | else |
903 | { |
922 | { |
904 | // send RST |
923 | // send RST |
905 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
924 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
906 | pkt [offs++] = CMD_IP_RST; |
925 | pkt [offs++] = CMD_IP_RST; |
907 | } |
926 | } |
908 | |
927 | |
909 | int rdlen = offs - rdlen_offs; |
928 | int rdlen = offs - rdlen_offs; |
910 | |
929 | |
911 | pkt [rdlen_offs - 2] = rdlen >> 8; |
930 | pkt [rdlen_offs - 2] = rdlen >> 8; |
912 | pkt [rdlen_offs - 1] = rdlen; |
931 | pkt [rdlen_offs - 1] = rdlen; |
913 | |
932 | |
914 | if (dns) |
933 | if (dns) |
915 | { |
934 | { |
916 | // now update dns_rcv copy |
935 | // now update dns_rcv copy |
917 | rcv->pkt->len = offs; |
936 | rcv->pkt->len = offs; |
918 | memcpy (rcv->pkt->at (0), pkt.at (0), offs); |
937 | memcpy (rcv->pkt->at (0), pkt.at (0), offs); |
919 | } |
938 | } |
|
|
939 | } |
920 | |
940 | |
921 | duplicate_request: ; |
941 | duplicate_request: ; |
922 | } |
942 | } |
923 | else |
943 | else |
924 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
944 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
… | |
… | |
1142 | } |
1162 | } |
1143 | } |
1163 | } |
1144 | } |
1164 | } |
1145 | |
1165 | |
1146 | bool |
1166 | bool |
1147 | connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
1167 | vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
1148 | { |
1168 | { |
|
|
1169 | int client = ntohs (si.port); |
|
|
1170 | |
|
|
1171 | assert (0 < client && client <= conns.size ()); |
|
|
1172 | |
|
|
1173 | connection *c = conns [client - 1]; |
|
|
1174 | |
1149 | if (!dns) |
1175 | if (!c->dns) |
1150 | dns = new dns_connection (this); |
1176 | c->dns = new dns_connection (c); |
1151 | |
1177 | |
1152 | if (!dns->snddq.put (pkt)) |
1178 | if (!c->dns->snddq.put (pkt)) |
1153 | return false; |
1179 | return false; |
1154 | |
1180 | |
1155 | dns->tw.trigger (); |
1181 | c->dns->tw.trigger (); |
1156 | |
1182 | |
1157 | return true; |
1183 | return true; |
1158 | } |
1184 | } |
1159 | |
1185 | |
1160 | void |
1186 | void |
… | |
… | |
1214 | { |
1240 | { |
1215 | if (vpn->dns_sndpq.empty ()) |
1241 | if (vpn->dns_sndpq.empty ()) |
1216 | { |
1242 | { |
1217 | send = new dns_snd (this); |
1243 | send = new dns_snd (this); |
1218 | |
1244 | |
|
|
1245 | printf ("new conn %p %d\n", this, c->conf->id);//D |
1219 | cfg.reset (THISNODE->id); |
1246 | cfg.reset (THISNODE->id); |
1220 | send->gen_syn_req (cfg); |
1247 | send->gen_syn_req (); |
1221 | } |
1248 | } |
1222 | } |
1249 | } |
1223 | else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING |
1250 | else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING |
1224 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
1251 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
1225 | { |
1252 | { |
|
|
1253 | //printf ("sending data request etc.\n"); //D |
1226 | if (!snddq.empty ()) |
1254 | if (!snddq.empty ()) |
1227 | { |
1255 | { |
1228 | poll_interval = send_interval; |
1256 | poll_interval = send_interval; |
1229 | NEXT (NOW + send_interval); |
1257 | NEXT (NOW + send_interval); |
1230 | } |
1258 | } |