Linux 4.1.16
[linux/fpc-iii.git] / net / phonet / pep-gprs.c
blobfa8237fdc57b26dfc0bbc2253f69e6f6df731afb
1 /*
2 * File: pep-gprs.c
4 * GPRS over Phonet pipe end point socket
6 * Copyright (C) 2008 Nokia Corporation.
8 * Author: RĂ©mi Denis-Courmont
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
22 * 02110-1301 USA
25 #include <linux/kernel.h>
26 #include <linux/netdevice.h>
27 #include <linux/if_ether.h>
28 #include <linux/if_arp.h>
29 #include <net/sock.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
37 struct gprs_dev {
38 struct sock *sk;
39 void (*old_state_change)(struct sock *);
40 void (*old_data_ready)(struct sock *);
41 void (*old_write_space)(struct sock *);
43 struct net_device *dev;
46 static __be16 gprs_type_trans(struct sk_buff *skb)
48 const u8 *pvfc;
49 u8 buf;
51 pvfc = skb_header_pointer(skb, 0, 1, &buf);
52 if (!pvfc)
53 return htons(0);
54 /* Look at IP version field */
55 switch (*pvfc >> 4) {
56 case 4:
57 return htons(ETH_P_IP);
58 case 6:
59 return htons(ETH_P_IPV6);
61 return htons(0);
64 static void gprs_writeable(struct gprs_dev *gp)
66 struct net_device *dev = gp->dev;
68 if (pep_writeable(gp->sk))
69 netif_wake_queue(dev);
73 * Socket callbacks
76 static void gprs_state_change(struct sock *sk)
78 struct gprs_dev *gp = sk->sk_user_data;
80 if (sk->sk_state == TCP_CLOSE_WAIT) {
81 struct net_device *dev = gp->dev;
83 netif_stop_queue(dev);
84 netif_carrier_off(dev);
88 static int gprs_recv(struct gprs_dev *gp, struct sk_buff *skb)
90 struct net_device *dev = gp->dev;
91 int err = 0;
92 __be16 protocol = gprs_type_trans(skb);
94 if (!protocol) {
95 err = -EINVAL;
96 goto drop;
99 if (skb_headroom(skb) & 3) {
100 struct sk_buff *rskb, *fs;
101 int flen = 0;
103 /* Phonet Pipe data header may be misaligned (3 bytes),
104 * so wrap the IP packet as a single fragment of an head-less
105 * socket buffer. The network stack will pull what it needs,
106 * but at least, the whole IP payload is not memcpy'd. */
107 rskb = netdev_alloc_skb(dev, 0);
108 if (!rskb) {
109 err = -ENOBUFS;
110 goto drop;
112 skb_shinfo(rskb)->frag_list = skb;
113 rskb->len += skb->len;
114 rskb->data_len += rskb->len;
115 rskb->truesize += rskb->len;
117 /* Avoid nested fragments */
118 skb_walk_frags(skb, fs)
119 flen += fs->len;
120 skb->next = skb_shinfo(skb)->frag_list;
121 skb_frag_list_init(skb);
122 skb->len -= flen;
123 skb->data_len -= flen;
124 skb->truesize -= flen;
126 skb = rskb;
129 skb->protocol = protocol;
130 skb_reset_mac_header(skb);
131 skb->dev = dev;
133 if (likely(dev->flags & IFF_UP)) {
134 dev->stats.rx_packets++;
135 dev->stats.rx_bytes += skb->len;
136 netif_rx(skb);
137 skb = NULL;
138 } else
139 err = -ENODEV;
141 drop:
142 if (skb) {
143 dev_kfree_skb(skb);
144 dev->stats.rx_dropped++;
146 return err;
149 static void gprs_data_ready(struct sock *sk)
151 struct gprs_dev *gp = sk->sk_user_data;
152 struct sk_buff *skb;
154 while ((skb = pep_read(sk)) != NULL) {
155 skb_orphan(skb);
156 gprs_recv(gp, skb);
160 static void gprs_write_space(struct sock *sk)
162 struct gprs_dev *gp = sk->sk_user_data;
164 if (netif_running(gp->dev))
165 gprs_writeable(gp);
169 * Network device callbacks
172 static int gprs_open(struct net_device *dev)
174 struct gprs_dev *gp = netdev_priv(dev);
176 gprs_writeable(gp);
177 return 0;
180 static int gprs_close(struct net_device *dev)
182 netif_stop_queue(dev);
183 return 0;
186 static netdev_tx_t gprs_xmit(struct sk_buff *skb, struct net_device *dev)
188 struct gprs_dev *gp = netdev_priv(dev);
189 struct sock *sk = gp->sk;
190 int len, err;
192 switch (skb->protocol) {
193 case htons(ETH_P_IP):
194 case htons(ETH_P_IPV6):
195 break;
196 default:
197 dev_kfree_skb(skb);
198 return NETDEV_TX_OK;
201 skb_orphan(skb);
202 skb_set_owner_w(skb, sk);
203 len = skb->len;
204 err = pep_write(sk, skb);
205 if (err) {
206 net_dbg_ratelimited("%s: TX error (%d)\n", dev->name, err);
207 dev->stats.tx_aborted_errors++;
208 dev->stats.tx_errors++;
209 } else {
210 dev->stats.tx_packets++;
211 dev->stats.tx_bytes += len;
214 netif_stop_queue(dev);
215 if (pep_writeable(sk))
216 netif_wake_queue(dev);
217 return NETDEV_TX_OK;
220 static int gprs_set_mtu(struct net_device *dev, int new_mtu)
222 if ((new_mtu < 576) || (new_mtu > (PHONET_MAX_MTU - 11)))
223 return -EINVAL;
225 dev->mtu = new_mtu;
226 return 0;
229 static const struct net_device_ops gprs_netdev_ops = {
230 .ndo_open = gprs_open,
231 .ndo_stop = gprs_close,
232 .ndo_start_xmit = gprs_xmit,
233 .ndo_change_mtu = gprs_set_mtu,
236 static void gprs_setup(struct net_device *dev)
238 dev->features = NETIF_F_FRAGLIST;
239 dev->type = ARPHRD_PHONET_PIPE;
240 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
241 dev->mtu = GPRS_DEFAULT_MTU;
242 dev->hard_header_len = 0;
243 dev->addr_len = 0;
244 dev->tx_queue_len = 10;
246 dev->netdev_ops = &gprs_netdev_ops;
247 dev->destructor = free_netdev;
251 * External interface
255 * Attach a GPRS interface to a datagram socket.
256 * Returns the interface index on success, negative error code on error.
258 int gprs_attach(struct sock *sk)
260 static const char ifname[] = "gprs%d";
261 struct gprs_dev *gp;
262 struct net_device *dev;
263 int err;
265 if (unlikely(sk->sk_type == SOCK_STREAM))
266 return -EINVAL; /* need packet boundaries */
268 /* Create net device */
269 dev = alloc_netdev(sizeof(*gp), ifname, NET_NAME_UNKNOWN, gprs_setup);
270 if (!dev)
271 return -ENOMEM;
272 gp = netdev_priv(dev);
273 gp->sk = sk;
274 gp->dev = dev;
276 netif_stop_queue(dev);
277 err = register_netdev(dev);
278 if (err) {
279 free_netdev(dev);
280 return err;
283 lock_sock(sk);
284 if (unlikely(sk->sk_user_data)) {
285 err = -EBUSY;
286 goto out_rel;
288 if (unlikely((1 << sk->sk_state & (TCPF_CLOSE|TCPF_LISTEN)) ||
289 sock_flag(sk, SOCK_DEAD))) {
290 err = -EINVAL;
291 goto out_rel;
293 sk->sk_user_data = gp;
294 gp->old_state_change = sk->sk_state_change;
295 gp->old_data_ready = sk->sk_data_ready;
296 gp->old_write_space = sk->sk_write_space;
297 sk->sk_state_change = gprs_state_change;
298 sk->sk_data_ready = gprs_data_ready;
299 sk->sk_write_space = gprs_write_space;
300 release_sock(sk);
301 sock_hold(sk);
303 printk(KERN_DEBUG"%s: attached\n", dev->name);
304 return dev->ifindex;
306 out_rel:
307 release_sock(sk);
308 unregister_netdev(dev);
309 return err;
312 void gprs_detach(struct sock *sk)
314 struct gprs_dev *gp = sk->sk_user_data;
315 struct net_device *dev = gp->dev;
317 lock_sock(sk);
318 sk->sk_user_data = NULL;
319 sk->sk_state_change = gp->old_state_change;
320 sk->sk_data_ready = gp->old_data_ready;
321 sk->sk_write_space = gp->old_write_space;
322 release_sock(sk);
324 printk(KERN_DEBUG"%s: detached\n", dev->name);
325 unregister_netdev(dev);
326 sock_put(sk);