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
mac802154_wpan_update_llsec(struct net_device * dev)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, ¶ms, changed);
43 }
44
45 return rc;
46 }
47
48 static int
mac802154_wpan_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)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
mac802154_wpan_mac_addr(struct net_device * dev,void * p)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
ieee802154_setup_hw(struct ieee802154_sub_if_data * sdata)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
mac802154_slave_open(struct net_device * dev)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
ieee802154_check_mac_settings(struct ieee802154_local * local,struct ieee802154_sub_if_data * sdata,struct ieee802154_sub_if_data * nsdata)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
ieee802154_check_concurrent_iface(struct ieee802154_sub_if_data * sdata,enum nl802154_iftype iftype)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
mac802154_wpan_open(struct net_device * dev)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
mac802154_slave_close(struct net_device * dev)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 if (mac802154_is_scanning(local))
306 mac802154_abort_scan_locked(local, sdata);
307
308 if (mac802154_is_beaconing(local))
309 mac802154_stop_beacons_locked(local, sdata);
310
311 netif_stop_queue(dev);
312 local->open_count--;
313
314 clear_bit(SDATA_STATE_RUNNING, &sdata->state);
315
316 if (!local->open_count)
317 ieee802154_stop_device(local);
318
319 return 0;
320 }
321
mac802154_set_header_security(struct ieee802154_sub_if_data * sdata,struct ieee802154_hdr * hdr,const struct ieee802154_mac_cb * cb)322 static int mac802154_set_header_security(struct ieee802154_sub_if_data *sdata,
323 struct ieee802154_hdr *hdr,
324 const struct ieee802154_mac_cb *cb)
325 {
326 struct ieee802154_llsec_params params;
327 u8 level;
328
329 mac802154_llsec_get_params(&sdata->sec, ¶ms);
330
331 if (!params.enabled && cb->secen_override && cb->secen)
332 return -EINVAL;
333 if (!params.enabled ||
334 (cb->secen_override && !cb->secen) ||
335 !params.out_level)
336 return 0;
337 if (cb->seclevel_override && !cb->seclevel)
338 return -EINVAL;
339
340 level = cb->seclevel_override ? cb->seclevel : params.out_level;
341
342 hdr->fc.security_enabled = 1;
343 hdr->sec.level = level;
344 hdr->sec.key_id_mode = params.out_key.mode;
345 if (params.out_key.mode == IEEE802154_SCF_KEY_SHORT_INDEX)
346 hdr->sec.short_src = params.out_key.short_source;
347 else if (params.out_key.mode == IEEE802154_SCF_KEY_HW_INDEX)
348 hdr->sec.extended_src = params.out_key.extended_source;
349 hdr->sec.key_id = params.out_key.id;
350
351 return 0;
352 }
353
ieee802154_header_create(struct sk_buff * skb,struct net_device * dev,const struct ieee802154_addr * daddr,const struct ieee802154_addr * saddr,unsigned len)354 static int ieee802154_header_create(struct sk_buff *skb,
355 struct net_device *dev,
356 const struct ieee802154_addr *daddr,
357 const struct ieee802154_addr *saddr,
358 unsigned len)
359 {
360 struct ieee802154_hdr hdr;
361 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
362 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
363 struct ieee802154_mac_cb *cb = mac_cb(skb);
364 int hlen;
365
366 if (!daddr)
367 return -EINVAL;
368
369 memset(&hdr.fc, 0, sizeof(hdr.fc));
370 hdr.fc.type = cb->type;
371 hdr.fc.security_enabled = cb->secen;
372 hdr.fc.ack_request = cb->ackreq;
373 hdr.seq = atomic_inc_return(&dev->ieee802154_ptr->dsn) & 0xFF;
374
375 if (mac802154_set_header_security(sdata, &hdr, cb) < 0)
376 return -EINVAL;
377
378 if (!saddr) {
379 if (wpan_dev->short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST) ||
380 wpan_dev->short_addr == cpu_to_le16(IEEE802154_ADDR_UNDEF) ||
381 wpan_dev->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST)) {
382 hdr.source.mode = IEEE802154_ADDR_LONG;
383 hdr.source.extended_addr = wpan_dev->extended_addr;
384 } else {
385 hdr.source.mode = IEEE802154_ADDR_SHORT;
386 hdr.source.short_addr = wpan_dev->short_addr;
387 }
388
389 hdr.source.pan_id = wpan_dev->pan_id;
390 } else {
391 hdr.source = *(const struct ieee802154_addr *)saddr;
392 }
393
394 hdr.dest = *(const struct ieee802154_addr *)daddr;
395
396 hlen = ieee802154_hdr_push(skb, &hdr);
397 if (hlen < 0)
398 return -EINVAL;
399
400 skb_reset_mac_header(skb);
401 skb->mac_len = hlen;
402
403 if (len > ieee802154_max_payload(&hdr))
404 return -EMSGSIZE;
405
406 return hlen;
407 }
408
409 static const struct wpan_dev_header_ops ieee802154_header_ops = {
410 .create = ieee802154_header_create,
411 };
412
413 /* This header create functionality assumes a 8 byte array for
414 * source and destination pointer at maximum. To adapt this for
415 * the 802.15.4 dataframe header we use extended address handling
416 * here only and intra pan connection. fc fields are mostly fallback
417 * handling. For provide dev_hard_header for dgram sockets.
418 */
mac802154_header_create(struct sk_buff * skb,struct net_device * dev,unsigned short type,const void * daddr,const void * saddr,unsigned len)419 static int mac802154_header_create(struct sk_buff *skb,
420 struct net_device *dev,
421 unsigned short type,
422 const void *daddr,
423 const void *saddr,
424 unsigned len)
425 {
426 struct ieee802154_hdr hdr;
427 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
428 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
429 struct ieee802154_mac_cb cb = { };
430 int hlen;
431
432 if (!daddr)
433 return -EINVAL;
434
435 memset(&hdr.fc, 0, sizeof(hdr.fc));
436 hdr.fc.type = IEEE802154_FC_TYPE_DATA;
437 hdr.fc.ack_request = wpan_dev->ackreq;
438 hdr.seq = atomic_inc_return(&dev->ieee802154_ptr->dsn) & 0xFF;
439
440 /* TODO currently a workaround to give zero cb block to set
441 * security parameters defaults according MIB.
442 */
443 if (mac802154_set_header_security(sdata, &hdr, &cb) < 0)
444 return -EINVAL;
445
446 hdr.dest.pan_id = wpan_dev->pan_id;
447 hdr.dest.mode = IEEE802154_ADDR_LONG;
448 ieee802154_be64_to_le64(&hdr.dest.extended_addr, daddr);
449
450 hdr.source.pan_id = hdr.dest.pan_id;
451 hdr.source.mode = IEEE802154_ADDR_LONG;
452
453 if (!saddr)
454 hdr.source.extended_addr = wpan_dev->extended_addr;
455 else
456 ieee802154_be64_to_le64(&hdr.source.extended_addr, saddr);
457
458 hlen = ieee802154_hdr_push(skb, &hdr);
459 if (hlen < 0)
460 return -EINVAL;
461
462 skb_reset_mac_header(skb);
463 skb->mac_len = hlen;
464
465 if (len > ieee802154_max_payload(&hdr))
466 return -EMSGSIZE;
467
468 return hlen;
469 }
470
471 static int
mac802154_header_parse(const struct sk_buff * skb,unsigned char * haddr)472 mac802154_header_parse(const struct sk_buff *skb, unsigned char *haddr)
473 {
474 struct ieee802154_hdr hdr;
475
476 if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) {
477 pr_debug("malformed packet\n");
478 return 0;
479 }
480
481 if (hdr.source.mode == IEEE802154_ADDR_LONG) {
482 ieee802154_le64_to_be64(haddr, &hdr.source.extended_addr);
483 return IEEE802154_EXTENDED_ADDR_LEN;
484 }
485
486 return 0;
487 }
488
489 static const struct header_ops mac802154_header_ops = {
490 .create = mac802154_header_create,
491 .parse = mac802154_header_parse,
492 };
493
494 static const struct net_device_ops mac802154_wpan_ops = {
495 .ndo_open = mac802154_wpan_open,
496 .ndo_stop = mac802154_slave_close,
497 .ndo_start_xmit = ieee802154_subif_start_xmit,
498 .ndo_do_ioctl = mac802154_wpan_ioctl,
499 .ndo_set_mac_address = mac802154_wpan_mac_addr,
500 };
501
502 static const struct net_device_ops mac802154_monitor_ops = {
503 .ndo_open = mac802154_wpan_open,
504 .ndo_stop = mac802154_slave_close,
505 .ndo_start_xmit = ieee802154_monitor_start_xmit,
506 };
507
mac802154_wpan_free(struct net_device * dev)508 static void mac802154_wpan_free(struct net_device *dev)
509 {
510 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
511
512 mac802154_llsec_destroy(&sdata->sec);
513 }
514
ieee802154_if_setup(struct net_device * dev)515 static void ieee802154_if_setup(struct net_device *dev)
516 {
517 dev->addr_len = IEEE802154_EXTENDED_ADDR_LEN;
518 memset(dev->broadcast, 0xff, IEEE802154_EXTENDED_ADDR_LEN);
519
520 /* Let hard_header_len set to IEEE802154_MIN_HEADER_LEN. AF_PACKET
521 * will not send frames without any payload, but ack frames
522 * has no payload, so substract one that we can send a 3 bytes
523 * frame. The xmit callback assumes at least a hard header where two
524 * bytes fc and sequence field are set.
525 */
526 dev->hard_header_len = IEEE802154_MIN_HEADER_LEN - 1;
527 /* The auth_tag header is for security and places in private payload
528 * room of mac frame which stucks between payload and FCS field.
529 */
530 dev->needed_tailroom = IEEE802154_MAX_AUTH_TAG_LEN +
531 IEEE802154_FCS_LEN;
532 /* The mtu size is the payload without mac header in this case.
533 * We have a dynamic length header with a minimum header length
534 * which is hard_header_len. In this case we let mtu to the size
535 * of maximum payload which is IEEE802154_MTU - IEEE802154_FCS_LEN -
536 * hard_header_len. The FCS which is set by hardware or ndo_start_xmit
537 * and the minimum mac header which can be evaluated inside driver
538 * layer. The rest of mac header will be part of payload if greater
539 * than hard_header_len.
540 */
541 dev->mtu = IEEE802154_MTU - IEEE802154_FCS_LEN -
542 dev->hard_header_len;
543 dev->tx_queue_len = 300;
544 dev->flags = IFF_NOARP | IFF_BROADCAST;
545 }
546
547 static int
ieee802154_setup_sdata(struct ieee802154_sub_if_data * sdata,enum nl802154_iftype type)548 ieee802154_setup_sdata(struct ieee802154_sub_if_data *sdata,
549 enum nl802154_iftype type)
550 {
551 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
552 int ret;
553 u8 tmp;
554
555 /* set some type-dependent values */
556 sdata->wpan_dev.iftype = type;
557
558 get_random_bytes(&tmp, sizeof(tmp));
559 atomic_set(&wpan_dev->bsn, tmp);
560 get_random_bytes(&tmp, sizeof(tmp));
561 atomic_set(&wpan_dev->dsn, tmp);
562
563 /* defaults per 802.15.4-2011 */
564 wpan_dev->min_be = 3;
565 wpan_dev->max_be = 5;
566 wpan_dev->csma_retries = 4;
567 wpan_dev->frame_retries = 3;
568
569 wpan_dev->pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST);
570 wpan_dev->short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST);
571
572 switch (type) {
573 case NL802154_IFTYPE_COORD:
574 case NL802154_IFTYPE_NODE:
575 ieee802154_be64_to_le64(&wpan_dev->extended_addr,
576 sdata->dev->dev_addr);
577
578 sdata->dev->header_ops = &mac802154_header_ops;
579 sdata->dev->needs_free_netdev = true;
580 sdata->dev->priv_destructor = mac802154_wpan_free;
581 sdata->dev->netdev_ops = &mac802154_wpan_ops;
582 sdata->dev->ml_priv = &mac802154_mlme_wpan;
583 sdata->iface_default_filtering = IEEE802154_FILTERING_4_FRAME_FIELDS;
584 wpan_dev->header_ops = &ieee802154_header_ops;
585
586 mutex_init(&sdata->sec_mtx);
587
588 mac802154_llsec_init(&sdata->sec);
589 ret = mac802154_wpan_update_llsec(sdata->dev);
590 if (ret < 0)
591 return ret;
592
593 break;
594 case NL802154_IFTYPE_MONITOR:
595 sdata->dev->needs_free_netdev = true;
596 sdata->dev->netdev_ops = &mac802154_monitor_ops;
597 sdata->iface_default_filtering = IEEE802154_FILTERING_NONE;
598 break;
599 default:
600 BUG();
601 }
602
603 return 0;
604 }
605
606 struct net_device *
ieee802154_if_add(struct ieee802154_local * local,const char * name,unsigned char name_assign_type,enum nl802154_iftype type,__le64 extended_addr)607 ieee802154_if_add(struct ieee802154_local *local, const char *name,
608 unsigned char name_assign_type, enum nl802154_iftype type,
609 __le64 extended_addr)
610 {
611 u8 addr[IEEE802154_EXTENDED_ADDR_LEN];
612 struct net_device *ndev = NULL;
613 struct ieee802154_sub_if_data *sdata = NULL;
614 int ret;
615
616 ASSERT_RTNL();
617
618 ndev = alloc_netdev(sizeof(*sdata), name,
619 name_assign_type, ieee802154_if_setup);
620 if (!ndev)
621 return ERR_PTR(-ENOMEM);
622
623 ndev->needed_headroom = local->hw.extra_tx_headroom +
624 IEEE802154_MAX_HEADER_LEN;
625
626 ret = dev_alloc_name(ndev, ndev->name);
627 if (ret < 0)
628 goto err;
629
630 ieee802154_le64_to_be64(ndev->perm_addr,
631 &local->hw.phy->perm_extended_addr);
632 switch (type) {
633 case NL802154_IFTYPE_COORD:
634 case NL802154_IFTYPE_NODE:
635 ndev->type = ARPHRD_IEEE802154;
636 if (ieee802154_is_valid_extended_unicast_addr(extended_addr)) {
637 ieee802154_le64_to_be64(addr, &extended_addr);
638 dev_addr_set(ndev, addr);
639 } else {
640 dev_addr_set(ndev, ndev->perm_addr);
641 }
642 break;
643 case NL802154_IFTYPE_MONITOR:
644 ndev->type = ARPHRD_IEEE802154_MONITOR;
645 break;
646 default:
647 ret = -EINVAL;
648 goto err;
649 }
650
651 /* TODO check this */
652 SET_NETDEV_DEV(ndev, &local->phy->dev);
653 dev_net_set(ndev, wpan_phy_net(local->hw.phy));
654 sdata = netdev_priv(ndev);
655 ndev->ieee802154_ptr = &sdata->wpan_dev;
656 memcpy(sdata->name, ndev->name, IFNAMSIZ);
657 sdata->dev = ndev;
658 sdata->wpan_dev.wpan_phy = local->hw.phy;
659 sdata->local = local;
660 INIT_LIST_HEAD(&sdata->wpan_dev.list);
661
662 /* setup type-dependent data */
663 ret = ieee802154_setup_sdata(sdata, type);
664 if (ret)
665 goto err;
666
667 ret = register_netdevice(ndev);
668 if (ret < 0)
669 goto err;
670
671 mutex_lock(&local->iflist_mtx);
672 list_add_tail_rcu(&sdata->list, &local->interfaces);
673 mutex_unlock(&local->iflist_mtx);
674
675 return ndev;
676
677 err:
678 free_netdev(ndev);
679 return ERR_PTR(ret);
680 }
681
ieee802154_if_remove(struct ieee802154_sub_if_data * sdata)682 void ieee802154_if_remove(struct ieee802154_sub_if_data *sdata)
683 {
684 ASSERT_RTNL();
685
686 mutex_lock(&sdata->local->iflist_mtx);
687 list_del_rcu(&sdata->list);
688 mutex_unlock(&sdata->local->iflist_mtx);
689
690 synchronize_rcu();
691 unregister_netdevice(sdata->dev);
692 }
693
ieee802154_remove_interfaces(struct ieee802154_local * local)694 void ieee802154_remove_interfaces(struct ieee802154_local *local)
695 {
696 struct ieee802154_sub_if_data *sdata, *tmp;
697
698 mutex_lock(&local->iflist_mtx);
699 list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
700 list_del(&sdata->list);
701
702 unregister_netdevice(sdata->dev);
703 }
704 mutex_unlock(&local->iflist_mtx);
705 }
706
netdev_notify(struct notifier_block * nb,unsigned long state,void * ptr)707 static int netdev_notify(struct notifier_block *nb,
708 unsigned long state, void *ptr)
709 {
710 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
711 struct ieee802154_sub_if_data *sdata;
712
713 if (state != NETDEV_CHANGENAME)
714 return NOTIFY_DONE;
715
716 if (!dev->ieee802154_ptr || !dev->ieee802154_ptr->wpan_phy)
717 return NOTIFY_DONE;
718
719 if (dev->ieee802154_ptr->wpan_phy->privid != mac802154_wpan_phy_privid)
720 return NOTIFY_DONE;
721
722 sdata = IEEE802154_DEV_TO_SUB_IF(dev);
723 memcpy(sdata->name, dev->name, IFNAMSIZ);
724
725 return NOTIFY_OK;
726 }
727
728 static struct notifier_block mac802154_netdev_notifier = {
729 .notifier_call = netdev_notify,
730 };
731
ieee802154_iface_init(void)732 int ieee802154_iface_init(void)
733 {
734 return register_netdevice_notifier(&mac802154_netdev_notifier);
735 }
736
ieee802154_iface_exit(void)737 void ieee802154_iface_exit(void)
738 {
739 unregister_netdevice_notifier(&mac802154_netdev_notifier);
740 }
741