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.
10 #include <net/if_arp.h>
20 DEFINE_MTYPE_STATIC(NHRPD
, NHRP_IF
, "NHRP interface")
22 static int nhrp_if_new_hook(struct interface
*ifp
)
24 struct nhrp_interface
*nifp
;
27 nifp
= XCALLOC(MTYPE_NHRP_IF
, sizeof(struct nhrp_interface
));
32 notifier_init(&nifp
->notifier_list
);
33 for (afi
= 0; afi
< AFI_MAX
; afi
++) {
34 struct nhrp_afi_data
*ad
= &nifp
->afi
[afi
];
35 ad
->holdtime
= NHRPD_DEFAULT_HOLDTIME
;
36 list_init(&ad
->nhslist_head
);
42 static int nhrp_if_delete_hook(struct interface
*ifp
)
44 XFREE(MTYPE_NHRP_IF
, ifp
->info
);
48 void nhrp_interface_init(void)
50 hook_register_prio(if_add
, 0, nhrp_if_new_hook
);
51 hook_register_prio(if_del
, 0, nhrp_if_delete_hook
);
54 void nhrp_interface_update_mtu(struct interface
*ifp
, afi_t afi
)
56 struct nhrp_interface
*nifp
= ifp
->info
;
57 struct nhrp_afi_data
*if_ad
= &nifp
->afi
[afi
];
58 unsigned short new_mtu
;
60 if (if_ad
->configured_mtu
< 0)
61 new_mtu
= nifp
->nbmaifp
? nifp
->nbmaifp
->mtu
: 0;
63 new_mtu
= if_ad
->configured_mtu
;
67 if (new_mtu
!= if_ad
->mtu
) {
68 debugf(NHRP_DEBUG_IF
, "%s: MTU changed to %d", ifp
->name
,
71 notifier_call(&nifp
->notifier_list
,
72 NOTIFY_INTERFACE_MTU_CHANGED
);
76 static void nhrp_interface_update_source(struct interface
*ifp
)
78 struct nhrp_interface
*nifp
= ifp
->info
;
80 if (!nifp
->source
|| !nifp
->nbmaifp
81 || (ifindex_t
)nifp
->linkidx
== nifp
->nbmaifp
->ifindex
)
84 nifp
->linkidx
= nifp
->nbmaifp
->ifindex
;
85 debugf(NHRP_DEBUG_IF
, "%s: bound device index changed to %d", ifp
->name
,
87 netlink_gre_set_link(ifp
->ifindex
, nifp
->linkidx
);
90 static void nhrp_interface_interface_notifier(struct notifier_block
*n
,
93 struct nhrp_interface
*nifp
=
94 container_of(n
, struct nhrp_interface
, nbmanifp_notifier
);
95 struct interface
*nbmaifp
= nifp
->nbmaifp
;
96 struct nhrp_interface
*nbmanifp
= nbmaifp
->info
;
97 char buf
[SU_ADDRSTRLEN
];
100 case NOTIFY_INTERFACE_CHANGED
:
101 nhrp_interface_update_mtu(nifp
->ifp
, AFI_IP
);
102 nhrp_interface_update_source(nifp
->ifp
);
104 case NOTIFY_INTERFACE_ADDRESS_CHANGED
:
105 nifp
->nbma
= nbmanifp
->afi
[AFI_IP
].addr
;
106 nhrp_interface_update(nifp
->ifp
);
107 notifier_call(&nifp
->notifier_list
,
108 NOTIFY_INTERFACE_NBMA_CHANGED
);
109 debugf(NHRP_DEBUG_IF
, "%s: NBMA change: address %s",
111 sockunion2str(&nifp
->nbma
, buf
, sizeof buf
));
116 static void nhrp_interface_update_nbma(struct interface
*ifp
)
118 struct nhrp_interface
*nifp
= ifp
->info
, *nbmanifp
= NULL
;
119 struct interface
*nbmaifp
= NULL
;
120 union sockunion nbma
;
122 sockunion_family(&nbma
) = AF_UNSPEC
;
125 nbmaifp
= if_lookup_by_name(nifp
->source
, VRF_DEFAULT
);
127 switch (ifp
->ll_type
) {
128 case ZEBRA_LLT_IPGRE
: {
129 struct in_addr saddr
= {0};
130 netlink_gre_get_info(ifp
->ifindex
, &nifp
->grekey
,
131 &nifp
->linkidx
, &saddr
);
132 debugf(NHRP_DEBUG_IF
, "%s: GRE: %x %x %x", ifp
->name
,
133 nifp
->grekey
, nifp
->linkidx
, saddr
.s_addr
);
135 sockunion_set(&nbma
, AF_INET
, (uint8_t *)&saddr
.s_addr
,
136 sizeof(saddr
.s_addr
));
137 else if (!nbmaifp
&& nifp
->linkidx
!= IFINDEX_INTERNAL
)
139 if_lookup_by_index(nifp
->linkidx
, VRF_DEFAULT
);
146 nbmanifp
= nbmaifp
->info
;
148 if (nbmaifp
!= nifp
->nbmaifp
) {
150 notifier_del(&nifp
->nbmanifp_notifier
);
151 nifp
->nbmaifp
= nbmaifp
;
153 notifier_add(&nifp
->nbmanifp_notifier
,
154 &nbmanifp
->notifier_list
,
155 nhrp_interface_interface_notifier
);
156 debugf(NHRP_DEBUG_IF
, "%s: bound to %s", ifp
->name
,
162 if (sockunion_family(&nbma
) == AF_UNSPEC
)
163 nbma
= nbmanifp
->afi
[AFI_IP
].addr
;
164 nhrp_interface_update_mtu(ifp
, AFI_IP
);
165 nhrp_interface_update_source(ifp
);
168 if (!sockunion_same(&nbma
, &nifp
->nbma
)) {
170 nhrp_interface_update(nifp
->ifp
);
171 debugf(NHRP_DEBUG_IF
, "%s: NBMA address changed", ifp
->name
);
172 notifier_call(&nifp
->notifier_list
,
173 NOTIFY_INTERFACE_NBMA_CHANGED
);
176 nhrp_interface_update(ifp
);
179 static void nhrp_interface_update_address(struct interface
*ifp
, afi_t afi
,
182 const int family
= afi2family(afi
);
183 struct nhrp_interface
*nifp
= ifp
->info
;
184 struct nhrp_afi_data
*if_ad
= &nifp
->afi
[afi
];
185 struct nhrp_cache
*nc
;
186 struct connected
*c
, *best
;
187 struct listnode
*cnode
;
188 union sockunion addr
;
189 char buf
[PREFIX_STRLEN
];
191 /* Select new best match preferring primary address */
193 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
194 if (PREFIX_FAMILY(c
->address
) != family
)
200 if ((best
->flags
& ZEBRA_IFA_SECONDARY
)
201 && !(c
->flags
& ZEBRA_IFA_SECONDARY
)) {
205 if (!(best
->flags
& ZEBRA_IFA_SECONDARY
)
206 && (c
->flags
& ZEBRA_IFA_SECONDARY
))
208 if (best
->address
->prefixlen
> c
->address
->prefixlen
) {
212 if (best
->address
->prefixlen
< c
->address
->prefixlen
)
216 /* On NHRP interfaces a host prefix is required */
217 if (best
&& if_ad
->configured
218 && best
->address
->prefixlen
!= 8 * prefix_blen(best
->address
)) {
219 zlog_notice("%s: %s is not a host prefix", ifp
->name
,
220 prefix2str(best
->address
, buf
, sizeof buf
));
224 /* Update address if it changed */
226 prefix2sockunion(best
->address
, &addr
);
228 memset(&addr
, 0, sizeof(addr
));
230 if (!force
&& sockunion_same(&if_ad
->addr
, &addr
))
233 if (sockunion_family(&if_ad
->addr
) != AF_UNSPEC
) {
234 nc
= nhrp_cache_get(ifp
, &if_ad
->addr
, 0);
236 nhrp_cache_update_binding(nc
, NHRP_CACHE_LOCAL
, -1,
240 debugf(NHRP_DEBUG_KERNEL
, "%s: IPv%d address changed to %s", ifp
->name
,
241 afi
== AFI_IP
? 4 : 6,
242 best
? prefix2str(best
->address
, buf
, sizeof buf
) : "(none)");
245 if (if_ad
->configured
&& sockunion_family(&if_ad
->addr
) != AF_UNSPEC
) {
246 nc
= nhrp_cache_get(ifp
, &addr
, 1);
248 nhrp_cache_update_binding(nc
, NHRP_CACHE_LOCAL
, 0, NULL
,
252 notifier_call(&nifp
->notifier_list
, NOTIFY_INTERFACE_ADDRESS_CHANGED
);
255 void nhrp_interface_update(struct interface
*ifp
)
257 struct nhrp_interface
*nifp
= ifp
->info
;
258 struct nhrp_afi_data
*if_ad
;
262 notifier_call(&nifp
->notifier_list
, NOTIFY_INTERFACE_CHANGED
);
264 for (afi
= 0; afi
< AFI_MAX
; afi
++) {
265 if_ad
= &nifp
->afi
[afi
];
267 if (sockunion_family(&nifp
->nbma
) == AF_UNSPEC
268 || ifp
->ifindex
== IFINDEX_INTERNAL
|| !if_is_up(ifp
)
269 || !if_ad
->network_id
) {
270 if (if_ad
->configured
) {
271 if_ad
->configured
= 0;
272 nhrp_interface_update_address(ifp
, afi
, 1);
277 if (!if_ad
->configured
) {
278 os_configure_dmvpn(ifp
->ifindex
, ifp
->name
,
280 if_ad
->configured
= 1;
281 nhrp_interface_update_address(ifp
, afi
, 1);
287 if (enabled
!= nifp
->enabled
) {
288 nifp
->enabled
= enabled
;
289 notifier_call(&nifp
->notifier_list
,
290 enabled
? NOTIFY_INTERFACE_UP
291 : NOTIFY_INTERFACE_DOWN
);
295 int nhrp_interface_add(int cmd
, struct zclient
*client
, zebra_size_t length
,
298 struct interface
*ifp
;
300 /* read and add the interface in the iflist. */
301 ifp
= zebra_interface_add_read(client
->ibuf
, vrf_id
);
305 debugf(NHRP_DEBUG_IF
, "if-add: %s, ifindex: %u, hw_type: %d %s",
306 ifp
->name
, ifp
->ifindex
, ifp
->ll_type
,
307 if_link_type_str(ifp
->ll_type
));
309 nhrp_interface_update_nbma(ifp
);
314 int nhrp_interface_delete(int cmd
, struct zclient
*client
, zebra_size_t length
,
317 struct interface
*ifp
;
321 ifp
= zebra_interface_state_read(s
, vrf_id
);
325 debugf(NHRP_DEBUG_IF
, "if-delete: %s", ifp
->name
);
326 if_set_index(ifp
, ifp
->ifindex
);
327 nhrp_interface_update(ifp
);
328 /* if_delete(ifp); */
332 int nhrp_interface_up(int cmd
, struct zclient
*client
, zebra_size_t length
,
335 struct interface
*ifp
;
337 ifp
= zebra_interface_state_read(client
->ibuf
, vrf_id
);
341 debugf(NHRP_DEBUG_IF
, "if-up: %s", ifp
->name
);
342 nhrp_interface_update_nbma(ifp
);
347 int nhrp_interface_down(int cmd
, struct zclient
*client
, zebra_size_t length
,
350 struct interface
*ifp
;
352 ifp
= zebra_interface_state_read(client
->ibuf
, vrf_id
);
356 debugf(NHRP_DEBUG_IF
, "if-down: %s", ifp
->name
);
357 nhrp_interface_update(ifp
);
361 int nhrp_interface_address_add(int cmd
, struct zclient
*client
,
362 zebra_size_t length
, vrf_id_t vrf_id
)
364 struct connected
*ifc
;
365 char buf
[PREFIX_STRLEN
];
367 ifc
= zebra_interface_address_read(cmd
, client
->ibuf
, vrf_id
);
371 debugf(NHRP_DEBUG_IF
, "if-addr-add: %s: %s", ifc
->ifp
->name
,
372 prefix2str(ifc
->address
, buf
, sizeof buf
));
374 nhrp_interface_update_address(
375 ifc
->ifp
, family2afi(PREFIX_FAMILY(ifc
->address
)), 0);
380 int nhrp_interface_address_delete(int cmd
, struct zclient
*client
,
381 zebra_size_t length
, vrf_id_t vrf_id
)
383 struct connected
*ifc
;
384 char buf
[PREFIX_STRLEN
];
386 ifc
= zebra_interface_address_read(cmd
, client
->ibuf
, vrf_id
);
390 debugf(NHRP_DEBUG_IF
, "if-addr-del: %s: %s", ifc
->ifp
->name
,
391 prefix2str(ifc
->address
, buf
, sizeof buf
));
393 nhrp_interface_update_address(
394 ifc
->ifp
, family2afi(PREFIX_FAMILY(ifc
->address
)), 0);
400 void nhrp_interface_notify_add(struct interface
*ifp
, struct notifier_block
*n
,
403 struct nhrp_interface
*nifp
= ifp
->info
;
404 notifier_add(n
, &nifp
->notifier_list
, fn
);
407 void nhrp_interface_notify_del(struct interface
*ifp
, struct notifier_block
*n
)
412 void nhrp_interface_set_protection(struct interface
*ifp
, const char *profile
,
413 const char *fallback_profile
)
415 struct nhrp_interface
*nifp
= ifp
->info
;
417 if (nifp
->ipsec_profile
)
418 free(nifp
->ipsec_profile
);
419 nifp
->ipsec_profile
= profile
? strdup(profile
) : NULL
;
421 if (nifp
->ipsec_fallback_profile
)
422 free(nifp
->ipsec_fallback_profile
);
423 nifp
->ipsec_fallback_profile
=
424 fallback_profile
? strdup(fallback_profile
) : NULL
;
426 notifier_call(&nifp
->notifier_list
, NOTIFY_INTERFACE_ADDRESS_CHANGED
);
429 void nhrp_interface_set_source(struct interface
*ifp
, const char *ifname
)
431 struct nhrp_interface
*nifp
= ifp
->info
;
435 nifp
->source
= ifname
? strdup(ifname
) : NULL
;
437 nhrp_interface_update_nbma(ifp
);