2 subnet.c -- handle subnet lookups and lists
3 Copyright (C) 2000,2001 Guus Sliepen <guus@sliepen.warande.net>,
4 2000,2001 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.24 2001/08/28 20:52:39 guus Exp $
31 #include "connection.h"
39 /* lists type of subnet */
41 avl_tree_t *subnet_tree;
43 void init_subnets(void)
46 subnet_tree = avl_alloc_tree((avl_compare_t)subnet_compare, (avl_action_t)free_subnet);
50 /* Subnet comparison */
52 int subnet_compare_mac(subnet_t *a, subnet_t *b)
55 return memcmp(&a->net.mac.address, &b->net.mac.address, sizeof(mac_t));
58 int subnet_compare_ipv4(subnet_t *a, subnet_t *b)
61 /* We compare as if a subnet is a number that equals (address << 32 + netmask). */
63 if(a->net.ipv4.address == b->net.ipv4.address)
65 if(a->net.ipv4.mask < b->net.ipv4.mask)
67 else if(a->net.ipv4.mask > b->net.ipv4.mask)
74 if(a->net.ipv4.address < b->net.ipv4.address)
76 else if(a->net.ipv4.address > b->net.ipv4.address)
83 int subnet_compare_ipv6(subnet_t *a, subnet_t *b)
86 /* Same as ipv4 case, but with nasty 128 bit addresses */
88 if(memcmp(&a->net.ipv6.mask, &b->net.ipv6.mask, sizeof(ipv6_t)) > 0)
89 if((a->net.ipv6.address.x[0] & b->net.ipv6.mask.x[0]) == b->net.ipv6.address.x[0] &&
90 (a->net.ipv6.address.x[1] & b->net.ipv6.mask.x[1]) == b->net.ipv6.address.x[1] &&
91 (a->net.ipv6.address.x[2] & b->net.ipv6.mask.x[2]) == b->net.ipv6.address.x[2] &&
92 (a->net.ipv6.address.x[3] & b->net.ipv6.mask.x[3]) == b->net.ipv6.address.x[3] &&
93 (a->net.ipv6.address.x[4] & b->net.ipv6.mask.x[4]) == b->net.ipv6.address.x[4] &&
94 (a->net.ipv6.address.x[5] & b->net.ipv6.mask.x[5]) == b->net.ipv6.address.x[5] &&
95 (a->net.ipv6.address.x[6] & b->net.ipv6.mask.x[6]) == b->net.ipv6.address.x[6] &&
96 (a->net.ipv6.address.x[7] & b->net.ipv6.mask.x[7]) == b->net.ipv6.address.x[7])
99 return memcmp(&a->net.ipv6.address, &b->net.ipv6.address, sizeof(ipv6_t));
102 int subnet_compare(subnet_t *a, subnet_t *b)
106 x = a->type - b->type;
113 return subnet_compare_mac(a, b);
115 return subnet_compare_ipv4(a, b);
117 return subnet_compare_ipv6(a, b);
119 syslog(LOG_ERR, _("subnet_compare() was called with unknown subnet type %d, restarting!"), a->type);
125 /* Allocating and freeing space for subnets */
127 subnet_t *new_subnet(void)
130 return (subnet_t *)xmalloc(sizeof(subnet_t));
133 void free_subnet(subnet_t *subnet)
139 /* Linked list management */
141 void subnet_add(connection_t *cl, subnet_t *subnet)
146 while(!avl_insert(subnet_tree, subnet))
150 old = (subnet_t *)avl_search(subnet_tree, subnet);
152 if(debug_lvl >= DEBUG_PROTOCOL)
155 subnetstr = net2str(subnet);
156 syslog(LOG_WARNING, _("Duplicate subnet %s for %s (%s), previous owner %s (%s)!"),
157 subnetstr, cl->name, cl->hostname, old->owner->name, old->owner->hostname);
164 avl_insert(cl->subnet_tree, subnet);
168 void subnet_del(subnet_t *subnet)
171 avl_delete(subnet->owner->subnet_tree, subnet);
173 avl_delete(subnet_tree, subnet);
177 /* Ascii representation of subnets */
179 subnet_t *str2net(char *subnetstr)
184 if(sscanf(subnetstr, "%d,", &type) != 1)
187 subnet = new_subnet();
192 if(sscanf(subnetstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &subnet->type,
193 &subnet->net.mac.address.x[0],
194 &subnet->net.mac.address.x[1],
195 &subnet->net.mac.address.x[2],
196 &subnet->net.mac.address.x[3],
197 &subnet->net.mac.address.x[4],
198 &subnet->net.mac.address.x[5]) != 7)
205 if(sscanf(subnetstr, "%d,%lx/%lx", &subnet->type, &subnet->net.ipv4.address, &subnet->net.ipv4.mask) != 3)
212 if(sscanf(subnetstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", &subnet->type,
213 &subnet->net.ipv6.address.x[0],
214 &subnet->net.ipv6.address.x[1],
215 &subnet->net.ipv6.address.x[2],
216 &subnet->net.ipv6.address.x[3],
217 &subnet->net.ipv6.address.x[4],
218 &subnet->net.ipv6.address.x[5],
219 &subnet->net.ipv6.address.x[6],
220 &subnet->net.ipv6.address.x[7],
221 &subnet->net.ipv6.mask.x[0],
222 &subnet->net.ipv6.mask.x[1],
223 &subnet->net.ipv6.mask.x[2],
224 &subnet->net.ipv6.mask.x[3],
225 &subnet->net.ipv6.mask.x[4],
226 &subnet->net.ipv6.mask.x[5],
227 &subnet->net.ipv6.mask.x[6],
228 &subnet->net.ipv6.mask.x[7]) != 17)
242 char *net2str(subnet_t *subnet)
249 asprintf(&netstr, "%d,%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", subnet->type,
250 subnet->net.mac.address.x[0],
251 subnet->net.mac.address.x[1],
252 subnet->net.mac.address.x[2],
253 subnet->net.mac.address.x[3],
254 subnet->net.mac.address.x[4],
255 subnet->net.mac.address.x[5]);
258 asprintf(&netstr, "%d,%lx/%lx", subnet->type, subnet->net.ipv4.address, subnet->net.ipv4.mask);
261 asprintf(&netstr, "%d,%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx/%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", subnet->type,
262 subnet->net.ipv6.address.x[0],
263 subnet->net.ipv6.address.x[1],
264 subnet->net.ipv6.address.x[2],
265 subnet->net.ipv6.address.x[3],
266 subnet->net.ipv6.address.x[4],
267 subnet->net.ipv6.address.x[5],
268 subnet->net.ipv6.address.x[6],
269 subnet->net.ipv6.address.x[7],
270 subnet->net.ipv6.mask.x[0],
271 subnet->net.ipv6.mask.x[1],
272 subnet->net.ipv6.mask.x[2],
273 subnet->net.ipv6.mask.x[3],
274 subnet->net.ipv6.mask.x[4],
275 subnet->net.ipv6.mask.x[5],
276 subnet->net.ipv6.mask.x[6],
277 subnet->net.ipv6.mask.x[7]);
280 asprintf(&netstr, _("unknown subnet type"));
286 /* Subnet lookup routines */
288 subnet_t *lookup_subnet_mac(mac_t *address)
292 subnet.type = SUBNET_MAC;
293 memcpy(&subnet.net.mac.address, address, sizeof(mac_t));
295 p = (subnet_t *)avl_search(subnet_tree, &subnet);
300 subnet_t *lookup_subnet_ipv4(ipv4_t *address)
304 subnet.type = SUBNET_IPV4;
305 subnet.net.ipv4.address = *address;
306 subnet.net.ipv4.mask = 0xFFFFFFFF;
312 p = (subnet_t *)avl_search_closest_smaller(subnet_tree, &subnet);
314 /* Check if the found subnet REALLY matches */
318 if ((*address & p->net.ipv4.mask) == p->net.ipv4.address)
322 /* Otherwise, see if there is a bigger enclosing subnet */
324 subnet.net.ipv4.mask = p->net.ipv4.mask << 1;
325 subnet.net.ipv4.address = p->net.ipv4.address & subnet.net.ipv4.mask;
333 subnet_t *lookup_subnet_ipv6(ipv6_t *address)
338 subnet.type = SUBNET_IPV6;
339 memcpy(&subnet.net.ipv6.address, address, sizeof(ipv6_t));
340 memset(&subnet.net.ipv6.mask, 0xFF, 16);
342 p = (subnet_t *)avl_search_closest_greater(subnet_tree, &subnet);
346 if((address->x[i] & p->net.ipv6.address.x[i]) != p->net.ipv6.address.x[i])
352 void dump_subnet_list(void)
358 syslog(LOG_DEBUG, _("Subnet list:"));
359 for(node = subnet_tree->head; node; node = node->next)
361 subnet = (subnet_t *)node->data;
362 netstr = net2str(subnet);
363 syslog(LOG_DEBUG, " %s owner %s", netstr, subnet->owner->name);
366 syslog(LOG_DEBUG, _("End of subnet list."));