Blame view

net/ax25/af_ax25.c 43.8 KB
2874c5fd2   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3
4
5
6
7
8
9
10
11
12
   *
   * Copyright (C) Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
   * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
   * Copyright (C) Darryl Miles G7LED (dlm@g7led.demon.co.uk)
   * Copyright (C) Steven Whitehouse GW7RRM (stevew@acm.org)
   * Copyright (C) Joerg Reuter DL1BKE (jreuter@yaina.de)
   * Copyright (C) Hans-Joachim Hetscher DD8NE (dd8ne@bnv-bamberg.de)
   * Copyright (C) Hans Alblas PE1AYX (hans@esrac.ele.tue.nl)
   * Copyright (C) Frederic Rible F1OAT (frible@teaser.fr)
   */
4fc268d24   Randy Dunlap   [PATCH] capable/c...
13
  #include <linux/capability.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
14
15
16
17
18
19
  #include <linux/module.h>
  #include <linux/errno.h>
  #include <linux/types.h>
  #include <linux/socket.h>
  #include <linux/in.h>
  #include <linux/kernel.h>
3f07c0144   Ingo Molnar   sched/headers: Pr...
20
  #include <linux/sched/signal.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21
22
  #include <linux/timer.h>
  #include <linux/string.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23
24
  #include <linux/sockios.h>
  #include <linux/net.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
25
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
27
28
29
30
31
  #include <net/ax25.h>
  #include <linux/inet.h>
  #include <linux/netdevice.h>
  #include <linux/if_arp.h>
  #include <linux/skbuff.h>
  #include <net/sock.h>
7c0f6ba68   Linus Torvalds   Replace <asm/uacc...
32
  #include <linux/uaccess.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33
34
35
36
37
38
39
  #include <linux/fcntl.h>
  #include <linux/termios.h>	/* For TIOCINQ/OUTQ */
  #include <linux/mm.h>
  #include <linux/interrupt.h>
  #include <linux/notifier.h>
  #include <linux/proc_fs.h>
  #include <linux/stat.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
40
41
42
  #include <linux/sysctl.h>
  #include <linux/init.h>
  #include <linux/spinlock.h>
457c4cbc5   Eric W. Biederman   [NET]: Make /proc...
43
  #include <net/net_namespace.h>
c752f0739   Arnaldo Carvalho de Melo   [TCP]: Move the t...
44
  #include <net/tcp_states.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45
46
47
48
49
50
51
  #include <net/ip.h>
  #include <net/arp.h>
  
  
  
  HLIST_HEAD(ax25_list);
  DEFINE_SPINLOCK(ax25_list_lock);
90ddc4f04   Eric Dumazet   [NET]: move struc...
52
  static const struct proto_ops ax25_proto_ops;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
