pep-gprs.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 int gprs_set_mtu(struct net_device *dev, int new_mtu)
  186. {
  187. if ((new_mtu < 576) || (new_mtu > (PHONET_MAX_MTU - 11)))
  188. return -EINVAL;
  189. dev->mtu = new_mtu;
  190. return 0;
  191. }
  192. static const struct net_device_ops gprs_netdev_ops = {
  193. .ndo_open = gprs_open,
  194. .ndo_stop = gprs_close,
  195. .ndo_start_xmit = gprs_xmit,
  196. .ndo_change_mtu = gprs_set_mtu,
  197. };
  198. static void gprs_setup(struct net_device *dev)
  199. {
  200. dev->features = NETIF_F_FRAGLIST;
  201. dev->type = ARPHRD_PHONET_PIPE;
  202. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  203. dev->mtu = GPRS_DEFAULT_MTU;
  204. dev->hard_header_len = 0;
  205. dev->addr_len = 0;
  206. dev->tx_queue_len = 10;
  207. dev->netdev_ops = &gprs_netdev_ops;
  208. dev->destructor = free_netdev;
  209. }
  210. /*
  211. * External interface
  212. */
  213. /*
  214. * Attach a GPRS interface to a datagram socket.
  215. * Returns the interface index on success, negative error code on error.
  216. */
  217. int gprs_attach(struct sock *sk)
  218. {
  219. static const char ifname[] = "gprs%d";
  220. struct gprs_dev *gp;
  221. struct net_device *dev;
  222. int err;
  223. if (unlikely(sk->sk_type == SOCK_STREAM))
  224. return -EINVAL; /* need packet boundaries */
  225. /* Create net device */
  226. dev = alloc_netdev(sizeof(*gp), ifname, NET_NAME_UNKNOWN, gprs_setup);
  227. if (!dev)
  228. return -ENOMEM;
  229. gp = netdev_priv(dev);
  230. gp->sk = sk;
  231. gp->dev = dev;
  232. netif_stop_queue(dev);
  233. err = register_netdev(dev);
  234. if (err) {
  235. free_netdev(dev);
  236. return err;
  237. }
  238. lock_sock(sk);
  239. if (unlikely(sk->sk_user_data)) {
  240. err = -EBUSY;
  241. goto out_rel;
  242. }
  243. if (unlikely((1 << sk->sk_state & (TCPF_CLOSE|TCPF_LISTEN)) ||
  244. sock_flag(sk, SOCK_DEAD))) {
  245. err = -EINVAL;
  246. goto out_rel;
  247. }
  248. sk->sk_user_data = gp;
  249. gp->old_state_change = sk->sk_state_change;
  250. gp->old_data_ready = sk->sk_data_ready;
  251. gp->old_write_space = sk->sk_write_space;
  252. sk->sk_state_change = gprs_state_change;
  253. sk->sk_data_ready = gprs_data_ready;
  254. sk->sk_write_space = gprs_write_space;
  255. release_sock(sk);
  256. sock_hold(sk);
  257. printk(KERN_DEBUG"%s: attached\n", dev->name);
  258. return dev->ifindex;
  259. out_rel:
  260. release_sock(sk);
  261. unregister_netdev(dev);
  262. return err;
  263. }
  264. void gprs_detach(struct sock *sk)
  265. {
  266. struct gprs_dev *gp = sk->sk_user_data;
  267. struct net_device *dev = gp->dev;
  268. lock_sock(sk);
  269. sk->sk_user_data = NULL;
  270. sk->sk_state_change = gp->old_state_change;
  271. sk->sk_data_ready = gp->old_data_ready;
  272. sk->sk_write_space = gp->old_write_space;
  273. release_sock(sk);
  274. printk(KERN_DEBUG"%s: detached\n", dev->name);
  275. unregister_netdev(dev);
  276. sock_put(sk);
  277. }