2 * Copyright (c) 1997 Joerg Wunsch. All rights reserved.
4 * Copyright (c) 1997, 2000 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
28 *---------------------------------------------------------------------------
30 * i4b_isppp.c - isdn4bsd kernel SyncPPP driver
31 * --------------------------------------------
33 * Uses Serge Vakulenko's sppp backend (originally contributed with
34 * the "cx" driver for Cronyx's HDLC-in-hardware device). This driver
35 * is only the glue between sppp and i4b.
37 * $Id: i4b_isppp.c,v 1.44 2000/08/31 07:07:26 hm Exp $
39 * $FreeBSD: src/sys/i4b/driver/i4b_isppp.c,v 1.7.2.3 2003/02/06 14:50:53 gj Exp $
41 * last edit-date: [Thu Aug 31 09:02:27 2000]
43 *---------------------------------------------------------------------------*/
46 #include "use_i4bisppp.h"
49 # error "You need to define `device sppp <N>' with options ISPPP"
52 #include <sys/param.h>
53 #include <sys/systm.h>
55 #include <sys/socket.h>
56 #include <sys/errno.h>
57 #include <sys/sockio.h>
58 #include <sys/kernel.h>
59 #include <sys/thread2.h>
62 #include <net/if_arp.h>
63 #include <net/if_types.h>
64 #include <net/sppp/if_sppp.h>
69 #include <net/i4b/include/machine/i4b_debug.h>
70 #include <net/i4b/include/machine/i4b_ioctl.h>
72 #include "../include/i4b_global.h"
73 #include "../include/i4b_l3l4.h"
74 #include "../layer4/i4b_l4.h"
76 #define PDEVSTATIC static
78 #define ISPPP_FMT "%s: "
79 #define ISPPP_ARG(sc) ((sc)->sc_if.if_xname)
80 #define IFP2UNIT(ifp) ((struct i4bisppp_softc *)ifp->if_softc)->sc_unit
82 # define IOCTL_CMD_T u_long
84 PDEVSTATIC void i4bispppattach(void *);
85 PSEUDO_SET(i4bispppattach, i4b_isppp);
87 #define I4BISPPPACCT 1 /* enable accounting messages */
88 #define I4BISPPPACCTINTVL 2 /* accounting msg interval in secs */
89 #define I4BISPPPDISCDEBUG 1
91 #define PPP_HDRLEN 4 /* 4 octetts PPP header length */
93 struct i4bisppp_softc {
95 * struct sppp starts with a struct ifnet, but we gotta allocate
96 * more space for it. NB: do not relocate this union, it must
97 * be first in isppp_softc. The tls and tlf hooks below want to
98 * convert a ``struct sppp *'' into a ``struct isppp_softc *''.
104 #define sc_if sc_if_un.scu_if
106 int sc_state; /* state of the interface */
107 int sc_unit; /* unit number */
109 call_desc_t *sc_cdp; /* ptr to call descriptor */
112 int sc_iinb; /* isdn driver # of inbytes */
113 int sc_ioutb; /* isdn driver # of outbytes */
114 int sc_inb; /* # of bytes rx'd */
115 int sc_outb; /* # of bytes tx'd */
116 int sc_linb; /* last # of bytes rx'd */
117 int sc_loutb; /* last # of bytes tx'd */
118 int sc_fn; /* flag, first null acct */
121 struct callout sc_timeout;
123 } i4bisppp_softc[NI4BISPPP];
125 static void i4bisppp_init_linktab(int unit);
126 static int i4bisppp_ioctl(struct ifnet *ifp, IOCTL_CMD_T cmd, caddr_t data,
130 static void i4bisppp_send(struct ifnet *ifp);
133 static void i4bisppp_start(struct ifnet *ifp);
135 #if 0 /* never used ??? */
136 static void i4bisppp_timeout(void *cookie);
139 static void i4bisppp_tls(struct sppp *sp);
140 static void i4bisppp_tlf(struct sppp *sp);
141 static void i4bisppp_state_changed(struct sppp *sp, int new_state);
142 static void i4bisppp_negotiation_complete(struct sppp *sp);
143 static void i4bisppp_watchdog(struct ifnet *ifp);
144 time_t i4bisppp_idletime(int unit);
146 /* initialized by L4 */
148 static drvr_link_t i4bisppp_drvr_linktab[NI4BISPPP];
149 static isdn_link_t *isdn_linktab[NI4BISPPP];
151 enum i4bisppp_states {
152 ST_IDLE, /* initialized, ready, idle */
153 ST_DIALING, /* dialling out to remote */
154 ST_CONNECTED, /* connected to remote */
157 /*===========================================================================*
158 * DEVICE DRIVER ROUTINES
159 *===========================================================================*/
161 /*---------------------------------------------------------------------------*
162 * interface attach routine at kernel boot time
163 *---------------------------------------------------------------------------*/
165 i4bispppattach(void *dummy)
167 struct i4bisppp_softc *sc = i4bisppp_softc;
170 #ifndef HACK_NO_PSEUDO_ATTACH_MSG
172 kprintf("i4bisppp: %d ISDN SyncPPP device(s) attached (VJ header compression)\n", NI4BISPPP);
174 kprintf("i4bisppp: %d ISDN SyncPPP device(s) attached\n", NI4BISPPP);
178 for(i = 0; i < NI4BISPPP; sc++, i++) {
179 i4bisppp_init_linktab(i);
181 sc->sc_if.if_softc = sc;
183 if_initname(&(sc->sc_if), "isp", i);
184 sc->sc_if.if_mtu = PP_MTU;
186 sc->sc_if.if_flags = IFF_SIMPLEX | IFF_POINTOPOINT;
188 sc->sc_if.if_type = IFT_ISDNBASIC;
189 sc->sc_state = ST_IDLE;
191 sc->sc_if.if_ioctl = i4bisppp_ioctl;
193 /* actually initialized by sppp_attach() */
194 /* sc->sc_if.if_output = sppp_output; */
196 sc->sc_if.if_start = i4bisppp_start;
198 sc->sc_if.if_hdrlen = 0;
199 sc->sc_if.if_addrlen = 0;
200 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
202 sc->sc_if.if_ipackets = 0;
203 sc->sc_if.if_ierrors = 0;
204 sc->sc_if.if_opackets = 0;
205 sc->sc_if.if_oerrors = 0;
206 sc->sc_if.if_collisions = 0;
207 sc->sc_if.if_ibytes = 0;
208 sc->sc_if.if_obytes = 0;
209 sc->sc_if.if_imcasts = 0;
210 sc->sc_if.if_omcasts = 0;
211 sc->sc_if.if_iqdrops = 0;
212 sc->sc_if.if_noproto = 0;
215 sc->sc_if.if_timer = 0;
216 sc->sc_if.if_watchdog = i4bisppp_watchdog;
226 sc->sc_if_un.scu_sp.pp_tls = i4bisppp_tls;
227 sc->sc_if_un.scu_sp.pp_tlf = i4bisppp_tlf;
228 sc->sc_if_un.scu_sp.pp_con = i4bisppp_negotiation_complete;
229 sc->sc_if_un.scu_sp.pp_chg = i4bisppp_state_changed;
231 sppp_attach(&sc->sc_if);
232 /* XXX: validate / add proper code */
233 ether_ifattach_bpf(&sc->sc_if, ((struct arpcom*)sc)->ac_enaddr,
234 DLT_PPP, PPP_HDRLEN, NULL);
235 callout_init(&sc->sc_timeout);
239 /*---------------------------------------------------------------------------*
241 *---------------------------------------------------------------------------*/
243 i4bisppp_ioctl(struct ifnet *ifp, IOCTL_CMD_T cmd, caddr_t data,
246 struct i4bisppp_softc *sc = ifp->if_softc;
248 struct sppp *sp = (struct sppp *)sc;
249 struct ifaddr *ifa = (struct ifaddr *) data;
250 struct ifreq *ifr = (struct ifreq *) data;
255 error = sppp_ioctl(&sc->sc_if, cmd, data);
261 #if 0 /* never used ??? */
263 if ((ifp->if_flags & IFF_UP) == 0)
264 callout_stop(&sc->sc_timeout);
273 /*---------------------------------------------------------------------------*
274 * start output to ISDN B-channel
275 *---------------------------------------------------------------------------*/
277 i4bisppp_start(struct ifnet *ifp)
279 struct i4bisppp_softc *sc = ifp->if_softc;
282 int unit = IFP2UNIT(ifp);
284 if (sppp_isempty(ifp))
287 if(sc->sc_state != ST_CONNECTED)
292 * ifp->if_flags |= IFF_OACTIVE; // - need to clear this somewhere
296 while ((m = sppp_dequeue(&sc->sc_if)) != NULL)
300 microtime(&ifp->if_lastchange);
302 if(IF_QFULL(isdn_linktab[unit]->tx_queue))
304 NDBGL4(L4_ISPDBG, "isp%d, tx queue full!", unit);
310 sc->sc_if.if_obytes += m->m_pkthdr.len;
312 sc->sc_outb += m->m_pkthdr.len;
313 sc->sc_if.if_opackets++;
315 IF_ENQUEUE(isdn_linktab[unit]->tx_queue, m);
318 isdn_linktab[unit]->bch_tx_start(isdn_linktab[unit]->unit,
319 isdn_linktab[unit]->channel);
323 /*---------------------------------------------------------------------------*
325 *---------------------------------------------------------------------------*/
327 i4bisppp_watchdog(struct ifnet *ifp)
329 struct i4bisppp_softc *sc = ifp->if_softc;
330 int unit = IFP2UNIT(ifp);
331 bchan_statistics_t bs;
333 (*isdn_linktab[unit]->bch_stat)
334 (isdn_linktab[unit]->unit, isdn_linktab[unit]->channel, &bs);
336 sc->sc_ioutb += bs.outbytes;
337 sc->sc_iinb += bs.inbytes;
339 if((sc->sc_iinb != sc->sc_linb) || (sc->sc_ioutb != sc->sc_loutb) || sc->sc_fn)
341 int ri = (sc->sc_iinb - sc->sc_linb)/I4BISPPPACCTINTVL;
342 int ro = (sc->sc_ioutb - sc->sc_loutb)/I4BISPPPACCTINTVL;
344 if((sc->sc_iinb == sc->sc_linb) && (sc->sc_ioutb == sc->sc_loutb))
349 sc->sc_linb = sc->sc_iinb;
350 sc->sc_loutb = sc->sc_ioutb;
352 i4b_l4_accounting(BDRV_ISPPP, unit, ACCT_DURING,
353 sc->sc_ioutb, sc->sc_iinb, ro, ri, sc->sc_outb, sc->sc_inb);
355 sc->sc_if.if_timer = I4BISPPPACCTINTVL;
357 #if 0 /* old stuff, keep it around */
358 kprintf(ISPPP_FMT "transmit timeout\n", ISPPP_ARG(sc));
362 #endif /* I4BISPPPACCT */
365 *===========================================================================*
366 * SyncPPP layer interface routines
367 *===========================================================================*
370 #if 0 /* never used ??? */
371 /*---------------------------------------------------------------------------*
372 * just an alias for i4bisppp_tls, but of type timeout_t
373 *---------------------------------------------------------------------------*/
375 i4bisppp_timeout(void *cookie)
377 i4bisppp_tls((struct sppp *)cookie);
381 /*---------------------------------------------------------------------------*
382 * PPP this-layer-started action
383 *---------------------------------------------------------------------------*
386 i4bisppp_tls(struct sppp *sp)
388 struct i4bisppp_softc *sc = (struct i4bisppp_softc *)sp;
389 struct ifnet *ifp = (struct ifnet *)sp;
391 if(sc->sc_state == ST_CONNECTED)
394 i4b_l4_dialout(BDRV_ISPPP, IFP2UNIT(ifp));
397 /*---------------------------------------------------------------------------*
398 * PPP this-layer-finished action
399 *---------------------------------------------------------------------------*
402 i4bisppp_tlf(struct sppp *sp)
404 struct i4bisppp_softc *sc = (struct i4bisppp_softc *)sp;
405 /* call_desc_t *cd = sc->sc_cdp; */
406 struct ifnet *ifp = (struct ifnet *)sp;
408 if(sc->sc_state != ST_CONNECTED)
411 #if 0 /* never used ??? */
412 callout_stop(&sc->sc_timeout);
415 i4b_l4_drvrdisc(BDRV_ISPPP, IFP2UNIT(ifp));
417 /*---------------------------------------------------------------------------*
418 * PPP interface phase change
419 *---------------------------------------------------------------------------*
422 i4bisppp_state_changed(struct sppp *sp, int new_state)
424 struct i4bisppp_softc *sc = (struct i4bisppp_softc *)sp;
426 i4b_l4_ifstate_changed(sc->sc_cdp, new_state);
429 /*---------------------------------------------------------------------------*
430 * PPP control protocol negotiation complete (run ip-up script now)
431 *---------------------------------------------------------------------------*
434 i4bisppp_negotiation_complete(struct sppp *sp)
436 struct i4bisppp_softc *sc = (struct i4bisppp_softc *)sp;
438 i4b_l4_negcomplete(sc->sc_cdp);
441 /*===========================================================================*
442 * ISDN INTERFACE ROUTINES
443 *===========================================================================*/
445 /*---------------------------------------------------------------------------*
446 * this routine is called from L4 handler at connect time
447 *---------------------------------------------------------------------------*/
449 i4bisppp_connect(int unit, void *cdp)
451 struct i4bisppp_softc *sc = &i4bisppp_softc[unit];
452 struct sppp *sp = &sc->sc_if_un.scu_sp;
456 sc->sc_cdp = (call_desc_t *)cdp;
457 sc->sc_state = ST_CONNECTED;
466 sc->sc_if.if_timer = I4BISPPPACCTINTVL;
469 #if 0 /* never used ??? */
470 callout_stop(&sc->sc_timeout);
473 sp->pp_up(sp); /* tell PPP we are ready */
474 sp->pp_last_sent = sp->pp_last_recv = SECOND;
479 /*---------------------------------------------------------------------------*
480 * this routine is called from L4 handler at disconnect time
481 *---------------------------------------------------------------------------*/
483 i4bisppp_disconnect(int unit, void *cdp)
485 call_desc_t *cd = (call_desc_t *)cdp;
486 struct i4bisppp_softc *sc = &i4bisppp_softc[unit];
487 struct sppp *sp = &sc->sc_if_un.scu_sp;
491 /* new stuff to check that the active channel is being closed */
492 if (cd != sc->sc_cdp)
494 NDBGL4(L4_ISPDBG, "isp%d, channel%d not active!", unit, cd->channelid);
500 sc->sc_if.if_timer = 0;
503 i4b_l4_accounting(BDRV_ISPPP, unit, ACCT_FINAL,
504 sc->sc_ioutb, sc->sc_iinb, 0, 0, sc->sc_outb, sc->sc_inb);
506 if (sc->sc_state == ST_CONNECTED)
508 #if 0 /* never used ??? */
509 callout_stop(&sc->sc_timeout);
512 /* do this here because pp_down calls i4bisppp_tlf */
513 sc->sc_state = ST_IDLE;
514 sp->pp_down(sp); /* tell PPP we have hung up */
520 /*---------------------------------------------------------------------------*
521 * this routine is used to give a feedback from userland demon
522 * in case of dial problems
523 *---------------------------------------------------------------------------*/
525 i4bisppp_dialresponse(int unit, int status, cause_t cause)
527 struct i4bisppp_softc *sc = &i4bisppp_softc[unit];
528 struct sppp *sp = &sc->sc_if_un.scu_sp;
530 NDBGL4(L4_ISPDBG, "isp%d: status=%d, cause=%d", unit, status, cause);
532 if(status != DSTAT_NONE)
536 NDBGL4(L4_ISPDBG, "isp%d: clearing queues", unit);
538 if(!(sppp_isempty(&sc->sc_if)))
540 while((m = sppp_dequeue(&sc->sc_if)) != NULL)
545 /* do this here because pp_down calls i4bisppp_tlf */
546 sc->sc_state = ST_IDLE;
548 * Ahh, sppp doesn't like to get a down event when
549 * dialing fails. So first tell it that we are up
550 * (doesn't hurt us since sc_state != ST_CONNECTED)
558 /*---------------------------------------------------------------------------*
560 *---------------------------------------------------------------------------*/
562 i4bisppp_updown(int unit, int updown)
564 /* could probably do something useful here */
567 /*---------------------------------------------------------------------------*
568 * this routine is called from the HSCX interrupt handler
569 * when a new frame (mbuf) has been received and was put on
571 *---------------------------------------------------------------------------*/
573 i4bisppp_rx_data_rdy(int unit)
575 struct i4bisppp_softc *sc = &i4bisppp_softc[unit];
578 if((m = *isdn_linktab[unit]->rx_mbuf) == NULL)
581 m->m_pkthdr.rcvif = &sc->sc_if;
582 m->m_pkthdr.len = m->m_len;
584 microtime(&sc->sc_if.if_lastchange);
586 sc->sc_if.if_ipackets++;
588 sc->sc_if.if_ibytes += m->m_pkthdr.len;
592 sc->sc_inb += m->m_pkthdr.len;
596 kprintf("i4bisppp_rx_data_ready: received packet!\n");
599 BPF_MTAP(&sc->sc_if, m);
602 sppp_input(&sc->sc_if, m);
606 /*---------------------------------------------------------------------------*
607 * this routine is called from the HSCX interrupt handler
608 * when the last frame has been sent out and there is no
609 * further frame (mbuf) in the tx queue.
610 *---------------------------------------------------------------------------*/
612 i4bisppp_tx_queue_empty(int unit)
614 i4bisppp_start(&i4bisppp_softc[unit].sc_if);
617 /*---------------------------------------------------------------------------*
618 * THIS should be used instead of last_active_time to implement
619 * an activity timeout mechanism.
621 * Sending back the time difference unneccessarily complicates the
622 * idletime checks in i4b_l4.c. Return the largest time instead.
623 * That way the code in i4b_l4.c needs only minimal changes.
624 *---------------------------------------------------------------------------*/
626 i4bisppp_idletime(int unit)
629 sp = (struct sppp *) &i4bisppp_softc[unit];
631 return((sp->pp_last_recv < sp->pp_last_sent) ?
632 sp->pp_last_sent : sp->pp_last_recv);
635 /*---------------------------------------------------------------------------*
636 * this routine is called from the HSCX interrupt handler
637 * each time a packet is received or transmitted. It should
638 * be used to implement an activity timeout mechanism.
639 *---------------------------------------------------------------------------*/
641 i4bisppp_activity(int unit, int rxtx)
643 i4bisppp_softc[unit].sc_cdp->last_active_time = SECOND;
646 /*---------------------------------------------------------------------------*
647 * return this drivers linktab address
648 *---------------------------------------------------------------------------*/
650 i4bisppp_ret_linktab(int unit)
652 return(&i4bisppp_drvr_linktab[unit]);
655 /*---------------------------------------------------------------------------*
656 * setup the isdn_linktab for this driver
657 *---------------------------------------------------------------------------*/
659 i4bisppp_set_linktab(int unit, isdn_link_t *ilt)
661 isdn_linktab[unit] = ilt;
664 /*---------------------------------------------------------------------------*
665 * initialize this drivers linktab
666 *---------------------------------------------------------------------------*/
668 i4bisppp_init_linktab(int unit)
670 i4bisppp_drvr_linktab[unit].unit = unit;
671 i4bisppp_drvr_linktab[unit].bch_rx_data_ready = i4bisppp_rx_data_rdy;
672 i4bisppp_drvr_linktab[unit].bch_tx_queue_empty = i4bisppp_tx_queue_empty;
673 i4bisppp_drvr_linktab[unit].bch_activity = i4bisppp_activity;
674 i4bisppp_drvr_linktab[unit].line_connected = i4bisppp_connect;
675 i4bisppp_drvr_linktab[unit].line_disconnected = i4bisppp_disconnect;
676 i4bisppp_drvr_linktab[unit].dial_response = i4bisppp_dialresponse;
677 i4bisppp_drvr_linktab[unit].updown_ind = i4bisppp_updown;
680 /*===========================================================================*/