iface.c 11.5 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
#include "driver-ops.h"
34

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

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

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

48
		params.hwaddr = sdata->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 62 63 64
	struct sockaddr_ieee802154 *sa =
		(struct sockaddr_ieee802154 *)&ifr->ifr_addr;
	int err = -ENOIOCTLCMD;

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

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

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

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

		err = 0;
		break;
87
	}
88 89
	case SIOCSIFADDR:
		dev_warn(&dev->dev,
M
Masanari Iida 已提交
90
			 "Using DEBUGing ioctl SIOCSIFADDR isn't recommended!\n");
91 92 93 94 95 96 97 98 99
		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;
		}

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

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

107
	spin_unlock_bh(&sdata->mib_lock);
108 109 110 111 112 113 114 115 116 117 118 119 120
	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);
121
	return mac802154_wpan_update_llsec(dev);
122 123
}

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

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

	return 0;
}

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

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

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
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();

	if (sdata->type == IEEE802154_DEV_WPAN) {
		mutex_lock(&sdata->local->iflist_mtx);
		list_for_each_entry(subif, &sdata->local->interfaces, list) {
			if (subif != sdata && subif->type == sdata->type &&
			    subif->running) {
				mutex_unlock(&sdata->local->iflist_mtx);
				return -EBUSY;
			}
		}
		mutex_unlock(&sdata->local->iflist_mtx);
	}

	mutex_lock(&sdata->local->iflist_mtx);
	sdata->running = true;
	mutex_unlock(&sdata->local->iflist_mtx);

	if (local->open_count++ == 0) {
172
		res = drv_start(local);
173 174 175 176 177 178 179 180 181 182 183 184 185
		WARN_ON(res);
		if (res)
			goto err;
	}

	netif_start_queue(dev);
	return 0;
err:
	sdata->local->open_count--;

	return res;
}

186
static int mac802154_wpan_open(struct net_device *dev)
187 188
{
	int rc;
189
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
190
	struct ieee802154_local *local = sdata->local;
191
	struct wpan_phy *phy = sdata->local->phy;
192 193 194 195 196 197 198 199

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

	mutex_lock(&phy->pib_lock);

	if (phy->set_txpower) {
200
		rc = drv_set_tx_power(local, sdata->mac_params.transmit_power);
201 202 203 204 205
		if (rc < 0)
			goto out;
	}

	if (phy->set_lbt) {
206
		rc = drv_set_lbt_mode(local, sdata->mac_params.lbt);
207 208 209 210 211
		if (rc < 0)
			goto out;
	}

	if (phy->set_cca_mode) {
212
		rc = drv_set_cca_mode(local, sdata->mac_params.cca_mode);
213 214 215 216 217
		if (rc < 0)
			goto out;
	}

	if (phy->set_cca_ed_level) {
218 219
		rc = drv_set_cca_ed_level(local,
					  sdata->mac_params.cca_ed_level);
220 221 222 223 224
		if (rc < 0)
			goto out;
	}

	if (phy->set_csma_params) {
225 226 227
		rc = drv_set_csma_params(local, sdata->mac_params.min_be,
					 sdata->mac_params.max_be,
					 sdata->mac_params.csma_retries);
228 229 230 231 232
		if (rc < 0)
			goto out;
	}

	if (phy->set_frame_retries) {
233 234
		rc = drv_set_max_frame_retries(local,
					       sdata->mac_params.frame_retries);
235 236 237 238 239 240 241 242 243 244 245 246
		if (rc < 0)
			goto out;
	}

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

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

247 248 249 250 251 252 253 254 255 256 257 258 259 260
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);

	mutex_lock(&sdata->local->iflist_mtx);
	sdata->running = false;
	mutex_unlock(&sdata->local->iflist_mtx);

	if (!--local->open_count)
261
		drv_stop(local);
262 263 264 265

	return 0;
}

266
static int mac802154_set_header_security(struct ieee802154_sub_if_data *sdata,
267 268 269 270 271 272
					 struct ieee802154_hdr *hdr,
					 const struct ieee802154_mac_cb *cb)
{
	struct ieee802154_llsec_params params;
	u8 level;

273
	mac802154_llsec_get_params(&sdata->sec, &params);
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

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

298 299 300
static int mac802154_header_create(struct sk_buff *skb,
				   struct net_device *dev,
				   unsigned short type,
301 302
				   const void *daddr,
				   const void *saddr,
303 304
				   unsigned len)
{
305
	struct ieee802154_hdr hdr;
306
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
307
	struct ieee802154_mac_cb *cb = mac_cb(skb);
308
	int hlen;
309 310 311 312

	if (!daddr)
		return -EINVAL;

313
	memset(&hdr.fc, 0, sizeof(hdr.fc));
314 315 316 317
	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);
318

319
	if (mac802154_set_header_security(sdata, &hdr, cb) < 0)
320 321
		return -EINVAL;

322
	if (!saddr) {
323
		spin_lock_bh(&sdata->mib_lock);
324

325 326 327
		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)) {
328
			hdr.source.mode = IEEE802154_ADDR_LONG;
329
			hdr.source.extended_addr = sdata->extended_addr;
330
		} else {
331
			hdr.source.mode = IEEE802154_ADDR_SHORT;
332
			hdr.source.short_addr = sdata->short_addr;
333 334
		}

