wl1251: fix a memory leak in probe
[safe/jmp/linux-2.6] / net / llc / af_llc.c
index 5bcc452..023ba82 100644 (file)
@@ -25,6 +25,7 @@
 #include <linux/module.h>
 #include <linux/rtnetlink.h>
 #include <linux/init.h>
+#include <linux/slab.h>
 #include <net/llc.h>
 #include <net/llc_sap.h>
 #include <net/llc_pdu.h>
@@ -47,6 +48,10 @@ static int llc_ui_wait_for_busy_core(struct sock *sk, long timeout);
 #define dprintk(args...)
 #endif
 
+/* Maybe we'll add some more in the future. */
+#define LLC_CMSG_PKTINFO       1
+
+
 /**
  *     llc_ui_next_link_no - return the next unused link number for a sap
  *     @sap: Address of sap to get link number from.
@@ -103,7 +108,6 @@ static inline u8 llc_ui_header_len(struct sock *sk, struct sockaddr_llc *addr)
  *     llc_ui_send_data - send data via reliable llc2 connection
  *     @sk: Connection the socket is using.
  *     @skb: Data the user wishes to send.
- *     @addr: Source and destination fields provided by the user.
  *     @noblock: can we block waiting for data?
  *
  *     Send data via reliable llc2 connection.
@@ -137,18 +141,22 @@ static struct proto llc_proto = {
        .name     = "LLC",
        .owner    = THIS_MODULE,
        .obj_size = sizeof(struct llc_sock),
+       .slab_flags = SLAB_DESTROY_BY_RCU,
 };
 
 /**
  *     llc_ui_create - alloc and init a new llc_ui socket
+ *     @net: network namespace (must be default network)
  *     @sock: Socket to initialize and attach allocated sk to.
  *     @protocol: Unused.
+ *     @kern: on behalf of kernel or userspace
  *
  *     Allocate and initialize a new llc_ui socket, validate the user wants a
  *     socket type we have available.
  *     Returns 0 upon success, negative upon failure.
  */
