blob: cc6b5fa241f19ae7b86ac28346793a4cdcec5cd9 [file] [log] [blame]
Timo Teräsdafa05e2017-01-19 17:27:01 +02001/* 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
17static struct zclient *zclient;
18static struct route_table *zebra_rib[AFI_MAX];
19
20struct route_info {
21 union sockunion via;
22 struct interface *ifp;
23 struct interface *nhrp_ifp;
24};
25
26static void nhrp_zebra_connected(struct zclient *zclient)
27{
28 /* No real VRF support yet -- bind only to the default vrf */
29 zclient_send_requests (zclient, VRF_DEFAULT);
30}
31
32static struct route_node *nhrp_route_update_get(const struct prefix *p, int create)
33{
34 struct route_node *rn;
35 afi_t afi = family2afi(PREFIX_FAMILY(p));
36
37 if (!zebra_rib[afi])
38 return NULL;
39
40 if (create) {
41 rn = route_node_get(zebra_rib[afi], p);
42 if (!rn->info) {
43 rn->info = XCALLOC(MTYPE_NHRP_ROUTE, sizeof(struct route_info));
44 route_lock_node(rn);
45 }
46 return rn;
47 } else {
48 return route_node_lookup(zebra_rib[afi], p);
49 }
50}
51
52static void nhrp_route_update_put(struct route_node *rn)
53{
54 struct route_info *ri = rn->info;
55
56 if (!ri->ifp && !ri->nhrp_ifp && sockunion_family(&ri->via) == AF_UNSPEC) {
57 XFREE(MTYPE_NHRP_ROUTE, rn->info);
58 rn->info = NULL;
59 route_unlock_node(rn);
60 }
61 route_unlock_node(rn);
62}
63
64static void nhrp_route_update_zebra(const struct prefix *p, union sockunion *nexthop, struct interface *ifp)
65{
66 struct route_node *rn;
67 struct route_info *ri;
68
69 rn = nhrp_route_update_get(p, (sockunion_family(nexthop) != AF_UNSPEC) || ifp);
70 if (rn) {
71 ri = rn->info;
72 ri->via = *nexthop;
73 ri->ifp = ifp;
74 nhrp_route_update_put(rn);
75 }
76}
77
78void nhrp_route_update_nhrp(const struct prefix *p, struct interface *ifp)
79{
80 struct route_node *rn;
81 struct route_info *ri;
82
83 rn = nhrp_route_update_get(p, ifp != NULL);
84 if (rn) {
85 ri = rn->info;
86 ri->nhrp_ifp = ifp;
87 nhrp_route_update_put(rn);
88 }
89}
90
91void nhrp_route_announce(int add, enum nhrp_cache_type type, const struct prefix *p, struct interface *ifp, const union sockunion *nexthop, uint32_t mtu)
92{
93 struct in_addr *nexthop_ipv4;
94 int flags = 0;
95
96 if (zclient->sock < 0)
97 return;
98
99 switch (type) {
100 case NHRP_CACHE_NEGATIVE:
101 SET_FLAG(flags, ZEBRA_FLAG_REJECT);
102 break;
103 case NHRP_CACHE_DYNAMIC:
104 case NHRP_CACHE_NHS:
105 case NHRP_CACHE_STATIC:
106 /* Regular route, so these are announced
107 * to other routing daemons */
108 break;
109 default:
110 SET_FLAG(flags, ZEBRA_FLAG_FIB_OVERRIDE);
111 break;
112 }
113 SET_FLAG(flags, ZEBRA_FLAG_INTERNAL);
114
115 if (p->family == AF_INET) {
116 struct zapi_ipv4 api;
117
118 memset(&api, 0, sizeof(api));
119 api.flags = flags;
120 api.type = ZEBRA_ROUTE_NHRP;
121 api.safi = SAFI_UNICAST;
122
123 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
124 if (nexthop) {
125 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
126 nexthop_ipv4 = (struct in_addr *) sockunion_get_addr(nexthop);
127 api.nexthop_num = 1;
128 api.nexthop = &nexthop_ipv4;
129 }
130 if (ifp) {
131 SET_FLAG(api.message, ZAPI_MESSAGE_IFINDEX);
132 api.ifindex_num = 1;
133 api.ifindex = &ifp->ifindex;
134 }
135 if (mtu) {
136 SET_FLAG(api.message, ZAPI_MESSAGE_MTU);
137 api.mtu = mtu;
138 }
139
140 if (unlikely(debug_flags & NHRP_DEBUG_ROUTE)) {
141 char buf[2][INET_ADDRSTRLEN];
142 zlog_debug("Zebra send: IPv4 route %s %s/%d nexthop %s metric %u"
143 " count %d dev %s",
144 add ? "add" : "del",
145 inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])),
146 p->prefixlen,
147 nexthop ? inet_ntop(AF_INET, api.nexthop[0], buf[1], sizeof(buf[1])) : "<onlink>",
148 api.metric, api.nexthop_num, ifp->name);
149 }
150
151 zapi_ipv4_route(
152 add ? ZEBRA_IPV4_ROUTE_ADD : ZEBRA_IPV4_ROUTE_DELETE,
153 zclient, (struct prefix_ipv4 *) p, &api);
154 }
155}
156
157int nhrp_route_read(int cmd, struct zclient *zclient, zebra_size_t length, vrf_id_t vrf_id)
158{
159 struct stream *s;
160 struct interface *ifp = NULL;
161 struct prefix prefix;
162 union sockunion nexthop_addr;
163 unsigned char message, nexthop_num, ifindex_num;
164 unsigned ifindex;
165 char buf[2][PREFIX_STRLEN];
166 int i, afaddrlen, added;
167
168 s = zclient->ibuf;
169 memset(&prefix, 0, sizeof(prefix));
170 sockunion_family(&nexthop_addr) = AF_UNSPEC;
171
172 /* Type, flags, message. */
173 /*type =*/ stream_getc(s);
174 /*flags =*/ stream_getc(s);
175 message = stream_getc(s);
176
177 /* Prefix */
178 switch (cmd) {
179 case ZEBRA_IPV4_ROUTE_ADD:
180 case ZEBRA_IPV4_ROUTE_DELETE:
181 prefix.family = AF_INET;
182 break;
183 case ZEBRA_IPV6_ROUTE_ADD:
184 case ZEBRA_IPV6_ROUTE_DELETE:
185 prefix.family = AF_INET6;
186 break;
187 default:
188 return -1;
189 }
190 afaddrlen = family2addrsize(prefix.family);
191 prefix.prefixlen = stream_getc(s);
192 stream_get(&prefix.u.val, s, PSIZE(prefix.prefixlen));
193
194 /* Nexthop, ifindex, distance, metric. */
195 if (CHECK_FLAG(message, ZAPI_MESSAGE_NEXTHOP|ZAPI_MESSAGE_IFINDEX)) {
196 nexthop_num = stream_getc(s);
197 for (i = 0; i < nexthop_num; i++) {
198 stream_get(buf[0], s, afaddrlen);
199 if (i == 0) sockunion_set(&nexthop_addr, prefix.family, (u_char*) buf[0], afaddrlen);
200 }
201 ifindex_num = stream_getc(s);
202 for (i = 0; i < ifindex_num; i++) {
203 ifindex = stream_getl(s);
204 if (i == 0 && ifindex != IFINDEX_INTERNAL)
205 ifp = if_lookup_by_index(ifindex);
206 }
207 }
208 if (CHECK_FLAG(message, ZAPI_MESSAGE_DISTANCE))
209 /*distance =*/ stream_getc(s);
210 if (CHECK_FLAG(message, ZAPI_MESSAGE_METRIC))
211 /*metric =*/ stream_getl(s);
212
213 added = (cmd == ZEBRA_IPV4_ROUTE_ADD || cmd == ZEBRA_IPV6_ROUTE_ADD);
214 debugf(NHRP_DEBUG_ROUTE, "if-route-%s: %s via %s dev %s",
215 added ? "add" : "del",
216 prefix2str(&prefix, buf[0], sizeof buf[0]),
217 sockunion2str(&nexthop_addr, buf[1], sizeof buf[1]),
218 ifp ? ifp->name : "(none)");
219
220 nhrp_route_update_zebra(&prefix, &nexthop_addr, ifp);
221 nhrp_shortcut_prefix_change(&prefix, !added);
222
223 return 0;
224}
225
226int nhrp_route_get_nexthop(const union sockunion *addr, struct prefix *p, union sockunion *via, struct interface **ifp)
227{
228 struct route_node *rn;
229 struct route_info *ri;
230 struct prefix lookup;
231 afi_t afi = family2afi(sockunion_family(addr));
232 char buf[PREFIX_STRLEN];
233
234 sockunion2hostprefix(addr, &lookup);
235
236 rn = route_node_match(zebra_rib[afi], &lookup);
237 if (!rn) return 0;
238
239 ri = rn->info;
240 if (ri->nhrp_ifp) {
241 debugf(NHRP_DEBUG_ROUTE, "lookup %s: nhrp_if=%s",
242 prefix2str(&lookup, buf, sizeof buf),
243 ri->nhrp_ifp->name);
244
245 if (via) sockunion_family(via) = AF_UNSPEC;
246 if (ifp) *ifp = ri->nhrp_ifp;
247 } else {
248 debugf(NHRP_DEBUG_ROUTE, "lookup %s: zebra route dev %s",
249 prefix2str(&lookup, buf, sizeof buf),
250 ri->ifp ? ri->ifp->name : "(none)");
251
252 if (via) *via = ri->via;
253 if (ifp) *ifp = ri->ifp;
254 }
255 if (p) *p = rn->p;
256 route_unlock_node(rn);
257 return 1;
258}
259
260enum nhrp_route_type nhrp_route_address(struct interface *in_ifp, union sockunion *addr, struct prefix *p, struct nhrp_peer **peer)
261{
262 struct interface *ifp = in_ifp;
263 struct nhrp_interface *nifp;
264 struct nhrp_cache *c;
265 union sockunion via[4];
266 uint32_t network_id = 0;
267 afi_t afi = family2afi(sockunion_family(addr));
268 int i;
269
270 if (ifp) {
271 nifp = ifp->info;
272 network_id = nifp->afi[afi].network_id;
273
274 c = nhrp_cache_get(ifp, addr, 0);
275 if (c && c->cur.type == NHRP_CACHE_LOCAL) {
276 if (p) memset(p, 0, sizeof(*p));
277 return NHRP_ROUTE_LOCAL;
278 }
279 }
280
281 for (i = 0; i < 4; i++) {
282 if (!nhrp_route_get_nexthop(addr, p, &via[i], &ifp))
283 return NHRP_ROUTE_BLACKHOLE;
284 if (ifp) {
285 /* Departing from nbma network? */
286 nifp = ifp->info;
287 if (network_id && network_id != nifp->afi[afi].network_id)
288 return NHRP_ROUTE_OFF_NBMA;
289 }
290 if (sockunion_family(&via[i]) == AF_UNSPEC)
291 break;
292 /* Resolve via node, but return the prefix of first match */
293 addr = &via[i];
294 p = NULL;
295 }
296
297 if (ifp) {
298 c = nhrp_cache_get(ifp, addr, 0);
299 if (c && c->cur.type >= NHRP_CACHE_DYNAMIC) {
300 if (p) memset(p, 0, sizeof(*p));
301 if (c->cur.type == NHRP_CACHE_LOCAL)
302 return NHRP_ROUTE_LOCAL;
303 if (peer) *peer = nhrp_peer_ref(c->cur.peer);
304 return NHRP_ROUTE_NBMA_NEXTHOP;
305 }
306 }
307
308 return NHRP_ROUTE_BLACKHOLE;
309}
310
311void nhrp_zebra_init(void)
312{
313 zebra_rib[AFI_IP] = route_table_init();
314 zebra_rib[AFI_IP6] = route_table_init();
315
316 zclient = zclient_new(master);
317 zclient->zebra_connected = nhrp_zebra_connected;
318 zclient->interface_add = nhrp_interface_add;
319 zclient->interface_delete = nhrp_interface_delete;
320 zclient->interface_up = nhrp_interface_up;
321 zclient->interface_down = nhrp_interface_down;
322 zclient->interface_address_add = nhrp_interface_address_add;
323 zclient->interface_address_delete = nhrp_interface_address_delete;
324 zclient->ipv4_route_add = nhrp_route_read;
325 zclient->ipv4_route_delete = nhrp_route_read;
326 zclient->ipv6_route_add = nhrp_route_read;
327 zclient->ipv6_route_delete = nhrp_route_read;
328
329 zclient_init(zclient, ZEBRA_ROUTE_NHRP);
330 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, ZEBRA_ROUTE_KERNEL, VRF_DEFAULT);
331 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, ZEBRA_ROUTE_CONNECT, VRF_DEFAULT);
332 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, ZEBRA_ROUTE_STATIC, VRF_DEFAULT);
333 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, ZEBRA_ROUTE_RIP, VRF_DEFAULT);
334 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, ZEBRA_ROUTE_OSPF, VRF_DEFAULT);
335 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, ZEBRA_ROUTE_ISIS, VRF_DEFAULT);
336 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, ZEBRA_ROUTE_BGP, VRF_DEFAULT);
337}
338
339void nhrp_zebra_terminate(void)
340{
341 zclient_stop(zclient);
342 route_table_finish(zebra_rib[AFI_IP]);
343 route_table_finish(zebra_rib[AFI_IP6]);
344}
345