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(ZAPI_CALLBACK_ARGS
)
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
];
196 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
199 /* we completely ignore srcdest routes for now. */
200 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
203 sockunion_family(&nexthop_addr
) = AF_UNSPEC
;
204 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)) {
205 api_nh
= &api
.nexthops
[0];
207 nexthop_addr
.sa
.sa_family
= api
.prefix
.family
;
208 switch (nexthop_addr
.sa
.sa_family
) {
210 nexthop_addr
.sin
.sin_addr
= api_nh
->gate
.ipv4
;
213 nexthop_addr
.sin6
.sin6_addr
= api_nh
->gate
.ipv6
;
217 if (api_nh
->ifindex
!= IFINDEX_INTERNAL
)
218 ifp
= if_lookup_by_index(api_nh
->ifindex
, VRF_DEFAULT
);
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)");
228 nhrp_route_update_zebra(&api
.prefix
, &nexthop_addr
, ifp
);
229 nhrp_shortcut_prefix_change(&api
.prefix
, !added
);
234 int nhrp_route_get_nexthop(const union sockunion
*addr
, struct prefix
*p
,
235 union sockunion
*via
, struct interface
**ifp
)
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
];
243 sockunion2hostprefix(addr
, &lookup
);
245 rn
= route_node_match(zebra_rib
[afi
], &lookup
);
251 debugf(NHRP_DEBUG_ROUTE
, "lookup %s: nhrp_if=%s",
252 prefix2str(&lookup
, buf
, sizeof buf
),
256 sockunion_family(via
) = AF_UNSPEC
;
260 debugf(NHRP_DEBUG_ROUTE
, "lookup %s: zebra route dev %s",
261 prefix2str(&lookup
, buf
, sizeof buf
),
262 ri
->ifp
? ri
->ifp
->name
: "(none)");
271 route_unlock_node(rn
);
275 enum nhrp_route_type
nhrp_route_address(struct interface
*in_ifp
,
276 union sockunion
*addr
, struct prefix
*p
,
277 struct nhrp_peer
**peer
)
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
));
289 network_id
= nifp
->afi
[afi
].network_id
;
291 c
= nhrp_cache_get(ifp
, addr
, 0);
292 if (c
&& c
->cur
.type
== NHRP_CACHE_LOCAL
) {
294 memset(p
, 0, sizeof(*p
));
295 return NHRP_ROUTE_LOCAL
;
299 for (i
= 0; i
< 4; i
++) {
300 if (!nhrp_route_get_nexthop(addr
, p
, &via
[i
], &ifp
))
301 return NHRP_ROUTE_BLACKHOLE
;
303 /* Departing from nbma network? */
306 && network_id
!= nifp
->afi
[afi
].network_id
)
307 return NHRP_ROUTE_OFF_NBMA
;
309 if (sockunion_family(&via
[i
]) == AF_UNSPEC
)
311 /* Resolve via node, but return the prefix of first match */
317 c
= nhrp_cache_get(ifp
, addr
, 0);
318 if (c
&& c
->cur
.type
>= NHRP_CACHE_DYNAMIC
) {
320 memset(p
, 0, sizeof(*p
));
321 if (c
->cur
.type
== NHRP_CACHE_LOCAL
)
322 return NHRP_ROUTE_LOCAL
;
324 *peer
= nhrp_peer_ref(c
->cur
.peer
);
325 return NHRP_ROUTE_NBMA_NEXTHOP
;
329 return NHRP_ROUTE_BLACKHOLE
;
332 static void nhrp_zebra_connected(struct zclient
*zclient
)
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
);
341 void nhrp_zebra_init(void)
343 zebra_rib
[AFI_IP
] = route_table_init();
344 zebra_rib
[AFI_IP6
] = route_table_init();
346 zclient
= zclient_new(master
, &zclient_options_default
);
347 zclient
->zebra_connected
= nhrp_zebra_connected
;
348 zclient
->interface_address_add
= nhrp_interface_address_add
;
349 zclient
->interface_address_delete
= nhrp_interface_address_delete
;
350 zclient
->redistribute_route_add
= nhrp_route_read
;
351 zclient
->redistribute_route_del
= nhrp_route_read
;
353 zclient_init(zclient
, ZEBRA_ROUTE_NHRP
, 0, &nhrpd_privs
);
356 void nhrp_zebra_terminate(void)
358 zclient_stop(zclient
);
359 zclient_free(zclient
);
360 route_table_finish(zebra_rib
[AFI_IP
]);
361 route_table_finish(zebra_rib
[AFI_IP6
]);