x25_facilities.c 9.1 KB

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