1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * 6LoWPAN next header compression
6 * Alexander Aring <aar@pengutronix.de>
9 #include <linux/netdevice.h>
15 static const struct lowpan_nhc
*lowpan_nexthdr_nhcs
[NEXTHDR_MAX
+ 1];
16 static DEFINE_SPINLOCK(lowpan_nhc_lock
);
18 static const struct lowpan_nhc
*lowpan_nhc_by_nhcid(struct sk_buff
*skb
)
20 const struct lowpan_nhc
*nhc
;
24 if (!pskb_may_pull(skb
, 1))
29 for (i
= 0; i
< NEXTHDR_MAX
+ 1; i
++) {
30 nhc
= lowpan_nexthdr_nhcs
[i
];
34 if ((id
& nhc
->idmask
) == nhc
->id
)
41 int lowpan_nhc_check_compression(struct sk_buff
*skb
,
42 const struct ipv6hdr
*hdr
, u8
**hc_ptr
)
44 const struct lowpan_nhc
*nhc
;
47 spin_lock_bh(&lowpan_nhc_lock
);
49 nhc
= lowpan_nexthdr_nhcs
[hdr
->nexthdr
];
50 if (!(nhc
&& nhc
->compress
))
53 spin_unlock_bh(&lowpan_nhc_lock
);
58 int lowpan_nhc_do_compression(struct sk_buff
*skb
, const struct ipv6hdr
*hdr
,
62 const struct lowpan_nhc
*nhc
;
64 spin_lock_bh(&lowpan_nhc_lock
);
66 nhc
= lowpan_nexthdr_nhcs
[hdr
->nexthdr
];
67 /* check if the nhc module was removed in unlocked part.
68 * TODO: this is a workaround we should prevent unloading
69 * of nhc modules while unlocked part, this will always drop
70 * the lowpan packet but it's very unlikely.
72 * Solution isn't easy because we need to decide at
73 * lowpan_nhc_check_compression if we do a compression or not.
74 * Because the inline data which is added to skb, we can't move this
77 if (unlikely(!nhc
|| !nhc
->compress
)) {
82 /* In the case of RAW sockets the transport header is not set by
83 * the ip6 stack so we must set it ourselves
85 if (skb
->transport_header
== skb
->network_header
)
86 skb_set_transport_header(skb
, sizeof(struct ipv6hdr
));
88 ret
= nhc
->compress(skb
, hc_ptr
);
92 /* skip the transport header */
93 skb_pull(skb
, nhc
->nexthdrlen
);
96 spin_unlock_bh(&lowpan_nhc_lock
);
101 int lowpan_nhc_do_uncompression(struct sk_buff
*skb
,
102 const struct net_device
*dev
,
105 const struct lowpan_nhc
*nhc
;
108 spin_lock_bh(&lowpan_nhc_lock
);
110 nhc
= lowpan_nhc_by_nhcid(skb
);
112 if (nhc
->uncompress
) {
113 ret
= nhc
->uncompress(skb
, sizeof(struct ipv6hdr
) +
116 spin_unlock_bh(&lowpan_nhc_lock
);
120 spin_unlock_bh(&lowpan_nhc_lock
);
121 netdev_warn(dev
, "received nhc id for %s which is not implemented.\n",
126 spin_unlock_bh(&lowpan_nhc_lock
);
127 netdev_warn(dev
, "received unknown nhc id which was not found.\n");
131 hdr
->nexthdr
= nhc
->nexthdr
;
132 skb_reset_transport_header(skb
);
133 raw_dump_table(__func__
, "raw transport header dump",
134 skb_transport_header(skb
), nhc
->nexthdrlen
);
136 spin_unlock_bh(&lowpan_nhc_lock
);
141 int lowpan_nhc_add(const struct lowpan_nhc
*nhc
)
145 spin_lock_bh(&lowpan_nhc_lock
);
147 if (lowpan_nexthdr_nhcs
[nhc
->nexthdr
]) {
152 lowpan_nexthdr_nhcs
[nhc
->nexthdr
] = nhc
;
154 spin_unlock_bh(&lowpan_nhc_lock
);
157 EXPORT_SYMBOL(lowpan_nhc_add
);
159 void lowpan_nhc_del(const struct lowpan_nhc
*nhc
)
161 spin_lock_bh(&lowpan_nhc_lock
);
163 lowpan_nexthdr_nhcs
[nhc
->nexthdr
] = NULL
;
165 spin_unlock_bh(&lowpan_nhc_lock
);
169 EXPORT_SYMBOL(lowpan_nhc_del
);