xref: /linux/net/mac802154/iface.c (revision 41e0d49104dbff888ef6446ea46842fde66c0a76)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2007-2012 Siemens AG
4  *
5  * Written by:
6  * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
7  * Sergey Lapin <slapin@ossfans.org>
8  * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
9  * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
10  */
11 
12 #include <linux/netdevice.h>
13 #include <linux/module.h>
14 #include <linux/if_arp.h>
15 #include <linux/ieee802154.h>
16 
17 #include <net/nl802154.h>
18 #include <net/mac802154.h>
19 #include <net/ieee802154_netdev.h>
20 #include <net/cfg802154.h>
21 
22 #include "ieee802154_i.h"
23 #include "driver-ops.h"
24 
25 int mac802154_wpan_update_llsec(struct net_device *dev)
26 {
27 	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
28 	struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
29 	struct wpan_dev *wpan_dev = &sdata->wpan_dev;
30 	int rc = 0;
31 
32 	if (ops->llsec) {
33 		struct ieee802154_llsec_params params;
34 		int changed = 0;
35 
36 		params.pan_id = wpan_dev->pan_id;
37 		changed |= IEEE802154_LLSEC_PARAM_PAN_ID;
38 
39 		params.hwaddr = wpan_dev->extended_addr;
40 		changed |= IEEE802154_LLSEC_PARAM_HWADDR;
41 
42 		rc = ops->llsec->set_params(dev, &params, changed);
43 	}
44 
45 	return rc;
46 }
47 
48 static int
49 mac802154_wpan_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
50 {
51 	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
52 	struct wpan_dev *wpan_dev = &sdata->wpan_dev;
53 	struct sockaddr_ieee802154 *sa =
54 		(struct sockaddr_ieee802154 *)&ifr->ifr_addr;
55 	int err = -ENOIOCTLCMD;
56 
57 	if (cmd != SIOCGIFADDR && cmd != SIOCSIFADDR)
58 		return err;
59 
60 	rtnl_lock();
61 
62 	switch (cmd) {
63 	case SIOCGIFADDR:
64 	{
65 		u16 pan_id, short_addr;
66 
67 		pan_id = le16_to_cpu(wpan_dev->pan_id);
68 		short_addr = le16_to_cpu(wpan_dev->short_addr);
69 		if (pan_id == IEEE802154_PANID_BROADCAST ||
70 		    short_addr == IEEE802154_ADDR_BROADCAST) {
71 			err = -EADDRNOTAVAIL;
72 			break;
73 		}
74 
75 		sa->family = AF_IEEE802154;
76 		sa->addr.addr_type = IEEE802154_ADDR_SHORT;
77 		sa->addr.pan_id = pan_id;
78 		sa->addr.short_addr = short_addr;
79 
80 		err = 0;
81 		break;
82 	}
83 	case SIOCSIFADDR:
84 		if (netif_running(dev)) {
85 			rtnl_unlock();
86 			return -EBUSY;
87 		}
88 
89 		dev_warn(&dev->dev,
90 			 "Using DEBUGing ioctl SIOCSIFADDR isn't recommended!\n");
91 		if (sa->family != AF_IEEE802154 ||
92 		    sa->addr.addr_type != IEEE802154_ADDR_SHORT ||
93 		    sa->addr.pan_id == IEEE802154_PANID_BROADCAST ||
94 		    sa->addr.short_addr == IEEE802154_ADDR_BROADCAST ||
95 		    sa->addr.short_addr == IEEE802154_ADDR_UNDEF) {
96 			err = -EINVAL;
97 			break;
98 		}
99 
100 		wpan_dev->pan_id = cpu_to_le16(sa->addr.pan_id);
101 		wpan_dev->short_addr = cpu_to_le16(sa->addr.short_addr);
102 
103 		err = mac802154_wpan_update_llsec(dev);
104 		break;
105 	}
106 
107 	rtnl_unlock();
108 	return err;
109 }
110 
111 static int mac802154_wpan_mac_addr(struct net_device *dev, void *p)
112 {
113 	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
114 	struct sockaddr *addr = p;
115 	__le64 extended_addr;
116 
117 	if (netif_running(dev))
118 		return -EBUSY;
119 
120 	/* lowpan need to be down for update
121 	 * SLAAC address after ifup
122 	 */
123 	if (sdata->wpan_dev.lowpan_dev) {
124 		if (netif_running(sdata->wpan_dev.lowpan_dev))
125 			return -EBUSY;
126 	}
127 
128 	ieee802154_be64_to_le64(&extended_addr, addr->sa_data);
129 	if (!ieee802154_is_valid_extended_unicast_addr(extended_addr))
130 		return -EINVAL;
131 
132 	dev_addr_set(dev, addr->sa_data);
133 	sdata->wpan_dev.extended_addr = extended_addr;
134 
135 	/* update lowpan interface mac address when
136 	 * wpan mac has been changed
137 	 */
138 	if (sdata->wpan_dev.lowpan_dev)
139 		dev_addr_set(sdata->wpan_dev.lowpan_dev, dev->dev_addr);
140 
141 	return mac802154_wpan_update_llsec(dev);
142 }
143 
144 static int ieee802154_setup_hw(struct ieee802154_sub_if_data *sdata)
145 {
146 	struct ieee802154_local *local = sdata->local;
147 	struct wpan_dev *wpan_dev = &sdata->wpan_dev;
148 	int ret;
149 
150 	sdata->required_filtering = sdata->iface_default_filtering;
151 
152 	if (local->hw.flags & IEEE802154_HW_AFILT) {
153 		local->addr_filt.pan_id = wpan_dev->pan_id;
154 		local->addr_filt.ieee_addr = wpan_dev->extended_addr;
155 		local->addr_filt.short_addr = wpan_dev->short_addr;
156 	}
157 
158 	if (local->hw.flags & IEEE802154_HW_LBT) {
159 		ret = drv_set_lbt_mode(local, wpan_dev->lbt);
160 		if (ret < 0)
161 			return ret;
162 	}
163 
164 	if (local->hw.flags & IEEE802154_HW_CSMA_PARAMS) {
165 		ret = drv_set_csma_params(local, wpan_dev->min_be,
166 					  wpan_dev->max_be,
167 					  wpan_dev->csma_retries);
168 		if (ret < 0)
169 			return ret;
170 	}
171 
172 	if (local->hw.flags & IEEE802154_HW_FRAME_RETRIES) {
173 		ret = drv_set_max_frame_retries(local, wpan_dev->frame_retries);
174 		if (ret < 0)
175 			return ret;
176 	}
177 
178 	return 0;
179 }
180 
181 static int mac802154_slave_open(struct net_device *dev)
182 {
183 	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
184 	struct ieee802154_local *local = sdata->local;
185 	int res;
186 
187 	ASSERT_RTNL();
188 
189 	set_bit(SDATA_STATE_RUNNING, &sdata->state);
190 
191 	if (!local->open_count) {
192 		res = ieee802154_setup_hw(sdata);
193 		if (res)
194 			goto err;
195 
196 		res = drv_start(local, sdata->required_filtering,
197 				&local->addr_filt);
198 		if (res)
199 			goto err;
200 	}
201 
202 	local->open_count++;
203 	netif_start_queue(dev);
204 	return 0;
205 err:
206 	/* might already be clear but that doesn't matter */
207 	clear_bit(SDATA_STATE_RUNNING, &sdata->state);
208 
209 	return res;
210 }
211 
212 static int
213 ieee802154_check_mac_settings(struct ieee802154_local *local,
214 			      struct ieee802154_sub_if_data *sdata,
215 			      struct ieee802154_sub_if_data *nsdata)
216 {
217 	struct wpan_dev *nwpan_dev = &nsdata->wpan_dev;
218 	struct wpan_dev *wpan_dev = &sdata->wpan_dev;
219 
220 	ASSERT_RTNL();
221 
222 	if (sdata->iface_default_filtering != nsdata->iface_default_filtering)
223 		return -EBUSY;
224 
225 	if (local->hw.flags & IEEE802154_HW_AFILT) {
226 		if (wpan_dev->pan_id != nwpan_dev->pan_id ||
227 		    wpan_dev->short_addr != nwpan_dev->short_addr ||
228 		    wpan_dev->extended_addr != nwpan_dev->extended_addr)
229 			return -EBUSY;
230 	}
231 
232 	if (local->hw.flags & IEEE802154_HW_CSMA_PARAMS) {
233 		if (wpan_dev->min_be != nwpan_dev->min_be ||
234 		    wpan_dev->max_be != nwpan_dev->max_be ||
235 		    wpan_dev->csma_retries != nwpan_dev->csma_retries)
236 			return -EBUSY;
237 	}
238 
239 	if (local->hw.flags & IEEE802154_HW_FRAME_RETRIES) {
240 		if (wpan_dev->frame_retries != nwpan_dev->frame_retries)
241 			return -EBUSY;
242 	}
243 
244 	if (local->hw.flags & IEEE802154_HW_LBT) {
245 		if (wpan_dev->lbt != nwpan_dev->lbt)
246 			return -EBUSY;
247 	}
248 
249 	return 0;
250 }
251 
252 static int
253 ieee802154_check_concurrent_iface(struct ieee802154_sub_if_data *sdata,
254 				  enum nl802154_iftype iftype)
255 {
256 	struct ieee802154_local *local = sdata->local;
257 	struct ieee802154_sub_if_data *nsdata;
258 
259 	/* we hold the RTNL here so can safely walk the list */
260 	list_for_each_entry(nsdata, &local->interfaces, list) {
261 		if (nsdata != sdata && ieee802154_sdata_running(nsdata)) {
262 			int ret;
263 
264 			/* TODO currently we don't support multiple node/coord
265 			 * types we need to run skb_clone at rx path. Check if
266 			 * there exist really an use case if we need to support
267 			 * multiple node/coord types at the same time.
268 			 */
269 			if (sdata->wpan_dev.iftype != NL802154_IFTYPE_MONITOR &&
270 			    nsdata->wpan_dev.iftype != NL802154_IFTYPE_MONITOR)
271 				return -EBUSY;
272 
273 			/* check all phy mac sublayer settings are the same.
274 			 * We have only one phy, different values makes trouble.
275 			 */
276 			ret = ieee802154_check_mac_settings(local, sdata, nsdata);
277 			if (ret < 0)
278 				return ret;
279 		}
280 	}
281 
282 	return 0;
283 }
284 
285 static int mac802154_wpan_open(struct net_device *dev)
286 {
287 	int rc;
288 	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
289 	struct wpan_dev *wpan_dev = &sdata->wpan_dev;
290 
291 	rc = ieee802154_check_concurrent_iface(sdata, wpan_dev->iftype);
292 	if (rc < 0)
293 		return rc;
294 
295 	return mac802154_slave_open(dev);
296 }
297 
298 static int mac802154_slave_close(struct net_device *dev)
299 {
300 	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
301 	struct ieee802154_local *local = sdata->local;
302 
303 	ASSERT_RTNL();
304 
305 	netif_stop_queue(dev);
306 	local->open_count--;
307 
308 	clear_bit(SDATA_STATE_RUNNING, &sdata->state);
309 
310 	if (!local->open_count)
311 		ieee802154_stop_device(local);
312 
313 	return 0;
314 }
315 
316 static int mac802154_set_header_security(struct ieee802154_sub_if_data *sdata,
317 					 struct ieee802154_hdr *hdr,
318 					 const struct ieee802154_mac_cb *cb)
319 {
320 	struct ieee802154_llsec_params params;
321 	u8 level;
322 
323 	mac802154_llsec_get_params(&sdata->sec, &params);
324 
325 	if (!params.enabled && cb->secen_override && cb->secen)
326 		return -EINVAL;
327 	if (!params.enabled ||
328 	    (cb->secen_override && !cb->secen) ||
329 	    !params.out_level)
330 		return 0;
331 	if (cb->seclevel_override && !cb->seclevel)
332 		return -EINVAL;
333 
334 	level = cb->seclevel_override ? cb->seclevel : params.out_level;
335 
336 	hdr->fc.security_enabled = 1;
337 	hdr->sec.level = level;
338 	hdr->sec.key_id_mode = params.out_key.mode;
339 	if (params.out_key.mode == IEEE802154_SCF_KEY_SHORT_INDEX)
340 		hdr->sec.short_src = params.out_key.short_source;
341 	else if (params.out_key.mode == IEEE802154_SCF_KEY_HW_INDEX)
342 		hdr->sec.extended_src = params.out_key.extended_source;
343 	hdr->sec.key_id = params.out_key.id;
344 
345 	return 0;
346 }
347 
348 static int ieee802154_header_create(struct sk_buff *skb,
349 				    struct net_device *dev,
350 				    const struct ieee802154_addr *daddr,
351 				    const struct ieee802154_addr *saddr,
352 				    unsigned len)
353 {
354 	struct ieee802154_hdr hdr;
355 	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
356 	struct wpan_dev *wpan_dev = &sdata->wpan_dev;
357 	struct ieee802154_mac_cb *cb = mac_cb(skb);
358 	int hlen;
359 
360 	if (!daddr)
361 		return -EINVAL;
362 
363 	memset(&hdr.fc, 0, sizeof(hdr.fc));
364 	hdr.fc.type = cb->type;
365 	hdr.fc.security_enabled = cb->secen;
366 	hdr.fc.ack_request = cb->ackreq;
367 	hdr.seq = atomic_inc_return(&dev->ieee802154_ptr->dsn) & 0xFF;
368 
369 	if (mac802154_set_header_security(sdata, &hdr, cb) < 0)
370 		return -EINVAL;
371 
372 	if (!saddr) {
373 		if (wpan_dev->short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST) ||
374 		    wpan_dev->short_addr == cpu_to_le16(IEEE802154_ADDR_UNDEF) ||
375 		    wpan_dev->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST)) {
376 			hdr.source.mode = IEEE802154_ADDR_LONG;
377 			hdr.source.extended_addr = wpan_dev->extended_addr;
378 		} else {
379 			hdr.source.mode = IEEE802154_ADDR_SHORT;
380 			hdr.source.short_addr = wpan_dev->short_addr;
381 		}
382 
383 		hdr.source.pan_id = wpan_dev->pan_id;
384 	} else {
385 		hdr.source = *(const struct ieee802154_addr *)saddr;
386 	}
387 
388 	hdr.dest = *(const struct ieee802154_addr *)daddr;
389 
390 	hlen = ieee802154_hdr_push(skb, &hdr);
391 	if (hlen < 0)
392 		return -EINVAL;
393 
394 	skb_reset_mac_header(skb);
395 	skb->mac_len = hlen;
396 
397 	if (len > ieee802154_max_payload(&hdr))
398 		return -EMSGSIZE;
399 
400 	return hlen;
401 }
402 
403 static const struct wpan_dev_header_ops ieee802154_header_ops = {
404 	.create		= ieee802154_header_create,
405 };
406 
407 /* This header create functionality assumes a 8 byte array for
408  * source and destination pointer at maximum. To adapt this for
409  * the 802.15.4 dataframe header we use extended address handling
410  * here only and intra pan connection. fc fields are mostly fallback
411  * handling. For provide dev_hard_header for dgram sockets.
412  */
413 static int mac802154_header_create(struct sk_buff *skb,
414 				   struct net_device *dev,
415 				   unsigned short type,
416 				   const void *daddr,
417 				   const void *saddr,
418 				   unsigned len)
419 {
420 	struct ieee802154_hdr hdr;
421 	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
422 	struct wpan_dev *wpan_dev = &sdata->wpan_dev;
423 	struct ieee802154_mac_cb cb = { };
424 	int hlen;
425 
426 	if (!daddr)
427 		return -EINVAL;
428 
429 	memset(&hdr.fc, 0, sizeof(hdr.fc));
430 	hdr.fc.type = IEEE802154_FC_TYPE_DATA;
431 	hdr.fc.ack_request = wpan_dev->ackreq;
432 	hdr.seq = atomic_inc_return(&dev->ieee802154_ptr->dsn) & 0xFF;
433 
434 	/* TODO currently a workaround to give zero cb block to set
435 	 * security parameters defaults according MIB.
436 	 */
437 	if (mac802154_set_header_security(sdata, &hdr, &cb) < 0)
438 		return -EINVAL;
439 
440 	hdr.dest.pan_id = wpan_dev->pan_id;
441 	hdr.dest.mode = IEEE802154_ADDR_LONG;
442 	ieee802154_be64_to_le64(&hdr.dest.extended_addr, daddr);
443 
444 	hdr.source.pan_id = hdr.dest.pan_id;
445 	hdr.source.mode = IEEE802154_ADDR_LONG;
446 
447 	if (!saddr)
448 		hdr.source.extended_addr = wpan_dev->extended_addr;
449 	else
450 		ieee802154_be64_to_le64(&hdr.source.extended_addr, saddr);
451 
452 	hlen = ieee802154_hdr_push(skb, &hdr);
453 	if (hlen < 0)
454 		return -EINVAL;
455 
456 	skb_reset_mac_header(skb);
457 	skb->mac_len = hlen;
458 
459 	if (len > ieee802154_max_payload(&hdr))
460 		return -EMSGSIZE;
461 
462 	return hlen;
463 }
464 
465 static int
466 mac802154_header_parse(const struct sk_buff *skb, unsigned char *haddr)
467 {
468 	struct ieee802154_hdr hdr;
469 
470 	if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) {
471 		pr_debug("malformed packet\n");
472 		return 0;
473 	}
474 
475 	if (hdr.source.mode == IEEE802154_ADDR_LONG) {
476 		ieee802154_le64_to_be64(haddr, &hdr.source.extended_addr);
477 		return IEEE802154_EXTENDED_ADDR_LEN;
478 	}
479 
480 	return 0;
481 }
482 
483 static const struct header_ops mac802154_header_ops = {
484 	.create         = mac802154_header_create,
485 	.parse          = mac802154_header_parse,
486 };
487 
488 static const struct net_device_ops mac802154_wpan_ops = {
489 	.ndo_open		= mac802154_wpan_open,
490 	.ndo_stop		= mac802154_slave_close,
491 	.ndo_start_xmit		= ieee802154_subif_start_xmit,
492 	.ndo_do_ioctl		= mac802154_wpan_ioctl,
493 	.ndo_set_mac_address	= mac802154_wpan_mac_addr,
494 };
495 
496 static const struct net_device_ops mac802154_monitor_ops = {
497 	.ndo_open		= mac802154_wpan_open,
498 	.ndo_stop		= mac802154_slave_close,
499 	.ndo_start_xmit		= ieee802154_monitor_start_xmit,
500 };
501 
502 static void mac802154_wpan_free(struct net_device *dev)
503 {
504 	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
505 
506 	mac802154_llsec_destroy(&sdata->sec);
507 }
508 
509 static void ieee802154_if_setup(struct net_device *dev)
510 {
511 	dev->addr_len		= IEEE802154_EXTENDED_ADDR_LEN;
512 	memset(dev->broadcast, 0xff, IEEE802154_EXTENDED_ADDR_LEN);
513 
514 	/* Let hard_header_len set to IEEE802154_MIN_HEADER_LEN. AF_PACKET
515 	 * will not send frames without any payload, but ack frames
516 	 * has no payload, so substract one that we can send a 3 bytes
517 	 * frame. The xmit callback assumes at least a hard header where two
518 	 * bytes fc and sequence field are set.
519 	 */
520 	dev->hard_header_len	= IEEE802154_MIN_HEADER_LEN - 1;
521 	/* The auth_tag header is for security and places in private payload
522 	 * room of mac frame which stucks between payload and FCS field.
523 	 */
524 	dev->needed_tailroom	= IEEE802154_MAX_AUTH_TAG_LEN +
525 				  IEEE802154_FCS_LEN;
526 	/* The mtu size is the payload without mac header in this case.
527 	 * We have a dynamic length header with a minimum header length
528 	 * which is hard_header_len. In this case we let mtu to the size
529 	 * of maximum payload which is IEEE802154_MTU - IEEE802154_FCS_LEN -
530 	 * hard_header_len. The FCS which is set by hardware or ndo_start_xmit
531 	 * and the minimum mac header which can be evaluated inside driver
532 	 * layer. The rest of mac header will be part of payload if greater
533 	 * than hard_header_len.
534 	 */
535 	dev->mtu		= IEEE802154_MTU - IEEE802154_FCS_LEN -
536 				  dev->hard_header_len;
537 	dev->tx_queue_len	= 300;
538 	dev->flags		= IFF_NOARP | IFF_BROADCAST;
539 }
540 
541 static int
542 ieee802154_setup_sdata(struct ieee802154_sub_if_data *sdata,
543 		       enum nl802154_iftype type)
544 {
545 	struct wpan_dev *wpan_dev = &sdata->wpan_dev;
546 	int ret;
547 	u8 tmp;
548 
549 	/* set some type-dependent values */
550 	sdata->wpan_dev.iftype = type;
551 
552 	get_random_bytes(&tmp, sizeof(tmp));
553 	atomic_set(&wpan_dev->bsn, tmp);
554 	get_random_bytes(&tmp, sizeof(tmp));
555 	atomic_set(&wpan_dev->dsn, tmp);
556 
557 	/* defaults per 802.15.4-2011 */
558 	wpan_dev->min_be = 3;
559 	wpan_dev->max_be = 5;
560 	wpan_dev->csma_retries = 4;
561 	wpan_dev->frame_retries = 3;
562 
563 	wpan_dev->pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST);
564 	wpan_dev->short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST);
565 
566 	switch (type) {
567 	case NL802154_IFTYPE_COORD:
568 	case NL802154_IFTYPE_NODE:
569 		ieee802154_be64_to_le64(&wpan_dev->extended_addr,
570 					sdata->dev->dev_addr);
571 
572 		sdata->dev->header_ops = &mac802154_header_ops;
573 		sdata->dev->needs_free_netdev = true;
574 		sdata->dev->priv_destructor = mac802154_wpan_free;
575 		sdata->dev->netdev_ops = &mac802154_wpan_ops;
576 		sdata->dev->ml_priv = &mac802154_mlme_wpan;
577 		sdata->iface_default_filtering = IEEE802154_FILTERING_4_FRAME_FIELDS;
578 		wpan_dev->header_ops = &ieee802154_header_ops;
579 
580 		mutex_init(&sdata->sec_mtx);
581 
582 		mac802154_llsec_init(&sdata->sec);
583 		ret = mac802154_wpan_update_llsec(sdata->dev);
584 		if (ret < 0)
585 			return ret;
586 
587 		break;
588 	case NL802154_IFTYPE_MONITOR:
589 		sdata->dev->needs_free_netdev = true;
590 		sdata->dev->netdev_ops = &mac802154_monitor_ops;
591 		sdata->iface_default_filtering = IEEE802154_FILTERING_NONE;
592 		break;
593 	default:
594 		BUG();
595 	}
596 
597 	return 0;
598 }
599 
600 struct net_device *
601 ieee802154_if_add(struct ieee802154_local *local, const char *name,
602 		  unsigned char name_assign_type, enum nl802154_iftype type,
603 		  __le64 extended_addr)
604 {
605 	u8 addr[IEEE802154_EXTENDED_ADDR_LEN];
606 	struct net_device *ndev = NULL;
607 	struct ieee802154_sub_if_data *sdata = NULL;
608 	int ret;
609 
610 	ASSERT_RTNL();
611 
612 	ndev = alloc_netdev(sizeof(*sdata), name,
613 			    name_assign_type, ieee802154_if_setup);
614 	if (!ndev)
615 		return ERR_PTR(-ENOMEM);
616 
617 	ndev->needed_headroom = local->hw.extra_tx_headroom +
618 				IEEE802154_MAX_HEADER_LEN;
619 
620 	ret = dev_alloc_name(ndev, ndev->name);
621 	if (ret < 0)
622 		goto err;
623 
624 	ieee802154_le64_to_be64(ndev->perm_addr,
625 				&local->hw.phy->perm_extended_addr);
626 	switch (type) {
627 	case NL802154_IFTYPE_COORD:
628 	case NL802154_IFTYPE_NODE:
629 		ndev->type = ARPHRD_IEEE802154;
630 		if (ieee802154_is_valid_extended_unicast_addr(extended_addr)) {
631 			ieee802154_le64_to_be64(addr, &extended_addr);
632 			dev_addr_set(ndev, addr);
633 		} else {
634 			dev_addr_set(ndev, ndev->perm_addr);
635 		}
636 		break;
637 	case NL802154_IFTYPE_MONITOR:
638 		ndev->type = ARPHRD_IEEE802154_MONITOR;
639 		break;
640 	default:
641 		ret = -EINVAL;
642 		goto err;
643 	}
644 
645 	/* TODO check this */
646 	SET_NETDEV_DEV(ndev, &local->phy->dev);
647 	dev_net_set(ndev, wpan_phy_net(local->hw.phy));
648 	sdata = netdev_priv(ndev);
649 	ndev->ieee802154_ptr = &sdata->wpan_dev;
650 	memcpy(sdata->name, ndev->name, IFNAMSIZ);
651 	sdata->dev = ndev;
652 	sdata->wpan_dev.wpan_phy = local->hw.phy;
653 	sdata->local = local;
654 	INIT_LIST_HEAD(&sdata->wpan_dev.list);
655 
656 	/* setup type-dependent data */
657 	ret = ieee802154_setup_sdata(sdata, type);
658 	if (ret)
659 		goto err;
660 
661 	ret = register_netdevice(ndev);
662 	if (ret < 0)
663 		goto err;
664 
665 	mutex_lock(&local->iflist_mtx);
666 	list_add_tail_rcu(&sdata->list, &local->interfaces);
667 	mutex_unlock(&local->iflist_mtx);
668 
669 	return ndev;
670 
671 err:
672 	free_netdev(ndev);
673 	return ERR_PTR(ret);
674 }
675 
676 void ieee802154_if_remove(struct ieee802154_sub_if_data *sdata)
677 {
678 	ASSERT_RTNL();
679 
680 	mutex_lock(&sdata->local->iflist_mtx);
681 	list_del_rcu(&sdata->list);
682 	mutex_unlock(&sdata->local->iflist_mtx);
683 
684 	synchronize_rcu();
685 	unregister_netdevice(sdata->dev);
686 }
687 
688 void ieee802154_remove_interfaces(struct ieee802154_local *local)
689 {
690 	struct ieee802154_sub_if_data *sdata, *tmp;
691 
692 	mutex_lock(&local->iflist_mtx);
693 	list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
694 		list_del(&sdata->list);
695 
696 		unregister_netdevice(sdata->dev);
697 	}
698 	mutex_unlock(&local->iflist_mtx);
699 }
700 
701 static int netdev_notify(struct notifier_block *nb,
702 			 unsigned long state, void *ptr)
703 {
704 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
705 	struct ieee802154_sub_if_data *sdata;
706 
707 	if (state != NETDEV_CHANGENAME)
708 		return NOTIFY_DONE;
709 
710 	if (!dev->ieee802154_ptr || !dev->ieee802154_ptr->wpan_phy)
711 		return NOTIFY_DONE;
712 
713 	if (dev->ieee802154_ptr->wpan_phy->privid != mac802154_wpan_phy_privid)
714 		return NOTIFY_DONE;
715 
716 	sdata = IEEE802154_DEV_TO_SUB_IF(dev);
717 	memcpy(sdata->name, dev->name, IFNAMSIZ);
718 
719 	return NOTIFY_OK;
720 }
721 
722 static struct notifier_block mac802154_netdev_notifier = {
723 	.notifier_call = netdev_notify,
724 };
725 
726 int ieee802154_iface_init(void)
727 {
728 	return register_netdevice_notifier(&mac802154_netdev_notifier);
729 }
730 
731 void ieee802154_iface_exit(void)
732 {
733 	unregister_netdevice_notifier(&mac802154_netdev_notifier);
734 }
735