2 * Copyright 2011, Siemens AG
3 * written by Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
7 * Based on patches from Jon Smirl <jonsmirl@gmail.com>
8 * Copyright (c) 2011 Jon Smirl <jonsmirl@gmail.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 /* Jon's code is based on 6lowpan implementation for Contiki which is:
25 * Copyright (c) 2008, Swedish Institute of Computer Science.
26 * All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. Neither the name of the Institute nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 #include <linux/bitops.h>
56 #include <linux/if_arp.h>
57 #include <linux/module.h>
58 #include <linux/moduleparam.h>
59 #include <linux/netdevice.h>
60 #include <net/af_ieee802154.h>
61 #include <net/ieee802154.h>
62 #include <net/ieee802154_netdev.h>
67 /* TTL uncompression values */
68 static const u8 lowpan_ttl_values
[] = {0, 1, 64, 255};
70 static LIST_HEAD(lowpan_devices
);
73 * Uncompression of linklocal:
74 * 0 -> 16 bytes from packet
75 * 1 -> 2 bytes from prefix - bunch of zeroes and 8 from packet
76 * 2 -> 2 bytes from prefix - zeroes + 2 from packet
77 * 3 -> 2 bytes from prefix - infer 8 bytes from lladdr
79 * NOTE: => the uncompress function does change 0xf to 0x10
80 * NOTE: 0x00 => no-autoconfig => unspecified
82 static const u8 lowpan_unc_llconf
[] = {0x0f, 0x28, 0x22, 0x20};
85 * Uncompression of ctx-based:
86 * 0 -> 0 bits from packet [unspecified / reserved]
87 * 1 -> 8 bytes from prefix - bunch of zeroes and 8 from packet
88 * 2 -> 8 bytes from prefix - zeroes + 2 from packet
89 * 3 -> 8 bytes from prefix - infer 8 bytes from lladdr
91 static const u8 lowpan_unc_ctxconf
[] = {0x00, 0x88, 0x82, 0x80};
94 * Uncompression of ctx-base
95 * 0 -> 0 bits from packet
96 * 1 -> 2 bytes from prefix - bunch of zeroes 5 from packet
97 * 2 -> 2 bytes from prefix - zeroes + 3 from packet
98 * 3 -> 2 bytes from prefix - infer 1 bytes from lladdr
100 static const u8 lowpan_unc_mxconf
[] = {0x0f, 0x25, 0x23, 0x21};
102 /* Link local prefix */
103 static const u8 lowpan_llprefix
[] = {0xfe, 0x80};
105 /* private device info */
106 struct lowpan_dev_info
{
107 struct net_device
*real_dev
; /* real WPAN device ptr */
108 struct mutex dev_list_mtx
; /* mutex for list ops */
111 struct lowpan_dev_record
{
112 struct net_device
*ldev
;
113 struct list_head list
;
117 lowpan_dev_info
*lowpan_dev_info(const struct net_device
*dev
)
119 return netdev_priv(dev
);
122 static inline void lowpan_address_flip(u8
*src
, u8
*dest
)
125 for (i
= 0; i
< IEEE802154_ADDR_LEN
; i
++)
126 (dest
)[IEEE802154_ADDR_LEN
- i
- 1] = (src
)[i
];
129 /* list of all 6lowpan devices, uses for package delivering */
130 /* print data in line */
131 static inline void lowpan_raw_dump_inline(const char *caller
, char *msg
,
132 unsigned char *buf
, int len
)
136 pr_debug("(%s) %s: ", caller
, msg
);
137 print_hex_dump(KERN_DEBUG
, "", DUMP_PREFIX_NONE
,
138 16, 1, buf
, len
, false);
143 * print data in a table format:
145 * addr: xx xx xx xx xx xx
146 * addr: xx xx xx xx xx xx
149 static inline void lowpan_raw_dump_table(const char *caller
, char *msg
,
150 unsigned char *buf
, int len
)
154 pr_debug("(%s) %s:\n", caller
, msg
);
155 print_hex_dump(KERN_DEBUG
, "\t", DUMP_PREFIX_OFFSET
,
156 16, 1, buf
, len
, false);
161 lowpan_compress_addr_64(u8
**hc06_ptr
, u8 shift
, const struct in6_addr
*ipaddr
,
162 const unsigned char *lladdr
)
166 if (is_addr_mac_addr_based(ipaddr
, lladdr
))
167 val
= 3; /* 0-bits */
168 else if (lowpan_is_iid_16_bit_compressable(ipaddr
)) {
169 /* compress IID to 16 bits xxxx::XXXX */
170 memcpy(*hc06_ptr
, &ipaddr
->s6_addr16
[7], 2);
172 val
= 2; /* 16-bits */
174 /* do not compress IID => xxxx::IID */
175 memcpy(*hc06_ptr
, &ipaddr
->s6_addr16
[4], 8);
177 val
= 1; /* 64-bits */
180 return rol8(val
, shift
);
184 lowpan_uip_ds6_set_addr_iid(struct in6_addr
*ipaddr
, unsigned char *lladdr
)
186 memcpy(&ipaddr
->s6_addr
[8], lladdr
, IEEE802154_ALEN
);
187 /* second bit-flip (Universe/Local) is done according RFC2464 */
188 ipaddr
->s6_addr
[8] ^= 0x02;
192 * Uncompress addresses based on a prefix and a postfix with zeroes in
193 * between. If the postfix is zero in length it will use the link address
194 * to configure the IP address (autoconf style).
195 * pref_post_count takes a byte where the first nibble specify prefix count
196 * and the second postfix count (NOTE: 15/0xf => 16 bytes copy).
199 lowpan_uncompress_addr(struct sk_buff
*skb
, struct in6_addr
*ipaddr
,
200 u8
const *prefix
, u8 pref_post_count
, unsigned char *lladdr
)
202 u8 prefcount
= pref_post_count
>> 4;
203 u8 postcount
= pref_post_count
& 0x0f;
205 /* full nibble 15 => 16 */
206 prefcount
= (prefcount
== 15 ? 16 : prefcount
);
207 postcount
= (postcount
== 15 ? 16 : postcount
);
210 lowpan_raw_dump_inline(__func__
, "linklocal address",
211 lladdr
, IEEE802154_ALEN
);
213 memcpy(ipaddr
, prefix
, prefcount
);
215 if (prefcount
+ postcount
< 16)
216 memset(&ipaddr
->s6_addr
[prefcount
], 0,
217 16 - (prefcount
+ postcount
));
220 memcpy(&ipaddr
->s6_addr
[16 - postcount
], skb
->data
, postcount
);
221 skb_pull(skb
, postcount
);
222 } else if (prefcount
> 0) {
226 /* no IID based configuration if no prefix and no data */
227 lowpan_uip_ds6_set_addr_iid(ipaddr
, lladdr
);
230 pr_debug("(%s): uncompressing %d + %d => ", __func__
, prefcount
,
232 lowpan_raw_dump_inline(NULL
, NULL
, ipaddr
->s6_addr
, 16);
237 static u8
lowpan_fetch_skb_u8(struct sk_buff
*skb
)
247 static int lowpan_header_create(struct sk_buff
*skb
,
248 struct net_device
*dev
,
249 unsigned short type
, const void *_daddr
,
250 const void *_saddr
, unsigned len
)
252 u8 tmp
, iphc0
, iphc1
, *hc06_ptr
;
254 const u8
*saddr
= _saddr
;
255 const u8
*daddr
= _daddr
;
257 struct ieee802154_addr sa
, da
;
259 if (type
!= ETH_P_IPV6
)
262 * if this package isn't ipv6 one, where should it be routed?
264 head
= kzalloc(100, GFP_KERNEL
);
271 pr_debug("(%s): IPv6 header dump:\n\tversion = %d\n\tlength = %d\n"
272 "\tnexthdr = 0x%02x\n\thop_lim = %d\n", __func__
,
273 hdr
->version
, ntohs(hdr
->payload_len
), hdr
->nexthdr
,
276 lowpan_raw_dump_table(__func__
, "raw skb network header dump",
277 skb_network_header(skb
), sizeof(struct ipv6hdr
));
280 saddr
= dev
->dev_addr
;
282 lowpan_raw_dump_inline(__func__
, "saddr", (unsigned char *)saddr
, 8);
285 * As we copy some bit-length fields, in the IPHC encoding bytes,
286 * we sometimes use |=
287 * If the field is 0, and the current bit value in memory is 1,
288 * this does not work. We therefore reset the IPHC encoding here
290 iphc0
= LOWPAN_DISPATCH_IPHC
;
293 /* TODO: context lookup */
295 lowpan_raw_dump_inline(__func__
, "daddr", (unsigned char *)daddr
, 8);
298 * Traffic class, flow label
299 * If flow label is 0, compress it. If traffic class is 0, compress it
300 * We have to process both in the same time as the offset of traffic
301 * class depends on the presence of version and flow label
304 /* hc06 format of TC is ECN | DSCP , original one is DSCP | ECN */
305 tmp
= (hdr
->priority
<< 4) | (hdr
->flow_lbl
[0] >> 4);
306 tmp
= ((tmp
& 0x03) << 6) | (tmp
>> 2);
308 if (((hdr
->flow_lbl
[0] & 0x0F) == 0) &&
309 (hdr
->flow_lbl
[1] == 0) && (hdr
->flow_lbl
[2] == 0)) {
310 /* flow label can be compressed */
311 iphc0
|= LOWPAN_IPHC_FL_C
;
312 if ((hdr
->priority
== 0) &&
313 ((hdr
->flow_lbl
[0] & 0xF0) == 0)) {
314 /* compress (elide) all */
315 iphc0
|= LOWPAN_IPHC_TC_C
;
317 /* compress only the flow label */
322 /* Flow label cannot be compressed */
323 if ((hdr
->priority
== 0) &&
324 ((hdr
->flow_lbl
[0] & 0xF0) == 0)) {
325 /* compress only traffic class */
326 iphc0
|= LOWPAN_IPHC_TC_C
;
327 *hc06_ptr
= (tmp
& 0xc0) | (hdr
->flow_lbl
[0] & 0x0F);
328 memcpy(hc06_ptr
+ 1, &hdr
->flow_lbl
[1], 2);
331 /* compress nothing */
332 memcpy(hc06_ptr
, &hdr
, 4);
333 /* replace the top byte with new ECN | DSCP format */
339 /* NOTE: payload length is always compressed */
341 /* Next Header is compress if UDP */
342 if (hdr
->nexthdr
== UIP_PROTO_UDP
)
343 iphc0
|= LOWPAN_IPHC_NH_C
;
345 /* TODO: next header compression */
347 if ((iphc0
& LOWPAN_IPHC_NH_C
) == 0) {
348 *hc06_ptr
= hdr
->nexthdr
;
354 * if 1: compress, encoding is 01
355 * if 64: compress, encoding is 10
356 * if 255: compress, encoding is 11
357 * else do not compress
359 switch (hdr
->hop_limit
) {
361 iphc0
|= LOWPAN_IPHC_TTL_1
;
364 iphc0
|= LOWPAN_IPHC_TTL_64
;
367 iphc0
|= LOWPAN_IPHC_TTL_255
;
370 *hc06_ptr
= hdr
->hop_limit
;
374 /* source address compression */
375 if (is_addr_unspecified(&hdr
->saddr
)) {
376 pr_debug("(%s): source address is unspecified, setting SAC\n",
378 iphc1
|= LOWPAN_IPHC_SAC
;
379 /* TODO: context lookup */
380 } else if (is_addr_link_local(&hdr
->saddr
)) {
381 pr_debug("(%s): source address is link-local\n", __func__
);
382 iphc1
|= lowpan_compress_addr_64(&hc06_ptr
,
383 LOWPAN_IPHC_SAM_BIT
, &hdr
->saddr
, saddr
);
385 pr_debug("(%s): send the full source address\n", __func__
);
386 memcpy(hc06_ptr
, &hdr
->saddr
.s6_addr16
[0], 16);
390 /* destination address compression */
391 if (is_addr_mcast(&hdr
->daddr
)) {
392 pr_debug("(%s): destination address is multicast", __func__
);
393 iphc1
|= LOWPAN_IPHC_M
;
394 if (lowpan_is_mcast_addr_compressable8(&hdr
->daddr
)) {
395 pr_debug("compressed to 1 octet\n");
396 iphc1
|= LOWPAN_IPHC_DAM_11
;
398 *hc06_ptr
= hdr
->daddr
.s6_addr
[15];
400 } else if (lowpan_is_mcast_addr_compressable32(&hdr
->daddr
)) {
401 pr_debug("compressed to 4 octets\n");
402 iphc1
|= LOWPAN_IPHC_DAM_10
;
403 /* second byte + the last three */
404 *hc06_ptr
= hdr
->daddr
.s6_addr
[1];
405 memcpy(hc06_ptr
+ 1, &hdr
->daddr
.s6_addr
[13], 3);
407 } else if (lowpan_is_mcast_addr_compressable48(&hdr
->daddr
)) {
408 pr_debug("compressed to 6 octets\n");
409 iphc1
|= LOWPAN_IPHC_DAM_01
;
410 /* second byte + the last five */
411 *hc06_ptr
= hdr
->daddr
.s6_addr
[1];
412 memcpy(hc06_ptr
+ 1, &hdr
->daddr
.s6_addr
[11], 5);
415 pr_debug("using full address\n");
416 iphc1
|= LOWPAN_IPHC_DAM_00
;
417 memcpy(hc06_ptr
, &hdr
->daddr
.s6_addr
[0], 16);
421 pr_debug("(%s): destination address is unicast: ", __func__
);
422 /* TODO: context lookup */
423 if (is_addr_link_local(&hdr
->daddr
)) {
424 pr_debug("destination address is link-local\n");
425 iphc1
|= lowpan_compress_addr_64(&hc06_ptr
,
426 LOWPAN_IPHC_DAM_BIT
, &hdr
->daddr
, daddr
);
428 pr_debug("using full address\n");
429 memcpy(hc06_ptr
, &hdr
->daddr
.s6_addr16
[0], 16);
434 /* TODO: UDP header compression */
435 /* TODO: Next Header compression */
440 skb_pull(skb
, sizeof(struct ipv6hdr
));
441 memcpy(skb_push(skb
, hc06_ptr
- head
), head
, hc06_ptr
- head
);
445 lowpan_raw_dump_table(__func__
, "raw skb data dump", skb
->data
,
449 * NOTE1: I'm still unsure about the fact that compression and WPAN
450 * header are created here and not later in the xmit. So wait for
451 * an opinion of net maintainers.
454 * NOTE2: to be absolutely correct, we must derive PANid information
455 * from MAC subif of the 'dev' and 'real_dev' network devices, but
456 * this isn't implemented in mainline yet, so currently we assign 0xff
459 /* prepare wpan address data */
460 sa
.addr_type
= IEEE802154_ADDR_LONG
;
463 da
.addr_type
= IEEE802154_ADDR_LONG
;
466 memcpy(&(da
.hwaddr
), daddr
, 8);
467 memcpy(&(sa
.hwaddr
), saddr
, 8);
469 mac_cb(skb
)->flags
= IEEE802154_FC_TYPE_DATA
;
470 return dev_hard_header(skb
, lowpan_dev_info(dev
)->real_dev
,
471 type
, (void *)&da
, (void *)&sa
, skb
->len
);
475 static int lowpan_skb_deliver(struct sk_buff
*skb
, struct ipv6hdr
*hdr
)
478 struct lowpan_dev_record
*entry
;
479 int stat
= NET_RX_SUCCESS
;
481 new = skb_copy_expand(skb
, sizeof(struct ipv6hdr
), skb_tailroom(skb
),
488 skb_push(new, sizeof(struct ipv6hdr
));
489 skb_reset_network_header(new);
490 skb_copy_to_linear_data(new, hdr
, sizeof(struct ipv6hdr
));
492 new->protocol
= htons(ETH_P_IPV6
);
493 new->pkt_type
= PACKET_HOST
;
496 list_for_each_entry_rcu(entry
, &lowpan_devices
, list
)
497 if (lowpan_dev_info(entry
->ldev
)->real_dev
== new->dev
) {
498 skb
= skb_copy(new, GFP_ATOMIC
);
504 skb
->dev
= entry
->ldev
;
505 stat
= netif_rx(skb
);
515 lowpan_process_data(struct sk_buff
*skb
)
518 u8 tmp
, iphc0
, iphc1
, num_context
= 0;
522 lowpan_raw_dump_table(__func__
, "raw skb data dump", skb
->data
,
524 /* at least two bytes will be used for the encoding */
527 iphc0
= lowpan_fetch_skb_u8(skb
);
528 iphc1
= lowpan_fetch_skb_u8(skb
);
530 _saddr
= mac_cb(skb
)->sa
.hwaddr
;
531 _daddr
= mac_cb(skb
)->da
.hwaddr
;
533 pr_debug("(%s): iphc0 = %02x, iphc1 = %02x\n", __func__
, iphc0
, iphc1
);
535 /* another if the CID flag is set */
536 if (iphc1
& LOWPAN_IPHC_CID
) {
537 pr_debug("(%s): CID flag is set, increase header with one\n",
541 num_context
= lowpan_fetch_skb_u8(skb
);
546 /* Traffic Class and Flow Label */
547 switch ((iphc0
& LOWPAN_IPHC_TF
) >> 3) {
549 * Traffic Class and FLow Label carried in-line
550 * ECN + DSCP + 4-bit Pad + Flow Label (4 bytes)
555 tmp
= lowpan_fetch_skb_u8(skb
);
556 memcpy(&hdr
.flow_lbl
, &skb
->data
[0], 3);
558 hdr
.priority
= ((tmp
>> 2) & 0x0f);
559 hdr
.flow_lbl
[0] = ((tmp
>> 2) & 0x30) | (tmp
<< 6) |
560 (hdr
.flow_lbl
[0] & 0x0f);
563 * Traffic class carried in-line
564 * ECN + DSCP (1 byte), Flow Label is elided
569 tmp
= lowpan_fetch_skb_u8(skb
);
570 hdr
.priority
= ((tmp
>> 2) & 0x0f);
571 hdr
.flow_lbl
[0] = ((tmp
<< 6) & 0xC0) | ((tmp
>> 2) & 0x30);
576 * Flow Label carried in-line
577 * ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided
582 tmp
= lowpan_fetch_skb_u8(skb
);
583 hdr
.flow_lbl
[0] = (skb
->data
[0] & 0x0F) | ((tmp
>> 2) & 0x30);
584 memcpy(&hdr
.flow_lbl
[1], &skb
->data
[0], 2);
587 /* Traffic Class and Flow Label are elided */
599 if ((iphc0
& LOWPAN_IPHC_NH_C
) == 0) {
600 /* Next header is carried inline */
603 hdr
.nexthdr
= lowpan_fetch_skb_u8(skb
);
604 pr_debug("(%s): NH flag is set, next header is carried "
605 "inline: %02x\n", __func__
, hdr
.nexthdr
);
609 if ((iphc0
& 0x03) != LOWPAN_IPHC_TTL_I
)
610 hdr
.hop_limit
= lowpan_ttl_values
[iphc0
& 0x03];
614 hdr
.hop_limit
= lowpan_fetch_skb_u8(skb
);
617 /* Extract SAM to the tmp variable */
618 tmp
= ((iphc1
& LOWPAN_IPHC_SAM
) >> LOWPAN_IPHC_SAM_BIT
) & 0x03;
620 /* Source address uncompression */
621 pr_debug("(%s): source address stateless compression\n", __func__
);
622 err
= lowpan_uncompress_addr(skb
, &hdr
.saddr
, lowpan_llprefix
,
623 lowpan_unc_llconf
[tmp
], skb
->data
);
627 /* Extract DAM to the tmp variable */
628 tmp
= ((iphc1
& LOWPAN_IPHC_DAM_11
) >> LOWPAN_IPHC_DAM_BIT
) & 0x03;
630 /* check for Multicast Compression */
631 if (iphc1
& LOWPAN_IPHC_M
) {
632 if (iphc1
& LOWPAN_IPHC_DAC
) {
633 pr_debug("(%s): destination address context-based "
634 "multicast compression\n", __func__
);
635 /* TODO: implement this */
637 u8 prefix
[] = {0xff, 0x02};
639 pr_debug("(%s): destination address non-context-based"
640 " multicast compression\n", __func__
);
641 if (0 < tmp
&& tmp
< 3) {
645 prefix
[1] = lowpan_fetch_skb_u8(skb
);
648 err
= lowpan_uncompress_addr(skb
, &hdr
.daddr
, prefix
,
649 lowpan_unc_mxconf
[tmp
], NULL
);
654 pr_debug("(%s): destination address stateless compression\n",
656 err
= lowpan_uncompress_addr(skb
, &hdr
.daddr
, lowpan_llprefix
,
657 lowpan_unc_llconf
[tmp
], skb
->data
);
662 /* TODO: UDP header parse */
664 /* Not fragmented package */
665 hdr
.payload_len
= htons(skb
->len
);
667 pr_debug("(%s): skb headroom size = %d, data length = %d\n", __func__
,
668 skb_headroom(skb
), skb
->len
);
670 pr_debug("(%s): IPv6 header dump:\n\tversion = %d\n\tlength = %d\n\t"
671 "nexthdr = 0x%02x\n\thop_lim = %d\n", __func__
, hdr
.version
,
672 ntohs(hdr
.payload_len
), hdr
.nexthdr
, hdr
.hop_limit
);
674 lowpan_raw_dump_table(__func__
, "raw header dump", (u8
*)&hdr
,
676 return lowpan_skb_deliver(skb
, &hdr
);
682 static int lowpan_set_address(struct net_device
*dev
, void *p
)
684 struct sockaddr
*sa
= p
;
686 if (netif_running(dev
))
689 /* TODO: validate addr */
690 memcpy(dev
->dev_addr
, sa
->sa_data
, dev
->addr_len
);
695 static netdev_tx_t
lowpan_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
699 pr_debug("(%s): package xmit\n", __func__
);
701 skb
->dev
= lowpan_dev_info(dev
)->real_dev
;
702 if (skb
->dev
== NULL
) {
703 pr_debug("(%s) ERROR: no real wpan device found\n", __func__
);
706 err
= dev_queue_xmit(skb
);
708 return (err
< 0 ? NETDEV_TX_BUSY
: NETDEV_TX_OK
);
711 static void lowpan_dev_free(struct net_device
*dev
)
713 dev_put(lowpan_dev_info(dev
)->real_dev
);
717 static struct header_ops lowpan_header_ops
= {
718 .create
= lowpan_header_create
,
721 static const struct net_device_ops lowpan_netdev_ops
= {
722 .ndo_start_xmit
= lowpan_xmit
,
723 .ndo_set_mac_address
= lowpan_set_address
,
726 static void lowpan_setup(struct net_device
*dev
)
728 pr_debug("(%s)\n", __func__
);
730 dev
->addr_len
= IEEE802154_ADDR_LEN
;
731 memset(dev
->broadcast
, 0xff, IEEE802154_ADDR_LEN
);
732 dev
->type
= ARPHRD_IEEE802154
;
733 dev
->features
= NETIF_F_NO_CSUM
;
734 /* Frame Control + Sequence Number + Address fields + Security Header */
735 dev
->hard_header_len
= 2 + 1 + 20 + 14;
736 dev
->needed_tailroom
= 2; /* FCS */
738 dev
->tx_queue_len
= 0;
739 dev
->flags
= IFF_NOARP
| IFF_BROADCAST
;
740 dev
->watchdog_timeo
= 0;
742 dev
->netdev_ops
= &lowpan_netdev_ops
;
743 dev
->header_ops
= &lowpan_header_ops
;
744 dev
->destructor
= lowpan_dev_free
;
747 static int lowpan_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
749 pr_debug("(%s)\n", __func__
);
751 if (tb
[IFLA_ADDRESS
]) {
752 if (nla_len(tb
[IFLA_ADDRESS
]) != IEEE802154_ADDR_LEN
)
758 static int lowpan_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
759 struct packet_type
*pt
, struct net_device
*orig_dev
)
761 if (!netif_running(dev
))
764 if (dev
->type
!= ARPHRD_IEEE802154
)
767 /* check that it's our buffer */
768 if ((skb
->data
[0] & 0xe0) == 0x60)
769 lowpan_process_data(skb
);
771 return NET_RX_SUCCESS
;
778 static int lowpan_newlink(struct net
*src_net
, struct net_device
*dev
,
779 struct nlattr
*tb
[], struct nlattr
*data
[])
781 struct net_device
*real_dev
;
782 struct lowpan_dev_record
*entry
;
784 pr_debug("(%s)\n", __func__
);
788 /* find and hold real wpan device */
789 real_dev
= dev_get_by_index(src_net
, nla_get_u32(tb
[IFLA_LINK
]));
793 lowpan_dev_info(dev
)->real_dev
= real_dev
;
794 mutex_init(&lowpan_dev_info(dev
)->dev_list_mtx
);
796 entry
= kzalloc(sizeof(struct lowpan_dev_record
), GFP_KERNEL
);
799 lowpan_dev_info(dev
)->real_dev
= NULL
;
805 mutex_lock(&lowpan_dev_info(dev
)->dev_list_mtx
);
806 INIT_LIST_HEAD(&entry
->list
);
807 list_add_tail(&entry
->list
, &lowpan_devices
);
808 mutex_unlock(&lowpan_dev_info(dev
)->dev_list_mtx
);
810 register_netdevice(dev
);
815 static void lowpan_dellink(struct net_device
*dev
, struct list_head
*head
)
817 struct lowpan_dev_info
*lowpan_dev
= lowpan_dev_info(dev
);
818 struct net_device
*real_dev
= lowpan_dev
->real_dev
;
819 struct lowpan_dev_record
*entry
;
820 struct lowpan_dev_record
*tmp
;
824 mutex_lock(&lowpan_dev_info(dev
)->dev_list_mtx
);
825 list_for_each_entry_safe(entry
, tmp
, &lowpan_devices
, list
) {
826 if (entry
->ldev
== dev
) {
827 list_del(&entry
->list
);
831 mutex_unlock(&lowpan_dev_info(dev
)->dev_list_mtx
);
833 mutex_destroy(&lowpan_dev_info(dev
)->dev_list_mtx
);
835 unregister_netdevice_queue(dev
, head
);
840 static struct rtnl_link_ops lowpan_link_ops __read_mostly
= {
842 .priv_size
= sizeof(struct lowpan_dev_info
),
843 .setup
= lowpan_setup
,
844 .newlink
= lowpan_newlink
,
845 .dellink
= lowpan_dellink
,
846 .validate
= lowpan_validate
,
849 static inline int __init
lowpan_netlink_init(void)
851 return rtnl_link_register(&lowpan_link_ops
);
854 static inline void __init
lowpan_netlink_fini(void)
856 rtnl_link_unregister(&lowpan_link_ops
);
859 static struct packet_type lowpan_packet_type
= {
860 .type
= __constant_htons(ETH_P_IEEE802154
),
864 static int __init
lowpan_init_module(void)
868 pr_debug("(%s)\n", __func__
);
870 err
= lowpan_netlink_init();
874 dev_add_pack(&lowpan_packet_type
);
879 static void __exit
lowpan_cleanup_module(void)
881 pr_debug("(%s)\n", __func__
);
883 lowpan_netlink_fini();
885 dev_remove_pack(&lowpan_packet_type
);
888 module_init(lowpan_init_module
);
889 module_exit(lowpan_cleanup_module
);
890 MODULE_LICENSE("GPL");
891 MODULE_ALIAS_RTNL_LINK("lowpan");