x25_facilities.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * X.25 Packet Layer release 002
  3. *
  4. * This is ALPHA test software. This code may break your machine,
  5. * randomly fail to work with new releases, misbehave and/or generally
  6. * screw up. It might even work.
  7. *
  8. * This code REQUIRES 2.1.15 or higher
  9. *
  10. * This module:
  11. * This module is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. *
  16. * History
  17. * X.25 001 Split from x25_subr.c
  18. * mar/20/00 Daniela Squassoni Disabling/enabling of facilities
  19. * negotiation.
  20. * apr/14/05 Shaun Pereira - Allow fast select with no restriction
  21. * on response.
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/string.h>
  25. #include <linux/skbuff.h>
  26. #include <net/sock.h>
  27. #include <net/x25.h>
  28. /*
  29. * Parse a set of facilities into the facilities structures. Unrecognised
  30. * facilities are written to the debug log file.
  31. */
  32. int x25_parse_facilities(struct sk_buff *skb, struct x25_facilities *facilities,
  33. struct x25_dte_facilities *dte_facs, unsigned long *vc_fac_mask)
  34. {
  35. unsigned char *p = skb->data;
  36. unsigned int len = *p++;
  37. *vc_fac_mask = 0;
  38. /*
  39. * The kernel knows which facilities were set on an incoming call but
  40. * currently this information is not available to userspace. Here we
  41. * give userspace who read incoming call facilities 0 length to indicate
  42. * it wasn't set.
  43. */
  44. dte_facs->calling_len = 0;
  45. dte_facs->called_len = 0;
  46. memset(dte_facs->called_ae, '\0', sizeof(dte_facs->called_ae));
  47. memset(dte_facs->calling_ae, '\0', sizeof(dte_facs->calling_ae));
  48. while (len > 0) {
  49. switch (*p & X25_FAC_CLASS_MASK) {
  50. case X25_FAC_CLASS_A:
  51. switch (*p) {
  52. case X25_FAC_REVERSE:
  53. if((p[1] & 0x81) == 0x81) {
  54. facilities->reverse = p[1] & 0x81;
  55. *vc_fac_mask |= X25_MASK_REVERSE;
  56. break;
  57. }
  58. if((p[1] & 0x01) == 0x01) {
  59. facilities->reverse = p[1] & 0x01;
  60. *vc_fac_mask |= X25_MASK_REVERSE;
  61. break;
  62. }
  63. if((p[1] & 0x80) == 0x80) {
  64. facilities->reverse = p[1] & 0x80;
  65. *vc_fac_mask |= X25_MASK_REVERSE;
  66. break;
  67. }
  68. if(p[1] == 0x00) {
  69. facilities->reverse
  70. = X25_DEFAULT_REVERSE;
  71. *vc_fac_mask |= X25_MASK_REVERSE;
  72. break;
  73. }
  74. case X25_FAC_THROUGHPUT:
  75. facilities->throughput = p[1];
  76. *vc_fac_mask |= X25_MASK_THROUGHPUT;
  77. break;
  78. case X25_MARKER:
  79. break;
  80. default:
  81. printk(KERN_DEBUG "X.25: unknown facility "
  82. "%02X, value %02X\n",
  83. p[0], p[1]);
  84. break;
  85. }
  86. p += 2;
  87. len -= 2;
  88. break;
  89. case X25_FAC_CLASS_B:
  90. switch (*p) {
  91. case X25_FAC_PACKET_SIZE:
  92. facilities->pacsize_in = p[1];
  93. facilities->pacsize_out = p[2];
  94. *vc_fac_mask |= X25_MASK_PACKET_SIZE;
  95. break;
  96. case X25_FAC_WINDOW_SIZE:
  97. facilities->winsize_in = p[1];
  98. facilities->winsize_out = p[2];
  99. *vc_fac_mask |= X25_MASK_WINDOW_SIZE;
  100. break;
  101. default:
  102. printk(KERN_DEBUG "X.25: unknown facility "
  103. "%02X, values %02X, %02X\n",
  104. p[0], p[1], p[2]);
  105. break;
  106. }
  107. p += 3;
  108. len -= 3;
  109. break;
  110. case X25_FAC_CLASS_C:
  111. printk(KERN_DEBUG "X.25: unknown facility %02X, "
  112. "values %02X, %02X, %02X\n",
  113. p[0], p[1], p[2], p[3]);
  114. p += 4;
  115. len -= 4;
  116. break;
  117. case X25_FAC_CLASS_D:
  118. switch (*p) {
  119. case X25_FAC_CALLING_AE:
  120. if (p[1] > X25_MAX_DTE_FACIL_LEN)
  121. break;
  122. dte_facs->calling_len = p[2];
  123. memcpy(dte_facs->calling_ae, &p[3], p[1] - 1);
  124. *vc_fac_mask |= X25_MASK_CALLING_AE;
  125. break;
  126. case X25_FAC_CALLED_AE:
  127. if (p[1] > X25_MAX_DTE_FACIL_LEN)
  128. break;
  129. dte_facs->called_len = p[2];
  130. memcpy(dte_facs->called_ae, &p[3], p[1] - 1);
  131. *vc_fac_mask |= X25_MASK_CALLED_AE;
  132. break;
  133. default:
  134. printk(KERN_DEBUG "X.25: unknown facility %02X,"
  135. "length %d, values %02X, %02X, "
  136. "%02X, %02X\n",
  137. p[0], p[1], p[2], p[3], p[4], p[5]);
  138. break;
  139. }
  140. len -= p[1] + 2;
  141. p += p[1] + 2;
  142. break;
  143. }
  144. }
  145. return p - skb->data;
  146. }
  147. /*
  148. * Create a set of facilities.
  149. */
  150. int x25_create_facilities(unsigned char *buffer,
  151. struct x25_facilities *facilities,
  152. struct x25_dte_facilities *dte_facs, unsigned long facil_mask)
  153. {
  154. unsigned char *p = buffer + 1;
  155. int len;
  156. if (!facil_mask) {
  157. /*
  158. * Length of the facilities field in call_req or
  159. * call_accept packets
  160. */
  161. buffer[0] = 0;
  162. len = 1; /* 1 byte for the length field */
  163. return len;
  164. }
  165. if (facilities->reverse && (facil_mask & X25_MASK_REVERSE)) {
  166. *p++ = X25_FAC_REVERSE;
  167. *p++ = facilities->reverse;
  168. }
  169. if (facilities->throughput && (facil_mask & X25_MASK_THROUGHPUT)) {
  170. *p++ = X25_FAC_THROUGHPUT;
  171. *p++ = facilities->throughput;
  172. }
  173. if ((facilities->pacsize_in || facilities->pacsize_out) &&
  174. (facil_mask & X25_MASK_PACKET_SIZE)) {
  175. *p++ = X25_FAC_PACKET_SIZE;
  176. *p++ = facilities->pacsize_in ? : facilities->pacsize_out;
  177. *p++ = facilities->pacsize_out ? : facilities->pacsize_in;
  178. }
  179. if ((facilities->winsize_in || facilities->winsize_out) &&
  180. (facil_mask & X25_MASK_WINDOW_SIZE)) {
  181. *p++ = X25_FAC_WINDOW_SIZE;
  182. *p++ = facilities->winsize_in ? : facilities->winsize_out;
  183. *p++ = facilities->winsize_out ? : facilities->winsize_in;
  184. }
  185. if (facil_mask & (X25_MASK_CALLING_AE|X25_MASK_CALLED_AE)) {
  186. *p++ = X25_MARKER;
  187. *p++ = X25_DTE_SERVICES;
  188. }
  189. if (dte_facs->calling_len && (facil_mask & X25_MASK_CALLING_AE)) {
  190. unsigned bytecount = (dte_facs->calling_len % 2) ?
  191. dte_facs->calling_len / 2 + 1 :
  192. dte_facs->calling_len / 2;
  193. *p++ = X25_FAC_CALLING_AE;
  194. *p++ = 1 + bytecount;
  195. *p++ = dte_facs->calling_len;
  196. memcpy(p, dte_facs->calling_ae, bytecount);
  197. p += bytecount;
  198. }
  199. if (dte_facs->called_len && (facil_mask & X25_MASK_CALLED_AE)) {
  200. unsigned bytecount = (dte_facs->called_len % 2) ?
  201. dte_facs->called_len / 2 + 1 :
  202. dte_facs->called_len / 2;
  203. *p++ = X25_FAC_CALLED_AE;
  204. *p++ = 1 + bytecount;
  205. *p++ = dte_facs->called_len;
  206. memcpy(p, dte_facs->called_ae, bytecount);
  207. p+=bytecount;
  208. }
  209. len = p - buffer;
  210. buffer[0] = len - 1;
  211. return len;
  212. }
  213. /*
  214. * Try to reach a compromise on a set of facilities.
  215. *
  216. * The only real problem is with reverse charging.
  217. */
  218. int x25_negotiate_facilities(struct sk_buff *skb, struct sock *sk,
  219. struct x25_facilities *new, struct x25_dte_facilities *dte)
  220. {
  221. struct x25_sock *x25 = x25_sk(sk);
  222. struct x25_facilities *ours = &x25->facilities;
  223. struct x25_facilities theirs;
  224. int len;
  225. memset(&theirs, 0, sizeof(theirs));
  226. memcpy(new, ours, sizeof(*new));
  227. len = x25_parse_facilities(skb, &theirs, dte, &x25->vc_facil_mask);
  228. /*
  229. * They want reverse charging, we won't accept it.
  230. */
  231. if ((theirs.reverse & 0x01 ) && (ours->reverse & 0x01)) {
  232. SOCK_DEBUG(sk, "X.25: rejecting reverse charging request\n");
  233. return -1;
  234. }
  235. new->reverse = theirs.reverse;
  236. if (theirs.throughput) {
  237. if (theirs.throughput < ours->throughput) {
  238. SOCK_DEBUG(sk, "X.25: throughput negotiated down\n");
  239. new->throughput = theirs.throughput;
  240. }
  241. }
  242. if (theirs.pacsize_in && theirs.pacsize_out) {
  243. if (theirs.pacsize_in < ours->pacsize_in) {
  244. SOCK_DEBUG(sk, "X.25: packet size inwards negotiated down\n");
  245. new->pacsize_in = theirs.pacsize_in;
  246. }
  247. if (theirs.pacsize_out < ours->pacsize_out) {
  248. SOCK_DEBUG(sk, "X.25: packet size outwards negotiated down\n");
  249. new->pacsize_out = theirs.pacsize_out;
  250. }
  251. }
  252. if (theirs.winsize_in && theirs.winsize_out) {
  253. if (theirs.winsize_in < ours->winsize_in) {
  254. SOCK_DEBUG(sk, "X.25: window size inwards negotiated down\n");
  255. new->winsize_in = theirs.winsize_in;
  256. }
  257. if (theirs.winsize_out < ours->winsize_out) {
  258. SOCK_DEBUG(sk, "X.25: window size outwards negotiated down\n");
  259. new->winsize_out = theirs.winsize_out;
  260. }
  261. }
  262. return len;
  263. }
  264. /*
  265. * Limit values of certain facilities according to the capability of the
  266. * currently attached x25 link.
  267. */
  268. void x25_limit_facilities(struct x25_facilities *facilities,
  269. struct x25_neigh *nb)
  270. {
  271. if (!nb->extended) {
  272. if (facilities->winsize_in > 7) {
  273. printk(KERN_DEBUG "X.25: incoming winsize limited to 7\n");
  274. facilities->winsize_in = 7;
  275. }
  276. if (facilities->winsize_out > 7) {
  277. facilities->winsize_out = 7;
  278. printk( KERN_DEBUG "X.25: outgoing winsize limited to 7\n");
  279. }
  280. }
  281. }