blob: 1f5df0432d371f596c4757724d5e5e0566f2960c [file] [log] [blame]
Thomas Gleixner2b27bdc2019-05-29 16:57:50 -07001// SPDX-License-Identifier: GPL-2.0-only
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -07002/*
3 * File: pep-gprs.c
4 *
5 * GPRS over Phonet pipe end point socket
6 *
7 * Copyright (C) 2008 Nokia Corporation.
8 *
Rémi Denis-Courmont31fdc552012-06-13 22:29:03 +00009 * Author: Rémi Denis-Courmont
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -070010 */
11
12#include <linux/kernel.h>
13#include <linux/netdevice.h>
14#include <linux/if_ether.h>
15#include <linux/if_arp.h>
16#include <net/sock.h>
17
18#include <linux/if_phonet.h>
19#include <net/tcp_states.h>
20#include <net/phonet/gprs.h>
21
22#define GPRS_DEFAULT_MTU 1400
23
24struct gprs_dev {
25 struct sock *sk;
26 void (*old_state_change)(struct sock *);
David S. Miller676d2362014-04-11 16:15:36 -040027 void (*old_data_ready)(struct sock *);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -070028 void (*old_write_space)(struct sock *);
29
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -080030 struct net_device *dev;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -070031};
32
Harvey Harrison5c7f0332008-11-06 23:10:50 -080033static __be16 gprs_type_trans(struct sk_buff *skb)
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -070034{
35 const u8 *pvfc;
36 u8 buf;
37
38 pvfc = skb_header_pointer(skb, 0, 1, &buf);
39 if (!pvfc)
Harvey Harrison5c7f0332008-11-06 23:10:50 -080040 return htons(0);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -070041 /* Look at IP version field */
42 switch (*pvfc >> 4) {
43 case 4:
44 return htons(ETH_P_IP);
45 case 6:
46 return htons(ETH_P_IPV6);
47 }
Harvey Harrison5c7f0332008-11-06 23:10:50 -080048 return htons(0);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -070049}
50
Rémi Denis-Courmont893873f2008-12-17 15:48:50 -080051static void gprs_writeable(struct gprs_dev *gp)
52{
53 struct net_device *dev = gp->dev;
54
55 if (pep_writeable(gp->sk))
56 netif_wake_queue(dev);
57}
58
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -070059/*
60 * Socket callbacks
61 */
62
63static void gprs_state_change(struct sock *sk)
64{
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -080065 struct gprs_dev *gp = sk->sk_user_data;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -070066
67 if (sk->sk_state == TCP_CLOSE_WAIT) {
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -080068 struct net_device *dev = gp->dev;
69
70 netif_stop_queue(dev);
71 netif_carrier_off(dev);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -070072 }
73}
74
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -080075static int gprs_recv(struct gprs_dev *gp, struct sk_buff *skb)
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -070076{
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -080077 struct net_device *dev = gp->dev;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -070078 int err = 0;
Harvey Harrison5c7f0332008-11-06 23:10:50 -080079 __be16 protocol = gprs_type_trans(skb);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -070080
81 if (!protocol) {
82 err = -EINVAL;
83 goto drop;
84 }
85
Rémi Denis-Courmontfc6a1102010-01-04 02:02:47 +000086 if (skb_headroom(skb) & 3) {
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -070087 struct sk_buff *rskb, *fs;
88 int flen = 0;
89
Rémi Denis-Courmontfc6a1102010-01-04 02:02:47 +000090 /* Phonet Pipe data header may be misaligned (3 bytes),
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -070091 * so wrap the IP packet as a single fragment of an head-less
92 * socket buffer. The network stack will pull what it needs,
93 * but at least, the whole IP payload is not memcpy'd. */
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -080094 rskb = netdev_alloc_skb(dev, 0);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -070095 if (!rskb) {
96 err = -ENOBUFS;
97 goto drop;
98 }
99 skb_shinfo(rskb)->frag_list = skb;
100 rskb->len += skb->len;
101 rskb->data_len += rskb->len;
102 rskb->truesize += rskb->len;
103
104 /* Avoid nested fragments */
David S. Miller5c313e92009-06-09 00:21:58 -0700105 skb_walk_frags(skb, fs)
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700106 flen += fs->len;
107 skb->next = skb_shinfo(skb)->frag_list;
David S. Miller5c313e92009-06-09 00:21:58 -0700108 skb_frag_list_init(skb);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700109 skb->len -= flen;
110 skb->data_len -= flen;
111 skb->truesize -= flen;
112
113 skb = rskb;
114 }
115
116 skb->protocol = protocol;
117 skb_reset_mac_header(skb);
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800118 skb->dev = dev;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700119
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800120 if (likely(dev->flags & IFF_UP)) {
121 dev->stats.rx_packets++;
122 dev->stats.rx_bytes += skb->len;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700123 netif_rx(skb);
124 skb = NULL;
125 } else
126 err = -ENODEV;
127
128drop:
129 if (skb) {
130 dev_kfree_skb(skb);
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800131 dev->stats.rx_dropped++;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700132 }
133 return err;
134}
135
David S. Miller676d2362014-04-11 16:15:36 -0400136static void gprs_data_ready(struct sock *sk)
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700137{
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800138 struct gprs_dev *gp = sk->sk_user_data;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700139 struct sk_buff *skb;
140
141 while ((skb = pep_read(sk)) != NULL) {
142 skb_orphan(skb);
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800143 gprs_recv(gp, skb);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700144 }
145}
146
147static void gprs_write_space(struct sock *sk)
148{
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800149 struct gprs_dev *gp = sk->sk_user_data;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700150
Rémi Denis-Courmont893873f2008-12-17 15:48:50 -0800151 if (netif_running(gp->dev))
152 gprs_writeable(gp);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700153}
154
155/*
156 * Network device callbacks
157 */
158
Rémi Denis-Courmont4798a2b2008-12-15 00:53:57 -0800159static int gprs_open(struct net_device *dev)
160{
161 struct gprs_dev *gp = netdev_priv(dev);
162
Rémi Denis-Courmont893873f2008-12-17 15:48:50 -0800163 gprs_writeable(gp);
Rémi Denis-Courmont4798a2b2008-12-15 00:53:57 -0800164 return 0;
165}
166
167static int gprs_close(struct net_device *dev)
168{
Rémi Denis-Courmont4798a2b2008-12-15 00:53:57 -0800169 netif_stop_queue(dev);
Rémi Denis-Courmont4798a2b2008-12-15 00:53:57 -0800170 return 0;
171}
172
Stephen Hemminger424efe92009-08-31 19:50:51 +0000173static netdev_tx_t gprs_xmit(struct sk_buff *skb, struct net_device *dev)
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700174{
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800175 struct gprs_dev *gp = netdev_priv(dev);
Rémi Denis-Courmont893873f2008-12-17 15:48:50 -0800176 struct sock *sk = gp->sk;
177 int len, err;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700178
179 switch (skb->protocol) {
180 case htons(ETH_P_IP):
181 case htons(ETH_P_IPV6):
182 break;
183 default:
184 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000185 return NETDEV_TX_OK;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700186 }
187
Rémi Denis-Courmont893873f2008-12-17 15:48:50 -0800188 skb_orphan(skb);
189 skb_set_owner_w(skb, sk);
190 len = skb->len;
191 err = pep_write(sk, skb);
192 if (err) {
Joe Perchesba7a46f2014-11-11 10:59:17 -0800193 net_dbg_ratelimited("%s: TX error (%d)\n", dev->name, err);
Rémi Denis-Courmont893873f2008-12-17 15:48:50 -0800194 dev->stats.tx_aborted_errors++;
195 dev->stats.tx_errors++;
Rémi Denis-Courmont893873f2008-12-17 15:48:50 -0800196 } else {
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800197 dev->stats.tx_packets++;
Rémi Denis-Courmont893873f2008-12-17 15:48:50 -0800198 dev->stats.tx_bytes += len;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700199 }
200
Rémi Denis-Courmontbbd58982009-06-01 00:35:16 +0000201 netif_stop_queue(dev);
202 if (pep_writeable(sk))
203 netif_wake_queue(dev);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000204 return NETDEV_TX_OK;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700205}
206
Stephen Hemmingerab638e62009-01-07 17:24:34 -0800207static const struct net_device_ops gprs_netdev_ops = {
208 .ndo_open = gprs_open,
209 .ndo_stop = gprs_close,
210 .ndo_start_xmit = gprs_xmit,
Stephen Hemmingerab638e62009-01-07 17:24:34 -0800211};
212
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800213static void gprs_setup(struct net_device *dev)
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700214{
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800215 dev->features = NETIF_F_FRAGLIST;
Rémi Denis-Courmont57c81ff2008-12-17 15:47:48 -0800216 dev->type = ARPHRD_PHONET_PIPE;
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800217 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
218 dev->mtu = GPRS_DEFAULT_MTU;
Jarod Wilsonb3e38932016-10-20 13:55:22 -0400219 dev->min_mtu = 576;
220 dev->max_mtu = (PHONET_MAX_MTU - 11);
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800221 dev->hard_header_len = 0;
222 dev->addr_len = 0;
223 dev->tx_queue_len = 10;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700224
Stephen Hemmingerab638e62009-01-07 17:24:34 -0800225 dev->netdev_ops = &gprs_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -0400226 dev->needs_free_netdev = true;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700227}
228
229/*
230 * External interface
231 */
232
233/*
234 * Attach a GPRS interface to a datagram socket.
235 * Returns the interface index on success, negative error code on error.
236 */
237int gprs_attach(struct sock *sk)
238{
239 static const char ifname[] = "gprs%d";
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800240 struct gprs_dev *gp;
241 struct net_device *dev;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700242 int err;
243
244 if (unlikely(sk->sk_type == SOCK_STREAM))
245 return -EINVAL; /* need packet boundaries */
246
247 /* Create net device */
Tom Gundersenc835a672014-07-14 16:37:24 +0200248 dev = alloc_netdev(sizeof(*gp), ifname, NET_NAME_UNKNOWN, gprs_setup);
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800249 if (!dev)
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700250 return -ENOMEM;
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800251 gp = netdev_priv(dev);
Rémi Denis-Courmont893873f2008-12-17 15:48:50 -0800252 gp->sk = sk;
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800253 gp->dev = dev;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700254
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800255 netif_stop_queue(dev);
256 err = register_netdev(dev);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700257 if (err) {
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800258 free_netdev(dev);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700259 return err;
260 }
261
262 lock_sock(sk);
263 if (unlikely(sk->sk_user_data)) {
264 err = -EBUSY;
265 goto out_rel;
266 }
267 if (unlikely((1 << sk->sk_state & (TCPF_CLOSE|TCPF_LISTEN)) ||
268 sock_flag(sk, SOCK_DEAD))) {
269 err = -EINVAL;
270 goto out_rel;
271 }
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800272 sk->sk_user_data = gp;
273 gp->old_state_change = sk->sk_state_change;
274 gp->old_data_ready = sk->sk_data_ready;
275 gp->old_write_space = sk->sk_write_space;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700276 sk->sk_state_change = gprs_state_change;
277 sk->sk_data_ready = gprs_data_ready;
278 sk->sk_write_space = gprs_write_space;
279 release_sock(sk);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700280 sock_hold(sk);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700281
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800282 printk(KERN_DEBUG"%s: attached\n", dev->name);
283 return dev->ifindex;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700284
285out_rel:
286 release_sock(sk);
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800287 unregister_netdev(dev);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700288 return err;
289}
290
291void gprs_detach(struct sock *sk)
292{
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800293 struct gprs_dev *gp = sk->sk_user_data;
294 struct net_device *dev = gp->dev;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700295
296 lock_sock(sk);
297 sk->sk_user_data = NULL;
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800298 sk->sk_state_change = gp->old_state_change;
299 sk->sk_data_ready = gp->old_data_ready;
300 sk->sk_write_space = gp->old_write_space;
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700301 release_sock(sk);
302
Rémi Denis-Courmont09a2c3c2008-12-16 01:18:31 -0800303 printk(KERN_DEBUG"%s: detached\n", dev->name);
304 unregister_netdev(dev);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700305 sock_put(sk);
Rémi Denis-Courmont02a47612008-10-05 11:16:16 -0700306}