1 /*
2 * AX.25 release 037
3 *
4 * This code REQUIRES 2.1.15 or higher/ NET3.038
5 *
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.
11 *
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;
17 *
18 * History
19 * AX.25 036 Jonathan(G4KLX) Split from ax25_subr.c.
20 */
21
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>
42
43 /*
44 * The null address is defined as a callsign of all spaces with an
45 * SSID of zero.
46 */
47 ax25_address null_ax25_address = {{0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00}};
48
49 /*
50 * ax25 -> ascii conversion
51 */
ax2asc(ax25_address * a)52 char *ax2asc(ax25_address *a)
53 {
54 static char buf[11];
55 char c, *s;
56 int n;
57
58 for (n = 0, s = buf; n < 6; n++) {
59 c = (a->ax25_call[n] >> 1) & 0x7F;
60
61 if (c != ' ') *s++ = c;
62 }
63
64 *s++ = '-';
65
66 if ((n = ((a->ax25_call[6] >> 1) & 0x0F)) > 9) {
67 *s++ = '1';
68 n -= 10;
69 }
70
71 *s++ = n + '0';
72 *s++ = '\0';
73
74 if (*buf == '\0' || *buf == '-')
75 return "*";
76
77 return buf;
78
79 }
80
81 /*
82 * ascii -> ax25 conversion
83 */
asc2ax(char * callsign)84 ax25_address *asc2ax(char *callsign)
85 {
86 static ax25_address addr;
87 char *s;
88 int n;
89
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;
97 }
98
99 if (*s++ == '\0') {
100 addr.ax25_call[6] = 0x00;
101 return &addr;
102 }
103
104 addr.ax25_call[6] = *s++ - '0';
105
106 if (*s != '\0') {
107 addr.ax25_call[6] *= 10;
108 addr.ax25_call[6] += *s++ - '0';
109 }
110
111 addr.ax25_call[6] <<= 1;
112 addr.ax25_call[6] &= 0x1E;
113
114 return &addr;
115 }
116
117 /*
118 * Compare two ax.25 addresses
119 */
ax25cmp(ax25_address * a,ax25_address * b)120 int ax25cmp(ax25_address *a, ax25_address *b)
121 {
122 int ct = 0;
123
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++;
128 }
129
130 if ((a->ax25_call[ct] & 0x1E) == (b->ax25_call[ct] & 0x1E)) /* SSID without control bit */
131 return 0;
132
133 return 2; /* Partial match */
134 }
135
136 /*
137 * Compare two AX.25 digipeater paths.
138 */
ax25digicmp(ax25_digi * digi1,ax25_digi * digi2)139 int ax25digicmp(ax25_digi *digi1, ax25_digi *digi2)
140 {
141 int i;
142
143 if (digi1->ndigi != digi2->ndigi)
144 return 1;
145
146 if (digi1->lastrepeat != digi2->lastrepeat)
147 return 1;
148
149 for (i = 0; i < digi1->ndigi; i++)
150 if (ax25cmp(&digi1->calls[i], &digi2->calls[i]) != 0)
151 return 1;
152
153 return 0;
154 }
155
156 /*
157 * Given an AX.25 address pull of to, from, digi list, command/response and the start of data
158 *
159 */
ax25_addr_parse(unsigned char * buf,int len,ax25_address * src,ax25_address * dest,ax25_digi * digi,int * flags,int * dama)160 unsigned char *ax25_addr_parse(unsigned char *buf, int len, ax25_address *src, ax25_address *dest, ax25_digi *digi, int *flags, int *dama)
161 {
162 int d = 0;
163
164 if (len < 14) return NULL;
165
166 if (flags != NULL) {
167 *flags = 0;
168
169 if (buf[6] & AX25_CBIT)
170 *flags = AX25_COMMAND;
171 if (buf[13] & AX25_CBIT)
172 *flags = AX25_RESPONSE;
173 }
174
175 if (dama != NULL)
176 *dama = ~buf[13] & AX25_DAMA_FLAG;
177
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);
183
184 buf += 2 * AX25_ADDR_LEN;
185 len -= 2 * AX25_ADDR_LEN;
186
187 digi->lastrepeat = -1;
188 digi->ndigi = 0;
189
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 */
193
194 memcpy(&digi->calls[d], buf, AX25_ADDR_LEN);
195 digi->ndigi = d + 1;
196
197 if (buf[6] & AX25_HBIT) {
198 digi->repeated[d] = 1;
199 digi->lastrepeat = d;
200 } else {
201 digi->repeated[d] = 0;
202 }
203
204 buf += AX25_ADDR_LEN;
205 len -= AX25_ADDR_LEN;
206 d++;
207 }
208
209 return buf;
210 }
211
212 /*
213 * Assemble an AX.25 header from the bits
214 */
ax25_addr_build(unsigned char * buf,ax25_address * src,ax25_address * dest,ax25_digi * d,int flag,int modulus)215 int ax25_addr_build(unsigned char *buf, ax25_address *src, ax25_address *dest, ax25_digi *d, int flag, int modulus)
216 {
217 int len = 0;
218 int ct = 0;
219
220 memcpy(buf, dest, AX25_ADDR_LEN);
221 buf[6] &= ~(AX25_EBIT | AX25_CBIT);
222 buf[6] |= AX25_SSSID_SPARE;
223
224 if (flag == AX25_COMMAND) buf[6] |= AX25_CBIT;
225
226 buf += AX25_ADDR_LEN;
227 len += AX25_ADDR_LEN;
228
229 memcpy(buf, src, AX25_ADDR_LEN);
230 buf[6] &= ~(AX25_EBIT | AX25_CBIT);
231 buf[6] &= ~AX25_SSSID_SPARE;
232
233 if (modulus == AX25_MODULUS)
234 buf[6] |= AX25_SSSID_SPARE;
235 else
236 buf[6] |= AX25_ESSID_SPARE;
237
238 if (flag == AX25_RESPONSE) buf[6] |= AX25_CBIT;
239
240 /*
241 * Fast path the normal digiless path
242 */
243 if (d == NULL || d->ndigi == 0) {
244 buf[6] |= AX25_EBIT;
245 return 2 * AX25_ADDR_LEN;
246 }
247
248 buf += AX25_ADDR_LEN;
249 len += AX25_ADDR_LEN;
250
251 while (ct < d->ndigi) {
252 memcpy(buf, &d->calls[ct], AX25_ADDR_LEN);
253
254 if (d->repeated[ct])
255 buf[6] |= AX25_HBIT;
256 else
257 buf[6] &= ~AX25_HBIT;
258
259 buf[6] &= ~AX25_EBIT;
260 buf[6] |= AX25_SSSID_SPARE;
261
262 buf += AX25_ADDR_LEN;
263 len += AX25_ADDR_LEN;
264 ct++;
265 }
266
267 buf[-1] |= AX25_EBIT;
268
269 return len;
270 }
271
ax25_addr_size(ax25_digi * dp)272 int ax25_addr_size(ax25_digi *dp)
273 {
274 if (dp == NULL)
275 return 2 * AX25_ADDR_LEN;
276
277 return AX25_ADDR_LEN * (2 + dp->ndigi);
278 }
279
280 /*
281 * Reverse Digipeat List. May not pass both parameters as same struct
282 */
ax25_digi_invert(ax25_digi * in,ax25_digi * out)283 void ax25_digi_invert(ax25_digi *in, ax25_digi *out)
284 {
285 int ct;
286
287 out->ndigi = in->ndigi;
288 out->lastrepeat = in->ndigi - in->lastrepeat - 2;
289
290 /* Invert the digipeaters */
291 for (ct = 0; ct < in->ndigi; ct++) {
292 out->calls[ct] = in->calls[in->ndigi - ct - 1];
293
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;
300 }
301 }
302 }
303
304