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 #include <trace/events/sock.h>
24 #define GPRS_DEFAULT_MTU 1400
28 void (*old_state_change
)(struct sock
*);
29 void (*old_data_ready
)(struct sock
*);
30 void (*old_write_space
)(struct sock
*);
32 struct net_device
*dev
;
35 static __be16
gprs_type_trans(struct sk_buff
*skb
)
40 pvfc
= skb_header_pointer(skb
, 0, 1, &buf
);
43 /* Look at IP version field */
46 return htons(ETH_P_IP
);
48 return htons(ETH_P_IPV6
);
53 static void gprs_writeable(struct gprs_dev
*gp
)
55 struct net_device
*dev
= gp
->dev
;
57 if (pep_writeable(gp
->sk
))
58 netif_wake_queue(dev
);
65 static void gprs_state_change(struct sock
*sk
)
67 struct gprs_dev
*gp
= sk
->sk_user_data
;
69 if (sk
->sk_state
== TCP_CLOSE_WAIT
) {
70 struct net_device
*dev
= gp
->dev
;
72 netif_stop_queue(dev
);
73 netif_carrier_off(dev
);
77 static int gprs_recv(struct gprs_dev
*gp
, struct sk_buff
*skb
)
79 struct net_device
*dev
= gp
->dev
;
81 __be16 protocol
= gprs_type_trans(skb
);
88 if (skb_headroom(skb
) & 3) {
89 struct sk_buff
*rskb
, *fs
;
92 /* Phonet Pipe data header may be misaligned (3 bytes),
93 * so wrap the IP packet as a single fragment of an head-less
94 * socket buffer. The network stack will pull what it needs,
95 * but at least, the whole IP payload is not memcpy'd. */
96 rskb
= netdev_alloc_skb(dev
, 0);
101 skb_shinfo(rskb
)->frag_list
= skb
;
102 rskb
->len
+= skb
->len
;
103 rskb
->data_len
+= rskb
->len
;
104 rskb
->truesize
+= rskb
->len
;
106 /* Avoid nested fragments */
107 skb_walk_frags(skb
, fs
)
109 skb
->next
= skb_shinfo(skb
)->frag_list
;
110 skb_frag_list_init(skb
);
112 skb
->data_len
-= flen
;
113 skb
->truesize
-= flen
;
118 skb
->protocol
= protocol
;
119 skb_reset_mac_header(skb
);
122 if (likely(dev
->flags
& IFF_UP
)) {
123 dev
->stats
.rx_packets
++;
124 dev
->stats
.rx_bytes
+= skb
->len
;
133 dev
->stats
.rx_dropped
++;
138 static void gprs_data_ready(struct sock
*sk
)
140 struct gprs_dev
*gp
= sk
->sk_user_data
;
143 trace_sk_data_ready(sk
);
145 while ((skb
= pep_read(sk
)) != NULL
) {
151 static void gprs_write_space(struct sock
*sk
)
153 struct gprs_dev
*gp
= sk
->sk_user_data
;
155 if (netif_running(gp
->dev
))
160 * Network device callbacks
163 static int gprs_open(struct net_device
*dev
)
165 struct gprs_dev
*gp
= netdev_priv(dev
);
171 static int gprs_close(struct net_device
*dev
)
173 netif_stop_queue(dev
);
177 static netdev_tx_t
gprs_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
179 struct gprs_dev
*gp
= netdev_priv(dev
);
180 struct sock
*sk
= gp
->sk
;
183 switch (skb
->protocol
) {
184 case htons(ETH_P_IP
):
185 case htons(ETH_P_IPV6
):
193 skb_set_owner_w(skb
, sk
);
195 err
= pep_write(sk
, skb
);
197 net_dbg_ratelimited("%s: TX error (%d)\n", dev
->name
, err
);
198 dev
->stats
.tx_aborted_errors
++;
199 dev
->stats
.tx_errors
++;
201 dev
->stats
.tx_packets
++;
202 dev
->stats
.tx_bytes
+= len
;
205 netif_stop_queue(dev
);
206 if (pep_writeable(sk
))
207 netif_wake_queue(dev
);
211 static const struct net_device_ops gprs_netdev_ops
= {
212 .ndo_open
= gprs_open
,
213 .ndo_stop
= gprs_close
,
214 .ndo_start_xmit
= gprs_xmit
,
217 static void gprs_setup(struct net_device
*dev
)
219 dev
->features
= NETIF_F_FRAGLIST
;
220 dev
->type
= ARPHRD_PHONET_PIPE
;
221 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
;
222 dev
->mtu
= GPRS_DEFAULT_MTU
;
224 dev
->max_mtu
= (PHONET_MAX_MTU
- 11);
225 dev
->hard_header_len
= 0;
227 dev
->tx_queue_len
= 10;
229 dev
->netdev_ops
= &gprs_netdev_ops
;
230 dev
->needs_free_netdev
= true;
238 * Attach a GPRS interface to a datagram socket.
239 * Returns the interface index on success, negative error code on error.
241 int gprs_attach(struct sock
*sk
)
243 static const char ifname
[] = "gprs%d";
245 struct net_device
*dev
;
248 if (unlikely(sk
->sk_type
== SOCK_STREAM
))
249 return -EINVAL
; /* need packet boundaries */
251 /* Create net device */
252 dev
= alloc_netdev(sizeof(*gp
), ifname
, NET_NAME_UNKNOWN
, gprs_setup
);
255 gp
= netdev_priv(dev
);
259 netif_stop_queue(dev
);
260 err
= register_netdev(dev
);
267 if (unlikely(sk
->sk_user_data
)) {
271 if (unlikely((1 << sk
->sk_state
& (TCPF_CLOSE
|TCPF_LISTEN
)) ||
272 sock_flag(sk
, SOCK_DEAD
))) {
276 sk
->sk_user_data
= gp
;
277 gp
->old_state_change
= sk
->sk_state_change
;
278 gp
->old_data_ready
= sk
->sk_data_ready
;
279 gp
->old_write_space
= sk
->sk_write_space
;
280 sk
->sk_state_change
= gprs_state_change
;
281 sk
->sk_data_ready
= gprs_data_ready
;
282 sk
->sk_write_space
= gprs_write_space
;
286 printk(KERN_DEBUG
"%s: attached\n", dev
->name
);
291 unregister_netdev(dev
);
295 void gprs_detach(struct sock
*sk
)
297 struct gprs_dev
*gp
= sk
->sk_user_data
;
298 struct net_device
*dev
= gp
->dev
;
301 sk
->sk_user_data
= NULL
;
302 sk
->sk_state_change
= gp
->old_state_change
;
303 sk
->sk_data_ready
= gp
->old_data_ready
;
304 sk
->sk_write_space
= gp
->old_write_space
;
307 printk(KERN_DEBUG
"%s: detached\n", dev
->name
);
308 unregister_netdev(dev
);