ax25_addr.c 5.6 KB

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