• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2011, Siemens AG
2  * written by Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
3  */
4 
5 /* Based on patches from Jon Smirl <jonsmirl@gmail.com>
6  * Copyright (c) 2011 Jon Smirl <jonsmirl@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17 
18 /* Jon's code is based on 6lowpan implementation for Contiki which is:
19  * Copyright (c) 2008, Swedish Institute of Computer Science.
20  * All rights reserved.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the above copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. Neither the name of the Institute nor the names of its contributors
31  *    may be used to endorse or promote products derived from this software
32  *    without specific prior written permission.
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
35  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
38  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  */
46 
47 #include <linux/bitops.h>
48 #include <linux/if_arp.h>
49 #include <linux/module.h>
50 #include <linux/moduleparam.h>
51 #include <linux/netdevice.h>
52 #include <net/af_ieee802154.h>
53 #include <net/ieee802154.h>
54 #include <net/ieee802154_netdev.h>
55 #include <net/6lowpan.h>
56 #include <net/ipv6.h>
57 
58 #include "reassembly.h"
59 
60 static LIST_HEAD(lowpan_devices);
61 
62 /* private device info */
63 struct lowpan_dev_info {
64 	struct net_device	*real_dev; /* real WPAN device ptr */
65 	struct mutex		dev_list_mtx; /* mutex for list ops */
66 	__be16			fragment_tag;
67 };
68 
69 struct lowpan_dev_record {
70 	struct net_device *ldev;
71 	struct list_head list;
72 };
73 
74 /* don't save pan id, it's intra pan */
75 struct lowpan_addr {
76 	u8 mode;
77 	union {
78 		/* IPv6 needs big endian here */
79 		__be64 extended_addr;
80 		__be16 short_addr;
81 	} u;
82 };
83 
84 struct lowpan_addr_info {
85 	struct lowpan_addr daddr;
86 	struct lowpan_addr saddr;
87 };
88 
89 static inline struct
lowpan_dev_info(const struct net_device * dev)90 lowpan_dev_info *lowpan_dev_info(const struct net_device *dev)
91 {
92 	return netdev_priv(dev);
93 }
94 
95 static inline struct
lowpan_skb_priv(const struct sk_buff * skb)96 lowpan_addr_info *lowpan_skb_priv(const struct sk_buff *skb)
97 {
98 	WARN_ON_ONCE(skb_headroom(skb) < sizeof(struct lowpan_addr_info));
99 	return (struct lowpan_addr_info *)(skb->data -
100 			sizeof(struct lowpan_addr_info));
101 }
102 
lowpan_header_create(struct sk_buff * skb,struct net_device * dev,unsigned short type,const void * _daddr,const void * _saddr,unsigned int len)103 static int lowpan_header_create(struct sk_buff *skb, struct net_device *dev,
104 				unsigned short type, const void *_daddr,
105 				const void *_saddr, unsigned int len)
106 {
107 	const u8 *saddr = _saddr;
108 	const u8 *daddr = _daddr;
109 	struct lowpan_addr_info *info;
110 
111 	/* TODO:
112 	 * if this package isn't ipv6 one, where should it be routed?
113 	 */
114 	if (type != ETH_P_IPV6)
115 		return 0;
116 
117 	if (!saddr)
118 		saddr = dev->dev_addr;
119 
120 	raw_dump_inline(__func__, "saddr", (unsigned char *)saddr, 8);
121 	raw_dump_inline(__func__, "daddr", (unsigned char *)daddr, 8);
122 
123 	info = lowpan_skb_priv(skb);
124 
125 	/* TODO: Currently we only support extended_addr */
126 	info->daddr.mode = IEEE802154_ADDR_LONG;
127 	memcpy(&info->daddr.u.extended_addr, daddr,
128 	       sizeof(info->daddr.u.extended_addr));
129 	info->saddr.mode = IEEE802154_ADDR_LONG;
130 	memcpy(&info->saddr.u.extended_addr, saddr,
131 	       sizeof(info->daddr.u.extended_addr));
132 
133 	return 0;
134 }
135 
lowpan_give_skb_to_devices(struct sk_buff * skb,struct net_device * dev)136 static int lowpan_give_skb_to_devices(struct sk_buff *skb,
137 				      struct net_device *dev)
138 {
139 	struct lowpan_dev_record *entry;
140 	struct sk_buff *skb_cp;
141 	int stat = NET_RX_SUCCESS;
142 
143 	rcu_read_lock();
144 	list_for_each_entry_rcu(entry, &lowpan_devices, list)
145 		if (lowpan_dev_info(entry->ldev)->real_dev == skb->dev) {
146 			skb_cp = skb_copy(skb, GFP_ATOMIC);
147 			if (!skb_cp) {
148 				stat = -ENOMEM;
149 				break;
150 			}
151 
152 			skb_cp->dev = entry->ldev;
153 			stat = netif_rx(skb_cp);
154 		}
155 	rcu_read_unlock();
156 
157 	return stat;
158 }
159 
process_data(struct sk_buff * skb,const struct ieee802154_hdr * hdr)160 static int process_data(struct sk_buff *skb, const struct ieee802154_hdr *hdr)
161 {
162 	u8 iphc0, iphc1;
163 	struct ieee802154_addr_sa sa, da;
164 	void *sap, *dap;
165 
166 	raw_dump_table(__func__, "raw skb data dump", skb->data, skb->len);
167 	/* at least two bytes will be used for the encoding */
168 	if (skb->len < 2)
169 		goto drop;
170 
171 	if (lowpan_fetch_skb_u8(skb, &iphc0))
172 		goto drop;
173 
174 	if (lowpan_fetch_skb_u8(skb, &iphc1))
175 		goto drop;
176 
177 	ieee802154_addr_to_sa(&sa, &hdr->source);
178 	ieee802154_addr_to_sa(&da, &hdr->dest);
179 
180 	if (sa.addr_type == IEEE802154_ADDR_SHORT)
181 		sap = &sa.short_addr;
182 	else
183 		sap = &sa.hwaddr;
184 
185 	if (da.addr_type == IEEE802154_ADDR_SHORT)
186 		dap = &da.short_addr;
187 	else
188 		dap = &da.hwaddr;
189 
190 	return lowpan_process_data(skb, skb->dev, sap, sa.addr_type,
191 				   IEEE802154_ADDR_LEN, dap, da.addr_type,
192 				   IEEE802154_ADDR_LEN, iphc0, iphc1,
193 				   lowpan_give_skb_to_devices);
194 
195 drop:
196 	kfree_skb(skb);
197 	return -EINVAL;
198 }
199 
lowpan_set_address(struct net_device * dev,void * p)200 static int lowpan_set_address(struct net_device *dev, void *p)
201 {
202 	struct sockaddr *sa = p;
203 
204 	if (netif_running(dev))
205 		return -EBUSY;
206 
207 	/* TODO: validate addr */
208 	memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
209 
210 	return 0;
211 }
212 
213 static struct sk_buff*
lowpan_alloc_frag(struct sk_buff * skb,int size,const struct ieee802154_hdr * master_hdr)214 lowpan_alloc_frag(struct sk_buff *skb, int size,
215 		  const struct ieee802154_hdr *master_hdr)
216 {
217 	struct net_device *real_dev = lowpan_dev_info(skb->dev)->real_dev;
218 	struct sk_buff *frag;
219 	int rc;
220 
221 	frag = alloc_skb(real_dev->hard_header_len +
222 			 real_dev->needed_tailroom + size,
223 			 GFP_ATOMIC);
224 
225 	if (likely(frag)) {
226 		frag->dev = real_dev;
227 		frag->priority = skb->priority;
228 		skb_reserve(frag, real_dev->hard_header_len);
229 		skb_reset_network_header(frag);
230 		*mac_cb(frag) = *mac_cb(skb);
231 
232 		rc = dev_hard_header(frag, real_dev, 0, &master_hdr->dest,
233 				     &master_hdr->source, size);
234 		if (rc < 0) {
235 			kfree_skb(frag);
236 			return ERR_PTR(-rc);
237 		}
238 	} else {
239 		frag = ERR_PTR(-ENOMEM);
240 	}
241 
242 	return frag;
243 }
244 
245 static int
lowpan_xmit_fragment(struct sk_buff * skb,const struct ieee802154_hdr * wpan_hdr,u8 * frag_hdr,int frag_hdrlen,int offset,int len)246 lowpan_xmit_fragment(struct sk_buff *skb, const struct ieee802154_hdr *wpan_hdr,
247 		     u8 *frag_hdr, int frag_hdrlen,
248 		     int offset, int len)
249 {
250 	struct sk_buff *frag;
251 
252 	raw_dump_inline(__func__, " fragment header", frag_hdr, frag_hdrlen);
253 
254 	frag = lowpan_alloc_frag(skb, frag_hdrlen + len, wpan_hdr);
255 	if (IS_ERR(frag))
256 		return -PTR_ERR(frag);
257 
258 	memcpy(skb_put(frag, frag_hdrlen), frag_hdr, frag_hdrlen);
259 	memcpy(skb_put(frag, len), skb_network_header(skb) + offset, len);
260 
261 	raw_dump_table(__func__, " fragment dump", frag->data, frag->len);
262 
263 	return dev_queue_xmit(frag);
264 }
265 
266 static int
lowpan_xmit_fragmented(struct sk_buff * skb,struct net_device * dev,const struct ieee802154_hdr * wpan_hdr)267 lowpan_xmit_fragmented(struct sk_buff *skb, struct net_device *dev,
268 		       const struct ieee802154_hdr *wpan_hdr)
269 {
270 	u16 dgram_size, dgram_offset;
271 	__be16 frag_tag;
272 	u8 frag_hdr[5];
273 	int frag_cap, frag_len, payload_cap, rc;
274 	int skb_unprocessed, skb_offset;
275 
276 	dgram_size = lowpan_uncompress_size(skb, &dgram_offset) -
277 		     skb->mac_len;
278 	frag_tag = lowpan_dev_info(dev)->fragment_tag++;
279 
280 	frag_hdr[0] = LOWPAN_DISPATCH_FRAG1 | ((dgram_size >> 8) & 0x07);
281 	frag_hdr[1] = dgram_size & 0xff;
282 	memcpy(frag_hdr + 2, &frag_tag, sizeof(frag_tag));
283 
284 	payload_cap = ieee802154_max_payload(wpan_hdr);
285 
286 	frag_len = round_down(payload_cap - LOWPAN_FRAG1_HEAD_SIZE -
287 			      skb_network_header_len(skb), 8);
288 
289 	skb_offset = skb_network_header_len(skb);
290 	skb_unprocessed = skb->len - skb->mac_len - skb_offset;
291 
292 	rc = lowpan_xmit_fragment(skb, wpan_hdr, frag_hdr,
293 				  LOWPAN_FRAG1_HEAD_SIZE, 0,
294 				  frag_len + skb_network_header_len(skb));
295 	if (rc) {
296 		pr_debug("%s unable to send FRAG1 packet (tag: %d)",
297 			 __func__, frag_tag);
298 		goto err;
299 	}
300 
301 	frag_hdr[0] &= ~LOWPAN_DISPATCH_FRAG1;
302 	frag_hdr[0] |= LOWPAN_DISPATCH_FRAGN;
303 	frag_cap = round_down(payload_cap - LOWPAN_FRAGN_HEAD_SIZE, 8);
304 
305 	do {
306 		dgram_offset += frag_len;
307 		skb_offset += frag_len;
308 		skb_unprocessed -= frag_len;
309 		frag_len = min(frag_cap, skb_unprocessed);
310 
311 		frag_hdr[4] = dgram_offset >> 3;
312 
313 		rc = lowpan_xmit_fragment(skb, wpan_hdr, frag_hdr,
314 					  LOWPAN_FRAGN_HEAD_SIZE, skb_offset,
315 					  frag_len);
316 		if (rc) {
317 			pr_debug("%s unable to send a FRAGN packet. (tag: %d, offset: %d)\n",
318 				 __func__, frag_tag, skb_offset);
319 			goto err;
320 		}
321 	} while (skb_unprocessed > frag_cap);
322 
323 	consume_skb(skb);
324 	return NET_XMIT_SUCCESS;
325 
326 err:
327 	kfree_skb(skb);
328 	return rc;
329 }
330 
lowpan_header(struct sk_buff * skb,struct net_device * dev)331 static int lowpan_header(struct sk_buff *skb, struct net_device *dev)
332 {
333 	struct ieee802154_addr sa, da;
334 	struct ieee802154_mac_cb *cb = mac_cb_init(skb);
335 	struct lowpan_addr_info info;
336 	void *daddr, *saddr;
337 
338 	memcpy(&info, lowpan_skb_priv(skb), sizeof(info));
339 
340 	/* TODO: Currently we only support extended_addr */
341 	daddr = &info.daddr.u.extended_addr;
342 	saddr = &info.saddr.u.extended_addr;
343 
344 	lowpan_header_compress(skb, dev, ETH_P_IPV6, daddr, saddr, skb->len);
345 
346 	cb->type = IEEE802154_FC_TYPE_DATA;
347 
348 	/* prepare wpan address data */
349 	sa.mode = IEEE802154_ADDR_LONG;
350 	sa.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
351 	sa.extended_addr = ieee802154_devaddr_from_raw(saddr);
352 
353 	/* intra-PAN communications */
354 	da.pan_id = sa.pan_id;
355 
356 	/* if the destination address is the broadcast address, use the
357 	 * corresponding short address
358 	 */
359 	if (lowpan_is_addr_broadcast((const u8 *)daddr)) {
360 		da.mode = IEEE802154_ADDR_SHORT;
361 		da.short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST);
362 		cb->ackreq = false;
363 	} else {
364 		da.mode = IEEE802154_ADDR_LONG;
365 		da.extended_addr = ieee802154_devaddr_from_raw(daddr);
366 		cb->ackreq = true;
367 	}
368 
369 	return dev_hard_header(skb, lowpan_dev_info(dev)->real_dev,
370 			ETH_P_IPV6, (void *)&da, (void *)&sa, 0);
371 }
372 
lowpan_xmit(struct sk_buff * skb,struct net_device * dev)373 static netdev_tx_t lowpan_xmit(struct sk_buff *skb, struct net_device *dev)
374 {
375 	struct ieee802154_hdr wpan_hdr;
376 	int max_single, ret;
377 
378 	pr_debug("package xmit\n");
379 
380 	/* We must take a copy of the skb before we modify/replace the ipv6
381 	 * header as the header could be used elsewhere
382 	 */
383 	skb = skb_unshare(skb, GFP_ATOMIC);
384 	if (!skb)
385 		return NET_XMIT_DROP;
386 
387 	ret = lowpan_header(skb, dev);
388 	if (ret < 0) {
389 		kfree_skb(skb);
390 		return NET_XMIT_DROP;
391 	}
392 
393 	if (ieee802154_hdr_peek(skb, &wpan_hdr) < 0) {
394 		kfree_skb(skb);
395 		return NET_XMIT_DROP;
396 	}
397 
398 	max_single = ieee802154_max_payload(&wpan_hdr);
399 
400 	if (skb_tail_pointer(skb) - skb_network_header(skb) <= max_single) {
401 		skb->dev = lowpan_dev_info(dev)->real_dev;
402 		return dev_queue_xmit(skb);
403 	} else {
404 		netdev_tx_t rc;
405 
406 		pr_debug("frame is too big, fragmentation is needed\n");
407 		rc = lowpan_xmit_fragmented(skb, dev, &wpan_hdr);
408 
409 		return rc < 0 ? NET_XMIT_DROP : rc;
410 	}
411 }
412 
lowpan_get_phy(const struct net_device * dev)413 static struct wpan_phy *lowpan_get_phy(const struct net_device *dev)
414 {
415 	struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
416 
417 	return ieee802154_mlme_ops(real_dev)->get_phy(real_dev);
418 }
419 
lowpan_get_pan_id(const struct net_device * dev)420 static __le16 lowpan_get_pan_id(const struct net_device *dev)
421 {
422 	struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
423 
424 	return ieee802154_mlme_ops(real_dev)->get_pan_id(real_dev);
425 }
426 
lowpan_get_short_addr(const struct net_device * dev)427 static __le16 lowpan_get_short_addr(const struct net_device *dev)
428 {
429 	struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
430 
431 	return ieee802154_mlme_ops(real_dev)->get_short_addr(real_dev);
432 }
433 
lowpan_get_dsn(const struct net_device * dev)434 static u8 lowpan_get_dsn(const struct net_device *dev)
435 {
436 	struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
437 
438 	return ieee802154_mlme_ops(real_dev)->get_dsn(real_dev);
439 }
440 
441 static struct header_ops lowpan_header_ops = {
442 	.create	= lowpan_header_create,
443 };
444 
445 static struct lock_class_key lowpan_tx_busylock;
446 static struct lock_class_key lowpan_netdev_xmit_lock_key;
447 
lowpan_set_lockdep_class_one(struct net_device * dev,struct netdev_queue * txq,void * _unused)448 static void lowpan_set_lockdep_class_one(struct net_device *dev,
449 					 struct netdev_queue *txq,
450 					 void *_unused)
451 {
452 	lockdep_set_class(&txq->_xmit_lock,
453 			  &lowpan_netdev_xmit_lock_key);
454 }
455 
456 
lowpan_dev_init(struct net_device * dev)457 static int lowpan_dev_init(struct net_device *dev)
458 {
459 	netdev_for_each_tx_queue(dev, lowpan_set_lockdep_class_one, NULL);
460 	dev->qdisc_tx_busylock = &lowpan_tx_busylock;
461 	return 0;
462 }
463 
464 static const struct net_device_ops lowpan_netdev_ops = {
465 	.ndo_init		= lowpan_dev_init,
466 	.ndo_start_xmit		= lowpan_xmit,
467 	.ndo_set_mac_address	= lowpan_set_address,
468 };
469 
470 static struct ieee802154_mlme_ops lowpan_mlme = {
471 	.get_pan_id = lowpan_get_pan_id,
472 	.get_phy = lowpan_get_phy,
473 	.get_short_addr = lowpan_get_short_addr,
474 	.get_dsn = lowpan_get_dsn,
475 };
476 
lowpan_setup(struct net_device * dev)477 static void lowpan_setup(struct net_device *dev)
478 {
479 	dev->addr_len		= IEEE802154_ADDR_LEN;
480 	memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
481 	dev->type		= ARPHRD_IEEE802154;
482 	/* Frame Control + Sequence Number + Address fields + Security Header */
483 	dev->hard_header_len	= 2 + 1 + 20 + 14;
484 	dev->needed_tailroom	= 2; /* FCS */
485 	dev->mtu		= IPV6_MIN_MTU;
486 	dev->tx_queue_len	= 0;
487 	dev->flags		= IFF_BROADCAST | IFF_MULTICAST;
488 	dev->watchdog_timeo	= 0;
489 
490 	dev->netdev_ops		= &lowpan_netdev_ops;
491 	dev->header_ops		= &lowpan_header_ops;
492 	dev->ml_priv		= &lowpan_mlme;
493 	dev->destructor		= free_netdev;
494 }
495 
lowpan_validate(struct nlattr * tb[],struct nlattr * data[])496 static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[])
497 {
498 	if (tb[IFLA_ADDRESS]) {
499 		if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN)
500 			return -EINVAL;
501 	}
502 	return 0;
503 }
504 
lowpan_rcv(struct sk_buff * skb,struct net_device * dev,struct packet_type * pt,struct net_device * orig_dev)505 static int lowpan_rcv(struct sk_buff *skb, struct net_device *dev,
506 		      struct packet_type *pt, struct net_device *orig_dev)
507 {
508 	struct ieee802154_hdr hdr;
509 	int ret;
510 
511 	skb = skb_share_check(skb, GFP_ATOMIC);
512 	if (!skb)
513 		goto drop;
514 
515 	if (!netif_running(dev))
516 		goto drop_skb;
517 
518 	if (dev->type != ARPHRD_IEEE802154)
519 		goto drop_skb;
520 
521 	if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0)
522 		goto drop_skb;
523 
524 	/* check that it's our buffer */
525 	if (skb->data[0] == LOWPAN_DISPATCH_IPV6) {
526 		skb->protocol = htons(ETH_P_IPV6);
527 		skb->pkt_type = PACKET_HOST;
528 
529 		/* Pull off the 1-byte of 6lowpan header. */
530 		skb_pull(skb, 1);
531 
532 		ret = lowpan_give_skb_to_devices(skb, NULL);
533 		if (ret == NET_RX_DROP)
534 			goto drop;
535 	} else {
536 		switch (skb->data[0] & 0xe0) {
537 		case LOWPAN_DISPATCH_IPHC:	/* ipv6 datagram */
538 			ret = process_data(skb, &hdr);
539 			if (ret == NET_RX_DROP)
540 				goto drop;
541 			break;
542 		case LOWPAN_DISPATCH_FRAG1:	/* first fragment header */
543 			ret = lowpan_frag_rcv(skb, LOWPAN_DISPATCH_FRAG1);
544 			if (ret == 1) {
545 				ret = process_data(skb, &hdr);
546 				if (ret == NET_RX_DROP)
547 					goto drop;
548 			}
549 			break;
550 		case LOWPAN_DISPATCH_FRAGN:	/* next fragments headers */
551 			ret = lowpan_frag_rcv(skb, LOWPAN_DISPATCH_FRAGN);
552 			if (ret == 1) {
553 				ret = process_data(skb, &hdr);
554 				if (ret == NET_RX_DROP)
555 					goto drop;
556 			}
557 			break;
558 		default:
559 			break;
560 		}
561 	}
562 
563 	return NET_RX_SUCCESS;
564 drop_skb:
565 	kfree_skb(skb);
566 drop:
567 	return NET_RX_DROP;
568 }
569 
lowpan_newlink(struct net * src_net,struct net_device * dev,struct nlattr * tb[],struct nlattr * data[])570 static int lowpan_newlink(struct net *src_net, struct net_device *dev,
571 			  struct nlattr *tb[], struct nlattr *data[])
572 {
573 	struct net_device *real_dev;
574 	struct lowpan_dev_record *entry;
575 
576 	pr_debug("adding new link\n");
577 
578 	if (!tb[IFLA_LINK])
579 		return -EINVAL;
580 	/* find and hold real wpan device */
581 	real_dev = dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
582 	if (!real_dev)
583 		return -ENODEV;
584 	if (real_dev->type != ARPHRD_IEEE802154) {
585 		dev_put(real_dev);
586 		return -EINVAL;
587 	}
588 
589 	lowpan_dev_info(dev)->real_dev = real_dev;
590 	mutex_init(&lowpan_dev_info(dev)->dev_list_mtx);
591 
592 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
593 	if (!entry) {
594 		dev_put(real_dev);
595 		lowpan_dev_info(dev)->real_dev = NULL;
596 		return -ENOMEM;
597 	}
598 
599 	entry->ldev = dev;
600 
601 	/* Set the lowpan harware address to the wpan hardware address. */
602 	memcpy(dev->dev_addr, real_dev->dev_addr, IEEE802154_ADDR_LEN);
603 
604 	mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
605 	INIT_LIST_HEAD(&entry->list);
606 	list_add_tail(&entry->list, &lowpan_devices);
607 	mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
608 
609 	register_netdevice(dev);
610 
611 	return 0;
612 }
613 
lowpan_dellink(struct net_device * dev,struct list_head * head)614 static void lowpan_dellink(struct net_device *dev, struct list_head *head)
615 {
616 	struct lowpan_dev_info *lowpan_dev = lowpan_dev_info(dev);
617 	struct net_device *real_dev = lowpan_dev->real_dev;
618 	struct lowpan_dev_record *entry, *tmp;
619 
620 	ASSERT_RTNL();
621 
622 	mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
623 	list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
624 		if (entry->ldev == dev) {
625 			list_del(&entry->list);
626 			kfree(entry);
627 		}
628 	}
629 	mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
630 
631 	mutex_destroy(&lowpan_dev_info(dev)->dev_list_mtx);
632 
633 	unregister_netdevice_queue(dev, head);
634 
635 	dev_put(real_dev);
636 }
637 
638 static struct rtnl_link_ops lowpan_link_ops __read_mostly = {
639 	.kind		= "lowpan",
640 	.priv_size	= sizeof(struct lowpan_dev_info),
641 	.setup		= lowpan_setup,
642 	.newlink	= lowpan_newlink,
643 	.dellink	= lowpan_dellink,
644 	.validate	= lowpan_validate,
645 };
646 
lowpan_netlink_init(void)647 static inline int __init lowpan_netlink_init(void)
648 {
649 	return rtnl_link_register(&lowpan_link_ops);
650 }
651 
lowpan_netlink_fini(void)652 static inline void lowpan_netlink_fini(void)
653 {
654 	rtnl_link_unregister(&lowpan_link_ops);
655 }
656 
lowpan_device_event(struct notifier_block * unused,unsigned long event,void * ptr)657 static int lowpan_device_event(struct notifier_block *unused,
658 			       unsigned long event, void *ptr)
659 {
660 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
661 	LIST_HEAD(del_list);
662 	struct lowpan_dev_record *entry, *tmp;
663 
664 	if (dev->type != ARPHRD_IEEE802154)
665 		goto out;
666 
667 	if (event == NETDEV_UNREGISTER) {
668 		list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
669 			if (lowpan_dev_info(entry->ldev)->real_dev == dev)
670 				lowpan_dellink(entry->ldev, &del_list);
671 		}
672 
673 		unregister_netdevice_many(&del_list);
674 	}
675 
676 out:
677 	return NOTIFY_DONE;
678 }
679 
680 static struct notifier_block lowpan_dev_notifier = {
681 	.notifier_call = lowpan_device_event,
682 };
683 
684 static struct packet_type lowpan_packet_type = {
685 	.type = htons(ETH_P_IEEE802154),
686 	.func = lowpan_rcv,
687 };
688 
lowpan_init_module(void)689 static int __init lowpan_init_module(void)
690 {
691 	int err = 0;
692 
693 	err = lowpan_net_frag_init();
694 	if (err < 0)
695 		goto out;
696 
697 	err = lowpan_netlink_init();
698 	if (err < 0)
699 		goto out_frag;
700 
701 	dev_add_pack(&lowpan_packet_type);
702 
703 	err = register_netdevice_notifier(&lowpan_dev_notifier);
704 	if (err < 0)
705 		goto out_pack;
706 
707 	return 0;
708 
709 out_pack:
710 	dev_remove_pack(&lowpan_packet_type);
711 	lowpan_netlink_fini();
712 out_frag:
713 	lowpan_net_frag_exit();
714 out:
715 	return err;
716 }
717 
lowpan_cleanup_module(void)718 static void __exit lowpan_cleanup_module(void)
719 {
720 	lowpan_netlink_fini();
721 
722 	dev_remove_pack(&lowpan_packet_type);
723 
724 	lowpan_net_frag_exit();
725 
726 	unregister_netdevice_notifier(&lowpan_dev_notifier);
727 }
728 
729 module_init(lowpan_init_module);
730 module_exit(lowpan_cleanup_module);
731 MODULE_LICENSE("GPL");
732 MODULE_ALIAS_RTNL_LINK("lowpan");
733