2 * 6LoWPAN IPv6 UDP compression according to RFC6282
6 * Alexander Aring <aar@pengutronix.de>
9 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
10 * Jon Smirl <jonsmirl@gmail.com>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
20 #define LOWPAN_NHC_UDP_IDLEN 1
22 static int udp_uncompress(struct sk_buff *skb, size_t needed)
29 fail = lowpan_fetch_skb(skb, &tmp, sizeof(tmp));
31 pr_debug("UDP header uncompression\n");
32 switch (tmp & LOWPAN_NHC_UDP_CS_P_11) {
33 case LOWPAN_NHC_UDP_CS_P_00:
34 fail |= lowpan_fetch_skb(skb, &uh.source, sizeof(uh.source));
35 fail |= lowpan_fetch_skb(skb, &uh.dest, sizeof(uh.dest));
37 case LOWPAN_NHC_UDP_CS_P_01:
38 fail |= lowpan_fetch_skb(skb, &uh.source, sizeof(uh.source));
39 fail |= lowpan_fetch_skb(skb, &val, sizeof(val));
40 uh.dest = htons(val + LOWPAN_NHC_UDP_8BIT_PORT);
42 case LOWPAN_NHC_UDP_CS_P_10:
43 fail |= lowpan_fetch_skb(skb, &val, sizeof(val));
44 uh.source = htons(val + LOWPAN_NHC_UDP_8BIT_PORT);
45 fail |= lowpan_fetch_skb(skb, &uh.dest, sizeof(uh.dest));
47 case LOWPAN_NHC_UDP_CS_P_11:
48 fail |= lowpan_fetch_skb(skb, &val, sizeof(val));
49 uh.source = htons(LOWPAN_NHC_UDP_4BIT_PORT + (val >> 4));
50 uh.dest = htons(LOWPAN_NHC_UDP_4BIT_PORT + (val & 0x0f));
56 pr_debug("uncompressed UDP ports: src = %d, dst = %d\n",
57 ntohs(uh.source), ntohs(uh.dest));
60 if (tmp & LOWPAN_NHC_UDP_CS_C) {
61 pr_debug_ratelimited("checksum elided currently not supported\n");
64 fail |= lowpan_fetch_skb(skb, &uh.check, sizeof(uh.check));
70 /* UDP length needs to be infered from the lower layers
71 * here, we obtain the hint from the remaining size of the
74 switch (lowpan_priv(skb->dev)->lltype) {
75 case LOWPAN_LLTYPE_IEEE802154:
76 if (lowpan_802154_cb(skb)->d_size)
77 uh.len = htons(lowpan_802154_cb(skb)->d_size -
78 sizeof(struct ipv6hdr));
80 uh.len = htons(skb->len + sizeof(struct udphdr));
83 uh.len = htons(skb->len + sizeof(struct udphdr));
86 pr_debug("uncompressed UDP length: src = %d", ntohs(uh.len));
88 /* replace the compressed UDP head by the uncompressed UDP
91 err = skb_cow(skb, needed);
95 skb_push(skb, sizeof(struct udphdr));
96 skb_copy_to_linear_data(skb, &uh, sizeof(struct udphdr));
101 static int udp_compress(struct sk_buff *skb, u8 **hc_ptr)
103 const struct udphdr *uh = udp_hdr(skb);
106 if (((ntohs(uh->source) & LOWPAN_NHC_UDP_4BIT_MASK) ==
107 LOWPAN_NHC_UDP_4BIT_PORT) &&
108 ((ntohs(uh->dest) & LOWPAN_NHC_UDP_4BIT_MASK) ==
109 LOWPAN_NHC_UDP_4BIT_PORT)) {
110 pr_debug("UDP header: both ports compression to 4 bits\n");
111 /* compression value */
112 tmp = LOWPAN_NHC_UDP_CS_P_11;
113 lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
114 /* source and destination port */
115 tmp = ntohs(uh->dest) - LOWPAN_NHC_UDP_4BIT_PORT +
116 ((ntohs(uh->source) - LOWPAN_NHC_UDP_4BIT_PORT) << 4);
117 lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
118 } else if ((ntohs(uh->dest) & LOWPAN_NHC_UDP_8BIT_MASK) ==
119 LOWPAN_NHC_UDP_8BIT_PORT) {
120 pr_debug("UDP header: remove 8 bits of dest\n");
121 /* compression value */
122 tmp = LOWPAN_NHC_UDP_CS_P_01;
123 lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
125 lowpan_push_hc_data(hc_ptr, &uh->source, sizeof(uh->source));
126 /* destination port */
127 tmp = ntohs(uh->dest) - LOWPAN_NHC_UDP_8BIT_PORT;
128 lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
129 } else if ((ntohs(uh->source) & LOWPAN_NHC_UDP_8BIT_MASK) ==
130 LOWPAN_NHC_UDP_8BIT_PORT) {
131 pr_debug("UDP header: remove 8 bits of source\n");
132 /* compression value */
133 tmp = LOWPAN_NHC_UDP_CS_P_10;
134 lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
136 tmp = ntohs(uh->source) - LOWPAN_NHC_UDP_8BIT_PORT;
137 lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
138 /* destination port */
139 lowpan_push_hc_data(hc_ptr, &uh->dest, sizeof(uh->dest));
141 pr_debug("UDP header: can't compress\n");
142 /* compression value */
143 tmp = LOWPAN_NHC_UDP_CS_P_00;
144 lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
146 lowpan_push_hc_data(hc_ptr, &uh->source, sizeof(uh->source));
147 /* destination port */
148 lowpan_push_hc_data(hc_ptr, &uh->dest, sizeof(uh->dest));
151 /* checksum is always inline */
152 lowpan_push_hc_data(hc_ptr, &uh->check, sizeof(uh->check));
157 static void udp_nhid_setup(struct lowpan_nhc *nhc)
159 nhc->id[0] = LOWPAN_NHC_UDP_ID;
160 nhc->idmask[0] = LOWPAN_NHC_UDP_MASK;
163 LOWPAN_NHC(nhc_udp, "RFC6282 UDP", NEXTHDR_UDP, sizeof(struct udphdr),
164 udp_nhid_setup, LOWPAN_NHC_UDP_IDLEN, udp_uncompress, udp_compress);
166 module_lowpan_nhc(nhc_udp);
167 MODULE_DESCRIPTION("6LoWPAN next header RFC6282 UDP compression");
168 MODULE_LICENSE("GPL");