- pre3:
[davej-history.git] / net / ax25 / ax25_addr.c
blob1b0f9da67e11ea8eb1ea49bce768c81cb3f9b775
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/config.h>
23 #if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/socket.h>
27 #include <linux/in.h>
28 #include <linux/kernel.h>
29 #include <linux/sched.h>
30 #include <linux/timer.h>
31 #include <linux/string.h>
32 #include <linux/sockios.h>
33 #include <linux/net.h>
34 #include <net/ax25.h>
35 #include <linux/inet.h>
36 #include <linux/netdevice.h>
37 #include <linux/skbuff.h>
38 #include <net/sock.h>
39 #include <asm/uaccess.h>
40 #include <asm/system.h>
41 #include <linux/fcntl.h>
42 #include <linux/mm.h>
43 #include <linux/interrupt.h>
46 * The null address is defined as a callsign of all spaces with an
47 * SSID of zero.
49 ax25_address null_ax25_address = {{0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00}};
52 * ax25 -> ascii conversion
54 char *ax2asc(ax25_address *a)
56 static char buf[11];
57 char c, *s;
58 int n;
60 for (n = 0, s = buf; n < 6; n++) {
61 c = (a->ax25_call[n] >> 1) & 0x7F;
63 if (c != ' ') *s++ = c;
66 *s++ = '-';
68 if ((n = ((a->ax25_call[6] >> 1) & 0x0F)) > 9) {
69 *s++ = '1';
70 n -= 10;
73 *s++ = n + '0';
74 *s++ = '\0';
76 if (*buf == '\0' || *buf == '-')
77 return "*";
79 return buf;
84 * ascii -> ax25 conversion
86 ax25_address *asc2ax(char *callsign)
88 static ax25_address addr;
89 char *s;
90 int n;
92 for (s = callsign, n = 0; n < 6; n++) {
93 if (*s != '\0' && *s != '-')
94 addr.ax25_call[n] = *s++;
95 else
96 addr.ax25_call[n] = ' ';
97 addr.ax25_call[n] <<= 1;
98 addr.ax25_call[n] &= 0xFE;
101 if (*s++ == '\0') {
102 addr.ax25_call[6] = 0x00;
103 return &addr;
106 addr.ax25_call[6] = *s++ - '0';
108 if (*s != '\0') {
109 addr.ax25_call[6] *= 10;
110 addr.ax25_call[6] += *s++ - '0';
113 addr.ax25_call[6] <<= 1;
114 addr.ax25_call[6] &= 0x1E;
116 return &addr;
120 * Compare two ax.25 addresses
122 int ax25cmp(ax25_address *a, ax25_address *b)
124 int ct = 0;
126 while (ct < 6) {
127 if ((a->ax25_call[ct] & 0xFE) != (b->ax25_call[ct] & 0xFE)) /* Clean off repeater bits */
128 return 1;
129 ct++;
132 if ((a->ax25_call[ct] & 0x1E) == (b->ax25_call[ct] & 0x1E)) /* SSID without control bit */
133 return 0;
135 return 2; /* Partial match */
139 * Compare two AX.25 digipeater paths.
141 int ax25digicmp(ax25_digi *digi1, ax25_digi *digi2)
143 int i;
145 if (digi1->ndigi != digi2->ndigi)
146 return 1;
148 if (digi1->lastrepeat != digi2->lastrepeat)
149 return 1;
151 for (i = 0; i < digi1->ndigi; i++)
152 if (ax25cmp(&digi1->calls[i], &digi2->calls[i]) != 0)
153 return 1;
155 return 0;
159 * Given an AX.25 address pull of to, from, digi list, command/response and the start of data
162 unsigned char *ax25_addr_parse(unsigned char *buf, int len, ax25_address *src, ax25_address *dest, ax25_digi *digi, int *flags, int *dama)
164 int d = 0;
166 if (len < 14) return NULL;
168 if (flags != NULL) {
169 *flags = 0;
171 if (buf[6] & AX25_CBIT)
172 *flags = AX25_COMMAND;
173 if (buf[13] & AX25_CBIT)
174 *flags = AX25_RESPONSE;
177 if (dama != NULL)
178 *dama = ~buf[13] & AX25_DAMA_FLAG;
180 /* Copy to, from */
181 if (dest != NULL)
182 memcpy(dest, buf + 0, AX25_ADDR_LEN);
183 if (src != NULL)
184 memcpy(src, buf + 7, AX25_ADDR_LEN);
186 buf += 2 * AX25_ADDR_LEN;
187 len -= 2 * AX25_ADDR_LEN;
189 digi->lastrepeat = -1;
190 digi->ndigi = 0;
192 while (!(buf[-1] & AX25_EBIT)) {
193 if (d >= AX25_MAX_DIGIS) return NULL; /* Max of 6 digis */
194 if (len < 7) return NULL; /* Short packet */
196 memcpy(&digi->calls[d], buf, AX25_ADDR_LEN);
197 digi->ndigi = d + 1;
199 if (buf[6] & AX25_HBIT) {
200 digi->repeated[d] = 1;
201 digi->lastrepeat = d;
202 } else {
203 digi->repeated[d] = 0;
206 buf += AX25_ADDR_LEN;
207 len -= AX25_ADDR_LEN;
208 d++;
211 return buf;
215 * Assemble an AX.25 header from the bits
217 int ax25_addr_build(unsigned char *buf, ax25_address *src, ax25_address *dest, ax25_digi *d, int flag, int modulus)
219 int len = 0;
220 int ct = 0;
222 memcpy(buf, dest, AX25_ADDR_LEN);
223 buf[6] &= ~(AX25_EBIT | AX25_CBIT);
224 buf[6] |= AX25_SSSID_SPARE;
226 if (flag == AX25_COMMAND) buf[6] |= AX25_CBIT;
228 buf += AX25_ADDR_LEN;
229 len += AX25_ADDR_LEN;
231 memcpy(buf, src, AX25_ADDR_LEN);
232 buf[6] &= ~(AX25_EBIT | AX25_CBIT);
233 buf[6] &= ~AX25_SSSID_SPARE;
235 if (modulus == AX25_MODULUS)
236 buf[6] |= AX25_SSSID_SPARE;
237 else
238 buf[6] |= AX25_ESSID_SPARE;
240 if (flag == AX25_RESPONSE) buf[6] |= AX25_CBIT;
243 * Fast path the normal digiless path
245 if (d == NULL || d->ndigi == 0) {
246 buf[6] |= AX25_EBIT;
247 return 2 * AX25_ADDR_LEN;
250 buf += AX25_ADDR_LEN;
251 len += AX25_ADDR_LEN;
253 while (ct < d->ndigi) {
254 memcpy(buf, &d->calls[ct], AX25_ADDR_LEN);
256 if (d->repeated[ct])
257 buf[6] |= AX25_HBIT;
258 else
259 buf[6] &= ~AX25_HBIT;
261 buf[6] &= ~AX25_EBIT;
262 buf[6] |= AX25_SSSID_SPARE;
264 buf += AX25_ADDR_LEN;
265 len += AX25_ADDR_LEN;
266 ct++;
269 buf[-1] |= AX25_EBIT;
271 return len;
274 int ax25_addr_size(ax25_digi *dp)
276 if (dp == NULL)
277 return 2 * AX25_ADDR_LEN;
279 return AX25_ADDR_LEN * (2 + dp->ndigi);
283 * Reverse Digipeat List. May not pass both parameters as same struct
285 void ax25_digi_invert(ax25_digi *in, ax25_digi *out)
287 int ct;
289 out->ndigi = in->ndigi;
290 out->lastrepeat = in->ndigi - in->lastrepeat - 2;
292 /* Invert the digipeaters */
293 for (ct = 0; ct < in->ndigi; ct++) {
294 out->calls[ct] = in->calls[in->ndigi - ct - 1];
296 if (ct <= out->lastrepeat) {
297 out->calls[ct].ax25_call[6] |= AX25_HBIT;
298 out->repeated[ct] = 1;
299 } else {
300 out->calls[ct].ax25_call[6] &= ~AX25_HBIT;
301 out->repeated[ct] = 0;
306 #endif