ax25_addr.c 5.5 KB

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