iface.c 13.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright 2007-2012 Siemens AG
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Written by:
 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
 * Sergey Lapin <slapin@ossfans.org>
 * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
 */

#include <linux/netdevice.h>
#include <linux/module.h>
#include <linux/if_arp.h>
23
#include <linux/ieee802154.h>
24 25 26 27 28 29

#include <net/rtnetlink.h>
#include <linux/nl802154.h>
#include <net/af_ieee802154.h>
#include <net/mac802154.h>
#include <net/ieee802154_netdev.h>
30
#include <net/cfg802154.h>
31

32
#include "ieee802154_i.h"
33

34 35
static int mac802154_wpan_update_llsec(struct net_device *dev)
{
36
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
37 38 39 40 41 42 43
	struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
	int rc = 0;

	if (ops->llsec) {
		struct ieee802154_llsec_params params;
		int changed = 0;

44
		params.pan_id = sdata->pan_id;
45 46
		changed |= IEEE802154_LLSEC_PARAM_PAN_ID;

47
		params.hwaddr = sdata->extended_addr;
48 49 50 51 52 53 54 55
		changed |= IEEE802154_LLSEC_PARAM_HWADDR;

		rc = ops->llsec->set_params(dev, &params, changed);
	}

	return rc;
}

56 57 58
static int
mac802154_wpan_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
{
59
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
60 61 62 63
	struct sockaddr_ieee802154 *sa =
		(struct sockaddr_ieee802154 *)&ifr->ifr_addr;
	int err = -ENOIOCTLCMD;

64
	spin_lock_bh(&sdata->mib_lock);
65 66 67

	switch (cmd) {
	case SIOCGIFADDR:
68 69 70
	{
		u16 pan_id, short_addr;

71 72
		pan_id = le16_to_cpu(sdata->pan_id);
		short_addr = le16_to_cpu(sdata->short_addr);
73 74
		if (pan_id == IEEE802154_PANID_BROADCAST ||
		    short_addr == IEEE802154_ADDR_BROADCAST) {
75 76 77 78 79 80
			err = -EADDRNOTAVAIL;
			break;
		}

		sa->family = AF_IEEE802154;
		sa->addr.addr_type = IEEE802154_ADDR_SHORT;
81 82
		sa->addr.pan_id = pan_id;
		sa->addr.short_addr = short_addr;
83 84 85

		err = 0;
		break;
86
	}
87 88
	case SIOCSIFADDR:
		dev_warn(&dev->dev,
M
Masanari Iida 已提交
89
			 "Using DEBUGing ioctl SIOCSIFADDR isn't recommended!\n");
90 91 92 93 94 95 96 97 98
		if (sa->family != AF_IEEE802154 ||
		    sa->addr.addr_type != IEEE802154_ADDR_SHORT ||
		    sa->addr.pan_id == IEEE802154_PANID_BROADCAST ||
		    sa->addr.short_addr == IEEE802154_ADDR_BROADCAST ||
		    sa->addr.short_addr == IEEE802154_ADDR_UNDEF) {
			err = -EINVAL;
			break;
		}

99 100
		sdata->pan_id = cpu_to_le16(sa->addr.pan_id);
		sdata->short_addr = cpu_to_le16(sa->addr.short_addr);
101

102
		err = mac802154_wpan_update_llsec(dev);
103 104 105
		break;
	}

106
	spin_unlock_bh(&sdata->mib_lock);
107 108 109 110 111 112 113 114 115 116 117 118 119
	return err;
}

static int mac802154_wpan_mac_addr(struct net_device *dev, void *p)
{
	struct sockaddr *addr = p;

	if (netif_running(dev))
		return -EBUSY;

	/* FIXME: validate addr */
	memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
	mac802154_dev_set_ieee_addr(dev);
120
	return mac802154_wpan_update_llsec(dev);
121 122
}

123 124 125
int mac802154_set_mac_params(struct net_device *dev,
			     const struct ieee802154_mac_params *params)
{
126
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
127

128
	mutex_lock(&sdata->local->iflist_mtx);
129
	sdata->mac_params = *params;
130
	mutex_unlock(&sdata->local->iflist_mtx);
131 132 133 134 135 136 137

	return 0;
}

