dgram.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. skb = skb_recv_datagram(sk, flags, noblock, &err);
  237. if (!skb)
  238. goto out;
  239. copied = skb->len;
  240. if (len < copied) {
  241. msg->msg_flags |= MSG_TRUNC;
  242. copied = len;
  243. }
  244. /* FIXME: skip headers if necessary ?! */
  245. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  246. if (err)
  247. goto done;
  248. sock_recv_ts_and_drops(msg, sk, skb);
  249. if (flags & MSG_TRUNC)
  250. copied = skb->len;
  251. done:
  252. skb_free_datagram(sk, skb);
  253. out:
  254. if (err)
  255. return err;
  256. return copied;
  257. }
  258. static int dgram_rcv_skb(struct sock *sk, struct sk_buff *skb)
  259. {
  260. if (sock_queue_rcv_skb(sk, skb) < 0) {
  261. kfree_skb(skb);
  262. return NET_RX_DROP;
  263. }
  264. return NET_RX_SUCCESS;
  265. }
  266. static inline int ieee802154_match_sock(u8 *hw_addr, u16 pan_id,
  267. u16 short_addr, struct dgram_sock *ro)
  268. {
  269. if (!ro->bound)
  270. return 1;
  271. if (ro->src_addr.addr_type == IEEE802154_ADDR_LONG &&
  272. !memcmp(ro->src_addr.hwaddr, hw_addr, IEEE802154_ADDR_LEN))
  273. return 1;
  274. if (ro->src_addr.addr_type == IEEE802154_ADDR_SHORT &&
  275. pan_id == ro->src_addr.pan_id &&
  276. short_addr == ro->src_addr.short_addr)
  277. return 1;
  278. return 0;
  279. }
  280. int ieee802154_dgram_deliver(struct net_device *dev, struct sk_buff *skb)
  281. {
  282. struct sock *sk, *prev = NULL;
  283. struct hlist_node *node;
  284. int ret = NET_RX_SUCCESS;
  285. u16 pan_id, short_addr;
  286. /* Data frame processing */
  287. BUG_ON(dev->type != ARPHRD_IEEE802154);
  288. pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
  289. short_addr = ieee802154_mlme_ops(dev)->get_short_addr(dev);
  290. read_lock(&dgram_lock);
  291. sk_for_each(sk, node, &dgram_head) {
  292. if (ieee802154_match_sock(dev->dev_addr, pan_id, short_addr,
  293. dgram_sk(sk))) {
  294. if (prev) {
  295. struct sk_buff *clone;
  296. clone = skb_clone(skb, GFP_ATOMIC);
  297. if (clone)
  298. dgram_rcv_skb(prev, clone);
  299. }
  300. prev = sk;
  301. }
  302. }
  303. if (prev)
  304. dgram_rcv_skb(prev, skb);
  305. else {
  306. kfree_skb(skb);
  307. ret = NET_RX_DROP;
  308. }
  309. read_unlock(&dgram_lock);
  310. return ret;
  311. }
  312. static int dgram_getsockopt(struct sock *sk, int level, int optname,
  313. char __user *optval, int __user *optlen)
  314. {
  315. struct dgram_sock *ro = dgram_sk(sk);
  316. int val, len;
  317. if (level != SOL_IEEE802154)
  318. return -EOPNOTSUPP;
  319. if (get_user(len, optlen))
  320. return -EFAULT;
  321. len = min_t(unsigned int, len, sizeof(int));
  322. switch (optname) {
  323. case WPAN_WANTACK:
  324. val = ro->want_ack;
  325. break;
  326. default:
  327. return -ENOPROTOOPT;
  328. }
  329. if (put_user(len, optlen))
  330. return -EFAULT;
  331. if (copy_to_user(optval, &val, len))
  332. return -EFAULT;
  333. return 0;
  334. }
  335. static int dgram_setsockopt(struct sock *sk, int level, int optname,
  336. char __user *optval, unsigned int optlen)
  337. {
  338. struct dgram_sock *ro = dgram_sk(sk);
  339. int val;
  340. int err = 0;
  341. if (optlen < sizeof(int))
  342. return -EINVAL;
  343. if (get_user(val, (int __user *)optval))
  344. return -EFAULT;
  345. lock_sock(sk);
  346. switch (optname) {
  347. case WPAN_WANTACK:
  348. ro->want_ack = !!val;
  349. break;
  350. default:
  351. err = -ENOPROTOOPT;
  352. break;
  353. }
  354. release_sock(sk);
  355. return err;
  356. }
  357. struct proto ieee802154_dgram_prot = {
  358. .name = "IEEE-802.15.4-MAC",
  359. .owner = THIS_MODULE,
  360. .obj_size = sizeof(struct dgram_sock),
  361. .init = dgram_init,
  362. .close = dgram_close,
  363. .bind = dgram_bind,
  364. .sendmsg = dgram_sendmsg,
  365. .recvmsg = dgram_recvmsg,
  366. .hash = dgram_hash,
  367. .unhash = dgram_unhash,
  368. .connect = dgram_connect,
  369. .disconnect = dgram_disconnect,
  370. .ioctl = dgram_ioctl,
  371. .getsockopt = dgram_getsockopt,
  372. .setsockopt = dgram_setsockopt,
  373. };