18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci#include <linux/errno.h>
78c2ecf20Sopenharmony_ci#include <linux/types.h>
88c2ecf20Sopenharmony_ci#include <linux/socket.h>
98c2ecf20Sopenharmony_ci#include <linux/in.h>
108c2ecf20Sopenharmony_ci#include <linux/kernel.h>
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/timer.h>
138c2ecf20Sopenharmony_ci#include <linux/string.h>
148c2ecf20Sopenharmony_ci#include <linux/sockios.h>
158c2ecf20Sopenharmony_ci#include <linux/net.h>
168c2ecf20Sopenharmony_ci#include <net/ax25.h>
178c2ecf20Sopenharmony_ci#include <linux/inet.h>
188c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
198c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
208c2ecf20Sopenharmony_ci#include <net/sock.h>
218c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
228c2ecf20Sopenharmony_ci#include <linux/fcntl.h>
238c2ecf20Sopenharmony_ci#include <linux/mm.h>
248c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci/*
278c2ecf20Sopenharmony_ci * The default broadcast address of an interface is QST-0; the default address
288c2ecf20Sopenharmony_ci * is LINUX-1.  The null address is defined as a callsign of all spaces with
298c2ecf20Sopenharmony_ci * an SSID of zero.
308c2ecf20Sopenharmony_ci */
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ciconst ax25_address ax25_bcast =
338c2ecf20Sopenharmony_ci	{{'Q' << 1, 'S' << 1, 'T' << 1, ' ' << 1, ' ' << 1, ' ' << 1, 0 << 1}};
348c2ecf20Sopenharmony_ciconst ax25_address ax25_defaddr =
358c2ecf20Sopenharmony_ci	{{'L' << 1, 'I' << 1, 'N' << 1, 'U' << 1, 'X' << 1, ' ' << 1, 1 << 1}};
368c2ecf20Sopenharmony_ciconst ax25_address null_ax25_address =
378c2ecf20Sopenharmony_ci	{{' ' << 1, ' ' << 1, ' ' << 1, ' ' << 1, ' ' << 1, ' ' << 1, 0 << 1}};
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ax25_bcast);
408c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ax25_defaddr);
418c2ecf20Sopenharmony_ciEXPORT_SYMBOL(null_ax25_address);
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/*
448c2ecf20Sopenharmony_ci *	ax25 -> ascii conversion
458c2ecf20Sopenharmony_ci */
468c2ecf20Sopenharmony_cichar *ax2asc(char *buf, const ax25_address *a)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	char c, *s;
498c2ecf20Sopenharmony_ci	int n;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	for (n = 0, s = buf; n < 6; n++) {
528c2ecf20Sopenharmony_ci		c = (a->ax25_call[n] >> 1) & 0x7F;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci		if (c != ' ') *s++ = c;
558c2ecf20Sopenharmony_ci	}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	*s++ = '-';
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	if ((n = ((a->ax25_call[6] >> 1) & 0x0F)) > 9) {
608c2ecf20Sopenharmony_ci		*s++ = '1';
618c2ecf20Sopenharmony_ci		n -= 10;
628c2ecf20Sopenharmony_ci	}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	*s++ = n + '0';
658c2ecf20Sopenharmony_ci	*s++ = '\0';
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	if (*buf == '\0' || *buf == '-')
688c2ecf20Sopenharmony_ci	   return "*";
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	return buf;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ax2asc);
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci/*
778c2ecf20Sopenharmony_ci *	ascii -> ax25 conversion
788c2ecf20Sopenharmony_ci */
798c2ecf20Sopenharmony_civoid asc2ax(ax25_address *addr, const char *callsign)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	const char *s;
828c2ecf20Sopenharmony_ci	int n;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	for (s = callsign, n = 0; n < 6; n++) {
858c2ecf20Sopenharmony_ci		if (*s != '\0' && *s != '-')
868c2ecf20Sopenharmony_ci			addr->ax25_call[n] = *s++;
878c2ecf20Sopenharmony_ci		else
888c2ecf20Sopenharmony_ci			addr->ax25_call[n] = ' ';
898c2ecf20Sopenharmony_ci		addr->ax25_call[n] <<= 1;
908c2ecf20Sopenharmony_ci		addr->ax25_call[n] &= 0xFE;
918c2ecf20Sopenharmony_ci	}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	if (*s++ == '\0') {
948c2ecf20Sopenharmony_ci		addr->ax25_call[6] = 0x00;
958c2ecf20Sopenharmony_ci		return;
968c2ecf20Sopenharmony_ci	}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	addr->ax25_call[6] = *s++ - '0';
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	if (*s != '\0') {
1018c2ecf20Sopenharmony_ci		addr->ax25_call[6] *= 10;
1028c2ecf20Sopenharmony_ci		addr->ax25_call[6] += *s++ - '0';
1038c2ecf20Sopenharmony_ci	}
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	addr->ax25_call[6] <<= 1;
1068c2ecf20Sopenharmony_ci	addr->ax25_call[6] &= 0x1E;
1078c2ecf20Sopenharmony_ci}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ciEXPORT_SYMBOL(asc2ax);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci/*
1128c2ecf20Sopenharmony_ci *	Compare two ax.25 addresses
1138c2ecf20Sopenharmony_ci */
1148c2ecf20Sopenharmony_ciint ax25cmp(const ax25_address *a, const ax25_address *b)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	int ct = 0;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	while (ct < 6) {
1198c2ecf20Sopenharmony_ci		if ((a->ax25_call[ct] & 0xFE) != (b->ax25_call[ct] & 0xFE))	/* Clean off repeater bits */
1208c2ecf20Sopenharmony_ci			return 1;
1218c2ecf20Sopenharmony_ci		ct++;
1228c2ecf20Sopenharmony_ci	}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	if ((a->ax25_call[ct] & 0x1E) == (b->ax25_call[ct] & 0x1E))	/* SSID without control bit */
1258c2ecf20Sopenharmony_ci		return 0;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	return 2;			/* Partial match */
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ax25cmp);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci/*
1338c2ecf20Sopenharmony_ci *	Compare two AX.25 digipeater paths.
1348c2ecf20Sopenharmony_ci */
1358c2ecf20Sopenharmony_ciint ax25digicmp(const ax25_digi *digi1, const ax25_digi *digi2)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	int i;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	if (digi1->ndigi != digi2->ndigi)
1408c2ecf20Sopenharmony_ci		return 1;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	if (digi1->lastrepeat != digi2->lastrepeat)
1438c2ecf20Sopenharmony_ci		return 1;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	for (i = 0; i < digi1->ndigi; i++)
1468c2ecf20Sopenharmony_ci		if (ax25cmp(&digi1->calls[i], &digi2->calls[i]) != 0)
1478c2ecf20Sopenharmony_ci			return 1;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	return 0;
1508c2ecf20Sopenharmony_ci}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci/*
1538c2ecf20Sopenharmony_ci *	Given an AX.25 address pull of to, from, digi list, command/response and the start of data
1548c2ecf20Sopenharmony_ci *
1558c2ecf20Sopenharmony_ci */
1568c2ecf20Sopenharmony_ciconst unsigned char *ax25_addr_parse(const unsigned char *buf, int len,
1578c2ecf20Sopenharmony_ci	ax25_address *src, ax25_address *dest, ax25_digi *digi, int *flags,
1588c2ecf20Sopenharmony_ci	int *dama)
1598c2ecf20Sopenharmony_ci{
1608c2ecf20Sopenharmony_ci	int d = 0;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	if (len < 14) return NULL;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	if (flags != NULL) {
1658c2ecf20Sopenharmony_ci		*flags = 0;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci		if (buf[6] & AX25_CBIT)
1688c2ecf20Sopenharmony_ci			*flags = AX25_COMMAND;
1698c2ecf20Sopenharmony_ci		if (buf[13] & AX25_CBIT)
1708c2ecf20Sopenharmony_ci			*flags = AX25_RESPONSE;
1718c2ecf20Sopenharmony_ci	}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	if (dama != NULL)
1748c2ecf20Sopenharmony_ci		*dama = ~buf[13] & AX25_DAMA_FLAG;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	/* Copy to, from */
1778c2ecf20Sopenharmony_ci	if (dest != NULL)
1788c2ecf20Sopenharmony_ci		memcpy(dest, buf + 0, AX25_ADDR_LEN);
1798c2ecf20Sopenharmony_ci	if (src != NULL)
1808c2ecf20Sopenharmony_ci		memcpy(src,  buf + 7, AX25_ADDR_LEN);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	buf += 2 * AX25_ADDR_LEN;
1838c2ecf20Sopenharmony_ci	len -= 2 * AX25_ADDR_LEN;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	digi->lastrepeat = -1;
1868c2ecf20Sopenharmony_ci	digi->ndigi      = 0;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	while (!(buf[-1] & AX25_EBIT)) {
1898c2ecf20Sopenharmony_ci		if (d >= AX25_MAX_DIGIS)
1908c2ecf20Sopenharmony_ci			return NULL;
1918c2ecf20Sopenharmony_ci		if (len < AX25_ADDR_LEN)
1928c2ecf20Sopenharmony_ci			return NULL;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci		memcpy(&digi->calls[d], buf, AX25_ADDR_LEN);
1958c2ecf20Sopenharmony_ci		digi->ndigi = d + 1;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci		if (buf[6] & AX25_HBIT) {
1988c2ecf20Sopenharmony_ci			digi->repeated[d] = 1;
1998c2ecf20Sopenharmony_ci			digi->lastrepeat  = d;
2008c2ecf20Sopenharmony_ci		} else {
2018c2ecf20Sopenharmony_ci			digi->repeated[d] = 0;
2028c2ecf20Sopenharmony_ci		}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci		buf += AX25_ADDR_LEN;
2058c2ecf20Sopenharmony_ci		len -= AX25_ADDR_LEN;
2068c2ecf20Sopenharmony_ci		d++;
2078c2ecf20Sopenharmony_ci	}
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	return buf;
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci/*
2138c2ecf20Sopenharmony_ci *	Assemble an AX.25 header from the bits
2148c2ecf20Sopenharmony_ci */
2158c2ecf20Sopenharmony_ciint ax25_addr_build(unsigned char *buf, const ax25_address *src,
2168c2ecf20Sopenharmony_ci	const ax25_address *dest, const ax25_digi *d, int flag, int modulus)
2178c2ecf20Sopenharmony_ci{
2188c2ecf20Sopenharmony_ci	int len = 0;
2198c2ecf20Sopenharmony_ci	int ct  = 0;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	memcpy(buf, dest, AX25_ADDR_LEN);
2228c2ecf20Sopenharmony_ci	buf[6] &= ~(AX25_EBIT | AX25_CBIT);
2238c2ecf20Sopenharmony_ci	buf[6] |= AX25_SSSID_SPARE;
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	if (flag == AX25_COMMAND) buf[6] |= AX25_CBIT;
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	buf += AX25_ADDR_LEN;
2288c2ecf20Sopenharmony_ci	len += AX25_ADDR_LEN;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	memcpy(buf, src, AX25_ADDR_LEN);
2318c2ecf20Sopenharmony_ci	buf[6] &= ~(AX25_EBIT | AX25_CBIT);
2328c2ecf20Sopenharmony_ci	buf[6] &= ~AX25_SSSID_SPARE;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	if (modulus == AX25_MODULUS)
2358c2ecf20Sopenharmony_ci		buf[6] |= AX25_SSSID_SPARE;
2368c2ecf20Sopenharmony_ci	else
2378c2ecf20Sopenharmony_ci		buf[6] |= AX25_ESSID_SPARE;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	if (flag == AX25_RESPONSE) buf[6] |= AX25_CBIT;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	/*
2428c2ecf20Sopenharmony_ci	 *	Fast path the normal digiless path
2438c2ecf20Sopenharmony_ci	 */
2448c2ecf20Sopenharmony_ci	if (d == NULL || d->ndigi == 0) {
2458c2ecf20Sopenharmony_ci		buf[6] |= AX25_EBIT;
2468c2ecf20Sopenharmony_ci		return 2 * AX25_ADDR_LEN;
2478c2ecf20Sopenharmony_ci	}
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	buf += AX25_ADDR_LEN;
2508c2ecf20Sopenharmony_ci	len += AX25_ADDR_LEN;
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	while (ct < d->ndigi) {
2538c2ecf20Sopenharmony_ci		memcpy(buf, &d->calls[ct], AX25_ADDR_LEN);
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci		if (d->repeated[ct])
2568c2ecf20Sopenharmony_ci			buf[6] |= AX25_HBIT;
2578c2ecf20Sopenharmony_ci		else
2588c2ecf20Sopenharmony_ci			buf[6] &= ~AX25_HBIT;
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci		buf[6] &= ~AX25_EBIT;
2618c2ecf20Sopenharmony_ci		buf[6] |= AX25_SSSID_SPARE;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci		buf += AX25_ADDR_LEN;
2648c2ecf20Sopenharmony_ci		len += AX25_ADDR_LEN;
2658c2ecf20Sopenharmony_ci		ct++;
2668c2ecf20Sopenharmony_ci	}
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	buf[-1] |= AX25_EBIT;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	return len;
2718c2ecf20Sopenharmony_ci}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ciint ax25_addr_size(const ax25_digi *dp)
2748c2ecf20Sopenharmony_ci{
2758c2ecf20Sopenharmony_ci	if (dp == NULL)
2768c2ecf20Sopenharmony_ci		return 2 * AX25_ADDR_LEN;
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	return AX25_ADDR_LEN * (2 + dp->ndigi);
2798c2ecf20Sopenharmony_ci}
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci/*
2828c2ecf20Sopenharmony_ci *	Reverse Digipeat List. May not pass both parameters as same struct
2838c2ecf20Sopenharmony_ci */
2848c2ecf20Sopenharmony_civoid ax25_digi_invert(const ax25_digi *in, ax25_digi *out)
2858c2ecf20Sopenharmony_ci{
2868c2ecf20Sopenharmony_ci	int ct;
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	out->ndigi      = in->ndigi;
2898c2ecf20Sopenharmony_ci	out->lastrepeat = in->ndigi - in->lastrepeat - 2;
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	/* Invert the digipeaters */
2928c2ecf20Sopenharmony_ci	for (ct = 0; ct < in->ndigi; ct++) {
2938c2ecf20Sopenharmony_ci		out->calls[ct] = in->calls[in->ndigi - ct - 1];
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci		if (ct <= out->lastrepeat) {
2968c2ecf20Sopenharmony_ci			out->calls[ct].ax25_call[6] |= AX25_HBIT;
2978c2ecf20Sopenharmony_ci			out->repeated[ct]            = 1;
2988c2ecf20Sopenharmony_ci		} else {
2998c2ecf20Sopenharmony_ci			out->calls[ct].ax25_call[6] &= ~AX25_HBIT;
3008c2ecf20Sopenharmony_ci			out->repeated[ct]            = 0;
3018c2ecf20Sopenharmony_ci		}
3028c2ecf20Sopenharmony_ci	}
3038c2ecf20Sopenharmony_ci}
304