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)
9 #include <linux/errno.h>
10 #include <linux/types.h>
11 #include <linux/socket.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/sched.h>
16 #include <linux/timer.h>
17 #include <linux/string.h>
18 #include <linux/sockios.h>
19 #include <linux/net.h>
21 #include <linux/inet.h>
22 #include <linux/netdevice.h>
23 #include <linux/skbuff.h>
25 #include <asm/uaccess.h>
26 #include <asm/system.h>
27 #include <linux/fcntl.h>
29 #include <linux/interrupt.h>
32 * The null address is defined as a callsign of all spaces with an
35 ax25_address null_ax25_address = {{0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00}};
37 EXPORT_SYMBOL(null_ax25_address);
40 * ax25 -> ascii conversion
42 char *ax2asc(char *buf, const ax25_address *a)
47 for (n = 0, s = buf; n < 6; n++) {
48 c = (a->ax25_call[n] >> 1) & 0x7F;
50 if (c != ' ') *s++ = c;
55 if ((n = ((a->ax25_call[6] >> 1) & 0x0F)) > 9) {
63 if (*buf == '\0' || *buf == '-')
70 EXPORT_SYMBOL(ax2asc);
73 * ascii -> ax25 conversion
75 void asc2ax(ax25_address *addr, const char *callsign)
80 for (s = callsign, n = 0; n < 6; n++) {
81 if (*s != '\0' && *s != '-')
82 addr->ax25_call[n] = *s++;
84 addr->ax25_call[n] = ' ';
85 addr->ax25_call[n] <<= 1;
86 addr->ax25_call[n] &= 0xFE;
90 addr->ax25_call[6] = 0x00;
94 addr->ax25_call[6] = *s++ - '0';
97 addr->ax25_call[6] *= 10;
98 addr->ax25_call[6] += *s++ - '0';
101 addr->ax25_call[6] <<= 1;
102 addr->ax25_call[6] &= 0x1E;
105 EXPORT_SYMBOL(asc2ax);
108 * Compare two ax.25 addresses
110 int ax25cmp(const ax25_address *a, const ax25_address *b)
115 if ((a->ax25_call[ct] & 0xFE) != (b->ax25_call[ct] & 0xFE)) /* Clean off repeater bits */
120 if ((a->ax25_call[ct] & 0x1E) == (b->ax25_call[ct] & 0x1E)) /* SSID without control bit */
123 return 2; /* Partial match */
126 EXPORT_SYMBOL(ax25cmp);
129 * Compare two AX.25 digipeater paths.
131 int ax25digicmp(const ax25_digi *digi1, const ax25_digi *digi2)
135 if (digi1->ndigi != digi2->ndigi)
138 if (digi1->lastrepeat != digi2->lastrepeat)
141 for (i = 0; i < digi1->ndigi; i++)
142 if (ax25cmp(&digi1->calls[i], &digi2->calls[i]) != 0)
149 * Given an AX.25 address pull of to, from, digi list, command/response and the start of data
152 const unsigned char *ax25_addr_parse(const unsigned char *buf, int len,
153 ax25_address *src, ax25_address *dest, ax25_digi *digi, int *flags,
158 if (len < 14) return NULL;
163 if (buf[6] & AX25_CBIT)
164 *flags = AX25_COMMAND;
165 if (buf[13] & AX25_CBIT)
166 *flags = AX25_RESPONSE;
170 *dama = ~buf[13] & AX25_DAMA_FLAG;
174 memcpy(dest, buf + 0, AX25_ADDR_LEN);
176 memcpy(src, buf + 7, AX25_ADDR_LEN);
178 buf += 2 * AX25_ADDR_LEN;
179 len -= 2 * AX25_ADDR_LEN;
181 digi->lastrepeat = -1;
184 while (!(buf[-1] & AX25_EBIT)) {
185 if (d >= AX25_MAX_DIGIS) return NULL; /* Max of 6 digis */
186 if (len < 7) return NULL; /* Short packet */
188 memcpy(&digi->calls[d], buf, AX25_ADDR_LEN);
191 if (buf[6] & AX25_HBIT) {
192 digi->repeated[d] = 1;
193 digi->lastrepeat = d;
195 digi->repeated[d] = 0;
198 buf += AX25_ADDR_LEN;
199 len -= AX25_ADDR_LEN;
207 * Assemble an AX.25 header from the bits
209 int ax25_addr_build(unsigned char *buf, const ax25_address *src,
210 const ax25_address *dest, const ax25_digi *d, int flag, int modulus)
215 memcpy(buf, dest, AX25_ADDR_LEN);
216 buf[6] &= ~(AX25_EBIT | AX25_CBIT);
217 buf[6] |= AX25_SSSID_SPARE;
219 if (flag == AX25_COMMAND) buf[6] |= AX25_CBIT;
221 buf += AX25_ADDR_LEN;
222 len += AX25_ADDR_LEN;
224 memcpy(buf, src, AX25_ADDR_LEN);
225 buf[6] &= ~(AX25_EBIT | AX25_CBIT);
226 buf[6] &= ~AX25_SSSID_SPARE;
228 if (modulus == AX25_MODULUS)
229 buf[6] |= AX25_SSSID_SPARE;
231 buf[6] |= AX25_ESSID_SPARE;
233 if (flag == AX25_RESPONSE) buf[6] |= AX25_CBIT;
236 * Fast path the normal digiless path
238 if (d == NULL || d->ndigi == 0) {
240 return 2 * AX25_ADDR_LEN;
243 buf += AX25_ADDR_LEN;
244 len += AX25_ADDR_LEN;
246 while (ct < d->ndigi) {
247 memcpy(buf, &d->calls[ct], AX25_ADDR_LEN);
252 buf[6] &= ~AX25_HBIT;
254 buf[6] &= ~AX25_EBIT;
255 buf[6] |= AX25_SSSID_SPARE;
257 buf += AX25_ADDR_LEN;
258 len += AX25_ADDR_LEN;
262 buf[-1] |= AX25_EBIT;
267 int ax25_addr_size(const ax25_digi *dp)
270 return 2 * AX25_ADDR_LEN;
272 return AX25_ADDR_LEN * (2 + dp->ndigi);
276 * Reverse Digipeat List. May not pass both parameters as same struct
278 void ax25_digi_invert(const ax25_digi *in, ax25_digi *out)
282 out->ndigi = in->ndigi;
283 out->lastrepeat = in->ndigi - in->lastrepeat - 2;
285 /* Invert the digipeaters */
286 for (ct = 0; ct < in->ndigi; ct++) {
287 out->calls[ct] = in->calls[in->ndigi - ct - 1];
289 if (ct <= out->lastrepeat) {
290 out->calls[ct].ax25_call[6] |= AX25_HBIT;
291 out->repeated[ct] = 1;
293 out->calls[ct].ax25_call[6] &= ~AX25_HBIT;
294 out->repeated[ct] = 0;