]>
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 | { | |
9350687d RW |
89 | struct zapi_route api; |
90 | struct zapi_nexthop *api_nh; | |
2fb975da TT |
91 | |
92 | if (zclient->sock < 0) | |
93 | return; | |
94 | ||
9350687d RW |
95 | memset(&api, 0, sizeof(api)); |
96 | api.type = ZEBRA_ROUTE_NHRP; | |
97 | api.safi = SAFI_UNICAST; | |
98 | api.prefix = *p; | |
99 | ||
2fb975da TT |
100 | switch (type) { |
101 | case NHRP_CACHE_NEGATIVE: | |
9350687d | 102 | SET_FLAG(api.flags, ZEBRA_FLAG_REJECT); |
2fb975da TT |
103 | break; |
104 | case NHRP_CACHE_DYNAMIC: | |
105 | case NHRP_CACHE_NHS: | |
106 | case NHRP_CACHE_STATIC: | |
107 | /* Regular route, so these are announced | |
108 | * to other routing daemons */ | |
109 | break; | |
110 | default: | |
9350687d | 111 | SET_FLAG(api.flags, ZEBRA_FLAG_FIB_OVERRIDE); |
2fb975da TT |
112 | break; |
113 | } | |
9350687d | 114 | SET_FLAG(api.flags, ZEBRA_FLAG_INTERNAL); |
2fb975da | 115 | |
9350687d RW |
116 | SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP); |
117 | api.nexthop_num = 1; | |
118 | api_nh = &api.nexthops[0]; | |
2fb975da | 119 | |
9350687d RW |
120 | switch (api.prefix.family) { |
121 | case AF_INET: | |
2fb975da | 122 | if (nexthop) { |
6f27a9f6 RW |
123 | api_nh->gate.ipv4 = nexthop->sin.sin_addr; |
124 | api_nh->type = NEXTHOP_TYPE_IPV4; | |
2fb975da TT |
125 | } |
126 | if (ifp) { | |
6f27a9f6 RW |
127 | api_nh->ifindex = ifp->ifindex; |
128 | if (api_nh->type == NEXTHOP_TYPE_IPV4) | |
129 | api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX; | |
130 | else | |
131 | api_nh->type = NEXTHOP_TYPE_IFINDEX; | |
2fb975da | 132 | } |
9350687d RW |
133 | break; |
134 | case AF_INET6: | |
37dc8ab5 | 135 | if (nexthop) { |
6f27a9f6 RW |
136 | api_nh->gate.ipv6 = nexthop->sin6.sin6_addr; |
137 | api_nh->type = NEXTHOP_TYPE_IPV6; | |
37dc8ab5 TT |
138 | } |
139 | if (ifp) { | |
6f27a9f6 RW |
140 | api_nh->ifindex = ifp->ifindex; |
141 | if (api_nh->type == NEXTHOP_TYPE_IPV6) | |
142 | api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX; | |
143 | else | |
144 | api_nh->type = NEXTHOP_TYPE_IFINDEX; | |
37dc8ab5 | 145 | } |
9350687d RW |
146 | break; |
147 | } | |
148 | if (mtu) { | |
149 | SET_FLAG(api.message, ZAPI_MESSAGE_MTU); | |
150 | api.mtu = mtu; | |
151 | } | |
37dc8ab5 | 152 | |
9350687d RW |
153 | if (unlikely(debug_flags & NHRP_DEBUG_ROUTE)) { |
154 | char buf[2][PREFIX_STRLEN]; | |
37dc8ab5 | 155 | |
9350687d RW |
156 | prefix2str(&api.prefix, buf[0], sizeof(buf[0])); |
157 | zlog_debug("Zebra send: route %s %s nexthop %s metric %u" | |
158 | " count %d dev %s", | |
159 | add ? "add" : "del", buf[0], | |
160 | nexthop ? inet_ntop(api.prefix.family, &api_nh->gate, buf[1], sizeof(buf[1])) : "<onlink>", | |
161 | api.metric, api.nexthop_num, ifp->name); | |
2fb975da | 162 | } |
9350687d RW |
163 | |
164 | zclient_route_send(add ? ZEBRA_ROUTE_ADD : ZEBRA_ROUTE_DELETE, zclient, | |
165 | &api); | |
2fb975da TT |
166 | } |
167 | ||
168 | int nhrp_route_read(int cmd, struct zclient *zclient, zebra_size_t length, vrf_id_t vrf_id) | |
169 | { | |
170 | struct stream *s; | |
171 | struct interface *ifp = NULL; | |
172 | struct prefix prefix; | |
c125d1d4 | 173 | struct prefix_ipv6 src_p; |
2fb975da TT |
174 | union sockunion nexthop_addr; |
175 | unsigned char message, nexthop_num, ifindex_num; | |
176 | unsigned ifindex; | |
177 | char buf[2][PREFIX_STRLEN]; | |
178 | int i, afaddrlen, added; | |
179 | ||
180 | s = zclient->ibuf; | |
181 | memset(&prefix, 0, sizeof(prefix)); | |
182 | sockunion_family(&nexthop_addr) = AF_UNSPEC; | |
183 | ||
184 | /* Type, flags, message. */ | |
185 | /*type =*/ stream_getc(s); | |
a1f1bab0 JB |
186 | /*instance =*/ stream_getw(s); |
187 | /*flags =*/ stream_getl(s); | |
2fb975da TT |
188 | message = stream_getc(s); |
189 | ||
190 | /* Prefix */ | |
191 | switch (cmd) { | |
a1f1bab0 JB |
192 | case ZEBRA_REDISTRIBUTE_IPV4_ADD: |
193 | case ZEBRA_REDISTRIBUTE_IPV4_DEL: | |
2fb975da | 194 | prefix.family = AF_INET; |
e959008b | 195 | prefix.prefixlen = MIN(IPV4_MAX_PREFIXLEN, stream_getc(s)); |
2fb975da | 196 | break; |
a1f1bab0 JB |
197 | case ZEBRA_REDISTRIBUTE_IPV6_ADD: |
198 | case ZEBRA_REDISTRIBUTE_IPV6_DEL: | |
2fb975da | 199 | prefix.family = AF_INET6; |
e959008b | 200 | prefix.prefixlen = MIN(IPV6_MAX_PREFIXLEN, stream_getc(s)); |
2fb975da TT |
201 | break; |
202 | default: | |
203 | return -1; | |
204 | } | |
205 | afaddrlen = family2addrsize(prefix.family); | |
2fb975da TT |
206 | stream_get(&prefix.u.val, s, PSIZE(prefix.prefixlen)); |
207 | ||
c125d1d4 RW |
208 | memset(&src_p, 0, sizeof(src_p)); |
209 | if (prefix.family == AF_INET6 && | |
210 | CHECK_FLAG(message, ZAPI_MESSAGE_SRCPFX)) { | |
211 | src_p.family = AF_INET6; | |
212 | src_p.prefixlen = stream_getc(s); | |
213 | stream_get(&src_p.prefix, s, PSIZE(src_p.prefixlen)); | |
214 | } | |
215 | if (src_p.prefixlen) | |
216 | /* we completely ignore srcdest routes for now. */ | |
217 | return 0; | |
218 | ||
2fb975da TT |
219 | /* Nexthop, ifindex, distance, metric. */ |
220 | if (CHECK_FLAG(message, ZAPI_MESSAGE_NEXTHOP|ZAPI_MESSAGE_IFINDEX)) { | |
221 | nexthop_num = stream_getc(s); | |
222 | for (i = 0; i < nexthop_num; i++) { | |
223 | stream_get(buf[0], s, afaddrlen); | |
224 | if (i == 0) sockunion_set(&nexthop_addr, prefix.family, (u_char*) buf[0], afaddrlen); | |
225 | } | |
226 | ifindex_num = stream_getc(s); | |
227 | for (i = 0; i < ifindex_num; i++) { | |
228 | ifindex = stream_getl(s); | |
229 | if (i == 0 && ifindex != IFINDEX_INTERNAL) | |
7e2b7603 | 230 | ifp = if_lookup_by_index(ifindex, VRF_DEFAULT); |
2fb975da TT |
231 | } |
232 | } | |
233 | if (CHECK_FLAG(message, ZAPI_MESSAGE_DISTANCE)) | |
234 | /*distance =*/ stream_getc(s); | |
235 | if (CHECK_FLAG(message, ZAPI_MESSAGE_METRIC)) | |
236 | /*metric =*/ stream_getl(s); | |
237 | ||
a1f1bab0 | 238 | added = (cmd == ZEBRA_REDISTRIBUTE_IPV4_ADD || cmd == ZEBRA_REDISTRIBUTE_IPV6_ADD); |
2fb975da TT |
239 | debugf(NHRP_DEBUG_ROUTE, "if-route-%s: %s via %s dev %s", |
240 | added ? "add" : "del", | |
241 | prefix2str(&prefix, buf[0], sizeof buf[0]), | |
242 | sockunion2str(&nexthop_addr, buf[1], sizeof buf[1]), | |
243 | ifp ? ifp->name : "(none)"); | |
244 | ||
245 | nhrp_route_update_zebra(&prefix, &nexthop_addr, ifp); | |
246 | nhrp_shortcut_prefix_change(&prefix, !added); | |
247 | ||
248 | return 0; | |
249 | } | |
250 | ||
251 | int nhrp_route_get_nexthop(const union sockunion *addr, struct prefix *p, union sockunion *via, struct interface **ifp) | |
252 | { | |
253 | struct route_node *rn; | |
254 | struct route_info *ri; | |
255 | struct prefix lookup; | |
256 | afi_t afi = family2afi(sockunion_family(addr)); | |
257 | char buf[PREFIX_STRLEN]; | |
258 | ||
259 | sockunion2hostprefix(addr, &lookup); | |
260 | ||
261 | rn = route_node_match(zebra_rib[afi], &lookup); | |
262 | if (!rn) return 0; | |
263 | ||
264 | ri = rn->info; | |
265 | if (ri->nhrp_ifp) { | |
266 | debugf(NHRP_DEBUG_ROUTE, "lookup %s: nhrp_if=%s", | |
267 | prefix2str(&lookup, buf, sizeof buf), | |
268 | ri->nhrp_ifp->name); | |
269 | ||
270 | if (via) sockunion_family(via) = AF_UNSPEC; | |
271 | if (ifp) *ifp = ri->nhrp_ifp; | |
272 | } else { | |
273 | debugf(NHRP_DEBUG_ROUTE, "lookup %s: zebra route dev %s", | |
274 | prefix2str(&lookup, buf, sizeof buf), | |
275 | ri->ifp ? ri->ifp->name : "(none)"); | |
276 | ||
277 | if (via) *via = ri->via; | |
278 | if (ifp) *ifp = ri->ifp; | |
279 | } | |
280 | if (p) *p = rn->p; | |
281 | route_unlock_node(rn); | |
282 | return 1; | |
283 | } | |
284 | ||
285 | enum nhrp_route_type nhrp_route_address(struct interface *in_ifp, union sockunion *addr, struct prefix *p, struct nhrp_peer **peer) | |
286 | { | |
287 | struct interface *ifp = in_ifp; | |
288 | struct nhrp_interface *nifp; | |
289 | struct nhrp_cache *c; | |
290 | union sockunion via[4]; | |
291 | uint32_t network_id = 0; | |
292 | afi_t afi = family2afi(sockunion_family(addr)); | |
293 | int i; | |
294 | ||
295 | if (ifp) { | |
296 | nifp = ifp->info; | |
297 | network_id = nifp->afi[afi].network_id; | |
298 | ||
299 | c = nhrp_cache_get(ifp, addr, 0); | |
300 | if (c && c->cur.type == NHRP_CACHE_LOCAL) { | |
301 | if (p) memset(p, 0, sizeof(*p)); | |
302 | return NHRP_ROUTE_LOCAL; | |
303 | } | |
304 | } | |
305 | ||
306 | for (i = 0; i < 4; i++) { | |
307 | if (!nhrp_route_get_nexthop(addr, p, &via[i], &ifp)) | |
308 | return NHRP_ROUTE_BLACKHOLE; | |
309 | if (ifp) { | |
310 | /* Departing from nbma network? */ | |
311 | nifp = ifp->info; | |
312 | if (network_id && network_id != nifp->afi[afi].network_id) | |
313 | return NHRP_ROUTE_OFF_NBMA; | |
314 | } | |
315 | if (sockunion_family(&via[i]) == AF_UNSPEC) | |
316 | break; | |
317 | /* Resolve via node, but return the prefix of first match */ | |
318 | addr = &via[i]; | |
319 | p = NULL; | |
320 | } | |
321 | ||
322 | if (ifp) { | |
323 | c = nhrp_cache_get(ifp, addr, 0); | |
324 | if (c && c->cur.type >= NHRP_CACHE_DYNAMIC) { | |
325 | if (p) memset(p, 0, sizeof(*p)); | |
326 | if (c->cur.type == NHRP_CACHE_LOCAL) | |
327 | return NHRP_ROUTE_LOCAL; | |
328 | if (peer) *peer = nhrp_peer_ref(c->cur.peer); | |
329 | return NHRP_ROUTE_NBMA_NEXTHOP; | |
330 | } | |
331 | } | |
332 | ||
333 | return NHRP_ROUTE_BLACKHOLE; | |
334 | } | |
335 | ||
a1f1bab0 JB |
336 | static void |
337 | nhrp_zebra_connected (struct zclient *zclient) | |
338 | { | |
339 | zclient_send_reg_requests(zclient, VRF_DEFAULT); | |
340 | zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP, | |
341 | ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT); | |
342 | zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP6, | |
343 | ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT); | |
344 | } | |
345 | ||
2fb975da TT |
346 | void nhrp_zebra_init(void) |
347 | { | |
348 | zebra_rib[AFI_IP] = route_table_init(); | |
349 | zebra_rib[AFI_IP6] = route_table_init(); | |
350 | ||
351 | zclient = zclient_new(master); | |
a1f1bab0 | 352 | zclient->zebra_connected = nhrp_zebra_connected; |
2fb975da TT |
353 | zclient->interface_add = nhrp_interface_add; |
354 | zclient->interface_delete = nhrp_interface_delete; | |
355 | zclient->interface_up = nhrp_interface_up; | |
356 | zclient->interface_down = nhrp_interface_down; | |
357 | zclient->interface_address_add = nhrp_interface_address_add; | |
358 | zclient->interface_address_delete = nhrp_interface_address_delete; | |
819dc8bb DL |
359 | zclient->redistribute_route_ipv4_add = nhrp_route_read; |
360 | zclient->redistribute_route_ipv4_del = nhrp_route_read; | |
361 | zclient->redistribute_route_ipv6_add = nhrp_route_read; | |
362 | zclient->redistribute_route_ipv6_del = nhrp_route_read; | |
363 | ||
364 | zclient_init(zclient, ZEBRA_ROUTE_NHRP, 0); | |
2fb975da TT |
365 | } |
366 | ||
367 | void nhrp_zebra_terminate(void) | |
368 | { | |
369 | zclient_stop(zclient); | |
20a11b25 | 370 | zclient_free(zclient); |
2fb975da TT |
371 | route_table_finish(zebra_rib[AFI_IP]); |
372 | route_table_finish(zebra_rib[AFI_IP6]); | |
373 | } | |
374 |