x25_facilities.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. */
  21. #include <linux/kernel.h>
  22. #include <linux/string.h>
  23. #include <linux/skbuff.h>
  24. #include <net/sock.h>
  25. #include <net/x25.h>
  26. /*
  27. * Parse a set of facilities into the facilities structure. Unrecognised
  28. * facilities are written to the debug log file.
  29. */
  30. int x25_parse_facilities(struct sk_buff *skb,
  31. struct x25_facilities *facilities,
  32. unsigned long *vc_fac_mask)
  33. {
  34. unsigned char *p = skb->data;
  35. unsigned int len = *p++;
  36. *vc_fac_mask = 0;
  37. while (len > 0) {
  38. switch (*p & X25_FAC_CLASS_MASK) {
  39. case X25_FAC_CLASS_A:
  40. switch (*p) {
  41. case X25_FAC_REVERSE:
  42. facilities->reverse = p[1] & 0x01;
  43. *vc_fac_mask |= X25_MASK_REVERSE;
  44. break;
  45. case X25_FAC_THROUGHPUT:
  46. facilities->throughput = p[1];
  47. *vc_fac_mask |= X25_MASK_THROUGHPUT;
  48. break;
  49. default:
  50. printk(KERN_DEBUG "X.25: unknown facility "
  51. "%02X, value %02X\n",
  52. p[0], p[1]);
  53. break;
  54. }
  55. p += 2;
  56. len -= 2;
  57. break;
  58. case X25_FAC_CLASS_B:
  59. switch (*p) {
  60. case X25_FAC_PACKET_SIZE:
  61. facilities->pacsize_in = p[1];
  62. facilities->pacsize_out = p[2];
  63. *vc_fac_mask |= X25_MASK_PACKET_SIZE;
  64. break;
  65. case X25_FAC_WINDOW_SIZE:
  66. facilities->winsize_in = p[1];
  67. facilities->winsize_out = p[2];
  68. *vc_fac_mask |= X25_MASK_WINDOW_SIZE;
  69. break;
  70. default:
  71. printk(KERN_DEBUG "X.25: unknown facility "
  72. "%02X, values %02X, %02X\n",
  73. p[0], p[1], p[2]);
  74. break;
  75. }
  76. p += 3;
  77. len -= 3;
  78. break;
  79. case X25_FAC_CLASS_C:
  80. printk(KERN_DEBUG "X.25: unknown facility %02X, "
  81. "values %02X, %02X, %02X\n",
  82. p[0], p[1], p[2], p[3]);
  83. p += 4;
  84. len -= 4;
  85. break;
  86. case X25_FAC_CLASS_D:
  87. printk(KERN_DEBUG "X.25: unknown facility %02X, "
  88. "length %d, values %02X, %02X, %02X, %02X\n",
  89. p[0], p[1], p[2], p[3], p[4], p[5]);
  90. len -= p[1] + 2;
  91. p += p[1] + 2;
  92. break;
  93. }
  94. }
  95. return p - skb->data;
  96. }
  97. /*
  98. * Create a set of facilities.
  99. */
  100. int x25_create_facilities(unsigned char *buffer,
  101. struct x25_facilities *facilities,
  102. unsigned long facil_mask)
  103. {
  104. unsigned char *p = buffer + 1;
  105. int len;
  106. if (!facil_mask) {
  107. /*
  108. * Length of the facilities field in call_req or
  109. * call_accept packets
  110. */
  111. buffer[0] = 0;
  112. len = 1; /* 1 byte for the length field */
  113. return len;
  114. }
  115. if (facilities->reverse && (facil_mask & X25_MASK_REVERSE)) {
  116. *p++ = X25_FAC_REVERSE;
  117. *p++ = !!facilities->reverse;
  118. }
  119. if (facilities->throughput && (facil_mask & X25_MASK_THROUGHPUT)) {
  120. *p++ = X25_FAC_THROUGHPUT;
  121. *p++ = facilities->throughput;
  122. }
  123. if ((facilities->pacsize_in || facilities->pacsize_out) &&
  124. (facil_mask & X25_MASK_PACKET_SIZE)) {
  125. *p++ = X25_FAC_PACKET_SIZE;
  126. *p++ = facilities->pacsize_in ? : facilities->pacsize_out;
  127. *p++ = facilities->pacsize_out ? : facilities->pacsize_in;
  128. }
  129. if ((facilities->winsize_in || facilities->winsize_out) &&
  130. (facil_mask & X25_MASK_WINDOW_SIZE)) {
  131. *p++ = X25_FAC_WINDOW_SIZE;
  132. *p++ = facilities->winsize_in ? : facilities->winsize_out;
  133. *p++ = facilities->winsize_out ? : facilities->winsize_in;
  134. }
  135. len = p - buffer;
  136. buffer[0] = len - 1;
  137. return len;
  138. }
  139. /*
  140. * Try to reach a compromise on a set of facilities.
  141. *
  142. * The only real problem is with reverse charging.
  143. */
  144. int x25_negotiate_facilities(struct sk_buff *skb, struct sock *sk,
  145. struct x25_facilities *new)
  146. {
  147. struct x25_sock *x25 = x25_sk(sk);
  148. struct x25_facilities *ours = &x25->facilities;
  149. struct x25_facilities theirs;
  150. int len;
  151. memset(&theirs, 0, sizeof(theirs));
  152. memcpy(new, ours, sizeof(*new));
  153. len = x25_parse_facilities(skb, &theirs, &x25->vc_facil_mask);
  154. /*
  155. * They want reverse charging, we won't accept it.
  156. */
  157. if (theirs.reverse && ours->reverse) {
  158. SOCK_DEBUG(sk, "X.25: rejecting reverse charging request");
  159. return -1;
  160. }
  161. new->reverse = theirs.reverse;
  162. if (theirs.throughput) {
  163. if (theirs.throughput < ours->throughput) {
  164. SOCK_DEBUG(sk, "X.25: throughput negotiated down");
  165. new->throughput = theirs.throughput;
  166. }
  167. }
  168. if (theirs.pacsize_in && theirs.pacsize_out) {
  169. if (theirs.pacsize_in < ours->pacsize_in) {
  170. SOCK_DEBUG(sk, "X.25: packet size inwards negotiated down");
  171. new->pacsize_in = theirs.pacsize_in;
  172. }
  173. if (theirs.pacsize_out < ours->pacsize_out) {
  174. SOCK_DEBUG(sk, "X.25: packet size outwards negotiated down");
  175. new->pacsize_out = theirs.pacsize_out;
  176. }
  177. }
  178. if (theirs.winsize_in && theirs.winsize_out) {
  179. if (theirs.winsize_in < ours->winsize_in) {
  180. SOCK_DEBUG(sk, "X.25: window size inwards negotiated down");
  181. new->winsize_in = theirs.winsize_in;
  182. }
  183. if (theirs.winsize_out < ours->winsize_out) {
  184. SOCK_DEBUG(sk, "X.25: window size outwards negotiated down");
  185. new->winsize_out = theirs.winsize_out;
  186. }
  187. }
  188. return len;
  189. }
  190. /*
  191. * Limit values of certain facilities according to the capability of the
  192. * currently attached x25 link.
  193. */
  194. void x25_limit_facilities(struct x25_facilities *facilities,
  195. struct x25_neigh *nb)
  196. {
  197. if (!nb->extended) {
  198. if (facilities->winsize_in > 7) {
  199. printk(KERN_DEBUG "X.25: incoming winsize limited to 7\n");
  200. facilities->winsize_in = 7;
  201. }
  202. if (facilities->winsize_out > 7) {
  203. facilities->winsize_out = 7;
  204. printk( KERN_DEBUG "X.25: outgoing winsize limited to 7\n");
  205. }
  206. }
  207. }