dgram.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. * IEEE 802.15.4 dgram socket interface
  3. *
  4. * Copyright 2007, 2008 Siemens AG
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2
  8. * as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Written by:
  20. * Sergey Lapin <slapin@ossfans.org>
  21. * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
  22. */
  23. #include <linux/net.h>
  24. #include <linux/module.h>
  25. #include <linux/if_arp.h>
  26. #include <linux/list.h>
  27. #include <linux/slab.h>
  28. #include <net/sock.h>
  29. #include <net/af_ieee802154.h>
  30. #include <net/ieee802154.h>
  31. #include <net/ieee802154_netdev.h>
  32. #include <asm/ioctls.h>
  33. #include "af802154.h"
  34. static HLIST_HEAD(dgram_head);
  35. static DEFINE_RWLOCK(dgram_lock);
  36. struct dgram_sock {
  37. struct sock sk;
  38. struct ieee802154_addr src_addr;
  39. struct ieee802154_addr dst_addr;
  40. unsigned int bound:1;
  41. unsigned int want_ack:1;
  42. };
  43. static inline struct dgram_sock *dgram_sk(const struct sock *sk)
  44. {
  45. return container_of(sk, struct dgram_sock, sk);
  46. }
  47. static void dgram_hash(struct sock *sk)
  48. {
  49. write_lock_bh(&dgram_lock);
  50. sk_add_node(sk, &dgram_head);
  51. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  52. write_unlock_bh(&dgram_lock);
  53. }
  54. static void dgram_unhash(struct sock *sk)
  55. {
  56. write_lock_bh(&dgram_lock);
  57. if (sk_del_node_init(sk))
  58. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  59. write_unlock_bh(&dgram_lock);
  60. }
  61. static int dgram_init(struct sock *sk)
  62. {
  63. struct dgram_sock *ro = dgram_sk(sk);
  64. ro->dst_addr.addr_type = IEEE802154_ADDR_LONG;
  65. ro->dst_addr.pan_id = 0xffff;
  66. ro->want_ack = 1;
  67. memset(&ro->dst_addr.hwaddr, 0xff, sizeof(ro->dst_addr.hwaddr));
  68. return 0;
  69. }
  70. static void dgram_close(struct sock *sk, long timeout)
  71. {
  72. sk_common_release(sk);
  73. }
  74. static int dgram_bind(struct sock *sk, struct sockaddr *uaddr, int len)
  75. {
  76. struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr;
  77. struct dgram_sock *ro = dgram_sk(sk);
  78. int err = -EINVAL;
  79. struct net_device *dev;
  80. lock_sock(sk);
  81. ro->bound = 0;
  82. if (len < sizeof(*addr))
  83. goto out;
  84. if (addr->family != AF_IEEE802154)
  85. goto out;
  86. dev = ieee802154_get_dev(sock_net(sk), &addr->addr);
  87. if (!dev) {
  88. err = -ENODEV;
  89. goto out;
  90. }
  91. if (dev->type != ARPHRD_IEEE802154) {
  92. err = -ENODEV;
  93. goto out_put;
  94. }
  95. memcpy(&ro->src_addr, &addr->addr, sizeof(struct ieee802154_addr));
  96. ro->bound = 1;
  97. err = 0;
  98. out_put:
  99. dev_put(dev);
  100. out:
  101. release_sock(sk);
  102. return err;
  103. }
  104. static int dgram_ioctl(struct sock *sk, int cmd, unsigned long arg)
  105. {
  106. switch (cmd) {
  107. case SIOCOUTQ:
  108. {
  109. int amount = sk_wmem_alloc_get(sk);
  110. return put_user(amount, (int __user *)arg);
  111. }
  112. case SIOCINQ:
  113. {
  114. struct sk_buff *skb;
  115. unsigned long amount;
  116. amount = 0;
  117. spin_lock_bh(&sk->sk_receive_queue.lock);
  118. skb = skb_peek(&sk->sk_receive_queue);
  119. if (skb != NULL) {
  120. /*
  121. * We will only return the amount
  122. * of this packet since that is all
  123. * that will be read.
  124. */
  125. /* FIXME: parse the header for more correct value */
  126. amount = skb->len - (3+8+8);
  127. }
  128. spin_unlock_bh(&sk->sk_receive_queue.lock);
  129. return put_user(amount, (int __user *)arg);
  130. }
  131. }
  132. return -ENOIOCTLCMD;
  133. }
  134. /* FIXME: autobind */
  135. static int dgram_connect(struct sock *sk, struct sockaddr *uaddr,
  136. int len)
  137. {
  138. struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr;
  139. struct dgram_sock *ro = dgram_sk(sk);
  140. int err = 0;
  141. if (len < sizeof(*addr))
  142. return -EINVAL;
  143. if (addr->family != AF_IEEE802154)
  144. return -EINVAL;
  145. lock_sock(sk);
  146. if (!ro->bound) {
  147. err = -ENETUNREACH;
  148. goto out;
  149. }
  150. memcpy(&ro->dst_addr, &addr->addr, sizeof(struct ieee802154_addr));
  151. out:
  152. release_sock(sk);
  153. return err;
  154. }
  155. static int dgram_disconnect(struct sock *sk, int flags)
  156. {
  157. struct dgram_sock *ro = dgram_sk(sk);
  158. lock_sock(sk);
  159. ro->dst_addr.addr_type = IEEE802154_ADDR_LONG;
  160. memset(&ro->dst_addr.hwaddr, 0xff, sizeof(ro->dst_addr.hwaddr));
  161. release_sock(sk);
  162. return 0;
  163. }
  164. static int dgram_sendmsg(struct kiocb *iocb, struct sock *sk,
  165. struct msghdr *msg, size_t size)
  166. {
  167. struct net_device *dev;
  168. unsigned int mtu;
  169. struct sk_buff *skb;
  170. struct dgram_sock *ro = dgram_sk(sk);
  171. int hlen, tlen;
  172. int err;
  173. if (msg->msg_flags & MSG_OOB) {
  174. pr_debug("msg->msg_flags = 0x%x\n", msg->msg_flags);
  175. return -EOPNOTSUPP;
  176. }
  177. if (!ro->bound)
  178. dev = dev_getfirstbyhwtype(sock_net(sk), ARPHRD_IEEE802154);
  179. else
  180. dev = ieee802154_get_dev(sock_net(sk), &ro->src_addr);
  181. if (!dev) {
  182. pr_debug("no dev\n");
  183. err = -ENXIO;
  184. goto out;
  185. }
  186. mtu = dev->mtu;
  187. pr_debug("name = %s, mtu = %u\n", dev->name, mtu);
  188. hlen = LL_RESERVED_SPACE(dev);
  189. tlen = dev->needed_tailroom;
  190. skb = sock_alloc_send_skb(sk, hlen + tlen + size,
  191. msg->msg_flags & MSG_DONTWAIT,
  192. &err);
  193. if (!skb)
  194. goto out_dev;
  195. skb_reserve(skb, hlen);
  196. skb_reset_network_header(skb);
  197. mac_cb(skb)->flags = IEEE802154_FC_TYPE_DATA;
  198. if (ro->want_ack)
  199. mac_cb(skb)->flags |= MAC_CB_FLAG_ACKREQ;
  200. mac_cb(skb)->seq = ieee802154_mlme_ops(dev)->get_dsn(dev);
  201. err = dev_hard_header(skb, dev, ETH_P_IEEE802154, &ro->dst_addr,
  202. ro->bound ? &ro->src_addr : NULL, size);
  203. if (err < 0)
  204. goto out_skb;
  205. skb_reset_mac_header(skb);
  206. err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
  207. if (err < 0)
  208. goto out_skb;
  209. if (size > mtu) {
  210. pr_debug("size = %Zu, mtu = %u\n", size, mtu);
  211. err = -EINVAL;
  212. goto out_skb;
  213. }
  214. skb->dev = dev;
  215. skb->sk = sk;
  216. skb->protocol = htons(ETH_P_IEEE802154);
  217. dev_put(dev);
  218. err = dev_queue_xmit(skb);
  219. if (err > 0)
  220. err = net_xmit_errno(err);
  221. return err ?: size;
  222. out_skb:
  223. kfree_skb(skb);
  224. out_dev:
  225. dev_put(dev);
  226. out:
  227. return err;
  228. }
  229. static int dgram_recvmsg(struct kiocb *iocb, struct sock *sk,
  230. struct msghdr *msg, size_t len, int noblock, int flags,
  231. int *addr_len)
  232. {
  233. size_t copied = 0;
  234. int err = -EOPNOTSUPP;
  235. struct sk_buff *skb;
  236. struct sockaddr_ieee802154 *saddr;
  237. saddr = (struct sockaddr_ieee802154 *)msg->msg_name;
  238. skb = skb_recv_datagram(sk, flags, noblock, &err);
  239. if (!skb)
  240. goto out;
  241. copied = skb->len;
  242. if (len < copied) {
  243. msg->msg_flags |= MSG_TRUNC;
  244. copied = len;
  245. }
  246. /* FIXME: skip headers if necessary ?! */
  247. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  248. if (err)
  249. goto done;
  250. sock_recv_ts_and_drops(msg, sk, skb);
  251. if (saddr) {
  252. saddr->family = AF_IEEE802154;
  253. saddr->addr = mac_cb(skb)->sa;
  254. *addr_len = sizeof(*saddr);
  255. }
  256. if (flags & MSG_TRUNC)
  257. copied = skb->len;
  258. done:
  259. skb_free_datagram(sk, skb);
  260. out:
  261. if (err)
  262. return err;
  263. return copied;
  264. }
  265. static int dgram_rcv_skb(struct sock *sk, struct sk_buff *skb)
  266. {
  267. if (sock_queue_rcv_skb(sk, skb) < 0) {
  268. kfree_skb(skb);
  269. return NET_RX_DROP;
  270. }
  271. return NET_RX_SUCCESS;
  272. }
  273. static inline int ieee802154_match_sock(u8 *hw_addr, u16 pan_id,
  274. u16 short_addr, struct dgram_sock *ro)
  275. {
  276. if (!ro->bound)
  277. return 1;
  278. if (ro->src_addr.addr_type == IEEE802154_ADDR_LONG &&
  279. !memcmp(ro->src_addr.hwaddr, hw_addr, IEEE802154_ADDR_LEN))
  280. return 1;
  281. if (ro->src_addr.addr_type == IEEE802154_ADDR_SHORT &&
  282. pan_id == ro->src_addr.pan_id &&
  283. short_addr == ro->src_addr.short_addr)
  284. return 1;
  285. return 0;
  286. }
  287. int ieee802154_dgram_deliver(struct net_device *dev, struct sk_buff *skb)
  288. {
  289. struct sock *sk, *prev = NULL;
  290. struct hlist_node *node;
  291. int ret = NET_RX_SUCCESS;
  292. u16 pan_id, short_addr;
  293. /* Data frame processing */
  294. BUG_ON(dev->type != ARPHRD_IEEE802154);
  295. pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
  296. short_addr = ieee802154_mlme_ops(dev)->get_short_addr(dev);
  297. read_lock(&dgram_lock);
  298. sk_for_each(sk, node, &dgram_head) {
  299. if (ieee802154_match_sock(dev->dev_addr, pan_id, short_addr,
  300. dgram_sk(sk))) {
  301. if (prev) {
  302. struct sk_buff *clone;
  303. clone = skb_clone(skb, GFP_ATOMIC);
  304. if (clone)
  305. dgram_rcv_skb(prev, clone);
  306. }
  307. prev = sk;
  308. }
  309. }
  310. if (prev)
  311. dgram_rcv_skb(prev, skb);
  312. else {
  313. kfree_skb(skb);
  314. ret = NET_RX_DROP;
  315. }
  316. read_unlock(&dgram_lock);
  317. return ret;
  318. }
  319. static int dgram_getsockopt(struct sock *sk, int level, int optname,
  320. char __user *optval, int __user *optlen)
  321. {
  322. struct dgram_sock *ro = dgram_sk(sk);
  323. int val, len;
  324. if (level != SOL_IEEE802154)
  325. return -EOPNOTSUPP;
  326. if (get_user(len, optlen))
  327. return -EFAULT;
  328. len = min_t(unsigned int, len, sizeof(int));
  329. switch (optname) {
  330. case WPAN_WANTACK:
  331. val = ro->want_ack;
  332. break;
  333. default:
  334. return -ENOPROTOOPT;
  335. }
  336. if (put_user(len, optlen))
  337. return -EFAULT;
  338. if (copy_to_user(optval, &val, len))
  339. return -EFAULT;
  340. return 0;
  341. }
  342. static int dgram_setsockopt(struct sock *sk, int level, int optname,
  343. char __user *optval, unsigned int optlen)
  344. {
  345. struct dgram_sock *ro = dgram_sk(sk);
  346. int val;
  347. int err = 0;
  348. if (optlen < sizeof(int))
  349. return -EINVAL;
  350. if (get_user(val, (int __user *)optval))
  351. return -EFAULT;
  352. lock_sock(sk);
  353. switch (optname) {
  354. case WPAN_WANTACK:
  355. ro->want_ack = !!val;
  356. break;
  357. default:
  358. err = -ENOPROTOOPT;
  359. break;
  360. }
  361. release_sock(sk);
  362. return err;
  363. }
  364. struct proto ieee802154_dgram_prot = {
  365. .name = "IEEE-802.15.4-MAC",
  366. .owner = THIS_MODULE,
  367. .obj_size = sizeof(struct dgram_sock),
  368. .init = dgram_init,
  369. .close = dgram_close,
  370. .bind = dgram_bind,
  371. .sendmsg = dgram_sendmsg,
  372. .recvmsg = dgram_recvmsg,
  373. .hash = dgram_hash,
  374. .unhash = dgram_unhash,
  375. .connect = dgram_connect,
  376. .disconnect = dgram_disconnect,
  377. .ioctl = dgram_ioctl,
  378. .getsockopt = dgram_getsockopt,
  379. .setsockopt = dgram_setsockopt,
  380. };