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.
39 * Thomas(DL9SAU) Fixed missing initialization of skb->protocol.
42 #include <linux/config.h>
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 skbn = alloc_skb(AX25_MAX_HEADER_LEN +
90 skb_queue_purge(&ax25->frag_queue);
94 skb_reserve(skbn, AX25_MAX_HEADER_LEN);
96 skbn->dev = ax25->ax25_dev->dev;
97 skbn->h.raw = skbn->data;
98 skbn->nh.raw = skbn->data;
100 /* Copy data from the fragments */
101 while ((skbo = skb_dequeue(&ax25->frag_queue)) != NULL) {
102 memcpy(skb_put(skbn, skbo->len), skbo->data, skbo->len);
108 if (ax25_rx_iframe(ax25, skbn) == 0)
116 /* First fragment received */
117 if (*skb->data & AX25_SEG_FIRST) {
118 skb_queue_purge(&ax25->frag_queue);
119 ax25->fragno = *skb->data & AX25_SEG_REM;
120 skb_pull(skb, 1); /* skip fragno */
121 ax25->fraglen = skb->len;
122 skb_queue_tail(&ax25->frag_queue, skb);
131 * This is where all valid I frames are sent to, to be dispatched to
132 * whichever protocol requires them.
134 int ax25_rx_iframe(ax25_cb *ax25, struct sk_buff *skb)
136 int (*func)(struct sk_buff *, ax25_cb *);
137 volatile int queued = 0;
140 if (skb == NULL) return 0;
142 ax25_start_idletimer(ax25);
147 if (pid == AX25_P_IP) {
148 /* working around a TCP bug to keep additional listeners
149 * happy. TCP re-uses the buffer and destroys the original
152 struct sk_buff *skbn = skb_copy(skb, GFP_ATOMIC);
158 skb_pull(skb, 1); /* Remove PID */
159 skb->h.raw = skb->data;
160 skb->nh.raw = skb->data;
161 skb->dev = ax25->ax25_dev->dev;
162 skb->pkt_type = PACKET_HOST;
163 skb->protocol = htons(ETH_P_IP);
164 ip_rcv(skb, skb->dev, NULL); /* Wrong ptype */
168 if (pid == AX25_P_SEGMENT) {
169 skb_pull(skb, 1); /* Remove PID */
170 return ax25_rx_fragment(ax25, skb);
173 if ((func = ax25_protocol_function(pid)) != NULL) {
174 skb_pull(skb, 1); /* Remove PID */
175 return (*func)(skb, ax25);
178 if (ax25->sk != NULL && ax25->ax25_dev->values[AX25_VALUES_CONMODE] == 2) {
179 if ((!ax25->pidincl && ax25->sk->protocol == pid) || ax25->pidincl) {
180 if (sock_queue_rcv_skb(ax25->sk, skb) == 0)
183 ax25->condition |= AX25_COND_OWN_RX_BUSY;
191 * Higher level upcall for a LAPB frame
193 static int ax25_process_rx_frame(ax25_cb *ax25, struct sk_buff *skb, int type, int dama)
197 if (ax25->state == AX25_STATE_0)
200 switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
201 case AX25_PROTO_STD_SIMPLEX:
202 case AX25_PROTO_STD_DUPLEX:
203 queued = ax25_std_frame_in(ax25, skb, type);
206 #ifdef CONFIG_AX25_DAMA_SLAVE
207 case AX25_PROTO_DAMA_SLAVE:
208 if (dama || ax25->ax25_dev->dama.slave)
209 queued = ax25_ds_frame_in(ax25, skb, type);
211 queued = ax25_std_frame_in(ax25, skb, type);
219 static int ax25_rcv(struct sk_buff *skb, struct net_device *dev, ax25_address *dev_addr, struct packet_type *ptype)
224 ax25_digi dp, reverse_dp;
226 ax25_address src, dest;
227 ax25_address *next_digi = NULL;
234 * Process the AX.25/LAPB frame.
237 skb->h.raw = skb->data;
239 if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL) {
245 * Parse the address header.
248 if (ax25_addr_parse(skb->data, skb->len, &src, &dest, &dp, &type, &dama) == NULL) {
256 if (dp.lastrepeat + 1 < dp.ndigi) /* Not yet digipeated completely */
257 next_digi = &dp.calls[dp.lastrepeat + 1];
260 * Pull of the AX.25 headers leaving the CTRL/PID bytes
262 skb_pull(skb, ax25_addr_size(&dp));
264 /* For our port addresses ? */
265 if (ax25cmp(&dest, dev_addr) == 0 && dp.lastrepeat + 1 == dp.ndigi)
268 /* Also match on any registered callsign from L3/4 */
269 if (!mine && ax25_listen_mine(&dest, dev) && dp.lastrepeat + 1 == dp.ndigi)
272 /* UI frame - bypass LAPB processing */
273 if ((*skb->data & ~0x10) == AX25_UI && dp.lastrepeat + 1 == dp.ndigi) {
274 skb->h.raw = skb->data + 2; /* skip control and pid */
276 if ((raw = ax25_addr_match(&dest)) != NULL)
277 ax25_send_to_raw(raw, skb, skb->data[1]);
279 if (!mine && ax25cmp(&dest, (ax25_address *)dev->broadcast) != 0) {
284 /* Now we are pointing at the pid byte */
285 switch (skb->data[1]) {
288 skb_pull(skb,2); /* drop PID/CTRL */
289 skb->h.raw = skb->data;
290 skb->nh.raw = skb->data;
292 skb->pkt_type = PACKET_HOST;
293 skb->protocol = htons(ETH_P_IP);
294 ip_rcv(skb, dev, ptype); /* Note ptype here is the wrong one, fix me later */
299 skb->h.raw = skb->data;
300 skb->nh.raw = skb->data;
302 skb->pkt_type = PACKET_HOST;
303 skb->protocol = htons(ETH_P_ARP);
304 arp_rcv(skb, dev, ptype); /* Note ptype here is wrong... */
308 /* Now find a suitable dgram socket */
309 if ((sk = ax25_find_socket(&dest, &src, SOCK_DGRAM)) != NULL) {
310 if (atomic_read(&sk->rmem_alloc) >= sk->rcvbuf) {
314 * Remove the control and PID.
317 if (sock_queue_rcv_skb(sk, skb) != 0)
326 kfree_skb(skb); /* Will scan SOCK_AX25 RAW sockets */
334 * Is connected mode supported on this device ?
335 * If not, should we DM the incoming frame (except DMs) or
336 * silently ignore them. For now we stay quiet.
338 if (ax25_dev->values[AX25_VALUES_CONMODE] == 0) {
345 /* AX.25 state 1-4 */
347 ax25_digi_invert(&dp, &reverse_dp);
349 if ((ax25 = ax25_find_cb(&dest, &src, &reverse_dp, dev)) != NULL) {
351 * Process the frame. If it is queued up internally it returns one otherwise we
352 * free it immediately. This routine itself wakes the user context layers so we
355 if (ax25_process_rx_frame(ax25, skb, type, dama) == 0)
361 /* AX.25 state 0 (disconnected) */
363 /* a) received not a SABM(E) */
365 if ((*skb->data & ~AX25_PF) != AX25_SABM && (*skb->data & ~AX25_PF) != AX25_SABME) {
367 * Never reply to a DM. Also ignore any connects for
368 * addresses that are not our interfaces and not a socket.
370 if ((*skb->data & ~AX25_PF) != AX25_DM && mine)
371 ax25_return_dm(dev, &src, &dest, &dp);
377 /* b) received SABM(E) */
379 if (dp.lastrepeat + 1 == dp.ndigi)
380 sk = ax25_find_listener(&dest, 0, dev, SOCK_SEQPACKET);
382 sk = ax25_find_listener(next_digi, 1, dev, SOCK_SEQPACKET);
385 if (sk->ack_backlog == sk->max_ack_backlog || (make = ax25_make_new(sk, ax25_dev)) == NULL) {
386 if (mine) ax25_return_dm(dev, &src, &dest, &dp);
391 ax25 = make->protinfo.ax25;
392 skb_set_owner_r(skb, make);
393 skb_queue_head(&sk->receive_queue, skb);
395 make->state = TCP_ESTABLISHED;
405 if ((ax25 = ax25_create_cb()) == NULL) {
406 ax25_return_dm(dev, &src, &dest, &dp);
411 ax25_fillin_cb(ax25, ax25_dev);
414 ax25->source_addr = dest;
415 ax25->dest_addr = src;
418 * Sort out any digipeated paths.
420 if (dp.ndigi && !ax25->digipeat &&
421 (ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
423 ax25_destroy_socket(ax25);
428 if (ax25->digipeat != NULL) {
429 kfree(ax25->digipeat);
430 ax25->digipeat = NULL;
433 /* Reverse the source SABM's path */
434 memcpy(ax25->digipeat, &reverse_dp, sizeof(ax25_digi));
437 if ((*skb->data & ~AX25_PF) == AX25_SABME) {
438 ax25->modulus = AX25_EMODULUS;
439 ax25->window = ax25_dev->values[AX25_VALUES_EWINDOW];
441 ax25->modulus = AX25_MODULUS;
442 ax25->window = ax25_dev->values[AX25_VALUES_WINDOW];
445 ax25_send_control(ax25, AX25_UA, AX25_POLLON, AX25_RESPONSE);
447 #ifdef CONFIG_AX25_DAMA_SLAVE
448 if (dama && ax25->ax25_dev->values[AX25_VALUES_PROTOCOL] == AX25_PROTO_DAMA_SLAVE)
452 ax25->state = AX25_STATE_3;
454 ax25_insert_socket(ax25);
456 ax25_start_heartbeat(ax25);
457 ax25_start_t3timer(ax25);
458 ax25_start_idletimer(ax25);
462 sk->data_ready(sk, skb->len);
471 * Receive an AX.25 frame via a SLIP interface.
473 int ax25_kiss_rcv(struct sk_buff *skb, struct net_device *dev,
474 struct packet_type *ptype)
476 skb->sk = NULL; /* Initially we don't know who it's for */
477 skb->destructor = NULL; /* Who initializes this, dammit?! */
479 if ((*skb->data & 0x0F) != 0) {
480 kfree_skb(skb); /* Not a KISS data frame */
484 skb_pull(skb, AX25_KISS_HEADER_LEN); /* Remove the KISS byte */
486 return ax25_rcv(skb, dev, (ax25_address *)dev->dev_addr, ptype);