mac802154: add llsec decryption method
[linux-2.6-block.git] / net / mac802154 / wpan.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 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 *
17 * Written by:
18 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
19 * Sergey Lapin <slapin@ossfans.org>
20 * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
21 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
22 */
23
24#include <linux/netdevice.h>
25#include <linux/module.h>
26#include <linux/if_arp.h>
27
28#include <net/rtnetlink.h>
29#include <linux/nl802154.h>
30#include <net/af_ieee802154.h>
31#include <net/mac802154.h>
32#include <net/ieee802154_netdev.h>
33#include <net/ieee802154.h>
34#include <net/wpan-phy.h>
35
36#include "mac802154.h"
37
32bad7e3 38static int
39mac802154_wpan_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
40{
41 struct mac802154_sub_if_data *priv = netdev_priv(dev);
42 struct sockaddr_ieee802154 *sa =
43 (struct sockaddr_ieee802154 *)&ifr->ifr_addr;
44 int err = -ENOIOCTLCMD;
45
46 spin_lock_bh(&priv->mib_lock);
47
48 switch (cmd) {
49 case SIOCGIFADDR:
b70ab2e8
PB
50 {
51 u16 pan_id, short_addr;
52
53 pan_id = le16_to_cpu(priv->pan_id);
54 short_addr = le16_to_cpu(priv->short_addr);
55 if (pan_id == IEEE802154_PANID_BROADCAST ||
56 short_addr == IEEE802154_ADDR_BROADCAST) {
32bad7e3 57 err = -EADDRNOTAVAIL;
58 break;
59 }
60
61 sa->family = AF_IEEE802154;
62 sa->addr.addr_type = IEEE802154_ADDR_SHORT;
b70ab2e8
PB
63 sa->addr.pan_id = pan_id;
64 sa->addr.short_addr = short_addr;
32bad7e3 65
66 err = 0;
67 break;
b70ab2e8 68 }
32bad7e3 69 case SIOCSIFADDR:
70 dev_warn(&dev->dev,
71 "Using DEBUGing ioctl SIOCSIFADDR isn't recommened!\n");
72 if (sa->family != AF_IEEE802154 ||
73 sa->addr.addr_type != IEEE802154_ADDR_SHORT ||
74 sa->addr.pan_id == IEEE802154_PANID_BROADCAST ||
75 sa->addr.short_addr == IEEE802154_ADDR_BROADCAST ||
76 sa->addr.short_addr == IEEE802154_ADDR_UNDEF) {
77 err = -EINVAL;
78 break;
79 }
80
b70ab2e8
PB
81 priv->pan_id = cpu_to_le16(sa->addr.pan_id);
82 priv->short_addr = cpu_to_le16(sa->addr.short_addr);
32bad7e3 83
84 err = 0;
85 break;
86 }
87
88 spin_unlock_bh(&priv->mib_lock);
89 return err;
90}
91
92static int mac802154_wpan_mac_addr(struct net_device *dev, void *p)
93{
94 struct sockaddr *addr = p;
95
96 if (netif_running(dev))
97 return -EBUSY;
98
99 /* FIXME: validate addr */
100 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
101 mac802154_dev_set_ieee_addr(dev);
102 return 0;
103}
104
e462ded6
PB
105int mac802154_set_mac_params(struct net_device *dev,
106 const struct ieee802154_mac_params *params)
107{
108 struct mac802154_sub_if_data *priv = netdev_priv(dev);
109
110 mutex_lock(&priv->hw->slaves_mtx);
111 priv->mac_params = *params;
112 mutex_unlock(&priv->hw->slaves_mtx);
113
114 return 0;
115}
116
117void mac802154_get_mac_params(struct net_device *dev,
118 struct ieee802154_mac_params *params)
119{
120 struct mac802154_sub_if_data *priv = netdev_priv(dev);
121
122 mutex_lock(&priv->hw->slaves_mtx);
123 *params = priv->mac_params;
124 mutex_unlock(&priv->hw->slaves_mtx);
125}
126
6ef0023a 127static int mac802154_wpan_open(struct net_device *dev)
e462ded6
PB
128{
129 int rc;
130 struct mac802154_sub_if_data *priv = netdev_priv(dev);
131 struct wpan_phy *phy = priv->hw->phy;
132
133 rc = mac802154_slave_open(dev);
134 if (rc < 0)
135 return rc;
136
137 mutex_lock(&phy->pib_lock);
138
139 if (phy->set_txpower) {
140 rc = phy->set_txpower(phy, priv->mac_params.transmit_power);
141 if (rc < 0)
142 goto out;
143 }
144
145 if (phy->set_lbt) {
146 rc = phy->set_lbt(phy, priv->mac_params.lbt);
147 if (rc < 0)
148 goto out;
149 }
150
151 if (phy->set_cca_mode) {
152 rc = phy->set_cca_mode(phy, priv->mac_params.cca_mode);
153 if (rc < 0)
154 goto out;
155 }
156
157 if (phy->set_cca_ed_level) {
158 rc = phy->set_cca_ed_level(phy, priv->mac_params.cca_ed_level);
159 if (rc < 0)
160 goto out;
161 }
162
163 if (phy->set_csma_params) {
164 rc = phy->set_csma_params(phy, priv->mac_params.min_be,
165 priv->mac_params.max_be,
166 priv->mac_params.csma_retries);
167 if (rc < 0)
168 goto out;
169 }
170
171 if (phy->set_frame_retries) {
172 rc = phy->set_frame_retries(phy,
173 priv->mac_params.frame_retries);
174 if (rc < 0)
175 goto out;
176 }
177
178 mutex_unlock(&phy->pib_lock);
179 return 0;
180
181out:
182 mutex_unlock(&phy->pib_lock);
183 return rc;
184}
185
32bad7e3 186static int mac802154_header_create(struct sk_buff *skb,
187 struct net_device *dev,
188 unsigned short type,
e6278d92
PB
189 const void *daddr,
190 const void *saddr,
32bad7e3 191 unsigned len)
192{
e6278d92 193 struct ieee802154_hdr hdr;
32bad7e3 194 struct mac802154_sub_if_data *priv = netdev_priv(dev);
32edc40a 195 struct ieee802154_mac_cb *cb = mac_cb(skb);
e6278d92 196 int hlen;
32bad7e3 197
198 if (!daddr)
199 return -EINVAL;
200
e6278d92 201 memset(&hdr.fc, 0, sizeof(hdr.fc));
32edc40a
PB
202 hdr.fc.type = cb->type;
203 hdr.fc.security_enabled = cb->secen;
204 hdr.fc.ack_request = cb->ackreq;
205 hdr.seq = ieee802154_mlme_ops(dev)->get_dsn(dev);
32bad7e3 206
207 if (!saddr) {
208 spin_lock_bh(&priv->mib_lock);
209
b70ab2e8
PB
210 if (priv->short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST) ||
211 priv->short_addr == cpu_to_le16(IEEE802154_ADDR_UNDEF) ||
212 priv->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST)) {
e6278d92
PB
213 hdr.source.mode = IEEE802154_ADDR_LONG;
214 hdr.source.extended_addr = priv->extended_addr;
32bad7e3 215 } else {
e6278d92
PB
216 hdr.source.mode = IEEE802154_ADDR_SHORT;
217 hdr.source.short_addr = priv->short_addr;
32bad7e3 218 }
219
e6278d92 220 hdr.source.pan_id = priv->pan_id;
32bad7e3 221
222 spin_unlock_bh(&priv->mib_lock);
e6278d92
PB
223 } else {
224 hdr.source = *(const struct ieee802154_addr *)saddr;
32bad7e3 225 }
226
e6278d92 227 hdr.dest = *(const struct ieee802154_addr *)daddr;
32bad7e3 228
e6278d92
PB
229 hlen = ieee802154_hdr_push(skb, &hdr);
230 if (hlen < 0)
231 return -EINVAL;
32bad7e3 232
3e69162e 233 skb_reset_mac_header(skb);
e6278d92 234 skb->mac_len = hlen;
32bad7e3 235
8c84296f 236 if (len > ieee802154_max_payload(&hdr))
d1d7358e
PB
237 return -EMSGSIZE;
238
e6278d92 239 return hlen;
32bad7e3 240}
241
242static int
243mac802154_header_parse(const struct sk_buff *skb, unsigned char *haddr)
244{
e6278d92
PB
245 struct ieee802154_hdr hdr;
246 struct ieee802154_addr *addr = (struct ieee802154_addr *)haddr;
32bad7e3 247
e6278d92
PB
248 if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) {
249 pr_debug("malformed packet\n");
250 return 0;
32bad7e3 251 }
252
e6278d92
PB
253 *addr = hdr.source;
254 return sizeof(*addr);
32bad7e3 255}
256
257static netdev_tx_t
258mac802154_wpan_xmit(struct sk_buff *skb, struct net_device *dev)
259{
260 struct mac802154_sub_if_data *priv;
261 u8 chan, page;
262
263 priv = netdev_priv(dev);
264
265 spin_lock_bh(&priv->mib_lock);
266 chan = priv->chan;
267 page = priv->page;
268 spin_unlock_bh(&priv->mib_lock);
269
270 if (chan == MAC802154_CHAN_NONE ||
271 page >= WPAN_NUM_PAGES ||
fcefbe9f
AO
272 chan >= WPAN_NUM_CHANNELS) {
273 kfree_skb(skb);
32bad7e3 274 return NETDEV_TX_OK;
fcefbe9f 275 }
32bad7e3 276
277 skb->skb_iif = dev->ifindex;
278 dev->stats.tx_packets++;
279 dev->stats.tx_bytes += skb->len;
280
281 return mac802154_tx(priv->hw, skb, page, chan);
282}
283
284static struct header_ops mac802154_header_ops = {
285 .create = mac802154_header_create,
286 .parse = mac802154_header_parse,
287};
288
289static const struct net_device_ops mac802154_wpan_ops = {
e462ded6 290 .ndo_open = mac802154_wpan_open,
32bad7e3 291 .ndo_stop = mac802154_slave_close,
292 .ndo_start_xmit = mac802154_wpan_xmit,
293 .ndo_do_ioctl = mac802154_wpan_ioctl,
294 .ndo_set_mac_address = mac802154_wpan_mac_addr,
295};
296
297void mac802154_wpan_setup(struct net_device *dev)
298{
299 struct mac802154_sub_if_data *priv;
300
301 dev->addr_len = IEEE802154_ADDR_LEN;
302 memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
303
304 dev->hard_header_len = MAC802154_FRAME_HARD_HEADER_LEN;
305 dev->header_ops = &mac802154_header_ops;
306 dev->needed_tailroom = 2; /* FCS */
307 dev->mtu = IEEE802154_MTU;
e937f583 308 dev->tx_queue_len = 300;
32bad7e3 309 dev->type = ARPHRD_IEEE802154;
310 dev->flags = IFF_NOARP | IFF_BROADCAST;
311 dev->watchdog_timeo = 0;
312
313 dev->destructor = free_netdev;
314 dev->netdev_ops = &mac802154_wpan_ops;
315 dev->ml_priv = &mac802154_mlme_wpan;
316
317 priv = netdev_priv(dev);
318 priv->type = IEEE802154_DEV_WPAN;
319
320 priv->chan = MAC802154_CHAN_NONE;
321 priv->page = 0;
322
323 spin_lock_init(&priv->mib_lock);
324
325 get_random_bytes(&priv->bsn, 1);
326 get_random_bytes(&priv->dsn, 1);
327
e462ded6
PB
328 /* defaults per 802.15.4-2011 */
329 priv->mac_params.min_be = 3;
330 priv->mac_params.max_be = 5;
331 priv->mac_params.csma_retries = 4;
332 priv->mac_params.frame_retries = -1; /* for compatibility, actual default is 3 */
333
b70ab2e8
PB
334 priv->pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST);
335 priv->short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST);
32bad7e3 336}
337
338static int mac802154_process_data(struct net_device *dev, struct sk_buff *skb)
339{
5ff3fec6 340 return netif_rx_ni(skb);
32bad7e3 341}
342
343static int
344mac802154_subif_frame(struct mac802154_sub_if_data *sdata, struct sk_buff *skb)
345{
ae531b94 346 __le16 span, sshort;
b70ab2e8 347
32bad7e3 348 pr_debug("getting packet via slave interface %s\n", sdata->dev->name);
349
350 spin_lock_bh(&sdata->mib_lock);
351
ae531b94
PB
352 span = sdata->pan_id;
353 sshort = sdata->short_addr;
b70ab2e8 354
ae531b94 355 switch (mac_cb(skb)->dest.mode) {
32bad7e3 356 case IEEE802154_ADDR_NONE:
ae531b94 357 if (mac_cb(skb)->dest.mode != IEEE802154_ADDR_NONE)
32bad7e3 358 /* FIXME: check if we are PAN coordinator */
359 skb->pkt_type = PACKET_OTHERHOST;
360 else
361 /* ACK comes with both addresses empty */
362 skb->pkt_type = PACKET_HOST;
363 break;
364 case IEEE802154_ADDR_LONG:
ae531b94
PB
365 if (mac_cb(skb)->dest.pan_id != span &&
366 mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST))
32bad7e3 367 skb->pkt_type = PACKET_OTHERHOST;
ae531b94 368 else if (mac_cb(skb)->dest.extended_addr == sdata->extended_addr)
32bad7e3 369 skb->pkt_type = PACKET_HOST;
370 else
371 skb->pkt_type = PACKET_OTHERHOST;
372 break;
373 case IEEE802154_ADDR_SHORT:
ae531b94
PB
374 if (mac_cb(skb)->dest.pan_id != span &&
375 mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST))
32bad7e3 376 skb->pkt_type = PACKET_OTHERHOST;
ae531b94 377 else if (mac_cb(skb)->dest.short_addr == sshort)
32bad7e3 378 skb->pkt_type = PACKET_HOST;
ae531b94
PB
379 else if (mac_cb(skb)->dest.short_addr ==
380 cpu_to_le16(IEEE802154_ADDR_BROADCAST))
32bad7e3 381 skb->pkt_type = PACKET_BROADCAST;
382 else
383 skb->pkt_type = PACKET_OTHERHOST;
384 break;
385 default:
386 break;
387 }
388
389 spin_unlock_bh(&sdata->mib_lock);
390
391 skb->dev = sdata->dev;
392
393 sdata->dev->stats.rx_packets++;
394 sdata->dev->stats.rx_bytes += skb->len;
395
32edc40a 396 switch (mac_cb(skb)->type) {
32bad7e3 397 case IEEE802154_FC_TYPE_DATA:
398 return mac802154_process_data(sdata->dev, skb);
399 default:
2cc33c7e 400 pr_warn("ieee802154: bad frame received (type = %d)\n",
32edc40a 401 mac_cb(skb)->type);
32bad7e3 402 kfree_skb(skb);
403 return NET_RX_DROP;
404 }
405}
406
e6278d92
PB
407static void mac802154_print_addr(const char *name,
408 const struct ieee802154_addr *addr)
32bad7e3 409{
e6278d92
PB
410 if (addr->mode == IEEE802154_ADDR_NONE)
411 pr_debug("%s not present\n", name);
32bad7e3 412
e6278d92
PB
413 pr_debug("%s PAN ID: %04x\n", name, le16_to_cpu(addr->pan_id));
414 if (addr->mode == IEEE802154_ADDR_SHORT) {
415 pr_debug("%s is short: %04x\n", name,
416 le16_to_cpu(addr->short_addr));
417 } else {
418 u64 hw = swab64((__force u64) addr->extended_addr);
32bad7e3 419
e6278d92
PB
420 pr_debug("%s is hardware: %8phC\n", name, &hw);
421 }
422}
32bad7e3 423
e6278d92
PB
424static int mac802154_parse_frame_start(struct sk_buff *skb)
425{
e6278d92 426 int hlen;
ae531b94 427 struct ieee802154_hdr hdr;
32edc40a 428 struct ieee802154_mac_cb *cb = mac_cb_init(skb);
32bad7e3 429
e6278d92
PB
430 hlen = ieee802154_hdr_pull(skb, &hdr);
431 if (hlen < 0)
432 return -EINVAL;
32bad7e3 433
e6278d92 434 skb->mac_len = hlen;
32bad7e3 435
e6278d92
PB
436 pr_debug("fc: %04x dsn: %02x\n", le16_to_cpup((__le16 *)&hdr.fc),
437 hdr.seq);
32bad7e3 438
32edc40a
PB
439 cb->type = hdr.fc.type;
440 cb->ackreq = hdr.fc.ack_request;
441 cb->secen = hdr.fc.security_enabled;
32bad7e3 442
e6278d92
PB
443 mac802154_print_addr("destination", &hdr.dest);
444 mac802154_print_addr("source", &hdr.source);
32bad7e3 445
32edc40a
PB
446 cb->source = hdr.source;
447 cb->dest = hdr.dest;
ae531b94 448
e6278d92
PB
449 if (hdr.fc.security_enabled) {
450 u64 key;
32bad7e3 451
e6278d92 452 pr_debug("seclevel %i\n", hdr.sec.level);
32bad7e3 453
e6278d92
PB
454 switch (hdr.sec.key_id_mode) {
455 case IEEE802154_SCF_KEY_IMPLICIT:
456 pr_debug("implicit key\n");
457 break;
32bad7e3 458
e6278d92
PB
459 case IEEE802154_SCF_KEY_INDEX:
460 pr_debug("key %02x\n", hdr.sec.key_id);
461 break;
32bad7e3 462
e6278d92
PB
463 case IEEE802154_SCF_KEY_SHORT_INDEX:
464 pr_debug("key %04x:%04x %02x\n",
465 le32_to_cpu(hdr.sec.short_src) >> 16,
466 le32_to_cpu(hdr.sec.short_src) & 0xffff,
467 hdr.sec.key_id);
468 break;
32bad7e3 469
e6278d92
PB
470 case IEEE802154_SCF_KEY_HW_INDEX:
471 key = swab64((__force u64) hdr.sec.extended_src);
472 pr_debug("key source %8phC %02x\n", &key,
473 hdr.sec.key_id);
474 break;
32bad7e3 475 }
e6278d92
PB
476
477 return -EINVAL;
32bad7e3 478 }
479
480 return 0;
32bad7e3 481}
482
483void mac802154_wpans_rx(struct mac802154_priv *priv, struct sk_buff *skb)
484{
485 int ret;
486 struct sk_buff *sskb;
487 struct mac802154_sub_if_data *sdata;
488
489 ret = mac802154_parse_frame_start(skb);
490 if (ret) {
491 pr_debug("got invalid frame\n");
492 return;
493 }
494
495 rcu_read_lock();
496 list_for_each_entry_rcu(sdata, &priv->slaves, list) {
497 if (sdata->type != IEEE802154_DEV_WPAN)
498 continue;
499
500 sskb = skb_clone(skb, GFP_ATOMIC);
501 if (sskb)
502 mac802154_subif_frame(sdata, sskb);
503 }
504 rcu_read_unlock();
505}