iface.c 13.1 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

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

31
#include "ieee802154_i.h"
32
#include "driver-ops.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
	struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
38
	struct wpan_dev *wpan_dev = &sdata->wpan_dev;
39 40 41 42 43 44
	int rc = 0;

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

45
		params.pan_id = wpan_dev->pan_id;
46 47
		changed |= IEEE802154_LLSEC_PARAM_PAN_ID;

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

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

	return rc;
}

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

66 67
	ASSERT_RTNL();

68
	spin_lock_bh(&sdata->mib_lock);
69 70 71

	switch (cmd) {
	case SIOCGIFADDR:
72 73 74
	{
		u16 pan_id, short_addr;

75 76
		pan_id = le16_to_cpu(wpan_dev->pan_id);
		short_addr = le16_to_cpu(wpan_dev->short_addr);
77 78
		if (pan_id == IEEE802154_PANID_BROADCAST ||
		    short_addr == IEEE802154_ADDR_BROADCAST) {
79 80 81 82 83 84
			err = -EADDRNOTAVAIL;
			break;
		}

		sa->family = AF_IEEE802154;
		sa->addr.addr_type = IEEE802154_ADDR_SHORT;
85 86
		sa->addr.pan_id = pan_id;
		sa->addr.short_addr = short_addr;
87 88 89

		err = 0;
		break;
90
	}
91
	case SIOCSIFADDR:
92 93 94 95 96
		if (netif_running(dev)) {
			spin_unlock_bh(&sdata->mib_lock);
			return -EBUSY;
		}

97
		dev_warn(&dev->dev,
M
Masanari Iida 已提交
98
			 "Using DEBUGing ioctl SIOCSIFADDR isn't recommended!\n");
99 100 101 102 103 104 105 106 107
		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;
		}

108 109
		wpan_dev->pan_id = cpu_to_le16(sa->addr.pan_id);
		wpan_dev->short_addr = cpu_to_le16(sa->addr.short_addr);
110

111
		err = mac802154_wpan_update_llsec(dev);
112 113 114
		break;
	}

115
	spin_unlock_bh(&sdata->mib_lock);
116 117 118 119 120
	return err;
}

