summaryrefslogtreecommitdiffstats
path: root/net/phonet/pep-gprs.c
blob: 803eeef0aa856877ddced0e45fc283f4641da2ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
 * File: pep-gprs.c
 *
 * GPRS over Phonet pipe end point socket
 *
 * Copyright (C) 2008 Nokia Corporation.
 *
 * Author: RĂ©mi Denis-Courmont <remi.denis-courmont@nokia.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/if_ether.h>
#include <linux/if_arp.h>
#include <net/sock.h>

#include <linux/if_phonet.h>
#include <net/tcp_states.h>
#include <net/phonet/gprs.h>

#define GPRS_DEFAULT_MTU 1400

struct gprs_dev {
	struct sock		*sk;
	void			(*old_state_change)(struct sock *);
	void			(*old_data_ready)(struct sock *, int);
	void			(*old_write_space)(struct sock *);

	struct net_device	*net;
	struct net_device_stats	stats;

	struct sk_buff_head	tx_queue;
	struct work_struct	tx_work;
	spinlock_t		tx_lock;
	unsigned		tx_max;
};

static int gprs_type_trans(struct sk_buff *skb)
{
	const u8 *pvfc;
	u8 buf;

	pvfc = skb_header_pointer(skb, 0, 1, &buf);
	if (!pvfc)
		return 0;
	/* Look at IP version field */
	switch (*pvfc >> 4) {
	case 4:
		return htons(ETH_P_IP);
	case 6:
		return htons(ETH_P_IPV6);
	}
	return 0;
}

/*
 * Socket callbacks
 */

static void gprs_state_change(struct sock *sk)
{
	struct gprs_dev *dev = sk->sk_user_data;

	if (sk->sk_state == TCP_CLOSE_WAIT) {
		netif_stop_queue(dev->net);
		netif_carrier_off(dev->net);
	}
}

static int gprs_recv(struct gprs_dev *dev, struct sk_buff *skb)
{
	int err = 0;
	u16 protocol = gprs_type_trans(skb);

	if (!protocol) {
		err = -EINVAL;
		goto drop;
	}

	if (likely(skb_headroom(skb) & 3)) {
		struct sk_buff *rskb, *fs;
		int flen = 0;

		/* Phonet Pipe data header is misaligned (3 bytes),
		 * so wrap the IP packet as a single fragment of an head-less
		 * socket buffer. The network stack will pull what it needs,
		 * but at least, the whole IP payload is not memcpy'd. */
		rskb = netdev_alloc_skb(dev->net, 0);
		if (!rskb) {
			err = -ENOBUFS;
			goto drop;
		}
		skb_shinfo(rskb)->frag_list = skb;
		rskb->len += skb->len;
		rskb->data_len += rskb->len;
		rskb->truesize += rskb->len;

		/* Avoid nested fragments */
		for (fs = skb_shinfo(skb)->frag_list; fs; fs = fs->next)
			flen += fs->len;
		skb->next = skb_shinfo(skb)->frag_list;
		skb_shinfo(skb)->frag_list = NULL;
		skb->len -= flen;
		skb->data_len -= flen;
		skb->truesize -= flen;

		skb = rskb;
	}

	skb->protocol = protocol;
	skb_reset_mac_header(skb);
	skb->dev = dev->net;

	if (likely(dev->net->flags & IFF_UP)) {
		dev->stats.rx_packets++;
		dev->stats.rx_bytes += skb->len;
		netif_rx(skb);
		skb = NULL;
	} else
		err = -ENODEV;

drop:
	if (skb) {
		dev_kfree_skb(skb);
		dev->stats.rx_dropped++;
	}
	return err;
}

static void gprs_data_ready(struct sock *sk, int len)
{
	struct gprs_dev *dev = sk->sk_user_data;
	struct sk_buff *skb;

	while ((skb = pep_read(sk)) != NULL) {
		skb_orphan(skb);
		gprs_recv(dev, skb);
	}
}

static void gprs_write_space(struct sock *sk)
{
	struct gprs_dev *dev = sk->sk_user_data;
	struct net_device *net = dev->net;
	unsigned credits = pep_writeable(sk);

	spin_lock_bh(&dev->tx_lock);
	dev->tx_max = credits;
	if (credits > skb_queue_len(&dev->tx_queue) && netif_running(net))
		netif_wake_queue(net);
	spin_unlock_bh(&dev->tx_lock);
}

/*
 * Network device callbacks
 */

static int gprs_open(struct net_device *dev)
{
	struct gprs_dev *gp = netdev_priv(dev);

	gprs_write_space(gp->sk);
	return 0;
}

static int gprs_close(struct net_device *dev)
{
	struct gprs_dev *gp = netdev_priv(dev);

	netif_stop_queue(dev);
	flush_work(&gp->tx_work);
	return 0;
}

