1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
/*
* Copyright (c) 2008-2024 OARC, Inc.
* Copyright (c) 2007-2008, Internet Systems Consortium, Inc.
* Copyright (c) 2003-2007, The Measurement Factory, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "query_classification_index.h"
#include "config_hooks.h"
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
enum {
CLASS_OK,
CLASS_NONAUTH_TLD,
CLASS_ROOT_SERVERS_NET,
CLASS_LOCALHOST,
CLASS_A_FOR_A,
CLASS_A_FOR_ROOT,
CLASS_RFC1918_PTR,
CLASS_FUNNY_QCLASS,
CLASS_FUNNY_QTYPE,
CLASS_SRC_PORT_ZERO,
CLASS_MALFORMED
};
static int
nonauth_tld(const dns_message* m)
{
unsigned int i;
const char* tld = dns_message_tld((dns_message*)m);
for (i = 0; KnownTLDS[i]; i++)
if (0 == strcmp(KnownTLDS[i], tld))
return 0;
return CLASS_NONAUTH_TLD;
}
static int
root_servers_net(const dns_message* m)
{
if (0 == strcmp(m->qname + strlen(m->qname) - 16, "root-servers.net"))
return CLASS_ROOT_SERVERS_NET;
return 0;
}
static int
localhost(const dns_message* m)
{
if (0 == strcmp(m->qname + strlen(m->qname) - 9, "localhost"))
return CLASS_LOCALHOST;
return 0;
}
static int
a_for_a(const dns_message* m)
{
struct in_addr a;
if (m->qtype != T_A)
return 0;
if (inet_aton(m->qname, &a))
return CLASS_A_FOR_A;
return 0;
}
static int
a_for_root(const dns_message* m)
{
if (m->qtype != T_A)
return 0;
if (0 == strcmp(m->qname, "."))
return CLASS_A_FOR_ROOT;
return 0;
}
static int
rfc1918_ptr(const dns_message* m)
{
char* tok = 0;
char* t;
char q[1024];
unsigned int i = 0;
if (m->qtype != T_PTR)
return 0;
q[sizeof(q) - 1] = 0;
strncpy(q, m->qname, sizeof(q));
if (q[sizeof(q) - 1] != 0) {
// String was truncated
return 0;
}
if (NULL == (t = strstr(q, ".in-addr.arpa")))
return 0;
*t = '\0';
for (t = strtok_r(q, ".", &tok); t; t = strtok_r(NULL, ".", &tok)) {
i >>= 8;
i |= ((atoi(t) & 0xff) << 24);
}
if ((i & 0xff000000) == 0x0a000000) /* 10.0.0.0/8 */
return CLASS_RFC1918_PTR;
if ((i & 0xfff00000) == 0xac100000) /* 172.16.0.0/12 */
return CLASS_RFC1918_PTR;
if ((i & 0xffff0000) == 0xc0a80000) /* 192.168.0.0/16 */
return CLASS_RFC1918_PTR;
return 0;
}
static int
funny_qclass(const dns_message* m)
{
switch (m->qclass) {
case C_IN:
case C_CHAOS:
case C_ANY:
case C_NONE:
case C_HS:
return 0;
default:
break;
}
return CLASS_FUNNY_QCLASS;
}
static int
funny_qtype(const dns_message* m)
{
switch (m->qtype) {
case T_A:
case T_NS:
case T_MD:
case T_MF:
case T_CNAME:
case T_SOA:
case T_MB:
case T_MG:
case T_MR:
case T_NULL:
case T_WKS:
case T_PTR:
case T_HINFO:
case T_MINFO:
case T_MX:
case T_TXT:
case T_RP:
case T_AFSDB:
case T_X25:
case T_ISDN:
case T_RT:
case T_NSAP:
case T_NSAP_PTR:
case T_SIG:
case T_KEY:
case T_PX:
case T_GPOS:
case T_AAAA:
case T_A6:
case T_LOC:
case T_NXT:
case T_EID:
case T_NIMLOC:
case T_SRV:
case T_ATMA:
case T_NAPTR:
case T_OPT:
case T_IXFR:
case T_AXFR:
case T_MAILB:
case T_MAILA:
case T_ANY:
return 0;
default:
break;
}
return CLASS_FUNNY_QTYPE;
}
static int
src_port_zero(const dns_message* m)
{
if (0 == m->tm->src_port)
return CLASS_SRC_PORT_ZERO;
return 0;
}
static int
malformed(const dns_message* m)
{
if (m->malformed)
return CLASS_MALFORMED;
return 0;
}
int query_classification_indexer(const dns_message* m)
{
int x;
if ((x = malformed(m)))
return x;
if ((x = src_port_zero(m)))
return x;
if ((x = funny_qclass(m)))
return x;
if ((x = funny_qtype(m)))
return x;
if ((x = a_for_a(m)))
return x;
if ((x = a_for_root(m)))
return x;
if ((x = localhost(m)))
return x;
if ((x = root_servers_net(m)))
return x;
if ((x = nonauth_tld(m)))
return x;
if ((x = rfc1918_ptr(m)))
return x;
return CLASS_OK;
}
int query_classification_iterator(const char** label)
{
static int next_iter = 0;
if (NULL == label) {
next_iter = 0;
return CLASS_MALFORMED + 1;
}
if (CLASS_OK == next_iter)
*label = "ok";
else if (CLASS_NONAUTH_TLD == next_iter)
*label = "non-auth-tld";
else if (CLASS_ROOT_SERVERS_NET == next_iter)
*label = "root-servers.net";
else if (CLASS_LOCALHOST == next_iter)
*label = "localhost";
else if (CLASS_A_FOR_ROOT == next_iter)
*label = "a-for-root";
else if (CLASS_A_FOR_A == next_iter)
*label = "a-for-a";
else if (CLASS_RFC1918_PTR == next_iter)
*label = "rfc1918-ptr";
else if (CLASS_FUNNY_QCLASS == next_iter)
*label = "funny-qclass";
else if (CLASS_FUNNY_QTYPE == next_iter)
*label = "funny-qtype";
else if (CLASS_SRC_PORT_ZERO == next_iter)
*label = "src-port-zero";
else if (CLASS_MALFORMED == next_iter)
*label = "malformed";
else
return -1;
return next_iter++;
}
|