summaryrefslogtreecommitdiffstats
path: root/drivers/isdn/isdn_x25iface.c
blob: 9a50748015c5ce13eabbc6eac8896703be96578f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* $Id: isdn_x25iface.c,v 1.7 1999/08/22 20:26:13 calle Exp $
 * stuff needed to support the Linux X.25 PLP code on top of devices that
 * can provide a lab_b service using the concap_proto mechanism.
 * This module supports a network interface wich provides lapb_sematics
 * -- as defined in ../../Documentation/networking/x25-iface.txt -- to
 * the upper layer and assumes that the lower layer provides a reliable
 * data link service by means of the concap_device_ops callbacks.
 *
 * Only protocol specific stuff goes here. Device specific stuff
 * goes to another -- device related -- concap_proto support source file.
 *
 * $Log: isdn_x25iface.c,v $
 * Revision 1.7  1999/08/22 20:26:13  calle
 * backported changes from kernel 2.3.14:
 * - several #include "config.h" gone, others come.
 * - "struct device" changed to "struct net_device" in 2.3.14, added a
 *   define in isdn_compat.h for older kernel versions.
 *
 * Revision 1.6  1999/01/27 22:53:19  he
 * minor updates (spellings, jiffies wrap around in isdn_tty)
 *
 * Revision 1.5  1998/10/30 17:55:39  he
 * dialmode for x25iface and multulink ppp
 *
 * Revision 1.4  1998/06/17 19:51:00  he
 * merged with 2.1.10[34] (cosmetics and udelay() -> mdelay())
 * brute force fix to avoid Ugh's in isdn_tty_write()
 * cleaned up some dead code
 *
 * Revision 1.3  1998/02/20 17:25:20  fritz
 * Changes for recent kernels.
 *
 * Revision 1.2  1998/01/31 22:49:22  keil
 * correct comments
 *
 * Revision 1.1  1998/01/31 22:27:58  keil
 * New files from Henner Eisen for X.25 support
 *
 */

/* #include <linux/isdn.h> */
#include <linux/netdevice.h>
#include <linux/concap.h>
#include <linux/wanrouter.h>
#include "isdn_x25iface.h"

/* for debugging messages not to cause an oops when device pointer is NULL*/
#define MY_DEVNAME(dev)  ( (dev) ? (dev)->name : "DEVICE UNSPECIFIED" )


typedef struct isdn_x25iface_proto_data {
	int magic;
	enum wan_states state;
	/* Private stuff, not to be accessed via proto_data. We provide the
	   other storage for the concap_proto instance here as well,
	   enabling us to allocate both with just one kmalloc(): */ 
	struct concap_proto priv;
} ix25_pdata_t;



/* is now in header file (extern): struct concap_proto * isdn_x25iface_proto_new(void); */
void isdn_x25iface_proto_del( struct concap_proto * );
int isdn_x25iface_proto_close( struct concap_proto * );
int isdn_x25iface_proto_restart( struct concap_proto *,
				 struct net_device *,
				 struct concap_device_ops *);
int isdn_x25iface_xmit( struct concap_proto *, struct sk_buff * );
int isdn_x25iface_receive( struct concap_proto *, struct sk_buff * );
int isdn_x25iface_connect_ind( struct concap_proto * );
int isdn_x25iface_disconn_ind( struct concap_proto * );


static struct concap_proto_ops ix25_pops = {
	&isdn_x25iface_proto_new,
	&isdn_x25iface_proto_del,
	&isdn_x25iface_proto_restart,
	&isdn_x25iface_proto_close,
	&isdn_x25iface_xmit,
	&isdn_x25iface_receive,
	&isdn_x25iface_connect_ind,
	&isdn_x25iface_disconn_ind
};

/* error message helper function */
static void illegal_state_warn( unsigned state, unsigned char firstbyte) 
{
	printk( KERN_WARNING "isdn_x25iface: firstbyte %x illegal in"
		"current state %d\n",firstbyte, state );
}

/* check protocol data field for consistency */
static int pdata_is_bad( ix25_pdata_t * pda ){

	if( pda  &&  pda -> magic == ISDN_X25IFACE_MAGIC ) return 0;
	printk( KERN_WARNING
		"isdn_x25iface_xxx: illegal pointer to proto data\n" );
	return 1;
}

/* create a new x25 interface protocol instance
 */
struct concap_proto * isdn_x25iface_proto_new()
{
	ix25_pdata_t * tmp = kmalloc(sizeof(ix25_pdata_t),GFP_KERNEL);
	IX25DEBUG("isdn_x25iface_proto_new\n");
	if( tmp ){
		tmp -> magic = ISDN_X25IFACE_MAGIC;
		tmp -> state = WAN_UNCONFIGURED;
		/* private data space used to hold the concap_proto data.
		   Only to be accessed via the returned pointer */
		tmp -> priv.dops       = NULL;
		tmp -> priv.net_dev    = NULL;
		tmp -> priv.pops       = &ix25_pops;
		tmp -> priv.flags      = 0;
		tmp -> priv.proto_data = tmp;
		return( &(tmp -> priv) );
	}
	return NULL;
};

