dgram.c 11.0 KB
Newer Older
1
/*
2
 * IEEE 802.15.4 dgram socket interface
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * Copyright 2007, 2008 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:
 * Sergey Lapin <slapin@ossfans.org>
 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
 */

20
#include <linux/capability.h>
21 22 23 24
#include <linux/net.h>
#include <linux/module.h>
#include <linux/if_arp.h>
#include <linux/list.h>
25
#include <linux/slab.h>
26
#include <linux/ieee802154.h>
27
#include <net/sock.h>
28 29
#include <net/af_ieee802154.h>
#include <net/ieee802154_netdev.h>
30 31 32 33 34 35 36 37 38 39 40

#include <asm/ioctls.h>

#include "af802154.h"

static HLIST_HEAD(dgram_head);
static DEFINE_RWLOCK(dgram_lock);

struct dgram_sock {
	struct sock sk;

41 42
	struct ieee802154_addr src_addr;
	struct ieee802154_addr dst_addr;
43

44
	unsigned int bound:1;
45
	unsigned int connected:1;
46
	unsigned int want_ack:1;
47 48 49 50
	unsigned int secen:1;
	unsigned int secen_override:1;
	unsigned int seclevel:3;
	unsigned int seclevel_override:1;
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
};

static inline struct dgram_sock *dgram_sk(const struct sock *sk)
{
	return container_of(sk, struct dgram_sock, sk);
}

static void dgram_hash(struct sock *sk)
{
	write_lock_bh(&dgram_lock);
	sk_add_node(sk, &dgram_head);
	sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
	write_unlock_bh(&dgram_lock);
}

static void dgram_unhash(struct sock *sk)
{
	write_lock_bh(&dgram_lock);
	if (sk_del_node_init(sk))
		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
	write_unlock_bh(&dgram_lock);
}

static int dgram_init(struct sock *sk)
{
	struct dgram_sock *ro = dgram_sk(sk);

78
	ro->want_ack = 1;
79 80 81 82 83 84 85 86 87 88 89
	return 0;
}

static void dgram_close(struct sock *sk, long timeout)
{
	sk_common_release(sk);
}

static int dgram_bind(struct sock *sk, struct sockaddr *uaddr, int len)
{
	struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr;
90
	struct ieee802154_addr haddr;
91
	struct dgram_sock *ro = dgram_sk(sk);
92
	int err = -EINVAL;
93 94
	struct net_device *dev;

95 96
	lock_sock(sk);

97 98 99
	ro->bound = 0;

	if (len < sizeof(*addr))
100
		goto out;
101 102

	if (addr->family != AF_IEEE802154)
103
		goto out;
104

105 106
	ieee802154_addr_from_sa(&haddr, &addr->addr);
	dev = ieee802154_get_dev(sock_net(sk), &haddr);
107 108 109 110 111 112 113 114 115 116
	if (!dev) {
		err = -ENODEV;
		goto out;
	}

	if (dev->type != ARPHRD_IEEE802154) {
		err = -ENODEV;
		goto out_put;
	}

117
	ro->src_addr = haddr;
118 119

	ro->bound = 1;
120
	err = 0;
121 122 123 124 125 126 127 128 129 130 131 132 133
out_put:
	dev_put(dev);
out:
	release_sock(sk);

	return err;
}

static int dgram_ioctl(struct sock *sk, int cmd, unsigned long arg)
{
	switch (cmd) {
	case SIOCOUTQ:
	{
134 135
		int amount = sk_wmem_alloc_get(sk);

136 137 138 139 140 141 142 143 144 145 146 147
		return put_user(amount, (int __user *)arg);
	}

	case SIOCINQ:
	{
		struct sk_buff *skb;
		unsigned long amount;

		amount = 0;
		spin_lock_bh(&sk->sk_receive_queue.lock);
		skb = skb_peek(&sk->sk_receive_queue);
		if (skb != NULL) {
148
			/* We will only return the amount
149 150 151
			 * of this packet since that is all
			 * that will be read.
			 */
152
			amount = skb->len - ieee802154_hdr_length(skb);
153 154 155 156 157
		}
		spin_unlock_bh(&sk->sk_receive_queue.lock);
		return put_user(amount, (int __user *)arg);
	}
	}
158

159 160 161 162 163
	return -ENOIOCTLCMD;
}

