1 /* NHRP routing functions
2 * Copyright (c) 2014-2015 Timo Teräs
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.
17 static struct zclient *zclient;
18 static struct route_table *zebra_rib[AFI_MAX];
22 struct interface *ifp;
23 struct interface *nhrp_ifp;
26 static void nhrp_zebra_connected(struct zclient *zclient)
28 /* No real VRF support yet -- bind only to the default vrf */
29 zclient_send_requests (zclient, VRF_DEFAULT);
32 static struct route_node *nhrp_route_update_get(const struct prefix *p, int create)
34 struct route_node *rn;
35 afi_t afi = family2afi(PREFIX_FAMILY(p));
41 rn = route_node_get(zebra_rib[afi], p);
43 rn->info = XCALLOC(MTYPE_NHRP_ROUTE, sizeof(struct route_info));
48 return route_node_lookup(zebra_rib[afi], p);
52 static void nhrp_route_update_put(struct route_node *rn)
54 struct route_info *ri = rn->info;
56 if (!ri->ifp && !ri->nhrp_ifp && sockunion_family(&ri->via) == AF_UNSPEC) {
57 XFREE(MTYPE_NHRP_ROUTE, rn->info);
59 route_unlock_node(rn);
61 route_unlock_node(rn);
64 static void nhrp_route_update_zebra(const struct prefix *p, union sockunion *nexthop, struct interface *ifp)
66 struct route_node *rn;
67 struct route_info *ri;
69 rn = nhrp_route_update_get(p, (sockunion_family(nexthop) != AF_UNSPEC) || ifp);
74 nhrp_route_update_put(rn);
78 void nhrp_route_update_nhrp(const struct prefix *p, struct interface *ifp)
80 struct route_node *rn;
81 struct route_info *ri;
83 rn = nhrp_route_update_get(p, ifp != NULL);
87 nhrp_route_update_put(rn);
91 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)
95 if (zclient->sock < 0)
99 case NHRP_CACHE_NEGATIVE:
100 SET_FLAG(flags, ZEBRA_FLAG_REJECT);
102 case NHRP_CACHE_DYNAMIC:
104 case NHRP_CACHE_STATIC:
105 /* Regular route, so these are announced
106 * to other routing daemons */
109 SET_FLAG(flags, ZEBRA_FLAG_FIB_OVERRIDE);
112 SET_FLAG(flags, ZEBRA_FLAG_INTERNAL);
114 if (p->family == AF_INET) {
115 struct in_addr *nexthop_ipv4;
116 struct zapi_ipv4 api;
118 memset(&api, 0, sizeof(api));
120 api.type = ZEBRA_ROUTE_NHRP;
121 api.safi = SAFI_UNICAST;
123 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
125 nexthop_ipv4 = (struct in_addr *) sockunion_get_addr(nexthop);
127 api.nexthop = &nexthop_ipv4;
130 SET_FLAG(api.message, ZAPI_MESSAGE_IFINDEX);
132 api.ifindex = &ifp->ifindex;
135 SET_FLAG(api.message, ZAPI_MESSAGE_MTU);
139 if (unlikely(debug_flags & NHRP_DEBUG_ROUTE)) {
140 char buf[2][INET_ADDRSTRLEN];
141 zlog_debug("Zebra send: IPv4 route %s %s/%d nexthop %s metric %u"
144 inet_ntop(AF_INET, &p->u.prefix4, buf[0], sizeof(buf[0])),
146 nexthop ? inet_ntop(AF_INET, api.nexthop[0], buf[1], sizeof(buf[1])) : "<onlink>",
147 api.metric, api.nexthop_num, ifp->name);
151 add ? ZEBRA_IPV4_ROUTE_ADD : ZEBRA_IPV4_ROUTE_DELETE,
152 zclient, (struct prefix_ipv4 *) p, &api);
153 } else if (p->family == AF_INET6) {
154 struct in6_addr *nexthop_ipv6;
155 struct zapi_ipv6 api;
157 memset(&api, 0, sizeof(api));
159 api.type = ZEBRA_ROUTE_NHRP;
160 api.safi = SAFI_UNICAST;
162 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
164 nexthop_ipv6 = (struct in6_addr *) sockunion_get_addr(nexthop);
166 api.nexthop = &nexthop_ipv6;
169 SET_FLAG(api.message, ZAPI_MESSAGE_IFINDEX);
171 api.ifindex = &ifp->ifindex;
174 SET_FLAG(api.message, ZAPI_MESSAGE_MTU);
178 if (unlikely(debug_flags & NHRP_DEBUG_ROUTE)) {
179 char buf[2][INET6_ADDRSTRLEN];
180 zlog_debug("Zebra send: IPv6 route %s %s/%d nexthop %s metric %u"
183 inet_ntop(AF_INET6, &p->u.prefix6, buf[0], sizeof(buf[0])),
185 nexthop ? inet_ntop(AF_INET6, api.nexthop[0], buf[1], sizeof(buf[1])) : "<onlink>",
186 api.metric, api.nexthop_num, ifp->name);
190 add ? ZEBRA_IPV6_ROUTE_ADD : ZEBRA_IPV6_ROUTE_DELETE,
191 zclient, (struct prefix_ipv6 *) p, &api);
195 int nhrp_route_read(int cmd, struct zclient *zclient, zebra_size_t length, vrf_id_t vrf_id)
198 struct interface *ifp = NULL;
199 struct prefix prefix;
200 union sockunion nexthop_addr;
201 unsigned char message, nexthop_num, ifindex_num;
203 char buf[2][PREFIX_STRLEN];
204 int i, afaddrlen, added;
207 memset(&prefix, 0, sizeof(prefix));
208 sockunion_family(&nexthop_addr) = AF_UNSPEC;
210 /* Type, flags, message. */
211 /*type =*/ stream_getc(s);
212 /*flags =*/ stream_getc(s);
213 message = stream_getc(s);
217 case ZEBRA_IPV4_ROUTE_ADD:
218 case ZEBRA_IPV4_ROUTE_DELETE:
219 prefix.family = AF_INET;
221 case ZEBRA_IPV6_ROUTE_ADD:
222 case ZEBRA_IPV6_ROUTE_DELETE:
223 prefix.family = AF_INET6;
228 afaddrlen = family2addrsize(prefix.family);
229 prefix.prefixlen = stream_getc(s);
230 stream_get(&prefix.u.val, s, PSIZE(prefix.prefixlen));
232 /* Nexthop, ifindex, distance, metric. */
233 if (CHECK_FLAG(message, ZAPI_MESSAGE_NEXTHOP|ZAPI_MESSAGE_IFINDEX)) {
234 nexthop_num = stream_getc(s);
235 for (i = 0; i < nexthop_num; i++) {
236 stream_get(buf[0], s, afaddrlen);
237 if (i == 0) sockunion_set(&nexthop_addr, prefix.family, (u_char*) buf[0], afaddrlen);
239 ifindex_num = stream_getc(s);
240 for (i = 0; i < ifindex_num; i++) {
241 ifindex = stream_getl(s);
242 if (i == 0 && ifindex != IFINDEX_INTERNAL)
243 ifp = if_lookup_by_index(ifindex);
246 if (CHECK_FLAG(message, ZAPI_MESSAGE_DISTANCE))
247 /*distance =*/ stream_getc(s);
248 if (CHECK_FLAG(message, ZAPI_MESSAGE_METRIC))
249 /*metric =*/ stream_getl(s);
251 added = (cmd == ZEBRA_IPV4_ROUTE_ADD || cmd == ZEBRA_IPV6_ROUTE_ADD);
252 debugf(NHRP_DEBUG_ROUTE, "if-route-%s: %s via %s dev %s",
253 added ? "add" : "del",
254 prefix2str(&prefix, buf[0], sizeof buf[0]),
255 sockunion2str(&nexthop_addr, buf[1], sizeof buf[1]),
256 ifp ? ifp->name : "(none)");
258 nhrp_route_update_zebra(&prefix, &nexthop_addr, ifp);
259 nhrp_shortcut_prefix_change(&prefix, !added);
264 int nhrp_route_get_nexthop(const union sockunion *addr, struct prefix *p, union sockunion *via, struct interface **ifp)
266 struct route_node *rn;
267 struct route_info *ri;
268 struct prefix lookup;
269 afi_t afi = family2afi(sockunion_family(addr));
270 char buf[PREFIX_STRLEN];
272 sockunion2hostprefix(addr, &lookup);
274 rn = route_node_match(zebra_rib[afi], &lookup);
279 debugf(NHRP_DEBUG_ROUTE, "lookup %s: nhrp_if=%s",
280 prefix2str(&lookup, buf, sizeof buf),
283 if (via) sockunion_family(via) = AF_UNSPEC;
284 if (ifp) *ifp = ri->nhrp_ifp;
286 debugf(NHRP_DEBUG_ROUTE, "lookup %s: zebra route dev %s",
287 prefix2str(&lookup, buf, sizeof buf),
288 ri->ifp ? ri->ifp->name : "(none)");
290 if (via) *via = ri->via;
291 if (ifp) *ifp = ri->ifp;
294 route_unlock_node(rn);
298 enum nhrp_route_type nhrp_route_address(struct interface *in_ifp, union sockunion *addr, struct prefix *p, struct nhrp_peer **peer)
300 struct interface *ifp = in_ifp;
301 struct nhrp_interface *nifp;
302 struct nhrp_cache *c;
303 union sockunion via[4];
304 uint32_t network_id = 0;
305 afi_t afi = family2afi(sockunion_family(addr));
310 network_id = nifp->afi[afi].network_id;
312 c = nhrp_cache_get(ifp, addr, 0);
313 if (c && c->cur.type == NHRP_CACHE_LOCAL) {
314 if (p) memset(p, 0, sizeof(*p));
315 return NHRP_ROUTE_LOCAL;
319 for (i = 0; i < 4; i++) {
320 if (!nhrp_route_get_nexthop(addr, p, &via[i], &ifp))
321 return NHRP_ROUTE_BLACKHOLE;
323 /* Departing from nbma network? */
325 if (network_id && network_id != nifp->afi[afi].network_id)
326 return NHRP_ROUTE_OFF_NBMA;
328 if (sockunion_family(&via[i]) == AF_UNSPEC)
330 /* Resolve via node, but return the prefix of first match */
336 c = nhrp_cache_get(ifp, addr, 0);
337 if (c && c->cur.type >= NHRP_CACHE_DYNAMIC) {
338 if (p) memset(p, 0, sizeof(*p));
339 if (c->cur.type == NHRP_CACHE_LOCAL)
340 return NHRP_ROUTE_LOCAL;
341 if (peer) *peer = nhrp_peer_ref(c->cur.peer);
342 return NHRP_ROUTE_NBMA_NEXTHOP;
346 return NHRP_ROUTE_BLACKHOLE;
349 void nhrp_zebra_init(void)
351 zebra_rib[AFI_IP] = route_table_init();
352 zebra_rib[AFI_IP6] = route_table_init();
354 zclient = zclient_new(master);
355 zclient->zebra_connected = nhrp_zebra_connected;
356 zclient->interface_add = nhrp_interface_add;
357 zclient->interface_delete = nhrp_interface_delete;
358 zclient->interface_up = nhrp_interface_up;
359 zclient->interface_down = nhrp_interface_down;
360 zclient->interface_address_add = nhrp_interface_address_add;
361 zclient->interface_address_delete = nhrp_interface_address_delete;
362 zclient->ipv4_route_add = nhrp_route_read;
363 zclient->ipv4_route_delete = nhrp_route_read;
364 zclient->ipv6_route_add = nhrp_route_read;
365 zclient->ipv6_route_delete = nhrp_route_read;
367 zclient_init(zclient, ZEBRA_ROUTE_NHRP);
368 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, ZEBRA_ROUTE_KERNEL, VRF_DEFAULT);
369 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, ZEBRA_ROUTE_CONNECT, VRF_DEFAULT);
370 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, ZEBRA_ROUTE_STATIC, VRF_DEFAULT);
371 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, ZEBRA_ROUTE_RIP, VRF_DEFAULT);
372 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, ZEBRA_ROUTE_OSPF, VRF_DEFAULT);
373 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, ZEBRA_ROUTE_ISIS, VRF_DEFAULT);
374 zclient_redistribute(ZEBRA_REDISTRIBUTE_ADD, zclient, ZEBRA_ROUTE_BGP, VRF_DEFAULT);
377 void nhrp_zebra_terminate(void)
379 zclient_stop(zclient);
380 route_table_finish(zebra_rib[AFI_IP]);
381 route_table_finish(zebra_rib[AFI_IP6]);