- Kai Germaschewski: ymfpci cleanups and resource leak fixes
[davej-history.git] / net / ax25 / ax25_in.c
blob6efbc57f21229f5d65dcfa21f4475042dd2371a5
1 /*
2 * AX.25 release 037
4 * This code REQUIRES 2.1.15 or higher/ NET3.038
6 * This module:
7 * This module is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 * Most of this code is based on the SDL diagrams published in the 7th
13 * ARRL Computer Networking Conference papers. The diagrams have mistakes
14 * in them, but are mostly correct. Before you modify the code could you
15 * read the SDL diagrams as the code is not obvious and probably very
16 * easy to break;
18 * History
19 * AX.25 028a Jonathan(G4KLX) New state machine based on SDL diagrams.
20 * AX.25 028b Jonathan(G4KLX) Extracted AX25 control block from
21 * the sock structure.
22 * AX.25 029 Alan(GW4PTS) Switched to KA9Q constant names.
23 * Jonathan(G4KLX) Added IP mode registration.
24 * AX.25 030 Jonathan(G4KLX) Added AX.25 fragment reception.
25 * Upgraded state machine for SABME.
26 * Added arbitrary protocol id support.
27 * AX.25 031 Joerg(DL1BKE) Added DAMA support
28 * HaJo(DD8NE) Added Idle Disc Timer T5
29 * Joerg(DL1BKE) Renamed it to "IDLE" with a slightly
30 * different behaviour. Fixed defrag
31 * routine (I hope)
32 * AX.25 032 Darryl(G7LED) AX.25 segmentation fixed.
33 * AX.25 033 Jonathan(G4KLX) Remove auto-router.
34 * Modularisation changes.
35 * AX.25 035 Hans(PE1AYX) Fixed interface to IP layer.
36 * AX.25 036 Jonathan(G4KLX) Move DAMA code into own file.
37 * Joerg(DL1BKE) Fixed DAMA Slave.
38 * AX.25 037 Jonathan(G4KLX) New timer architecture.
41 #include <linux/config.h>
42 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
43 #include <linux/errno.h>
44 #include <linux/types.h>
45 #include <linux/socket.h>
46 #include <linux/in.h>
47 #include <linux/kernel.h>
48 #include <linux/sched.h>
49 #include <linux/timer.h>
50 #include <linux/string.h>
51 #include <linux/sockios.h>
52 #include <linux/net.h>
53 #include <net/ax25.h>
54 #include <linux/inet.h>
55 #include <linux/netdevice.h>
56 #include <linux/skbuff.h>
57 #include <linux/netfilter.h>
58 #include <net/sock.h>
59 #include <net/ip.h> /* For ip_rcv */
60 #include <net/arp.h> /* For arp_rcv */
61 #include <asm/uaccess.h>
62 #include <asm/system.h>
63 #include <linux/fcntl.h>
64 #include <linux/mm.h>
65 #include <linux/interrupt.h>
68 * Given a fragment, queue it on the fragment queue and if the fragment
69 * is complete, send it back to ax25_rx_iframe.
71 static int ax25_rx_fragment(ax25_cb *ax25, struct sk_buff *skb)
73 struct sk_buff *skbn, *skbo;
75 if (ax25->fragno != 0) {
76 if (!(*skb->data & AX25_SEG_FIRST)) {
77 if ((ax25->fragno - 1) == (*skb->data & AX25_SEG_REM)) {
78 /* Enqueue fragment */
79 ax25->fragno = *skb->data & AX25_SEG_REM;
80 skb_pull(skb, 1); /* skip fragno */
81 ax25->fraglen += skb->len;
82 skb_queue_tail(&ax25->frag_queue, skb);
84 /* Last fragment received ? */
85 if (ax25->fragno == 0) {
86 if ((skbn = alloc_skb(AX25_MAX_HEADER_LEN + ax25->fraglen, GFP_ATOMIC)) == NULL) {
87 while ((skbo = skb_dequeue(&ax25->frag_queue)) != NULL)
88 kfree_skb(skbo);
89 return 1;
92 skb_reserve(skbn, AX25_MAX_HEADER_LEN);
94 skbn->dev = ax25->ax25_dev->dev;
95 skbn->h.raw = skbn->data;
96 skbn->nh.raw = skbn->data;
98 /* Copy data from the fragments */
99 while ((skbo = skb_dequeue(&ax25->frag_queue)) != NULL) {
100 memcpy(skb_put(skbn, skbo->len), skbo->data, skbo->len);
101 kfree_skb(skbo);
104 ax25->fraglen = 0;
106 if (ax25_rx_iframe(ax25, skbn) == 0)
107 kfree_skb(skbn);
110 return 1;
113 } else {
114 /* First fragment received */
115 if (*skb->data & AX25_SEG_FIRST) {
116 while ((skbo = skb_dequeue(&ax25->frag_queue)) != NULL)
117 kfree_skb(skbo);
118 ax25->fragno = *skb->data & AX25_SEG_REM;
119 skb_pull(skb, 1); /* skip fragno */
120 ax25->fraglen = skb->len;
121 skb_queue_tail(&ax25->frag_queue, skb);
122 return 1;
126 return 0;
130 * This is where all valid I frames are sent to, to be dispatched to
131 * whichever protocol requires them.
133 int ax25_rx_iframe(ax25_cb *ax25, struct sk_buff *skb)
135 int (*func)(struct sk_buff *, ax25_cb *);
136 volatile int queued = 0;
137 unsigned char pid;
139 if (skb == NULL) return 0;
141 ax25_start_idletimer(ax25);
143 pid = *skb->data;
145 #ifdef CONFIG_INET
146 if (pid == AX25_P_IP) {
147 /* working around a TCP bug to keep additional listeners
148 * happy. TCP re-uses the buffer and destroys the original
149 * content.
151 struct sk_buff *skbn = skb_copy(skb, GFP_ATOMIC);
152 if (skbn != NULL) {
153 kfree_skb(skb);
154 skb = skbn;
157 skb_pull(skb, 1); /* Remove PID */
158 skb->h.raw = skb->data;
159 skb->nh.raw = skb->data;
160 skb->dev = ax25->ax25_dev->dev;
161 skb->pkt_type = PACKET_HOST;
162 ip_rcv(skb, skb->dev, NULL); /* Wrong ptype */
163 return 1;
165 #endif
166 if (pid == AX25_P_SEGMENT) {
167 skb_pull(skb, 1); /* Remove PID */
168 return ax25_rx_fragment(ax25, skb);
171 if ((func = ax25_protocol_function(pid)) != NULL) {
172 skb_pull(skb, 1); /* Remove PID */
173 return (*func)(skb, ax25);
176 if (ax25->sk != NULL && ax25->ax25_dev->values[AX25_VALUES_CONMODE] == 2) {
177 if ((!ax25->pidincl && ax25->sk->protocol == pid) || ax25->pidincl) {
178 if (sock_queue_rcv_skb(ax25->sk, skb) == 0)
179 queued = 1;
180 else
181 ax25->condition |= AX25_COND_OWN_RX_BUSY;
185 return queued;
189 * Higher level upcall for a LAPB frame
191 static int ax25_process_rx_frame(ax25_cb *ax25, struct sk_buff *skb, int type, int dama)
193 int queued = 0;
195 if (ax25->state == AX25_STATE_0)
196 return 0;
198 switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
199 case AX25_PROTO_STD_SIMPLEX:
200 case AX25_PROTO_STD_DUPLEX:
201 queued = ax25_std_frame_in(ax25, skb, type);
202 break;
204 #ifdef CONFIG_AX25_DAMA_SLAVE
205 case AX25_PROTO_DAMA_SLAVE:
206 if (dama || ax25->ax25_dev->dama.slave)
207 queued = ax25_ds_frame_in(ax25, skb, type);
208 else
209 queued = ax25_std_frame_in(ax25, skb, type);
210 break;
211 #endif
214 return queued;
217 static int ax25_rcv(struct sk_buff *skb, struct net_device *dev, ax25_address *dev_addr, struct packet_type *ptype)
219 struct sock *make;
220 struct sock *sk;
221 int type = 0;
222 ax25_digi dp, reverse_dp;
223 ax25_cb *ax25;
224 ax25_address src, dest;
225 ax25_address *next_digi = NULL;
226 ax25_dev *ax25_dev;
227 struct sock *raw;
228 int mine = 0;
229 int dama;
232 * Process the AX.25/LAPB frame.
235 skb->h.raw = skb->data;
237 if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL) {
238 kfree_skb(skb);
239 return 0;
243 * Parse the address header.
246 if (ax25_addr_parse(skb->data, skb->len, &src, &dest, &dp, &type, &dama) == NULL) {
247 kfree_skb(skb);
248 return 0;
252 * Ours perhaps ?
254 if (dp.lastrepeat + 1 < dp.ndigi) /* Not yet digipeated completely */
255 next_digi = &dp.calls[dp.lastrepeat + 1];
258 * Pull of the AX.25 headers leaving the CTRL/PID bytes
260 skb_pull(skb, ax25_addr_size(&dp));
262 /* For our port addresses ? */
263 if (ax25cmp(&dest, dev_addr) == 0 && dp.lastrepeat + 1 == dp.ndigi)
264 mine = 1;
266 /* Also match on any registered callsign from L3/4 */
267 if (!mine && ax25_listen_mine(&dest, dev) && dp.lastrepeat + 1 == dp.ndigi)
268 mine = 1;
270 /* UI frame - bypass LAPB processing */
271 if ((*skb->data & ~0x10) == AX25_UI && dp.lastrepeat + 1 == dp.ndigi) {
272 skb->h.raw = skb->data + 2; /* skip control and pid */
274 if ((raw = ax25_addr_match(&dest)) != NULL)
275 ax25_send_to_raw(raw, skb, skb->data[1]);
277 if (!mine && ax25cmp(&dest, (ax25_address *)dev->broadcast) != 0) {
278 kfree_skb(skb);
279 return 0;
282 /* Now we are pointing at the pid byte */
283 switch (skb->data[1]) {
284 #ifdef CONFIG_INET
285 case AX25_P_IP:
286 skb_pull(skb,2); /* drop PID/CTRL */
287 skb->h.raw = skb->data;
288 skb->nh.raw = skb->data;
289 skb->dev = dev;
290 skb->pkt_type = PACKET_HOST;
291 ip_rcv(skb, dev, ptype); /* Note ptype here is the wrong one, fix me later */
292 break;
294 case AX25_P_ARP:
295 skb_pull(skb,2);
296 skb->h.raw = skb->data;
297 skb->nh.raw = skb->data;
298 skb->dev = dev;
299 skb->pkt_type = PACKET_HOST;
300 arp_rcv(skb, dev, ptype); /* Note ptype here is wrong... */
301 break;
302 #endif
303 case AX25_P_TEXT:
304 /* Now find a suitable dgram socket */
305 if ((sk = ax25_find_socket(&dest, &src, SOCK_DGRAM)) != NULL) {
306 if (atomic_read(&sk->rmem_alloc) >= sk->rcvbuf) {
307 kfree_skb(skb);
308 } else {
310 * Remove the control and PID.
312 skb_pull(skb, 2);
313 if (sock_queue_rcv_skb(sk, skb) != 0)
314 kfree_skb(skb);
316 } else {
317 kfree_skb(skb);
319 break;
321 default:
322 kfree_skb(skb); /* Will scan SOCK_AX25 RAW sockets */
323 break;
326 return 0;
330 * Is connected mode supported on this device ?
331 * If not, should we DM the incoming frame (except DMs) or
332 * silently ignore them. For now we stay quiet.
334 if (ax25_dev->values[AX25_VALUES_CONMODE] == 0) {
335 kfree_skb(skb);
336 return 0;
339 /* LAPB */
341 /* AX.25 state 1-4 */
343 ax25_digi_invert(&dp, &reverse_dp);
345 if ((ax25 = ax25_find_cb(&dest, &src, &reverse_dp, dev)) != NULL) {
347 * Process the frame. If it is queued up internally it returns one otherwise we
348 * free it immediately. This routine itself wakes the user context layers so we
349 * do no further work
351 if (ax25_process_rx_frame(ax25, skb, type, dama) == 0)
352 kfree_skb(skb);
354 return 0;
357 /* AX.25 state 0 (disconnected) */
359 /* a) received not a SABM(E) */
361 if ((*skb->data & ~AX25_PF) != AX25_SABM && (*skb->data & ~AX25_PF) != AX25_SABME) {
363 * Never reply to a DM. Also ignore any connects for
364 * addresses that are not our interfaces and not a socket.
366 if ((*skb->data & ~AX25_PF) != AX25_DM && mine)
367 ax25_return_dm(dev, &src, &dest, &dp);
369 kfree_skb(skb);
370 return 0;
373 /* b) received SABM(E) */
375 if (dp.lastrepeat + 1 == dp.ndigi)
376 sk = ax25_find_listener(&dest, 0, dev, SOCK_SEQPACKET);
377 else
378 sk = ax25_find_listener(next_digi, 1, dev, SOCK_SEQPACKET);
380 if (sk != NULL) {
381 if (sk->ack_backlog == sk->max_ack_backlog || (make = ax25_make_new(sk, ax25_dev)) == NULL) {
382 if (mine) ax25_return_dm(dev, &src, &dest, &dp);
383 kfree_skb(skb);
384 return 0;
387 ax25 = make->protinfo.ax25;
388 skb_set_owner_r(skb, make);
389 skb_queue_head(&sk->receive_queue, skb);
391 make->state = TCP_ESTABLISHED;
392 make->pair = sk;
394 sk->ack_backlog++;
395 } else {
396 if (!mine) {
397 kfree_skb(skb);
398 return 0;
401 if ((ax25 = ax25_create_cb()) == NULL) {
402 ax25_return_dm(dev, &src, &dest, &dp);
403 kfree_skb(skb);
404 return 0;
407 ax25_fillin_cb(ax25, ax25_dev);
410 ax25->source_addr = dest;
411 ax25->dest_addr = src;
414 * Sort out any digipeated paths.
416 if (dp.ndigi != 0 && ax25->digipeat == NULL && (ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
417 kfree_skb(skb);
418 ax25_destroy_socket(ax25);
419 return 0;
422 if (dp.ndigi == 0) {
423 if (ax25->digipeat != NULL) {
424 kfree(ax25->digipeat);
425 ax25->digipeat = NULL;
427 } else {
428 /* Reverse the source SABM's path */
429 memcpy(&ax25->digipeat, &reverse_dp, sizeof(ax25_digi));
432 if ((*skb->data & ~AX25_PF) == AX25_SABME) {
433 ax25->modulus = AX25_EMODULUS;
434 ax25->window = ax25_dev->values[AX25_VALUES_EWINDOW];
435 } else {
436 ax25->modulus = AX25_MODULUS;
437 ax25->window = ax25_dev->values[AX25_VALUES_WINDOW];
440 ax25_send_control(ax25, AX25_UA, AX25_POLLON, AX25_RESPONSE);
442 #ifdef CONFIG_AX25_DAMA_SLAVE
443 if (dama && ax25->ax25_dev->values[AX25_VALUES_PROTOCOL] == AX25_PROTO_DAMA_SLAVE)
444 ax25_dama_on(ax25);
445 #endif
447 ax25->state = AX25_STATE_3;
449 ax25_insert_socket(ax25);
451 ax25_start_heartbeat(ax25);
452 ax25_start_t3timer(ax25);
453 ax25_start_idletimer(ax25);
455 if (sk != NULL) {
456 if (!sk->dead)
457 sk->data_ready(sk, skb->len);
458 } else {
459 kfree_skb(skb);
462 return 0;
466 * Receive an AX.25 frame via a SLIP interface.
468 int ax25_kiss_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *ptype)
470 skb->sk = NULL; /* Initially we don't know who it's for */
471 skb->destructor = NULL; /* Who initializes this, dammit?! */
473 if ((*skb->data & 0x0F) != 0) {
474 kfree_skb(skb); /* Not a KISS data frame */
475 return 0;
478 skb_pull(skb, AX25_KISS_HEADER_LEN); /* Remove the KISS byte */
480 return ax25_rcv(skb, dev, (ax25_address *)dev->dev_addr, ptype);
483 #endif