-static int llc_ui_create(struct net *net, struct socket *sock, int protocol)
+static int llc_ui_create(struct net *net, struct socket *sock, int protocol,
+                        int kern)
 {
        struct sock *sk;
        int rc = -ESOCKTNOSUPPORT;
@@ -156,7 +164,7 @@ static int llc_ui_create(struct net *net, struct socket *sock, int protocol)
        if (!capable(CAP_NET_RAW))
                return -EPERM;
 
-       if (net != &init_net)
+       if (!net_eq(net, &init_net))
                return -EAFNOSUPPORT;
 
        if (likely(sock->type == SOCK_DGRAM || sock->type == SOCK_STREAM)) {
@@ -190,10 +198,8 @@ static int llc_ui_release(struct socket *sock)
                llc->laddr.lsap, llc->daddr.lsap);
        if (!llc_send_disc(sk))
                llc_ui_wait_for_disc(sk, sk->sk_rcvtimeo);
-       if (!sock_flag(sk, SOCK_ZAPPED)) {
-               llc_sap_put(llc->sap);
+       if (!sock_flag(sk, SOCK_ZAPPED))
                llc_sap_remove_socket(llc->sap, sk);
-       }
        release_sock(sk);
        if (llc->dev)
                dev_put(llc->dev);
@@ -253,7 +259,14 @@ static int llc_ui_autobind(struct socket *sock, struct sockaddr_llc *addr)
        if (!sock_flag(sk, SOCK_ZAPPED))
                goto out;
        rc = -ENODEV;
-       llc->dev = dev_getfirstbyhwtype(&init_net, addr->sllc_arphrd);
+       if (sk->sk_bound_dev_if) {
+               llc->dev = dev_get_by_index(&init_net, sk->sk_bound_dev_if);
+               if (llc->dev && addr->sllc_arphrd != llc->dev->type) {
+                       dev_put(llc->dev);
+                       llc->dev = NULL;
+               }
+       } else
+               llc->dev = dev_getfirstbyhwtype(&init_net, addr->sllc_arphrd);
        if (!llc->dev)
                goto out;
        rc = -EUSERS;
@@ -304,7 +317,25 @@ static int llc_ui_bind(struct socket *sock, struct sockaddr *uaddr, int addrlen)
                goto out;
        rc = -ENODEV;
        rtnl_lock();
-       llc->dev = dev_getbyhwaddr(&init_net, addr->sllc_arphrd, addr->sllc_mac);
+       if (sk->sk_bound_dev_if) {
+               llc->dev = dev_get_by_index(&init_net, sk->sk_bound_dev_if);
+               if (llc->dev) {
+                       if (!addr->sllc_arphrd)
+                               addr->sllc_arphrd = llc->dev->type;
+                       if (llc_mac_null(addr->sllc_mac))
+                               memcpy(addr->sllc_mac, llc->dev->dev_addr,
+                                      IFHWADDRLEN);
+                       if (addr->sllc_arphrd != llc->dev->type ||
+                           !llc_mac_match(addr->sllc_mac,
+                                          llc->dev->dev_addr)) {
+                               rc = -EINVAL;
+                               dev_put(llc->dev);
+                               llc->dev = NULL;
+                       }
+               }
+       } else
+               llc->dev = dev_getbyhwaddr(&init_net, addr->sllc_arphrd,
+                                          addr->sllc_mac);
        rtnl_unlock();
        if (!llc->dev)
                goto out;
@@ -320,7 +351,6 @@ static int llc_ui_bind(struct socket *sock, struct sockaddr *uaddr, int addrlen)
                rc = -EBUSY; /* some other network layer is using the sap */
                if (!sap)
                        goto out;
-               llc_sap_hold(sap);
        } else {
                struct llc_addr laddr, daddr;
                struct sock *ask;
@@ -506,7 +536,7 @@ static int llc_ui_wait_for_disc(struct sock *sk, long timeout)
        int rc = 0;
 
        while (1) {
-               prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
+               prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
                if (sk_wait_event(sk, &timeout, sk->sk_state == TCP_CLOSE))
                        break;
                rc = -ERESTARTSYS;
@@ -517,7 +547,7 @@ static int llc_ui_wait_for_disc(struct sock *sk, long timeout)
                        break;
                rc = 0;
        }
-       finish_wait(sk->sk_sleep, &wait);
+       finish_wait(sk_sleep(sk), &wait);
        return rc;
 }
 
@@ -526,13 +556,13 @@ static int llc_ui_wait_for_conn(struct sock *sk, long timeout)
        DEFINE_WAIT(wait);
 
        while (1) {
-               prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
+               prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
                if (sk_wait_event(sk, &timeout, sk->sk_state != TCP_SYN_SENT))
                        break;
                if (signal_pending(current) || !timeout)
                        break;
        }
-       finish_wait(sk->sk_sleep, &wait);
+       finish_wait(sk_sleep(sk), &wait);
        return timeout;
 }
 
@@ -543,7 +573,7 @@ static int llc_ui_wait_for_busy_core(struct sock *sk, long timeout)
        int rc;
 
        while (1) {
-               prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
+               prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
                rc = 0;
                if (sk_wait_event(sk, &timeout,
                                  (sk->sk_shutdown & RCV_SHUTDOWN) ||
@@ -558,7 +588,7 @@ static int llc_ui_wait_for_busy_core(struct sock *sk, long timeout)
                if (!timeout)
                        break;
        }
-       finish_wait(sk->sk_sleep, &wait);
+       finish_wait(sk_sleep(sk), &wait);
        return rc;
 }
 
@@ -589,6 +619,20 @@ static int llc_wait_data(struct sock *sk, long timeo)
        return rc;
 }
 
