ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / x25 / x25_dev.c
1 /*
2  *      X.25 Packet Layer release 002
3  *
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. 
6  *
7  *      This code REQUIRES 2.1.15 or higher
8  *
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.
14  *
15  *      History
16  *      X.25 001        Jonathan Naylor Started coding.
17  *      2000-09-04      Henner Eisen    Prevent freeing a dangling skb.
18  */
19
20 #include <linux/config.h>
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/stat.h>
32 #include <linux/inet.h>
33 #include <linux/netdevice.h>
34 #include <linux/skbuff.h>
35 #include <net/sock.h>
36 #include <asm/system.h>
37 #include <asm/uaccess.h>
38 #include <linux/fcntl.h>
39 #include <linux/termios.h>      /* For TIOCINQ/OUTQ */
40 #include <linux/mm.h>
41 #include <linux/interrupt.h>
42 #include <linux/notifier.h>
43 #include <linux/proc_fs.h>
44 #include <linux/if_arp.h>
45 #include <net/x25.h>
46
47 static int x25_receive_data(struct sk_buff *skb, struct x25_neigh *nb)
48 {
49         struct sock *sk;
50         unsigned short frametype;
51         unsigned int lci;
52
53         frametype = skb->data[2];
54         lci = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF);
55
56         /*
57          *      LCI of zero is always for us, and its always a link control
58          *      frame.
59          */
60         if (lci == 0) {
61                 x25_link_control(skb, nb, frametype);
62                 return 0;
63         }
64
65         /*
66          *      Find an existing socket.
67          */
68         if ((sk = x25_find_socket(lci, nb)) != NULL) {
69                 int queued = 1;
70
71                 skb->h.raw = skb->data;
72                 bh_lock_sock(sk);
73                 if (!sock_owned_by_user(sk)) {
74                         queued = x25_process_rx_frame(sk, skb);
75                 } else {
76                         sk_add_backlog(sk, skb);
77                 }
78                 bh_unlock_sock(sk);
79                 return queued;
80         }
81
82         /*
83          *      Is is a Call Request ? if so process it.
84          */
85         if (frametype == X25_CALL_REQUEST)
86                 return x25_rx_call_request(skb, nb, lci);
87
88         /*
89          *      Its not a Call Request, nor is it a control frame.
90          *      Let caller throw it away.
91          */
92 /*
93         x25_transmit_clear_request(nb, lci, 0x0D);
94 */
95         printk(KERN_DEBUG "x25_receive_data(): unknown frame type %2x\n",frametype);
96
97         return 0;
98 }
99
100 int x25_lapb_receive_frame(struct sk_buff *skb, struct net_device *dev,
101                            struct packet_type *ptype)
102 {
103         struct sk_buff *nskb;
104         struct x25_neigh *nb;
105
106         nskb = skb_copy(skb, GFP_ATOMIC);
107         if (!nskb)
108                 goto drop;
109         kfree_skb(skb);
110         skb = nskb;
111
112         /*
113          * Packet received from unrecognised device, throw it away.
114          */
115         nb = x25_get_neigh(dev);
116         if (!nb) {
117                 printk(KERN_DEBUG "X.25: unknown neighbour - %s\n", dev->name);
118                 goto drop;
119         }
120
121         switch (skb->data[0]) {
122                 case 0x00:
123                         skb_pull(skb, 1);
124                         if (x25_receive_data(skb, nb)) {
125                                 x25_neigh_put(nb);
126                                 goto out;
127                         }
128                         break;
129                 case 0x01:
130                         x25_link_established(nb);
131                         break;
132                 case 0x02:
133                         x25_link_terminated(nb);
134                         break;
135         }
136         x25_neigh_put(nb);
137 drop:
138         kfree_skb(skb);
139 out:
140         return 0;
141 }
142
143 int x25_llc_receive_frame(struct sk_buff *skb, struct net_device *dev,
144                           struct packet_type *ptype)
145 {
146         struct x25_neigh *nb;
147         int rc = 0;
148
149         skb->sk = NULL;
150
151         /*
152          * Packet received from unrecognised device, throw it away.
153          */
154         nb = x25_get_neigh(dev);
155         if (!nb) {
156                 printk(KERN_DEBUG "X.25: unknown_neighbour - %s\n", dev->name);
157                 kfree_skb(skb);
158         } else {
159                 rc = x25_receive_data(skb, nb);
160                 x25_neigh_put(nb);
161         }
162
163         return rc;
164 }
165
166 void x25_establish_link(struct x25_neigh *nb)
167 {
168         struct sk_buff *skb;
169         unsigned char *ptr;
170
171         switch (nb->dev->type) {
172                 case ARPHRD_X25:
173                         if ((skb = alloc_skb(1, GFP_ATOMIC)) == NULL) {
174                                 printk(KERN_ERR "x25_dev: out of memory\n");
175                                 return;
176                         }
177                         ptr  = skb_put(skb, 1);
178                         *ptr = 0x01;
179                         break;
180
181 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
182                 case ARPHRD_ETHER:
183                         return;
184 #endif
185                 default:
186                         return;
187         }
188
189         skb->protocol = htons(ETH_P_X25);
190         skb->dev      = nb->dev;
191
192         dev_queue_xmit(skb);
193 }
194
195 void x25_terminate_link(struct x25_neigh *nb)
196 {
197         struct sk_buff *skb;
198         unsigned char *ptr;
199
200 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
201         if (nb->dev->type == ARPHRD_ETHER)
202                 return;
203 #endif
204         if (nb->dev->type != ARPHRD_X25)
205                 return;
206
207         skb = alloc_skb(1, GFP_ATOMIC);
208         if (!skb) {
209                 printk(KERN_ERR "x25_dev: out of memory\n");
210                 return;
211         }
212
213         ptr  = skb_put(skb, 1);
214         *ptr = 0x02;
215
216         skb->protocol = htons(ETH_P_X25);
217         skb->dev      = nb->dev;
218         dev_queue_xmit(skb);
219 }
220
221 void x25_send_frame(struct sk_buff *skb, struct x25_neigh *nb)
222 {
223         unsigned char *dptr;
224
225         skb->nh.raw = skb->data;
226
227         switch (nb->dev->type) {
228                 case ARPHRD_X25:
229                         dptr  = skb_push(skb, 1);
230                         *dptr = 0x00;
231                         break;
232
233 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
234                 case ARPHRD_ETHER:
235                         kfree_skb(skb);
236                         return;
237 #endif
238                 default:
239                         kfree_skb(skb);
240                         return;
241         }
242
243         skb->protocol = htons(ETH_P_X25);
244         skb->dev      = nb->dev;
245
246         dev_queue_xmit(skb);
247 }