/* FIXME: autobind */
static int dgram_connect(struct sock *sk, struct sockaddr *uaddr,
164
			 int len)
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
{
	struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr;
	struct dgram_sock *ro = dgram_sk(sk);
	int err = 0;

	if (len < sizeof(*addr))
		return -EINVAL;

	if (addr->family != AF_IEEE802154)
		return -EINVAL;

	lock_sock(sk);

	if (!ro->bound) {
		err = -ENETUNREACH;
		goto out;
	}

183
	ieee802154_addr_from_sa(&ro->dst_addr, &addr->addr);
184
	ro->connected = 1;
185 186 187 188 189 190 191 192 193 194 195

out:
	release_sock(sk);
	return err;
}

static int dgram_disconnect(struct sock *sk, int flags)
{
	struct dgram_sock *ro = dgram_sk(sk);

	lock_sock(sk);
196
	ro->connected = 0;
197 198 199 200 201 202
	release_sock(sk);

	return 0;
}

static int dgram_sendmsg(struct kiocb *iocb, struct sock *sk,
203
			 struct msghdr *msg, size_t size)
204 205
{
	struct net_device *dev;
206
	unsigned int mtu;
207
	struct sk_buff *skb;
208
	struct ieee802154_mac_cb *cb;
209
	struct dgram_sock *ro = dgram_sk(sk);
210
	struct ieee802154_addr dst_addr;
211
	int hlen, tlen;
212 213 214 215 216 217 218
	int err;

	if (msg->msg_flags & MSG_OOB) {
		pr_debug("msg->msg_flags = 0x%x\n", msg->msg_flags);
		return -EOPNOTSUPP;
	}

219 220 221 222 223
	if (!ro->connected && !msg->msg_name)
		return -EDESTADDRREQ;
	else if (ro->connected && msg->msg_name)
		return -EISCONN;

224 225 226 227 228 229 230 231 232 233 234 235 236
	if (!ro->bound)
		dev = dev_getfirstbyhwtype(sock_net(sk), ARPHRD_IEEE802154);
	else
		dev = ieee802154_get_dev(sock_net(sk), &ro->src_addr);

	if (!dev) {
		pr_debug("no dev\n");
		err = -ENXIO;
		goto out;
	}
	mtu = dev->mtu;
	pr_debug("name = %s, mtu = %u\n", dev->name, mtu);

237 238
	if (size > mtu) {
		pr_debug("size = %Zu, mtu = %u\n", size, mtu);
239
		err = -EMSGSIZE;
240 241 242
		goto out_dev;
	}

243 244 245
	hlen = LL_RESERVED_SPACE(dev);
	tlen = dev->needed_tailroom;
	skb = sock_alloc_send_skb(sk, hlen + tlen + size,
246 247
				  msg->msg_flags & MSG_DONTWAIT,
				  &err);
248 249 250
	if (!skb)
		goto out_dev;

251
	skb_reserve(skb, hlen);
252 253 254

	skb_reset_network_header(skb);

255 256 257
	cb = mac_cb_init(skb);
	cb->type = IEEE802154_FC_TYPE_DATA;
	cb->ackreq = ro->want_ack;
258

259
	if (msg->msg_name) {
260 261
		DECLARE_SOCKADDR(struct sockaddr_ieee802154*,
				 daddr, msg->msg_name);
262 263 264 265 266 267

		ieee802154_addr_from_sa(&dst_addr, &daddr->addr);
	} else {
		dst_addr = ro->dst_addr;
	}

268 269 270 271 272
	cb->secen = ro->secen;
	cb->secen_override = ro->secen_override;
	cb->seclevel = ro->seclevel;
	cb->seclevel_override = ro->seclevel_override;

273 274
	err = dev_hard_header(skb, dev, ETH_P_IEEE802154, &dst_addr,
			      ro->bound ? &ro->src_addr : NULL, size);
275 276 277
	if (err < 0)
		goto out_skb;

A
Al Viro 已提交
278
	err = memcpy_from_msg(skb_put(skb, size), msg, size);
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
	if (err < 0)
		goto out_skb;

	skb->dev = dev;
	skb->sk  = sk;
	skb->protocol = htons(ETH_P_IEEE802154);

	dev_put(dev);

	err = dev_queue_xmit(skb);
	if (err > 0)
		err = net_xmit_errno(err);

	return err ?: size;

out_skb:
	kfree_skb(skb);
out_dev:
	dev_put(dev);
out:
	return err;
}

