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
, int create
)
30 struct route_node
*rn
;
31 afi_t afi
= family2afi(PREFIX_FAMILY(p
));
37 rn
= route_node_get(zebra_rib
[afi
], p
);
39 rn
->info
= XCALLOC(MTYPE_NHRP_ROUTE
, sizeof(struct route_info
));
44 return route_node_lookup(zebra_rib
[afi
], p
);
48 static void nhrp_route_update_put(struct route_node
*rn
)
50 struct route_info
*ri
= rn
->info
;
52 if (!ri
->ifp
&& !ri
->nhrp_ifp
&& sockunion_family(&ri
->via
) == AF_UNSPEC
) {
53 XFREE(MTYPE_NHRP_ROUTE
, rn
->info
);
55 route_unlock_node(rn
);
57 route_unlock_node(rn
);
60 static void nhrp_route_update_zebra(const struct prefix
*p
, union sockunion
*nexthop
, struct interface
*ifp
)
62 struct route_node
*rn
;
63 struct route_info
*ri
;
65 rn
= nhrp_route_update_get(p
, (sockunion_family(nexthop
) != AF_UNSPEC
) || ifp
);
70 nhrp_route_update_put(rn
);
74 void nhrp_route_update_nhrp(const struct prefix
*p
, struct interface
*ifp
)
76 struct route_node
*rn
;
77 struct route_info
*ri
;
79 rn
= nhrp_route_update_get(p
, ifp
!= NULL
);
83 nhrp_route_update_put(rn
);
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
)
89 struct zapi_route api
;
90 struct zapi_nexthop
*api_nh
;
92 if (zclient
->sock
< 0)
95 memset(&api
, 0, sizeof(api
));
96 api
.type
= ZEBRA_ROUTE_NHRP
;
97 api
.safi
= SAFI_UNICAST
;
101 case NHRP_CACHE_NEGATIVE
:
102 zapi_route_set_blackhole(&api
, BLACKHOLE_REJECT
);
106 case NHRP_CACHE_DYNAMIC
:
108 case NHRP_CACHE_STATIC
:
109 /* Regular route, so these are announced
110 * to other routing daemons */
113 SET_FLAG(api
.flags
, ZEBRA_FLAG_FIB_OVERRIDE
);
116 SET_FLAG(api
.flags
, ZEBRA_FLAG_INTERNAL
);
118 SET_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
);
120 api_nh
= &api
.nexthops
[0];
122 switch (api
.prefix
.family
) {
125 api_nh
->gate
.ipv4
= nexthop
->sin
.sin_addr
;
126 api_nh
->type
= NEXTHOP_TYPE_IPV4
;
129 api_nh
->ifindex
= ifp
->ifindex
;
130 if (api_nh
->type
== NEXTHOP_TYPE_IPV4
)
131 api_nh
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
133 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
138 api_nh
->gate
.ipv6
= nexthop
->sin6
.sin6_addr
;
139 api_nh
->type
= NEXTHOP_TYPE_IPV6
;
142 api_nh
->ifindex
= ifp
->ifindex
;
143 if (api_nh
->type
== NEXTHOP_TYPE_IPV6
)
144 api_nh
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
146 api_nh
->type
= NEXTHOP_TYPE_IFINDEX
;
151 SET_FLAG(api
.message
, ZAPI_MESSAGE_MTU
);
155 if (unlikely(debug_flags
& NHRP_DEBUG_ROUTE
)) {
156 char buf
[2][PREFIX_STRLEN
];
158 prefix2str(&api
.prefix
, buf
[0], sizeof(buf
[0]));
159 zlog_debug("Zebra send: route %s %s nexthop %s metric %u"
161 add
? "add" : "del", buf
[0],
162 nexthop
? inet_ntop(api
.prefix
.family
, &api_nh
->gate
, buf
[1], sizeof(buf
[1])) : "<onlink>",
163 api
.metric
, api
.nexthop_num
, ifp
? ifp
->name
: "none");
166 zclient_route_send(add
? ZEBRA_ROUTE_ADD
: ZEBRA_ROUTE_DELETE
, zclient
,
170 int nhrp_route_read(int cmd
, struct zclient
*zclient
, zebra_size_t length
, vrf_id_t vrf_id
)
172 struct zapi_route api
;
173 struct zapi_nexthop
*api_nh
;
174 struct interface
*ifp
= NULL
;
175 union sockunion nexthop_addr
;
176 char buf
[2][PREFIX_STRLEN
];
179 if (zapi_route_decode(zclient
->ibuf
, &api
) < 0)
182 /* we completely ignore srcdest routes for now. */
183 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_SRCPFX
))
186 sockunion_family(&nexthop_addr
) = AF_UNSPEC
;
187 if (CHECK_FLAG(api
.message
, ZAPI_MESSAGE_NEXTHOP
)) {
188 api_nh
= &api
.nexthops
[0];
190 nexthop_addr
.sa
.sa_family
= api
.prefix
.family
;
191 switch (nexthop_addr
.sa
.sa_family
) {
193 nexthop_addr
.sin
.sin_addr
= api_nh
->gate
.ipv4
;
196 nexthop_addr
.sin6
.sin6_addr
= api_nh
->gate
.ipv6
;
200 if (api_nh
->ifindex
!= IFINDEX_INTERNAL
)
201 ifp
= if_lookup_by_index(api_nh
->ifindex
, VRF_DEFAULT
);
204 added
= (cmd
== ZEBRA_REDISTRIBUTE_ROUTE_ADD
);
205 debugf(NHRP_DEBUG_ROUTE
, "if-route-%s: %s via %s dev %s",
206 added
? "add" : "del",
207 prefix2str(&api
.prefix
, buf
[0], sizeof buf
[0]),
208 sockunion2str(&nexthop_addr
, buf
[1], sizeof buf
[1]),
209 ifp
? ifp
->name
: "(none)");
211 nhrp_route_update_zebra(&api
.prefix
, &nexthop_addr
, ifp
);
212 nhrp_shortcut_prefix_change(&api
.prefix
, !added
);
217 int nhrp_route_get_nexthop(const union sockunion
*addr
, struct prefix
*p
, union sockunion
*via
, struct interface
**ifp
)
219 struct route_node
*rn
;
220 struct route_info
*ri
;
221 struct prefix lookup
;
222 afi_t afi
= family2afi(sockunion_family(addr
));
223 char buf
[PREFIX_STRLEN
];
225 sockunion2hostprefix(addr
, &lookup
);
227 rn
= route_node_match(zebra_rib
[afi
], &lookup
);
232 debugf(NHRP_DEBUG_ROUTE
, "lookup %s: nhrp_if=%s",
233 prefix2str(&lookup
, buf
, sizeof buf
),
236 if (via
) sockunion_family(via
) = AF_UNSPEC
;
237 if (ifp
) *ifp
= ri
->nhrp_ifp
;
239 debugf(NHRP_DEBUG_ROUTE
, "lookup %s: zebra route dev %s",
240 prefix2str(&lookup
, buf
, sizeof buf
),
241 ri
->ifp
? ri
->ifp
->name
: "(none)");
243 if (via
) *via
= ri
->via
;
244 if (ifp
) *ifp
= ri
->ifp
;
247 route_unlock_node(rn
);
251 enum nhrp_route_type
nhrp_route_address(struct interface
*in_ifp
, union sockunion
*addr
, struct prefix
*p
, struct nhrp_peer
**peer
)
253 struct interface
*ifp
= in_ifp
;
254 struct nhrp_interface
*nifp
;
255 struct nhrp_cache
*c
;
256 union sockunion via
[4];
257 uint32_t network_id
= 0;
258 afi_t afi
= family2afi(sockunion_family(addr
));
263 network_id
= nifp
->afi
[afi
].network_id
;
265 c
= nhrp_cache_get(ifp
, addr
, 0);
266 if (c
&& c
->cur
.type
== NHRP_CACHE_LOCAL
) {
267 if (p
) memset(p
, 0, sizeof(*p
));
268 return NHRP_ROUTE_LOCAL
;
272 for (i
= 0; i
< 4; i
++) {
273 if (!nhrp_route_get_nexthop(addr
, p
, &via
[i
], &ifp
))
274 return NHRP_ROUTE_BLACKHOLE
;
276 /* Departing from nbma network? */
278 if (network_id
&& network_id
!= nifp
->afi
[afi
].network_id
)
279 return NHRP_ROUTE_OFF_NBMA
;
281 if (sockunion_family(&via
[i
]) == AF_UNSPEC
)
283 /* Resolve via node, but return the prefix of first match */
289 c
= nhrp_cache_get(ifp
, addr
, 0);
290 if (c
&& c
->cur
.type
>= NHRP_CACHE_DYNAMIC
) {
291 if (p
) memset(p
, 0, sizeof(*p
));
292 if (c
->cur
.type
== NHRP_CACHE_LOCAL
)
293 return NHRP_ROUTE_LOCAL
;
294 if (peer
) *peer
= nhrp_peer_ref(c
->cur
.peer
);
295 return NHRP_ROUTE_NBMA_NEXTHOP
;
299 return NHRP_ROUTE_BLACKHOLE
;
303 nhrp_zebra_connected (struct zclient
*zclient
)
305 zclient_send_reg_requests(zclient
, VRF_DEFAULT
);
306 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP
,
307 ZEBRA_ROUTE_ALL
, 0, VRF_DEFAULT
);
308 zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD
, zclient
, AFI_IP6
,
309 ZEBRA_ROUTE_ALL
, 0, VRF_DEFAULT
);
312 void nhrp_zebra_init(void)
314 zebra_rib
[AFI_IP
] = route_table_init();
315 zebra_rib
[AFI_IP6
] = route_table_init();
317 zclient
= zclient_new_notify(master
, &zclient_options_default
);
318 zclient
->zebra_connected
= nhrp_zebra_connected
;
319 zclient
->interface_add
= nhrp_interface_add
;
320 zclient
->interface_delete
= nhrp_interface_delete
;
321 zclient
->interface_up
= nhrp_interface_up
;
322 zclient
->interface_down
= nhrp_interface_down
;
323 zclient
->interface_address_add
= nhrp_interface_address_add
;
324 zclient
->interface_address_delete
= nhrp_interface_address_delete
;
325 zclient
->redistribute_route_add
= nhrp_route_read
;
326 zclient
->redistribute_route_del
= nhrp_route_read
;
328 zclient_init(zclient
, ZEBRA_ROUTE_NHRP
, 0, &nhrpd_privs
);
331 void nhrp_zebra_terminate(void)
333 zclient_stop(zclient
);
334 zclient_free(zclient
);
335 route_table_finish(zebra_rib
[AFI_IP
]);
336 route_table_finish(zebra_rib
[AFI_IP6
]);