]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/mac802154/iface.c
2a5878889289c81a4d82b399c506b355e20b9a0e
[mirror_ubuntu-artful-kernel.git] / net / mac802154 / iface.c
1 /*
2 * Copyright 2007-2012 Siemens AG
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * Written by:
14 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
15 * Sergey Lapin <slapin@ossfans.org>
16 * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
17 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
18 */
19
20 #include <linux/netdevice.h>
21 #include <linux/module.h>
22 #include <linux/if_arp.h>
23 #include <linux/ieee802154.h>
24
25 #include <net/nl802154.h>
26 #include <net/mac802154.h>
27 #include <net/ieee802154_netdev.h>
28 #include <net/cfg802154.h>
29
30 #include "ieee802154_i.h"
31 #include "driver-ops.h"
32
33 static int mac802154_wpan_update_llsec(struct net_device *dev)
34 {
35 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
36 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
37 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
38 int rc = 0;
39
40 if (ops->llsec) {
41 struct ieee802154_llsec_params params;
42 int changed = 0;
43
44 params.pan_id = wpan_dev->pan_id;
45 changed |= IEEE802154_LLSEC_PARAM_PAN_ID;
46
47 params.hwaddr = wpan_dev->extended_addr;
48 changed |= IEEE802154_LLSEC_PARAM_HWADDR;
49
50 rc = ops->llsec->set_params(dev, &params, changed);
51 }
52
53 return rc;
54 }
55
56 static int
57 mac802154_wpan_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
58 {
59 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
60 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
61 struct sockaddr_ieee802154 *sa =
62 (struct sockaddr_ieee802154 *)&ifr->ifr_addr;
63 int err = -ENOIOCTLCMD;
64
65 rtnl_lock();
66 spin_lock_bh(&sdata->mib_lock);
67
68 switch (cmd) {
69 case SIOCGIFADDR:
70 {
71 u16 pan_id, short_addr;
72
73 pan_id = le16_to_cpu(wpan_dev->pan_id);
74 short_addr = le16_to_cpu(wpan_dev->short_addr);
75 if (pan_id == IEEE802154_PANID_BROADCAST ||
76 short_addr == IEEE802154_ADDR_BROADCAST) {
77 err = -EADDRNOTAVAIL;
78 break;
79 }
80
81 sa->family = AF_IEEE802154;
82 sa->addr.addr_type = IEEE802154_ADDR_SHORT;
83 sa->addr.pan_id = pan_id;
84 sa->addr.short_addr = short_addr;
85
86 err = 0;
87 break;
88 }
89 case SIOCSIFADDR:
90 if (netif_running(dev)) {
91 spin_unlock_bh(&sdata->mib_lock);
92 rtnl_unlock();
93 return -EBUSY;
94 }
95
96 dev_warn(&dev->dev,
97 "Using DEBUGing ioctl SIOCSIFADDR isn't recommended!\n");
98 if (sa->family != AF_IEEE802154 ||
99 sa->addr.addr_type != IEEE802154_ADDR_SHORT ||
100 sa->addr.pan_id == IEEE802154_PANID_BROADCAST ||
101 sa->addr.short_addr == IEEE802154_ADDR_BROADCAST ||
102 sa->addr.short_addr == IEEE802154_ADDR_UNDEF) {
103 err = -EINVAL;
104 break;
105 }
106
107 wpan_dev->pan_id = cpu_to_le16(sa->addr.pan_id);
108 wpan_dev->short_addr = cpu_to_le16(sa->addr.short_addr);
109
110 err = mac802154_wpan_update_llsec(dev);
111 break;
112 }
113
114 spin_unlock_bh(&sdata->mib_lock);
115 rtnl_unlock();
116 return err;
117 }
118
119 static int mac802154_wpan_mac_addr(struct net_device *dev, void *p)
120 {
121 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
122 struct sockaddr *addr = p;
123 __le64 extended_addr;
124
125 if (netif_running(dev))
126 return -EBUSY;
127
128 ieee802154_be64_to_le64(&extended_addr, addr->sa_data);
129 if (!ieee802154_is_valid_extended_addr(extended_addr))
130 return -EINVAL;
131
132 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
133 sdata->wpan_dev.extended_addr = extended_addr;
134
135 return mac802154_wpan_update_llsec(dev);
136 }
137
138 static int mac802154_slave_open(struct net_device *dev)
139 {
140 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
141 struct ieee802154_local *local = sdata->local;
142 int res = 0;
143
144 ASSERT_RTNL();
145
146 set_bit(SDATA_STATE_RUNNING, &sdata->state);
147
148 if (!local->open_count) {
149 res = drv_start(local);
150 WARN_ON(res);
151 if (res)
152 goto err;
153 }
154
155 local->open_count++;
156 netif_start_queue(dev);
157 return 0;
158 err:
159 /* might already be clear but that doesn't matter */
160 clear_bit(SDATA_STATE_RUNNING, &sdata->state);
161
162 return res;
163 }
164
165 static int
166 ieee802154_check_mac_settings(struct ieee802154_local *local,
167 struct wpan_dev *wpan_dev,
168 struct wpan_dev *nwpan_dev)
169 {
170 ASSERT_RTNL();
171
172 if (local->hw.flags & IEEE802154_HW_PROMISCUOUS) {
173 if (wpan_dev->promiscuous_mode != nwpan_dev->promiscuous_mode)
174 return -EBUSY;
175 }
176
177 if (local->hw.flags & IEEE802154_HW_AFILT) {
178 if (wpan_dev->pan_id != nwpan_dev->pan_id ||
179 wpan_dev->short_addr != nwpan_dev->short_addr ||
180 wpan_dev->extended_addr != nwpan_dev->extended_addr)
181 return -EBUSY;
182 }
183
184 if (local->hw.flags & IEEE802154_HW_CSMA_PARAMS) {
185 if (wpan_dev->min_be != nwpan_dev->min_be ||
186 wpan_dev->max_be != nwpan_dev->max_be ||
187 wpan_dev->csma_retries != nwpan_dev->csma_retries)
188 return -EBUSY;
189 }
190
191 if (local->hw.flags & IEEE802154_HW_FRAME_RETRIES) {
192 if (wpan_dev->frame_retries != nwpan_dev->frame_retries)
193 return -EBUSY;
194 }
195
196 if (local->hw.flags & IEEE802154_HW_LBT) {
197 if (wpan_dev->lbt != nwpan_dev->lbt)
198 return -EBUSY;
199 }
200
201 return 0;
202 }
203
204 static int
205 ieee802154_check_concurrent_iface(struct ieee802154_sub_if_data *sdata,
206 enum nl802154_iftype iftype)
207 {
208 struct ieee802154_local *local = sdata->local;
209 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
210 struct ieee802154_sub_if_data *nsdata;
211
212 /* we hold the RTNL here so can safely walk the list */
213 list_for_each_entry(nsdata, &local->interfaces, list) {
214 if (nsdata != sdata && ieee802154_sdata_running(nsdata)) {
215 int ret;
216
217 /* TODO currently we don't support multiple node types
218 * we need to run skb_clone at rx path. Check if there
219 * exist really an use case if we need to support
220 * multiple node types at the same time.
221 */
222 if (sdata->vif.type == NL802154_IFTYPE_NODE &&
223 nsdata->vif.type == NL802154_IFTYPE_NODE)
224 return -EBUSY;
225
226 /* check all phy mac sublayer settings are the same.
227 * We have only one phy, different values makes trouble.
228 */
229 ret = ieee802154_check_mac_settings(local, wpan_dev,
230 &nsdata->wpan_dev);
231 if (ret < 0)
232 return ret;
233 }
234 }
235
236 return 0;
237 }
238
239 static int mac802154_wpan_open(struct net_device *dev)
240 {
241 int rc;
242 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
243 struct ieee802154_local *local = sdata->local;
244 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
245 struct wpan_phy *phy = sdata->local->phy;
246
247 rc = ieee802154_check_concurrent_iface(sdata, sdata->vif.type);
248 if (rc < 0)
249 return rc;
250
251 rc = mac802154_slave_open(dev);
252 if (rc < 0)
253 return rc;
254
255 mutex_lock(&phy->pib_lock);
256
257 if (local->hw.flags & IEEE802154_HW_PROMISCUOUS) {
258 rc = drv_set_promiscuous_mode(local,
259 wpan_dev->promiscuous_mode);
260 if (rc < 0)
261 goto out;
262 }
263
264 if (local->hw.flags & IEEE802154_HW_AFILT) {
265 rc = drv_set_pan_id(local, wpan_dev->pan_id);
266 if (rc < 0)
267 goto out;
268
269 rc = drv_set_extended_addr(local, wpan_dev->extended_addr);
270 if (rc < 0)
271 goto out;
272
273 rc = drv_set_short_addr(local, wpan_dev->short_addr);
274 if (rc < 0)
275 goto out;
276 }
277
278 if (local->hw.flags & IEEE802154_HW_LBT) {
279 rc = drv_set_lbt_mode(local, wpan_dev->lbt);
280 if (rc < 0)
281 goto out;
282 }
283
284 if (local->hw.flags & IEEE802154_HW_CSMA_PARAMS) {
285 rc = drv_set_csma_params(local, wpan_dev->min_be,
286 wpan_dev->max_be,
287 wpan_dev->csma_retries);
288 if (rc < 0)
289 goto out;
290 }
291
292 if (local->hw.flags & IEEE802154_HW_FRAME_RETRIES) {
293 rc = drv_set_max_frame_retries(local, wpan_dev->frame_retries);
294 if (rc < 0)
295 goto out;
296 }
297
298 mutex_unlock(&phy->pib_lock);
299 return 0;
300
301 out:
302 mutex_unlock(&phy->pib_lock);
303 return rc;
304 }
305
306 static int mac802154_slave_close(struct net_device *dev)
307 {
308 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
309 struct ieee802154_local *local = sdata->local;
310
311 ASSERT_RTNL();
312
313 hrtimer_cancel(&local->ifs_timer);
314
315 netif_stop_queue(dev);
316 local->open_count--;
317
318 clear_bit(SDATA_STATE_RUNNING, &sdata->state);
319
320 if (!local->open_count)
321 drv_stop(local);
322
323 return 0;
324 }
325
326 static int mac802154_set_header_security(struct ieee802154_sub_if_data *sdata,
327 struct ieee802154_hdr *hdr,
328 const struct ieee802154_mac_cb *cb)
329 {
330 struct ieee802154_llsec_params params;
331 u8 level;
332
333 mac802154_llsec_get_params(&sdata->sec, &params);
334
335 if (!params.enabled && cb->secen_override && cb->secen)
336 return -EINVAL;
337 if (!params.enabled ||
338 (cb->secen_override && !cb->secen) ||
339 !params.out_level)
340 return 0;
341 if (cb->seclevel_override && !cb->seclevel)
342 return -EINVAL;
343
344 level = cb->seclevel_override ? cb->seclevel : params.out_level;
345
346 hdr->fc.security_enabled = 1;
347 hdr->sec.level = level;
348 hdr->sec.key_id_mode = params.out_key.mode;
349 if (params.out_key.mode == IEEE802154_SCF_KEY_SHORT_INDEX)
350 hdr->sec.short_src = params.out_key.short_source;
351 else if (params.out_key.mode == IEEE802154_SCF_KEY_HW_INDEX)
352 hdr->sec.extended_src = params.out_key.extended_source;
353 hdr->sec.key_id = params.out_key.id;
354
355 return 0;
356 }
357
358 static int mac802154_header_create(struct sk_buff *skb,
359 struct net_device *dev,
360 unsigned short type,
361 const void *daddr,
362 const void *saddr,
363 unsigned len)
364 {
365 struct ieee802154_hdr hdr;
366 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
367 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
368 struct ieee802154_mac_cb *cb = mac_cb(skb);
369 int hlen;
370
371 if (!daddr)
372 return -EINVAL;
373
374 memset(&hdr.fc, 0, sizeof(hdr.fc));
375 hdr.fc.type = cb->type;
376 hdr.fc.security_enabled = cb->secen;
377 hdr.fc.ack_request = cb->ackreq;
378 hdr.seq = ieee802154_mlme_ops(dev)->get_dsn(dev);
379
380 if (mac802154_set_header_security(sdata, &hdr, cb) < 0)
381 return -EINVAL;
382
383 if (!saddr) {
384 spin_lock_bh(&sdata->mib_lock);
385
386 if (wpan_dev->short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST) ||
387 wpan_dev->short_addr == cpu_to_le16(IEEE802154_ADDR_UNDEF) ||
388 wpan_dev->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST)) {
389 hdr.source.mode = IEEE802154_ADDR_LONG;
390 hdr.source.extended_addr = wpan_dev->extended_addr;
391 } else {
392 hdr.source.mode = IEEE802154_ADDR_SHORT;
393 hdr.source.short_addr = wpan_dev->short_addr;
394 }
395
396 hdr.source.pan_id = wpan_dev->pan_id;
397
398 spin_unlock_bh(&sdata->mib_lock);
399 } else {
400 hdr.source = *(const struct ieee802154_addr *)saddr;
401 }
402
403 hdr.dest = *(const struct ieee802154_addr *)daddr;
404
405 hlen = ieee802154_hdr_push(skb, &hdr);
406 if (hlen < 0)
407 return -EINVAL;
408
409 skb_reset_mac_header(skb);
410 skb->mac_len = hlen;
411
412 if (len > ieee802154_max_payload(&hdr))
413 return -EMSGSIZE;
414
415 return hlen;
416 }
417
418 static int
419 mac802154_header_parse(const struct sk_buff *skb, unsigned char *haddr)
420 {
421 struct ieee802154_hdr hdr;
422 struct ieee802154_addr *addr = (struct ieee802154_addr *)haddr;
423
424 if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) {
425 pr_debug("malformed packet\n");
426 return 0;
427 }
428
429 *addr = hdr.source;
430 return sizeof(*addr);
431 }
432
433 static struct header_ops mac802154_header_ops = {
434 .create = mac802154_header_create,
435 .parse = mac802154_header_parse,
436 };
437
438 static const struct net_device_ops mac802154_wpan_ops = {
439 .ndo_open = mac802154_wpan_open,
440 .ndo_stop = mac802154_slave_close,
441 .ndo_start_xmit = ieee802154_subif_start_xmit,
442 .ndo_do_ioctl = mac802154_wpan_ioctl,
443 .ndo_set_mac_address = mac802154_wpan_mac_addr,
444 };
445
446 static const struct net_device_ops mac802154_monitor_ops = {
447 .ndo_open = mac802154_wpan_open,
448 .ndo_stop = mac802154_slave_close,
449 .ndo_start_xmit = ieee802154_monitor_start_xmit,
450 };
451
452 static void mac802154_wpan_free(struct net_device *dev)
453 {
454 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
455
456 mac802154_llsec_destroy(&sdata->sec);
457
458 free_netdev(dev);
459 }
460
461 static void ieee802154_if_setup(struct net_device *dev)
462 {
463 dev->addr_len = IEEE802154_EXTENDED_ADDR_LEN;
464 memset(dev->broadcast, 0xff, IEEE802154_EXTENDED_ADDR_LEN);
465
466 dev->hard_header_len = MAC802154_FRAME_HARD_HEADER_LEN;
467 dev->needed_tailroom = 2 + 16; /* FCS + MIC */
468 dev->mtu = IEEE802154_MTU;
469 dev->tx_queue_len = 300;
470 dev->flags = IFF_NOARP | IFF_BROADCAST;
471 }
472
473 static int
474 ieee802154_setup_sdata(struct ieee802154_sub_if_data *sdata,
475 enum nl802154_iftype type)
476 {
477 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
478
479 /* set some type-dependent values */
480 sdata->vif.type = type;
481 sdata->wpan_dev.iftype = type;
482
483 get_random_bytes(&wpan_dev->bsn, 1);
484 get_random_bytes(&wpan_dev->dsn, 1);
485
486 /* defaults per 802.15.4-2011 */
487 wpan_dev->min_be = 3;
488 wpan_dev->max_be = 5;
489 wpan_dev->csma_retries = 4;
490 /* for compatibility, actual default is 3 */
491 wpan_dev->frame_retries = -1;
492
493 wpan_dev->pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST);
494 wpan_dev->short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST);
495
496 switch (type) {
497 case NL802154_IFTYPE_NODE:
498 ieee802154_be64_to_le64(&wpan_dev->extended_addr,
499 sdata->dev->dev_addr);
500
501 sdata->dev->header_ops = &mac802154_header_ops;
502 sdata->dev->destructor = mac802154_wpan_free;
503 sdata->dev->netdev_ops = &mac802154_wpan_ops;
504 sdata->dev->ml_priv = &mac802154_mlme_wpan;
505 wpan_dev->promiscuous_mode = false;
506
507 spin_lock_init(&sdata->mib_lock);
508 mutex_init(&sdata->sec_mtx);
509
510 mac802154_llsec_init(&sdata->sec);
511 break;
512 case NL802154_IFTYPE_MONITOR:
513 sdata->dev->destructor = free_netdev;
514 sdata->dev->netdev_ops = &mac802154_monitor_ops;
515 wpan_dev->promiscuous_mode = true;
516 break;
517 default:
518 BUG();
519 }
520
521 return 0;
522 }
523
524 struct net_device *
525 ieee802154_if_add(struct ieee802154_local *local, const char *name,
526 unsigned char name_assign_type, enum nl802154_iftype type,
527 __le64 extended_addr)
528 {
529 struct net_device *ndev = NULL;
530 struct ieee802154_sub_if_data *sdata = NULL;
531 int ret = -ENOMEM;
532
533 ASSERT_RTNL();
534
535 ndev = alloc_netdev(sizeof(*sdata) + local->hw.vif_data_size, name,
536 name_assign_type, ieee802154_if_setup);
537 if (!ndev)
538 return ERR_PTR(-ENOMEM);
539
540 ndev->needed_headroom = local->hw.extra_tx_headroom;
541
542 ret = dev_alloc_name(ndev, ndev->name);
543 if (ret < 0)
544 goto err;
545
546 ieee802154_le64_to_be64(ndev->perm_addr,
547 &local->hw.phy->perm_extended_addr);
548 switch (type) {
549 case NL802154_IFTYPE_NODE:
550 ndev->type = ARPHRD_IEEE802154;
551 if (ieee802154_is_valid_extended_addr(extended_addr))
552 ieee802154_le64_to_be64(ndev->dev_addr, &extended_addr);
553 else
554 memcpy(ndev->dev_addr, ndev->perm_addr,
555 IEEE802154_EXTENDED_ADDR_LEN);
556 break;
557 case NL802154_IFTYPE_MONITOR:
558 ndev->type = ARPHRD_IEEE802154_MONITOR;
559 break;
560 default:
561 ret = -EINVAL;
562 goto err;
563 }
564
565 /* TODO check this */
566 SET_NETDEV_DEV(ndev, &local->phy->dev);
567 sdata = netdev_priv(ndev);
568 ndev->ieee802154_ptr = &sdata->wpan_dev;
569 memcpy(sdata->name, ndev->name, IFNAMSIZ);
570 sdata->dev = ndev;
571 sdata->wpan_dev.wpan_phy = local->hw.phy;
572 sdata->local = local;
573
574 /* setup type-dependent data */
575 ret = ieee802154_setup_sdata(sdata, type);
576 if (ret)
577 goto err;
578
579 ret = register_netdevice(ndev);
580 if (ret < 0)
581 goto err;
582
583 mutex_lock(&local->iflist_mtx);
584 list_add_tail_rcu(&sdata->list, &local->interfaces);
585 mutex_unlock(&local->iflist_mtx);
586
587 return ndev;
588
589 err:
590 free_netdev(ndev);
591 return ERR_PTR(ret);
592 }
593
594 void ieee802154_if_remove(struct ieee802154_sub_if_data *sdata)
595 {
596 ASSERT_RTNL();
597
598 mutex_lock(&sdata->local->iflist_mtx);
599 list_del_rcu(&sdata->list);
600 mutex_unlock(&sdata->local->iflist_mtx);
601
602 synchronize_rcu();
603 unregister_netdevice(sdata->dev);
604 }
605
606 void ieee802154_remove_interfaces(struct ieee802154_local *local)
607 {
608 struct ieee802154_sub_if_data *sdata, *tmp;
609
610 mutex_lock(&local->iflist_mtx);
611 list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
612 list_del(&sdata->list);
613
614 unregister_netdevice(sdata->dev);
615 }
616 mutex_unlock(&local->iflist_mtx);
617 }
618
619 static int netdev_notify(struct notifier_block *nb,
620 unsigned long state, void *ptr)
621 {
622 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
623 struct ieee802154_sub_if_data *sdata;
624
625 if (state != NETDEV_CHANGENAME)
626 return NOTIFY_DONE;
627
628 if (!dev->ieee802154_ptr || !dev->ieee802154_ptr->wpan_phy)
629 return NOTIFY_DONE;
630
631 if (dev->ieee802154_ptr->wpan_phy->privid != mac802154_wpan_phy_privid)
632 return NOTIFY_DONE;
633
634 sdata = IEEE802154_DEV_TO_SUB_IF(dev);
635 memcpy(sdata->name, dev->name, IFNAMSIZ);
636
637 return NOTIFY_OK;
638 }
639
640 static struct notifier_block mac802154_netdev_notifier = {
641 .notifier_call = netdev_notify,
642 };
643
644 int ieee802154_iface_init(void)
645 {
646 return register_netdevice_notifier(&mac802154_netdev_notifier);
647 }
648
649 void ieee802154_iface_exit(void)
650 {
651 unregister_netdevice_notifier(&mac802154_netdev_notifier);
652 }