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