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