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.
14 #include <net/if_arp.h>
24 DEFINE_MTYPE_STATIC(NHRPD
, NHRP_IF
, "NHRP interface")
26 static void nhrp_interface_update_cache_config(struct interface
*ifp
,
30 static int nhrp_if_new_hook(struct interface
*ifp
)
32 struct nhrp_interface
*nifp
;
35 nifp
= XCALLOC(MTYPE_NHRP_IF
, sizeof(struct nhrp_interface
));
40 notifier_init(&nifp
->notifier_list
);
41 for (afi
= 0; afi
< AFI_MAX
; afi
++) {
42 struct nhrp_afi_data
*ad
= &nifp
->afi
[afi
];
43 ad
->holdtime
= NHRPD_DEFAULT_HOLDTIME
;
44 list_init(&ad
->nhslist_head
);
50 static int nhrp_if_delete_hook(struct interface
*ifp
)
52 struct nhrp_interface
*nifp
= ifp
->info
;
54 debugf(NHRP_DEBUG_IF
, "Deleted interface (%s)", ifp
->name
);
56 nhrp_cache_interface_del(ifp
);
57 nhrp_nhs_interface_del(ifp
);
58 nhrp_peer_interface_del(ifp
);
60 if (nifp
->ipsec_profile
)
61 free(nifp
->ipsec_profile
);
62 if (nifp
->ipsec_fallback_profile
)
63 free(nifp
->ipsec_fallback_profile
);
67 XFREE(MTYPE_NHRP_IF
, ifp
->info
);
71 void nhrp_interface_init(void)
73 hook_register_prio(if_add
, 0, nhrp_if_new_hook
);
74 hook_register_prio(if_del
, 0, nhrp_if_delete_hook
);
77 void nhrp_interface_update_mtu(struct interface
*ifp
, afi_t afi
)
79 struct nhrp_interface
*nifp
= ifp
->info
;
80 struct nhrp_afi_data
*if_ad
= &nifp
->afi
[afi
];
81 unsigned short new_mtu
;
83 if (if_ad
->configured_mtu
< 0)
84 new_mtu
= nifp
->nbmaifp
? nifp
->nbmaifp
->mtu
: 0;
86 new_mtu
= if_ad
->configured_mtu
;
90 if (new_mtu
!= if_ad
->mtu
) {
91 debugf(NHRP_DEBUG_IF
, "%s: MTU changed to %d", ifp
->name
,
94 notifier_call(&nifp
->notifier_list
,
95 NOTIFY_INTERFACE_MTU_CHANGED
);
99 static void nhrp_interface_update_source(struct interface
*ifp
)
101 struct nhrp_interface
*nifp
= ifp
->info
;
103 if (!nifp
->source
|| !nifp
->nbmaifp
104 || (ifindex_t
)nifp
->linkidx
== nifp
->nbmaifp
->ifindex
)
107 nifp
->linkidx
= nifp
->nbmaifp
->ifindex
;
108 debugf(NHRP_DEBUG_IF
, "%s: bound device index changed to %d", ifp
->name
,
110 netlink_gre_set_link(ifp
->ifindex
, nifp
->linkidx
);
113 static void nhrp_interface_interface_notifier(struct notifier_block
*n
,
116 struct nhrp_interface
*nifp
=
117 container_of(n
, struct nhrp_interface
, nbmanifp_notifier
);
118 struct interface
*nbmaifp
= nifp
->nbmaifp
;
119 struct nhrp_interface
*nbmanifp
= nbmaifp
->info
;
120 char buf
[SU_ADDRSTRLEN
];
123 case NOTIFY_INTERFACE_CHANGED
:
124 nhrp_interface_update_mtu(nifp
->ifp
, AFI_IP
);
125 nhrp_interface_update_source(nifp
->ifp
);
127 case NOTIFY_INTERFACE_ADDRESS_CHANGED
:
128 nifp
->nbma
= nbmanifp
->afi
[AFI_IP
].addr
;
129 nhrp_interface_update(nifp
->ifp
);
130 notifier_call(&nifp
->notifier_list
,
131 NOTIFY_INTERFACE_NBMA_CHANGED
);
132 debugf(NHRP_DEBUG_IF
, "%s: NBMA change: address %s",
134 sockunion2str(&nifp
->nbma
, buf
, sizeof(buf
)));
139 static void nhrp_interface_update_nbma(struct interface
*ifp
)
141 struct nhrp_interface
*nifp
= ifp
->info
, *nbmanifp
= NULL
;
142 struct interface
*nbmaifp
= NULL
;
143 union sockunion nbma
;
145 sockunion_family(&nbma
) = AF_UNSPEC
;
148 nbmaifp
= if_lookup_by_name(nifp
->source
, VRF_DEFAULT
);
150 switch (ifp
->ll_type
) {
151 case ZEBRA_LLT_IPGRE
: {
152 struct in_addr saddr
= {0};
153 netlink_gre_get_info(ifp
->ifindex
, &nifp
->grekey
,
154 &nifp
->linkidx
, &saddr
);
155 debugf(NHRP_DEBUG_IF
, "%s: GRE: %x %x %x", ifp
->name
,
156 nifp
->grekey
, nifp
->linkidx
, saddr
.s_addr
);
157 if (saddr
.s_addr
!= INADDR_ANY
)
158 sockunion_set(&nbma
, AF_INET
, (uint8_t *)&saddr
.s_addr
,
159 sizeof(saddr
.s_addr
));
160 else if (!nbmaifp
&& nifp
->linkidx
!= IFINDEX_INTERNAL
)
162 if_lookup_by_index(nifp
->linkidx
, VRF_DEFAULT
);
169 nbmanifp
= nbmaifp
->info
;
171 if (nbmaifp
!= nifp
->nbmaifp
) {
173 notifier_del(&nifp
->nbmanifp_notifier
);
174 nifp
->nbmaifp
= nbmaifp
;
176 notifier_add(&nifp
->nbmanifp_notifier
,
177 &nbmanifp
->notifier_list
,
178 nhrp_interface_interface_notifier
);
179 debugf(NHRP_DEBUG_IF
, "%s: bound to %s", ifp
->name
,
185 if (sockunion_family(&nbma
) == AF_UNSPEC
)
186 nbma
= nbmanifp
->afi
[AFI_IP
].addr
;
187 nhrp_interface_update_mtu(ifp
, AFI_IP
);
188 nhrp_interface_update_source(ifp
);
191 if (!sockunion_same(&nbma
, &nifp
->nbma
)) {
193 nhrp_interface_update(nifp
->ifp
);
194 debugf(NHRP_DEBUG_IF
, "%s: NBMA address changed", ifp
->name
);
195 notifier_call(&nifp
->notifier_list
,
196 NOTIFY_INTERFACE_NBMA_CHANGED
);
199 nhrp_interface_update(ifp
);
202 static void nhrp_interface_update_address(struct interface
*ifp
, afi_t afi
,
205 const int family
= afi2family(afi
);
206 struct nhrp_interface
*nifp
= ifp
->info
;
207 struct nhrp_afi_data
*if_ad
= &nifp
->afi
[afi
];
208 struct nhrp_cache
*nc
;
209 struct connected
*c
, *best
;
210 struct listnode
*cnode
;
211 union sockunion addr
;
212 char buf
[PREFIX_STRLEN
];
214 /* Select new best match preferring primary address */
216 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
217 if (PREFIX_FAMILY(c
->address
) != family
)
223 if ((best
->flags
& ZEBRA_IFA_SECONDARY
)
224 && !(c
->flags
& ZEBRA_IFA_SECONDARY
)) {
228 if (!(best
->flags
& ZEBRA_IFA_SECONDARY
)
229 && (c
->flags
& ZEBRA_IFA_SECONDARY
))
231 if (best
->address
->prefixlen
> c
->address
->prefixlen
) {
235 if (best
->address
->prefixlen
< c
->address
->prefixlen
)
239 /* On NHRP interfaces a host prefix is required */
240 if (best
&& if_ad
->configured
241 && best
->address
->prefixlen
!= 8 * prefix_blen(best
->address
)) {
242 zlog_notice("%s: %pFX is not a host prefix", ifp
->name
,
247 /* Update address if it changed */
249 prefix2sockunion(best
->address
, &addr
);
251 memset(&addr
, 0, sizeof(addr
));
253 if (!force
&& sockunion_same(&if_ad
->addr
, &addr
))
256 if (sockunion_family(&if_ad
->addr
) != AF_UNSPEC
) {
257 nc
= nhrp_cache_get(ifp
, &if_ad
->addr
, 0);
259 nhrp_cache_update_binding(nc
, NHRP_CACHE_LOCAL
, -1,
263 debugf(NHRP_DEBUG_KERNEL
, "%s: IPv%d address changed to %s", ifp
->name
,
264 afi
== AFI_IP
? 4 : 6,
265 best
? prefix2str(best
->address
, buf
, sizeof(buf
)) : "(none)");
268 if (if_ad
->configured
&& sockunion_family(&if_ad
->addr
) != AF_UNSPEC
) {
269 nc
= nhrp_cache_get(ifp
, &addr
, 1);
271 nhrp_cache_update_binding(nc
, NHRP_CACHE_LOCAL
, 0, NULL
,
275 notifier_call(&nifp
->notifier_list
, NOTIFY_INTERFACE_ADDRESS_CHANGED
);
278 void nhrp_interface_update(struct interface
*ifp
)
280 struct nhrp_interface
*nifp
= ifp
->info
;
281 struct nhrp_afi_data
*if_ad
;
285 notifier_call(&nifp
->notifier_list
, NOTIFY_INTERFACE_CHANGED
);
287 for (afi
= 0; afi
< AFI_MAX
; afi
++) {
288 if_ad
= &nifp
->afi
[afi
];
290 if (sockunion_family(&nifp
->nbma
) == AF_UNSPEC
291 || ifp
->ifindex
== IFINDEX_INTERNAL
|| !if_is_up(ifp
)
292 || !if_ad
->network_id
) {
293 if (if_ad
->configured
) {
294 if_ad
->configured
= 0;
295 nhrp_interface_update_address(ifp
, afi
, 1);
300 if (!if_ad
->configured
) {
301 os_configure_dmvpn(ifp
->ifindex
, ifp
->name
,
303 if_ad
->configured
= 1;
304 nhrp_interface_update_address(ifp
, afi
, 1);
310 if (enabled
!= nifp
->enabled
) {
311 nifp
->enabled
= enabled
;
312 notifier_call(&nifp
->notifier_list
,
313 enabled
? NOTIFY_INTERFACE_UP
314 : NOTIFY_INTERFACE_DOWN
);
318 int nhrp_ifp_create(struct interface
*ifp
)
320 debugf(NHRP_DEBUG_IF
, "if-add: %s, ifindex: %u, hw_type: %d %s",
321 ifp
->name
, ifp
->ifindex
, ifp
->ll_type
,
322 if_link_type_str(ifp
->ll_type
));
324 nhrp_interface_update_nbma(ifp
);
329 int nhrp_ifp_destroy(struct interface
*ifp
)
331 debugf(NHRP_DEBUG_IF
, "if-delete: %s", ifp
->name
);
333 nhrp_interface_update_cache_config(ifp
, false, AF_INET
);
334 nhrp_interface_update_cache_config(ifp
, false, AF_INET6
);
335 nhrp_interface_update(ifp
);
345 static void interface_config_update_nhrp_map(struct nhrp_cache_config
*cc
,
348 struct map_ctx
*ctx
= data
;
349 struct interface
*ifp
= cc
->ifp
;
350 struct nhrp_cache
*c
;
351 union sockunion nbma_addr
;
353 if (sockunion_family(&cc
->remote_addr
) != ctx
->family
)
356 /* gre layer not ready */
357 if (ifp
->vrf_id
== VRF_UNKNOWN
)
360 c
= nhrp_cache_get(ifp
, &cc
->remote_addr
, ctx
->enabled
? 1 : 0);
361 if (!c
&& !ctx
->enabled
)
367 nhrp_cache_update_binding(
369 nhrp_peer_get(ifp
, &nbma_addr
), 0, NULL
);
378 if (cc
->type
== NHRP_CACHE_LOCAL
)
379 nhrp_cache_update_binding(c
, NHRP_CACHE_LOCAL
, 0, NULL
, 0,
382 nhrp_cache_update_binding(c
, NHRP_CACHE_STATIC
, 0,
383 nhrp_peer_get(ifp
, &cc
->nbma
), 0,
388 static void nhrp_interface_update_cache_config(struct interface
*ifp
, bool available
, uint8_t family
)
390 struct map_ctx mapctx
;
392 mapctx
= (struct map_ctx
){
396 nhrp_cache_config_foreach(ifp
, interface_config_update_nhrp_map
,
401 int nhrp_ifp_up(struct interface
*ifp
)
403 debugf(NHRP_DEBUG_IF
, "if-up: %s", ifp
->name
);
404 nhrp_interface_update_nbma(ifp
);
409 int nhrp_ifp_down(struct interface
*ifp
)
411 debugf(NHRP_DEBUG_IF
, "if-down: %s", ifp
->name
);
412 nhrp_interface_update(ifp
);
417 int nhrp_interface_address_add(ZAPI_CALLBACK_ARGS
)
419 struct connected
*ifc
;
421 ifc
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
425 debugf(NHRP_DEBUG_IF
, "if-addr-add: %s: %pFX", ifc
->ifp
->name
,
428 nhrp_interface_update_address(
429 ifc
->ifp
, family2afi(PREFIX_FAMILY(ifc
->address
)), 0);
430 nhrp_interface_update_cache_config(ifc
->ifp
, true, PREFIX_FAMILY(ifc
->address
));
434 int nhrp_interface_address_delete(ZAPI_CALLBACK_ARGS
)
436 struct connected
*ifc
;
438 ifc
= zebra_interface_address_read(cmd
, zclient
->ibuf
, vrf_id
);
442 debugf(NHRP_DEBUG_IF
, "if-addr-del: %s: %pFX", ifc
->ifp
->name
,
445 nhrp_interface_update_address(
446 ifc
->ifp
, family2afi(PREFIX_FAMILY(ifc
->address
)), 0);
447 connected_free(&ifc
);
452 void nhrp_interface_notify_add(struct interface
*ifp
, struct notifier_block
*n
,
455 struct nhrp_interface
*nifp
= ifp
->info
;
456 notifier_add(n
, &nifp
->notifier_list
, fn
);
459 void nhrp_interface_notify_del(struct interface
*ifp
, struct notifier_block
*n
)
464 void nhrp_interface_set_protection(struct interface
*ifp
, const char *profile
,
465 const char *fallback_profile
)
467 struct nhrp_interface
*nifp
= ifp
->info
;
469 if (nifp
->ipsec_profile
)
470 free(nifp
->ipsec_profile
);
471 nifp
->ipsec_profile
= profile
? strdup(profile
) : NULL
;
473 if (nifp
->ipsec_fallback_profile
)
474 free(nifp
->ipsec_fallback_profile
);
475 nifp
->ipsec_fallback_profile
=
476 fallback_profile
? strdup(fallback_profile
) : NULL
;
478 notifier_call(&nifp
->notifier_list
, NOTIFY_INTERFACE_ADDRESS_CHANGED
);
481 void nhrp_interface_set_source(struct interface
*ifp
, const char *ifname
)
483 struct nhrp_interface
*nifp
= ifp
->info
;
487 nifp
->source
= ifname
? strdup(ifname
) : NULL
;
489 nhrp_interface_update_nbma(ifp
);