]> git.proxmox.com Git - mirror_frr.git/blame - nhrpd/nhrp_route.c
build: fix & clean up *SAN flags
[mirror_frr.git] / nhrpd / nhrp_route.c
CommitLineData
2fb975da
TT
1/* NHRP routing functions
2 * Copyright (c) 2014-2015 Timo Teräs
3 *
4 * This file is free software: you may copy, redistribute and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 */
9
10#include "nhrpd.h"
11#include "table.h"
12#include "memory.h"
13#include "stream.h"
14#include "log.h"
15#include "zclient.h"
16
819dc8bb
DL
17DEFINE_MTYPE_STATIC(NHRPD, NHRP_ROUTE, "NHRP routing entry")
18
2fb975da
TT
19static struct zclient *zclient;
20static struct route_table *zebra_rib[AFI_MAX];
21
22struct route_info {
23 union sockunion via;
24 struct interface *ifp;
25 struct interface *nhrp_ifp;
26};
27
996c9314
LB
28static struct route_node *nhrp_route_update_get(const struct prefix *p,
29 int create)
2fb975da
TT
30{
31 struct route_node *rn;
32 afi_t afi = family2afi(PREFIX_FAMILY(p));
33
34 if (!zebra_rib[afi])
35 return NULL;
36
37 if (create) {
38 rn = route_node_get(zebra_rib[afi], p);
39 if (!rn->info) {
996c9314
LB
40 rn->info = XCALLOC(MTYPE_NHRP_ROUTE,
41 sizeof(struct route_info));
2fb975da
TT
42 route_lock_node(rn);
43 }
44 return rn;
45 } else {
46 return route_node_lookup(zebra_rib[afi], p);
47 }
48}
49
50static void nhrp_route_update_put(struct route_node *rn)
51{
52 struct route_info *ri = rn->info;
53
996c9314
LB
54 if (!ri->ifp && !ri->nhrp_ifp
55 && sockunion_family(&ri->via) == AF_UNSPEC) {
2fb975da
TT
56 XFREE(MTYPE_NHRP_ROUTE, rn->info);
57 rn->info = NULL;
58 route_unlock_node(rn);
59 }
60 route_unlock_node(rn);
61}
62
996c9314
LB
63static void nhrp_route_update_zebra(const struct prefix *p,
64 union sockunion *nexthop,
65 struct interface *ifp)
2fb975da
TT
66{
67 struct route_node *rn;
68 struct route_info *ri;
69
996c9314
LB
70 rn = nhrp_route_update_get(
71 p, (sockunion_family(nexthop) != AF_UNSPEC) || ifp);
2fb975da
TT
72 if (rn) {
73 ri = rn->info;
74 ri->via = *nexthop;
75 ri->ifp = ifp;
76 nhrp_route_update_put(rn);
77 }
78}
79
80void nhrp_route_update_nhrp(const struct prefix *p, struct interface *ifp)
81{
82 struct route_node *rn;
83 struct route_info *ri;
84
85 rn = nhrp_route_update_get(p, ifp != NULL);
86 if (rn) {
87 ri = rn->info;
88 ri->nhrp_ifp = ifp;
89 nhrp_route_update_put(rn);
90 }
91}
92
996c9314
LB
93void nhrp_route_announce(int add, enum nhrp_cache_type type,
94 const struct prefix *p, struct interface *ifp,
95 const union sockunion *nexthop, uint32_t mtu)
2fb975da 96{
9350687d
RW
97 struct zapi_route api;
98 struct zapi_nexthop *api_nh;
2fb975da
TT
99
100 if (zclient->sock < 0)
101 return;
102
9350687d
RW
103 memset(&api, 0, sizeof(api));
104 api.type = ZEBRA_ROUTE_NHRP;
105 api.safi = SAFI_UNICAST;
d5b2119c 106 api.vrf_id = VRF_DEFAULT;
9350687d
RW
107 api.prefix = *p;
108
2fb975da
TT
109 switch (type) {
110 case NHRP_CACHE_NEGATIVE:
09a484dd
DL
111 zapi_route_set_blackhole(&api, BLACKHOLE_REJECT);
112 ifp = NULL;
113 nexthop = NULL;
2fb975da
TT
114 break;
115 case NHRP_CACHE_DYNAMIC:
116 case NHRP_CACHE_NHS:
117 case NHRP_CACHE_STATIC:
118 /* Regular route, so these are announced
119 * to other routing daemons */
120 break;
121 default:
9350687d 122 SET_FLAG(api.flags, ZEBRA_FLAG_FIB_OVERRIDE);
2fb975da
TT
123 break;
124 }
4e8b02f4 125 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
2fb975da 126
9350687d
RW
127 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
128 api.nexthop_num = 1;
129 api_nh = &api.nexthops[0];
4a7371e9 130 api_nh->vrf_id = VRF_DEFAULT;
2fb975da 131
9350687d
RW
132 switch (api.prefix.family) {
133 case AF_INET:
2fb975da 134 if (nexthop) {
6f27a9f6
RW
135 api_nh->gate.ipv4 = nexthop->sin.sin_addr;
136 api_nh->type = NEXTHOP_TYPE_IPV4;
2fb975da
TT
137 }
138 if (ifp) {
6f27a9f6
RW
139 api_nh->ifindex = ifp->ifindex;
140 if (api_nh->type == NEXTHOP_TYPE_IPV4)
141 api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
142 else
143 api_nh->type = NEXTHOP_TYPE_IFINDEX;
2fb975da 144 }
9350687d
RW
145 break;
146 case AF_INET6:
37dc8ab5 147 if (nexthop) {
6f27a9f6
RW
148 api_nh->gate.ipv6 = nexthop->sin6.sin6_addr;
149 api_nh->type = NEXTHOP_TYPE_IPV6;
37dc8ab5
TT
150 }
151 if (ifp) {
6f27a9f6
RW
152 api_nh->ifindex = ifp->ifindex;
153 if (api_nh->type == NEXTHOP_TYPE_IPV6)
154 api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
155 else
156 api_nh->type = NEXTHOP_TYPE_IFINDEX;
37dc8ab5 157 }
9350687d
RW
158 break;
159 }
160 if (mtu) {
161 SET_FLAG(api.message, ZAPI_MESSAGE_MTU);
162 api.mtu = mtu;
163 }
37dc8ab5 164
9350687d
RW
165 if (unlikely(debug_flags & NHRP_DEBUG_ROUTE)) {
166 char buf[2][PREFIX_STRLEN];
37dc8ab5 167
9350687d 168 prefix2str(&api.prefix, buf[0], sizeof(buf[0]));
996c9314
LB
169 zlog_debug(
170 "Zebra send: route %s %s nexthop %s metric %u"
9350687d
RW
171 " count %d dev %s",
172 add ? "add" : "del", buf[0],
996c9314
LB
173 nexthop ? inet_ntop(api.prefix.family, &api_nh->gate,
174 buf[1], sizeof(buf[1]))
175 : "<onlink>",
09a484dd 176 api.metric, api.nexthop_num, ifp ? ifp->name : "none");
2fb975da 177 }
9350687d
RW
178
179 zclient_route_send(add ? ZEBRA_ROUTE_ADD : ZEBRA_ROUTE_DELETE, zclient,
180 &api);
2fb975da
TT
181}
182
996c9314
LB
183int nhrp_route_read(int cmd, struct zclient *zclient, zebra_size_t length,
184 vrf_id_t vrf_id)
2fb975da 185{
74489921
RW
186 struct zapi_route api;
187 struct zapi_nexthop *api_nh;
2fb975da 188 struct interface *ifp = NULL;
2fb975da 189 union sockunion nexthop_addr;
2fb975da 190 char buf[2][PREFIX_STRLEN];
74489921 191 int added;
2fb975da 192
74489921 193 if (zapi_route_decode(zclient->ibuf, &api) < 0)
2fb975da 194 return -1;
74489921
RW
195
196 /* we completely ignore srcdest routes for now. */
197 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
c125d1d4
RW
198 return 0;
199
74489921
RW
200 sockunion_family(&nexthop_addr) = AF_UNSPEC;
201 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP)) {
202 api_nh = &api.nexthops[0];
203
204 nexthop_addr.sa.sa_family = api.prefix.family;
205 switch (nexthop_addr.sa.sa_family) {
206 case AF_INET:
207 nexthop_addr.sin.sin_addr = api_nh->gate.ipv4;
208 break;
209 case AF_INET6:
210 nexthop_addr.sin6.sin6_addr = api_nh->gate.ipv6;
211 break;
2fb975da 212 }
74489921
RW
213
214 if (api_nh->ifindex != IFINDEX_INTERNAL)
996c9314 215 ifp = if_lookup_by_index(api_nh->ifindex, VRF_DEFAULT);
2fb975da 216 }
2fb975da 217
74489921 218 added = (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD);
2fb975da 219 debugf(NHRP_DEBUG_ROUTE, "if-route-%s: %s via %s dev %s",
996c9314
LB
220 added ? "add" : "del",
221 prefix2str(&api.prefix, buf[0], sizeof buf[0]),
222 sockunion2str(&nexthop_addr, buf[1], sizeof buf[1]),
223 ifp ? ifp->name : "(none)");
2fb975da 224
74489921
RW
225 nhrp_route_update_zebra(&api.prefix, &nexthop_addr, ifp);
226 nhrp_shortcut_prefix_change(&api.prefix, !added);
2fb975da
TT
227
228 return 0;
229}
230
996c9314
LB
231int nhrp_route_get_nexthop(const union sockunion *addr, struct prefix *p,
232 union sockunion *via, struct interface **ifp)
2fb975da
TT
233{
234 struct route_node *rn;
235 struct route_info *ri;
236 struct prefix lookup;
237 afi_t afi = family2afi(sockunion_family(addr));
238 char buf[PREFIX_STRLEN];
239
240 sockunion2hostprefix(addr, &lookup);
241
242 rn = route_node_match(zebra_rib[afi], &lookup);
996c9314
LB
243 if (!rn)
244 return 0;
2fb975da
TT
245
246 ri = rn->info;
247 if (ri->nhrp_ifp) {
248 debugf(NHRP_DEBUG_ROUTE, "lookup %s: nhrp_if=%s",
996c9314
LB
249 prefix2str(&lookup, buf, sizeof buf),
250 ri->nhrp_ifp->name);
2fb975da 251
996c9314
LB
252 if (via)
253 sockunion_family(via) = AF_UNSPEC;
254 if (ifp)
255 *ifp = ri->nhrp_ifp;
2fb975da
TT
256 } else {
257 debugf(NHRP_DEBUG_ROUTE, "lookup %s: zebra route dev %s",
996c9314
LB
258 prefix2str(&lookup, buf, sizeof buf),
259 ri->ifp ? ri->ifp->name : "(none)");
2fb975da 260
996c9314
LB
261 if (via)
262 *via = ri->via;
263 if (ifp)
264 *ifp = ri->ifp;
2fb975da 265 }
996c9314
LB
266 if (p)
267 *p = rn->p;
2fb975da
TT
268 route_unlock_node(rn);
269 return 1;
270}
271
996c9314
LB
272enum nhrp_route_type nhrp_route_address(struct interface *in_ifp,
273 union sockunion *addr, struct prefix *p,
274 struct nhrp_peer **peer)
2fb975da
TT
275{
276 struct interface *ifp = in_ifp;
277 struct nhrp_interface *nifp;
278 struct nhrp_cache *c;
279 union sockunion via[4];
280 uint32_t network_id = 0;
281 afi_t afi = family2afi(sockunion_family(addr));
282 int i;
283
284 if (ifp) {
285 nifp = ifp->info;
286 network_id = nifp->afi[afi].network_id;
287
288 c = nhrp_cache_get(ifp, addr, 0);
289 if (c && c->cur.type == NHRP_CACHE_LOCAL) {
996c9314
LB
290 if (p)
291 memset(p, 0, sizeof(*p));
2fb975da
TT
292 return NHRP_ROUTE_LOCAL;
293 }
294 }
295
296 for (i = 0; i < 4; i++) {
297 if (!nhrp_route_get_nexthop(addr, p, &via[i], &ifp))
298 return NHRP_ROUTE_BLACKHOLE;
299 if (ifp) {
300 /* Departing from nbma network? */
301 nifp = ifp->info;
996c9314
LB
302 if (network_id
303 && network_id != nifp->afi[afi].network_id)
2fb975da
TT
304 return NHRP_ROUTE_OFF_NBMA;
305 }
306 if (sockunion_family(&via[i]) == AF_UNSPEC)
307 break;
308 /* Resolve via node, but return the prefix of first match */
309 addr = &via[i];
310 p = NULL;
311 }
312
313 if (ifp) {
314 c = nhrp_cache_get(ifp, addr, 0);
315 if (c && c->cur.type >= NHRP_CACHE_DYNAMIC) {
996c9314
LB
316 if (p)
317 memset(p, 0, sizeof(*p));
2fb975da
TT
318 if (c->cur.type == NHRP_CACHE_LOCAL)
319 return NHRP_ROUTE_LOCAL;
996c9314
LB
320 if (peer)
321 *peer = nhrp_peer_ref(c->cur.peer);
2fb975da
TT
322 return NHRP_ROUTE_NBMA_NEXTHOP;
323 }
324 }
325
326 return NHRP_ROUTE_BLACKHOLE;
327}
328
996c9314 329static void nhrp_zebra_connected(struct zclient *zclient)
a1f1bab0
JB
330{
331 zclient_send_reg_requests(zclient, VRF_DEFAULT);
332 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP,
996c9314 333 ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT);
a1f1bab0 334 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP6,
996c9314 335 ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT);
a1f1bab0
JB
336}
337
2fb975da
TT
338void nhrp_zebra_init(void)
339{
340 zebra_rib[AFI_IP] = route_table_init();
341 zebra_rib[AFI_IP6] = route_table_init();
342
e1a1880d 343 zclient = zclient_new_notify(master, &zclient_options_default);
a1f1bab0 344 zclient->zebra_connected = nhrp_zebra_connected;
2fb975da
TT
345 zclient->interface_add = nhrp_interface_add;
346 zclient->interface_delete = nhrp_interface_delete;
347 zclient->interface_up = nhrp_interface_up;
348 zclient->interface_down = nhrp_interface_down;
349 zclient->interface_address_add = nhrp_interface_address_add;
350 zclient->interface_address_delete = nhrp_interface_address_delete;
74489921
RW
351 zclient->redistribute_route_add = nhrp_route_read;
352 zclient->redistribute_route_del = nhrp_route_read;
819dc8bb 353
342213ea 354 zclient_init(zclient, ZEBRA_ROUTE_NHRP, 0, &nhrpd_privs);
2fb975da
TT
355}
356
357void nhrp_zebra_terminate(void)
358{
359 zclient_stop(zclient);
20a11b25 360 zclient_free(zclient);
2fb975da
TT
361 route_table_finish(zebra_rib[AFI_IP]);
362 route_table_finish(zebra_rib[AFI_IP6]);
363}