17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * demand.c - Support routines for demand-dialling.
37c478bd9Sstevel@tonic-gate  *
4*f53eecf5SJames Carlson  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
57c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
67c478bd9Sstevel@tonic-gate  *
77c478bd9Sstevel@tonic-gate  * Copyright (c) 1993 The Australian National University.
87c478bd9Sstevel@tonic-gate  * All rights reserved.
97c478bd9Sstevel@tonic-gate  *
107c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
117c478bd9Sstevel@tonic-gate  * provided that the above copyright notice and this paragraph are
127c478bd9Sstevel@tonic-gate  * duplicated in all such forms and that any documentation,
137c478bd9Sstevel@tonic-gate  * advertising materials, and other materials related to such
147c478bd9Sstevel@tonic-gate  * distribution and use acknowledge that the software was developed
157c478bd9Sstevel@tonic-gate  * by the Australian National University.  The name of the University
167c478bd9Sstevel@tonic-gate  * may not be used to endorse or promote products derived from this
177c478bd9Sstevel@tonic-gate  * software without specific prior written permission.
187c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
197c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
207c478bd9Sstevel@tonic-gate  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate #include <stdio.h>
247c478bd9Sstevel@tonic-gate #include <stdlib.h>
257c478bd9Sstevel@tonic-gate #include <string.h>
267c478bd9Sstevel@tonic-gate #include <errno.h>
277c478bd9Sstevel@tonic-gate #include <fcntl.h>
287c478bd9Sstevel@tonic-gate #include <netdb.h>
297c478bd9Sstevel@tonic-gate #include <sys/param.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/wait.h>
327c478bd9Sstevel@tonic-gate #include <sys/time.h>
337c478bd9Sstevel@tonic-gate #include <sys/resource.h>
347c478bd9Sstevel@tonic-gate #include <sys/stat.h>
357c478bd9Sstevel@tonic-gate #include <sys/socket.h>
367c478bd9Sstevel@tonic-gate #ifdef PPP_FILTER
377c478bd9Sstevel@tonic-gate #include <net/if.h>
387c478bd9Sstevel@tonic-gate #include <net/bpf.h>
397c478bd9Sstevel@tonic-gate #include <pcap.h>
407c478bd9Sstevel@tonic-gate #endif
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include "pppd.h"
437c478bd9Sstevel@tonic-gate #include "fsm.h"
447c478bd9Sstevel@tonic-gate #include "lcp.h"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate static char *frame;
477c478bd9Sstevel@tonic-gate static int framelen;
487c478bd9Sstevel@tonic-gate static int framemax;
497c478bd9Sstevel@tonic-gate static int escape_flag;
507c478bd9Sstevel@tonic-gate static int flush_flag;
517c478bd9Sstevel@tonic-gate static int fcs;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate struct packet {
547c478bd9Sstevel@tonic-gate     int length;
557c478bd9Sstevel@tonic-gate     struct packet *next;
567c478bd9Sstevel@tonic-gate     unsigned char data[1];
577c478bd9Sstevel@tonic-gate };
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static struct packet *pend_q;
607c478bd9Sstevel@tonic-gate static struct packet *pend_qtail;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate static int active_packet __P((unsigned char *, int));
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate  * demand_conf - configure the interface for doing dial-on-demand.
667c478bd9Sstevel@tonic-gate  */
677c478bd9Sstevel@tonic-gate void
demand_conf()687c478bd9Sstevel@tonic-gate demand_conf()
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate     int i;
717c478bd9Sstevel@tonic-gate     struct protent *protp;
727c478bd9Sstevel@tonic-gate     int mtu;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate     framemax = lcp_wantoptions[0].mru;
757c478bd9Sstevel@tonic-gate     if (framemax < PPP_MRU)
767c478bd9Sstevel@tonic-gate 	    framemax = PPP_MRU;
777c478bd9Sstevel@tonic-gate     framemax += PPP_HDRLEN + PPP_FCSLEN;
787c478bd9Sstevel@tonic-gate     frame = malloc(framemax);
797c478bd9Sstevel@tonic-gate     if (frame == NULL)
807c478bd9Sstevel@tonic-gate 	novm("demand frame");
817c478bd9Sstevel@tonic-gate     framelen = 0;
827c478bd9Sstevel@tonic-gate     pend_q = NULL;
837c478bd9Sstevel@tonic-gate     escape_flag = 0;
847c478bd9Sstevel@tonic-gate     flush_flag = 0;
857c478bd9Sstevel@tonic-gate     fcs = PPP_INITFCS;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate     if ((mtu = lcp_allowoptions[0].mru) == 0)
887c478bd9Sstevel@tonic-gate         mtu = PPP_MTU;
897c478bd9Sstevel@tonic-gate     ppp_send_config(0, mtu, (u_int32_t) 0, 0, 0);
907c478bd9Sstevel@tonic-gate     ppp_recv_config(0, framemax, (u_int32_t) 0, 0, 0);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate #ifdef PPP_FILTER
937c478bd9Sstevel@tonic-gate     set_filters(&pass_filter, &active_filter);
947c478bd9Sstevel@tonic-gate #endif
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate     /*
977c478bd9Sstevel@tonic-gate      * Call the demand_conf procedure for each protocol that's got one.
987c478bd9Sstevel@tonic-gate      */
997c478bd9Sstevel@tonic-gate     for (i = 0; (protp = protocols[i]) != NULL; ++i)
1007c478bd9Sstevel@tonic-gate 	if (protp->enabled_flag && protp->demand_conf != NULL)
1017c478bd9Sstevel@tonic-gate 	    if (!((*protp->demand_conf)(0)))
1027c478bd9Sstevel@tonic-gate 		fatal("unable to set demand configuration on %s", protp->name);
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  * demand_block - set each network protocol to block further packets.
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate void
demand_block()1107c478bd9Sstevel@tonic-gate demand_block()
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate     int i;
1137c478bd9Sstevel@tonic-gate     struct protent *protp;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate     for (i = 0; (protp = protocols[i]) != NULL; ++i)
1167c478bd9Sstevel@tonic-gate 	if (protp->enabled_flag && protp->demand_conf != NULL &&
1177c478bd9Sstevel@tonic-gate 	    !sifnpmode(0, protp->protocol & ~0x8000, NPMODE_QUEUE))
1187c478bd9Sstevel@tonic-gate 	    fatal("unable to enable queuing for %s", protp->name);
1197c478bd9Sstevel@tonic-gate     /* Intentionally discard return value; we're on our way up now. */
1207c478bd9Sstevel@tonic-gate     (void) get_loop_output();
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate  * demand_discard - set each network protocol to discard packets
1257c478bd9Sstevel@tonic-gate  * with an error.
1267c478bd9Sstevel@tonic-gate  */
1277c478bd9Sstevel@tonic-gate void
demand_discard()1287c478bd9Sstevel@tonic-gate demand_discard()
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate     struct packet *pkt, *nextpkt;
1317c478bd9Sstevel@tonic-gate     int i;
1327c478bd9Sstevel@tonic-gate     struct protent *protp;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate     for (i = 0; (protp = protocols[i]) != NULL; ++i)
1357c478bd9Sstevel@tonic-gate 	if (protp->enabled_flag && protp->demand_conf != NULL &&
1367c478bd9Sstevel@tonic-gate 	    !sifnpmode(0, protp->protocol & ~0x8000, NPMODE_DROP))
1377c478bd9Sstevel@tonic-gate 	    fatal("unable to disable %s", protp->name);
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate     /* Intentionally discard return value; we're on our way down now. */
1407c478bd9Sstevel@tonic-gate     (void) get_loop_output();
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate     /* discard all saved packets */
1437c478bd9Sstevel@tonic-gate     for (pkt = pend_q; pkt != NULL; pkt = nextpkt) {
1447c478bd9Sstevel@tonic-gate 	nextpkt = pkt->next;
1457c478bd9Sstevel@tonic-gate 	free(pkt);
1467c478bd9Sstevel@tonic-gate     }
1477c478bd9Sstevel@tonic-gate     pend_q = NULL;
1487c478bd9Sstevel@tonic-gate     framelen = 0;
1497c478bd9Sstevel@tonic-gate     flush_flag = 0;
1507c478bd9Sstevel@tonic-gate     escape_flag = 0;
1517c478bd9Sstevel@tonic-gate     fcs = PPP_INITFCS;
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate /*
1557c478bd9Sstevel@tonic-gate  * demand_unblock - set each enabled network protocol to pass packets.
1567c478bd9Sstevel@tonic-gate  */
1577c478bd9Sstevel@tonic-gate void
demand_unblock()1587c478bd9Sstevel@tonic-gate demand_unblock()
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate     int i;
1617c478bd9Sstevel@tonic-gate     struct protent *protp;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate     for (i = 0; (protp = protocols[i]) != NULL; ++i)
1647c478bd9Sstevel@tonic-gate 	if (protp->enabled_flag && protp->demand_conf != NULL &&
1657c478bd9Sstevel@tonic-gate 	    !sifnpmode(0, protp->protocol & ~0x8000, NPMODE_PASS))
1667c478bd9Sstevel@tonic-gate 	    fatal("unable to enable %s", protp->name);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate  * FCS lookup table as calculated by genfcstab.
1717c478bd9Sstevel@tonic-gate  */
1727c478bd9Sstevel@tonic-gate static u_short fcstab[256] = {
1737c478bd9Sstevel@tonic-gate 	0x0000,	0x1189,	0x2312,	0x329b,	0x4624,	0x57ad,	0x6536,	0x74bf,
1747c478bd9Sstevel@tonic-gate 	0x8c48,	0x9dc1,	0xaf5a,	0xbed3,	0xca6c,	0xdbe5,	0xe97e,	0xf8f7,
1757c478bd9Sstevel@tonic-gate 	0x1081,	0x0108,	0x3393,	0x221a,	0x56a5,	0x472c,	0x75b7,	0x643e,
1767c478bd9Sstevel@tonic-gate 	0x9cc9,	0x8d40,	0xbfdb,	0xae52,	0xdaed,	0xcb64,	0xf9ff,	0xe876,
1777c478bd9Sstevel@tonic-gate 	0x2102,	0x308b,	0x0210,	0x1399,	0x6726,	0x76af,	0x4434,	0x55bd,
1787c478bd9Sstevel@tonic-gate 	0xad4a,	0xbcc3,	0x8e58,	0x9fd1,	0xeb6e,	0xfae7,	0xc87c,	0xd9f5,
1797c478bd9Sstevel@tonic-gate 	0x3183,	0x200a,	0x1291,	0x0318,	0x77a7,	0x662e,	0x54b5,	0x453c,
1807c478bd9Sstevel@tonic-gate 	0xbdcb,	0xac42,	0x9ed9,	0x8f50,	0xfbef,	0xea66,	0xd8fd,	0xc974,
1817c478bd9Sstevel@tonic-gate 	0x4204,	0x538d,	0x6116,	0x709f,	0x0420,	0x15a9,	0x2732,	0x36bb,
1827c478bd9Sstevel@tonic-gate 	0xce4c,	0xdfc5,	0xed5e,	0xfcd7,	0x8868,	0x99e1,	0xab7a,	0xbaf3,
1837c478bd9Sstevel@tonic-gate 	0x5285,	0x430c,	0x7197,	0x601e,	0x14a1,	0x0528,	0x37b3,	0x263a,
1847c478bd9Sstevel@tonic-gate 	0xdecd,	0xcf44,	0xfddf,	0xec56,	0x98e9,	0x8960,	0xbbfb,	0xaa72,
1857c478bd9Sstevel@tonic-gate 	0x6306,	0x728f,	0x4014,	0x519d,	0x2522,	0x34ab,	0x0630,	0x17b9,
1867c478bd9Sstevel@tonic-gate 	0xef4e,	0xfec7,	0xcc5c,	0xddd5,	0xa96a,	0xb8e3,	0x8a78,	0x9bf1,
1877c478bd9Sstevel@tonic-gate 	0x7387,	0x620e,	0x5095,	0x411c,	0x35a3,	0x242a,	0x16b1,	0x0738,
1887c478bd9Sstevel@tonic-gate 	0xffcf,	0xee46,	0xdcdd,	0xcd54,	0xb9eb,	0xa862,	0x9af9,	0x8b70,
1897c478bd9Sstevel@tonic-gate 	0x8408,	0x9581,	0xa71a,	0xb693,	0xc22c,	0xd3a5,	0xe13e,	0xf0b7,
1907c478bd9Sstevel@tonic-gate 	0x0840,	0x19c9,	0x2b52,	0x3adb,	0x4e64,	0x5fed,	0x6d76,	0x7cff,
1917c478bd9Sstevel@tonic-gate 	0x9489,	0x8500,	0xb79b,	0xa612,	0xd2ad,	0xc324,	0xf1bf,	0xe036,
1927c478bd9Sstevel@tonic-gate 	0x18c1,	0x0948,	0x3bd3,	0x2a5a,	0x5ee5,	0x4f6c,	0x7df7,	0x6c7e,
1937c478bd9Sstevel@tonic-gate 	0xa50a,	0xb483,	0x8618,	0x9791,	0xe32e,	0xf2a7,	0xc03c,	0xd1b5,
1947c478bd9Sstevel@tonic-gate 	0x2942,	0x38cb,	0x0a50,	0x1bd9,	0x6f66,	0x7eef,	0x4c74,	0x5dfd,
1957c478bd9Sstevel@tonic-gate 	0xb58b,	0xa402,	0x9699,	0x8710,	0xf3af,	0xe226,	0xd0bd,	0xc134,
1967c478bd9Sstevel@tonic-gate 	0x39c3,	0x284a,	0x1ad1,	0x0b58,	0x7fe7,	0x6e6e,	0x5cf5,	0x4d7c,
1977c478bd9Sstevel@tonic-gate 	0xc60c,	0xd785,	0xe51e,	0xf497,	0x8028,	0x91a1,	0xa33a,	0xb2b3,
1987c478bd9Sstevel@tonic-gate 	0x4a44,	0x5bcd,	0x6956,	0x78df,	0x0c60,	0x1de9,	0x2f72,	0x3efb,
1997c478bd9Sstevel@tonic-gate 	0xd68d,	0xc704,	0xf59f,	0xe416,	0x90a9,	0x8120,	0xb3bb,	0xa232,
2007c478bd9Sstevel@tonic-gate 	0x5ac5,	0x4b4c,	0x79d7,	0x685e,	0x1ce1,	0x0d68,	0x3ff3,	0x2e7a,
2017c478bd9Sstevel@tonic-gate 	0xe70e,	0xf687,	0xc41c,	0xd595,	0xa12a,	0xb0a3,	0x8238,	0x93b1,
2027c478bd9Sstevel@tonic-gate 	0x6b46,	0x7acf,	0x4854,	0x59dd,	0x2d62,	0x3ceb,	0x0e70,	0x1ff9,
2037c478bd9Sstevel@tonic-gate 	0xf78f,	0xe606,	0xd49d,	0xc514,	0xb1ab,	0xa022,	0x92b9,	0x8330,
2047c478bd9Sstevel@tonic-gate 	0x7bc7,	0x6a4e,	0x58d5,	0x495c,	0x3de3,	0x2c6a,	0x1ef1,	0x0f78
2057c478bd9Sstevel@tonic-gate };
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate /*
2087c478bd9Sstevel@tonic-gate  * loop_chars - process characters received from the loopback.
2097c478bd9Sstevel@tonic-gate  * Calls loop_frame when a complete frame has been accumulated.
2107c478bd9Sstevel@tonic-gate  * Return value is 1 if we need to bring up the link, 0 otherwise.
2117c478bd9Sstevel@tonic-gate  */
2127c478bd9Sstevel@tonic-gate int
loop_chars(p,n)2137c478bd9Sstevel@tonic-gate loop_chars(p, n)
2147c478bd9Sstevel@tonic-gate     unsigned char *p;
2157c478bd9Sstevel@tonic-gate     int n;
2167c478bd9Sstevel@tonic-gate {
2177c478bd9Sstevel@tonic-gate     int c, rv;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate     rv = 0;
2207c478bd9Sstevel@tonic-gate     for (; n > 0; --n) {
2217c478bd9Sstevel@tonic-gate 	c = *p++;
2227c478bd9Sstevel@tonic-gate 	if (c == PPP_FLAG) {
2237c478bd9Sstevel@tonic-gate 	    if (!escape_flag && !flush_flag
2247c478bd9Sstevel@tonic-gate 		&& framelen > 2 && fcs == PPP_GOODFCS) {
2257c478bd9Sstevel@tonic-gate 		framelen -= 2;
2267c478bd9Sstevel@tonic-gate 		if (loop_frame((unsigned char *)frame, framelen))
2277c478bd9Sstevel@tonic-gate 		    rv = 1;
2287c478bd9Sstevel@tonic-gate 	    }
2297c478bd9Sstevel@tonic-gate 	    framelen = 0;
2307c478bd9Sstevel@tonic-gate 	    flush_flag = 0;
2317c478bd9Sstevel@tonic-gate 	    escape_flag = 0;
2327c478bd9Sstevel@tonic-gate 	    fcs = PPP_INITFCS;
2337c478bd9Sstevel@tonic-gate 	    continue;
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 	if (flush_flag)
2367c478bd9Sstevel@tonic-gate 	    continue;
2377c478bd9Sstevel@tonic-gate 	if (escape_flag) {
2387c478bd9Sstevel@tonic-gate 	    c ^= PPP_TRANS;
2397c478bd9Sstevel@tonic-gate 	    escape_flag = 0;
2407c478bd9Sstevel@tonic-gate 	} else if (c == PPP_ESCAPE) {
2417c478bd9Sstevel@tonic-gate 	    escape_flag = 1;
2427c478bd9Sstevel@tonic-gate 	    continue;
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 	if (framelen >= framemax) {
2457c478bd9Sstevel@tonic-gate 	    flush_flag = 1;
2467c478bd9Sstevel@tonic-gate 	    continue;
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 	frame[framelen++] = c;
2497c478bd9Sstevel@tonic-gate 	fcs = PPP_FCS(fcs, c);
2507c478bd9Sstevel@tonic-gate     }
2517c478bd9Sstevel@tonic-gate     return rv;
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate /*
2557c478bd9Sstevel@tonic-gate  * loop_frame - given a frame obtained from the loopback,
2567c478bd9Sstevel@tonic-gate  * decide whether to bring up the link or not, and, if we want
2577c478bd9Sstevel@tonic-gate  * to transmit this frame later, put it on the pending queue.
2587c478bd9Sstevel@tonic-gate  * Return value is 1 if we need to bring up the link, 0 otherwise.
2597c478bd9Sstevel@tonic-gate  * We assume that the kernel driver has already applied the
2607c478bd9Sstevel@tonic-gate  * pass_filter, so we won't get packets it rejected.
2617c478bd9Sstevel@tonic-gate  * We apply the active_filter to see if we want this packet to
2627c478bd9Sstevel@tonic-gate  * bring up the link.
2637c478bd9Sstevel@tonic-gate  */
2647c478bd9Sstevel@tonic-gate int
loop_frame(frame,len)2657c478bd9Sstevel@tonic-gate loop_frame(frame, len)
2667c478bd9Sstevel@tonic-gate     unsigned char *frame;
2677c478bd9Sstevel@tonic-gate     int len;
2687c478bd9Sstevel@tonic-gate {
2697c478bd9Sstevel@tonic-gate     struct packet *pkt;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate     if (len < PPP_HDRLEN)
2727c478bd9Sstevel@tonic-gate 	return 0;
2737c478bd9Sstevel@tonic-gate     if ((PPP_PROTOCOL(frame) & 0x8000) != 0)
2747c478bd9Sstevel@tonic-gate 	return 0;		/* shouldn't get any of these anyway */
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate     /* Note - once we have pending packets, we don't drop any more. */
2777c478bd9Sstevel@tonic-gate     if (pend_q == NULL && !active_packet(frame, len))
2787c478bd9Sstevel@tonic-gate 	return 0;
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate     pkt = (struct packet *) malloc(sizeof(struct packet) + len);
2817c478bd9Sstevel@tonic-gate     if (pkt != NULL) {
2827c478bd9Sstevel@tonic-gate 	pkt->length = len;
2837c478bd9Sstevel@tonic-gate 	pkt->next = NULL;
2847c478bd9Sstevel@tonic-gate 	(void) memcpy(pkt->data, frame, len);
2857c478bd9Sstevel@tonic-gate 	if (pend_q == NULL)
2867c478bd9Sstevel@tonic-gate 	    pend_q = pkt;
2877c478bd9Sstevel@tonic-gate 	else
2887c478bd9Sstevel@tonic-gate 	    pend_qtail->next = pkt;
2897c478bd9Sstevel@tonic-gate 	pend_qtail = pkt;
2907c478bd9Sstevel@tonic-gate     }
2917c478bd9Sstevel@tonic-gate     return 1;
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate /*
2957c478bd9Sstevel@tonic-gate  * demand_rexmit - Resend all those frames that we got via the
2967c478bd9Sstevel@tonic-gate  * loopback, now that the real serial link is up.
2977c478bd9Sstevel@tonic-gate  */
2987c478bd9Sstevel@tonic-gate void
demand_rexmit(proto)2997c478bd9Sstevel@tonic-gate demand_rexmit(proto)
3007c478bd9Sstevel@tonic-gate     int proto;
3017c478bd9Sstevel@tonic-gate {
3027c478bd9Sstevel@tonic-gate     struct packet *pkt, *prev, *nextpkt;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate     prev = NULL;
3057c478bd9Sstevel@tonic-gate     pkt = pend_q;
3067c478bd9Sstevel@tonic-gate     pend_q = NULL;
3077c478bd9Sstevel@tonic-gate     for (; pkt != NULL; pkt = nextpkt) {
3087c478bd9Sstevel@tonic-gate 	nextpkt = pkt->next;
3097c478bd9Sstevel@tonic-gate 	if (PPP_PROTOCOL(pkt->data) == proto) {
3107c478bd9Sstevel@tonic-gate 	    output(0, pkt->data, pkt->length);
3117c478bd9Sstevel@tonic-gate 	    free(pkt);
3127c478bd9Sstevel@tonic-gate 	} else {
3137c478bd9Sstevel@tonic-gate 	    if (prev == NULL)
3147c478bd9Sstevel@tonic-gate 		pend_q = pkt;
3157c478bd9Sstevel@tonic-gate 	    else
3167c478bd9Sstevel@tonic-gate 		prev->next = pkt;
3177c478bd9Sstevel@tonic-gate 	    prev = pkt;
3187c478bd9Sstevel@tonic-gate 	}
3197c478bd9Sstevel@tonic-gate     }
3207c478bd9Sstevel@tonic-gate     pend_qtail = prev;
3217c478bd9Sstevel@tonic-gate     if (prev != NULL)
3227c478bd9Sstevel@tonic-gate 	prev->next = NULL;
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate /*
3267c478bd9Sstevel@tonic-gate  * Scan a packet to decide whether it is an "active" packet,
3277c478bd9Sstevel@tonic-gate  * that is, whether it is worth bringing up the link for.
3287c478bd9Sstevel@tonic-gate  */
3297c478bd9Sstevel@tonic-gate static int
active_packet(p,len)3307c478bd9Sstevel@tonic-gate active_packet(p, len)
3317c478bd9Sstevel@tonic-gate     unsigned char *p;
3327c478bd9Sstevel@tonic-gate     int len;
3337c478bd9Sstevel@tonic-gate {
3347c478bd9Sstevel@tonic-gate     int proto, i;
3357c478bd9Sstevel@tonic-gate     struct protent *protp;
3367c478bd9Sstevel@tonic-gate     const char *cp;
3377c478bd9Sstevel@tonic-gate     char pbuf[32];
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate     if (len < PPP_HDRLEN)
3407c478bd9Sstevel@tonic-gate 	return 0;
3417c478bd9Sstevel@tonic-gate #ifdef PPP_FILTER
3427c478bd9Sstevel@tonic-gate     if (active_filter.bf_len != 0
3437c478bd9Sstevel@tonic-gate 	&& bpf_filter(active_filter.bf_insns, frame, len, len) == 0) {
3447c478bd9Sstevel@tonic-gate 	dbglog("BPF identified packet as not worth bringing up the link.");
3457c478bd9Sstevel@tonic-gate 	return 0;
3467c478bd9Sstevel@tonic-gate     }
3477c478bd9Sstevel@tonic-gate #endif
3487c478bd9Sstevel@tonic-gate     proto = PPP_PROTOCOL(p);
3497c478bd9Sstevel@tonic-gate     for (i = 0; (protp = protocols[i]) != NULL; ++i) {
3507c478bd9Sstevel@tonic-gate 	if (protp->protocol < 0xC000 && (protp->protocol & ~0x8000) == proto) {
3517c478bd9Sstevel@tonic-gate 	    if (!protp->enabled_flag) {
3527c478bd9Sstevel@tonic-gate 		dbglog("%s: not enabled; not bringing up link", protp->name);
3537c478bd9Sstevel@tonic-gate 		return 0;
3547c478bd9Sstevel@tonic-gate 	    }
3557c478bd9Sstevel@tonic-gate 	    if (protp->active_pkt == NULL) {
356*f53eecf5SJames Carlson 		notice("%s: no active test; bringing up link", protp->name);
3577c478bd9Sstevel@tonic-gate 		return 1;
3587c478bd9Sstevel@tonic-gate 	    }
3597c478bd9Sstevel@tonic-gate 	    i = (*protp->active_pkt)(p, len);
360*f53eecf5SJames Carlson 	    if (i != 0)
361*f53eecf5SJames Carlson 		    notice("%s: active test; bringing up link", protp->name);
362*f53eecf5SJames Carlson 	    else
363*f53eecf5SJames Carlson 		    dbglog("%s: active test; not bringing up link",
364*f53eecf5SJames Carlson 			protp->name);
3657c478bd9Sstevel@tonic-gate 	    return i;
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate     }
3687c478bd9Sstevel@tonic-gate     if ((cp = protocol_name(proto)) == NULL) {
3697c478bd9Sstevel@tonic-gate 	(void) slprintf(pbuf, sizeof (pbuf), "0x#X", proto);
3707c478bd9Sstevel@tonic-gate 	cp = (const char *)pbuf;
3717c478bd9Sstevel@tonic-gate     }
3727c478bd9Sstevel@tonic-gate     dbglog("%s: unknown protocol; not bringing up link", cp);
3737c478bd9Sstevel@tonic-gate     return 0;			/* not a supported protocol !!?? */
3747c478bd9Sstevel@tonic-gate }
375