Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / net / x25 / x25_dev.c
blobfbc781dcecaf2bce85a42afddda20e0fb2a56c30
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 Jonathan Naylor Started coding.
17 * 2000-09-04 Henner Eisen Prevent freeing a dangling skb.
20 #include <linux/config.h>
21 #if defined(CONFIG_X25) || defined(CONFIG_X25_MODULE)
22 #include <linux/errno.h>
23 #include <linux/types.h>
24 #include <linux/socket.h>
25 #include <linux/in.h>
26 #include <linux/kernel.h>
27 #include <linux/sched.h>
28 #include <linux/timer.h>
29 #include <linux/string.h>
30 #include <linux/sockios.h>
31 #include <linux/net.h>
32 #include <linux/stat.h>
33 #include <linux/inet.h>
34 #include <linux/netdevice.h>
35 #include <linux/skbuff.h>
36 #include <net/sock.h>
37 #include <asm/segment.h>
38 #include <asm/system.h>
39 #include <asm/uaccess.h>
40 #include <linux/fcntl.h>
41 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
42 #include <linux/mm.h>
43 #include <linux/interrupt.h>
44 #include <linux/notifier.h>
45 #include <linux/proc_fs.h>
46 #include <linux/if_arp.h>
47 #include <net/x25.h>
49 static int x25_receive_data(struct sk_buff *skb, struct x25_neigh *neigh)
51 struct sock *sk;
52 unsigned short frametype;
53 unsigned int lci;
55 frametype = skb->data[2];
56 lci = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF);
59 * LCI of zero is always for us, and its always a link control
60 * frame.
62 if (lci == 0) {
63 x25_link_control(skb, neigh, frametype);
64 return 0;
68 * Find an existing socket.
70 if ((sk = x25_find_socket(lci, neigh)) != NULL) {
71 int queued = 1;
73 skb->h.raw = skb->data;
74 bh_lock_sock(sk);
75 if (!sk->lock.users) {
76 queued = x25_process_rx_frame(sk, skb);
77 } else {
78 sk_add_backlog(sk, skb);
80 bh_unlock_sock(sk);
81 return queued;
85 * Is is a Call Request ? if so process it.
87 if (frametype == X25_CALL_REQUEST)
88 return x25_rx_call_request(skb, neigh, lci);
91 * Its not a Call Request, nor is it a control frame.
92 * Let caller throw it away.
95 x25_transmit_clear_request(neigh, lci, 0x0D);
97 printk(KERN_DEBUG "x25_receive_data(): unknown frame type %2x\n",frametype);
99 return 0;
102 int x25_lapb_receive_frame(struct sk_buff *skb, struct net_device *dev, struct packet_type *ptype)
104 struct x25_neigh *neigh;
105 int queued;
107 skb->sk = NULL;
110 * Packet received from unrecognised device, throw it away.
112 if ((neigh = x25_get_neigh(dev)) == NULL) {
113 printk(KERN_DEBUG "X.25: unknown neighbour - %s\n", dev->name);
114 kfree_skb(skb);
115 return 0;
118 switch (skb->data[0]) {
119 case 0x00:
120 skb_pull(skb, 1);
121 queued = x25_receive_data(skb, neigh);
122 if( ! queued )
123 /* We need to free the skb ourselves because
124 * net_bh() won't care about our return code.
126 kfree_skb(skb);
127 return 0;
129 case 0x01:
130 x25_link_established(neigh);
131 kfree_skb(skb);
132 return 0;
134 case 0x02:
135 x25_link_terminated(neigh);
136 kfree_skb(skb);
137 return 0;
139 case 0x03:
140 kfree_skb(skb);
141 return 0;
143 default:
144 kfree_skb(skb);
145 return 0;
149 int x25_llc_receive_frame(struct sk_buff *skb, struct net_device *dev, struct packet_type *ptype)
151 struct x25_neigh *neigh;
153 skb->sk = NULL;
156 * Packet received from unrecognised device, throw it away.
158 if ((neigh = x25_get_neigh(dev)) == NULL) {
159 printk(KERN_DEBUG "X.25: unknown_neighbour - %s\n", dev->name);
160 kfree_skb(skb);
161 return 0;
164 return x25_receive_data(skb, neigh);
167 void x25_establish_link(struct x25_neigh *neigh)
169 struct sk_buff *skb;
170 unsigned char *ptr;
172 switch (neigh->dev->type) {
173 case ARPHRD_X25:
174 if ((skb = alloc_skb(1, GFP_ATOMIC)) == NULL) {
175 printk(KERN_ERR "x25_dev: out of memory\n");
176 return;
178 ptr = skb_put(skb, 1);
179 *ptr = 0x01;
180 break;
182 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
183 case ARPHRD_ETHER:
184 return;
185 #endif
186 default:
187 return;
190 skb->protocol = htons(ETH_P_X25);
191 skb->dev = neigh->dev;
193 dev_queue_xmit(skb);
196 void x25_terminate_link(struct x25_neigh *neigh)
198 struct sk_buff *skb;
199 unsigned char *ptr;
201 switch (neigh->dev->type) {
202 case ARPHRD_X25:
203 if ((skb = alloc_skb(1, GFP_ATOMIC)) == NULL) {
204 printk(KERN_ERR "x25_dev: out of memory\n");
205 return;
207 ptr = skb_put(skb, 1);
208 *ptr = 0x02;
209 break;
211 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
212 case ARPHRD_ETHER:
213 return;
214 #endif
215 default:
216 return;
219 skb->protocol = htons(ETH_P_X25);
220 skb->dev = neigh->dev;
222 dev_queue_xmit(skb);
225 void x25_send_frame(struct sk_buff *skb, struct x25_neigh *neigh)
227 unsigned char *dptr;
229 skb->nh.raw = skb->data;
231 switch (neigh->dev->type) {
232 case ARPHRD_X25:
233 dptr = skb_push(skb, 1);
234 *dptr = 0x00;
235 break;
237 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
238 case ARPHRD_ETHER:
239 kfree_skb(skb);
240 return;
241 #endif
242 default:
243 kfree_skb(skb);
244 return;
247 skb->protocol = htons(ETH_P_X25);
248 skb->dev = neigh->dev;
250 dev_queue_xmit(skb);
253 #endif