335
		hdr.source.pan_id = sdata->pan_id;
336

337
		spin_unlock_bh(&sdata->mib_lock);
338 339
	} else {
		hdr.source = *(const struct ieee802154_addr *)saddr;
340 341
	}

342
	hdr.dest = *(const struct ieee802154_addr *)daddr;
343

344 345 346
	hlen = ieee802154_hdr_push(skb, &hdr);
	if (hlen < 0)
		return -EINVAL;
347

348
	skb_reset_mac_header(skb);
349
	skb->mac_len = hlen;
350

351
	if (len > ieee802154_max_payload(&hdr))
352 353
		return -EMSGSIZE;

354
	return hlen;
355 356 357 358 359
}

static int
mac802154_header_parse(const struct sk_buff *skb, unsigned char *haddr)
{
360 361
	struct ieee802154_hdr hdr;
	struct ieee802154_addr *addr = (struct ieee802154_addr *)haddr;
362

363 364 365
	if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) {
		pr_debug("malformed packet\n");
		return 0;
366 367
	}

368 369
	*addr = hdr.source;
	return sizeof(*addr);
370 371 372 373 374 375 376 377
}

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

static const struct net_device_ops mac802154_wpan_ops = {
378
	.ndo_open		= mac802154_wpan_open,
379
	.ndo_stop		= mac802154_slave_close,
380
	.ndo_start_xmit		= ieee802154_subif_start_xmit,
381 382 383 384
	.ndo_do_ioctl		= mac802154_wpan_ioctl,
	.ndo_set_mac_address	= mac802154_wpan_mac_addr,
};

385 386 387 388 389 390
static const struct net_device_ops mac802154_monitor_ops = {
	.ndo_open		= mac802154_slave_open,
	.ndo_stop		= mac802154_slave_close,
	.ndo_start_xmit		= ieee802154_monitor_start_xmit,
};

391 392
static void mac802154_wpan_free(struct net_device *dev)
{
393
	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
394

395
	mac802154_llsec_destroy(&sdata->sec);
396 397 398 399

	free_netdev(dev);
}

400 401
void mac802154_wpan_setup(struct net_device *dev)
{
402
	struct ieee802154_sub_if_data *sdata;
403 404 405 406 407 408

	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;
409
	dev->needed_tailroom	= 2 + 16; /* FCS + MIC */
410
	dev->mtu		= IEEE802154_MTU;
A
Alan Ott 已提交
411
	dev->tx_queue_len	= 300;
412 413 414 415
	dev->type		= ARPHRD_IEEE802154;
	dev->flags		= IFF_NOARP | IFF_BROADCAST;
	dev->watchdog_timeo	= 0;

416
	dev->destructor		= mac802154_wpan_free;
417 418 419
	dev->netdev_ops		= &mac802154_wpan_ops;
	dev->ml_priv		= &mac802154_mlme_wpan;

420
	sdata = IEEE802154_DEV_TO_SUB_IF(dev);
421
	sdata->type = IEEE802154_DEV_WPAN;
422

423 424
	sdata->chan = MAC802154_CHAN_NONE;
	sdata->page = 0;
425

426 427
	spin_lock_init(&sdata->mib_lock);
	mutex_init(&sdata->sec_mtx);
428

429 430
	get_random_bytes(&sdata->bsn, 1);
	get_random_bytes(&sdata->dsn, 1);
431

432
	/* defaults per 802.15.4-2011 */
433 434 435 436 437
	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;
438

439 440
	sdata->pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST);
	sdata->short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST);
441

442
	mac802154_llsec_init(&sdata->sec);
443
}
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467

void mac802154_monitor_setup(struct net_device *dev)
{
	struct ieee802154_sub_if_data *sdata;

	dev->addr_len		= 0;
	dev->hard_header_len	= 0;
	dev->needed_tailroom	= 2; /* room for FCS */
	dev->mtu		= IEEE802154_MTU;
	dev->tx_queue_len	= 10;
	dev->type		= ARPHRD_IEEE802154_MONITOR;
	dev->flags		= IFF_NOARP | IFF_BROADCAST;
	dev->watchdog_timeo	= 0;

	dev->destructor		= free_netdev;
	dev->netdev_ops		= &mac802154_monitor_ops;
	dev->ml_priv		= &mac802154_mlme_reduced;

	sdata = IEEE802154_DEV_TO_SUB_IF(dev);
	sdata->type = IEEE802154_DEV_MONITOR;

	sdata->chan = MAC802154_CHAN_NONE; /* not initialized */
	sdata->page = 0;
}