1 /* NHRP virtual connection
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.
20 DEFINE_MTYPE_STATIC(NHRPD
, NHRP_VC
, "NHRP virtual connection");
22 PREDECL_DLIST(childlist
);
27 struct childlist_item childlist_entry
;
30 DECLARE_DLIST(childlist
, struct child_sa
, childlist_entry
);
32 static struct hash
*nhrp_vc_hash
;
33 static struct childlist_head childlist_head
[512];
35 static unsigned int nhrp_vc_key(const void *peer_data
)
37 const struct nhrp_vc
*vc
= peer_data
;
38 return jhash_2words(sockunion_hash(&vc
->local
.nbma
),
39 sockunion_hash(&vc
->remote
.nbma
), 0);
42 static bool nhrp_vc_cmp(const void *cache_data
, const void *key_data
)
44 const struct nhrp_vc
*a
= cache_data
;
45 const struct nhrp_vc
*b
= key_data
;
47 return sockunion_same(&a
->local
.nbma
, &b
->local
.nbma
)
48 && sockunion_same(&a
->remote
.nbma
, &b
->remote
.nbma
);
51 static void *nhrp_vc_alloc(void *data
)
53 struct nhrp_vc
*vc
, *key
= data
;
55 vc
= XMALLOC(MTYPE_NHRP_VC
, sizeof(struct nhrp_vc
));
57 *vc
= (struct nhrp_vc
){
58 .local
.nbma
= key
->local
.nbma
,
59 .remote
.nbma
= key
->remote
.nbma
,
61 NOTIFIER_LIST_INITIALIZER(&vc
->notifier_list
),
67 static void nhrp_vc_free(void *data
)
69 XFREE(MTYPE_NHRP_VC
, data
);
72 struct nhrp_vc
*nhrp_vc_get(const union sockunion
*src
,
73 const union sockunion
*dst
, int create
)
76 key
.local
.nbma
= *src
;
77 key
.remote
.nbma
= *dst
;
78 return hash_get(nhrp_vc_hash
, &key
, create
? nhrp_vc_alloc
: 0);
81 static void nhrp_vc_check_delete(struct nhrp_vc
*vc
)
83 if (vc
->updating
|| vc
->ipsec
|| notifier_active(&vc
->notifier_list
))
85 hash_release(nhrp_vc_hash
, vc
);
89 static void nhrp_vc_update(struct nhrp_vc
*vc
, long cmd
)
92 notifier_call(&vc
->notifier_list
, cmd
);
94 nhrp_vc_check_delete(vc
);
97 static void nhrp_vc_ipsec_reset(struct nhrp_vc
*vc
)
100 vc
->local
.certlen
= 0;
101 vc
->remote
.id
[0] = 0;
102 vc
->remote
.certlen
= 0;
105 int nhrp_vc_ipsec_updown(uint32_t child_id
, struct nhrp_vc
*vc
)
107 struct child_sa
*sa
= NULL
, *lsa
;
108 uint32_t child_hash
= child_id
% array_size(childlist_head
);
109 int abort_migration
= 0;
111 frr_each (childlist
, &childlist_head
[child_hash
], lsa
) {
112 if (lsa
->id
== child_id
) {
122 sa
= XMALLOC(MTYPE_NHRP_VC
, sizeof(struct child_sa
));
124 *sa
= (struct child_sa
){
128 childlist_add_tail(&childlist_head
[child_hash
], sa
);
135 /* Attach first to new VC */
137 nhrp_vc_update(vc
, NOTIFY_VC_IPSEC_CHANGED
);
140 /* Notify old VC of migration */
141 sa
->vc
->abort_migration
= 0;
142 debugf(NHRP_DEBUG_COMMON
, "IPsec NBMA change of %pSU to %pSU",
143 &sa
->vc
->remote
.nbma
, &vc
->remote
.nbma
);
144 nhrp_vc_update(sa
->vc
, NOTIFY_VC_IPSEC_UPDATE_NBMA
);
145 abort_migration
= sa
->vc
->abort_migration
;
148 /* Deattach old VC */
151 nhrp_vc_ipsec_reset(sa
->vc
);
152 nhrp_vc_update(sa
->vc
, NOTIFY_VC_IPSEC_CHANGED
);
158 childlist_del(&childlist_head
[child_hash
], sa
);
159 XFREE(MTYPE_NHRP_VC
, sa
);
162 return abort_migration
;
165 void nhrp_vc_notify_add(struct nhrp_vc
*vc
, struct notifier_block
*n
,
166 notifier_fn_t action
)
168 notifier_add(n
, &vc
->notifier_list
, action
);
171 void nhrp_vc_notify_del(struct nhrp_vc
*vc
, struct notifier_block
*n
)
173 notifier_del(n
, &vc
->notifier_list
);
174 nhrp_vc_check_delete(vc
);
178 struct nhrp_vc_iterator_ctx
{
179 void (*cb
)(struct nhrp_vc
*, void *);
183 static void nhrp_vc_iterator(struct hash_bucket
*b
, void *ctx
)
185 struct nhrp_vc_iterator_ctx
*ic
= ctx
;
186 ic
->cb(b
->data
, ic
->ctx
);
189 void nhrp_vc_foreach(void (*cb
)(struct nhrp_vc
*, void *), void *ctx
)
191 struct nhrp_vc_iterator_ctx ic
= {
192 .cb
= cb
, .ctx
= ctx
,
194 hash_iterate(nhrp_vc_hash
, nhrp_vc_iterator
, &ic
);
197 void nhrp_vc_init(void)
201 nhrp_vc_hash
= hash_create(nhrp_vc_key
, nhrp_vc_cmp
, "NHRP VC hash");
202 for (i
= 0; i
< array_size(childlist_head
); i
++)
203 childlist_init(&childlist_head
[i
]);
206 void nhrp_vc_reset(void)
211 for (i
= 0; i
< array_size(childlist_head
); i
++) {
212 frr_each_safe (childlist
, &childlist_head
[i
], sa
)
213 nhrp_vc_ipsec_updown(sa
->id
, 0);
217 void nhrp_vc_terminate(void)
220 hash_clean(nhrp_vc_hash
, nhrp_vc_free
);