pipe: fix pipe buffer resizing
[safe/jmp/linux-2.6] / net / llc / llc_sap.c
index e6d5389..94e7fca 100644 (file)
 #include <net/sock.h>
 #include <net/tcp_states.h>
 #include <linux/llc.h>
+#include <linux/slab.h>
+
+static int llc_mac_header_len(unsigned short devtype)
+{
+       switch (devtype) {
+       case ARPHRD_ETHER:
+       case ARPHRD_LOOPBACK:
+               return sizeof(struct ethhdr);
+#if defined(CONFIG_TR) || defined(CONFIG_TR_MODULE)
+       case ARPHRD_IEEE802_TR:
+               return sizeof(struct trh_hdr);
+#endif
+       }
+       return 0;
+}
 
 /**
  *     llc_alloc_frame - allocates sk_buff for frame
  *     @dev: network device this skb will be sent over
+ *     @type: pdu type to allocate
+ *     @data_size: data size to allocate
  *
  *     Allocates an sk_buff for frame and initializes sk_buff fields.
  *     Returns allocated skb or %NULL when out of memory.
  */
-struct sk_buff *llc_alloc_frame(struct sock *sk, struct net_device *dev)
+struct sk_buff *llc_alloc_frame(struct sock *sk, struct net_device *dev,
+                               u8 type, u32 data_size)
 {
-       struct sk_buff *skb = alloc_skb(128, GFP_ATOMIC);
+       int hlen = type == LLC_PDU_TYPE_U ? 3 : 4;
+       struct sk_buff *skb;
+
+       hlen += llc_mac_header_len(dev->type);
+       skb = alloc_skb(hlen + data_size, GFP_ATOMIC);
 
        if (skb) {
-               skb_reserve(skb, 50);
-               skb->nh.raw   = skb->h.raw = skb->data;
+               skb_reset_mac_header(skb);
+               skb_reserve(skb, hlen);
+               skb_reset_network_header(skb);
+               skb_reset_transport_header(skb);
                skb->protocol = htons(ETH_P_802_2);
                skb->dev      = dev;
-               skb->mac.raw  = skb->head;
                if (sk != NULL)
                        skb_set_owner_w(skb, sk);
        }
@@ -49,9 +72,12 @@ struct sk_buff *llc_alloc_frame(struct sock *sk, struct net_device *dev)
 
 void llc_save_primitive(struct sock *sk, struct sk_buff* skb, u8 prim)
 {
-       struct sockaddr_llc *addr = llc_ui_skb_cb(skb);
+       struct sockaddr_llc *addr;
 
        /* save primitive for use by the user. */
+       addr              = llc_ui_skb_cb(skb);
+
+       memset(addr, 0, sizeof(*addr));
        addr->sllc_family = sk->sk_family;
        addr->sllc_arphrd = skb->dev->type;
        addr->sllc_test   = prim == LLC_TEST_PRIM;
@@ -198,7 +224,7 @@ static void llc_sap_state_process(struct llc_sap *sap, struct sk_buff *skb)
                        if (sock_queue_rcv_skb(skb->sk, skb))
                                kfree_skb(skb);
                }
-       } 
+       }
        kfree_skb(skb);
 }
 
@@ -212,7 +238,7 @@ static void llc_sap_state_process(struct llc_sap *sap, struct sk_buff *skb)
  *     This function is called when upper layer wants to send a TEST pdu.
  *     Returns 0 for success, 1 otherwise.
  */