/* close the x25iface encapsulation protocol 
 */
int isdn_x25iface_proto_close(struct concap_proto *cprot){

	ix25_pdata_t *tmp;
        int ret = 0;
	ulong flags;

	if( ! cprot ){
		printk( KERN_ERR "isdn_x25iface_proto_close: "
			"invalid concap_proto pointer\n" );
		return -1;
	}
	IX25DEBUG( "isdn_x25iface_proto_close %s \n", MY_DEVNAME(cprot -> net_dev) );
	save_flags(flags);
	cli();  /* avoid races with incoming events calling pops methods while
		 cprot members are inconsistent */  
	cprot -> dops    = NULL;
	cprot -> net_dev = NULL;
	tmp = cprot -> proto_data;
	if( pdata_is_bad( tmp ) ){
		ret = -1;
	} else {
		tmp -> state = WAN_UNCONFIGURED;
	}
	restore_flags(flags);

	return ret;
}

/* Delete the x25iface encapsulation protocol instance
 */
void isdn_x25iface_proto_del(struct concap_proto *cprot){

	ix25_pdata_t * tmp;
 
	IX25DEBUG( "isdn_x25iface_proto_del \n" );
	if( ! cprot ){
		printk( KERN_ERR "isdn_x25iface_proto_del: "
			"concap_proto pointer is NULL\n" );
		return;
	}
	tmp = cprot -> proto_data;
	if( tmp == NULL ){ 
		printk( KERN_ERR "isdn_x25iface_proto_del: inconsistent "
			"proto_data pointer (maybe already deleted?)\n"); 
		return;
	}
	/* close if the protocol is still open */
	if( cprot -> dops ) isdn_x25iface_proto_close(cprot);
	/* freeing the storage should be sufficient now. But some additional
	   settings might help to catch wild pointer bugs */
	tmp -> magic = 0;
	cprot -> proto_data = NULL;

	kfree( tmp );
	return;
}

/* (re-)initialize the data structures for x25iface encapsulation
 */
int isdn_x25iface_proto_restart(struct concap_proto *cprot,
				struct net_device *ndev, 
				struct concap_device_ops *dops)
{
	ix25_pdata_t * pda = cprot -> proto_data ;
	ulong flags;

	IX25DEBUG( "isdn_x25iface_proto_restart %s \n", MY_DEVNAME(ndev) );

	if ( pdata_is_bad( pda ) ) return -1;

	if( !( dops  && dops -> data_req && dops -> connect_req 
	       && dops -> disconn_req )  ){
		printk( KERN_WARNING "isdn_x25iface_restart: required dops"
			" missing\n" );
		isdn_x25iface_proto_close(cprot);
		return -1;
	}
	save_flags(flags);
	cli();  /* avoid races with incoming events calling pops methods while
		 cprot members are inconsistent */  
	cprot -> net_dev = ndev;
	cprot -> pops = &ix25_pops;
	cprot -> dops = dops;
	pda -> state = WAN_DISCONNECTED;
	restore_flags(flags);
	return 0;
}

/* deliver a dl_data frame received from i4l HL driver to the network layer 
 */
int isdn_x25iface_receive(struct concap_proto *cprot, struct sk_buff *skb)
{
  	IX25DEBUG( "isdn_x25iface_receive %s \n", MY_DEVNAME(cprot->net_dev) );
	if ( ( (ix25_pdata_t*) (cprot->proto_data) ) 
	     -> state == WAN_CONNECTED ){
		skb -> dev = cprot -> net_dev;
		skb -> protocol = htons(ETH_P_X25);
		skb -> pkt_type = PACKET_HOST;
		if( skb_push(skb, 1)){
			skb -> data[0]=0x00;
			skb -> mac.raw = skb -> data;
			netif_rx(skb);
			return 0;
		}
	}
	printk(KERN_WARNING "isdn_x25iface_receive %s: not connected, skb dropped\n", MY_DEVNAME(cprot->net_dev) );
	dev_kfree_skb(skb);
	return -1;
}

/* a connection set up is indicated by lower layer 
 */
