]> git.proxmox.com Git - mirror_frr.git/blob - nhrpd/nhrp_route.c
Merge branch 'master' into docuser
[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 #include "nhrpd.h"
11 #include "table.h"
12 #include "memory.h"
13 #include "stream.h"
14 #include "log.h"
15 #include "zclient.h"
16
17 DEFINE_MTYPE_STATIC(NHRPD, NHRP_ROUTE, "NHRP routing entry")
18
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
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 zapi_route api;
90 struct zapi_nexthop *api_nh;
91
92 if (zclient->sock < 0)
93 return;
94
95 memset(&api, 0, sizeof(api));
96 api.type = ZEBRA_ROUTE_NHRP;
97 api.safi = SAFI_UNICAST;
98 api.vrf_id = VRF_DEFAULT;
99 api.prefix = *p;
100
101 switch (type) {
102 case NHRP_CACHE_NEGATIVE:
103 zapi_route_set_blackhole(&api, BLACKHOLE_REJECT);
104 ifp = NULL;
105 nexthop = NULL;
106 break;
107 case NHRP_CACHE_DYNAMIC:
108 case NHRP_CACHE_NHS:
109 case NHRP_CACHE_STATIC:
110 /* Regular route, so these are announced
111 * to other routing daemons */
112 break;
113 default:
114 SET_FLAG(api.flags, ZEBRA_FLAG_FIB_OVERRIDE);
115 break;
116 }
117 SET_FLAG(api.flags, ZEBRA_FLAG_ALLOW_RECURSION);
118
119 SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
120 api.nexthop_num = 1;
121 api_nh = &api.nexthops[0];
122 api_nh->vrf_id = VRF_DEFAULT;
123
124 switch (api.prefix.family) {
125 case AF_INET:
126 if (nexthop) {
127 api_nh->gate.ipv4 = nexthop->sin.sin_addr;
128 api_nh->type = NEXTHOP_TYPE_IPV4;
129 }
130 if (ifp) {
131 api_nh->ifindex = ifp->ifindex;
132 if (api_nh->type == NEXTHOP_TYPE_IPV4)
133 api_nh->type = NEXTHOP_TYPE_IPV4_IFINDEX;
134 else
135 api_nh->type = NEXTHOP_TYPE_IFINDEX;
136 }
137 break;
138 case AF_INET6:
139 if (nexthop) {
140 api_nh->gate.ipv6 = nexthop->sin6.sin6_addr;
141 api_nh->type = NEXTHOP_TYPE_IPV6;
142 }
143 if (ifp) {
144 api_nh->ifindex = ifp->ifindex;
145 if (api_nh->type == NEXTHOP_TYPE_IPV6)
146 api_nh->type = NEXTHOP_TYPE_IPV6_IFINDEX;
147 else
148 api_nh->type = NEXTHOP_TYPE_IFINDEX;
149 }
150 break;
151 }
152 if (mtu) {
153 SET_FLAG(api.message, ZAPI_MESSAGE_MTU);
154 api.mtu = mtu;
155 }
156
157 if (unlikely(debug_flags & NHRP_DEBUG_ROUTE)) {
158 char buf[2][PREFIX_STRLEN];
159
160 prefix2str(&api.prefix, buf[0], sizeof(buf[0]));
161 zlog_debug("Zebra send: route %s %s nexthop %s metric %u"
162 " count %d dev %s",
163 add ? "add" : "del", buf[0],
164 nexthop ? inet_ntop(api.prefix.family, &api_nh->gate, buf[1], sizeof(buf[1])) : "<onlink>",
165 api.metric, api.nexthop_num, ifp ? ifp->name : "none");
166 }
167
168 zclient_route_send(add ? ZEBRA_ROUTE_ADD : ZEBRA_ROUTE_DELETE, zclient,
169 &api);
170 }
171
172 int nhrp_route_read(int cmd, struct zclient *zclient, zebra_size_t length, vrf_id_t vrf_id)
173 {
174 struct zapi_route api;
175 struct zapi_nexthop *api_nh;
176 struct interface *ifp = NULL;
177 union sockunion nexthop_addr;
178 char buf[2][PREFIX_STRLEN];
179 int added;
180
181 if (zapi_route_decode(zclient->ibuf, &api) < 0)
182 return -1;
183
184 /* we completely ignore srcdest routes for now. */
185 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX))
186 return 0;
187
188 sockunion_family(&nexthop_addr) = AF_UNSPEC;
189 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP)) {
190 api_nh = &api.nexthops[0];
191
192 nexthop_addr.sa.sa_family = api.prefix.family;
193 switch (nexthop_addr.sa.sa_family) {
194 case AF_INET:
195 nexthop_addr.sin.sin_addr = api_nh->gate.ipv4;
196 break;
197 case AF_INET6:
198 nexthop_addr.sin6.sin6_addr = api_nh->gate.ipv6;
199 break;
200 }
201
202 if (api_nh->ifindex != IFINDEX_INTERNAL)
203 ifp = if_lookup_by_index(api_nh->ifindex, VRF_DEFAULT);
204 }
205
206 added = (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD);
207 debugf(NHRP_DEBUG_ROUTE, "if-route-%s: %s via %s dev %s",
208 added ? "add" : "del",
209 prefix2str(&api.prefix, buf[0], sizeof buf[0]),
210 sockunion2str(&nexthop_addr, buf[1], sizeof buf[1]),
211 ifp ? ifp->name : "(none)");
212
213 nhrp_route_update_zebra(&api.prefix, &nexthop_addr, ifp);
214 nhrp_shortcut_prefix_change(&api.prefix, !added);
215
216 return 0;
217 }
218
219 int nhrp_route_get_nexthop(const union sockunion *addr, struct prefix *p, union sockunion *via, struct interface **ifp)
220 {
221 struct route_node *rn;
222 struct route_info *ri;
223 struct prefix lookup;
224 afi_t afi = family2afi(sockunion_family(addr));
225 char buf[PREFIX_STRLEN];
226
227 sockunion2hostprefix(addr, &lookup);
228
229 rn = route_node_match(zebra_rib[afi], &lookup);
230 if (!rn) return 0;
231
232 ri = rn->info;
233 if (ri->nhrp_ifp) {
234 debugf(NHRP_DEBUG_ROUTE, "lookup %s: nhrp_if=%s",
235 prefix2str(&lookup, buf, sizeof buf),
236 ri->nhrp_ifp->name);
237
238 if (via) sockunion_family(via) = AF_UNSPEC;
239 if (ifp) *ifp = ri->nhrp_ifp;
240 } else {
241 debugf(NHRP_DEBUG_ROUTE, "lookup %s: zebra route dev %s",
242 prefix2str(&lookup, buf, sizeof buf),
243 ri->ifp ? ri->ifp->name : "(none)");
244
245 if (via) *via = ri->via;
246 if (ifp) *ifp = ri->ifp;
247 }
248 if (p) *p = rn->p;
249 route_unlock_node(rn);
250 return 1;
251 }
252
253 enum nhrp_route_type nhrp_route_address(struct interface *in_ifp, union sockunion *addr, struct prefix *p, struct nhrp_peer **peer)
254 {
255 struct interface *ifp = in_ifp;
256 struct nhrp_interface *nifp;
257 struct nhrp_cache *c;
258 union sockunion via[4];
259 uint32_t network_id = 0;
260 afi_t afi = family2afi(sockunion_family(addr));
261 int i;
262
263 if (ifp) {
264 nifp = ifp->info;
265 network_id = nifp->afi[afi].network_id;
266
267 c = nhrp_cache_get(ifp, addr, 0);
268 if (c && c->cur.type == NHRP_CACHE_LOCAL) {
269 if (p) memset(p, 0, sizeof(*p));
270 return NHRP_ROUTE_LOCAL;
271 }
272 }
273
274 for (i = 0; i < 4; i++) {
275 if (!nhrp_route_get_nexthop(addr, p, &via[i], &ifp))
276 return NHRP_ROUTE_BLACKHOLE;
277 if (ifp) {
278 /* Departing from nbma network? */
279 nifp = ifp->info;
280 if (network_id && network_id != nifp->afi[afi].network_id)
281 return NHRP_ROUTE_OFF_NBMA;
282 }
283 if (sockunion_family(&via[i]) == AF_UNSPEC)
284 break;
285 /* Resolve via node, but return the prefix of first match */
286 addr = &via[i];
287 p = NULL;
288 }
289
290 if (ifp) {
291 c = nhrp_cache_get(ifp, addr, 0);
292 if (c && c->cur.type >= NHRP_CACHE_DYNAMIC) {
293 if (p) memset(p, 0, sizeof(*p));
294 if (c->cur.type == NHRP_CACHE_LOCAL)
295 return NHRP_ROUTE_LOCAL;
296 if (peer) *peer = nhrp_peer_ref(c->cur.peer);
297 return NHRP_ROUTE_NBMA_NEXTHOP;
298 }
299 }
300
301 return NHRP_ROUTE_BLACKHOLE;
302 }
303
304 static void
305 nhrp_zebra_connected (struct zclient *zclient)
306 {
307 zclient_send_reg_requests(zclient, VRF_DEFAULT);
308 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP,
309 ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT);
310 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP6,
311 ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT);
312 }
313
314 void nhrp_zebra_init(void)
315 {
316 zebra_rib[AFI_IP] = route_table_init();
317 zebra_rib[AFI_IP6] = route_table_init();
318
319 zclient = zclient_new_notify(master, &zclient_options_default);
320 zclient->zebra_connected = nhrp_zebra_connected;
321 zclient->interface_add = nhrp_interface_add;
322 zclient->interface_delete = nhrp_interface_delete;
323 zclient->interface_up = nhrp_interface_up;
324 zclient->interface_down = nhrp_interface_down;
325 zclient->interface_address_add = nhrp_interface_address_add;
326 zclient->interface_address_delete = nhrp_interface_address_delete;
327 zclient->redistribute_route_add = nhrp_route_read;
328 zclient->redistribute_route_del = nhrp_route_read;
329
330 zclient_init(zclient, ZEBRA_ROUTE_NHRP, 0, &nhrpd_privs);
331 }
332
333 void nhrp_zebra_terminate(void)
334 {
335 zclient_stop(zclient);
336 zclient_free(zclient);
337 route_table_finish(zebra_rib[AFI_IP]);
338 route_table_finish(zebra_rib[AFI_IP6]);
339 }