-void llc_build_and_send_test_pkt(struct llc_sap *sap, 
+void llc_build_and_send_test_pkt(struct llc_sap *sap,
                                 struct sk_buff *skb, u8 *dmac, u8 dsap)
 {
        struct llc_sap_state_ev *ev = llc_sap_ev(skb);
@@ -221,7 +247,7 @@ void llc_build_and_send_test_pkt(struct llc_sap *sap,
        ev->daddr.lsap = dsap;
        memcpy(ev->saddr.mac, skb->dev->dev_addr, IFHWADDRLEN);
        memcpy(ev->daddr.mac, dmac, IFHWADDRLEN);
-       
+
        ev->type      = LLC_SAP_EV_TYPE_PRIM;
        ev->prim      = LLC_TEST_PRIM;
        ev->prim_type = LLC_PRIM_TYPE_REQ;
@@ -261,15 +287,28 @@ void llc_build_and_send_xid_pkt(struct llc_sap *sap, struct sk_buff *skb,
  *
  *     Sends received pdus to the sap state machine.
  */
-static void llc_sap_rcv(struct llc_sap *sap, struct sk_buff *skb)
+static void llc_sap_rcv(struct llc_sap *sap, struct sk_buff *skb,
+                       struct sock *sk)
 {
        struct llc_sap_state_ev *ev = llc_sap_ev(skb);
 
        ev->type   = LLC_SAP_EV_TYPE_PDU;
        ev->reason = 0;
+       skb->sk = sk;
        llc_sap_state_process(sap, skb);
 }
 
+static inline bool llc_dgram_match(const struct llc_sap *sap,
+                                  const struct llc_addr *laddr,
+                                  const struct sock *sk)
+{
+     struct llc_sock *llc = llc_sk(sk);
+
+     return sk->sk_type == SOCK_DGRAM &&
+         llc->laddr.lsap == laddr->lsap &&
+         llc_mac_match(llc->laddr.mac, laddr->mac);
+}
+
 /**
  *     llc_lookup_dgram - Finds dgram socket for the local sap/mac
  *     @sap: SAP
@@ -279,41 +318,127 @@ static void llc_sap_rcv(struct llc_sap *sap, struct sk_buff *skb)
  *     mac, and local sap. Returns pointer for socket found, %NULL otherwise.
  */
 static struct sock *llc_lookup_dgram(struct llc_sap *sap,
-                                    struct llc_addr *laddr)
+                                    const struct llc_addr *laddr)
 {
        struct sock *rc;
-       struct hlist_node *node;
-
-       read_lock_bh(&sap->sk_list.lock);
-       sk_for_each(rc, node, &sap->sk_list.list) {
-               struct llc_sock *llc = llc_sk(rc);
+       struct hlist_nulls_node *node;
+       int slot = llc_sk_laddr_hashfn(sap, laddr);
+       struct hlist_nulls_head *laddr_hb = &sap->sk_laddr_hash[slot];
 
-               if (rc->sk_type == SOCK_DGRAM &&
-                   llc->laddr.lsap == laddr->lsap &&
-                   llc_mac_match(llc->laddr.mac, laddr->mac)) {
-                       sock_hold(rc);
+       rcu_read_lock_bh();
+again:
+       sk_nulls_for_each_rcu(rc, node, laddr_hb) {
+               if (llc_dgram_match(sap, laddr, rc)) {
+                       /* Extra checks required by SLAB_DESTROY_BY_RCU */
+                       if (unlikely(!atomic_inc_not_zero(&rc->sk_refcnt)))
+                               goto again;
+                       if (unlikely(llc_sk(rc)->sap != sap ||
+                                    !llc_dgram_match(sap, laddr, rc))) {
+                               sock_put(rc);
+                               continue;
+                       }
                        goto found;
                }
        }
        rc = NULL;
+       /*
+        * if the nulls value we got at the end of this lookup is
+        * not the expected one, we must restart lookup.
+        * We probably met an item that was moved to another chain.
+        */
+       if (unlikely(get_nulls_value(node) != slot))
+               goto again;
 found:
-       read_unlock_bh(&sap->sk_list.lock);
+       rcu_read_unlock_bh();
        return rc;
 }
 
+static inline bool llc_mcast_match(const struct llc_sap *sap,
+                                  const struct llc_addr *laddr,
+                                  const struct sk_buff *skb,
+                                  const struct sock *sk)
+{
+     struct llc_sock *llc = llc_sk(sk);
+
+     return sk->sk_type == SOCK_DGRAM &&
+         llc->laddr.lsap == laddr->lsap &&
+         llc->dev == skb->dev;
+}
+
+static void llc_do_mcast(struct llc_sap *sap, struct sk_buff *skb,
+                        struct sock **stack, int count)
+{
+       struct sk_buff *skb1;
+       int i;
+
+       for (i = 0; i < count; i++) {
+               skb1 = skb_clone(skb, GFP_ATOMIC);
+               if (!skb1) {
+                       sock_put(stack[i]);
+                       continue;
+               }
+
+               llc_sap_rcv(sap, skb1, stack[i]);
+               sock_put(stack[i]);
+       }
+}
+
+/**
+ *     llc_sap_mcast - Deliver multicast PDU's to all matching datagram sockets.
+ *     @sap: SAP
+ *     @laddr: address of local LLC (MAC + SAP)
+ *
+ *     Search socket list of the SAP and finds connections with same sap.
+ *     Deliver clone to each.
+ */
+static void llc_sap_mcast(struct llc_sap *sap,
+                         const struct llc_addr *laddr,
+                         struct sk_buff *skb)
+{
+       int i = 0, count = 256 / sizeof(struct sock *);
+       struct sock *sk, *stack[count];
+       struct hlist_node *node;
+       struct llc_sock *llc;
+       struct hlist_head *dev_hb = llc_sk_dev_hash(sap, skb->dev->ifindex);
+
+       spin_lock_bh(&sap->sk_lock);
+       hlist_for_each_entry(llc, node, dev_hb, dev_hash_node) {
+
+               sk = &llc->sk;
+
+               if (!llc_mcast_match(sap, laddr, skb, sk))
+                       continue;
+
+               sock_hold(sk);
+               if (i < count)
+                       stack[i++] = sk;
+               else {
+                       llc_do_mcast(sap, skb, stack, i);
+                       i = 0;
+               }
+       }
+       spin_unlock_bh(&sap->sk_lock);
+
+       llc_do_mcast(sap, skb, stack, i);
+}
+
+
 void llc_sap_handler(struct llc_sap *sap, struct sk_buff *skb)
 {
        struct llc_addr laddr;
-       struct sock *sk;
 
        llc_pdu_decode_da(skb, laddr.mac);
        llc_pdu_decode_dsap(skb, &laddr.lsap);
 
-       sk = llc_lookup_dgram(sap, &laddr);
-       if (sk) {
-               skb_set_owner_r(skb, sk);
-               llc_sap_rcv(sap, skb);
-               sock_put(sk);
-       } else
+       if (llc_mac_multicast(laddr.mac)) {
+               llc_sap_mcast(sap, &laddr, skb);
                kfree_skb(skb);
+       } else {
+               struct sock *sk = llc_lookup_dgram(sap, &laddr);
+               if (sk) {
+                       llc_sap_rcv(sap, skb, sk);
+                       sock_put(sk);
+               } else
+                       kfree_skb(skb);
+       }
 }