2 subnet.c -- handle subnet lookups and lists
3 Copyright (C) 2000 Guus Sliepen <guus@sliepen.warande.net>,
4 2000 Ivo Timmermans <itimmermans@bigfoot.com>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 $Id: subnet.c,v 1.1.2.15 2000/11/24 23:13:06 guus Exp $
30 #include "connection.h"
38 /* lists type of subnet */
40 rbltree_t *subnet_tree;
42 void init_subnets(void)
45 subnet_tree = new_rbltree((rbl_compare_t)subnet_compare, (rbl_action_t)free_subnet);
49 /* Subnet comparison */
51 int subnet_compare_mac(subnet_t *a, subnet_t *b)
54 return memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
57 int subnet_compare_ipv4(subnet_t *a, subnet_t *b)
60 /* If the subnet of a falls within the range of subnet b,
61 then we consider a smaller then b.
62 Otherwise, the addresses alone (and not the subnet masks) will be compared.
65 if(a->net.ipv4.mask > b->net.ipv4.mask)
66 if((a->net.ipv4.address & b->net.ipv4.mask) == b->net.ipv4.address)
69 return a->net.ipv4.address - b->net.ipv4.address;
72 int subnet_compare_ipv6(subnet_t *a, subnet_t *b)
75 /* Same as ipv4 case, but with nasty 128 bit addresses */
77 if(memcmp(&a->net.ipv6.mask, &b->net.ipv6.mask, sizeof(ipv6_t)) > 0)
78 if((a->net.ipv6.address.x[0] & b->net.ipv6.mask.x[0]) == b->net.ipv6.address.x[0] &&
79 (a->net.ipv6.address.x[1] & b->net.ipv6.mask.x[1]) == b->net.ipv6.address.x[1] &&
80 (a->net.ipv6.address.x[2] & b->net.ipv6.mask.x[2]) == b->net.ipv6.address.x[2] &&
81 (a->net.ipv6.address.x[3] & b->net.ipv6.mask.x[3]) == b->net.ipv6.address.x[3] &&
82 (a->net.ipv6.address.x[4] & b->net.ipv6.mask.x[4]) == b->net.ipv6.address.x[4] &&
83 (a->net.ipv6.address.x[5] & b->net.ipv6.mask.x[5]) == b->net.ipv6.address.x[5] &&
84 (a->net.ipv6.address.x[6] & b->net.ipv6.mask.x[6]) == b->net.ipv6.address.x[6] &&
85 (a->net.ipv6.address.x[7] & b->net.ipv6.mask.x[7]) == b->net.ipv6.address.x[7])
88 return memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
91 int subnet_compare(subnet_t *a, subnet_t *b)
95 x = a->type - b->type;
102 return subnet_compare_mac(a, b);
104 return subnet_compare_ipv4(a, b);
106 return subnet_compare_ipv6(a, b);
108 syslog(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, restarting!"), a->type);
114 /* Allocating and freeing space for subnets */
116 subnet_t *new_subnet(void)
119 return (subnet_t *)xmalloc(sizeof(subnet_t));
122 void free_subnet(subnet_t *subnet)
128 /* Linked list management */
130 void subnet_add(connection_t *cl, subnet_t *subnet)
134 rbl_insert(subnet_tree, subnet);
135 rbl_insert(cl->subnet_tree, subnet);
139 void subnet_del(subnet_t *subnet)
142 rbl_delete(subnet->owner->subnet_tree, subnet);
144 rbl_delete(subnet_tree, subnet);
148 /* Ascii representation of subnets */
150 subnet_t *str2net(char *subnetstr)
155 if(sscanf(subnetstr, "%d,", &type) != 1)
158 subnet = new_subnet();
163 if(sscanf(subnetstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &subnet->type,
164 &subnet->net.mac.address.x[0],
165 &subnet->net.mac.address.x[1],
166 &subnet->net.mac.address.x[2],
167 &subnet->net.mac.address.x[3],
168 &subnet->net.mac.address.x[4],
169 &subnet->net.mac.address.x[5]) != 7)
176 if(sscanf(subnetstr, "%d,%lx/%lx", &subnet->type, &subnet->net.ipv4.address, &subnet->net.ipv4.mask) != 3)
183 if(sscanf(subnetstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", &subnet->type,
184 &subnet->net.ipv6.address.x[0],
185 &subnet->net.ipv6.address.x[1],
186 &subnet->net.ipv6.address.x[2],
187 &subnet->net.ipv6.address.x[3],
188 &subnet->net.ipv6.address.x[4],
189 &subnet->net.ipv6.address.x[5],
190 &subnet->net.ipv6.address.x[6],
191 &subnet->net.ipv6.address.x[7],
192 &subnet->net.ipv6.mask.x[0],
193 &subnet->net.ipv6.mask.x[1],
194 &subnet->net.ipv6.mask.x[2],
195 &subnet->net.ipv6.mask.x[3],
196 &subnet->net.ipv6.mask.x[4],
197 &subnet->net.ipv6.mask.x[5],
198 &subnet->net.ipv6.mask.x[6],
199 &subnet->net.ipv6.mask.x[7]) != 17)
213 char *net2str(subnet_t *subnet)
220 asprintf(&netstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", subnet->type,
221 subnet->net.mac.address.x[0],
222 subnet->net.mac.address.x[1],
223 subnet->net.mac.address.x[2],
224 subnet->net.mac.address.x[3],
225 subnet->net.mac.address.x[4],
226 subnet->net.mac.address.x[5]);
229 asprintf(&netstr, "%d,%lx/%lx", subnet->type, subnet->net.ipv4.address, subnet->net.ipv4.mask);
232 asprintf(&netstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", subnet->type,
233 subnet->net.ipv6.address.x[0],
234 subnet->net.ipv6.address.x[1],
235 subnet->net.ipv6.address.x[2],
236 subnet->net.ipv6.address.x[3],
237 subnet->net.ipv6.address.x[4],
238 subnet->net.ipv6.address.x[5],
239 subnet->net.ipv6.address.x[6],
240 subnet->net.ipv6.address.x[7],
241 subnet->net.ipv6.mask.x[0],
242 subnet->net.ipv6.mask.x[1],
243 subnet->net.ipv6.mask.x[2],
244 subnet->net.ipv6.mask.x[3],
245 subnet->net.ipv6.mask.x[4],
246 subnet->net.ipv6.mask.x[5],
247 subnet->net.ipv6.mask.x[6],
248 subnet->net.ipv6.mask.x[7]);
251 asprintf(&netstr, _("unknown"));
257 /* Subnet lookup routines */
259 subnet_t *lookup_subnet_mac(mac_t address)
263 subnet.type = SUBNET_MAC;
264 subnet.net.mac.address = address;
266 p = (subnet_t *)rbl_search_closest(subnet_tree, &subnet);
268 if(p && !memcmp(&address, &p->net.mac.address, sizeof(mac_t)))
274 subnet_t *lookup_subnet_ipv4(ipv4_t address)
278 subnet.type = SUBNET_IPV4;
279 subnet.net.ipv4.address = address;
280 subnet.net.ipv4.mask = 0xFFFFFFFF;
282 p = (subnet_t *)rbl_search_closest_greater(subnet_tree, &subnet);
284 /* Check if the found subnet REALLY matches */
286 if(p && ((address & p->net.ipv4.mask) == p->net.ipv4.address))
292 subnet_t *lookup_subnet_ipv6(ipv6_t address)
296 subnet.type = SUBNET_IPV6;
297 subnet.net.ipv6.address = address;
298 memset(&subnet.net.ipv6.mask, 0xFF, 16);
300 /* FIXME: check if it REALLY matches */
302 return (subnet_t *)rbl_search_closest(subnet_tree, &subnet);
305 void dump_subnet_list(void)
311 syslog(LOG_DEBUG, _("Subnet list:"));
312 RBL_FOREACH(subnet_tree, rbl)
314 subnet = (subnet_t *)rbl->data;
315 netstr = net2str(subnet);
316 syslog(LOG_DEBUG, " %s owner %s", netstr, subnet->owner->name);
319 syslog(LOG_DEBUG, _("End of subnet list."));