… | |
… | |
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> |
… | |
… | |
264 | static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyO"); |
265 | static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyO"); |
265 | |
266 | |
266 | ///////////////////////////////////////////////////////////////////////////// |
267 | ///////////////////////////////////////////////////////////////////////////// |
267 | |
268 | |
268 | #define HDRSIZE 6 |
269 | #define HDRSIZE 6 |
269 | |
270 | |
270 | inline void encode_header (char *data, int clientid, int seqno, int retry = 0) |
271 | inline void encode_header (char *data, int clientid, int seqno, int retry = 0) |
271 | { |
272 | { |
272 | seqno &= SEQNO_MASK; |
273 | seqno &= SEQNO_MASK; |
273 | |
274 | |
274 | u8 hdr[3] = { |
275 | u8 hdr[3] = { |
… | |
… | |
408 | struct dns_cfg |
409 | struct dns_cfg |
409 | { |
410 | { |
410 | static int next_uid; |
411 | static int next_uid; |
411 | |
412 | |
412 | u8 id1, id2, id3, id4; |
413 | u8 id1, id2, id3, id4; |
|
|
414 | |
413 | u8 version; |
415 | u8 version; |
|
|
416 | u8 flags; |
414 | u8 rrtype; |
417 | u8 rrtype; |
415 | u8 flags; |
|
|
416 | u8 def_ttl; |
418 | u8 def_ttl; |
417 | u8 rcv_cdc; |
419 | |
418 | u8 snd_cdc; |
|
|
419 | u16 max_size; |
|
|
420 | u16 client; |
420 | u16 client; |
421 | u16 uid; // to make request unique |
421 | u16 uid; // to make request unique |
422 | |
422 | |
423 | u8 reserved[8]; |
423 | u16 max_size; |
|
|
424 | u8 seq_cdc; |
|
|
425 | u8 req_cdc; |
|
|
426 | |
|
|
427 | u8 rep_cdc; |
|
|
428 | u8 r2, r3, r4; |
|
|
429 | |
|
|
430 | u8 r5, r6, r7, r8; |
424 | |
431 | |
425 | void reset (int clientid); |
432 | void reset (int clientid); |
426 | bool valid (); |
433 | bool valid (); |
427 | }; |
434 | }; |
428 | |
435 | |
… | |
… | |
437 | |
444 | |
438 | version = 1; |
445 | version = 1; |
439 | |
446 | |
440 | rrtype = RR_TYPE_TXT; |
447 | rrtype = RR_TYPE_TXT; |
441 | flags = 0; |
448 | flags = 0; |
442 | def_ttl = 1; |
449 | def_ttl = 0; |
|
|
450 | seq_cdc = 26; |
|
|
451 | req_cdc = 62; |
443 | rcv_cdc = 0; |
452 | rep_cdc = 0; |
444 | snd_cdc = 62; |
|
|
445 | max_size = ntohs (MAX_PKT_SIZE); |
453 | max_size = ntohs (MAX_PKT_SIZE); |
446 | client = ntohs (clientid); |
454 | client = ntohs (clientid); |
447 | uid = next_uid++; |
455 | uid = next_uid++; |
448 | |
456 | |
449 | memset (reserved, 0, 8); |
457 | r2 = r3 = r4 = 0; |
|
|
458 | r4 = r5 = r6 = r7 = 0; |
450 | } |
459 | } |
451 | |
460 | |
452 | bool dns_cfg::valid () |
461 | bool dns_cfg::valid () |
453 | { |
462 | { |
454 | return id1 == 'G' |
463 | return id1 == 'G' |
455 | && id2 == 'V' |
464 | && id2 == 'V' |
456 | && id3 == 'P' |
465 | && id3 == 'P' |
457 | && id4 == 'E' |
466 | && id4 == 'E' |
|
|
467 | && seq_cdc == 26 |
|
|
468 | && req_cdc == 62 |
|
|
469 | && rep_cdc == 0 |
458 | && version == 1 |
470 | && version == 1 |
459 | && flags == 0 |
|
|
460 | && rcv_cdc == 0 |
|
|
461 | && snd_cdc == 62 |
|
|
462 | && max_size == ntohs (MAX_PKT_SIZE); |
471 | && max_size == ntohs (MAX_PKT_SIZE); |
463 | } |
472 | } |
464 | |
473 | |
465 | struct dns_packet : net_packet |
474 | struct dns_packet : net_packet |
466 | { |
475 | { |
467 | u16 id; |
476 | u16 id; |
468 | u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 |
477 | u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 |
469 | u16 qdcount, ancount, nscount, arcount; |
478 | u16 qdcount, ancount, nscount, arcount; |
470 | |
479 | |
471 | u8 data[MAXSIZE - 6 * 2]; |
480 | u8 data [MAXSIZE - 6 * 2]; |
472 | |
481 | |
473 | int decode_label (char *data, int size, int &offs); |
482 | int decode_label (char *data, int size, int &offs); |
474 | }; |
483 | }; |
475 | |
484 | |
476 | int dns_packet::decode_label (char *data, int size, int &offs) |
485 | int dns_packet::decode_label (char *data, int size, int &offs) |
… | |
… | |
662 | } |
671 | } |
663 | |
672 | |
664 | void dns_snd::gen_syn_req () |
673 | void dns_snd::gen_syn_req () |
665 | { |
674 | { |
666 | timeout = NOW + INITIAL_SYN_TIMEOUT; |
675 | timeout = NOW + INITIAL_SYN_TIMEOUT; |
667 | |
|
|
668 | printf ("send syn\n");//D |
|
|
669 | |
676 | |
670 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
677 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
671 | pkt->qdcount = htons (1); |
678 | pkt->qdcount = htons (1); |
672 | |
679 | |
673 | int offs = 6 * 2; |
680 | int offs = 6 * 2; |
… | |
… | |
806 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
813 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
807 | |
814 | |
808 | if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK)) |
815 | if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK)) |
809 | && pkt.qdcount == htons (1)) |
816 | && pkt.qdcount == htons (1)) |
810 | { |
817 | { |
811 | char qname[MAXSIZE]; |
818 | char qname [MAXSIZE]; |
812 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
819 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
813 | |
820 | |
814 | u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; |
821 | u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; |
815 | u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; |
822 | u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; |
816 | |
823 | |
… | |
… | |
823 | |
830 | |
824 | int dlen = strlen (THISNODE->domain); |
831 | int dlen = strlen (THISNODE->domain); |
825 | |
832 | |
826 | if (qclass == RR_CLASS_IN |
833 | if (qclass == RR_CLASS_IN |
827 | && qlen > dlen + 1 |
834 | && qlen > dlen + 1 |
828 | && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) |
835 | && !memcmp (qname + qlen - (dlen + 1), THISNODE->domain, dlen)) |
829 | { |
836 | { |
830 | // now generate reply |
837 | // now generate reply |
831 | pkt.ancount = htons (1); // one answer RR |
838 | pkt.ancount = htons (1); // one answer RR |
832 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
839 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
833 | |
840 | |
… | |
… | |
874 | // new packet, queue |
881 | // new packet, queue |
875 | rcv = new dns_rcv (seqno, data, datalen); |
882 | rcv = new dns_rcv (seqno, data, datalen); |
876 | dns->receive_rep (rcv); |
883 | dns->receive_rep (rcv); |
877 | } |
884 | } |
878 | |
885 | |
|
|
886 | { |
879 | pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section |
887 | pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section |
880 | |
888 | |
881 | int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A; |
889 | int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A; |
882 | pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; // type |
890 | pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; // type |
883 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
891 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
884 | pkt [offs++] = 0; pkt [offs++] = 0; |
892 | pkt [offs++] = 0; pkt [offs++] = 0; |
885 | pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL |
893 | pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL |
886 | |
894 | |
887 | int rdlen_offs = offs += 2; |
895 | int rdlen_offs = offs += 2; |
888 | |
896 | |
889 | int dlen = (dns ? ntohs (dns->cfg.max_size) : MAX_PKT_SIZE) - offs; |
897 | int dlen = (dns ? ntohs (dns->cfg.max_size) : MAX_PKT_SIZE) - offs; |
890 | // bind doesn't compress well, so reduce further by one label length |
898 | // bind doesn't compress well, so reduce further by one label length |
891 | dlen -= qlen; |
899 | dlen -= qlen; |
892 | |
900 | |
893 | if (dns) |
901 | if (dns) |
894 | { |
902 | { |
895 | // only put data into in-order sequence packets, if |
903 | // only put data into in-order sequence packets, if |
896 | // we receive out-of-order packets we generate empty |
904 | // we receive out-of-order packets we generate empty |
897 | // replies |
905 | // replies |
898 | while (dlen > 1 && !dns->snddq.empty () && in_seq) |
906 | while (dlen > 1 && !dns->snddq.empty () && in_seq) |
899 | { |
907 | { |
900 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
908 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
901 | |
909 | |
902 | if (txtlen > dns->snddq.size ()) |
910 | if (txtlen > dns->snddq.size ()) |
903 | txtlen = dns->snddq.size (); |
911 | txtlen = dns->snddq.size (); |
904 | |
912 | |
905 | pkt[offs++] = txtlen; |
913 | pkt[offs++] = txtlen; |
906 | memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); |
914 | memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); |
907 | offs += txtlen; |
915 | offs += txtlen; |
908 | dns->snddq.remove (txtlen); |
916 | dns->snddq.remove (txtlen); |
909 | |
917 | |
910 | dlen -= txtlen + 1; |
918 | dlen -= txtlen + 1; |
911 | } |
919 | } |
912 | |
920 | |
913 | // avoid empty TXT rdata |
921 | // avoid empty TXT rdata |
914 | if (offs == rdlen_offs) |
922 | if (offs == rdlen_offs) |
915 | pkt[offs++] = 0; |
923 | pkt[offs++] = 0; |
916 | |
924 | |
917 | slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ()); |
925 | slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ()); |
918 | } |
926 | } |
919 | else |
927 | else |
920 | { |
928 | { |
921 | // send RST |
929 | // send RST |
922 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
930 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
923 | pkt [offs++] = CMD_IP_RST; |
931 | pkt [offs++] = CMD_IP_RST; |
924 | } |
932 | } |
925 | |
933 | |
926 | int rdlen = offs - rdlen_offs; |
934 | int rdlen = offs - rdlen_offs; |
927 | |
935 | |
928 | pkt [rdlen_offs - 2] = rdlen >> 8; |
936 | pkt [rdlen_offs - 2] = rdlen >> 8; |
929 | pkt [rdlen_offs - 1] = rdlen; |
937 | pkt [rdlen_offs - 1] = rdlen; |
930 | |
938 | |
931 | if (dns) |
939 | if (dns) |
932 | { |
940 | { |
933 | // now update dns_rcv copy |
941 | // now update dns_rcv copy |
934 | rcv->pkt->len = offs; |
942 | rcv->pkt->len = offs; |
935 | memcpy (rcv->pkt->at (0), pkt.at (0), offs); |
943 | memcpy (rcv->pkt->at (0), pkt.at (0), offs); |
936 | } |
944 | } |
|
|
945 | } |
937 | |
946 | |
938 | duplicate_request: ; |
947 | duplicate_request: ; |
939 | } |
948 | } |
940 | else |
949 | else |
941 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
950 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
… | |
… | |
953 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
962 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
954 | pkt [offs++] = 0; pkt [offs++] = 0; |
963 | pkt [offs++] = 0; pkt [offs++] = 0; |
955 | pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL |
964 | pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL |
956 | pkt [offs++] = 0; pkt [offs++] = 4; // rdlength |
965 | pkt [offs++] = 0; pkt [offs++] = 4; // rdlength |
957 | |
966 | |
958 | slog (L_INFO, _("DNS: client %d tries to connect"), client); |
967 | slog (L_INFO, _("DNS: client %d connects"), client); |
959 | |
968 | |
960 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
969 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
961 | pkt [offs++] = CMD_IP_REJ; |
970 | pkt [offs++] = CMD_IP_REJ; |
962 | |
971 | |
963 | if (0 < client && client <= conns.size ()) |
972 | if (0 < client && client <= conns.size ()) |
… | |
… | |
1145 | |
1154 | |
1146 | pkt->len = recvfrom (w.fd, pkt->at (0), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
1155 | pkt->len = recvfrom (w.fd, pkt->at (0), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
1147 | |
1156 | |
1148 | if (pkt->len > 0) |
1157 | if (pkt->len > 0) |
1149 | { |
1158 | { |
1150 | if (THISNODE->dns_port) |
1159 | if (ntohs (pkt->flags) & FLAG_RESPONSE) |
|
|
1160 | dnsv4_client (*pkt); |
|
|
1161 | else |
1151 | { |
1162 | { |
1152 | dnsv4_server (*pkt); |
1163 | dnsv4_server (*pkt); |
1153 | sendto (w.fd, pkt->at (0), pkt->len, 0, (sockaddr *)&sa, sa_len); |
1164 | sendto (w.fd, pkt->at (0), pkt->len, 0, (sockaddr *)&sa, sa_len); |
1154 | } |
1165 | } |
1155 | else |
|
|
1156 | dnsv4_client (*pkt); |
|
|
1157 | |
1166 | |
1158 | delete pkt; |
1167 | delete pkt; |
1159 | } |
1168 | } |
1160 | } |
1169 | } |
1161 | } |
1170 | } |
… | |
… | |
1237 | { |
1246 | { |
1238 | if (vpn->dns_sndpq.empty ()) |
1247 | if (vpn->dns_sndpq.empty ()) |
1239 | { |
1248 | { |
1240 | send = new dns_snd (this); |
1249 | send = new dns_snd (this); |
1241 | |
1250 | |
1242 | printf ("new conn %p %d\n", this, c->conf->id);//D |
|
|
1243 | cfg.reset (THISNODE->id); |
1251 | cfg.reset (THISNODE->id); |
1244 | send->gen_syn_req (); |
1252 | send->gen_syn_req (); |
1245 | } |
1253 | } |
1246 | } |
1254 | } |
1247 | else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING |
1255 | else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING |