af_phonet.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * File: af_phonet.c
  3. *
  4. * Phonet protocols family
  5. *
  6. * Copyright (C) 2008 Nokia Corporation.
  7. *
  8. * Contact: Remi Denis-Courmont <remi.denis-courmont@nokia.com>
  9. * Original author: Sakari Ailus <sakari.ailus@nokia.com>
  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/module.h>
  27. #include <asm/unaligned.h>
  28. #include <net/sock.h>
  29. #include <linux/if_phonet.h>
  30. #include <linux/phonet.h>
  31. #include <net/phonet/phonet.h>
  32. #include <net/phonet/pn_dev.h>
  33. static struct net_proto_family phonet_proto_family;
  34. static struct phonet_protocol *phonet_proto_get(int protocol);
  35. static inline void phonet_proto_put(struct phonet_protocol *pp);
  36. /* protocol family functions */
  37. static int pn_socket_create(struct net *net, struct socket *sock, int protocol)
  38. {
  39. struct sock *sk;
  40. struct pn_sock *pn;
  41. struct phonet_protocol *pnp;
  42. int err;
  43. if (net != &init_net)
  44. return -EAFNOSUPPORT;
  45. if (!capable(CAP_SYS_ADMIN))
  46. return -EPERM;
  47. if (protocol == 0) {
  48. /* Default protocol selection */
  49. switch (sock->type) {
  50. case SOCK_DGRAM:
  51. protocol = PN_PROTO_PHONET;
  52. break;
  53. default:
  54. return -EPROTONOSUPPORT;
  55. }
  56. }
  57. pnp = phonet_proto_get(protocol);
  58. if (pnp == NULL)
  59. return -EPROTONOSUPPORT;
  60. if (sock->type != pnp->sock_type) {
  61. err = -EPROTONOSUPPORT;
  62. goto out;
  63. }
  64. sk = sk_alloc(net, PF_PHONET, GFP_KERNEL, pnp->prot);
  65. if (sk == NULL) {
  66. err = -ENOMEM;
  67. goto out;
  68. }
  69. sock_init_data(sock, sk);
  70. sock->state = SS_UNCONNECTED;
  71. sock->ops = pnp->ops;
  72. sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
  73. sk->sk_protocol = protocol;
  74. pn = pn_sk(sk);
  75. pn->sobject = 0;
  76. pn->resource = 0;
  77. sk->sk_prot->init(sk);
  78. err = 0;
  79. out:
  80. phonet_proto_put(pnp);
  81. return err;
  82. }
  83. static struct net_proto_family phonet_proto_family = {
  84. .family = AF_PHONET,
  85. .create = pn_socket_create,
  86. .owner = THIS_MODULE,
  87. };
  88. /* packet type functions */
  89. /*
  90. * Stuff received packets to associated sockets.
  91. * On error, returns non-zero and releases the skb.
  92. */
  93. static int phonet_rcv(struct sk_buff *skb, struct net_device *dev,
  94. struct packet_type *pkttype,
  95. struct net_device *orig_dev)
  96. {
  97. struct phonethdr *ph;
  98. struct sock *sk;
  99. struct sockaddr_pn sa;
  100. u16 len;
  101. if (dev_net(dev) != &init_net)
  102. goto out;
  103. /* check we have at least a full Phonet header */
  104. if (!pskb_pull(skb, sizeof(struct phonethdr)))
  105. goto out;
  106. /* check that the advertised length is correct */
  107. ph = pn_hdr(skb);
  108. len = get_unaligned_be16(&ph->pn_length);
  109. if (len < 2)
  110. goto out;
  111. len -= 2;
  112. if ((len > skb->len) || pskb_trim(skb, len))
  113. goto out;
  114. skb_reset_transport_header(skb);
  115. pn_skb_get_dst_sockaddr(skb, &sa);
  116. if (pn_sockaddr_get_addr(&sa) == 0)
  117. goto out; /* currently, we cannot be device 0 */
  118. sk = pn_find_sock_by_sa(&sa);
  119. if (sk == NULL)
  120. goto out;
  121. /* Push data to the socket (or other sockets connected to it). */
  122. return sk_receive_skb(sk, skb, 0);
  123. out:
  124. kfree_skb(skb);
  125. return NET_RX_DROP;
  126. }
  127. static struct packet_type phonet_packet_type = {
  128. .type = __constant_htons(ETH_P_PHONET),
  129. .dev = NULL,
  130. .func = phonet_rcv,
  131. };
  132. /* Transport protocol registration */
  133. static struct phonet_protocol *proto_tab[PHONET_NPROTO] __read_mostly;
  134. static DEFINE_SPINLOCK(proto_tab_lock);
  135. int __init_or_module phonet_proto_register(int protocol,
  136. struct phonet_protocol *pp)
  137. {
  138. int err = 0;
  139. if (protocol >= PHONET_NPROTO)
  140. return -EINVAL;
  141. err = proto_register(pp->prot, 1);
  142. if (err)
  143. return err;
  144. spin_lock(&proto_tab_lock);
  145. if (proto_tab[protocol])
  146. err = -EBUSY;
  147. else
  148. proto_tab[protocol] = pp;
  149. spin_unlock(&proto_tab_lock);
  150. return err;
  151. }
  152. EXPORT_SYMBOL(phonet_proto_register);
  153. void phonet_proto_unregister(int protocol, struct phonet_protocol *pp)
  154. {
  155. spin_lock(&proto_tab_lock);
  156. BUG_ON(proto_tab[protocol] != pp);
  157. proto_tab[protocol] = NULL;
  158. spin_unlock(&proto_tab_lock);
  159. proto_unregister(pp->prot);
  160. }
  161. EXPORT_SYMBOL(phonet_proto_unregister);
  162. static struct phonet_protocol *phonet_proto_get(int protocol)
  163. {
  164. struct phonet_protocol *pp;
  165. if (protocol >= PHONET_NPROTO)
  166. return NULL;
  167. spin_lock(&proto_tab_lock);
  168. pp = proto_tab[protocol];
  169. if (pp && !try_module_get(pp->prot->owner))
  170. pp = NULL;
  171. spin_unlock(&proto_tab_lock);
  172. return pp;
  173. }
  174. static inline void phonet_proto_put(struct phonet_protocol *pp)
  175. {
  176. module_put(pp->prot->owner);
  177. }
  178. /* Module registration */
  179. static int __init phonet_init(void)
  180. {
  181. int err;
  182. err = sock_register(&phonet_proto_family);
  183. if (err) {
  184. printk(KERN_ALERT
  185. "phonet protocol family initialization failed\n");
  186. return err;
  187. }
  188. phonet_device_init();
  189. dev_add_pack(&phonet_packet_type);
  190. phonet_netlink_register();
  191. return 0;
  192. }
  193. static void __exit phonet_exit(void)
  194. {
  195. sock_unregister(AF_PHONET);
  196. dev_remove_pack(&phonet_packet_type);
  197. phonet_device_exit();
  198. }
  199. module_init(phonet_init);
  200. module_exit(phonet_exit);
  201. MODULE_DESCRIPTION("Phonet protocol stack for Linux");
  202. MODULE_LICENSE("GPL");