ax25_addr.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
  5. */
  6. #include <linux/errno.h>
  7. #include <linux/types.h>
  8. #include <linux/socket.h>
  9. #include <linux/in.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/timer.h>
  13. #include <linux/string.h>
  14. #include <linux/sockios.h>
  15. #include <linux/net.h>
  16. #include <net/ax25.h>
  17. #include <linux/inet.h>
  18. #include <linux/netdevice.h>
  19. #include <linux/skbuff.h>
  20. #include <net/sock.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/fcntl.h>
  23. #include <linux/mm.h>
  24. #include <linux/interrupt.h>
  25. /*
  26. * The default broadcast address of an interface is QST-0; the default address
  27. * is LINUX-1. The null address is defined as a callsign of all spaces with
  28. * an SSID of zero.
  29. */
  30. const ax25_address ax25_bcast =
  31. {{'Q' << 1, 'S' << 1, 'T' << 1, ' ' << 1, ' ' << 1, ' ' << 1, 0 << 1}};
  32. const ax25_address ax25_defaddr =
  33. {{'L' << 1, 'I' << 1, 'N' << 1, 'U' << 1, 'X' << 1, ' ' << 1, 1 << 1}};
  34. const ax25_address null_ax25_address =
  35. {{' ' << 1, ' ' << 1, ' ' << 1, ' ' << 1, ' ' << 1, ' ' << 1, 0 << 1}};
  36. EXPORT_SYMBOL_GPL(ax25_bcast);
  37. EXPORT_SYMBOL_GPL(ax25_defaddr);
  38. EXPORT_SYMBOL(null_ax25_address);
  39. /*
  40. * ax25 -> ascii conversion
  41. */
  42. char *ax2asc(char *buf, const ax25_address *a)
  43. {
  44. char c, *s;
  45. int n;
  46. for (n = 0, s = buf; n < 6; n++) {
  47. c = (a->ax25_call[n] >> 1) & 0x7F;
  48. if (c != ' ') *s++ = c;
  49. }
  50. *s++ = '-';
  51. if ((n = ((a->ax25_call[6] >> 1) & 0x0F)) > 9) {
  52. *s++ = '1';
  53. n -= 10;
  54. }
  55. *s++ = n + '0';
  56. *s++ = '\0';
  57. if (*buf == '\0' || *buf == '-')
  58. return "*";
  59. return buf;
  60. }
  61. EXPORT_SYMBOL(ax2asc);
  62. /*
  63. * ascii -> ax25 conversion
  64. */
  65. void asc2ax(ax25_address *addr, const char *callsign)
  66. {
  67. const char *s;
  68. int n;
  69. for (s = callsign, n = 0; n < 6; n++) {
  70. if (*s != '\0' && *s != '-')
  71. addr->ax25_call[n] = *s++;
  72. else
  73. addr->ax25_call[n] = ' ';
  74. addr->ax25_call[n] <<= 1;
  75. addr->ax25_call[n] &= 0xFE;
  76. }
  77. if (*s++ == '\0') {
  78. addr->ax25_call[6] = 0x00;
  79. return;
  80. }
  81. addr->ax25_call[6] = *s++ - '0';
  82. if (*s != '\0') {
  83. addr->ax25_call[6] *= 10;
  84. addr->ax25_call[6] += *s++ - '0';
  85. }
  86. addr->ax25_call[6] <<= 1;
  87. addr->ax25_call[6] &= 0x1E;
  88. }
  89. EXPORT_SYMBOL(asc2ax);
  90. /*
  91. * Compare two ax.25 addresses
  92. */
  93. int ax25cmp(const ax25_address *a, const ax25_address *b)
  94. {
  95. int ct = 0;
  96. while (ct < 6) {
  97. if ((a->ax25_call[ct] & 0xFE) != (b->ax25_call[ct] & 0xFE)) /* Clean off repeater bits */
  98. return 1;
  99. ct++;
  100. }
  101. if ((a->ax25_call[ct] & 0x1E) == (b->ax25_call[ct] & 0x1E)) /* SSID without control bit */
  102. return 0;
  103. return 2; /* Partial match */
  104. }
  105. EXPORT_SYMBOL(ax25cmp);
  106. /*
  107. * Compare two AX.25 digipeater paths.
  108. */
  109. int ax25digicmp(const ax25_digi *digi1, const ax25_digi *digi2)
  110. {
  111. int i;
  112. if (digi1->ndigi != digi2->ndigi)
  113. return 1;
  114. if (digi1->lastrepeat != digi2->lastrepeat)
  115. return 1;
  116. for (i = 0; i < digi1->ndigi; i++)
  117. if (ax25cmp(&digi1->calls[i], &digi2->calls[i]) != 0)
  118. return 1;
  119. return 0;
  120. }
  121. /*
  122. * Given an AX.25 address pull of to, from, digi list, command/response and the start of data
  123. *
  124. */
  125. const unsigned char *ax25_addr_parse(const unsigned char *buf, int len,
  126. ax25_address *src, ax25_address *dest, ax25_digi *digi, int *flags,
  127. int *dama)
  128. {
  129. int d = 0;
  130. if (len < 14) return NULL;
  131. if (flags != NULL) {
  132. *flags = 0;
  133. if (buf[6] & AX25_CBIT)
  134. *flags = AX25_COMMAND;
  135. if (buf[13] & AX25_CBIT)
  136. *flags = AX25_RESPONSE;
  137. }
  138. if (dama != NULL)
  139. *dama = ~buf[13] & AX25_DAMA_FLAG;
  140. /* Copy to, from */
  141. if (dest != NULL)
  142. memcpy(dest, buf + 0, AX25_ADDR_LEN);
  143. if (src != NULL)
  144. memcpy(src, buf + 7, AX25_ADDR_LEN);
  145. buf += 2 * AX25_ADDR_LEN;
  146. len -= 2 * AX25_ADDR_LEN;
  147. digi->lastrepeat = -1;
  148. digi->ndigi = 0;
  149. while (!(buf[-1] & AX25_EBIT)) {
  150. if (d >= AX25_MAX_DIGIS)
  151. return NULL;
  152. if (len < AX25_ADDR_LEN)
  153. return NULL;
  154. memcpy(&digi->calls[d], buf, AX25_ADDR_LEN);
  155. digi->ndigi = d + 1;
  156. if (buf[6] & AX25_HBIT) {
  157. digi->repeated[d] = 1;
  158. digi->lastrepeat = d;
  159. } else {
  160. digi->repeated[d] = 0;
  161. }
  162. buf += AX25_ADDR_LEN;
  163. len -= AX25_ADDR_LEN;
  164. d++;
  165. }
  166. return buf;
  167. }
  168. /*
  169. * Assemble an AX.25 header from the bits
  170. */
  171. int ax25_addr_build(unsigned char *buf, const ax25_address *src,
  172. const ax25_address *dest, const ax25_digi *d, int flag, int modulus)
  173. {
  174. int len = 0;
  175. int ct = 0;
  176. memcpy(buf, dest, AX25_ADDR_LEN);
  177. buf[6] &= ~(AX25_EBIT | AX25_CBIT);
  178. buf[6] |= AX25_SSSID_SPARE;
  179. if (flag == AX25_COMMAND) buf[6] |= AX25_CBIT;
  180. buf += AX25_ADDR_LEN;
  181. len += AX25_ADDR_LEN;
  182. memcpy(buf, src, AX25_ADDR_LEN);
  183. buf[6] &= ~(AX25_EBIT | AX25_CBIT);
  184. buf[6] &= ~AX25_SSSID_SPARE;
  185. if (modulus == AX25_MODULUS)
  186. buf[6] |= AX25_SSSID_SPARE;
  187. else
  188. buf[6] |= AX25_ESSID_SPARE;
  189. if (flag == AX25_RESPONSE) buf[6] |= AX25_CBIT;
  190. /*
  191. * Fast path the normal digiless path
  192. */
  193. if (d == NULL || d->ndigi == 0) {
  194. buf[6] |= AX25_EBIT;
  195. return 2 * AX25_ADDR_LEN;
  196. }
  197. buf += AX25_ADDR_LEN;
  198. len += AX25_ADDR_LEN;
  199. while (ct < d->ndigi) {
  200. memcpy(buf, &d->calls[ct], AX25_ADDR_LEN);
  201. if (d->repeated[ct])
  202. buf[6] |= AX25_HBIT;
  203. else
  204. buf[6] &= ~AX25_HBIT;
  205. buf[6] &= ~AX25_EBIT;
  206. buf[6] |= AX25_SSSID_SPARE;
  207. buf += AX25_ADDR_LEN;
  208. len += AX25_ADDR_LEN;
  209. ct++;
  210. }
  211. buf[-1] |= AX25_EBIT;
  212. return len;
  213. }
  214. int ax25_addr_size(const ax25_digi *dp)
  215. {
  216. if (dp == NULL)
  217. return 2 * AX25_ADDR_LEN;
  218. return AX25_ADDR_LEN * (2 + dp->ndigi);
  219. }
  220. /*
  221. * Reverse Digipeat List. May not pass both parameters as same struct
  222. */
  223. void ax25_digi_invert(const ax25_digi *in, ax25_digi *out)
  224. {
  225. int ct;
  226. out->ndigi = in->ndigi;
  227. out->lastrepeat = in->ndigi - in->lastrepeat - 2;
  228. /* Invert the digipeaters */
  229. for (ct = 0; ct < in->ndigi; ct++) {
  230. out->calls[ct] = in->calls[in->ndigi - ct - 1];
  231. if (ct <= out->lastrepeat) {
  232. out->calls[ct].ax25_call[6] |= AX25_HBIT;
  233. out->repeated[ct] = 1;
  234. } else {
  235. out->calls[ct].ax25_call[6] &= ~AX25_HBIT;
  236. out->repeated[ct] = 0;
  237. }
  238. }
  239. }