1 | /*
|
---|
2 | * Copyright (c) 2012 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup inet
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * @brief
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <bitops.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <fibril_synch.h>
|
---|
40 | #include <io/log.h>
|
---|
41 | #include <ipc/loc.h>
|
---|
42 | #include <stdlib.h>
|
---|
43 | #include <str.h>
|
---|
44 | #include "sroute.h"
|
---|
45 | #include "inetsrv.h"
|
---|
46 | #include "inet_link.h"
|
---|
47 |
|
---|
48 | static FIBRIL_MUTEX_INITIALIZE(sroute_list_lock);
|
---|
49 | static LIST_INITIALIZE(sroute_list);
|
---|
50 | static sysarg_t sroute_id = 0;
|
---|
51 |
|
---|
52 | inet_sroute_t *inet_sroute_new(void)
|
---|
53 | {
|
---|
54 | inet_sroute_t *sroute = calloc(1, sizeof(inet_sroute_t));
|
---|
55 |
|
---|
56 | if (sroute == NULL) {
|
---|
57 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating static route object. "
|
---|
58 | "Out of memory.");
|
---|
59 | return NULL;
|
---|
60 | }
|
---|
61 |
|
---|
62 | link_initialize(&sroute->sroute_list);
|
---|
63 | fibril_mutex_lock(&sroute_list_lock);
|
---|
64 | sroute->id = ++sroute_id;
|
---|
65 | fibril_mutex_unlock(&sroute_list_lock);
|
---|
66 |
|
---|
67 | return sroute;
|
---|
68 | }
|
---|
69 |
|
---|
70 | void inet_sroute_delete(inet_sroute_t *sroute)
|
---|
71 | {
|
---|
72 | if (sroute->name != NULL)
|
---|
73 | free(sroute->name);
|
---|
74 | free(sroute);
|
---|
75 | }
|
---|
76 |
|
---|
77 | void inet_sroute_add(inet_sroute_t *sroute)
|
---|
78 | {
|
---|
79 | fibril_mutex_lock(&sroute_list_lock);
|
---|
80 | list_append(&sroute->sroute_list, &sroute_list);
|
---|
81 | fibril_mutex_unlock(&sroute_list_lock);
|
---|
82 | }
|
---|
83 |
|
---|
84 | void inet_sroute_remove(inet_sroute_t *sroute)
|
---|
85 | {
|
---|
86 | fibril_mutex_lock(&sroute_list_lock);
|
---|
87 | list_remove(&sroute->sroute_list);
|
---|
88 | fibril_mutex_unlock(&sroute_list_lock);
|
---|
89 | }
|
---|
90 |
|
---|
91 | /** Find static route object matching address @a addr.
|
---|
92 | *
|
---|
93 | * @param addr Address
|
---|
94 | */
|
---|
95 | inet_sroute_t *inet_sroute_find(inet_addr_t *addr)
|
---|
96 | {
|
---|
97 | ip_ver_t addr_ver = inet_addr_get(addr, NULL, NULL);
|
---|
98 |
|
---|
99 | inet_sroute_t *best = NULL;
|
---|
100 | uint8_t best_bits = 0;
|
---|
101 |
|
---|
102 | fibril_mutex_lock(&sroute_list_lock);
|
---|
103 |
|
---|
104 | list_foreach(sroute_list, sroute_list, inet_sroute_t, sroute) {
|
---|
105 | uint8_t dest_bits;
|
---|
106 | ip_ver_t dest_ver = inet_naddr_get(&sroute->dest, NULL, NULL,
|
---|
107 | &dest_bits);
|
---|
108 |
|
---|
109 | /* Skip comparison with different address family */
|
---|
110 | if (addr_ver != dest_ver)
|
---|
111 | continue;
|
---|
112 |
|
---|
113 | /* Look for the most specific route */
|
---|
114 | if ((best != NULL) && (best_bits >= dest_bits))
|
---|
115 | continue;
|
---|
116 |
|
---|
117 | if (inet_naddr_compare_mask(&sroute->dest, addr)) {
|
---|
118 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_sroute_find: found candidate %p",
|
---|
119 | sroute);
|
---|
120 |
|
---|
121 | best = sroute;
|
---|
122 | best_bits = dest_bits;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | if (best == NULL)
|
---|
127 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_sroute_find: Not found");
|
---|
128 |
|
---|
129 | fibril_mutex_unlock(&sroute_list_lock);
|
---|
130 |
|
---|
131 | return best;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /** Find static route with a specific name.
|
---|
135 | *
|
---|
136 | * @param name Address object name
|
---|
137 | * @return Address object
|
---|
138 | */
|
---|
139 | inet_sroute_t *inet_sroute_find_by_name(const char *name)
|
---|
140 | {
|
---|
141 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_sroute_find_by_name('%s')",
|
---|
142 | name);
|
---|
143 |
|
---|
144 | fibril_mutex_lock(&sroute_list_lock);
|
---|
145 |
|
---|
146 | list_foreach(sroute_list, sroute_list, inet_sroute_t, sroute) {
|
---|
147 | if (str_cmp(sroute->name, name) == 0) {
|
---|
148 | fibril_mutex_unlock(&sroute_list_lock);
|
---|
149 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_sroute_find_by_name: found %p",
|
---|
150 | sroute);
|
---|
151 | return sroute;
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_sroute_find_by_name: Not found");
|
---|
156 | fibril_mutex_unlock(&sroute_list_lock);
|
---|
157 |
|
---|
158 | return NULL;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /** Find static route with the given ID.
|
---|
162 | *
|
---|
163 | * @param id Address object ID
|
---|
164 | * @return Address object
|
---|
165 | */
|
---|
166 | inet_sroute_t *inet_sroute_get_by_id(sysarg_t id)
|
---|
167 | {
|
---|
168 | log_msg(LOG_DEFAULT, LVL_DEBUG, "inet_sroute_get_by_id(%zu)", (size_t)id);
|
---|
169 |
|
---|
170 | fibril_mutex_lock(&sroute_list_lock);
|
---|
171 |
|
---|
172 | list_foreach(sroute_list, sroute_list, inet_sroute_t, sroute) {
|
---|
173 | if (sroute->id == id) {
|
---|
174 | fibril_mutex_unlock(&sroute_list_lock);
|
---|
175 | return sroute;
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | fibril_mutex_unlock(&sroute_list_lock);
|
---|
180 |
|
---|
181 | return NULL;
|
---|
182 | }
|
---|
183 |
|
---|
184 | /** Get IDs of all static routes. */
|
---|
185 | int inet_sroute_get_id_list(sysarg_t **rid_list, size_t *rcount)
|
---|
186 | {
|
---|
187 | sysarg_t *id_list;
|
---|
188 | size_t count, i;
|
---|
189 |
|
---|
190 | fibril_mutex_lock(&sroute_list_lock);
|
---|
191 | count = list_count(&sroute_list);
|
---|
192 |
|
---|
193 | id_list = calloc(count, sizeof(sysarg_t));
|
---|
194 | if (id_list == NULL) {
|
---|
195 | fibril_mutex_unlock(&sroute_list_lock);
|
---|
196 | return ENOMEM;
|
---|
197 | }
|
---|
198 |
|
---|
199 | i = 0;
|
---|
200 | list_foreach(sroute_list, sroute_list, inet_sroute_t, sroute) {
|
---|
201 | id_list[i++] = sroute->id;
|
---|
202 | }
|
---|
203 |
|
---|
204 | fibril_mutex_unlock(&sroute_list_lock);
|
---|
205 |
|
---|
206 | *rid_list = id_list;
|
---|
207 | *rcount = count;
|
---|
208 |
|
---|
209 | return EOK;
|
---|
210 | }
|
---|
211 |
|
---|
212 | /** @}
|
---|
213 | */
|
---|