3 * The olsr.org Optimized Link-State Routing daemon version 2 (olsrd2)
4 * Copyright (c) 2004-2015, the olsr.org team - see HISTORY file
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of olsr.org, olsrd nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 * Visit http://www.olsr.org for more information.
36 * If you find this software useful feel free to make a donation
37 * to the project. For more information see the website or contact
38 * the copyright holders.
46 #include "common/avl.h"
47 #include "common/avl_comp.h"
48 #include "common/common_types.h"
49 #include "common/netaddr.h"
50 #include "core/oonf_logging.h"
51 #include "core/oonf_subsystem.h"
52 #include "subsystems/oonf_class.h"
53 #include "subsystems/oonf_rfc5444.h"
55 #include "nhdp/nhdp.h"
56 #include "olsrv2/olsrv2.h"
57 #include "olsrv2/olsrv2_lan.h"
59 static void _remove(struct olsrv2_lan_entry *entry);
61 /* originator set class and timer */
62 static struct oonf_class _lan_class = {
63 .name = "OLSRV2 LAN set",
64 .size = sizeof(struct olsrv2_lan_entry),
67 /* global tree of originator set entries */
68 static struct avl_tree _lan_tree;
71 * Initialize olsrv2 lan set
74 olsrv2_lan_init(void) {
75 oonf_class_add(&_lan_class);
77 avl_init(&_lan_tree, os_routing_avl_cmp_route_key, false);
81 * Cleanup all resources allocated by orignator set
84 olsrv2_lan_cleanup(void) {
85 struct olsrv2_lan_entry *entry, *e_it;
87 /* remove all originator entries */
88 avl_for_each_element_safe(&_lan_tree, entry, _node, e_it) {
93 oonf_class_remove(&_lan_class);
97 * Add a new entry to the olsrv2 local attached network
98 * @param domain NHDP domain for data
99 * @param prefix local attacked network prefix
100 * @param metric outgoing metric
101 * @param distance hopcount distance
102 * @return pointer to lan entry, NULL if out of memory
104 struct olsrv2_lan_entry *
105 olsrv2_lan_add(struct nhdp_domain *domain,
106 const struct os_route_key *prefix,
107 uint32_t metric, uint8_t distance) {
108 struct olsrv2_lan_entry *entry;
109 struct olsrv2_lan_domaindata *lan_data;
113 entry = olsrv2_lan_get(prefix);
115 entry = oonf_class_malloc(&_lan_class);
120 /* copy key and append to tree */
121 memcpy(&entry->prefix, prefix, sizeof(*prefix));
122 entry->_node.key = &entry->prefix;
123 avl_insert(&_lan_tree, &entry->_node);
125 entry->same_distance = true;
127 /* initialize linkcost */
128 for (i=0; i<NHDP_MAXIMUM_DOMAINS; i++) {
129 entry->_domaindata[i].outgoing_metric = RFC7181_METRIC_INFINITE;
133 lan_data = olsrv2_lan_get_domaindata(domain, entry);
134 lan_data->outgoing_metric = metric;
135 lan_data->distance = distance;
136 lan_data->active = true;
139 entry->same_distance = true;
140 for (i=0; i<NHDP_MAXIMUM_DOMAINS; i++) {
141 lan_data = &entry->_domaindata[i];
142 if (lan_data->active) {
144 /* copy first valid distance */
145 tmp_dist = lan_data->distance;
147 if (tmp_dist != lan_data->distance) {
148 /* we found a difference */
149 entry->same_distance = false;
158 * Remove a local attached network entry
159 * @param prefix local attacked network prefix
160 * @param domain NHDP domain for data
163 olsrv2_lan_remove(struct nhdp_domain *domain,
164 const struct os_route_key *prefix) {
165 struct olsrv2_lan_entry *entry;
166 struct olsrv2_lan_domaindata *lan_data;
169 entry = olsrv2_lan_get(prefix);
174 lan_data = olsrv2_lan_get_domaindata(domain, entry);
175 lan_data->active = false;
177 for (i=0; i<NHDP_MAXIMUM_DOMAINS; i++) {
178 if (entry->_domaindata[i].active) {
179 /* entry is still in use */
187 * Get tree of locally attached networks
191 olsrv2_lan_get_tree(void) {
196 * Remove a local attached network entry
197 * @param entry LAN entry
200 _remove(struct olsrv2_lan_entry *entry) {
201 avl_remove(&_lan_tree, &entry->_node);
202 oonf_class_free(&_lan_class, entry);