af_phonet.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. static struct net_proto_family phonet_proto_family;
  33. static struct phonet_protocol *phonet_proto_get(int protocol);
  34. static inline void phonet_proto_put(struct phonet_protocol *pp);
  35. /* protocol family functions */
  36. static int pn_socket_create(struct net *net, struct socket *sock, int protocol)
  37. {
  38. struct phonet_protocol *pnp;
  39. int err;
  40. if (net != &init_net)
  41. return -EAFNOSUPPORT;
  42. if (!capable(CAP_SYS_ADMIN))
  43. return -EPERM;
  44. if (protocol == 0) {
  45. /* Default protocol selection */
  46. switch (sock->type) {
  47. case SOCK_DGRAM:
  48. protocol = PN_PROTO_PHONET;
  49. break;
  50. default:
  51. return -EPROTONOSUPPORT;
  52. }
  53. }
  54. pnp = phonet_proto_get(protocol);
  55. if (pnp == NULL)
  56. return -EPROTONOSUPPORT;
  57. if (sock->type != pnp->sock_type) {
  58. err = -EPROTONOSUPPORT;
  59. goto out;
  60. }
  61. /* TODO: create and init the struct sock */
  62. err = -EPROTONOSUPPORT;
  63. out:
  64. phonet_proto_put(pnp);
  65. return err;
  66. }
  67. static struct net_proto_family phonet_proto_family = {
  68. .family = AF_PHONET,
  69. .create = pn_socket_create,
  70. .owner = THIS_MODULE,
  71. };
  72. /* packet type functions */
  73. /*
  74. * Stuff received packets to associated sockets.
  75. * On error, returns non-zero and releases the skb.
  76. */
  77. static int phonet_rcv(struct sk_buff *skb, struct net_device *dev,
  78. struct packet_type *pkttype,
  79. struct net_device *orig_dev)
  80. {
  81. struct phonethdr *ph;
  82. struct sockaddr_pn sa;
  83. u16 len;
  84. if (dev_net(dev) != &init_net)
  85. goto out;
  86. /* check we have at least a full Phonet header */
  87. if (!pskb_pull(skb, sizeof(struct phonethdr)))
  88. goto out;
  89. /* check that the advertised length is correct */
  90. ph = pn_hdr(skb);
  91. len = get_unaligned_be16(&ph->pn_length);
  92. if (len < 2)
  93. goto out;
  94. len -= 2;
  95. if ((len > skb->len) || pskb_trim(skb, len))
  96. goto out;
  97. skb_reset_transport_header(skb);
  98. pn_skb_get_dst_sockaddr(skb, &sa);
  99. if (pn_sockaddr_get_addr(&sa) == 0)
  100. goto out; /* currently, we cannot be device 0 */
  101. /* TODO: put packets to sockets backlog */
  102. out:
  103. kfree_skb(skb);
  104. return NET_RX_DROP;
  105. }
  106. static struct packet_type phonet_packet_type = {
  107. .type = __constant_htons(ETH_P_PHONET),
  108. .dev = NULL,
  109. .func = phonet_rcv,
  110. };
  111. /* Transport protocol registration */
  112. static struct phonet_protocol *proto_tab[PHONET_NPROTO] __read_mostly;
  113. static DEFINE_SPINLOCK(proto_tab_lock);
  114. int __init_or_module phonet_proto_register(int protocol,
  115. struct phonet_protocol *pp)
  116. {
  117. int err = 0;
  118. if (protocol >= PHONET_NPROTO)
  119. return -EINVAL;
  120. err = proto_register(pp->prot, 1);
  121. if (err)
  122. return err;
  123. spin_lock(&proto_tab_lock);
  124. if (proto_tab[protocol])
  125. err = -EBUSY;
  126. else
  127. proto_tab[protocol] = pp;
  128. spin_unlock(&proto_tab_lock);
  129. return err;
  130. }
  131. EXPORT_SYMBOL(phonet_proto_register);
  132. void phonet_proto_unregister(int protocol, struct phonet_protocol *pp)
  133. {
  134. spin_lock(&proto_tab_lock);
  135. BUG_ON(proto_tab[protocol] != pp);
  136. proto_tab[protocol] = NULL;
  137. spin_unlock(&proto_tab_lock);
  138. proto_unregister(pp->prot);
  139. }
  140. EXPORT_SYMBOL(phonet_proto_unregister);
  141. static struct phonet_protocol *phonet_proto_get(int protocol)
  142. {
  143. struct phonet_protocol *pp;
  144. if (protocol >= PHONET_NPROTO)
  145. return NULL;
  146. spin_lock(&proto_tab_lock);
  147. pp = proto_tab[protocol];
  148. if (pp && !try_module_get(pp->prot->owner))
  149. pp = NULL;
  150. spin_unlock(&proto_tab_lock);
  151. return pp;
  152. }
  153. static inline void phonet_proto_put(struct phonet_protocol *pp)
  154. {
  155. module_put(pp->prot->owner);
  156. }
  157. /* Module registration */
  158. static int __init phonet_init(void)
  159. {
  160. int err;
  161. err = sock_register(&phonet_proto_family);
  162. if (err) {
  163. printk(KERN_ALERT
  164. "phonet protocol family initialization failed\n");
  165. return err;
  166. }
  167. dev_add_pack(&phonet_packet_type);
  168. return 0;
  169. }
  170. static void __exit phonet_exit(void)
  171. {
  172. sock_unregister(AF_PHONET);
  173. dev_remove_pack(&phonet_packet_type);
  174. }
  175. module_init(phonet_init);
  176. module_exit(phonet_exit);
  177. MODULE_DESCRIPTION("Phonet protocol stack for Linux");
  178. MODULE_LICENSE("GPL");