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.
18 DEFINE_MTYPE_STATIC(NHRPD
, NHRP_CACHE
, "NHRP cache entry")
20 unsigned long nhrp_cache_counts
[NHRP_CACHE_NUM_TYPES
];
22 const char *const nhrp_cache_type_str
[] = {
23 [NHRP_CACHE_INVALID
] = "invalid",
24 [NHRP_CACHE_INCOMPLETE
] = "incomplete",
25 [NHRP_CACHE_NEGATIVE
] = "negative",
26 [NHRP_CACHE_CACHED
] = "cached",
27 [NHRP_CACHE_DYNAMIC
] = "dynamic",
28 [NHRP_CACHE_NHS
] = "nhs",
29 [NHRP_CACHE_STATIC
] = "static",
30 [NHRP_CACHE_LOCAL
] = "local",
33 static unsigned int nhrp_cache_protocol_key(void *peer_data
)
35 struct nhrp_cache
*p
= peer_data
;
36 return sockunion_hash(&p
->remote_addr
);
39 static int nhrp_cache_protocol_cmp(const void *cache_data
, const void *key_data
)
41 const struct nhrp_cache
*a
= cache_data
;
42 const struct nhrp_cache
*b
= key_data
;
43 return sockunion_same(&a
->remote_addr
, &b
->remote_addr
);
46 static void *nhrp_cache_alloc(void *data
)
48 struct nhrp_cache
*p
, *key
= data
;
50 p
= XMALLOC(MTYPE_NHRP_CACHE
, sizeof(struct nhrp_cache
));
52 *p
= (struct nhrp_cache
){
53 .cur
.type
= NHRP_CACHE_INVALID
,
54 .new.type
= NHRP_CACHE_INVALID
,
55 .remote_addr
= key
->remote_addr
,
58 NOTIFIER_LIST_INITIALIZER(&p
->notifier_list
),
60 nhrp_cache_counts
[p
->cur
.type
]++;
66 static void nhrp_cache_free(struct nhrp_cache
*c
)
68 struct nhrp_interface
*nifp
= c
->ifp
->info
;
70 zassert(c
->cur
.type
== NHRP_CACHE_INVALID
&& c
->cur
.peer
== NULL
);
71 zassert(c
->new.type
== NHRP_CACHE_INVALID
&& c
->new.peer
== NULL
);
72 nhrp_cache_counts
[c
->cur
.type
]--;
73 notifier_call(&c
->notifier_list
, NOTIFY_CACHE_DELETE
);
74 zassert(!notifier_active(&c
->notifier_list
));
75 hash_release(nifp
->cache_hash
, c
);
76 XFREE(MTYPE_NHRP_CACHE
, c
);
79 struct nhrp_cache
*nhrp_cache_get(struct interface
*ifp
,
80 union sockunion
*remote_addr
, int create
)
82 struct nhrp_interface
*nifp
= ifp
->info
;
83 struct nhrp_cache key
;
85 if (!nifp
->cache_hash
) {
87 hash_create(nhrp_cache_protocol_key
,
88 nhrp_cache_protocol_cmp
, "NHRP Cache");
89 if (!nifp
->cache_hash
)
93 key
.remote_addr
= *remote_addr
;
96 return hash_get(nifp
->cache_hash
, &key
,
97 create
? nhrp_cache_alloc
: NULL
);
100 static int nhrp_cache_do_free(struct thread
*t
)
102 struct nhrp_cache
*c
= THREAD_ARG(t
);
108 static int nhrp_cache_do_timeout(struct thread
*t
)
110 struct nhrp_cache
*c
= THREAD_ARG(t
);
112 if (c
->cur
.type
!= NHRP_CACHE_INVALID
)
113 nhrp_cache_update_binding(c
, c
->cur
.type
, -1, NULL
, 0, NULL
);
117 static void nhrp_cache_update_route(struct nhrp_cache
*c
)
120 struct nhrp_peer
*p
= c
->cur
.peer
;
122 sockunion2hostprefix(&c
->remote_addr
, &pfx
);
124 if (p
&& nhrp_peer_check(p
, 1)) {
125 netlink_update_binding(p
->ifp
, &c
->remote_addr
,
126 &p
->vc
->remote
.nbma
);
127 nhrp_route_announce(1, c
->cur
.type
, &pfx
, c
->ifp
, NULL
,
129 if (c
->cur
.type
>= NHRP_CACHE_DYNAMIC
) {
130 nhrp_route_update_nhrp(&pfx
, c
->ifp
);
131 c
->nhrp_route_installed
= 1;
132 } else if (c
->nhrp_route_installed
) {
133 nhrp_route_update_nhrp(&pfx
, NULL
);
134 c
->nhrp_route_installed
= 0;
136 if (!c
->route_installed
) {
137 notifier_call(&c
->notifier_list
, NOTIFY_CACHE_UP
);
138 c
->route_installed
= 1;
141 if (c
->nhrp_route_installed
) {
142 nhrp_route_update_nhrp(&pfx
, NULL
);
143 c
->nhrp_route_installed
= 0;
145 if (c
->route_installed
) {
146 sockunion2hostprefix(&c
->remote_addr
, &pfx
);
147 notifier_call(&c
->notifier_list
, NOTIFY_CACHE_DOWN
);
148 nhrp_route_announce(0, c
->cur
.type
, &pfx
, NULL
, NULL
,
150 c
->route_installed
= 0;
155 static void nhrp_cache_peer_notifier(struct notifier_block
*n
,
158 struct nhrp_cache
*c
=
159 container_of(n
, struct nhrp_cache
, peer_notifier
);
163 nhrp_cache_update_route(c
);
165 case NOTIFY_PEER_DOWN
:
166 case NOTIFY_PEER_IFCONFIG_CHANGED
:
167 notifier_call(&c
->notifier_list
, NOTIFY_CACHE_DOWN
);
168 nhrp_cache_update_binding(c
, c
->cur
.type
, -1, NULL
, 0, NULL
);
170 case NOTIFY_PEER_NBMA_CHANGING
:
171 if (c
->cur
.type
== NHRP_CACHE_DYNAMIC
)
172 c
->cur
.peer
->vc
->abort_migration
= 1;
177 static void nhrp_cache_reset_new(struct nhrp_cache
*c
)
179 THREAD_OFF(c
->t_auth
);
180 if (list_hashed(&c
->newpeer_notifier
.notifier_entry
))
181 nhrp_peer_notify_del(c
->new.peer
, &c
->newpeer_notifier
);
182 nhrp_peer_unref(c
->new.peer
);
183 memset(&c
->new, 0, sizeof(c
->new));
184 c
->new.type
= NHRP_CACHE_INVALID
;
187 static void nhrp_cache_update_timers(struct nhrp_cache
*c
)
189 THREAD_OFF(c
->t_timeout
);
191 switch (c
->cur
.type
) {
192 case NHRP_CACHE_INVALID
:
194 thread_add_timer_msec(master
, nhrp_cache_do_free
, c
, 10,
199 thread_add_timer(master
, nhrp_cache_do_timeout
, c
,
200 c
->cur
.expires
- monotime(NULL
),
206 static void nhrp_cache_authorize_binding(struct nhrp_reqid
*r
, void *arg
)
208 struct nhrp_cache
*c
= container_of(r
, struct nhrp_cache
, eventid
);
209 char buf
[SU_ADDRSTRLEN
];
211 debugf(NHRP_DEBUG_COMMON
, "cache: %s %s: %s", c
->ifp
->name
,
212 sockunion2str(&c
->remote_addr
, buf
, sizeof buf
),
215 nhrp_reqid_free(&nhrp_event_reqid
, r
);
217 if (arg
&& strcmp(arg
, "accept") == 0) {
219 netlink_update_binding(c
->cur
.peer
->ifp
,
220 &c
->remote_addr
, NULL
);
221 nhrp_peer_notify_del(c
->cur
.peer
, &c
->peer_notifier
);
222 nhrp_peer_unref(c
->cur
.peer
);
224 nhrp_cache_counts
[c
->cur
.type
]--;
225 nhrp_cache_counts
[c
->new.type
]++;
227 c
->cur
.peer
= nhrp_peer_ref(c
->cur
.peer
);
228 nhrp_cache_reset_new(c
);
230 nhrp_peer_notify_add(c
->cur
.peer
, &c
->peer_notifier
,
231 nhrp_cache_peer_notifier
);
232 nhrp_cache_update_route(c
);
233 notifier_call(&c
->notifier_list
, NOTIFY_CACHE_BINDING_CHANGE
);
235 nhrp_cache_reset_new(c
);
238 nhrp_cache_update_timers(c
);
241 static int nhrp_cache_do_auth_timeout(struct thread
*t
)
243 struct nhrp_cache
*c
= THREAD_ARG(t
);
245 nhrp_cache_authorize_binding(&c
->eventid
, (void *)"timeout");
249 static void nhrp_cache_newpeer_notifier(struct notifier_block
*n
,
252 struct nhrp_cache
*c
=
253 container_of(n
, struct nhrp_cache
, newpeer_notifier
);
257 if (nhrp_peer_check(c
->new.peer
, 1)) {
258 evmgr_notify("authorize-binding", c
,
259 nhrp_cache_authorize_binding
);
260 thread_add_timer(master
, nhrp_cache_do_auth_timeout
, c
,
264 case NOTIFY_PEER_DOWN
:
265 case NOTIFY_PEER_IFCONFIG_CHANGED
:
266 nhrp_cache_reset_new(c
);
271 int nhrp_cache_update_binding(struct nhrp_cache
*c
, enum nhrp_cache_type type
,
272 int holding_time
, struct nhrp_peer
*p
,
273 uint32_t mtu
, union sockunion
*nbma_oa
)
275 if (c
->cur
.type
> type
|| c
->new.type
> type
) {
281 switch (sockunion_family(&c
->remote_addr
)) {
283 if (mtu
< 576 || mtu
>= 1500)
285 /* Opennhrp announces nbma mtu, but we use protocol mtu.
286 * This heuristic tries to fix up it. */
288 mtu
= (mtu
& -16) - 80;
295 nhrp_cache_reset_new(c
);
296 if (c
->cur
.type
== type
&& c
->cur
.peer
== p
&& c
->cur
.mtu
== mtu
) {
297 if (holding_time
> 0)
298 c
->cur
.expires
= monotime(NULL
) + holding_time
;
300 c
->cur
.remote_nbma_natoa
= *nbma_oa
;
302 memset(&c
->cur
.remote_nbma_natoa
, 0,
303 sizeof c
->cur
.remote_nbma_natoa
);
310 c
->new.remote_nbma_natoa
= *nbma_oa
;
312 if (holding_time
> 0)
313 c
->new.expires
= monotime(NULL
) + holding_time
;
314 else if (holding_time
< 0)
315 nhrp_cache_reset_new(c
);
317 if (c
->new.type
== NHRP_CACHE_INVALID
318 || c
->new.type
>= NHRP_CACHE_STATIC
|| c
->map
) {
319 nhrp_cache_authorize_binding(&c
->eventid
,
322 nhrp_peer_notify_add(c
->new.peer
, &c
->newpeer_notifier
,
323 nhrp_cache_newpeer_notifier
);
324 nhrp_cache_newpeer_notifier(&c
->newpeer_notifier
,
326 thread_add_timer(master
, nhrp_cache_do_auth_timeout
, c
,
330 nhrp_cache_update_timers(c
);
335 void nhrp_cache_set_used(struct nhrp_cache
*c
, int used
)
339 notifier_call(&c
->notifier_list
, NOTIFY_CACHE_USED
);
342 struct nhrp_cache_iterator_ctx
{
343 void (*cb
)(struct nhrp_cache
*, void *);
347 static void nhrp_cache_iterator(struct hash_backet
*b
, void *ctx
)
349 struct nhrp_cache_iterator_ctx
*ic
= ctx
;
350 ic
->cb(b
->data
, ic
->ctx
);
353 void nhrp_cache_foreach(struct interface
*ifp
,
354 void (*cb
)(struct nhrp_cache
*, void *), void *ctx
)
356 struct nhrp_interface
*nifp
= ifp
->info
;
357 struct nhrp_cache_iterator_ctx ic
= {
358 .cb
= cb
, .ctx
= ctx
,
361 if (nifp
->cache_hash
)
362 hash_iterate(nifp
->cache_hash
, nhrp_cache_iterator
, &ic
);
365 void nhrp_cache_notify_add(struct nhrp_cache
*c
, struct notifier_block
*n
,
368 notifier_add(n
, &c
->notifier_list
, fn
);
371 void nhrp_cache_notify_del(struct nhrp_cache
*c
, struct notifier_block
*n
)