Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *
0004  * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
0005  */
0006 #include <linux/errno.h>
0007 #include <linux/types.h>
0008 #include <linux/socket.h>
0009 #include <linux/in.h>
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/timer.h>
0013 #include <linux/string.h>
0014 #include <linux/sockios.h>
0015 #include <linux/net.h>
0016 #include <net/ax25.h>
0017 #include <linux/inet.h>
0018 #include <linux/netdevice.h>
0019 #include <linux/skbuff.h>
0020 #include <net/sock.h>
0021 #include <linux/uaccess.h>
0022 #include <linux/fcntl.h>
0023 #include <linux/mm.h>
0024 #include <linux/interrupt.h>
0025 
0026 /*
0027  * The default broadcast address of an interface is QST-0; the default address
0028  * is LINUX-1.  The null address is defined as a callsign of all spaces with
0029  * an SSID of zero.
0030  */
0031 
0032 const ax25_address ax25_bcast =
0033     {{'Q' << 1, 'S' << 1, 'T' << 1, ' ' << 1, ' ' << 1, ' ' << 1, 0 << 1}};
0034 const ax25_address ax25_defaddr =
0035     {{'L' << 1, 'I' << 1, 'N' << 1, 'U' << 1, 'X' << 1, ' ' << 1, 1 << 1}};
0036 const ax25_address null_ax25_address =
0037     {{' ' << 1, ' ' << 1, ' ' << 1, ' ' << 1, ' ' << 1, ' ' << 1, 0 << 1}};
0038 
0039 EXPORT_SYMBOL_GPL(ax25_bcast);
0040 EXPORT_SYMBOL_GPL(ax25_defaddr);
0041 EXPORT_SYMBOL(null_ax25_address);
0042 
0043 /*
0044  *  ax25 -> ascii conversion
0045  */
0046 char *ax2asc(char *buf, const ax25_address *a)
0047 {
0048     char c, *s;
0049     int n;
0050 
0051     for (n = 0, s = buf; n < 6; n++) {
0052         c = (a->ax25_call[n] >> 1) & 0x7F;
0053 
0054         if (c != ' ') *s++ = c;
0055     }
0056 
0057     *s++ = '-';
0058 
0059     if ((n = ((a->ax25_call[6] >> 1) & 0x0F)) > 9) {
0060         *s++ = '1';
0061         n -= 10;
0062     }
0063 
0064     *s++ = n + '0';
0065     *s++ = '\0';
0066 
0067     if (*buf == '\0' || *buf == '-')
0068        return "*";
0069 
0070     return buf;
0071 
0072 }
0073 
0074 EXPORT_SYMBOL(ax2asc);
0075 
0076 /*
0077  *  ascii -> ax25 conversion
0078  */
0079 void asc2ax(ax25_address *addr, const char *callsign)
0080 {
0081     const char *s;
0082     int n;
0083 
0084     for (s = callsign, n = 0; n < 6; n++) {
0085         if (*s != '\0' && *s != '-')
0086             addr->ax25_call[n] = *s++;
0087         else
0088             addr->ax25_call[n] = ' ';
0089         addr->ax25_call[n] <<= 1;
0090         addr->ax25_call[n] &= 0xFE;
0091     }
0092 
0093     if (*s++ == '\0') {
0094         addr->ax25_call[6] = 0x00;
0095         return;
0096     }
0097 
0098     addr->ax25_call[6] = *s++ - '0';
0099 
0100     if (*s != '\0') {
0101         addr->ax25_call[6] *= 10;
0102         addr->ax25_call[6] += *s++ - '0';
0103     }
0104 
0105     addr->ax25_call[6] <<= 1;
0106     addr->ax25_call[6] &= 0x1E;
0107 }
0108 
0109 EXPORT_SYMBOL(asc2ax);
0110 
0111 /*
0112  *  Compare two ax.25 addresses
0113  */
0114 int ax25cmp(const ax25_address *a, const ax25_address *b)
0115 {
0116     int ct = 0;
0117 
0118     while (ct < 6) {
0119         if ((a->ax25_call[ct] & 0xFE) != (b->ax25_call[ct] & 0xFE)) /* Clean off repeater bits */
0120             return 1;
0121         ct++;
0122     }
0123 
0124     if ((a->ax25_call[ct] & 0x1E) == (b->ax25_call[ct] & 0x1E)) /* SSID without control bit */
0125         return 0;
0126 
0127     return 2;           /* Partial match */
0128 }
0129 
0130 EXPORT_SYMBOL(ax25cmp);
0131 
0132 /*
0133  *  Compare two AX.25 digipeater paths.
0134  */
0135 int ax25digicmp(const ax25_digi *digi1, const ax25_digi *digi2)
0136 {
0137     int i;
0138 
0139     if (digi1->ndigi != digi2->ndigi)
0140         return 1;
0141 
0142     if (digi1->lastrepeat != digi2->lastrepeat)
0143         return 1;
0144 
0145     for (i = 0; i < digi1->ndigi; i++)
0146         if (ax25cmp(&digi1->calls[i], &digi2->calls[i]) != 0)
0147             return 1;
0148 
0149     return 0;
0150 }
0151 
0152 /*
0153  *  Given an AX.25 address pull of to, from, digi list, command/response and the start of data
0154  *
0155  */
0156 const unsigned char *ax25_addr_parse(const unsigned char *buf, int len,
0157     ax25_address *src, ax25_address *dest, ax25_digi *digi, int *flags,
0158     int *dama)
0159 {
0160     int d = 0;
0161 
0162     if (len < 14) return NULL;
0163 
0164     if (flags != NULL) {
0165         *flags = 0;
0166 
0167         if (buf[6] & AX25_CBIT)
0168             *flags = AX25_COMMAND;
0169         if (buf[13] & AX25_CBIT)
0170             *flags = AX25_RESPONSE;
0171     }
0172 
0173     if (dama != NULL)
0174         *dama = ~buf[13] & AX25_DAMA_FLAG;
0175 
0176     /* Copy to, from */
0177     if (dest != NULL)
0178         memcpy(dest, buf + 0, AX25_ADDR_LEN);
0179     if (src != NULL)
0180         memcpy(src,  buf + 7, AX25_ADDR_LEN);
0181 
0182     buf += 2 * AX25_ADDR_LEN;
0183     len -= 2 * AX25_ADDR_LEN;
0184 
0185     digi->lastrepeat = -1;
0186     digi->ndigi      = 0;
0187 
0188     while (!(buf[-1] & AX25_EBIT)) {
0189         if (d >= AX25_MAX_DIGIS)
0190             return NULL;
0191         if (len < AX25_ADDR_LEN)
0192             return NULL;
0193 
0194         memcpy(&digi->calls[d], buf, AX25_ADDR_LEN);
0195         digi->ndigi = d + 1;
0196 
0197         if (buf[6] & AX25_HBIT) {
0198             digi->repeated[d] = 1;
0199             digi->lastrepeat  = d;
0200         } else {
0201             digi->repeated[d] = 0;
0202         }
0203 
0204         buf += AX25_ADDR_LEN;
0205         len -= AX25_ADDR_LEN;
0206         d++;
0207     }
0208 
0209     return buf;
0210 }
0211 
0212 /*
0213  *  Assemble an AX.25 header from the bits
0214  */
0215 int ax25_addr_build(unsigned char *buf, const ax25_address *src,
0216     const ax25_address *dest, const ax25_digi *d, int flag, int modulus)
0217 {
0218     int len = 0;
0219     int ct  = 0;
0220 
0221     memcpy(buf, dest, AX25_ADDR_LEN);
0222     buf[6] &= ~(AX25_EBIT | AX25_CBIT);
0223     buf[6] |= AX25_SSSID_SPARE;
0224 
0225     if (flag == AX25_COMMAND) buf[6] |= AX25_CBIT;
0226 
0227     buf += AX25_ADDR_LEN;
0228     len += AX25_ADDR_LEN;
0229 
0230     memcpy(buf, src, AX25_ADDR_LEN);
0231     buf[6] &= ~(AX25_EBIT | AX25_CBIT);
0232     buf[6] &= ~AX25_SSSID_SPARE;
0233 
0234     if (modulus == AX25_MODULUS)
0235         buf[6] |= AX25_SSSID_SPARE;
0236     else
0237         buf[6] |= AX25_ESSID_SPARE;
0238 
0239     if (flag == AX25_RESPONSE) buf[6] |= AX25_CBIT;
0240 
0241     /*
0242      *  Fast path the normal digiless path
0243      */
0244     if (d == NULL || d->ndigi == 0) {
0245         buf[6] |= AX25_EBIT;
0246         return 2 * AX25_ADDR_LEN;
0247     }
0248 
0249     buf += AX25_ADDR_LEN;
0250     len += AX25_ADDR_LEN;
0251 
0252     while (ct < d->ndigi) {
0253         memcpy(buf, &d->calls[ct], AX25_ADDR_LEN);
0254 
0255         if (d->repeated[ct])
0256             buf[6] |= AX25_HBIT;
0257         else
0258             buf[6] &= ~AX25_HBIT;
0259 
0260         buf[6] &= ~AX25_EBIT;
0261         buf[6] |= AX25_SSSID_SPARE;
0262 
0263         buf += AX25_ADDR_LEN;
0264         len += AX25_ADDR_LEN;
0265         ct++;
0266     }
0267 
0268     buf[-1] |= AX25_EBIT;
0269 
0270     return len;
0271 }
0272 
0273 int ax25_addr_size(const ax25_digi *dp)
0274 {
0275     if (dp == NULL)
0276         return 2 * AX25_ADDR_LEN;
0277 
0278     return AX25_ADDR_LEN * (2 + dp->ndigi);
0279 }
0280 
0281 /*
0282  *  Reverse Digipeat List. May not pass both parameters as same struct
0283  */
0284 void ax25_digi_invert(const ax25_digi *in, ax25_digi *out)
0285 {
0286     int ct;
0287 
0288     out->ndigi      = in->ndigi;
0289     out->lastrepeat = in->ndigi - in->lastrepeat - 2;
0290 
0291     /* Invert the digipeaters */
0292     for (ct = 0; ct < in->ndigi; ct++) {
0293         out->calls[ct] = in->calls[in->ndigi - ct - 1];
0294 
0295         if (ct <= out->lastrepeat) {
0296             out->calls[ct].ax25_call[6] |= AX25_HBIT;
0297             out->repeated[ct]            = 1;
0298         } else {
0299             out->calls[ct].ax25_call[6] &= ~AX25_HBIT;
0300             out->repeated[ct]            = 0;
0301         }
0302     }
0303 }