* better
[mascara-docs.git] / i386 / linux-2.3.21 / net / x25 / x25_facilities.c
blob18de2da1a9c0ee3846b614e85271de08ed2b847e
1 /*
2 * X.25 Packet Layer release 002
4 * This is ALPHA test software. This code may break your machine, randomly fail to work with new
5 * releases, misbehave and/or generally screw up. It might even work.
7 * This code REQUIRES 2.1.15 or higher
9 * This module:
10 * This module is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
15 * History
16 * X.25 001 Split from x25_subr.c
19 #include <linux/config.h>
20 #if defined(CONFIG_X25) || defined(CONFIG_X25_MODULE)
21 #include <linux/errno.h>
22 #include <linux/types.h>
23 #include <linux/socket.h>
24 #include <linux/in.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/timer.h>
28 #include <linux/string.h>
29 #include <linux/sockios.h>
30 #include <linux/net.h>
31 #include <linux/inet.h>
32 #include <linux/netdevice.h>
33 #include <linux/skbuff.h>
34 #include <net/sock.h>
35 #include <asm/segment.h>
36 #include <asm/system.h>
37 #include <linux/fcntl.h>
38 #include <linux/mm.h>
39 #include <linux/interrupt.h>
40 #include <net/x25.h>
43 * Parse a set of facilities into the facilities structure. Unrecognised
44 * facilities are written to the debug log file.
46 int x25_parse_facilities(struct sk_buff *skb, struct x25_facilities *facilities)
48 unsigned int len;
49 unsigned char *p = skb->data;
51 len = *p++;
53 while (len > 0) {
54 switch (*p & X25_FAC_CLASS_MASK) {
55 case X25_FAC_CLASS_A:
56 switch (*p) {
57 case X25_FAC_REVERSE:
58 facilities->reverse = (p[1] & 0x01);
59 break;
60 case X25_FAC_THROUGHPUT:
61 facilities->throughput = p[1];
62 break;
63 default:
64 printk(KERN_DEBUG "X.25: unknown facility %02X, value %02X\n", p[0], p[1]);
65 break;
67 p += 2;
68 len -= 2;
69 break;
71 case X25_FAC_CLASS_B:
72 switch (*p) {
73 case X25_FAC_PACKET_SIZE:
74 facilities->pacsize_in = p[1];
75 facilities->pacsize_out = p[2];
76 break;
77 case X25_FAC_WINDOW_SIZE:
78 facilities->winsize_in = p[1];
79 facilities->winsize_out = p[2];
80 break;
81 default:
82 printk(KERN_DEBUG "X.25: unknown facility %02X, values %02X, %02X\n", p[0], p[1], p[2]);
83 break;
85 p += 3;
86 len -= 3;
87 break;
89 case X25_FAC_CLASS_C:
90 printk(KERN_DEBUG "X.25: unknown facility %02X, values %02X, %02X, %02X\n", p[0], p[1], p[2], p[3]);
91 p += 4;
92 len -= 4;
93 break;
95 case X25_FAC_CLASS_D:
96 printk(KERN_DEBUG "X.25: unknown facility %02X, length %d, values %02X, %02X, %02X, %02X\n", p[0], p[1], p[2], p[3], p[4], p[5]);
97 p += p[1] + 2;
98 len -= p[1] + 2;
99 break;
103 return p - skb->data;
107 * Create a set of facilities.
109 int x25_create_facilities(unsigned char *buffer, struct x25_facilities *facilities)
111 unsigned char *p = buffer + 1;
112 int len;
114 if (facilities->reverse != 0) {
115 *p++ = X25_FAC_REVERSE;
116 *p++ = (facilities->reverse) ? 0x01 : 0x00;
119 if (facilities->throughput != 0) {
120 *p++ = X25_FAC_THROUGHPUT;
121 *p++ = facilities->throughput;
124 if (facilities->pacsize_in != 0 || facilities->pacsize_out != 0) {
125 *p++ = X25_FAC_PACKET_SIZE;
126 *p++ = (facilities->pacsize_in == 0) ? facilities->pacsize_out : facilities->pacsize_in;
127 *p++ = (facilities->pacsize_out == 0) ? facilities->pacsize_in : facilities->pacsize_out;
130 if (facilities->winsize_in != 0 || facilities->winsize_out != 0) {
131 *p++ = X25_FAC_WINDOW_SIZE;
132 *p++ = (facilities->winsize_in == 0) ? facilities->winsize_out : facilities->winsize_in;
133 *p++ = (facilities->winsize_out == 0) ? facilities->winsize_in : facilities->winsize_out;
136 len = p - buffer;
137 buffer[0] = len - 1;
139 return len;
143 * Try to reach a compromise on a set of facilities.
145 * The only real problem is with reverse charging.
147 int x25_negotiate_facilities(struct sk_buff *skb, struct sock *sk, struct x25_facilities *new)
149 struct x25_facilities *ours;
150 struct x25_facilities theirs;
151 int len;
153 memset(&theirs, 0x00, sizeof(struct x25_facilities));
155 ours = &sk->protinfo.x25->facilities;
157 *new = *ours;
159 len = x25_parse_facilities(skb, &theirs);
162 * They want reverse charging, we won't accept it.
164 if (theirs.reverse != 0 && ours->reverse == 0) {
165 SOCK_DEBUG(sk, "X.25: rejecting reverse charging request");
166 return -1;
169 new->reverse = theirs.reverse;
171 if (theirs.throughput != 0) {
172 if (theirs.throughput < ours->throughput) {
173 SOCK_DEBUG(sk, "X.25: throughput negotiated down");
174 new->throughput = theirs.throughput;
178 if (theirs.pacsize_in != 0 && theirs.pacsize_out != 0) {
179 if (theirs.pacsize_in < ours->pacsize_in) {
180 SOCK_DEBUG(sk, "X.25: packet size inwards negotiated down");
181 new->pacsize_in = theirs.pacsize_in;
183 if (theirs.pacsize_out < ours->pacsize_out) {
184 SOCK_DEBUG(sk, "X.25: packet size outwards negotiated down");
185 new->pacsize_out = theirs.pacsize_out;
189 if (theirs.winsize_in != 0 && theirs.winsize_out != 0) {
190 if (theirs.winsize_in < ours->winsize_in) {
191 SOCK_DEBUG(sk, "X.25: window size inwards negotiated down");
192 new->winsize_in = theirs.winsize_in;
194 if (theirs.winsize_out < ours->winsize_out) {
195 SOCK_DEBUG(sk, "X.25: window size outwards negotiated down");
196 new->winsize_out = theirs.winsize_out;
200 return len;
204 * Limit values of certain facilities according to the capability of the
205 * currently attached x25 link.
207 void x25_limit_facilities(struct x25_facilities *facilities,
208 struct x25_neigh *neighbour)
211 if( ! neighbour->extended ){
212 if( facilities->winsize_in > 7 ){
213 printk(KERN_DEBUG "X.25: incoming winsize limited to 7\n");
214 facilities->winsize_in = 7;
216 if( facilities->winsize_out > 7 ){
217 facilities->winsize_out = 7;
218 printk( KERN_DEBUG "X.25: outgoing winsize limited to 7\n");
223 #endif