datagram.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * File: datagram.c
  3. *
  4. * Datagram (ISI) Phonet sockets
  5. *
  6. * Copyright (C) 2008 Nokia Corporation.
  7. *
  8. * Authors: Sakari Ailus <sakari.ailus@nokia.com>
  9. * Rémi Denis-Courmont
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * version 2 as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  23. * 02110-1301 USA
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/slab.h>
  27. #include <linux/socket.h>
  28. #include <asm/ioctls.h>
  29. #include <net/sock.h>
  30. #include <linux/phonet.h>
  31. #include <linux/export.h>
  32. #include <net/phonet/phonet.h>
  33. static int pn_backlog_rcv(struct sock *sk, struct sk_buff *skb);
  34. /* associated socket ceases to exist */
  35. static void pn_sock_close(struct sock *sk, long timeout)
  36. {
  37. sk_common_release(sk);
  38. }
  39. static int pn_ioctl(struct sock *sk, int cmd, unsigned long arg)
  40. {
  41. struct sk_buff *skb;
  42. int answ;
  43. switch (cmd) {
  44. case SIOCINQ:
  45. lock_sock(sk);
  46. skb = skb_peek(&sk->sk_receive_queue);
  47. answ = skb ? skb->len : 0;
  48. release_sock(sk);
  49. return put_user(answ, (int __user *)arg);
  50. case SIOCPNADDRESOURCE:
  51. case SIOCPNDELRESOURCE: {
  52. u32 res;
  53. if (get_user(res, (u32 __user *)arg))
  54. return -EFAULT;
  55. if (res >= 256)
  56. return -EINVAL;
  57. if (cmd == SIOCPNADDRESOURCE)
  58. return pn_sock_bind_res(sk, res);
  59. else
  60. return pn_sock_unbind_res(sk, res);
  61. }
  62. }
  63. return -ENOIOCTLCMD;
  64. }
  65. /* Destroy socket. All references are gone. */
  66. static void pn_destruct(struct sock *sk)
  67. {
  68. skb_queue_purge(&sk->sk_receive_queue);
  69. }
  70. static int pn_init(struct sock *sk)
  71. {
  72. sk->sk_destruct = pn_destruct;
  73. return 0;
  74. }
  75. static int pn_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
  76. {
  77. DECLARE_SOCKADDR(struct sockaddr_pn *, target, msg->msg_name);
  78. struct sk_buff *skb;
  79. int err;
  80. if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_NOSIGNAL|
  81. MSG_CMSG_COMPAT))
  82. return -EOPNOTSUPP;
  83. if (target == NULL)
  84. return -EDESTADDRREQ;
  85. if (msg->msg_namelen < sizeof(struct sockaddr_pn))
  86. return -EINVAL;
  87. if (target->spn_family != AF_PHONET)
  88. return -EAFNOSUPPORT;
  89. skb = sock_alloc_send_skb(sk, MAX_PHONET_HEADER + len,
  90. msg->msg_flags & MSG_DONTWAIT, &err);
  91. if (skb == NULL)
  92. return err;
  93. skb_reserve(skb, MAX_PHONET_HEADER);
  94. err = memcpy_from_msg((void *)skb_put(skb, len), msg, len);
  95. if (err < 0) {
  96. kfree_skb(skb);
  97. return err;
  98. }
  99. /*
  100. * Fill in the Phonet header and
  101. * finally pass the packet forwards.
  102. */
  103. err = pn_skb_send(sk, skb, target);
  104. /* If ok, return len. */
  105. return (err >= 0) ? len : err;
  106. }
  107. static int pn_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
  108. int noblock, int flags, int *addr_len)
  109. {
  110. struct sk_buff *skb = NULL;
  111. struct sockaddr_pn sa;
  112. int rval = -EOPNOTSUPP;
  113. int copylen;
  114. if (flags & ~(MSG_PEEK|MSG_TRUNC|MSG_DONTWAIT|MSG_NOSIGNAL|
  115. MSG_CMSG_COMPAT))
  116. goto out_nofree;
  117. skb = skb_recv_datagram(sk, flags, noblock, &rval);
  118. if (skb == NULL)
  119. goto out_nofree;
  120. pn_skb_get_src_sockaddr(skb, &sa);
  121. copylen = skb->len;
  122. if (len < copylen) {
  123. msg->msg_flags |= MSG_TRUNC;
  124. copylen = len;
  125. }
  126. rval = skb_copy_datagram_msg(skb, 0, msg, copylen);
  127. if (rval) {
  128. rval = -EFAULT;
  129. goto out;
  130. }
  131. rval = (flags & MSG_TRUNC) ? skb->len : copylen;
  132. if (msg->msg_name != NULL) {
  133. __sockaddr_check_size(sizeof(sa));
  134. memcpy(msg->msg_name, &sa, sizeof(sa));
  135. *addr_len = sizeof(sa);
  136. }
  137. out:
  138. skb_free_datagram(sk, skb);
  139. out_nofree:
  140. return rval;
  141. }
  142. /* Queue an skb for a sock. */
  143. static int pn_backlog_rcv(struct sock *sk, struct sk_buff *skb)
  144. {
  145. int err = sock_queue_rcv_skb(sk, skb);
  146. if (err < 0)
  147. kfree_skb(skb);
  148. return err ? NET_RX_DROP : NET_RX_SUCCESS;
  149. }
  150. /* Module registration */
  151. static struct proto pn_proto = {
  152. .close = pn_sock_close,
  153. .ioctl = pn_ioctl,
  154. .init = pn_init,
  155. .sendmsg = pn_sendmsg,
  156. .recvmsg = pn_recvmsg,
  157. .backlog_rcv = pn_backlog_rcv,
  158. .hash = pn_sock_hash,
  159. .unhash = pn_sock_unhash,
  160. .get_port = pn_sock_get_port,
  161. .obj_size = sizeof(struct pn_sock),
  162. .owner = THIS_MODULE,
  163. .name = "PHONET",
  164. };
  165. static struct phonet_protocol pn_dgram_proto = {
  166. .ops = &phonet_dgram_ops,
  167. .prot = &pn_proto,
  168. .sock_type = SOCK_DGRAM,
  169. };
  170. int __init isi_register(void)
  171. {
  172. return phonet_proto_register(PN_PROTO_PHONET, &pn_dgram_proto);
  173. }
  174. void __exit isi_unregister(void)
  175. {
  176. phonet_proto_unregister(PN_PROTO_PHONET, &pn_dgram_proto);
  177. }