提交 29c7cf96 编写于 作者: S Sridhar Samudrala 提交者: David S. Miller

[SCTP]: Handle address add/delete events in a more efficient way.

Currently in SCTP, we maintain a local address list by rebuilding the whole
list from the device list whenever we get a address add/delete event.

This patch fixes it by only adding/deleting the address for which we
receive the event.

Also removed the sctp_local_addr_lock() which is no longer needed as we
now use list_for_each_safe() to traverse this list. This fixes the bugs
in sctp_copy_laddrs_xxx() routines where we do copy_to_user() while
holding this lock.
Signed-off-by: NSridhar Samudrala <sri@us.ibm.com>
Signed-off-by: NDavid S. Miller <davem@davemloft.net>
上级 6931ba7c
...@@ -201,13 +201,12 @@ extern struct sctp_globals { ...@@ -201,13 +201,12 @@ extern struct sctp_globals {
struct sctp_bind_hashbucket *port_hashtable; struct sctp_bind_hashbucket *port_hashtable;
/* This is the global local address list. /* This is the global local address list.
* We actively maintain this complete list of interfaces on * We actively maintain this complete list of addresses on
* the system by catching routing events. * the system by catching address add/delete events.
* *
* It is a list of sctp_sockaddr_entry. * It is a list of sctp_sockaddr_entry.
*/ */
struct list_head local_addr_list; struct list_head local_addr_list;
spinlock_t local_addr_lock;
/* Flag to indicate if addip is enabled. */ /* Flag to indicate if addip is enabled. */
int addip_enable; int addip_enable;
...@@ -243,7 +242,6 @@ extern struct sctp_globals { ...@@ -243,7 +242,6 @@ extern struct sctp_globals {
#define sctp_port_alloc_lock (sctp_globals.port_alloc_lock) #define sctp_port_alloc_lock (sctp_globals.port_alloc_lock)
#define sctp_port_hashtable (sctp_globals.port_hashtable) #define sctp_port_hashtable (sctp_globals.port_hashtable)
#define sctp_local_addr_list (sctp_globals.local_addr_list) #define sctp_local_addr_list (sctp_globals.local_addr_list)
#define sctp_local_addr_lock (sctp_globals.local_addr_lock)
#define sctp_addip_enable (sctp_globals.addip_enable) #define sctp_addip_enable (sctp_globals.addip_enable)
#define sctp_prsctp_enable (sctp_globals.prsctp_enable) #define sctp_prsctp_enable (sctp_globals.prsctp_enable)
......
...@@ -78,8 +78,44 @@ ...@@ -78,8 +78,44 @@
#include <asm/uaccess.h> #include <asm/uaccess.h>
/* Event handler for inet6 address addition/deletion events. */
int sctp_inet6addr_event(struct notifier_block *this, unsigned long ev,
void *ptr)
{
struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
struct sctp_sockaddr_entry *addr;
struct list_head *pos, *temp;
switch (ev) {
case NETDEV_UP:
addr = kmalloc(sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC);
if (addr) {
addr->a.v6.sin6_family = AF_INET6;
addr->a.v6.sin6_port = 0;
memcpy(&addr->a.v6.sin6_addr, &ifa->addr,
sizeof(struct in6_addr));
addr->a.v6.sin6_scope_id = ifa->idev->dev->ifindex;
list_add_tail(&addr->list, &sctp_local_addr_list);
}
break;
case NETDEV_DOWN:
list_for_each_safe(pos, temp, &sctp_local_addr_list) {
addr = list_entry(pos, struct sctp_sockaddr_entry, list);
if (ipv6_addr_equal(&addr->a.v6.sin6_addr, &ifa->addr)) {
list_del(pos);
kfree(addr);
break;
}
}
break;
}
return NOTIFY_DONE;
}
static struct notifier_block sctp_inet6addr_notifier = { static struct notifier_block sctp_inet6addr_notifier = {
.notifier_call = sctp_inetaddr_event, .notifier_call = sctp_inet6addr_event,
}; };
/* ICMP error handler. */ /* ICMP error handler. */
......
...@@ -163,7 +163,7 @@ static void sctp_v4_copy_addrlist(struct list_head *addrlist, ...@@ -163,7 +163,7 @@ static void sctp_v4_copy_addrlist(struct list_head *addrlist,
/* Extract our IP addresses from the system and stash them in the /* Extract our IP addresses from the system and stash them in the
* protocol structure. * protocol structure.
*/ */
static void __sctp_get_local_addr_list(void) static void sctp_get_local_addr_list(void)
{ {
struct net_device *dev; struct net_device *dev;
struct list_head *pos; struct list_head *pos;
...@@ -179,17 +179,8 @@ static void __sctp_get_local_addr_list(void) ...@@ -179,17 +179,8 @@ static void __sctp_get_local_addr_list(void)
read_unlock(&dev_base_lock); read_unlock(&dev_base_lock);
} }
static void sctp_get_local_addr_list(void)
{
unsigned long flags;
sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
__sctp_get_local_addr_list();
sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
}
/* Free the existing local addresses. */ /* Free the existing local addresses. */
static void __sctp_free_local_addr_list(void) static void sctp_free_local_addr_list(void)
{ {
struct sctp_sockaddr_entry *addr; struct sctp_sockaddr_entry *addr;
struct list_head *pos, *temp; struct list_head *pos, *temp;
...@@ -201,27 +192,15 @@ static void __sctp_free_local_addr_list(void) ...@@ -201,27 +192,15 @@ static void __sctp_free_local_addr_list(void)
} }
} }
/* Free the existing local addresses. */
static void sctp_free_local_addr_list(void)
{
unsigned long flags;
sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags);
__sctp_free_local_addr_list();
sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
}
/* Copy the local addresses which are valid for 'scope' into 'bp'. */ /* Copy the local addresses which are valid for 'scope' into 'bp'. */
int sctp_copy_local_addr_list(struct sctp_bind_addr *bp, sctp_scope_t scope, int sctp_copy_local_addr_list(struct sctp_bind_addr *bp, sctp_scope_t scope,
gfp_t gfp, int copy_flags) gfp_t gfp, int copy_flags)
{ {
struct sctp_sockaddr_entry *addr; struct sctp_sockaddr_entry *addr;
int error = 0; int error = 0;
struct list_head *pos; struct list_head *pos, *temp;
unsigned long flags;
sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags); list_for_each_safe(pos, temp, &sctp_local_addr_list) {
list_for_each(pos, &sctp_local_addr_list) {
addr = list_entry(pos, struct sctp_sockaddr_entry, list); addr = list_entry(pos, struct sctp_sockaddr_entry, list);
if (sctp_in_scope(&addr->a, scope)) { if (sctp_in_scope(&addr->a, scope)) {
/* Now that the address is in scope, check to see if /* Now that the address is in scope, check to see if
...@@ -242,7 +221,6 @@ int sctp_copy_local_addr_list(struct sctp_bind_addr *bp, sctp_scope_t scope, ...@@ -242,7 +221,6 @@ int sctp_copy_local_addr_list(struct sctp_bind_addr *bp, sctp_scope_t scope,
} }
end_copy: end_copy:
sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
return error; return error;
} }
...@@ -622,18 +600,36 @@ static void sctp_v4_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr) ...@@ -622,18 +600,36 @@ static void sctp_v4_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
seq_printf(seq, "%d.%d.%d.%d ", NIPQUAD(addr->v4.sin_addr)); seq_printf(seq, "%d.%d.%d.%d ", NIPQUAD(addr->v4.sin_addr));
} }
/* Event handler for inet address addition/deletion events. /* Event handler for inet address addition/deletion events. */
* Basically, whenever there is an event, we re-build our local address list.
*/
int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev, int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev,
void *ptr) void *ptr)
{ {
unsigned long flags; struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
struct sctp_sockaddr_entry *addr;
struct list_head *pos, *temp;
sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags); switch (ev) {
__sctp_free_local_addr_list(); case NETDEV_UP:
__sctp_get_local_addr_list(); addr = kmalloc(sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC);
sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags); if (addr) {
addr->a.v4.sin_family = AF_INET;
addr->a.v4.sin_port = 0;
addr->a.v4.sin_addr.s_addr = ifa->ifa_local;
list_add_tail(&addr->list, &sctp_local_addr_list);
}
break;
case NETDEV_DOWN:
list_for_each_safe(pos, temp, &sctp_local_addr_list) {
addr = list_entry(pos, struct sctp_sockaddr_entry, list);
if (addr->a.v4.sin_addr.s_addr == ifa->ifa_local) {
list_del(pos);
kfree(addr);
break;
}
}
break;
}
return NOTIFY_DONE; return NOTIFY_DONE;
} }
...@@ -1172,13 +1168,12 @@ SCTP_STATIC __init int sctp_init(void) ...@@ -1172,13 +1168,12 @@ SCTP_STATIC __init int sctp_init(void)
/* Initialize the local address list. */ /* Initialize the local address list. */
INIT_LIST_HEAD(&sctp_local_addr_list); INIT_LIST_HEAD(&sctp_local_addr_list);
spin_lock_init(&sctp_local_addr_lock);
sctp_get_local_addr_list();
/* Register notifier for inet address additions/deletions. */ /* Register notifier for inet address additions/deletions. */
register_inetaddr_notifier(&sctp_inetaddr_notifier); register_inetaddr_notifier(&sctp_inetaddr_notifier);
sctp_get_local_addr_list();
__unsafe(THIS_MODULE); __unsafe(THIS_MODULE);
status = 0; status = 0;
out: out:
......
...@@ -3821,10 +3821,9 @@ static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len, ...@@ -3821,10 +3821,9 @@ static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len,
sctp_assoc_t id; sctp_assoc_t id;
struct sctp_bind_addr *bp; struct sctp_bind_addr *bp;
struct sctp_association *asoc; struct sctp_association *asoc;
struct list_head *pos; struct list_head *pos, *temp;
struct sctp_sockaddr_entry *addr; struct sctp_sockaddr_entry *addr;
rwlock_t *addr_lock; rwlock_t *addr_lock;
unsigned long flags;
int cnt = 0; int cnt = 0;
if (len != sizeof(sctp_assoc_t)) if (len != sizeof(sctp_assoc_t))
...@@ -3859,8 +3858,7 @@ static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len, ...@@ -3859,8 +3858,7 @@ static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len,
addr = list_entry(bp->address_list.next, addr = list_entry(bp->address_list.next,
struct sctp_sockaddr_entry, list); struct sctp_sockaddr_entry, list);
if (sctp_is_any(&addr->a)) { if (sctp_is_any(&addr->a)) {
sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags); list_for_each_safe(pos, temp, &sctp_local_addr_list) {
list_for_each(pos, &sctp_local_addr_list) {
addr = list_entry(pos, addr = list_entry(pos,
struct sctp_sockaddr_entry, struct sctp_sockaddr_entry,
list); list);
...@@ -3869,8 +3867,6 @@ static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len, ...@@ -3869,8 +3867,6 @@ static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len,
continue; continue;
cnt++; cnt++;
} }
sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
flags);
} else { } else {
cnt = 1; cnt = 1;
} }
...@@ -3892,15 +3888,13 @@ static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len, ...@@ -3892,15 +3888,13 @@ static int sctp_getsockopt_local_addrs_num_old(struct sock *sk, int len,
static int sctp_copy_laddrs_to_user_old(struct sock *sk, __u16 port, int max_addrs, static int sctp_copy_laddrs_to_user_old(struct sock *sk, __u16 port, int max_addrs,
void __user *to) void __user *to)
{ {
struct list_head *pos; struct list_head *pos, *next;
struct sctp_sockaddr_entry *addr; struct sctp_sockaddr_entry *addr;
unsigned long flags;
union sctp_addr temp; union sctp_addr temp;
int cnt = 0; int cnt = 0;
int addrlen; int addrlen;
sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags); list_for_each_safe(pos, next, &sctp_local_addr_list) {
list_for_each(pos, &sctp_local_addr_list) {
addr = list_entry(pos, struct sctp_sockaddr_entry, list); addr = list_entry(pos, struct sctp_sockaddr_entry, list);
if ((PF_INET == sk->sk_family) && if ((PF_INET == sk->sk_family) &&
(AF_INET6 == addr->a.sa.sa_family)) (AF_INET6 == addr->a.sa.sa_family))
...@@ -3909,16 +3903,13 @@ static int sctp_copy_laddrs_to_user_old(struct sock *sk, __u16 port, int max_add ...@@ -3909,16 +3903,13 @@ static int sctp_copy_laddrs_to_user_old(struct sock *sk, __u16 port, int max_add
sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk), sctp_get_pf_specific(sk->sk_family)->addr_v4map(sctp_sk(sk),
&temp); &temp);
addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len; addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
if (copy_to_user(to, &temp, addrlen)) { if (copy_to_user(to, &temp, addrlen))
sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
flags);
return -EFAULT; return -EFAULT;
}
to += addrlen; to += addrlen;
cnt ++; cnt ++;
if (cnt >= max_addrs) break; if (cnt >= max_addrs) break;
} }
sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
return cnt; return cnt;
} }
...@@ -3926,15 +3917,13 @@ static int sctp_copy_laddrs_to_user_old(struct sock *sk, __u16 port, int max_add ...@@ -3926,15 +3917,13 @@ static int sctp_copy_laddrs_to_user_old(struct sock *sk, __u16 port, int max_add
static int sctp_copy_laddrs_to_user(struct sock *sk, __u16 port, static int sctp_copy_laddrs_to_user(struct sock *sk, __u16 port,
void __user **to, size_t space_left) void __user **to, size_t space_left)
{ {
struct list_head *pos; struct list_head *pos, *next;
struct sctp_sockaddr_entry *addr; struct sctp_sockaddr_entry *addr;
unsigned long flags;
union sctp_addr temp; union sctp_addr temp;
int cnt = 0; int cnt = 0;
int addrlen; int addrlen;
sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags); list_for_each_safe(pos, next, &sctp_local_addr_list) {
list_for_each(pos, &sctp_local_addr_list) {
addr = list_entry(pos, struct sctp_sockaddr_entry, list); addr = list_entry(pos, struct sctp_sockaddr_entry, list);
if ((PF_INET == sk->sk_family) && if ((PF_INET == sk->sk_family) &&
(AF_INET6 == addr->a.sa.sa_family)) (AF_INET6 == addr->a.sa.sa_family))
...@@ -3945,16 +3934,13 @@ static int sctp_copy_laddrs_to_user(struct sock *sk, __u16 port, ...@@ -3945,16 +3934,13 @@ static int sctp_copy_laddrs_to_user(struct sock *sk, __u16 port,
addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len; addrlen = sctp_get_af_specific(temp.sa.sa_family)->sockaddr_len;
if(space_left<addrlen) if(space_left<addrlen)
return -ENOMEM; return -ENOMEM;
if (copy_to_user(*to, &temp, addrlen)) { if (copy_to_user(*to, &temp, addrlen))
sctp_spin_unlock_irqrestore(&sctp_local_addr_lock,
flags);
return -EFAULT; return -EFAULT;
}
*to += addrlen; *to += addrlen;
cnt ++; cnt ++;
space_left -= addrlen; space_left -= addrlen;
} }
sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags);
return cnt; return cnt;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册