Blame
Date:
Sat Aug 1 09:01:52 2020 UTC
Message:
move extended RCODE's out of the RCODE section those are only 0x00 through 0x0F make a section that shows extended RCODE's that are added to a EDNS0 tag.
001
2020-07-16
pjp
/*
002
2020-07-16
pjp
* Copyright (c) 2014-2020 Peter J. Philipp
003
2020-07-16
pjp
* All rights reserved.
004
2020-07-16
pjp
*
005
2020-07-16
pjp
* Redistribution and use in source and binary forms, with or without
006
2020-07-16
pjp
* modification, are permitted provided that the following conditions
007
2020-07-16
pjp
* are met:
008
2020-07-16
pjp
* 1. Redistributions of source code must retain the above copyright
009
2020-07-16
pjp
* notice, this list of conditions and the following disclaimer.
010
2020-07-16
pjp
* 2. Redistributions in binary form must reproduce the above copyright
011
2020-07-16
pjp
* notice, this list of conditions and the following disclaimer in the
012
2020-07-16
pjp
* documentation and/or other materials provided with the distribution.
013
2020-07-16
pjp
* 3. The name of the author may not be used to endorse or promote products
014
2020-07-16
pjp
* derived from this software without specific prior written permission
015
2020-07-16
pjp
*
016
2020-07-16
pjp
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
017
2020-07-16
pjp
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
018
2020-07-16
pjp
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
019
2020-07-16
pjp
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
020
2020-07-16
pjp
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
021
2020-07-16
pjp
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
022
2020-07-16
pjp
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
023
2020-07-16
pjp
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
024
2020-07-16
pjp
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
025
2020-07-16
pjp
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
026
2020-07-16
pjp
*
027
2020-07-16
pjp
*/
028
2020-07-16
pjp
029
2020-07-16
pjp
/*
030
2020-07-16
pjp
* $Id: passlist.c,v 1.1 2020/07/16 17:54:03 pjp Exp $
031
2020-07-16
pjp
*/
032
2020-07-16
pjp
033
2020-07-16
pjp
034
2020-07-16
pjp
/*
035
2020-07-16
pjp
* this file is based on filter.c
036
2020-07-16
pjp
*/
037
2020-07-16
pjp
038
2020-07-16
pjp
039
2020-07-16
pjp
#include <sys/types.h>
040
2020-07-16
pjp
#include <sys/socket.h>
041
2020-07-16
pjp
042
2020-07-16
pjp
#include <netinet/in.h>
043
2020-07-16
pjp
#include <arpa/inet.h>
044
2020-07-16
pjp
#include <netdb.h>
045
2020-07-16
pjp
046
2020-07-16
pjp
#include <stdio.h>
047
2020-07-16
pjp
#include <stdlib.h>
048
2020-07-16
pjp
#include <string.h>
049
2020-07-16
pjp
050
2020-07-16
pjp
#ifdef __linux__
051
2020-07-16
pjp
#include <grp.h>
052
2020-07-16
pjp
#define __USE_BSD 1
053
2020-07-16
pjp
#include <endian.h>
054
2020-07-16
pjp
#include <bsd/stdlib.h>
055
2020-07-16
pjp
#include <bsd/string.h>
056
2020-07-16
pjp
#include <bsd/sys/queue.h>
057
2020-07-16
pjp
#define __unused
058
2020-07-16
pjp
#include <bsd/sys/tree.h>
059
2020-07-16
pjp
#include <bsd/sys/endian.h>
060
2020-07-16
pjp
#else /* not linux */
061
2020-07-16
pjp
#include <sys/queue.h>
062
2020-07-16
pjp
#include <sys/tree.h>
063
2020-07-16
pjp
#endif /* __linux__ */
064
2020-07-16
pjp
065
2020-07-16
pjp
066
2020-07-16
pjp
#include "ddd-dns.h"
067
2020-07-16
pjp
#include "ddd-db.h"
068
2020-07-16
pjp
069
2020-07-16
pjp
int find_passlist(struct sockaddr_storage *, int);
070
2020-07-16
pjp
void init_passlist(void);
071
2020-07-16
pjp
int insert_passlist(char *, char *);
072
2020-07-16
pjp
073
2020-07-16
pjp
extern void dolog(int, char *, ...);
074
2020-07-16
pjp
extern in_addr_t getmask(int);
075
2020-07-16
pjp
extern int getmask6(int, struct sockaddr_in6 *);
076
2020-07-16
pjp
077
2020-07-16
pjp
extern int debug, verbose;
078
2020-07-16
pjp
079
2020-07-16
pjp
int passlist = 0; /* passlist is off by default */
080
2020-07-16
pjp
081
2020-07-16
pjp
SLIST_HEAD(listhead, passlistentry) passlisthead;
082
2020-07-16
pjp
083
2020-07-16
pjp
static struct passlistentry {
084
2020-07-16
pjp
char name[INET6_ADDRSTRLEN];
085
2020-07-16
pjp
int family;
086
2020-07-16
pjp
struct sockaddr_storage hostmask;
087
2020-07-16
pjp
struct sockaddr_storage netmask;
088
2020-07-16
pjp
u_int8_t prefixlen;
089
2020-07-16
pjp
SLIST_ENTRY(passlistentry) passlist_entry;
090
2020-07-16
pjp
} *wln2, *wlnp;
091
2020-07-16
pjp
092
2020-07-16
pjp
093
2020-07-16
pjp
/*
094
2020-07-16
pjp
* INIT_PASSLIST - initialize the passlist singly linked list
095
2020-07-16
pjp
*/
096
2020-07-16
pjp
097
2020-07-16
pjp
void
098
2020-07-16
pjp
init_passlist(void)
099
2020-07-16
pjp
{
100
2020-07-16
pjp
SLIST_INIT(&passlisthead);
101
2020-07-16
pjp
return;
102
2020-07-16
pjp
}
103
2020-07-16
pjp
104
2020-07-16
pjp
/*
105
2020-07-16
pjp
* INSERT_PASSLIST - insert an address and prefixlen into the passlist slist
106
2020-07-16
pjp
*/
107
2020-07-16
pjp
108
2020-07-16
pjp
int
109
2020-07-16
pjp
insert_passlist(char *address, char *prefixlen)
110
2020-07-16
pjp
{
111
2020-07-16
pjp
struct sockaddr_in *sin;
112
2020-07-16
pjp
struct sockaddr_in6 *sin6;
113
2020-07-16
pjp
int pnum;
114
2020-07-16
pjp
int ret;
115
2020-07-16
pjp
116
2020-07-16
pjp
pnum = atoi(prefixlen);
117
2020-07-16
pjp
wln2 = malloc(sizeof(struct passlistentry)); /* Insert after. */
118
2020-07-16
pjp
119
2020-07-16
pjp
if (strchr(address, ':') != NULL) {
120
2020-07-16
pjp
wln2->family = AF_INET6;
121
2020-07-16
pjp
sin6 = (struct sockaddr_in6 *)&wln2->hostmask;
122
2020-07-16
pjp
if ((ret = inet_pton(AF_INET6, address, &sin6->sin6_addr.s6_addr)) != 1)
123
2020-07-16
pjp
return (-1);
124
2020-07-16
pjp
sin6->sin6_family = AF_INET6;
125
2020-07-16
pjp
sin6 = (struct sockaddr_in6 *)&wln2->netmask;
126
2020-07-16
pjp
sin6->sin6_family = AF_INET6;
127
2020-07-16
pjp
if (getmask6(pnum, sin6) < 0)
128
2020-07-16
pjp
return(-1);
129
2020-07-16
pjp
wln2->prefixlen = pnum;
130
2020-07-16
pjp
} else {
131
2020-07-16
pjp
132
2020-07-16
pjp
wln2->family = AF_INET;
133
2020-07-16
pjp
sin = (struct sockaddr_in *)&wln2->hostmask;
134
2020-07-16
pjp
sin->sin_family = AF_INET;
135
2020-07-16
pjp
sin->sin_addr.s_addr = inet_addr(address);
136
2020-07-16
pjp
sin = (struct sockaddr_in *)&wln2->netmask;
137
2020-07-16
pjp
sin->sin_family = AF_INET;
138
2020-07-16
pjp
sin->sin_addr.s_addr = getmask(pnum);
139
2020-07-16
pjp
wln2->prefixlen = pnum;
140
2020-07-16
pjp
141
2020-07-16
pjp
}
142
2020-07-16
pjp
143
2020-07-16
pjp
SLIST_INSERT_HEAD(&passlisthead, wln2, passlist_entry);
144
2020-07-16
pjp
145
2020-07-16
pjp
return (0);
146
2020-07-16
pjp
}
147
2020-07-16
pjp
148
2020-07-16
pjp
/*
149
2020-07-16
pjp
* FIND_PASSLIST - walk the passlist list and find the correponding network
150
2020-07-16
pjp
* if a network matches return 1, if no match is found return
151
2020-07-16
pjp
* 0.
152
2020-07-16
pjp
*/
153
2020-07-16
pjp
154
2020-07-16
pjp
int
155
2020-07-16
pjp
find_passlist(struct sockaddr_storage *sst, int family)
156
2020-07-16
pjp
{
157
2020-07-16
pjp
struct sockaddr_in *sin, *sin0;
158
2020-07-16
pjp
struct sockaddr_in6 *sin6, *sin60, *sin61;
159
2020-07-16
pjp
u_int32_t hostmask, netmask;
160
2020-07-16
pjp
u_int32_t a;
161
2020-07-16
pjp
#ifdef __amd64
162
2020-07-16
pjp
u_int64_t *hm[2], *nm[2], *a6[2];
163
2020-07-16
pjp
#else
164
2020-07-16
pjp
u_int32_t *hm[4], *nm[4], *a6[4];
165
2020-07-16
pjp
#endif
166
2020-07-16
pjp
167
2020-07-16
pjp
SLIST_FOREACH(wlnp, &passlisthead, passlist_entry) {
168
2020-07-16
pjp
if (wlnp->family == AF_INET) {
169
2020-07-16
pjp
if (family != AF_INET)
170
2020-07-16
pjp
continue;
171
2020-07-16
pjp
sin = (struct sockaddr_in *)sst;
172
2020-07-16
pjp
a = sin->sin_addr.s_addr;
173
2020-07-16
pjp
sin = (struct sockaddr_in *)&wlnp->hostmask;
174
2020-07-16
pjp
sin0 = (struct sockaddr_in *)&wlnp->netmask;
175
2020-07-16
pjp
hostmask = sin->sin_addr.s_addr;
176
2020-07-16
pjp
netmask = sin0->sin_addr.s_addr;
177
2020-07-16
pjp
if ((hostmask & netmask) == (a & netmask)) {
178
2020-07-16
pjp
return (1);
179
2020-07-16
pjp
} /* if hostmask */
180
2020-07-16
pjp
} else if (wlnp->family == AF_INET6) {
181
2020-07-16
pjp
if (family != AF_INET6)
182
2020-07-16
pjp
continue;
183
2020-07-16
pjp
sin6 = (struct sockaddr_in6 *)sst;
184
2020-07-16
pjp
sin60 = (struct sockaddr_in6 *)&wlnp->hostmask;
185
2020-07-16
pjp
sin61 = (struct sockaddr_in6 *)&wlnp->netmask;
186
2020-07-16
pjp
#ifdef __amd64
187
2020-07-16
pjp
/*
188
2020-07-16
pjp
* If this is on a 64 bit machine, we'll benefit
189
2020-07-16
pjp
* by using 64 bit registers, this should make it
190
2020-07-16
pjp
* a tad faster...
191
2020-07-16
pjp
*/
192
2020-07-16
pjp
hm[0] = (u_int64_t *)&sin60->sin6_addr.s6_addr;
193
2020-07-16
pjp
hm[1] = (hm[0] + 1);
194
2020-07-16
pjp
nm[0] = (u_int64_t *)&sin61->sin6_addr.s6_addr;
195
2020-07-16
pjp
nm[1] = (nm[0] + 1);
196
2020-07-16
pjp
a6[0] = (u_int64_t *)&sin6->sin6_addr.s6_addr;
197
2020-07-16
pjp
a6[1] = (a6[0] + 1);
198
2020-07-16
pjp
if ( ((*hm[0] & *nm[0]) == (*a6[0] & *nm[0]))&&
199
2020-07-16
pjp
((*hm[1] & *nm[1]) == (*a6[1] & *nm[1]))) {
200
2020-07-16
pjp
#else
201
2020-07-16
pjp
hm[0] = (u_int32_t *)&sin60->sin6_addr.s6_addr;
202
2020-07-16
pjp
hm[1] = (hm[0] + 1); hm[2] = (hm[1] + 1);
203
2020-07-16
pjp
hm[3] = (hm[2] + 1);
204
2020-07-16
pjp
nm[0] = (u_int32_t *)&sin61->sin6_addr.s6_addr;
205
2020-07-16
pjp
nm[1] = (nm[0] + 1); nm[2] = (nm[1] + 1);
206
2020-07-16
pjp
nm[3] = (nm[2] + 1);
207
2020-07-16
pjp
a6[0] = (u_int32_t *)&sin6->sin6_addr.s6_addr;
208
2020-07-16
pjp
a6[1] = (a6[0] + 1); a6[2] = (a6[1] + 1);
209
2020-07-16
pjp
a6[3] = (a6[2] + 1);
210
2020-07-16
pjp
211
2020-07-16
pjp
if ( ((*hm[0] & *nm[0]) == (*a6[0] & *nm[0]))&&
212
2020-07-16
pjp
((*hm[1] & *nm[1]) == (*a6[1] & *nm[1]))&&
213
2020-07-16
pjp
((*hm[2] & *nm[2]) == (*a6[2] & *nm[2]))&&
214
2020-07-16
pjp
((*hm[3] & *nm[3]) == (*a6[3] & *nm[3]))) {
215
2020-07-16
pjp
#endif
216
2020-07-16
pjp
217
2020-07-16
pjp
return (1);
218
2020-07-16
pjp
} /* if ip6 address */
219
2020-07-16
pjp
220
2020-07-16
pjp
} /* if AF_INET6 */
221
2020-07-16
pjp
} /* SLIST */
222
2020-07-16
pjp
223
2020-07-16
pjp
return (0);
224
2020-07-16
pjp
}
repomaster@centroid.eu