void mac802154_get_mac_params(struct net_device *dev,
			      struct ieee802154_mac_params *params)
{
138
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
139

140
	mutex_lock(&sdata->local->iflist_mtx);
141
	*params = sdata->mac_params;
142
	mutex_unlock(&sdata->local->iflist_mtx);
143 144
}

145
static int mac802154_wpan_open(struct net_device *dev)
146 147
{
	int rc;
148
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
149
	struct wpan_phy *phy = sdata->local->phy;
150 151 152 153 154 155 156 157

	rc = mac802154_slave_open(dev);
	if (rc < 0)
		return rc;

	mutex_lock(&phy->pib_lock);

	if (phy->set_txpower) {
158
		rc = phy->set_txpower(phy, sdata->mac_params.transmit_power);
159 160 161 162 163
		if (rc < 0)
			goto out;
	}

	if (phy->set_lbt) {
164
		rc = phy->set_lbt(phy, sdata->mac_params.lbt);
165 166 167 168 169
		if (rc < 0)
			goto out;
	}

	if (phy->set_cca_mode) {
170
		rc = phy->set_cca_mode(phy, sdata->mac_params.cca_mode);
171 172 173 174 175
		if (rc < 0)
			goto out;
	}

	if (phy->set_cca_ed_level) {
176
		rc = phy->set_cca_ed_level(phy, sdata->mac_params.cca_ed_level);
177 178 179 180 181
		if (rc < 0)
			goto out;
	}

	if (phy->set_csma_params) {
182 183 184
		rc = phy->set_csma_params(phy, sdata->mac_params.min_be,
					  sdata->mac_params.max_be,
					  sdata->mac_params.csma_retries);
185 186 187 188 189 190
		if (rc < 0)
			goto out;
	}

	if (phy->set_frame_retries) {
		rc = phy->set_frame_retries(phy,
191
					    sdata->mac_params.frame_retries);
192 193 194 195 196 197 198 199 200 201 202 203
		if (rc < 0)
			goto out;
	}

	mutex_unlock(&phy->pib_lock);
	return 0;

out:
	mutex_unlock(&phy->pib_lock);
	return rc;
}

204
static int mac802154_set_header_security(struct ieee802154_sub_if_data *sdata,
205 206 207 208 209 210
					 struct ieee802154_hdr *hdr,
					 const struct ieee802154_mac_cb *cb)
{
	struct ieee802154_llsec_params params;
	u8 level;

211
	mac802154_llsec_get_params(&sdata->sec, &params);
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235

	if (!params.enabled && cb->secen_override && cb->secen)
		return -EINVAL;
	if (!params.enabled ||
	    (cb->secen_override && !cb->secen) ||
	    !params.out_level)
		return 0;
	if (cb->seclevel_override && !cb->seclevel)
		return -EINVAL;

	level = cb->seclevel_override ? cb->seclevel : params.out_level;

	hdr->fc.security_enabled = 1;
	hdr->sec.level = level;
	hdr->sec.key_id_mode = params.out_key.mode;
	if (params.out_key.mode == IEEE802154_SCF_KEY_SHORT_INDEX)
		hdr->sec.short_src = params.out_key.short_source;
	else if (params.out_key.mode == IEEE802154_SCF_KEY_HW_INDEX)
		hdr->sec.extended_src = params.out_key.extended_source;
	hdr->sec.key_id = params.out_key.id;

	return 0;
}

