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