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 036 Jonathan(G4KLX) Split from ax25_subr.c.
22 #include <linux/errno.h>
23 #include <linux/types.h>
24 #include <linux/socket.h>
26 #include <linux/kernel.h>
27 #include <linux/sched.h>
28 #include <linux/timer.h>
29 #include <linux/string.h>
30 #include <linux/sockios.h>
31 #include <linux/net.h>
33 #include <linux/inet.h>
34 #include <linux/netdevice.h>
35 #include <linux/skbuff.h>
37 #include <asm/uaccess.h>
38 #include <asm/system.h>
39 #include <linux/fcntl.h>
41 #include <linux/interrupt.h>
44 * The null address is defined as a callsign of all spaces with an
47 ax25_address null_ax25_address
= {{0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00}};
50 * ax25 -> ascii conversion
52 char *ax2asc(ax25_address
*a
)
58 for (n
= 0, s
= buf
; n
< 6; n
++) {
59 c
= (a
->ax25_call
[n
] >> 1) & 0x7F;
61 if (c
!= ' ') *s
++ = c
;
66 if ((n
= ((a
->ax25_call
[6] >> 1) & 0x0F)) > 9) {
74 if (*buf
== '\0' || *buf
== '-')
82 * ascii -> ax25 conversion
84 ax25_address
*asc2ax(char *callsign
)
86 static ax25_address addr
;
90 for (s
= callsign
, n
= 0; n
< 6; n
++) {
91 if (*s
!= '\0' && *s
!= '-')
92 addr
.ax25_call
[n
] = *s
++;
94 addr
.ax25_call
[n
] = ' ';
95 addr
.ax25_call
[n
] <<= 1;
96 addr
.ax25_call
[n
] &= 0xFE;
100 addr
.ax25_call
[6] = 0x00;
104 addr
.ax25_call
[6] = *s
++ - '0';
107 addr
.ax25_call
[6] *= 10;
108 addr
.ax25_call
[6] += *s
++ - '0';
111 addr
.ax25_call
[6] <<= 1;
112 addr
.ax25_call
[6] &= 0x1E;
118 * Compare two ax.25 addresses
120 int ax25cmp(ax25_address
*a
, ax25_address
*b
)
125 if ((a
->ax25_call
[ct
] & 0xFE) != (b
->ax25_call
[ct
] & 0xFE)) /* Clean off repeater bits */
130 if ((a
->ax25_call
[ct
] & 0x1E) == (b
->ax25_call
[ct
] & 0x1E)) /* SSID without control bit */
133 return 2; /* Partial match */
137 * Compare two AX.25 digipeater paths.
139 int ax25digicmp(ax25_digi
*digi1
, ax25_digi
*digi2
)
143 if (digi1
->ndigi
!= digi2
->ndigi
)
146 if (digi1
->lastrepeat
!= digi2
->lastrepeat
)
149 for (i
= 0; i
< digi1
->ndigi
; i
++)
150 if (ax25cmp(&digi1
->calls
[i
], &digi2
->calls
[i
]) != 0)
157 * Given an AX.25 address pull of to, from, digi list, command/response and the start of data
160 unsigned char *ax25_addr_parse(unsigned char *buf
, int len
, ax25_address
*src
, ax25_address
*dest
, ax25_digi
*digi
, int *flags
, int *dama
)
164 if (len
< 14) return NULL
;
169 if (buf
[6] & AX25_CBIT
)
170 *flags
= AX25_COMMAND
;
171 if (buf
[13] & AX25_CBIT
)
172 *flags
= AX25_RESPONSE
;
176 *dama
= ~buf
[13] & AX25_DAMA_FLAG
;
180 memcpy(dest
, buf
+ 0, AX25_ADDR_LEN
);
182 memcpy(src
, buf
+ 7, AX25_ADDR_LEN
);
184 buf
+= 2 * AX25_ADDR_LEN
;
185 len
-= 2 * AX25_ADDR_LEN
;
187 digi
->lastrepeat
= -1;
190 while (!(buf
[-1] & AX25_EBIT
)) {
191 if (d
>= AX25_MAX_DIGIS
) return NULL
; /* Max of 6 digis */
192 if (len
< 7) return NULL
; /* Short packet */
194 memcpy(&digi
->calls
[d
], buf
, AX25_ADDR_LEN
);
197 if (buf
[6] & AX25_HBIT
) {
198 digi
->repeated
[d
] = 1;
199 digi
->lastrepeat
= d
;
201 digi
->repeated
[d
] = 0;
204 buf
+= AX25_ADDR_LEN
;
205 len
-= AX25_ADDR_LEN
;
213 * Assemble an AX.25 header from the bits
215 int ax25_addr_build(unsigned char *buf
, ax25_address
*src
, ax25_address
*dest
, ax25_digi
*d
, int flag
, int modulus
)
220 memcpy(buf
, dest
, AX25_ADDR_LEN
);
221 buf
[6] &= ~(AX25_EBIT
| AX25_CBIT
);
222 buf
[6] |= AX25_SSSID_SPARE
;
224 if (flag
== AX25_COMMAND
) buf
[6] |= AX25_CBIT
;
226 buf
+= AX25_ADDR_LEN
;
227 len
+= AX25_ADDR_LEN
;
229 memcpy(buf
, src
, AX25_ADDR_LEN
);
230 buf
[6] &= ~(AX25_EBIT
| AX25_CBIT
);
231 buf
[6] &= ~AX25_SSSID_SPARE
;
233 if (modulus
== AX25_MODULUS
)
234 buf
[6] |= AX25_SSSID_SPARE
;
236 buf
[6] |= AX25_ESSID_SPARE
;
238 if (flag
== AX25_RESPONSE
) buf
[6] |= AX25_CBIT
;
241 * Fast path the normal digiless path
243 if (d
== NULL
|| d
->ndigi
== 0) {
245 return 2 * AX25_ADDR_LEN
;
248 buf
+= AX25_ADDR_LEN
;
249 len
+= AX25_ADDR_LEN
;
251 while (ct
< d
->ndigi
) {
252 memcpy(buf
, &d
->calls
[ct
], AX25_ADDR_LEN
);
257 buf
[6] &= ~AX25_HBIT
;
259 buf
[6] &= ~AX25_EBIT
;
260 buf
[6] |= AX25_SSSID_SPARE
;
262 buf
+= AX25_ADDR_LEN
;
263 len
+= AX25_ADDR_LEN
;
267 buf
[-1] |= AX25_EBIT
;
272 int ax25_addr_size(ax25_digi
*dp
)
275 return 2 * AX25_ADDR_LEN
;
277 return AX25_ADDR_LEN
* (2 + dp
->ndigi
);
281 * Reverse Digipeat List. May not pass both parameters as same struct
283 void ax25_digi_invert(ax25_digi
*in
, ax25_digi
*out
)
287 out
->ndigi
= in
->ndigi
;
288 out
->lastrepeat
= in
->ndigi
- in
->lastrepeat
- 2;
290 /* Invert the digipeaters */
291 for (ct
= 0; ct
< in
->ndigi
; ct
++) {
292 out
->calls
[ct
] = in
->calls
[in
->ndigi
- ct
- 1];
294 if (ct
<= out
->lastrepeat
) {
295 out
->calls
[ct
].ax25_call
[6] |= AX25_HBIT
;
296 out
->repeated
[ct
] = 1;
298 out
->calls
[ct
].ax25_call
[6] &= ~AX25_HBIT
;
299 out
->repeated
[ct
] = 0;