x25_facilities.c 9.0 KB

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