static int mac802154_wpan_mac_addr(struct net_device *dev, void *p)
{
121
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
122
	struct sockaddr *addr = p;
123
	__le64 extended_addr;
124 125 126 127

	if (netif_running(dev))
		return -EBUSY;

128
	ieee802154_be64_to_le64(&extended_addr, addr->sa_data);
129 130 131
	if (!ieee802154_is_valid_extended_addr(extended_addr))
		return -EINVAL;

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 139 140 141 142 143 144 145 146
static int mac802154_slave_open(struct net_device *dev)
{
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
	struct ieee802154_sub_if_data *subif;
	struct ieee802154_local *local = sdata->local;
	int res = 0;

	ASSERT_RTNL();

147
	if (sdata->vif.type == IEEE802154_DEV_WPAN) {
148 149
		mutex_lock(&sdata->local->iflist_mtx);
		list_for_each_entry(subif, &sdata->local->interfaces, list) {
150 151
			if (subif != sdata &&
			    subif->vif.type == sdata->vif.type &&
152
			    ieee802154_sdata_running(subif)) {
153 154 155 156 157 158 159
				mutex_unlock(&sdata->local->iflist_mtx);
				return -EBUSY;
			}
		}
		mutex_unlock(&sdata->local->iflist_mtx);
	}

160
	set_bit(SDATA_STATE_RUNNING, &sdata->state);
161

162
	if (!local->open_count) {
163
		res = drv_start(local);
164 165 166 167 168
		WARN_ON(res);
		if (res)
			goto err;
	}

169
	local->open_count++;
170 171 172
	netif_start_queue(dev);
	return 0;
err:
173 174
	/* might already be clear but that doesn't matter */
	clear_bit(SDATA_STATE_RUNNING, &sdata->state);
175 176 177 178

	return res;
}

179
static int mac802154_wpan_open(struct net_device *dev)
180 181
{
	int rc;
182
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
183
	struct ieee802154_local *local = sdata->local;
184
	struct wpan_dev *wpan_dev = &sdata->wpan_dev;
185
	struct wpan_phy *phy = sdata->local->phy;
186 187 188 189 190 191 192

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

	mutex_lock(&phy->pib_lock);

193
	if (local->hw.flags & IEEE802154_HW_PROMISCUOUS) {
194 195
		rc = drv_set_promiscuous_mode(local,
					      wpan_dev->promiscuous_mode);
196 197 198 199
		if (rc < 0)
			goto out;
	}

200
	if (local->hw.flags & IEEE802154_HW_AFILT) {
201
		rc = drv_set_pan_id(local, wpan_dev->pan_id);
202 203 204
		if (rc < 0)
			goto out;

205
		rc = drv_set_extended_addr(local, wpan_dev->extended_addr);
206 207
		if (rc < 0)
			goto out;
208

209
		rc = drv_set_short_addr(local, wpan_dev->short_addr);
210 211
		if (rc < 0)
			goto out;
212 213
	}

214
	if (local->hw.flags & IEEE802154_HW_LBT) {
215
		rc = drv_set_lbt_mode(local, wpan_dev->lbt);
216 217 218 219
		if (rc < 0)
			goto out;
	}

220
	if (local->hw.flags & IEEE802154_HW_CSMA_PARAMS) {
221 222 223
		rc = drv_set_csma_params(local, wpan_dev->min_be,
					 wpan_dev->max_be,
					 wpan_dev->csma_retries);
224 225 226 227
		if (rc < 0)
			goto out;
	}

228
	if (local->hw.flags & IEEE802154_HW_FRAME_RETRIES) {
229
		rc = drv_set_max_frame_retries(local, wpan_dev->frame_retries);
230 231 232 233 234 235 236 237 238 239 240 241
		if (rc < 0)
			goto out;
	}

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

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

242 243 244 245 246 247 248 249
static int mac802154_slave_close(struct net_device *dev)
{
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
	struct ieee802154_local *local = sdata->local;

	ASSERT_RTNL();

	netif_stop_queue(dev);
250
	local->open_count--;
251

252
	clear_bit(SDATA_STATE_RUNNING, &sdata->state);
253

254
	if (!local->open_count)
255
		drv_stop(local);
256 257 258 259

	return 0;
}

260
static int mac802154_set_header_security(struct ieee802154_sub_if_data *sdata,
261 262 263 264 265 266
					 struct ieee802154_hdr *hdr,
					 const struct ieee802154_mac_cb *cb)
{
	struct ieee802154_llsec_params params;
	u8 level;

267
	mac802154_llsec_get_params(&sdata->sec, &params);
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291

	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;
}

292 293 294
static int mac802154_header_create(struct sk_buff *skb,
				   struct net_device *dev,
				   unsigned short type,
295 296
				   const void *daddr,
				   const void *saddr,
297 298
				   unsigned len)
{
299
	struct ieee802154_hdr hdr;
300
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
301
	struct wpan_dev *wpan_dev = &sdata->wpan_dev;
302
	struct ieee802154_mac_cb *cb = mac_cb(skb);
303
	int hlen;
304 305 306 307

	if (!daddr)
		return -EINVAL;

308
	memset(&hdr.fc, 0, sizeof(hdr.fc));
309 310 311 312
	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);
313

314
	if (mac802154_set_header_security(sdata, &hdr, cb) < 0)
315 316
		return -EINVAL;

317
	if (!saddr) {
318
		spin_lock_bh(&sdata->mib_lock);
319

320 321 322
		if (wpan_dev->short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST) ||
		    wpan_dev->short_addr == cpu_to_le16(IEEE802154_ADDR_UNDEF) ||
		    wpan_dev->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST)) {
323
			hdr.source.mode = IEEE802154_ADDR_LONG;
324
			hdr.source.extended_addr = wpan_dev->extended_addr;
325
		} else {
326
			hdr.source.mode = IEEE802154_ADDR_SHORT;
327
			hdr.source.short_addr = wpan_dev->short_addr;
328 329
		}

330
		hdr.source.pan_id = wpan_dev->pan_id;
331

332
		spin_unlock_bh(&sdata->mib_lock);
333 334
	} else {
		hdr.source = *(const struct ieee802154_addr *)saddr;
335 336
	}

