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
]++;
65 static void nhrp_cache_free(struct nhrp_cache
*c
)
67 struct nhrp_interface
*nifp
= c
->ifp
->info
;
69 zassert(c
->cur
.type
== NHRP_CACHE_INVALID
&& c
->cur
.peer
== NULL
);
70 zassert(c
->new.type
== NHRP_CACHE_INVALID
&& c
->new.peer
== NULL
);
71 nhrp_cache_counts
[c
->cur
.type
]--;
72 notifier_call(&c
->notifier_list
, NOTIFY_CACHE_DELETE
);
73 zassert(!notifier_active(&c
->notifier_list
));
74 hash_release(nifp
->cache_hash
, c
);
75 XFREE(MTYPE_NHRP_CACHE
, c
);
78 struct nhrp_cache
*nhrp_cache_get(struct interface
*ifp
,
79 union sockunion
*remote_addr
, int create
)
81 struct nhrp_interface
*nifp
= ifp
->info
;
82 struct nhrp_cache key
;
84 if (!nifp
->cache_hash
) {
86 hash_create(nhrp_cache_protocol_key
,
87 nhrp_cache_protocol_cmp
, "NHRP Cache");
88 if (!nifp
->cache_hash
)
92 key
.remote_addr
= *remote_addr
;
95 return hash_get(nifp
->cache_hash
, &key
,
96 create
? nhrp_cache_alloc
: NULL
);
99 static int nhrp_cache_do_free(struct thread
*t
)
101 struct nhrp_cache
*c
= THREAD_ARG(t
);
107 static int nhrp_cache_do_timeout(struct thread
*t
)
109 struct nhrp_cache
*c
= THREAD_ARG(t
);
111 if (c
->cur
.type
!= NHRP_CACHE_INVALID
)
112 nhrp_cache_update_binding(c
, c
->cur
.type
, -1, NULL
, 0, NULL
);
116 static void nhrp_cache_update_route(struct nhrp_cache
*c
)
119 struct nhrp_peer
*p
= c
->cur
.peer
;
121 sockunion2hostprefix(&c
->remote_addr
, &pfx
);
123 if (p
&& nhrp_peer_check(p
, 1)) {
124 netlink_update_binding(p
->ifp
, &c
->remote_addr
,
125 &p
->vc
->remote
.nbma
);
126 nhrp_route_announce(1, c
->cur
.type
, &pfx
, c
->ifp
, NULL
,
128 if (c
->cur
.type
>= NHRP_CACHE_DYNAMIC
) {
129 nhrp_route_update_nhrp(&pfx
, c
->ifp
);
130 c
->nhrp_route_installed
= 1;
131 } else if (c
->nhrp_route_installed
) {
132 nhrp_route_update_nhrp(&pfx
, NULL
);
133 c
->nhrp_route_installed
= 0;
135 if (!c
->route_installed
) {
136 notifier_call(&c
->notifier_list
, NOTIFY_CACHE_UP
);
137 c
->route_installed
= 1;
140 if (c
->nhrp_route_installed
) {
141 nhrp_route_update_nhrp(&pfx
, NULL
);
142 c
->nhrp_route_installed
= 0;
144 if (c
->route_installed
) {
145 sockunion2hostprefix(&c
->remote_addr
, &pfx
);
146 notifier_call(&c
->notifier_list
, NOTIFY_CACHE_DOWN
);
147 nhrp_route_announce(0, c
->cur
.type
, &pfx
, NULL
, NULL
,
149 c
->route_installed
= 0;
154 static void nhrp_cache_peer_notifier(struct notifier_block
*n
,
157 struct nhrp_cache
*c
=
158 container_of(n
, struct nhrp_cache
, peer_notifier
);
162 nhrp_cache_update_route(c
);
164 case NOTIFY_PEER_DOWN
:
165 case NOTIFY_PEER_IFCONFIG_CHANGED
:
166 notifier_call(&c
->notifier_list
, NOTIFY_CACHE_DOWN
);
167 nhrp_cache_update_binding(c
, c
->cur
.type
, -1, NULL
, 0, NULL
);
169 case NOTIFY_PEER_NBMA_CHANGING
:
170 if (c
->cur
.type
== NHRP_CACHE_DYNAMIC
)
171 c
->cur
.peer
->vc
->abort_migration
= 1;
176 static void nhrp_cache_reset_new(struct nhrp_cache
*c
)
178 THREAD_OFF(c
->t_auth
);
179 if (list_hashed(&c
->newpeer_notifier
.notifier_entry
))
180 nhrp_peer_notify_del(c
->new.peer
, &c
->newpeer_notifier
);
181 nhrp_peer_unref(c
->new.peer
);
182 memset(&c
->new, 0, sizeof(c
->new));
183 c
->new.type
= NHRP_CACHE_INVALID
;
186 static void nhrp_cache_update_timers(struct nhrp_cache
*c
)
188 THREAD_OFF(c
->t_timeout
);
190 switch (c
->cur
.type
) {
191 case NHRP_CACHE_INVALID
:
193 thread_add_timer_msec(master
, nhrp_cache_do_free
, c
, 10,
198 thread_add_timer(master
, nhrp_cache_do_timeout
, c
,
199 c
->cur
.expires
- monotime(NULL
),
205 static void nhrp_cache_authorize_binding(struct nhrp_reqid
*r
, void *arg
)
207 struct nhrp_cache
*c
= container_of(r
, struct nhrp_cache
, eventid
);
208 char buf
[SU_ADDRSTRLEN
];
210 debugf(NHRP_DEBUG_COMMON
, "cache: %s %s: %s", c
->ifp
->name
,
211 sockunion2str(&c
->remote_addr
, buf
, sizeof buf
),
214 nhrp_reqid_free(&nhrp_event_reqid
, r
);
216 if (arg
&& strcmp(arg
, "accept") == 0) {
218 netlink_update_binding(c
->cur
.peer
->ifp
,
219 &c
->remote_addr
, NULL
);
220 nhrp_peer_notify_del(c
->cur
.peer
, &c
->peer_notifier
);
221 nhrp_peer_unref(c
->cur
.peer
);
223 nhrp_cache_counts
[c
->cur
.type
]--;
224 nhrp_cache_counts
[c
->new.type
]++;
226 c
->cur
.peer
= nhrp_peer_ref(c
->cur
.peer
);
227 nhrp_cache_reset_new(c
);
229 nhrp_peer_notify_add(c
->cur
.peer
, &c
->peer_notifier
,
230 nhrp_cache_peer_notifier
);
231 nhrp_cache_update_route(c
);
232 notifier_call(&c
->notifier_list
, NOTIFY_CACHE_BINDING_CHANGE
);
234 nhrp_cache_reset_new(c
);
237 nhrp_cache_update_timers(c
);
240 static int nhrp_cache_do_auth_timeout(struct thread
*t
)
242 struct nhrp_cache
*c
= THREAD_ARG(t
);
244 nhrp_cache_authorize_binding(&c
->eventid
, (void *)"timeout");
248 static void nhrp_cache_newpeer_notifier(struct notifier_block
*n
,
251 struct nhrp_cache
*c
=
252 container_of(n
, struct nhrp_cache
, newpeer_notifier
);
256 if (nhrp_peer_check(c
->new.peer
, 1)) {
257 evmgr_notify("authorize-binding", c
,
258 nhrp_cache_authorize_binding
);
259 thread_add_timer(master
, nhrp_cache_do_auth_timeout
, c
,
263 case NOTIFY_PEER_DOWN
:
264 case NOTIFY_PEER_IFCONFIG_CHANGED
:
265 nhrp_cache_reset_new(c
);
270 int nhrp_cache_update_binding(struct nhrp_cache
*c
, enum nhrp_cache_type type
,
271 int holding_time
, struct nhrp_peer
*p
,
272 uint32_t mtu
, union sockunion
*nbma_oa
)
274 if (c
->cur
.type
> type
|| c
->new.type
> type
) {
280 switch (sockunion_family(&c
->remote_addr
)) {
282 if (mtu
< 576 || mtu
>= 1500)
284 /* Opennhrp announces nbma mtu, but we use protocol mtu.
285 * This heuristic tries to fix up it. */
287 mtu
= (mtu
& -16) - 80;
294 nhrp_cache_reset_new(c
);
295 if (c
->cur
.type
== type
&& c
->cur
.peer
== p
&& c
->cur
.mtu
== mtu
) {
296 if (holding_time
> 0)
297 c
->cur
.expires
= monotime(NULL
) + holding_time
;
299 c
->cur
.remote_nbma_natoa
= *nbma_oa
;
301 memset(&c
->cur
.remote_nbma_natoa
, 0,
302 sizeof c
->cur
.remote_nbma_natoa
);
309 c
->new.remote_nbma_natoa
= *nbma_oa
;
311 if (holding_time
> 0)
312 c
->new.expires
= monotime(NULL
) + holding_time
;
313 else if (holding_time
< 0)
314 nhrp_cache_reset_new(c
);
316 if (c
->new.type
== NHRP_CACHE_INVALID
317 || c
->new.type
>= NHRP_CACHE_STATIC
|| c
->map
) {
318 nhrp_cache_authorize_binding(&c
->eventid
,
321 nhrp_peer_notify_add(c
->new.peer
, &c
->newpeer_notifier
,
322 nhrp_cache_newpeer_notifier
);
323 nhrp_cache_newpeer_notifier(&c
->newpeer_notifier
,
325 thread_add_timer(master
, nhrp_cache_do_auth_timeout
, c
,
329 nhrp_cache_update_timers(c
);
334 void nhrp_cache_set_used(struct nhrp_cache
*c
, int used
)
338 notifier_call(&c
->notifier_list
, NOTIFY_CACHE_USED
);
341 struct nhrp_cache_iterator_ctx
{
342 void (*cb
)(struct nhrp_cache
*, void *);
346 static void nhrp_cache_iterator(struct hash_backet
*b
, void *ctx
)
348 struct nhrp_cache_iterator_ctx
*ic
= ctx
;
349 ic
->cb(b
->data
, ic
->ctx
);
352 void nhrp_cache_foreach(struct interface
*ifp
,
353 void (*cb
)(struct nhrp_cache
*, void *), void *ctx
)
355 struct nhrp_interface
*nifp
= ifp
->info
;
356 struct nhrp_cache_iterator_ctx ic
= {
357 .cb
= cb
, .ctx
= ctx
,
360 if (nifp
->cache_hash
)
361 hash_iterate(nifp
->cache_hash
, nhrp_cache_iterator
, &ic
);
364 void nhrp_cache_notify_add(struct nhrp_cache
*c
, struct notifier_block
*n
,
367 notifier_add(n
, &c
->notifier_list
, fn
);
370 void nhrp_cache_notify_del(struct nhrp_cache
*c
, struct notifier_block
*n
)