5adcbd87a4f595d46894fc6c13c04a225a79bec3
[deliverable/linux.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
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
32 #include "ieee802154_i.h"
33
34 static 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
56 static int
57 mac802154_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:
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) {
75 err = -EADDRNOTAVAIL;
76 break;
77 }
78
79 sa->family = AF_IEEE802154;
80 sa->addr.addr_type = IEEE802154_ADDR_SHORT;
81 sa->addr.pan_id = pan_id;
82 sa->addr.short_addr = short_addr;
83
84 err = 0;
85 break;
86 }
87 case SIOCSIFADDR:
88 dev_warn(&dev->dev,
89 "Using DEBUGing ioctl SIOCSIFADDR isn't recommended!\n");
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
99 priv->pan_id = cpu_to_le16(sa->addr.pan_id);
100 priv->short_addr = cpu_to_le16(sa->addr.short_addr);
101
102 err = mac802154_wpan_update_llsec(dev);
103 break;
104 }
105
106 spin_unlock_bh(&priv->mib_lock);
107 return err;
108 }
109
110 static 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);
120 return mac802154_wpan_update_llsec(dev);
121 }
122
123 int 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
135 void 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
145 static int mac802154_wpan_open(struct net_device *dev)
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
199 out:
200 mutex_unlock(&phy->pib_lock);
201 return rc;
202 }
203
204 static 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
236 static int mac802154_header_create(struct sk_buff *skb,
237 struct net_device *dev,
238 unsigned short type,
239 const void *daddr,
240 const void *saddr,
241 unsigned len)
242 {
243 struct ieee802154_hdr hdr;
244 struct mac802154_sub_if_data *priv = netdev_priv(dev);
245 struct ieee802154_mac_cb *cb = mac_cb(skb);
246 int hlen;
247
248 if (!daddr)
249 return -EINVAL;
250
251 memset(&hdr.fc, 0, sizeof(hdr.fc));
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);
256
257 if (mac802154_set_header_security(priv, &hdr, cb) < 0)
258 return -EINVAL;
259
260 if (!saddr) {
261 spin_lock_bh(&priv->mib_lock);
262
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)) {
266 hdr.source.mode = IEEE802154_ADDR_LONG;
267 hdr.source.extended_addr = priv->extended_addr;
268 } else {
269 hdr.source.mode = IEEE802154_ADDR_SHORT;
270 hdr.source.short_addr = priv->short_addr;
271 }
272
273 hdr.source.pan_id = priv->pan_id;
274
275 spin_unlock_bh(&priv->mib_lock);
276 } else {
277 hdr.source = *(const struct ieee802154_addr *)saddr;
278 }
279
280 hdr.dest = *(const struct ieee802154_addr *)daddr;
281
282 hlen = ieee802154_hdr_push(skb, &hdr);
283 if (hlen < 0)
284 return -EINVAL;
285
286 skb_reset_mac_header(skb);
287 skb->mac_len = hlen;
288
289 if (len > ieee802154_max_payload(&hdr))
290 return -EMSGSIZE;
291
292 return hlen;
293 }
294
295 static int
296 mac802154_header_parse(const struct sk_buff *skb, unsigned char *haddr)
297 {
298 struct ieee802154_hdr hdr;
299 struct ieee802154_addr *addr = (struct ieee802154_addr *)haddr;
300
301 if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) {
302 pr_debug("malformed packet\n");
303 return 0;
304 }
305
306 *addr = hdr.source;
307 return sizeof(*addr);
308 }
309
310 static netdev_tx_t
311 mac802154_wpan_xmit(struct sk_buff *skb, struct net_device *dev)
312 {
313 struct mac802154_sub_if_data *priv;
314 u8 chan, page;
315 int rc;
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 ||
326 chan >= WPAN_NUM_CHANNELS) {
327 kfree_skb(skb);
328 return NETDEV_TX_OK;
329 }
330
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
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
345 static struct header_ops mac802154_header_ops = {
346 .create = mac802154_header_create,
347 .parse = mac802154_header_parse,
348 };
349
350 static const struct net_device_ops mac802154_wpan_ops = {
351 .ndo_open = mac802154_wpan_open,
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
358 static 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
367 void 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;
376 dev->needed_tailroom = 2 + 16; /* FCS + MIC */
377 dev->mtu = IEEE802154_MTU;
378 dev->tx_queue_len = 300;
379 dev->type = ARPHRD_IEEE802154;
380 dev->flags = IFF_NOARP | IFF_BROADCAST;
381 dev->watchdog_timeo = 0;
382
383 dev->destructor = mac802154_wpan_free;
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);
394 mutex_init(&priv->sec_mtx);
395
396 get_random_bytes(&priv->bsn, 1);
397 get_random_bytes(&priv->dsn, 1);
398
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
405 priv->pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST);
406 priv->short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST);
407
408 mac802154_llsec_init(&priv->sec);
409 }
410
411 static int mac802154_process_data(struct net_device *dev, struct sk_buff *skb)
412 {
413 return netif_rx_ni(skb);
414 }
415
416 static int
417 mac802154_subif_frame(struct mac802154_sub_if_data *sdata, struct sk_buff *skb,
418 const struct ieee802154_hdr *hdr)
419 {
420 __le16 span, sshort;
421 int rc;
422
423 pr_debug("getting packet via slave interface %s\n", sdata->dev->name);
424
425 spin_lock_bh(&sdata->mib_lock);
426
427 span = sdata->pan_id;
428 sshort = sdata->short_addr;
429
430 switch (mac_cb(skb)->dest.mode) {
431 case IEEE802154_ADDR_NONE:
432 if (mac_cb(skb)->dest.mode != IEEE802154_ADDR_NONE)
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:
440 if (mac_cb(skb)->dest.pan_id != span &&
441 mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST))
442 skb->pkt_type = PACKET_OTHERHOST;
443 else if (mac_cb(skb)->dest.extended_addr == sdata->extended_addr)
444 skb->pkt_type = PACKET_HOST;
445 else
446 skb->pkt_type = PACKET_OTHERHOST;
447 break;
448 case IEEE802154_ADDR_SHORT:
449 if (mac_cb(skb)->dest.pan_id != span &&
450 mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST))
451 skb->pkt_type = PACKET_OTHERHOST;
452 else if (mac_cb(skb)->dest.short_addr == sshort)
453 skb->pkt_type = PACKET_HOST;
454 else if (mac_cb(skb)->dest.short_addr ==
455 cpu_to_le16(IEEE802154_ADDR_BROADCAST))
456 skb->pkt_type = PACKET_BROADCAST;
457 else
458 skb->pkt_type = PACKET_OTHERHOST;
459 break;
460 default:
461 spin_unlock_bh(&sdata->mib_lock);
462 pr_debug("invalid dest mode\n");
463 kfree_skb(skb);
464 return NET_RX_DROP;
465 }
466
467 spin_unlock_bh(&sdata->mib_lock);
468
469 skb->dev = sdata->dev;
470
471 rc = mac802154_llsec_decrypt(&sdata->sec, skb);
472 if (rc) {
473 pr_debug("decryption failed: %i\n", rc);
474 goto fail;
475 }
476
477 sdata->dev->stats.rx_packets++;
478 sdata->dev->stats.rx_bytes += skb->len;
479
480 switch (mac_cb(skb)->type) {
481 case IEEE802154_FC_TYPE_DATA:
482 return mac802154_process_data(sdata->dev, skb);
483 default:
484 pr_warn("ieee802154: bad frame received (type = %d)\n",
485 mac_cb(skb)->type);
486 goto fail;
487 }
488
489 fail:
490 kfree_skb(skb);
491 return NET_RX_DROP;
492 }
493
494 static void mac802154_print_addr(const char *name,
495 const struct ieee802154_addr *addr)
496 {
497 if (addr->mode == IEEE802154_ADDR_NONE)
498 pr_debug("%s not present\n", name);
499
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);
506
507 pr_debug("%s is hardware: %8phC\n", name, &hw);
508 }
509 }
510
511 static int mac802154_parse_frame_start(struct sk_buff *skb,
512 struct ieee802154_hdr *hdr)
513 {
514 int hlen;
515 struct ieee802154_mac_cb *cb = mac_cb_init(skb);
516
517 hlen = ieee802154_hdr_pull(skb, hdr);
518 if (hlen < 0)
519 return -EINVAL;
520
521 skb->mac_len = hlen;
522
523 pr_debug("fc: %04x dsn: %02x\n", le16_to_cpup((__le16 *)&hdr->fc),
524 hdr->seq);
525
526 cb->type = hdr->fc.type;
527 cb->ackreq = hdr->fc.ack_request;
528 cb->secen = hdr->fc.security_enabled;
529
530 mac802154_print_addr("destination", &hdr->dest);
531 mac802154_print_addr("source", &hdr->source);
532
533 cb->source = hdr->source;
534 cb->dest = hdr->dest;
535
536 if (hdr->fc.security_enabled) {
537 u64 key;
538
539 pr_debug("seclevel %i\n", hdr->sec.level);
540
541 switch (hdr->sec.key_id_mode) {
542 case IEEE802154_SCF_KEY_IMPLICIT:
543 pr_debug("implicit key\n");
544 break;
545
546 case IEEE802154_SCF_KEY_INDEX:
547 pr_debug("key %02x\n", hdr->sec.key_id);
548 break;
549
550 case IEEE802154_SCF_KEY_SHORT_INDEX:
551 pr_debug("key %04x:%04x %02x\n",
552 le32_to_cpu(hdr->sec.short_src) >> 16,
553 le32_to_cpu(hdr->sec.short_src) & 0xffff,
554 hdr->sec.key_id);
555 break;
556
557 case IEEE802154_SCF_KEY_HW_INDEX:
558 key = swab64((__force u64) hdr->sec.extended_src);
559 pr_debug("key source %8phC %02x\n", &key,
560 hdr->sec.key_id);
561 break;
562 }
563 }
564
565 return 0;
566 }
567
568 void mac802154_wpans_rx(struct mac802154_priv *priv, struct sk_buff *skb)
569 {
570 int ret;
571 struct mac802154_sub_if_data *sdata;
572 struct ieee802154_hdr hdr;
573
574 ret = mac802154_parse_frame_start(skb, &hdr);
575 if (ret) {
576 pr_debug("got invalid frame\n");
577 kfree_skb(skb);
578 return;
579 }
580
581 rcu_read_lock();
582 list_for_each_entry_rcu(sdata, &priv->slaves, list) {
583 if (sdata->type != IEEE802154_DEV_WPAN ||
584 !netif_running(sdata->dev))
585 continue;
586
587 mac802154_subif_frame(sdata, skb, &hdr);
588 skb = NULL;
589 break;
590 }
591 rcu_read_unlock();
592
593 if (skb)
594 kfree_skb(skb);
595 }
This page took 0.05086 seconds and 4 git commands to generate.