| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#include "ovpn.h" |
| 11 |
#include "bind.h" |
| 12 |
#include "peer.h" |
| 13 |
|
| 14 |
#include <linux/in.h> |
| 15 |
#include <linux/in6.h> |
| 16 |
#include <linux/socket.h> |
| 17 |
#include <linux/types.h> |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
struct ovpn_bind *ovpn_bind_from_sockaddr(const struct sockaddr_storage *ss) |
| 25 |
{ |
| 26 |
struct ovpn_bind *bind; |
| 27 |
size_t sa_len; |
| 28 |
|
| 29 |
if (ss->ss_family == AF_INET) |
| 30 |
sa_len = sizeof(struct sockaddr_in); |
| 31 |
else if (ss->ss_family == AF_INET6) |
| 32 |
sa_len = sizeof(struct sockaddr_in6); |
| 33 |
else |
| 34 |
return ERR_PTR(-EAFNOSUPPORT); |
| 35 |
|
| 36 |
bind = kzalloc(sizeof(*bind), GFP_ATOMIC); |
| 37 |
if (unlikely(!bind)) |
| 38 |
return ERR_PTR(-ENOMEM); |
| 39 |
|
| 40 |
memcpy(&bind->sa, ss, sa_len); |
| 41 |
|
| 42 |
return bind; |
| 43 |
} |
| 44 |
|
| 45 |
static void ovpn_bind_release_rcu(struct rcu_head *head) |
| 46 |
{ |
| 47 |
struct ovpn_bind *bind = container_of(head, struct ovpn_bind, rcu); |
| 48 |
|
| 49 |
kfree(bind); |
| 50 |
} |
| 51 |
|
| 52 |
void ovpn_bind_reset(struct ovpn_peer *peer, struct ovpn_bind *new) |
| 53 |
{ |
| 54 |
struct ovpn_bind *old; |
| 55 |
|
| 56 |
spin_lock_bh(&peer->lock); |
| 57 |
old = rcu_replace_pointer(peer->bind, new, true); |
| 58 |
spin_unlock_bh(&peer->lock); |
| 59 |
|
| 60 |
if (old) |
| 61 |
call_rcu(&old->rcu, ovpn_bind_release_rcu); |
| 62 |
} |