pep-gprs.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 <remi.denis-courmont@nokia.com>
  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 *, int);
  37. void (*old_write_space)(struct sock *);
  38. struct net_device *dev;
  39. struct sk_buff_head tx_queue;
  40. struct work_struct tx_work;
  41. spinlock_t tx_lock;
  42. unsigned tx_max;
  43. };
  44. static __be16 gprs_type_trans(struct sk_buff *skb)
  45. {
  46. const u8 *pvfc;
  47. u8 buf;
  48. pvfc = skb_header_pointer(skb, 0, 1, &buf);
  49. if (!pvfc)
  50. return htons(0);
  51. /* Look at IP version field */
  52. switch (*pvfc >> 4) {
  53. case 4:
  54. return htons(ETH_P_IP);
  55. case 6:
  56. return htons(ETH_P_IPV6);
  57. }
  58. return htons(0);
  59. }
  60. /*
  61. * Socket callbacks
  62. */
  63. static void gprs_state_change(struct sock *sk)
  64. {
  65. struct gprs_dev *gp = sk->sk_user_data;
  66. if (sk->sk_state == TCP_CLOSE_WAIT) {
  67. struct net_device *dev = gp->dev;
  68. netif_stop_queue(dev);
  69. netif_carrier_off(dev);
  70. }
  71. }
  72. static int gprs_recv(struct gprs_dev *gp, struct sk_buff *skb)
  73. {
  74. struct net_device *dev = gp->dev;
  75. int err = 0;
  76. __be16 protocol = gprs_type_trans(skb);
  77. if (!protocol) {
  78. err = -EINVAL;
  79. goto drop;
  80. }
  81. if (likely(skb_headroom(skb) & 3)) {
  82. struct sk_buff *rskb, *fs;
  83. int flen = 0;
  84. /* Phonet Pipe data header is misaligned (3 bytes),
  85. * so wrap the IP packet as a single fragment of an head-less
  86. * socket buffer. The network stack will pull what it needs,
  87. * but at least, the whole IP payload is not memcpy'd. */
  88. rskb = netdev_alloc_skb(dev, 0);
  89. if (!rskb) {
  90. err = -ENOBUFS;
  91. goto drop;
  92. }
  93. skb_shinfo(rskb)->frag_list = skb;
  94. rskb->len += skb->len;
  95. rskb->data_len += rskb->len;
  96. rskb->truesize += rskb->len;
  97. /* Avoid nested fragments */
  98. for (fs = skb_shinfo(skb)->frag_list; fs; fs = fs->next)
  99. flen += fs->len;
  100. skb->next = skb_shinfo(skb)->frag_list;
  101. skb_shinfo(skb)->frag_list = NULL;
  102. skb->len -= flen;
  103. skb->data_len -= flen;
  104. skb->truesize -= flen;
  105. skb = rskb;
  106. }
  107. skb->protocol = protocol;
  108. skb_reset_mac_header(skb);
  109. skb->dev = dev;
  110. if (likely(dev->flags & IFF_UP)) {
  111. dev->stats.rx_packets++;
  112. dev->stats.rx_bytes += skb->len;
  113. netif_rx(skb);
  114. skb = NULL;
  115. } else
  116. err = -ENODEV;
  117. drop:
  118. if (skb) {
  119. dev_kfree_skb(skb);
  120. dev->stats.rx_dropped++;
  121. }
  122. return err;
  123. }
  124. static void gprs_data_ready(struct sock *sk, int len)
  125. {
  126. struct gprs_dev *gp = sk->sk_user_data;
  127. struct sk_buff *skb;
  128. while ((skb = pep_read(sk)) != NULL) {
  129. skb_orphan(skb);
  130. gprs_recv(gp, skb);
  131. }
  132. }
  133. static void gprs_write_space(struct sock *sk)
  134. {
  135. struct gprs_dev *gp = sk->sk_user_data;
  136. struct net_device *dev = gp->dev;
  137. unsigned credits = pep_writeable(sk);
  138. spin_lock_bh(&gp->tx_lock);
  139. gp->tx_max = credits;
  140. if (credits > skb_queue_len(&gp->tx_queue) && netif_running(dev))
  141. netif_wake_queue(dev);
  142. spin_unlock_bh(&gp->tx_lock);
  143. }
  144. /*
  145. * Network device callbacks
  146. */
  147. static int gprs_open(struct net_device *dev)
  148. {
  149. struct gprs_dev *gp = netdev_priv(dev);
  150. gprs_write_space(gp->sk);
  151. return 0;
  152. }
  153. static int gprs_close(struct net_device *dev)
  154. {
  155. struct gprs_dev *gp = netdev_priv(dev);
  156. netif_stop_queue(dev);
  157. flush_work(&gp->tx_work);
  158. return 0;
  159. }
  160. static int gprs_xmit(struct sk_buff *skb, struct net_device *dev)
  161. {
  162. struct gprs_dev *gp = netdev_priv(dev);
  163. switch (skb->protocol) {
  164. case htons(ETH_P_IP):
  165. case htons(ETH_P_IPV6):
  166. break;
  167. default:
  168. dev_kfree_skb(skb);
  169. return 0;
  170. }
  171. spin_lock(&gp->tx_lock);
  172. if (likely(skb_queue_len(&gp->tx_queue) < gp->tx_max)) {
  173. skb_queue_tail(&gp->tx_queue, skb);
  174. skb = NULL;
  175. }
  176. if (skb_queue_len(&gp->tx_queue) >= gp->tx_max)
  177. netif_stop_queue(dev);
  178. spin_unlock(&gp->tx_lock);
  179. schedule_work(&gp->tx_work);
  180. if (unlikely(skb))
  181. dev_kfree_skb(skb);
  182. return 0;
  183. }
  184. static void gprs_tx(struct work_struct *work)
  185. {
  186. struct gprs_dev *gp = container_of(work, struct gprs_dev, tx_work);
  187. struct net_device *dev = gp->dev;
  188. struct sock *sk = gp->sk;
  189. struct sk_buff *skb;
  190. while ((skb = skb_dequeue(&gp->tx_queue)) != NULL) {
  191. int err;
  192. dev->stats.tx_bytes += skb->len;
  193. dev->stats.tx_packets++;
  194. skb_orphan(skb);
  195. skb_set_owner_w(skb, sk);
  196. lock_sock(sk);
  197. err = pep_write(sk, skb);
  198. if (err) {
  199. LIMIT_NETDEBUG(KERN_WARNING"%s: TX error (%d)\n",
  200. dev->name, err);
  201. dev->stats.tx_aborted_errors++;
  202. dev->stats.tx_errors++;
  203. }
  204. release_sock(sk);
  205. }
  206. lock_sock(sk);
  207. gprs_write_space(sk);
  208. release_sock(sk);
  209. }
  210. static int gprs_set_mtu(struct net_device *dev, int new_mtu)
  211. {
  212. if ((new_mtu < 576) || (new_mtu > (PHONET_MAX_MTU - 11)))
  213. return -EINVAL;
  214. dev->mtu = new_mtu;
  215. return 0;
  216. }
  217. static void gprs_setup(struct net_device *dev)
  218. {
  219. dev->features = NETIF_F_FRAGLIST;
  220. dev->type = ARPHRD_NONE;
  221. dev->flags = IFF_POINTOPOINT | IFF_NOARP;
  222. dev->mtu = GPRS_DEFAULT_MTU;
  223. dev->hard_header_len = 0;
  224. dev->addr_len = 0;
  225. dev->tx_queue_len = 10;
  226. dev->destructor = free_netdev;
  227. dev->open = gprs_open;
  228. dev->stop = gprs_close;
  229. dev->hard_start_xmit = gprs_xmit; /* mandatory */
  230. dev->change_mtu = gprs_set_mtu;
  231. }
  232. /*
  233. * External interface
  234. */
  235. /*
  236. * Attach a GPRS interface to a datagram socket.
  237. * Returns the interface index on success, negative error code on error.
  238. */
  239. int gprs_attach(struct sock *sk)
  240. {
  241. static const char ifname[] = "gprs%d";
  242. struct gprs_dev *gp;
  243. struct net_device *dev;
  244. int err;
  245. if (unlikely(sk->sk_type == SOCK_STREAM))
  246. return -EINVAL; /* need packet boundaries */
  247. /* Create net device */
  248. dev = alloc_netdev(sizeof(*gp), ifname, gprs_setup);
  249. if (!dev)
  250. return -ENOMEM;
  251. gp = netdev_priv(dev);
  252. gp->dev = dev;
  253. gp->tx_max = 0;
  254. spin_lock_init(&gp->tx_lock);
  255. skb_queue_head_init(&gp->tx_queue);
  256. INIT_WORK(&gp->tx_work, gprs_tx);
  257. netif_stop_queue(dev);
  258. err = register_netdev(dev);
  259. if (err) {
  260. free_netdev(dev);
  261. return err;
  262. }
  263. lock_sock(sk);
  264. if (unlikely(sk->sk_user_data)) {
  265. err = -EBUSY;
  266. goto out_rel;
  267. }
  268. if (unlikely((1 << sk->sk_state & (TCPF_CLOSE|TCPF_LISTEN)) ||
  269. sock_flag(sk, SOCK_DEAD))) {
  270. err = -EINVAL;
  271. goto out_rel;
  272. }
  273. sk->sk_user_data = gp;
  274. gp->old_state_change = sk->sk_state_change;
  275. gp->old_data_ready = sk->sk_data_ready;
  276. gp->old_write_space = sk->sk_write_space;
  277. sk->sk_state_change = gprs_state_change;
  278. sk->sk_data_ready = gprs_data_ready;
  279. sk->sk_write_space = gprs_write_space;
  280. release_sock(sk);
  281. sock_hold(sk);
  282. gp->sk = sk;
  283. printk(KERN_DEBUG"%s: attached\n", dev->name);
  284. return dev->ifindex;
  285. out_rel:
  286. release_sock(sk);
  287. unregister_netdev(dev);
  288. return err;
  289. }
  290. void gprs_detach(struct sock *sk)
  291. {
  292. struct gprs_dev *gp = sk->sk_user_data;
  293. struct net_device *dev = gp->dev;
  294. lock_sock(sk);
  295. sk->sk_user_data = NULL;
  296. sk->sk_state_change = gp->old_state_change;
  297. sk->sk_data_ready = gp->old_data_ready;
  298. sk->sk_write_space = gp->old_write_space;
  299. release_sock(sk);
  300. printk(KERN_DEBUG"%s: detached\n", dev->name);
  301. unregister_netdev(dev);
  302. sock_put(sk);
  303. }