+static void llc_cmsg_rcv(struct msghdr *msg, struct sk_buff *skb)
+{
+       struct llc_sock *llc = llc_sk(skb->sk);
+
+       if (llc->cmsg_flags & LLC_CMSG_PKTINFO) {
+               struct llc_pktinfo info;
+
+               info.lpi_ifindex = llc_sk(skb->sk)->dev->ifindex;
+               llc_pdu_decode_dsap(skb, &info.lpi_sap);
+               llc_pdu_decode_da(skb, info.lpi_mac);
+               put_cmsg(msg, SOL_LLC, LLC_OPT_PKTINFO, sizeof(info), &info);
+       }
+}
+
 /**
  *     llc_ui_accept - accept a new incoming connection.
  *     @sock: Socket which connections arrive on.
@@ -810,6 +854,8 @@ copy_uaddr:
                memcpy(uaddr, llc_ui_skb_cb(skb), sizeof(*uaddr));
                msg->msg_namelen = sizeof(*uaddr);
        }
+       if (llc_sk(sk)->cmsg_flags)
+               llc_cmsg_rcv(msg, skb);
        goto out;
 }
 
@@ -915,6 +961,7 @@ static int llc_ui_getname(struct socket *sock, struct sockaddr *uaddr,
        struct llc_sock *llc = llc_sk(sk);
        int rc = 0;
 
+       memset(&sllc, 0, sizeof(sllc));
        lock_sock(sk);
        if (sock_flag(sk, SOCK_ZAPPED))
                goto out;
@@ -936,7 +983,7 @@ static int llc_ui_getname(struct socket *sock, struct sockaddr *uaddr,
 
                if (llc->dev) {
                        sllc.sllc_arphrd = llc->dev->type;
-                       memcpy(&sllc.sllc_mac, &llc->dev->dev_addr,
+                       memcpy(&sllc.sllc_mac, llc->dev->dev_addr,
                               IFHWADDRLEN);
                }
        }
@@ -973,7 +1020,7 @@ static int llc_ui_ioctl(struct socket *sock, unsigned int cmd,
  *     Set various connection specific parameters.
  */
 static int llc_ui_setsockopt(struct socket *sock, int level, int optname,
-                            char __user *optval, int optlen)
+                            char __user *optval, unsigned int optlen)
 {
        struct sock *sk = sock->sk;
        struct llc_sock *llc = llc_sk(sk);
@@ -1027,6 +1074,12 @@ static int llc_ui_setsockopt(struct socket *sock, int level, int optname,
                        goto out;
                llc->rw = opt;
                break;
+       case LLC_OPT_PKTINFO:
+               if (opt)
+                       llc->cmsg_flags |= LLC_CMSG_PKTINFO;
+               else
+                       llc->cmsg_flags &= ~LLC_CMSG_PKTINFO;
+               break;
        default:
                rc = -ENOPROTOOPT;
                goto out;
@@ -1080,6 +1133,9 @@ static int llc_ui_getsockopt(struct socket *sock, int level, int optname,
                val = llc->k;                           break;
        case LLC_OPT_RX_WIN:
                val = llc->rw;                          break;
+       case LLC_OPT_PKTINFO:
+               val = (llc->cmsg_flags & LLC_CMSG_PKTINFO) != 0;
+               break;
        default:
                rc = -ENOPROTOOPT;
                goto out;
@@ -1092,7 +1148,7 @@ out:
        return rc;
 }
 
-static struct net_proto_family llc_ui_family_ops = {
+static const struct net_proto_family llc_ui_family_ops = {
        .family = PF_LLC,
        .create = llc_ui_create,
        .owner  = THIS_MODULE,
@@ -1119,11 +1175,11 @@ static const struct proto_ops llc_ui_ops = {
        .sendpage    = sock_no_sendpage,
 };
 
-static char llc_proc_err_msg[] __initdata =
+static const char llc_proc_err_msg[] __initconst =
        KERN_CRIT "LLC: Unable to register the proc_fs entries\n";
-static char llc_sysctl_err_msg[] __initdata =
+static const char llc_sysctl_err_msg[] __initconst =
        KERN_CRIT "LLC: Unable to register the sysctl entries\n";
-static char llc_sock_err_msg[] __initdata =
+static const char llc_sock_err_msg[] __initconst =
        KERN_CRIT "LLC: Unable to register the network family\n";
 
 static int __init llc2_init(void)