4 * This code REQUIRES 2.1.15 or higher/ NET3.038
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
19 * AX.25 028a Jonathan(G4KLX) New state machine based on SDL diagrams.
20 * AX.25 028b Jonathan(G4KLX) Extracted AX25 control block from
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
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>
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>
54 #include <linux/inet.h>
55 #include <linux/netdevice.h>
56 #include <linux/skbuff.h>
57 #include <linux/netfilter.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>
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
)
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
);
106 if (ax25_rx_iframe(ax25
, skbn
) == 0)
114 /* First fragment received */
115 if (*skb
->data
& AX25_SEG_FIRST
) {
116 while ((skbo
= skb_dequeue(&ax25
->frag_queue
)) != NULL
)
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
);
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;
139 if (skb
== NULL
) return 0;
141 ax25_start_idletimer(ax25
);
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
151 struct sk_buff
*skbn
= skb_copy(skb
, GFP_ATOMIC
);
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 */
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)
181 ax25
->condition
|= AX25_COND_OWN_RX_BUSY
;
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
)
195 if (ax25
->state
== AX25_STATE_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
);
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
);
209 queued
= ax25_std_frame_in(ax25
, skb
, type
);
217 static int ax25_rcv(struct sk_buff
*skb
, struct net_device
*dev
, ax25_address
*dev_addr
, struct packet_type
*ptype
)
222 ax25_digi dp
, reverse_dp
;
224 ax25_address src
, dest
;
225 ax25_address
*next_digi
= NULL
;
232 * Process the AX.25/LAPB frame.
235 skb
->h
.raw
= skb
->data
;
237 if ((ax25_dev
= ax25_dev_ax25dev(dev
)) == NULL
) {
243 * Parse the address header.
246 if (ax25_addr_parse(skb
->data
, skb
->len
, &src
, &dest
, &dp
, &type
, &dama
) == NULL
) {
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
)
266 /* Also match on any registered callsign from L3/4 */
267 if (!mine
&& ax25_listen_mine(&dest
, dev
) && dp
.lastrepeat
+ 1 == dp
.ndigi
)
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) {
282 /* Now we are pointing at the pid byte */
283 switch (skb
->data
[1]) {
286 skb_pull(skb
,2); /* drop PID/CTRL */
287 skb
->h
.raw
= skb
->data
;
288 skb
->nh
.raw
= skb
->data
;
290 skb
->pkt_type
= PACKET_HOST
;
291 ip_rcv(skb
, dev
, ptype
); /* Note ptype here is the wrong one, fix me later */
296 skb
->h
.raw
= skb
->data
;
297 skb
->nh
.raw
= skb
->data
;
299 skb
->pkt_type
= PACKET_HOST
;
300 arp_rcv(skb
, dev
, ptype
); /* Note ptype here is wrong... */
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
) {
310 * Remove the control and PID.
313 if (sock_queue_rcv_skb(sk
, skb
) != 0)
322 kfree_skb(skb
); /* Will scan SOCK_AX25 RAW sockets */
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) {
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
351 if (ax25_process_rx_frame(ax25
, skb
, type
, dama
) == 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
);
373 /* b) received SABM(E) */
375 if (dp
.lastrepeat
+ 1 == dp
.ndigi
)
376 sk
= ax25_find_listener(&dest
, 0, dev
, SOCK_SEQPACKET
);
378 sk
= ax25_find_listener(next_digi
, 1, dev
, SOCK_SEQPACKET
);
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
);
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
;
401 if ((ax25
= ax25_create_cb()) == NULL
) {
402 ax25_return_dm(dev
, &src
, &dest
, &dp
);
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
) {
418 ax25_destroy_socket(ax25
);
423 if (ax25
->digipeat
!= NULL
) {
424 kfree(ax25
->digipeat
);
425 ax25
->digipeat
= NULL
;
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
];
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
)
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
);
457 sk
->data_ready(sk
, skb
->len
);
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 */
478 skb_pull(skb
, AX25_KISS_HEADER_LEN
); /* Remove the KISS byte */
480 return ax25_rcv(skb
, dev
, (ax25_address
*)dev
->dev_addr
, ptype
);