]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i4b/driver/i4b_ipr.c
Apply error and success logic consistently to the function netisr_queue() and
[FreeBSD/FreeBSD.git] / sys / i4b / driver / i4b_ipr.c
1 /*
2  * Copyright (c) 1997, 2002 Hellmuth Michaelis. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  *---------------------------------------------------------------------------
26  *
27  *      i4b_ipr.c - isdn4bsd IP over raw HDLC ISDN network driver
28  *      ---------------------------------------------------------
29  *      last edit-date: [Sun Mar 17 09:32:58 2002]
30  *
31  *---------------------------------------------------------------------------*
32  *
33  *      statistics counter usage (interface lifetime):
34  *      ----------------------------------------------
35  *      sc->sc_if.if_ipackets   # of received packets
36  *      sc->sc_if.if_ierrors    # of error packets not going to upper layers
37  *      sc->sc_if.if_opackets   # of transmitted packets
38  *      sc->sc_if.if_oerrors    # of error packets not being transmitted
39  *      sc->sc_if.if_collisions # of invalid ip packets after VJ decompression
40  *      sc->sc_if.if_ibytes     # of bytes coming in from the line (before VJ)
41  *      sc->sc_if.if_obytes     # of bytes going out to the line (after VJ)
42  *      sc->sc_if.if_imcasts      (currently unused)
43  *      sc->sc_if.if_omcasts    # of frames sent out of the fastqueue
44  *      sc->sc_if.if_iqdrops    # of frames dropped on input because queue full
45  *      sc->sc_if.if_noproto    # of frames dropped on output because !AF_INET
46  *
47  *      statistics counter usage (connection lifetime):
48  *      -----------------------------------------------
49  *      sc->sc_iinb             # of ISDN incoming bytes from HSCX
50  *      sc->sc_ioutb            # of ISDN outgoing bytes from HSCX
51  *      sc->sc_inb              # of incoming bytes after decompression
52  *      sc->sc_outb             # of outgoing bytes before compression
53  *
54  *---------------------------------------------------------------------------*/ 
55
56 #include <sys/cdefs.h>
57 __FBSDID("$FreeBSD$");
58
59 #include "i4bipr.h"
60
61 #include "opt_i4b.h"
62
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/mbuf.h>
66 #include <sys/socket.h>
67 #include <sys/errno.h>
68 #include <sys/ioccom.h>
69 #include <sys/sockio.h>
70 #include <sys/kernel.h>
71 #include <net/if.h>
72 #include <net/if_types.h>
73 #include <net/netisr.h>
74 #include <netinet/in.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/in_var.h>
77 #include <netinet/ip.h>
78
79 #ifdef IPR_VJ
80 #include <sys/malloc.h>
81 #include <net/slcompress.h>       
82 #define IPR_COMPRESS IFF_LINK0  /* compress TCP traffic */
83 #define IPR_AUTOCOMP IFF_LINK1  /* auto-enable TCP compression */
84
85 /*---------------------------------------------------------------------------
86  * NOTICE: using NO separate buffer relies on the assumption, that the HSCX
87  * IRQ handler _always_ allocates a single, continuous mbuf cluster large
88  * enough to hold the maximum MTU size if the ipr interface !
89  *
90  * CAUTION: i have re-defined IPR_VJ_USEBUFFER because it makes problems
91  *          with 2 i4b's back to back running cvs over ssh, cvs simply
92  *          aborts because it gets bad data. Everything else (telnet/ftp?etc)
93  *          functions fine. 
94  *---------------------------------------------------------------------------*/
95 #define IPR_VJ_USEBUFFER        /* define to use an allocated separate buffer*/
96                                 /* undef to uncompress in the mbuf itself    */
97 #endif /* IPR_VJ */
98
99 #include <sys/time.h>
100 #include <net/bpf.h>
101
102 #include <machine/i4b_ioctl.h>
103 #include <machine/i4b_debug.h>
104
105 #include <i4b/include/i4b_global.h>
106 #include <i4b/include/i4b_l3l4.h>
107
108 #include <i4b/layer4/i4b_l4.h>
109
110 #define I4BIPRMTU       1500            /* regular MTU */
111 #define I4BIPRMAXMTU    2000            /* max MTU */
112 #define I4BIPRMINMTU    500             /* min MTU */
113
114 #define I4BIPRMAXQLEN   50              /* max queue length */
115
116 #define I4BIPRACCT      1               /* enable accounting messages */
117 #define I4BIPRACCTINTVL 2               /* accounting msg interval in secs */
118 #define I4BIPRADJFRXP   1               /* adjust 1st rxd packet */
119
120 /* initialized by L4 */
121
122 static drvr_link_t ipr_drvr_linktab[NI4BIPR];
123 static isdn_link_t *isdn_linktab[NI4BIPR];
124
125 struct ipr_softc {
126         struct ifnet    sc_if;          /* network-visible interface    */
127         int             sc_state;       /* state of the interface       */
128         call_desc_t     *sc_cdp;        /* ptr to call descriptor       */
129         int             sc_updown;      /* soft state of interface      */
130         struct ifqueue  sc_fastq;       /* interactive traffic          */
131         int             sc_dialresp;    /* dialresponse                 */
132         int             sc_lastdialresp;/* last dialresponse            */
133         struct callout_handle   sc_callout;
134         
135 #if I4BIPRACCT
136         int             sc_iinb;        /* isdn driver # of inbytes     */
137         int             sc_ioutb;       /* isdn driver # of outbytes    */
138         int             sc_inb;         /* # of bytes rx'd              */
139         int             sc_outb;        /* # of bytes tx'd              */
140         int             sc_linb;        /* last # of bytes rx'd         */
141         int             sc_loutb;       /* last # of bytes tx'd         */
142         int             sc_fn;          /* flag, first null acct        */
143 #endif  
144
145 #ifdef I4BIPRADJFRXP
146         int             sc_first_pkt;   /* flag, first rxd packet       */
147 #endif
148
149 #if IPR_LOG
150         int             sc_log_first;   /* log first n packets          */
151 #endif
152
153 #ifdef IPR_VJ
154         struct slcompress sc_compr;     /* tcp compression data         */
155 #ifdef IPR_VJ_USEBUFFER
156         u_char          *sc_cbuf;       /* tcp decompression buffer     */
157 #endif
158 #endif
159
160 } ipr_softc[NI4BIPR];
161
162 enum ipr_states {
163         ST_IDLE,                        /* initialized, ready, idle     */
164         ST_DIALING,                     /* dialling out to remote       */
165         ST_CONNECTED_W,                 /* connected to remote          */
166         ST_CONNECTED_A,                 /* connected to remote          */
167 };
168
169 static void i4biprattach(void *);
170 PSEUDO_SET(i4biprattach, i4b_ipr);
171 static int i4biprioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
172
173 static void iprwatchdog(struct ifnet *ifp);
174 static void ipr_init_linktab(int unit);
175 static void ipr_tx_queue_empty(int unit);
176 static int i4biproutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, struct rtentry *rtp);
177 static void iprclearqueues(struct ipr_softc *sc);
178
179 /*===========================================================================*
180  *                      DEVICE DRIVER ROUTINES
181  *===========================================================================*/
182
183 /*---------------------------------------------------------------------------*
184  *      interface attach routine at kernel boot time
185  *---------------------------------------------------------------------------*/
186 static void
187 i4biprattach(void *dummy)
188 {
189         struct ipr_softc *sc = ipr_softc;
190         int i;
191
192 #ifdef IPR_VJ
193         printf("i4bipr: %d IP over raw HDLC ISDN device(s) attached (VJ header compression)\n", NI4BIPR);
194 #else
195         printf("i4bipr: %d IP over raw HDLC ISDN device(s) attached\n", NI4BIPR);
196 #endif
197         
198         for(i=0; i < NI4BIPR; sc++, i++)
199         {
200                 ipr_init_linktab(i);
201
202                 NDBGL4(L4_DIALST, "setting dial state to ST_IDLE");
203
204                 sc->sc_if.if_softc = sc;
205                 sc->sc_state = ST_IDLE;
206                 if_initname(&sc->sc_if, "ipr", i);
207
208
209 #ifdef  IPR_VJ
210                 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_SIMPLEX | IPR_AUTOCOMP;
211 #else
212                 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_SIMPLEX;
213 #endif
214
215                 sc->sc_if.if_mtu = I4BIPRMTU;
216                 sc->sc_if.if_type = IFT_ISDNBASIC;
217                 sc->sc_if.if_ioctl = i4biprioctl;
218                 sc->sc_if.if_output = i4biproutput;
219
220                 sc->sc_if.if_snd.ifq_maxlen = I4BIPRMAXQLEN;
221                 sc->sc_fastq.ifq_maxlen = I4BIPRMAXQLEN;
222
223                 if(!mtx_initialized(&sc->sc_fastq.ifq_mtx))
224                         mtx_init(&sc->sc_fastq.ifq_mtx, "i4b_ipr_fastq", NULL, MTX_DEF);
225
226                 sc->sc_if.if_ipackets = 0;
227                 sc->sc_if.if_ierrors = 0;
228                 sc->sc_if.if_opackets = 0;
229                 sc->sc_if.if_oerrors = 0;
230                 sc->sc_if.if_collisions = 0;
231                 sc->sc_if.if_ibytes = 0;
232                 sc->sc_if.if_obytes = 0;
233                 sc->sc_if.if_imcasts = 0;
234                 sc->sc_if.if_omcasts = 0;
235                 sc->sc_if.if_iqdrops = 0;
236                 sc->sc_if.if_noproto = 0;
237
238 #if I4BIPRACCT
239                 sc->sc_if.if_timer = 0; 
240                 sc->sc_if.if_watchdog = iprwatchdog;    
241                 sc->sc_iinb = 0;
242                 sc->sc_ioutb = 0;
243                 sc->sc_inb = 0;
244                 sc->sc_outb = 0;
245                 sc->sc_linb = 0;
246                 sc->sc_loutb = 0;
247                 sc->sc_fn = 1;
248 #endif
249
250 #if IPR_LOG
251                 sc->sc_log_first = IPR_LOG;
252 #endif
253
254 #ifdef IPR_VJ
255                 sl_compress_init(&sc->sc_compr, -1);
256
257 #ifdef IPR_VJ_USEBUFFER
258                 if(!((sc->sc_cbuf =
259                    (u_char *)malloc(I4BIPRMAXMTU+128, M_DEVBUF, M_WAITOK))))
260                 {
261                         panic("if_ipr.c, ipr_attach: VJ malloc failed");
262                 }
263 #endif
264 #endif
265                 sc->sc_updown = SOFT_ENA;       /* soft enabled */
266                 sc->sc_dialresp = DSTAT_NONE;   /* no response */
267                 sc->sc_lastdialresp = DSTAT_NONE;
268                 
269                 if_attach(&sc->sc_if);
270
271                 bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
272         }
273 }
274
275 /*---------------------------------------------------------------------------*
276  *      output a packet to the ISDN B-channel
277  *---------------------------------------------------------------------------*/
278 static int
279 i4biproutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
280          struct rtentry *rtp)
281 {
282         struct ipr_softc *sc;
283         int unit;
284         int s;
285         struct ifqueue *ifq;
286         struct ip *ip;
287         
288         s = SPLI4B();
289
290         sc = ifp->if_softc;
291         unit = ifp->if_dunit;
292
293         /* check for IP */
294         
295         if(dst->sa_family != AF_INET)
296         {
297                 if_printf(ifp, "af%d not supported\n", dst->sa_family);
298                 m_freem(m);
299                 splx(s);
300                 sc->sc_if.if_noproto++;
301                 sc->sc_if.if_oerrors++;
302                 return(EAFNOSUPPORT);
303         }
304
305         /* check interface state = UP */
306         
307         if(!(ifp->if_flags & IFF_UP))
308         {
309                 NDBGL4(L4_IPRDBG, "ipr%d: interface is DOWN!", unit);
310                 m_freem(m);
311                 splx(s);
312                 sc->sc_if.if_oerrors++;
313                 return(ENETDOWN);
314         }
315
316         /* dial if necessary */
317         
318         if(sc->sc_state == ST_IDLE || sc->sc_state == ST_DIALING)
319         {
320
321 #ifdef NOTDEF
322                 switch(sc->sc_dialresp)
323                 {
324                         case DSTAT_TFAIL:       /* transient failure */
325                                 NDBGL4(L4_IPRDBG, "ipr%d: transient dial failure!", unit);
326                                 m_freem(m);
327                                 iprclearqueues(sc);
328                                 sc->sc_dialresp = DSTAT_NONE;
329                                 splx(s);
330                                 sc->sc_if.if_oerrors++;
331                                 return(ENETUNREACH);
332                                 break;
333
334                         case DSTAT_PFAIL:       /* permanent failure */
335                                 NDBGL4(L4_IPRDBG, "ipr%d: permanent dial failure!", unit);
336                                 m_freem(m);
337                                 iprclearqueues(sc);
338                                 sc->sc_dialresp = DSTAT_NONE;
339                                 splx(s);
340                                 sc->sc_if.if_oerrors++;
341                                 return(EHOSTUNREACH);                           
342                                 break;
343
344                         case DSTAT_INONLY:      /* no dialout allowed*/
345                                 NDBGL4(L4_IPRDBG, "ipr%d: dialout not allowed failure!", unit);
346                                 m_freem(m);
347                                 iprclearqueues(sc);
348                                 sc->sc_dialresp = DSTAT_NONE;
349                                 splx(s);
350                                 sc->sc_if.if_oerrors++;
351                                 return(EHOSTUNREACH);                           
352                                 break;
353                 }
354 #endif
355
356                 NDBGL4(L4_IPRDBG, "ipr%d: send dial request message!", unit);
357                 NDBGL4(L4_DIALST, "ipr%d: setting dial state to ST_DIALING", unit);
358                 i4b_l4_dialout(BDRV_IPR, unit);
359                 sc->sc_state = ST_DIALING;
360         }
361
362 #if IPR_LOG
363         if(sc->sc_log_first > 0)
364         {
365                 --(sc->sc_log_first);
366                 i4b_l4_packet_ind(BDRV_IPR, unit, 1, m );
367         }
368 #endif
369
370         /* update access time */
371         
372         microtime(&sc->sc_if.if_lastchange);
373
374         /*
375          * check, if type of service indicates interactive, i.e. telnet,
376          * traffic. in case it is interactive, put it into the fast queue,
377          * else (i.e. ftp traffic) put it into the "normal" queue
378          */
379
380         ip = mtod(m, struct ip *);              /* get ptr to ip header */
381          
382         if(ip->ip_tos & IPTOS_LOWDELAY)
383                 ifq = &sc->sc_fastq;
384         else
385                 ifq = (struct ifqueue *)&sc->sc_if.if_snd;
386
387         /* check for space in choosen send queue */
388         
389         if(! IF_HANDOFF(ifq, m, NULL))
390         {
391                 NDBGL4(L4_IPRDBG, "ipr%d: send queue full!", unit);
392                 splx(s);
393                 sc->sc_if.if_oerrors++;
394                 return(ENOBUFS);
395         }
396         
397         NDBGL4(L4_IPRDBG, "ipr%d: add packet to send queue!", unit);
398         
399         ipr_tx_queue_empty(unit);
400
401         splx(s);
402
403         return (0);
404 }
405
406 /*---------------------------------------------------------------------------*
407  *      process ioctl
408  *---------------------------------------------------------------------------*/
409 static int
410 i4biprioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
411 {
412         struct ipr_softc *sc = ifp->if_softc;
413         struct ifreq *ifr = (struct ifreq *)data;
414         struct ifaddr *ifa = (struct ifaddr *)data;
415         int s;
416         int error = 0;
417
418         s = SPLI4B();
419         
420         switch (cmd)
421         {
422                 case SIOCAIFADDR:       /* add interface address */
423                 case SIOCSIFADDR:       /* set interface address */
424                 case SIOCSIFDSTADDR:    /* set interface destination address */
425                         if(ifa->ifa_addr->sa_family != AF_INET)
426                                 error = EAFNOSUPPORT;
427                         else
428                                 sc->sc_if.if_flags |= IFF_UP;
429                         microtime(&sc->sc_if.if_lastchange);
430                         break;
431
432                 case SIOCSIFFLAGS:      /* set interface flags */
433                         if(!(ifr->ifr_flags & IFF_UP))
434                         {
435                                 if(sc->sc_if.if_flags & IFF_RUNNING)
436                                 {
437                                         /* disconnect ISDN line */
438                                         i4b_l4_drvrdisc(BDRV_IPR,
439                                             ifp->if_dunit);
440                                         sc->sc_if.if_flags &= ~IFF_RUNNING;
441                                 }
442
443                                 sc->sc_state = ST_IDLE;
444
445                                 /* empty queues */
446
447                                 iprclearqueues(sc);
448                         }
449
450                         if(ifr->ifr_flags & IFF_DEBUG)
451                         {
452                                 /* enable debug messages */
453                         }
454                         
455                         microtime(&sc->sc_if.if_lastchange);
456                         break;
457
458                 case SIOCSIFMTU:        /* set interface MTU */
459                         if(ifr->ifr_mtu > I4BIPRMAXMTU)
460                                 error = EINVAL;
461                         else if(ifr->ifr_mtu < I4BIPRMINMTU)
462                                 error = EINVAL;
463                         else
464                         {
465                                 ifp->if_mtu = ifr->ifr_mtu;
466                                 microtime(&sc->sc_if.if_lastchange);
467                         }
468                         break;
469 #if 0
470         /* not needed for FreeBSD, done in sl_compress_init() (-hm) */
471         
472                         /* need to add an ioctl:        set VJ max slot ID
473                          * #define IPRIOCSMAXCID        _IOW('I', XXX, int)
474                          */
475 #ifdef IPR_VJ
476                 case IPRIOCSMAXCID:
477                         {
478                         struct thread *td = curthread;  /* XXX */
479
480                         if((error = suser(td)))
481                                 return (error);
482                         sl_compress_setup(sc->sc_compr, *(int *)data);
483                         }
484                         break;
485 #endif
486 #endif /* #if 0 */
487
488                 default:
489                         error = EINVAL;
490                         break;
491         }
492         splx(s);
493         return(error);
494 }
495
496 /*---------------------------------------------------------------------------*
497  *      clear the interface's send queues
498  *---------------------------------------------------------------------------*/
499 static void
500 iprclearqueues(struct ipr_softc *sc)
501 {
502         int x;
503
504         x = splimp();
505         IF_DRAIN(&sc->sc_fastq);
506         IF_DRAIN(&sc->sc_if.if_snd);
507         splx(x);
508 }
509         
510 #if I4BIPRACCT
511 /*---------------------------------------------------------------------------*
512  *      watchdog routine
513  *---------------------------------------------------------------------------*/
514 static void
515 iprwatchdog(struct ifnet *ifp)
516 {
517         int unit = ifp->if_dunit;
518         struct ipr_softc *sc = ifp->if_softc;
519         bchan_statistics_t bs;
520         
521         /* get # of bytes in and out from the HSCX driver */ 
522         
523         (*isdn_linktab[unit]->bch_stat)
524                 (isdn_linktab[unit]->unit, isdn_linktab[unit]->channel, &bs);
525
526         sc->sc_ioutb += bs.outbytes;
527         sc->sc_iinb += bs.inbytes;
528         
529         if((sc->sc_iinb != sc->sc_linb) || (sc->sc_ioutb != sc->sc_loutb) || sc->sc_fn) 
530         {
531                 int ri = (sc->sc_iinb - sc->sc_linb)/I4BIPRACCTINTVL;
532                 int ro = (sc->sc_ioutb - sc->sc_loutb)/I4BIPRACCTINTVL;
533
534                 if((sc->sc_iinb == sc->sc_linb) && (sc->sc_ioutb == sc->sc_loutb))
535                         sc->sc_fn = 0;
536                 else
537                         sc->sc_fn = 1;
538                         
539                 sc->sc_linb = sc->sc_iinb;
540                 sc->sc_loutb = sc->sc_ioutb;
541
542                 i4b_l4_accounting(BDRV_IPR, unit, ACCT_DURING,
543                          sc->sc_ioutb, sc->sc_iinb, ro, ri, sc->sc_outb, sc->sc_inb);
544         }
545         sc->sc_if.if_timer = I4BIPRACCTINTVL;   
546 }
547 #endif /* I4BIPRACCT */
548
549 /*===========================================================================*
550  *                      ISDN INTERFACE ROUTINES
551  *===========================================================================*/
552
553 /*---------------------------------------------------------------------------*
554  *      start transmitting after connect
555  *---------------------------------------------------------------------------*/
556 static void
557 i4bipr_connect_startio(struct ipr_softc *sc)
558 {
559         int s = SPLI4B();
560         
561         if(sc->sc_state == ST_CONNECTED_W)
562         {
563                 sc->sc_state = ST_CONNECTED_A;
564                 ipr_tx_queue_empty(sc->sc_if.if_dunit);
565         }
566
567         splx(s);
568 }
569  
570 /*---------------------------------------------------------------------------*
571  *      this routine is called from L4 handler at connect time
572  *---------------------------------------------------------------------------*/
573 static void
574 ipr_connect(int unit, void *cdp)
575 {
576         struct ipr_softc *sc = &ipr_softc[unit];
577         int s;
578
579         sc->sc_cdp = (call_desc_t *)cdp;
580
581         s = SPLI4B();
582
583         NDBGL4(L4_DIALST, "ipr%d: setting dial state to ST_CONNECTED", unit);
584
585         sc->sc_if.if_flags |= IFF_RUNNING;
586         sc->sc_state = ST_CONNECTED_W;
587
588         sc->sc_dialresp = DSTAT_NONE;
589         sc->sc_lastdialresp = DSTAT_NONE;       
590         
591 #if I4BIPRACCT
592         sc->sc_iinb = 0;
593         sc->sc_ioutb = 0;
594         sc->sc_inb = 0;
595         sc->sc_outb = 0;
596         sc->sc_linb = 0;
597         sc->sc_loutb = 0;
598         sc->sc_if.if_timer = I4BIPRACCTINTVL;
599 #endif
600
601 #ifdef I4BIPRADJFRXP
602         sc->sc_first_pkt = 1;
603 #endif
604
605         /*
606          * Sometimes ISDN B-channels are switched thru asymmetic. This
607          * means that under such circumstances B-channel data (the first
608          * three packets of a TCP connection in my case) may get lost,
609          * causing a large delay until the connection is started.
610          * When the sending of the very first packet of a TCP connection
611          * is delayed for a to be empirically determined delay (close
612          * to a second in my case) those packets go thru and the TCP
613          * connection comes up "almost" immediately (-hm).
614          */
615
616         if(sc->sc_cdp->isdntxdelay > 0)
617         {
618                 int delay;
619
620                 if (hz == 100) {
621                         delay = sc->sc_cdp->isdntxdelay;        /* avoid any rounding */
622                 } else {
623                         delay = sc->sc_cdp->isdntxdelay*hz;
624                         delay /= 100;
625                 }
626
627                 START_TIMER(sc->sc_callout, (TIMEOUT_FUNC_T)i4bipr_connect_startio, (void *)sc,  delay);
628         }
629         else
630         {
631                 sc->sc_state = ST_CONNECTED_A;
632                 ipr_tx_queue_empty(unit);
633         }
634
635         splx(s);
636
637         /* we don't need any negotiation - pass event back right now */
638         i4b_l4_negcomplete(sc->sc_cdp);
639 }
640         
641 /*---------------------------------------------------------------------------*
642  *      this routine is called from L4 handler at disconnect time
643  *---------------------------------------------------------------------------*/
644 static void
645 ipr_disconnect(int unit, void *cdp)
646 {
647         call_desc_t *cd = (call_desc_t *)cdp;
648         struct ipr_softc *sc = &ipr_softc[unit];
649
650         /* new stuff to check that the active channel is being closed */
651
652         if (cd != sc->sc_cdp)
653         {
654                 NDBGL4(L4_IPRDBG, "ipr%d: channel %d not active",
655                                 cd->driver_unit, cd->channelid);
656                 return;
657         }
658
659 #if I4BIPRACCT
660         sc->sc_if.if_timer = 0;
661 #endif
662
663 #if IPR_LOG
664         /* show next IPR_LOG packets again */
665         sc->sc_log_first = IPR_LOG;
666 #endif
667
668         i4b_l4_accounting(BDRV_IPR, cd->driver_unit, ACCT_FINAL,
669                  sc->sc_ioutb, sc->sc_iinb, 0, 0, sc->sc_outb, sc->sc_inb);
670         
671         sc->sc_cdp = (call_desc_t *)0;  
672
673         NDBGL4(L4_DIALST, "setting dial state to ST_IDLE");
674
675         sc->sc_dialresp = DSTAT_NONE;
676         sc->sc_lastdialresp = DSTAT_NONE;       
677
678         sc->sc_if.if_flags &= ~IFF_RUNNING;
679         sc->sc_state = ST_IDLE;
680 }
681
682 /*---------------------------------------------------------------------------*
683  *      this routine is used to give a feedback from userland daemon
684  *      in case of dial problems
685  *---------------------------------------------------------------------------*/
686 static void
687 ipr_dialresponse(int unit, int status, cause_t cause)
688 {
689         struct ipr_softc *sc = &ipr_softc[unit];
690         sc->sc_dialresp = status;
691
692         NDBGL4(L4_IPRDBG, "ipr%d: last=%d, this=%d",
693                 unit, sc->sc_lastdialresp, sc->sc_dialresp);
694
695         if(status != DSTAT_NONE)
696         {
697                 NDBGL4(L4_IPRDBG, "ipr%d: clearing queues", unit);
698                 iprclearqueues(sc);
699         }
700 }
701         
702 /*---------------------------------------------------------------------------*
703  *      interface soft up/down
704  *---------------------------------------------------------------------------*/
705 static void
706 ipr_updown(int unit, int updown)
707 {
708         struct ipr_softc *sc = &ipr_softc[unit];
709         sc->sc_updown = updown;
710 }
711         
712 /*---------------------------------------------------------------------------*
713  *      this routine is called from the HSCX interrupt handler
714  *      when a new frame (mbuf) has been received and was put on
715  *      the rx queue. It is assumed that this routines runs at
716  *      pri level splimp() ! Keep it short !
717  *---------------------------------------------------------------------------*/
718 static void
719 ipr_rx_data_rdy(int unit)
720 {
721         register struct ipr_softc *sc = &ipr_softc[unit];
722         register struct mbuf *m;
723
724 #ifdef IPR_VJ
725 #ifdef IPR_VJ_USEBUFFER
726         u_char *cp = sc->sc_cbuf;
727 #endif  
728         int len, c;
729 #endif
730         
731         if((m = *isdn_linktab[unit]->rx_mbuf) == NULL)
732                 return;
733
734         m->m_pkthdr.rcvif = &sc->sc_if;
735
736         m->m_pkthdr.len = m->m_len;
737
738         microtime(&sc->sc_if.if_lastchange);
739
740 #ifdef I4BIPRADJFRXP
741
742         /*
743          * The very first packet after the B channel is switched thru
744          * has very often several bytes of random data prepended. This
745          * routine looks where the IP header starts and removes the
746          * the bad data.
747          */
748
749         if(sc->sc_first_pkt)
750         {
751                 unsigned char *mp = m->m_data;
752                 int i;
753                 
754                 sc->sc_first_pkt = 0;
755
756                 for(i = 0; i < m->m_len; i++, mp++)
757                 {
758                         if( ((*mp & 0xf0) == 0x40) &&
759                             ((*mp & 0x0f) >= 0x05) )
760                         {
761                                 m->m_data = mp;
762                                 m->m_pkthdr.len -= i;                           
763                                 break;
764                         }
765                 }
766         }
767 #endif
768                 
769         sc->sc_if.if_ipackets++;
770         sc->sc_if.if_ibytes += m->m_pkthdr.len;
771
772 #ifdef  IPR_VJ
773         if((c = (*(mtod(m, u_char *)) & 0xf0)) != (IPVERSION << 4))
774         {
775                 /* copy data to buffer */
776
777                 len = m->m_len;
778
779 #ifdef IPR_VJ_USEBUFFER
780 /* XXX */       m_copydata(m, 0, len, cp);
781 #endif
782                 
783                 if(c & 0x80)
784                 {
785                         c = TYPE_COMPRESSED_TCP;
786                 }
787                 else if(c == TYPE_UNCOMPRESSED_TCP)
788                 {
789 #ifdef IPR_VJ_USEBUFFER
790                         *cp &= 0x4f;            /* XXX */
791 #else
792                         *(mtod(m, u_char *)) &= 0x4f;                   
793 #endif                  
794                 }
795
796                 /*
797                  * We've got something that's not an IP packet.
798                  * If compression is enabled, try to decompress it.
799                  * Otherwise, if `auto-enable' compression is on and
800                  * it's a reasonable packet, decompress it and then
801                  * enable compression.  Otherwise, drop it.
802                  */
803                 if(sc->sc_if.if_flags & IPR_COMPRESS)
804                 {
805 #ifdef IPR_VJ_USEBUFFER
806                         len = sl_uncompress_tcp(&cp,len,(u_int)c,&sc->sc_compr);
807 #else
808                         len = sl_uncompress_tcp((u_char **)&m->m_data, len,
809                                         (u_int)c, &sc->sc_compr);
810 #endif                  
811
812                         if(len <= 0)
813                         {
814 #ifdef DEBUG_IPR_VJ
815                                 printf("i4b_ipr, ipr_rx_data_rdy: len <= 0 IPR_COMPRESS!\n");
816 #endif
817                                 goto error;
818                         }
819                 }
820                 else if((sc->sc_if.if_flags & IPR_AUTOCOMP) &&
821                         (c == TYPE_UNCOMPRESSED_TCP) && (len >= 40))
822                 {
823 #ifdef IPR_VJ_USEBUFFER
824                         len = sl_uncompress_tcp(&cp,len,(u_int)c,&sc->sc_compr);
825 #else
826                         len = sl_uncompress_tcp((u_char **)&m->m_data, len,
827                                         (u_int)c, &sc->sc_compr);
828 #endif
829
830                         if(len <= 0)
831                         {
832 #ifdef DEBUG_IPR_VJ
833                                 printf("i4b_ipr, ipr_rx_data_rdy: len <= 0 IPR_AUTOCOMP!\n");
834 #endif
835                                 goto error;
836                         }
837
838                         sc->sc_if.if_flags |= IPR_COMPRESS;
839                 }
840                 else
841                 {
842 #ifdef DEBUG_IPR_VJ
843                         printf("i4b_ipr, ipr_input: invalid ip packet!\n");
844 #endif
845
846 error:
847                         sc->sc_if.if_ierrors++;
848                         sc->sc_if.if_collisions++;
849                         m_freem(m);
850                         return;
851                 }
852 #ifdef IPR_VJ_USEBUFFER
853 /* XXX */       m_copyback(m, 0, len, cp);
854 #else
855                 m->m_len = m->m_pkthdr.len = len;
856 #endif
857         }
858 #endif
859
860 #if I4BIPRACCT
861         /* NB. do the accounting after decompression!           */
862         sc->sc_inb += m->m_pkthdr.len;
863 #endif
864
865 #if IPR_LOG
866         if(sc->sc_log_first > 0)
867         {
868                 --(sc->sc_log_first);
869                 i4b_l4_packet_ind(BDRV_IPR, unit, 0, m );
870         }
871 #endif
872
873         if(sc->sc_if.if_bpf)
874         {
875                 /* prepend the address family as a four byte field */           
876                 struct mbuf mm;
877                 u_int af = AF_INET;
878                 mm.m_next = m;
879                 mm.m_len = 4;
880                 mm.m_data = (char *)&af;
881                 BPF_MTAP(&sc->sc_if, &mm);
882         }
883
884         if(netisr_queue(NETISR_IP, m))  /* (0) on success. */
885         {
886                 NDBGL4(L4_IPRDBG, "ipr%d: ipintrq full!", unit);
887                 sc->sc_if.if_ierrors++;
888                 sc->sc_if.if_iqdrops++;         
889         }
890 }
891
892 /*---------------------------------------------------------------------------*
893  *      this routine is called from the HSCX interrupt handler
894  *      when the last frame has been sent out and there is no
895  *      further frame (mbuf) in the tx queue.
896  *---------------------------------------------------------------------------*/
897 static void
898 ipr_tx_queue_empty(int unit)
899 {
900         register struct ipr_softc *sc = &ipr_softc[unit];
901         register struct mbuf *m;
902 #ifdef  IPR_VJ  
903         struct ip *ip;  
904 #endif
905         int x = 0;
906
907         if(sc->sc_state != ST_CONNECTED_A)
908                 return;
909                 
910         for(;;)
911         {
912                 IF_DEQUEUE(&sc->sc_fastq, m);
913                 if(m)
914                 {
915                         sc->sc_if.if_omcasts++;
916                 }
917                 else
918                 {
919                         IF_DEQUEUE(&sc->sc_if.if_snd, m);
920                         if(m == NULL)
921                                 break;
922                 }
923
924                 microtime(&sc->sc_if.if_lastchange);
925                 
926                 if(sc->sc_if.if_bpf)
927                 {
928                         /* prepend the address family as a four byte field */
929         
930                         struct mbuf mm;
931                         u_int af = AF_INET;
932                         mm.m_next = m;
933                         mm.m_len = 4;
934                         mm.m_data = (char *)&af;
935                         BPF_MTAP(&sc->sc_if, &mm);
936                 }
937         
938 #if I4BIPRACCT
939                 sc->sc_outb += m->m_pkthdr.len; /* size before compression */
940 #endif
941
942 #ifdef IPR_VJ   
943                 if((ip = mtod(m, struct ip *))->ip_p == IPPROTO_TCP)
944                 {
945                         if(sc->sc_if.if_flags & IPR_COMPRESS)
946                         {
947                                 *mtod(m, u_char *) |= sl_compress_tcp(m, ip,
948                                         &sc->sc_compr, 1);
949                         }
950                 }
951 #endif
952                 x = 1;
953
954                 IF_LOCK(isdn_linktab[unit]->tx_queue);
955                 if(_IF_QFULL(isdn_linktab[unit]->tx_queue))
956                 {
957                         NDBGL4(L4_IPRDBG, "ipr%d: tx queue full!", unit);
958                         m_freem(m);
959                 }
960                 else
961                 {
962                         sc->sc_if.if_obytes += m->m_pkthdr.len;
963
964                         sc->sc_if.if_opackets++;
965
966                         _IF_ENQUEUE(isdn_linktab[unit]->tx_queue, m);
967
968                 }
969                 IF_UNLOCK(isdn_linktab[unit]->tx_queue);
970         }
971
972         if(x)
973                 (*isdn_linktab[unit]->bch_tx_start)(isdn_linktab[unit]->unit, isdn_linktab[unit]->channel);
974 }
975
976 /*---------------------------------------------------------------------------*
977  *      this routine is called from the HSCX interrupt handler
978  *      each time a packet is received or transmitted. It should
979  *      be used to implement an activity timeout mechanism.
980  *---------------------------------------------------------------------------*/
981 static void
982 ipr_activity(int unit, int rxtx)
983 {
984         ipr_softc[unit].sc_cdp->last_active_time = SECOND;
985 }
986
987 /*---------------------------------------------------------------------------*
988  *      return this drivers linktab address
989  *---------------------------------------------------------------------------*/
990 drvr_link_t *
991 ipr_ret_linktab(int unit)
992 {
993         return(&ipr_drvr_linktab[unit]);
994 }
995
996 /*---------------------------------------------------------------------------*
997  *      setup the isdn_linktab for this driver
998  *---------------------------------------------------------------------------*/
999 void
1000 ipr_set_linktab(int unit, isdn_link_t *ilt)
1001 {
1002         isdn_linktab[unit] = ilt;
1003 }
1004
1005 /*---------------------------------------------------------------------------*
1006  *      initialize this drivers linktab
1007  *---------------------------------------------------------------------------*/
1008 static void
1009 ipr_init_linktab(int unit)
1010 {
1011         ipr_drvr_linktab[unit].unit = unit;
1012         ipr_drvr_linktab[unit].bch_rx_data_ready = ipr_rx_data_rdy;
1013         ipr_drvr_linktab[unit].bch_tx_queue_empty = ipr_tx_queue_empty;
1014         ipr_drvr_linktab[unit].bch_activity = ipr_activity;
1015         ipr_drvr_linktab[unit].line_connected = ipr_connect;
1016         ipr_drvr_linktab[unit].line_disconnected = ipr_disconnect;
1017         ipr_drvr_linktab[unit].dial_response = ipr_dialresponse;
1018         ipr_drvr_linktab[unit].updown_ind = ipr_updown; 
1019 }
1020
1021 /*===========================================================================*/