Skip to content

Commit

Permalink
ipv6: avoid dev_hold()/dev_put() in rawv6_bind()
Browse files Browse the repository at this point in the history
Using RCU helps not touching device refcount in rawv6_bind()

Signed-off-by: Eric Dumazet <eric.dumazet@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Eric Dumazet authored and David S. Miller committed Nov 8, 2009
1 parent 18294ad commit fd5c002
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions net/ipv6/raw.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,15 @@ static int rawv6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)

/* Raw sockets are IPv6 only */
if (addr_type == IPV6_ADDR_MAPPED)
return(-EADDRNOTAVAIL);
return -EADDRNOTAVAIL;

lock_sock(sk);

err = -EINVAL;
if (sk->sk_state != TCP_CLOSE)
goto out;

rcu_read_lock();
/* Check if the address belongs to the host. */
if (addr_type != IPV6_ADDR_ANY) {
struct net_device *dev = NULL;
Expand All @@ -272,13 +273,13 @@ static int rawv6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)

/* Binding to link-local address requires an interface */
if (!sk->sk_bound_dev_if)
goto out;
goto out_unlock;

dev = dev_get_by_index(sock_net(sk), sk->sk_bound_dev_if);
if (!dev) {
err = -ENODEV;
goto out;
}
err = -ENODEV;
dev = dev_get_by_index_rcu(sock_net(sk),
sk->sk_bound_dev_if);
if (!dev)
goto out_unlock;
}

/* ipv4 addr of the socket is invalid. Only the
Expand All @@ -289,20 +290,18 @@ static int rawv6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
err = -EADDRNOTAVAIL;
if (!ipv6_chk_addr(sock_net(sk), &addr->sin6_addr,
dev, 0)) {
if (dev)
dev_put(dev);
goto out;
goto out_unlock;
}
}
if (dev)
dev_put(dev);
}

inet->inet_rcv_saddr = inet->inet_saddr = v4addr;
ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
if (!(addr_type & IPV6_ADDR_MULTICAST))
ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
err = 0;
out_unlock:
rcu_read_unlock();
out:
release_sock(sk);
return err;
Expand Down

0 comments on commit fd5c002

Please sign in to comment.