This commit was generated by cvs2svn to compensate for changes in r517,
[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
96         if (frametype != X25_CLEAR_CONFIRMATION)
97                 printk(KERN_DEBUG "x25_receive_data(): unknown frame type %2x\n",frametype);
98
99         return 0;
100 }
101
102 int x25_lapb_receive_frame(struct sk_buff *skb, struct net_device *dev,
103                            struct packet_type *ptype)
104 {
105         struct sk_buff *nskb;
106         struct x25_neigh *nb;
107
108         nskb = skb_copy(skb, GFP_ATOMIC);
109         if (!nskb)
110                 goto drop;
111         kfree_skb(skb);
112         skb = nskb;
113
114         /*
115          * Packet received from unrecognised device, throw it away.
116          */
117         nb = x25_get_neigh(dev);
118         if (!nb) {
119                 printk(KERN_DEBUG "X.25: unknown neighbour - %s\n", dev->name);
120                 goto drop;
121         }
122
123         switch (skb->data[0]) {
124                 case 0x00:
125                         skb_pull(skb, 1);
126                         if (x25_receive_data(skb, nb)) {
127                                 x25_neigh_put(nb);
128                                 goto out;
129                         }
130                         break;
131                 case 0x01:
132                         x25_link_established(nb);
133                         break;
134                 case 0x02:
135                         x25_link_terminated(nb);
136                         break;
137         }
138         x25_neigh_put(nb);
139 drop:
140         kfree_skb(skb);
141 out:
142         return 0;
143 }
144
145 int x25_llc_receive_frame(struct sk_buff *skb, struct net_device *dev,
146                           struct packet_type *ptype)
147 {
148         struct x25_neigh *nb;
149         int rc = 0;
150
151         skb->sk = NULL;
152
153         /*
154          * Packet received from unrecognised device, throw it away.
155          */
156         nb = x25_get_neigh(dev);
157         if (!nb) {
158                 printk(KERN_DEBUG "X.25: unknown_neighbour - %s\n", dev->name);
159                 kfree_skb(skb);
160         } else {
161                 rc = x25_receive_data(skb, nb);
162                 x25_neigh_put(nb);
163         }
164
165         return rc;
166 }
167
168 void x25_establish_link(struct x25_neigh *nb)
169 {
170         struct sk_buff *skb;
171         unsigned char *ptr;
172
173         switch (nb->dev->type) {
174                 case ARPHRD_X25:
175                         if ((skb = alloc_skb(1, GFP_ATOMIC)) == NULL) {
176                                 printk(KERN_ERR "x25_dev: out of memory\n");
177                                 return;
178                         }
179                         ptr  = skb_put(skb, 1);
180                         *ptr = 0x01;
181                         break;
182
183 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
184                 case ARPHRD_ETHER:
185                         return;
186 #endif
187                 default:
188                         return;
189         }
190
191         skb->protocol = htons(ETH_P_X25);
192         skb->dev      = nb->dev;
193
194         dev_queue_xmit(skb);
195 }
196
197 void x25_terminate_link(struct x25_neigh *nb)
198 {
199         struct sk_buff *skb;
200         unsigned char *ptr;
201
202 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
203         if (nb->dev->type == ARPHRD_ETHER)
204                 return;
205 #endif
206         if (nb->dev->type != ARPHRD_X25)
207                 return;
208
209         skb = alloc_skb(1, GFP_ATOMIC);
210         if (!skb) {
211                 printk(KERN_ERR "x25_dev: out of memory\n");
212                 return;
213         }
214
215         ptr  = skb_put(skb, 1);
216         *ptr = 0x02;
217
218         skb->protocol = htons(ETH_P_X25);
219         skb->dev      = nb->dev;
220         dev_queue_xmit(skb);
221 }
222
223 void x25_send_frame(struct sk_buff *skb, struct x25_neigh *nb)
224 {
225         unsigned char *dptr;
226
227         skb->nh.raw = skb->data;
228
229         switch (nb->dev->type) {
230                 case ARPHRD_X25:
231                         dptr  = skb_push(skb, 1);
232                         *dptr = 0x00;
233                         break;
234
235 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
236                 case ARPHRD_ETHER:
237                         kfree_skb(skb);
238                         return;
239 #endif
240                 default:
241                         kfree_skb(skb);
242                         return;
243         }
244
245         skb->protocol = htons(ETH_P_X25);
246         skb->dev      = nb->dev;
247
248         dev_queue_xmit(skb);
249 }