x25_facilities.c 9.1 KB

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