1 // SPDX-License-Identifier: GPL-2.0-only
5 * GPRS over Phonet pipe end point socket
7 * Copyright (C) 2008 Nokia Corporation.
9 * Author: RĂ©mi Denis-Courmont
12 #include <linux/kernel.h>
13 #include <linux/netdevice.h>
14 #include <linux/if_ether.h>
15 #include <linux/if_arp.h>
18 #include <linux/if_phonet.h>
19 #include <net/tcp_states.h>
20 #include <net/phonet/gprs.h>
22 #define GPRS_DEFAULT_MTU 1400
26 void (*old_state_change)(struct sock *);
27 void (*old_data_ready)(struct sock *);
28 void (*old_write_space)(struct sock *);
30 struct net_device *dev;
33 static __be16 gprs_type_trans(struct sk_buff *skb)
38 pvfc = skb_header_pointer(skb, 0, 1, &buf);
41 /* Look at IP version field */
44 return htons(ETH_P_IP);
46 return htons(ETH_P_IPV6);
51 static void gprs_writeable(struct gprs_dev *gp)
53 struct net_device *dev = gp->dev;
55 if (pep_writeable(gp->sk))
56 netif_wake_queue(dev);
63 static void gprs_state_change(struct sock *sk)
65 struct gprs_dev *gp = sk->sk_user_data;
67 if (sk->sk_state == TCP_CLOSE_WAIT) {
68 struct net_device *dev = gp->dev;
70 netif_stop_queue(dev);
71 netif_carrier_off(dev);
75 static int gprs_recv(struct gprs_dev *gp, struct sk_buff *skb)
77 struct net_device *dev = gp->dev;
79 __be16 protocol = gprs_type_trans(skb);
86 if (skb_headroom(skb) & 3) {
87 struct sk_buff *rskb, *fs;
90 /* Phonet Pipe data header may be misaligned (3 bytes),
91 * 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. */
94 rskb = netdev_alloc_skb(dev, 0);
99 skb_shinfo(rskb)->frag_list = skb;
100 rskb->len += skb->len;
101 rskb->data_len += rskb->len;
102 rskb->truesize += rskb->len;
104 /* Avoid nested fragments */
105 skb_walk_frags(skb, fs)
107 skb->next = skb_shinfo(skb)->frag_list;
108 skb_frag_list_init(skb);
110 skb->data_len -= flen;
111 skb->truesize -= flen;
116 skb->protocol = protocol;
117 skb_reset_mac_header(skb);
120 if (likely(dev->flags & IFF_UP)) {
121 dev->stats.rx_packets++;
122 dev->stats.rx_bytes += skb->len;
131 dev->stats.rx_dropped++;
136 static void gprs_data_ready(struct sock *sk)
138 struct gprs_dev *gp = sk->sk_user_data;
141 while ((skb = pep_read(sk)) != NULL) {
147 static void gprs_write_space(struct sock *sk)
149 struct gprs_dev *gp = sk->sk_user_data;
151 if (netif_running(gp->dev))
156 * Network device callbacks
159 static int gprs_open(struct net_device *dev)
161 struct gprs_dev *gp = netdev_priv(dev);
167 static int gprs_close(struct net_device *dev)
169 netif_stop_queue(dev);
173 static netdev_tx_t gprs_xmit(struct sk_buff *skb, struct net_device *dev)
175 struct gprs_dev *gp = netdev_priv(dev);
176 struct sock *sk = gp->sk;
179 switch (skb->protocol) {
180 case htons(ETH_P_IP):
181 case htons(ETH_P_IPV6):
189 skb_set_owner_w(skb, sk);
191 err = pep_write(sk, skb);
193 net_dbg_ratelimited("%s: TX error (%d)\n", dev->name, err);
194 dev->stats.tx_aborted_errors++;
195 dev->stats.tx_errors++;
197 dev->stats.tx_packets++;
198 dev->stats.tx_bytes += len;
201 netif_stop_queue(dev);
202 if (pep_writeable(sk))
203 netif_wake_queue(dev);
207 static const struct net_device_ops gprs_netdev_ops = {
208 .ndo_open = gprs_open,
209 .ndo_stop = gprs_close,
210 .ndo_start_xmit = gprs_xmit,
213 static void gprs_setup(struct net_device *dev)
215 dev->features = NETIF_F_FRAGLIST;
216 dev->type = ARPHRD_PHONET_PIPE;
217 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
218 dev->mtu = GPRS_DEFAULT_MTU;
220 dev->max_mtu = (PHONET_MAX_MTU - 11);
221 dev->hard_header_len = 0;
223 dev->tx_queue_len = 10;
225 dev->netdev_ops = &gprs_netdev_ops;
226 dev->needs_free_netdev = true;
234 * Attach a GPRS interface to a datagram socket.
235 * Returns the interface index on success, negative error code on error.
237 int gprs_attach(struct sock *sk)
239 static const char ifname[] = "gprs%d";
241 struct net_device *dev;
244 if (unlikely(sk->sk_type == SOCK_STREAM))
245 return -EINVAL; /* need packet boundaries */
247 /* Create net device */
248 dev = alloc_netdev(sizeof(*gp), ifname, NET_NAME_UNKNOWN, gprs_setup);
251 gp = netdev_priv(dev);
255 netif_stop_queue(dev);
256 err = register_netdev(dev);
263 if (unlikely(sk->sk_user_data)) {
267 if (unlikely((1 << sk->sk_state & (TCPF_CLOSE|TCPF_LISTEN)) ||
268 sock_flag(sk, SOCK_DEAD))) {
272 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;
276 sk->sk_state_change = gprs_state_change;
277 sk->sk_data_ready = gprs_data_ready;
278 sk->sk_write_space = gprs_write_space;
282 printk(KERN_DEBUG"%s: attached\n", dev->name);
287 unregister_netdev(dev);
291 void gprs_detach(struct sock *sk)
293 struct gprs_dev *gp = sk->sk_user_data;
294 struct net_device *dev = gp->dev;
297 sk->sk_user_data = NULL;
298 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;
303 printk(KERN_DEBUG"%s: detached\n", dev->name);
304 unregister_netdev(dev);