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"
58 #include "olsrv2/olsrv2_routing.h"
60 static void _remove(struct olsrv2_lan_entry *entry);
62 /* originator set class and timer */
63 static struct oonf_class _lan_class = {
64 .name = "OLSRV2 LAN set",
65 .size = sizeof(struct olsrv2_lan_entry),
68 /* global tree of originator set entries */
69 static struct avl_tree _lan_tree;
72 * Initialize olsrv2 lan set
75 olsrv2_lan_init(void) {
76 oonf_class_add(&_lan_class);
78 avl_init(&_lan_tree, os_routing_avl_cmp_route_key, false);
82 * Cleanup all resources allocated by orignator set
85 olsrv2_lan_cleanup(void) {
86 struct olsrv2_lan_entry *entry, *e_it;
88 /* remove all originator entries */
89 avl_for_each_element_safe(&_lan_tree, entry, _node, e_it) {
94 oonf_class_remove(&_lan_class);
98 * Add a new entry to the olsrv2 local attached network
99 * @param domain NHDP domain for data
100 * @param prefix local attacked network prefix
101 * @param metric outgoing metric
102 * @param distance hopcount distance
103 * @return pointer to lan entry, NULL if out of memory
105 struct olsrv2_lan_entry *
106 olsrv2_lan_add(struct nhdp_domain *domain,
107 const struct os_route_key *prefix,
108 uint32_t metric, uint8_t distance) {
109 struct olsrv2_lan_entry *entry;
110 struct olsrv2_lan_domaindata *lan_data;
114 entry = olsrv2_lan_get(prefix);
116 entry = oonf_class_malloc(&_lan_class);
121 /* copy key and append to tree */
122 memcpy(&entry->prefix, prefix, sizeof(*prefix));
123 entry->_node.key = &entry->prefix;
124 avl_insert(&_lan_tree, &entry->_node);
126 entry->same_distance = true;
128 /* initialize linkcost */
129 for (i=0; i<NHDP_MAXIMUM_DOMAINS; i++) {
130 entry->_domaindata[i].outgoing_metric = RFC7181_METRIC_INFINITE;
134 lan_data = olsrv2_lan_get_domaindata(domain, entry);
135 lan_data->outgoing_metric = metric;
136 lan_data->distance = distance;
137 lan_data->active = true;
138 olsrv2_routing_domain_changed(domain, true);
141 entry->same_distance = true;
142 for (i=0; i<NHDP_MAXIMUM_DOMAINS; i++) {
143 lan_data = &entry->_domaindata[i];
144 if (lan_data->active) {
146 /* copy first valid distance */
147 tmp_dist = lan_data->distance;
149 if (tmp_dist != lan_data->distance) {
150 /* we found a difference */
151 entry->same_distance = false;
160 * Remove a local attached network entry
161 * @param prefix local attacked network prefix
162 * @param domain NHDP domain for data
165 olsrv2_lan_remove(struct nhdp_domain *domain,
166 const struct os_route_key *prefix) {
167 struct olsrv2_lan_entry *entry;
168 struct olsrv2_lan_domaindata *lan_data;
171 entry = olsrv2_lan_get(prefix);
176 lan_data = olsrv2_lan_get_domaindata(domain, entry);
177 lan_data->active = false;
178 olsrv2_routing_domain_changed(domain, true);
180 for (i=0; i<NHDP_MAXIMUM_DOMAINS; i++) {
181 if (entry->_domaindata[i].active) {
182 /* entry is still in use */
190 * Get tree of locally attached networks
194 olsrv2_lan_get_tree(void) {
199 * Remove a local attached network entry
200 * @param entry LAN entry
203 _remove(struct olsrv2_lan_entry *entry) {
204 avl_remove(&_lan_tree, &entry->_node);
205 oonf_class_free(&_lan_class, entry);