]>
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; | |
196 | union sockunion nexthop_addr; | |
197 | unsigned char message, nexthop_num, ifindex_num; | |
198 | unsigned ifindex; | |
199 | char buf[2][PREFIX_STRLEN]; | |
200 | int i, afaddrlen, added; | |
201 | ||
202 | s = zclient->ibuf; | |
203 | memset(&prefix, 0, sizeof(prefix)); | |
204 | sockunion_family(&nexthop_addr) = AF_UNSPEC; | |
205 | ||
206 | /* Type, flags, message. */ | |
207 | /*type =*/ stream_getc(s); | |
a1f1bab0 JB |
208 | /*instance =*/ stream_getw(s); |
209 | /*flags =*/ stream_getl(s); | |
2fb975da TT |
210 | message = stream_getc(s); |
211 | ||
212 | /* Prefix */ | |
213 | switch (cmd) { | |
a1f1bab0 JB |
214 | case ZEBRA_REDISTRIBUTE_IPV4_ADD: |
215 | case ZEBRA_REDISTRIBUTE_IPV4_DEL: | |
2fb975da TT |
216 | prefix.family = AF_INET; |
217 | break; | |
a1f1bab0 JB |
218 | case ZEBRA_REDISTRIBUTE_IPV6_ADD: |
219 | case ZEBRA_REDISTRIBUTE_IPV6_DEL: | |
2fb975da TT |
220 | prefix.family = AF_INET6; |
221 | break; | |
222 | default: | |
223 | return -1; | |
224 | } | |
225 | afaddrlen = family2addrsize(prefix.family); | |
226 | prefix.prefixlen = stream_getc(s); | |
227 | stream_get(&prefix.u.val, s, PSIZE(prefix.prefixlen)); | |
228 | ||
229 | /* Nexthop, ifindex, distance, metric. */ | |
230 | if (CHECK_FLAG(message, ZAPI_MESSAGE_NEXTHOP|ZAPI_MESSAGE_IFINDEX)) { | |
231 | nexthop_num = stream_getc(s); | |
232 | for (i = 0; i < nexthop_num; i++) { | |
233 | stream_get(buf[0], s, afaddrlen); | |
234 | if (i == 0) sockunion_set(&nexthop_addr, prefix.family, (u_char*) buf[0], afaddrlen); | |
235 | } | |
236 | ifindex_num = stream_getc(s); | |
237 | for (i = 0; i < ifindex_num; i++) { | |
238 | ifindex = stream_getl(s); | |
239 | if (i == 0 && ifindex != IFINDEX_INTERNAL) | |
7e2b7603 | 240 | ifp = if_lookup_by_index(ifindex, VRF_DEFAULT); |
2fb975da TT |
241 | } |
242 | } | |
243 | if (CHECK_FLAG(message, ZAPI_MESSAGE_DISTANCE)) | |
244 | /*distance =*/ stream_getc(s); | |
245 | if (CHECK_FLAG(message, ZAPI_MESSAGE_METRIC)) | |
246 | /*metric =*/ stream_getl(s); | |
247 | ||
a1f1bab0 | 248 | added = (cmd == ZEBRA_REDISTRIBUTE_IPV4_ADD || cmd == ZEBRA_REDISTRIBUTE_IPV6_ADD); |
2fb975da TT |
249 | debugf(NHRP_DEBUG_ROUTE, "if-route-%s: %s via %s dev %s", |
250 | added ? "add" : "del", | |
251 | prefix2str(&prefix, buf[0], sizeof buf[0]), | |
252 | sockunion2str(&nexthop_addr, buf[1], sizeof buf[1]), | |
253 | ifp ? ifp->name : "(none)"); | |
254 | ||
255 | nhrp_route_update_zebra(&prefix, &nexthop_addr, ifp); | |
256 | nhrp_shortcut_prefix_change(&prefix, !added); | |
257 | ||
258 | return 0; | |
259 | } | |
260 | ||
261 | int nhrp_route_get_nexthop(const union sockunion *addr, struct prefix *p, union sockunion *via, struct interface **ifp) | |
262 | { | |
263 | struct route_node *rn; | |
264 | struct route_info *ri; | |
265 | struct prefix lookup; | |
266 | afi_t afi = family2afi(sockunion_family(addr)); | |
267 | char buf[PREFIX_STRLEN]; | |
268 | ||
269 | sockunion2hostprefix(addr, &lookup); | |
270 | ||
271 | rn = route_node_match(zebra_rib[afi], &lookup); | |
272 | if (!rn) return 0; | |
273 | ||
274 | ri = rn->info; | |
275 | if (ri->nhrp_ifp) { | |
276 | debugf(NHRP_DEBUG_ROUTE, "lookup %s: nhrp_if=%s", | |
277 | prefix2str(&lookup, buf, sizeof buf), | |
278 | ri->nhrp_ifp->name); | |
279 | ||
280 | if (via) sockunion_family(via) = AF_UNSPEC; | |
281 | if (ifp) *ifp = ri->nhrp_ifp; | |
282 | } else { | |
283 | debugf(NHRP_DEBUG_ROUTE, "lookup %s: zebra route dev %s", | |
284 | prefix2str(&lookup, buf, sizeof buf), | |
285 | ri->ifp ? ri->ifp->name : "(none)"); | |
286 | ||
287 | if (via) *via = ri->via; | |
288 | if (ifp) *ifp = ri->ifp; | |
289 | } | |
290 | if (p) *p = rn->p; | |
291 | route_unlock_node(rn); | |
292 | return 1; | |
293 | } | |
294 | ||
295 | enum nhrp_route_type nhrp_route_address(struct interface *in_ifp, union sockunion *addr, struct prefix *p, struct nhrp_peer **peer) | |
296 | { | |
297 | struct interface *ifp = in_ifp; | |
298 | struct nhrp_interface *nifp; | |
299 | struct nhrp_cache *c; | |
300 | union sockunion via[4]; | |
301 | uint32_t network_id = 0; | |
302 | afi_t afi = family2afi(sockunion_family(addr)); | |
303 | int i; | |
304 | ||
305 | if (ifp) { | |
306 | nifp = ifp->info; | |
307 | network_id = nifp->afi[afi].network_id; | |
308 | ||
309 | c = nhrp_cache_get(ifp, addr, 0); | |
310 | if (c && c->cur.type == NHRP_CACHE_LOCAL) { | |
311 | if (p) memset(p, 0, sizeof(*p)); | |
312 | return NHRP_ROUTE_LOCAL; | |
313 | } | |
314 | } | |
315 | ||
316 | for (i = 0; i < 4; i++) { | |
317 | if (!nhrp_route_get_nexthop(addr, p, &via[i], &ifp)) | |
318 | return NHRP_ROUTE_BLACKHOLE; | |
319 | if (ifp) { | |
320 | /* Departing from nbma network? */ | |
321 | nifp = ifp->info; | |
322 | if (network_id && network_id != nifp->afi[afi].network_id) | |
323 | return NHRP_ROUTE_OFF_NBMA; | |
324 | } | |
325 | if (sockunion_family(&via[i]) == AF_UNSPEC) | |
326 | break; | |
327 | /* Resolve via node, but return the prefix of first match */ | |
328 | addr = &via[i]; | |
329 | p = NULL; | |
330 | } | |
331 | ||
332 | if (ifp) { | |
333 | c = nhrp_cache_get(ifp, addr, 0); | |
334 | if (c && c->cur.type >= NHRP_CACHE_DYNAMIC) { | |
335 | if (p) memset(p, 0, sizeof(*p)); | |
336 | if (c->cur.type == NHRP_CACHE_LOCAL) | |
337 | return NHRP_ROUTE_LOCAL; | |
338 | if (peer) *peer = nhrp_peer_ref(c->cur.peer); | |
339 | return NHRP_ROUTE_NBMA_NEXTHOP; | |
340 | } | |
341 | } | |
342 | ||
343 | return NHRP_ROUTE_BLACKHOLE; | |
344 | } | |
345 | ||
a1f1bab0 JB |
346 | static void |
347 | nhrp_zebra_connected (struct zclient *zclient) | |
348 | { | |
349 | zclient_send_reg_requests(zclient, VRF_DEFAULT); | |
350 | zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP, | |
351 | ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT); | |
352 | zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP6, | |
353 | ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT); | |
354 | } | |
355 | ||
2fb975da TT |
356 | void nhrp_zebra_init(void) |
357 | { | |
358 | zebra_rib[AFI_IP] = route_table_init(); | |
359 | zebra_rib[AFI_IP6] = route_table_init(); | |
360 | ||
361 | zclient = zclient_new(master); | |
a1f1bab0 | 362 | zclient->zebra_connected = nhrp_zebra_connected; |
2fb975da TT |
363 | zclient->interface_add = nhrp_interface_add; |
364 | zclient->interface_delete = nhrp_interface_delete; | |
365 | zclient->interface_up = nhrp_interface_up; | |
366 | zclient->interface_down = nhrp_interface_down; | |
367 | zclient->interface_address_add = nhrp_interface_address_add; | |
368 | zclient->interface_address_delete = nhrp_interface_address_delete; | |
819dc8bb DL |
369 | zclient->redistribute_route_ipv4_add = nhrp_route_read; |
370 | zclient->redistribute_route_ipv4_del = nhrp_route_read; | |
371 | zclient->redistribute_route_ipv6_add = nhrp_route_read; | |
372 | zclient->redistribute_route_ipv6_del = nhrp_route_read; | |
373 | ||
374 | zclient_init(zclient, ZEBRA_ROUTE_NHRP, 0); | |
2fb975da TT |
375 | } |
376 | ||
377 | void nhrp_zebra_terminate(void) | |
378 | { | |
379 | zclient_stop(zclient); | |
20a11b25 | 380 | zclient_free(zclient); |
2fb975da TT |
381 | route_table_finish(zebra_rib[AFI_IP]); |
382 | route_table_finish(zebra_rib[AFI_IP6]); | |
383 | } | |
384 |