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