int isdn_x25iface_connect_ind(struct concap_proto *cprot)
{
	struct sk_buff * skb = dev_alloc_skb(1);
	enum wan_states *state_p 
	  = &( ( (ix25_pdata_t*) (cprot->proto_data) ) -> state);
	IX25DEBUG( "isdn_x25iface_connect_ind %s \n"
		   , MY_DEVNAME(cprot->net_dev) );
	if( *state_p == WAN_UNCONFIGURED ){ 
		printk(KERN_WARNING 
		       "isdn_x25iface_connect_ind while unconfigured %s\n"
		       , MY_DEVNAME(cprot->net_dev) );
		return -1;
	}
	*state_p = WAN_CONNECTED;
	if( skb ){
		*( skb_put(skb, 1) ) = 0x01;
		skb -> mac.raw = skb -> data;
		skb -> dev  = cprot -> net_dev;
		skb -> protocol = htons(ETH_P_X25);
		skb -> pkt_type = PACKET_HOST;
		netif_rx(skb);
		return 0;
	} else {
		printk(KERN_WARNING "isdn_x25iface_connect_ind: "
		       " out of memory -- disconnecting\n");
		cprot -> dops -> disconn_req(cprot);
		return -1;
	}
}
	
/* a disconnect is indicated by lower layer 
 */
int isdn_x25iface_disconn_ind(struct concap_proto *cprot)
{
	struct sk_buff *skb;
	enum wan_states *state_p 
	  = &( ( (ix25_pdata_t*) (cprot->proto_data) ) -> state);
	IX25DEBUG( "isdn_x25iface_disconn_ind %s \n", MY_DEVNAME(cprot -> net_dev) );
	if( *state_p == WAN_UNCONFIGURED ){ 
		printk(KERN_WARNING 
		       "isdn_x25iface_disconn_ind while unconfigured\n");
		return -1;
	}
	if(! cprot -> net_dev) return -1;
	*state_p = WAN_DISCONNECTED;
	skb = dev_alloc_skb(1);
	if( skb ){
		*( skb_put(skb, 1) ) = 0x02;
		skb -> mac.raw = skb -> data;
		skb -> dev  = cprot -> net_dev;
		skb -> protocol = htons(ETH_P_X25);
		skb -> pkt_type = PACKET_HOST;
		netif_rx(skb);
		return 0;
	} else {
		printk(KERN_WARNING "isdn_x25iface_disconn_ind:"
		       " out of memory\n");
		return -1;
	}
}

/* process a frame handed over to us from linux network layer. First byte
   semantics as defined in ../../Documentation/networking/x25-iface.txt 
   */
int isdn_x25iface_xmit(struct concap_proto *cprot, struct sk_buff *skb)
{
	unsigned char firstbyte = skb->data[0];
	unsigned *state = 
		&( ( (ix25_pdata_t*) (cprot -> proto_data) ) -> state  );
	int ret = 0;
	IX25DEBUG( "isdn_x25iface_xmit: %s first=%x state=%d \n", MY_DEVNAME(cprot -> net_dev), firstbyte, *state );
	switch ( firstbyte ){
	case 0x00: /* dl_data request */
		if( *state == WAN_CONNECTED ){
			skb_pull(skb, 1);
			cprot -> net_dev -> trans_start = jiffies;
			ret = ( cprot -> dops -> data_req(cprot, skb) );
			/* prepare for future retransmissions */
			if( ret ) skb_push(skb,1);
			return ret;
		}
		illegal_state_warn( *state, firstbyte ); 
		break;
	case 0x01: /* dl_connect request */
		if( *state == WAN_DISCONNECTED ){
			*state = WAN_CONNECTING;
		        ret = cprot -> dops -> connect_req(cprot);
			if(ret){
				/* reset state and notify upper layer about
				 * immidiatly failed attempts */
				isdn_x25iface_disconn_ind(cprot);
			}
		} else {
			illegal_state_warn( *state, firstbyte );
		}
		break;
	case 0x02: /* dl_disconnect request */
		switch ( *state ){
		case WAN_DISCONNECTED: 
			/* Should not happen. However, give upper layer a
			   chance to recover from inconstistency  but don't
			   trust the lower layer sending the disconn_confirm
			   when already disconnected */
			printk(KERN_WARNING "isdn_x25iface_xmit: disconnect "
			       " requested while disconnected\n" );
			isdn_x25iface_disconn_ind(cprot);
			break; /* prevent infinite loops */
		case WAN_CONNECTING:
		case WAN_CONNECTED:
			*state = WAN_DISCONNECTED;
			cprot -> dops -> disconn_req(cprot);
			break;
		default:
			illegal_state_warn( *state, firstbyte );
		}
		break;
	case 0x03: /* changing lapb parameters requested */
		printk(KERN_WARNING "isdn_x25iface_xmit: setting of lapb"
		       " options not yet supported\n");
		break;
	default:
		printk(KERN_WARNING "isdn_x25iface_xmit: frame with illegal"
		       " first byte %x ignored:\n", firstbyte);
	}
	dev_kfree_skb(skb);
	return 0;
}