xref: /third_party/lwip/src/netif/ppp/demand.c (revision 195972f6)
1195972f6Sopenharmony_ci/*
2195972f6Sopenharmony_ci * demand.c - Support routines for demand-dialling.
3195972f6Sopenharmony_ci *
4195972f6Sopenharmony_ci * Copyright (c) 1996-2002 Paul Mackerras. All rights reserved.
5195972f6Sopenharmony_ci *
6195972f6Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
7195972f6Sopenharmony_ci * modification, are permitted provided that the following conditions
8195972f6Sopenharmony_ci * are met:
9195972f6Sopenharmony_ci *
10195972f6Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright
11195972f6Sopenharmony_ci *    notice, this list of conditions and the following disclaimer.
12195972f6Sopenharmony_ci *
13195972f6Sopenharmony_ci * 2. The name(s) of the authors of this software must not be used to
14195972f6Sopenharmony_ci *    endorse or promote products derived from this software without
15195972f6Sopenharmony_ci *    prior written permission.
16195972f6Sopenharmony_ci *
17195972f6Sopenharmony_ci * 3. Redistributions of any form whatsoever must retain the following
18195972f6Sopenharmony_ci *    acknowledgment:
19195972f6Sopenharmony_ci *    "This product includes software developed by Paul Mackerras
20195972f6Sopenharmony_ci *     <paulus@samba.org>".
21195972f6Sopenharmony_ci *
22195972f6Sopenharmony_ci * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
23195972f6Sopenharmony_ci * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
24195972f6Sopenharmony_ci * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
25195972f6Sopenharmony_ci * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
26195972f6Sopenharmony_ci * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
27195972f6Sopenharmony_ci * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
28195972f6Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29195972f6Sopenharmony_ci */
30195972f6Sopenharmony_ci
31195972f6Sopenharmony_ci#include "netif/ppp/ppp_opts.h"
32195972f6Sopenharmony_ci#if PPP_SUPPORT && DEMAND_SUPPORT  /* don't build if not configured for use in lwipopts.h */
33195972f6Sopenharmony_ci
34195972f6Sopenharmony_ci#include <stdio.h>
35195972f6Sopenharmony_ci#include <stdlib.h>
36195972f6Sopenharmony_ci#include <string.h>
37195972f6Sopenharmony_ci#include <errno.h>
38195972f6Sopenharmony_ci#include <fcntl.h>
39195972f6Sopenharmony_ci#include <netdb.h>
40195972f6Sopenharmony_ci#include <unistd.h>
41195972f6Sopenharmony_ci#include <syslog.h>
42195972f6Sopenharmony_ci#include <sys/param.h>
43195972f6Sopenharmony_ci#include <sys/types.h>
44195972f6Sopenharmony_ci#include <sys/wait.h>
45195972f6Sopenharmony_ci#include <sys/time.h>
46195972f6Sopenharmony_ci#include <sys/resource.h>
47195972f6Sopenharmony_ci#include <sys/stat.h>
48195972f6Sopenharmony_ci#include <sys/socket.h>
49195972f6Sopenharmony_ci#include <netinet/in.h>
50195972f6Sopenharmony_ci#include <arpa/inet.h>
51195972f6Sopenharmony_ci#ifdef PPP_FILTER
52195972f6Sopenharmony_ci#include <pcap-bpf.h>
53195972f6Sopenharmony_ci#endif
54195972f6Sopenharmony_ci
55195972f6Sopenharmony_ci#include "netif/ppp/ppp_impl.h"
56195972f6Sopenharmony_ci
57195972f6Sopenharmony_ci#include "netif/ppp/fsm.h"
58195972f6Sopenharmony_ci#include "netif/ppp/ipcp.h"
59195972f6Sopenharmony_ci#include "netif/ppp/lcp.h"
60195972f6Sopenharmony_ci
61195972f6Sopenharmony_cichar *frame;
62195972f6Sopenharmony_ciint framelen;
63195972f6Sopenharmony_ciint framemax;
64195972f6Sopenharmony_ciint escape_flag;
65195972f6Sopenharmony_ciint flush_flag;
66195972f6Sopenharmony_ciint fcs;
67195972f6Sopenharmony_ci
68195972f6Sopenharmony_cistruct packet {
69195972f6Sopenharmony_ci    int length;
70195972f6Sopenharmony_ci    struct packet *next;
71195972f6Sopenharmony_ci    unsigned char data[1];
72195972f6Sopenharmony_ci};
73195972f6Sopenharmony_ci
74195972f6Sopenharmony_cistruct packet *pend_q;
75195972f6Sopenharmony_cistruct packet *pend_qtail;
76195972f6Sopenharmony_ci
77195972f6Sopenharmony_cistatic int active_packet (unsigned char *, int);
78195972f6Sopenharmony_ci
79195972f6Sopenharmony_ci/*
80195972f6Sopenharmony_ci * demand_conf - configure the interface for doing dial-on-demand.
81195972f6Sopenharmony_ci */
82195972f6Sopenharmony_civoid
83195972f6Sopenharmony_cidemand_conf()
84195972f6Sopenharmony_ci{
85195972f6Sopenharmony_ci    int i;
86195972f6Sopenharmony_ci    const struct protent *protp;
87195972f6Sopenharmony_ci
88195972f6Sopenharmony_ci/*    framemax = lcp_allowoptions[0].mru;
89195972f6Sopenharmony_ci    if (framemax < PPP_MRU) */
90195972f6Sopenharmony_ci	framemax = PPP_MRU;
91195972f6Sopenharmony_ci    framemax += PPP_HDRLEN + PPP_FCSLEN;
92195972f6Sopenharmony_ci    frame = malloc(framemax);
93195972f6Sopenharmony_ci    if (frame == NULL)
94195972f6Sopenharmony_ci	novm("demand frame");
95195972f6Sopenharmony_ci    framelen = 0;
96195972f6Sopenharmony_ci    pend_q = NULL;
97195972f6Sopenharmony_ci    escape_flag = 0;
98195972f6Sopenharmony_ci    flush_flag = 0;
99195972f6Sopenharmony_ci    fcs = PPP_INITFCS;
100195972f6Sopenharmony_ci
101195972f6Sopenharmony_ci    netif_set_mtu(pcb, LWIP_MIN(lcp_allowoptions[0].mru, PPP_MRU));
102195972f6Sopenharmony_ci    if (ppp_send_config(pcb, PPP_MRU, (u32_t) 0, 0, 0) < 0
103195972f6Sopenharmony_ci	|| ppp_recv_config(pcb, PPP_MRU, (u32_t) 0, 0, 0) < 0)
104195972f6Sopenharmony_ci	    fatal("Couldn't set up demand-dialled PPP interface: %m");
105195972f6Sopenharmony_ci
106195972f6Sopenharmony_ci#ifdef PPP_FILTER
107195972f6Sopenharmony_ci    set_filters(&pass_filter, &active_filter);
108195972f6Sopenharmony_ci#endif
109195972f6Sopenharmony_ci
110195972f6Sopenharmony_ci    /*
111195972f6Sopenharmony_ci     * Call the demand_conf procedure for each protocol that's got one.
112195972f6Sopenharmony_ci     */
113195972f6Sopenharmony_ci    for (i = 0; (protp = protocols[i]) != NULL; ++i)
114195972f6Sopenharmony_ci	if (protp->demand_conf != NULL)
115195972f6Sopenharmony_ci	    ((*protp->demand_conf)(pcb));
116195972f6Sopenharmony_ci/* FIXME: find a way to die() here */
117195972f6Sopenharmony_ci#if 0
118195972f6Sopenharmony_ci	    if (!((*protp->demand_conf)(pcb)))
119195972f6Sopenharmony_ci		die(1);
120195972f6Sopenharmony_ci#endif
121195972f6Sopenharmony_ci}
122195972f6Sopenharmony_ci
123195972f6Sopenharmony_ci
124195972f6Sopenharmony_ci/*
125195972f6Sopenharmony_ci * demand_block - set each network protocol to block further packets.
126195972f6Sopenharmony_ci */
127195972f6Sopenharmony_civoid
128195972f6Sopenharmony_cidemand_block()
129195972f6Sopenharmony_ci{
130195972f6Sopenharmony_ci    int i;
131195972f6Sopenharmony_ci    const struct protent *protp;
132195972f6Sopenharmony_ci
133195972f6Sopenharmony_ci    for (i = 0; (protp = protocols[i]) != NULL; ++i)
134195972f6Sopenharmony_ci	if (protp->demand_conf != NULL)
135195972f6Sopenharmony_ci	    sifnpmode(pcb, protp->protocol & ~0x8000, NPMODE_QUEUE);
136195972f6Sopenharmony_ci    get_loop_output();
137195972f6Sopenharmony_ci}
138195972f6Sopenharmony_ci
139195972f6Sopenharmony_ci/*
140195972f6Sopenharmony_ci * demand_discard - set each network protocol to discard packets
141195972f6Sopenharmony_ci * with an error.
142195972f6Sopenharmony_ci */
143195972f6Sopenharmony_civoid
144195972f6Sopenharmony_cidemand_discard()
145195972f6Sopenharmony_ci{
146195972f6Sopenharmony_ci    struct packet *pkt, *nextpkt;
147195972f6Sopenharmony_ci    int i;
148195972f6Sopenharmony_ci    const struct protent *protp;
149195972f6Sopenharmony_ci
150195972f6Sopenharmony_ci    for (i = 0; (protp = protocols[i]) != NULL; ++i)
151195972f6Sopenharmony_ci	if (protp->demand_conf != NULL)
152195972f6Sopenharmony_ci	    sifnpmode(pcb, protp->protocol & ~0x8000, NPMODE_ERROR);
153195972f6Sopenharmony_ci    get_loop_output();
154195972f6Sopenharmony_ci
155195972f6Sopenharmony_ci    /* discard all saved packets */
156195972f6Sopenharmony_ci    for (pkt = pend_q; pkt != NULL; pkt = nextpkt) {
157195972f6Sopenharmony_ci	nextpkt = pkt->next;
158195972f6Sopenharmony_ci	free(pkt);
159195972f6Sopenharmony_ci    }
160195972f6Sopenharmony_ci    pend_q = NULL;
161195972f6Sopenharmony_ci    framelen = 0;
162195972f6Sopenharmony_ci    flush_flag = 0;
163195972f6Sopenharmony_ci    escape_flag = 0;
164195972f6Sopenharmony_ci    fcs = PPP_INITFCS;
165195972f6Sopenharmony_ci}
166195972f6Sopenharmony_ci
167195972f6Sopenharmony_ci/*
168195972f6Sopenharmony_ci * demand_unblock - set each enabled network protocol to pass packets.
169195972f6Sopenharmony_ci */
170195972f6Sopenharmony_civoid
171195972f6Sopenharmony_cidemand_unblock()
172195972f6Sopenharmony_ci{
173195972f6Sopenharmony_ci    int i;
174195972f6Sopenharmony_ci    const struct protent *protp;
175195972f6Sopenharmony_ci
176195972f6Sopenharmony_ci    for (i = 0; (protp = protocols[i]) != NULL; ++i)
177195972f6Sopenharmony_ci	if (protp->demand_conf != NULL)
178195972f6Sopenharmony_ci	    sifnpmode(pcb, protp->protocol & ~0x8000, NPMODE_PASS);
179195972f6Sopenharmony_ci}
180195972f6Sopenharmony_ci
181195972f6Sopenharmony_ci/*
182195972f6Sopenharmony_ci * FCS lookup table as calculated by genfcstab.
183195972f6Sopenharmony_ci */
184195972f6Sopenharmony_cistatic u_short fcstab[256] = {
185195972f6Sopenharmony_ci	0x0000,	0x1189,	0x2312,	0x329b,	0x4624,	0x57ad,	0x6536,	0x74bf,
186195972f6Sopenharmony_ci	0x8c48,	0x9dc1,	0xaf5a,	0xbed3,	0xca6c,	0xdbe5,	0xe97e,	0xf8f7,
187195972f6Sopenharmony_ci	0x1081,	0x0108,	0x3393,	0x221a,	0x56a5,	0x472c,	0x75b7,	0x643e,
188195972f6Sopenharmony_ci	0x9cc9,	0x8d40,	0xbfdb,	0xae52,	0xdaed,	0xcb64,	0xf9ff,	0xe876,
189195972f6Sopenharmony_ci	0x2102,	0x308b,	0x0210,	0x1399,	0x6726,	0x76af,	0x4434,	0x55bd,
190195972f6Sopenharmony_ci	0xad4a,	0xbcc3,	0x8e58,	0x9fd1,	0xeb6e,	0xfae7,	0xc87c,	0xd9f5,
191195972f6Sopenharmony_ci	0x3183,	0x200a,	0x1291,	0x0318,	0x77a7,	0x662e,	0x54b5,	0x453c,
192195972f6Sopenharmony_ci	0xbdcb,	0xac42,	0x9ed9,	0x8f50,	0xfbef,	0xea66,	0xd8fd,	0xc974,
193195972f6Sopenharmony_ci	0x4204,	0x538d,	0x6116,	0x709f,	0x0420,	0x15a9,	0x2732,	0x36bb,
194195972f6Sopenharmony_ci	0xce4c,	0xdfc5,	0xed5e,	0xfcd7,	0x8868,	0x99e1,	0xab7a,	0xbaf3,
195195972f6Sopenharmony_ci	0x5285,	0x430c,	0x7197,	0x601e,	0x14a1,	0x0528,	0x37b3,	0x263a,
196195972f6Sopenharmony_ci	0xdecd,	0xcf44,	0xfddf,	0xec56,	0x98e9,	0x8960,	0xbbfb,	0xaa72,
197195972f6Sopenharmony_ci	0x6306,	0x728f,	0x4014,	0x519d,	0x2522,	0x34ab,	0x0630,	0x17b9,
198195972f6Sopenharmony_ci	0xef4e,	0xfec7,	0xcc5c,	0xddd5,	0xa96a,	0xb8e3,	0x8a78,	0x9bf1,
199195972f6Sopenharmony_ci	0x7387,	0x620e,	0x5095,	0x411c,	0x35a3,	0x242a,	0x16b1,	0x0738,
200195972f6Sopenharmony_ci	0xffcf,	0xee46,	0xdcdd,	0xcd54,	0xb9eb,	0xa862,	0x9af9,	0x8b70,
201195972f6Sopenharmony_ci	0x8408,	0x9581,	0xa71a,	0xb693,	0xc22c,	0xd3a5,	0xe13e,	0xf0b7,
202195972f6Sopenharmony_ci	0x0840,	0x19c9,	0x2b52,	0x3adb,	0x4e64,	0x5fed,	0x6d76,	0x7cff,
203195972f6Sopenharmony_ci	0x9489,	0x8500,	0xb79b,	0xa612,	0xd2ad,	0xc324,	0xf1bf,	0xe036,
204195972f6Sopenharmony_ci	0x18c1,	0x0948,	0x3bd3,	0x2a5a,	0x5ee5,	0x4f6c,	0x7df7,	0x6c7e,
205195972f6Sopenharmony_ci	0xa50a,	0xb483,	0x8618,	0x9791,	0xe32e,	0xf2a7,	0xc03c,	0xd1b5,
206195972f6Sopenharmony_ci	0x2942,	0x38cb,	0x0a50,	0x1bd9,	0x6f66,	0x7eef,	0x4c74,	0x5dfd,
207195972f6Sopenharmony_ci	0xb58b,	0xa402,	0x9699,	0x8710,	0xf3af,	0xe226,	0xd0bd,	0xc134,
208195972f6Sopenharmony_ci	0x39c3,	0x284a,	0x1ad1,	0x0b58,	0x7fe7,	0x6e6e,	0x5cf5,	0x4d7c,
209195972f6Sopenharmony_ci	0xc60c,	0xd785,	0xe51e,	0xf497,	0x8028,	0x91a1,	0xa33a,	0xb2b3,
210195972f6Sopenharmony_ci	0x4a44,	0x5bcd,	0x6956,	0x78df,	0x0c60,	0x1de9,	0x2f72,	0x3efb,
211195972f6Sopenharmony_ci	0xd68d,	0xc704,	0xf59f,	0xe416,	0x90a9,	0x8120,	0xb3bb,	0xa232,
212195972f6Sopenharmony_ci	0x5ac5,	0x4b4c,	0x79d7,	0x685e,	0x1ce1,	0x0d68,	0x3ff3,	0x2e7a,
213195972f6Sopenharmony_ci	0xe70e,	0xf687,	0xc41c,	0xd595,	0xa12a,	0xb0a3,	0x8238,	0x93b1,
214195972f6Sopenharmony_ci	0x6b46,	0x7acf,	0x4854,	0x59dd,	0x2d62,	0x3ceb,	0x0e70,	0x1ff9,
215195972f6Sopenharmony_ci	0xf78f,	0xe606,	0xd49d,	0xc514,	0xb1ab,	0xa022,	0x92b9,	0x8330,
216195972f6Sopenharmony_ci	0x7bc7,	0x6a4e,	0x58d5,	0x495c,	0x3de3,	0x2c6a,	0x1ef1,	0x0f78
217195972f6Sopenharmony_ci};
218195972f6Sopenharmony_ci
219195972f6Sopenharmony_ci/*
220195972f6Sopenharmony_ci * loop_chars - process characters received from the loopback.
221195972f6Sopenharmony_ci * Calls loop_frame when a complete frame has been accumulated.
222195972f6Sopenharmony_ci * Return value is 1 if we need to bring up the link, 0 otherwise.
223195972f6Sopenharmony_ci */
224195972f6Sopenharmony_ciint
225195972f6Sopenharmony_ciloop_chars(p, n)
226195972f6Sopenharmony_ci    unsigned char *p;
227195972f6Sopenharmony_ci    int n;
228195972f6Sopenharmony_ci{
229195972f6Sopenharmony_ci    int c, rv;
230195972f6Sopenharmony_ci
231195972f6Sopenharmony_ci    rv = 0;
232195972f6Sopenharmony_ci
233195972f6Sopenharmony_ci/* check for synchronous connection... */
234195972f6Sopenharmony_ci
235195972f6Sopenharmony_ci    if ( (p[0] == 0xFF) && (p[1] == 0x03) ) {
236195972f6Sopenharmony_ci        rv = loop_frame(p,n);
237195972f6Sopenharmony_ci        return rv;
238195972f6Sopenharmony_ci    }
239195972f6Sopenharmony_ci
240195972f6Sopenharmony_ci    for (; n > 0; --n) {
241195972f6Sopenharmony_ci	c = *p++;
242195972f6Sopenharmony_ci	if (c == PPP_FLAG) {
243195972f6Sopenharmony_ci	    if (!escape_flag && !flush_flag
244195972f6Sopenharmony_ci		&& framelen > 2 && fcs == PPP_GOODFCS) {
245195972f6Sopenharmony_ci		framelen -= 2;
246195972f6Sopenharmony_ci		if (loop_frame((unsigned char *)frame, framelen))
247195972f6Sopenharmony_ci		    rv = 1;
248195972f6Sopenharmony_ci	    }
249195972f6Sopenharmony_ci	    framelen = 0;
250195972f6Sopenharmony_ci	    flush_flag = 0;
251195972f6Sopenharmony_ci	    escape_flag = 0;
252195972f6Sopenharmony_ci	    fcs = PPP_INITFCS;
253195972f6Sopenharmony_ci	    continue;
254195972f6Sopenharmony_ci	}
255195972f6Sopenharmony_ci	if (flush_flag)
256195972f6Sopenharmony_ci	    continue;
257195972f6Sopenharmony_ci	if (escape_flag) {
258195972f6Sopenharmony_ci	    c ^= PPP_TRANS;
259195972f6Sopenharmony_ci	    escape_flag = 0;
260195972f6Sopenharmony_ci	} else if (c == PPP_ESCAPE) {
261195972f6Sopenharmony_ci	    escape_flag = 1;
262195972f6Sopenharmony_ci	    continue;
263195972f6Sopenharmony_ci	}
264195972f6Sopenharmony_ci	if (framelen >= framemax) {
265195972f6Sopenharmony_ci	    flush_flag = 1;
266195972f6Sopenharmony_ci	    continue;
267195972f6Sopenharmony_ci	}
268195972f6Sopenharmony_ci	frame[framelen++] = c;
269195972f6Sopenharmony_ci	fcs = PPP_FCS(fcs, c);
270195972f6Sopenharmony_ci    }
271195972f6Sopenharmony_ci    return rv;
272195972f6Sopenharmony_ci}
273195972f6Sopenharmony_ci
274195972f6Sopenharmony_ci/*
275195972f6Sopenharmony_ci * loop_frame - given a frame obtained from the loopback,
276195972f6Sopenharmony_ci * decide whether to bring up the link or not, and, if we want
277195972f6Sopenharmony_ci * to transmit this frame later, put it on the pending queue.
278195972f6Sopenharmony_ci * Return value is 1 if we need to bring up the link, 0 otherwise.
279195972f6Sopenharmony_ci * We assume that the kernel driver has already applied the
280195972f6Sopenharmony_ci * pass_filter, so we won't get packets it rejected.
281195972f6Sopenharmony_ci * We apply the active_filter to see if we want this packet to
282195972f6Sopenharmony_ci * bring up the link.
283195972f6Sopenharmony_ci */
284195972f6Sopenharmony_ciint
285195972f6Sopenharmony_ciloop_frame(frame, len)
286195972f6Sopenharmony_ci    unsigned char *frame;
287195972f6Sopenharmony_ci    int len;
288195972f6Sopenharmony_ci{
289195972f6Sopenharmony_ci    struct packet *pkt;
290195972f6Sopenharmony_ci
291195972f6Sopenharmony_ci    /* dbglog("from loop: %P", frame, len); */
292195972f6Sopenharmony_ci    if (len < PPP_HDRLEN)
293195972f6Sopenharmony_ci	return 0;
294195972f6Sopenharmony_ci    if ((PPP_PROTOCOL(frame) & 0x8000) != 0)
295195972f6Sopenharmony_ci	return 0;		/* shouldn't get any of these anyway */
296195972f6Sopenharmony_ci    if (!active_packet(frame, len))
297195972f6Sopenharmony_ci	return 0;
298195972f6Sopenharmony_ci
299195972f6Sopenharmony_ci    pkt = (struct packet *) malloc(sizeof(struct packet) + len);
300195972f6Sopenharmony_ci    if (pkt != NULL) {
301195972f6Sopenharmony_ci	pkt->length = len;
302195972f6Sopenharmony_ci	pkt->next = NULL;
303195972f6Sopenharmony_ci	memcpy(pkt->data, frame, len);
304195972f6Sopenharmony_ci	if (pend_q == NULL)
305195972f6Sopenharmony_ci	    pend_q = pkt;
306195972f6Sopenharmony_ci	else
307195972f6Sopenharmony_ci	    pend_qtail->next = pkt;
308195972f6Sopenharmony_ci	pend_qtail = pkt;
309195972f6Sopenharmony_ci    }
310195972f6Sopenharmony_ci    return 1;
311195972f6Sopenharmony_ci}
312195972f6Sopenharmony_ci
313195972f6Sopenharmony_ci/*
314195972f6Sopenharmony_ci * demand_rexmit - Resend all those frames which we got via the
315195972f6Sopenharmony_ci * loopback, now that the real serial link is up.
316195972f6Sopenharmony_ci */
317195972f6Sopenharmony_civoid
318195972f6Sopenharmony_cidemand_rexmit(proto, newip)
319195972f6Sopenharmony_ci    int proto;
320195972f6Sopenharmony_ci    u32_t newip;
321195972f6Sopenharmony_ci{
322195972f6Sopenharmony_ci    struct packet *pkt, *prev, *nextpkt;
323195972f6Sopenharmony_ci    unsigned short checksum;
324195972f6Sopenharmony_ci    unsigned short pkt_checksum = 0;
325195972f6Sopenharmony_ci    unsigned iphdr;
326195972f6Sopenharmony_ci    struct timeval tv;
327195972f6Sopenharmony_ci    char cv = 0;
328195972f6Sopenharmony_ci    char ipstr[16];
329195972f6Sopenharmony_ci
330195972f6Sopenharmony_ci    prev = NULL;
331195972f6Sopenharmony_ci    pkt = pend_q;
332195972f6Sopenharmony_ci    pend_q = NULL;
333195972f6Sopenharmony_ci    tv.tv_sec = 1;
334195972f6Sopenharmony_ci    tv.tv_usec = 0;
335195972f6Sopenharmony_ci    select(0,NULL,NULL,NULL,&tv);	/* Sleep for 1 Seconds */
336195972f6Sopenharmony_ci    for (; pkt != NULL; pkt = nextpkt) {
337195972f6Sopenharmony_ci	nextpkt = pkt->next;
338195972f6Sopenharmony_ci	if (PPP_PROTOCOL(pkt->data) == proto) {
339195972f6Sopenharmony_ci            if ( (proto == PPP_IP) && newip ) {
340195972f6Sopenharmony_ci		/* Get old checksum */
341195972f6Sopenharmony_ci
342195972f6Sopenharmony_ci		iphdr = (pkt->data[4] & 15) << 2;
343195972f6Sopenharmony_ci		checksum = *((unsigned short *) (pkt->data+14));
344195972f6Sopenharmony_ci                if (checksum == 0xFFFF) {
345195972f6Sopenharmony_ci                    checksum = 0;
346195972f6Sopenharmony_ci                }
347195972f6Sopenharmony_ci
348195972f6Sopenharmony_ci
349195972f6Sopenharmony_ci                if (pkt->data[13] == 17) {
350195972f6Sopenharmony_ci                    pkt_checksum =  *((unsigned short *) (pkt->data+10+iphdr));
351195972f6Sopenharmony_ci		    if (pkt_checksum) {
352195972f6Sopenharmony_ci                        cv = 1;
353195972f6Sopenharmony_ci                        if (pkt_checksum == 0xFFFF) {
354195972f6Sopenharmony_ci                            pkt_checksum = 0;
355195972f6Sopenharmony_ci                        }
356195972f6Sopenharmony_ci                    }
357195972f6Sopenharmony_ci                    else {
358195972f6Sopenharmony_ci                       cv = 0;
359195972f6Sopenharmony_ci                    }
360195972f6Sopenharmony_ci                }
361195972f6Sopenharmony_ci
362195972f6Sopenharmony_ci		if (pkt->data[13] == 6) {
363195972f6Sopenharmony_ci		    pkt_checksum = *((unsigned short *) (pkt->data+20+iphdr));
364195972f6Sopenharmony_ci		    cv = 1;
365195972f6Sopenharmony_ci                    if (pkt_checksum == 0xFFFF) {
366195972f6Sopenharmony_ci                        pkt_checksum = 0;
367195972f6Sopenharmony_ci                    }
368195972f6Sopenharmony_ci		}
369195972f6Sopenharmony_ci
370195972f6Sopenharmony_ci		/* Delete old Source-IP-Address */
371195972f6Sopenharmony_ci                checksum -= *((unsigned short *) (pkt->data+16)) ^ 0xFFFF;
372195972f6Sopenharmony_ci                checksum -= *((unsigned short *) (pkt->data+18)) ^ 0xFFFF;
373195972f6Sopenharmony_ci
374195972f6Sopenharmony_ci		pkt_checksum -= *((unsigned short *) (pkt->data+16)) ^ 0xFFFF;
375195972f6Sopenharmony_ci		pkt_checksum -= *((unsigned short *) (pkt->data+18)) ^ 0xFFFF;
376195972f6Sopenharmony_ci
377195972f6Sopenharmony_ci		/* Change Source-IP-Address */
378195972f6Sopenharmony_ci                * ((u32_t *) (pkt->data + 16)) = newip;
379195972f6Sopenharmony_ci
380195972f6Sopenharmony_ci		/* Add new Source-IP-Address */
381195972f6Sopenharmony_ci                checksum += *((unsigned short *) (pkt->data+16)) ^ 0xFFFF;
382195972f6Sopenharmony_ci                checksum += *((unsigned short *) (pkt->data+18)) ^ 0xFFFF;
383195972f6Sopenharmony_ci
384195972f6Sopenharmony_ci                pkt_checksum += *((unsigned short *) (pkt->data+16)) ^ 0xFFFF;
385195972f6Sopenharmony_ci                pkt_checksum += *((unsigned short *) (pkt->data+18)) ^ 0xFFFF;
386195972f6Sopenharmony_ci
387195972f6Sopenharmony_ci		/* Write new checksum */
388195972f6Sopenharmony_ci                if (!checksum) {
389195972f6Sopenharmony_ci                    checksum = 0xFFFF;
390195972f6Sopenharmony_ci                }
391195972f6Sopenharmony_ci                *((unsigned short *) (pkt->data+14)) = checksum;
392195972f6Sopenharmony_ci		if (pkt->data[13] == 6) {
393195972f6Sopenharmony_ci		    *((unsigned short *) (pkt->data+20+iphdr)) = pkt_checksum;
394195972f6Sopenharmony_ci		}
395195972f6Sopenharmony_ci		if (cv && (pkt->data[13] == 17) ) {
396195972f6Sopenharmony_ci		    *((unsigned short *) (pkt->data+10+iphdr)) = pkt_checksum;
397195972f6Sopenharmony_ci		}
398195972f6Sopenharmony_ci
399195972f6Sopenharmony_ci		/* Log Packet */
400195972f6Sopenharmony_ci		strcpy(ipstr,inet_ntoa(*( (struct in_addr *) (pkt->data+16))));
401195972f6Sopenharmony_ci		if (pkt->data[13] == 1) {
402195972f6Sopenharmony_ci		    syslog(LOG_INFO,"Open ICMP %s -> %s\n",
403195972f6Sopenharmony_ci			ipstr,
404195972f6Sopenharmony_ci			inet_ntoa(*( (struct in_addr *) (pkt->data+20))));
405195972f6Sopenharmony_ci		} else {
406195972f6Sopenharmony_ci		    syslog(LOG_INFO,"Open %s %s:%d -> %s:%d\n",
407195972f6Sopenharmony_ci			pkt->data[13] == 6 ? "TCP" : "UDP",
408195972f6Sopenharmony_ci			ipstr,
409195972f6Sopenharmony_ci			ntohs(*( (short *) (pkt->data+iphdr+4))),
410195972f6Sopenharmony_ci			inet_ntoa(*( (struct in_addr *) (pkt->data+20))),
411195972f6Sopenharmony_ci			ntohs(*( (short *) (pkt->data+iphdr+6))));
412195972f6Sopenharmony_ci                }
413195972f6Sopenharmony_ci            }
414195972f6Sopenharmony_ci	    output(pcb, pkt->data, pkt->length);
415195972f6Sopenharmony_ci	    free(pkt);
416195972f6Sopenharmony_ci	} else {
417195972f6Sopenharmony_ci	    if (prev == NULL)
418195972f6Sopenharmony_ci		pend_q = pkt;
419195972f6Sopenharmony_ci	    else
420195972f6Sopenharmony_ci		prev->next = pkt;
421195972f6Sopenharmony_ci	    prev = pkt;
422195972f6Sopenharmony_ci	}
423195972f6Sopenharmony_ci    }
424195972f6Sopenharmony_ci    pend_qtail = prev;
425195972f6Sopenharmony_ci    if (prev != NULL)
426195972f6Sopenharmony_ci	prev->next = NULL;
427195972f6Sopenharmony_ci}
428195972f6Sopenharmony_ci
429195972f6Sopenharmony_ci/*
430195972f6Sopenharmony_ci * Scan a packet to decide whether it is an "active" packet,
431195972f6Sopenharmony_ci * that is, whether it is worth bringing up the link for.
432195972f6Sopenharmony_ci */
433195972f6Sopenharmony_cistatic int
434195972f6Sopenharmony_ciactive_packet(p, len)
435195972f6Sopenharmony_ci    unsigned char *p;
436195972f6Sopenharmony_ci    int len;
437195972f6Sopenharmony_ci{
438195972f6Sopenharmony_ci    int proto, i;
439195972f6Sopenharmony_ci    const struct protent *protp;
440195972f6Sopenharmony_ci
441195972f6Sopenharmony_ci    if (len < PPP_HDRLEN)
442195972f6Sopenharmony_ci	return 0;
443195972f6Sopenharmony_ci    proto = PPP_PROTOCOL(p);
444195972f6Sopenharmony_ci#ifdef PPP_FILTER
445195972f6Sopenharmony_ci    p[0] = 1;		/* outbound packet indicator */
446195972f6Sopenharmony_ci    if ((pass_filter.bf_len != 0
447195972f6Sopenharmony_ci	 && bpf_filter(pass_filter.bf_insns, p, len, len) == 0)
448195972f6Sopenharmony_ci	|| (active_filter.bf_len != 0
449195972f6Sopenharmony_ci	    && bpf_filter(active_filter.bf_insns, p, len, len) == 0)) {
450195972f6Sopenharmony_ci	p[0] = 0xff;
451195972f6Sopenharmony_ci	return 0;
452195972f6Sopenharmony_ci    }
453195972f6Sopenharmony_ci    p[0] = 0xff;
454195972f6Sopenharmony_ci#endif
455195972f6Sopenharmony_ci    for (i = 0; (protp = protocols[i]) != NULL; ++i) {
456195972f6Sopenharmony_ci	if (protp->protocol < 0xC000 && (protp->protocol & ~0x8000) == proto) {
457195972f6Sopenharmony_ci	    if (protp->active_pkt == NULL)
458195972f6Sopenharmony_ci		return 1;
459195972f6Sopenharmony_ci	    return (*protp->active_pkt)(p, len);
460195972f6Sopenharmony_ci	}
461195972f6Sopenharmony_ci    }
462195972f6Sopenharmony_ci    return 0;			/* not a supported protocol !!?? */
463195972f6Sopenharmony_ci}
464195972f6Sopenharmony_ci
465195972f6Sopenharmony_ci#endif /* PPP_SUPPORT && DEMAND_SUPPORT */
466