53
54
55
  
  static void ax25_free_sock(struct sock *sk)
  {
3200392b8   David Miller   ax25: Stop using ...
56
  	ax25_cb_put(sk_to_ax25(sk));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  }
  
  /*
   *	Socket removal during an interrupt is now safe.
   */
  static void ax25_cb_del(ax25_cb *ax25)
  {
  	if (!hlist_unhashed(&ax25->ax25_node)) {
  		spin_lock_bh(&ax25_list_lock);
  		hlist_del_init(&ax25->ax25_node);
  		spin_unlock_bh(&ax25_list_lock);
  		ax25_cb_put(ax25);
  	}
  }
  
  /*
   *	Kill all bound sockets on a dropped device.
   */
  static void ax25_kill_by_device(struct net_device *dev)
  {
  	ax25_dev *ax25_dev;
  	ax25_cb *s;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
79
80
81
82
83
  
  	if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
  		return;
  
  	spin_lock_bh(&ax25_list_lock);
ecd2ebdea   Jarek Poplawski   [AX25] af_ax25: P...
84
  again:
b67bfe0d4   Sasha Levin   hlist: drop the n...
85
  	ax25_for_each(s, &ax25_list) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86
87
  		if (s->ax25_dev == ax25_dev) {
  			s->ax25_dev = NULL;
ecd2ebdea   Jarek Poplawski   [AX25] af_ax25: P...
88
  			spin_unlock_bh(&ax25_list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
89
  			ax25_disconnect(s, ENETUNREACH);
ecd2ebdea   Jarek Poplawski   [AX25] af_ax25: P...
90
91
92
93
94
95
96
97
98
99
  			spin_lock_bh(&ax25_list_lock);
  
  			/* The entry could have been deleted from the
  			 * list meanwhile and thus the next pointer is
  			 * no longer valid.  Play it safe and restart
  			 * the scan.  Forward progress is ensured
  			 * because we set s->ax25_dev to NULL and we
  			 * are never passed a NULL 'dev' argument.
  			 */
  			goto again;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
100
101
102
103
104
105
106
107
108
  		}
  	}
  	spin_unlock_bh(&ax25_list_lock);
  }
  
  /*
   *	Handle device status changes.
   */
  static int ax25_device_event(struct notifier_block *this, unsigned long event,
351638e7d   Jiri Pirko   net: pass info st...
109
  			     void *ptr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
110
  {
351638e7d   Jiri Pirko   net: pass info st...
111
  	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
112

721499e89   YOSHIFUJI Hideaki   netns: Use net_eq...
113
  	if (!net_eq(dev_net(dev), &init_net))
e9dc86534   Eric W. Biederman   [NET]: Make devic...
114
  		return NOTIFY_DONE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  	/* Reject non AX.25 devices */
  	if (dev->type != ARPHRD_AX25)
  		return NOTIFY_DONE;
  
  	switch (event) {
  	case NETDEV_UP:
  		ax25_dev_device_up(dev);
  		break;
  	case NETDEV_DOWN:
  		ax25_kill_by_device(dev);
  		ax25_rt_device_down(dev);
  		ax25_dev_device_down(dev);
  		break;
  	default:
  		break;
  	}
  
  	return NOTIFY_DONE;
  }
  
  /*
   *	Add a socket to the bound sockets list.
   */
  void ax25_cb_add(ax25_cb *ax25)
  {
  	spin_lock_bh(&ax25_list_lock);
  	ax25_cb_hold(ax25);
  	hlist_add_head(&ax25->ax25_node, &ax25_list);
  	spin_unlock_bh(&ax25_list_lock);
  }
  
  /*
   *	Find a socket that wants to accept the SABM we have just
   *	received.
   */
  struct sock *ax25_find_listener(ax25_address *addr, int digi,
  	struct net_device *dev, int type)
  {
  	ax25_cb *s;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
154

c19c4b9c9   Ralf Baechle   [AX.25]: Optimize...
155
  	spin_lock(&ax25_list_lock);
b67bfe0d4   Sasha Levin   hlist: drop the n...
156
  	ax25_for_each(s, &ax25_list) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
157
158
159
160
161
162
163
  		if ((s->iamdigi && !digi) || (!s->iamdigi && digi))
  			continue;
  		if (s->sk && !ax25cmp(&s->source_addr, addr) &&
  		    s->sk->sk_type == type && s->sk->sk_state == TCP_LISTEN) {
  			/* If device is null we match any device */
  			if (s->ax25_dev == NULL || s->ax25_dev->dev == dev) {
  				sock_hold(s->sk);
c19c4b9c9   Ralf Baechle   [AX.25]: Optimize...
164
  				spin_unlock(&ax25_list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
165
166
167
168
  				return s->sk;
  			}
  		}
  	}
c19c4b9c9   Ralf Baechle   [AX.25]: Optimize...
169
  	spin_unlock(&ax25_list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
170
171
172
173
174
175
176
177
178
179
180
181
  
  	return NULL;
  }
  
  /*
   *	Find an AX.25 socket given both ends.
   */
  struct sock *ax25_get_socket(ax25_address *my_addr, ax25_address *dest_addr,
  	int type)
  {
  	struct sock *sk = NULL;
  	ax25_cb *s;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
182

c19c4b9c9   Ralf Baechle   [AX.25]: Optimize...
183
  	spin_lock(&ax25_list_lock);
b67bfe0d4   Sasha Levin   hlist: drop the n...
184
  	ax25_for_each(s, &ax25_list) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
185
186
187
188
189
190
191
192
  		if (s->sk && !ax25cmp(&s->source_addr, my_addr) &&
  		    !ax25cmp(&s->dest_addr, dest_addr) &&
  		    s->sk->sk_type == type) {
  			sk = s->sk;
  			sock_hold(sk);
  			break;
  		}
  	}
c19c4b9c9   Ralf Baechle   [AX.25]: Optimize...
193
  	spin_unlock(&ax25_list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
194
195
196
197
198
199
200
201
202
203
204
205
  
  	return sk;
  }
  
  /*
   *	Find an AX.25 control block given both ends. It will only pick up
   *	floating AX.25 control blocks or non Raw socket bound control blocks.
   */
  ax25_cb *ax25_find_cb(ax25_address *src_addr, ax25_address *dest_addr,
  	ax25_digi *digi, struct net_device *dev)
  {
  	ax25_cb *s;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
206
207
  
  	spin_lock_bh(&ax25_list_lock);
b67bfe0d4   Sasha Levin   hlist: drop the n...
208
  	ax25_for_each(s, &ax25_list) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
  		if (s->sk && s->sk->sk_type != SOCK_SEQPACKET)
  			continue;
  		if (s->ax25_dev == NULL)
  			continue;
  		if (ax25cmp(&s->source_addr, src_addr) == 0 && ax25cmp(&s->dest_addr, dest_addr) == 0 && s->ax25_dev->dev == dev) {
  			if (digi != NULL && digi->ndigi != 0) {
  				if (s->digipeat == NULL)
  					continue;
  				if (ax25digicmp(s->digipeat, digi) != 0)
  					continue;
  			} else {
  				if (s->digipeat != NULL && s->digipeat->ndigi != 0)
  					continue;
  			}
  			ax25_cb_hold(s);
  			spin_unlock_bh(&ax25_list_lock);
  
  			return s;
  		}
  	}
  	spin_unlock_bh(&ax25_list_lock);
  
  	return NULL;
  }
70868eace   Ralf Baechle   [AX.25]: Move AX....
233
  EXPORT_SYMBOL(ax25_find_cb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
234
235
236
237
  void ax25_send_to_raw(ax25_address *addr, struct sk_buff *skb, int proto)
  {
  	ax25_cb *s;
  	struct sk_buff *copy;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
238

c19c4b9c9   Ralf Baechle   [AX.25]: Optimize...
239
  	spin_lock(&ax25_list_lock);
b67bfe0d4   Sasha Levin   hlist: drop the n...
240
  	ax25_for_each(s, &ax25_list) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
241
242
243
244
245
246
247
248
249
250
251
  		if (s->sk != NULL && ax25cmp(&s->source_addr, addr) == 0 &&
  		    s->sk->sk_type == SOCK_RAW &&
  		    s->sk->sk_protocol == proto &&
  		    s->ax25_dev->dev == skb->dev &&
  		    atomic_read(&s->sk->sk_rmem_alloc) <= s->sk->sk_rcvbuf) {
  			if ((copy = skb_clone(skb, GFP_ATOMIC)) == NULL)
  				continue;
  			if (sock_queue_rcv_skb(s->sk, copy) != 0)
  				kfree_skb(copy);
  		}
  	}
c19c4b9c9   Ralf Baechle   [AX.25]: Optimize...
252
  	spin_unlock(&ax25_list_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
253
254
255
256
257
258
259
260
261
262
  }
  
  /*
   *	Deferred destroy.
   */
  void ax25_destroy_socket(ax25_cb *);
  
  /*
   *	Handler for deferred kills.
   */
8dbd05ff5   Kees Cook   net: ax25: Conver...
263
  static void ax25_destroy_timer(struct timer_list *t)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
264
  {
8dbd05ff5   Kees Cook   net: ax25: Conver...
265
  	ax25_cb *ax25 = from_timer(ax25, t, dtimer);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
266
  	struct sock *sk;
528930b91   YOSHIFUJI Hideaki   [NET] AX25: Fix w...
267

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
268
  	sk=ax25->sk;
528930b91   YOSHIFUJI Hideaki   [NET] AX25: Fix w...
269

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  	bh_lock_sock(sk);
  	sock_hold(sk);
  	ax25_destroy_socket(ax25);
  	bh_unlock_sock(sk);
  	sock_put(sk);
  }
  
  /*
   *	This is called from user mode and the timers. Thus it protects itself
   *	against interrupt users but doesn't worry about being called during
   *	work. Once it is removed from the queue no interrupt or bottom half
   *	will touch it and we are (fairly 8-) ) safe.
   */
  void ax25_destroy_socket(ax25_cb *ax25)
  {
  	struct sk_buff *skb;
  
  	ax25_cb_del(ax25);
  
  	ax25_stop_heartbeat(ax25);
  	ax25_stop_t1timer(ax25);
  	ax25_stop_t2timer(ax25);
  	ax25_stop_t3timer(ax25);
  	ax25_stop_idletimer(ax25);
  
  	ax25_clear_queues(ax25);	/* Flush the queues */
  
  	if (ax25->sk != NULL) {
  		while ((skb = skb_dequeue(&ax25->sk->sk_receive_queue)) != NULL) {
  			if (skb->sk != ax25->sk) {
  				/* A pending connection */
3200392b8   David Miller   ax25: Stop using ...
301
  				ax25_cb *sax25 = sk_to_ax25(skb->sk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
302
303
304
  
  				/* Queue the unaccepted socket for death */
  				sock_orphan(skb->sk);
33d1d2c52   David S. Miller   ax25: Quick fix f...
305
306
  				/* 9A4GL: hack to release unaccepted sockets */
  				skb->sk->sk_state = TCP_LISTEN;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
307
308
309
310
311
312
313
314
315
316
  				ax25_start_heartbeat(sax25);
  				sax25->state = AX25_STATE_0;
  			}
  
  			kfree_skb(skb);
  		}
  		skb_queue_purge(&ax25->sk->sk_write_queue);
  	}
  
  	if (ax25->sk != NULL) {
c564039fd   Eric Dumazet   net: sk_wmem_allo...
317
  		if (sk_has_allocations(ax25->sk)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
318
  			/* Defer: outstanding buffers */
8dbd05ff5   Kees Cook   net: ax25: Conver...
319
  			timer_setup(&ax25->dtimer, ax25_destroy_timer, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
320
  			ax25->dtimer.expires  = jiffies + 2 * HZ;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
  			add_timer(&ax25->dtimer);
  		} else {
  			struct sock *sk=ax25->sk;
  			ax25->sk=NULL;
  			sock_put(sk);
  		}
  	} else {
  		ax25_cb_put(ax25);
  	}
  }
  
  /*
   * dl1bke 960311: set parameters for existing AX.25 connections,
   *		  includes a KILL command to abort any connection.
   *		  VERY useful for debugging ;-)
   */
  static int ax25_ctl_ioctl(const unsigned int cmd, void __user *arg)
  {
  	struct ax25_ctl_struct ax25_ctl;
  	ax25_digi digi;
  	ax25_dev *ax25_dev;
  	ax25_cb *ax25;
  	unsigned int k;
c0181d420   Jarek Poplawski   ax25: Fix ax25_cb...
344
  	int ret = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
345
346
347
348
349
350
351
352
353
  
  	if (copy_from_user(&ax25_ctl, arg, sizeof(ax25_ctl)))
  		return -EFAULT;
  
  	if ((ax25_dev = ax25_addr_ax25dev(&ax25_ctl.port_addr)) == NULL)
  		return -ENODEV;
  
  	if (ax25_ctl.digi_count > AX25_MAX_DIGIS)
  		return -EINVAL;
43ab85021   roel kluin   ax25: unsigned ca...
354
355
  	if (ax25_ctl.arg > ULONG_MAX / HZ && ax25_ctl.cmd != AX25_KILL)
  		return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
  	digi.ndigi = ax25_ctl.digi_count;
  	for (k = 0; k < digi.ndigi; k++)
  		digi.calls[k] = ax25_ctl.digi_addr[k];
  
  	if ((ax25 = ax25_find_cb(&ax25_ctl.source_addr, &ax25_ctl.dest_addr, &digi, ax25_dev->dev)) == NULL)
  		return -ENOTCONN;
  
  	switch (ax25_ctl.cmd) {
  	case AX25_KILL:
  		ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
  #ifdef CONFIG_AX25_DAMA_SLAVE
  		if (ax25_dev->dama.slave && ax25->ax25_dev->values[AX25_VALUES_PROTOCOL] == AX25_PROTO_DAMA_SLAVE)
  			ax25_dama_off(ax25);
  #endif
  		ax25_disconnect(ax25, ENETRESET);
  		break;
528930b91   YOSHIFUJI Hideaki   [NET] AX25: Fix w...
372
373
374
  	case AX25_WINDOW:
  		if (ax25->modulus == AX25_MODULUS) {
  			if (ax25_ctl.arg < 1 || ax25_ctl.arg > 7)
c0181d420   Jarek Poplawski   ax25: Fix ax25_cb...
375
  				goto einval_put;
528930b91   YOSHIFUJI Hideaki   [NET] AX25: Fix w...
376
377
  		} else {
  			if (ax25_ctl.arg < 1 || ax25_ctl.arg > 63)
c0181d420   Jarek Poplawski   ax25: Fix ax25_cb...
378
  				goto einval_put;
528930b91   YOSHIFUJI Hideaki   [NET] AX25: Fix w...
379
380
381
382
383
  		}
  		ax25->window = ax25_ctl.arg;
  		break;
  
  	case AX25_T1:
be639ac69   Ralf Baechle   NET: AX.25: Check...
384
  		if (ax25_ctl.arg < 1 || ax25_ctl.arg > ULONG_MAX / HZ)
c0181d420   Jarek Poplawski   ax25: Fix ax25_cb...
385
  			goto einval_put;
528930b91   YOSHIFUJI Hideaki   [NET] AX25: Fix w...
386
387
388
389
390
  		ax25->rtt = (ax25_ctl.arg * HZ) / 2;
  		ax25->t1  = ax25_ctl.arg * HZ;
  		break;
  
  	case AX25_T2:
be639ac69   Ralf Baechle   NET: AX.25: Check...
391
  		if (ax25_ctl.arg < 1 || ax25_ctl.arg > ULONG_MAX / HZ)
c0181d420   Jarek Poplawski   ax25: Fix ax25_cb...
392
  			goto einval_put;
528930b91   YOSHIFUJI Hideaki   [NET] AX25: Fix w...
393
394
395
396
397
  		ax25->t2 = ax25_ctl.arg * HZ;
  		break;
  
  	case AX25_N2:
  		if (ax25_ctl.arg < 1 || ax25_ctl.arg > 31)
c0181d420   Jarek Poplawski   ax25: Fix ax25_cb...
398
  			goto einval_put;
528930b91   YOSHIFUJI Hideaki   [NET] AX25: Fix w...
399
400
401
402
403
  		ax25->n2count = 0;
  		ax25->n2 = ax25_ctl.arg;
  		break;
  
  	case AX25_T3:
be639ac69   Ralf Baechle   NET: AX.25: Check...
404
405
  		if (ax25_ctl.arg > ULONG_MAX / HZ)
  			goto einval_put;
528930b91   YOSHIFUJI Hideaki   [NET] AX25: Fix w...
406
407
408
409
  		ax25->t3 = ax25_ctl.arg * HZ;
  		break;
  
  	case AX25_IDLE:
be639ac69   Ralf Baechle   NET: AX.25: Check...
410
411
  		if (ax25_ctl.arg > ULONG_MAX / (60 * HZ))
  			goto einval_put;
528930b91   YOSHIFUJI Hideaki   [NET] AX25: Fix w...
412
413
414
415
416
  		ax25->idle = ax25_ctl.arg * 60 * HZ;
  		break;
  
  	case AX25_PACLEN:
  		if (ax25_ctl.arg < 16 || ax25_ctl.arg > 65535)
c0181d420   Jarek Poplawski   ax25: Fix ax25_cb...
417
  			goto einval_put;
528930b91   YOSHIFUJI Hideaki   [NET] AX25: Fix w...
418
419
420
421
  		ax25->paclen = ax25_ctl.arg;
  		break;
  
  	default:
c0181d420   Jarek Poplawski   ax25: Fix ax25_cb...
422
  		goto einval_put;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
423
  	  }
c0181d420   Jarek Poplawski   ax25: Fix ax25_cb...
424
425
426
427
428
429
430
  out_put:
  	ax25_cb_put(ax25);
  	return ret;
  
  einval_put:
  	ret = -EINVAL;
  	goto out_put;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
431
  }
e1fdb5b39   Ralf Baechle   [AX.25]: Eleminat...
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
  static void ax25_fillin_cb_from_dev(ax25_cb *ax25, ax25_dev *ax25_dev)
  {
  	ax25->rtt     = msecs_to_jiffies(ax25_dev->values[AX25_VALUES_T1]) / 2;
  	ax25->t1      = msecs_to_jiffies(ax25_dev->values[AX25_VALUES_T1]);
  	ax25->t2      = msecs_to_jiffies(ax25_dev->values[AX25_VALUES_T2]);
  	ax25->t3      = msecs_to_jiffies(ax25_dev->values[AX25_VALUES_T3]);
  	ax25->n2      = ax25_dev->values[AX25_VALUES_N2];
  	ax25->paclen  = ax25_dev->values[AX25_VALUES_PACLEN];
  	ax25->idle    = msecs_to_jiffies(ax25_dev->values[AX25_VALUES_IDLE]);
  	ax25->backoff = ax25_dev->values[AX25_VALUES_BACKOFF];
  
  	if (ax25_dev->values[AX25_VALUES_AXDEFMODE]) {
  		ax25->modulus = AX25_EMODULUS;
  		ax25->window  = ax25_dev->values[AX25_VALUES_EWINDOW];
  	} else {
  		ax25->modulus = AX25_MODULUS;
  		ax25->window  = ax25_dev->values[AX25_VALUES_WINDOW];
  	}
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
451
452
453
454
455
456
457
458
459
  /*
   *	Fill in a created AX.25 created control block with the default
   *	values for a particular device.
   */
  void ax25_fillin_cb(ax25_cb *ax25, ax25_dev *ax25_dev)
  {
  	ax25->ax25_dev = ax25_dev;
  
  	if (ax25->ax25_dev != NULL) {
e1fdb5b39   Ralf Baechle   [AX.25]: Eleminat...
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
  		ax25_fillin_cb_from_dev(ax25, ax25_dev);
  		return;
  	}
  
  	/*
  	 * No device, use kernel / AX.25 spec default values
  	 */
  	ax25->rtt     = msecs_to_jiffies(AX25_DEF_T1) / 2;
  	ax25->t1      = msecs_to_jiffies(AX25_DEF_T1);
  	ax25->t2      = msecs_to_jiffies(AX25_DEF_T2);
  	ax25->t3      = msecs_to_jiffies(AX25_DEF_T3);
  	ax25->n2      = AX25_DEF_N2;
  	ax25->paclen  = AX25_DEF_PACLEN;
  	ax25->idle    = msecs_to_jiffies(AX25_DEF_IDLE);
  	ax25->backoff = AX25_DEF_BACKOFF;
  
  	if (AX25_DEF_AXDEFMODE) {
  		ax25->modulus = AX25_EMODULUS;
  		ax25->window  = AX25_DEF_EWINDOW;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
479
  	} else {
e1fdb5b39   Ralf Baechle   [AX.25]: Eleminat...
480
481
  		ax25->modulus = AX25_MODULUS;
  		ax25->window  = AX25_DEF_WINDOW;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
482
483
484
485
486
487
488
489
490
  	}
  }
  
  /*
   * Create an empty AX.25 control block.
   */
  ax25_cb *ax25_create_cb(void)
  {
  	ax25_cb *ax25;
1b30dd359   Ralf Baechle   [AX.25]: Use kzalloc
491
  	if ((ax25 = kzalloc(sizeof(*ax25), GFP_ATOMIC)) == NULL)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
492
  		return NULL;
b6d52ede2   Reshetova, Elena   net, ax25: conver...
493
  	refcount_set(&ax25->refcount, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
494
495
496
497
498
  
  	skb_queue_head_init(&ax25->write_queue);
  	skb_queue_head_init(&ax25->frag_queue);
  	skb_queue_head_init(&ax25->ack_queue);
  	skb_queue_head_init(&ax25->reseq_queue);
21fab4a86   Jarek Poplawski   [AX25] ax25_timer...
499
  	ax25_setup_timers(ax25);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
500
501
502
503
504
505
506
507
508
509
510
511
512
513
  
  	ax25_fillin_cb(ax25, NULL);
  
  	ax25->state = AX25_STATE_0;
  
  	return ax25;
  }
  
  /*
   *	Handling for system calls applied via the various interfaces to an
   *	AX25 socket object
   */
  
  static int ax25_setsockopt(struct socket *sock, int level, int optname,
a7b75c5a8   Christoph Hellwig   net: pass a sockp...
514
  		sockptr_t optval, unsigned int optlen)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
515
516
517
518
519
  {
  	struct sock *sk = sock->sk;
  	ax25_cb *ax25;
  	struct net_device *dev;
  	char devname[IFNAMSIZ];
ba1cffe02   Xi Wang   ax25: avoid overf...
520
521
  	unsigned long opt;
  	int res = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
522
523
524
  
  	if (level != SOL_AX25)
  		return -ENOPROTOOPT;
ba1cffe02   Xi Wang   ax25: avoid overf...
525
  	if (optlen < sizeof(unsigned int))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
526
  		return -EINVAL;
a7b75c5a8   Christoph Hellwig   net: pass a sockp...
527
  	if (copy_from_sockptr(&opt, optval, sizeof(unsigned int)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
528
529
530
  		return -EFAULT;
  
  	lock_sock(sk);
3200392b8   David Miller   ax25: Stop using ...
531
  	ax25 = sk_to_ax25(sk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
  
  	switch (optname) {
  	case AX25_WINDOW:
  		if (ax25->modulus == AX25_MODULUS) {
  			if (opt < 1 || opt > 7) {
  				res = -EINVAL;
  				break;
  			}
  		} else {
  			if (opt < 1 || opt > 63) {
  				res = -EINVAL;
  				break;
  			}
  		}
  		ax25->window = opt;
  		break;
  
  	case AX25_T1:
be639ac69   Ralf Baechle   NET: AX.25: Check...
550
  		if (opt < 1 || opt > ULONG_MAX / HZ) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
551
552
553
  			res = -EINVAL;
  			break;
  		}
f16f3026d   Eric Dumazet   [AX25]: sparse cl...
554
  		ax25->rtt = (opt * HZ) >> 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
555
556
557
558
  		ax25->t1  = opt * HZ;
  		break;
  
  	case AX25_T2:
be639ac69   Ralf Baechle   NET: AX.25: Check...
559
  		if (opt < 1 || opt > ULONG_MAX / HZ) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
  			res = -EINVAL;
  			break;
  		}
  		ax25->t2 = opt * HZ;
  		break;
  
  	case AX25_N2:
  		if (opt < 1 || opt > 31) {
  			res = -EINVAL;
  			break;
  		}
  		ax25->n2 = opt;
  		break;
  
  	case AX25_T3:
be639ac69   Ralf Baechle   NET: AX.25: Check...
575
  		if (opt < 1 || opt > ULONG_MAX / HZ) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
576
577
578
579
580
581
582
  			res = -EINVAL;
  			break;
  		}
  		ax25->t3 = opt * HZ;
  		break;
  
  	case AX25_IDLE:
ba1cffe02   Xi Wang   ax25: avoid overf...
583
  		if (opt > ULONG_MAX / (60 * HZ)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
584
585
586
587
588
589
590
  			res = -EINVAL;
  			break;
  		}
  		ax25->idle = opt * 60 * HZ;
  		break;
  
  	case AX25_BACKOFF:
ba1cffe02   Xi Wang   ax25: avoid overf...
591
  		if (opt > 2) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
  			res = -EINVAL;
  			break;
  		}
  		ax25->backoff = opt;
  		break;
  
  	case AX25_EXTSEQ:
  		ax25->modulus = opt ? AX25_EMODULUS : AX25_MODULUS;
  		break;
  
  	case AX25_PIDINCL:
  		ax25->pidincl = opt ? 1 : 0;
  		break;
  
  	case AX25_IAMDIGI:
  		ax25->iamdigi = opt ? 1 : 0;
  		break;
  
  	case AX25_PACLEN:
  		if (opt < 16 || opt > 65535) {
  			res = -EINVAL;
  			break;
  		}
  		ax25->paclen = opt;
  		break;
  
  	case SO_BINDTODEVICE:
687775cec   Eric Dumazet   ax25: fix setsock...
619
620
621
622
  		if (optlen > IFNAMSIZ - 1)
  			optlen = IFNAMSIZ - 1;
  
  		memset(devname, 0, sizeof(devname));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
623

a7b75c5a8   Christoph Hellwig   net: pass a sockp...
624
  		if (copy_from_sockptr(devname, optval, optlen)) {
2f72291d3   Ralf Baechle   ax25: Add missing...
625
  			res = -EFAULT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
626
627
628
629
630
631
632
  			break;
  		}
  
  		if (sk->sk_type == SOCK_SEQPACKET &&
  		   (sock->state != SS_UNCONNECTED ||
  		    sk->sk_state == TCP_LISTEN)) {
  			res = -EADDRNOTAVAIL;
2f72291d3   Ralf Baechle   ax25: Add missing...
633
634
  			break;
  		}
c43357045   Cong Wang   ax25: fix a use-a...
635
636
  		rtnl_lock();
  		dev = __dev_get_by_name(&init_net, devname);
2f72291d3   Ralf Baechle   ax25: Add missing...
637
  		if (!dev) {
c43357045   Cong Wang   ax25: fix a use-a...
638
  			rtnl_unlock();
2f72291d3   Ralf Baechle   ax25: Add missing...
639
  			res = -ENODEV;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
640
641
642
643
  			break;
  		}
  
  		ax25->ax25_dev = ax25_dev_ax25dev(dev);
c43357045   Cong Wang   ax25: fix a use-a...
644
645
646
647
648
  		if (!ax25->ax25_dev) {
  			rtnl_unlock();
  			res = -ENODEV;
  			break;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
649
  		ax25_fillin_cb(ax25, ax25->ax25_dev);
c43357045   Cong Wang   ax25: fix a use-a...
650
  		rtnl_unlock();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
  		break;
  
  	default:
  		res = -ENOPROTOOPT;
  	}
  	release_sock(sk);
  
  	return res;
  }
  
  static int ax25_getsockopt(struct socket *sock, int level, int optname,
  	char __user *optval, int __user *optlen)
  {
  	struct sock *sk = sock->sk;
  	ax25_cb *ax25;
  	struct ax25_dev *ax25_dev;
  	char devname[IFNAMSIZ];
  	void *valptr;
  	int val = 0;
  	int maxlen, length;
  
  	if (level != SOL_AX25)
  		return -ENOPROTOOPT;
  
  	if (get_user(maxlen, optlen))
  		return -EFAULT;
  
  	if (maxlen < 1)
  		return -EFAULT;
  
  	valptr = (void *) &val;
  	length = min_t(unsigned int, maxlen, sizeof(int));
  
  	lock_sock(sk);
3200392b8   David Miller   ax25: Stop using ...
685
  	ax25 = sk_to_ax25(sk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
  
  	switch (optname) {
  	case AX25_WINDOW:
  		val = ax25->window;
  		break;
  
  	case AX25_T1:
  		val = ax25->t1 / HZ;
  		break;
  
  	case AX25_T2:
  		val = ax25->t2 / HZ;
  		break;
  
  	case AX25_N2:
  		val = ax25->n2;
  		break;
  
  	case AX25_T3:
  		val = ax25->t3 / HZ;
  		break;
  
  	case AX25_IDLE:
  		val = ax25->idle / (60 * HZ);
  		break;
  
  	case AX25_BACKOFF:
  		val = ax25->backoff;
  		break;
  
  	case AX25_EXTSEQ:
  		val = (ax25->modulus == AX25_EMODULUS);
  		break;
  
  	case AX25_PIDINCL:
  		val = ax25->pidincl;
  		break;
  
  	case AX25_IAMDIGI:
  		val = ax25->iamdigi;
  		break;
  
  	case AX25_PACLEN:
  		val = ax25->paclen;
  		break;
  
  	case SO_BINDTODEVICE:
  		ax25_dev = ax25->ax25_dev;
  
  		if (ax25_dev != NULL && ax25_dev->dev != NULL) {
  			strlcpy(devname, ax25_dev->dev->name, sizeof(devname));
  			length = strlen(devname) + 1;
  		} else {
  			*devname = '\0';
  			length = 1;
  		}
  
  		valptr = (void *) devname;
  		break;
  
  	default:
  		release_sock(sk);
  		return -ENOPROTOOPT;
  	}
  	release_sock(sk);
  
  	if (put_user(length, optlen))
  		return -EFAULT;
  
  	return copy_to_user(optval, valptr, length) ? -EFAULT : 0;
  }
  
  static int ax25_listen(struct socket *sock, int backlog)
  {
  	struct sock *sk = sock->sk;
  	int res = 0;
  
  	lock_sock(sk);
  	if (sk->sk_type == SOCK_SEQPACKET && sk->sk_state != TCP_LISTEN) {
  		sk->sk_max_ack_backlog = backlog;
  		sk->sk_state           = TCP_LISTEN;
  		goto out;
  	}
  	res = -EOPNOTSUPP;
  
  out:
  	release_sock(sk);
  
  	return res;
  }
  
  /*
   * XXX: when creating ax25_sock we should update the .obj_size setting
   * below.
   */
  static struct proto ax25_proto = {
  	.name	  = "AX25",
  	.owner	  = THIS_MODULE,
3200392b8   David Miller   ax25: Stop using ...
784
  	.obj_size = sizeof(struct ax25_sock),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
785
  };
3f378b684   Eric Paris   net: pass kern to...
786
787
  static int ax25_create(struct net *net, struct socket *sock, int protocol,
  		       int kern)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
788
789
790
  {
  	struct sock *sk;
  	ax25_cb *ax25;
e9cdced78   Mat Martineau   net: Make sock pr...
791
  	if (protocol < 0 || protocol > U8_MAX)
79462ad02   Hannes Frederic Sowa   net: add validati...
792
  		return -EINVAL;
09ad9bc75   Octavian Purdila   net: use net_eq t...
793
  	if (!net_eq(net, &init_net))
1b8d7ae42   Eric W. Biederman   [NET]: Make socke...
794
  		return -EAFNOSUPPORT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
  	switch (sock->type) {
  	case SOCK_DGRAM:
  		if (protocol == 0 || protocol == PF_AX25)
  			protocol = AX25_P_TEXT;
  		break;
  
  	case SOCK_SEQPACKET:
  		switch (protocol) {
  		case 0:
  		case PF_AX25:	/* For CLX */
  			protocol = AX25_P_TEXT;
  			break;
  		case AX25_P_SEGMENT:
  #ifdef CONFIG_INET
  		case AX25_P_ARP:
  		case AX25_P_IP:
  #endif
  #ifdef CONFIG_NETROM
  		case AX25_P_NETROM:
  #endif
  #ifdef CONFIG_ROSE
  		case AX25_P_ROSE:
  #endif
  			return -ESOCKTNOSUPPORT;
  #ifdef CONFIG_NETROM_MODULE
  		case AX25_P_NETROM:
  			if (ax25_protocol_is_registered(AX25_P_NETROM))
  				return -ESOCKTNOSUPPORT;
ef764a13b   Alan Cox   ax25: Fix missing...
823
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
824
825
826
827
828
829
830
831
832
833
834
835
  #endif
  #ifdef CONFIG_ROSE_MODULE
  		case AX25_P_ROSE:
  			if (ax25_protocol_is_registered(AX25_P_ROSE))
  				return -ESOCKTNOSUPPORT;
  #endif
  		default:
  			break;
  		}
  		break;
  
  	case SOCK_RAW:
0614e2b73   Ori Nimron   ax25: enforce CAP...
836
837
  		if (!capable(CAP_NET_RAW))
  			return -EPERM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
838
839
840
841
  		break;
  	default:
  		return -ESOCKTNOSUPPORT;
  	}
11aa9c28b   Eric W. Biederman   net: Pass kern fr...
842
  	sk = sk_alloc(net, PF_AX25, GFP_ATOMIC, &ax25_proto, kern);
6257ff217   Pavel Emelyanov   [NET]: Forget the...
843
  	if (sk == NULL)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
844
  		return -ENOMEM;
3200392b8   David Miller   ax25: Stop using ...
845
  	ax25 = ax25_sk(sk)->cb = ax25_create_cb();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
  	if (!ax25) {
  		sk_free(sk);
  		return -ENOMEM;
  	}
  
  	sock_init_data(sock, sk);
  
  	sk->sk_destruct = ax25_free_sock;
  	sock->ops    = &ax25_proto_ops;
  	sk->sk_protocol = protocol;
  
  	ax25->sk    = sk;
  
  	return 0;
  }
  
  struct sock *ax25_make_new(struct sock *osk, struct ax25_dev *ax25_dev)
  {
  	struct sock *sk;
  	ax25_cb *ax25, *oax25;
11aa9c28b   Eric W. Biederman   net: Pass kern fr...
866
  	sk = sk_alloc(sock_net(osk), PF_AX25, GFP_ATOMIC, osk->sk_prot, 0);
6257ff217   Pavel Emelyanov   [NET]: Forget the...
867
  	if (sk == NULL)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
  		return NULL;
  
  	if ((ax25 = ax25_create_cb()) == NULL) {
  		sk_free(sk);
  		return NULL;
  	}
  
  	switch (osk->sk_type) {
  	case SOCK_DGRAM:
  		break;
  	case SOCK_SEQPACKET:
  		break;
  	default:
  		sk_free(sk);
  		ax25_cb_put(ax25);
  		return NULL;
  	}
  
  	sock_init_data(NULL, sk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
887
  	sk->sk_type     = osk->sk_type;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
888
889
890
891
892
  	sk->sk_priority = osk->sk_priority;
  	sk->sk_protocol = osk->sk_protocol;
  	sk->sk_rcvbuf   = osk->sk_rcvbuf;
  	sk->sk_sndbuf   = osk->sk_sndbuf;
  	sk->sk_state    = TCP_ESTABLISHED;
53b924b31   Ralf Baechle   [NET]: Fix socket...
893
  	sock_copy_flags(sk, osk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
894

3200392b8   David Miller   ax25: Stop using ...
895
  	oax25 = sk_to_ax25(osk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
  
  	ax25->modulus = oax25->modulus;
  	ax25->backoff = oax25->backoff;
  	ax25->pidincl = oax25->pidincl;
  	ax25->iamdigi = oax25->iamdigi;
  	ax25->rtt     = oax25->rtt;
  	ax25->t1      = oax25->t1;
  	ax25->t2      = oax25->t2;
  	ax25->t3      = oax25->t3;
  	ax25->n2      = oax25->n2;
  	ax25->idle    = oax25->idle;
  	ax25->paclen  = oax25->paclen;
  	ax25->window  = oax25->window;
  
  	ax25->ax25_dev    = ax25_dev;
  	ax25->source_addr = oax25->source_addr;
  
  	if (oax25->digipeat != NULL) {
0459d70ad   Arnaldo Carvalho de Melo   [AX25]: Use kmemdup
914
915
916
  		ax25->digipeat = kmemdup(oax25->digipeat, sizeof(ax25_digi),
  					 GFP_ATOMIC);
  		if (ax25->digipeat == NULL) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
917
918
919
920
  			sk_free(sk);
  			ax25_cb_put(ax25);
  			return NULL;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
921
  	}
3200392b8   David Miller   ax25: Stop using ...
922
  	ax25_sk(sk)->cb = ax25;
8c185ab61   Jarek Poplawski   ax25: Fix possibl...
923
  	sk->sk_destruct = ax25_free_sock;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
  	ax25->sk    = sk;
  
  	return sk;
  }
  
  static int ax25_release(struct socket *sock)
  {
  	struct sock *sk = sock->sk;
  	ax25_cb *ax25;
  
  	if (sk == NULL)
  		return 0;
  
  	sock_hold(sk);
  	sock_orphan(sk);
  	lock_sock(sk);
3200392b8   David Miller   ax25: Stop using ...
940
  	ax25 = sk_to_ax25(sk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
  
  	if (sk->sk_type == SOCK_SEQPACKET) {
  		switch (ax25->state) {
  		case AX25_STATE_0:
  			release_sock(sk);
  			ax25_disconnect(ax25, 0);
  			lock_sock(sk);
  			ax25_destroy_socket(ax25);
  			break;
  
  		case AX25_STATE_1:
  		case AX25_STATE_2:
  			ax25_send_control(ax25, AX25_DISC, AX25_POLLON, AX25_COMMAND);
  			release_sock(sk);
  			ax25_disconnect(ax25, 0);
  			lock_sock(sk);
4a7d99ea1   Basil Gunn   AX.25: Close sock...
957
958
  			if (!sock_flag(ax25->sk, SOCK_DESTROY))
  				ax25_destroy_socket(ax25);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
  			break;
  
  		case AX25_STATE_3:
  		case AX25_STATE_4:
  			ax25_clear_queues(ax25);
  			ax25->n2count = 0;
  
  			switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
  			case AX25_PROTO_STD_SIMPLEX:
  			case AX25_PROTO_STD_DUPLEX:
  				ax25_send_control(ax25,
  						  AX25_DISC,
  						  AX25_POLLON,
  						  AX25_COMMAND);
  				ax25_stop_t2timer(ax25);
  				ax25_stop_t3timer(ax25);
  				ax25_stop_idletimer(ax25);
  				break;
  #ifdef CONFIG_AX25_DAMA_SLAVE
  			case AX25_PROTO_DAMA_SLAVE:
  				ax25_stop_t3timer(ax25);
  				ax25_stop_idletimer(ax25);
  				break;
  #endif
  			}
  			ax25_calculate_t1(ax25);
  			ax25_start_t1timer(ax25);
  			ax25->state = AX25_STATE_2;
  			sk->sk_state                = TCP_CLOSE;
  			sk->sk_shutdown            |= SEND_SHUTDOWN;
  			sk->sk_state_change(sk);
  			sock_set_flag(sk, SOCK_DESTROY);
  			break;
  
  		default:
  			break;
  		}
  	} else {
  		sk->sk_state     = TCP_CLOSE;
  		sk->sk_shutdown |= SEND_SHUTDOWN;
  		sk->sk_state_change(sk);
  		ax25_destroy_socket(ax25);
  	}
  
  	sock->sk   = NULL;
  	release_sock(sk);
  	sock_put(sk);
  
  	return 0;
  }
  
  /*
   *	We support a funny extension here so you can (as root) give any callsign
   *	digipeated via a local address as source. This hack is obsolete now
   *	that we've implemented support for SO_BINDTODEVICE. It is however small
   *	and trivially backward compatible.
   */
  static int ax25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  {
  	struct sock *sk = sock->sk;
  	struct full_sockaddr_ax25 *addr = (struct full_sockaddr_ax25 *)uaddr;
  	ax25_dev *ax25_dev = NULL;
01d7dd0e9   Ralf Baechle   [AX25]: UID fixes
1021
1022
  	ax25_uid_assoc *user;
  	ax25_address call;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1023
1024
1025
1026
  	ax25_cb *ax25;
  	int err = 0;
  
  	if (addr_len != sizeof(struct sockaddr_ax25) &&
1987e7b48   maximilian attems   [AX25]: Kill ax25...
1027
1028
1029
1030
  	    addr_len != sizeof(struct full_sockaddr_ax25))
  		/* support for old structure may go away some time
  		 * ax25_bind(): uses old (6 digipeater) socket structure.
  		 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1031
  		if ((addr_len < sizeof(struct sockaddr_ax25) + sizeof(ax25_address) * 6) ||
1987e7b48   maximilian attems   [AX25]: Kill ax25...
1032
  		    (addr_len > sizeof(struct full_sockaddr_ax25)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1033
  			return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1034
1035
1036
  
  	if (addr->fsa_ax25.sax25_family != AF_AX25)
  		return -EINVAL;
734004072   David Howells   CRED: Wrap task c...
1037
  	user = ax25_findbyuid(current_euid());
01d7dd0e9   Ralf Baechle   [AX25]: UID fixes
1038
1039
1040
1041
1042
1043
1044
1045
  	if (user) {
  		call = user->call;
  		ax25_uid_put(user);
  	} else {
  		if (ax25_uid_policy && !capable(CAP_NET_ADMIN))
  			return -EACCES;
  
  		call = addr->fsa_ax25.sax25_call;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1046
1047
1048
  	}
  
  	lock_sock(sk);
3200392b8   David Miller   ax25: Stop using ...
1049
  	ax25 = sk_to_ax25(sk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1050
1051
1052
1053
  	if (!sock_flag(sk, SOCK_ZAPPED)) {
  		err = -EINVAL;
  		goto out;
  	}
01d7dd0e9   Ralf Baechle   [AX25]: UID fixes
1054
  	ax25->source_addr = call;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
  
  	/*
  	 * User already set interface with SO_BINDTODEVICE
  	 */
  	if (ax25->ax25_dev != NULL)
  		goto done;
  
  	if (addr_len > sizeof(struct sockaddr_ax25) && addr->fsa_ax25.sax25_ndigis == 1) {
  		if (ax25cmp(&addr->fsa_digipeater[0], &null_ax25_address) != 0 &&
  		    (ax25_dev = ax25_addr_ax25dev(&addr->fsa_digipeater[0])) == NULL) {
  			err = -EADDRNOTAVAIL;
  			goto out;
  		}
  	} else {
  		if ((ax25_dev = ax25_addr_ax25dev(&addr->fsa_ax25.sax25_call)) == NULL) {
  			err = -EADDRNOTAVAIL;
  			goto out;
  		}
  	}
  
  	if (ax25_dev != NULL)
  		ax25_fillin_cb(ax25, ax25_dev);
  
  done:
  	ax25_cb_add(ax25);
  	sock_reset_flag(sk, SOCK_ZAPPED);
  
  out:
  	release_sock(sk);
49339ccae   Julia Lawall   net/ax25: Use ava...
1084
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1085
1086
1087
1088
1089
  }
  
  /*
   *	FIXME: nonblock behaviour looks like it may have a bug.
   */
c9266b99e   Ralf Baechle   [AX.25]: Mark all...
1090
1091
  static int __must_check ax25_connect(struct socket *sock,
  	struct sockaddr *uaddr, int addr_len, int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1092
1093
  {
  	struct sock *sk = sock->sk;
3200392b8   David Miller   ax25: Stop using ...
1094
  	ax25_cb *ax25 = sk_to_ax25(sk), *ax25t;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1095
1096
1097
1098
1099
1100
1101
  	struct full_sockaddr_ax25 *fsa = (struct full_sockaddr_ax25 *)uaddr;
  	ax25_digi *digi = NULL;
  	int ct = 0, err = 0;
  
  	/*
  	 * some sanity checks. code further down depends on this
  	 */
27d1cba21   maximilian attems   [AX25]: Kill user...
1102
1103
1104
1105
1106
1107
1108
1109
1110
  	if (addr_len == sizeof(struct sockaddr_ax25))
  		/* support for this will go away in early 2.5.x
  		 * ax25_connect(): uses obsolete socket structure
  		 */
  		;
  	else if (addr_len != sizeof(struct full_sockaddr_ax25))
  		/* support for old structure may go away some time
  		 * ax25_connect(): uses old (6 digipeater) socket structure.
  		 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1111
  		if ((addr_len < sizeof(struct sockaddr_ax25) + sizeof(ax25_address) * 6) ||
27d1cba21   maximilian attems   [AX25]: Kill user...
1112
  		    (addr_len > sizeof(struct full_sockaddr_ax25)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1113
  			return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1114

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
  
  	if (fsa->fsa_ax25.sax25_family != AF_AX25)
  		return -EINVAL;
  
  	lock_sock(sk);
  
  	/* deal with restarts */
  	if (sock->state == SS_CONNECTING) {
  		switch (sk->sk_state) {
  		case TCP_SYN_SENT: /* still trying */
  			err = -EINPROGRESS;
75606dc69   Ralf Baechle   [AX25/NETROM/ROSE...
1126
  			goto out_release;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1127
1128
1129
  
  		case TCP_ESTABLISHED: /* connection established */
  			sock->state = SS_CONNECTED;
75606dc69   Ralf Baechle   [AX25/NETROM/ROSE...
1130
  			goto out_release;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1131
1132
1133
1134
  
  		case TCP_CLOSE: /* connection refused */
  			sock->state = SS_UNCONNECTED;
  			err = -ECONNREFUSED;
75606dc69   Ralf Baechle   [AX25/NETROM/ROSE...
1135
  			goto out_release;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1136
1137
1138
1139
1140
  		}
  	}
  
  	if (sk->sk_state == TCP_ESTABLISHED && sk->sk_type == SOCK_SEQPACKET) {
  		err = -EISCONN;	/* No reconnect on a seqpacket socket */
75606dc69   Ralf Baechle   [AX25/NETROM/ROSE...
1141
  		goto out_release;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1142
1143
1144
1145
  	}
  
  	sk->sk_state   = TCP_CLOSE;
  	sock->state = SS_UNCONNECTED;
a51482bde   Jesper Juhl   [NET]: kfree cleanup
1146
1147
  	kfree(ax25->digipeat);
  	ax25->digipeat = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1148
1149
1150
1151
1152
1153
1154
  
  	/*
  	 *	Handle digi-peaters to be used.
  	 */
  	if (addr_len > sizeof(struct sockaddr_ax25) &&
  	    fsa->fsa_ax25.sax25_ndigis != 0) {
  		/* Valid number of digipeaters ? */
2f2a7ffad   Peilin Ye   AX.25: Fix out-of...
1155
  		if (fsa->fsa_ax25.sax25_ndigis < 1 ||
17ad73e94   Dan Carpenter   AX.25: Prevent in...
1156
  		    fsa->fsa_ax25.sax25_ndigis > AX25_MAX_DIGIS ||
2f2a7ffad   Peilin Ye   AX.25: Fix out-of...
1157
1158
  		    addr_len < sizeof(struct sockaddr_ax25) +
  		    sizeof(ax25_address) * fsa->fsa_ax25.sax25_ndigis) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1159
  			err = -EINVAL;
75606dc69   Ralf Baechle   [AX25/NETROM/ROSE...
1160
  			goto out_release;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1161
1162
1163
1164
  		}
  
  		if ((digi = kmalloc(sizeof(ax25_digi), GFP_KERNEL)) == NULL) {
  			err = -ENOBUFS;
75606dc69   Ralf Baechle   [AX25/NETROM/ROSE...
1165
  			goto out_release;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
  		}
  
  		digi->ndigi      = fsa->fsa_ax25.sax25_ndigis;
  		digi->lastrepeat = -1;
  
  		while (ct < fsa->fsa_ax25.sax25_ndigis) {
  			if ((fsa->fsa_digipeater[ct].ax25_call[6] &
  			     AX25_HBIT) && ax25->iamdigi) {
  				digi->repeated[ct] = 1;
  				digi->lastrepeat   = ct;
  			} else {
  				digi->repeated[ct] = 0;
  			}
  			digi->calls[ct] = fsa->fsa_digipeater[ct];
  			ct++;
  		}
  	}
  
  	/*
  	 *	Must bind first - autobinding in this may or may not work. If
  	 *	the socket is already bound, check to see if the device has
  	 *	been filled in, error if it hasn't.
  	 */
  	if (sock_flag(sk, SOCK_ZAPPED)) {
  		/* check if we can remove this feature. It is broken. */
  		printk(KERN_WARNING "ax25_connect(): %s uses autobind, please contact jreuter@yaina.de
  ",
  			current->comm);
  		if ((err = ax25_rt_autobind(ax25, &fsa->fsa_ax25.sax25_call)) < 0) {
  			kfree(digi);
75606dc69   Ralf Baechle   [AX25/NETROM/ROSE...
1196
  			goto out_release;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1197
1198
1199
1200
1201
1202
1203
1204
  		}
  
  		ax25_fillin_cb(ax25, ax25->ax25_dev);
  		ax25_cb_add(ax25);
  	} else {
  		if (ax25->ax25_dev == NULL) {
  			kfree(digi);
  			err = -EHOSTUNREACH;
75606dc69   Ralf Baechle   [AX25/NETROM/ROSE...
1205
  			goto out_release;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1206
1207
1208
1209
1210
  		}
  	}
  
  	if (sk->sk_type == SOCK_SEQPACKET &&
  	    (ax25t=ax25_find_cb(&ax25->source_addr, &fsa->fsa_ax25.sax25_call, digi,
528930b91   YOSHIFUJI Hideaki   [NET] AX25: Fix w...
1211
  			 ax25->ax25_dev->dev))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1212
1213
1214
  		kfree(digi);
  		err = -EADDRINUSE;		/* Already such a connection */
  		ax25_cb_put(ax25t);
75606dc69   Ralf Baechle   [AX25/NETROM/ROSE...
1215
  		goto out_release;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1216
1217
1218
1219
1220
1221
1222
1223
1224
  	}
  
  	ax25->dest_addr = fsa->fsa_ax25.sax25_call;
  	ax25->digipeat  = digi;
  
  	/* First the easy one */
  	if (sk->sk_type != SOCK_SEQPACKET) {
  		sock->state = SS_CONNECTED;
  		sk->sk_state   = TCP_ESTABLISHED;
75606dc69   Ralf Baechle   [AX25/NETROM/ROSE...
1225
  		goto out_release;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
  	}
  
  	/* Move to connecting socket, ax.25 lapb WAIT_UA.. */
  	sock->state        = SS_CONNECTING;
  	sk->sk_state          = TCP_SYN_SENT;
  
  	switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
  	case AX25_PROTO_STD_SIMPLEX:
  	case AX25_PROTO_STD_DUPLEX:
  		ax25_std_establish_data_link(ax25);
  		break;
  
  #ifdef CONFIG_AX25_DAMA_SLAVE
  	case AX25_PROTO_DAMA_SLAVE:
  		ax25->modulus = AX25_MODULUS;
  		ax25->window  = ax25->ax25_dev->values[AX25_VALUES_WINDOW];
  		if (ax25->ax25_dev->dama.slave)
  			ax25_ds_establish_data_link(ax25);
  		else
  			ax25_std_establish_data_link(ax25);
  		break;
  #endif
  	}
  
  	ax25->state = AX25_STATE_1;
  
  	ax25_start_heartbeat(ax25);
  
  	/* Now the loop */
  	if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK)) {
  		err = -EINPROGRESS;
75606dc69   Ralf Baechle   [AX25/NETROM/ROSE...
1257
  		goto out_release;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1258
1259
1260
  	}
  
  	if (sk->sk_state == TCP_SYN_SENT) {
75606dc69   Ralf Baechle   [AX25/NETROM/ROSE...
1261
  		DEFINE_WAIT(wait);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1262

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1263
  		for (;;) {
aa3951451   Eric Dumazet   net: sk_sleep() h...
1264
  			prepare_to_wait(sk_sleep(sk), &wait,
bd3b071b9   YOSHIFUJI Hideaki   [NET] AX25: Fix w...
1265
  					TASK_INTERRUPTIBLE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1266
1267
  			if (sk->sk_state != TCP_SYN_SENT)
  				break;
75606dc69   Ralf Baechle   [AX25/NETROM/ROSE...
1268
1269
  			if (!signal_pending(current)) {
  				release_sock(sk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1270
1271
1272
1273
  				schedule();
  				lock_sock(sk);
  				continue;
  			}
75606dc69   Ralf Baechle   [AX25/NETROM/ROSE...
1274
1275
  			err = -ERESTARTSYS;
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1276
  		}
aa3951451   Eric Dumazet   net: sk_sleep() h...
1277
  		finish_wait(sk_sleep(sk), &wait);
75606dc69   Ralf Baechle   [AX25/NETROM/ROSE...
1278
1279
1280
  
  		if (err)
  			goto out_release;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1281
1282
1283
1284
1285
1286
  	}
  
  	if (sk->sk_state != TCP_ESTABLISHED) {
  		/* Not in ABM, not in WAIT_UA -> failed */
  		sock->state = SS_UNCONNECTED;
  		err = sock_error(sk);	/* Always set at this point */
75606dc69   Ralf Baechle   [AX25/NETROM/ROSE...
1287
  		goto out_release;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1288
1289
1290
  	}
  
  	sock->state = SS_CONNECTED;
75606dc69   Ralf Baechle   [AX25/NETROM/ROSE...
1291
1292
  	err = 0;
  out_release:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1293
1294
1295
1296
  	release_sock(sk);
  
  	return err;
  }
cdfbabfb2   David Howells   net: Work around ...
1297
1298
  static int ax25_accept(struct socket *sock, struct socket *newsock, int flags,
  		       bool kern)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1299
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1300
1301
  	struct sk_buff *skb;
  	struct sock *newsk;
75606dc69   Ralf Baechle   [AX25/NETROM/ROSE...
1302
  	DEFINE_WAIT(wait);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
  	struct sock *sk;
  	int err = 0;
  
  	if (sock->state != SS_UNCONNECTED)
  		return -EINVAL;
  
  	if ((sk = sock->sk) == NULL)
  		return -EINVAL;
  
  	lock_sock(sk);
  	if (sk->sk_type != SOCK_SEQPACKET) {
  		err = -EOPNOTSUPP;
  		goto out;
  	}
  
  	if (sk->sk_state != TCP_LISTEN) {
  		err = -EINVAL;
  		goto out;
  	}
  
  	/*
  	 *	The read queue this time is holding sockets ready to use
  	 *	hooked into the SABM we saved
  	 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1327
  	for (;;) {
aa3951451   Eric Dumazet   net: sk_sleep() h...
1328
  		prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1329
1330
1331
  		skb = skb_dequeue(&sk->sk_receive_queue);
  		if (skb)
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1332
  		if (flags & O_NONBLOCK) {
75606dc69   Ralf Baechle   [AX25/NETROM/ROSE...
1333
1334
  			err = -EWOULDBLOCK;
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1335
  		}
75606dc69   Ralf Baechle   [AX25/NETROM/ROSE...
1336
1337
  		if (!signal_pending(current)) {
  			release_sock(sk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1338
1339
1340
1341
  			schedule();
  			lock_sock(sk);
  			continue;
  		}
75606dc69   Ralf Baechle   [AX25/NETROM/ROSE...
1342
1343
  		err = -ERESTARTSYS;
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1344
  	}
aa3951451   Eric Dumazet   net: sk_sleep() h...
1345
  	finish_wait(sk_sleep(sk), &wait);
75606dc69   Ralf Baechle   [AX25/NETROM/ROSE...
1346
1347
1348
  
  	if (err)
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1349
1350
  
  	newsk		 = skb->sk;
9375cb8a1   David S. Miller   ax25: Use sock_gr...
1351
  	sock_graft(newsk, newsock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1352
1353
1354
  
  	/* Now attach up the new socket */
  	kfree_skb(skb);
7976a11b3   Eric Dumazet   net: use helpers ...
1355
  	sk_acceptq_removed(sk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1356
1357
1358
1359
1360
1361
1362
1363
1364
  	newsock->state = SS_CONNECTED;
  
  out:
  	release_sock(sk);
  
  	return err;
  }
  
  static int ax25_getname(struct socket *sock, struct sockaddr *uaddr,
9b2c45d47   Denys Vlasenko   net: make getname...
1365
  	int peer)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1366
1367
1368
1369
1370
1371
  {
  	struct full_sockaddr_ax25 *fsa = (struct full_sockaddr_ax25 *)uaddr;
  	struct sock *sk = sock->sk;
  	unsigned char ndigi, i;
  	ax25_cb *ax25;
  	int err = 0;
5b919f833   Kees Cook   net: ax25: fix in...
1372
  	memset(fsa, 0, sizeof(*fsa));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1373
  	lock_sock(sk);
3200392b8   David Miller   ax25: Stop using ...
1374
  	ax25 = sk_to_ax25(sk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1375
1376
1377
1378
1379
1380
1381
1382
1383
  
  	if (peer != 0) {
  		if (sk->sk_state != TCP_ESTABLISHED) {
  			err = -ENOTCONN;
  			goto out;
  		}
  
  		fsa->fsa_ax25.sax25_family = AF_AX25;
  		fsa->fsa_ax25.sax25_call   = ax25->dest_addr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
  
  		if (ax25->digipeat != NULL) {
  			ndigi = ax25->digipeat->ndigi;
  			fsa->fsa_ax25.sax25_ndigis = ndigi;
  			for (i = 0; i < ndigi; i++)
  				fsa->fsa_digipeater[i] =
  						ax25->digipeat->calls[i];
  		}
  	} else {
  		fsa->fsa_ax25.sax25_family = AF_AX25;
  		fsa->fsa_ax25.sax25_call   = ax25->source_addr;
  		fsa->fsa_ax25.sax25_ndigis = 1;
  		if (ax25->ax25_dev != NULL) {
  			memcpy(&fsa->fsa_digipeater[0],
  			       ax25->ax25_dev->dev->dev_addr, AX25_ADDR_LEN);
  		} else {
  			fsa->fsa_digipeater[0] = null_ax25_address;
  		}
  	}
9b2c45d47   Denys Vlasenko   net: make getname...
1403
  	err = sizeof (struct full_sockaddr_ax25);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1404
1405
1406
1407
1408
1409
  
  out:
  	release_sock(sk);
  
  	return err;
  }
1b7841404   Ying Xue   net: Remove iocb ...
1410
  static int ax25_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1411
  {
342dfc306   Steffen Hurrle   net: add build-ti...
1412
  	DECLARE_SOCKADDR(struct sockaddr_ax25 *, usax, msg->msg_name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1413
1414
1415
1416
  	struct sock *sk = sock->sk;
  	struct sockaddr_ax25 sax;
  	struct sk_buff *skb;
  	ax25_digi dtmp, *dp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1417
1418
1419
1420
1421
1422
1423
1424
  	ax25_cb *ax25;
  	size_t size;
  	int lv, err, addr_len = msg->msg_namelen;
  
  	if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_CMSG_COMPAT))
  		return -EINVAL;
  
  	lock_sock(sk);
3200392b8   David Miller   ax25: Stop using ...
1425
  	ax25 = sk_to_ax25(sk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
  
  	if (sock_flag(sk, SOCK_ZAPPED)) {
  		err = -EADDRNOTAVAIL;
  		goto out;
  	}
  
  	if (sk->sk_shutdown & SEND_SHUTDOWN) {
  		send_sig(SIGPIPE, current, 0);
  		err = -EPIPE;
  		goto out;
  	}
  
  	if (ax25->ax25_dev == NULL) {
  		err = -ENETUNREACH;
  		goto out;
  	}
  
  	if (len > ax25->ax25_dev->dev->mtu) {
  		err = -EMSGSIZE;
  		goto out;
  	}
528930b91   YOSHIFUJI Hideaki   [NET] AX25: Fix w...
1447

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1448
1449
1450
1451
1452
  	if (usax != NULL) {
  		if (usax->sax25_family != AF_AX25) {
  			err = -EINVAL;
  			goto out;
  		}
27d1cba21   maximilian attems   [AX25]: Kill user...
1453
1454
1455
1456
1457
1458
1459
1460
  		if (addr_len == sizeof(struct sockaddr_ax25))
  			/* ax25_sendmsg(): uses obsolete socket structure */
  			;
  		else if (addr_len != sizeof(struct full_sockaddr_ax25))
  			/* support for old structure may go away some time
  			 * ax25_sendmsg(): uses old (6 digipeater)
  			 * socket structure.
  			 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1461
  			if ((addr_len < sizeof(struct sockaddr_ax25) + sizeof(ax25_address) * 6) ||
528930b91   YOSHIFUJI Hideaki   [NET] AX25: Fix w...
1462
1463
  			    (addr_len > sizeof(struct full_sockaddr_ax25))) {
  				err = -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1464
1465
  				goto out;
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1466
1467
1468
1469
1470
1471
  
  		if (addr_len > sizeof(struct sockaddr_ax25) && usax->sax25_ndigis != 0) {
  			int ct           = 0;
  			struct full_sockaddr_ax25 *fsa = (struct full_sockaddr_ax25 *)usax;
  
  			/* Valid number of digipeaters ? */
17ad73e94   Dan Carpenter   AX.25: Prevent in...
1472
1473
1474
  			if (usax->sax25_ndigis < 1 ||
  			    usax->sax25_ndigis > AX25_MAX_DIGIS ||
  			    addr_len < sizeof(struct sockaddr_ax25) +
8885bb062   Peilin Ye   AX.25: Prevent ou...
1475
  			    sizeof(ax25_address) * usax->sax25_ndigis) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
  				err = -EINVAL;
  				goto out;
  			}
  
  			dtmp.ndigi      = usax->sax25_ndigis;
  
  			while (ct < usax->sax25_ndigis) {
  				dtmp.repeated[ct] = 0;
  				dtmp.calls[ct]    = fsa->fsa_digipeater[ct];
  				ct++;
  			}
  
  			dtmp.lastrepeat = 0;
  		}
  
  		sax = *usax;
  		if (sk->sk_type == SOCK_SEQPACKET &&
  		    ax25cmp(&ax25->dest_addr, &sax.sax25_call)) {
  			err = -EISCONN;
  			goto out;
  		}
  		if (usax->sax25_ndigis == 0)
  			dp = NULL;
  		else
  			dp = &dtmp;
  	} else {
  		/*
  		 *	FIXME: 1003.1g - if the socket is like this because
  		 *	it has become closed (not started closed) and is VC
  		 *	we ought to SIGPIPE, EPIPE
  		 */
  		if (sk->sk_state != TCP_ESTABLISHED) {
  			err = -ENOTCONN;
  			goto out;
  		}
  		sax.sax25_family = AF_AX25;
  		sax.sax25_call   = ax25->dest_addr;
  		dp = ax25->digipeat;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1515
  	/* Build a packet */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1516
1517
1518
1519
1520
1521
1522
1523
  	/* Assume the worst case */
  	size = len + ax25->ax25_dev->dev->hard_header_len;
  
  	skb = sock_alloc_send_skb(sk, size, msg->msg_flags&MSG_DONTWAIT, &err);
  	if (skb == NULL)
  		goto out;
  
  	skb_reserve(skb, size - len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1524
  	/* User data follows immediately after the AX.25 data */
6ce8e9ce5   Al Viro   new helper: memcp...
1525
  	if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1526
1527
1528
1529
  		err = -EFAULT;
  		kfree_skb(skb);
  		goto out;
  	}
c1d2bbe1c   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
1530
  	skb_reset_network_header(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1531
1532
  
  	/* Add the PID if one is not supplied by the user in the skb */
967b05f64   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
1533
  	if (!ax25->pidincl)
d58ff3512   Johannes Berg   networking: make ...
1534
  		*(u8 *)skb_push(skb, 1) = sk->sk_protocol;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1535

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
  	if (sk->sk_type == SOCK_SEQPACKET) {
  		/* Connected mode sockets go via the LAPB machine */
  		if (sk->sk_state != TCP_ESTABLISHED) {
  			kfree_skb(skb);
  			err = -ENOTCONN;
  			goto out;
  		}
  
  		/* Shove it onto the queue and kick */
  		ax25_output(ax25, ax25->paclen, skb);
  
  		err = len;
  		goto out;
  	}
967b05f64   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
1550
  	skb_push(skb, 1 + ax25_addr_size(dp));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1551

8849b720e   Ralf Baechle   NET: AX.25, NETRO...
1552
  	/* Building AX.25 Header */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1553
1554
  
  	/* Build an AX.25 header */
967b05f64   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
1555
1556
  	lv = ax25_addr_build(skb->data, &ax25->source_addr, &sax.sax25_call,
  			     dp, AX25_COMMAND, AX25_MODULUS);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1557

967b05f64   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
1558
  	skb_set_transport_header(skb, lv);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1559

9c70220b7   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
1560
  	*skb_transport_header(skb) = AX25_UI;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1561
1562
  
  	/* Datagram frames go straight out of the door as UI */
29c4be51e   Arnaldo Carvalho de Melo   [AX25]: make ax25...
1563
  	ax25_queue_xmit(skb, ax25->ax25_dev->dev);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1564
1565
1566
1567
1568
1569
1570
1571
  
  	err = len;
  
  out:
  	release_sock(sk);
  
  	return err;
  }
1b7841404   Ying Xue   net: Remove iocb ...
1572
1573
  static int ax25_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
  			int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
  {
  	struct sock *sk = sock->sk;
  	struct sk_buff *skb;
  	int copied;
  	int err = 0;
  
  	lock_sock(sk);
  	/*
  	 * 	This works for seqpacket too. The receiver has ordered the
  	 *	queue for us! We do one quick check first though
  	 */
  	if (sk->sk_type == SOCK_SEQPACKET && sk->sk_state != TCP_ESTABLISHED) {
  		err =  -ENOTCONN;
  		goto out;
  	}
  
  	/* Now we can treat all alike */
  	skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
528930b91   YOSHIFUJI Hideaki   [NET] AX25: Fix w...
1592
  				flags & MSG_DONTWAIT, &err);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1593
1594
  	if (skb == NULL)
  		goto out;
3200392b8   David Miller   ax25: Stop using ...
1595
  	if (!sk_to_ax25(sk)->pidincl)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1596
  		skb_pull(skb, 1);		/* Remove PID */
badff6d01   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
1597
1598
  	skb_reset_transport_header(skb);
  	copied = skb->len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1599
1600
1601
1602
1603
  
  	if (copied > size) {
  		copied = size;
  		msg->msg_flags |= MSG_TRUNC;
  	}
51f3d02b9   David S. Miller   net: Add and use ...
1604
  	skb_copy_datagram_msg(skb, 0, msg, copied);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1605

f3d334260   Hannes Frederic Sowa   net: rework recvm...
1606
  	if (msg->msg_name) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1607
1608
  		ax25_digi digi;
  		ax25_address src;
98e399f82   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
1609
  		const unsigned char *mac = skb_mac_header(skb);
342dfc306   Steffen Hurrle   net: add build-ti...
1610
  		DECLARE_SOCKADDR(struct sockaddr_ax25 *, sax, msg->msg_name);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1611

ef3313e84   Mathias Krause   ax25: fix info le...
1612
  		memset(sax, 0, sizeof(struct full_sockaddr_ax25));
98e399f82   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
1613
1614
  		ax25_addr_parse(mac + 1, skb->data - mac - 1, &src, NULL,
  				&digi, NULL, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
  		sax->sax25_family = AF_AX25;
  		/* We set this correctly, even though we may not let the
  		   application know the digi calls further down (because it
  		   did NOT ask to know them).  This could get political... **/
  		sax->sax25_ndigis = digi.ndigi;
  		sax->sax25_call   = src;
  
  		if (sax->sax25_ndigis != 0) {
  			int ct;
  			struct full_sockaddr_ax25 *fsa = (struct full_sockaddr_ax25 *)sax;
  
  			for (ct = 0; ct < digi.ndigi; ct++)
  				fsa->fsa_digipeater[ct] = digi.calls[ct];
  		}
  		msg->msg_namelen = sizeof(struct full_sockaddr_ax25);
  	}
  
  	skb_free_datagram(sk, skb);
  	err = copied;
  
  out:
  	release_sock(sk);
  
  	return err;
  }
  
  static int ax25_shutdown(struct socket *sk, int how)
  {
  	/* FIXME - generate DM and RNR states */
  	return -EOPNOTSUPP;
  }
  
  static int ax25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  {
  	struct sock *sk = sock->sk;
  	void __user *argp = (void __user *)arg;
  	int res = 0;
  
  	lock_sock(sk);
  	switch (cmd) {
  	case TIOCOUTQ: {
  		long amount;
31e6d363a   Eric Dumazet   net: correct off-...
1657
1658
  
  		amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
  		if (amount < 0)
  			amount = 0;
  		res = put_user(amount, (int __user *)argp);
  		break;
  	}
  
  	case TIOCINQ: {
  		struct sk_buff *skb;
  		long amount = 0L;
  		/* These two are safe on a single CPU system as only user tasks fiddle here */
  		if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
  			amount = skb->len;
20b7d10a3   Ralf Baechle   [AX.25/ROSE]: Whi...
1671
  		res = put_user(amount, (int __user *) argp);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1672
1673
  		break;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
  	case SIOCAX25ADDUID:	/* Add a uid to the uid/call map table */
  	case SIOCAX25DELUID:	/* Delete a uid from the uid/call map table */
  	case SIOCAX25GETUID: {
  		struct sockaddr_ax25 sax25;
  		if (copy_from_user(&sax25, argp, sizeof(sax25))) {
  			res = -EFAULT;
  			break;
  		}
  		res = ax25_uid_ioctl(cmd, &sax25);
  		break;
  	}
  
  	case SIOCAX25NOUID: {	/* Set the default policy (default/bar) */
  		long amount;
  		if (!capable(CAP_NET_ADMIN)) {
  			res = -EPERM;
  			break;
  		}
  		if (get_user(amount, (long __user *)argp)) {
  			res = -EFAULT;
  			break;
  		}
768877537   Dan Carpenter   ax25: cleanup a r...
1696
  		if (amount < 0 || amount > AX25_NOUID_BLOCK) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
  			res = -EINVAL;
  			break;
  		}
  		ax25_uid_policy = amount;
  		res = 0;
  		break;
  	}
  
  	case SIOCADDRT:
  	case SIOCDELRT:
  	case SIOCAX25OPTRT:
  		if (!capable(CAP_NET_ADMIN)) {
  			res = -EPERM;
  			break;
  		}
  		res = ax25_rt_ioctl(cmd, argp);
  		break;
  
  	case SIOCAX25CTLCON:
  		if (!capable(CAP_NET_ADMIN)) {
  			res = -EPERM;
  			break;
  		}
  		res = ax25_ctl_ioctl(cmd, argp);
  		break;
  
  	case SIOCAX25GETINFO:
  	case SIOCAX25GETINFOOLD: {
3200392b8   David Miller   ax25: Stop using ...
1725
  		ax25_cb *ax25 = sk_to_ax25(sk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
  		struct ax25_info_struct ax25_info;
  
  		ax25_info.t1        = ax25->t1   / HZ;
  		ax25_info.t2        = ax25->t2   / HZ;
  		ax25_info.t3        = ax25->t3   / HZ;
  		ax25_info.idle      = ax25->idle / (60 * HZ);
  		ax25_info.n2        = ax25->n2;
  		ax25_info.t1timer   = ax25_display_timer(&ax25->t1timer)   / HZ;
  		ax25_info.t2timer   = ax25_display_timer(&ax25->t2timer)   / HZ;
  		ax25_info.t3timer   = ax25_display_timer(&ax25->t3timer)   / HZ;
  		ax25_info.idletimer = ax25_display_timer(&ax25->idletimer) / (60 * HZ);
  		ax25_info.n2count   = ax25->n2count;
  		ax25_info.state     = ax25->state;
407fc5cf0   Eric Dumazet   ax25: Fix SIOCAX2...
1739
1740
  		ax25_info.rcv_q     = sk_rmem_alloc_get(sk);
  		ax25_info.snd_q     = sk_wmem_alloc_get(sk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
  		ax25_info.vs        = ax25->vs;
  		ax25_info.vr        = ax25->vr;
  		ax25_info.va        = ax25->va;
  		ax25_info.vs_max    = ax25->vs; /* reserved */
  		ax25_info.paclen    = ax25->paclen;
  		ax25_info.window    = ax25->window;
  
  		/* old structure? */
  		if (cmd == SIOCAX25GETINFOOLD) {
  			static int warned = 0;
  			if (!warned) {
  				printk(KERN_INFO "%s uses old SIOCAX25GETINFO
  ",
  					current->comm);
  				warned=1;
  			}
  
  			if (copy_to_user(argp, &ax25_info, sizeof(struct ax25_info_struct_deprecated))) {
  				res = -EFAULT;
  				break;
  			}
  		} else {
  			if (copy_to_user(argp, &ax25_info, sizeof(struct ax25_info_struct))) {
  				res = -EINVAL;
  				break;
  			}
  		}
  		res = 0;
  		break;
  	}
  
  	case SIOCAX25ADDFWD:
  	case SIOCAX25DELFWD: {
  		struct ax25_fwd_struct ax25_fwd;
  		if (!capable(CAP_NET_ADMIN)) {
  			res = -EPERM;
  			break;
  		}
  		if (copy_from_user(&ax25_fwd, argp, sizeof(ax25_fwd))) {
  			res = -EFAULT;
  			break;
  		}
  		res = ax25_fwd_ioctl(cmd, &ax25_fwd);
  		break;
  	}
  
  	case SIOCGIFADDR:
  	case SIOCSIFADDR:
  	case SIOCGIFDSTADDR:
  	case SIOCSIFDSTADDR:
  	case SIOCGIFBRDADDR:
  	case SIOCSIFBRDADDR:
  	case SIOCGIFNETMASK:
  	case SIOCSIFNETMASK:
  	case SIOCGIFMETRIC:
  	case SIOCSIFMETRIC:
  		res = -EINVAL;
  		break;
  
  	default:
b5e5fa5e0   Christoph Hellwig   [NET]: Add a dev_...
1801
  		res = -ENOIOCTLCMD;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
  		break;
  	}
  	release_sock(sk);
  
  	return res;
  }
  
  #ifdef CONFIG_PROC_FS
  
  static void *ax25_info_start(struct seq_file *seq, loff_t *pos)
f16f3026d   Eric Dumazet   [AX25]: sparse cl...
1812
  	__acquires(ax25_list_lock)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1813
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1814
  	spin_lock_bh(&ax25_list_lock);
b512f3d84   Li Zefan   net: ax25: use se...
1815
  	return seq_hlist_start(&ax25_list, *pos);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1816
1817
1818
1819
  }
  
  static void *ax25_info_next(struct seq_file *seq, void *v, loff_t *pos)
  {
b512f3d84   Li Zefan   net: ax25: use se...
1820
  	return seq_hlist_next(v, &ax25_list, pos);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1821
  }
528930b91   YOSHIFUJI Hideaki   [NET] AX25: Fix w...
1822

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1823
  static void ax25_info_stop(struct seq_file *seq, void *v)
f16f3026d   Eric Dumazet   [AX25]: sparse cl...
1824
  	__releases(ax25_list_lock)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1825
1826
1827
1828
1829
1830
  {
  	spin_unlock_bh(&ax25_list_lock);
  }
  
  static int ax25_info_show(struct seq_file *seq, void *v)
  {
b512f3d84   Li Zefan   net: ax25: use se...
1831
  	ax25_cb *ax25 = hlist_entry(v, struct ax25_cb, ax25_node);
f75268cd6   Ralf Baechle   [AX25]: Make ax2a...
1832
  	char buf[11];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1833
1834
1835
1836
1837
1838
1839
  	int k;
  
  
  	/*
  	 * New format:
  	 * magic dev src_addr dest_addr,digi1,digi2,.. st vs vr va t1 t1 t2 t2 t3 t3 idle idle n2 n2 rtt window paclen Snd-Q Rcv-Q inode
  	 */
966cddef2   Fuqian Huang   net: ax25: fix mi...
1840
1841
  	seq_printf(seq, "%p %s %s%s ",
  		   ax25,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1842
  		   ax25->ax25_dev == NULL? "???" : ax25->ax25_dev->dev->name,
f75268cd6   Ralf Baechle   [AX25]: Make ax2a...
1843
  		   ax2asc(buf, &ax25->source_addr),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1844
  		   ax25->iamdigi? "*":"");
f75268cd6   Ralf Baechle   [AX25]: Make ax2a...
1845
  	seq_printf(seq, "%s", ax2asc(buf, &ax25->dest_addr));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1846
1847
1848
  
  	for (k=0; (ax25->digipeat != NULL) && (k < ax25->digipeat->ndigi); k++) {
  		seq_printf(seq, ",%s%s",
f75268cd6   Ralf Baechle   [AX25]: Make ax2a...
1849
  			   ax2asc(buf, &ax25->digipeat->calls[k]),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
  			   ax25->digipeat->repeated[k]? "*":"");
  	}
  
  	seq_printf(seq, " %d %d %d %d %lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %d %d",
  		   ax25->state,
  		   ax25->vs, ax25->vr, ax25->va,
  		   ax25_display_timer(&ax25->t1timer) / HZ, ax25->t1 / HZ,
  		   ax25_display_timer(&ax25->t2timer) / HZ, ax25->t2 / HZ,
  		   ax25_display_timer(&ax25->t3timer) / HZ, ax25->t3 / HZ,
  		   ax25_display_timer(&ax25->idletimer) / (60 * HZ),
  		   ax25->idle / (60 * HZ),
  		   ax25->n2count, ax25->n2,
  		   ax25->rtt / HZ,
  		   ax25->window,
  		   ax25->paclen);
  
  	if (ax25->sk != NULL) {
1105b5d1d   Jarek Poplawski   [AX25] af_ax25: r...
1867
1868
  		seq_printf(seq, " %d %d %lu
  ",
31e6d363a   Eric Dumazet   net: correct off-...
1869
1870
  			   sk_wmem_alloc_get(ax25->sk),
  			   sk_rmem_alloc_get(ax25->sk),
1105b5d1d   Jarek Poplawski   [AX25] af_ax25: r...
1871
  			   sock_i_ino(ax25->sk));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1872
1873
1874
1875
1876
1877
  	} else {
  		seq_puts(seq, " * * *
  ");
  	}
  	return 0;
  }
56b3d975b   Philippe De Muyter   [NET]: Make all i...
1878
  static const struct seq_operations ax25_info_seqops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1879
1880
1881
1882
1883
  	.start = ax25_info_start,
  	.next = ax25_info_next,
  	.stop = ax25_info_stop,
  	.show = ax25_info_show,
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1884
  #endif
ec1b4cf74   Stephen Hemminger   net: mark net_pro...
1885
  static const struct net_proto_family ax25_family_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1886
1887
1888
1889
  	.family =	PF_AX25,
  	.create =	ax25_create,
  	.owner	=	THIS_MODULE,
  };
90ddc4f04   Eric Dumazet   [NET]: move struc...
1890
  static const struct proto_ops ax25_proto_ops = {
4676356b5   Ralf Baechle   [AX.25]: Reformat...
1891
1892
1893
1894
1895
1896
1897
1898
  	.family		= PF_AX25,
  	.owner		= THIS_MODULE,
  	.release	= ax25_release,
  	.bind		= ax25_bind,
  	.connect	= ax25_connect,
  	.socketpair	= sock_no_socketpair,
  	.accept		= ax25_accept,
  	.getname	= ax25_getname,
a11e1d432   Linus Torvalds   Revert changes to...
1899
  	.poll		= datagram_poll,
4676356b5   Ralf Baechle   [AX.25]: Reformat...
1900
  	.ioctl		= ax25_ioctl,
c7cbdbf29   Arnd Bergmann   net: rework SIOCG...
1901
  	.gettstamp	= sock_gettstamp,
4676356b5   Ralf Baechle   [AX.25]: Reformat...
1902
1903
1904
1905
1906
1907
1908
1909
  	.listen		= ax25_listen,
  	.shutdown	= ax25_shutdown,
  	.setsockopt	= ax25_setsockopt,
  	.getsockopt	= ax25_getsockopt,
  	.sendmsg	= ax25_sendmsg,
  	.recvmsg	= ax25_recvmsg,
  	.mmap		= sock_no_mmap,
  	.sendpage	= sock_no_sendpage,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1910
1911
1912
1913
1914
  };
  
  /*
   *	Called by socket.c on kernel start up
   */
7546dd97d   Stephen Hemminger   net: convert usag...
1915
  static struct packet_type ax25_packet_type __read_mostly = {
09640e636   Harvey Harrison   net: replace uses...
1916
  	.type	=	cpu_to_be16(ETH_P_AX25),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1917
1918
1919
1920
  	.func	=	ax25_kiss_rcv,
  };
  
  static struct notifier_block ax25_dev_notifier = {
351638e7d   Jiri Pirko   net: pass info st...
1921
  	.notifier_call = ax25_device_event,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1922
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
  static int __init ax25_init(void)
  {
  	int rc = proto_register(&ax25_proto, 0);
  
  	if (rc != 0)
  		goto out;
  
  	sock_register(&ax25_family_ops);
  	dev_add_pack(&ax25_packet_type);
  	register_netdevice_notifier(&ax25_dev_notifier);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1933

fddda2b7b   Christoph Hellwig   proc: introduce p...
1934
1935
1936
1937
  	proc_create_seq("ax25_route", 0444, init_net.proc_net, &ax25_rt_seqops);
  	proc_create_seq("ax25", 0444, init_net.proc_net, &ax25_info_seqops);
  	proc_create_seq("ax25_calls", 0444, init_net.proc_net,
  			&ax25_uid_seqops);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
  out:
  	return rc;
  }
  module_init(ax25_init);
  
  
  MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
  MODULE_DESCRIPTION("The amateur radio AX.25 link layer protocol");
  MODULE_LICENSE("GPL");
  MODULE_ALIAS_NETPROTO(PF_AX25);
  
  static void __exit ax25_exit(void)
  {
ece31ffd5   Gao feng   net: proc: change...
1951
1952
1953
  	remove_proc_entry("ax25_route", init_net.proc_net);
  	remove_proc_entry("ax25", init_net.proc_net);
  	remove_proc_entry("ax25_calls", init_net.proc_net);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1954

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1955
1956
1957
1958
1959
1960
  	unregister_netdevice_notifier(&ax25_dev_notifier);
  
  	dev_remove_pack(&ax25_packet_type);
  
  	sock_unregister(PF_AX25);
  	proto_unregister(&ax25_proto);
3adadc08c   Eric W. Biederman   net ax25: Reorder...
1961
1962
1963
1964
  
  	ax25_rt_free();
  	ax25_uid_free();
  	ax25_dev_free();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1965
1966
  }
  module_exit(ax25_exit);