Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / net / ax25 / ax25_addr.c
blobf96c65586c04b80b2354557c82e72b8f290ed70f
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 036 Jonathan(G4KLX) Split from ax25_subr.c.
22 #include <linux/errno.h>
23 #include <linux/types.h>
24 #include <linux/socket.h>
25 #include <linux/in.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>
32 #include <net/ax25.h>
33 #include <linux/inet.h>
34 #include <linux/netdevice.h>
35 #include <linux/skbuff.h>
36 #include <net/sock.h>
37 #include <asm/uaccess.h>
38 #include <asm/system.h>
39 #include <linux/fcntl.h>
40 #include <linux/mm.h>
41 #include <linux/interrupt.h>
44 * The null address is defined as a callsign of all spaces with an
45 * SSID of zero.
47 ax25_address null_ax25_address = {{0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00}};
50 * ax25 -> ascii conversion
52 char *ax2asc(ax25_address *a)
54 static char buf[11];
55 char c, *s;
56 int n;
58 for (n = 0, s = buf; n < 6; n++) {
59 c = (a->ax25_call[n] >> 1) & 0x7F;
61 if (c != ' ') *s++ = c;
64 *s++ = '-';
66 if ((n = ((a->ax25_call[6] >> 1) & 0x0F)) > 9) {
67 *s++ = '1';
68 n -= 10;
71 *s++ = n + '0';
72 *s++ = '\0';
74 if (*buf == '\0' || *buf == '-')
75 return "*";
77 return buf;
82 * ascii -> ax25 conversion
84 ax25_address *asc2ax(char *callsign)
86 static ax25_address addr;
87 char *s;
88 int n;
90 for (s = callsign, n = 0; n < 6; n++) {
91 if (*s != '\0' && *s != '-')
92 addr.ax25_call[n] = *s++;
93 else
94 addr.ax25_call[n] = ' ';
95 addr.ax25_call[n] <<= 1;
96 addr.ax25_call[n] &= 0xFE;
99 if (*s++ == '\0') {
100 addr.ax25_call[6] = 0x00;
101 return &addr;
104 addr.ax25_call[6] = *s++ - '0';
106 if (*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;
114 return &addr;
118 * Compare two ax.25 addresses
120 int ax25cmp(ax25_address *a, ax25_address *b)
122 int ct = 0;
124 while (ct < 6) {
125 if ((a->ax25_call[ct] & 0xFE) != (b->ax25_call[ct] & 0xFE)) /* Clean off repeater bits */
126 return 1;
127 ct++;
130 if ((a->ax25_call[ct] & 0x1E) == (b->ax25_call[ct] & 0x1E)) /* SSID without control bit */
131 return 0;
133 return 2; /* Partial match */
137 * Compare two AX.25 digipeater paths.
139 int ax25digicmp(ax25_digi *digi1, ax25_digi *digi2)
141 int i;
143 if (digi1->ndigi != digi2->ndigi)
144 return 1;
146 if (digi1->lastrepeat != digi2->lastrepeat)
147 return 1;
149 for (i = 0; i < digi1->ndigi; i++)
150 if (ax25cmp(&digi1->calls[i], &digi2->calls[i]) != 0)
151 return 1;
153 return 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)
162 int d = 0;
164 if (len < 14) return NULL;
166 if (flags != NULL) {
167 *flags = 0;
169 if (buf[6] & AX25_CBIT)
170 *flags = AX25_COMMAND;
171 if (buf[13] & AX25_CBIT)
172 *flags = AX25_RESPONSE;
175 if (dama != NULL)
176 *dama = ~buf[13] & AX25_DAMA_FLAG;
178 /* Copy to, from */
179 if (dest != NULL)
180 memcpy(dest, buf + 0, AX25_ADDR_LEN);
181 if (src != NULL)
182 memcpy(src, buf + 7, AX25_ADDR_LEN);
184 buf += 2 * AX25_ADDR_LEN;
185 len -= 2 * AX25_ADDR_LEN;
187 digi->lastrepeat = -1;
188 digi->ndigi = 0;
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);
195 digi->ndigi = d + 1;
197 if (buf[6] & AX25_HBIT) {
198 digi->repeated[d] = 1;
199 digi->lastrepeat = d;
200 } else {
201 digi->repeated[d] = 0;
204 buf += AX25_ADDR_LEN;
205 len -= AX25_ADDR_LEN;
206 d++;
209 return buf;
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)
217 int len = 0;
218 int ct = 0;
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;
235 else
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) {
244 buf[6] |= AX25_EBIT;
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);
254 if (d->repeated[ct])
255 buf[6] |= AX25_HBIT;
256 else
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;
264 ct++;
267 buf[-1] |= AX25_EBIT;
269 return len;
272 int ax25_addr_size(ax25_digi *dp)
274 if (dp == NULL)
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)
285 int ct;
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;
297 } else {
298 out->calls[ct].ax25_call[6] &= ~AX25_HBIT;
299 out->repeated[ct] = 0;