Merge branch develop-3.10 into develop-3.10-next
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / rockchip_wlan / rkwifi / bcmdhd / dhd_ip.c
1 /*
2  * IP Packet Parser Module.
3  *
4  * $Copyright Open Broadcom Corporation$
5  *
6  * $Id$
7  */
8 #include <typedefs.h>
9 #include <osl.h>
10
11 #include <proto/ethernet.h>
12 #include <proto/vlan.h>
13 #include <proto/802.3.h>
14 #include <proto/bcmip.h>
15 #include <bcmendian.h>
16
17 #include <dhd_dbg.h>
18
19 #include <dhd_ip.h>
20
21 /* special values */
22 /* 802.3 llc/snap header */
23 static const uint8 llc_snap_hdr[SNAP_HDR_LEN] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
24
25 pkt_frag_t pkt_frag_info(osl_t *osh, void *p)
26 {
27         uint8 *frame;
28         int length;
29         uint8 *pt;                      /* Pointer to type field */
30         uint16 ethertype;
31         struct ipv4_hdr *iph;           /* IP frame pointer */
32         int ipl;                        /* IP frame length */
33         uint16 iph_frag;
34
35         ASSERT(osh && p);
36
37         frame = PKTDATA(osh, p);
38         length = PKTLEN(osh, p);
39
40         /* Process Ethernet II or SNAP-encapsulated 802.3 frames */
41         if (length < ETHER_HDR_LEN) {
42                 DHD_INFO(("%s: short eth frame (%d)\n", __FUNCTION__, length));
43                 return DHD_PKT_FRAG_NONE;
44         } else if (ntoh16(*(uint16 *)(frame + ETHER_TYPE_OFFSET)) >= ETHER_TYPE_MIN) {
45                 /* Frame is Ethernet II */
46                 pt = frame + ETHER_TYPE_OFFSET;
47         } else if (length >= ETHER_HDR_LEN + SNAP_HDR_LEN + ETHER_TYPE_LEN &&
48                    !bcmp(llc_snap_hdr, frame + ETHER_HDR_LEN, SNAP_HDR_LEN)) {
49                 pt = frame + ETHER_HDR_LEN + SNAP_HDR_LEN;
50         } else {
51                 DHD_INFO(("%s: non-SNAP 802.3 frame\n", __FUNCTION__));
52                 return DHD_PKT_FRAG_NONE;
53         }
54
55         ethertype = ntoh16(*(uint16 *)pt);
56
57         /* Skip VLAN tag, if any */
58         if (ethertype == ETHER_TYPE_8021Q) {
59                 pt += VLAN_TAG_LEN;
60
61                 if (pt + ETHER_TYPE_LEN > frame + length) {
62                         DHD_INFO(("%s: short VLAN frame (%d)\n", __FUNCTION__, length));
63                         return DHD_PKT_FRAG_NONE;
64                 }
65
66                 ethertype = ntoh16(*(uint16 *)pt);
67         }
68
69         if (ethertype != ETHER_TYPE_IP) {
70                 DHD_INFO(("%s: non-IP frame (ethertype 0x%x, length %d)\n",
71                         __FUNCTION__, ethertype, length));
72                 return DHD_PKT_FRAG_NONE;
73         }
74
75         iph = (struct ipv4_hdr *)(pt + ETHER_TYPE_LEN);
76         ipl = length - (pt + ETHER_TYPE_LEN - frame);
77
78         /* We support IPv4 only */
79         if ((ipl < IPV4_OPTIONS_OFFSET) || (IP_VER(iph) != IP_VER_4)) {
80                 DHD_INFO(("%s: short frame (%d) or non-IPv4\n", __FUNCTION__, ipl));
81                 return DHD_PKT_FRAG_NONE;
82         }
83
84         iph_frag = ntoh16(iph->frag);
85
86         if (iph_frag & IPV4_FRAG_DONT) {
87                 return DHD_PKT_FRAG_NONE;
88         } else if ((iph_frag & IPV4_FRAG_MORE) == 0) {
89                 return DHD_PKT_FRAG_LAST;
90         } else {
91                 return (iph_frag & IPV4_FRAG_OFFSET_MASK)? DHD_PKT_FRAG_CONT : DHD_PKT_FRAG_FIRST;
92         }
93 }