Linux 2.6.16.42-rc1
[linux/fpc-iii.git] / net / ax25 / ax25_addr.c
blob0164a155b8c492b2ef6918d3b8010d7150126119
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
8 */
9 #include <linux/errno.h>
10 #include <linux/types.h>
11 #include <linux/socket.h>
12 #include <linux/in.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/timer.h>
16 #include <linux/string.h>
17 #include <linux/sockios.h>
18 #include <linux/net.h>
19 #include <net/ax25.h>
20 #include <linux/inet.h>
21 #include <linux/netdevice.h>
22 #include <linux/skbuff.h>
23 #include <net/sock.h>
24 #include <asm/uaccess.h>
25 #include <asm/system.h>
26 #include <linux/fcntl.h>
27 #include <linux/mm.h>
28 #include <linux/interrupt.h>
31 * The null address is defined as a callsign of all spaces with an
32 * SSID of zero.
34 ax25_address null_ax25_address = {{0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00}};
37 * ax25 -> ascii conversion
39 char *ax2asc(char *buf, ax25_address *a)
41 char c, *s;
42 int n;
44 for (n = 0, s = buf; n < 6; n++) {
45 c = (a->ax25_call[n] >> 1) & 0x7F;
47 if (c != ' ') *s++ = c;
50 *s++ = '-';
52 if ((n = ((a->ax25_call[6] >> 1) & 0x0F)) > 9) {
53 *s++ = '1';
54 n -= 10;
57 *s++ = n + '0';
58 *s++ = '\0';
60 if (*buf == '\0' || *buf == '-')
61 return "*";
63 return buf;
68 * ascii -> ax25 conversion
70 void asc2ax(ax25_address *addr, char *callsign)
72 char *s;
73 int n;
75 for (s = callsign, n = 0; n < 6; n++) {
76 if (*s != '\0' && *s != '-')
77 addr->ax25_call[n] = *s++;
78 else
79 addr->ax25_call[n] = ' ';
80 addr->ax25_call[n] <<= 1;
81 addr->ax25_call[n] &= 0xFE;
84 if (*s++ == '\0') {
85 addr->ax25_call[6] = 0x00;
86 return;
89 addr->ax25_call[6] = *s++ - '0';
91 if (*s != '\0') {
92 addr->ax25_call[6] *= 10;
93 addr->ax25_call[6] += *s++ - '0';
96 addr->ax25_call[6] <<= 1;
97 addr->ax25_call[6] &= 0x1E;
101 * Compare two ax.25 addresses
103 int ax25cmp(ax25_address *a, ax25_address *b)
105 int ct = 0;
107 while (ct < 6) {
108 if ((a->ax25_call[ct] & 0xFE) != (b->ax25_call[ct] & 0xFE)) /* Clean off repeater bits */
109 return 1;
110 ct++;
113 if ((a->ax25_call[ct] & 0x1E) == (b->ax25_call[ct] & 0x1E)) /* SSID without control bit */
114 return 0;
116 return 2; /* Partial match */
120 * Compare two AX.25 digipeater paths.
122 int ax25digicmp(ax25_digi *digi1, ax25_digi *digi2)
124 int i;
126 if (digi1->ndigi != digi2->ndigi)
127 return 1;
129 if (digi1->lastrepeat != digi2->lastrepeat)
130 return 1;
132 for (i = 0; i < digi1->ndigi; i++)
133 if (ax25cmp(&digi1->calls[i], &digi2->calls[i]) != 0)
134 return 1;
136 return 0;
140 * Given an AX.25 address pull of to, from, digi list, command/response and the start of data
143 unsigned char *ax25_addr_parse(unsigned char *buf, int len, ax25_address *src, ax25_address *dest, ax25_digi *digi, int *flags, int *dama)
145 int d = 0;
147 if (len < 14) return NULL;
149 if (flags != NULL) {
150 *flags = 0;
152 if (buf[6] & AX25_CBIT)
153 *flags = AX25_COMMAND;
154 if (buf[13] & AX25_CBIT)
155 *flags = AX25_RESPONSE;
158 if (dama != NULL)
159 *dama = ~buf[13] & AX25_DAMA_FLAG;
161 /* Copy to, from */
162 if (dest != NULL)
163 memcpy(dest, buf + 0, AX25_ADDR_LEN);
164 if (src != NULL)
165 memcpy(src, buf + 7, AX25_ADDR_LEN);
167 buf += 2 * AX25_ADDR_LEN;
168 len -= 2 * AX25_ADDR_LEN;
170 digi->lastrepeat = -1;
171 digi->ndigi = 0;
173 while (!(buf[-1] & AX25_EBIT)) {
174 if (d >= AX25_MAX_DIGIS) return NULL; /* Max of 6 digis */
175 if (len < 7) return NULL; /* Short packet */
177 memcpy(&digi->calls[d], buf, AX25_ADDR_LEN);
178 digi->ndigi = d + 1;
180 if (buf[6] & AX25_HBIT) {
181 digi->repeated[d] = 1;
182 digi->lastrepeat = d;
183 } else {
184 digi->repeated[d] = 0;
187 buf += AX25_ADDR_LEN;
188 len -= AX25_ADDR_LEN;
189 d++;
192 return buf;
196 * Assemble an AX.25 header from the bits
198 int ax25_addr_build(unsigned char *buf, ax25_address *src, ax25_address *dest, ax25_digi *d, int flag, int modulus)
200 int len = 0;
201 int ct = 0;
203 memcpy(buf, dest, AX25_ADDR_LEN);
204 buf[6] &= ~(AX25_EBIT | AX25_CBIT);
205 buf[6] |= AX25_SSSID_SPARE;
207 if (flag == AX25_COMMAND) buf[6] |= AX25_CBIT;
209 buf += AX25_ADDR_LEN;
210 len += AX25_ADDR_LEN;
212 memcpy(buf, src, AX25_ADDR_LEN);
213 buf[6] &= ~(AX25_EBIT | AX25_CBIT);
214 buf[6] &= ~AX25_SSSID_SPARE;
216 if (modulus == AX25_MODULUS)
217 buf[6] |= AX25_SSSID_SPARE;
218 else
219 buf[6] |= AX25_ESSID_SPARE;
221 if (flag == AX25_RESPONSE) buf[6] |= AX25_CBIT;
224 * Fast path the normal digiless path
226 if (d == NULL || d->ndigi == 0) {
227 buf[6] |= AX25_EBIT;
228 return 2 * AX25_ADDR_LEN;
231 buf += AX25_ADDR_LEN;
232 len += AX25_ADDR_LEN;
234 while (ct < d->ndigi) {
235 memcpy(buf, &d->calls[ct], AX25_ADDR_LEN);
237 if (d->repeated[ct])
238 buf[6] |= AX25_HBIT;
239 else
240 buf[6] &= ~AX25_HBIT;
242 buf[6] &= ~AX25_EBIT;
243 buf[6] |= AX25_SSSID_SPARE;
245 buf += AX25_ADDR_LEN;
246 len += AX25_ADDR_LEN;
247 ct++;
250 buf[-1] |= AX25_EBIT;
252 return len;
255 int ax25_addr_size(ax25_digi *dp)
257 if (dp == NULL)
258 return 2 * AX25_ADDR_LEN;
260 return AX25_ADDR_LEN * (2 + dp->ndigi);
264 * Reverse Digipeat List. May not pass both parameters as same struct
266 void ax25_digi_invert(ax25_digi *in, ax25_digi *out)
268 int ct;
270 out->ndigi = in->ndigi;
271 out->lastrepeat = in->ndigi - in->lastrepeat - 2;
273 /* Invert the digipeaters */
274 for (ct = 0; ct < in->ndigi; ct++) {
275 out->calls[ct] = in->calls[in->ndigi - ct - 1];
277 if (ct <= out->lastrepeat) {
278 out->calls[ct].ax25_call[6] |= AX25_HBIT;
279 out->repeated[ct] = 1;
280 } else {
281 out->calls[ct].ax25_call[6] &= ~AX25_HBIT;
282 out->repeated[ct] = 0;