]> git.proxmox.com Git - mirror_frr.git/blob - nhrpd/nhrp_route.c
Merge pull request #3506 from opensourcerouting/6.0-init-fixes
[mirror_frr.git] / nhrpd / nhrp_route.c
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
17 DEFINE_MTYPE_STATIC(NHRPD, NHRP_ROUTE, "NHRP routing entry")
18
19 static struct zclient *zclient;
20 static struct route_table *zebra_rib[AFI_MAX];
21
22 struct route_info {
23 union sockunion via;
24 struct interface *ifp;
25 struct interface *nhrp_ifp;
26 };
27
28 static struct route_node *nhrp_route_update_get(const struct prefix *p,
29 int create)
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) {
40 rn->info = XCALLOC(MTYPE_NHRP_ROUTE,
41 sizeof(struct route_info));
42 route_lock_node(rn);
43 }
44 return rn;
45 } else {
46 return route_node_lookup(zebra_rib[afi], p);
47 }
48 }
49
50 static void nhrp_route_update_put(struct route_node *rn)
51 {
52 struct route_info *ri = rn->info;
53
54 if (!ri->ifp && !ri->nhrp_ifp
55 && sockunion_family(&ri->via) == AF_UNSPEC) {
56 XFREE(MTYPE_NHRP_ROUTE, rn->info);
57 rn->info = NULL;
58 route_unlock_node(rn);
59 }
60 route_unlock_node(rn);
61 }
62
63 static void nhrp_route_update_zebra(const struct prefix *p,
64 union sockunion *nexthop,
65 struct interface *ifp)
66 {
67 struct route_node *rn;
68 struct route_info *ri;
69
70 rn = nhrp_route_update_get(
71 p, (sockunion_family(nexthop) != AF_UNSPEC) || ifp);
72 if (rn) {
73 ri = rn->info;
74 ri->via = *nexthop;
75 ri->ifp = ifp;
76 nhrp_route_update_put(rn);
77 }
78 }
79
80 void 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
93 void 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)
96 {
97 struct zapi_route api;
98 struct zapi_nexthop *api_nh;
99
100 if (zclient->sock < 0)
101 return;
102
103 memset(&api, 0, sizeof(api));
104 api.type = ZEBRA_ROUTE_NHRP;
105 api.safi = SAFI_UNICAST;
106 api.vrf_id = VRF_DEFAULT;
107 api.prefix = *p;
108
109 switch (type) {
110 case NHRP_CACHE_NEGATIVE:
111 zapi_route_set_blackhole(&api, BLACKHOLE_REJECT);
112 ifp = NULL;
113 nexthop = NULL;
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:
122 SET_FLAG(api.flags, ZEBRA_FLAG_FIB_OVERRIDE);
123 break;
124 }
125 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
126
127 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
128 api.nexthop_num = 1;
129 api_nh = &api.nexthops[0];
130 api_nh->vrf_id = VRF_DEFAULT;
131
132 switch (api.prefix.family) {
133 case AF_INET:
134 if (nexthop) {
135 api_nh->gate.ipv4 = nexthop->sin.sin_addr;
136 api_nh->type = NEXTHOP_TYPE_IPV4;
137 }
138 if (ifp) {
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;
144 }
145 break;
146 case AF_INET6:
147 if (nexthop) {
148 api_nh->gate.ipv6 = nexthop->sin6.sin6_addr;
149 api_nh->type = NEXTHOP_TYPE_IPV6;
150 }
151 if (ifp) {
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;
157 }
158 break;
159 }
160 if (mtu) {
161 SET_FLAG(api.message, ZAPI_MESSAGE_MTU);
162 api.mtu = mtu;
163 }
164
165 if (unlikely(debug_flags & NHRP_DEBUG_ROUTE)) {
166 char buf[2][PREFIX_STRLEN];
167
168 prefix2str(&api.prefix, buf[0], sizeof(buf[0]));
169 zlog_debug(
170 "Zebra send: route %s %s nexthop %s metric %u"
171 " count %d dev %s",
172 add ? "add" : "del", buf[0],
173 nexthop ? inet_ntop(api.prefix.family, &api_nh->gate,
174 buf[1], sizeof(buf[1]))
175 : "<onlink>",
176 api.metric, api.nexthop_num, ifp ? ifp->name : "none");
177 }
178
179 zclient_route_send(add ? ZEBRA_ROUTE_ADD : ZEBRA_ROUTE_DELETE, zclient,
180 &api);
181 }
182
183 int nhrp_route_read(int cmd, struct zclient *zclient, zebra_size_t length,
184 vrf_id_t vrf_id)
185 {
186 struct zapi_route api;
187 struct zapi_nexthop *api_nh;
188 struct interface *ifp = NULL;
189 union sockunion nexthop_addr;
190 char buf[2][PREFIX_STRLEN];
191 int added;
192
193 if (zapi_route_decode(zclient->ibuf, &api) < 0)
194 return -1;
195
196 /* we completely ignore srcdest routes for now. */
197 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
198 return 0;
199
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;
212 }
213
214 if (api_nh->ifindex != IFINDEX_INTERNAL)
215 ifp = if_lookup_by_index(api_nh->ifindex, VRF_DEFAULT);
216 }
217
218 added = (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD);
219 debugf(NHRP_DEBUG_ROUTE, "if-route-%s: %s via %s dev %s",
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)");
224
225 nhrp_route_update_zebra(&api.prefix, &nexthop_addr, ifp);
226 nhrp_shortcut_prefix_change(&api.prefix, !added);
227
228 return 0;
229 }
230
231 int nhrp_route_get_nexthop(const union sockunion *addr, struct prefix *p,
232 union sockunion *via, struct interface **ifp)
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);
243 if (!rn)
244 return 0;
245
246 ri = rn->info;
247 if (ri->nhrp_ifp) {
248 debugf(NHRP_DEBUG_ROUTE, "lookup %s: nhrp_if=%s",
249 prefix2str(&lookup, buf, sizeof buf),
250 ri->nhrp_ifp->name);
251
252 if (via)
253 sockunion_family(via) = AF_UNSPEC;
254 if (ifp)
255 *ifp = ri->nhrp_ifp;
256 } else {
257 debugf(NHRP_DEBUG_ROUTE, "lookup %s: zebra route dev %s",
258 prefix2str(&lookup, buf, sizeof buf),
259 ri->ifp ? ri->ifp->name : "(none)");
260
261 if (via)
262 *via = ri->via;
263 if (ifp)
264 *ifp = ri->ifp;
265 }
266 if (p)
267 *p = rn->p;
268 route_unlock_node(rn);
269 return 1;
270 }
271
272 enum nhrp_route_type nhrp_route_address(struct interface *in_ifp,
273 union sockunion *addr, struct prefix *p,
274 struct nhrp_peer **peer)
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) {
290 if (p)
291 memset(p, 0, sizeof(*p));
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;
302 if (network_id
303 && network_id != nifp->afi[afi].network_id)
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) {
316 if (p)
317 memset(p, 0, sizeof(*p));
318 if (c->cur.type == NHRP_CACHE_LOCAL)
319 return NHRP_ROUTE_LOCAL;
320 if (peer)
321 *peer = nhrp_peer_ref(c->cur.peer);
322 return NHRP_ROUTE_NBMA_NEXTHOP;
323 }
324 }
325
326 return NHRP_ROUTE_BLACKHOLE;
327 }
328
329 static void nhrp_zebra_connected(struct zclient *zclient)
330 {
331 zclient_send_reg_requests(zclient, VRF_DEFAULT);
332 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP,
333 ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT);
334 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP6,
335 ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT);
336 }
337
338 void nhrp_zebra_init(void)
339 {
340 zebra_rib[AFI_IP] = route_table_init();
341 zebra_rib[AFI_IP6] = route_table_init();
342
343 zclient = zclient_new_notify(master, &zclient_options_default);
344 zclient->zebra_connected = nhrp_zebra_connected;
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;
351 zclient->redistribute_route_add = nhrp_route_read;
352 zclient->redistribute_route_del = nhrp_route_read;
353
354 zclient_init(zclient, ZEBRA_ROUTE_NHRP, 0, &nhrpd_privs);
355 }
356
357 void nhrp_zebra_terminate(void)
358 {
359 zclient_stop(zclient);
360 zclient_free(zclient);
361 route_table_finish(zebra_rib[AFI_IP]);
362 route_table_finish(zebra_rib[AFI_IP6]);
363 }