236 237 238
static int mac802154_header_create(struct sk_buff *skb,
				   struct net_device *dev,
				   unsigned short type,
239 240
				   const void *daddr,
				   const void *saddr,
241 242
				   unsigned len)
{
243
	struct ieee802154_hdr hdr;
244
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
245
	struct ieee802154_mac_cb *cb = mac_cb(skb);
246
	int hlen;
247 248 249 250

	if (!daddr)
		return -EINVAL;

251
	memset(&hdr.fc, 0, sizeof(hdr.fc));
252 253 254 255
	hdr.fc.type = cb->type;
	hdr.fc.security_enabled = cb->secen;
	hdr.fc.ack_request = cb->ackreq;
	hdr.seq = ieee802154_mlme_ops(dev)->get_dsn(dev);
256

257
	if (mac802154_set_header_security(sdata, &hdr, cb) < 0)
258 259
		return -EINVAL;

260
	if (!saddr) {
261
		spin_lock_bh(&sdata->mib_lock);
262

263 264 265
		if (sdata->short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST) ||
		    sdata->short_addr == cpu_to_le16(IEEE802154_ADDR_UNDEF) ||
		    sdata->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST)) {
266
			hdr.source.mode = IEEE802154_ADDR_LONG;
267
			hdr.source.extended_addr = sdata->extended_addr;
268
		} else {
269
			hdr.source.mode = IEEE802154_ADDR_SHORT;
270
			hdr.source.short_addr = sdata->short_addr;
271 272
		}

273
		hdr.source.pan_id = sdata->pan_id;
274

275
		spin_unlock_bh(&sdata->mib_lock);
276 277
	} else {
		hdr.source = *(const struct ieee802154_addr *)saddr;
278 279
	}

280
	hdr.dest = *(const struct ieee802154_addr *)daddr;
281

282 283 284
	hlen = ieee802154_hdr_push(skb, &hdr);
	if (hlen < 0)
		return -EINVAL;
285

286
	skb_reset_mac_header(skb);
287
	skb->mac_len = hlen;
288

289
	if (len > ieee802154_max_payload(&hdr))
290 291
		return -EMSGSIZE;

292
	return hlen;
293 294 295 296 297
}

static int
mac802154_header_parse(const struct sk_buff *skb, unsigned char *haddr)
{
298 299
	struct ieee802154_hdr hdr;
	struct ieee802154_addr *addr = (struct ieee802154_addr *)haddr;
300

301 302 303
	if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) {
		pr_debug("malformed packet\n");
		return 0;
304 305
	}

306 307
	*addr = hdr.source;
	return sizeof(*addr);
308 309 310 311 312 313 314 315
}

static struct header_ops mac802154_header_ops = {
	.create		= mac802154_header_create,
	.parse		= mac802154_header_parse,
};

static const struct net_device_ops mac802154_wpan_ops = {
316
	.ndo_open		= mac802154_wpan_open,
317 318 319 320 321 322
	.ndo_stop		= mac802154_slave_close,
	.ndo_start_xmit		= mac802154_wpan_xmit,
	.ndo_do_ioctl		= mac802154_wpan_ioctl,
	.ndo_set_mac_address	= mac802154_wpan_mac_addr,
};

323 324
static void mac802154_wpan_free(struct net_device *dev)
{
325
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
326

327
	mac802154_llsec_destroy(&sdata->sec);
328 329 330 331

	free_netdev(dev);
}

332 333
void mac802154_wpan_setup(struct net_device *dev)
{
334
	struct ieee802154_sub_if_data *sdata;
335 336 337 338 339 340

	dev->addr_len		= IEEE802154_ADDR_LEN;
	memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);

	dev->hard_header_len	= MAC802154_FRAME_HARD_HEADER_LEN;
	dev->header_ops		= &mac802154_header_ops;
341
	dev->needed_tailroom	= 2 + 16; /* FCS + MIC */
342
	dev->mtu		= IEEE802154_MTU;
A
Alan Ott 已提交
343
	dev->tx_queue_len	= 300;
344 345 346 347
	dev->type		= ARPHRD_IEEE802154;
	dev->flags		= IFF_NOARP | IFF_BROADCAST;
	dev->watchdog_timeo	= 0;

348
	dev->destructor		= mac802154_wpan_free;
349 350 351
	dev->netdev_ops		= &mac802154_wpan_ops;
	dev->ml_priv		= &mac802154_mlme_wpan;

352
	sdata = IEEE802154_DEV_TO_SUB_IF(dev);
