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 dev_addr_set(dev, addr->sa_data);
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 dev_addr_set(sdata->wpan_dev.lowpan_dev, dev->dev_addr);
141 return mac802154_wpan_update_llsec(dev);
144 static int ieee802154_setup_hw(struct ieee802154_sub_if_data *sdata)
146 struct ieee802154_local *local = sdata->local;
147 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
150 sdata->required_filtering = sdata->iface_default_filtering;
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;
158 if (local->hw.flags & IEEE802154_HW_LBT) {
159 ret = drv_set_lbt_mode(local, wpan_dev->lbt);
164 if (local->hw.flags & IEEE802154_HW_CSMA_PARAMS) {
165 ret = drv_set_csma_params(local, wpan_dev->min_be,
167 wpan_dev->csma_retries);
172 if (local->hw.flags & IEEE802154_HW_FRAME_RETRIES) {
173 ret = drv_set_max_frame_retries(local, wpan_dev->frame_retries);
181 static int mac802154_slave_open(struct net_device *dev)
183 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
184 struct ieee802154_local *local = sdata->local;
189 set_bit(SDATA_STATE_RUNNING, &sdata->state);
191 if (!local->open_count) {
192 res = ieee802154_setup_hw(sdata);
196 res = drv_start(local, sdata->required_filtering,
203 netif_start_queue(dev);
206 /* might already be clear but that doesn't matter */
207 clear_bit(SDATA_STATE_RUNNING, &sdata->state);
213 ieee802154_check_mac_settings(struct ieee802154_local *local,
214 struct ieee802154_sub_if_data *sdata,
215 struct ieee802154_sub_if_data *nsdata)
217 struct wpan_dev *nwpan_dev = &nsdata->wpan_dev;
218 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
222 if (sdata->iface_default_filtering != nsdata->iface_default_filtering)
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)
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)
239 if (local->hw.flags & IEEE802154_HW_FRAME_RETRIES) {
240 if (wpan_dev->frame_retries != nwpan_dev->frame_retries)
244 if (local->hw.flags & IEEE802154_HW_LBT) {
245 if (wpan_dev->lbt != nwpan_dev->lbt)
253 ieee802154_check_concurrent_iface(struct ieee802154_sub_if_data *sdata,
254 enum nl802154_iftype iftype)
256 struct ieee802154_local *local = sdata->local;
257 struct ieee802154_sub_if_data *nsdata;
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)) {
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.
269 if (sdata->wpan_dev.iftype != NL802154_IFTYPE_MONITOR &&
270 nsdata->wpan_dev.iftype != NL802154_IFTYPE_MONITOR)
273 /* check all phy mac sublayer settings are the same.
274 * We have only one phy, different values makes trouble.
276 ret = ieee802154_check_mac_settings(local, sdata, nsdata);
285 static int mac802154_wpan_open(struct net_device *dev)
288 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
289 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
291 rc = ieee802154_check_concurrent_iface(sdata, wpan_dev->iftype);
295 return mac802154_slave_open(dev);
298 static int mac802154_slave_close(struct net_device *dev)
300 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
301 struct ieee802154_local *local = sdata->local;
305 if (mac802154_is_scanning(local))
306 mac802154_abort_scan_locked(local, sdata);
308 if (mac802154_is_beaconing(local))
309 mac802154_stop_beacons_locked(local, sdata);
311 netif_stop_queue(dev);
314 clear_bit(SDATA_STATE_RUNNING, &sdata->state);
316 if (!local->open_count)
317 ieee802154_stop_device(local);
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)
326 struct ieee802154_llsec_params params;
329 mac802154_llsec_get_params(&sdata->sec, ¶ms);
331 if (!params.enabled && cb->secen_override && cb->secen)
333 if (!params.enabled ||
334 (cb->secen_override && !cb->secen) ||
337 if (cb->seclevel_override && !cb->seclevel)
340 level = cb->seclevel_override ? cb->seclevel : params.out_level;
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;
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,
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);
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;
375 if (mac802154_set_header_security(sdata, &hdr, cb) < 0)
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;
385 hdr.source.mode = IEEE802154_ADDR_SHORT;
386 hdr.source.short_addr = wpan_dev->short_addr;
389 hdr.source.pan_id = wpan_dev->pan_id;
391 hdr.source = *(const struct ieee802154_addr *)saddr;
394 hdr.dest = *(const struct ieee802154_addr *)daddr;
396 hlen = ieee802154_hdr_push(skb, &hdr);
400 skb_reset_mac_header(skb);
403 if (len > ieee802154_max_payload(&hdr))
409 static const struct wpan_dev_header_ops ieee802154_header_ops = {
410 .create = ieee802154_header_create,
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.
419 static int mac802154_header_create(struct sk_buff *skb,
420 struct net_device *dev,
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 = { };
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;
440 /* TODO currently a workaround to give zero cb block to set
441 * security parameters defaults according MIB.
443 if (mac802154_set_header_security(sdata, &hdr, &cb) < 0)
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);
450 hdr.source.pan_id = hdr.dest.pan_id;
451 hdr.source.mode = IEEE802154_ADDR_LONG;
454 hdr.source.extended_addr = wpan_dev->extended_addr;
456 ieee802154_be64_to_le64(&hdr.source.extended_addr, saddr);
458 hlen = ieee802154_hdr_push(skb, &hdr);
462 skb_reset_mac_header(skb);
465 if (len > ieee802154_max_payload(&hdr))
472 mac802154_header_parse(const struct sk_buff *skb, unsigned char *haddr)
474 struct ieee802154_hdr hdr;
476 if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) {
477 pr_debug("malformed packet\n");
481 if (hdr.source.mode == IEEE802154_ADDR_LONG) {
482 ieee802154_le64_to_be64(haddr, &hdr.source.extended_addr);
483 return IEEE802154_EXTENDED_ADDR_LEN;
489 static const struct header_ops mac802154_header_ops = {
490 .create = mac802154_header_create,
491 .parse = mac802154_header_parse,
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,
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,
508 static void mac802154_wpan_free(struct net_device *dev)
510 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
512 mac802154_llsec_destroy(&sdata->sec);
515 static void ieee802154_if_setup(struct net_device *dev)
517 dev->addr_len = IEEE802154_EXTENDED_ADDR_LEN;
518 memset(dev->broadcast, 0xff, IEEE802154_EXTENDED_ADDR_LEN);
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.
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.
530 dev->needed_tailroom = IEEE802154_MAX_AUTH_TAG_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.
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;
548 ieee802154_setup_sdata(struct ieee802154_sub_if_data *sdata,
549 enum nl802154_iftype type)
551 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
555 /* set some type-dependent values */
556 sdata->wpan_dev.iftype = type;
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);
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;
569 wpan_dev->pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST);
570 wpan_dev->short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST);
573 case NL802154_IFTYPE_COORD:
574 case NL802154_IFTYPE_NODE:
575 ieee802154_be64_to_le64(&wpan_dev->extended_addr,
576 sdata->dev->dev_addr);
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;
586 mutex_init(&sdata->sec_mtx);
588 mac802154_llsec_init(&sdata->sec);
589 ret = mac802154_wpan_update_llsec(sdata->dev);
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;
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)
611 u8 addr[IEEE802154_EXTENDED_ADDR_LEN];
612 struct net_device *ndev = NULL;
613 struct ieee802154_sub_if_data *sdata = NULL;
618 ndev = alloc_netdev(sizeof(*sdata), name,
619 name_assign_type, ieee802154_if_setup);
621 return ERR_PTR(-ENOMEM);
623 ndev->needed_headroom = local->hw.extra_tx_headroom +
624 IEEE802154_MAX_HEADER_LEN;
626 ret = dev_alloc_name(ndev, ndev->name);
630 ieee802154_le64_to_be64(ndev->perm_addr,
631 &local->hw.phy->perm_extended_addr);
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);
640 dev_addr_set(ndev, ndev->perm_addr);
643 case NL802154_IFTYPE_MONITOR:
644 ndev->type = ARPHRD_IEEE802154_MONITOR;
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);
658 sdata->wpan_dev.wpan_phy = local->hw.phy;
659 sdata->local = local;
660 INIT_LIST_HEAD(&sdata->wpan_dev.list);
662 /* setup type-dependent data */
663 ret = ieee802154_setup_sdata(sdata, type);
667 ret = register_netdevice(ndev);
671 mutex_lock(&local->iflist_mtx);
672 list_add_tail_rcu(&sdata->list, &local->interfaces);
673 mutex_unlock(&local->iflist_mtx);
682 void ieee802154_if_remove(struct ieee802154_sub_if_data *sdata)
686 mutex_lock(&sdata->local->iflist_mtx);
687 list_del_rcu(&sdata->list);
688 mutex_unlock(&sdata->local->iflist_mtx);
691 unregister_netdevice(sdata->dev);
694 void ieee802154_remove_interfaces(struct ieee802154_local *local)
696 struct ieee802154_sub_if_data *sdata, *tmp;
698 mutex_lock(&local->iflist_mtx);
699 list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
700 list_del(&sdata->list);
702 unregister_netdevice(sdata->dev);
704 mutex_unlock(&local->iflist_mtx);
707 static int netdev_notify(struct notifier_block *nb,
708 unsigned long state, void *ptr)
710 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
711 struct ieee802154_sub_if_data *sdata;
713 if (state != NETDEV_CHANGENAME)
716 if (!dev->ieee802154_ptr || !dev->ieee802154_ptr->wpan_phy)
719 if (dev->ieee802154_ptr->wpan_phy->privid != mac802154_wpan_phy_privid)
722 sdata = IEEE802154_DEV_TO_SUB_IF(dev);
723 memcpy(sdata->name, dev->name, IFNAMSIZ);
728 static struct notifier_block mac802154_netdev_notifier = {
729 .notifier_call = netdev_notify,
732 int ieee802154_iface_init(void)
734 return register_netdevice_notifier(&mac802154_netdev_notifier);
737 void ieee802154_iface_exit(void)
739 unregister_netdevice_notifier(&mac802154_netdev_notifier);