raw.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Raw IEEE 802.15.4 sockets
  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 "af802154.h"
  31. static HLIST_HEAD(raw_head);
  32. static DEFINE_RWLOCK(raw_lock);
  33. static void raw_hash(struct sock *sk)
  34. {
  35. write_lock_bh(&raw_lock);
  36. sk_add_node(sk, &raw_head);
  37. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  38. write_unlock_bh(&raw_lock);
  39. }
  40. static void raw_unhash(struct sock *sk)
  41. {
  42. write_lock_bh(&raw_lock);
  43. if (sk_del_node_init(sk))
  44. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  45. write_unlock_bh(&raw_lock);
  46. }
  47. static void raw_close(struct sock *sk, long timeout)
  48. {
  49. sk_common_release(sk);
  50. }
  51. static int raw_bind(struct sock *sk, struct sockaddr *uaddr, int len)
  52. {
  53. struct sockaddr_ieee802154 *addr = (struct sockaddr_ieee802154 *)uaddr;
  54. int err = 0;
  55. struct net_device *dev = NULL;
  56. if (len < sizeof(*addr))
  57. return -EINVAL;
  58. if (addr->family != AF_IEEE802154)
  59. return -EINVAL;
  60. lock_sock(sk);
  61. dev = ieee802154_get_dev(sock_net(sk), &addr->addr);
  62. if (!dev) {
  63. err = -ENODEV;
  64. goto out;
  65. }
  66. if (dev->type != ARPHRD_IEEE802154) {
  67. err = -ENODEV;
  68. goto out_put;
  69. }
  70. sk->sk_bound_dev_if = dev->ifindex;
  71. sk_dst_reset(sk);
  72. out_put:
  73. dev_put(dev);
  74. out:
  75. release_sock(sk);
  76. return err;
  77. }
  78. static int raw_connect(struct sock *sk, struct sockaddr *uaddr,
  79. int addr_len)
  80. {
  81. return -ENOTSUPP;
  82. }
  83. static int raw_disconnect(struct sock *sk, int flags)
  84. {
  85. return 0;
  86. }
  87. static int raw_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  88. size_t size)
  89. {
  90. struct net_device *dev;
  91. unsigned mtu;
  92. struct sk_buff *skb;
  93. int err;
  94. if (msg->msg_flags & MSG_OOB) {
  95. pr_debug("msg->msg_flags = 0x%x\n", msg->msg_flags);
  96. return -EOPNOTSUPP;
  97. }
  98. lock_sock(sk);
  99. if (!sk->sk_bound_dev_if)
  100. dev = dev_getfirstbyhwtype(sock_net(sk), ARPHRD_IEEE802154);
  101. else
  102. dev = dev_get_by_index(sock_net(sk), sk->sk_bound_dev_if);
  103. release_sock(sk);
  104. if (!dev) {
  105. pr_debug("no dev\n");
  106. err = -ENXIO;
  107. goto out;
  108. }
  109. mtu = dev->mtu;
  110. pr_debug("name = %s, mtu = %u\n", dev->name, mtu);
  111. if (size > mtu) {
  112. pr_debug("size = %Zu, mtu = %u\n", size, mtu);
  113. err = -EINVAL;
  114. goto out_dev;
  115. }
  116. skb = sock_alloc_send_skb(sk, LL_ALLOCATED_SPACE(dev) + size,
  117. msg->msg_flags & MSG_DONTWAIT, &err);
  118. if (!skb)
  119. goto out_dev;
  120. skb_reserve(skb, LL_RESERVED_SPACE(dev));
  121. skb_reset_mac_header(skb);
  122. skb_reset_network_header(skb);
  123. err = memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size);
  124. if (err < 0)
  125. goto out_skb;
  126. skb->dev = dev;
  127. skb->sk = sk;
  128. skb->protocol = htons(ETH_P_IEEE802154);
  129. dev_put(dev);
  130. err = dev_queue_xmit(skb);
  131. if (err > 0)
  132. err = net_xmit_errno(err);
  133. return err ?: size;
  134. out_skb:
  135. kfree_skb(skb);
  136. out_dev:
  137. dev_put(dev);
  138. out:
  139. return err;
  140. }
  141. static int raw_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  142. size_t len, int noblock, int flags, int *addr_len)
  143. {
  144. size_t copied = 0;
  145. int err = -EOPNOTSUPP;
  146. struct sk_buff *skb;
  147. skb = skb_recv_datagram(sk, flags, noblock, &err);
  148. if (!skb)
  149. goto out;
  150. copied = skb->len;
  151. if (len < copied) {
  152. msg->msg_flags |= MSG_TRUNC;
  153. copied = len;
  154. }
  155. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  156. if (err)
  157. goto done;
  158. sock_recv_ts_and_drops(msg, sk, skb);
  159. if (flags & MSG_TRUNC)
  160. copied = skb->len;
  161. done:
  162. skb_free_datagram(sk, skb);
  163. out:
  164. if (err)
  165. return err;
  166. return copied;
  167. }
  168. static int raw_rcv_skb(struct sock *sk, struct sk_buff *skb)
  169. {
  170. if (sock_queue_rcv_skb(sk, skb) < 0) {
  171. kfree_skb(skb);
  172. return NET_RX_DROP;
  173. }
  174. return NET_RX_SUCCESS;
  175. }
  176. void ieee802154_raw_deliver(struct net_device *dev, struct sk_buff *skb)
  177. {
  178. struct sock *sk;
  179. struct hlist_node *node;
  180. read_lock(&raw_lock);
  181. sk_for_each(sk, node, &raw_head) {
  182. bh_lock_sock(sk);
  183. if (!sk->sk_bound_dev_if ||
  184. sk->sk_bound_dev_if == dev->ifindex) {
  185. struct sk_buff *clone;
  186. clone = skb_clone(skb, GFP_ATOMIC);
  187. if (clone)
  188. raw_rcv_skb(sk, clone);
  189. }
  190. bh_unlock_sock(sk);
  191. }
  192. read_unlock(&raw_lock);
  193. }
  194. static int raw_getsockopt(struct sock *sk, int level, int optname,
  195. char __user *optval, int __user *optlen)
  196. {
  197. return -EOPNOTSUPP;
  198. }
  199. static int raw_setsockopt(struct sock *sk, int level, int optname,
  200. char __user *optval, unsigned int optlen)
  201. {
  202. return -EOPNOTSUPP;
  203. }
  204. struct proto ieee802154_raw_prot = {
  205. .name = "IEEE-802.15.4-RAW",
  206. .owner = THIS_MODULE,
  207. .obj_size = sizeof(struct sock),
  208. .close = raw_close,
  209. .bind = raw_bind,
  210. .sendmsg = raw_sendmsg,
  211. .recvmsg = raw_recvmsg,
  212. .hash = raw_hash,
  213. .unhash = raw_unhash,
  214. .connect = raw_connect,
  215. .disconnect = raw_disconnect,
  216. .getsockopt = raw_getsockopt,
  217. .setsockopt = raw_setsockopt,
  218. };