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