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
));
34 notifier_init(&nifp
->notifier_list
);
35 for (afi
= 0; afi
< AFI_MAX
; afi
++) {
36 struct nhrp_afi_data
*ad
= &nifp
->afi
[afi
];
37 ad
->holdtime
= NHRPD_DEFAULT_HOLDTIME
;
38 list_init(&ad
->nhslist_head
);
44 static int nhrp_if_delete_hook(struct interface
*ifp
)
46 XFREE(MTYPE_NHRP_IF
, ifp
->info
);
50 void nhrp_interface_init(void)
52 hook_register_prio(if_add
, 0, nhrp_if_new_hook
);
53 hook_register_prio(if_del
, 0, nhrp_if_delete_hook
);
56 void nhrp_interface_update_mtu(struct interface
*ifp
, afi_t afi
)
58 struct nhrp_interface
*nifp
= ifp
->info
;
59 struct nhrp_afi_data
*if_ad
= &nifp
->afi
[afi
];
60 unsigned short new_mtu
;
62 if (if_ad
->configured_mtu
< 0)
63 new_mtu
= nifp
->nbmaifp
? nifp
->nbmaifp
->mtu
: 0;
65 new_mtu
= if_ad
->configured_mtu
;
69 if (new_mtu
!= if_ad
->mtu
) {
70 debugf(NHRP_DEBUG_IF
, "%s: MTU changed to %d", ifp
->name
,
73 notifier_call(&nifp
->notifier_list
,
74 NOTIFY_INTERFACE_MTU_CHANGED
);
78 static void nhrp_interface_update_source(struct interface
*ifp
)
80 struct nhrp_interface
*nifp
= ifp
->info
;
82 if (!nifp
->source
|| !nifp
->nbmaifp
83 || (ifindex_t
)nifp
->linkidx
== nifp
->nbmaifp
->ifindex
)
86 nifp
->linkidx
= nifp
->nbmaifp
->ifindex
;
87 debugf(NHRP_DEBUG_IF
, "%s: bound device index changed to %d", ifp
->name
,
89 netlink_gre_set_link(ifp
->ifindex
, nifp
->linkidx
);
92 static void nhrp_interface_interface_notifier(struct notifier_block
*n
,
95 struct nhrp_interface
*nifp
=
96 container_of(n
, struct nhrp_interface
, nbmanifp_notifier
);
97 struct interface
*nbmaifp
= nifp
->nbmaifp
;
98 struct nhrp_interface
*nbmanifp
= nbmaifp
->info
;
99 char buf
[SU_ADDRSTRLEN
];
102 case NOTIFY_INTERFACE_CHANGED
:
103 nhrp_interface_update_mtu(nifp
->ifp
, AFI_IP
);
104 nhrp_interface_update_source(nifp
->ifp
);
106 case NOTIFY_INTERFACE_ADDRESS_CHANGED
:
107 nifp
->nbma
= nbmanifp
->afi
[AFI_IP
].addr
;
108 nhrp_interface_update(nifp
->ifp
);
109 notifier_call(&nifp
->notifier_list
,
110 NOTIFY_INTERFACE_NBMA_CHANGED
);
111 debugf(NHRP_DEBUG_IF
, "%s: NBMA change: address %s",
113 sockunion2str(&nifp
->nbma
, buf
, sizeof buf
));
118 static void nhrp_interface_update_nbma(struct interface
*ifp
)
120 struct nhrp_interface
*nifp
= ifp
->info
, *nbmanifp
= NULL
;
121 struct interface
*nbmaifp
= NULL
;
122 union sockunion nbma
;
124 sockunion_family(&nbma
) = AF_UNSPEC
;
127 nbmaifp
= if_lookup_by_name(nifp
->source
, VRF_DEFAULT
);
129 switch (ifp
->ll_type
) {
130 case ZEBRA_LLT_IPGRE
: {
131 struct in_addr saddr
= {0};
132 netlink_gre_get_info(ifp
->ifindex
, &nifp
->grekey
,
133 &nifp
->linkidx
, &saddr
);
134 debugf(NHRP_DEBUG_IF
, "%s: GRE: %x %x %x", ifp
->name
,
135 nifp
->grekey
, nifp
->linkidx
, saddr
.s_addr
);
137 sockunion_set(&nbma
, AF_INET
, (uint8_t *)&saddr
.s_addr
,
138 sizeof(saddr
.s_addr
));
139 else if (!nbmaifp
&& nifp
->linkidx
!= IFINDEX_INTERNAL
)
141 if_lookup_by_index(nifp
->linkidx
, VRF_DEFAULT
);
148 nbmanifp
= nbmaifp
->info
;
150 if (nbmaifp
!= nifp
->nbmaifp
) {
152 notifier_del(&nifp
->nbmanifp_notifier
);
153 nifp
->nbmaifp
= nbmaifp
;
155 notifier_add(&nifp
->nbmanifp_notifier
,
156 &nbmanifp
->notifier_list
,
157 nhrp_interface_interface_notifier
);
158 debugf(NHRP_DEBUG_IF
, "%s: bound to %s", ifp
->name
,
164 if (sockunion_family(&nbma
) == AF_UNSPEC
)
165 nbma
= nbmanifp
->afi
[AFI_IP
].addr
;
166 nhrp_interface_update_mtu(ifp
, AFI_IP
);
167 nhrp_interface_update_source(ifp
);
170 if (!sockunion_same(&nbma
, &nifp
->nbma
)) {
172 nhrp_interface_update(nifp
->ifp
);
173 debugf(NHRP_DEBUG_IF
, "%s: NBMA address changed", ifp
->name
);
174 notifier_call(&nifp
->notifier_list
,
175 NOTIFY_INTERFACE_NBMA_CHANGED
);
178 nhrp_interface_update(ifp
);
181 static void nhrp_interface_update_address(struct interface
*ifp
, afi_t afi
,
184 const int family
= afi2family(afi
);
185 struct nhrp_interface
*nifp
= ifp
->info
;
186 struct nhrp_afi_data
*if_ad
= &nifp
->afi
[afi
];
187 struct nhrp_cache
*nc
;
188 struct connected
*c
, *best
;
189 struct listnode
*cnode
;
190 union sockunion addr
;
191 char buf
[PREFIX_STRLEN
];
193 /* Select new best match preferring primary address */
195 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
196 if (PREFIX_FAMILY(c
->address
) != family
)
202 if ((best
->flags
& ZEBRA_IFA_SECONDARY
)
203 && !(c
->flags
& ZEBRA_IFA_SECONDARY
)) {
207 if (!(best
->flags
& ZEBRA_IFA_SECONDARY
)
208 && (c
->flags
& ZEBRA_IFA_SECONDARY
))
210 if (best
->address
->prefixlen
> c
->address
->prefixlen
) {
214 if (best
->address
->prefixlen
< c
->address
->prefixlen
)
218 /* On NHRP interfaces a host prefix is required */
219 if (best
&& if_ad
->configured
220 && best
->address
->prefixlen
!= 8 * prefix_blen(best
->address
)) {
221 zlog_notice("%s: %s is not a host prefix", ifp
->name
,
222 prefix2str(best
->address
, buf
, sizeof buf
));
226 /* Update address if it changed */
228 prefix2sockunion(best
->address
, &addr
);
230 memset(&addr
, 0, sizeof(addr
));
232 if (!force
&& sockunion_same(&if_ad
->addr
, &addr
))
235 if (sockunion_family(&if_ad
->addr
) != AF_UNSPEC
) {
236 nc
= nhrp_cache_get(ifp
, &if_ad
->addr
, 0);
238 nhrp_cache_update_binding(nc
, NHRP_CACHE_LOCAL
, -1,
242 debugf(NHRP_DEBUG_KERNEL
, "%s: IPv%d address changed to %s", ifp
->name
,
243 afi
== AFI_IP
? 4 : 6,
244 best
? prefix2str(best
->address
, buf
, sizeof buf
) : "(none)");
247 if (if_ad
->configured
&& sockunion_family(&if_ad
->addr
) != AF_UNSPEC
) {
248 nc
= nhrp_cache_get(ifp
, &addr
, 1);
250 nhrp_cache_update_binding(nc
, NHRP_CACHE_LOCAL
, 0, NULL
,
254 notifier_call(&nifp
->notifier_list
, NOTIFY_INTERFACE_ADDRESS_CHANGED
);
257 void nhrp_interface_update(struct interface
*ifp
)
259 struct nhrp_interface
*nifp
= ifp
->info
;
260 struct nhrp_afi_data
*if_ad
;
264 notifier_call(&nifp
->notifier_list
, NOTIFY_INTERFACE_CHANGED
);
266 for (afi
= 0; afi
< AFI_MAX
; afi
++) {
267 if_ad
= &nifp
->afi
[afi
];
269 if (sockunion_family(&nifp
->nbma
) == AF_UNSPEC
270 || ifp
->ifindex
== IFINDEX_INTERNAL
|| !if_is_up(ifp
)
271 || !if_ad
->network_id
) {
272 if (if_ad
->configured
) {
273 if_ad
->configured
= 0;
274 nhrp_interface_update_address(ifp
, afi
, 1);
279 if (!if_ad
->configured
) {
280 os_configure_dmvpn(ifp
->ifindex
, ifp
->name
,
282 if_ad
->configured
= 1;
283 nhrp_interface_update_address(ifp
, afi
, 1);
289 if (enabled
!= nifp
->enabled
) {
290 nifp
->enabled
= enabled
;
291 notifier_call(&nifp
->notifier_list
,
292 enabled
? NOTIFY_INTERFACE_UP
293 : NOTIFY_INTERFACE_DOWN
);
297 int nhrp_interface_add(int cmd
, struct zclient
*client
, zebra_size_t length
,
300 struct interface
*ifp
;
302 /* read and add the interface in the iflist. */
303 ifp
= zebra_interface_add_read(client
->ibuf
, vrf_id
);
307 debugf(NHRP_DEBUG_IF
, "if-add: %s, ifindex: %u, hw_type: %d %s",
308 ifp
->name
, ifp
->ifindex
, ifp
->ll_type
,
309 if_link_type_str(ifp
->ll_type
));
311 nhrp_interface_update_nbma(ifp
);
316 int nhrp_interface_delete(int cmd
, struct zclient
*client
, zebra_size_t length
,
319 struct interface
*ifp
;
323 ifp
= zebra_interface_state_read(s
, vrf_id
);
327 debugf(NHRP_DEBUG_IF
, "if-delete: %s", ifp
->name
);
328 if_set_index(ifp
, ifp
->ifindex
);
329 nhrp_interface_update(ifp
);
330 /* if_delete(ifp); */
334 int nhrp_interface_up(int cmd
, struct zclient
*client
, zebra_size_t length
,
337 struct interface
*ifp
;
339 ifp
= zebra_interface_state_read(client
->ibuf
, vrf_id
);
343 debugf(NHRP_DEBUG_IF
, "if-up: %s", ifp
->name
);
344 nhrp_interface_update_nbma(ifp
);
349 int nhrp_interface_down(int cmd
, struct zclient
*client
, zebra_size_t length
,
352 struct interface
*ifp
;
354 ifp
= zebra_interface_state_read(client
->ibuf
, vrf_id
);
358 debugf(NHRP_DEBUG_IF
, "if-down: %s", ifp
->name
);
359 nhrp_interface_update(ifp
);
363 int nhrp_interface_address_add(int cmd
, struct zclient
*client
,
364 zebra_size_t length
, vrf_id_t vrf_id
)
366 struct connected
*ifc
;
367 char buf
[PREFIX_STRLEN
];
369 ifc
= zebra_interface_address_read(cmd
, client
->ibuf
, vrf_id
);
373 debugf(NHRP_DEBUG_IF
, "if-addr-add: %s: %s", ifc
->ifp
->name
,
374 prefix2str(ifc
->address
, buf
, sizeof buf
));
376 nhrp_interface_update_address(
377 ifc
->ifp
, family2afi(PREFIX_FAMILY(ifc
->address
)), 0);
382 int nhrp_interface_address_delete(int cmd
, struct zclient
*client
,
383 zebra_size_t length
, vrf_id_t vrf_id
)
385 struct connected
*ifc
;
386 char buf
[PREFIX_STRLEN
];
388 ifc
= zebra_interface_address_read(cmd
, client
->ibuf
, vrf_id
);
392 debugf(NHRP_DEBUG_IF
, "if-addr-del: %s: %s", ifc
->ifp
->name
,
393 prefix2str(ifc
->address
, buf
, sizeof buf
));
395 nhrp_interface_update_address(
396 ifc
->ifp
, family2afi(PREFIX_FAMILY(ifc
->address
)), 0);
402 void nhrp_interface_notify_add(struct interface
*ifp
, struct notifier_block
*n
,
405 struct nhrp_interface
*nifp
= ifp
->info
;
406 notifier_add(n
, &nifp
->notifier_list
, fn
);
409 void nhrp_interface_notify_del(struct interface
*ifp
, struct notifier_block
*n
)
414 void nhrp_interface_set_protection(struct interface
*ifp
, const char *profile
,
415 const char *fallback_profile
)
417 struct nhrp_interface
*nifp
= ifp
->info
;
419 if (nifp
->ipsec_profile
)
420 free(nifp
->ipsec_profile
);
421 nifp
->ipsec_profile
= profile
? strdup(profile
) : NULL
;
423 if (nifp
->ipsec_fallback_profile
)
424 free(nifp
->ipsec_fallback_profile
);
425 nifp
->ipsec_fallback_profile
=
426 fallback_profile
? strdup(fallback_profile
) : NULL
;
428 notifier_call(&nifp
->notifier_list
, NOTIFY_INTERFACE_ADDRESS_CHANGED
);
431 void nhrp_interface_set_source(struct interface
*ifp
, const char *ifname
)
433 struct nhrp_interface
*nifp
= ifp
->info
;
437 nifp
->source
= ifname
? strdup(ifname
) : NULL
;
439 nhrp_interface_update_nbma(ifp
);