1 /* 2 * edns.h -- EDNS definitions (RFC 2671). 3 * 4 * Copyright (c) 2001-2006, NLnet Labs. All rights reserved. 5 * 6 * See LICENSE for the license. 7 * 8 */ 9 10 #ifndef EDNS_H 11 #define EDNS_H 12 13 #include "buffer.h" 14 struct nsd; 15 struct query; 16 17 #define OPT_LEN 9U /* Length of the NSD EDNS response record minus 2 */ 18 #define OPT_RDATA 2 /* holds the rdata length comes after OPT_LEN */ 19 #define OPT_HDR 4U /* NSID opt header length */ 20 #define NSID_CODE 3 /* nsid option code */ 21 #define COOKIE_CODE 10 /* COOKIE option code */ 22 #define EDE_CODE 15 /* Extended DNS Errors option code */ 23 #define ZONEVERSION_CODE 19 /* ZONEVERSION option code */ 24 #define DNSSEC_OK_MASK 0x8000U /* do bit mask */ 25 26 /* https://iana.org/assignments/dns-parameters/#zoneversion-type-values */ 27 #define ZONEVERSION_SOA_SERIAL 0 28 29 struct edns_data 30 { 31 unsigned char ok[OPT_LEN]; 32 unsigned char error[OPT_LEN]; 33 unsigned char rdata_none[OPT_RDATA]; 34 unsigned char nsid[OPT_HDR]; 35 unsigned char cookie[OPT_HDR]; 36 }; 37 typedef struct edns_data edns_data_type; 38 39 enum edns_status 40 { 41 EDNS_NOT_PRESENT, 42 EDNS_OK, 43 /* EDNS states may be extended in the future */ 44 EDNS_ERROR 45 }; 46 typedef enum edns_status edns_status_type; 47 48 enum cookie_status 49 { 50 COOKIE_NOT_PRESENT, 51 COOKIE_UNVERIFIED, 52 COOKIE_VALID, 53 COOKIE_VALID_REUSE, 54 COOKIE_INVALID 55 }; 56 typedef enum cookie_status cookie_status_type; 57 58 struct edns_record 59 { 60 edns_status_type status; 61 size_t position; 62 size_t maxlen; 63 size_t opt_reserved_space; 64 int dnssec_ok; 65 int nsid; 66 int zoneversion; 67 cookie_status_type cookie_status; 68 size_t cookie_len; 69 uint8_t cookie[40]; 70 int ede; /* RFC 8914 - Extended DNS Errors */ 71 char* ede_text; /* RFC 8914 - Extended DNS Errors text*/ 72 uint16_t ede_text_len; 73 }; 74 typedef struct edns_record edns_record_type; 75 76 /* The Extended DNS Error codes (RFC8914) we use */ 77 #define EDE_OTHER 0 78 #define EDE_NOT_READY 14 79 #define EDE_PROHIBITED 18 80 #define EDE_NOT_AUTHORITATIVE 20 81 #define EDE_NOT_SUPPORTED 21 82 #define EDE_INVALID_DATA 24 83 84 /* ASSIGN_EDE_CODE_AND_STRING_LITERAL may only be used with string literals. 85 * This is guaranteed by concatenating and empty string to LITERAL, which 86 * will make compilation fail if this macro is used with variables. 87 */ 88 #define ASSIGN_EDE_CODE_AND_STRING_LITERAL(EDE, CODE, LITERAL) \ 89 do { \ 90 EDE = (CODE); \ 91 EDE ## _text = (LITERAL ""); \ 92 EDE ## _text_len = sizeof(LITERAL) - 1; \ 93 } while (0) 94 95 void edns_init_data(edns_data_type *data, uint16_t max_length); 96 void edns_init_record(edns_record_type *data); 97 int edns_parse_record(edns_record_type *data, buffer_type *packet, 98 struct query* q, struct nsd* nsd); 99 100 /* 101 * The amount of space to reserve in the response for the EDNS data 102 * (if required). 103 */ 104 size_t edns_reserved_space(edns_record_type *data); 105 106 void edns_init_nsid(edns_data_type *data, uint16_t nsid_len); 107 108 void cookie_verify(struct query *q, struct nsd* nsd, uint32_t *now_p); 109 void cookie_create(struct query *q, struct nsd* nsd, uint32_t *now_p); 110 111 #endif /* EDNS_H */ 112