]> git.proxmox.com Git - mirror_frr.git/blob - nhrpd/nhrp_route.c
sharpd: Allow sharpd to accept nexthop group as part of route install
[mirror_frr.git] / nhrpd / nhrp_route.c
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 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13
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
21 DEFINE_MTYPE_STATIC(NHRPD, NHRP_ROUTE, "NHRP routing entry")
22
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
32 static struct route_node *nhrp_route_update_get(const struct prefix *p,
33 int create)
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) {
44 rn->info = XCALLOC(MTYPE_NHRP_ROUTE,
45 sizeof(struct route_info));
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
58 if (!ri->ifp && !ri->nhrp_ifp
59 && sockunion_family(&ri->via) == AF_UNSPEC) {
60 XFREE(MTYPE_NHRP_ROUTE, rn->info);
61 rn->info = NULL;
62 route_unlock_node(rn);
63 }
64 route_unlock_node(rn);
65 }
66
67 static void nhrp_route_update_zebra(const struct prefix *p,
68 union sockunion *nexthop,
69 struct interface *ifp)
70 {
71 struct route_node *rn;
72 struct route_info *ri;
73
74 rn = nhrp_route_update_get(
75 p, (sockunion_family(nexthop) != AF_UNSPEC) || ifp);
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
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)
100 {
101 struct zapi_route api;
102 struct zapi_nexthop *api_nh;
103
104 if (zclient->sock < 0)
105 return;
106
107 memset(&api, 0, sizeof(api));
108 api.type = ZEBRA_ROUTE_NHRP;
109 api.safi = SAFI_UNICAST;
110 api.vrf_id = VRF_DEFAULT;
111 api.prefix = *p;
112
113 switch (type) {
114 case NHRP_CACHE_NEGATIVE:
115 zapi_route_set_blackhole(&api, BLACKHOLE_REJECT);
116 ifp = NULL;
117 nexthop = NULL;
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:
126 SET_FLAG(api.flags, ZEBRA_FLAG_FIB_OVERRIDE);
127 break;
128 }
129 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
130
131 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
132 api.nexthop_num = 1;
133 api_nh = &api.nexthops[0];
134 api_nh->vrf_id = VRF_DEFAULT;
135
136 switch (api.prefix.family) {
137 case AF_INET:
138 if (nexthop) {
139 api_nh->gate.ipv4 = nexthop->sin.sin_addr;
140 api_nh->type = NEXTHOP_TYPE_IPV4;
141 }
142 if (ifp) {
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;
148 }
149 break;
150 case AF_INET6:
151 if (nexthop) {
152 api_nh->gate.ipv6 = nexthop->sin6.sin6_addr;
153 api_nh->type = NEXTHOP_TYPE_IPV6;
154 }
155 if (ifp) {
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;
161 }
162 break;
163 }
164 if (mtu) {
165 SET_FLAG(api.message, ZAPI_MESSAGE_MTU);
166 api.mtu = mtu;
167 }
168
169 if (unlikely(debug_flags & NHRP_DEBUG_ROUTE)) {
170 char buf[2][PREFIX_STRLEN];
171
172 prefix2str(&api.prefix, buf[0], sizeof(buf[0]));
173 zlog_debug(
174 "Zebra send: route %s %s nexthop %s metric %u"
175 " count %d dev %s",
176 add ? "add" : "del", buf[0],
177 nexthop ? inet_ntop(api.prefix.family, &api_nh->gate,
178 buf[1], sizeof(buf[1]))
179 : "<onlink>",
180 api.metric, api.nexthop_num, ifp ? ifp->name : "none");
181 }
182
183 zclient_route_send(add ? ZEBRA_ROUTE_ADD : ZEBRA_ROUTE_DELETE, zclient,
184 &api);
185 }
186
187 int nhrp_route_read(int cmd, struct zclient *zclient, zebra_size_t length,
188 vrf_id_t vrf_id)
189 {
190 struct zapi_route api;
191 struct zapi_nexthop *api_nh;
192 struct interface *ifp = NULL;
193 union sockunion nexthop_addr;
194 char buf[2][PREFIX_STRLEN];
195 int added;
196
197 if (zapi_route_decode(zclient->ibuf, &api) < 0)
198 return -1;
199
200 /* we completely ignore srcdest routes for now. */
201 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
202 return 0;
203
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;
216 }
217
218 if (api_nh->ifindex != IFINDEX_INTERNAL)
219 ifp = if_lookup_by_index(api_nh->ifindex, VRF_DEFAULT);
220 }
221
222 added = (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD);
223 debugf(NHRP_DEBUG_ROUTE, "if-route-%s: %s via %s dev %s",
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)");
228
229 nhrp_route_update_zebra(&api.prefix, &nexthop_addr, ifp);
230 nhrp_shortcut_prefix_change(&api.prefix, !added);
231
232 return 0;
233 }
234
235 int nhrp_route_get_nexthop(const union sockunion *addr, struct prefix *p,
236 union sockunion *via, struct interface **ifp)
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);
247 if (!rn)
248 return 0;
249
250 ri = rn->info;
251 if (ri->nhrp_ifp) {
252 debugf(NHRP_DEBUG_ROUTE, "lookup %s: nhrp_if=%s",
253 prefix2str(&lookup, buf, sizeof buf),
254 ri->nhrp_ifp->name);
255
256 if (via)
257 sockunion_family(via) = AF_UNSPEC;
258 if (ifp)
259 *ifp = ri->nhrp_ifp;
260 } else {
261 debugf(NHRP_DEBUG_ROUTE, "lookup %s: zebra route dev %s",
262 prefix2str(&lookup, buf, sizeof buf),
263 ri->ifp ? ri->ifp->name : "(none)");
264
265 if (via)
266 *via = ri->via;
267 if (ifp)
268 *ifp = ri->ifp;
269 }
270 if (p)
271 *p = rn->p;
272 route_unlock_node(rn);
273 return 1;
274 }
275
276 enum nhrp_route_type nhrp_route_address(struct interface *in_ifp,
277 union sockunion *addr, struct prefix *p,
278 struct nhrp_peer **peer)
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) {
294 if (p)
295 memset(p, 0, sizeof(*p));
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;
306 if (network_id
307 && network_id != nifp->afi[afi].network_id)
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) {
320 if (p)
321 memset(p, 0, sizeof(*p));
322 if (c->cur.type == NHRP_CACHE_LOCAL)
323 return NHRP_ROUTE_LOCAL;
324 if (peer)
325 *peer = nhrp_peer_ref(c->cur.peer);
326 return NHRP_ROUTE_NBMA_NEXTHOP;
327 }
328 }
329
330 return NHRP_ROUTE_BLACKHOLE;
331 }
332
333 static void nhrp_zebra_connected(struct zclient *zclient)
334 {
335 zclient_send_reg_requests(zclient, VRF_DEFAULT);
336 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP,
337 ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT);
338 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP6,
339 ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT);
340 }
341
342 void nhrp_zebra_init(void)
343 {
344 zebra_rib[AFI_IP] = route_table_init();
345 zebra_rib[AFI_IP6] = route_table_init();
346
347 zclient = zclient_new(master, &zclient_options_default);
348 zclient->zebra_connected = nhrp_zebra_connected;
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;
355 zclient->redistribute_route_add = nhrp_route_read;
356 zclient->redistribute_route_del = nhrp_route_read;
357
358 zclient_init(zclient, ZEBRA_ROUTE_NHRP, 0, &nhrpd_privs);
359 }
360
361 void nhrp_zebra_terminate(void)
362 {
363 zclient_stop(zclient);
364 zclient_free(zclient);
365 route_table_finish(zebra_rib[AFI_IP]);
366 route_table_finish(zebra_rib[AFI_IP6]);
367 }