ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / ax25 / ax25_in.c
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * Copyright (C) Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
8  * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
9  * Copyright (C) Joerg Reuter DL1BKE (jreuter@yaina.de)
10  * Copyright (C) Hans-Joachim Hetscher DD8NE (dd8ne@bnv-bamberg.de)
11  */
12 #include <linux/config.h>
13 #include <linux/errno.h>
14 #include <linux/types.h>
15 #include <linux/socket.h>
16 #include <linux/in.h>
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
19 #include <linux/timer.h>
20 #include <linux/string.h>
21 #include <linux/sockios.h>
22 #include <linux/net.h>
23 #include <net/ax25.h>
24 #include <linux/inet.h>
25 #include <linux/netdevice.h>
26 #include <linux/skbuff.h>
27 #include <linux/netfilter.h>
28 #include <net/sock.h>
29 #include <net/ip.h>                     /* For ip_rcv */
30 #include <net/tcp.h>
31 #include <net/arp.h>                    /* For arp_rcv */
32 #include <asm/uaccess.h>
33 #include <asm/system.h>
34 #include <linux/fcntl.h>
35 #include <linux/mm.h>
36 #include <linux/interrupt.h>
37
38 /*
39  *      Given a fragment, queue it on the fragment queue and if the fragment
40  *      is complete, send it back to ax25_rx_iframe.
41  */
42 static int ax25_rx_fragment(ax25_cb *ax25, struct sk_buff *skb)
43 {
44         struct sk_buff *skbn, *skbo;
45
46         if (ax25->fragno != 0) {
47                 if (!(*skb->data & AX25_SEG_FIRST)) {
48                         if ((ax25->fragno - 1) == (*skb->data & AX25_SEG_REM)) {
49                                 /* Enqueue fragment */
50                                 ax25->fragno = *skb->data & AX25_SEG_REM;
51                                 skb_pull(skb, 1);       /* skip fragno */
52                                 ax25->fraglen += skb->len;
53                                 skb_queue_tail(&ax25->frag_queue, skb);
54
55                                 /* Last fragment received ? */
56                                 if (ax25->fragno == 0) {
57                                         skbn = alloc_skb(AX25_MAX_HEADER_LEN +
58                                                          ax25->fraglen,
59                                                          GFP_ATOMIC);
60                                         if (!skbn) {
61                                                 skb_queue_purge(&ax25->frag_queue);
62                                                 return 1;
63                                         }
64
65                                         skb_reserve(skbn, AX25_MAX_HEADER_LEN);
66
67                                         skbn->dev   = ax25->ax25_dev->dev;
68                                         skbn->h.raw = skbn->data;
69                                         skbn->nh.raw = skbn->data;
70
71                                         /* Copy data from the fragments */
72                                         while ((skbo = skb_dequeue(&ax25->frag_queue)) != NULL) {
73                                                 memcpy(skb_put(skbn, skbo->len), skbo->data, skbo->len);
74                                                 kfree_skb(skbo);
75                                         }
76
77                                         ax25->fraglen = 0;
78
79                                         if (ax25_rx_iframe(ax25, skbn) == 0)
80                                                 kfree_skb(skbn);
81                                 }
82
83                                 return 1;
84                         }
85                 }
86         } else {
87                 /* First fragment received */
88                 if (*skb->data & AX25_SEG_FIRST) {
89                         skb_queue_purge(&ax25->frag_queue);
90                         ax25->fragno = *skb->data & AX25_SEG_REM;
91                         skb_pull(skb, 1);               /* skip fragno */
92                         ax25->fraglen = skb->len;
93                         skb_queue_tail(&ax25->frag_queue, skb);
94                         return 1;
95                 }
96         }
97
98         return 0;
99 }
100
101 /*
102  *      This is where all valid I frames are sent to, to be dispatched to
103  *      whichever protocol requires them.
104  */
105 int ax25_rx_iframe(ax25_cb *ax25, struct sk_buff *skb)
106 {
107         int (*func)(struct sk_buff *, ax25_cb *);
108         volatile int queued = 0;
109         unsigned char pid;
110
111         if (skb == NULL) return 0;
112
113         ax25_start_idletimer(ax25);
114
115         pid = *skb->data;
116
117 #ifdef CONFIG_INET
118         if (pid == AX25_P_IP) {
119                 /* working around a TCP bug to keep additional listeners
120                  * happy. TCP re-uses the buffer and destroys the original
121                  * content.
122                  */
123                 struct sk_buff *skbn = skb_copy(skb, GFP_ATOMIC);
124                 if (skbn != NULL) {
125                         kfree_skb(skb);
126                         skb = skbn;
127                 }
128
129                 skb_pull(skb, 1);       /* Remove PID */
130                 skb->h.raw    = skb->data;
131                 skb->nh.raw   = skb->data;
132                 skb->dev      = ax25->ax25_dev->dev;
133                 skb->pkt_type = PACKET_HOST;
134                 skb->protocol = htons(ETH_P_IP);
135                 ip_rcv(skb, skb->dev, NULL);    /* Wrong ptype */
136                 return 1;
137         }
138 #endif
139         if (pid == AX25_P_SEGMENT) {
140                 skb_pull(skb, 1);       /* Remove PID */
141                 return ax25_rx_fragment(ax25, skb);
142         }
143
144         if ((func = ax25_protocol_function(pid)) != NULL) {
145                 skb_pull(skb, 1);       /* Remove PID */
146                 return (*func)(skb, ax25);
147         }
148
149         if (ax25->sk != NULL && ax25->ax25_dev->values[AX25_VALUES_CONMODE] == 2) {
150                 if ((!ax25->pidincl && ax25->sk->sk_protocol == pid) ||
151                     ax25->pidincl) {
152                         if (sock_queue_rcv_skb(ax25->sk, skb) == 0)
153                                 queued = 1;
154                         else
155                                 ax25->condition |= AX25_COND_OWN_RX_BUSY;
156                 }
157         }
158
159         return queued;
160 }
161
162 /*
163  *      Higher level upcall for a LAPB frame
164  */
165 static int ax25_process_rx_frame(ax25_cb *ax25, struct sk_buff *skb, int type, int dama)
166 {
167         int queued = 0;
168
169         if (ax25->state == AX25_STATE_0)
170                 return 0;
171
172         switch (ax25->ax25_dev->values[AX25_VALUES_PROTOCOL]) {
173         case AX25_PROTO_STD_SIMPLEX:
174         case AX25_PROTO_STD_DUPLEX:
175                 queued = ax25_std_frame_in(ax25, skb, type);
176                 break;
177
178 #ifdef CONFIG_AX25_DAMA_SLAVE
179         case AX25_PROTO_DAMA_SLAVE:
180                 if (dama || ax25->ax25_dev->dama.slave)
181                         queued = ax25_ds_frame_in(ax25, skb, type);
182                 else
183                         queued = ax25_std_frame_in(ax25, skb, type);
184                 break;
185 #endif
186         }
187
188         return queued;
189 }
190
191 static int ax25_rcv(struct sk_buff *skb, struct net_device *dev,
192         ax25_address *dev_addr, struct packet_type *ptype)
193 {
194         ax25_address src, dest, *next_digi = NULL;
195         int type = 0, mine = 0, dama;
196         struct sock *make, *sk;
197         ax25_digi dp, reverse_dp;
198         ax25_cb *ax25;
199         ax25_dev *ax25_dev;
200
201         /*
202          *      Process the AX.25/LAPB frame.
203          */
204
205         skb->h.raw = skb->data;
206
207         if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL) {
208                 kfree_skb(skb);
209                 return 0;
210         }
211
212         /*
213          *      Parse the address header.
214          */
215
216         if (ax25_addr_parse(skb->data, skb->len, &src, &dest, &dp, &type, &dama) == NULL) {
217                 kfree_skb(skb);
218                 return 0;
219         }
220
221         /*
222          *      Ours perhaps ?
223          */
224         if (dp.lastrepeat + 1 < dp.ndigi)               /* Not yet digipeated completely */
225                 next_digi = &dp.calls[dp.lastrepeat + 1];
226
227         /*
228          *      Pull of the AX.25 headers leaving the CTRL/PID bytes
229          */
230         skb_pull(skb, ax25_addr_size(&dp));
231
232         /* For our port addresses ? */
233         if (ax25cmp(&dest, dev_addr) == 0 && dp.lastrepeat + 1 == dp.ndigi)
234                 mine = 1;
235
236         /* Also match on any registered callsign from L3/4 */
237         if (!mine && ax25_listen_mine(&dest, dev) && dp.lastrepeat + 1 == dp.ndigi)
238                 mine = 1;
239
240         /* UI frame - bypass LAPB processing */
241         if ((*skb->data & ~0x10) == AX25_UI && dp.lastrepeat + 1 == dp.ndigi) {
242                 skb->h.raw = skb->data + 2;             /* skip control and pid */
243
244                 ax25_send_to_raw(&dest, skb, skb->data[1]);
245
246                 if (!mine && ax25cmp(&dest, (ax25_address *)dev->broadcast) != 0) {
247                         kfree_skb(skb);
248                         return 0;
249                 }
250
251                 /* Now we are pointing at the pid byte */
252                 switch (skb->data[1]) {
253 #ifdef CONFIG_INET
254                 case AX25_P_IP:
255                         skb_pull(skb,2);                /* drop PID/CTRL */
256                         skb->h.raw    = skb->data;
257                         skb->nh.raw   = skb->data;
258                         skb->dev      = dev;
259                         skb->pkt_type = PACKET_HOST;
260                         skb->protocol = htons(ETH_P_IP);
261                         ip_rcv(skb, dev, ptype);        /* Note ptype here is the wrong one, fix me later */
262                         break;
263
264                 case AX25_P_ARP:
265                         skb_pull(skb,2);
266                         skb->h.raw    = skb->data;
267                         skb->nh.raw   = skb->data;
268                         skb->dev      = dev;
269                         skb->pkt_type = PACKET_HOST;
270                         skb->protocol = htons(ETH_P_ARP);
271                         arp_rcv(skb, dev, ptype);       /* Note ptype here is wrong... */
272                         break;
273 #endif
274                 case AX25_P_TEXT:
275                         /* Now find a suitable dgram socket */
276                         sk = ax25_get_socket(&dest, &src, SOCK_DGRAM);
277                         if (sk != NULL) {
278                                 if (atomic_read(&sk->sk_rmem_alloc) >=
279                                     sk->sk_rcvbuf) {
280                                         kfree_skb(skb);
281                                 } else {
282                                         /*
283                                          *      Remove the control and PID.
284                                          */
285                                         skb_pull(skb, 2);
286                                         if (sock_queue_rcv_skb(sk, skb) != 0)
287                                                 kfree_skb(skb);
288                                 }
289                                 release_sock(sk);
290                         } else {
291                                 kfree_skb(skb);
292                         }
293                         break;
294
295                 default:
296                         kfree_skb(skb); /* Will scan SOCK_AX25 RAW sockets */
297                         break;
298                 }
299
300                 return 0;
301         }
302
303         /*
304          *      Is connected mode supported on this device ?
305          *      If not, should we DM the incoming frame (except DMs) or
306          *      silently ignore them. For now we stay quiet.
307          */
308         if (ax25_dev->values[AX25_VALUES_CONMODE] == 0) {
309                 kfree_skb(skb);
310                 return 0;
311         }
312
313         /* LAPB */
314
315         /* AX.25 state 1-4 */
316
317         ax25_digi_invert(&dp, &reverse_dp);
318
319         if ((ax25 = ax25_find_cb(&dest, &src, &reverse_dp, dev)) != NULL) {
320                 /*
321                  *      Process the frame. If it is queued up internally it
322                  *      returns one otherwise we free it immediately. This
323                  *      routine itself wakes the user context layers so we do
324                  *      no further work
325                  */
326                 if (ax25_process_rx_frame(ax25, skb, type, dama) == 0)
327                         kfree_skb(skb);
328
329                 ax25_cb_put(ax25);
330                 return 0;
331         }
332
333         /* AX.25 state 0 (disconnected) */
334
335         /* a) received not a SABM(E) */
336
337         if ((*skb->data & ~AX25_PF) != AX25_SABM &&
338             (*skb->data & ~AX25_PF) != AX25_SABME) {
339                 /*
340                  *      Never reply to a DM. Also ignore any connects for
341                  *      addresses that are not our interfaces and not a socket.
342                  */
343                 if ((*skb->data & ~AX25_PF) != AX25_DM && mine)
344                         ax25_return_dm(dev, &src, &dest, &dp);
345
346                 kfree_skb(skb);
347                 return 0;
348         }
349
350         /* b) received SABM(E) */
351
352         if (dp.lastrepeat + 1 == dp.ndigi)
353                 sk = ax25_find_listener(&dest, 0, dev, SOCK_SEQPACKET);
354         else
355                 sk = ax25_find_listener(next_digi, 1, dev, SOCK_SEQPACKET);
356
357         if (sk != NULL) {
358                 bh_lock_sock(sk);
359                 if (sk->sk_ack_backlog == sk->sk_max_ack_backlog ||
360                     (make = ax25_make_new(sk, ax25_dev)) == NULL) {
361                         if (mine)
362                                 ax25_return_dm(dev, &src, &dest, &dp);
363                         kfree_skb(skb);
364                         bh_unlock_sock(sk);
365                         sock_put(sk);
366
367                         return 0;
368                 }
369
370                 ax25 = ax25_sk(make);
371                 skb_set_owner_r(skb, make);
372                 skb_queue_head(&sk->sk_receive_queue, skb);
373
374                 make->sk_state = TCP_ESTABLISHED;
375                 make->sk_pair  = sk;
376
377                 sk->sk_ack_backlog++;
378                 bh_unlock_sock(sk);
379         } else {
380                 if (!mine) {
381                         kfree_skb(skb);
382                         return 0;
383                 }
384
385                 if ((ax25 = ax25_create_cb()) == NULL) {
386                         ax25_return_dm(dev, &src, &dest, &dp);
387                         kfree_skb(skb);
388                         return 0;
389                 }
390
391                 ax25_fillin_cb(ax25, ax25_dev);
392         }
393
394         ax25->source_addr = dest;
395         ax25->dest_addr   = src;
396
397         /*
398          *      Sort out any digipeated paths.
399          */
400         if (dp.ndigi && !ax25->digipeat &&
401             (ax25->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
402                 kfree_skb(skb);
403                 ax25_destroy_socket(ax25);
404                 if (sk)
405                         sock_put(sk);
406                 return 0;
407         }
408
409         if (dp.ndigi == 0) {
410                 if (ax25->digipeat != NULL) {
411                         kfree(ax25->digipeat);
412                         ax25->digipeat = NULL;
413                 }
414         } else {
415                 /* Reverse the source SABM's path */
416                 memcpy(ax25->digipeat, &reverse_dp, sizeof(ax25_digi));
417         }
418
419         if ((*skb->data & ~AX25_PF) == AX25_SABME) {
420                 ax25->modulus = AX25_EMODULUS;
421                 ax25->window  = ax25_dev->values[AX25_VALUES_EWINDOW];
422         } else {
423                 ax25->modulus = AX25_MODULUS;
424                 ax25->window  = ax25_dev->values[AX25_VALUES_WINDOW];
425         }
426
427         ax25_send_control(ax25, AX25_UA, AX25_POLLON, AX25_RESPONSE);
428
429 #ifdef CONFIG_AX25_DAMA_SLAVE
430         if (dama && ax25->ax25_dev->values[AX25_VALUES_PROTOCOL] == AX25_PROTO_DAMA_SLAVE)
431                 ax25_dama_on(ax25);
432 #endif
433
434         ax25->state = AX25_STATE_3;
435
436         ax25_cb_add(ax25);
437
438         ax25_start_heartbeat(ax25);
439         ax25_start_t3timer(ax25);
440         ax25_start_idletimer(ax25);
441
442         if (sk) {
443                 if (!sock_flag(sk, SOCK_DEAD))
444                         sk->sk_data_ready(sk, skb->len);
445                 sock_put(sk);
446         } else
447                 kfree_skb(skb);
448
449         return 0;
450 }
451
452 /*
453  *      Receive an AX.25 frame via a SLIP interface.
454  */
455 int ax25_kiss_rcv(struct sk_buff *skb, struct net_device *dev,
456                   struct packet_type *ptype)
457 {
458         skb->sk = NULL;         /* Initially we don't know who it's for */
459         skb->destructor = NULL; /* Who initializes this, dammit?! */
460
461         if ((*skb->data & 0x0F) != 0) {
462                 kfree_skb(skb); /* Not a KISS data frame */
463                 return 0;
464         }
465
466         skb_pull(skb, AX25_KISS_HEADER_LEN);    /* Remove the KISS byte */
467
468         return ax25_rcv(skb, dev, (ax25_address *)dev->dev_addr, ptype);
469 }