x25_facilities.c 8.9 KB

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