1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2007-2012 Siemens AG
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>
12 #include <linux/netdevice.h>
13 #include <linux/module.h>
14 #include <linux/if_arp.h>
15 #include <linux/ieee802154.h>
17 #include <net/nl802154.h>
18 #include <net/mac802154.h>
19 #include <net/ieee802154_netdev.h>
20 #include <net/cfg802154.h>
22 #include "ieee802154_i.h"
23 #include "driver-ops.h"
25 int mac802154_wpan_update_llsec(struct net_device *dev)
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;
33 struct ieee802154_llsec_params params;
36 params.pan_id = wpan_dev->pan_id;
37 changed |= IEEE802154_LLSEC_PARAM_PAN_ID;
39 params.hwaddr = wpan_dev->extended_addr;
40 changed |= IEEE802154_LLSEC_PARAM_HWADDR;
42 rc = ops->llsec->set_params(dev, ¶ms, changed);
49 mac802154_wpan_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
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;
57 if (cmd != SIOCGIFADDR && cmd != SIOCSIFADDR)
65 u16 pan_id, short_addr;
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) {
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;
84 if (netif_running(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) {
100 wpan_dev->pan_id = cpu_to_le16(sa->addr.pan_id);
101 wpan_dev->short_addr = cpu_to_le16(sa->addr.short_addr);
103 err = mac802154_wpan_update_llsec(dev);
111 static int mac802154_wpan_mac_addr(struct net_device *dev, void *p)
113 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
114 struct sockaddr *addr = p;
115 __le64 extended_addr;
117 if (netif_running(dev))
120 /* lowpan need to be down for update
121 * SLAAC address after ifup
123 if (sdata->wpan_dev.lowpan_dev) {
124 if (netif_running(sdata->wpan_dev.lowpan_dev))
128 ieee802154_be64_to_le64(&extended_addr, addr->sa_data);
129 if (!ieee802154_is_valid_extended_unicast_addr(extended_addr))
132 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
133 sdata->wpan_dev.extended_addr = extended_addr;
135 /* update lowpan interface mac address when
136 * wpan mac has been changed
138 if (sdata->wpan_dev.lowpan_dev)
139 memcpy(sdata->wpan_dev.lowpan_dev->dev_addr, dev->dev_addr,
142 return mac802154_wpan_update_llsec(dev);
145 static int ieee802154_setup_hw(struct ieee802154_sub_if_data *sdata)
147 struct ieee802154_local *local = sdata->local;
148 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
151 if (local->hw.flags & IEEE802154_HW_PROMISCUOUS) {
152 ret = drv_set_promiscuous_mode(local,
153 wpan_dev->promiscuous_mode);
158 if (local->hw.flags & IEEE802154_HW_AFILT) {
159 ret = drv_set_pan_id(local, wpan_dev->pan_id);
163 ret = drv_set_extended_addr(local, wpan_dev->extended_addr);
167 ret = drv_set_short_addr(local, wpan_dev->short_addr);
172 if (local->hw.flags & IEEE802154_HW_LBT) {
173 ret = drv_set_lbt_mode(local, wpan_dev->lbt);
178 if (local->hw.flags & IEEE802154_HW_CSMA_PARAMS) {
179 ret = drv_set_csma_params(local, wpan_dev->min_be,
181 wpan_dev->csma_retries);
186 if (local->hw.flags & IEEE802154_HW_FRAME_RETRIES) {
187 ret = drv_set_max_frame_retries(local, wpan_dev->frame_retries);
195 static int mac802154_slave_open(struct net_device *dev)
197 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
198 struct ieee802154_local *local = sdata->local;
203 set_bit(SDATA_STATE_RUNNING, &sdata->state);
205 if (!local->open_count) {
206 res = ieee802154_setup_hw(sdata);
210 res = drv_start(local);
216 netif_start_queue(dev);
219 /* might already be clear but that doesn't matter */
220 clear_bit(SDATA_STATE_RUNNING, &sdata->state);
226 ieee802154_check_mac_settings(struct ieee802154_local *local,
227 struct wpan_dev *wpan_dev,
228 struct wpan_dev *nwpan_dev)
232 if (local->hw.flags & IEEE802154_HW_PROMISCUOUS) {
233 if (wpan_dev->promiscuous_mode != nwpan_dev->promiscuous_mode)
237 if (local->hw.flags & IEEE802154_HW_AFILT) {
238 if (wpan_dev->pan_id != nwpan_dev->pan_id ||
239 wpan_dev->short_addr != nwpan_dev->short_addr ||
240 wpan_dev->extended_addr != nwpan_dev->extended_addr)
244 if (local->hw.flags & IEEE802154_HW_CSMA_PARAMS) {
245 if (wpan_dev->min_be != nwpan_dev->min_be ||
246 wpan_dev->max_be != nwpan_dev->max_be ||
247 wpan_dev->csma_retries != nwpan_dev->csma_retries)
251 if (local->hw.flags & IEEE802154_HW_FRAME_RETRIES) {
252 if (wpan_dev->frame_retries != nwpan_dev->frame_retries)
256 if (local->hw.flags & IEEE802154_HW_LBT) {
257 if (wpan_dev->lbt != nwpan_dev->lbt)
265 ieee802154_check_concurrent_iface(struct ieee802154_sub_if_data *sdata,
266 enum nl802154_iftype iftype)
268 struct ieee802154_local *local = sdata->local;
269 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
270 struct ieee802154_sub_if_data *nsdata;
272 /* we hold the RTNL here so can safely walk the list */
273 list_for_each_entry(nsdata, &local->interfaces, list) {
274 if (nsdata != sdata && ieee802154_sdata_running(nsdata)) {
277 /* TODO currently we don't support multiple node types
278 * we need to run skb_clone at rx path. Check if there
279 * exist really an use case if we need to support
280 * multiple node types at the same time.
282 if (wpan_dev->iftype == NL802154_IFTYPE_NODE &&
283 nsdata->wpan_dev.iftype == NL802154_IFTYPE_NODE)
286 /* check all phy mac sublayer settings are the same.
287 * We have only one phy, different values makes trouble.
289 ret = ieee802154_check_mac_settings(local, wpan_dev,
299 static int mac802154_wpan_open(struct net_device *dev)
302 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
303 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
305 rc = ieee802154_check_concurrent_iface(sdata, wpan_dev->iftype);
309 return mac802154_slave_open(dev);
312 static int mac802154_slave_close(struct net_device *dev)
314 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
315 struct ieee802154_local *local = sdata->local;
319 netif_stop_queue(dev);
322 clear_bit(SDATA_STATE_RUNNING, &sdata->state);
324 if (!local->open_count)
325 ieee802154_stop_device(local);
330 static int mac802154_set_header_security(struct ieee802154_sub_if_data *sdata,
331 struct ieee802154_hdr *hdr,
332 const struct ieee802154_mac_cb *cb)
334 struct ieee802154_llsec_params params;
337 mac802154_llsec_get_params(&sdata->sec, ¶ms);
339 if (!params.enabled && cb->secen_override && cb->secen)
341 if (!params.enabled ||
342 (cb->secen_override && !cb->secen) ||
345 if (cb->seclevel_override && !cb->seclevel)
348 level = cb->seclevel_override ? cb->seclevel : params.out_level;
350 hdr->fc.security_enabled = 1;
351 hdr->sec.level = level;
352 hdr->sec.key_id_mode = params.out_key.mode;
353 if (params.out_key.mode == IEEE802154_SCF_KEY_SHORT_INDEX)
354 hdr->sec.short_src = params.out_key.short_source;
355 else if (params.out_key.mode == IEEE802154_SCF_KEY_HW_INDEX)
356 hdr->sec.extended_src = params.out_key.extended_source;
357 hdr->sec.key_id = params.out_key.id;
362 static int ieee802154_header_create(struct sk_buff *skb,
363 struct net_device *dev,
364 const struct ieee802154_addr *daddr,
365 const struct ieee802154_addr *saddr,
368 struct ieee802154_hdr hdr;
369 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
370 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
371 struct ieee802154_mac_cb *cb = mac_cb(skb);
377 memset(&hdr.fc, 0, sizeof(hdr.fc));
378 hdr.fc.type = cb->type;
379 hdr.fc.security_enabled = cb->secen;
380 hdr.fc.ack_request = cb->ackreq;
381 hdr.seq = atomic_inc_return(&dev->ieee802154_ptr->dsn) & 0xFF;
383 if (mac802154_set_header_security(sdata, &hdr, cb) < 0)
387 if (wpan_dev->short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST) ||
388 wpan_dev->short_addr == cpu_to_le16(IEEE802154_ADDR_UNDEF) ||
389 wpan_dev->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST)) {
390 hdr.source.mode = IEEE802154_ADDR_LONG;
391 hdr.source.extended_addr = wpan_dev->extended_addr;
393 hdr.source.mode = IEEE802154_ADDR_SHORT;
394 hdr.source.short_addr = wpan_dev->short_addr;
397 hdr.source.pan_id = wpan_dev->pan_id;
399 hdr.source = *(const struct ieee802154_addr *)saddr;
402 hdr.dest = *(const struct ieee802154_addr *)daddr;
404 hlen = ieee802154_hdr_push(skb, &hdr);
408 skb_reset_mac_header(skb);
411 if (len > ieee802154_max_payload(&hdr))
417 static const struct wpan_dev_header_ops ieee802154_header_ops = {
418 .create = ieee802154_header_create,
421 /* This header create functionality assumes a 8 byte array for
422 * source and destination pointer at maximum. To adapt this for
423 * the 802.15.4 dataframe header we use extended address handling
424 * here only and intra pan connection. fc fields are mostly fallback
425 * handling. For provide dev_hard_header for dgram sockets.
427 static int mac802154_header_create(struct sk_buff *skb,
428 struct net_device *dev,
434 struct ieee802154_hdr hdr;
435 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
436 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
437 struct ieee802154_mac_cb cb = { };
443 memset(&hdr.fc, 0, sizeof(hdr.fc));
444 hdr.fc.type = IEEE802154_FC_TYPE_DATA;
445 hdr.fc.ack_request = wpan_dev->ackreq;
446 hdr.seq = atomic_inc_return(&dev->ieee802154_ptr->dsn) & 0xFF;
448 /* TODO currently a workaround to give zero cb block to set
449 * security parameters defaults according MIB.
451 if (mac802154_set_header_security(sdata, &hdr, &cb) < 0)
454 hdr.dest.pan_id = wpan_dev->pan_id;
455 hdr.dest.mode = IEEE802154_ADDR_LONG;
456 ieee802154_be64_to_le64(&hdr.dest.extended_addr, daddr);
458 hdr.source.pan_id = hdr.dest.pan_id;
459 hdr.source.mode = IEEE802154_ADDR_LONG;
462 hdr.source.extended_addr = wpan_dev->extended_addr;
464 ieee802154_be64_to_le64(&hdr.source.extended_addr, saddr);
466 hlen = ieee802154_hdr_push(skb, &hdr);
470 skb_reset_mac_header(skb);
473 if (len > ieee802154_max_payload(&hdr))
480 mac802154_header_parse(const struct sk_buff *skb, unsigned char *haddr)
482 struct ieee802154_hdr hdr;
484 if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) {
485 pr_debug("malformed packet\n");
489 if (hdr.source.mode == IEEE802154_ADDR_LONG) {
490 ieee802154_le64_to_be64(haddr, &hdr.source.extended_addr);
491 return IEEE802154_EXTENDED_ADDR_LEN;
497 static const struct header_ops mac802154_header_ops = {
498 .create = mac802154_header_create,
499 .parse = mac802154_header_parse,
502 static const struct net_device_ops mac802154_wpan_ops = {
503 .ndo_open = mac802154_wpan_open,
504 .ndo_stop = mac802154_slave_close,
505 .ndo_start_xmit = ieee802154_subif_start_xmit,
506 .ndo_do_ioctl = mac802154_wpan_ioctl,
507 .ndo_set_mac_address = mac802154_wpan_mac_addr,
510 static const struct net_device_ops mac802154_monitor_ops = {
511 .ndo_open = mac802154_wpan_open,
512 .ndo_stop = mac802154_slave_close,
513 .ndo_start_xmit = ieee802154_monitor_start_xmit,
516 static void mac802154_wpan_free(struct net_device *dev)
518 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
520 mac802154_llsec_destroy(&sdata->sec);
523 static void ieee802154_if_setup(struct net_device *dev)
525 dev->addr_len = IEEE802154_EXTENDED_ADDR_LEN;
526 memset(dev->broadcast, 0xff, IEEE802154_EXTENDED_ADDR_LEN);
528 /* Let hard_header_len set to IEEE802154_MIN_HEADER_LEN. AF_PACKET
529 * will not send frames without any payload, but ack frames
530 * has no payload, so substract one that we can send a 3 bytes
531 * frame. The xmit callback assumes at least a hard header where two
532 * bytes fc and sequence field are set.
534 dev->hard_header_len = IEEE802154_MIN_HEADER_LEN - 1;
535 /* The auth_tag header is for security and places in private payload
536 * room of mac frame which stucks between payload and FCS field.
538 dev->needed_tailroom = IEEE802154_MAX_AUTH_TAG_LEN +
540 /* The mtu size is the payload without mac header in this case.
541 * We have a dynamic length header with a minimum header length
542 * which is hard_header_len. In this case we let mtu to the size
543 * of maximum payload which is IEEE802154_MTU - IEEE802154_FCS_LEN -
544 * hard_header_len. The FCS which is set by hardware or ndo_start_xmit
545 * and the minimum mac header which can be evaluated inside driver
546 * layer. The rest of mac header will be part of payload if greater
547 * than hard_header_len.
549 dev->mtu = IEEE802154_MTU - IEEE802154_FCS_LEN -
550 dev->hard_header_len;
551 dev->tx_queue_len = 300;
552 dev->flags = IFF_NOARP | IFF_BROADCAST;
556 ieee802154_setup_sdata(struct ieee802154_sub_if_data *sdata,
557 enum nl802154_iftype type)
559 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
563 /* set some type-dependent values */
564 sdata->wpan_dev.iftype = type;
566 get_random_bytes(&tmp, sizeof(tmp));
567 atomic_set(&wpan_dev->bsn, tmp);
568 get_random_bytes(&tmp, sizeof(tmp));
569 atomic_set(&wpan_dev->dsn, tmp);
571 /* defaults per 802.15.4-2011 */
572 wpan_dev->min_be = 3;
573 wpan_dev->max_be = 5;
574 wpan_dev->csma_retries = 4;
575 wpan_dev->frame_retries = 3;
577 wpan_dev->pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST);
578 wpan_dev->short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST);
581 case NL802154_IFTYPE_NODE:
582 ieee802154_be64_to_le64(&wpan_dev->extended_addr,
583 sdata->dev->dev_addr);
585 sdata->dev->header_ops = &mac802154_header_ops;
586 sdata->dev->needs_free_netdev = true;
587 sdata->dev->priv_destructor = mac802154_wpan_free;
588 sdata->dev->netdev_ops = &mac802154_wpan_ops;
589 sdata->dev->ml_priv = &mac802154_mlme_wpan;
590 wpan_dev->promiscuous_mode = false;
591 wpan_dev->header_ops = &ieee802154_header_ops;
593 mutex_init(&sdata->sec_mtx);
595 mac802154_llsec_init(&sdata->sec);
596 ret = mac802154_wpan_update_llsec(sdata->dev);
601 case NL802154_IFTYPE_MONITOR:
602 sdata->dev->needs_free_netdev = true;
603 sdata->dev->netdev_ops = &mac802154_monitor_ops;
604 wpan_dev->promiscuous_mode = true;
614 ieee802154_if_add(struct ieee802154_local *local, const char *name,
615 unsigned char name_assign_type, enum nl802154_iftype type,
616 __le64 extended_addr)
618 struct net_device *ndev = NULL;
619 struct ieee802154_sub_if_data *sdata = NULL;
624 ndev = alloc_netdev(sizeof(*sdata), name,
625 name_assign_type, ieee802154_if_setup);
627 return ERR_PTR(-ENOMEM);
629 ndev->needed_headroom = local->hw.extra_tx_headroom +
630 IEEE802154_MAX_HEADER_LEN;
632 ret = dev_alloc_name(ndev, ndev->name);
636 ieee802154_le64_to_be64(ndev->perm_addr,
637 &local->hw.phy->perm_extended_addr);
639 case NL802154_IFTYPE_NODE:
640 ndev->type = ARPHRD_IEEE802154;
641 if (ieee802154_is_valid_extended_unicast_addr(extended_addr))
642 ieee802154_le64_to_be64(ndev->dev_addr, &extended_addr);
644 memcpy(ndev->dev_addr, ndev->perm_addr,
645 IEEE802154_EXTENDED_ADDR_LEN);
647 case NL802154_IFTYPE_MONITOR:
648 ndev->type = ARPHRD_IEEE802154_MONITOR;
655 /* TODO check this */
656 SET_NETDEV_DEV(ndev, &local->phy->dev);
657 dev_net_set(ndev, wpan_phy_net(local->hw.phy));
658 sdata = netdev_priv(ndev);
659 ndev->ieee802154_ptr = &sdata->wpan_dev;
660 memcpy(sdata->name, ndev->name, IFNAMSIZ);
662 sdata->wpan_dev.wpan_phy = local->hw.phy;
663 sdata->local = local;
665 /* setup type-dependent data */
666 ret = ieee802154_setup_sdata(sdata, type);
670 ret = register_netdevice(ndev);
674 mutex_lock(&local->iflist_mtx);
675 list_add_tail_rcu(&sdata->list, &local->interfaces);
676 mutex_unlock(&local->iflist_mtx);
685 void ieee802154_if_remove(struct ieee802154_sub_if_data *sdata)
689 mutex_lock(&sdata->local->iflist_mtx);
690 list_del_rcu(&sdata->list);
691 mutex_unlock(&sdata->local->iflist_mtx);
694 unregister_netdevice(sdata->dev);
697 void ieee802154_remove_interfaces(struct ieee802154_local *local)
699 struct ieee802154_sub_if_data *sdata, *tmp;
701 mutex_lock(&local->iflist_mtx);
702 list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
703 list_del(&sdata->list);
705 unregister_netdevice(sdata->dev);
707 mutex_unlock(&local->iflist_mtx);
710 static int netdev_notify(struct notifier_block *nb,
711 unsigned long state, void *ptr)
713 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
714 struct ieee802154_sub_if_data *sdata;
716 if (state != NETDEV_CHANGENAME)
719 if (!dev->ieee802154_ptr || !dev->ieee802154_ptr->wpan_phy)
722 if (dev->ieee802154_ptr->wpan_phy->privid != mac802154_wpan_phy_privid)
725 sdata = IEEE802154_DEV_TO_SUB_IF(dev);
726 memcpy(sdata->name, dev->name, IFNAMSIZ);
731 static struct notifier_block mac802154_netdev_notifier = {
732 .notifier_call = netdev_notify,
735 int ieee802154_iface_init(void)
737 return register_netdevice_notifier(&mac802154_netdev_notifier);
740 void ieee802154_iface_exit(void)
742 unregister_netdevice_notifier(&mac802154_netdev_notifier);