af_phonet.c 4.9 KB

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