4 * GPRS over Phonet pipe end point socket
6 * Copyright (C) 2008 Nokia Corporation.
8 * Author: RĂ©mi Denis-Courmont <remi.denis-courmont@nokia.com>
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.
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.
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
25 #include <linux/kernel.h>
26 #include <linux/netdevice.h>
27 #include <linux/if_ether.h>
28 #include <linux/if_arp.h>
31 #include <linux/if_phonet.h>
32 #include <net/tcp_states.h>
33 #include <net/phonet/gprs.h>
35 #define GPRS_DEFAULT_MTU 1400
39 void (*old_state_change)(struct sock *);
40 void (*old_data_ready)(struct sock *, int);
41 void (*old_write_space)(struct sock *);
43 struct net_device *net;
45 struct sk_buff_head tx_queue;
46 struct work_struct tx_work;
51 static __be16 gprs_type_trans(struct sk_buff *skb)
56 pvfc = skb_header_pointer(skb, 0, 1, &buf);
59 /* Look at IP version field */
62 return htons(ETH_P_IP);
64 return htons(ETH_P_IPV6);
73 static void gprs_state_change(struct sock *sk)
75 struct gprs_dev *dev = sk->sk_user_data;
77 if (sk->sk_state == TCP_CLOSE_WAIT) {
78 netif_stop_queue(dev->net);
79 netif_carrier_off(dev->net);
83 static int gprs_recv(struct gprs_dev *dev, struct sk_buff *skb)
85 struct net_device *net = dev->net;
87 __be16 protocol = gprs_type_trans(skb);
94 if (likely(skb_headroom(skb) & 3)) {
95 struct sk_buff *rskb, *fs;
98 /* Phonet Pipe data header is misaligned (3 bytes),
99 * so wrap the IP packet as a single fragment of an head-less
100 * socket buffer. The network stack will pull what it needs,
101 * but at least, the whole IP payload is not memcpy'd. */
102 rskb = netdev_alloc_skb(net, 0);
107 skb_shinfo(rskb)->frag_list = skb;
108 rskb->len += skb->len;
109 rskb->data_len += rskb->len;
110 rskb->truesize += rskb->len;
112 /* Avoid nested fragments */
113 for (fs = skb_shinfo(skb)->frag_list; fs; fs = fs->next)
115 skb->next = skb_shinfo(skb)->frag_list;
116 skb_shinfo(skb)->frag_list = NULL;
118 skb->data_len -= flen;
119 skb->truesize -= flen;
124 skb->protocol = protocol;
125 skb_reset_mac_header(skb);
128 if (likely(net->flags & IFF_UP)) {
129 net->stats.rx_packets++;
130 net->stats.rx_bytes += skb->len;
139 net->stats.rx_dropped++;
144 static void gprs_data_ready(struct sock *sk, int len)
146 struct gprs_dev *dev = sk->sk_user_data;
149 while ((skb = pep_read(sk)) != NULL) {
155 static void gprs_write_space(struct sock *sk)
157 struct gprs_dev *dev = sk->sk_user_data;
158 unsigned credits = pep_writeable(sk);
160 spin_lock_bh(&dev->tx_lock);
161 dev->tx_max = credits;
162 if (credits > skb_queue_len(&dev->tx_queue))
163 netif_wake_queue(dev->net);
164 spin_unlock_bh(&dev->tx_lock);
168 * Network device callbacks
171 static int gprs_xmit(struct sk_buff *skb, struct net_device *net)
173 struct gprs_dev *dev = netdev_priv(net);
175 switch (skb->protocol) {
176 case htons(ETH_P_IP):
177 case htons(ETH_P_IPV6):
184 spin_lock(&dev->tx_lock);
185 if (likely(skb_queue_len(&dev->tx_queue) < dev->tx_max)) {
186 skb_queue_tail(&dev->tx_queue, skb);
189 if (skb_queue_len(&dev->tx_queue) >= dev->tx_max)
190 netif_stop_queue(net);
191 spin_unlock(&dev->tx_lock);
193 schedule_work(&dev->tx_work);
199 static void gprs_tx(struct work_struct *work)
201 struct gprs_dev *dev = container_of(work, struct gprs_dev, tx_work);
202 struct net_device *net = dev->net;
203 struct sock *sk = dev->sk;
206 while ((skb = skb_dequeue(&dev->tx_queue)) != NULL) {
209 net->stats.tx_bytes += skb->len;
210 net->stats.tx_packets++;
213 skb_set_owner_w(skb, sk);
216 err = pep_write(sk, skb);
218 LIMIT_NETDEBUG(KERN_WARNING"%s: TX error (%d)\n",
220 net->stats.tx_aborted_errors++;
221 net->stats.tx_errors++;
227 gprs_write_space(sk);
231 static int gprs_set_mtu(struct net_device *net, int new_mtu)
233 if ((new_mtu < 576) || (new_mtu > (PHONET_MAX_MTU - 11)))
240 static void gprs_setup(struct net_device *net)
242 net->features = NETIF_F_FRAGLIST;
243 net->type = ARPHRD_NONE;
244 net->flags = IFF_POINTOPOINT | IFF_NOARP;
245 net->mtu = GPRS_DEFAULT_MTU;
246 net->hard_header_len = 0;
248 net->tx_queue_len = 10;
250 net->destructor = free_netdev;
251 net->hard_start_xmit = gprs_xmit; /* mandatory */
252 net->change_mtu = gprs_set_mtu;
260 * Attach a GPRS interface to a datagram socket.
261 * Returns the interface index on success, negative error code on error.
263 int gprs_attach(struct sock *sk)
265 static const char ifname[] = "gprs%d";
266 struct gprs_dev *dev;
267 struct net_device *net;
270 if (unlikely(sk->sk_type == SOCK_STREAM))
271 return -EINVAL; /* need packet boundaries */
273 /* Create net device */
274 net = alloc_netdev(sizeof(*dev), ifname, gprs_setup);
277 dev = netdev_priv(net);
280 spin_lock_init(&dev->tx_lock);
281 skb_queue_head_init(&dev->tx_queue);
282 INIT_WORK(&dev->tx_work, gprs_tx);
284 netif_stop_queue(net);
285 err = register_netdev(net);
292 if (unlikely(sk->sk_user_data)) {
296 if (unlikely((1 << sk->sk_state & (TCPF_CLOSE|TCPF_LISTEN)) ||
297 sock_flag(sk, SOCK_DEAD))) {
301 sk->sk_user_data = dev;
302 dev->old_state_change = sk->sk_state_change;
303 dev->old_data_ready = sk->sk_data_ready;
304 dev->old_write_space = sk->sk_write_space;
305 sk->sk_state_change = gprs_state_change;
306 sk->sk_data_ready = gprs_data_ready;
307 sk->sk_write_space = gprs_write_space;
313 printk(KERN_DEBUG"%s: attached\n", net->name);
314 gprs_write_space(sk); /* kick off TX */
319 unregister_netdev(net);
323 void gprs_detach(struct sock *sk)
325 struct gprs_dev *dev = sk->sk_user_data;
326 struct net_device *net = dev->net;
329 sk->sk_user_data = NULL;
330 sk->sk_state_change = dev->old_state_change;
331 sk->sk_data_ready = dev->old_data_ready;
332 sk->sk_write_space = dev->old_write_space;
335 printk(KERN_DEBUG"%s: detached\n", net->name);
336 unregister_netdev(net);
337 flush_scheduled_work();
339 skb_queue_purge(&dev->tx_queue);