Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / netisdn / i4b_ipr.c
blob8585b08bd0894a2f2c7ee0cf4f8ac9768d9e18d8
1 /*
2 * Copyright (c) 1997, 2000 Hellmuth Michaelis. All rights reserved.
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.
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.
25 *---------------------------------------------------------------------------
27 * i4b_ipr.c - isdn4bsd IP over raw HDLC ISDN network driver
28 * ---------------------------------------------------------
30 * $Id: i4b_ipr.c,v 1.33 2009/03/18 10:22:43 cegger Exp $
32 * $FreeBSD$
34 * last edit-date: [Fri Jan 5 11:33:47 2001]
36 *---------------------------------------------------------------------------*
38 * statistics counter usage (interface lifetime):
39 * ----------------------------------------------
40 * sc->sc_if.if_ipackets # of received packets
41 * sc->sc_if.if_ierrors # of error packets not going to upper layers
42 * sc->sc_if.if_opackets # of transmitted packets
43 * sc->sc_if.if_oerrors # of error packets not being transmitted
44 * sc->sc_if.if_collisions # of invalid ip packets after VJ decompression
45 * sc->sc_if.if_ibytes # of bytes coming in from the line (before VJ)
46 * sc->sc_if.if_obytes # of bytes going out to the line (after VJ)
47 * sc->sc_if.if_imcasts (currently unused)
48 * sc->sc_if.if_omcasts # of frames sent out of the fastqueue
49 * sc->sc_if.if_iqdrops # of frames dropped on input because queue full
50 * sc->sc_if.if_noproto # of frames dropped on output because !AF_INET
52 * statistics counter usage (connection lifetime):
53 * -----------------------------------------------
54 * sc->sc_iinb # of ISDN incoming bytes from HSCX
55 * sc->sc_ioutb # of ISDN outgoing bytes from HSCX
56 * sc->sc_inb # of incoming bytes after decompression
57 * sc->sc_outb # of outgoing bytes before compression
59 *---------------------------------------------------------------------------*/
61 #include <sys/cdefs.h>
62 __KERNEL_RCSID(0, "$NetBSD: i4b_ipr.c,v 1.32 2009/03/14 14:46:10 dsl Exp $");
64 #include "irip.h"
65 #include "opt_irip.h"
67 #if NIRIP > 0
69 #ifdef __FreeBSD__
70 #include "opt_i4b.h"
71 #endif
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/mbuf.h>
76 #include <sys/socket.h>
77 #include <sys/errno.h>
78 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
79 #include <sys/ioccom.h>
80 #include <sys/sockio.h>
81 #ifdef IPR_VJ
82 #include <sys/malloc.h>
83 #endif
84 #else
85 #include <sys/ioctl.h>
86 #endif
88 #if defined(__NetBSD__) && __NetBSD_Version__ >= 104230000
89 #include <sys/callout.h>
90 #endif
92 #include <sys/kernel.h>
93 #include <sys/protosw.h>
95 #include <net/if.h>
96 #include <net/if_types.h>
97 #include <net/netisr.h>
98 #include <net/route.h>
100 #include <netinet/in.h>
101 #include <netinet/in_systm.h>
102 #include <netinet/in_var.h>
103 #include <netinet/ip.h>
105 #ifdef IRIP_VJ
106 #include <net/slcompress.h>
107 #define IPR_COMPRESS IFF_LINK0 /* compress TCP traffic */
108 #define IPR_AUTOCOMP IFF_LINK1 /* auto-enable TCP compression */
110 /*---------------------------------------------------------------------------
111 * NOTICE: using NO separate buffer relies on the assumption, that the HSCX
112 * IRQ handler _always_ allocates a single, continuous mbuf cluster large
113 * enough to hold the maximum MTU size if the ipr interface !
115 * CAUTION: i have re-defined IPR_VJ_USEBUFFER because it makes problems
116 * with 2 i4b's back to back running cvs over ssh, cvs simply
117 * aborts because it gets bad data. Everything else (telnet/ftp?etc)
118 * functions fine.
119 *---------------------------------------------------------------------------*/
120 #define IPR_VJ_USEBUFFER /* define to use an allocated separate buffer*/
121 /* undef to uncompress in the mbuf itself */
122 #endif /* IRIP_VJ */
124 #if defined(__FreeBSD_version) && __FreeBSD_version >= 400008
125 #include "bpf.h"
126 #else
127 #include "bpfilter.h"
128 #endif
129 #if NBPFILTER > 0 || NBPF > 0
130 #include <sys/time.h>
131 #include <net/bpf.h>
132 #endif
134 #ifdef __FreeBSD__
135 #include <machine/i4b_ioctl.h>
136 #include <machine/i4b_debug.h>
137 #else
138 #include <netisdn/i4b_debug.h>
139 #include <netisdn/i4b_ioctl.h>
140 #endif
142 #include <netisdn/i4b_global.h>
143 #include <netisdn/i4b_mbuf.h>
144 #include <netisdn/i4b_l3l4.h>
146 #include <netisdn/i4b_l4.h>
148 #ifndef __FreeBSD__
149 #include <sys/cpu.h> /* For softnet */
150 #endif
152 #ifdef __FreeBSD__
153 #define IPR_FMT "irip%d: "
154 #define IPR_ARG(sc) ((sc)->sc_if.if_unit)
155 #define PDEVSTATIC static
156 #elif defined(__bsdi__)
157 #define IPR_FMT "irip%d: "
158 #define IPR_ARG(sc) ((sc)->sc_if.if_unit)
159 #define PDEVSTATIC /* not static */
160 #else
161 #define IPR_FMT "%s: "
162 #define IPR_ARG(sc) ((sc)->sc_if.if_xname)
163 #define PDEVSTATIC /* not static */
164 #endif
166 #define I4BIPRMTU 1500 /* regular MTU */
167 #define I4BIPRMAXMTU 2000 /* max MTU */
168 #define I4BIPRMINMTU 500 /* min MTU */
170 #define I4BIPRMAXQLEN 50 /* max queue length */
172 #define I4BIPRACCT 1 /* enable accounting messages */
173 #define I4BIPRACCTINTVL 2 /* accounting msg interval in secs */
174 #define I4BIPRADJFRXP 1 /* adjust 1st rxd packet */
177 struct ipr_softc {
178 struct ifnet sc_if; /* network-visible interface */
179 int sc_state; /* state of the interface */
181 call_desc_t *sc_cdp; /* ptr to call descriptor */
182 isdn_link_t *sc_ilt; /* ptr to B channel driver/state */
184 int sc_unit; /* which instance are we? */
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 */
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
200 #if defined(__NetBSD__) && __NetBSD_Version__ >= 104230000
201 struct callout sc_callout;
202 #endif
203 #if defined(__FreeBSD__)
204 struct callout_handle sc_callout;
205 #endif
207 #ifdef I4BIPRADJFRXP
208 int sc_first_pkt; /* flag, first rxd packet */
209 #endif
210 #if IPR_LOG
211 int sc_log_first; /* log first n packets */
212 #endif
214 #ifdef IRIP_VJ
215 struct slcompress sc_compr; /* tcp compression data */
216 #ifdef IPR_VJ_USEBUFFER
217 u_char *sc_cbuf; /* tcp decompression buffer */
218 #endif
219 #endif
221 } ipr_softc[NIRIP];
223 enum ipr_states {
224 ST_IDLE, /* initialized, ready, idle */
225 ST_DIALING, /* dialling out to remote */
226 ST_CONNECTED_W, /* connected to remote */
227 ST_CONNECTED_A, /* connected to remote */
230 #if defined(__FreeBSD__) || defined(__bsdi__)
231 #define THE_UNIT sc->sc_if.if_unit
232 #else
233 #define THE_UNIT sc->sc_unit
234 #endif
236 #ifdef __FreeBSD__
237 #if defined(__FreeBSD_version) && __FreeBSD_version >= 300001
238 # define IOCTL_CMD_T u_long
239 #else
240 #ifdef __NetBSD__
241 # define IOCTL_CMD_T u_long
242 #else
243 # define IOCTL_CMD_T int
244 #endif
245 #endif
246 PDEVSTATIC void iripattach(void *);
247 PSEUDO_SET(iripattach, i4b_ipr);
248 static int irpioctl(struct ifnet *ifp, IOCTL_CMD_T cmd, void *data);
249 #else
250 PDEVSTATIC void iripattach(void);
251 static int iripioctl(struct ifnet *ifp, u_long cmd, void *data);
252 #endif
254 #ifdef __bsdi__
255 static int iprwatchdog(int unit);
256 #else
257 static void iprwatchdog(struct ifnet *ifp);
258 #endif
259 static void ipr_tx_queue_empty(void *);
260 static int iripoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, struct rtentry *rtp);
261 static void iripclearqueues(struct ipr_softc *sc);
262 static void ipr_set_linktab(void *softc, isdn_link_t *ilt);
263 static void ipr_activity(void *softc, int rxtx);
264 static void ipr_rx_data_rdy(void *softc);
265 static void ipr_disconnect(void *softc, void *cdp);
266 static void ipr_connect(void *softc, void *cdp);
267 static void ipr_dialresponse(void *softc, int status, cause_t cause);
268 static void ipr_updown(void *softc, int updown);
269 static void* ipr_get_softc(int unit);
271 static const struct isdn_l4_driver_functions
272 ipr_l4_functions = {
273 ipr_rx_data_rdy,
274 ipr_tx_queue_empty,
275 ipr_activity,
276 ipr_connect,
277 ipr_disconnect,
278 ipr_dialresponse,
279 ipr_updown,
280 ipr_get_softc,
281 ipr_set_linktab,
282 NULL
285 static int irip_driver_id = -1;
287 /*===========================================================================*
288 * DEVICE DRIVER ROUTINES
289 *===========================================================================*/
291 /*---------------------------------------------------------------------------*
292 * interface attach routine at kernel boot time
293 *---------------------------------------------------------------------------*/
294 PDEVSTATIC void
295 #ifdef __FreeBSD__
296 iripattach(void *dummy)
297 #else
298 iripattach(void)
299 #endif
301 struct ipr_softc *sc = ipr_softc;
302 int i;
304 irip_driver_id = isdn_l4_driver_attach("irip", NIRIP, &ipr_l4_functions);
306 for(i=0; i < NIRIP; sc++, i++)
308 NDBGL4(L4_DIALST, "setting dial state to ST_IDLE");
310 sc->sc_state = ST_IDLE;
311 sc->sc_unit = i;
313 #ifdef __FreeBSD__
314 sc->sc_if.if_name = "irip";
315 #if __FreeBSD__ < 3
316 sc->sc_if.if_next = NULL;
317 #endif
318 sc->sc_if.if_unit = i;
319 #elif defined(__bsdi__)
320 sc->sc_if.if_name = "irip";
321 sc->sc_if.if_unit = i;
322 #else
323 snprintf(sc->sc_if.if_xname, sizeof(sc->sc_if.if_xname),
324 "irip%d", i);
325 sc->sc_if.if_softc = sc;
326 #endif
328 #ifdef IRIP_VJ
329 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_SIMPLEX | IPR_AUTOCOMP;
330 #else
331 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_SIMPLEX;
332 #endif
334 #if defined(__NetBSD__) && __NetBSD_Version__ >= 104230000
335 callout_init(&sc->sc_callout, 0);
336 #endif
338 sc->sc_if.if_mtu = I4BIPRMTU;
339 sc->sc_if.if_type = IFT_ISDNBASIC;
340 sc->sc_if.if_ioctl = iripioctl;
341 sc->sc_if.if_output = iripoutput;
343 IFQ_SET_MAXLEN(&sc->sc_if.if_snd, I4BIPRMAXQLEN);
344 sc->sc_fastq.ifq_maxlen = I4BIPRMAXQLEN;
345 IFQ_SET_READY(&sc->sc_if.if_snd);
347 sc->sc_if.if_ipackets = 0;
348 sc->sc_if.if_ierrors = 0;
349 sc->sc_if.if_opackets = 0;
350 sc->sc_if.if_oerrors = 0;
351 sc->sc_if.if_collisions = 0;
352 sc->sc_if.if_ibytes = 0;
353 sc->sc_if.if_obytes = 0;
354 sc->sc_if.if_imcasts = 0;
355 sc->sc_if.if_omcasts = 0;
356 sc->sc_if.if_iqdrops = 0;
357 sc->sc_if.if_noproto = 0;
359 #if I4BIPRACCT
360 sc->sc_if.if_timer = 0;
361 sc->sc_if.if_watchdog = iprwatchdog;
362 sc->sc_iinb = 0;
363 sc->sc_ioutb = 0;
364 sc->sc_inb = 0;
365 sc->sc_outb = 0;
366 sc->sc_linb = 0;
367 sc->sc_loutb = 0;
368 sc->sc_fn = 1;
369 #endif
370 #if IPR_LOG
371 sc->sc_log_first = IPR_LOG;
372 #endif
374 #ifdef IRIP_VJ
375 #ifdef __FreeBSD__
376 sl_compress_init(&sc->sc_compr, -1);
377 #else
378 sl_compress_init(&sc->sc_compr);
379 #endif
381 #ifdef IPR_VJ_USEBUFFER
382 if(!((sc->sc_cbuf =
383 (u_char *)malloc(I4BIPRMAXMTU+128, M_DEVBUF, M_WAITOK))))
385 panic("if_ipr.c, ipr_attach: VJ malloc failed");
387 #endif
388 #endif
390 sc->sc_updown = SOFT_ENA; /* soft enabled */
392 sc->sc_dialresp = DSTAT_NONE; /* no response */
393 sc->sc_lastdialresp = DSTAT_NONE;
395 #if defined(__FreeBSD_version) && ((__FreeBSD_version >= 500009) || (410000 <= __FreeBSD_version && __FreeBSD_version < 500000))
396 /* do not call bpfattach in ether_ifattach */
397 ether_ifattach(&sc->sc_if, 0);
398 #else
399 if_attach(&sc->sc_if);
400 if_alloc_sadl(&sc->sc_if);
401 #endif
403 #if NBPFILTER > 0 || NBPF > 0
404 #ifdef __FreeBSD__
405 bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
406 #else
407 bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
408 #endif
409 #endif
413 /*---------------------------------------------------------------------------*
414 * output a packet to the ISDN B-channel
415 *---------------------------------------------------------------------------*/
416 static int
417 iripoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
418 struct rtentry *rtp)
420 struct ipr_softc *sc;
421 int s, rv;
422 struct ifqueue *ifq = NULL;
423 struct ip *ip;
424 ALTQ_DECL(struct altq_pktattr pktattr;)
426 s = splnet();
428 #if defined(__FreeBSD__) || defined(__bsdi__)
429 unit = ifp->if_unit;
430 sc = &ipr_softc[unit];
431 #else
432 sc = ifp->if_softc;
433 #endif
435 /* check for IP */
437 if(dst->sa_family != AF_INET)
439 printf(IPR_FMT "af%d not supported\n", IPR_ARG(sc), dst->sa_family);
440 m_freem(m);
441 splx(s);
442 sc->sc_if.if_noproto++;
443 sc->sc_if.if_oerrors++;
444 return(EAFNOSUPPORT);
447 /* check interface state = UP */
449 if(!(ifp->if_flags & IFF_UP))
451 NDBGL4(L4_IPRDBG, "%s: interface is DOWN!", sc->sc_if.if_xname);
452 m_freem(m);
453 splx(s);
454 sc->sc_if.if_oerrors++;
455 return(ENETDOWN);
458 /* dial if necessary */
460 if(sc->sc_state == ST_IDLE || sc->sc_state == ST_DIALING)
463 #ifdef NOTDEF
464 switch(sc->sc_dialresp)
466 case DSTAT_TFAIL: /* transient failure */
467 NDBGL4(L4_IPRDBG, "%s: transient dial failure!", sc->sc_if.if_xname);
468 m_freem(m);
469 iripclearqueues(sc);
470 sc->sc_dialresp = DSTAT_NONE;
471 splx(s);
472 sc->sc_if.if_oerrors++;
473 return(ENETUNREACH);
474 break;
476 case DSTAT_PFAIL: /* permanent failure */
477 NDBGL4(L4_IPRDBG, "%s: permanent dial failure!", sc->sc_if.if_xname);
478 m_freem(m);
479 iripclearqueues(sc);
480 sc->sc_dialresp = DSTAT_NONE;
481 splx(s);
482 sc->sc_if.if_oerrors++;
483 return(EHOSTUNREACH);
484 break;
486 case DSTAT_INONLY: /* no dialout allowed*/
487 NDBGL4(L4_IPRDBG, "%s: dialout not allowed failure!", sc->sc_if.if_xname);
488 m_freem(m);
489 iripclearqueues(sc);
490 sc->sc_dialresp = DSTAT_NONE;
491 splx(s);
492 sc->sc_if.if_oerrors++;
493 return(EHOSTUNREACH);
494 break;
496 #endif
498 NDBGL4(L4_IPRDBG, "%s: send dial request message!", sc->sc_if.if_xname);
499 NDBGL4(L4_DIALST, "%s: setting dial state to ST_DIALING", sc->sc_if.if_xname);
500 i4b_l4_dialout(irip_driver_id, sc->sc_unit);
501 sc->sc_state = ST_DIALING;
504 #if IPR_LOG
505 if(sc->sc_log_first > 0)
507 --(sc->sc_log_first);
508 i4b_l4_packet_ind(irip_driver_id, sc->sc_unit, 1, m );
510 #endif
513 * check, if type of service indicates interactive, i.e. telnet,
514 * traffic. in case it is interactive, put it into the fast queue,
515 * else (i.e. ftp traffic) put it into the "normal" queue
518 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
520 ip = mtod(m, struct ip *); /* get ptr to ip header */
522 if(ip->ip_tos & IPTOS_LOWDELAY)
523 ifq = &sc->sc_fastq;
525 /* check for space in choosen send queue */
527 if ((ifq != NULL)
528 #ifdef ALTQ
529 && ALTQ_IS_ENABLED(&sc->sc_if.if_snd) == 0
530 #endif
532 if(IF_QFULL(ifq))
534 NDBGL4(L4_IPRDBG, "%s: send queue full!", sc->sc_if.if_xname);
535 IF_DROP(ifq);
536 m_freem(m);
537 sc->sc_if.if_oerrors++;
538 splx(s);
539 return(ENOBUFS);
541 IF_ENQUEUE(ifq, m);
542 } else {
543 IFQ_ENQUEUE(&sc->sc_if.if_snd, m, &pktattr, rv);
544 if (rv != 0) {
545 sc->sc_if.if_oerrors++;
546 splx(s);
547 return rv;
551 NDBGL4(L4_IPRDBG, "%s: added packet to send queue!", sc->sc_if.if_xname);
553 ipr_tx_queue_empty(sc);
555 splx(s);
557 return (0);
560 /*---------------------------------------------------------------------------*
561 * process ioctl
562 *---------------------------------------------------------------------------*/
563 static int
564 iripioctl(struct ifnet *ifp, u_long cmd, void *data)
566 struct ipr_softc *sc = ifp->if_softc;
568 struct ifreq *ifr = (struct ifreq *)data;
569 struct ifaddr *ifa = (struct ifaddr *)data;
570 int s;
571 int error = 0;
573 s = splnet();
575 switch (cmd)
577 case SIOCAIFADDR: /* add interface address */
578 case SIOCINITIFADDR: /* set interface address */
579 case SIOCSIFDSTADDR: /* set interface destination address */
580 if(ifa->ifa_addr->sa_family != AF_INET)
581 error = EAFNOSUPPORT;
582 else
583 sc->sc_if.if_flags |= IFF_UP;
584 break;
586 case SIOCSIFFLAGS: /* set interface flags */
587 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
588 break;
589 if ((ifr->ifr_flags & IFF_UP) == 0) {
590 if (sc->sc_if.if_flags & IFF_RUNNING) {
591 /* disconnect ISDN line */
592 i4b_l4_drvrdisc(sc->sc_cdp->cdid);
593 sc->sc_if.if_flags &= ~IFF_RUNNING;
596 sc->sc_state = ST_IDLE;
598 /* empty queues */
600 iripclearqueues(sc);
603 #if 0
604 if(ifr->ifr_flags & IFF_DEBUG)
605 ; /* enable debug messages */
606 #endif
608 break;
610 #if !defined(__OpenBSD__)
611 case SIOCSIFMTU: /* set interface MTU */
612 if(ifr->ifr_mtu > I4BIPRMAXMTU)
613 error = EINVAL;
614 else if(ifr->ifr_mtu < I4BIPRMINMTU)
615 error = EINVAL;
616 else if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
617 error = 0;
618 break;
619 #endif /* __OPENBSD__ */
621 #if 0
622 /* not needed for FreeBSD, done in sl_compress_init() (-hm) */
624 /* need to add an ioctl: set VJ max slot ID
625 * #define IPRIOCSMAXCID _IOW('I', XXX, int)
627 #ifdef IPR_VJ
628 case IPRIOCSMAXCID:
630 struct lwp *l = curlwp; /* XXX */
632 if((error = kauth_authorize_network(l->l_cred,
633 KAUTH_NETWORK_INTERFACE,
634 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp,
635 (void *)cmd, NULL)) != 0)
636 break;
637 sl_compress_setup(sc->sc_compr, *(int *)data);
639 break;
640 #endif
641 #endif
642 default:
643 error = ifioctl_common(ifp, cmd, data);
644 if (error == ENETRESET)
645 error = 0;
646 break;
649 splx(s);
651 return(error);
654 /*---------------------------------------------------------------------------*
655 * clear the interface's send queues
656 *---------------------------------------------------------------------------*/
657 static void
658 iripclearqueues(struct ipr_softc *sc)
660 int x;
661 struct mbuf *m;
663 for(;;)
665 x = splnet();
666 IF_DEQUEUE(&sc->sc_fastq, m);
667 splx(x);
669 if(m)
670 m_freem(m);
671 else
672 break;
675 for(;;)
677 x = splnet();
678 IFQ_DEQUEUE(&sc->sc_if.if_snd, m);
679 splx(x);
681 if(m)
682 m_freem(m);
683 else
684 break;
688 #if I4BIPRACCT
689 /*---------------------------------------------------------------------------*
690 * watchdog routine
691 *---------------------------------------------------------------------------*/
692 #ifdef __bsdi__
693 static int
694 iprwatchdog(int unit)
696 #else
697 static void
698 iprwatchdog(struct ifnet *ifp)
700 #endif
701 #ifdef __FreeBSD__
702 int unit = ifp->if_unit;
703 struct ipr_softc *sc = &ipr_softc[unit];
704 #elif defined(__bsdi__)
705 struct ipr_softc *sc = &ipr_softc[unit];
706 struct ifnet *ifp = &ipr_softc[unit].sc_if;
707 #else
708 struct ipr_softc *sc = ifp->if_softc;
709 #endif
710 bchan_statistics_t bs;
712 /* get # of bytes in and out from the HSCX driver */
714 (*sc->sc_ilt->bchannel_driver->bch_stat)
715 (sc->sc_ilt->l1token, sc->sc_ilt->channel, &bs);
717 sc->sc_ioutb += bs.outbytes;
718 sc->sc_iinb += bs.inbytes;
720 if((sc->sc_iinb != sc->sc_linb) || (sc->sc_ioutb != sc->sc_loutb) || sc->sc_fn)
722 int ri = (sc->sc_iinb - sc->sc_linb)/I4BIPRACCTINTVL;
723 int ro = (sc->sc_ioutb - sc->sc_loutb)/I4BIPRACCTINTVL;
725 if((sc->sc_iinb == sc->sc_linb) && (sc->sc_ioutb == sc->sc_loutb))
726 sc->sc_fn = 0;
727 else
728 sc->sc_fn = 1;
730 sc->sc_linb = sc->sc_iinb;
731 sc->sc_loutb = sc->sc_ioutb;
733 if (sc->sc_cdp)
734 i4b_l4_accounting(sc->sc_cdp->cdid, ACCT_DURING,
735 sc->sc_ioutb, sc->sc_iinb, ro, ri, sc->sc_outb, sc->sc_inb);
737 sc->sc_if.if_timer = I4BIPRACCTINTVL;
738 #ifdef __bsdi__
739 return 0;
740 #endif
742 #endif /* I4BIPRACCT */
744 /*===========================================================================*
745 * ISDN INTERFACE ROUTINES
746 *===========================================================================*/
748 /*---------------------------------------------------------------------------*
749 * start transmitting after connect
750 *---------------------------------------------------------------------------*/
751 static void
752 i4bipr_connect_startio(struct ipr_softc *sc)
754 int s = splnet();
756 if(sc->sc_state == ST_CONNECTED_W)
758 sc->sc_state = ST_CONNECTED_A;
759 ipr_tx_queue_empty(sc);
762 splx(s);
765 /*---------------------------------------------------------------------------*
766 * this routine is called from L4 handler at connect time
767 *---------------------------------------------------------------------------*/
768 static void
769 ipr_connect(void *softc, void *cdp)
771 struct ipr_softc *sc = softc;
772 int s;
774 sc->sc_cdp = (call_desc_t *)cdp;
776 s = splnet();
778 NDBGL4(L4_DIALST, "%s: setting dial state to ST_CONNECTED", sc->sc_if.if_xname);
780 sc->sc_if.if_flags |= IFF_RUNNING;
781 sc->sc_state = ST_CONNECTED_W;
783 sc->sc_dialresp = DSTAT_NONE;
784 sc->sc_lastdialresp = DSTAT_NONE;
786 #if I4BIPRACCT
787 sc->sc_iinb = 0;
788 sc->sc_ioutb = 0;
789 sc->sc_inb = 0;
790 sc->sc_outb = 0;
791 sc->sc_linb = 0;
792 sc->sc_loutb = 0;
793 sc->sc_if.if_timer = I4BIPRACCTINTVL;
794 #endif
796 #ifdef I4BIPRADJFRXP
797 sc->sc_first_pkt = 1;
798 #endif
801 * Sometimes ISDN B-channels are switched thru asymmetic. This
802 * means that under such circumstances B-channel data (the first
803 * three packets of a TCP connection in my case) may get lost,
804 * causing a large delay until the connection is started.
805 * When the sending of the very first packet of a TCP connection
806 * is delayed for a to be empirically determined delay (close
807 * to a second in my case) those packets go thru and the TCP
808 * connection comes up "almost" immediately (-hm).
811 if(sc->sc_cdp->isdntxdelay > 0)
813 int xdelay;
815 if (hz == 100) {
816 xdelay = sc->sc_cdp->isdntxdelay; /* avoid any rounding */
817 } else {
818 xdelay = sc->sc_cdp->isdntxdelay*hz;
819 xdelay /= 100;
822 START_TIMER(sc->sc_callout, (TIMEOUT_FUNC_T)i4bipr_connect_startio, (void *)sc, xdelay);
824 else
826 sc->sc_state = ST_CONNECTED_A;
827 ipr_tx_queue_empty(sc);
830 splx(s);
832 /* we don't need any negotiation - pass event back right now */
833 i4b_l4_negcomplete(sc->sc_cdp);
836 /*---------------------------------------------------------------------------*
837 * this routine is called from L4 handler at disconnect time
838 *---------------------------------------------------------------------------*/
839 static void
840 ipr_disconnect(void *softc, void *cdp)
842 call_desc_t *cd = (call_desc_t *)cdp;
843 struct ipr_softc *sc = softc;
845 /* new stuff to check that the active channel is being closed */
847 if (cd != sc->sc_cdp)
849 NDBGL4(L4_IPRDBG, "%s: channel %d not active",
850 sc->sc_if.if_xname, cd->channelid);
851 return;
854 #if I4BIPRACCT
855 sc->sc_if.if_timer = 0;
856 #endif
857 #if IPR_LOG
858 /* show next IPR_LOG packets again */
859 sc->sc_log_first = IPR_LOG;
860 #endif
862 i4b_l4_accounting(cd->cdid, ACCT_FINAL,
863 sc->sc_ioutb, sc->sc_iinb, 0, 0, sc->sc_outb, sc->sc_inb);
865 sc->sc_cdp = NULL;
867 NDBGL4(L4_DIALST, "setting dial state to ST_IDLE");
869 sc->sc_dialresp = DSTAT_NONE;
870 sc->sc_lastdialresp = DSTAT_NONE;
872 sc->sc_if.if_flags &= ~IFF_RUNNING;
873 sc->sc_state = ST_IDLE;
876 /*---------------------------------------------------------------------------*
877 * this routine is used to give a feedback from userland daemon
878 * in case of dial problems
879 *---------------------------------------------------------------------------*/
880 static void
881 ipr_dialresponse(void *softc, int status, cause_t cause)
883 struct ipr_softc *sc = softc;
884 sc->sc_dialresp = status;
886 NDBGL4(L4_IPRDBG, "%s: last=%d, this=%d",
887 sc->sc_if.if_xname, sc->sc_lastdialresp, sc->sc_dialresp);
889 if(status != DSTAT_NONE)
891 NDBGL4(L4_IPRDBG, "%s: clearing queues", sc->sc_if.if_xname);
892 iripclearqueues(sc);
896 /*---------------------------------------------------------------------------*
897 * interface soft up/down
898 *---------------------------------------------------------------------------*/
899 static void
900 ipr_updown(void *softc, int updown)
902 struct ipr_softc *sc = softc;
903 sc->sc_updown = updown;
906 /*---------------------------------------------------------------------------*
907 * this routine is called from the HSCX interrupt handler
908 * when a new frame (mbuf) has been received and was put on
909 * the rx queue. It is assumed that this routines runs at
910 * appropriate protection level! Keep it short !
911 *---------------------------------------------------------------------------*/
912 static void
913 ipr_rx_data_rdy(void *softc)
915 register struct ipr_softc *sc = softc;
916 register struct mbuf *m;
917 #ifdef IRIP_VJ
918 #ifdef IPR_VJ_USEBUFFER
919 u_char *cp = sc->sc_cbuf;
920 #endif
921 int len, c;
922 #endif
924 if((m = *sc->sc_ilt->rx_mbuf) == NULL)
925 return;
927 m->m_pkthdr.rcvif = &sc->sc_if;
929 m->m_pkthdr.len = m->m_len;
931 #ifdef I4BIPRADJFRXP
934 * The very first packet after the B channel is switched thru
935 * has very often several bytes of random data prepended. This
936 * routine looks where the IP header starts and removes the
937 * the bad data.
940 if(sc->sc_first_pkt)
942 unsigned char *mp = m->m_data;
943 int i;
945 sc->sc_first_pkt = 0;
947 for(i = 0; i < m->m_len; i++, mp++)
949 if( ((*mp & 0xf0) == 0x40) &&
950 ((*mp & 0x0f) >= 0x05) )
952 m->m_data = mp;
953 m->m_pkthdr.len -= i;
954 break;
958 #endif
960 sc->sc_if.if_ipackets++;
961 sc->sc_if.if_ibytes += m->m_pkthdr.len;
963 #ifdef IRIP_VJ
964 if((c = (*(mtod(m, u_char *)) & 0xf0)) != (IPVERSION << 4))
966 /* copy data to buffer */
968 len = m->m_len;
970 #ifdef IPR_VJ_USEBUFFER
971 /* XXX */ m_copydata(m, 0, len, cp);
972 #endif
974 if(c & 0x80)
976 c = TYPE_COMPRESSED_TCP;
978 else if(c == TYPE_UNCOMPRESSED_TCP)
980 #ifdef IPR_VJ_USEBUFFER
981 *cp &= 0x4f; /* XXX */
982 #else
983 *(mtod(m, u_char *)) &= 0x4f;
984 #endif
988 * We've got something that's not an IP packet.
989 * If compression is enabled, try to decompress it.
990 * Otherwise, if `auto-enable' compression is on and
991 * it's a reasonable packet, decompress it and then
992 * enable compression. Otherwise, drop it.
994 if(sc->sc_if.if_flags & IPR_COMPRESS)
996 #ifdef IPR_VJ_USEBUFFER
997 len = sl_uncompress_tcp(&cp,len,(u_int)c,&sc->sc_compr);
998 #else
999 len = sl_uncompress_tcp((u_char **)&m->m_data, len,
1000 (u_int)c, &sc->sc_compr);
1001 #endif
1003 if(len <= 0)
1005 #ifdef DEBUG_IPR_VJ
1006 printf("i4b_ipr, ipr_rx_data_rdy: len <= 0 IPR_COMPRESS!\n");
1007 #endif
1008 goto error;
1011 else if((sc->sc_if.if_flags & IPR_AUTOCOMP) &&
1012 (c == TYPE_UNCOMPRESSED_TCP) && (len >= 40))
1014 #ifdef IPR_VJ_USEBUFFER
1015 len = sl_uncompress_tcp(&cp,len,(u_int)c,&sc->sc_compr);
1016 #else
1017 len = sl_uncompress_tcp((u_char **)&m->m_data, len,
1018 (u_int)c, &sc->sc_compr);
1019 #endif
1021 if(len <= 0)
1023 #ifdef DEBUG_IPR_VJ
1024 printf("i4b_ipr, ipr_rx_data_rdy: len <= 0 IPR_AUTOCOMP!\n");
1025 #endif
1026 goto error;
1029 sc->sc_if.if_flags |= IPR_COMPRESS;
1031 else
1033 #ifdef DEBUG_IPR_VJ
1034 printf("i4b_ipr, ipr_input: invalid ip packet!\n");
1035 #endif
1037 error:
1038 sc->sc_if.if_ierrors++;
1039 sc->sc_if.if_collisions++;
1040 m_freem(m);
1041 return;
1043 #ifdef IPR_VJ_USEBUFFER
1044 /* XXX */ m_copyback(m, 0, len, cp);
1045 #else
1046 m->m_len = m->m_pkthdr.len = len;
1047 #endif
1049 #endif
1051 #if I4BIPRACCT
1052 /* NB. do the accounting after decompression! */
1053 sc->sc_inb += m->m_pkthdr.len;
1054 #endif
1055 #if IPR_LOG
1056 if(sc->sc_log_first > 0)
1058 --(sc->sc_log_first);
1059 i4b_l4_packet_ind(irip_driver_id, sc->sc_unit, 0, m );
1061 #endif
1063 #if NBPFILTER > 0 || NBPF > 0
1064 if(sc->sc_if.if_bpf)
1066 /* prepend the address family as a four byte field */
1067 struct mbuf mm;
1068 u_int af = AF_INET;
1069 mm.m_next = m;
1070 mm.m_len = 4;
1071 mm.m_data = (char *)&af;
1073 #ifdef __FreeBSD__
1074 bpf_mtap(&sc->sc_if, &mm);
1075 #else
1076 bpf_mtap(sc->sc_if.if_bpf, &mm);
1077 #endif
1079 #endif /* NBPFILTER > 0 || NBPF > 0 */
1081 if(IF_QFULL(&ipintrq))
1083 NDBGL4(L4_IPRDBG, "%s: ipintrq full!", sc->sc_if.if_xname);
1085 IF_DROP(&ipintrq);
1086 sc->sc_if.if_ierrors++;
1087 sc->sc_if.if_iqdrops++;
1088 m_freem(m);
1090 else
1092 IF_ENQUEUE(&ipintrq, m);
1093 schednetisr(NETISR_IP);
1097 /*---------------------------------------------------------------------------*
1098 * this routine is called from the HSCX interrupt handler
1099 * when the last frame has been sent out and there is no
1100 * further frame (mbuf) in the tx queue.
1101 *---------------------------------------------------------------------------*/
1102 static void
1103 ipr_tx_queue_empty(void *softc)
1105 register struct ipr_softc *sc = softc;
1106 register struct mbuf *m;
1107 #ifdef IRIP_VJ
1108 struct ip *ip;
1109 #endif
1110 int x = 0;
1112 if(sc->sc_state != ST_CONNECTED_A)
1113 return;
1115 for(;;)
1117 IF_DEQUEUE(&sc->sc_fastq, m);
1118 if(m)
1120 sc->sc_if.if_omcasts++;
1122 else
1124 IFQ_DEQUEUE(&sc->sc_if.if_snd, m);
1125 if(m == NULL)
1126 break;
1129 #if NBPFILTER > 0 || NBPF > 0
1130 if(sc->sc_if.if_bpf)
1132 /* prepend the address family as a four byte field */
1134 struct mbuf mm;
1135 u_int af = AF_INET;
1136 mm.m_next = m;
1137 mm.m_len = 4;
1138 mm.m_data = (char *)&af;
1140 #ifdef __FreeBSD__
1141 bpf_mtap(&sc->sc_if, &mm);
1142 #else
1143 bpf_mtap(sc->sc_if.if_bpf, &mm);
1144 #endif
1146 #endif /* NBPFILTER */
1148 #if I4BIPRACCT
1149 sc->sc_outb += m->m_pkthdr.len; /* size before compression */
1150 #endif
1152 #ifdef IRIP_VJ
1153 if((ip = mtod(m, struct ip *))->ip_p == IPPROTO_TCP)
1155 if(sc->sc_if.if_flags & IPR_COMPRESS)
1157 *mtod(m, u_char *) |= sl_compress_tcp(m, ip,
1158 &sc->sc_compr, 1);
1161 #endif
1162 x = 1;
1164 if(IF_QFULL(sc->sc_ilt->tx_queue))
1166 NDBGL4(L4_IPRDBG, "%s: tx queue full!", sc->sc_if.if_xname);
1167 m_freem(m);
1169 else
1171 IF_ENQUEUE(sc->sc_ilt->tx_queue, m);
1173 sc->sc_if.if_obytes += m->m_pkthdr.len;
1175 sc->sc_if.if_opackets++;
1179 if(x)
1180 (*sc->sc_ilt->bchannel_driver->bch_tx_start)(sc->sc_ilt->l1token, sc->sc_ilt->channel);
1183 /*---------------------------------------------------------------------------*
1184 * this routine is called from the HSCX interrupt handler
1185 * each time a packet is received or transmitted. It should
1186 * be used to implement an activity timeout mechanism.
1187 *---------------------------------------------------------------------------*/
1188 static void
1189 ipr_activity(void *softc, int rxtx)
1191 struct ipr_softc *sc = softc;
1192 sc->sc_cdp->last_active_time = SECOND;
1195 /*---------------------------------------------------------------------------*
1196 * return this drivers linktab address
1197 *---------------------------------------------------------------------------*/
1198 static void*
1199 ipr_get_softc(int unit)
1201 return &ipr_softc[unit];
1204 /*---------------------------------------------------------------------------*
1205 * setup the isdn_linktab for this driver
1206 *---------------------------------------------------------------------------*/
1207 static void
1208 ipr_set_linktab(void *softc, isdn_link_t *ilt)
1210 struct ipr_softc *sc = softc;
1211 sc->sc_ilt = ilt;
1214 /*===========================================================================*/
1216 #endif /* NIRIP > 0 */