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_is_null(&ri
->via
)) {
60 XFREE(MTYPE_NHRP_ROUTE
, rn
->info
);
61 route_unlock_node(rn
);
63 route_unlock_node(rn
);
66 static void nhrp_route_update_zebra(const struct prefix
*p
,
67 union sockunion
*nexthop
,
68 struct interface
*ifp
)
70 struct route_node
*rn
;
71 struct route_info
*ri
;
73 rn
= nhrp_route_update_get(p
, !sockunion_is_null(nexthop
) || ifp
);
78 nhrp_route_update_put(rn
);
82 void nhrp_route_update_nhrp(const struct prefix
*p
, struct interface
*ifp
)
84 struct route_node
*rn
;
85 struct route_info
*ri
;
87 rn
= nhrp_route_update_get(p
, ifp
!= NULL
);
91 nhrp_route_update_put(rn
);
95 void nhrp_route_announce(int add
, enum nhrp_cache_type type
,
96 const struct prefix
*p
, struct interface
*ifp
,
97 const union sockunion
*nexthop
, uint32_t mtu
)
99 struct zapi_route api
;
100 struct zapi_nexthop
*api_nh
;
102 if (zclient
->sock
< 0)
105 memset(&api
, 0, sizeof(api
));
106 api
.type
= ZEBRA_ROUTE_NHRP
;
107 api
.safi
= SAFI_UNICAST
;
108 api
.vrf_id
= VRF_DEFAULT
;
112 case NHRP_CACHE_NEGATIVE
:
113 zapi_route_set_blackhole(&api
, BLACKHOLE_REJECT
);
117 case NHRP_CACHE_DYNAMIC
:
119 case NHRP_CACHE_STATIC
:
120 /* Regular route, so these are announced
121 * to other routing daemons */
124 SET_FLAG(api
.flags
, ZEBRA_FLAG_FIB_OVERRIDE
);
127 SET_FLAG(api
.flags
, ZEBRA_FLAG_ALLOW_RECURSION
);
129 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
131 api_nh
= &api
.nexthops
[0];
132 api_nh
->vrf_id
= VRF_DEFAULT
;
134 switch (api
.prefix
.family
) {
137 api_nh
->gate
.ipv4
= nexthop
->sin
.sin_addr
;
138 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
141 api_nh
->ifindex
= ifp
->ifindex
;
142 if (api_nh
->type
== NEXTHOP_TYPE_IPV4
)
143 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
145 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
150 api_nh
->gate
.ipv6
= nexthop
->sin6
.sin6_addr
;
151 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
154 api_nh
->ifindex
= ifp
->ifindex
;
155 if (api_nh
->type
== NEXTHOP_TYPE_IPV6
)
156 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
158 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
163 SET_FLAG(api
.message
, ZAPI_MESSAGE_MTU
);
167 if (unlikely(debug_flags
& NHRP_DEBUG_ROUTE
)) {
168 char buf
[PREFIX_STRLEN
];
171 "Zebra send: route %s %pFX nexthop %s metric %u count %d dev %s",
172 add
? "add" : "del", &api
.prefix
,
173 nexthop
? inet_ntop(api
.prefix
.family
, &api_nh
->gate
,
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(ZAPI_CALLBACK_ARGS
)
185 struct zapi_route api
;
186 struct zapi_nexthop
*api_nh
;
187 struct interface
*ifp
= NULL
;
188 union sockunion nexthop_addr
;
189 char buf
[PREFIX_STRLEN
];
192 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
195 /* we completely ignore srcdest routes for now. */
196 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
199 /* ignore our routes */
200 if (api
.type
== ZEBRA_ROUTE_NHRP
)
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: %pFX via %s dev %s",
223 added
? "add" : "del", &api
.prefix
,
224 sockunion2str(&nexthop_addr
, buf
, sizeof(buf
)),
225 ifp
? ifp
->name
: "(none)");
227 nhrp_route_update_zebra(&api
.prefix
, &nexthop_addr
, added
? ifp
: NULL
);
228 nhrp_shortcut_prefix_change(&api
.prefix
, !added
);
233 int nhrp_route_get_nexthop(const union sockunion
*addr
, struct prefix
*p
,
234 union sockunion
*via
, struct interface
**ifp
)
236 struct route_node
*rn
;
237 struct route_info
*ri
;
238 struct prefix lookup
;
239 afi_t afi
= family2afi(sockunion_family(addr
));
241 sockunion2hostprefix(addr
, &lookup
);
243 rn
= route_node_match(zebra_rib
[afi
], &lookup
);
249 debugf(NHRP_DEBUG_ROUTE
, "lookup %pFX: nhrp_if=%s", &lookup
,
253 sockunion_family(via
) = AF_UNSPEC
;
257 debugf(NHRP_DEBUG_ROUTE
, "lookup %pFX: zebra route dev %s",
258 &lookup
, ri
->ifp
? ri
->ifp
->name
: "(none)");
267 route_unlock_node(rn
);
271 enum nhrp_route_type
nhrp_route_address(struct interface
*in_ifp
,
272 union sockunion
*addr
, struct prefix
*p
,
273 struct nhrp_peer
**peer
)
275 struct interface
*ifp
= in_ifp
;
276 struct nhrp_interface
*nifp
;
277 struct nhrp_cache
*c
;
278 union sockunion via
[4];
279 uint32_t network_id
= 0;
280 afi_t afi
= family2afi(sockunion_family(addr
));
285 network_id
= nifp
->afi
[afi
].network_id
;
287 c
= nhrp_cache_get(ifp
, addr
, 0);
288 if (c
&& c
->cur
.type
== NHRP_CACHE_LOCAL
) {
290 memset(p
, 0, sizeof(*p
));
291 return NHRP_ROUTE_LOCAL
;
295 for (i
= 0; i
< 4; i
++) {
296 if (!nhrp_route_get_nexthop(addr
, p
, &via
[i
], &ifp
))
297 return NHRP_ROUTE_BLACKHOLE
;
299 /* Departing from nbma network? */
302 && network_id
!= nifp
->afi
[afi
].network_id
)
303 return NHRP_ROUTE_OFF_NBMA
;
305 if (sockunion_family(&via
[i
]) == AF_UNSPEC
)
307 /* Resolve via node, but return the prefix of first match */
313 c
= nhrp_cache_get(ifp
, addr
, 0);
314 if (c
&& c
->cur
.type
>= NHRP_CACHE_DYNAMIC
) {
316 memset(p
, 0, sizeof(*p
));
317 if (c
->cur
.type
== NHRP_CACHE_LOCAL
)
318 return NHRP_ROUTE_LOCAL
;
320 *peer
= nhrp_peer_ref(c
->cur
.peer
);
321 return NHRP_ROUTE_NBMA_NEXTHOP
;
325 return NHRP_ROUTE_BLACKHOLE
;
328 static void nhrp_zebra_connected(struct zclient
*zclient
)
330 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
331 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP
,
332 ZEBRA_ROUTE_ALL
, 0, VRF_DEFAULT
);
333 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP6
,
334 ZEBRA_ROUTE_ALL
, 0, VRF_DEFAULT
);
337 void nhrp_zebra_init(void)
339 zebra_rib
[AFI_IP
] = route_table_init();
340 zebra_rib
[AFI_IP6
] = route_table_init();
342 zclient
= zclient_new(master
, &zclient_options_default
);
343 zclient
->zebra_connected
= nhrp_zebra_connected
;
344 zclient
->interface_address_add
= nhrp_interface_address_add
;
345 zclient
->interface_address_delete
= nhrp_interface_address_delete
;
346 zclient
->redistribute_route_add
= nhrp_route_read
;
347 zclient
->redistribute_route_del
= nhrp_route_read
;
349 zclient_init(zclient
, ZEBRA_ROUTE_NHRP
, 0, &nhrpd_privs
);
352 static void nhrp_table_node_cleanup(struct route_table
*table
,
353 struct route_node
*node
)
358 XFREE(MTYPE_NHRP_ROUTE
, node
->info
);
361 void nhrp_zebra_terminate(void)
363 zclient_stop(zclient
);
364 zclient_free(zclient
);
366 zebra_rib
[AFI_IP
]->cleanup
= nhrp_table_node_cleanup
;
367 zebra_rib
[AFI_IP6
]->cleanup
= nhrp_table_node_cleanup
;
368 route_table_finish(zebra_rib
[AFI_IP
]);
369 route_table_finish(zebra_rib
[AFI_IP6
]);