337
	hdr.dest = *(const struct ieee802154_addr *)daddr;
338

339 340 341
	hlen = ieee802154_hdr_push(skb, &hdr);
	if (hlen < 0)
		return -EINVAL;
342

343
	skb_reset_mac_header(skb);
344
	skb->mac_len = hlen;
345

346
	if (len > ieee802154_max_payload(&hdr))
347 348
		return -EMSGSIZE;

349
	return hlen;
350 351 352 353 354
}

static int
mac802154_header_parse(const struct sk_buff *skb, unsigned char *haddr)
{
355 356
	struct ieee802154_hdr hdr;
	struct ieee802154_addr *addr = (struct ieee802154_addr *)haddr;
357

358 359 360
	if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) {
		pr_debug("malformed packet\n");
		return 0;
361 362
	}

363 364
	*addr = hdr.source;
	return sizeof(*addr);
365 366 367 368 369 370 371 372
}

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

static const struct net_device_ops mac802154_wpan_ops = {
373
	.ndo_open		= mac802154_wpan_open,
374
	.ndo_stop		= mac802154_slave_close,
375
	.ndo_start_xmit		= ieee802154_subif_start_xmit,
376 377 378 379
	.ndo_do_ioctl		= mac802154_wpan_ioctl,
	.ndo_set_mac_address	= mac802154_wpan_mac_addr,
};

380
static const struct net_device_ops mac802154_monitor_ops = {
381
	.ndo_open		= mac802154_wpan_open,
382 383 384 385
	.ndo_stop		= mac802154_slave_close,
	.ndo_start_xmit		= ieee802154_monitor_start_xmit,
};

386 387
static void mac802154_wpan_free(struct net_device *dev)
{
388
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
389

390
	mac802154_llsec_destroy(&sdata->sec);
391 392 393 394

	free_netdev(dev);
}

395
static void ieee802154_if_setup(struct net_device *dev)
396
{
397 398
	dev->addr_len		= IEEE802154_EXTENDED_ADDR_LEN;
	memset(dev->broadcast, 0xff, IEEE802154_EXTENDED_ADDR_LEN);
399 400

	dev->hard_header_len	= MAC802154_FRAME_HARD_HEADER_LEN;
401
	dev->needed_tailroom	= 2 + 16; /* FCS + MIC */
402
	dev->mtu		= IEEE802154_MTU;
A
Alan Ott 已提交
403
	dev->tx_queue_len	= 300;
404
	dev->flags		= IFF_NOARP | IFF_BROADCAST;
405
}
406

407 408 409
static int
ieee802154_setup_sdata(struct ieee802154_sub_if_data *sdata, int type)
{
410 411
	struct wpan_dev *wpan_dev = &sdata->wpan_dev;

412
	/* set some type-dependent values */
413
	sdata->vif.type = type;
414
	sdata->wpan_dev.iftype = type;
415

416 417
	get_random_bytes(&wpan_dev->bsn, 1);
	get_random_bytes(&wpan_dev->dsn, 1);
418

419
	/* defaults per 802.15.4-2011 */
420 421 422
	wpan_dev->min_be = 3;
	wpan_dev->max_be = 5;
	wpan_dev->csma_retries = 4;
423
	/* for compatibility, actual default is 3 */
424
	wpan_dev->frame_retries = -1;
425

426 427
	wpan_dev->pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST);
	wpan_dev->short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST);
428

