Debian Codegraph @

agent/mibgroup/ip-mib/data_access/ipaddress_common.c

raw
1 /*
2 * Ipaddress MIB architecture support
3 *
4 * $Id$
5 */
6 #include <net-snmp/net-snmp-config.h>
7 #include <net-snmp/net-snmp-includes.h>
8
9 #include <net-snmp/agent/net-snmp-agent-includes.h>
10 #include <net-snmp/data_access/ipaddress.h>
11 #include <net-snmp/data_access/interface.h>
12
13 #include "ip-mib/ipAddressTable/ipAddressTable_constants.h"
14 #include "ipaddress.h"
15 #include "ipaddress_private.h"
16
17 #include <net-snmp/net-snmp-features.h>
18
19 netsnmp_feature_child_of(ipaddress_common, libnetsnmpmibs);
20
21 netsnmp_feature_child_of(ipaddress_common_copy_utilities, ipaddress_common);
22 netsnmp_feature_child_of(ipaddress_entry_copy, ipaddress_common);
23 netsnmp_feature_child_of(ipaddress_entry_update, ipaddress_common);
24 netsnmp_feature_child_of(ipaddress_prefix_copy, ipaddress_common_copy_utilities);
25
26 #ifdef NETSNMP_FEATURE_REQUIRE_IPADDRESS_ENTRY_COPY
27 netsnmp_feature_require(ipaddress_arch_entry_copy);
28 #endif /* NETSNMP_FEATURE_REQUIRE_IPADDRESS_ENTRY_COPY */
29
30 /**---------------------------------------------------------------------*/
31 /*
32 * local static prototypes
33 */
34 static int _access_ipaddress_entry_compare_addr(const void *lhs,
35 const void *rhs);
36 static void _access_ipaddress_entry_release(void *entry, void *unused);
37
38 /**---------------------------------------------------------------------*/
39 /*
40 * container functions
41 */
42 /**
43 */
44 netsnmp_container *
45 netsnmp_access_ipaddress_container_init(u_int flags)
46 {
47 netsnmp_container *container1;
48
49 DEBUGMSGTL(("access:ipaddress:container", "init\n"));
50
51 /*
52 * create the containers. one indexed by ifIndex, the other
53 * indexed by ifName.
54 */
55 container1 = netsnmp_container_find("access_ipaddress:table_container");
56 if (NULL == container1) {
57 snmp_log(LOG_ERR, "ipaddress primary container not found\n");
58 return NULL;
59 }
60 container1->container_name = strdup("ia_index");
61 container1->flags = CONTAINER_KEY_ALLOW_DUPLICATES;
62
63 if (flags & NETSNMP_ACCESS_IPADDRESS_INIT_ADDL_IDX_BY_ADDR) {
64 netsnmp_container *container2 =
65 netsnmp_container_find("ipaddress_addr:access_ipaddress:table_container");
66 if (NULL == container2) {
67 snmp_log(LOG_ERR, "ipaddress secondary container not found\n");
68 CONTAINER_FREE(container1);
69 return NULL;
70 }
71
72 container2->compare = _access_ipaddress_entry_compare_addr;
73 container2->container_name = strdup("ia_addr");
74 /*
75 * With allowed duplicates, CONTAINER_INSERT does not need to sort whole
76 * container and check for duplicates. We remove duplicates manually in
77 * netsnmp_access_ipaddress_container_load.
78 */
79 container2->flags = CONTAINER_KEY_ALLOW_DUPLICATES;
80
81 netsnmp_container_add_index(container1, container2);
82 }
83
84 return container1;
85 }
86
87 /**
88 * Remove duplicate entries from the container.
89 * This function returns new copy of the container and destroys
90 * the original one. Use like this:
91 * c = _remove_duplicates(c, flags);
92 */
93 static netsnmp_container *
94 _remove_duplicates(netsnmp_container *container, u_int container_flags)
95 {
96 netsnmp_container *c;
97 netsnmp_iterator *it;
98 netsnmp_container *ret;
99 netsnmp_ipaddress_entry *entry, *prev_entry;
100
101 if (! (container_flags & NETSNMP_ACCESS_IPADDRESS_INIT_ADDL_IDX_BY_ADDR)) {
102 /* We don't have address index, we can't detect duplicates */
103 return container;
104 }
105
106 ret = netsnmp_access_ipaddress_container_init(container_flags);
107
108 /* use the IpAddress index */
109 c = container->next;
110 it = CONTAINER_ITERATOR(c);
111 /* Sort the address index */
112 CONTAINER_FIND(c, ITERATOR_FIRST(it));
113
114
115 /*
116 * Sequentially iterate over sorted container and add only unique entries
117 * to 'ret'
118 */
119 prev_entry = NULL;
120 for (entry = ITERATOR_FIRST(it); entry; entry = ITERATOR_NEXT(it)) {
121 if (prev_entry && _access_ipaddress_entry_compare_addr(prev_entry, entry) == 0) {
122 /* 'entry' is duplicate of the previous one -> delete it */
123 netsnmp_access_ipaddress_entry_free(entry);
124 } else {
125 CONTAINER_INSERT(ret, entry);
126 prev_entry = entry;
127 }
128 }
129 CONTAINER_FREE(container);
130 free(it);
131 return ret;
132 }
133
134 /**
135 * @retval NULL error
136 * @retval !NULL pointer to container
137 */
138 netsnmp_container*
139 netsnmp_access_ipaddress_container_load(netsnmp_container* container,
140 u_int load_flags)
141 {
142 int rc;
143 u_int container_flags = 0;
144
145 DEBUGMSGTL(("access:ipaddress:container", "load\n"));
146
147 if (load_flags & NETSNMP_ACCESS_IPADDRESS_LOAD_ADDL_IDX_BY_ADDR)
148 container_flags |= NETSNMP_ACCESS_IPADDRESS_INIT_ADDL_IDX_BY_ADDR;
149
150 if (NULL == container) {
151 container = netsnmp_access_ipaddress_container_init(container_flags);
152 }
153 if (NULL == container) {
154 snmp_log(LOG_ERR, "no container specified/found for access_ipaddress\n");
155 return NULL;
156 }
157
158 rc = netsnmp_arch_ipaddress_container_load(container, load_flags);
159 if (0 != rc) {
160 netsnmp_access_ipaddress_container_free(container,
161 NETSNMP_ACCESS_IPADDRESS_FREE_NOFLAGS);
162 container = NULL;
163 }
164
165 if (container)
166 container = _remove_duplicates(container, container_flags);
167
168 return container;
169 }
170
171 void
172 netsnmp_access_ipaddress_container_free(netsnmp_container *container, u_int free_flags)
173 {
174 DEBUGMSGTL(("access:ipaddress:container", "free\n"));
175
176 if (NULL == container) {
177 snmp_log(LOG_ERR, "invalid container for netsnmp_access_ipaddress_free\n");
178 return;
179 }
180
181 if(! (free_flags & NETSNMP_ACCESS_IPADDRESS_FREE_DONT_CLEAR)) {
182 /*
183 * free all items.
184 */
185 CONTAINER_CLEAR(container, _access_ipaddress_entry_release, NULL);
186 }
187
188 if(! (free_flags & NETSNMP_ACCESS_IPADDRESS_FREE_KEEP_CONTAINER))
189 CONTAINER_FREE(container);
190 }
191
192 /**---------------------------------------------------------------------*/
193 /*
194 * ipaddress_entry functions
195 */
196 /**
197 */
198 /**
199 */
200 netsnmp_ipaddress_entry *
201 netsnmp_access_ipaddress_entry_create(void)
202 {
203 netsnmp_ipaddress_entry *entry =
204 SNMP_MALLOC_TYPEDEF(netsnmp_ipaddress_entry);
205 int rc = 0;
206
207 entry->oid_index.len = 1;
208 entry->oid_index.oids = &entry->ns_ia_index;
209
210 /*
211 * set up defaults
212 */
213 entry->ia_type = IPADDRESSTYPE_UNICAST;
214 entry->ia_status = IPADDRESSSTATUSTC_PREFERRED;
215 entry->ia_storagetype = STORAGETYPE_VOLATILE;
216
217 rc = netsnmp_arch_ipaddress_entry_init(entry);
218 if (SNMP_ERR_NOERROR != rc) {
219 DEBUGMSGT(("access:ipaddress:create","error %d in arch init\n", rc));
220 netsnmp_access_ipaddress_entry_free(entry);
221 entry = NULL;
222 }
223
224 return entry;
225 }
226
227 /**
228 */
229 void
230 netsnmp_access_ipaddress_entry_free(netsnmp_ipaddress_entry * entry)
231 {
232 if (NULL == entry)
233 return;
234
235 if (NULL != entry->arch_data)
236 netsnmp_arch_ipaddress_entry_cleanup(entry);
237
238 free(entry);
239 }
240
241 /**
242 * update underlying data store (kernel) for entry
243 *
244 * @retval 0 : success
245 * @retval -1 : error
246 */
247 int
248 netsnmp_access_ipaddress_entry_set(netsnmp_ipaddress_entry * entry)
249 {
250 int rc = SNMP_ERR_NOERROR;
251
252 if (NULL == entry) {
253 netsnmp_assert(NULL != entry);
254 return -1;
255 }
256
257 /*
258 * make sure interface and ifIndex match up
259 */
260 if (NULL == netsnmp_access_interface_name_find(entry->if_index)) {
261 DEBUGMSGT(("access:ipaddress:set",
262 "cant find name for index %" NETSNMP_PRIo "d\n",
263 entry->if_index));
264 return -1;
265 }
266
267 /*
268 * don't support non-volatile yet
269 */
270 if (STORAGETYPE_VOLATILE != entry->ia_storagetype) {
271 DEBUGMSGT(("access:ipaddress:set",
272 "non-volatile storagetypes unsupported\n"));
273 return -1;
274 }
275
276 /*
277 *
278 */
279 rc = -1;
280 if (entry->flags & NETSNMP_ACCESS_IPADDRESS_CREATE) {
281 rc = netsnmp_arch_ipaddress_create(entry);
282 }
283 else if (entry->flags & NETSNMP_ACCESS_IPADDRESS_CHANGE) {
284 }
285 else if (entry->flags & NETSNMP_ACCESS_IPADDRESS_DELETE) {
286 rc = netsnmp_arch_ipaddress_delete(entry);
287 }
288 else {
289 snmp_log(LOG_ERR,"netsnmp_access_ipaddress_entry_set with no mode\n");
290 netsnmp_assert(!"ipaddress_entry_set == unknown mode"); /* always false */
291 rc = -1;
292 }
293
294 return rc;
295 }
296
297 #ifndef NETSNMP_FEATURE_REMOVE_IPADDRESS_ENTRY_UPDATE
298 /**
299 * update an old ipaddress_entry from a new one
300 *
301 * @note: only mib related items are compared. Internal objects
302 * such as oid_index, ns_ia_index and flags are not compared.
303 *
304 * @retval -1 : error
305 * @retval >=0 : number of fields updated
306 */
307 int
308 netsnmp_access_ipaddress_entry_update(netsnmp_ipaddress_entry *lhs,
309 netsnmp_ipaddress_entry *rhs)
310 {
311 int rc, changed = 0;
312
313 /*
314 * copy arch stuff. we don't care if it changed
315 */
316 rc = netsnmp_arch_ipaddress_entry_copy(lhs,rhs);
317 if (0 != rc) {
318 snmp_log(LOG_ERR,"arch ipaddress copy failed\n");
319 return -1;
320 }
321
322 if (lhs->if_index != rhs->if_index) {
323 ++changed;
324 lhs->if_index = rhs->if_index;
325 }
326
327 if (lhs->ia_storagetype != rhs->ia_storagetype) {
328 ++changed;
329 lhs->ia_storagetype = rhs->ia_storagetype;
330 }
331
332 if (lhs->ia_address_len != rhs->ia_address_len) {
333 changed += 2;
334 lhs->ia_address_len = rhs->ia_address_len;
335 memcpy(lhs->ia_address, rhs->ia_address, rhs->ia_address_len);
336 }
337 else if (memcmp(lhs->ia_address, rhs->ia_address, rhs->ia_address_len) != 0) {
338 ++changed;
339 memcpy(lhs->ia_address, rhs->ia_address, rhs->ia_address_len);
340 }
341
342 if (lhs->ia_type != rhs->ia_type) {
343 ++changed;
344 lhs->ia_type = rhs->ia_type;
345 }
346
347 if (lhs->ia_status != rhs->ia_status) {
348 ++changed;
349 lhs->ia_status = rhs->ia_status;
350 }
351
352 if (lhs->ia_origin != rhs->ia_origin) {
353 ++changed;
354 lhs->ia_origin = rhs->ia_origin;
355 }
356
357 if (lhs->ia_onlink_flag != rhs->ia_onlink_flag) {
358 ++changed;
359 lhs->ia_onlink_flag = rhs->ia_onlink_flag;
360 }
361
362 if (lhs->ia_autonomous_flag != rhs->ia_autonomous_flag) {
363 ++changed;
364 lhs->ia_autonomous_flag = rhs->ia_autonomous_flag;
365 }
366
367 if (lhs->ia_prefered_lifetime != rhs->ia_prefered_lifetime) {
368 ++changed;
369 lhs->ia_prefered_lifetime = rhs->ia_prefered_lifetime;
370 }
371
372 if (lhs->ia_valid_lifetime != rhs->ia_valid_lifetime) {
373 ++changed;
374 lhs->ia_valid_lifetime = rhs->ia_valid_lifetime;
375 }
376
377
378 return changed;
379 }
380 #endif /* NETSNMP_FEATURE_REMOVE_IPADDRESS_ENTRY_UPDATE */
381
382 #ifndef NETSNMP_FEATURE_REMOVE_IPADDRESS_ENTRY_COPY
383 /**
384 * copy an ipaddress_entry
385 *
386 * @retval -1 : error
387 * @retval 0 : no error
388 */
389 int
390 netsnmp_access_ipaddress_entry_copy(netsnmp_ipaddress_entry *lhs,
391 netsnmp_ipaddress_entry *rhs)
392 {
393 int rc;
394
395 /*
396 * copy arch stuff. we don't care if it changed
397 */
398 rc = netsnmp_arch_ipaddress_entry_copy(lhs,rhs);
399 if (0 != rc) {
400 snmp_log(LOG_ERR,"arch ipaddress copy failed\n");
401 return -1;
402 }
403
404 lhs->if_index = rhs->if_index;
405 lhs->ia_storagetype = rhs->ia_storagetype;
406 lhs->ia_address_len = rhs->ia_address_len;
407 memcpy(lhs->ia_address, rhs->ia_address, rhs->ia_address_len);
408 lhs->ia_type = rhs->ia_type;
409 lhs->ia_status = rhs->ia_status;
410 lhs->ia_origin = rhs->ia_origin;
411
412 return 0;
413 }
414 #endif /* NETSNMP_FEATURE_REMOVE_IPADDRESS_ENTRY_COPY */
415
416 /**---------------------------------------------------------------------*/
417 /*
418 * Utility routines
419 */
420
421 #ifndef NETSNMP_FEATURE_REMOVE_IPADDRESS_PREFIX_COPY
422 /**
423 * copy the prefix portion of an ip address
424 */
425 int
426 netsnmp_ipaddress_prefix_copy(u_char *dst, u_char *src, int addr_len, int pfx_len)
427 {
428 int bytes = pfx_len / 8;
429 int bits = pfx_len % 8;
430
431 if ((NULL == dst) || (NULL == src) || (0 == pfx_len))
432 return 0;
433
434 memcpy(dst, src, bytes);
435
436 if (bytes < addr_len)
437 memset(&dst[bytes],0x0, addr_len - bytes);
438
439 if (bits) {
440 u_char mask = (0xff << (8-bits));
441
442 dst[bytes] = (src[bytes] & mask);
443 }
444
445 return pfx_len;
446 }
447 #endif /* NETSNMP_FEATURE_REMOVE_IPADDRESS_PREFIX_COPY */
448
449
450 /**
451 * Compute the prefix length of a network mask
452 *
453 * @param mask network byte order mask
454 *
455 * @returns number of prefix bits
456 */
457 int
458 netsnmp_ipaddress_ipv4_prefix_len(in_addr_t mask)
459 {
460 int i, len = 0;
461 unsigned char *mp = (unsigned char *)&mask;
462
463 for (i = 0; i < 4; i++)
464 if (mp[i] == 0xFF) len += 8;
465 else break;
466
467 if (i == 4)
468 return len;
469
470 while(0x80 & mp[i]) {
471 ++len;
472 mp[i] <<= 1;
473 }
474
475 return len;
476 }
477
478 in_addr_t netsnmp_ipaddress_ipv4_mask(int len)
479 {
480 int i = 0, m = 0x80;
481 in_addr_t mask;
482 unsigned char *mp = (unsigned char *)&mask;
483
484 if (len < 0 || len > 32) abort();
485
486 memset(mp, 0, sizeof(mask));
487
488 while (len >= 8) {
489 mp[i] = 0xFF;
490 len -= 8;
491 i++;
492 }
493 while (len) {
494 mp[i] |= m;
495 m >>= 1;
496 len--;
497 }
498 return mask;
499 }
500
501 int
502 netsnmp_ipaddress_ipv6_prefix_len(struct in6_addr mask)
503 {
504 int i, len = 0;
505 unsigned char *mp = (unsigned char *)&mask.s6_addr;
506
507 for (i = 0; i < 16; i++)
508 if (mp[i] == 0xFF) len += 8;
509 else break;
510
511 if (i == 16)
512 return len;
513
514 while(0x80 & mp[i]) {
515 ++len;
516 mp[i] <<= 1;
517 }
518
519 return len;
520 }
521
522
523 /**
524 */
525 void
526 _access_ipaddress_entry_release(void *entry, void *context)
527 {
528 netsnmp_access_ipaddress_entry_free(entry);
529 }
530
531 static int _access_ipaddress_entry_compare_addr(const void *lhs,
532 const void *rhs)
533 {
534 const netsnmp_ipaddress_entry *lh = (const netsnmp_ipaddress_entry *)lhs;
535 const netsnmp_ipaddress_entry *rh = (const netsnmp_ipaddress_entry *)rhs;
536
537 netsnmp_assert(NULL != lhs);
538 netsnmp_assert(NULL != rhs);
539
540 /*
541 * compare address length
542 */
543 if (lh->ia_address_len < rh->ia_address_len)
544 return -1;
545 else if (lh->ia_address_len > rh->ia_address_len)
546 return 1;
547
548 /*
549 * length equal, compare address
550 */
551 return memcmp(lh->ia_address, rh->ia_address, lh->ia_address_len);
552 }
553
554 #ifndef NETSNMP_FEATURE_REMOVE_IPADDRESS_COMMON_COPY_UTILITIES
555 int
556 netsnmp_ipaddress_flags_copy(u_long *ipAddressPrefixAdvPreferredLifetime,
557 u_long *ipAddressPrefixAdvValidLifetime,
558 u_long *ipAddressPrefixOnLinkFlag,
559 u_long *ipAddressPrefixAutonomousFlag,
560 u_long *ia_prefered_lifetime,
561 u_long *ia_valid_lifetime,
562 u_char *ia_onlink_flag,
563 u_char *ia_autonomous_flag)
564 {
565
566 /*Copy all the flags*/
567 *ipAddressPrefixAdvPreferredLifetime = *ia_prefered_lifetime;
568 *ipAddressPrefixAdvValidLifetime = *ia_valid_lifetime;
569 *ipAddressPrefixOnLinkFlag = *ia_onlink_flag;
570 *ipAddressPrefixAutonomousFlag = *ia_autonomous_flag;
571 return 0;
572 }
573
574 int
575 netsnmp_ipaddress_prefix_origin_copy(u_long *ipAddressPrefixOrigin,
576 u_char ia_origin,
577 int flags,
578 u_long ipAddressAddrType)
579 {
580 if(ipAddressAddrType == INETADDRESSTYPE_IPV4){
581 if(ia_origin == 6) /*Random*/
582 (*ipAddressPrefixOrigin) = 3 /*IPADDRESSPREFIXORIGINTC_WELLKNOWN*/;
583 else
584 (*ipAddressPrefixOrigin) = ia_origin;
585 } else {
586 if(ia_origin == 5) { /*Link Layer*/
587 if(!flags) /*Global address assigned by router adv*/
588 (*ipAddressPrefixOrigin) = 5 /*IPADDRESSPREFIXORIGINTC_ROUTERADV*/;
589 else
590 (*ipAddressPrefixOrigin) = 3 /*IPADDRESSPREFIXORIGINTC_WELLKNOWN*/;
591 }
592 else if(ia_origin == 6) /*Random*/
593 (*ipAddressPrefixOrigin) = 5 /*IPADDRESSPREFIXORIGINTC_ROUTERADV*/;
594 else
595 (*ipAddressPrefixOrigin) = ia_origin;
596 }
597 return 0;
598 }
599 #endif /* NETSNMP_FEATURE_REMOVE_IPADDRESS_COMMON_COPY_UTILITIES */
600