2 * Copyright (c) 1997 Joerg Wunsch. All rights reserved.
4 * Copyright (c) 1997, 2002 Hellmuth Michaelis. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 /*---------------------------------------------------------------------------
31 * i4b_isppp.c - isdn4bsd kernel SyncPPP driver
32 * --------------------------------------------
34 * Uses Serge Vakulenko's sppp backend (originally contributed with
35 * the "cx" driver for Cronyx's HDLC-in-hardware device). This driver
36 * is only the glue between sppp and i4b.
38 * last edit-date: [Sat Mar 9 14:09:27 2002]
40 *---------------------------------------------------------------------------*/
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
48 #include <sys/systm.h>
50 #include <sys/socket.h>
51 #include <sys/errno.h>
52 #include <sys/ioccom.h>
53 #include <sys/sockio.h>
54 #include <sys/kernel.h>
57 #include <net/if_types.h>
58 #include <net/if_sppp.h>
63 #include <i4b/include/i4b_ioctl.h>
64 #include <i4b/include/i4b_debug.h>
66 #include <i4b/include/i4b_global.h>
67 #include <i4b/include/i4b_l3l4.h>
69 #include <i4b/layer4/i4b_l4.h>
71 #error "Cannot be used until I4B is locked."
73 #define ISPPP_FMT "isp%d: "
74 #define ISPPP_ARG(sc) (sc->sc_ifp->if_dunit)
75 #define PDEVSTATIC static
76 #define IFP2UNIT(ifp) (ifp)->if_dunit
78 # define CALLOUT_INIT(chan) callout_handle_init(chan)
79 # define TIMEOUT(fun, arg, chan, tick) chan = timeout(fun, arg, tick)
80 # define UNTIMEOUT(fun, arg, chan) untimeout(fun, arg, chan)
81 # define IOCTL_CMD_T u_long
83 PDEVSTATIC void i4bispppattach(void *);
84 PSEUDO_SET(i4bispppattach, i4b_isppp);
86 #define I4BISPPPACCT 1 /* enable accounting messages */
87 #define I4BISPPPACCTINTVL 2 /* accounting msg interval in secs */
88 #define I4BISPPPDISCDEBUG 1
90 #define PPP_HDRLEN 4 /* 4 octetts PPP header length */
92 struct i4bisppp_softc {
95 int sc_state; /* state of the interface */
96 call_desc_t *sc_cdp; /* ptr to call descriptor */
99 int sc_iinb; /* isdn driver # of inbytes */
100 int sc_ioutb; /* isdn driver # of outbytes */
101 int sc_inb; /* # of bytes rx'd */
102 int sc_outb; /* # of bytes tx'd */
103 int sc_linb; /* last # of bytes rx'd */
104 int sc_loutb; /* last # of bytes tx'd */
105 int sc_fn; /* flag, first null acct */
108 struct callout_handle sc_ch;
109 } i4bisppp_softc[NI4BISPPP];
111 static void i4bisppp_init_linktab(int unit);
112 static int i4bisppp_ioctl(struct ifnet *ifp, IOCTL_CMD_T cmd, caddr_t data);
115 static void i4bisppp_send(struct ifnet *ifp);
118 static void i4bisppp_start(struct ifnet *ifp);
120 #if 0 /* never used ??? */
121 static void i4bisppp_timeout(void *cookie);
124 static void i4bisppp_tls(struct sppp *sp);
125 static void i4bisppp_tlf(struct sppp *sp);
126 static void i4bisppp_state_changed(struct sppp *sp, int new_state);
127 static void i4bisppp_negotiation_complete(struct sppp *sp);
128 static void i4bisppp_watchdog(struct ifnet *ifp);
129 time_t i4bisppp_idletime(int unit);
131 /* initialized by L4 */
133 static drvr_link_t i4bisppp_drvr_linktab[NI4BISPPP];
134 static isdn_link_t *isdn_linktab[NI4BISPPP];
136 enum i4bisppp_states {
137 ST_IDLE, /* initialized, ready, idle */
138 ST_DIALING, /* dialling out to remote */
139 ST_CONNECTED, /* connected to remote */
142 /*===========================================================================*
143 * DEVICE DRIVER ROUTINES
144 *===========================================================================*/
146 /*---------------------------------------------------------------------------*
147 * interface attach routine at kernel boot time
148 *---------------------------------------------------------------------------*/
150 i4bispppattach(void *dummy)
152 struct i4bisppp_softc *sc = i4bisppp_softc;
157 printf("i4bisppp: %d ISDN SyncPPP device(s) attached (VJ header compression)\n", NI4BISPPP);
159 printf("i4bisppp: %d ISDN SyncPPP device(s) attached\n", NI4BISPPP);
162 for(i = 0; i < NI4BISPPP; sc++, i++) {
163 i4bisppp_init_linktab(i);
164 ifp = sc->sc_ifp = if_alloc(IFT_PPP);
166 panic("isp%d: cannot if_alloc()", i);
170 if_initname(sc->sc_ifp, "isp", i);
171 ifp->if_mtu = PP_MTU;
172 ifp->if_flags = IFF_SIMPLEX | IFF_POINTOPOINT;
174 * XXX: If there were a detach function this would
175 * require that it use if_free_type(). Not sure if this
178 ifp->if_type = IFT_ISDNBASIC;
179 sc->sc_state = ST_IDLE;
181 ifp->if_ioctl = i4bisppp_ioctl;
183 /* actually initialized by sppp_attach() */
184 /* ifp->if_output = sppp_output; */
186 ifp->if_start = i4bisppp_start;
190 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
192 ifp->if_ipackets = 0;
194 ifp->if_opackets = 0;
196 ifp->if_collisions = 0;
206 ifp->if_watchdog = i4bisppp_watchdog;
216 IFP2SP(sc->sc_ifp)->pp_tls = i4bisppp_tls;
217 IFP2SP(sc->sc_ifp)->pp_tlf = i4bisppp_tlf;
218 IFP2SP(sc->sc_ifp)->pp_con = i4bisppp_negotiation_complete;
219 IFP2SP(sc->sc_ifp)->pp_chg = i4bisppp_state_changed;
221 sppp_attach(sc->sc_ifp);
223 if_attach(sc->sc_ifp);
225 CALLOUT_INIT(&sc->sc_ch);
227 bpfattach(sc->sc_ifp, DLT_PPP, PPP_HDRLEN);
231 /*---------------------------------------------------------------------------*
233 *---------------------------------------------------------------------------*/
235 i4bisppp_ioctl(struct ifnet *ifp, IOCTL_CMD_T cmd, caddr_t data)
237 struct i4bisppp_softc *sc = ifp->if_softc;
239 struct sppp *sp = IFP2SP(sc->sc_ifp);
240 struct ifaddr *ifa = (struct ifaddr *) data;
241 struct ifreq *ifr = (struct ifreq *) data;
246 error = sppp_ioctl(sc->sc_ifp, cmd, data);
252 #if 0 /* never used ??? */
254 if ((ifp->if_flags & IFF_UP) == 0)
255 UNTIMEOUT(i4bisppp_timeout, (void *)sp, sc->sc_ch);
264 /*---------------------------------------------------------------------------*
265 * start output to ISDN B-channel
266 *---------------------------------------------------------------------------*/
268 i4bisppp_start(struct ifnet *ifp)
270 struct i4bisppp_softc *sc = ifp->if_softc;
273 int unit = IFP2UNIT(ifp);
275 if (sppp_isempty(ifp))
278 if(sc->sc_state != ST_CONNECTED)
283 * ifp->if_drv_flags |= IFF_DRV_OACTIVE; // - need to clear this somewhere
287 while ((m = sppp_dequeue(sc->sc_ifp)) != NULL)
292 microtime(&ifp->if_lastchange);
294 IF_LOCK(isdn_linktab[unit]->tx_queue);
295 if(_IF_QFULL(isdn_linktab[unit]->tx_queue))
297 NDBGL4(L4_ISPDBG, "isp%d, tx queue full!", unit);
303 sc->sc_ifp->if_obytes += m->m_pkthdr.len;
305 sc->sc_outb += m->m_pkthdr.len;
306 sc->sc_ifp->if_opackets++;
308 _IF_ENQUEUE(isdn_linktab[unit]->tx_queue, m);
310 IF_UNLOCK(isdn_linktab[unit]->tx_queue);
312 isdn_linktab[unit]->bch_tx_start(isdn_linktab[unit]->unit,
313 isdn_linktab[unit]->channel);
317 /*---------------------------------------------------------------------------*
319 *---------------------------------------------------------------------------*/
321 i4bisppp_watchdog(struct ifnet *ifp)
323 struct i4bisppp_softc *sc = ifp->if_softc;
324 int unit = IFP2UNIT(ifp);
325 bchan_statistics_t bs;
327 (*isdn_linktab[unit]->bch_stat)
328 (isdn_linktab[unit]->unit, isdn_linktab[unit]->channel, &bs);
330 sc->sc_ioutb += bs.outbytes;
331 sc->sc_iinb += bs.inbytes;
333 if((sc->sc_iinb != sc->sc_linb) || (sc->sc_ioutb != sc->sc_loutb) || sc->sc_fn)
335 int ri = (sc->sc_iinb - sc->sc_linb)/I4BISPPPACCTINTVL;
336 int ro = (sc->sc_ioutb - sc->sc_loutb)/I4BISPPPACCTINTVL;
338 if((sc->sc_iinb == sc->sc_linb) && (sc->sc_ioutb == sc->sc_loutb))
343 sc->sc_linb = sc->sc_iinb;
344 sc->sc_loutb = sc->sc_ioutb;
346 i4b_l4_accounting(BDRV_ISPPP, unit, ACCT_DURING,
347 sc->sc_ioutb, sc->sc_iinb, ro, ri, sc->sc_outb, sc->sc_inb);
349 sc->sc_ifp->if_timer = I4BISPPPACCTINTVL;
351 #if 0 /* old stuff, keep it around */
352 printf(ISPPP_FMT "transmit timeout\n", ISPPP_ARG(sc));
356 #endif /* I4BISPPPACCT */
359 *===========================================================================*
360 * SyncPPP layer interface routines
361 *===========================================================================*
364 #if 0 /* never used ??? */
365 /*---------------------------------------------------------------------------*
366 * just an alias for i4bisppp_tls, but of type timeout_t
367 *---------------------------------------------------------------------------*/
369 i4bisppp_timeout(void *cookie)
371 i4bisppp_tls((struct sppp *)cookie);
375 /*---------------------------------------------------------------------------*
376 * PPP this-layer-started action
377 *---------------------------------------------------------------------------*
380 i4bisppp_tls(struct sppp *sp)
382 struct ifnet *ifp = SP2IFP(sp);
383 struct i4bisppp_softc *sc = ifp->if_softc;
385 if(sc->sc_state == ST_CONNECTED)
388 i4b_l4_dialout(BDRV_ISPPP, IFP2UNIT(ifp));
391 /*---------------------------------------------------------------------------*
392 * PPP this-layer-finished action
393 *---------------------------------------------------------------------------*
396 i4bisppp_tlf(struct sppp *sp)
398 struct ifnet *ifp = SP2IFP(sp);
399 struct i4bisppp_softc *sc = ifp->if_softc;
400 /* call_desc_t *cd = sc->sc_cdp; */
402 if(sc->sc_state != ST_CONNECTED)
405 #if 0 /* never used ??? */
406 UNTIMEOUT(i4bisppp_timeout, (void *)sp, sc->sc_ch);
409 i4b_l4_drvrdisc(BDRV_ISPPP, IFP2UNIT(ifp));
411 /*---------------------------------------------------------------------------*
412 * PPP interface phase change
413 *---------------------------------------------------------------------------*
416 i4bisppp_state_changed(struct sppp *sp, int new_state)
418 struct i4bisppp_softc *sc = SP2IFP(sp)->if_softc;
420 i4b_l4_ifstate_changed(sc->sc_cdp, new_state);
423 /*---------------------------------------------------------------------------*
424 * PPP control protocol negotiation complete (run ip-up script now)
425 *---------------------------------------------------------------------------*
428 i4bisppp_negotiation_complete(struct sppp *sp)
430 struct i4bisppp_softc *sc = SP2IFP(sp)->if_softc;
432 i4b_l4_negcomplete(sc->sc_cdp);
435 /*===========================================================================*
436 * ISDN INTERFACE ROUTINES
437 *===========================================================================*/
439 /*---------------------------------------------------------------------------*
440 * this routine is called from L4 handler at connect time
441 *---------------------------------------------------------------------------*/
443 i4bisppp_connect(int unit, void *cdp)
445 struct i4bisppp_softc *sc = &i4bisppp_softc[unit];
446 struct sppp *sp = IFP2SP(sc->sc_ifp);
449 sc->sc_cdp = (call_desc_t *)cdp;
450 sc->sc_state = ST_CONNECTED;
459 sc->sc_ifp->if_timer = I4BISPPPACCTINTVL;
462 #if 0 /* never used ??? */
463 UNTIMEOUT(i4bisppp_timeout, (void *)sp, sc->sc_ch);
466 sp->pp_up(sp); /* tell PPP we are ready */
468 sp->pp_last_sent = sp->pp_last_recv = SECOND;
473 /*---------------------------------------------------------------------------*
474 * this routine is called from L4 handler at disconnect time
475 *---------------------------------------------------------------------------*/
477 i4bisppp_disconnect(int unit, void *cdp)
479 call_desc_t *cd = (call_desc_t *)cdp;
480 struct i4bisppp_softc *sc = &i4bisppp_softc[unit];
481 struct sppp *sp = IFP2SP(sc->sc_ifp);
485 /* new stuff to check that the active channel is being closed */
486 if (cd != sc->sc_cdp)
488 NDBGL4(L4_ISPDBG, "isp%d, channel%d not active!", unit, cd->channelid);
494 sc->sc_ifp->if_timer = 0;
497 i4b_l4_accounting(BDRV_ISPPP, unit, ACCT_FINAL,
498 sc->sc_ioutb, sc->sc_iinb, 0, 0, sc->sc_outb, sc->sc_inb);
500 if (sc->sc_state == ST_CONNECTED)
502 #if 0 /* never used ??? */
503 UNTIMEOUT(i4bisppp_timeout, (void *)sp, sc->sc_ch);
505 sc->sc_cdp = (call_desc_t *)0;
506 /* do this here because pp_down calls i4bisppp_tlf */
507 sc->sc_state = ST_IDLE;
508 sp->pp_down(sp); /* tell PPP we have hung up */
514 /*---------------------------------------------------------------------------*
515 * this routine is used to give a feedback from userland demon
516 * in case of dial problems
517 *---------------------------------------------------------------------------*/
519 i4bisppp_dialresponse(int unit, int status, cause_t cause)
521 struct i4bisppp_softc *sc = &i4bisppp_softc[unit];
522 struct sppp *sp = IFP2SP(sc->sc_ifp);
524 NDBGL4(L4_ISPDBG, "isp%d: status=%d, cause=%d", unit, status, cause);
526 if(status != DSTAT_NONE)
530 NDBGL4(L4_ISPDBG, "isp%d: clearing queues", unit);
532 if(!(sppp_isempty(sc->sc_ifp)))
534 while((m = sppp_dequeue(sc->sc_ifp)) != NULL)
538 sc->sc_cdp = (call_desc_t *)0;
539 /* do this here because pp_down calls i4bisppp_tlf */
540 sc->sc_state = ST_IDLE;
543 * Ahh, sppp doesn't like to get a down event when
544 * dialing fails. So first tell it that we are up
545 * (doesn't hurt us since sc_state != ST_CONNECTED)
553 /*---------------------------------------------------------------------------*
555 *---------------------------------------------------------------------------*/
557 i4bisppp_updown(int unit, int updown)
559 /* could probably do something useful here */
562 /*---------------------------------------------------------------------------*
563 * this routine is called from the HSCX interrupt handler
564 * when a new frame (mbuf) has been received and was put on
566 *---------------------------------------------------------------------------*/
568 i4bisppp_rx_data_rdy(int unit)
570 struct i4bisppp_softc *sc = &i4bisppp_softc[unit];
574 if((m = *isdn_linktab[unit]->rx_mbuf) == NULL)
577 m->m_pkthdr.rcvif = sc->sc_ifp;
578 m->m_pkthdr.len = m->m_len;
580 microtime(&sc->sc_ifp->if_lastchange);
582 sc->sc_ifp->if_ipackets++;
584 sc->sc_ifp->if_ibytes += m->m_pkthdr.len;
588 sc->sc_inb += m->m_pkthdr.len;
592 printf("i4bisppp_rx_data_ready: received packet!\n");
595 BPF_MTAP(sc->sc_ifp, m);
599 sppp_input(sc->sc_ifp, m);
604 /*---------------------------------------------------------------------------*
605 * this routine is called from the HSCX interrupt handler
606 * when the last frame has been sent out and there is no
607 * further frame (mbuf) in the tx queue.
608 *---------------------------------------------------------------------------*/
610 i4bisppp_tx_queue_empty(int unit)
612 i4bisppp_start((&i4bisppp_softc[unit])->sc_ifp);
615 /*---------------------------------------------------------------------------*
616 * THIS should be used instead of last_active_time to implement
617 * an activity timeout mechanism.
619 * Sending back the time difference unneccessarily complicates the
620 * idletime checks in i4b_l4.c. Return the largest time instead.
621 * That way the code in i4b_l4.c needs only minimal changes.
622 *---------------------------------------------------------------------------*/
624 i4bisppp_idletime(int unit)
626 struct sppp *sp = IFP2SP((&i4bisppp_softc[unit])->sc_ifp);
628 return((sp->pp_last_recv < sp->pp_last_sent) ?
629 sp->pp_last_sent : sp->pp_last_recv);
632 /*---------------------------------------------------------------------------*
633 * this routine is called from the HSCX interrupt handler
634 * each time a packet is received or transmitted. It should
635 * be used to implement an activity timeout mechanism.
636 *---------------------------------------------------------------------------*/
638 i4bisppp_activity(int unit, int rxtx)
640 i4bisppp_softc[unit].sc_cdp->last_active_time = SECOND;
643 /*---------------------------------------------------------------------------*
644 * return this drivers linktab address
645 *---------------------------------------------------------------------------*/
647 i4bisppp_ret_linktab(int unit)
649 return(&i4bisppp_drvr_linktab[unit]);
652 /*---------------------------------------------------------------------------*
653 * setup the isdn_linktab for this driver
654 *---------------------------------------------------------------------------*/
656 i4bisppp_set_linktab(int unit, isdn_link_t *ilt)
658 isdn_linktab[unit] = ilt;
661 /*---------------------------------------------------------------------------*
662 * initialize this drivers linktab
663 *---------------------------------------------------------------------------*/
665 i4bisppp_init_linktab(int unit)
667 i4bisppp_drvr_linktab[unit].unit = unit;
668 i4bisppp_drvr_linktab[unit].bch_rx_data_ready = i4bisppp_rx_data_rdy;
669 i4bisppp_drvr_linktab[unit].bch_tx_queue_empty = i4bisppp_tx_queue_empty;
670 i4bisppp_drvr_linktab[unit].bch_activity = i4bisppp_activity;
671 i4bisppp_drvr_linktab[unit].line_connected = i4bisppp_connect;
672 i4bisppp_drvr_linktab[unit].line_disconnected = i4bisppp_disconnect;
673 i4bisppp_drvr_linktab[unit].dial_response = i4bisppp_dialresponse;
674 i4bisppp_drvr_linktab[unit].updown_ind = i4bisppp_updown;
677 /*===========================================================================*/