2 * ppp.c - STREAMS multiplexing pseudo-device driver for PPP.
4 * Copyright (c) 1994 Paul Mackerras. 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.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * 3. The name(s) of the authors of this software must not be used to
19 * endorse or promote products derived from this software without
20 * prior written permission.
22 * 4. Redistributions of any form whatsoever must retain the following
24 * "This product includes software developed by Paul Mackerras
25 * <paulus@samba.org>".
27 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
28 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
29 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
30 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
31 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
32 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
33 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
35 * $Id: ppp.c,v 1.4 2005/06/27 00:59:57 carlsonj Exp $
39 * This file is used under Solaris 2, SVR4, SunOS 4, and Digital UNIX.
42 #include <sys/types.h>
43 #include <sys/param.h>
45 #include <sys/stream.h>
46 #include <sys/stropts.h>
47 #include <sys/errno.h>
49 #include <sys/ioctl.h>
50 #include <sys/cmn_err.h>
51 #define queclass(mp) ((mp)->b_band & QPCTL)
53 #include <sys/ioccom.h>
57 #include <sys/cmn_err.h>
62 #include <sys/ksynch.h>
63 #include <sys/kstat.h>
64 #include <sys/sunddi.h>
65 #include <sys/ethernet.h>
67 #include <sys/socket.h>
68 #include <sys/sockio.h>
70 #include <netinet/in.h>
75 #include <net/ppp_defs.h>
76 #include <net/pppio.h>
80 * Modifications marked with #ifdef PRIOQ are for priority queueing of
81 * interactive traffic, and are due to Marko Zec <zec@japa.tel.fer.hr>.
86 #include <netinet/in.h> /* leave this outside of PRIOQ for htons */
95 * The IP module may use this SAP value for IP packets.
98 #define ETHERTYPE_IP 0x800
101 #if !defined(ETHERTYPE_IPV6)
102 #define ETHERTYPE_IPV6 0x86dd
103 #endif /* !defined(ETHERTYPE_IPV6) */
105 #if !defined(ETHERTYPE_ALLSAP) && defined(SOL2)
106 #define ETHERTYPE_ALLSAP 0
107 #endif /* !defined(ETHERTYPE_ALLSAP) && defined(SOL2) */
109 #if !defined(PPP_ALLSAP) && defined(SOL2)
110 #define PPP_ALLSAP PPP_ALLSTATIONS
111 #endif /* !defined(PPP_ALLSAP) && defined(SOL2) */
117 * We use this reader-writer lock to ensure that the lower streams
118 * stay connected to the upper streams while the lower-side put and
119 * service procedures are running. Essentially it is an existence
120 * lock for the upper stream associated with each lower stream.
122 krwlock_t ppp_lower_lock;
123 #define LOCK_LOWER_W rw_enter(&ppp_lower_lock, RW_WRITER)
124 #define LOCK_LOWER_R rw_enter(&ppp_lower_lock, RW_READER)
125 #define TRYLOCK_LOWER_R rw_tryenter(&ppp_lower_lock, RW_READER)
126 #define UNLOCK_LOWER rw_exit(&ppp_lower_lock)
128 #define MT_ENTER(x) mutex_enter(x)
129 #define MT_EXIT(x) mutex_exit(x)
132 * Notes on multithreaded implementation for Solaris 2:
134 * We use an inner perimeter around each queue pair and an outer
135 * perimeter around the whole driver. The inner perimeter is
136 * entered exclusively for all entry points (open, close, put,
137 * service). The outer perimeter is entered exclusively for open
138 * and close and shared for put and service. This is all done for
139 * us by the streams framework.
141 * I used to think that the perimeters were entered for the lower
142 * streams' put and service routines as well as for the upper streams'.
143 * Because of problems experienced by people, and after reading the
144 * documentation more closely, I now don't think that is true. So we
145 * now use ppp_lower_lock to give us an existence guarantee on the
146 * upper stream controlling each lower stream.
148 * Shared entry to the outer perimeter protects the existence of all
149 * the upper streams and their upperstr_t structures, and guarantees
150 * that the following fields of any upperstr_t won't change:
151 * nextmn, next, nextppa. It guarantees that the lowerq field of an
152 * upperstr_t won't go from non-zero to zero, that the global `ppas'
153 * won't change and that the no lower stream will get unlinked.
155 * Shared (reader) access to ppa_lower_lock guarantees that no lower
156 * stream will be unlinked and that the lowerq field of all upperstr_t
157 * structures won't change.
161 #define LOCK_LOWER_W 0
162 #define LOCK_LOWER_R 0
163 #define TRYLOCK_LOWER_R 1
164 #define UNLOCK_LOWER 0
165 #define MT_ENTER(x) 0
171 * Private information; one per upper stream.
173 typedef struct upperstr {
174 minor_t mn; /* minor device number */
175 struct upperstr *nextmn; /* next minor device */
176 queue_t *q; /* read q associated with this upper stream */
177 int flags; /* flag bits, see below */
178 int state; /* current DLPI state */
179 int sap; /* service access point */
180 int req_sap; /* which SAP the DLPI client requested */
181 struct upperstr *ppa; /* control stream for our ppa */
182 struct upperstr *next; /* next stream for this ppa */
183 uint ioc_id; /* last ioctl ID for this stream */
184 enum NPmode npmode; /* what to do with packets on this SAP */
185 unsigned char rblocked; /* flow control has blocked upper read strm */
186 /* N.B. rblocked is only changed by control stream's put/srv procs */
188 * There is exactly one control stream for each PPA.
189 * The following fields are only used for control streams.
192 queue_t *lowerq; /* write queue attached below this PPA */
193 struct upperstr *nextppa; /* next control stream */
196 struct pppstat stats; /* statistics */
197 time_t last_sent; /* time last NP packet sent */
198 time_t last_recv; /* time last NP packet rcvd */
200 kmutex_t stats_lock; /* lock for stats updates */
201 kstat_t *kstats; /* stats for netstat */
205 char ifname[IFNAMSIZ];
206 struct ifstats ifstats;
210 /* Values for flags */
211 #define US_PRIV 1 /* stream was opened by superuser */
212 #define US_CONTROL 2 /* stream is a control stream */
213 #define US_BLOCKED 4 /* flow ctrl has blocked lower write stream */
214 #define US_LASTMOD 8 /* no PPP modules below us */
215 #define US_DBGLOG 0x10 /* log various occurrences */
216 #define US_RBLOCKED 0x20 /* flow ctrl has blocked upper read stream */
219 #if DL_CURRENT_VERSION >= 2
220 #define US_PROMISC 0x40 /* stream is promiscuous */
221 #endif /* DL_CURRENT_VERSION >= 2 */
222 #define US_RAWDATA 0x80 /* raw M_DATA, no DLPI header */
223 #endif /* defined(SOL2) */
226 static u_char max_band=0;
227 static u_char def_band=0;
229 #define IPPORT_DEFAULT 65535
232 * Port priority table
233 * Highest priority ports are listed first, lowest are listed last.
234 * ICMP & packets using unlisted ports will be treated as "default".
235 * If IPPORT_DEFAULT is not listed here, "default" packets will be
236 * assigned lowest priority.
237 * Each line should be terminated with "0".
238 * Line containing only "0" marks the end of the list.
241 static u_short prioq_table[]= {
243 22, 23, 513, 517, 518, 0,
247 20, 70, 80, 8001, 8008, 8080, 0, /* 8001,8008,8080 - common proxy ports */
253 static upperstr_t *minor_devs = NULL;
254 static upperstr_t *ppas = NULL;
257 static int pppopen __P((queue_t *, dev_t *, int, int, cred_t *));
258 static int pppclose __P((queue_t *, int, cred_t *));
260 static int pppopen __P((queue_t *, int, int, int));
261 static int pppclose __P((queue_t *, int));
263 static int pppurput __P((queue_t *, mblk_t *));
264 static int pppuwput __P((queue_t *, mblk_t *));
265 static int pppursrv __P((queue_t *));
266 static int pppuwsrv __P((queue_t *));
267 static int ppplrput __P((queue_t *, mblk_t *));
268 static int ppplwput __P((queue_t *, mblk_t *));
269 static int ppplrsrv __P((queue_t *));
270 static int ppplwsrv __P((queue_t *));
272 static void dlpi_request __P((queue_t *, mblk_t *, upperstr_t *));
273 static void dlpi_error __P((queue_t *, upperstr_t *, int, int, int));
274 static void dlpi_ok __P((queue_t *, int));
276 static int send_data __P((mblk_t *, upperstr_t *));
277 static void new_ppa __P((queue_t *, mblk_t *));
278 static void attach_ppa __P((queue_t *, mblk_t *));
280 static void detach_ppa __P((queue_t *, mblk_t *));
282 static void detach_lower __P((queue_t *, mblk_t *));
283 static void debug_dump __P((queue_t *, mblk_t *));
284 static upperstr_t *find_dest __P((upperstr_t *, int));
286 static upperstr_t *find_promisc __P((upperstr_t *, int));
287 static mblk_t *prepend_ether __P((upperstr_t *, mblk_t *, int));
288 static mblk_t *prepend_udind __P((upperstr_t *, mblk_t *, int));
289 static void promisc_sendup __P((upperstr_t *, mblk_t *, int, int));
290 #endif /* defined(SOL2) */
291 static int putctl2 __P((queue_t *, int, int, int));
292 static int putctl4 __P((queue_t *, int, int, int));
293 static int pass_packet __P((upperstr_t *ppa, mblk_t *mp, int outbound));
294 #ifdef FILTER_PACKETS
295 static int ip_hard_filter __P((upperstr_t *ppa, mblk_t *mp, int outbound));
296 #endif /* FILTER_PACKETS */
298 #define PPP_ID 0xb1a6
299 static struct module_info ppp_info = {
301 PPP_ID, "ppp", 0, 512, 512, 384
303 PPP_ID, "ppp", 0, 512, 512, 128
307 static struct qinit pppurint = {
308 pppurput, pppursrv, pppopen, pppclose, NULL, &ppp_info, NULL
311 static struct qinit pppuwint = {
312 pppuwput, pppuwsrv, NULL, NULL, NULL, &ppp_info, NULL
315 static struct qinit ppplrint = {
316 ppplrput, ppplrsrv, NULL, NULL, NULL, &ppp_info, NULL
319 static struct qinit ppplwint = {
320 ppplwput, ppplwsrv, NULL, NULL, NULL, &ppp_info, NULL
324 extern struct ifstats *ifstats;
328 struct streamtab pppinfo = {
329 &pppurint, &pppuwint,
336 * How we maintain statistics.
339 #define INCR_IPACKETS(ppa) \
340 if (ppa->kstats != 0) { \
341 KSTAT_NAMED_PTR(ppa->kstats)[0].value.ul++; \
343 #define INCR_IERRORS(ppa) \
344 if (ppa->kstats != 0) { \
345 KSTAT_NAMED_PTR(ppa->kstats)[1].value.ul++; \
347 #define INCR_OPACKETS(ppa) \
348 if (ppa->kstats != 0) { \
349 KSTAT_NAMED_PTR(ppa->kstats)[2].value.ul++; \
351 #define INCR_OERRORS(ppa) \
352 if (ppa->kstats != 0) { \
353 KSTAT_NAMED_PTR(ppa->kstats)[3].value.ul++; \
358 #define INCR_IPACKETS(ppa) ppa->ifstats.ifs_ipackets++;
359 #define INCR_IERRORS(ppa) ppa->ifstats.ifs_ierrors++;
360 #define INCR_OPACKETS(ppa) ppa->ifstats.ifs_opackets++;
361 #define INCR_OERRORS(ppa) ppa->ifstats.ifs_oerrors++;
365 * STREAMS driver entry points.
369 pppopen(q, devp, oflag, sflag, credp)
375 pppopen(q, dev, oflag, sflag)
377 int dev; /* really dev_t */
390 DRV_OPEN_OK(dev); /* device is already open */
393 /* Calculate max_bband & def_band from definitions in prioq.h
394 This colud be done at some more approtiate time (less often)
395 but this way it works well so I'll just leave it here */
403 if (*ptr++ == IPPORT_DEFAULT) {
407 max_band += new_band;
411 def_band = max_band - def_band;
415 if (sflag == CLONEOPEN) {
417 for (prevp = &minor_devs; (up = *prevp) != 0; prevp = &up->nextmn) {
424 mn = getminor(*devp);
428 for (prevp = &minor_devs; (up = *prevp) != 0; prevp = &up->nextmn) {
433 /* this can't happen */
434 q->q_ptr = WR(q)->q_ptr = (caddr_t) up;
440 * Construct a new minor node.
442 up = (upperstr_t *) ALLOC_SLEEP(sizeof(upperstr_t));
443 bzero((caddr_t) up, sizeof(upperstr_t));
445 DPRINT("pppopen: out of kernel memory\n");
452 *devp = makedevice(getmajor(*devp), mn);
456 up->flags |= US_PRIV;
458 up->state = DL_UNATTACHED;
461 up->ifflags = IFF_UP | IFF_POINTOPOINT;
464 up->last_sent = up->last_recv = time;
465 up->npmode = NPMODE_DROP;
466 q->q_ptr = (caddr_t) up;
467 WR(q)->q_ptr = (caddr_t) up;
470 mutex_init(&up->stats_lock, NULL, MUTEX_DRIVER, NULL);
475 DRV_OPEN_OK(makedev(major(dev), mn));
480 pppclose(q, flag, credp)
490 upperstr_t *up, **upp;
491 upperstr_t *as, *asnext;
496 up = (upperstr_t *) q->q_ptr;
498 DPRINT("pppclose: q_ptr = 0\n");
501 if (up->flags & US_DBGLOG)
502 DPRINT2("ppp/%d: close, flags=%x\n", up->mn, up->flags);
503 if (up->flags & US_CONTROL) {
505 struct ifstats *ifp, *pifp;
507 if (up->lowerq != 0) {
508 /* Gack! the lower stream should have be unlinked earlier! */
509 DPRINT1("ppp%d: lower stream still connected on close?\n",
512 up->lowerq->q_ptr = 0;
513 RD(up->lowerq)->q_ptr = 0;
519 * This stream represents a PPA:
520 * For all streams attached to the PPA, clear their
521 * references to this PPA.
522 * Then remove this PPA from the list of PPAs.
524 for (as = up->next; as != 0; as = asnext) {
528 if (as->flags & US_BLOCKED) {
529 as->flags &= ~US_BLOCKED;
530 flushq(WR(as->q), FLUSHDATA);
533 for (upp = &ppas; *upp != 0; upp = &(*upp)->nextppa)
539 /* Remove the statistics from the active list. */
540 for (ifp = ifstats, pifp = 0; ifp; ifp = ifp->ifs_next) {
541 if (ifp == &up->ifstats) {
543 pifp->ifs_next = ifp->ifs_next;
545 ifstats = ifp->ifs_next;
553 * If this stream is attached to a PPA,
554 * remove it from the PPA's list.
556 if ((as = up->ppa) != 0) {
557 for (; as->next != 0; as = as->next)
558 if (as->next == up) {
567 kstat_delete(up->kstats);
568 mutex_destroy(&up->stats_lock);
574 for (prevp = &minor_devs; *prevp != 0; prevp = &(*prevp)->nextmn) {
580 FREE(up, sizeof(upperstr_t));
587 * A message from on high. We do one of three things:
589 * - put the message on the lower write stream
590 * - queue it for our service routine
597 upperstr_t *us, *ppa, *nps;
607 struct ppp_idle *pip;
615 us = (upperstr_t *) q->q_ptr;
617 DPRINT("pppuwput: q_ptr = 0!\n");
621 DPRINT1("pppuwput/%d: mp = 0!\n", us->mn);
624 if (mp->b_datap == 0) {
625 DPRINT1("pppuwput/%d: mp->b_datap = 0!\n", us->mn);
628 switch (mp->b_datap->db_type) {
632 dlpi_request(q, mp, us);
637 if (us->flags & US_DBGLOG)
638 DPRINT3("ppp/%d: uwput M_DATA len=%d flags=%x\n",
639 us->mn, msgdsize(mp), us->flags);
640 if (us->ppa == 0 || msgdsize(mp) > us->ppa->mtu + PPP_HDRLEN
642 || (us->flags & US_CONTROL) == 0
645 DPRINT1("pppuwput: junk data len=%d\n", msgdsize(mp));
650 /* pass_packet frees the packet on returning 0 */
651 if ((us->flags & US_CONTROL) == 0 && !pass_packet(us, mp, 1))
654 if (!send_data(mp, us) && !putq(q, mp))
659 iop = (struct iocblk *) mp->b_rptr;
661 if (us->flags & US_DBGLOG)
662 DPRINT3("ppp/%d: ioctl %x count=%d\n",
663 us->mn, iop->ioc_cmd, iop->ioc_count);
664 switch (iop->ioc_cmd) {
666 case DLIOCRAW: /* raw M_DATA mode */
667 us->flags |= US_RAWDATA;
670 #endif /* defined(SOL2) */
672 if ((us->flags & US_CONTROL) == 0 || us->lowerq != 0)
674 if (mp->b_cont == 0) {
675 DPRINT1("pppuwput/%d: ioctl I_LINK b_cont = 0!\n", us->mn);
678 lb = (struct linkblk *) mp->b_cont->b_rptr;
681 DPRINT1("pppuwput/%d: ioctl I_LINK l_qbot = 0!\n", us->mn);
686 lq->q_ptr = (caddr_t) q;
687 RD(lq)->q_ptr = (caddr_t) us->q;
691 us->flags &= ~US_LASTMOD;
692 /* Unblock upper streams which now feed this lower stream. */
694 /* Send useful information down to the modules which
695 are now linked below us. */
696 putctl2(lq, M_CTL, PPPCTL_UNIT, us->ppa_id);
697 putctl4(lq, M_CTL, PPPCTL_MRU, us->mru);
698 putctl4(lq, M_CTL, PPPCTL_MTU, us->mtu);
700 /* Lower tty driver's queue hiwat/lowat from default 4096/128
701 to 256/128 since we don't want queueing of data on
702 output to physical device */
705 for (tlq = lq; tlq->q_next != NULL; tlq = tlq->q_next)
707 strqset(tlq, QHIWAT, 0, 256);
708 strqset(tlq, QLOWAT, 0, 128);
714 if (mp->b_cont == 0) {
715 DPRINT1("pppuwput/%d: ioctl I_UNLINK b_cont = 0!\n", us->mn);
718 lb = (struct linkblk *) mp->b_cont->b_rptr;
720 if (us->lowerq != lb->l_qbot) {
721 DPRINT2("ppp unlink: lowerq=%x qbot=%x\n",
722 us->lowerq, lb->l_qbot);
727 qwriter(q, mp, detach_lower, PERIM_OUTER);
733 if (us->flags & US_CONTROL)
735 if ((us->flags & US_PRIV) == 0) {
739 /* Arrange to return an int */
740 if ((mq = mp->b_cont) == 0
741 || mq->b_datap->db_lim - mq->b_rptr < sizeof(int)) {
742 mq = allocb(sizeof(int), BPRI_HI);
752 iop->ioc_count = sizeof(int);
753 mq->b_wptr = mq->b_rptr + sizeof(int);
754 qwriter(q, mp, new_ppa, PERIM_OUTER);
760 /* like dlpi_attach, for programs which can't write to
761 the stream (like pppstats) */
762 if (iop->ioc_count != sizeof(int) || us->ppa != 0)
764 if (mp->b_cont == 0) {
765 DPRINT1("pppuwput/%d: ioctl PPPIO_ATTACH b_cont = 0!\n", us->mn);
768 n = *(int *)mp->b_cont->b_rptr;
769 for (ppa = ppas; ppa != 0; ppa = ppa->nextppa)
770 if (ppa->ppa_id == n)
776 qwriter(q, mp, attach_ppa, PERIM_OUTER);
783 /* Attach to a given SAP. */
784 if (iop->ioc_count != sizeof(int) || us->ppa == 0)
786 if (mp->b_cont == 0) {
787 DPRINT1("pppuwput/%d: ioctl PPPIO_BIND b_cont = 0!\n", us->mn);
790 n = *(int *)mp->b_cont->b_rptr;
791 /* n must be a valid PPP network protocol number. */
792 if (n < 0x21 || n > 0x3fff || (n & 0x101) != 1)
794 /* check that no other stream is bound to this sap already. */
795 for (os = us->ppa; os != 0; os = os->next)
807 if (iop->ioc_count != sizeof(int) || (us->flags & US_CONTROL) == 0)
809 if (mp->b_cont == 0) {
810 DPRINT1("pppuwput/%d: ioctl PPPIO_MRU b_cont = 0!\n", us->mn);
813 n = *(int *)mp->b_cont->b_rptr;
814 if (n <= 0 || n > PPP_MAXMRU)
820 putctl4(us->lowerq, M_CTL, PPPCTL_MRU, n);
826 if (iop->ioc_count != sizeof(int) || (us->flags & US_CONTROL) == 0)
828 if (mp->b_cont == 0) {
829 DPRINT1("pppuwput/%d: ioctl PPPIO_MTU b_cont = 0!\n", us->mn);
832 n = *(int *)mp->b_cont->b_rptr;
833 if (n <= 0 || n > PPP_MAXMTU)
837 /* The MTU reported in netstat, not used as IP max packet size! */
838 us->ifstats.ifs_mtu = n;
841 putctl4(us->lowerq, M_CTL, PPPCTL_MTU, n);
847 us->flags |= US_LASTMOD;
852 if (iop->ioc_count != sizeof(int))
854 if (mp->b_cont == 0) {
855 DPRINT1("pppuwput/%d: ioctl PPPIO_DEBUG b_cont = 0!\n", us->mn);
858 n = *(int *)mp->b_cont->b_rptr;
859 if (n == PPPDBG_DUMP + PPPDBG_DRIVER) {
860 qwriter(q, mp, debug_dump, PERIM_OUTER);
863 } else if (n == PPPDBG_LOG + PPPDBG_DRIVER) {
864 DPRINT1("ppp/%d: debug log enabled\n", us->mn);
865 us->flags |= US_DBGLOG;
869 if (us->ppa == 0 || us->ppa->lowerq == 0)
871 putnext(us->ppa->lowerq, mp);
878 if (iop->ioc_count != 2 * sizeof(int))
880 if ((us->flags & US_CONTROL) == 0)
882 if (mp->b_cont == 0) {
883 DPRINT1("pppuwput/%d: ioctl PPPIO_NPMODE b_cont = 0!\n", us->mn);
886 sap = ((int *)mp->b_cont->b_rptr)[0];
887 for (nps = us->next; nps != 0; nps = nps->next) {
888 if (us->flags & US_DBGLOG)
889 DPRINT2("us = 0x%x, us->next->sap = 0x%x\n", nps, nps->sap);
894 if (us->flags & US_DBGLOG)
895 DPRINT2("ppp/%d: no stream for sap %x\n", us->mn, sap);
898 /* XXX possibly should use qwriter here */
899 nps->npmode = (enum NPmode) ((int *)mp->b_cont->b_rptr)[1];
900 if (nps->npmode != NPMODE_QUEUE && (nps->flags & US_BLOCKED) != 0)
907 if ((ppa = us->ppa) == 0)
909 mq = allocb(sizeof(struct ppp_idle), BPRI_HI);
918 pip = (struct ppp_idle *) mq->b_wptr;
919 pip->xmit_idle = time - ppa->last_sent;
920 pip->recv_idle = time - ppa->last_recv;
921 mq->b_wptr += sizeof(struct ppp_idle);
922 iop->ioc_count = sizeof(struct ppp_idle);
928 /* Sent from IP down to us. Attach the ifstats structure. */
929 if (iop->ioc_count != sizeof(struct ifreq) || us->ppa == 0)
931 ifr = (struct ifreq *)mp->b_cont->b_rptr;
932 /* Find the unit number in the interface name. */
933 for (i = 0; i < IFNAMSIZ; i++) {
934 if (ifr->ifr_name[i] == 0 ||
935 (ifr->ifr_name[i] >= '0' &&
936 ifr->ifr_name[i] <= '9'))
939 us->ifname[i] = ifr->ifr_name[i];
943 /* Convert the unit number to binary. */
944 for (n = 0; i < IFNAMSIZ; i++) {
945 if (ifr->ifr_name[i] == 0) {
949 n = n * 10 + ifr->ifr_name[i] - '0';
953 /* Verify the ppa. */
954 if (us->ppa->ppa_id != n)
958 /* Set up the netstat block. */
959 strncpy (ppa->ifname, us->ifname, IFNAMSIZ);
961 ppa->ifstats.ifs_name = ppa->ifname;
962 ppa->ifstats.ifs_unit = n;
963 ppa->ifstats.ifs_active = us->state != DL_UNBOUND;
964 ppa->ifstats.ifs_mtu = ppa->mtu;
966 /* Link in statistics used by netstat. */
967 ppa->ifstats.ifs_next = ifstats;
968 ifstats = &ppa->ifstats;
975 if (!(us->flags & US_CONTROL)) {
981 ((struct iocblk_in *)iop)->ioc_ifflags = us->ifflags;
986 if (!(us->flags & US_CONTROL)) {
992 us->ifflags = ((struct iocblk_in *)iop)->ioc_ifflags;
997 if (!(us->flags & US_CONTROL)) {
1003 us->ifflags |= IFF_RUNNING;
1004 ((struct iocblk_in *)iop)->ioc_ifflags |= IFF_RUNNING;
1010 * Vanilla SVR4 systems don't handle SIOCSIFMTU, rather
1011 * they take the MTU from the DL_INFO_ACK we sent in response
1012 * to their DL_INFO_REQ. Fortunately, they will update the
1013 * MTU if we send an unsolicited DL_INFO_ACK up.
1015 if ((mq = allocb(sizeof(dl_info_req_t), BPRI_HI)) == 0)
1016 break; /* should do bufcall */
1017 ((union DL_primitives *)mq->b_rptr)->dl_primitive = DL_INFO_REQ;
1018 mq->b_wptr = mq->b_rptr + sizeof(dl_info_req_t);
1019 dlpi_request(q, mq, us);
1020 /* mp is now gone */
1024 case SIOCGIFNETMASK:
1025 case SIOCSIFNETMASK:
1027 case SIOCGIFDSTADDR:
1028 case SIOCSIFDSTADDR:
1032 #endif /* LACHTCP */
1035 if (us->ppa == 0 || us->ppa->lowerq == 0)
1037 us->ioc_id = iop->ioc_id;
1039 switch (iop->ioc_cmd) {
1041 case PPPIO_GETCSTAT:
1042 if (us->flags & US_LASTMOD) {
1046 putnext(us->ppa->lowerq, mp);
1049 if (us->flags & US_PRIV)
1050 putnext(us->ppa->lowerq, mp);
1052 DPRINT1("ppp ioctl %x rejected\n", iop->ioc_cmd);
1061 iop->ioc_error = error;
1062 mp->b_datap->db_type = M_IOCNAK;
1064 } else if (error == 0) {
1065 mp->b_datap->db_type = M_IOCACK;
1071 if (us->flags & US_DBGLOG)
1072 DPRINT2("ppp/%d: flush %x\n", us->mn, *mp->b_rptr);
1073 if (*mp->b_rptr & FLUSHW)
1074 flushq(q, FLUSHDATA);
1075 if (*mp->b_rptr & FLUSHR) {
1076 *mp->b_rptr &= ~FLUSHW;
1091 dlpi_request(q, mp, us)
1096 union DL_primitives *d = (union DL_primitives *) mp->b_rptr;
1097 int size = mp->b_wptr - mp->b_rptr;
1099 upperstr_t *ppa, *os;
1101 dl_info_ack_t *info;
1102 dl_bind_ack_t *ackp;
1103 #if DL_CURRENT_VERSION >= 2
1104 dl_phys_addr_ack_t *paddrack;
1105 static struct ether_addr eaddr = {0};
1108 if (us->flags & US_DBGLOG)
1109 DPRINT3("ppp/%d: dlpi prim %x len=%d\n", us->mn,
1110 d->dl_primitive, size);
1111 switch (d->dl_primitive) {
1113 if (size < sizeof(dl_info_req_t))
1115 if ((reply = allocb(sizeof(dl_info_ack_t), BPRI_HI)) == 0)
1116 break; /* should do bufcall */
1117 reply->b_datap->db_type = M_PCPROTO;
1118 info = (dl_info_ack_t *) reply->b_wptr;
1119 reply->b_wptr += sizeof(dl_info_ack_t);
1120 bzero((caddr_t) info, sizeof(dl_info_ack_t));
1121 info->dl_primitive = DL_INFO_ACK;
1122 info->dl_max_sdu = us->ppa? us->ppa->mtu: PPP_MAXMTU;
1123 info->dl_min_sdu = 1;
1124 info->dl_addr_length = sizeof(uint);
1125 info->dl_mac_type = DL_ETHER; /* a bigger lie */
1126 info->dl_current_state = us->state;
1127 info->dl_service_mode = DL_CLDLS;
1128 info->dl_provider_style = DL_STYLE2;
1129 #if DL_CURRENT_VERSION >= 2
1130 info->dl_sap_length = sizeof(uint);
1131 info->dl_version = DL_CURRENT_VERSION;
1137 if (size < sizeof(dl_attach_req_t))
1139 if (us->state != DL_UNATTACHED || us->ppa != 0) {
1140 dlpi_error(q, us, DL_ATTACH_REQ, DL_OUTSTATE, 0);
1143 for (ppa = ppas; ppa != 0; ppa = ppa->nextppa)
1144 if (ppa->ppa_id == d->attach_req.dl_ppa)
1147 dlpi_error(q, us, DL_ATTACH_REQ, DL_BADPPA, 0);
1151 qwriter(q, mp, attach_ppa, PERIM_OUTER);
1155 if (size < sizeof(dl_detach_req_t))
1157 if (us->state != DL_UNBOUND || us->ppa == 0) {
1158 dlpi_error(q, us, DL_DETACH_REQ, DL_OUTSTATE, 0);
1161 qwriter(q, mp, detach_ppa, PERIM_OUTER);
1165 if (size < sizeof(dl_bind_req_t))
1167 if (us->state != DL_UNBOUND || us->ppa == 0) {
1168 dlpi_error(q, us, DL_BIND_REQ, DL_OUTSTATE, 0);
1172 /* apparently this test fails (unnecessarily?) on some systems */
1173 if (d->bind_req.dl_service_mode != DL_CLDLS) {
1174 dlpi_error(q, us, DL_BIND_REQ, DL_UNSUPPORTED, 0);
1179 /* saps must be valid PPP network protocol numbers,
1180 except that we accept ETHERTYPE_IP in place of PPP_IP. */
1181 sap = d->bind_req.dl_sap;
1185 if (us->flags & US_DBGLOG)
1186 DPRINT2("DL_BIND_REQ: ip gives sap = 0x%x, us = 0x%x", sap, us);
1188 if (sap == ETHERTYPE_IP) /* normal IFF_IPV4 */
1190 else if (sap == ETHERTYPE_IPV6) /* when IFF_IPV6 is set */
1192 else if (sap == ETHERTYPE_ALLSAP) /* snoop gives sap of 0 */
1195 DPRINT2("DL_BIND_REQ: unrecognized sap = 0x%x, us = 0x%x", sap, us);
1196 dlpi_error(q, us, DL_BIND_REQ, DL_BADADDR, 0);
1200 if (sap == ETHERTYPE_IP)
1202 if (sap < 0x21 || sap > 0x3fff || (sap & 0x101) != 1) {
1203 dlpi_error(q, us, DL_BIND_REQ, DL_BADADDR, 0);
1206 #endif /* defined(SOL2) */
1208 /* check that no other stream is bound to this sap already. */
1209 for (os = us->ppa; os != 0; os = os->next)
1213 dlpi_error(q, us, DL_BIND_REQ, DL_NOADDR, 0);
1218 us->state = DL_IDLE;
1220 if ((reply = allocb(sizeof(dl_bind_ack_t) + sizeof(uint),
1222 break; /* should do bufcall */
1223 ackp = (dl_bind_ack_t *) reply->b_wptr;
1224 reply->b_wptr += sizeof(dl_bind_ack_t) + sizeof(uint);
1225 reply->b_datap->db_type = M_PCPROTO;
1226 bzero((caddr_t) ackp, sizeof(dl_bind_ack_t));
1227 ackp->dl_primitive = DL_BIND_ACK;
1229 ackp->dl_addr_length = sizeof(uint);
1230 ackp->dl_addr_offset = sizeof(dl_bind_ack_t);
1231 *(uint *)(ackp+1) = sap;
1236 if (size < sizeof(dl_unbind_req_t))
1238 if (us->state != DL_IDLE) {
1239 dlpi_error(q, us, DL_UNBIND_REQ, DL_OUTSTATE, 0);
1243 us->state = DL_UNBOUND;
1245 us->ppa->ifstats.ifs_active = 0;
1247 dlpi_ok(q, DL_UNBIND_REQ);
1250 case DL_UNITDATA_REQ:
1251 if (size < sizeof(dl_unitdata_req_t))
1253 if (us->state != DL_IDLE) {
1254 dlpi_error(q, us, DL_UNITDATA_REQ, DL_OUTSTATE, 0);
1257 if ((ppa = us->ppa) == 0) {
1258 cmn_err(CE_CONT, "ppp: in state dl_idle but ppa == 0?\n");
1261 len = mp->b_cont == 0? 0: msgdsize(mp->b_cont);
1262 if (len > ppa->mtu) {
1263 DPRINT2("dlpi data too large (%d > %d)\n", len, ppa->mtu);
1269 * Should there be any promiscuous stream(s), send the data
1270 * up for each promiscuous stream that we recognize.
1273 promisc_sendup(ppa, mp->b_cont, us->sap, 0);
1274 #endif /* defined(SOL2) */
1278 /* Extract s_port & d_port from IP-packet, the code is a bit
1279 dirty here, but so am I, too... */
1280 if (mp->b_datap->db_type == M_PROTO && us->sap == PPP_IP
1281 && mp->b_cont != 0) {
1285 u_char band_unset, cur_band, syn;
1286 u_short s_port, d_port;
1288 bb = mp->b_cont->b_rptr; /* bb points to IP-header*/
1289 len = mp->b_cont->b_wptr - mp->b_cont->b_rptr;
1291 s_port = IPPORT_DEFAULT;
1292 d_port = IPPORT_DEFAULT;
1293 if (len >= 20) { /* 20 = minimum length of IP header */
1294 iphlen = (bb[0] & 0x0f) * 4;
1299 if (len >= 20) { /* min length of TCP header */
1300 s_port = (tlh[0] << 8) + tlh[1];
1301 d_port = (tlh[2] << 8) + tlh[3];
1302 syn = tlh[13] & 0x02;
1306 if (len >= 8) { /* min length of UDP header */
1307 s_port = (tlh[0] << 8) + tlh[1];
1308 d_port = (tlh[2] << 8) + tlh[3];
1315 * Now calculate b_band for this packet from the
1316 * port-priority table.
1319 cur_band = max_band;
1322 while (*ptr && band_unset)
1323 if (s_port == *ptr || d_port == *ptr++) {
1324 mp->b_band = cur_band;
1332 mp->b_band = def_band;
1333 /* It may be usable to urge SYN packets a bit */
1338 /* this assumes PPP_HDRLEN <= sizeof(dl_unitdata_req_t) */
1339 if (mp->b_datap->db_ref > 1) {
1340 np = allocb(PPP_HDRLEN, BPRI_HI);
1343 np->b_cont = mp->b_cont;
1348 mp->b_datap->db_type = M_DATA;
1349 /* XXX should use dl_dest_addr_offset/length here,
1350 but we would have to translate ETHERTYPE_IP -> PPP_IP */
1351 mp->b_wptr = mp->b_rptr + PPP_HDRLEN;
1352 mp->b_rptr[0] = PPP_ALLSTATIONS;
1353 mp->b_rptr[1] = PPP_UI;
1354 mp->b_rptr[2] = us->sap >> 8;
1355 mp->b_rptr[3] = us->sap;
1356 /* pass_packet frees the packet on returning 0 */
1357 if (pass_packet(us, mp, 1)) {
1358 if (!send_data(mp, us) && !putq(q, mp))
1363 #if DL_CURRENT_VERSION >= 2
1364 case DL_PHYS_ADDR_REQ:
1365 if (size < sizeof(dl_phys_addr_req_t))
1369 * Don't check state because ifconfig sends this one down too
1372 if ((reply = allocb(sizeof(dl_phys_addr_ack_t)+ETHERADDRL,
1374 break; /* should do bufcall */
1375 reply->b_datap->db_type = M_PCPROTO;
1376 paddrack = (dl_phys_addr_ack_t *) reply->b_wptr;
1377 reply->b_wptr += sizeof(dl_phys_addr_ack_t);
1378 bzero((caddr_t) paddrack, sizeof(dl_phys_addr_ack_t)+ETHERADDRL);
1379 paddrack->dl_primitive = DL_PHYS_ADDR_ACK;
1380 paddrack->dl_addr_length = ETHERADDRL;
1381 paddrack->dl_addr_offset = sizeof(dl_phys_addr_ack_t);
1382 bcopy(&eaddr, reply->b_wptr, ETHERADDRL);
1383 reply->b_wptr += ETHERADDRL;
1388 case DL_PROMISCON_REQ:
1389 if (size < sizeof(dl_promiscon_req_t))
1391 us->flags |= US_PROMISC;
1392 dlpi_ok(q, DL_PROMISCON_REQ);
1395 case DL_PROMISCOFF_REQ:
1396 if (size < sizeof(dl_promiscoff_req_t))
1398 us->flags &= ~US_PROMISC;
1399 dlpi_ok(q, DL_PROMISCOFF_REQ);
1402 case DL_PROMISCON_REQ: /* fall thru */
1403 case DL_PROMISCOFF_REQ: /* fall thru */
1404 #endif /* defined(SOL2) */
1405 #endif /* DL_CURRENT_VERSION >= 2 */
1407 #if DL_CURRENT_VERSION >= 2
1408 case DL_SET_PHYS_ADDR_REQ:
1409 case DL_SUBS_BIND_REQ:
1410 case DL_SUBS_UNBIND_REQ:
1411 case DL_ENABMULTI_REQ:
1412 case DL_DISABMULTI_REQ:
1415 case DL_REPLY_UPDATE_REQ:
1417 case DL_DATA_ACK_REQ:
1419 case DL_CONNECT_REQ:
1421 dlpi_error(q, us, d->dl_primitive, DL_NOTSUPPORTED, 0);
1424 case DL_CONNECT_RES:
1425 case DL_DISCONNECT_REQ:
1428 dlpi_error(q, us, d->dl_primitive, DL_OUTSTATE, 0);
1432 dlpi_error(q, us, d->dl_primitive, DL_BADQOSTYPE, 0);
1435 #if DL_CURRENT_VERSION >= 2
1442 if (us->flags & US_DBGLOG)
1443 DPRINT1("ppp: unknown dlpi prim 0x%x\n", d->dl_primitive);
1446 dlpi_error(q, us, d->dl_primitive, DL_BADPRIM, 0);
1453 dlpi_error(q, us, prim, err, uerr)
1456 int prim, err, uerr;
1459 dl_error_ack_t *errp;
1461 if (us->flags & US_DBGLOG)
1462 DPRINT3("ppp/%d: dlpi error, prim=%x, err=%x\n", us->mn, prim, err);
1463 reply = allocb(sizeof(dl_error_ack_t), BPRI_HI);
1465 return; /* XXX should do bufcall */
1466 reply->b_datap->db_type = M_PCPROTO;
1467 errp = (dl_error_ack_t *) reply->b_wptr;
1468 reply->b_wptr += sizeof(dl_error_ack_t);
1469 errp->dl_primitive = DL_ERROR_ACK;
1470 errp->dl_error_primitive = prim;
1471 errp->dl_errno = err;
1472 errp->dl_unix_errno = uerr;
1484 reply = allocb(sizeof(dl_ok_ack_t), BPRI_HI);
1486 return; /* XXX should do bufcall */
1487 reply->b_datap->db_type = M_PCPROTO;
1488 okp = (dl_ok_ack_t *) reply->b_wptr;
1489 reply->b_wptr += sizeof(dl_ok_ack_t);
1490 okp->dl_primitive = DL_OK_ACK;
1491 okp->dl_correct_primitive = prim;
1494 #endif /* NO_DLPI */
1497 * If return value is 0, then the packet has already been freed.
1500 pass_packet(us, mp, outbound)
1508 if ((ppa = us->ppa) == 0) {
1513 #ifdef FILTER_PACKETS
1514 pass = ip_hard_filter(us, mp, outbound);
1517 * Here is where we might, in future, decide whether to pass
1518 * or drop the packet, and whether it counts as link activity.
1521 #endif /* FILTER_PACKETS */
1524 /* pass only if link already up, and don't update time */
1525 if (ppa->lowerq == 0) {
1532 ppa->last_sent = time;
1534 ppa->last_recv = time;
1541 * We have some data to send down to the lower stream (or up the
1542 * control stream, if we don't have a lower stream attached).
1543 * Returns 1 if the message was dealt with, 0 if it wasn't able
1544 * to be sent on and should therefore be queued up.
1553 if ((us->flags & US_BLOCKED) || us->npmode == NPMODE_QUEUE)
1556 if (ppa == 0 || us->npmode == NPMODE_DROP || us->npmode == NPMODE_ERROR) {
1557 if (us->flags & US_DBGLOG)
1558 DPRINT2("ppp/%d: dropping pkt (npmode=%d)\n", us->mn, us->npmode);
1562 if (ppa->lowerq == 0) {
1563 /* try to send it up the control stream */
1564 if (bcanputnext(ppa->q, mp->b_band)) {
1566 * The message seems to get corrupted for some reason if
1567 * we just send the message up as it is, so we send a copy.
1569 mblk_t *np = copymsg(mp);
1572 putnext(ppa->q, np);
1576 if (bcanputnext(ppa->lowerq, mp->b_band)) {
1577 MT_ENTER(&ppa->stats_lock);
1578 ppa->stats.ppp_opackets++;
1579 ppa->stats.ppp_obytes += msgdsize(mp);
1580 #ifdef INCR_OPACKETS
1583 MT_EXIT(&ppa->stats_lock);
1585 * The lower queue is only ever detached while holding an
1586 * exclusive lock on the whole driver. So we can be confident
1587 * that the lower queue is still there.
1589 putnext(ppa->lowerq, mp);
1593 us->flags |= US_BLOCKED;
1598 * Allocate a new PPA id and link this stream into the list of PPAs.
1599 * This procedure is called with an exclusive lock on all queues in
1607 upperstr_t *us, *up, **usp;
1610 us = (upperstr_t *) q->q_ptr;
1612 DPRINT("new_ppa: q_ptr = 0!\n");
1618 while ((up = *usp) != 0 && ppa_id == up->ppa_id) {
1622 us->ppa_id = ppa_id;
1627 us->flags |= US_CONTROL;
1628 us->npmode = NPMODE_PASS;
1635 * Create a kstats record for our statistics, so netstat -i works.
1637 if (us->kstats == 0) {
1640 sprintf(unit, "ppp%d", us->ppa->ppa_id);
1641 us->kstats = kstat_create("ppp", us->ppa->ppa_id, unit,
1642 "net", KSTAT_TYPE_NAMED, 4, 0);
1643 if (us->kstats != 0) {
1644 kstat_named_t *kn = KSTAT_NAMED_PTR(us->kstats);
1646 strcpy(kn[0].name, "ipackets");
1647 kn[0].data_type = KSTAT_DATA_ULONG;
1648 strcpy(kn[1].name, "ierrors");
1649 kn[1].data_type = KSTAT_DATA_ULONG;
1650 strcpy(kn[2].name, "opackets");
1651 kn[2].data_type = KSTAT_DATA_ULONG;
1652 strcpy(kn[3].name, "oerrors");
1653 kn[3].data_type = KSTAT_DATA_ULONG;
1654 kstat_install(us->kstats);
1659 *(int *)mp->b_cont->b_rptr = ppa_id;
1660 mp->b_datap->db_type = M_IOCACK;
1671 us = (upperstr_t *) q->q_ptr;
1673 DPRINT("attach_ppa: q_ptr = 0!\n");
1678 us->state = DL_UNBOUND;
1680 for (t = us->ppa; t->next != 0; t = t->next)
1684 if (mp->b_datap->db_type == M_IOCTL) {
1685 mp->b_datap->db_type = M_IOCACK;
1689 dlpi_ok(q, DL_ATTACH_REQ);
1703 us = (upperstr_t *) q->q_ptr;
1705 DPRINT("detach_ppa: q_ptr = 0!\n");
1709 for (t = us->ppa; t->next != 0; t = t->next)
1710 if (t->next == us) {
1716 us->state = DL_UNATTACHED;
1717 dlpi_ok(q, DL_DETACH_REQ);
1723 * We call this with qwriter in order to give the upper queue procedures
1724 * the guarantee that the lower queue is not going to go away while
1725 * they are executing.
1734 us = (upperstr_t *) q->q_ptr;
1736 DPRINT("detach_lower: q_ptr = 0!\n");
1741 us->lowerq->q_ptr = 0;
1742 RD(us->lowerq)->q_ptr = 0;
1746 /* Unblock streams which now feed back up the control stream. */
1749 mp->b_datap->db_type = M_IOCACK;
1757 upperstr_t *us, *as;
1760 us = (upperstr_t *) q->q_ptr;
1762 DPRINT("pppuwsrv: q_ptr = 0!\n");
1767 * If this is a control stream, then this service procedure
1768 * probably got enabled because of flow control in the lower
1769 * stream being enabled (or because of the lower stream going
1770 * away). Therefore we enable the service procedure of all
1771 * attached upper streams.
1773 if (us->flags & US_CONTROL) {
1774 for (as = us->next; as != 0; as = as->next)
1778 /* Try to send on any data queued here. */
1779 us->flags &= ~US_BLOCKED;
1780 while ((mp = getq(q)) != 0) {
1781 if (!send_data(mp, us)) {
1790 /* should never get called... */
1807 * Flow control has back-enabled this stream:
1808 * enable the upper write service procedure for
1809 * the upper control stream for this lower stream.
1812 uq = (queue_t *) q->q_ptr;
1820 * This should only get called for control streams.
1827 upperstr_t *ppa, *us;
1831 ppa = (upperstr_t *) q->q_ptr;
1833 DPRINT("pppurput: q_ptr = 0!\n");
1837 switch (mp->b_datap->db_type) {
1839 MT_ENTER(&ppa->stats_lock);
1840 switch (*mp->b_rptr) {
1845 ppa->stats.ppp_ierrors++;
1851 ppa->stats.ppp_oerrors++;
1854 MT_EXIT(&ppa->stats_lock);
1861 * Attempt to match up the response with the stream
1862 * that the request came from.
1864 iop = (struct iocblk *) mp->b_rptr;
1865 for (us = ppa; us != 0; us = us->next)
1866 if (us->ioc_id == iop->ioc_id)
1876 * The serial device has hung up. We don't want to send
1877 * the M_HANGUP message up to pppd because that will stop
1878 * us from using the control stream any more. Instead we
1879 * send a zero-length message as an end-of-file indication.
1882 mp = allocb(1, BPRI_HI);
1884 DPRINT1("ppp/%d: couldn't allocate eof message!\n", ppa->mn);
1887 putnext(ppa->q, mp);
1892 if (mp->b_wptr - mp->b_rptr < PPP_HDRLEN) {
1893 PULLUP(mp, PPP_HDRLEN);
1895 DPRINT1("ppp_urput: msgpullup failed (len=%d)\n", len);
1899 MT_ENTER(&ppa->stats_lock);
1900 ppa->stats.ppp_ipackets++;
1901 ppa->stats.ppp_ibytes += len;
1902 #ifdef INCR_IPACKETS
1905 MT_EXIT(&ppa->stats_lock);
1907 proto = PPP_PROTOCOL(mp->b_rptr);
1911 * Should there be any promiscuous stream(s), send the data
1912 * up for each promiscuous stream that we recognize.
1914 promisc_sendup(ppa, mp, proto, 1);
1915 #endif /* defined(SOL2) */
1917 if (proto < 0x8000 && (us = find_dest(ppa, proto)) != 0) {
1919 * A data packet for some network protocol.
1920 * Queue it on the upper stream for that protocol.
1921 * XXX could we just putnext it? (would require thought)
1922 * The rblocked flag is there to ensure that we keep
1923 * messages in order for each network protocol.
1925 /* pass_packet frees the packet on returning 0 */
1926 if (!pass_packet(us, mp, 0))
1928 if (!us->rblocked && !canput(us->q))
1930 if (!putq(us->rblocked ? q : us->q, mp))
1939 * A control frame, a frame for an unknown protocol,
1940 * or some other message type.
1941 * Send it up to pppd via the control stream.
1943 if (queclass(mp) == QPCTL || canputnext(ppa->q))
1944 putnext(ppa->q, mp);
1945 else if (!putq(q, mp))
1957 upperstr_t *us, *as;
1960 dl_unitdata_ind_t *ud;
1964 us = (upperstr_t *) q->q_ptr;
1966 DPRINT("pppursrv: q_ptr = 0!\n");
1970 if (us->flags & US_CONTROL) {
1973 * If there is no lower queue attached, run the write service
1974 * routines of other upper streams attached to this PPA.
1976 if (us->lowerq == 0) {
1979 if (as->flags & US_BLOCKED)
1986 * Messages get queued on this stream's read queue if they
1987 * can't be queued on the read queue of the attached stream
1988 * that they are destined for. This is for flow control -
1989 * when this queue fills up, the lower read put procedure will
1990 * queue messages there and the flow control will propagate
1993 while ((mp = getq(q)) != 0) {
1994 proto = PPP_PROTOCOL(mp->b_rptr);
1995 if (proto < 0x8000 && (as = find_dest(us, proto)) != 0) {
1998 if (!putq(as->q, mp))
2009 /* can now put stuff directly on network protocol streams again */
2010 for (as = us->next; as != 0; as = as->next)
2015 * If this stream has a lower stream attached,
2016 * enable the read queue's service routine.
2017 * XXX we should really only do this if the queue length
2018 * has dropped below the low-water mark.
2020 if (us->lowerq != 0)
2021 qenable(RD(us->lowerq));
2025 * A network protocol stream. Put a DLPI header on each
2026 * packet and send it on.
2027 * (Actually, it seems that the IP module will happily
2028 * accept M_DATA messages without the DL_UNITDATA_IND header.)
2030 while ((mp = getq(q)) != 0) {
2031 if (!canputnext(q)) {
2036 proto = PPP_PROTOCOL(mp->b_rptr);
2037 mp->b_rptr += PPP_HDRLEN;
2038 hdr = allocb(sizeof(dl_unitdata_ind_t) + 2 * sizeof(uint),
2041 /* XXX should put it back and use bufcall */
2045 hdr->b_datap->db_type = M_PROTO;
2046 ud = (dl_unitdata_ind_t *) hdr->b_wptr;
2047 hdr->b_wptr += sizeof(dl_unitdata_ind_t) + 2 * sizeof(uint);
2049 ud->dl_primitive = DL_UNITDATA_IND;
2050 ud->dl_dest_addr_length = sizeof(uint);
2051 ud->dl_dest_addr_offset = sizeof(dl_unitdata_ind_t);
2052 ud->dl_src_addr_length = sizeof(uint);
2053 ud->dl_src_addr_offset = ud->dl_dest_addr_offset + sizeof(uint);
2054 #if DL_CURRENT_VERSION >= 2
2055 ud->dl_group_address = 0;
2057 /* Send the DLPI client the data with the SAP they requested,
2058 (e.g. ETHERTYPE_IP) rather than the PPP protocol number
2060 ((uint *)(ud + 1))[0] = us->req_sap; /* dest SAP */
2061 ((uint *)(ud + 1))[1] = us->req_sap; /* src SAP */
2065 #endif /* NO_DLPI */
2068 * Now that we have consumed some packets from this queue,
2069 * enable the control stream's read service routine so that we
2070 * can process any packets for us that might have got queued
2071 * there for flow control reasons.
2074 qenable(us->ppa->q);
2081 find_dest(ppa, proto)
2087 for (us = ppa->next; us != 0; us = us->next)
2088 if (proto == us->sap)
2095 * Test upstream promiscuous conditions. As of now, only pass IPv4 and
2096 * Ipv6 packets upstream (let PPP packets be decoded elsewhere).
2099 find_promisc(us, proto)
2104 if ((proto != PPP_IP) && (proto != PPP_IPV6))
2105 return (upperstr_t *)0;
2107 for ( ; us; us = us->next) {
2108 if ((us->flags & US_PROMISC) && (us->state == DL_IDLE))
2112 return (upperstr_t *)0;
2116 * Prepend an empty Ethernet header to msg for snoop, et al.
2119 prepend_ether(us, mp, proto)
2127 if ((eh = allocb(sizeof(struct ether_header), BPRI_HI)) == 0) {
2132 if (proto == PPP_IP)
2133 type = ETHERTYPE_IP;
2134 else if (proto == PPP_IPV6)
2135 type = ETHERTYPE_IPV6;
2137 type = proto; /* What else? Let decoder decide */
2139 eh->b_wptr += sizeof(struct ether_header);
2140 bzero((caddr_t)eh->b_rptr, sizeof(struct ether_header));
2141 ((struct ether_header *)eh->b_rptr)->ether_type = htons((short)type);
2147 * Prepend DL_UNITDATA_IND mblk to msg
2150 prepend_udind(us, mp, proto)
2155 dl_unitdata_ind_t *dlu;
2159 size = sizeof(dl_unitdata_ind_t);
2160 if ((dh = allocb(size, BPRI_MED)) == 0) {
2165 dh->b_datap->db_type = M_PROTO;
2166 dh->b_wptr = dh->b_datap->db_lim;
2167 dh->b_rptr = dh->b_wptr - size;
2169 dlu = (dl_unitdata_ind_t *)dh->b_rptr;
2170 dlu->dl_primitive = DL_UNITDATA_IND;
2171 dlu->dl_dest_addr_length = 0;
2172 dlu->dl_dest_addr_offset = sizeof(dl_unitdata_ind_t);
2173 dlu->dl_src_addr_length = 0;
2174 dlu->dl_src_addr_offset = sizeof(dl_unitdata_ind_t);
2175 dlu->dl_group_address = 0;
2182 * For any recognized promiscuous streams, send data upstream
2185 promisc_sendup(ppa, mp, proto, skip)
2190 mblk_t *dup_mp, *dup_dup_mp;
2191 upperstr_t *prus, *nprus;
2193 if ((prus = find_promisc(ppa, proto)) != 0) {
2194 if (dup_mp = dupmsg(mp)) {
2197 dup_mp->b_rptr += PPP_HDRLEN;
2199 for ( ; nprus = find_promisc(prus->next, proto);
2202 if (dup_dup_mp = dupmsg(dup_mp)) {
2203 if (canputnext(prus->q)) {
2204 if (prus->flags & US_RAWDATA) {
2205 dup_dup_mp = prepend_ether(prus, dup_dup_mp, proto);
2207 dup_dup_mp = prepend_udind(prus, dup_dup_mp, proto);
2209 if (dup_dup_mp == 0)
2211 putnext(prus->q, dup_dup_mp);
2213 DPRINT("ppp_urput: data to promisc q dropped\n");
2214 freemsg(dup_dup_mp);
2219 if (canputnext(prus->q)) {
2220 if (prus->flags & US_RAWDATA) {
2221 dup_mp = prepend_ether(prus, dup_mp, proto);
2223 dup_mp = prepend_udind(prus, dup_mp, proto);
2226 putnext(prus->q, dup_mp);
2228 DPRINT("ppp_urput: data to promisc q dropped\n");
2234 #endif /* defined(SOL2) */
2237 * We simply put the message on to the associated upper control stream
2238 * (either here or in ppplrsrv). That way we enter the perimeters
2239 * before looking through the list of attached streams to decide which
2240 * stream it should go up.
2250 switch (mp->b_datap->db_type) {
2252 iop = (struct iocblk *) mp->b_rptr;
2253 iop->ioc_error = EINVAL;
2254 mp->b_datap->db_type = M_IOCNAK;
2258 if (*mp->b_rptr & FLUSHR)
2259 flushq(q, FLUSHDATA);
2260 if (*mp->b_rptr & FLUSHW) {
2261 *mp->b_rptr &= ~FLUSHR;
2269 * If we can't get the lower lock straight away, queue this one
2270 * rather than blocking, to avoid the possibility of deadlock.
2272 if (!TRYLOCK_LOWER_R) {
2279 * Check that we're still connected to the driver.
2281 uq = (queue_t *) q->q_ptr;
2284 DPRINT1("ppplrput: q = %x, uq = 0??\n", q);
2290 * Try to forward the message to the put routine for the upper
2291 * control stream for this lower stream.
2292 * If there are already messages queued here, queue this one so
2293 * they don't get out of order.
2295 if (queclass(mp) == QPCTL || (qsize(q) == 0 && canput(uq)))
2297 else if (!putq(q, mp))
2312 * Packets get queued here for flow control reasons
2313 * or if the lrput routine couldn't get the lower lock
2317 uq = (queue_t *) q->q_ptr;
2320 flushq(q, FLUSHALL);
2321 DPRINT1("ppplrsrv: q = %x, uq = 0??\n", q);
2324 while ((mp = getq(q)) != 0) {
2325 if (queclass(mp) == QPCTL || canput(uq))
2337 putctl2(q, type, code, val)
2339 int type, code, val;
2343 mp = allocb(2, BPRI_HI);
2346 mp->b_datap->db_type = type;
2347 mp->b_wptr[0] = code;
2348 mp->b_wptr[1] = val;
2355 putctl4(q, type, code, val)
2357 int type, code, val;
2361 mp = allocb(4, BPRI_HI);
2364 mp->b_datap->db_type = type;
2365 mp->b_wptr[0] = code;
2366 ((short *)mp->b_wptr)[1] = val;
2380 DPRINT("ppp upper streams:\n");
2381 for (us = minor_devs; us != 0; us = us->nextmn) {
2383 DPRINT3(" %d: q=%x rlev=%d",
2384 us->mn, uq, (uq? qsize(uq): 0));
2385 DPRINT3(" wlev=%d flags=0x%b", (uq? qsize(WR(uq)): 0),
2386 us->flags, "\020\1priv\2control\3blocked\4last");
2387 DPRINT3(" state=%x sap=%x req_sap=%x", us->state, us->sap,
2392 DPRINT1(" ppa=%d\n", us->ppa->ppa_id);
2393 if (us->flags & US_CONTROL) {
2395 DPRINT3(" control for %d lq=%x rlev=%d",
2396 us->ppa_id, lq, (lq? qsize(RD(lq)): 0));
2397 DPRINT3(" wlev=%d mru=%d mtu=%d\n",
2398 (lq? qsize(lq): 0), us->mru, us->mtu);
2401 mp->b_datap->db_type = M_IOCACK;
2405 #ifdef FILTER_PACKETS
2406 #include <netinet/in_systm.h>
2407 #include <netinet/ip.h>
2408 #include <netinet/udp.h>
2409 #include <netinet/tcp.h>
2411 #define MAX_IPHDR 128 /* max TCP/IP header size */
2414 /* The following table contains a hard-coded list of protocol/port pairs.
2415 * Any matching packets are either discarded unconditionally, or,
2416 * if ok_if_link_up is non-zero when a connection does not currently exist
2417 * (i.e., they go through if the connection is present, but never initiate
2419 * This idea came from a post by dm@garage.uun.org (David Mazieres)
2421 static struct pktfilt_tab {
2424 u_short ok_if_link_up;
2426 { IPPROTO_UDP, 520, 1 }, /* RIP, ok to pass if link is up */
2427 { IPPROTO_UDP, 123, 1 }, /* NTP, don't keep up the link for it */
2428 { -1, 0, 0 } /* terminator entry has port == -1 */
2433 * Packet has already been freed if return value is 0.
2436 ip_hard_filter(us, mp, outbound)
2442 struct pktfilt_tab *pft;
2448 /* Note, the PPP header has already been pulled up in all cases */
2449 proto = PPP_PROTOCOL(mp->b_rptr);
2450 if (us->flags & US_DBGLOG)
2451 DPRINT3("ppp/%d: filter, proto=0x%x, out=%d\n", us->mn, proto, outbound);
2456 if ((mp->b_wptr - mp->b_rptr) == PPP_HDRLEN && mp->b_cont != 0) {
2457 temp_mp = mp->b_cont;
2458 len = msgdsize(temp_mp);
2459 hlen = (len < MAX_IPHDR) ? len : MAX_IPHDR;
2460 PULLUP(temp_mp, hlen);
2462 DPRINT2("ppp/%d: filter, pullup next failed, len=%d\n",
2464 mp->b_cont = 0; /* PULLUP() freed the rest */
2468 ip = (struct ip *)mp->b_cont->b_rptr;
2472 hlen = (len < (PPP_HDRLEN+MAX_IPHDR)) ? len : (PPP_HDRLEN+MAX_IPHDR);
2475 DPRINT2("ppp/%d: filter, pullup failed, len=%d\n",
2479 ip = (struct ip *)(mp->b_rptr + PPP_HDRLEN);
2482 /* For IP traffic, certain packets (e.g., RIP) may be either
2483 * 1. ignored - dropped completely
2484 * 2. will not initiate a connection, but
2485 * will be passed if a connection is currently up.
2487 for (pft=pktfilt_tab; pft->proto != -1; pft++) {
2488 if (ip->ip_p == pft->proto) {
2489 switch(pft->proto) {
2491 if (((struct udphdr *) &((int *)ip)[ip->ip_hl])->uh_dport
2492 == htons(pft->port)) goto endfor;
2495 if (((struct tcphdr *) &((int *)ip)[ip->ip_hl])->th_dport
2496 == htons(pft->port)) goto endfor;
2502 if (pft->proto != -1) {
2503 if (us->flags & US_DBGLOG)
2504 DPRINT3("ppp/%d: found IP pkt, proto=0x%x (%d)\n",
2505 us->mn, pft->proto, pft->port);
2506 /* Discard if not connected, or if not pass_with_link_up */
2507 /* else, if link is up let go by, but don't update time */
2508 if (pft->ok_if_link_up)
2514 } /* end switch (proto) */
2518 #endif /* FILTER_PACKETS */