1 /* 2 * Copyright (c) 2016-2021, OARC, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in 14 * the documentation and/or other materials provided with the 15 * distribution. 16 * 17 * 3. Neither the name of the copyright holder nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include "dnscap_common.h" 36 37 #include <sys/socket.h> 38 #include <netinet/in.h> 39 #include <arpa/inet.h> 40 #include <stdio.h> 41 42 #ifndef __dnscap_dump_cds_h 43 #define __dnscap_dump_cds_h 44 45 #define DUMP_CDS_OK 0 46 #define DUMP_CDS_EINVAL 1 47 #define DUMP_CDS_ENOMEM 2 48 #define DUMP_CDS_ECBOR 3 49 #define DUMP_CDS_ELDNS 4 50 #define DUMP_CDS_EWRITE 5 51 #define DUMP_CDS_FLUSH 6 52 #define DUMP_CDS_ENOSUP 7 53 #define DUMP_CDS_EBUF 8 54 55 #define CDS_OPTION_RLABELS 0 56 #define CDS_OPTION_RLABEL_MIN_SIZE 1 57 #define CDS_OPTION_RDATA_RINDEX_SIZE 2 58 #define CDS_OPTION_RDATA_RINDEX_MIN_SIZE 3 59 #define CDS_OPTION_USE_RDATA_INDEX 4 60 #define CDS_OPTION_RDATA_INDEX_MIN_SIZE 5 61 62 #define CDS_DEFAULT_MAX_RLABELS 255 63 #define CDS_DEFAULT_MIN_RLABEL_SIZE 3 64 #define CDS_DEFAULT_RDATA_INDEX_MIN_SIZE 5 65 #define CDS_DEFAULT_RDATA_RINDEX_SIZE 255 66 #define CDS_DEFAULT_RDATA_RINDEX_MIN_SIZE 5 67 68 typedef struct ip_header ip_header_t; 69 struct ip_header { 70 unsigned short is_v6 : 1; 71 unsigned short is_reverse : 1; 72 unsigned short have_src_addr : 1; 73 unsigned short have_src_port : 1; 74 unsigned short have_dest_addr : 1; 75 unsigned short have_dest_port : 1; 76 77 uint8_t bits; 78 struct in_addr src_addr4; 79 uint16_t src_port4; 80 struct in6_addr src_addr6; 81 uint16_t src_port6; 82 struct in_addr dest_addr4; 83 uint16_t dest_port4; 84 struct in6_addr dest_addr6; 85 uint16_t dest_port6; 86 }; 87 88 typedef struct dns_label dns_label_t; 89 struct dns_label { 90 unsigned short is_complete : 1; 91 unsigned short have_size : 1; 92 unsigned short have_extension_bits : 1; 93 unsigned short have_offset : 1; 94 unsigned short have_label : 1; 95 unsigned short have_n_offset : 1; 96 97 uint8_t size; 98 uint8_t extension_bits; 99 uint16_t offset; 100 uint8_t* offset_p; 101 uint8_t* label; 102 size_t n_offset; 103 }; 104 105 #define CDS_RLABEL_LABEL_T_LABEL 64 106 107 typedef struct dns_rlabel_label dns_rlabel_label_t; 108 struct dns_rlabel_label { 109 unsigned short have_n_offset : 1; 110 111 uint8_t size; 112 uint8_t label[CDS_RLABEL_LABEL_T_LABEL]; 113 size_t n_offset; 114 }; 115 116 #define CDS_RLABEL_T_LABELS 256 117 118 typedef struct dns_rlabel dns_rlabel_t; 119 struct dns_rlabel { 120 dns_rlabel_t* next; 121 dns_rlabel_t* prev; 122 123 uint8_t labels; 124 dns_rlabel_label_t label[CDS_RLABEL_T_LABELS]; 125 }; 126 127 typedef struct dns_rdata dns_rdata_t; 128 struct dns_rdata { 129 unsigned short is_complete : 1; 130 unsigned short have_labels : 1; 131 unsigned short have_rlabel_idx : 1; 132 unsigned short have_rdata : 1; 133 134 size_t rdata_len; 135 uint8_t* rdata; 136 size_t labels; 137 dns_label_t* label; 138 size_t rlabel_idx; 139 }; 140 141 typedef struct dns_rr dns_rr_t; 142 struct dns_rr { 143 unsigned short is_complete : 1; 144 unsigned short have_labels : 1; 145 unsigned short have_rlabel_idx : 1; 146 unsigned short have_bits : 1; 147 unsigned short have_type : 1; 148 unsigned short have_class : 1; 149 unsigned short have_ttl : 1; 150 unsigned short have_rdlength : 1; 151 unsigned short have_rdata : 1; 152 unsigned short have_mixed_rdata : 1; 153 unsigned short have_rdata_index : 1; 154 unsigned short have_rdata_rindex : 1; 155 156 size_t labels; 157 dns_label_t* label; 158 size_t rlabel_idx; 159 uint8_t bits; 160 uint16_t type; 161 uint16_t class; 162 uint32_t ttl; 163 uint16_t rdlength; 164 uint8_t* rdata; 165 size_t mixed_rdatas; 166 dns_rdata_t* mixed_rdata; 167 size_t rdata_index; 168 size_t rdata_rindex; 169 }; 170 171 typedef struct dns dns_t; 172 struct dns { 173 unsigned short header_is_complete : 1; 174 unsigned short have_id : 1; 175 unsigned short have_raw : 1; 176 unsigned short have_cnt_bits : 1; 177 unsigned short have_qdcount : 1; 178 unsigned short have_ancount : 1; 179 unsigned short have_nscount : 1; 180 unsigned short have_arcount : 1; 181 unsigned short have_rr_bits : 1; 182 unsigned short have_questions : 1; 183 unsigned short have_answers : 1; 184 unsigned short have_authorities : 1; 185 unsigned short have_additionals : 1; 186 187 int id; 188 uint16_t raw; 189 uint8_t cnt_bits; 190 uint16_t qdcount; 191 uint16_t ancount; 192 uint16_t nscount; 193 uint16_t arcount; 194 uint8_t rr_bits; 195 size_t questions; 196 dns_rr_t* question; 197 size_t answers; 198 dns_rr_t* answer; 199 size_t authorities; 200 dns_rr_t* authority; 201 size_t additionals; 202 dns_rr_t* additional; 203 }; 204 205 int cds_set_cbor_size(size_t size); 206 int cds_set_message_size(size_t size); 207 int cds_set_max_rlabels(size_t size); 208 int cds_set_min_rlabel_size(size_t size); 209 int cds_set_use_rdata_index(int use); 210 int cds_set_use_rdata_rindex(int use); 211 int cds_set_rdata_index_min_size(size_t size); 212 int cds_set_rdata_rindex_min_size(size_t size); 213 int cds_set_rdata_rindex_size(size_t size); 214 int output_cds(iaddr from, iaddr to, uint8_t proto, unsigned flags, unsigned sport, unsigned dport, my_bpftimeval ts, const u_char* pkt_copy, size_t olen, const u_char* payload, size_t payloadlen); 215 int dump_cds(FILE* fp); 216 int have_cds_support(); 217 218 #endif /* __dnscap_dump_cds_h */ 219