353
	sdata->type = IEEE802154_DEV_WPAN;
354

355 356
	sdata->chan = MAC802154_CHAN_NONE;
	sdata->page = 0;
357

358 359
	spin_lock_init(&sdata->mib_lock);
	mutex_init(&sdata->sec_mtx);
360

361 362
	get_random_bytes(&sdata->bsn, 1);
	get_random_bytes(&sdata->dsn, 1);
363

364
	/* defaults per 802.15.4-2011 */
365 366 367 368 369
	sdata->mac_params.min_be = 3;
	sdata->mac_params.max_be = 5;
	sdata->mac_params.csma_retries = 4;
	/* for compatibility, actual default is 3 */
	sdata->mac_params.frame_retries = -1;
370

371 372
	sdata->pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST);
	sdata->short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST);
373

374
	mac802154_llsec_init(&sdata->sec);
375 376 377 378
}

static int mac802154_process_data(struct net_device *dev, struct sk_buff *skb)
{
379
	return netif_rx_ni(skb);
380 381 382
}

static int
383
mac802154_subif_frame(struct ieee802154_sub_if_data *sdata, struct sk_buff *skb,
384
		      const struct ieee802154_hdr *hdr)
385
{
386
	__le16 span, sshort;
387
	int rc;
388

389 390 391 392
	pr_debug("getting packet via slave interface %s\n", sdata->dev->name);

	spin_lock_bh(&sdata->mib_lock);

393 394
	span = sdata->pan_id;
	sshort = sdata->short_addr;
395

396
	switch (mac_cb(skb)->dest.mode) {
397
	case IEEE802154_ADDR_NONE:
398
		if (mac_cb(skb)->dest.mode != IEEE802154_ADDR_NONE)
399 400 401 402 403 404 405
			/* FIXME: check if we are PAN coordinator */
			skb->pkt_type = PACKET_OTHERHOST;
		else
			/* ACK comes with both addresses empty */
			skb->pkt_type = PACKET_HOST;
		break;
	case IEEE802154_ADDR_LONG:
406 407
		if (mac_cb(skb)->dest.pan_id != span &&
		    mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST))
408
			skb->pkt_type = PACKET_OTHERHOST;
409
		else if (mac_cb(skb)->dest.extended_addr == sdata->extended_addr)
410 411 412 413 414
			skb->pkt_type = PACKET_HOST;
		else
			skb->pkt_type = PACKET_OTHERHOST;
		break;
	case IEEE802154_ADDR_SHORT:
415 416
		if (mac_cb(skb)->dest.pan_id != span &&
		    mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST))
417
			skb->pkt_type = PACKET_OTHERHOST;
418
		else if (mac_cb(skb)->dest.short_addr == sshort)
419
			skb->pkt_type = PACKET_HOST;
420 421
		else if (mac_cb(skb)->dest.short_addr ==
			  cpu_to_le16(IEEE802154_ADDR_BROADCAST))
422 423 424 425 426
			skb->pkt_type = PACKET_BROADCAST;
		else
			skb->pkt_type = PACKET_OTHERHOST;
		break;
	default:
427 428 429 430
		spin_unlock_bh(&sdata->mib_lock);
		pr_debug("invalid dest mode\n");
		kfree_skb(skb);
		return NET_RX_DROP;
431 432 433 434 435 436
	}

	spin_unlock_bh(&sdata->mib_lock);

	skb->dev = sdata->dev;

437 438 439
	rc = mac802154_llsec_decrypt(&sdata->sec, skb);
	if (rc) {
		pr_debug("decryption failed: %i\n", rc);
V
Varka Bhadram 已提交
440
		goto fail;
441 442
	}

443 444 445
	sdata->dev->stats.rx_packets++;
	sdata->dev->stats.rx_bytes += skb->len;

446
	switch (mac_cb(skb)->type) {
447 448 449
	case IEEE802154_FC_TYPE_DATA:
		return mac802154_process_data(sdata->dev, skb);
	default:
450
		pr_warn("ieee802154: bad frame received (type = %d)\n",
451
			mac_cb(skb)->type);
V
Varka Bhadram 已提交
452
		goto fail;
453
	}
