Skip to content

Commit 3bd5b95

Browse files
alex-bluesmandavem330
authored andcommitted
6LoWPAN: UDP header compression
This patch adds support for UDP header compression. Derived from Contiki OS. Signed-off-by: Alexander Smirnov <alex.bluesman.smirnov@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 4d039f6 commit 3bd5b95

2 files changed

Lines changed: 52 additions & 4 deletions

File tree

‎net/ieee802154/6lowpan.c‎

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,50 @@ lowpan_uncompress_addr(struct sk_buff *skb, struct in6_addr *ipaddr,
246246
return 0;
247247
}
248248

249+
static void
250+
lowpan_compress_udp_header(u8 **hc06_ptr, struct sk_buff *skb)
251+
{
252+
struct udphdr *uh = udp_hdr(skb);
253+
254+
pr_debug("(%s): UDP header compression\n", __func__);
255+
256+
if (((uh->source & LOWPAN_NHC_UDP_4BIT_MASK) ==
257+
LOWPAN_NHC_UDP_4BIT_PORT) &&
258+
((uh->dest & LOWPAN_NHC_UDP_4BIT_MASK) ==
259+
LOWPAN_NHC_UDP_4BIT_PORT)) {
260+
pr_debug("(%s): both ports compression to 4 bits\n", __func__);
261+
**hc06_ptr = LOWPAN_NHC_UDP_CS_P_11;
262+
**(hc06_ptr + 1) = /* subtraction is faster */
263+
(u8)((uh->dest - LOWPAN_NHC_UDP_4BIT_PORT) +
264+
((uh->source & LOWPAN_NHC_UDP_4BIT_PORT) << 4));
265+
*hc06_ptr += 2;
266+
} else if ((uh->dest & LOWPAN_NHC_UDP_8BIT_MASK) ==
267+
LOWPAN_NHC_UDP_8BIT_PORT) {
268+
pr_debug("(%s): remove 8 bits of dest\n", __func__);
269+
**hc06_ptr = LOWPAN_NHC_UDP_CS_P_01;
270+
memcpy(*hc06_ptr + 1, &uh->source, 2);
271+
**(hc06_ptr + 3) = (u8)(uh->dest - LOWPAN_NHC_UDP_8BIT_PORT);
272+
*hc06_ptr += 4;
273+
} else if ((uh->source & LOWPAN_NHC_UDP_8BIT_MASK) ==
274+
LOWPAN_NHC_UDP_8BIT_PORT) {
275+
pr_debug("(%s): remove 8 bits of source\n", __func__);
276+
**hc06_ptr = LOWPAN_NHC_UDP_CS_P_10;
277+
memcpy(*hc06_ptr + 1, &uh->dest, 2);
278+
**(hc06_ptr + 3) = (u8)(uh->source - LOWPAN_NHC_UDP_8BIT_PORT);
279+
*hc06_ptr += 4;
280+
} else {
281+
pr_debug("(%s): can't compress header\n", __func__);
282+
**hc06_ptr = LOWPAN_NHC_UDP_CS_P_00;
283+
memcpy(*hc06_ptr + 1, &uh->source, 2);
284+
memcpy(*hc06_ptr + 3, &uh->dest, 2);
285+
*hc06_ptr += 5;
286+
}
287+
288+
/* checksum is always inline */
289+
memcpy(*hc06_ptr, &uh->check, 2);
290+
*hc06_ptr += 2;
291+
}
292+
249293
static u8 lowpan_fetch_skb_u8(struct sk_buff *skb)
250294
{
251295
u8 ret;
@@ -365,8 +409,6 @@ static int lowpan_header_create(struct sk_buff *skb,
365409
if (hdr->nexthdr == UIP_PROTO_UDP)
366410
iphc0 |= LOWPAN_IPHC_NH_C;
367411

368-
/* TODO: next header compression */
369-
370412
if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
371413
*hc06_ptr = hdr->nexthdr;
372414
hc06_ptr += 1;
@@ -454,8 +496,9 @@ static int lowpan_header_create(struct sk_buff *skb,
454496
}
455497
}
456498

457-
/* TODO: UDP header compression */
458-
/* TODO: Next Header compression */
499+
/* UDP header compression */
500+
if (hdr->nexthdr == UIP_PROTO_UDP)
501+
lowpan_compress_udp_header(&hc06_ptr, skb);
459502

460503
head[0] = iphc0;
461504
head[1] = iphc1;

‎net/ieee802154/6lowpan.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,11 @@
219219
#define LOWPAN_NHC_UDP_CHECKSUMC 0x04
220220
#define LOWPAN_NHC_UDP_CHECKSUMI 0x00
221221

222+
#define LOWPAN_NHC_UDP_4BIT_PORT 0xF0B0
223+
#define LOWPAN_NHC_UDP_4BIT_MASK 0xFFF0
224+
#define LOWPAN_NHC_UDP_8BIT_PORT 0xF000
225+
#define LOWPAN_NHC_UDP_8BIT_MASK 0xFF00
226+
222227
/* values for port compression, _with checksum_ ie bit 5 set to 0 */
223228
#define LOWPAN_NHC_UDP_CS_P_00 0xF0 /* all inline */
224229
#define LOWPAN_NHC_UDP_CS_P_01 0xF1 /* source 16bit inline,

0 commit comments

Comments
 (0)