x25_facilities.c 8.6 KB

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