1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
/* NHRP virtual connection
* Copyright (c) 2014-2015 Timo Teräs
*
* This file is free software: you may copy, redistribute and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*/
#include "zebra.h"
#include "memory.h"
#include "stream.h"
#include "hash.h"
#include "thread.h"
#include "jhash.h"
#include "nhrpd.h"
#include "os.h"
DEFINE_MTYPE_STATIC(NHRPD, NHRP_VC, "NHRP virtual connection")
struct child_sa {
uint32_t id;
struct nhrp_vc *vc;
struct list_head childlist_entry;
};
static struct hash *nhrp_vc_hash;
static struct list_head childlist_head[512];
static unsigned int nhrp_vc_key(const void *peer_data)
{
const struct nhrp_vc *vc = peer_data;
return jhash_2words(sockunion_hash(&vc->local.nbma),
sockunion_hash(&vc->remote.nbma), 0);
}
static bool nhrp_vc_cmp(const void *cache_data, const void *key_data)
{
const struct nhrp_vc *a = cache_data;
const struct nhrp_vc *b = key_data;
return sockunion_same(&a->local.nbma, &b->local.nbma)
&& sockunion_same(&a->remote.nbma, &b->remote.nbma);
}
static void *nhrp_vc_alloc(void *data)
{
struct nhrp_vc *vc, *key = data;
vc = XMALLOC(MTYPE_NHRP_VC, sizeof(struct nhrp_vc));
*vc = (struct nhrp_vc){
.local.nbma = key->local.nbma,
.remote.nbma = key->remote.nbma,
.notifier_list =
NOTIFIER_LIST_INITIALIZER(&vc->notifier_list),
};
return vc;
}
static void nhrp_vc_free(void *data)
{
XFREE(MTYPE_NHRP_VC, data);
}
struct nhrp_vc *nhrp_vc_get(const union sockunion *src,
const union sockunion *dst, int create)
{
struct nhrp_vc key;
key.local.nbma = *src;
key.remote.nbma = *dst;
return hash_get(nhrp_vc_hash, &key, create ? nhrp_vc_alloc : 0);
}
static void nhrp_vc_check_delete(struct nhrp_vc *vc)
{
if (vc->updating || vc->ipsec || notifier_active(&vc->notifier_list))
return;
hash_release(nhrp_vc_hash, vc);
nhrp_vc_free(vc);
}
static void nhrp_vc_update(struct nhrp_vc *vc, long cmd)
{
vc->updating = 1;
notifier_call(&vc->notifier_list, cmd);
vc->updating = 0;
nhrp_vc_check_delete(vc);
}
static void nhrp_vc_ipsec_reset(struct nhrp_vc *vc)
{
vc->local.id[0] = 0;
vc->local.certlen = 0;
vc->remote.id[0] = 0;
vc->remote.certlen = 0;
}
int nhrp_vc_ipsec_updown(uint32_t child_id, struct nhrp_vc *vc)
{
char buf[2][SU_ADDRSTRLEN];
struct child_sa *sa = NULL, *lsa;
uint32_t child_hash = child_id % array_size(childlist_head);
int abort_migration = 0;
list_for_each_entry(lsa, &childlist_head[child_hash], childlist_entry)
{
if (lsa->id == child_id) {
sa = lsa;
break;
}
}
if (!sa) {
if (!vc)
return 0;
sa = XMALLOC(MTYPE_NHRP_VC, sizeof(struct child_sa));
*sa = (struct child_sa){
.id = child_id,
.childlist_entry =
LIST_INITIALIZER(sa->childlist_entry),
.vc = NULL,
};
list_add_tail(&sa->childlist_entry,
&childlist_head[child_hash]);
}
if (sa->vc == vc)
return 0;
if (vc) {
/* Attach first to new VC */
vc->ipsec++;
nhrp_vc_update(vc, NOTIFY_VC_IPSEC_CHANGED);
}
if (sa->vc && vc) {
/* Notify old VC of migration */
sa->vc->abort_migration = 0;
debugf(NHRP_DEBUG_COMMON, "IPsec NBMA change of %s to %s",
sockunion2str(&sa->vc->remote.nbma, buf[0],
sizeof buf[0]),
sockunion2str(&vc->remote.nbma, buf[1], sizeof buf[1]));
nhrp_vc_update(sa->vc, NOTIFY_VC_IPSEC_UPDATE_NBMA);
abort_migration = sa->vc->abort_migration;
}
if (sa->vc) {
/* Deattach old VC */
sa->vc->ipsec--;
if (!sa->vc->ipsec)
nhrp_vc_ipsec_reset(sa->vc);
nhrp_vc_update(sa->vc, NOTIFY_VC_IPSEC_CHANGED);
}
/* Update */
sa->vc = vc;
if (!vc) {
list_del(&sa->childlist_entry);
XFREE(MTYPE_NHRP_VC, sa);
}
return abort_migration;
}
void nhrp_vc_notify_add(struct nhrp_vc *vc, struct notifier_block *n,
notifier_fn_t action)
{
notifier_add(n, &vc->notifier_list, action);
}
void nhrp_vc_notify_del(struct nhrp_vc *vc, struct notifier_block *n)
{
notifier_del(n);
nhrp_vc_check_delete(vc);
}
struct nhrp_vc_iterator_ctx {
void (*cb)(struct nhrp_vc *, void *);
void *ctx;
};
static void nhrp_vc_iterator(struct hash_bucket *b, void *ctx)
{
struct nhrp_vc_iterator_ctx *ic = ctx;
ic->cb(b->data, ic->ctx);
}
void nhrp_vc_foreach(void (*cb)(struct nhrp_vc *, void *), void *ctx)
{
struct nhrp_vc_iterator_ctx ic = {
.cb = cb, .ctx = ctx,
};
hash_iterate(nhrp_vc_hash, nhrp_vc_iterator, &ic);
}
void nhrp_vc_init(void)
{
size_t i;
nhrp_vc_hash = hash_create(nhrp_vc_key, nhrp_vc_cmp, "NHRP VC hash");
for (i = 0; i < array_size(childlist_head); i++)
list_init(&childlist_head[i]);
}
void nhrp_vc_reset(void)
{
struct child_sa *sa, *n;
size_t i;
for (i = 0; i < array_size(childlist_head); i++) {
list_for_each_entry_safe(sa, n, &childlist_head[i],
childlist_entry)
nhrp_vc_ipsec_updown(sa->id, 0);
}
}
void nhrp_vc_terminate(void)
{
nhrp_vc_reset();
hash_clean(nhrp_vc_hash, nhrp_vc_free);
}
|