ax25_addr.c 6.1 KB

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