static int gprs_xmit(struct sk_buff *skb, struct net_device *net)
{
	struct gprs_dev *dev = netdev_priv(net);

	switch (skb->protocol) {
	case  htons(ETH_P_IP):
	case  htons(ETH_P_IPV6):
		break;
	default:
		dev_kfree_skb(skb);
		return 0;
	}

	spin_lock(&dev->tx_lock);
	if (likely(skb_queue_len(&dev->tx_queue) < dev->tx_max)) {
		skb_queue_tail(&dev->tx_queue, skb);
		skb = NULL;
	}
	if (skb_queue_len(&dev->tx_queue) >= dev->tx_max)
		netif_stop_queue(net);
	spin_unlock(&dev->tx_lock);

	schedule_work(&dev->tx_work);
	if (unlikely(skb))
		dev_kfree_skb(skb);
	return 0;
}

static void gprs_tx(struct work_struct *work)
{
	struct gprs_dev *dev = container_of(work, struct gprs_dev, tx_work);
	struct sock *sk = dev->sk;
	struct sk_buff *skb;

	while ((skb = skb_dequeue(&dev->tx_queue)) != NULL) {
		int err;

		dev->stats.tx_bytes += skb->len;
		dev->stats.tx_packets++;

		skb_orphan(skb);
		skb_set_owner_w(skb, sk);

		lock_sock(sk);
		err = pep_write(sk, skb);
		if (err) {
			LIMIT_NETDEBUG(KERN_WARNING"%s: TX error (%d)\n",
					dev->net->name, err);
			dev->stats.tx_aborted_errors++;
			dev->stats.tx_errors++;
		}
		release_sock(sk);
	}

	lock_sock(sk);
	gprs_write_space(sk);
	release_sock(sk);
}

static int gprs_set_mtu(struct net_device *net, int new_mtu)
{
	if ((new_mtu < 576) || (new_mtu > (PHONET_MAX_MTU - 11)))
		return -EINVAL;

	net->mtu = new_mtu;
	return 0;
}

static struct net_device_stats *gprs_get_stats(struct net_device *net)
{
	struct gprs_dev *dev = netdev_priv(net);

	return &dev->stats;
}

static void gprs_setup(struct net_device *net)
{
	net->features		= NETIF_F_FRAGLIST;
	net->type		= ARPHRD_NONE;
	net->flags		= IFF_POINTOPOINT | IFF_NOARP;
	net->mtu		= GPRS_DEFAULT_MTU;
	net->hard_header_len	= 0;
	net->addr_len		= 0;
	net->tx_queue_len	= 10;

	net->destructor		= free_netdev;
	net->open		= gprs_open;
	net->stop		= gprs_close;
	net->hard_start_xmit	= gprs_xmit; /* mandatory */
	net->change_mtu		= gprs_set_mtu;
	net->get_stats		= gprs_get_stats;
}

/*
 * External interface
 */

/*
 * Attach a GPRS interface to a datagram socket.
 * Returns the interface index on success, negative error code on error.
 */
int gprs_attach(struct sock *sk)
{
	static const char ifname[] = "gprs%d";
	struct gprs_dev *dev;
	struct net_device *net;
	int err;

	if (unlikely(sk->sk_type == SOCK_STREAM))
		return -EINVAL; /* need packet boundaries */

	/* Create net device */
	net = alloc_netdev(sizeof(*dev), ifname, gprs_setup);
	if (!net)
		return -ENOMEM;
	dev = netdev_priv(net);
	dev->net = net;
	dev->tx_max = 0;
	spin_lock_init(&dev->tx_lock);
	skb_queue_head_init(&dev->tx_queue);
	INIT_WORK(&dev->tx_work, gprs_tx);

	netif_stop_queue(net);
	err = register_netdev(net);
	if (err) {
		free_netdev(net);
		return err;
	}

	lock_sock(sk);
	if (unlikely(sk->sk_user_data)) {
		err = -EBUSY;
		goto out_rel;
	}
	if (unlikely((1 << sk->sk_state & (TCPF_CLOSE|TCPF_LISTEN)) ||
			sock_flag(sk, SOCK_DEAD))) {
		err = -EINVAL;
		goto out_rel;
	}
	sk->sk_user_data	= dev;
	dev->old_state_change	= sk->sk_state_change;
	dev->old_data_ready	= sk->sk_data_ready;
	dev->old_write_space	= sk->sk_write_space;
	sk->sk_state_change	= gprs_state_change;
	sk->sk_data_ready	= gprs_data_ready;
	sk->sk_write_space	= gprs_write_space;
	release_sock(sk);

	sock_hold(sk);
	dev->sk = sk;

	printk(KERN_DEBUG"%s: attached\n", net->name);
	return net->ifindex;

out_rel:
	release_sock(sk);
	unregister_netdev(net);
	return err;
}

void gprs_detach(struct sock *sk)
{
	struct gprs_dev *dev = sk->sk_user_data;
	struct net_device *net = dev->net;

	lock_sock(sk);
	sk->sk_user_data	= NULL;
	sk->sk_state_change	= dev->old_state_change;
	sk->sk_data_ready	= dev->old_data_ready;
	sk->sk_write_space	= dev->old_write_space;
	release_sock(sk);

	printk(KERN_DEBUG"%s: detached\n", net->name);
	unregister_netdev(net);
	sock_put(sk);
}
OpenPOWER on IntegriCloud