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 DEFINE_MTYPE_STATIC(NHRPD
, NHRP_ROUTE
, "NHRP routing entry")
19 static struct zclient
*zclient
;
20 static struct route_table
*zebra_rib
[AFI_MAX
];
24 struct interface
*ifp
;
25 struct interface
*nhrp_ifp
;
28 static struct route_node
*nhrp_route_update_get(const struct prefix
*p
,
31 struct route_node
*rn
;
32 afi_t afi
= family2afi(PREFIX_FAMILY(p
));
38 rn
= route_node_get(zebra_rib
[afi
], p
);
40 rn
->info
= XCALLOC(MTYPE_NHRP_ROUTE
,
41 sizeof(struct route_info
));
46 return route_node_lookup(zebra_rib
[afi
], p
);
50 static void nhrp_route_update_put(struct route_node
*rn
)
52 struct route_info
*ri
= rn
->info
;
54 if (!ri
->ifp
&& !ri
->nhrp_ifp
55 && sockunion_family(&ri
->via
) == AF_UNSPEC
) {
56 XFREE(MTYPE_NHRP_ROUTE
, rn
->info
);
58 route_unlock_node(rn
);
60 route_unlock_node(rn
);
63 static void nhrp_route_update_zebra(const struct prefix
*p
,
64 union sockunion
*nexthop
,
65 struct interface
*ifp
)
67 struct route_node
*rn
;
68 struct route_info
*ri
;
70 rn
= nhrp_route_update_get(
71 p
, (sockunion_family(nexthop
) != AF_UNSPEC
) || ifp
);
76 nhrp_route_update_put(rn
);
80 void nhrp_route_update_nhrp(const struct prefix
*p
, struct interface
*ifp
)
82 struct route_node
*rn
;
83 struct route_info
*ri
;
85 rn
= nhrp_route_update_get(p
, ifp
!= NULL
);
89 nhrp_route_update_put(rn
);
93 void nhrp_route_announce(int add
, enum nhrp_cache_type type
,
94 const struct prefix
*p
, struct interface
*ifp
,
95 const union sockunion
*nexthop
, uint32_t mtu
)
97 struct zapi_route api
;
98 struct zapi_nexthop
*api_nh
;
100 if (zclient
->sock
< 0)
103 memset(&api
, 0, sizeof(api
));
104 api
.type
= ZEBRA_ROUTE_NHRP
;
105 api
.safi
= SAFI_UNICAST
;
106 api
.vrf_id
= VRF_DEFAULT
;
110 case NHRP_CACHE_NEGATIVE
:
111 zapi_route_set_blackhole(&api
, BLACKHOLE_REJECT
);
115 case NHRP_CACHE_DYNAMIC
:
117 case NHRP_CACHE_STATIC
:
118 /* Regular route, so these are announced
119 * to other routing daemons */
122 SET_FLAG(api
.flags
, ZEBRA_FLAG_FIB_OVERRIDE
);
125 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
127 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
129 api_nh
= &api
.nexthops
[0];
130 api_nh
->vrf_id
= VRF_DEFAULT
;
132 switch (api
.prefix
.family
) {
135 api_nh
->gate
.ipv4
= nexthop
->sin
.sin_addr
;
136 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
139 api_nh
->ifindex
= ifp
->ifindex
;
140 if (api_nh
->type
== NEXTHOP_TYPE_IPV4
)
141 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
143 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
148 api_nh
->gate
.ipv6
= nexthop
->sin6
.sin6_addr
;
149 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
152 api_nh
->ifindex
= ifp
->ifindex
;
153 if (api_nh
->type
== NEXTHOP_TYPE_IPV6
)
154 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
156 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
161 SET_FLAG(api
.message
, ZAPI_MESSAGE_MTU
);
165 if (unlikely(debug_flags
& NHRP_DEBUG_ROUTE
)) {
166 char buf
[2][PREFIX_STRLEN
];
168 prefix2str(&api
.prefix
, buf
[0], sizeof(buf
[0]));
170 "Zebra send: route %s %s nexthop %s metric %u"
172 add
? "add" : "del", buf
[0],
173 nexthop
? inet_ntop(api
.prefix
.family
, &api_nh
->gate
,
174 buf
[1], sizeof(buf
[1]))
176 api
.metric
, api
.nexthop_num
, ifp
? ifp
->name
: "none");
179 zclient_route_send(add
? ZEBRA_ROUTE_ADD
: ZEBRA_ROUTE_DELETE
, zclient
,
183 int nhrp_route_read(int cmd
, struct zclient
*zclient
, zebra_size_t length
,
186 struct zapi_route api
;
187 struct zapi_nexthop
*api_nh
;
188 struct interface
*ifp
= NULL
;
189 union sockunion nexthop_addr
;
190 char buf
[2][PREFIX_STRLEN
];
193 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
196 /* we completely ignore srcdest routes for now. */
197 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
200 sockunion_family(&nexthop_addr
) = AF_UNSPEC
;
201 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)) {
202 api_nh
= &api
.nexthops
[0];
204 nexthop_addr
.sa
.sa_family
= api
.prefix
.family
;
205 switch (nexthop_addr
.sa
.sa_family
) {
207 nexthop_addr
.sin
.sin_addr
= api_nh
->gate
.ipv4
;
210 nexthop_addr
.sin6
.sin6_addr
= api_nh
->gate
.ipv6
;
214 if (api_nh
->ifindex
!= IFINDEX_INTERNAL
)
215 ifp
= if_lookup_by_index(api_nh
->ifindex
, VRF_DEFAULT
);
218 added
= (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
);
219 debugf(NHRP_DEBUG_ROUTE
, "if-route-%s: %s via %s dev %s",
220 added
? "add" : "del",
221 prefix2str(&api
.prefix
, buf
[0], sizeof buf
[0]),
222 sockunion2str(&nexthop_addr
, buf
[1], sizeof buf
[1]),
223 ifp
? ifp
->name
: "(none)");
225 nhrp_route_update_zebra(&api
.prefix
, &nexthop_addr
, ifp
);
226 nhrp_shortcut_prefix_change(&api
.prefix
, !added
);
231 int nhrp_route_get_nexthop(const union sockunion
*addr
, struct prefix
*p
,
232 union sockunion
*via
, struct interface
**ifp
)
234 struct route_node
*rn
;
235 struct route_info
*ri
;
236 struct prefix lookup
;
237 afi_t afi
= family2afi(sockunion_family(addr
));
238 char buf
[PREFIX_STRLEN
];
240 sockunion2hostprefix(addr
, &lookup
);
242 rn
= route_node_match(zebra_rib
[afi
], &lookup
);
248 debugf(NHRP_DEBUG_ROUTE
, "lookup %s: nhrp_if=%s",
249 prefix2str(&lookup
, buf
, sizeof buf
),
253 sockunion_family(via
) = AF_UNSPEC
;
257 debugf(NHRP_DEBUG_ROUTE
, "lookup %s: zebra route dev %s",
258 prefix2str(&lookup
, buf
, sizeof buf
),
259 ri
->ifp
? ri
->ifp
->name
: "(none)");
268 route_unlock_node(rn
);
272 enum nhrp_route_type
nhrp_route_address(struct interface
*in_ifp
,
273 union sockunion
*addr
, struct prefix
*p
,
274 struct nhrp_peer
**peer
)
276 struct interface
*ifp
= in_ifp
;
277 struct nhrp_interface
*nifp
;
278 struct nhrp_cache
*c
;
279 union sockunion via
[4];
280 uint32_t network_id
= 0;
281 afi_t afi
= family2afi(sockunion_family(addr
));
286 network_id
= nifp
->afi
[afi
].network_id
;
288 c
= nhrp_cache_get(ifp
, addr
, 0);
289 if (c
&& c
->cur
.type
== NHRP_CACHE_LOCAL
) {
291 memset(p
, 0, sizeof(*p
));
292 return NHRP_ROUTE_LOCAL
;
296 for (i
= 0; i
< 4; i
++) {
297 if (!nhrp_route_get_nexthop(addr
, p
, &via
[i
], &ifp
))
298 return NHRP_ROUTE_BLACKHOLE
;
300 /* Departing from nbma network? */
303 && network_id
!= nifp
->afi
[afi
].network_id
)
304 return NHRP_ROUTE_OFF_NBMA
;
306 if (sockunion_family(&via
[i
]) == AF_UNSPEC
)
308 /* Resolve via node, but return the prefix of first match */
314 c
= nhrp_cache_get(ifp
, addr
, 0);
315 if (c
&& c
->cur
.type
>= NHRP_CACHE_DYNAMIC
) {
317 memset(p
, 0, sizeof(*p
));
318 if (c
->cur
.type
== NHRP_CACHE_LOCAL
)
319 return NHRP_ROUTE_LOCAL
;
321 *peer
= nhrp_peer_ref(c
->cur
.peer
);
322 return NHRP_ROUTE_NBMA_NEXTHOP
;
326 return NHRP_ROUTE_BLACKHOLE
;
329 static void nhrp_zebra_connected(struct zclient
*zclient
)
331 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
332 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP
,
333 ZEBRA_ROUTE_ALL
, 0, VRF_DEFAULT
);
334 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP6
,
335 ZEBRA_ROUTE_ALL
, 0, VRF_DEFAULT
);
338 void nhrp_zebra_init(void)
340 zebra_rib
[AFI_IP
] = route_table_init();
341 zebra_rib
[AFI_IP6
] = route_table_init();
343 zclient
= zclient_new_notify(master
, &zclient_options_default
);
344 zclient
->zebra_connected
= nhrp_zebra_connected
;
345 zclient
->interface_add
= nhrp_interface_add
;
346 zclient
->interface_delete
= nhrp_interface_delete
;
347 zclient
->interface_up
= nhrp_interface_up
;
348 zclient
->interface_down
= nhrp_interface_down
;
349 zclient
->interface_address_add
= nhrp_interface_address_add
;
350 zclient
->interface_address_delete
= nhrp_interface_address_delete
;
351 zclient
->redistribute_route_add
= nhrp_route_read
;
352 zclient
->redistribute_route_del
= nhrp_route_read
;
354 zclient_init(zclient
, ZEBRA_ROUTE_NHRP
, 0, &nhrpd_privs
);
357 void nhrp_zebra_terminate(void)
359 zclient_stop(zclient
);
360 zclient_free(zclient
);
361 route_table_finish(zebra_rib
[AFI_IP
]);
362 route_table_finish(zebra_rib
[AFI_IP6
]);