OSDN Git Service

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