]>
Commit | Line | Data |
---|---|---|
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 |
17 | DEFINE_MTYPE_STATIC(NHRPD, NHRP_ROUTE, "NHRP routing entry") |
18 | ||
2fb975da TT |
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 | ||
2fb975da TT |
28 | static struct route_node *nhrp_route_update_get(const struct prefix *p, int create) |
29 | { | |
30 | struct route_node *rn; | |
31 | afi_t afi = family2afi(PREFIX_FAMILY(p)); | |
32 | ||
33 | if (!zebra_rib[afi]) | |
34 | return NULL; | |
35 | ||
36 | if (create) { | |
37 | rn = route_node_get(zebra_rib[afi], p); | |
38 | if (!rn->info) { | |
39 | rn->info = XCALLOC(MTYPE_NHRP_ROUTE, sizeof(struct route_info)); | |
40 | route_lock_node(rn); | |
41 | } | |
42 | return rn; | |
43 | } else { | |
44 | return route_node_lookup(zebra_rib[afi], p); | |
45 | } | |
46 | } | |
47 | ||
48 | static void nhrp_route_update_put(struct route_node *rn) | |
49 | { | |
50 | struct route_info *ri = rn->info; | |
51 | ||
52 | if (!ri->ifp && !ri->nhrp_ifp && sockunion_family(&ri->via) == AF_UNSPEC) { | |
53 | XFREE(MTYPE_NHRP_ROUTE, rn->info); | |
54 | rn->info = NULL; | |
55 | route_unlock_node(rn); | |
56 | } | |
57 | route_unlock_node(rn); | |
58 | } | |
59 | ||
60 | static void nhrp_route_update_zebra(const struct prefix *p, union sockunion *nexthop, struct interface *ifp) | |
61 | { | |
62 | struct route_node *rn; | |
63 | struct route_info *ri; | |
64 | ||
65 | rn = nhrp_route_update_get(p, (sockunion_family(nexthop) != AF_UNSPEC) || ifp); | |
66 | if (rn) { | |
67 | ri = rn->info; | |
68 | ri->via = *nexthop; | |
69 | ri->ifp = ifp; | |
70 | nhrp_route_update_put(rn); | |
71 | } | |
72 | } | |
73 | ||
74 | void nhrp_route_update_nhrp(const struct prefix *p, struct interface *ifp) | |
75 | { | |
76 | struct route_node *rn; | |
77 | struct route_info *ri; | |
78 | ||
79 | rn = nhrp_route_update_get(p, ifp != NULL); | |
80 | if (rn) { | |
81 | ri = rn->info; | |
82 | ri->nhrp_ifp = ifp; | |
83 | nhrp_route_update_put(rn); | |
84 | } | |
85 | } | |
86 | ||
87 | void nhrp_route_announce(int add, enum nhrp_cache_type type, const struct prefix *p, struct interface *ifp, const union sockunion *nexthop, uint32_t mtu) | |
88 | { | |
2fb975da TT |
89 | int flags = 0; |
90 | ||
91 | if (zclient->sock < 0) | |
92 | return; | |
93 | ||
94 | switch (type) { | |
95 | case NHRP_CACHE_NEGATIVE: | |
96 | SET_FLAG(flags, ZEBRA_FLAG_REJECT); | |
97 | break; | |
98 | case NHRP_CACHE_DYNAMIC: | |
99 | case NHRP_CACHE_NHS: | |
100 | case NHRP_CACHE_STATIC: | |
101 | /* Regular route, so these are announced | |
102 | * to other routing daemons */ | |
103 | break; | |
104 | default: | |
105 | SET_FLAG(flags, ZEBRA_FLAG_FIB_OVERRIDE); | |
106 | break; | |
107 | } | |
108 | SET_FLAG(flags, ZEBRA_FLAG_INTERNAL); | |
109 | ||
110 | if (p->family == AF_INET) { | |
37dc8ab5 | 111 | struct in_addr *nexthop_ipv4; |
2fb975da TT |
112 | struct zapi_ipv4 api; |
113 | ||
114 | memset(&api, 0, sizeof(api)); | |
115 | api.flags = flags; | |
116 | api.type = ZEBRA_ROUTE_NHRP; | |
117 | api.safi = SAFI_UNICAST; | |
118 | ||
119 | SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP); | |
120 | if (nexthop) { | |
2fb975da TT |
121 | nexthop_ipv4 = (struct in_addr *) sockunion_get_addr(nexthop); |
122 | api.nexthop_num = 1; | |
123 | api.nexthop = &nexthop_ipv4; | |
124 | } | |
125 | if (ifp) { | |
126 | SET_FLAG(api.message, ZAPI_MESSAGE_IFINDEX); | |
127 | api.ifindex_num = 1; | |
128 | api.ifindex = &ifp->ifindex; | |
129 | } | |
130 | if (mtu) { | |
131 | SET_FLAG(api.message, ZAPI_MESSAGE_MTU); | |
132 | api.mtu = mtu; | |
133 | } | |
134 | ||
135 | if (unlikely(debug_flags & NHRP_DEBUG_ROUTE)) { | |
136 | char buf[2][INET_ADDRSTRLEN]; | |
137 | zlog_debug("Zebra send: IPv4 route %s %s/%d nexthop %s metric %u" | |
138 | " count %d dev %s", | |
139 | add ? "add" : "del", | |
140 | inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])), | |
141 | p->prefixlen, | |
142 | nexthop ? inet_ntop(AF_INET, api.nexthop[0], buf[1], sizeof(buf[1])) : "<onlink>", | |
143 | api.metric, api.nexthop_num, ifp->name); | |
144 | } | |
145 | ||
146 | zapi_ipv4_route( | |
147 | add ? ZEBRA_IPV4_ROUTE_ADD : ZEBRA_IPV4_ROUTE_DELETE, | |
148 | zclient, (struct prefix_ipv4 *) p, &api); | |
37dc8ab5 TT |
149 | } else if (p->family == AF_INET6) { |
150 | struct in6_addr *nexthop_ipv6; | |
151 | struct zapi_ipv6 api; | |
152 | ||
153 | memset(&api, 0, sizeof(api)); | |
154 | api.flags = flags; | |
155 | api.type = ZEBRA_ROUTE_NHRP; | |
156 | api.safi = SAFI_UNICAST; | |
157 | ||
158 | SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP); | |
159 | if (nexthop) { | |
160 | nexthop_ipv6 = (struct in6_addr *) sockunion_get_addr(nexthop); | |
161 | api.nexthop_num = 1; | |
162 | api.nexthop = &nexthop_ipv6; | |
163 | } | |
164 | if (ifp) { | |
165 | SET_FLAG(api.message, ZAPI_MESSAGE_IFINDEX); | |
166 | api.ifindex_num = 1; | |
167 | api.ifindex = &ifp->ifindex; | |
168 | } | |
169 | if (mtu) { | |
170 | SET_FLAG(api.message, ZAPI_MESSAGE_MTU); | |
171 | api.mtu = mtu; | |
172 | } | |
173 | ||
174 | if (unlikely(debug_flags & NHRP_DEBUG_ROUTE)) { | |
175 | char buf[2][INET6_ADDRSTRLEN]; | |
176 | zlog_debug("Zebra send: IPv6 route %s %s/%d nexthop %s metric %u" | |
177 | " count %d dev %s", | |
178 | add ? "add" : "del", | |
179 | inet_ntop(AF_INET6, &p->u.prefix6, buf[0], sizeof(buf[0])), | |
180 | p->prefixlen, | |
181 | nexthop ? inet_ntop(AF_INET6, api.nexthop[0], buf[1], sizeof(buf[1])) : "<onlink>", | |
182 | api.metric, api.nexthop_num, ifp->name); | |
183 | } | |
184 | ||
185 | zapi_ipv6_route( | |
186 | add ? ZEBRA_IPV6_ROUTE_ADD : ZEBRA_IPV6_ROUTE_DELETE, | |
187 | zclient, (struct prefix_ipv6 *) p, NULL, &api); | |
2fb975da TT |
188 | } |
189 | } | |
190 | ||
191 | int nhrp_route_read(int cmd, struct zclient *zclient, zebra_size_t length, vrf_id_t vrf_id) | |
192 | { | |
193 | struct stream *s; | |
194 | struct interface *ifp = NULL; | |
195 | struct prefix prefix; | |
c125d1d4 | 196 | struct prefix_ipv6 src_p; |
2fb975da TT |
197 | union sockunion nexthop_addr; |
198 | unsigned char message, nexthop_num, ifindex_num; | |
199 | unsigned ifindex; | |
200 | char buf[2][PREFIX_STRLEN]; | |
201 | int i, afaddrlen, added; | |
202 | ||
203 | s = zclient->ibuf; | |
204 | memset(&prefix, 0, sizeof(prefix)); | |
205 | sockunion_family(&nexthop_addr) = AF_UNSPEC; | |
206 | ||
207 | /* Type, flags, message. */ | |
208 | /*type =*/ stream_getc(s); | |
a1f1bab0 JB |
209 | /*instance =*/ stream_getw(s); |
210 | /*flags =*/ stream_getl(s); | |
2fb975da TT |
211 | message = stream_getc(s); |
212 | ||
213 | /* Prefix */ | |
214 | switch (cmd) { | |
a1f1bab0 JB |
215 | case ZEBRA_REDISTRIBUTE_IPV4_ADD: |
216 | case ZEBRA_REDISTRIBUTE_IPV4_DEL: | |
2fb975da | 217 | prefix.family = AF_INET; |
e959008b | 218 | prefix.prefixlen = MIN(IPV4_MAX_PREFIXLEN, stream_getc(s)); |
2fb975da | 219 | break; |
a1f1bab0 JB |
220 | case ZEBRA_REDISTRIBUTE_IPV6_ADD: |
221 | case ZEBRA_REDISTRIBUTE_IPV6_DEL: | |
2fb975da | 222 | prefix.family = AF_INET6; |
e959008b | 223 | prefix.prefixlen = MIN(IPV6_MAX_PREFIXLEN, stream_getc(s)); |
2fb975da TT |
224 | break; |
225 | default: | |
226 | return -1; | |
227 | } | |
228 | afaddrlen = family2addrsize(prefix.family); | |
2fb975da TT |
229 | stream_get(&prefix.u.val, s, PSIZE(prefix.prefixlen)); |
230 | ||
c125d1d4 RW |
231 | memset(&src_p, 0, sizeof(src_p)); |
232 | if (prefix.family == AF_INET6 && | |
233 | CHECK_FLAG(message, ZAPI_MESSAGE_SRCPFX)) { | |
234 | src_p.family = AF_INET6; | |
235 | src_p.prefixlen = stream_getc(s); | |
236 | stream_get(&src_p.prefix, s, PSIZE(src_p.prefixlen)); | |
237 | } | |
238 | if (src_p.prefixlen) | |
239 | /* we completely ignore srcdest routes for now. */ | |
240 | return 0; | |
241 | ||
2fb975da TT |
242 | /* Nexthop, ifindex, distance, metric. */ |
243 | if (CHECK_FLAG(message, ZAPI_MESSAGE_NEXTHOP|ZAPI_MESSAGE_IFINDEX)) { | |
244 | nexthop_num = stream_getc(s); | |
245 | for (i = 0; i < nexthop_num; i++) { | |
246 | stream_get(buf[0], s, afaddrlen); | |
247 | if (i == 0) sockunion_set(&nexthop_addr, prefix.family, (u_char*) buf[0], afaddrlen); | |
248 | } | |
249 | ifindex_num = stream_getc(s); | |
250 | for (i = 0; i < ifindex_num; i++) { | |
251 | ifindex = stream_getl(s); | |
252 | if (i == 0 && ifindex != IFINDEX_INTERNAL) | |
7e2b7603 | 253 | ifp = if_lookup_by_index(ifindex, VRF_DEFAULT); |
2fb975da TT |
254 | } |
255 | } | |
256 | if (CHECK_FLAG(message, ZAPI_MESSAGE_DISTANCE)) | |
257 | /*distance =*/ stream_getc(s); | |
258 | if (CHECK_FLAG(message, ZAPI_MESSAGE_METRIC)) | |
259 | /*metric =*/ stream_getl(s); | |
260 | ||
a1f1bab0 | 261 | added = (cmd == ZEBRA_REDISTRIBUTE_IPV4_ADD || cmd == ZEBRA_REDISTRIBUTE_IPV6_ADD); |
2fb975da TT |
262 | debugf(NHRP_DEBUG_ROUTE, "if-route-%s: %s via %s dev %s", |
263 | added ? "add" : "del", | |
264 | prefix2str(&prefix, buf[0], sizeof buf[0]), | |
265 | sockunion2str(&nexthop_addr, buf[1], sizeof buf[1]), | |
266 | ifp ? ifp->name : "(none)"); | |
267 | ||
268 | nhrp_route_update_zebra(&prefix, &nexthop_addr, ifp); | |
269 | nhrp_shortcut_prefix_change(&prefix, !added); | |
270 | ||
271 | return 0; | |
272 | } | |
273 | ||
274 | int nhrp_route_get_nexthop(const union sockunion *addr, struct prefix *p, union sockunion *via, struct interface **ifp) | |
275 | { | |
276 | struct route_node *rn; | |
277 | struct route_info *ri; | |
278 | struct prefix lookup; | |
279 | afi_t afi = family2afi(sockunion_family(addr)); | |
280 | char buf[PREFIX_STRLEN]; | |
281 | ||
282 | sockunion2hostprefix(addr, &lookup); | |
283 | ||
284 | rn = route_node_match(zebra_rib[afi], &lookup); | |
285 | if (!rn) return 0; | |
286 | ||
287 | ri = rn->info; | |
288 | if (ri->nhrp_ifp) { | |
289 | debugf(NHRP_DEBUG_ROUTE, "lookup %s: nhrp_if=%s", | |
290 | prefix2str(&lookup, buf, sizeof buf), | |
291 | ri->nhrp_ifp->name); | |
292 | ||
293 | if (via) sockunion_family(via) = AF_UNSPEC; | |
294 | if (ifp) *ifp = ri->nhrp_ifp; | |
295 | } else { | |
296 | debugf(NHRP_DEBUG_ROUTE, "lookup %s: zebra route dev %s", | |
297 | prefix2str(&lookup, buf, sizeof buf), | |
298 | ri->ifp ? ri->ifp->name : "(none)"); | |
299 | ||
300 | if (via) *via = ri->via; | |
301 | if (ifp) *ifp = ri->ifp; | |
302 | } | |
303 | if (p) *p = rn->p; | |
304 | route_unlock_node(rn); | |
305 | return 1; | |
306 | } | |
307 | ||
308 | enum nhrp_route_type nhrp_route_address(struct interface *in_ifp, union sockunion *addr, struct prefix *p, struct nhrp_peer **peer) | |
309 | { | |
310 | struct interface *ifp = in_ifp; | |
311 | struct nhrp_interface *nifp; | |
312 | struct nhrp_cache *c; | |
313 | union sockunion via[4]; | |
314 | uint32_t network_id = 0; | |
315 | afi_t afi = family2afi(sockunion_family(addr)); | |
316 | int i; | |
317 | ||
318 | if (ifp) { | |
319 | nifp = ifp->info; | |
320 | network_id = nifp->afi[afi].network_id; | |
321 | ||
322 | c = nhrp_cache_get(ifp, addr, 0); | |
323 | if (c && c->cur.type == NHRP_CACHE_LOCAL) { | |
324 | if (p) memset(p, 0, sizeof(*p)); | |
325 | return NHRP_ROUTE_LOCAL; | |
326 | } | |
327 | } | |
328 | ||
329 | for (i = 0; i < 4; i++) { | |
330 | if (!nhrp_route_get_nexthop(addr, p, &via[i], &ifp)) | |
331 | return NHRP_ROUTE_BLACKHOLE; | |
332 | if (ifp) { | |
333 | /* Departing from nbma network? */ | |
334 | nifp = ifp->info; | |
335 | if (network_id && network_id != nifp->afi[afi].network_id) | |
336 | return NHRP_ROUTE_OFF_NBMA; | |
337 | } | |
338 | if (sockunion_family(&via[i]) == AF_UNSPEC) | |
339 | break; | |
340 | /* Resolve via node, but return the prefix of first match */ | |
341 | addr = &via[i]; | |
342 | p = NULL; | |
343 | } | |
344 | ||
345 | if (ifp) { | |
346 | c = nhrp_cache_get(ifp, addr, 0); | |
347 | if (c && c->cur.type >= NHRP_CACHE_DYNAMIC) { | |
348 | if (p) memset(p, 0, sizeof(*p)); | |
349 | if (c->cur.type == NHRP_CACHE_LOCAL) | |
350 | return NHRP_ROUTE_LOCAL; | |
351 | if (peer) *peer = nhrp_peer_ref(c->cur.peer); | |
352 | return NHRP_ROUTE_NBMA_NEXTHOP; | |
353 | } | |
354 | } | |
355 | ||
356 | return NHRP_ROUTE_BLACKHOLE; | |
357 | } | |
358 | ||
a1f1bab0 JB |
359 | static void |
360 | nhrp_zebra_connected (struct zclient *zclient) | |
361 | { | |
362 | zclient_send_reg_requests(zclient, VRF_DEFAULT); | |
363 | zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP, | |
364 | ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT); | |
365 | zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP6, | |
366 | ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT); | |
367 | } | |
368 | ||
2fb975da TT |
369 | void nhrp_zebra_init(void) |
370 | { | |
371 | zebra_rib[AFI_IP] = route_table_init(); | |
372 | zebra_rib[AFI_IP6] = route_table_init(); | |
373 | ||
374 | zclient = zclient_new(master); | |
a1f1bab0 | 375 | zclient->zebra_connected = nhrp_zebra_connected; |
2fb975da TT |
376 | zclient->interface_add = nhrp_interface_add; |
377 | zclient->interface_delete = nhrp_interface_delete; | |
378 | zclient->interface_up = nhrp_interface_up; | |
379 | zclient->interface_down = nhrp_interface_down; | |
380 | zclient->interface_address_add = nhrp_interface_address_add; | |
381 | zclient->interface_address_delete = nhrp_interface_address_delete; | |
819dc8bb DL |
382 | zclient->redistribute_route_ipv4_add = nhrp_route_read; |
383 | zclient->redistribute_route_ipv4_del = nhrp_route_read; | |
384 | zclient->redistribute_route_ipv6_add = nhrp_route_read; | |
385 | zclient->redistribute_route_ipv6_del = nhrp_route_read; | |
386 | ||
387 | zclient_init(zclient, ZEBRA_ROUTE_NHRP, 0); | |
2fb975da TT |
388 | } |
389 | ||
390 | void nhrp_zebra_terminate(void) | |
391 | { | |
392 | zclient_stop(zclient); | |
20a11b25 | 393 | zclient_free(zclient); |
2fb975da TT |
394 | route_table_finish(zebra_rib[AFI_IP]); |
395 | route_table_finish(zebra_rib[AFI_IP6]); | |
396 | } | |
397 |