dns.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <thread.h> /* for Ref */
  10. #define NS2MS(ns) ((ns) / 1000000L)
  11. #define S2MS(s) ((s) * 1000LL)
  12. #define timems() NS2MS(nsec())
  13. typedef struct Ndbtuple Ndbtuple;
  14. enum
  15. {
  16. /* RR types; see: http://www.iana.org/assignments/dns-parameters */
  17. Ta= 1,
  18. Tns= 2,
  19. Tmd= 3,
  20. Tmf= 4,
  21. Tcname= 5,
  22. Tsoa= 6,
  23. Tmb= 7,
  24. Tmg= 8,
  25. Tmr= 9,
  26. Tnull= 10,
  27. Twks= 11,
  28. Tptr= 12,
  29. Thinfo= 13,
  30. Tminfo= 14,
  31. Tmx= 15,
  32. Ttxt= 16,
  33. Trp= 17,
  34. Tafsdb= 18,
  35. Tx25= 19,
  36. Tisdn= 20,
  37. Trt= 21,
  38. Tnsap= 22,
  39. Tnsapptr= 23,
  40. Tsig= 24,
  41. Tkey= 25,
  42. Tpx= 26,
  43. Tgpos= 27,
  44. Taaaa= 28,
  45. Tloc= 29,
  46. Tnxt= 30,
  47. Teid= 31,
  48. Tnimloc= 32,
  49. Tsrv= 33,
  50. Tatma= 34,
  51. Tnaptr= 35,
  52. Tkx= 36,
  53. Tcert= 37,
  54. Ta6= 38,
  55. Tdname= 39,
  56. Tsink= 40,
  57. Topt= 41,
  58. Tapl= 42,
  59. Tds= 43,
  60. Tsshfp= 44,
  61. Tipseckey= 45,
  62. Trrsig= 46,
  63. Tnsec= 47,
  64. Tdnskey= 48,
  65. Tspf= 99,
  66. Tuinfo= 100,
  67. Tuid= 101,
  68. Tgid= 102,
  69. Tunspec= 103,
  70. /* query types (all RR types are also queries) */
  71. Ttkey= 249, /* transaction key */
  72. Ttsig= 250, /* transaction signature */
  73. Tixfr= 251, /* incremental zone transfer */
  74. Taxfr= 252, /* zone transfer */
  75. Tmailb= 253, /* { Tmb, Tmg, Tmr } */
  76. Tmaila= 254, /* obsolete */
  77. Tall= 255, /* all records */
  78. /* classes */
  79. Csym= 0, /* internal symbols */
  80. Cin= 1, /* internet */
  81. Ccs, /* CSNET (obsolete) */
  82. Cch, /* Chaos net */
  83. Chs, /* Hesiod (?) */
  84. /* class queries (all class types are also queries) */
  85. Call= 255, /* all classes */
  86. /* opcodes */
  87. Oquery= 0<<11, /* normal query */
  88. Oinverse= 1<<11, /* inverse query (retired) */
  89. Ostatus= 2<<11, /* status request */
  90. Onotify= 4<<11, /* notify slaves of updates */
  91. Oupdate= 5<<11,
  92. Omask= 0xf<<11, /* mask for opcode */
  93. /* response codes */
  94. Rok= 0,
  95. Rformat= 1, /* format error */
  96. Rserver= 2, /* server failure (e.g. no answer from something) */
  97. Rname= 3, /* bad name */
  98. Runimplimented= 4, /* unimplemented */
  99. Rrefused= 5, /* we don't like you */
  100. Ryxdomain= 6, /* name exists when it should not */
  101. Ryxrrset= 7, /* rr set exists when it should not */
  102. Rnxrrset= 8, /* rr set that should exist does not */
  103. Rnotauth= 9, /* not authoritative */
  104. Rnotzone= 10, /* name not in zone */
  105. Rbadvers= 16, /* bad opt version */
  106. /* Rbadsig= 16, */ /* also tsig signature failure */
  107. Rbadkey= 17, /* key not recognized */
  108. Rbadtime= 18, /* signature out of time window */
  109. Rbadmode= 19, /* bad tkey mode */
  110. Rbadname= 20, /* duplicate key name */
  111. Rbadalg= 21, /* algorithm not supported */
  112. Rmask= 0x1f, /* mask for response */
  113. Rtimeout= 1<<5, /* timeout sending (for internal use only) */
  114. /* bits in flag word (other than opcode and response) */
  115. Fresp= 1<<15, /* message is a response */
  116. Fauth= 1<<10, /* true if an authoritative response */
  117. Ftrunc= 1<<9, /* truncated message */
  118. Frecurse= 1<<8, /* request recursion */
  119. Fcanrec= 1<<7, /* server can recurse */
  120. Domlen= 256, /* max domain name length (with NULL) */
  121. Labellen= 64, /* max domain label length (with NULL) */
  122. Strlen= 256, /* max string length (with NULL) */
  123. /* time to live values (in seconds) */
  124. Min= 60,
  125. Hour= 60*Min, /* */
  126. Day= 24*Hour, /* Ta, Tmx */
  127. Week= 7*Day, /* Tsoa, Tns */
  128. Year= 52*Week,
  129. DEFTTL= Day,
  130. /* reserved time (can't be timed out earlier) */
  131. Reserved= 5*Min,
  132. /* tcp & udp port number */
  133. Dnsport= 53,
  134. /*
  135. * payload size. originally, 512 bytes was the upper bound, to
  136. * eliminate fragmentation when using udp transport.
  137. * with edns (rfc 6891), that has been raised to 4096.
  138. * we don't currently generate edns, but we might be sent edns packets.
  139. */
  140. Maxdnspayload= 512,
  141. Maxpayload= 4096,
  142. /* length of domain name hash table */
  143. HTLEN= 4*1024,
  144. Maxpath= 128, /* size of mntpt */
  145. Maxlcks= 10, /* max. query-type locks per domain name */
  146. RRmagic= 0xdeadbabe,
  147. DNmagic= 0xa110a110,
  148. /* parallelism: tune; was 32; allow lots */
  149. Maxactive= 250,
  150. /* tune; was 60*1000; keep it short */
  151. Maxreqtm= 8*1000, /* max. ms to process a request */
  152. Notauthoritative = 0,
  153. Authoritative,
  154. };
  155. typedef struct Area Area;
  156. typedef struct Block Block;
  157. typedef struct Cert Cert;
  158. typedef struct DN DN;
  159. typedef struct DNSmsg DNSmsg;
  160. typedef struct Key Key;
  161. typedef struct Null Null;
  162. typedef struct RR RR;
  163. typedef struct Request Request;
  164. typedef struct SOA SOA;
  165. typedef struct Server Server;
  166. typedef struct Sig Sig;
  167. typedef struct Srv Srv;
  168. typedef struct Txt Txt;
  169. /*
  170. * a structure to track a request and any slave process handling it
  171. */
  172. struct Request
  173. {
  174. int isslave; /* pid of slave */
  175. uint64_t aborttime; /* time in ms at which we give up */
  176. jmp_buf mret; /* where master jumps to after starting a slave */
  177. int id;
  178. char *from; /* who asked us? */
  179. };
  180. typedef struct Querylck Querylck;
  181. struct Querylck
  182. {
  183. QLock QLock;
  184. // Rendez;
  185. Ref Ref;
  186. };
  187. /*
  188. * a domain name
  189. */
  190. struct DN
  191. {
  192. DN *next; /* hash collision list */
  193. uint32_t magic;
  194. char *name; /* owner */
  195. RR *rr; /* resource records off this name */
  196. uint32_t referenced; /* time last referenced */
  197. uint32_t lookuptime; /* last time we tried to get a better value */
  198. /* refs was `char' but we've seen refs > 120, so go whole hog */
  199. uint32_t refs; /* for mark and sweep */
  200. uint32_t ordinal;
  201. uint16_t class; /* RR class */
  202. unsigned char keep; /* flag: never age this name */
  203. unsigned char respcode; /* response code */
  204. /* was: char nonexistent; *//* true if we get an authoritative nx for this domain */
  205. /* permit only 1 query per (domain name, type) at a time */
  206. Querylck querylck[Maxlcks];
  207. };
  208. /*
  209. * security info
  210. */
  211. struct Block
  212. {
  213. int dlen;
  214. unsigned char *data;
  215. };
  216. struct Key
  217. {
  218. int flags;
  219. int proto;
  220. int alg;
  221. Block Block;
  222. };
  223. struct Cert
  224. {
  225. int type;
  226. int tag;
  227. int alg;
  228. Block Block;
  229. };
  230. struct Sig
  231. {
  232. Cert Cert;
  233. int labels;
  234. uint32_t ttl;
  235. uint32_t exp;
  236. uint32_t incep;
  237. DN *signer;
  238. };
  239. struct Null
  240. {
  241. Block Block;
  242. };
  243. /*
  244. * text strings
  245. */
  246. struct Txt
  247. {
  248. Txt *next;
  249. char *p;
  250. };
  251. /*
  252. * an unpacked resource record
  253. */
  254. struct RR
  255. {
  256. RR *next;
  257. uint32_t magic;
  258. DN *owner; /* domain that owns this resource record */
  259. uintptr pc; /* for tracking memory allocation */
  260. uint32_t ttl; /* time to live to be passed on */
  261. uint32_t expire; /* time this entry expires locally */
  262. uint32_t marker; /* used locally when scanning rrlists */
  263. uint16_t type; /* RR type */
  264. uint16_t query; /* query type is in response to */
  265. unsigned char auth; /* flag: authoritative */
  266. unsigned char db; /* flag: from database */
  267. unsigned char cached; /* flag: rr in cache */
  268. unsigned char negative; /* flag: this is a cached negative response */
  269. union { /* discriminated by negative & type */
  270. DN *negsoaowner; /* soa for cached negative response */
  271. DN *host; /* hostname - soa, cname, mb, md, mf, mx, ns, srv */
  272. DN *cpu; /* cpu type - hinfo */
  273. DN *mb; /* mailbox - mg, minfo */
  274. DN *ip; /* ip address - a, aaaa */
  275. DN *rp; /* rp arg - rp */
  276. uintptr arg0; /* arg[01] are compared to find dups in dn.c */
  277. };
  278. union { /* discriminated by negative & type */
  279. int negrcode; /* response code for cached negative resp. */
  280. DN *rmb; /* responsible maibox - minfo, soa, rp */
  281. DN *ptr; /* pointer to domain name - ptr */
  282. DN *os; /* operating system - hinfo */
  283. uint32_t pref; /* preference value - mx */
  284. uint32_t local; /* ns served from local database - ns */
  285. uint16_t port; /* - srv */
  286. uintptr arg1; /* arg[01] are compared to find dups in dn.c */
  287. };
  288. union { /* discriminated by type */
  289. SOA *soa; /* soa timers - soa */
  290. Key *key;
  291. Cert *cert;
  292. Sig *sig;
  293. Null *null;
  294. Txt *txt;
  295. Srv *srv;
  296. };
  297. };
  298. /*
  299. * list of servers
  300. */
  301. struct Server
  302. {
  303. Server *next;
  304. char *name;
  305. };
  306. /*
  307. * timers for a start-of-authority record. all uint32_t's are in seconds.
  308. */
  309. struct SOA
  310. {
  311. uint32_t serial; /* zone serial # */
  312. uint32_t refresh; /* zone refresh interval */
  313. uint32_t retry; /* zone retry interval */
  314. uint32_t expire; /* time to expiration */
  315. uint32_t minttl; /* min. time to live for any entry */
  316. Server *slaves; /* slave servers */
  317. };
  318. /*
  319. * srv (service location) record (rfc2782):
  320. * _service._proto.name ttl class(IN) 'SRV' priority weight port target
  321. */
  322. struct Srv
  323. {
  324. uint16_t pri;
  325. uint16_t weight;
  326. };
  327. typedef struct Rrlist Rrlist;
  328. struct Rrlist
  329. {
  330. int count;
  331. RR *rrs;
  332. };
  333. /*
  334. * domain messages
  335. */
  336. struct DNSmsg
  337. {
  338. uint16_t id;
  339. int flags;
  340. int qdcount; /* questions */
  341. RR *qd;
  342. int ancount; /* answers */
  343. RR *an;
  344. int nscount; /* name servers */
  345. RR *ns;
  346. int arcount; /* hints */
  347. RR *ar;
  348. };
  349. /*
  350. * definition of local area for dblookup
  351. */
  352. struct Area
  353. {
  354. Area *next;
  355. int len; /* strlen(area->soarr->owner->name) */
  356. RR *soarr; /* soa defining this area */
  357. int neednotify;
  358. int needrefresh;
  359. };
  360. typedef struct Cfg Cfg;
  361. struct Cfg {
  362. int cachedb;
  363. int resolver;
  364. int justforw; /* flag: pure resolver, just forward queries */
  365. int serve; /* flag: serve udp queries */
  366. int inside;
  367. int straddle;
  368. };
  369. /* (udp) query stats */
  370. typedef struct {
  371. QLock QLock;
  372. uint32_t slavehiwat; /* procs */
  373. uint32_t qrecvd9p; /* query counts */
  374. uint32_t qrecvdudp;
  375. uint32_t qsent;
  376. uint32_t qrecvd9prpc; /* packet count */
  377. uint32_t alarms;
  378. /* reply times by count */
  379. uint32_t under10ths[3*10+2]; /* under n*0.1 seconds, n is index */
  380. uint32_t tmout;
  381. uint32_t tmoutcname;
  382. uint32_t tmoutv6;
  383. uint32_t answinmem; /* answers in memory */
  384. uint32_t negans; /* negative answers received */
  385. uint32_t negserver; /* neg ans with Rserver set */
  386. uint32_t negbaddeleg; /* neg ans with bad delegations */
  387. uint32_t negbdnoans; /* ⋯ and no answers */
  388. uint32_t negnorname; /* neg ans with no Rname set */
  389. uint32_t negcached; /* neg ans cached */
  390. } Stats;
  391. Stats stats;
  392. enum
  393. {
  394. Recurse,
  395. Dontrecurse,
  396. NOneg,
  397. OKneg,
  398. };
  399. extern Cfg cfg;
  400. extern char *dbfile;
  401. extern int debug;
  402. extern Area *delegated;
  403. extern char *logfile;
  404. extern int maxage; /* age of oldest entry in cache (secs) */
  405. extern char mntpt[];
  406. extern int needrefresh;
  407. extern int norecursion;
  408. extern uint32_t now; /* time base */
  409. extern int64_t nowns;
  410. extern Area *owned;
  411. extern int sendnotifies;
  412. extern uint32_t target;
  413. extern int testing; /* test cache whenever removing a DN */
  414. extern char *trace;
  415. extern int traceactivity;
  416. extern char *zonerefreshprogram;
  417. /* dn.c */
  418. extern char *rrtname[];
  419. extern char *rname[];
  420. extern unsigned nrname;
  421. extern char *opname[];
  422. extern Lock dnlock;
  423. void abort(); /* char*, ... */;
  424. void addserver(Server**, char*);
  425. Server* copyserverlist(Server*);
  426. void db2cache(int);
  427. void dnage(DN*);
  428. void dnageall(int);
  429. void dnagedb(void);
  430. void dnageallnever(void);
  431. void dnagenever(DN *, int);
  432. void dnauthdb(void);
  433. void dncheck(void*, int);
  434. void dndump(char*);
  435. void dnget(void);
  436. void dninit(void);
  437. DN* dnlookup(char*, int, int);
  438. void dnptr(unsigned char*, unsigned char*, char*, int, int, int);
  439. void dnpurge(void);
  440. void dnput(void);
  441. void dnslog(char*, ...);
  442. void dnstats(char *file);
  443. void* emalloc(int);
  444. char* estrdup(char*);
  445. void freeanswers(DNSmsg *mp);
  446. void freeserverlist(Server*);
  447. int getactivity(Request*, int);
  448. Area* inmyarea(char*);
  449. void putactivity(int);
  450. RR* randomize(RR*);
  451. RR* rralloc(int);
  452. void rrattach(RR*, int);
  453. int rravfmt(Fmt*);
  454. RR* rrcat(RR**, RR*);
  455. RR** rrcopy(RR*, RR**);
  456. int rrfmt(Fmt*);
  457. void rrfree(RR*);
  458. void rrfreelist(RR*);
  459. RR* rrlookup(DN*, int, int);
  460. char* rrname(int, char*, int);
  461. RR* rrremneg(RR**);
  462. RR* rrremtype(RR**, int);
  463. int rrsupported(int);
  464. int rrtype(char*);
  465. void slave(Request*);
  466. int subsume(char*, char*);
  467. int tsame(int, int);
  468. void unique(RR*);
  469. void warning(char*, ...);
  470. /* dnarea.c */
  471. void refresh_areas(Area*);
  472. void freearea(Area**);
  473. void addarea(DN *dp, RR *rp, Ndbtuple *t);
  474. /* dblookup.c */
  475. int baddelegation(RR*, RR*, unsigned char*);
  476. RR* dbinaddr(DN*, int);
  477. RR* dblookup(char*, int, int, int, int);
  478. void dnforceage(void);
  479. RR* dnsservers(int);
  480. RR* domainlist(int);
  481. int insideaddr(char *dom);
  482. int insidens(unsigned char *ip);
  483. int myaddr(char *addr);
  484. int opendatabase(void);
  485. unsigned char* outsidens(int);
  486. /* dns.c */
  487. char* walkup(char*);
  488. RR* getdnsservers(int);
  489. void logreply(int, unsigned char*, DNSmsg*);
  490. void logsend(int, int, unsigned char*, char*, char*, int);
  491. void procsetname(char *fmt, ...);
  492. /* dnresolve.c */
  493. RR* dnresolve(char*, int, int, Request*, RR**, int, int, int, int*);
  494. int udpport(char *);
  495. int mkreq(DN *dp, int type, unsigned char *buf, int flags, uint16_t reqno);
  496. int seerootns(void);
  497. void initdnsmsg(DNSmsg *mp, RR *rp, int flags, uint16_t reqno);
  498. DNSmsg* newdnsmsg(RR *rp, int flags, uint16_t reqno);
  499. /* dnserver.c */
  500. void dnserver(DNSmsg*, DNSmsg*, Request*, unsigned char *, int);
  501. void dnudpserver(char*);
  502. void dntcpserver(char*);
  503. /* dnnotify.c */
  504. void dnnotify(DNSmsg*, DNSmsg*, Request*);
  505. void notifyproc(void);
  506. /* convDNS2M.c */
  507. int convDNS2M(DNSmsg*, unsigned char*, int);
  508. /* convM2DNS.c */
  509. char* convM2DNS(unsigned char*, int, DNSmsg*, int*);