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