]> git.proxmox.com Git - mirror_frr.git/blob - nhrpd/nhrp_route.c
bgpd: fix large route-distinguisher's format
[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(ZAPI_CALLBACK_ARGS)
188 {
189 struct zapi_route api;
190 struct zapi_nexthop *api_nh;
191 struct interface *ifp = NULL;
192 union sockunion nexthop_addr;
193 char buf[2][PREFIX_STRLEN];
194 int added;
195
196 if (zapi_route_decode(zclient->ibuf, &api) < 0)
197 return -1;
198
199 /* we completely ignore srcdest routes for now. */
200 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
201 return 0;
202
203 sockunion_family(&nexthop_addr) = AF_UNSPEC;
204 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP)) {
205 api_nh = &api.nexthops[0];
206
207 nexthop_addr.sa.sa_family = api.prefix.family;
208 switch (nexthop_addr.sa.sa_family) {
209 case AF_INET:
210 nexthop_addr.sin.sin_addr = api_nh->gate.ipv4;
211 break;
212 case AF_INET6:
213 nexthop_addr.sin6.sin6_addr = api_nh->gate.ipv6;
214 break;
215 }
216
217 if (api_nh->ifindex != IFINDEX_INTERNAL)
218 ifp = if_lookup_by_index(api_nh->ifindex, VRF_DEFAULT);
219 }
220
221 added = (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD);
222 debugf(NHRP_DEBUG_ROUTE, "if-route-%s: %s via %s dev %s",
223 added ? "add" : "del",
224 prefix2str(&api.prefix, buf[0], sizeof buf[0]),
225 sockunion2str(&nexthop_addr, buf[1], sizeof buf[1]),
226 ifp ? ifp->name : "(none)");
227
228 nhrp_route_update_zebra(&api.prefix, &nexthop_addr, ifp);
229 nhrp_shortcut_prefix_change(&api.prefix, !added);
230
231 return 0;
232 }
233
234 int nhrp_route_get_nexthop(const union sockunion *addr, struct prefix *p,
235 union sockunion *via, struct interface **ifp)
236 {
237 struct route_node *rn;
238 struct route_info *ri;
239 struct prefix lookup;
240 afi_t afi = family2afi(sockunion_family(addr));
241 char buf[PREFIX_STRLEN];
242
243 sockunion2hostprefix(addr, &lookup);
244
245 rn = route_node_match(zebra_rib[afi], &lookup);
246 if (!rn)
247 return 0;
248
249 ri = rn->info;
250 if (ri->nhrp_ifp) {
251 debugf(NHRP_DEBUG_ROUTE, "lookup %s: nhrp_if=%s",
252 prefix2str(&lookup, buf, sizeof buf),
253 ri->nhrp_ifp->name);
254
255 if (via)
256 sockunion_family(via) = AF_UNSPEC;
257 if (ifp)
258 *ifp = ri->nhrp_ifp;
259 } else {
260 debugf(NHRP_DEBUG_ROUTE, "lookup %s: zebra route dev %s",
261 prefix2str(&lookup, buf, sizeof buf),
262 ri->ifp ? ri->ifp->name : "(none)");
263
264 if (via)
265 *via = ri->via;
266 if (ifp)
267 *ifp = ri->ifp;
268 }
269 if (p)
270 *p = rn->p;
271 route_unlock_node(rn);
272 return 1;
273 }
274
275 enum nhrp_route_type nhrp_route_address(struct interface *in_ifp,
276 union sockunion *addr, struct prefix *p,
277 struct nhrp_peer **peer)
278 {
279 struct interface *ifp = in_ifp;
280 struct nhrp_interface *nifp;
281 struct nhrp_cache *c;
282 union sockunion via[4];
283 uint32_t network_id = 0;
284 afi_t afi = family2afi(sockunion_family(addr));
285 int i;
286
287 if (ifp) {
288 nifp = ifp->info;
289 network_id = nifp->afi[afi].network_id;
290
291 c = nhrp_cache_get(ifp, addr, 0);
292 if (c && c->cur.type == NHRP_CACHE_LOCAL) {
293 if (p)
294 memset(p, 0, sizeof(*p));
295 return NHRP_ROUTE_LOCAL;
296 }
297 }
298
299 for (i = 0; i < 4; i++) {
300 if (!nhrp_route_get_nexthop(addr, p, &via[i], &ifp))
301 return NHRP_ROUTE_BLACKHOLE;
302 if (ifp) {
303 /* Departing from nbma network? */
304 nifp = ifp->info;
305 if (network_id
306 && network_id != nifp->afi[afi].network_id)
307 return NHRP_ROUTE_OFF_NBMA;
308 }
309 if (sockunion_family(&via[i]) == AF_UNSPEC)
310 break;
311 /* Resolve via node, but return the prefix of first match */
312 addr = &via[i];
313 p = NULL;
314 }
315
316 if (ifp) {
317 c = nhrp_cache_get(ifp, addr, 0);
318 if (c && c->cur.type >= NHRP_CACHE_DYNAMIC) {
319 if (p)
320 memset(p, 0, sizeof(*p));
321 if (c->cur.type == NHRP_CACHE_LOCAL)
322 return NHRP_ROUTE_LOCAL;
323 if (peer)
324 *peer = nhrp_peer_ref(c->cur.peer);
325 return NHRP_ROUTE_NBMA_NEXTHOP;
326 }
327 }
328
329 return NHRP_ROUTE_BLACKHOLE;
330 }
331
332 static void nhrp_zebra_connected(struct zclient *zclient)
333 {
334 zclient_send_reg_requests(zclient, VRF_DEFAULT);
335 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP,
336 ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT);
337 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP6,
338 ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT);
339 }
340
341 void nhrp_zebra_init(void)
342 {
343 zebra_rib[AFI_IP] = route_table_init();
344 zebra_rib[AFI_IP6] = route_table_init();
345
346 zclient = zclient_new(master, &zclient_options_default);
347 zclient->zebra_connected = nhrp_zebra_connected;
348 zclient->interface_add = nhrp_interface_add;
349 zclient->interface_delete = nhrp_interface_delete;
350 zclient->interface_up = nhrp_interface_up;
351 zclient->interface_down = nhrp_interface_down;
352 zclient->interface_address_add = nhrp_interface_address_add;
353 zclient->interface_address_delete = nhrp_interface_address_delete;
354 zclient->redistribute_route_add = nhrp_route_read;
355 zclient->redistribute_route_del = nhrp_route_read;
356
357 zclient_init(zclient, ZEBRA_ROUTE_NHRP, 0, &nhrpd_privs);
358 }
359
360 void nhrp_zebra_terminate(void)
361 {
362 zclient_stop(zclient);
363 zclient_free(zclient);
364 route_table_finish(zebra_rib[AFI_IP]);
365 route_table_finish(zebra_rib[AFI_IP6]);
366 }