blob: 2c9337946e3038f130d198bb51d061a648ef1ed4 [file] [log] [blame]
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07001/*
2 * File: pep-gprs.c
3 *
4 * GPRS over Phonet pipe end point socket
5 *
6 * Copyright (C) 2008 Nokia Corporation.
7 *
Rémi Denis-Courmont31fdc552012-06-13 22:29:03 +00008 * Author: Rémi Denis-Courmont
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07009 *
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
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>
30
31#include <linux/if_phonet.h>
32#include <net/tcp_states.h>
33#include <net/phonet/gprs.h>
34
35#define GPRS_DEFAULT_MTU 1400
36
37struct gprs_dev {
38 struct sock *sk;
39 void (*old_state_change)(struct sock *);
David S. Miller676d2362014-04-11 16:15:36 -040040 void (*old_data_ready)(struct sock *);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -070041 void (*old_write_space)(struct sock *);
42
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -080043 struct net_device *dev;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -070044};
45
Harvey Harrison5c7f0332008-11-06 23:10:50 -080046static __be16 gprs_type_trans(struct sk_buff *skb)
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -070047{
48 const u8 *pvfc;
49 u8 buf;
50
51 pvfc = skb_header_pointer(skb, 0, 1, &buf);
52 if (!pvfc)
Harvey Harrison5c7f0332008-11-06 23:10:50 -080053 return htons(0);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -070054 /* 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);
60 }
Harvey Harrison5c7f0332008-11-06 23:10:50 -080061 return htons(0);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -070062}
63
Rémi Denis-Courmont893873f2008-12-17 15:48:50 -080064static void gprs_writeable(struct gprs_dev *gp)
65{
66 struct net_device *dev = gp->dev;
67
68 if (pep_writeable(gp->sk))
69 netif_wake_queue(dev);
70}
71
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -070072/*
73 * Socket callbacks
74 */
75
76static void gprs_state_change(struct sock *sk)
77{
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -080078 struct gprs_dev *gp = sk->sk_user_data;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -070079
80 if (sk->sk_state == TCP_CLOSE_WAIT) {
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -080081 struct net_device *dev = gp->dev;
82
83 netif_stop_queue(dev);
84 netif_carrier_off(dev);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -070085 }
86}
87
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -080088static int gprs_recv(struct gprs_dev *gp, struct sk_buff *skb)
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -070089{
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -080090 struct net_device *dev = gp->dev;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -070091 int err = 0;
Harvey Harrison5c7f0332008-11-06 23:10:50 -080092 __be16 protocol = gprs_type_trans(skb);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -070093
94 if (!protocol) {
95 err = -EINVAL;
96 goto drop;
97 }
98
Rémi Denis-Courmontfc6a1102010-01-04 02:02:47 +000099 if (skb_headroom(skb) & 3) {
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700100 struct sk_buff *rskb, *fs;
101 int flen = 0;
102
Rémi Denis-Courmontfc6a1102010-01-04 02:02:47 +0000103 /* Phonet Pipe data header may be misaligned (3 bytes),
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700104 * 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. */
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800107 rskb = netdev_alloc_skb(dev, 0);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700108 if (!rskb) {
109 err = -ENOBUFS;
110 goto drop;
111 }
112 skb_shinfo(rskb)->frag_list = skb;
113 rskb->len += skb->len;
114 rskb->data_len += rskb->len;
115 rskb->truesize += rskb->len;
116
117 /* Avoid nested fragments */
David S. Miller5c313e92009-06-09 00:21:58 -0700118 skb_walk_frags(skb, fs)
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700119 flen += fs->len;
120 skb->next = skb_shinfo(skb)->frag_list;
David S. Miller5c313e92009-06-09 00:21:58 -0700121 skb_frag_list_init(skb);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700122 skb->len -= flen;
123 skb->data_len -= flen;
124 skb->truesize -= flen;
125
126 skb = rskb;
127 }
128
129 skb->protocol = protocol;
130 skb_reset_mac_header(skb);
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800131 skb->dev = dev;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700132
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800133 if (likely(dev->flags & IFF_UP)) {
134 dev->stats.rx_packets++;
135 dev->stats.rx_bytes += skb->len;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700136 netif_rx(skb);
137 skb = NULL;
138 } else
139 err = -ENODEV;
140
141drop:
142 if (skb) {
143 dev_kfree_skb(skb);
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800144 dev->stats.rx_dropped++;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700145 }
146 return err;
147}
148
David S. Miller676d2362014-04-11 16:15:36 -0400149static void gprs_data_ready(struct sock *sk)
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700150{
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800151 struct gprs_dev *gp = sk->sk_user_data;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700152 struct sk_buff *skb;
153
154 while ((skb = pep_read(sk)) != NULL) {
155 skb_orphan(skb);
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800156 gprs_recv(gp, skb);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700157 }
158}
159
160static void gprs_write_space(struct sock *sk)
161{
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800162 struct gprs_dev *gp = sk->sk_user_data;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700163
Rémi Denis-Courmont893873f2008-12-17 15:48:50 -0800164 if (netif_running(gp->dev))
165 gprs_writeable(gp);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700166}
167
168/*
169 * Network device callbacks
170 */
171
Rémi Denis-Courmont4798a2b2008-12-15 00:53:57 -0800172static int gprs_open(struct net_device *dev)
173{
174 struct gprs_dev *gp = netdev_priv(dev);
175
Rémi Denis-Courmont893873f2008-12-17 15:48:50 -0800176 gprs_writeable(gp);
Rémi Denis-Courmont4798a2b2008-12-15 00:53:57 -0800177 return 0;
178}
179
180static int gprs_close(struct net_device *dev)
181{
Rémi Denis-Courmont4798a2b2008-12-15 00:53:57 -0800182 netif_stop_queue(dev);
Rémi Denis-Courmont4798a2b2008-12-15 00:53:57 -0800183 return 0;
184}
185
Stephen Hemminger424efe92009-08-31 19:50:51 +0000186static netdev_tx_t gprs_xmit(struct sk_buff *skb, struct net_device *dev)
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700187{
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800188 struct gprs_dev *gp = netdev_priv(dev);
Rémi Denis-Courmont893873f2008-12-17 15:48:50 -0800189 struct sock *sk = gp->sk;
190 int len, err;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700191
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);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000198 return NETDEV_TX_OK;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700199 }
200
Rémi Denis-Courmont893873f2008-12-17 15:48:50 -0800201 skb_orphan(skb);
202 skb_set_owner_w(skb, sk);
203 len = skb->len;
204 err = pep_write(sk, skb);
205 if (err) {
Joe Perchesba7a46f2014-11-11 10:59:17 -0800206 net_dbg_ratelimited("%s: TX error (%d)\n", dev->name, err);
Rémi Denis-Courmont893873f2008-12-17 15:48:50 -0800207 dev->stats.tx_aborted_errors++;
208 dev->stats.tx_errors++;
Rémi Denis-Courmont893873f2008-12-17 15:48:50 -0800209 } else {
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800210 dev->stats.tx_packets++;
Rémi Denis-Courmont893873f2008-12-17 15:48:50 -0800211 dev->stats.tx_bytes += len;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700212 }
213
Rémi Denis-Courmontbbd58982009-06-01 00:35:16 +0000214 netif_stop_queue(dev);
215 if (pep_writeable(sk))
216 netif_wake_queue(dev);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000217 return NETDEV_TX_OK;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700218}
219
Stephen Hemmingerab638e62009-01-07 17:24:34 -0800220static const struct net_device_ops gprs_netdev_ops = {
221 .ndo_open = gprs_open,
222 .ndo_stop = gprs_close,
223 .ndo_start_xmit = gprs_xmit,
Stephen Hemmingerab638e62009-01-07 17:24:34 -0800224};
225
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800226static void gprs_setup(struct net_device *dev)
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700227{
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800228 dev->features = NETIF_F_FRAGLIST;
Rémi Denis-Courmont57c81ff2008-12-17 15:47:48 -0800229 dev->type = ARPHRD_PHONET_PIPE;
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800230 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
231 dev->mtu = GPRS_DEFAULT_MTU;
Jarod Wilsonb3e38932016-10-20 13:55:22 -0400232 dev->min_mtu = 576;
233 dev->max_mtu = (PHONET_MAX_MTU - 11);
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800234 dev->hard_header_len = 0;
235 dev->addr_len = 0;
236 dev->tx_queue_len = 10;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700237
Stephen Hemmingerab638e62009-01-07 17:24:34 -0800238 dev->netdev_ops = &gprs_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -0400239 dev->needs_free_netdev = true;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700240}
241
242/*
243 * External interface
244 */
245
246/*
247 * Attach a GPRS interface to a datagram socket.
248 * Returns the interface index on success, negative error code on error.
249 */
250int gprs_attach(struct sock *sk)
251{
252 static const char ifname[] = "gprs%d";
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800253 struct gprs_dev *gp;
254 struct net_device *dev;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700255 int err;
256
257 if (unlikely(sk->sk_type == SOCK_STREAM))
258 return -EINVAL; /* need packet boundaries */
259
260 /* Create net device */
Tom Gundersenc835a672014-07-14 16:37:24 +0200261 dev = alloc_netdev(sizeof(*gp), ifname, NET_NAME_UNKNOWN, gprs_setup);
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800262 if (!dev)
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700263 return -ENOMEM;
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800264 gp = netdev_priv(dev);
Rémi Denis-Courmont893873f2008-12-17 15:48:50 -0800265 gp->sk = sk;
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800266 gp->dev = dev;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700267
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800268 netif_stop_queue(dev);
269 err = register_netdev(dev);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700270 if (err) {
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800271 free_netdev(dev);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700272 return err;
273 }
274
275 lock_sock(sk);
276 if (unlikely(sk->sk_user_data)) {
277 err = -EBUSY;
278 goto out_rel;
279 }
280 if (unlikely((1 << sk->sk_state & (TCPF_CLOSE|TCPF_LISTEN)) ||
281 sock_flag(sk, SOCK_DEAD))) {
282 err = -EINVAL;
283 goto out_rel;
284 }
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800285 sk->sk_user_data = gp;
286 gp->old_state_change = sk->sk_state_change;
287 gp->old_data_ready = sk->sk_data_ready;
288 gp->old_write_space = sk->sk_write_space;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700289 sk->sk_state_change = gprs_state_change;
290 sk->sk_data_ready = gprs_data_ready;
291 sk->sk_write_space = gprs_write_space;
292 release_sock(sk);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700293 sock_hold(sk);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700294
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800295 printk(KERN_DEBUG"%s: attached\n", dev->name);
296 return dev->ifindex;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700297
298out_rel:
299 release_sock(sk);
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800300 unregister_netdev(dev);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700301 return err;
302}
303
304void gprs_detach(struct sock *sk)
305{
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800306 struct gprs_dev *gp = sk->sk_user_data;
307 struct net_device *dev = gp->dev;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700308
309 lock_sock(sk);
310 sk->sk_user_data = NULL;
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800311 sk->sk_state_change = gp->old_state_change;
312 sk->sk_data_ready = gp->old_data_ready;
313 sk->sk_write_space = gp->old_write_space;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700314 release_sock(sk);
315
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800316 printk(KERN_DEBUG"%s: detached\n", dev->name);
317 unregister_netdev(dev);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700318 sock_put(sk);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700319}