Blame view

net/ax25/ax25_in.c 10.6 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
  /*
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * Copyright (C) Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
   * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
   * Copyright (C) Joerg Reuter DL1BKE (jreuter@yaina.de)
   * Copyright (C) Hans-Joachim Hetscher DD8NE (dd8ne@bnv-bamberg.de)
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
12
13
14
15
16
  #include <linux/errno.h>
  #include <linux/types.h>
  #include <linux/socket.h>
  #include <linux/in.h>
  #include <linux/kernel.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17
18
19
20
  #include <linux/timer.h>
  #include <linux/string.h>
  #include <linux/sockios.h>
  #include <linux/net.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
21
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
22
23
24
25
26
27
  #include <net/ax25.h>
  #include <linux/inet.h>
  #include <linux/netdevice.h>
  #include <linux/skbuff.h>
  #include <linux/netfilter.h>
  #include <net/sock.h>
c752f0739   Arnaldo Carvalho de Melo   [TCP]: Move the t...
28
  #include <net/tcp_states.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  #include <asm/uaccess.h>
  #include <asm/system.h>
  #include <linux/fcntl.h>
  #include <linux/mm.h>
  #include <linux/interrupt.h>
  
  /*
   *	Given a fragment, queue it on the fragment queue and if the fragment
   *	is complete, send it back to ax25_rx_iframe.
   */
  static int ax25_rx_fragment(ax25_cb *ax25, struct sk_buff *skb)
  {
  	struct sk_buff *skbn, *skbo;
  
  	if (ax25->fragno != 0) {
  		if (!(*skb->data & AX25_SEG_FIRST)) {
  			if ((ax25->fragno - 1) == (*skb->data & AX25_SEG_REM)) {
  				/* Enqueue fragment */
  				ax25->fragno = *skb->data & AX25_SEG_REM;
  				skb_pull(skb, 1);	/* skip fragno */
  				ax25->fraglen += skb->len;
  				skb_queue_tail(&ax25->frag_queue, skb);
  
  				/* Last fragment received ? */
  				if (ax25->fragno == 0) {
  					skbn = alloc_skb(AX25_MAX_HEADER_LEN +
  							 ax25->fraglen,
  							 GFP_ATOMIC);
  					if (!skbn) {
  						skb_queue_purge(&ax25->frag_queue);
  						return 1;
  					}
  
  					skb_reserve(skbn, AX25_MAX_HEADER_LEN);
  
  					skbn->dev   = ax25->ax25_dev->dev;
c1d2bbe1c   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
65
  					skb_reset_network_header(skbn);
badff6d01   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
66
  					skb_reset_transport_header(skbn);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
67
68
69
  
  					/* Copy data from the fragments */
  					while ((skbo = skb_dequeue(&ax25->frag_queue)) != NULL) {
1a4e2d093   Arnaldo Carvalho de Melo   [SK_BUFF]: Some m...
70
71
72
  						skb_copy_from_linear_data(skbo,
  							  skb_put(skbn, skbo->len),
  									  skbo->len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  						kfree_skb(skbo);
  					}
  
  					ax25->fraglen = 0;
  
  					if (ax25_rx_iframe(ax25, skbn) == 0)
  						kfree_skb(skbn);
  				}
  
  				return 1;
  			}
  		}
  	} else {
  		/* First fragment received */
  		if (*skb->data & AX25_SEG_FIRST) {
  			skb_queue_purge(&ax25->frag_queue);
  			ax25->fragno = *skb->data & AX25_SEG_REM;
  			skb_pull(skb, 1);		/* skip fragno */
  			ax25->fraglen = skb->len;
  			skb_queue_tail(&ax25->frag_queue, skb);
  			return 1;
  		}
  	}
  
  	return 0;
  }
  
  /*
   *	This is where all valid I frames are sent to, to be dispatched to
   *	whichever protocol requires them.
   */
  int ax25_rx_iframe(ax25_cb *ax25, struct sk_buff *skb)
  {
  	int (*func)(struct sk_buff *, ax25_cb *);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
107
  	unsigned char pid;
bde3445c5   Ralf Baechle   [AX.25]: Get rid ...
108
  	int queued = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
110
111
112
113
114
  
  	if (skb == NULL) return 0;
  
  	ax25_start_idletimer(ax25);
  
  	pid = *skb->data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
115
116
117
118
119
120
121
122
123
124
125
126
  	if (pid == AX25_P_IP) {
  		/* working around a TCP bug to keep additional listeners
  		 * happy. TCP re-uses the buffer and destroys the original
  		 * content.
  		 */
  		struct sk_buff *skbn = skb_copy(skb, GFP_ATOMIC);
  		if (skbn != NULL) {
  			kfree_skb(skb);
  			skb = skbn;
  		}
  
  		skb_pull(skb, 1);	/* Remove PID */
c6e6ca712   David S. Miller   [NET]: Correct tw...
127
  		skb->mac_header = skb->network_header;
c1d2bbe1c   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
128
  		skb_reset_network_header(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
129
130
131
  		skb->dev      = ax25->ax25_dev->dev;
  		skb->pkt_type = PACKET_HOST;
  		skb->protocol = htons(ETH_P_IP);
98a82febb   Ralf Baechle   [AX25/NETROM]: Cl...
132
  		netif_rx(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
133
134
  		return 1;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  	if (pid == AX25_P_SEGMENT) {
  		skb_pull(skb, 1);	/* Remove PID */
  		return ax25_rx_fragment(ax25, skb);
  	}
  
  	if ((func = ax25_protocol_function(pid)) != NULL) {
  		skb_pull(skb, 1);	/* Remove PID */
  		return (*func)(skb, ax25);
  	}
  
  	if (ax25->sk != NULL && ax25->ax25_dev->values[AX25_VALUES_CONMODE] == 2) {
  		if ((!ax25->pidincl && ax25->sk->sk_protocol == pid) ||
  		    ax25->pidincl) {
  			if (sock_queue_rcv_skb(ax25->sk, skb) == 0)
  				queued = 1;
  			else
  				ax25->condition |= AX25_COND_OWN_RX_BUSY;
  		}
  	}
  
  	return queued;
  }
  
  /*
   *	Higher level upcall for a LAPB frame
   */
  static int ax25_process_rx_frame(ax25_cb *ax25, struct sk_buff *skb, int type, int dama)
  {
  	int queued = 0;
  
  	if (ax25->state == AX25_STATE_0)
  		return 0;
  
  	switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
  	case AX25_PROTO_STD_SIMPLEX:
  	case AX25_PROTO_STD_DUPLEX:
  		queued = ax25_std_frame_in(ax25, skb, type);
  		break;
  
  #ifdef CONFIG_AX25_DAMA_SLAVE
  	case AX25_PROTO_DAMA_SLAVE:
  		if (dama || ax25->ax25_dev->dama.slave)
  			queued = ax25_ds_frame_in(ax25, skb, type);
  		else
  			queued = ax25_std_frame_in(ax25, skb, type);
  		break;
  #endif
  	}
  
  	return queued;
  }
  
  static int ax25_rcv(struct sk_buff *skb, struct net_device *dev,
  	ax25_address *dev_addr, struct packet_type *ptype)
  {
  	ax25_address src, dest, *next_digi = NULL;
  	int type = 0, mine = 0, dama;
  	struct sock *make, *sk;
  	ax25_digi dp, reverse_dp;
  	ax25_cb *ax25;
  	ax25_dev *ax25_dev;
  
  	/*
  	 *	Process the AX.25/LAPB frame.
  	 */
badff6d01   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
200
  	skb_reset_transport_header(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
201

0eae1b98c   Ilpo Järvinen   ax25: join the re...
202
203
  	if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
  		goto free;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
204
205
206
207
  
  	/*
  	 *	Parse the address header.
  	 */
0eae1b98c   Ilpo Järvinen   ax25: join the re...
208
209
  	if (ax25_addr_parse(skb->data, skb->len, &src, &dest, &dp, &type, &dama) == NULL)
  		goto free;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
  
  	/*
  	 *	Ours perhaps ?
  	 */
  	if (dp.lastrepeat + 1 < dp.ndigi)		/* Not yet digipeated completely */
  		next_digi = &dp.calls[dp.lastrepeat + 1];
  
  	/*
  	 *	Pull of the AX.25 headers leaving the CTRL/PID bytes
  	 */
  	skb_pull(skb, ax25_addr_size(&dp));
  
  	/* For our port addresses ? */
  	if (ax25cmp(&dest, dev_addr) == 0 && dp.lastrepeat + 1 == dp.ndigi)
  		mine = 1;
  
  	/* Also match on any registered callsign from L3/4 */
  	if (!mine && ax25_listen_mine(&dest, dev) && dp.lastrepeat + 1 == dp.ndigi)
  		mine = 1;
  
  	/* UI frame - bypass LAPB processing */
  	if ((*skb->data & ~0x10) == AX25_UI && dp.lastrepeat + 1 == dp.ndigi) {
967b05f64   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
232
  		skb_set_transport_header(skb, 2); /* skip control and pid */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
233
234
  
  		ax25_send_to_raw(&dest, skb, skb->data[1]);
0eae1b98c   Ilpo Järvinen   ax25: join the re...
235
236
  		if (!mine && ax25cmp(&dest, (ax25_address *)dev->broadcast) != 0)
  			goto free;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
237
238
239
  
  		/* Now we are pointing at the pid byte */
  		switch (skb->data[1]) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
240
241
  		case AX25_P_IP:
  			skb_pull(skb,2);		/* drop PID/CTRL */
badff6d01   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
242
  			skb_reset_transport_header(skb);
c1d2bbe1c   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
243
  			skb_reset_network_header(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
244
245
246
  			skb->dev      = dev;
  			skb->pkt_type = PACKET_HOST;
  			skb->protocol = htons(ETH_P_IP);
98a82febb   Ralf Baechle   [AX25/NETROM]: Cl...
247
  			netif_rx(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
248
249
250
251
  			break;
  
  		case AX25_P_ARP:
  			skb_pull(skb,2);
badff6d01   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
252
  			skb_reset_transport_header(skb);
c1d2bbe1c   Arnaldo Carvalho de Melo   [SK_BUFF]: Introd...
253
  			skb_reset_network_header(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
254
255
256
  			skb->dev      = dev;
  			skb->pkt_type = PACKET_HOST;
  			skb->protocol = htons(ETH_P_ARP);
98a82febb   Ralf Baechle   [AX25/NETROM]: Cl...
257
  			netif_rx(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
258
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  		case AX25_P_TEXT:
  			/* Now find a suitable dgram socket */
  			sk = ax25_get_socket(&dest, &src, SOCK_DGRAM);
  			if (sk != NULL) {
  				bh_lock_sock(sk);
  				if (atomic_read(&sk->sk_rmem_alloc) >=
  				    sk->sk_rcvbuf) {
  					kfree_skb(skb);
  				} else {
  					/*
  					 *	Remove the control and PID.
  					 */
  					skb_pull(skb, 2);
  					if (sock_queue_rcv_skb(sk, skb) != 0)
  						kfree_skb(skb);
  				}
  				bh_unlock_sock(sk);
  				sock_put(sk);
  			} else {
  				kfree_skb(skb);
  			}
  			break;
  
  		default:
  			kfree_skb(skb);	/* Will scan SOCK_AX25 RAW sockets */
  			break;
  		}
  
  		return 0;
  	}
  
  	/*
  	 *	Is connected mode supported on this device ?
  	 *	If not, should we DM the incoming frame (except DMs) or
  	 *	silently ignore them. For now we stay quiet.
  	 */
0eae1b98c   Ilpo Järvinen   ax25: join the re...
295
296
  	if (ax25_dev->values[AX25_VALUES_CONMODE] == 0)
  		goto free;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  
  	/* LAPB */
  
  	/* AX.25 state 1-4 */
  
  	ax25_digi_invert(&dp, &reverse_dp);
  
  	if ((ax25 = ax25_find_cb(&dest, &src, &reverse_dp, dev)) != NULL) {
  		/*
  		 *	Process the frame. If it is queued up internally it
  		 *	returns one otherwise we free it immediately. This
  		 *	routine itself wakes the user context layers so we do
  		 *	no further work
  		 */
  		if (ax25_process_rx_frame(ax25, skb, type, dama) == 0)
  			kfree_skb(skb);
  
  		ax25_cb_put(ax25);
  		return 0;
  	}
  
  	/* AX.25 state 0 (disconnected) */
  
  	/* a) received not a SABM(E) */
  
  	if ((*skb->data & ~AX25_PF) != AX25_SABM &&
  	    (*skb->data & ~AX25_PF) != AX25_SABME) {
  		/*
  		 *	Never reply to a DM. Also ignore any connects for
  		 *	addresses that are not our interfaces and not a socket.
  		 */
  		if ((*skb->data & ~AX25_PF) != AX25_DM && mine)
  			ax25_return_dm(dev, &src, &dest, &dp);
0eae1b98c   Ilpo Järvinen   ax25: join the re...
330
  		goto free;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  	}
  
  	/* b) received SABM(E) */
  
  	if (dp.lastrepeat + 1 == dp.ndigi)
  		sk = ax25_find_listener(&dest, 0, dev, SOCK_SEQPACKET);
  	else
  		sk = ax25_find_listener(next_digi, 1, dev, SOCK_SEQPACKET);
  
  	if (sk != NULL) {
  		bh_lock_sock(sk);
  		if (sk_acceptq_is_full(sk) ||
  		    (make = ax25_make_new(sk, ax25_dev)) == NULL) {
  			if (mine)
  				ax25_return_dm(dev, &src, &dest, &dp);
  			kfree_skb(skb);
  			bh_unlock_sock(sk);
  			sock_put(sk);
  
  			return 0;
  		}
  
  		ax25 = ax25_sk(make);
  		skb_set_owner_r(skb, make);
  		skb_queue_head(&sk->sk_receive_queue, skb);
  
  		make->sk_state = TCP_ESTABLISHED;
  
  		sk->sk_ack_backlog++;
  		bh_unlock_sock(sk);
  	} else {
0eae1b98c   Ilpo Järvinen   ax25: join the re...
362
363
  		if (!mine)
  			goto free;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
364
365
366
  
  		if ((ax25 = ax25_create_cb()) == NULL) {
  			ax25_return_dm(dev, &src, &dest, &dp);
0eae1b98c   Ilpo Järvinen   ax25: join the re...
367
  			goto free;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
  		}
  
  		ax25_fillin_cb(ax25, ax25_dev);
  	}
  
  	ax25->source_addr = dest;
  	ax25->dest_addr   = src;
  
  	/*
  	 *	Sort out any digipeated paths.
  	 */
  	if (dp.ndigi && !ax25->digipeat &&
  	    (ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
  		kfree_skb(skb);
  		ax25_destroy_socket(ax25);
  		if (sk)
  			sock_put(sk);
  		return 0;
  	}
  
  	if (dp.ndigi == 0) {
a51482bde   Jesper Juhl   [NET]: kfree cleanup
389
390
  		kfree(ax25->digipeat);
  		ax25->digipeat = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
  	} else {
  		/* Reverse the source SABM's path */
  		memcpy(ax25->digipeat, &reverse_dp, sizeof(ax25_digi));
  	}
  
  	if ((*skb->data & ~AX25_PF) == AX25_SABME) {
  		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];
  	}
  
  	ax25_send_control(ax25, AX25_UA, AX25_POLLON, AX25_RESPONSE);
  
  #ifdef CONFIG_AX25_DAMA_SLAVE
  	if (dama && ax25->ax25_dev->values[AX25_VALUES_PROTOCOL] == AX25_PROTO_DAMA_SLAVE)
  		ax25_dama_on(ax25);
  #endif
  
  	ax25->state = AX25_STATE_3;
  
  	ax25_cb_add(ax25);
  
  	ax25_start_heartbeat(ax25);
  	ax25_start_t3timer(ax25);
  	ax25_start_idletimer(ax25);
  
  	if (sk) {
  		if (!sock_flag(sk, SOCK_DEAD))
  			sk->sk_data_ready(sk, skb->len);
  		sock_put(sk);
0eae1b98c   Ilpo Järvinen   ax25: join the re...
423
424
  	} else {
  free:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
425
  		kfree_skb(skb);
0eae1b98c   Ilpo Järvinen   ax25: join the re...
426
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
427
428
429
430
431
432
433
  	return 0;
  }
  
  /*
   *	Receive an AX.25 frame via a SLIP interface.
   */
  int ax25_kiss_rcv(struct sk_buff *skb, struct net_device *dev,
f2ccd8fa0   David S. Miller   [NET]: Kill skb->...
434
  		  struct packet_type *ptype, struct net_device *orig_dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
435
  {
d55d87fdf   Herbert Xu   net: Move rx skb_...
436
  	skb_orphan(skb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
437

721499e89   YOSHIFUJI Hideaki   netns: Use net_eq...
438
  	if (!net_eq(dev_net(dev), &init_net)) {
e730c1551   Eric W. Biederman   [NET]: Make packe...
439
440
441
  		kfree_skb(skb);
  		return 0;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
442
443
444
445
446
447
448
449
450
  	if ((*skb->data & 0x0F) != 0) {
  		kfree_skb(skb);	/* Not a KISS data frame */
  		return 0;
  	}
  
  	skb_pull(skb, AX25_KISS_HEADER_LEN);	/* Remove the KISS byte */
  
  	return ax25_rcv(skb, dev, (ax25_address *)dev->dev_addr, ptype);
  }