pep-gprs.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * File: pep-gprs.c
  3. *
  4. * GPRS over Phonet pipe end point socket
  5. *
  6. * Copyright (C) 2008 Nokia Corporation.
  7. *
  8. * Author: Rémi Denis-Courmont
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * version 2 as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  22. * 02110-1301 USA
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/if_ether.h>
  27. #include <linux/if_arp.h>
  28. #include <net/sock.h>
  29. #include <linux/if_phonet.h>
  30. #include <net/tcp_states.h>
  31. #include <net/phonet/gprs.h>
  32. #define GPRS_DEFAULT_MTU 1400
  33. struct gprs_dev {
  34. struct sock *sk;
  35. void (*old_state_change)(struct sock *);
  36. void (*old_data_ready)(struct sock *);
  37. void (*old_write_space)(struct sock *);
  38. struct net_device *dev;
  39. };
  40. static __be16 gprs_type_trans(struct sk_buff *skb)
  41. {
  42. const u8 *pvfc;
  43. u8 buf;
  44. pvfc = skb_header_pointer(skb, 0, 1, &buf);
  45. if (!pvfc)
  46. return htons(0);
  47. /* Look at IP version field */
  48. switch (*pvfc >> 4) {
  49. case 4:
  50. return htons(ETH_P_IP);
  51. case 6:
  52. return htons(ETH_P_IPV6);
  53. }
  54. return htons(0);
  55. }
  56. static void gprs_writeable(struct gprs_dev *gp)
  57. {
  58. struct net_device *dev = gp->dev;
  59. if (pep_writeable(gp->sk))
  60. netif_wake_queue(dev);
  61. }
  62. /*
  63. * Socket callbacks
  64. */
  65. static void gprs_state_change(struct sock *sk)
  66. {
  67. struct gprs_dev *gp = sk->sk_user_data;
  68. if (sk->sk_state == TCP_CLOSE_WAIT) {
  69. struct net_device *dev = gp->dev;
  70. netif_stop_queue(dev);
  71. netif_carrier_off(dev);
  72. }
  73. }
  74. static int gprs_recv(struct gprs_dev *gp, struct sk_buff *skb)
  75. {
  76. struct net_device *dev = gp->dev;
  77. int err = 0;
  78. __be16 protocol = gprs_type_trans(skb);
  79. if (!protocol) {
  80. err = -EINVAL;
  81. goto drop;
  82. }
  83. if (skb_headroom(skb) & 3) {
  84. struct sk_buff *rskb, *fs;
  85. int flen = 0;
  86. /* Phonet Pipe data header may be misaligned (3 bytes),
  87. * so wrap the IP packet as a single fragment of an head-less
  88. * socket buffer. The network stack will pull what it needs,
  89. * but at least, the whole IP payload is not memcpy'd. */
  90. rskb = netdev_alloc_skb(dev, 0);
  91. if (!rskb) {
  92. err = -ENOBUFS;
  93. goto drop;
  94. }
  95. skb_shinfo(rskb)->frag_list = skb;
  96. rskb->len += skb->len;
  97. rskb->data_len += rskb->len;
  98. rskb->truesize += rskb->len;
  99. /* Avoid nested fragments */
  100. skb_walk_frags(skb, fs)
  101. flen += fs->len;
  102. skb->next = skb_shinfo(skb)->frag_list;
  103. skb_frag_list_init(skb);
  104. skb->len -= flen;
  105. skb->data_len -= flen;
  106. skb->truesize -= flen;
  107. skb = rskb;
  108. }
  109. skb->protocol = protocol;
  110. skb_reset_mac_header(skb);
  111. skb->dev = dev;
  112. if (likely(dev->flags & IFF_UP)) {
  113. dev->stats.rx_packets++;
  114. dev->stats.rx_bytes += skb->len;
  115. netif_rx(skb);
  116. skb = NULL;
  117. } else
  118. err = -ENODEV;
  119. drop:
  120. if (skb) {
  121. dev_kfree_skb(skb);
  122. dev->stats.rx_dropped++;
  123. }
  124. return err;
  125. }
  126. static void gprs_data_ready(struct sock *sk)
  127. {
  128. struct gprs_dev *gp = sk->sk_user_data;
  129. struct sk_buff *skb;
  130. while ((skb = pep_read(sk)) != NULL) {
  131. skb_orphan(skb);
  132. gprs_recv(gp, skb);
  133. }
  134. }
  135. static void gprs_write_space(struct sock *sk)
  136. {
  137. struct gprs_dev *gp = sk->sk_user_data;
  138. if (netif_running(gp->dev))
  139. gprs_writeable(gp);
  140. }
  141. /*
  142. * Network device callbacks
  143. */
  144. static int gprs_open(struct net_device *dev)
  145. {
  146. struct gprs_dev *gp = netdev_priv(dev);
  147. gprs_writeable(gp);
  148. return 0;
  149. }
  150. static int gprs_close(struct net_device *dev)
  151. {
  152. netif_stop_queue(dev);
  153. return 0;
  154. }
  155. static netdev_tx_t gprs_xmit(struct sk_buff *skb, struct net_device *dev)
  156. {
  157. struct gprs_dev *gp = netdev_priv(dev);
  158. struct sock *sk = gp->sk;
  159. int len, err;
  160. switch (skb->protocol) {
  161. case htons(ETH_P_IP):
  162. case htons(ETH_P_IPV6):
  163. break;
  164. default:
  165. dev_kfree_skb(skb);
  166. return NETDEV_TX_OK;
  167. }
  168. skb_orphan(skb);
  169. skb_set_owner_w(skb, sk);
  170. len = skb->len;
  171. err = pep_write(sk, skb);
  172. if (err) {
  173. net_dbg_ratelimited("%s: TX error (%d)\n", dev->name, err);
  174. dev->stats.tx_aborted_errors++;
  175. dev->stats.tx_errors++;
  176. } else {
  177. dev->stats.tx_packets++;
  178. dev->stats.tx_bytes += len;
  179. }
  180. netif_stop_queue(dev);
  181. if (pep_writeable(sk))
  182. netif_wake_queue(dev);
  183. return NETDEV_TX_OK;
  184. }
  185. static const struct net_device_ops gprs_netdev_ops = {
  186. .ndo_open = gprs_open,
  187. .ndo_stop = gprs_close,
  188. .ndo_start_xmit = gprs_xmit,
  189. };
  190. static void gprs_setup(struct net_device *dev)
  191. {
  192. dev->features = NETIF_F_FRAGLIST;
  193. dev->type = ARPHRD_PHONET_PIPE;
  194. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  195. dev->mtu = GPRS_DEFAULT_MTU;
  196. dev->min_mtu = 576;
  197. dev->max_mtu = (PHONET_MAX_MTU - 11);
  198. dev->hard_header_len = 0;
  199. dev->addr_len = 0;
  200. dev->tx_queue_len = 10;
  201. dev->netdev_ops = &gprs_netdev_ops;
  202. dev->needs_free_netdev = true;
  203. }
  204. /*
  205. * External interface
  206. */
  207. /*
  208. * Attach a GPRS interface to a datagram socket.
  209. * Returns the interface index on success, negative error code on error.
  210. */
  211. int gprs_attach(struct sock *sk)
  212. {
  213. static const char ifname[] = "gprs%d";
  214. struct gprs_dev *gp;
  215. struct net_device *dev;
  216. int err;
  217. if (unlikely(sk->sk_type == SOCK_STREAM))
  218. return -EINVAL; /* need packet boundaries */
  219. /* Create net device */
  220. dev = alloc_netdev(sizeof(*gp), ifname, NET_NAME_UNKNOWN, gprs_setup);
  221. if (!dev)
  222. return -ENOMEM;
  223. gp = netdev_priv(dev);
  224. gp->sk = sk;
  225. gp->dev = dev;
  226. netif_stop_queue(dev);
  227. err = register_netdev(dev);
  228. if (err) {
  229. free_netdev(dev);
  230. return err;
  231. }
  232. lock_sock(sk);
  233. if (unlikely(sk->sk_user_data)) {
  234. err = -EBUSY;
  235. goto out_rel;
  236. }
  237. if (unlikely((1 << sk->sk_state & (TCPF_CLOSE|TCPF_LISTEN)) ||
  238. sock_flag(sk, SOCK_DEAD))) {
  239. err = -EINVAL;
  240. goto out_rel;
  241. }
  242. sk->sk_user_data = gp;
  243. gp->old_state_change = sk->sk_state_change;
  244. gp->old_data_ready = sk->sk_data_ready;
  245. gp->old_write_space = sk->sk_write_space;
  246. sk->sk_state_change = gprs_state_change;
  247. sk->sk_data_ready = gprs_data_ready;
  248. sk->sk_write_space = gprs_write_space;
  249. release_sock(sk);
  250. sock_hold(sk);
  251. printk(KERN_DEBUG"%s: attached\n", dev->name);
  252. return dev->ifindex;
  253. out_rel:
  254. release_sock(sk);
  255. unregister_netdev(dev);
  256. return err;
  257. }
  258. void gprs_detach(struct sock *sk)
  259. {
  260. struct gprs_dev *gp = sk->sk_user_data;
  261. struct net_device *dev = gp->dev;
  262. lock_sock(sk);
  263. sk->sk_user_data = NULL;
  264. sk->sk_state_change = gp->old_state_change;
  265. sk->sk_data_ready = gp->old_data_ready;
  266. sk->sk_write_space = gp->old_write_space;
  267. release_sock(sk);
  268. printk(KERN_DEBUG"%s: detached\n", dev->name);
  269. unregister_netdev(dev);
  270. sock_put(sk);
  271. }