mac802154: move wpan.c to iface.c
[linux-2.6-block.git] / net / mac802154 / iface.c
CommitLineData
32bad7e3 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 *
32bad7e3 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
24#include <net/rtnetlink.h>
25#include <linux/nl802154.h>
26#include <net/af_ieee802154.h>
27#include <net/mac802154.h>
28#include <net/ieee802154_netdev.h>
29#include <net/ieee802154.h>
30#include <net/wpan-phy.h>
31
0f1556bc 32#include "ieee802154_i.h"
32bad7e3 33
9b0bb4a8
PB
34static int mac802154_wpan_update_llsec(struct net_device *dev)
35{
36 struct mac802154_sub_if_data *priv = netdev_priv(dev);
37 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(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 = priv->pan_id;
45 changed |= IEEE802154_LLSEC_PARAM_PAN_ID;
46
47 params.hwaddr = priv->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
32bad7e3 56static int
57mac802154_wpan_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
58{
59 struct mac802154_sub_if_data *priv = netdev_priv(dev);
60 struct sockaddr_ieee802154 *sa =
61 (struct sockaddr_ieee802154 *)&ifr->ifr_addr;
62 int err = -ENOIOCTLCMD;
63
64 spin_lock_bh(&priv->mib_lock);
65
66 switch (cmd) {
67 case SIOCGIFADDR:
b70ab2e8
PB
68 {
69 u16 pan_id, short_addr;
70
71 pan_id = le16_to_cpu(priv->pan_id);
72 short_addr = le16_to_cpu(priv->short_addr);
73 if (pan_id == IEEE802154_PANID_BROADCAST ||
74 short_addr == IEEE802154_ADDR_BROADCAST) {
32bad7e3 75 err = -EADDRNOTAVAIL;
76 break;
77 }
78
79 sa->family = AF_IEEE802154;
80 sa->addr.addr_type = IEEE802154_ADDR_SHORT;
b70ab2e8
PB
81 sa->addr.pan_id = pan_id;
82 sa->addr.short_addr = short_addr;
32bad7e3 83
84 err = 0;
85 break;
b70ab2e8 86 }
32bad7e3 87 case SIOCSIFADDR:
88 dev_warn(&dev->dev,
9b13494c 89 "Using DEBUGing ioctl SIOCSIFADDR isn't recommended!\n");
32bad7e3 90 if (sa->family != AF_IEEE802154 ||
91 sa->addr.addr_type != IEEE802154_ADDR_SHORT ||
92 sa->addr.pan_id == IEEE802154_PANID_BROADCAST ||
93 sa->addr.short_addr == IEEE802154_ADDR_BROADCAST ||
94 sa->addr.short_addr == IEEE802154_ADDR_UNDEF) {
95 err = -EINVAL;
96 break;
97 }
98
b70ab2e8
PB
99 priv->pan_id = cpu_to_le16(sa->addr.pan_id);
100 priv->short_addr = cpu_to_le16(sa->addr.short_addr);
32bad7e3 101
9b0bb4a8 102 err = mac802154_wpan_update_llsec(dev);
32bad7e3 103 break;
104 }
105
106 spin_unlock_bh(&priv->mib_lock);
107 return err;
108}
109
110static int mac802154_wpan_mac_addr(struct net_device *dev, void *p)
111{
112 struct sockaddr *addr = p;
113
114 if (netif_running(dev))
115 return -EBUSY;
116
117 /* FIXME: validate addr */
118 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
119 mac802154_dev_set_ieee_addr(dev);
9b0bb4a8 120 return mac802154_wpan_update_llsec(dev);
32bad7e3 121}
122
e462ded6
PB
123int mac802154_set_mac_params(struct net_device *dev,
124 const struct ieee802154_mac_params *params)
125{
126 struct mac802154_sub_if_data *priv = netdev_priv(dev);
127
128 mutex_lock(&priv->hw->slaves_mtx);
129 priv->mac_params = *params;
130 mutex_unlock(&priv->hw->slaves_mtx);
131
132 return 0;
133}
134
135void mac802154_get_mac_params(struct net_device *dev,
136 struct ieee802154_mac_params *params)
137{
138 struct mac802154_sub_if_data *priv = netdev_priv(dev);
139
140 mutex_lock(&priv->hw->slaves_mtx);
141 *params = priv->mac_params;
142 mutex_unlock(&priv->hw->slaves_mtx);
143}
144
6ef0023a 145static int mac802154_wpan_open(struct net_device *dev)
e462ded6
PB
146{
147 int rc;
148 struct mac802154_sub_if_data *priv = netdev_priv(dev);
149 struct wpan_phy *phy = priv->hw->phy;
150
151 rc = mac802154_slave_open(dev);
152 if (rc < 0)
153 return rc;
154
155 mutex_lock(&phy->pib_lock);
156
157 if (phy->set_txpower) {
158 rc = phy->set_txpower(phy, priv->mac_params.transmit_power);
159 if (rc < 0)
160 goto out;
161 }
162
163 if (phy->set_lbt) {
164 rc = phy->set_lbt(phy, priv->mac_params.lbt);
165 if (rc < 0)
166 goto out;
167 }
168
169 if (phy->set_cca_mode) {
170 rc = phy->set_cca_mode(phy, priv->mac_params.cca_mode);
171 if (rc < 0)
172 goto out;
173 }
174
175 if (phy->set_cca_ed_level) {
176 rc = phy->set_cca_ed_level(phy, priv->mac_params.cca_ed_level);
177 if (rc < 0)
178 goto out;
179 }
180
181 if (phy->set_csma_params) {
182 rc = phy->set_csma_params(phy, priv->mac_params.min_be,
183 priv->mac_params.max_be,
184 priv->mac_params.csma_retries);
185 if (rc < 0)
186 goto out;
187 }
188
189 if (phy->set_frame_retries) {
190 rc = phy->set_frame_retries(phy,
191 priv->mac_params.frame_retries);
192 if (rc < 0)
193 goto out;
194 }
195
196 mutex_unlock(&phy->pib_lock);
197 return 0;
198
199out:
200 mutex_unlock(&phy->pib_lock);
201 return rc;
202}
203
f30be4d5
PB
204static int mac802154_set_header_security(struct mac802154_sub_if_data *priv,
205 struct ieee802154_hdr *hdr,
206 const struct ieee802154_mac_cb *cb)
207{
208 struct ieee802154_llsec_params params;
209 u8 level;
210
211 mac802154_llsec_get_params(&priv->sec, &params);
212
213 if (!params.enabled && cb->secen_override && cb->secen)
214 return -EINVAL;
215 if (!params.enabled ||
216 (cb->secen_override && !cb->secen) ||
217 !params.out_level)
218 return 0;
219 if (cb->seclevel_override && !cb->seclevel)
220 return -EINVAL;
221
222 level = cb->seclevel_override ? cb->seclevel : params.out_level;
223
224 hdr->fc.security_enabled = 1;
225 hdr->sec.level = level;
226 hdr->sec.key_id_mode = params.out_key.mode;
227 if (params.out_key.mode == IEEE802154_SCF_KEY_SHORT_INDEX)
228 hdr->sec.short_src = params.out_key.short_source;
229 else if (params.out_key.mode == IEEE802154_SCF_KEY_HW_INDEX)
230 hdr->sec.extended_src = params.out_key.extended_source;
231 hdr->sec.key_id = params.out_key.id;
232
233 return 0;
234}
235
32bad7e3 236static int mac802154_header_create(struct sk_buff *skb,
237 struct net_device *dev,
238 unsigned short type,
e6278d92
PB
239 const void *daddr,
240 const void *saddr,
32bad7e3 241 unsigned len)
242{
e6278d92 243 struct ieee802154_hdr hdr;
32bad7e3 244 struct mac802154_sub_if_data *priv = netdev_priv(dev);
32edc40a 245 struct ieee802154_mac_cb *cb = mac_cb(skb);
e6278d92 246 int hlen;
32bad7e3 247
248 if (!daddr)
249 return -EINVAL;
250
e6278d92 251 memset(&hdr.fc, 0, sizeof(hdr.fc));
32edc40a
PB
252 hdr.fc.type = cb->type;
253 hdr.fc.security_enabled = cb->secen;
254 hdr.fc.ack_request = cb->ackreq;
255 hdr.seq = ieee802154_mlme_ops(dev)->get_dsn(dev);
32bad7e3 256
f30be4d5
PB
257 if (mac802154_set_header_security(priv, &hdr, cb) < 0)
258 return -EINVAL;
259
32bad7e3 260 if (!saddr) {
261 spin_lock_bh(&priv->mib_lock);
262
b70ab2e8
PB
263 if (priv->short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST) ||
264 priv->short_addr == cpu_to_le16(IEEE802154_ADDR_UNDEF) ||
265 priv->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST)) {
e6278d92
PB
266 hdr.source.mode = IEEE802154_ADDR_LONG;
267 hdr.source.extended_addr = priv->extended_addr;
32bad7e3 268 } else {
e6278d92
PB
269 hdr.source.mode = IEEE802154_ADDR_SHORT;
270 hdr.source.short_addr = priv->short_addr;
32bad7e3 271 }
272
e6278d92 273 hdr.source.pan_id = priv->pan_id;
32bad7e3 274
275 spin_unlock_bh(&priv->mib_lock);
e6278d92
PB
276 } else {
277 hdr.source = *(const struct ieee802154_addr *)saddr;
32bad7e3 278 }
279
e6278d92 280 hdr.dest = *(const struct ieee802154_addr *)daddr;
32bad7e3 281
e6278d92
PB
282 hlen = ieee802154_hdr_push(skb, &hdr);
283 if (hlen < 0)
284 return -EINVAL;
32bad7e3 285
3e69162e 286 skb_reset_mac_header(skb);
e6278d92 287 skb->mac_len = hlen;
32bad7e3 288
8c84296f 289 if (len > ieee802154_max_payload(&hdr))
d1d7358e
PB
290 return -EMSGSIZE;
291
e6278d92 292 return hlen;
32bad7e3 293}
294
295static int
296mac802154_header_parse(const struct sk_buff *skb, unsigned char *haddr)
297{
e6278d92
PB
298 struct ieee802154_hdr hdr;
299 struct ieee802154_addr *addr = (struct ieee802154_addr *)haddr;
32bad7e3 300
e6278d92
PB
301 if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) {
302 pr_debug("malformed packet\n");
303 return 0;
32bad7e3 304 }
305
e6278d92
PB
306 *addr = hdr.source;
307 return sizeof(*addr);
32bad7e3 308}
309
310static netdev_tx_t
311mac802154_wpan_xmit(struct sk_buff *skb, struct net_device *dev)
312{
313 struct mac802154_sub_if_data *priv;
314 u8 chan, page;
f30be4d5 315 int rc;
32bad7e3 316
317 priv = netdev_priv(dev);
318
319 spin_lock_bh(&priv->mib_lock);
320 chan = priv->chan;
321 page = priv->page;
322 spin_unlock_bh(&priv->mib_lock);
323
324 if (chan == MAC802154_CHAN_NONE ||
325 page >= WPAN_NUM_PAGES ||
fcefbe9f
AO
326 chan >= WPAN_NUM_CHANNELS) {
327 kfree_skb(skb);
32bad7e3 328 return NETDEV_TX_OK;
fcefbe9f 329 }
32bad7e3 330
f30be4d5
PB
331 rc = mac802154_llsec_encrypt(&priv->sec, skb);
332 if (rc) {
333 pr_warn("encryption failed: %i\n", rc);
334 kfree_skb(skb);
335 return NETDEV_TX_OK;
336 }
337
32bad7e3 338 skb->skb_iif = dev->ifindex;
339 dev->stats.tx_packets++;
340 dev->stats.tx_bytes += skb->len;
341
342 return mac802154_tx(priv->hw, skb, page, chan);
343}
344
345static struct header_ops mac802154_header_ops = {
346 .create = mac802154_header_create,
347 .parse = mac802154_header_parse,
348};
349
350static const struct net_device_ops mac802154_wpan_ops = {
e462ded6 351 .ndo_open = mac802154_wpan_open,
32bad7e3 352 .ndo_stop = mac802154_slave_close,
353 .ndo_start_xmit = mac802154_wpan_xmit,
354 .ndo_do_ioctl = mac802154_wpan_ioctl,
355 .ndo_set_mac_address = mac802154_wpan_mac_addr,
356};
357
f30be4d5
PB
358static void mac802154_wpan_free(struct net_device *dev)
359{
360 struct mac802154_sub_if_data *priv = netdev_priv(dev);
361
362 mac802154_llsec_destroy(&priv->sec);
363
364 free_netdev(dev);
365}
366
32bad7e3 367void mac802154_wpan_setup(struct net_device *dev)
368{
369 struct mac802154_sub_if_data *priv;
370
371 dev->addr_len = IEEE802154_ADDR_LEN;
372 memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
373
374 dev->hard_header_len = MAC802154_FRAME_HARD_HEADER_LEN;
375 dev->header_ops = &mac802154_header_ops;
f30be4d5 376 dev->needed_tailroom = 2 + 16; /* FCS + MIC */
32bad7e3 377 dev->mtu = IEEE802154_MTU;
e937f583 378 dev->tx_queue_len = 300;
32bad7e3 379 dev->type = ARPHRD_IEEE802154;
380 dev->flags = IFF_NOARP | IFF_BROADCAST;
381 dev->watchdog_timeo = 0;
382
f30be4d5 383 dev->destructor = mac802154_wpan_free;
32bad7e3 384 dev->netdev_ops = &mac802154_wpan_ops;
385 dev->ml_priv = &mac802154_mlme_wpan;
386
387 priv = netdev_priv(dev);
388 priv->type = IEEE802154_DEV_WPAN;
389
390 priv->chan = MAC802154_CHAN_NONE;
391 priv->page = 0;
392
393 spin_lock_init(&priv->mib_lock);
f30be4d5 394 mutex_init(&priv->sec_mtx);
32bad7e3 395
396 get_random_bytes(&priv->bsn, 1);
397 get_random_bytes(&priv->dsn, 1);
398
e462ded6
PB
399 /* defaults per 802.15.4-2011 */
400 priv->mac_params.min_be = 3;
401 priv->mac_params.max_be = 5;
402 priv->mac_params.csma_retries = 4;
403 priv->mac_params.frame_retries = -1; /* for compatibility, actual default is 3 */
404
b70ab2e8
PB
405 priv->pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST);
406 priv->short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST);
f30be4d5
PB
407
408 mac802154_llsec_init(&priv->sec);
32bad7e3 409}
410
411static int mac802154_process_data(struct net_device *dev, struct sk_buff *skb)
412{
5ff3fec6 413 return netif_rx_ni(skb);
32bad7e3 414}
415
416static int
f30be4d5
PB
417mac802154_subif_frame(struct mac802154_sub_if_data *sdata, struct sk_buff *skb,
418 const struct ieee802154_hdr *hdr)
32bad7e3 419{
ae531b94 420 __le16 span, sshort;
f30be4d5 421 int rc;
b70ab2e8 422
32bad7e3 423 pr_debug("getting packet via slave interface %s\n", sdata->dev->name);
424
425 spin_lock_bh(&sdata->mib_lock);
426
ae531b94
PB
427 span = sdata->pan_id;
428 sshort = sdata->short_addr;
b70ab2e8 429
ae531b94 430 switch (mac_cb(skb)->dest.mode) {
32bad7e3 431 case IEEE802154_ADDR_NONE:
ae531b94 432 if (mac_cb(skb)->dest.mode != IEEE802154_ADDR_NONE)
32bad7e3 433 /* FIXME: check if we are PAN coordinator */
434 skb->pkt_type = PACKET_OTHERHOST;
435 else
436 /* ACK comes with both addresses empty */
437 skb->pkt_type = PACKET_HOST;
438 break;
439 case IEEE802154_ADDR_LONG:
ae531b94
PB
440 if (mac_cb(skb)->dest.pan_id != span &&
441 mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST))
32bad7e3 442 skb->pkt_type = PACKET_OTHERHOST;
ae531b94 443 else if (mac_cb(skb)->dest.extended_addr == sdata->extended_addr)
32bad7e3 444 skb->pkt_type = PACKET_HOST;
445 else
446 skb->pkt_type = PACKET_OTHERHOST;
447 break;
448 case IEEE802154_ADDR_SHORT:
ae531b94
PB
449 if (mac_cb(skb)->dest.pan_id != span &&
450 mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST))
32bad7e3 451 skb->pkt_type = PACKET_OTHERHOST;
ae531b94 452 else if (mac_cb(skb)->dest.short_addr == sshort)
32bad7e3 453 skb->pkt_type = PACKET_HOST;
ae531b94
PB
454 else if (mac_cb(skb)->dest.short_addr ==
455 cpu_to_le16(IEEE802154_ADDR_BROADCAST))
32bad7e3 456 skb->pkt_type = PACKET_BROADCAST;
457 else
458 skb->pkt_type = PACKET_OTHERHOST;
459 break;
460 default:
6e361d6f
MT
461 spin_unlock_bh(&sdata->mib_lock);
462 pr_debug("invalid dest mode\n");
463 kfree_skb(skb);
464 return NET_RX_DROP;
32bad7e3 465 }
466
467 spin_unlock_bh(&sdata->mib_lock);
468
469 skb->dev = sdata->dev;
470
f30be4d5
PB
471 rc = mac802154_llsec_decrypt(&sdata->sec, skb);
472 if (rc) {
473 pr_debug("decryption failed: %i\n", rc);
b288a496 474 goto fail;
f30be4d5
PB
475 }
476
32bad7e3 477 sdata->dev->stats.rx_packets++;
478 sdata->dev->stats.rx_bytes += skb->len;
479
32edc40a 480 switch (mac_cb(skb)->type) {
32bad7e3 481 case IEEE802154_FC_TYPE_DATA:
482 return mac802154_process_data(sdata->dev, skb);
483 default:
2cc33c7e 484 pr_warn("ieee802154: bad frame received (type = %d)\n",
32edc40a 485 mac_cb(skb)->type);
b288a496 486 goto fail;
32bad7e3 487 }
b288a496
VB
488
489fail:
490 kfree_skb(skb);
491 return NET_RX_DROP;
32bad7e3 492}
493
e6278d92
PB
494static void mac802154_print_addr(const char *name,
495 const struct ieee802154_addr *addr)
32bad7e3 496{
e6278d92
PB
497 if (addr->mode == IEEE802154_ADDR_NONE)
498 pr_debug("%s not present\n", name);
32bad7e3 499
e6278d92
PB
500 pr_debug("%s PAN ID: %04x\n", name, le16_to_cpu(addr->pan_id));
501 if (addr->mode == IEEE802154_ADDR_SHORT) {
502 pr_debug("%s is short: %04x\n", name,
503 le16_to_cpu(addr->short_addr));
504 } else {
505 u64 hw = swab64((__force u64) addr->extended_addr);
32bad7e3 506
e6278d92
PB
507 pr_debug("%s is hardware: %8phC\n", name, &hw);
508 }
509}
32bad7e3 510
f30be4d5
PB
511static int mac802154_parse_frame_start(struct sk_buff *skb,
512 struct ieee802154_hdr *hdr)
e6278d92 513{
e6278d92 514 int hlen;
32edc40a 515 struct ieee802154_mac_cb *cb = mac_cb_init(skb);
32bad7e3 516
f30be4d5 517 hlen = ieee802154_hdr_pull(skb, hdr);
e6278d92
PB
518 if (hlen < 0)
519 return -EINVAL;
32bad7e3 520
e6278d92 521 skb->mac_len = hlen;
32bad7e3 522
f30be4d5
PB
523 pr_debug("fc: %04x dsn: %02x\n", le16_to_cpup((__le16 *)&hdr->fc),
524 hdr->seq);
32bad7e3 525
f30be4d5
PB
526 cb->type = hdr->fc.type;
527 cb->ackreq = hdr->fc.ack_request;
528 cb->secen = hdr->fc.security_enabled;
32bad7e3 529
f30be4d5
PB
530 mac802154_print_addr("destination", &hdr->dest);
531 mac802154_print_addr("source", &hdr->source);
32bad7e3 532
f30be4d5
PB
533 cb->source = hdr->source;
534 cb->dest = hdr->dest;
ae531b94 535
f30be4d5 536 if (hdr->fc.security_enabled) {
e6278d92 537 u64 key;
32bad7e3 538
f30be4d5 539 pr_debug("seclevel %i\n", hdr->sec.level);
32bad7e3 540
f30be4d5 541 switch (hdr->sec.key_id_mode) {
e6278d92
PB
542 case IEEE802154_SCF_KEY_IMPLICIT:
543 pr_debug("implicit key\n");
544 break;
32bad7e3 545
e6278d92 546 case IEEE802154_SCF_KEY_INDEX:
f30be4d5 547 pr_debug("key %02x\n", hdr->sec.key_id);
e6278d92 548 break;
32bad7e3 549
e6278d92
PB
550 case IEEE802154_SCF_KEY_SHORT_INDEX:
551 pr_debug("key %04x:%04x %02x\n",
f30be4d5
PB
552 le32_to_cpu(hdr->sec.short_src) >> 16,
553 le32_to_cpu(hdr->sec.short_src) & 0xffff,
554 hdr->sec.key_id);
e6278d92 555 break;
32bad7e3 556
e6278d92 557 case IEEE802154_SCF_KEY_HW_INDEX:
f30be4d5 558 key = swab64((__force u64) hdr->sec.extended_src);
e6278d92 559 pr_debug("key source %8phC %02x\n", &key,
f30be4d5 560 hdr->sec.key_id);
e6278d92 561 break;
32bad7e3 562 }
563 }
564
565 return 0;
32bad7e3 566}
567
568void mac802154_wpans_rx(struct mac802154_priv *priv, struct sk_buff *skb)
569{
570 int ret;
32bad7e3 571 struct mac802154_sub_if_data *sdata;
f30be4d5 572 struct ieee802154_hdr hdr;
32bad7e3 573
f30be4d5 574 ret = mac802154_parse_frame_start(skb, &hdr);
32bad7e3 575 if (ret) {
576 pr_debug("got invalid frame\n");
7629d1ea 577 kfree_skb(skb);
32bad7e3 578 return;
579 }
580
581 rcu_read_lock();
582 list_for_each_entry_rcu(sdata, &priv->slaves, list) {
2d3b5b0a
PB
583 if (sdata->type != IEEE802154_DEV_WPAN ||
584 !netif_running(sdata->dev))
32bad7e3 585 continue;
586
2d3b5b0a
PB
587 mac802154_subif_frame(sdata, skb, &hdr);
588 skb = NULL;
589 break;
32bad7e3 590 }
591 rcu_read_unlock();
2d3b5b0a
PB
592
593 if (skb)
594 kfree_skb(skb);
32bad7e3 595}