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.
21 DEFINE_MTYPE_STATIC(NHRPD
, NHRP_ROUTE
, "NHRP routing entry")
23 static struct zclient
*zclient
;
24 static struct route_table
*zebra_rib
[AFI_MAX
];
28 struct interface
*ifp
;
29 struct interface
*nhrp_ifp
;
32 static struct route_node
*nhrp_route_update_get(const struct prefix
*p
,
35 struct route_node
*rn
;
36 afi_t afi
= family2afi(PREFIX_FAMILY(p
));
42 rn
= route_node_get(zebra_rib
[afi
], p
);
44 rn
->info
= XCALLOC(MTYPE_NHRP_ROUTE
,
45 sizeof(struct route_info
));
50 return route_node_lookup(zebra_rib
[afi
], p
);
54 static void nhrp_route_update_put(struct route_node
*rn
)
56 struct route_info
*ri
= rn
->info
;
58 if (!ri
->ifp
&& !ri
->nhrp_ifp
59 && sockunion_family(&ri
->via
) == AF_UNSPEC
) {
60 XFREE(MTYPE_NHRP_ROUTE
, rn
->info
);
62 route_unlock_node(rn
);
64 route_unlock_node(rn
);
67 static void nhrp_route_update_zebra(const struct prefix
*p
,
68 union sockunion
*nexthop
,
69 struct interface
*ifp
)
71 struct route_node
*rn
;
72 struct route_info
*ri
;
74 rn
= nhrp_route_update_get(
75 p
, (sockunion_family(nexthop
) != AF_UNSPEC
) || ifp
);
80 nhrp_route_update_put(rn
);
84 void nhrp_route_update_nhrp(const struct prefix
*p
, struct interface
*ifp
)
86 struct route_node
*rn
;
87 struct route_info
*ri
;
89 rn
= nhrp_route_update_get(p
, ifp
!= NULL
);
93 nhrp_route_update_put(rn
);
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
)
101 struct zapi_route api
;
102 struct zapi_nexthop
*api_nh
;
104 if (zclient
->sock
< 0)
107 memset(&api
, 0, sizeof(api
));
108 api
.type
= ZEBRA_ROUTE_NHRP
;
109 api
.safi
= SAFI_UNICAST
;
110 api
.vrf_id
= VRF_DEFAULT
;
114 case NHRP_CACHE_NEGATIVE
:
115 zapi_route_set_blackhole(&api
, BLACKHOLE_REJECT
);
119 case NHRP_CACHE_DYNAMIC
:
121 case NHRP_CACHE_STATIC
:
122 /* Regular route, so these are announced
123 * to other routing daemons */
126 SET_FLAG(api
.flags
, ZEBRA_FLAG_FIB_OVERRIDE
);
129 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
131 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
133 api_nh
= &api
.nexthops
[0];
134 api_nh
->vrf_id
= VRF_DEFAULT
;
136 switch (api
.prefix
.family
) {
139 api_nh
->gate
.ipv4
= nexthop
->sin
.sin_addr
;
140 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
143 api_nh
->ifindex
= ifp
->ifindex
;
144 if (api_nh
->type
== NEXTHOP_TYPE_IPV4
)
145 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
147 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
152 api_nh
->gate
.ipv6
= nexthop
->sin6
.sin6_addr
;
153 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
156 api_nh
->ifindex
= ifp
->ifindex
;
157 if (api_nh
->type
== NEXTHOP_TYPE_IPV6
)
158 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
160 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
165 SET_FLAG(api
.message
, ZAPI_MESSAGE_MTU
);
169 if (unlikely(debug_flags
& NHRP_DEBUG_ROUTE
)) {
170 char buf
[2][PREFIX_STRLEN
];
172 prefix2str(&api
.prefix
, buf
[0], sizeof(buf
[0]));
174 "Zebra send: route %s %s nexthop %s metric %u"
176 add
? "add" : "del", buf
[0],
177 nexthop
? inet_ntop(api
.prefix
.family
, &api_nh
->gate
,
178 buf
[1], sizeof(buf
[1]))
180 api
.metric
, api
.nexthop_num
, ifp
? ifp
->name
: "none");
183 zclient_route_send(add
? ZEBRA_ROUTE_ADD
: ZEBRA_ROUTE_DELETE
, zclient
,
187 int nhrp_route_read(int cmd
, struct zclient
*zclient
, zebra_size_t length
,
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
];
197 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
200 /* we completely ignore srcdest routes for now. */
201 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
204 sockunion_family(&nexthop_addr
) = AF_UNSPEC
;
205 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)) {
206 api_nh
= &api
.nexthops
[0];
208 nexthop_addr
.sa
.sa_family
= api
.prefix
.family
;
209 switch (nexthop_addr
.sa
.sa_family
) {
211 nexthop_addr
.sin
.sin_addr
= api_nh
->gate
.ipv4
;
214 nexthop_addr
.sin6
.sin6_addr
= api_nh
->gate
.ipv6
;
218 if (api_nh
->ifindex
!= IFINDEX_INTERNAL
)
219 ifp
= if_lookup_by_index(api_nh
->ifindex
, VRF_DEFAULT
);
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)");
229 nhrp_route_update_zebra(&api
.prefix
, &nexthop_addr
, ifp
);
230 nhrp_shortcut_prefix_change(&api
.prefix
, !added
);
235 int nhrp_route_get_nexthop(const union sockunion
*addr
, struct prefix
*p
,
236 union sockunion
*via
, struct interface
**ifp
)
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
];
244 sockunion2hostprefix(addr
, &lookup
);
246 rn
= route_node_match(zebra_rib
[afi
], &lookup
);
252 debugf(NHRP_DEBUG_ROUTE
, "lookup %s: nhrp_if=%s",
253 prefix2str(&lookup
, buf
, sizeof buf
),
257 sockunion_family(via
) = AF_UNSPEC
;
261 debugf(NHRP_DEBUG_ROUTE
, "lookup %s: zebra route dev %s",
262 prefix2str(&lookup
, buf
, sizeof buf
),
263 ri
->ifp
? ri
->ifp
->name
: "(none)");
272 route_unlock_node(rn
);
276 enum nhrp_route_type
nhrp_route_address(struct interface
*in_ifp
,
277 union sockunion
*addr
, struct prefix
*p
,
278 struct nhrp_peer
**peer
)
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
));
290 network_id
= nifp
->afi
[afi
].network_id
;
292 c
= nhrp_cache_get(ifp
, addr
, 0);
293 if (c
&& c
->cur
.type
== NHRP_CACHE_LOCAL
) {
295 memset(p
, 0, sizeof(*p
));
296 return NHRP_ROUTE_LOCAL
;
300 for (i
= 0; i
< 4; i
++) {
301 if (!nhrp_route_get_nexthop(addr
, p
, &via
[i
], &ifp
))
302 return NHRP_ROUTE_BLACKHOLE
;
304 /* Departing from nbma network? */
307 && network_id
!= nifp
->afi
[afi
].network_id
)
308 return NHRP_ROUTE_OFF_NBMA
;
310 if (sockunion_family(&via
[i
]) == AF_UNSPEC
)
312 /* Resolve via node, but return the prefix of first match */
318 c
= nhrp_cache_get(ifp
, addr
, 0);
319 if (c
&& c
->cur
.type
>= NHRP_CACHE_DYNAMIC
) {
321 memset(p
, 0, sizeof(*p
));
322 if (c
->cur
.type
== NHRP_CACHE_LOCAL
)
323 return NHRP_ROUTE_LOCAL
;
325 *peer
= nhrp_peer_ref(c
->cur
.peer
);
326 return NHRP_ROUTE_NBMA_NEXTHOP
;
330 return NHRP_ROUTE_BLACKHOLE
;
333 static void nhrp_zebra_connected(struct zclient
*zclient
)
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
);
342 void nhrp_zebra_init(void)
344 zebra_rib
[AFI_IP
] = route_table_init();
345 zebra_rib
[AFI_IP6
] = route_table_init();
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
;
358 zclient_init(zclient
, ZEBRA_ROUTE_NHRP
, 0, &nhrpd_privs
);
361 void nhrp_zebra_terminate(void)
363 zclient_stop(zclient
);
364 zclient_free(zclient
);
365 route_table_finish(zebra_rib
[AFI_IP
]);
366 route_table_finish(zebra_rib
[AFI_IP6
]);