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