V
Varka Bhadram 已提交
454 455 456 457

fail:
	kfree_skb(skb);
	return NET_RX_DROP;
458 459
}

460 461
static void mac802154_print_addr(const char *name,
				 const struct ieee802154_addr *addr)
462
{
463 464
	if (addr->mode == IEEE802154_ADDR_NONE)
		pr_debug("%s not present\n", name);
465

466 467 468 469 470 471
	pr_debug("%s PAN ID: %04x\n", name, le16_to_cpu(addr->pan_id));
	if (addr->mode == IEEE802154_ADDR_SHORT) {
		pr_debug("%s is short: %04x\n", name,
			 le16_to_cpu(addr->short_addr));
	} else {
		u64 hw = swab64((__force u64) addr->extended_addr);
472

473 474 475
		pr_debug("%s is hardware: %8phC\n", name, &hw);
	}
}
476

477 478
static int mac802154_parse_frame_start(struct sk_buff *skb,
				       struct ieee802154_hdr *hdr)
479 480
{
	int hlen;
481
	struct ieee802154_mac_cb *cb = mac_cb_init(skb);
482

483
	hlen = ieee802154_hdr_pull(skb, hdr);
484 485
	if (hlen < 0)
		return -EINVAL;
486

487
	skb->mac_len = hlen;
488

489 490
	pr_debug("fc: %04x dsn: %02x\n", le16_to_cpup((__le16 *)&hdr->fc),
		 hdr->seq);
491

492 493 494
	cb->type = hdr->fc.type;
	cb->ackreq = hdr->fc.ack_request;
	cb->secen = hdr->fc.security_enabled;
495

496 497
	mac802154_print_addr("destination", &hdr->dest);
	mac802154_print_addr("source", &hdr->source);
498

499 500
	cb->source = hdr->source;
	cb->dest = hdr->dest;
501

502
	if (hdr->fc.security_enabled) {
503
		u64 key;
504

505
		pr_debug("seclevel %i\n", hdr->sec.level);
506

507
		switch (hdr->sec.key_id_mode) {
508 509 510
		case IEEE802154_SCF_KEY_IMPLICIT:
			pr_debug("implicit key\n");
			break;
511

512
		case IEEE802154_SCF_KEY_INDEX:
513
			pr_debug("key %02x\n", hdr->sec.key_id);
514
			break;
515

516 517
		case IEEE802154_SCF_KEY_SHORT_INDEX:
			pr_debug("key %04x:%04x %02x\n",
518 519 520
				 le32_to_cpu(hdr->sec.short_src) >> 16,
				 le32_to_cpu(hdr->sec.short_src) & 0xffff,
				 hdr->sec.key_id);
521
			break;
522

523
		case IEEE802154_SCF_KEY_HW_INDEX:
524
			key = swab64((__force u64) hdr->sec.extended_src);
525
			pr_debug("key source %8phC %02x\n", &key,
526
				 hdr->sec.key_id);
527
			break;
528 529 530 531 532 533
		}
	}

	return 0;
}

534
void mac802154_wpans_rx(struct ieee802154_local *local, struct sk_buff *skb)
535 536
{
	int ret;
537
	struct ieee802154_sub_if_data *sdata;
538
	struct ieee802154_hdr hdr;
539

540
	ret = mac802154_parse_frame_start(skb, &hdr);
541 542
	if (ret) {
		pr_debug("got invalid frame\n");
543
		kfree_skb(skb);
544 545 546 547
		return;
	}

	rcu_read_lock();
548
	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
549 550
		if (sdata->type != IEEE802154_DEV_WPAN ||
		    !netif_running(sdata->dev))
551 552
			continue;

553 554 555
		mac802154_subif_frame(sdata, skb, &hdr);
		skb = NULL;
		break;
556 557
	}
	rcu_read_unlock();
558 559 560

	if (skb)
		kfree_skb(skb);
561
}