1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* NHRP virtual connection
3 * Copyright (c) 2014-2015 Timo Teräs
16 DEFINE_MTYPE_STATIC(NHRPD
, NHRP_VC
, "NHRP virtual connection");
18 PREDECL_DLIST(childlist
);
23 struct childlist_item childlist_entry
;
26 DECLARE_DLIST(childlist
, struct child_sa
, childlist_entry
);
28 static struct hash
*nhrp_vc_hash
;
29 static struct childlist_head childlist_head
[512];
31 static unsigned int nhrp_vc_key(const void *peer_data
)
33 const struct nhrp_vc
*vc
= peer_data
;
34 return jhash_2words(sockunion_hash(&vc
->local
.nbma
),
35 sockunion_hash(&vc
->remote
.nbma
), 0);
38 static bool nhrp_vc_cmp(const void *cache_data
, const void *key_data
)
40 const struct nhrp_vc
*a
= cache_data
;
41 const struct nhrp_vc
*b
= key_data
;
43 return sockunion_same(&a
->local
.nbma
, &b
->local
.nbma
)
44 && sockunion_same(&a
->remote
.nbma
, &b
->remote
.nbma
);
47 static void *nhrp_vc_alloc(void *data
)
49 struct nhrp_vc
*vc
, *key
= data
;
51 vc
= XMALLOC(MTYPE_NHRP_VC
, sizeof(struct nhrp_vc
));
53 *vc
= (struct nhrp_vc
){
54 .local
.nbma
= key
->local
.nbma
,
55 .remote
.nbma
= key
->remote
.nbma
,
57 NOTIFIER_LIST_INITIALIZER(&vc
->notifier_list
),
63 static void nhrp_vc_free(void *data
)
65 XFREE(MTYPE_NHRP_VC
, data
);
68 struct nhrp_vc
*nhrp_vc_get(const union sockunion
*src
,
69 const union sockunion
*dst
, int create
)
72 key
.local
.nbma
= *src
;
73 key
.remote
.nbma
= *dst
;
74 return hash_get(nhrp_vc_hash
, &key
, create
? nhrp_vc_alloc
: 0);
77 static void nhrp_vc_check_delete(struct nhrp_vc
*vc
)
79 if (vc
->updating
|| vc
->ipsec
|| notifier_active(&vc
->notifier_list
))
81 hash_release(nhrp_vc_hash
, vc
);
85 static void nhrp_vc_update(struct nhrp_vc
*vc
, long cmd
)
88 notifier_call(&vc
->notifier_list
, cmd
);
90 nhrp_vc_check_delete(vc
);
93 static void nhrp_vc_ipsec_reset(struct nhrp_vc
*vc
)
96 vc
->local
.certlen
= 0;
98 vc
->remote
.certlen
= 0;
101 int nhrp_vc_ipsec_updown(uint32_t child_id
, struct nhrp_vc
*vc
)
103 struct child_sa
*sa
= NULL
, *lsa
;
104 uint32_t child_hash
= child_id
% array_size(childlist_head
);
105 int abort_migration
= 0;
107 frr_each (childlist
, &childlist_head
[child_hash
], lsa
) {
108 if (lsa
->id
== child_id
) {
118 sa
= XMALLOC(MTYPE_NHRP_VC
, sizeof(struct child_sa
));
120 *sa
= (struct child_sa
){
124 childlist_add_tail(&childlist_head
[child_hash
], sa
);
131 /* Attach first to new VC */
133 nhrp_vc_update(vc
, NOTIFY_VC_IPSEC_CHANGED
);
136 /* Notify old VC of migration */
137 sa
->vc
->abort_migration
= 0;
138 debugf(NHRP_DEBUG_COMMON
, "IPsec NBMA change of %pSU to %pSU",
139 &sa
->vc
->remote
.nbma
, &vc
->remote
.nbma
);
140 nhrp_vc_update(sa
->vc
, NOTIFY_VC_IPSEC_UPDATE_NBMA
);
141 abort_migration
= sa
->vc
->abort_migration
;
144 /* Deattach old VC */
147 nhrp_vc_ipsec_reset(sa
->vc
);
148 nhrp_vc_update(sa
->vc
, NOTIFY_VC_IPSEC_CHANGED
);
154 childlist_del(&childlist_head
[child_hash
], sa
);
155 XFREE(MTYPE_NHRP_VC
, sa
);
158 return abort_migration
;
161 void nhrp_vc_notify_add(struct nhrp_vc
*vc
, struct notifier_block
*n
,
162 notifier_fn_t action
)
164 notifier_add(n
, &vc
->notifier_list
, action
);
167 void nhrp_vc_notify_del(struct nhrp_vc
*vc
, struct notifier_block
*n
)
169 notifier_del(n
, &vc
->notifier_list
);
170 nhrp_vc_check_delete(vc
);
174 struct nhrp_vc_iterator_ctx
{
175 void (*cb
)(struct nhrp_vc
*, void *);
179 static void nhrp_vc_iterator(struct hash_bucket
*b
, void *ctx
)
181 struct nhrp_vc_iterator_ctx
*ic
= ctx
;
182 ic
->cb(b
->data
, ic
->ctx
);
185 void nhrp_vc_foreach(void (*cb
)(struct nhrp_vc
*, void *), void *ctx
)
187 struct nhrp_vc_iterator_ctx ic
= {
188 .cb
= cb
, .ctx
= ctx
,
190 hash_iterate(nhrp_vc_hash
, nhrp_vc_iterator
, &ic
);
193 void nhrp_vc_init(void)
197 nhrp_vc_hash
= hash_create(nhrp_vc_key
, nhrp_vc_cmp
, "NHRP VC hash");
198 for (i
= 0; i
< array_size(childlist_head
); i
++)
199 childlist_init(&childlist_head
[i
]);
202 void nhrp_vc_reset(void)
207 for (i
= 0; i
< array_size(childlist_head
); i
++) {
208 frr_each_safe (childlist
, &childlist_head
[i
], sa
)
209 nhrp_vc_ipsec_updown(sa
->id
, 0);
213 void nhrp_vc_terminate(void)
216 hash_clean(nhrp_vc_hash
, nhrp_vc_free
);