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
,
57 .notifier_list
= NOTIFIER_LIST_INITIALIZER(&p
->notifier_list
),
59 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
, union sockunion
*remote_addr
, int create
)
80 struct nhrp_interface
*nifp
= ifp
->info
;
81 struct nhrp_cache key
;
83 if (!nifp
->cache_hash
) {
84 nifp
->cache_hash
= hash_create(nhrp_cache_protocol_key
, nhrp_cache_protocol_cmp
, NULL
);
85 if (!nifp
->cache_hash
)
89 key
.remote_addr
= *remote_addr
;
92 return hash_get(nifp
->cache_hash
, &key
, create
? nhrp_cache_alloc
: NULL
);
95 static int nhrp_cache_do_free(struct thread
*t
)
97 struct nhrp_cache
*c
= THREAD_ARG(t
);
103 static int nhrp_cache_do_timeout(struct thread
*t
)
105 struct nhrp_cache
*c
= THREAD_ARG(t
);
107 if (c
->cur
.type
!= NHRP_CACHE_INVALID
)
108 nhrp_cache_update_binding(c
, c
->cur
.type
, -1, NULL
, 0, NULL
);
112 static void nhrp_cache_update_route(struct nhrp_cache
*c
)
115 struct nhrp_peer
*p
= c
->cur
.peer
;
117 sockunion2hostprefix(&c
->remote_addr
, &pfx
);
119 if (p
&& nhrp_peer_check(p
, 1)) {
120 netlink_update_binding(p
->ifp
, &c
->remote_addr
, &p
->vc
->remote
.nbma
);
121 nhrp_route_announce(1, c
->cur
.type
, &pfx
, c
->ifp
, NULL
, c
->cur
.mtu
);
122 if (c
->cur
.type
>= NHRP_CACHE_DYNAMIC
) {
123 nhrp_route_update_nhrp(&pfx
, c
->ifp
);
124 c
->nhrp_route_installed
= 1;
125 } else if (c
->nhrp_route_installed
) {
126 nhrp_route_update_nhrp(&pfx
, NULL
);
127 c
->nhrp_route_installed
= 0;
129 if (!c
->route_installed
) {
130 notifier_call(&c
->notifier_list
, NOTIFY_CACHE_UP
);
131 c
->route_installed
= 1;
134 if (c
->nhrp_route_installed
) {
135 nhrp_route_update_nhrp(&pfx
, NULL
);
136 c
->nhrp_route_installed
= 0;
138 if (c
->route_installed
) {
139 sockunion2hostprefix(&c
->remote_addr
, &pfx
);
140 notifier_call(&c
->notifier_list
, NOTIFY_CACHE_DOWN
);
141 nhrp_route_announce(0, c
->cur
.type
, &pfx
, NULL
, NULL
, 0);
142 c
->route_installed
= 0;
147 static void nhrp_cache_peer_notifier(struct notifier_block
*n
, unsigned long cmd
)
149 struct nhrp_cache
*c
= container_of(n
, struct nhrp_cache
, peer_notifier
);
153 nhrp_cache_update_route(c
);
155 case NOTIFY_PEER_DOWN
:
156 case NOTIFY_PEER_IFCONFIG_CHANGED
:
157 notifier_call(&c
->notifier_list
, NOTIFY_CACHE_DOWN
);
158 nhrp_cache_update_binding(c
, c
->cur
.type
, -1, NULL
, 0, NULL
);
160 case NOTIFY_PEER_NBMA_CHANGING
:
161 if (c
->cur
.type
== NHRP_CACHE_DYNAMIC
)
162 c
->cur
.peer
->vc
->abort_migration
= 1;
167 static void nhrp_cache_reset_new(struct nhrp_cache
*c
)
169 THREAD_OFF(c
->t_auth
);
170 if (list_hashed(&c
->newpeer_notifier
.notifier_entry
))
171 nhrp_peer_notify_del(c
->new.peer
, &c
->newpeer_notifier
);
172 nhrp_peer_unref(c
->new.peer
);
173 memset(&c
->new, 0, sizeof(c
->new));
174 c
->new.type
= NHRP_CACHE_INVALID
;
177 static void nhrp_cache_update_timers(struct nhrp_cache
*c
)
179 THREAD_OFF(c
->t_timeout
);
181 switch (c
->cur
.type
) {
182 case NHRP_CACHE_INVALID
:
184 thread_add_timer_msec(master
, nhrp_cache_do_free
, c
,
189 thread_add_timer(master
, nhrp_cache_do_timeout
, c
,
190 c
->cur
.expires
- monotime(NULL
),
196 static void nhrp_cache_authorize_binding(struct nhrp_reqid
*r
, void *arg
)
198 struct nhrp_cache
*c
= container_of(r
, struct nhrp_cache
, eventid
);
199 char buf
[SU_ADDRSTRLEN
];
201 debugf(NHRP_DEBUG_COMMON
, "cache: %s %s: %s",
202 c
->ifp
->name
, sockunion2str(&c
->remote_addr
, buf
, sizeof buf
),
205 nhrp_reqid_free(&nhrp_event_reqid
, r
);
207 if (arg
&& strcmp(arg
, "accept") == 0) {
209 netlink_update_binding(c
->cur
.peer
->ifp
, &c
->remote_addr
, NULL
);
210 nhrp_peer_notify_del(c
->cur
.peer
, &c
->peer_notifier
);
211 nhrp_peer_unref(c
->cur
.peer
);
213 nhrp_cache_counts
[c
->cur
.type
]--;
214 nhrp_cache_counts
[c
->new.type
]++;
216 c
->cur
.peer
= nhrp_peer_ref(c
->cur
.peer
);
217 nhrp_cache_reset_new(c
);
219 nhrp_peer_notify_add(c
->cur
.peer
, &c
->peer_notifier
, nhrp_cache_peer_notifier
);
220 nhrp_cache_update_route(c
);
221 notifier_call(&c
->notifier_list
, NOTIFY_CACHE_BINDING_CHANGE
);
223 nhrp_cache_reset_new(c
);
226 nhrp_cache_update_timers(c
);
229 static int nhrp_cache_do_auth_timeout(struct thread
*t
)
231 struct nhrp_cache
*c
= THREAD_ARG(t
);
233 nhrp_cache_authorize_binding(&c
->eventid
, (void *) "timeout");
237 static void nhrp_cache_newpeer_notifier(struct notifier_block
*n
, unsigned long cmd
)
239 struct nhrp_cache
*c
= container_of(n
, struct nhrp_cache
, newpeer_notifier
);
243 if (nhrp_peer_check(c
->new.peer
, 1)) {
244 evmgr_notify("authorize-binding", c
, nhrp_cache_authorize_binding
);
245 thread_add_timer(master
, nhrp_cache_do_auth_timeout
,
249 case NOTIFY_PEER_DOWN
:
250 case NOTIFY_PEER_IFCONFIG_CHANGED
:
251 nhrp_cache_reset_new(c
);
256 int nhrp_cache_update_binding(struct nhrp_cache
*c
, enum nhrp_cache_type type
, int holding_time
, struct nhrp_peer
*p
, uint32_t mtu
, union sockunion
*nbma_oa
)
258 if (c
->cur
.type
> type
|| c
->new.type
> type
) {
264 switch (sockunion_family(&c
->remote_addr
)) {
266 if (mtu
< 576 || mtu
>= 1500)
268 /* Opennhrp announces nbma mtu, but we use protocol mtu.
269 * This heuristic tries to fix up it. */
270 if (mtu
> 1420) mtu
= (mtu
& -16) - 80;
277 nhrp_cache_reset_new(c
);
278 if (c
->cur
.type
== type
&& c
->cur
.peer
== p
&& c
->cur
.mtu
== mtu
) {
279 if (holding_time
> 0) c
->cur
.expires
= monotime(NULL
) + holding_time
;
280 if (nbma_oa
) c
->cur
.remote_nbma_natoa
= *nbma_oa
;
281 else memset(&c
->cur
.remote_nbma_natoa
, 0, sizeof c
->cur
.remote_nbma_natoa
);
287 if (nbma_oa
) c
->new.remote_nbma_natoa
= *nbma_oa
;
289 if (holding_time
> 0)
290 c
->new.expires
= monotime(NULL
) + holding_time
;
291 else if (holding_time
< 0)
292 nhrp_cache_reset_new(c
);
294 if (c
->new.type
== NHRP_CACHE_INVALID
||
295 c
->new.type
>= NHRP_CACHE_STATIC
||
297 nhrp_cache_authorize_binding(&c
->eventid
, (void *) "accept");
299 nhrp_peer_notify_add(c
->new.peer
, &c
->newpeer_notifier
, nhrp_cache_newpeer_notifier
);
300 nhrp_cache_newpeer_notifier(&c
->newpeer_notifier
, NOTIFY_PEER_UP
);
301 thread_add_timer(master
, nhrp_cache_do_auth_timeout
,
305 nhrp_cache_update_timers(c
);
310 void nhrp_cache_set_used(struct nhrp_cache
*c
, int used
)
314 notifier_call(&c
->notifier_list
, NOTIFY_CACHE_USED
);
317 struct nhrp_cache_iterator_ctx
{
318 void (*cb
)(struct nhrp_cache
*, void *);
322 static void nhrp_cache_iterator(struct hash_backet
*b
, void *ctx
)
324 struct nhrp_cache_iterator_ctx
*ic
= ctx
;
325 ic
->cb(b
->data
, ic
->ctx
);
328 void nhrp_cache_foreach(struct interface
*ifp
, void (*cb
)(struct nhrp_cache
*, void *), void *ctx
)
330 struct nhrp_interface
*nifp
= ifp
->info
;
331 struct nhrp_cache_iterator_ctx ic
= {
336 if (nifp
->cache_hash
)
337 hash_iterate(nifp
->cache_hash
, nhrp_cache_iterator
, &ic
);
340 void nhrp_cache_notify_add(struct nhrp_cache
*c
, struct notifier_block
*n
, notifier_fn_t fn
)
342 notifier_add(n
, &c
->notifier_list
, fn
);
345 void nhrp_cache_notify_del(struct nhrp_cache
*c
, struct notifier_block
*n
)