if_ppp.c revision 1.98 1 /* $NetBSD: if_ppp.c,v 1.98 2005/03/31 17:07:39 explorer Exp $ */
2 /* Id: if_ppp.c,v 1.6 1997/03/04 03:33:00 paulus Exp */
3
4 /*
5 * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
6 *
7 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The name "Carnegie Mellon University" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For permission or any legal
24 * details, please contact
25 * Office of Technology Transfer
26 * Carnegie Mellon University
27 * 5000 Forbes Avenue
28 * Pittsburgh, PA 15213-3890
29 * (412) 268-4387, fax: (412) 268-7395
30 * tech-transfer (at) andrew.cmu.edu
31 *
32 * 4. Redistributions of any form whatsoever must retain the following
33 * acknowledgment:
34 * "This product includes software developed by Computing Services
35 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
36 *
37 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
38 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
39 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
40 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
41 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
42 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
43 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
44 *
45 * Based on:
46 * @(#)if_sl.c 7.6.1.2 (Berkeley) 2/15/89
47 *
48 * Copyright (c) 1987 Regents of the University of California.
49 * All rights reserved.
50 *
51 * Redistribution and use in source and binary forms are permitted
52 * provided that the above copyright notice and this paragraph are
53 * duplicated in all such forms and that any documentation,
54 * advertising materials, and other materials related to such
55 * distribution and use acknowledge that the software was developed
56 * by the University of California, Berkeley. The name of the
57 * University may not be used to endorse or promote products derived
58 * from this software without specific prior written permission.
59 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
60 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
61 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
62 *
63 * Serial Line interface
64 *
65 * Rick Adams
66 * Center for Seismic Studies
67 * 1300 N 17th Street, Suite 1450
68 * Arlington, Virginia 22209
69 * (703)276-7900
70 * rick (at) seismo.ARPA
71 * seismo!rick
72 *
73 * Pounded on heavily by Chris Torek (chris (at) mimsy.umd.edu, umcp-cs!chris).
74 * Converted to 4.3BSD Beta by Chris Torek.
75 * Other changes made at Berkeley, based in part on code by Kirk Smith.
76 *
77 * Converted to 4.3BSD+ 386BSD by Brad Parker (brad (at) cayman.com)
78 * Added VJ tcp header compression; more unified ioctls
79 *
80 * Extensively modified by Paul Mackerras (paulus (at) cs.anu.edu.au).
81 * Cleaned up a lot of the mbuf-related code to fix bugs that
82 * caused system crashes and packet corruption. Changed pppstart
83 * so that it doesn't just give up with a collision if the whole
84 * packet doesn't fit in the output ring buffer.
85 *
86 * Added priority queueing for interactive IP packets, following
87 * the model of if_sl.c, plus hooks for bpf.
88 * Paul Mackerras (paulus (at) cs.anu.edu.au).
89 */
90
91 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
92 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
93
94 /*
95 * XXX IMP ME HARDER
96 *
97 * This is an explanation of that comment. This code used to use
98 * splimp() to block both network and tty interrupts. However,
99 * that call is deprecated. So, we have replaced the uses of
100 * splimp() with splhigh() in order to applomplish what it needs
101 * to accomplish, and added that happy little comment.
102 */
103
104 #include <sys/cdefs.h>
105 __KERNEL_RCSID(0, "$NetBSD: if_ppp.c,v 1.98 2005/03/31 17:07:39 explorer Exp $");
106
107 #include "ppp.h"
108
109 #include "opt_inet.h"
110 #include "opt_gateway.h"
111 #include "opt_ppp.h"
112
113 #ifdef INET
114 #define VJC
115 #endif
116 #define PPP_COMPRESS
117
118 #include <sys/param.h>
119 #include <sys/proc.h>
120 #include <sys/mbuf.h>
121 #include <sys/socket.h>
122 #include <sys/ioctl.h>
123 #include <sys/kernel.h>
124 #include <sys/systm.h>
125 #include <sys/time.h>
126 #include <sys/malloc.h>
127
128 #include <net/if.h>
129 #include <net/if_types.h>
130 #include <net/netisr.h>
131 #include <net/route.h>
132 #ifdef PPP_FILTER
133 #include <net/bpf.h>
134 #endif
135
136 #include <machine/intr.h>
137
138 #include <netinet/in.h>
139 #include <netinet/in_systm.h>
140 #include <netinet/in_var.h>
141 #ifdef INET
142 #include <netinet/ip.h>
143 #endif
144
145 #include "bpfilter.h"
146 #if NBPFILTER > 0
147 #include <sys/time.h>
148 #include <net/bpf.h>
149 #endif
150
151 #if defined(PPP_FILTER) || NBPFILTER > 0
152 #include <net/slip.h>
153 #endif
154
155 #ifdef VJC
156 #include <net/slcompress.h>
157 #endif
158
159 #include <net/ppp_defs.h>
160 #include <net/if_ppp.h>
161 #include <net/if_pppvar.h>
162 #include <machine/cpu.h>
163
164 #ifdef PPP_COMPRESS
165 #define PACKETPTR struct mbuf *
166 #include <net/ppp-comp.h>
167 #endif
168
169 static int pppsioctl __P((struct ifnet *, u_long, caddr_t));
170 static void ppp_requeue __P((struct ppp_softc *));
171 static void ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd));
172 static void ppp_ccp_closed __P((struct ppp_softc *));
173 static void ppp_inproc __P((struct ppp_softc *, struct mbuf *));
174 static void pppdumpm __P((struct mbuf *m0));
175 #ifdef ALTQ
176 static void ppp_ifstart __P((struct ifnet *ifp));
177 #endif
178
179 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
180 void pppnetisr(void);
181 #endif
182 void pppintr(void *);
183
184 /*
185 * Some useful mbuf macros not in mbuf.h.
186 */
187 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
188
189 #define M_DATASTART(m) \
190 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
191 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
192
193 #define M_DATASIZE(m) \
194 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
195 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
196
197 /*
198 * We define two link layer specific mbuf flags, to mark high-priority
199 * packets for output, and received packets following lost/corrupted
200 * packets.
201 */
202 #define M_HIGHPRI M_LINK0 /* output packet for sc_fastq */
203 #define M_ERRMARK M_LINK1 /* rx packet following lost/corrupted pkt */
204
205 static int ppp_clone_create(struct if_clone *, int);
206 static int ppp_clone_destroy(struct ifnet *);
207
208 static struct ppp_softc *ppp_create(const char *, int);
209
210 static LIST_HEAD(, ppp_softc) ppp_softc_list;
211
212 struct if_clone ppp_cloner =
213 IF_CLONE_INITIALIZER("ppp", ppp_clone_create, ppp_clone_destroy);
214
215 static struct simplelock ppp_list_mutex = SIMPLELOCK_INITIALIZER;
216
217 #ifdef PPP_COMPRESS
218 /*
219 * List of compressors we know about.
220 * We leave some space so maybe we can modload compressors.
221 */
222
223 extern struct compressor ppp_bsd_compress;
224 extern struct compressor ppp_deflate, ppp_deflate_draft;
225
226 struct compressor *ppp_compressors[PPP_COMPRESSORS_MAX] = {
227 #if DO_BSD_COMPRESS && defined(PPP_BSDCOMP)
228 &ppp_bsd_compress,
229 #endif
230 #if DO_DEFLATE && defined(PPP_DEFLATE)
231 &ppp_deflate,
232 &ppp_deflate_draft,
233 #endif
234 NULL
235 };
236 #endif /* PPP_COMPRESS */
237
238
239 /*
240 * Called from boot code to establish ppp interfaces.
241 */
242 void
243 pppattach(void)
244 {
245 LIST_INIT(&ppp_softc_list);
246 if_clone_attach(&ppp_cloner);
247 }
248
249 static struct ppp_softc *
250 ppp_create(const char *name, int unit)
251 {
252 struct ppp_softc *sc, *sci, *scl = NULL;
253
254 MALLOC(sc, struct ppp_softc *, sizeof(*sc), M_DEVBUF, M_WAIT|M_ZERO);
255
256 simple_lock(&ppp_list_mutex);
257 if (unit == -1) {
258 int i = 0;
259 LIST_FOREACH(sci, &ppp_softc_list, sc_iflist) {
260 scl = sci;
261 if (i < sci->sc_unit) {
262 unit = i;
263 break;
264 } else {
265 #ifdef DIAGNOSTIC
266 KASSERT(i == sci->sc_unit);
267 #endif
268 i++;
269 }
270 }
271 if (unit == -1)
272 unit = i;
273 } else {
274 LIST_FOREACH(sci, &ppp_softc_list, sc_iflist) {
275 scl = sci;
276 if (unit < sci->sc_unit)
277 break;
278 else if (unit == sci->sc_unit) {
279 FREE(sc, M_DEVBUF);
280 return NULL;
281 }
282 }
283 }
284
285 if (sci != NULL)
286 LIST_INSERT_BEFORE(sci, sc, sc_iflist);
287 else if (scl != NULL)
288 LIST_INSERT_AFTER(scl, sc, sc_iflist);
289 else
290 LIST_INSERT_HEAD(&ppp_softc_list, sc, sc_iflist);
291
292 simple_unlock(&ppp_list_mutex);
293
294 (void)snprintf(sc->sc_if.if_xname, sizeof(sc->sc_if.if_xname), "%s%d",
295 name, sc->sc_unit = unit);
296 callout_init(&sc->sc_timo_ch);
297 sc->sc_if.if_softc = sc;
298 sc->sc_if.if_mtu = PPP_MTU;
299 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
300 sc->sc_if.if_type = IFT_PPP;
301 sc->sc_if.if_hdrlen = PPP_HDRLEN;
302 sc->sc_if.if_dlt = DLT_NULL;
303 sc->sc_if.if_ioctl = pppsioctl;
304 sc->sc_if.if_output = pppoutput;
305 #ifdef ALTQ
306 sc->sc_if.if_start = ppp_ifstart;
307 #endif
308 IFQ_SET_MAXLEN(&sc->sc_if.if_snd, IFQ_MAXLEN);
309 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
310 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
311 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
312 IFQ_SET_READY(&sc->sc_if.if_snd);
313 if_attach(&sc->sc_if);
314 if_alloc_sadl(&sc->sc_if);
315 #if NBPFILTER > 0
316 bpfattach(&sc->sc_if, DLT_NULL, 0);
317 #endif
318 return sc;
319 }
320
321 static int
322 ppp_clone_create(struct if_clone *ifc, int unit)
323 {
324 return ppp_create(ifc->ifc_name, unit) == NULL ? EEXIST : 0;
325 }
326
327 static int
328 ppp_clone_destroy(struct ifnet *ifp)
329 {
330 struct ppp_softc *sc = (struct ppp_softc *)ifp->if_softc;
331
332 if (sc->sc_devp != NULL)
333 return EBUSY; /* Not removing it */
334
335 simple_lock(&ppp_list_mutex);
336 LIST_REMOVE(sc, sc_iflist);
337 simple_unlock(&ppp_list_mutex);
338
339 #if NBPFILTER > 0
340 bpfdetach(ifp);
341 #endif
342 if_detach(ifp);
343
344 FREE(sc, M_DEVBUF);
345 return 0;
346 }
347
348 /*
349 * Allocate a ppp interface unit and initialize it.
350 */
351 struct ppp_softc *
352 pppalloc(pid)
353 pid_t pid;
354 {
355 struct ppp_softc *sc = NULL, *scf;
356 int i;
357
358 simple_lock(&ppp_list_mutex);
359 for (scf = LIST_FIRST(&ppp_softc_list); scf != NULL;
360 scf = LIST_NEXT(scf, sc_iflist)) {
361 if (scf->sc_xfer == pid) {
362 scf->sc_xfer = 0;
363 simple_unlock(&ppp_list_mutex);
364 return scf;
365 }
366 if (scf->sc_devp == NULL && sc == NULL)
367 sc = scf;
368 }
369 simple_unlock(&ppp_list_mutex);
370
371 if (sc == NULL)
372 sc = ppp_create(ppp_cloner.ifc_name, -1);
373
374 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
375 sc->sc_si = softintr_establish(IPL_SOFTNET, pppintr, sc);
376 if (sc->sc_si == NULL) {
377 printf("%s: unable to establish softintr\n", sc->sc_if.if_xname);
378 return (NULL);
379 }
380 #endif
381 sc->sc_flags = 0;
382 sc->sc_mru = PPP_MRU;
383 sc->sc_relinq = NULL;
384 (void)memset(&sc->sc_stats, 0, sizeof(sc->sc_stats));
385 #ifdef VJC
386 MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
387 M_DEVBUF, M_NOWAIT);
388 if (sc->sc_comp)
389 sl_compress_init(sc->sc_comp);
390 #endif
391 #ifdef PPP_COMPRESS
392 sc->sc_xc_state = NULL;
393 sc->sc_rc_state = NULL;
394 #endif /* PPP_COMPRESS */
395 for (i = 0; i < NUM_NP; ++i)
396 sc->sc_npmode[i] = NPMODE_ERROR;
397 sc->sc_npqueue = NULL;
398 sc->sc_npqtail = &sc->sc_npqueue;
399 sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;
400
401 return sc;
402 }
403
404 /*
405 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
406 */
407 void
408 pppdealloc(sc)
409 struct ppp_softc *sc;
410 {
411 struct mbuf *m;
412
413 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
414 softintr_disestablish(sc->sc_si);
415 #endif
416 if_down(&sc->sc_if);
417 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
418 sc->sc_devp = NULL;
419 sc->sc_xfer = 0;
420 for (;;) {
421 IF_DEQUEUE(&sc->sc_rawq, m);
422 if (m == NULL)
423 break;
424 m_freem(m);
425 }
426 for (;;) {
427 IF_DEQUEUE(&sc->sc_inq, m);
428 if (m == NULL)
429 break;
430 m_freem(m);
431 }
432 for (;;) {
433 IF_DEQUEUE(&sc->sc_fastq, m);
434 if (m == NULL)
435 break;
436 m_freem(m);
437 }
438 while ((m = sc->sc_npqueue) != NULL) {
439 sc->sc_npqueue = m->m_nextpkt;
440 m_freem(m);
441 }
442 if (sc->sc_togo != NULL) {
443 m_freem(sc->sc_togo);
444 sc->sc_togo = NULL;
445 }
446 #ifdef PPP_COMPRESS
447 ppp_ccp_closed(sc);
448 sc->sc_xc_state = NULL;
449 sc->sc_rc_state = NULL;
450 #endif /* PPP_COMPRESS */
451 #ifdef PPP_FILTER
452 if (sc->sc_pass_filt_in.bf_insns != 0) {
453 FREE(sc->sc_pass_filt_in.bf_insns, M_DEVBUF);
454 sc->sc_pass_filt_in.bf_insns = 0;
455 sc->sc_pass_filt_in.bf_len = 0;
456 }
457 if (sc->sc_pass_filt_out.bf_insns != 0) {
458 FREE(sc->sc_pass_filt_out.bf_insns, M_DEVBUF);
459 sc->sc_pass_filt_out.bf_insns = 0;
460 sc->sc_pass_filt_out.bf_len = 0;
461 }
462 if (sc->sc_active_filt_in.bf_insns != 0) {
463 FREE(sc->sc_active_filt_in.bf_insns, M_DEVBUF);
464 sc->sc_active_filt_in.bf_insns = 0;
465 sc->sc_active_filt_in.bf_len = 0;
466 }
467 if (sc->sc_active_filt_out.bf_insns != 0) {
468 FREE(sc->sc_active_filt_out.bf_insns, M_DEVBUF);
469 sc->sc_active_filt_out.bf_insns = 0;
470 sc->sc_active_filt_out.bf_len = 0;
471 }
472 #endif /* PPP_FILTER */
473 #ifdef VJC
474 if (sc->sc_comp != 0) {
475 FREE(sc->sc_comp, M_DEVBUF);
476 sc->sc_comp = 0;
477 }
478 #endif
479 (void)ppp_clone_destroy(&sc->sc_if);
480 }
481
482 /*
483 * Ioctl routine for generic ppp devices.
484 */
485 int
486 pppioctl(sc, cmd, data, flag, p)
487 struct ppp_softc *sc;
488 u_long cmd;
489 caddr_t data;
490 int flag;
491 struct proc *p;
492 {
493 int s, error, flags, mru, npx;
494 u_int nb;
495 struct ppp_option_data *odp;
496 struct compressor **cp;
497 struct npioctl *npi;
498 time_t t;
499 #ifdef PPP_FILTER
500 struct bpf_program *bp, *nbp;
501 struct bpf_insn *newcode, *oldcode;
502 int newcodelen;
503 #endif /* PPP_FILTER */
504 #ifdef PPP_COMPRESS
505 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
506 #endif
507
508 switch (cmd) {
509 case FIONREAD:
510 *(int *)data = sc->sc_inq.ifq_len;
511 break;
512
513 case PPPIOCGUNIT:
514 *(int *)data = sc->sc_unit;
515 break;
516
517 case PPPIOCGFLAGS:
518 *(u_int *)data = sc->sc_flags;
519 break;
520
521 case PPPIOCGRAWIN:
522 {
523 struct ppp_rawin *rwin = (struct ppp_rawin *)data;
524 u_char p, q = 0;
525
526 for (p = sc->sc_rawin_start; p < sizeof(sc->sc_rawin.buf);)
527 rwin->buf[q++] = sc->sc_rawin.buf[p++];
528
529 for (p = 0; p < sc->sc_rawin_start;)
530 rwin->buf[q++] = sc->sc_rawin.buf[p++];
531
532 rwin->count = sc->sc_rawin.count;
533 }
534 break;
535
536 case PPPIOCSFLAGS:
537 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
538 return (error);
539 flags = *(int *)data & SC_MASK;
540 s = splsoftnet();
541 #ifdef PPP_COMPRESS
542 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
543 ppp_ccp_closed(sc);
544 #endif
545 splhigh(); /* XXX IMP ME HARDER */
546 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
547 splx(s);
548 break;
549
550 case PPPIOCSMRU:
551 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
552 return (error);
553 mru = *(int *)data;
554 if (mru >= PPP_MINMRU && mru <= PPP_MAXMRU)
555 sc->sc_mru = mru;
556 break;
557
558 case PPPIOCGMRU:
559 *(int *)data = sc->sc_mru;
560 break;
561
562 #ifdef VJC
563 case PPPIOCSMAXCID:
564 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
565 return (error);
566 if (sc->sc_comp) {
567 s = splsoftnet();
568 sl_compress_setup(sc->sc_comp, *(int *)data);
569 splx(s);
570 }
571 break;
572 #endif
573
574 case PPPIOCXFERUNIT:
575 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
576 return (error);
577 sc->sc_xfer = p->p_pid;
578 break;
579
580 #ifdef PPP_COMPRESS
581 case PPPIOCSCOMPRESS:
582 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
583 return (error);
584 odp = (struct ppp_option_data *) data;
585 nb = odp->length;
586 if (nb > sizeof(ccp_option))
587 nb = sizeof(ccp_option);
588 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
589 return (error);
590 if (ccp_option[1] < 2) /* preliminary check on the length byte */
591 return (EINVAL);
592 for (cp = ppp_compressors; *cp != NULL; ++cp)
593 if ((*cp)->compress_proto == ccp_option[0]) {
594 /*
595 * Found a handler for the protocol - try to allocate
596 * a compressor or decompressor.
597 */
598 error = 0;
599 if (odp->transmit) {
600 s = splsoftnet();
601 if (sc->sc_xc_state != NULL)
602 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
603 sc->sc_xcomp = *cp;
604 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
605 if (sc->sc_xc_state == NULL) {
606 if (sc->sc_flags & SC_DEBUG)
607 printf("%s: comp_alloc failed\n",
608 sc->sc_if.if_xname);
609 error = ENOBUFS;
610 }
611 splhigh(); /* XXX IMP ME HARDER */
612 sc->sc_flags &= ~SC_COMP_RUN;
613 splx(s);
614 } else {
615 s = splsoftnet();
616 if (sc->sc_rc_state != NULL)
617 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
618 sc->sc_rcomp = *cp;
619 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
620 if (sc->sc_rc_state == NULL) {
621 if (sc->sc_flags & SC_DEBUG)
622 printf("%s: decomp_alloc failed\n",
623 sc->sc_if.if_xname);
624 error = ENOBUFS;
625 }
626 splhigh(); /* XXX IMP ME HARDER */
627 sc->sc_flags &= ~SC_DECOMP_RUN;
628 splx(s);
629 }
630 return (error);
631 }
632 if (sc->sc_flags & SC_DEBUG)
633 printf("%s: no compressor for [%x %x %x], %x\n",
634 sc->sc_if.if_xname, ccp_option[0], ccp_option[1],
635 ccp_option[2], nb);
636 return (EINVAL); /* no handler found */
637 #endif /* PPP_COMPRESS */
638
639 case PPPIOCGNPMODE:
640 case PPPIOCSNPMODE:
641 npi = (struct npioctl *) data;
642 switch (npi->protocol) {
643 case PPP_IP:
644 npx = NP_IP;
645 break;
646 case PPP_IPV6:
647 npx = NP_IPV6;
648 break;
649 default:
650 return EINVAL;
651 }
652 if (cmd == PPPIOCGNPMODE) {
653 npi->mode = sc->sc_npmode[npx];
654 } else {
655 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
656 return (error);
657 if (npi->mode != sc->sc_npmode[npx]) {
658 s = splnet();
659 sc->sc_npmode[npx] = npi->mode;
660 if (npi->mode != NPMODE_QUEUE) {
661 ppp_requeue(sc);
662 ppp_restart(sc);
663 }
664 splx(s);
665 }
666 }
667 break;
668
669 case PPPIOCGIDLE:
670 s = splsoftnet();
671 t = time.tv_sec;
672 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
673 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
674 splx(s);
675 break;
676
677 #ifdef PPP_FILTER
678 case PPPIOCSPASS:
679 case PPPIOCSACTIVE:
680 /* These are no longer supported. */
681 return EOPNOTSUPP;
682
683 case PPPIOCSIPASS:
684 case PPPIOCSOPASS:
685 case PPPIOCSIACTIVE:
686 case PPPIOCSOACTIVE:
687 nbp = (struct bpf_program *) data;
688 if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
689 return EINVAL;
690 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
691 if (newcodelen != 0) {
692 newcode = malloc(newcodelen, M_DEVBUF, M_WAITOK);
693 /* WAITOK -- malloc() never fails. */
694 if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
695 newcodelen)) != 0) {
696 free(newcode, M_DEVBUF);
697 return error;
698 }
699 if (!bpf_validate(newcode, nbp->bf_len)) {
700 free(newcode, M_DEVBUF);
701 return EINVAL;
702 }
703 } else
704 newcode = 0;
705 switch (cmd) {
706 case PPPIOCSIPASS:
707 bp = &sc->sc_pass_filt_in;
708 break;
709
710 case PPPIOCSOPASS:
711 bp = &sc->sc_pass_filt_out;
712 break;
713
714 case PPPIOCSIACTIVE:
715 bp = &sc->sc_active_filt_in;
716 break;
717
718 case PPPIOCSOACTIVE:
719 bp = &sc->sc_active_filt_out;
720 break;
721 default:
722 free(newcode, M_DEVBUF);
723 return (EPASSTHROUGH);
724 }
725 oldcode = bp->bf_insns;
726 s = splnet();
727 bp->bf_len = nbp->bf_len;
728 bp->bf_insns = newcode;
729 splx(s);
730 if (oldcode != 0)
731 free(oldcode, M_DEVBUF);
732 break;
733 #endif /* PPP_FILTER */
734
735 default:
736 return (EPASSTHROUGH);
737 }
738 return (0);
739 }
740
741 /*
742 * Process an ioctl request to the ppp network interface.
743 */
744 static int
745 pppsioctl(ifp, cmd, data)
746 struct ifnet *ifp;
747 u_long cmd;
748 caddr_t data;
749 {
750 struct proc *p = curproc; /* XXX */
751 struct ppp_softc *sc = ifp->if_softc;
752 struct ifaddr *ifa = (struct ifaddr *)data;
753 struct ifreq *ifr = (struct ifreq *)data;
754 struct ppp_stats *psp;
755 #ifdef PPP_COMPRESS
756 struct ppp_comp_stats *pcp;
757 #endif
758 int s = splnet(), error = 0;
759
760 switch (cmd) {
761 case SIOCSIFFLAGS:
762 if ((ifp->if_flags & IFF_RUNNING) == 0)
763 ifp->if_flags &= ~IFF_UP;
764 break;
765
766 case SIOCSIFADDR:
767 switch (ifa->ifa_addr->sa_family) {
768 #ifdef INET
769 case AF_INET:
770 break;
771 #endif
772 #ifdef INET6
773 case AF_INET6:
774 break;
775 #endif
776 default:
777 error = EAFNOSUPPORT;
778 break;
779 }
780 break;
781
782 case SIOCSIFDSTADDR:
783 switch (ifa->ifa_addr->sa_family) {
784 #ifdef INET
785 case AF_INET:
786 break;
787 #endif
788 #ifdef INET6
789 case AF_INET6:
790 break;
791 #endif
792 default:
793 error = EAFNOSUPPORT;
794 break;
795 }
796 break;
797
798 case SIOCSIFMTU:
799 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
800 break;
801 sc->sc_if.if_mtu = ifr->ifr_mtu;
802 break;
803
804 case SIOCGIFMTU:
805 ifr->ifr_mtu = sc->sc_if.if_mtu;
806 break;
807
808 case SIOCADDMULTI:
809 case SIOCDELMULTI:
810 if (ifr == 0) {
811 error = EAFNOSUPPORT;
812 break;
813 }
814 switch (ifr->ifr_addr.sa_family) {
815 #ifdef INET
816 case AF_INET:
817 break;
818 #endif
819 #ifdef INET6
820 case AF_INET6:
821 break;
822 #endif
823 default:
824 error = EAFNOSUPPORT;
825 break;
826 }
827 break;
828
829 case SIOCGPPPSTATS:
830 psp = &((struct ifpppstatsreq *) data)->stats;
831 memset(psp, 0, sizeof(*psp));
832 psp->p = sc->sc_stats;
833 #if defined(VJC) && !defined(SL_NO_STATS)
834 if (sc->sc_comp) {
835 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
836 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
837 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
838 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
839 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
840 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
841 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
842 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
843 }
844 #endif /* VJC */
845 break;
846
847 #ifdef PPP_COMPRESS
848 case SIOCGPPPCSTATS:
849 pcp = &((struct ifpppcstatsreq *) data)->stats;
850 memset(pcp, 0, sizeof(*pcp));
851 if (sc->sc_xc_state != NULL)
852 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
853 if (sc->sc_rc_state != NULL)
854 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
855 break;
856 #endif /* PPP_COMPRESS */
857
858 default:
859 error = EINVAL;
860 }
861 splx(s);
862 return (error);
863 }
864
865 /*
866 * Queue a packet. Start transmission if not active.
867 * Packet is placed in Information field of PPP frame.
868 */
869 int
870 pppoutput(ifp, m0, dst, rtp)
871 struct ifnet *ifp;
872 struct mbuf *m0;
873 struct sockaddr *dst;
874 struct rtentry *rtp;
875 {
876 struct ppp_softc *sc = ifp->if_softc;
877 int protocol, address, control;
878 u_char *cp;
879 int s, error;
880 #ifdef INET
881 struct ip *ip;
882 #endif
883 struct ifqueue *ifq;
884 enum NPmode mode;
885 int len;
886 ALTQ_DECL(struct altq_pktattr pktattr;)
887
888 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
889 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
890 error = ENETDOWN; /* sort of */
891 goto bad;
892 }
893
894 IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family, &pktattr);
895
896 /*
897 * Compute PPP header.
898 */
899 m0->m_flags &= ~M_HIGHPRI;
900 switch (dst->sa_family) {
901 #ifdef INET
902 case AF_INET:
903 address = PPP_ALLSTATIONS;
904 control = PPP_UI;
905 protocol = PPP_IP;
906 mode = sc->sc_npmode[NP_IP];
907
908 /*
909 * If this packet has the "low delay" bit set in the IP header,
910 * put it on the fastq instead.
911 */
912 ip = mtod(m0, struct ip *);
913 if (ip->ip_tos & IPTOS_LOWDELAY)
914 m0->m_flags |= M_HIGHPRI;
915 break;
916 #endif
917 #ifdef INET6
918 case AF_INET6:
919 address = PPP_ALLSTATIONS; /*XXX*/
920 control = PPP_UI; /*XXX*/
921 protocol = PPP_IPV6;
922 mode = sc->sc_npmode[NP_IPV6];
923
924 #if 0 /* XXX flowinfo/traffic class, maybe? */
925 /*
926 * If this packet has the "low delay" bit set in the IP header,
927 * put it on the fastq instead.
928 */
929 ip = mtod(m0, struct ip *);
930 if (ip->ip_tos & IPTOS_LOWDELAY)
931 m0->m_flags |= M_HIGHPRI;
932 #endif
933 break;
934 #endif
935 case AF_UNSPEC:
936 address = PPP_ADDRESS(dst->sa_data);
937 control = PPP_CONTROL(dst->sa_data);
938 protocol = PPP_PROTOCOL(dst->sa_data);
939 mode = NPMODE_PASS;
940 break;
941 default:
942 printf("%s: af%d not supported\n", ifp->if_xname, dst->sa_family);
943 error = EAFNOSUPPORT;
944 goto bad;
945 }
946
947 /*
948 * Drop this packet, or return an error, if necessary.
949 */
950 if (mode == NPMODE_ERROR) {
951 error = ENETDOWN;
952 goto bad;
953 }
954 if (mode == NPMODE_DROP) {
955 error = 0;
956 goto bad;
957 }
958
959 /*
960 * Add PPP header.
961 */
962 M_PREPEND(m0, PPP_HDRLEN, M_DONTWAIT);
963 if (m0 == NULL) {
964 error = ENOBUFS;
965 goto bad;
966 }
967
968 cp = mtod(m0, u_char *);
969 *cp++ = address;
970 *cp++ = control;
971 *cp++ = protocol >> 8;
972 *cp++ = protocol & 0xff;
973
974 len = m_length(m0);
975
976 if (sc->sc_flags & SC_LOG_OUTPKT) {
977 printf("%s output: ", ifp->if_xname);
978 pppdumpm(m0);
979 }
980
981 if ((protocol & 0x8000) == 0) {
982 #ifdef PPP_FILTER
983 /*
984 * Apply the pass and active filters to the packet,
985 * but only if it is a data packet.
986 */
987 if (sc->sc_pass_filt_out.bf_insns != 0
988 && bpf_filter(sc->sc_pass_filt_out.bf_insns, (u_char *) m0,
989 len, 0) == 0) {
990 error = 0; /* drop this packet */
991 goto bad;
992 }
993
994 /*
995 * Update the time we sent the most recent packet.
996 */
997 if (sc->sc_active_filt_out.bf_insns == 0
998 || bpf_filter(sc->sc_active_filt_out.bf_insns, (u_char *) m0,
999 len, 0))
1000 sc->sc_last_sent = time.tv_sec;
1001 #else
1002 /*
1003 * Update the time we sent the most recent packet.
1004 */
1005 sc->sc_last_sent = time.tv_sec;
1006 #endif /* PPP_FILTER */
1007 }
1008
1009 #if NBPFILTER > 0
1010 /*
1011 * See if bpf wants to look at the packet.
1012 */
1013 if (sc->sc_if.if_bpf)
1014 bpf_mtap(sc->sc_if.if_bpf, m0);
1015 #endif
1016
1017 /*
1018 * Put the packet on the appropriate queue.
1019 */
1020 s = splnet();
1021 if (mode == NPMODE_QUEUE) {
1022 /* XXX we should limit the number of packets on this queue */
1023 *sc->sc_npqtail = m0;
1024 m0->m_nextpkt = NULL;
1025 sc->sc_npqtail = &m0->m_nextpkt;
1026 } else {
1027 ifq = NULL;
1028 if (m0->m_flags & M_HIGHPRI)
1029 ifq = &sc->sc_fastq;
1030 if ((error = ifq_enqueue2(&sc->sc_if, ifq, m0
1031 ALTQ_COMMA ALTQ_DECL(&pktattr))) != 0) {
1032 splx(s);
1033 sc->sc_if.if_oerrors++;
1034 sc->sc_stats.ppp_oerrors++;
1035 return (error);
1036 }
1037 ppp_restart(sc);
1038 }
1039 ifp->if_opackets++;
1040 ifp->if_obytes += len;
1041
1042 splx(s);
1043 return (0);
1044
1045 bad:
1046 m_freem(m0);
1047 return (error);
1048 }
1049
1050 /*
1051 * After a change in the NPmode for some NP, move packets from the
1052 * npqueue to the send queue or the fast queue as appropriate.
1053 * Should be called at splnet, since we muck with the queues.
1054 */
1055 static void
1056 ppp_requeue(sc)
1057 struct ppp_softc *sc;
1058 {
1059 struct mbuf *m, **mpp;
1060 struct ifqueue *ifq;
1061 enum NPmode mode;
1062 int error;
1063
1064 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
1065 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
1066 case PPP_IP:
1067 mode = sc->sc_npmode[NP_IP];
1068 break;
1069 case PPP_IPV6:
1070 mode = sc->sc_npmode[NP_IPV6];
1071 break;
1072 default:
1073 mode = NPMODE_PASS;
1074 }
1075
1076 switch (mode) {
1077 case NPMODE_PASS:
1078 /*
1079 * This packet can now go on one of the queues to be sent.
1080 */
1081 *mpp = m->m_nextpkt;
1082 m->m_nextpkt = NULL;
1083 ifq = NULL;
1084 if (m->m_flags & M_HIGHPRI)
1085 ifq = &sc->sc_fastq;
1086 if ((error = ifq_enqueue2(&sc->sc_if, ifq, m ALTQ_COMMA
1087 ALTQ_DECL(NULL))) != 0) {
1088 sc->sc_if.if_oerrors++;
1089 sc->sc_stats.ppp_oerrors++;
1090 }
1091 break;
1092
1093 case NPMODE_DROP:
1094 case NPMODE_ERROR:
1095 *mpp = m->m_nextpkt;
1096 m_freem(m);
1097 break;
1098
1099 case NPMODE_QUEUE:
1100 mpp = &m->m_nextpkt;
1101 break;
1102 }
1103 }
1104 sc->sc_npqtail = mpp;
1105 }
1106
1107 /*
1108 * Transmitter has finished outputting some stuff;
1109 * remember to call sc->sc_start later at splsoftnet.
1110 */
1111 void
1112 ppp_restart(sc)
1113 struct ppp_softc *sc;
1114 {
1115 int s = splhigh(); /* XXX IMP ME HARDER */
1116
1117 sc->sc_flags &= ~SC_TBUSY;
1118 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
1119 softintr_schedule(sc->sc_si);
1120 #else
1121 schednetisr(NETISR_PPP);
1122 #endif
1123 splx(s);
1124 }
1125
1126 /*
1127 * Get a packet to send. This procedure is intended to be called at
1128 * splsoftnet, since it may involve time-consuming operations such as
1129 * applying VJ compression, packet compression, address/control and/or
1130 * protocol field compression to the packet.
1131 */
1132 struct mbuf *
1133 ppp_dequeue(sc)
1134 struct ppp_softc *sc;
1135 {
1136 struct mbuf *m, *mp;
1137 u_char *cp;
1138 int address, control, protocol;
1139 int s;
1140
1141 /*
1142 * Grab a packet to send: first try the fast queue, then the
1143 * normal queue.
1144 */
1145 s = splnet();
1146 IF_DEQUEUE(&sc->sc_fastq, m);
1147 if (m == NULL)
1148 IFQ_DEQUEUE(&sc->sc_if.if_snd, m);
1149 splx(s);
1150
1151 if (m == NULL)
1152 return NULL;
1153
1154 ++sc->sc_stats.ppp_opackets;
1155
1156 /*
1157 * Extract the ppp header of the new packet.
1158 * The ppp header will be in one mbuf.
1159 */
1160 cp = mtod(m, u_char *);
1161 address = PPP_ADDRESS(cp);
1162 control = PPP_CONTROL(cp);
1163 protocol = PPP_PROTOCOL(cp);
1164
1165 switch (protocol) {
1166 case PPP_IP:
1167 #ifdef VJC
1168 /*
1169 * If the packet is a TCP/IP packet, see if we can compress it.
1170 */
1171 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
1172 struct ip *ip;
1173 int type;
1174
1175 mp = m;
1176 ip = (struct ip *) (cp + PPP_HDRLEN);
1177 if (mp->m_len <= PPP_HDRLEN) {
1178 mp = mp->m_next;
1179 if (mp == NULL)
1180 break;
1181 ip = mtod(mp, struct ip *);
1182 }
1183 /* this code assumes the IP/TCP header is in one non-shared mbuf */
1184 if (ip->ip_p == IPPROTO_TCP) {
1185 type = sl_compress_tcp(mp, ip, sc->sc_comp,
1186 !(sc->sc_flags & SC_NO_TCP_CCID));
1187 switch (type) {
1188 case TYPE_UNCOMPRESSED_TCP:
1189 protocol = PPP_VJC_UNCOMP;
1190 break;
1191 case TYPE_COMPRESSED_TCP:
1192 protocol = PPP_VJC_COMP;
1193 cp = mtod(m, u_char *);
1194 cp[0] = address; /* header has moved */
1195 cp[1] = control;
1196 cp[2] = 0;
1197 break;
1198 }
1199 cp[3] = protocol; /* update protocol in PPP header */
1200 }
1201 }
1202 #endif /* VJC */
1203 break;
1204
1205 #ifdef PPP_COMPRESS
1206 case PPP_CCP:
1207 ppp_ccp(sc, m, 0);
1208 break;
1209 #endif /* PPP_COMPRESS */
1210 }
1211
1212 #ifdef PPP_COMPRESS
1213 if (protocol != PPP_LCP && protocol != PPP_CCP
1214 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1215 struct mbuf *mcomp = NULL;
1216 int slen;
1217
1218 slen = 0;
1219 for (mp = m; mp != NULL; mp = mp->m_next)
1220 slen += mp->m_len;
1221 (*sc->sc_xcomp->compress)
1222 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
1223 if (mcomp != NULL) {
1224 if (sc->sc_flags & SC_CCP_UP) {
1225 /* Send the compressed packet instead of the original. */
1226 m_freem(m);
1227 m = mcomp;
1228 cp = mtod(m, u_char *);
1229 protocol = cp[3];
1230 } else {
1231 /* Can't transmit compressed packets until CCP is up. */
1232 m_freem(mcomp);
1233 }
1234 }
1235 }
1236 #endif /* PPP_COMPRESS */
1237
1238 /*
1239 * Compress the address/control and protocol, if possible.
1240 */
1241 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1242 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1243 protocol != PPP_LCP) {
1244 /* can compress address/control */
1245 m->m_data += 2;
1246 m->m_len -= 2;
1247 }
1248 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1249 /* can compress protocol */
1250 if (mtod(m, u_char *) == cp) {
1251 cp[2] = cp[1]; /* move address/control up */
1252 cp[1] = cp[0];
1253 }
1254 ++m->m_data;
1255 --m->m_len;
1256 }
1257
1258 return m;
1259 }
1260
1261 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
1262 void
1263 pppnetisr(void)
1264 {
1265 struct ppp_softc *sc;
1266
1267 for (sc = LIST_FIRST(&ppp_softc_list); sc != NULL;
1268 sc = LIST_NEXT(sc, sc_iflist))
1269 pppintr(sc);
1270 }
1271 #endif
1272
1273 /*
1274 * Software interrupt routine, called at splsoftnet.
1275 */
1276 void
1277 pppintr(void *arg)
1278 {
1279 struct ppp_softc *sc = arg;
1280 struct mbuf *m;
1281 int s;
1282
1283 if (!(sc->sc_flags & SC_TBUSY)
1284 && (IFQ_IS_EMPTY(&sc->sc_if.if_snd) == 0 || sc->sc_fastq.ifq_head
1285 || sc->sc_outm)) {
1286 s = splhigh(); /* XXX IMP ME HARDER */
1287 sc->sc_flags |= SC_TBUSY;
1288 splx(s);
1289 (*sc->sc_start)(sc);
1290 }
1291 for (;;) {
1292 s = splnet();
1293 IF_DEQUEUE(&sc->sc_rawq, m);
1294 splx(s);
1295 if (m == NULL)
1296 break;
1297 ppp_inproc(sc, m);
1298 }
1299 }
1300
1301 #ifdef PPP_COMPRESS
1302 /*
1303 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1304 * 0 if it is about to be transmitted.
1305 */
1306 static void
1307 ppp_ccp(sc, m, rcvd)
1308 struct ppp_softc *sc;
1309 struct mbuf *m;
1310 int rcvd;
1311 {
1312 u_char *dp, *ep;
1313 struct mbuf *mp;
1314 int slen, s;
1315
1316 /*
1317 * Get a pointer to the data after the PPP header.
1318 */
1319 if (m->m_len <= PPP_HDRLEN) {
1320 mp = m->m_next;
1321 if (mp == NULL)
1322 return;
1323 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1324 } else {
1325 mp = m;
1326 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1327 }
1328
1329 ep = mtod(mp, u_char *) + mp->m_len;
1330 if (dp + CCP_HDRLEN > ep)
1331 return;
1332 slen = CCP_LENGTH(dp);
1333 if (dp + slen > ep) {
1334 if (sc->sc_flags & SC_DEBUG)
1335 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1336 dp, slen, mtod(mp, u_char *), mp->m_len);
1337 return;
1338 }
1339
1340 switch (CCP_CODE(dp)) {
1341 case CCP_CONFREQ:
1342 case CCP_TERMREQ:
1343 case CCP_TERMACK:
1344 /* CCP must be going down - disable compression */
1345 if (sc->sc_flags & SC_CCP_UP) {
1346 s = splhigh(); /* XXX IMP ME HARDER */
1347 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1348 splx(s);
1349 }
1350 break;
1351
1352 case CCP_CONFACK:
1353 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1354 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1355 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1356 if (!rcvd) {
1357 /* we're agreeing to send compressed packets. */
1358 if (sc->sc_xc_state != NULL
1359 && (*sc->sc_xcomp->comp_init)
1360 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1361 sc->sc_unit, 0, sc->sc_flags & SC_DEBUG)) {
1362 s = splhigh(); /* XXX IMP ME HARDER */
1363 sc->sc_flags |= SC_COMP_RUN;
1364 splx(s);
1365 }
1366 } else {
1367 /* peer is agreeing to send compressed packets. */
1368 if (sc->sc_rc_state != NULL
1369 && (*sc->sc_rcomp->decomp_init)
1370 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1371 sc->sc_unit, 0, sc->sc_mru,
1372 sc->sc_flags & SC_DEBUG)) {
1373 s = splhigh(); /* XXX IMP ME HARDER */
1374 sc->sc_flags |= SC_DECOMP_RUN;
1375 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1376 splx(s);
1377 }
1378 }
1379 }
1380 break;
1381
1382 case CCP_RESETACK:
1383 if (sc->sc_flags & SC_CCP_UP) {
1384 if (!rcvd) {
1385 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1386 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1387 } else {
1388 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1389 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1390 s = splhigh(); /* XXX IMP ME HARDER */
1391 sc->sc_flags &= ~SC_DC_ERROR;
1392 splx(s);
1393 }
1394 }
1395 }
1396 break;
1397 }
1398 }
1399
1400 /*
1401 * CCP is down; free (de)compressor state if necessary.
1402 */
1403 static void
1404 ppp_ccp_closed(sc)
1405 struct ppp_softc *sc;
1406 {
1407 if (sc->sc_xc_state) {
1408 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1409 sc->sc_xc_state = NULL;
1410 }
1411 if (sc->sc_rc_state) {
1412 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1413 sc->sc_rc_state = NULL;
1414 }
1415 }
1416 #endif /* PPP_COMPRESS */
1417
1418 /*
1419 * PPP packet input routine.
1420 * The caller has checked and removed the FCS and has inserted
1421 * the address/control bytes and the protocol high byte if they
1422 * were omitted.
1423 */
1424 void
1425 ppppktin(sc, m, lost)
1426 struct ppp_softc *sc;
1427 struct mbuf *m;
1428 int lost;
1429 {
1430 int s = splhigh(); /* XXX IMP ME HARDER */
1431
1432 if (lost)
1433 m->m_flags |= M_ERRMARK;
1434 IF_ENQUEUE(&sc->sc_rawq, m);
1435 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
1436 softintr_schedule(sc->sc_si);
1437 #else
1438 schednetisr(NETISR_PPP);
1439 #endif
1440 splx(s);
1441 }
1442
1443 /*
1444 * Process a received PPP packet, doing decompression as necessary.
1445 * Should be called at splsoftnet.
1446 */
1447 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1448 TYPE_UNCOMPRESSED_TCP)
1449
1450 static void
1451 ppp_inproc(sc, m)
1452 struct ppp_softc *sc;
1453 struct mbuf *m;
1454 {
1455 struct ifnet *ifp = &sc->sc_if;
1456 struct ifqueue *inq;
1457 int s, ilen, proto, rv;
1458 u_char *cp, adrs, ctrl;
1459 struct mbuf *mp, *dmp = NULL;
1460 #ifdef VJC
1461 int xlen;
1462 u_char *iphdr;
1463 u_int hlen;
1464 #endif
1465
1466 sc->sc_stats.ppp_ipackets++;
1467
1468 if (sc->sc_flags & SC_LOG_INPKT) {
1469 ilen = 0;
1470 for (mp = m; mp != NULL; mp = mp->m_next)
1471 ilen += mp->m_len;
1472 printf("%s: got %d bytes\n", ifp->if_xname, ilen);
1473 pppdumpm(m);
1474 }
1475
1476 cp = mtod(m, u_char *);
1477 adrs = PPP_ADDRESS(cp);
1478 ctrl = PPP_CONTROL(cp);
1479 proto = PPP_PROTOCOL(cp);
1480
1481 if (m->m_flags & M_ERRMARK) {
1482 m->m_flags &= ~M_ERRMARK;
1483 s = splhigh(); /* XXX IMP ME HARDER */
1484 sc->sc_flags |= SC_VJ_RESET;
1485 splx(s);
1486 }
1487
1488 #ifdef PPP_COMPRESS
1489 /*
1490 * Decompress this packet if necessary, update the receiver's
1491 * dictionary, or take appropriate action on a CCP packet.
1492 */
1493 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1494 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1495 /* decompress this packet */
1496 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1497 if (rv == DECOMP_OK) {
1498 m_freem(m);
1499 if (dmp == NULL) {
1500 /* no error, but no decompressed packet produced */
1501 return;
1502 }
1503 m = dmp;
1504 cp = mtod(m, u_char *);
1505 proto = PPP_PROTOCOL(cp);
1506
1507 } else {
1508 /*
1509 * An error has occurred in decompression.
1510 * Pass the compressed packet up to pppd, which may take
1511 * CCP down or issue a Reset-Req.
1512 */
1513 if (sc->sc_flags & SC_DEBUG)
1514 printf("%s: decompress failed %d\n", ifp->if_xname, rv);
1515 s = splhigh(); /* XXX IMP ME HARDER */
1516 sc->sc_flags |= SC_VJ_RESET;
1517 if (rv == DECOMP_ERROR)
1518 sc->sc_flags |= SC_DC_ERROR;
1519 else
1520 sc->sc_flags |= SC_DC_FERROR;
1521 splx(s);
1522 }
1523
1524 } else {
1525 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1526 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1527 }
1528 if (proto == PPP_CCP) {
1529 ppp_ccp(sc, m, 1);
1530 }
1531 }
1532 #endif
1533
1534 ilen = 0;
1535 for (mp = m; mp != NULL; mp = mp->m_next)
1536 ilen += mp->m_len;
1537
1538 #ifdef VJC
1539 if (sc->sc_flags & SC_VJ_RESET) {
1540 /*
1541 * If we've missed a packet, we must toss subsequent compressed
1542 * packets which don't have an explicit connection ID.
1543 */
1544 if (sc->sc_comp)
1545 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1546 s = splhigh(); /* XXX IMP ME HARDER */
1547 sc->sc_flags &= ~SC_VJ_RESET;
1548 splx(s);
1549 }
1550
1551 /*
1552 * See if we have a VJ-compressed packet to uncompress.
1553 */
1554 if (proto == PPP_VJC_COMP) {
1555 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1556 goto bad;
1557
1558 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1559 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1560 sc->sc_comp, &iphdr, &hlen);
1561
1562 if (xlen <= 0) {
1563 if (sc->sc_flags & SC_DEBUG)
1564 printf("%s: VJ uncompress failed on type comp\n",
1565 ifp->if_xname);
1566 goto bad;
1567 }
1568
1569 /* Copy the PPP and IP headers into a new mbuf. */
1570 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1571 if (mp == NULL)
1572 goto bad;
1573 mp->m_len = 0;
1574 mp->m_next = NULL;
1575 if (hlen + PPP_HDRLEN > MHLEN) {
1576 MCLGET(mp, M_DONTWAIT);
1577 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1578 m_freem(mp);
1579 goto bad; /* lose if big headers and no clusters */
1580 }
1581 }
1582 cp = mtod(mp, u_char *);
1583 cp[0] = adrs;
1584 cp[1] = ctrl;
1585 cp[2] = 0;
1586 cp[3] = PPP_IP;
1587 proto = PPP_IP;
1588 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1589 mp->m_len = hlen + PPP_HDRLEN;
1590
1591 /*
1592 * Trim the PPP and VJ headers off the old mbuf
1593 * and stick the new and old mbufs together.
1594 */
1595 m->m_data += PPP_HDRLEN + xlen;
1596 m->m_len -= PPP_HDRLEN + xlen;
1597 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1598 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1599 mp->m_len += m->m_len;
1600 MFREE(m, mp->m_next);
1601 } else
1602 mp->m_next = m;
1603 m = mp;
1604 ilen += hlen - xlen;
1605
1606 } else if (proto == PPP_VJC_UNCOMP) {
1607 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1608 goto bad;
1609
1610 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1611 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1612 sc->sc_comp, &iphdr, &hlen);
1613
1614 if (xlen < 0) {
1615 if (sc->sc_flags & SC_DEBUG)
1616 printf("%s: VJ uncompress failed on type uncomp\n",
1617 ifp->if_xname);
1618 goto bad;
1619 }
1620
1621 proto = PPP_IP;
1622 cp[3] = PPP_IP;
1623 }
1624 #endif /* VJC */
1625
1626 /*
1627 * If the packet will fit in a header mbuf, don't waste a
1628 * whole cluster on it.
1629 */
1630 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1631 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1632 if (mp != NULL) {
1633 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1634 m_freem(m);
1635 m = mp;
1636 m->m_len = ilen;
1637 }
1638 }
1639 m->m_pkthdr.len = ilen;
1640 m->m_pkthdr.rcvif = ifp;
1641
1642 if ((proto & 0x8000) == 0) {
1643 #ifdef PPP_FILTER
1644 /*
1645 * See whether we want to pass this packet, and
1646 * if it counts as link activity.
1647 */
1648 if (sc->sc_pass_filt_in.bf_insns != 0
1649 && bpf_filter(sc->sc_pass_filt_in.bf_insns, (u_char *) m,
1650 ilen, 0) == 0) {
1651 /* drop this packet */
1652 m_freem(m);
1653 return;
1654 }
1655 if (sc->sc_active_filt_in.bf_insns == 0
1656 || bpf_filter(sc->sc_active_filt_in.bf_insns, (u_char *) m,
1657 ilen, 0))
1658 sc->sc_last_recv = time.tv_sec;
1659 #else
1660 /*
1661 * Record the time that we received this packet.
1662 */
1663 sc->sc_last_recv = time.tv_sec;
1664 #endif /* PPP_FILTER */
1665 }
1666
1667 #if NBPFILTER > 0
1668 /* See if bpf wants to look at the packet. */
1669 if (sc->sc_if.if_bpf)
1670 bpf_mtap(sc->sc_if.if_bpf, m);
1671 #endif
1672
1673 rv = 0;
1674 switch (proto) {
1675 #ifdef INET
1676 case PPP_IP:
1677 /*
1678 * IP packet - take off the ppp header and pass it up to IP.
1679 */
1680 if ((ifp->if_flags & IFF_UP) == 0
1681 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1682 /* interface is down - drop the packet. */
1683 m_freem(m);
1684 return;
1685 }
1686 m->m_pkthdr.len -= PPP_HDRLEN;
1687 m->m_data += PPP_HDRLEN;
1688 m->m_len -= PPP_HDRLEN;
1689 #ifdef GATEWAY
1690 if (ipflow_fastforward(m))
1691 return;
1692 #endif
1693 schednetisr(NETISR_IP);
1694 inq = &ipintrq;
1695 break;
1696 #endif
1697
1698 #ifdef INET6
1699 case PPP_IPV6:
1700 /*
1701 * IPv6 packet - take off the ppp header and pass it up to IPv6.
1702 */
1703 if ((ifp->if_flags & IFF_UP) == 0
1704 || sc->sc_npmode[NP_IPV6] != NPMODE_PASS) {
1705 /* interface is down - drop the packet. */
1706 m_freem(m);
1707 return;
1708 }
1709 m->m_pkthdr.len -= PPP_HDRLEN;
1710 m->m_data += PPP_HDRLEN;
1711 m->m_len -= PPP_HDRLEN;
1712 schednetisr(NETISR_IPV6);
1713 inq = &ip6intrq;
1714 break;
1715 #endif
1716
1717 default:
1718 /*
1719 * Some other protocol - place on input queue for read().
1720 */
1721 inq = &sc->sc_inq;
1722 rv = 1;
1723 break;
1724 }
1725
1726 /*
1727 * Put the packet on the appropriate input queue.
1728 */
1729 s = splnet();
1730 if (IF_QFULL(inq)) {
1731 IF_DROP(inq);
1732 splx(s);
1733 if (sc->sc_flags & SC_DEBUG)
1734 printf("%s: input queue full\n", ifp->if_xname);
1735 ifp->if_iqdrops++;
1736 goto bad;
1737 }
1738 IF_ENQUEUE(inq, m);
1739 splx(s);
1740 ifp->if_ipackets++;
1741 ifp->if_ibytes += ilen;
1742
1743 if (rv)
1744 (*sc->sc_ctlp)(sc);
1745
1746 return;
1747
1748 bad:
1749 m_freem(m);
1750 sc->sc_if.if_ierrors++;
1751 sc->sc_stats.ppp_ierrors++;
1752 }
1753
1754 #define MAX_DUMP_BYTES 128
1755
1756 static void
1757 pppdumpm(m0)
1758 struct mbuf *m0;
1759 {
1760 char buf[3*MAX_DUMP_BYTES+4];
1761 char *bp = buf;
1762 struct mbuf *m;
1763 static char digits[] = "0123456789abcdef";
1764
1765 for (m = m0; m; m = m->m_next) {
1766 int l = m->m_len;
1767 u_char *rptr = (u_char *)m->m_data;
1768
1769 while (l--) {
1770 if (bp > buf + sizeof(buf) - 4)
1771 goto done;
1772 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1773 *bp++ = digits[*rptr++ & 0xf];
1774 }
1775
1776 if (m->m_next) {
1777 if (bp > buf + sizeof(buf) - 3)
1778 goto done;
1779 *bp++ = '|';
1780 } else
1781 *bp++ = ' ';
1782 }
1783 done:
1784 if (m)
1785 *bp++ = '>';
1786 *bp = 0;
1787 printf("%s\n", buf);
1788 }
1789
1790 #ifdef ALTQ
1791 /*
1792 * a wrapper to transmit a packet from if_start since ALTQ uses
1793 * if_start to send a packet.
1794 */
1795 static void
1796 ppp_ifstart(ifp)
1797 struct ifnet *ifp;
1798 {
1799 struct ppp_softc *sc;
1800
1801 sc = ifp->if_softc;
1802 (*sc->sc_start)(sc);
1803 }
1804 #endif
1805