x25_facilities.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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;
  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. if (skb->len < 1)
  49. return 0;
  50. len = *p++;
  51. if (len >= skb->len)
  52. return -1;
  53. while (len > 0) {
  54. switch (*p & X25_FAC_CLASS_MASK) {
  55. case X25_FAC_CLASS_A:
  56. switch (*p) {
  57. case X25_FAC_REVERSE:
  58. if((p[1] & 0x81) == 0x81) {
  59. facilities->reverse = p[1] & 0x81;
  60. *vc_fac_mask |= X25_MASK_REVERSE;
  61. break;
  62. }
  63. if((p[1] & 0x01) == 0x01) {
  64. facilities->reverse = p[1] & 0x01;
  65. *vc_fac_mask |= X25_MASK_REVERSE;
  66. break;
  67. }
  68. if((p[1] & 0x80) == 0x80) {
  69. facilities->reverse = p[1] & 0x80;
  70. *vc_fac_mask |= X25_MASK_REVERSE;
  71. break;
  72. }
  73. if(p[1] == 0x00) {
  74. facilities->reverse
  75. = X25_DEFAULT_REVERSE;
  76. *vc_fac_mask |= X25_MASK_REVERSE;
  77. break;
  78. }
  79. case X25_FAC_THROUGHPUT:
  80. facilities->throughput = p[1];
  81. *vc_fac_mask |= X25_MASK_THROUGHPUT;
  82. break;
  83. case X25_MARKER:
  84. break;
  85. default:
  86. printk(KERN_DEBUG "X.25: unknown facility "
  87. "%02X, value %02X\n",
  88. p[0], p[1]);
  89. break;
  90. }
  91. p += 2;
  92. len -= 2;
  93. break;
  94. case X25_FAC_CLASS_B:
  95. switch (*p) {
  96. case X25_FAC_PACKET_SIZE:
  97. facilities->pacsize_in = p[1];
  98. facilities->pacsize_out = p[2];
  99. *vc_fac_mask |= X25_MASK_PACKET_SIZE;
  100. break;
  101. case X25_FAC_WINDOW_SIZE:
  102. facilities->winsize_in = p[1];
  103. facilities->winsize_out = p[2];
  104. *vc_fac_mask |= X25_MASK_WINDOW_SIZE;
  105. break;
  106. default:
  107. printk(KERN_DEBUG "X.25: unknown facility "
  108. "%02X, values %02X, %02X\n",
  109. p[0], p[1], p[2]);
  110. break;
  111. }
  112. p += 3;
  113. len -= 3;
  114. break;
  115. case X25_FAC_CLASS_C:
  116. printk(KERN_DEBUG "X.25: unknown facility %02X, "
  117. "values %02X, %02X, %02X\n",
  118. p[0], p[1], p[2], p[3]);
  119. p += 4;
  120. len -= 4;
  121. break;
  122. case X25_FAC_CLASS_D:
  123. switch (*p) {
  124. case X25_FAC_CALLING_AE:
  125. if (p[1] > X25_MAX_DTE_FACIL_LEN)
  126. break;
  127. dte_facs->calling_len = p[2];
  128. memcpy(dte_facs->calling_ae, &p[3], p[1] - 1);
  129. *vc_fac_mask |= X25_MASK_CALLING_AE;
  130. break;
  131. case X25_FAC_CALLED_AE:
  132. if (p[1] > X25_MAX_DTE_FACIL_LEN)
  133. break;
  134. dte_facs->called_len = p[2];
  135. memcpy(dte_facs->called_ae, &p[3], p[1] - 1);
  136. *vc_fac_mask |= X25_MASK_CALLED_AE;
  137. break;
  138. default:
  139. printk(KERN_DEBUG "X.25: unknown facility %02X,"
  140. "length %d, values %02X, %02X, "
  141. "%02X, %02X\n",
  142. p[0], p[1], p[2], p[3], p[4], p[5]);
  143. break;
  144. }
  145. len -= p[1] + 2;
  146. p += p[1] + 2;
  147. break;
  148. }
  149. }
  150. return p - skb->data;
  151. }
  152. /*
  153. * Create a set of facilities.
  154. */
  155. int x25_create_facilities(unsigned char *buffer,
  156. struct x25_facilities *facilities,
  157. struct x25_dte_facilities *dte_facs, unsigned long facil_mask)
  158. {
  159. unsigned char *p = buffer + 1;
  160. int len;
  161. if (!facil_mask) {
  162. /*
  163. * Length of the facilities field in call_req or
  164. * call_accept packets
  165. */
  166. buffer[0] = 0;
  167. len = 1; /* 1 byte for the length field */
  168. return len;
  169. }
  170. if (facilities->reverse && (facil_mask & X25_MASK_REVERSE)) {
  171. *p++ = X25_FAC_REVERSE;
  172. *p++ = facilities->reverse;
  173. }
  174. if (facilities->throughput && (facil_mask & X25_MASK_THROUGHPUT)) {
  175. *p++ = X25_FAC_THROUGHPUT;
  176. *p++ = facilities->throughput;
  177. }
  178. if ((facilities->pacsize_in || facilities->pacsize_out) &&
  179. (facil_mask & X25_MASK_PACKET_SIZE)) {
  180. *p++ = X25_FAC_PACKET_SIZE;
  181. *p++ = facilities->pacsize_in ? : facilities->pacsize_out;
  182. *p++ = facilities->pacsize_out ? : facilities->pacsize_in;
  183. }
  184. if ((facilities->winsize_in || facilities->winsize_out) &&
  185. (facil_mask & X25_MASK_WINDOW_SIZE)) {
  186. *p++ = X25_FAC_WINDOW_SIZE;
  187. *p++ = facilities->winsize_in ? : facilities->winsize_out;
  188. *p++ = facilities->winsize_out ? : facilities->winsize_in;
  189. }
  190. if (facil_mask & (X25_MASK_CALLING_AE|X25_MASK_CALLED_AE)) {
  191. *p++ = X25_MARKER;
  192. *p++ = X25_DTE_SERVICES;
  193. }
  194. if (dte_facs->calling_len && (facil_mask & X25_MASK_CALLING_AE)) {
  195. unsigned bytecount = (dte_facs->calling_len + 1) >> 1;
  196. *p++ = X25_FAC_CALLING_AE;
  197. *p++ = 1 + bytecount;
  198. *p++ = dte_facs->calling_len;
  199. memcpy(p, dte_facs->calling_ae, bytecount);
  200. p += bytecount;
  201. }
  202. if (dte_facs->called_len && (facil_mask & X25_MASK_CALLED_AE)) {
  203. unsigned bytecount = (dte_facs->called_len % 2) ?
  204. dte_facs->called_len / 2 + 1 :
  205. dte_facs->called_len / 2;
  206. *p++ = X25_FAC_CALLED_AE;
  207. *p++ = 1 + bytecount;
  208. *p++ = dte_facs->called_len;
  209. memcpy(p, dte_facs->called_ae, bytecount);
  210. p+=bytecount;
  211. }
  212. len = p - buffer;
  213. buffer[0] = len - 1;
  214. return len;
  215. }
  216. /*
  217. * Try to reach a compromise on a set of facilities.
  218. *
  219. * The only real problem is with reverse charging.
  220. */
  221. int x25_negotiate_facilities(struct sk_buff *skb, struct sock *sk,
  222. struct x25_facilities *new, struct x25_dte_facilities *dte)
  223. {
  224. struct x25_sock *x25 = x25_sk(sk);
  225. struct x25_facilities *ours = &x25->facilities;
  226. struct x25_facilities theirs;
  227. int len;
  228. memset(&theirs, 0, sizeof(theirs));
  229. memcpy(new, ours, sizeof(*new));
  230. len = x25_parse_facilities(skb, &theirs, dte, &x25->vc_facil_mask);
  231. if (len < 0)
  232. return len;
  233. /*
  234. * They want reverse charging, we won't accept it.
  235. */
  236. if ((theirs.reverse & 0x01 ) && (ours->reverse & 0x01)) {
  237. SOCK_DEBUG(sk, "X.25: rejecting reverse charging request\n");
  238. return -1;
  239. }
  240. new->reverse = theirs.reverse;
  241. if (theirs.throughput) {
  242. int theirs_in = theirs.throughput & 0x0f;
  243. int theirs_out = theirs.throughput & 0xf0;
  244. int ours_in = ours->throughput & 0x0f;
  245. int ours_out = ours->throughput & 0xf0;
  246. if (!ours_in || theirs_in < ours_in) {
  247. SOCK_DEBUG(sk, "X.25: inbound throughput negotiated\n");
  248. new->throughput = (new->throughput & 0xf0) | theirs_in;
  249. }
  250. if (!ours_out || theirs_out < ours_out) {
  251. SOCK_DEBUG(sk,
  252. "X.25: outbound throughput negotiated\n");
  253. new->throughput = (new->throughput & 0x0f) | theirs_out;
  254. }
  255. }
  256. if (theirs.pacsize_in && theirs.pacsize_out) {
  257. if (theirs.pacsize_in < ours->pacsize_in) {
  258. SOCK_DEBUG(sk, "X.25: packet size inwards negotiated down\n");
  259. new->pacsize_in = theirs.pacsize_in;
  260. }
  261. if (theirs.pacsize_out < ours->pacsize_out) {
  262. SOCK_DEBUG(sk, "X.25: packet size outwards negotiated down\n");
  263. new->pacsize_out = theirs.pacsize_out;
  264. }
  265. }
  266. if (theirs.winsize_in && theirs.winsize_out) {
  267. if (theirs.winsize_in < ours->winsize_in) {
  268. SOCK_DEBUG(sk, "X.25: window size inwards negotiated down\n");
  269. new->winsize_in = theirs.winsize_in;
  270. }
  271. if (theirs.winsize_out < ours->winsize_out) {
  272. SOCK_DEBUG(sk, "X.25: window size outwards negotiated down\n");
  273. new->winsize_out = theirs.winsize_out;
  274. }
  275. }
  276. return len;
  277. }
  278. /*
  279. * Limit values of certain facilities according to the capability of the
  280. * currently attached x25 link.
  281. */
  282. void x25_limit_facilities(struct x25_facilities *facilities,
  283. struct x25_neigh *nb)
  284. {
  285. if (!nb->extended) {
  286. if (facilities->winsize_in > 7) {
  287. printk(KERN_DEBUG "X.25: incoming winsize limited to 7\n");
  288. facilities->winsize_in = 7;
  289. }
  290. if (facilities->winsize_out > 7) {
  291. facilities->winsize_out = 7;
  292. printk( KERN_DEBUG "X.25: outgoing winsize limited to 7\n");
  293. }
  294. }
  295. }