static int dgram_recvmsg(struct kiocb *iocb, struct sock *sk,
303 304
			 struct msghdr *msg, size_t len, int noblock,
			 int flags, int *addr_len)
305 306 307 308
{
	size_t copied = 0;
	int err = -EOPNOTSUPP;
	struct sk_buff *skb;
309
	DECLARE_SOCKADDR(struct sockaddr_ieee802154 *, saddr, msg->msg_name);
310 311 312 313 314 315 316 317 318 319 320 321

	skb = skb_recv_datagram(sk, flags, noblock, &err);
	if (!skb)
		goto out;

	copied = skb->len;
	if (len < copied) {
		msg->msg_flags |= MSG_TRUNC;
		copied = len;
	}

	/* FIXME: skip headers if necessary ?! */
322
	err = skb_copy_datagram_msg(skb, 0, msg, copied);
323 324 325
	if (err)
		goto done;

326
	sock_recv_ts_and_drops(msg, sk, skb);
327

328 329
	if (saddr) {
		saddr->family = AF_IEEE802154;
330
		ieee802154_addr_to_sa(&saddr->addr, &mac_cb(skb)->source);
331
		*addr_len = sizeof(*saddr);
332
	}
333

334 335 336 337 338 339 340 341 342 343 344 345
	if (flags & MSG_TRUNC)
		copied = skb->len;
done:
	skb_free_datagram(sk, skb);
out:
	if (err)
		return err;
	return copied;
}

static int dgram_rcv_skb(struct sock *sk, struct sk_buff *skb)
{
346 347 348 349
	skb = skb_share_check(skb, GFP_ATOMIC);
	if (!skb)
		return NET_RX_DROP;

350 351 352 353 354 355 356 357
	if (sock_queue_rcv_skb(sk, skb) < 0) {
		kfree_skb(skb);
		return NET_RX_DROP;
	}

	return NET_RX_SUCCESS;
}

358 359 360
static inline bool
ieee802154_match_sock(__le64 hw_addr, __le16 pan_id, __le16 short_addr,
		      struct dgram_sock *ro)
361 362
{
	if (!ro->bound)
363
		return true;
364

365 366 367
	if (ro->src_addr.mode == IEEE802154_ADDR_LONG &&
	    hw_addr == ro->src_addr.extended_addr)
		return true;
368

369 370 371 372
	if (ro->src_addr.mode == IEEE802154_ADDR_SHORT &&
	    pan_id == ro->src_addr.pan_id &&
	    short_addr == ro->src_addr.short_addr)
		return true;
373

374
	return false;
375 376 377 378 379 380
}

int ieee802154_dgram_deliver(struct net_device *dev, struct sk_buff *skb)
{
	struct sock *sk, *prev = NULL;
	int ret = NET_RX_SUCCESS;
381 382
	__le16 pan_id, short_addr;
	__le64 hw_addr;
383 384 385 386

	/* Data frame processing */
	BUG_ON(dev->type != ARPHRD_IEEE802154);

387 388 389
	pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
	short_addr = ieee802154_mlme_ops(dev)->get_short_addr(dev);
	hw_addr = ieee802154_devaddr_from_raw(dev->dev_addr);
390 391

	read_lock(&dgram_lock);
392
	sk_for_each(sk, &dgram_head) {
393 394
		if (ieee802154_match_sock(hw_addr, pan_id, short_addr,
					  dgram_sk(sk))) {
395 396
			if (prev) {
				struct sk_buff *clone;
397

398 399 400 401 402 403 404 405 406
				clone = skb_clone(skb, GFP_ATOMIC);
				if (clone)
					dgram_rcv_skb(prev, clone);
			}

			prev = sk;
		}
	}

407
	if (prev) {
408
		dgram_rcv_skb(prev, skb);
409
	} else {
410 411 412 413 414 415 416 417
		kfree_skb(skb);
		ret = NET_RX_DROP;
	}
	read_unlock(&dgram_lock);

	return ret;
}

