ax25_addr.c 5.5 KB

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