429 430
	switch (type) {
	case IEEE802154_DEV_WPAN:
431 432 433
		ieee802154_be64_to_le64(&wpan_dev->extended_addr,
					sdata->dev->dev_addr);

434 435 436 437
		sdata->dev->header_ops = &mac802154_header_ops;
		sdata->dev->destructor = mac802154_wpan_free;
		sdata->dev->netdev_ops = &mac802154_wpan_ops;
		sdata->dev->ml_priv = &mac802154_mlme_wpan;
438
		wpan_dev->promiscuous_mode = false;
439

440 441
		spin_lock_init(&sdata->mib_lock);
		mutex_init(&sdata->sec_mtx);
442

443 444 445 446 447
		mac802154_llsec_init(&sdata->sec);
		break;
	case IEEE802154_DEV_MONITOR:
		sdata->dev->destructor = free_netdev;
		sdata->dev->netdev_ops = &mac802154_monitor_ops;
448
		wpan_dev->promiscuous_mode = true;
449 450 451 452
		break;
	default:
		BUG();
	}
453 454 455 456 457 458 459 460

	return 0;
}

struct net_device *
ieee802154_if_add(struct ieee802154_local *local, const char *name,
		  struct wpan_dev **new_wpan_dev, int type)
{
461 462 463 464 465 466
	struct net_device *ndev = NULL;
	struct ieee802154_sub_if_data *sdata = NULL;
	int ret = -ENOMEM;

	ASSERT_RTNL();

467 468
	ndev = alloc_netdev(sizeof(*sdata) + local->hw.vif_data_size, name,
			    NET_NAME_UNKNOWN, ieee802154_if_setup);
469 470 471 472 473 474 475 476
	if (!ndev)
		return ERR_PTR(-ENOMEM);

	ndev->needed_headroom = local->hw.extra_tx_headroom;

	ret = dev_alloc_name(ndev, ndev->name);
	if (ret < 0)
		goto err;
477 478 479

	switch (type) {
	case IEEE802154_DEV_WPAN:
480
		ndev->type = ARPHRD_IEEE802154;
481
		break;
482 483
	case IEEE802154_DEV_MONITOR:
		ndev->type = ARPHRD_IEEE802154_MONITOR;
484
		break;
485 486 487
	default:
		ret = -EINVAL;
		goto err;
488
	}
489

490 491 492
	ieee802154_le64_to_be64(ndev->perm_addr,
				&local->hw.phy->perm_extended_addr);
	memcpy(ndev->dev_addr, ndev->perm_addr, IEEE802154_EXTENDED_ADDR_LEN);
493 494 495 496 497 498 499 500 501 502 503 504
	/* TODO check this */
	SET_NETDEV_DEV(ndev, &local->phy->dev);
	sdata = netdev_priv(ndev);
	ndev->ieee802154_ptr = &sdata->wpan_dev;
	memcpy(sdata->name, ndev->name, IFNAMSIZ);
	sdata->dev = ndev;
	sdata->wpan_dev.wpan_phy = local->hw.phy;
	sdata->local = local;

	/* setup type-dependent data */
	ret = ieee802154_setup_sdata(sdata, type);
	if (ret)
505 506
		goto err;

507 508 509 510 511 512 513 514 515
	if (ndev) {
		ret = register_netdevice(ndev);
		if (ret < 0)
			goto err;
	}

	mutex_lock(&local->iflist_mtx);
	list_add_tail_rcu(&sdata->list, &local->interfaces);
	mutex_unlock(&local->iflist_mtx);
516

517 518 519 520
	if (new_wpan_dev)
		*new_wpan_dev = &sdata->wpan_dev;

	return ndev;
521 522

err:
523 524
	free_netdev(ndev);
	return ERR_PTR(ret);
525 526
}

527 528 529 530 531 532 533 534 535 536 537
void ieee802154_if_remove(struct ieee802154_sub_if_data *sdata)
{
	ASSERT_RTNL();

	mutex_lock(&sdata->local->iflist_mtx);
	list_del_rcu(&sdata->list);
	mutex_unlock(&sdata->local->iflist_mtx);

	synchronize_rcu();
	unregister_netdevice(sdata->dev);
}