418
static int dgram_getsockopt(struct sock *sk, int level, int optname,
419
			    char __user *optval, int __user *optlen)
420
{
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
	struct dgram_sock *ro = dgram_sk(sk);

	int val, len;

	if (level != SOL_IEEE802154)
		return -EOPNOTSUPP;

	if (get_user(len, optlen))
		return -EFAULT;

	len = min_t(unsigned int, len, sizeof(int));

	switch (optname) {
	case WPAN_WANTACK:
		val = ro->want_ack;
		break;
437 438 439 440 441 442 443 444 445 446 447 448 449 450
	case WPAN_SECURITY:
		if (!ro->secen_override)
			val = WPAN_SECURITY_DEFAULT;
		else if (ro->secen)
			val = WPAN_SECURITY_ON;
		else
			val = WPAN_SECURITY_OFF;
		break;
	case WPAN_SECURITY_LEVEL:
		if (!ro->seclevel_override)
			val = WPAN_SECURITY_LEVEL_DEFAULT;
		else
			val = ro->seclevel;
		break;
451 452 453 454 455 456 457 458 459
	default:
		return -ENOPROTOOPT;
	}

	if (put_user(len, optlen))
		return -EFAULT;
	if (copy_to_user(optval, &val, len))
		return -EFAULT;
	return 0;
460 461 462
}

static int dgram_setsockopt(struct sock *sk, int level, int optname,
463
			    char __user *optval, unsigned int optlen)
464
{
465
	struct dgram_sock *ro = dgram_sk(sk);
466
	struct net *net = sock_net(sk);
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
	int val;
	int err = 0;

	if (optlen < sizeof(int))
		return -EINVAL;

	if (get_user(val, (int __user *)optval))
		return -EFAULT;

	lock_sock(sk);

	switch (optname) {
	case WPAN_WANTACK:
		ro->want_ack = !!val;
		break;
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
	case WPAN_SECURITY:
		if (!ns_capable(net->user_ns, CAP_NET_ADMIN) &&
		    !ns_capable(net->user_ns, CAP_NET_RAW)) {
			err = -EPERM;
			break;
		}

		switch (val) {
		case WPAN_SECURITY_DEFAULT:
			ro->secen_override = 0;
			break;
		case WPAN_SECURITY_ON:
			ro->secen_override = 1;
			ro->secen = 1;
			break;
		case WPAN_SECURITY_OFF:
			ro->secen_override = 1;
			ro->secen = 0;
			break;
		default:
			err = -EINVAL;
			break;
		}
		break;
	case WPAN_SECURITY_LEVEL:
		if (!ns_capable(net->user_ns, CAP_NET_ADMIN) &&
		    !ns_capable(net->user_ns, CAP_NET_RAW)) {
			err = -EPERM;
			break;
		}

		if (val < WPAN_SECURITY_LEVEL_DEFAULT ||
		    val > IEEE802154_SCF_SECLEVEL_ENC_MIC128) {
			err = -EINVAL;
		} else if (val == WPAN_SECURITY_LEVEL_DEFAULT) {
			ro->seclevel_override = 0;
		} else {
			ro->seclevel_override = 1;
			ro->seclevel = val;
		}
		break;
523 524 525 526 527 528 529
	default:
		err = -ENOPROTOOPT;
		break;
	}

	release_sock(sk);
	return err;
530 531
}

532 533 534 535 536 537 538 539 540 541 542 543 544 545
struct proto ieee802154_dgram_prot = {
	.name		= "IEEE-802.15.4-MAC",
	.owner		= THIS_MODULE,
	.obj_size	= sizeof(struct dgram_sock),
	.init		= dgram_init,
	.close		= dgram_close,
	.bind		= dgram_bind,
	.sendmsg	= dgram_sendmsg,
	.recvmsg	= dgram_recvmsg,
	.hash		= dgram_hash,
	.unhash		= dgram_unhash,
	.connect	= dgram_connect,
	.disconnect	= dgram_disconnect,
	.ioctl		= dgram_ioctl,
546 547
	.getsockopt	= dgram_getsockopt,
	.setsockopt	= dgram_setsockopt,
548 549
};