if_ppp.c revision 1.93 1 /* $NetBSD: if_ppp.c,v 1.93 2004/12/05 13:32:03 he 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.93 2004/12/05 13:32:03 he 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_detach(ifp);
340
341 FREE(sc, M_DEVBUF);
342 return 0;
343 }
344
345 /*
346 * Allocate a ppp interface unit and initialize it.
347 */
348 struct ppp_softc *
349 pppalloc(pid)
350 pid_t pid;
351 {
352 struct ppp_softc *sc = NULL, *scf;
353 int i;
354
355 simple_lock(&ppp_list_mutex);
356 for (scf = LIST_FIRST(&ppp_softc_list); scf != NULL;
357 scf = LIST_NEXT(scf, sc_iflist)) {
358 if (scf->sc_xfer == pid) {
359 scf->sc_xfer = 0;
360 simple_unlock(&ppp_list_mutex);
361 return scf;
362 }
363 if (scf->sc_devp == NULL && sc == NULL)
364 sc = scf;
365 }
366 simple_lock(&ppp_list_mutex);
367
368 if (sc == NULL)
369 sc = ppp_create(ppp_cloner.ifc_name, -1);
370
371 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
372 sc->sc_si = softintr_establish(IPL_SOFTNET, pppintr, sc);
373 if (sc->sc_si == NULL) {
374 printf("%s: unable to establish softintr\n", sc->sc_if.if_xname);
375 return (NULL);
376 }
377 #endif
378 sc->sc_flags = 0;
379 sc->sc_mru = PPP_MRU;
380 sc->sc_relinq = NULL;
381 (void)memset(&sc->sc_stats, 0, sizeof(sc->sc_stats));
382 #ifdef VJC
383 MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
384 M_DEVBUF, M_NOWAIT);
385 if (sc->sc_comp)
386 sl_compress_init(sc->sc_comp);
387 #endif
388 #ifdef PPP_COMPRESS
389 sc->sc_xc_state = NULL;
390 sc->sc_rc_state = NULL;
391 #endif /* PPP_COMPRESS */
392 for (i = 0; i < NUM_NP; ++i)
393 sc->sc_npmode[i] = NPMODE_ERROR;
394 sc->sc_npqueue = NULL;
395 sc->sc_npqtail = &sc->sc_npqueue;
396 sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;
397
398 return sc;
399 }
400
401 /*
402 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
403 */
404 void
405 pppdealloc(sc)
406 struct ppp_softc *sc;
407 {
408 struct mbuf *m;
409
410 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
411 softintr_disestablish(sc->sc_si);
412 #endif
413 if_down(&sc->sc_if);
414 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
415 sc->sc_devp = NULL;
416 sc->sc_xfer = 0;
417 for (;;) {
418 IF_DEQUEUE(&sc->sc_rawq, m);
419 if (m == NULL)
420 break;
421 m_freem(m);
422 }
423 for (;;) {
424 IF_DEQUEUE(&sc->sc_inq, m);
425 if (m == NULL)
426 break;
427 m_freem(m);
428 }
429 for (;;) {
430 IF_DEQUEUE(&sc->sc_fastq, m);
431 if (m == NULL)
432 break;
433 m_freem(m);
434 }
435 while ((m = sc->sc_npqueue) != NULL) {
436 sc->sc_npqueue = m->m_nextpkt;
437 m_freem(m);
438 }
439 if (sc->sc_togo != NULL) {
440 m_freem(sc->sc_togo);
441 sc->sc_togo = NULL;
442 }
443 #ifdef PPP_COMPRESS
444 ppp_ccp_closed(sc);
445 sc->sc_xc_state = NULL;
446 sc->sc_rc_state = NULL;
447 #endif /* PPP_COMPRESS */
448 #ifdef PPP_FILTER
449 if (sc->sc_pass_filt_in.bf_insns != 0) {
450 FREE(sc->sc_pass_filt_in.bf_insns, M_DEVBUF);
451 sc->sc_pass_filt_in.bf_insns = 0;
452 sc->sc_pass_filt_in.bf_len = 0;
453 }
454 if (sc->sc_pass_filt_out.bf_insns != 0) {
455 FREE(sc->sc_pass_filt_out.bf_insns, M_DEVBUF);
456 sc->sc_pass_filt_out.bf_insns = 0;
457 sc->sc_pass_filt_out.bf_len = 0;
458 }
459 if (sc->sc_active_filt_in.bf_insns != 0) {
460 FREE(sc->sc_active_filt_in.bf_insns, M_DEVBUF);
461 sc->sc_active_filt_in.bf_insns = 0;
462 sc->sc_active_filt_in.bf_len = 0;
463 }
464 if (sc->sc_active_filt_out.bf_insns != 0) {
465 FREE(sc->sc_active_filt_out.bf_insns, M_DEVBUF);
466 sc->sc_active_filt_out.bf_insns = 0;
467 sc->sc_active_filt_out.bf_len = 0;
468 }
469 #endif /* PPP_FILTER */
470 #ifdef VJC
471 if (sc->sc_comp != 0) {
472 FREE(sc->sc_comp, M_DEVBUF);
473 sc->sc_comp = 0;
474 }
475 #endif
476 (void)ppp_clone_destroy(&sc->sc_if);
477 }
478
479 /*
480 * Ioctl routine for generic ppp devices.
481 */
482 int
483 pppioctl(sc, cmd, data, flag, p)
484 struct ppp_softc *sc;
485 u_long cmd;
486 caddr_t data;
487 int flag;
488 struct proc *p;
489 {
490 int s, error, flags, mru, npx;
491 u_int nb;
492 struct ppp_option_data *odp;
493 struct compressor **cp;
494 struct npioctl *npi;
495 time_t t;
496 #ifdef PPP_FILTER
497 struct bpf_program *bp, *nbp;
498 struct bpf_insn *newcode, *oldcode;
499 int newcodelen;
500 #endif /* PPP_FILTER */
501 #ifdef PPP_COMPRESS
502 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
503 #endif
504
505 switch (cmd) {
506 case FIONREAD:
507 *(int *)data = sc->sc_inq.ifq_len;
508 break;
509
510 case PPPIOCGUNIT:
511 *(int *)data = sc->sc_unit;
512 break;
513
514 case PPPIOCGFLAGS:
515 *(u_int *)data = sc->sc_flags;
516 break;
517
518 case PPPIOCGRAWIN:
519 {
520 struct ppp_rawin *rwin = (struct ppp_rawin *)data;
521 u_char p, q = 0;
522
523 for (p = sc->sc_rawin_start; p < sizeof(sc->sc_rawin.buf);)
524 rwin->buf[q++] = sc->sc_rawin.buf[p++];
525
526 for (p = 0; p < sc->sc_rawin_start;)
527 rwin->buf[q++] = sc->sc_rawin.buf[p++];
528
529 rwin->count = sc->sc_rawin.count;
530 }
531 break;
532
533 case PPPIOCSFLAGS:
534 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
535 return (error);
536 flags = *(int *)data & SC_MASK;
537 s = splsoftnet();
538 #ifdef PPP_COMPRESS
539 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
540 ppp_ccp_closed(sc);
541 #endif
542 splhigh(); /* XXX IMP ME HARDER */
543 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
544 splx(s);
545 break;
546
547 case PPPIOCSMRU:
548 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
549 return (error);
550 mru = *(int *)data;
551 if (mru >= PPP_MINMRU && mru <= PPP_MAXMRU)
552 sc->sc_mru = mru;
553 break;
554
555 case PPPIOCGMRU:
556 *(int *)data = sc->sc_mru;
557 break;
558
559 #ifdef VJC
560 case PPPIOCSMAXCID:
561 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
562 return (error);
563 if (sc->sc_comp) {
564 s = splsoftnet();
565 sl_compress_setup(sc->sc_comp, *(int *)data);
566 splx(s);
567 }
568 break;
569 #endif
570
571 case PPPIOCXFERUNIT:
572 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
573 return (error);
574 sc->sc_xfer = p->p_pid;
575 break;
576
577 #ifdef PPP_COMPRESS
578 case PPPIOCSCOMPRESS:
579 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
580 return (error);
581 odp = (struct ppp_option_data *) data;
582 nb = odp->length;
583 if (nb > sizeof(ccp_option))
584 nb = sizeof(ccp_option);
585 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
586 return (error);
587 if (ccp_option[1] < 2) /* preliminary check on the length byte */
588 return (EINVAL);
589 for (cp = ppp_compressors; *cp != NULL; ++cp)
590 if ((*cp)->compress_proto == ccp_option[0]) {
591 /*
592 * Found a handler for the protocol - try to allocate
593 * a compressor or decompressor.
594 */
595 error = 0;
596 if (odp->transmit) {
597 s = splsoftnet();
598 if (sc->sc_xc_state != NULL)
599 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
600 sc->sc_xcomp = *cp;
601 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
602 if (sc->sc_xc_state == NULL) {
603 if (sc->sc_flags & SC_DEBUG)
604 printf("%s: comp_alloc failed\n",
605 sc->sc_if.if_xname);
606 error = ENOBUFS;
607 }
608 splhigh(); /* XXX IMP ME HARDER */
609 sc->sc_flags &= ~SC_COMP_RUN;
610 splx(s);
611 } else {
612 s = splsoftnet();
613 if (sc->sc_rc_state != NULL)
614 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
615 sc->sc_rcomp = *cp;
616 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
617 if (sc->sc_rc_state == NULL) {
618 if (sc->sc_flags & SC_DEBUG)
619 printf("%s: decomp_alloc failed\n",
620 sc->sc_if.if_xname);
621 error = ENOBUFS;
622 }
623 splhigh(); /* XXX IMP ME HARDER */
624 sc->sc_flags &= ~SC_DECOMP_RUN;
625 splx(s);
626 }
627 return (error);
628 }
629 if (sc->sc_flags & SC_DEBUG)
630 printf("%s: no compressor for [%x %x %x], %x\n",
631 sc->sc_if.if_xname, ccp_option[0], ccp_option[1],
632 ccp_option[2], nb);
633 return (EINVAL); /* no handler found */
634 #endif /* PPP_COMPRESS */
635
636 case PPPIOCGNPMODE:
637 case PPPIOCSNPMODE:
638 npi = (struct npioctl *) data;
639 switch (npi->protocol) {
640 case PPP_IP:
641 npx = NP_IP;
642 break;
643 case PPP_IPV6:
644 npx = NP_IPV6;
645 break;
646 default:
647 return EINVAL;
648 }
649 if (cmd == PPPIOCGNPMODE) {
650 npi->mode = sc->sc_npmode[npx];
651 } else {
652 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
653 return (error);
654 if (npi->mode != sc->sc_npmode[npx]) {
655 s = splnet();
656 sc->sc_npmode[npx] = npi->mode;
657 if (npi->mode != NPMODE_QUEUE) {
658 ppp_requeue(sc);
659 ppp_restart(sc);
660 }
661 splx(s);
662 }
663 }
664 break;
665
666 case PPPIOCGIDLE:
667 s = splsoftnet();
668 t = time.tv_sec;
669 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
670 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
671 splx(s);
672 break;
673
674 #ifdef PPP_FILTER
675 case PPPIOCSPASS:
676 case PPPIOCSACTIVE:
677 /* These are no longer supported. */
678 return EOPNOTSUPP;
679
680 case PPPIOCSIPASS:
681 case PPPIOCSOPASS:
682 case PPPIOCSIACTIVE:
683 case PPPIOCSOACTIVE:
684 nbp = (struct bpf_program *) data;
685 if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
686 return EINVAL;
687 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
688 if (newcodelen != 0) {
689 newcode = malloc(newcodelen, M_DEVBUF, M_WAITOK);
690 /* WAITOK -- malloc() never fails. */
691 if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
692 newcodelen)) != 0) {
693 free(newcode, M_DEVBUF);
694 return error;
695 }
696 if (!bpf_validate(newcode, nbp->bf_len)) {
697 free(newcode, M_DEVBUF);
698 return EINVAL;
699 }
700 } else
701 newcode = 0;
702 switch (cmd) {
703 case PPPIOCSIPASS:
704 bp = &sc->sc_pass_filt_in;
705 break;
706
707 case PPPIOCSOPASS:
708 bp = &sc->sc_pass_filt_out;
709 break;
710
711 case PPPIOCSIACTIVE:
712 bp = &sc->sc_active_filt_in;
713 break;
714
715 case PPPIOCSOACTIVE:
716 bp = &sc->sc_active_filt_out;
717 break;
718 default:
719 free(newcode, M_DEVBUF);
720 return (EPASSTHROUGH);
721 }
722 oldcode = bp->bf_insns;
723 s = splnet();
724 bp->bf_len = nbp->bf_len;
725 bp->bf_insns = newcode;
726 splx(s);
727 if (oldcode != 0)
728 free(oldcode, M_DEVBUF);
729 break;
730 #endif /* PPP_FILTER */
731
732 default:
733 return (EPASSTHROUGH);
734 }
735 return (0);
736 }
737
738 /*
739 * Process an ioctl request to the ppp network interface.
740 */
741 static int
742 pppsioctl(ifp, cmd, data)
743 struct ifnet *ifp;
744 u_long cmd;
745 caddr_t data;
746 {
747 struct proc *p = curproc; /* XXX */
748 struct ppp_softc *sc = ifp->if_softc;
749 struct ifaddr *ifa = (struct ifaddr *)data;
750 struct ifreq *ifr = (struct ifreq *)data;
751 struct ppp_stats *psp;
752 #ifdef PPP_COMPRESS
753 struct ppp_comp_stats *pcp;
754 #endif
755 int s = splnet(), error = 0;
756
757 switch (cmd) {
758 case SIOCSIFFLAGS:
759 if ((ifp->if_flags & IFF_RUNNING) == 0)
760 ifp->if_flags &= ~IFF_UP;
761 break;
762
763 case SIOCSIFADDR:
764 switch (ifa->ifa_addr->sa_family) {
765 #ifdef INET
766 case AF_INET:
767 break;
768 #endif
769 #ifdef INET6
770 case AF_INET6:
771 break;
772 #endif
773 default:
774 error = EAFNOSUPPORT;
775 break;
776 }
777 break;
778
779 case SIOCSIFDSTADDR:
780 switch (ifa->ifa_addr->sa_family) {
781 #ifdef INET
782 case AF_INET:
783 break;
784 #endif
785 #ifdef INET6
786 case AF_INET6:
787 break;
788 #endif
789 default:
790 error = EAFNOSUPPORT;
791 break;
792 }
793 break;
794
795 case SIOCSIFMTU:
796 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
797 break;
798 sc->sc_if.if_mtu = ifr->ifr_mtu;
799 break;
800
801 case SIOCGIFMTU:
802 ifr->ifr_mtu = sc->sc_if.if_mtu;
803 break;
804
805 case SIOCADDMULTI:
806 case SIOCDELMULTI:
807 if (ifr == 0) {
808 error = EAFNOSUPPORT;
809 break;
810 }
811 switch (ifr->ifr_addr.sa_family) {
812 #ifdef INET
813 case AF_INET:
814 break;
815 #endif
816 #ifdef INET6
817 case AF_INET6:
818 break;
819 #endif
820 default:
821 error = EAFNOSUPPORT;
822 break;
823 }
824 break;
825
826 case SIOCGPPPSTATS:
827 psp = &((struct ifpppstatsreq *) data)->stats;
828 memset(psp, 0, sizeof(*psp));
829 psp->p = sc->sc_stats;
830 #if defined(VJC) && !defined(SL_NO_STATS)
831 if (sc->sc_comp) {
832 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
833 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
834 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
835 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
836 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
837 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
838 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
839 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
840 }
841 #endif /* VJC */
842 break;
843
844 #ifdef PPP_COMPRESS
845 case SIOCGPPPCSTATS:
846 pcp = &((struct ifpppcstatsreq *) data)->stats;
847 memset(pcp, 0, sizeof(*pcp));
848 if (sc->sc_xc_state != NULL)
849 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
850 if (sc->sc_rc_state != NULL)
851 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
852 break;
853 #endif /* PPP_COMPRESS */
854
855 default:
856 error = EINVAL;
857 }
858 splx(s);
859 return (error);
860 }
861
862 /*
863 * Queue a packet. Start transmission if not active.
864 * Packet is placed in Information field of PPP frame.
865 */
866 int
867 pppoutput(ifp, m0, dst, rtp)
868 struct ifnet *ifp;
869 struct mbuf *m0;
870 struct sockaddr *dst;
871 struct rtentry *rtp;
872 {
873 struct ppp_softc *sc = ifp->if_softc;
874 int protocol, address, control;
875 u_char *cp;
876 int s, error;
877 #ifdef INET
878 struct ip *ip;
879 #endif
880 struct ifqueue *ifq;
881 enum NPmode mode;
882 int len;
883 ALTQ_DECL(struct altq_pktattr pktattr;)
884
885 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
886 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
887 error = ENETDOWN; /* sort of */
888 goto bad;
889 }
890
891 IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family, &pktattr);
892
893 /*
894 * Compute PPP header.
895 */
896 m0->m_flags &= ~M_HIGHPRI;
897 switch (dst->sa_family) {
898 #ifdef INET
899 case AF_INET:
900 address = PPP_ALLSTATIONS;
901 control = PPP_UI;
902 protocol = PPP_IP;
903 mode = sc->sc_npmode[NP_IP];
904
905 /*
906 * If this packet has the "low delay" bit set in the IP header,
907 * put it on the fastq instead.
908 */
909 ip = mtod(m0, struct ip *);
910 if (ip->ip_tos & IPTOS_LOWDELAY)
911 m0->m_flags |= M_HIGHPRI;
912 break;
913 #endif
914 #ifdef INET6
915 case AF_INET6:
916 address = PPP_ALLSTATIONS; /*XXX*/
917 control = PPP_UI; /*XXX*/
918 protocol = PPP_IPV6;
919 mode = sc->sc_npmode[NP_IPV6];
920
921 #if 0 /* XXX flowinfo/traffic class, maybe? */
922 /*
923 * If this packet has the "low delay" bit set in the IP header,
924 * put it on the fastq instead.
925 */
926 ip = mtod(m0, struct ip *);
927 if (ip->ip_tos & IPTOS_LOWDELAY)
928 m0->m_flags |= M_HIGHPRI;
929 #endif
930 break;
931 #endif
932 case AF_UNSPEC:
933 address = PPP_ADDRESS(dst->sa_data);
934 control = PPP_CONTROL(dst->sa_data);
935 protocol = PPP_PROTOCOL(dst->sa_data);
936 mode = NPMODE_PASS;
937 break;
938 default:
939 printf("%s: af%d not supported\n", ifp->if_xname, dst->sa_family);
940 error = EAFNOSUPPORT;
941 goto bad;
942 }
943
944 /*
945 * Drop this packet, or return an error, if necessary.
946 */
947 if (mode == NPMODE_ERROR) {
948 error = ENETDOWN;
949 goto bad;
950 }
951 if (mode == NPMODE_DROP) {
952 error = 0;
953 goto bad;
954 }
955
956 /*
957 * Add PPP header.
958 */
959 M_PREPEND(m0, PPP_HDRLEN, M_DONTWAIT);
960 if (m0 == NULL) {
961 error = ENOBUFS;
962 goto bad;
963 }
964
965 cp = mtod(m0, u_char *);
966 *cp++ = address;
967 *cp++ = control;
968 *cp++ = protocol >> 8;
969 *cp++ = protocol & 0xff;
970
971 len = m_length(m0);
972
973 if (sc->sc_flags & SC_LOG_OUTPKT) {
974 printf("%s output: ", ifp->if_xname);
975 pppdumpm(m0);
976 }
977
978 if ((protocol & 0x8000) == 0) {
979 #ifdef PPP_FILTER
980 /*
981 * Apply the pass and active filters to the packet,
982 * but only if it is a data packet.
983 */
984 if (sc->sc_pass_filt_out.bf_insns != 0
985 && bpf_filter(sc->sc_pass_filt_out.bf_insns, (u_char *) m0,
986 len, 0) == 0) {
987 error = 0; /* drop this packet */
988 goto bad;
989 }
990
991 /*
992 * Update the time we sent the most recent packet.
993 */
994 if (sc->sc_active_filt_out.bf_insns == 0
995 || bpf_filter(sc->sc_active_filt_out.bf_insns, (u_char *) m0,
996 len, 0))
997 sc->sc_last_sent = time.tv_sec;
998 #else
999 /*
1000 * Update the time we sent the most recent packet.
1001 */
1002 sc->sc_last_sent = time.tv_sec;
1003 #endif /* PPP_FILTER */
1004 }
1005
1006 #if NBPFILTER > 0
1007 /*
1008 * See if bpf wants to look at the packet.
1009 */
1010 if (sc->sc_if.if_bpf)
1011 bpf_mtap(sc->sc_if.if_bpf, m0);
1012 #endif
1013
1014 /*
1015 * Put the packet on the appropriate queue.
1016 */
1017 s = splnet();
1018 if (mode == NPMODE_QUEUE) {
1019 /* XXX we should limit the number of packets on this queue */
1020 *sc->sc_npqtail = m0;
1021 m0->m_nextpkt = NULL;
1022 sc->sc_npqtail = &m0->m_nextpkt;
1023 } else {
1024 if ((m0->m_flags & M_HIGHPRI)
1025 #ifdef ALTQ
1026 && ALTQ_IS_ENABLED(&sc->sc_if.if_snd) == 0
1027 #endif
1028 ) {
1029 ifq = &sc->sc_fastq;
1030 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
1031 IF_DROP(ifq);
1032 splx(s);
1033 error = ENOBUFS;
1034 goto bad;
1035 } else {
1036 IF_ENQUEUE(ifq, m0);
1037 error = 0;
1038 }
1039 } else
1040 IFQ_ENQUEUE(&sc->sc_if.if_snd, m0, &pktattr, error);
1041 if (error) {
1042 splx(s);
1043 sc->sc_if.if_oerrors++;
1044 sc->sc_stats.ppp_oerrors++;
1045 return (error);
1046 }
1047 ppp_restart(sc);
1048 }
1049 ifp->if_opackets++;
1050 ifp->if_obytes += len;
1051
1052 splx(s);
1053 return (0);
1054
1055 bad:
1056 m_freem(m0);
1057 return (error);
1058 }
1059
1060 /*
1061 * After a change in the NPmode for some NP, move packets from the
1062 * npqueue to the send queue or the fast queue as appropriate.
1063 * Should be called at splnet, since we muck with the queues.
1064 */
1065 static void
1066 ppp_requeue(sc)
1067 struct ppp_softc *sc;
1068 {
1069 struct mbuf *m, **mpp;
1070 struct ifqueue *ifq;
1071 enum NPmode mode;
1072 int error;
1073
1074 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
1075 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
1076 case PPP_IP:
1077 mode = sc->sc_npmode[NP_IP];
1078 break;
1079 case PPP_IPV6:
1080 mode = sc->sc_npmode[NP_IPV6];
1081 break;
1082 default:
1083 mode = NPMODE_PASS;
1084 }
1085
1086 switch (mode) {
1087 case NPMODE_PASS:
1088 /*
1089 * This packet can now go on one of the queues to be sent.
1090 */
1091 *mpp = m->m_nextpkt;
1092 m->m_nextpkt = NULL;
1093 if ((m->m_flags & M_HIGHPRI)
1094 #ifdef ALTQ
1095 && ALTQ_IS_ENABLED(&sc->sc_if.if_snd) == 0
1096 #endif
1097 ) {
1098 ifq = &sc->sc_fastq;
1099 if (IF_QFULL(ifq)) {
1100 IF_DROP(ifq);
1101 m_freem(m);
1102 error = ENOBUFS;
1103 } else {
1104 IF_ENQUEUE(ifq, m);
1105 error = 0;
1106 }
1107 } else
1108 IFQ_ENQUEUE(&sc->sc_if.if_snd, m, NULL, error);
1109 if (error) {
1110 sc->sc_if.if_oerrors++;
1111 sc->sc_stats.ppp_oerrors++;
1112 }
1113 break;
1114
1115 case NPMODE_DROP:
1116 case NPMODE_ERROR:
1117 *mpp = m->m_nextpkt;
1118 m_freem(m);
1119 break;
1120
1121 case NPMODE_QUEUE:
1122 mpp = &m->m_nextpkt;
1123 break;
1124 }
1125 }
1126 sc->sc_npqtail = mpp;
1127 }
1128
1129 /*
1130 * Transmitter has finished outputting some stuff;
1131 * remember to call sc->sc_start later at splsoftnet.
1132 */
1133 void
1134 ppp_restart(sc)
1135 struct ppp_softc *sc;
1136 {
1137 int s = splhigh(); /* XXX IMP ME HARDER */
1138
1139 sc->sc_flags &= ~SC_TBUSY;
1140 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
1141 softintr_schedule(sc->sc_si);
1142 #else
1143 schednetisr(NETISR_PPP);
1144 #endif
1145 splx(s);
1146 }
1147
1148 /*
1149 * Get a packet to send. This procedure is intended to be called at
1150 * splsoftnet, since it may involve time-consuming operations such as
1151 * applying VJ compression, packet compression, address/control and/or
1152 * protocol field compression to the packet.
1153 */
1154 struct mbuf *
1155 ppp_dequeue(sc)
1156 struct ppp_softc *sc;
1157 {
1158 struct mbuf *m, *mp;
1159 u_char *cp;
1160 int address, control, protocol;
1161 int s;
1162
1163 /*
1164 * Grab a packet to send: first try the fast queue, then the
1165 * normal queue.
1166 */
1167 s = splnet();
1168 IF_DEQUEUE(&sc->sc_fastq, m);
1169 if (m == NULL)
1170 IFQ_DEQUEUE(&sc->sc_if.if_snd, m);
1171 splx(s);
1172
1173 if (m == NULL)
1174 return NULL;
1175
1176 ++sc->sc_stats.ppp_opackets;
1177
1178 /*
1179 * Extract the ppp header of the new packet.
1180 * The ppp header will be in one mbuf.
1181 */
1182 cp = mtod(m, u_char *);
1183 address = PPP_ADDRESS(cp);
1184 control = PPP_CONTROL(cp);
1185 protocol = PPP_PROTOCOL(cp);
1186
1187 switch (protocol) {
1188 case PPP_IP:
1189 #ifdef VJC
1190 /*
1191 * If the packet is a TCP/IP packet, see if we can compress it.
1192 */
1193 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
1194 struct ip *ip;
1195 int type;
1196
1197 mp = m;
1198 ip = (struct ip *) (cp + PPP_HDRLEN);
1199 if (mp->m_len <= PPP_HDRLEN) {
1200 mp = mp->m_next;
1201 if (mp == NULL)
1202 break;
1203 ip = mtod(mp, struct ip *);
1204 }
1205 /* this code assumes the IP/TCP header is in one non-shared mbuf */
1206 if (ip->ip_p == IPPROTO_TCP) {
1207 type = sl_compress_tcp(mp, ip, sc->sc_comp,
1208 !(sc->sc_flags & SC_NO_TCP_CCID));
1209 switch (type) {
1210 case TYPE_UNCOMPRESSED_TCP:
1211 protocol = PPP_VJC_UNCOMP;
1212 break;
1213 case TYPE_COMPRESSED_TCP:
1214 protocol = PPP_VJC_COMP;
1215 cp = mtod(m, u_char *);
1216 cp[0] = address; /* header has moved */
1217 cp[1] = control;
1218 cp[2] = 0;
1219 break;
1220 }
1221 cp[3] = protocol; /* update protocol in PPP header */
1222 }
1223 }
1224 #endif /* VJC */
1225 break;
1226
1227 #ifdef PPP_COMPRESS
1228 case PPP_CCP:
1229 ppp_ccp(sc, m, 0);
1230 break;
1231 #endif /* PPP_COMPRESS */
1232 }
1233
1234 #ifdef PPP_COMPRESS
1235 if (protocol != PPP_LCP && protocol != PPP_CCP
1236 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1237 struct mbuf *mcomp = NULL;
1238 int slen;
1239
1240 slen = 0;
1241 for (mp = m; mp != NULL; mp = mp->m_next)
1242 slen += mp->m_len;
1243 (*sc->sc_xcomp->compress)
1244 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
1245 if (mcomp != NULL) {
1246 if (sc->sc_flags & SC_CCP_UP) {
1247 /* Send the compressed packet instead of the original. */
1248 m_freem(m);
1249 m = mcomp;
1250 cp = mtod(m, u_char *);
1251 protocol = cp[3];
1252 } else {
1253 /* Can't transmit compressed packets until CCP is up. */
1254 m_freem(mcomp);
1255 }
1256 }
1257 }
1258 #endif /* PPP_COMPRESS */
1259
1260 /*
1261 * Compress the address/control and protocol, if possible.
1262 */
1263 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1264 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1265 protocol != PPP_LCP) {
1266 /* can compress address/control */
1267 m->m_data += 2;
1268 m->m_len -= 2;
1269 }
1270 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1271 /* can compress protocol */
1272 if (mtod(m, u_char *) == cp) {
1273 cp[2] = cp[1]; /* move address/control up */
1274 cp[1] = cp[0];
1275 }
1276 ++m->m_data;
1277 --m->m_len;
1278 }
1279
1280 return m;
1281 }
1282
1283 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
1284 void
1285 pppnetisr(void)
1286 {
1287 struct ppp_softc *sc;
1288
1289 for (sc = LIST_FIRST(&ppp_softc_list); sc != NULL;
1290 sc = LIST_NEXT(sc, sc_iflist))
1291 pppintr(sc);
1292 }
1293 #endif
1294
1295 /*
1296 * Software interrupt routine, called at splsoftnet.
1297 */
1298 void
1299 pppintr(void *arg)
1300 {
1301 struct ppp_softc *sc = arg;
1302 struct mbuf *m;
1303 int s;
1304
1305 if (!(sc->sc_flags & SC_TBUSY)
1306 && (IFQ_IS_EMPTY(&sc->sc_if.if_snd) == 0 || sc->sc_fastq.ifq_head
1307 || sc->sc_outm)) {
1308 s = splhigh(); /* XXX IMP ME HARDER */
1309 sc->sc_flags |= SC_TBUSY;
1310 splx(s);
1311 (*sc->sc_start)(sc);
1312 }
1313 for (;;) {
1314 s = splnet();
1315 IF_DEQUEUE(&sc->sc_rawq, m);
1316 splx(s);
1317 if (m == NULL)
1318 break;
1319 ppp_inproc(sc, m);
1320 }
1321 }
1322
1323 #ifdef PPP_COMPRESS
1324 /*
1325 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1326 * 0 if it is about to be transmitted.
1327 */
1328 static void
1329 ppp_ccp(sc, m, rcvd)
1330 struct ppp_softc *sc;
1331 struct mbuf *m;
1332 int rcvd;
1333 {
1334 u_char *dp, *ep;
1335 struct mbuf *mp;
1336 int slen, s;
1337
1338 /*
1339 * Get a pointer to the data after the PPP header.
1340 */
1341 if (m->m_len <= PPP_HDRLEN) {
1342 mp = m->m_next;
1343 if (mp == NULL)
1344 return;
1345 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1346 } else {
1347 mp = m;
1348 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1349 }
1350
1351 ep = mtod(mp, u_char *) + mp->m_len;
1352 if (dp + CCP_HDRLEN > ep)
1353 return;
1354 slen = CCP_LENGTH(dp);
1355 if (dp + slen > ep) {
1356 if (sc->sc_flags & SC_DEBUG)
1357 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1358 dp, slen, mtod(mp, u_char *), mp->m_len);
1359 return;
1360 }
1361
1362 switch (CCP_CODE(dp)) {
1363 case CCP_CONFREQ:
1364 case CCP_TERMREQ:
1365 case CCP_TERMACK:
1366 /* CCP must be going down - disable compression */
1367 if (sc->sc_flags & SC_CCP_UP) {
1368 s = splhigh(); /* XXX IMP ME HARDER */
1369 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1370 splx(s);
1371 }
1372 break;
1373
1374 case CCP_CONFACK:
1375 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1376 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1377 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1378 if (!rcvd) {
1379 /* we're agreeing to send compressed packets. */
1380 if (sc->sc_xc_state != NULL
1381 && (*sc->sc_xcomp->comp_init)
1382 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1383 sc->sc_unit, 0, sc->sc_flags & SC_DEBUG)) {
1384 s = splhigh(); /* XXX IMP ME HARDER */
1385 sc->sc_flags |= SC_COMP_RUN;
1386 splx(s);
1387 }
1388 } else {
1389 /* peer is agreeing to send compressed packets. */
1390 if (sc->sc_rc_state != NULL
1391 && (*sc->sc_rcomp->decomp_init)
1392 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1393 sc->sc_unit, 0, sc->sc_mru,
1394 sc->sc_flags & SC_DEBUG)) {
1395 s = splhigh(); /* XXX IMP ME HARDER */
1396 sc->sc_flags |= SC_DECOMP_RUN;
1397 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1398 splx(s);
1399 }
1400 }
1401 }
1402 break;
1403
1404 case CCP_RESETACK:
1405 if (sc->sc_flags & SC_CCP_UP) {
1406 if (!rcvd) {
1407 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1408 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1409 } else {
1410 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1411 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1412 s = splhigh(); /* XXX IMP ME HARDER */
1413 sc->sc_flags &= ~SC_DC_ERROR;
1414 splx(s);
1415 }
1416 }
1417 }
1418 break;
1419 }
1420 }
1421
1422 /*
1423 * CCP is down; free (de)compressor state if necessary.
1424 */
1425 static void
1426 ppp_ccp_closed(sc)
1427 struct ppp_softc *sc;
1428 {
1429 if (sc->sc_xc_state) {
1430 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1431 sc->sc_xc_state = NULL;
1432 }
1433 if (sc->sc_rc_state) {
1434 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1435 sc->sc_rc_state = NULL;
1436 }
1437 }
1438 #endif /* PPP_COMPRESS */
1439
1440 /*
1441 * PPP packet input routine.
1442 * The caller has checked and removed the FCS and has inserted
1443 * the address/control bytes and the protocol high byte if they
1444 * were omitted.
1445 */
1446 void
1447 ppppktin(sc, m, lost)
1448 struct ppp_softc *sc;
1449 struct mbuf *m;
1450 int lost;
1451 {
1452 int s = splhigh(); /* XXX IMP ME HARDER */
1453
1454 if (lost)
1455 m->m_flags |= M_ERRMARK;
1456 IF_ENQUEUE(&sc->sc_rawq, m);
1457 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
1458 softintr_schedule(sc->sc_si);
1459 #else
1460 schednetisr(NETISR_PPP);
1461 #endif
1462 splx(s);
1463 }
1464
1465 /*
1466 * Process a received PPP packet, doing decompression as necessary.
1467 * Should be called at splsoftnet.
1468 */
1469 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1470 TYPE_UNCOMPRESSED_TCP)
1471
1472 static void
1473 ppp_inproc(sc, m)
1474 struct ppp_softc *sc;
1475 struct mbuf *m;
1476 {
1477 struct ifnet *ifp = &sc->sc_if;
1478 struct ifqueue *inq;
1479 int s, ilen, proto, rv;
1480 u_char *cp, adrs, ctrl;
1481 struct mbuf *mp, *dmp = NULL;
1482 #ifdef VJC
1483 int xlen;
1484 u_char *iphdr;
1485 u_int hlen;
1486 #endif
1487
1488 sc->sc_stats.ppp_ipackets++;
1489
1490 if (sc->sc_flags & SC_LOG_INPKT) {
1491 ilen = 0;
1492 for (mp = m; mp != NULL; mp = mp->m_next)
1493 ilen += mp->m_len;
1494 printf("%s: got %d bytes\n", ifp->if_xname, ilen);
1495 pppdumpm(m);
1496 }
1497
1498 cp = mtod(m, u_char *);
1499 adrs = PPP_ADDRESS(cp);
1500 ctrl = PPP_CONTROL(cp);
1501 proto = PPP_PROTOCOL(cp);
1502
1503 if (m->m_flags & M_ERRMARK) {
1504 m->m_flags &= ~M_ERRMARK;
1505 s = splhigh(); /* XXX IMP ME HARDER */
1506 sc->sc_flags |= SC_VJ_RESET;
1507 splx(s);
1508 }
1509
1510 #ifdef PPP_COMPRESS
1511 /*
1512 * Decompress this packet if necessary, update the receiver's
1513 * dictionary, or take appropriate action on a CCP packet.
1514 */
1515 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1516 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1517 /* decompress this packet */
1518 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1519 if (rv == DECOMP_OK) {
1520 m_freem(m);
1521 if (dmp == NULL) {
1522 /* no error, but no decompressed packet produced */
1523 return;
1524 }
1525 m = dmp;
1526 cp = mtod(m, u_char *);
1527 proto = PPP_PROTOCOL(cp);
1528
1529 } else {
1530 /*
1531 * An error has occurred in decompression.
1532 * Pass the compressed packet up to pppd, which may take
1533 * CCP down or issue a Reset-Req.
1534 */
1535 if (sc->sc_flags & SC_DEBUG)
1536 printf("%s: decompress failed %d\n", ifp->if_xname, rv);
1537 s = splhigh(); /* XXX IMP ME HARDER */
1538 sc->sc_flags |= SC_VJ_RESET;
1539 if (rv == DECOMP_ERROR)
1540 sc->sc_flags |= SC_DC_ERROR;
1541 else
1542 sc->sc_flags |= SC_DC_FERROR;
1543 splx(s);
1544 }
1545
1546 } else {
1547 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1548 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1549 }
1550 if (proto == PPP_CCP) {
1551 ppp_ccp(sc, m, 1);
1552 }
1553 }
1554 #endif
1555
1556 ilen = 0;
1557 for (mp = m; mp != NULL; mp = mp->m_next)
1558 ilen += mp->m_len;
1559
1560 #ifdef VJC
1561 if (sc->sc_flags & SC_VJ_RESET) {
1562 /*
1563 * If we've missed a packet, we must toss subsequent compressed
1564 * packets which don't have an explicit connection ID.
1565 */
1566 if (sc->sc_comp)
1567 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1568 s = splhigh(); /* XXX IMP ME HARDER */
1569 sc->sc_flags &= ~SC_VJ_RESET;
1570 splx(s);
1571 }
1572
1573 /*
1574 * See if we have a VJ-compressed packet to uncompress.
1575 */
1576 if (proto == PPP_VJC_COMP) {
1577 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1578 goto bad;
1579
1580 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1581 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1582 sc->sc_comp, &iphdr, &hlen);
1583
1584 if (xlen <= 0) {
1585 if (sc->sc_flags & SC_DEBUG)
1586 printf("%s: VJ uncompress failed on type comp\n",
1587 ifp->if_xname);
1588 goto bad;
1589 }
1590
1591 /* Copy the PPP and IP headers into a new mbuf. */
1592 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1593 if (mp == NULL)
1594 goto bad;
1595 mp->m_len = 0;
1596 mp->m_next = NULL;
1597 if (hlen + PPP_HDRLEN > MHLEN) {
1598 MCLGET(mp, M_DONTWAIT);
1599 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1600 m_freem(mp);
1601 goto bad; /* lose if big headers and no clusters */
1602 }
1603 }
1604 cp = mtod(mp, u_char *);
1605 cp[0] = adrs;
1606 cp[1] = ctrl;
1607 cp[2] = 0;
1608 cp[3] = PPP_IP;
1609 proto = PPP_IP;
1610 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1611 mp->m_len = hlen + PPP_HDRLEN;
1612
1613 /*
1614 * Trim the PPP and VJ headers off the old mbuf
1615 * and stick the new and old mbufs together.
1616 */
1617 m->m_data += PPP_HDRLEN + xlen;
1618 m->m_len -= PPP_HDRLEN + xlen;
1619 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1620 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1621 mp->m_len += m->m_len;
1622 MFREE(m, mp->m_next);
1623 } else
1624 mp->m_next = m;
1625 m = mp;
1626 ilen += hlen - xlen;
1627
1628 } else if (proto == PPP_VJC_UNCOMP) {
1629 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1630 goto bad;
1631
1632 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1633 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1634 sc->sc_comp, &iphdr, &hlen);
1635
1636 if (xlen < 0) {
1637 if (sc->sc_flags & SC_DEBUG)
1638 printf("%s: VJ uncompress failed on type uncomp\n",
1639 ifp->if_xname);
1640 goto bad;
1641 }
1642
1643 proto = PPP_IP;
1644 cp[3] = PPP_IP;
1645 }
1646 #endif /* VJC */
1647
1648 /*
1649 * If the packet will fit in a header mbuf, don't waste a
1650 * whole cluster on it.
1651 */
1652 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1653 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1654 if (mp != NULL) {
1655 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1656 m_freem(m);
1657 m = mp;
1658 m->m_len = ilen;
1659 }
1660 }
1661 m->m_pkthdr.len = ilen;
1662 m->m_pkthdr.rcvif = ifp;
1663
1664 if ((proto & 0x8000) == 0) {
1665 #ifdef PPP_FILTER
1666 /*
1667 * See whether we want to pass this packet, and
1668 * if it counts as link activity.
1669 */
1670 if (sc->sc_pass_filt_in.bf_insns != 0
1671 && bpf_filter(sc->sc_pass_filt_in.bf_insns, (u_char *) m,
1672 ilen, 0) == 0) {
1673 /* drop this packet */
1674 m_freem(m);
1675 return;
1676 }
1677 if (sc->sc_active_filt_in.bf_insns == 0
1678 || bpf_filter(sc->sc_active_filt_in.bf_insns, (u_char *) m,
1679 ilen, 0))
1680 sc->sc_last_recv = time.tv_sec;
1681 #else
1682 /*
1683 * Record the time that we received this packet.
1684 */
1685 sc->sc_last_recv = time.tv_sec;
1686 #endif /* PPP_FILTER */
1687 }
1688
1689 #if NBPFILTER > 0
1690 /* See if bpf wants to look at the packet. */
1691 if (sc->sc_if.if_bpf)
1692 bpf_mtap(sc->sc_if.if_bpf, m);
1693 #endif
1694
1695 rv = 0;
1696 switch (proto) {
1697 #ifdef INET
1698 case PPP_IP:
1699 /*
1700 * IP packet - take off the ppp header and pass it up to IP.
1701 */
1702 if ((ifp->if_flags & IFF_UP) == 0
1703 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1704 /* interface is down - drop the packet. */
1705 m_freem(m);
1706 return;
1707 }
1708 m->m_pkthdr.len -= PPP_HDRLEN;
1709 m->m_data += PPP_HDRLEN;
1710 m->m_len -= PPP_HDRLEN;
1711 #ifdef GATEWAY
1712 if (ipflow_fastforward(m))
1713 return;
1714 #endif
1715 schednetisr(NETISR_IP);
1716 inq = &ipintrq;
1717 break;
1718 #endif
1719
1720 #ifdef INET6
1721 case PPP_IPV6:
1722 /*
1723 * IPv6 packet - take off the ppp header and pass it up to IPv6.
1724 */
1725 if ((ifp->if_flags & IFF_UP) == 0
1726 || sc->sc_npmode[NP_IPV6] != NPMODE_PASS) {
1727 /* interface is down - drop the packet. */
1728 m_freem(m);
1729 return;
1730 }
1731 m->m_pkthdr.len -= PPP_HDRLEN;
1732 m->m_data += PPP_HDRLEN;
1733 m->m_len -= PPP_HDRLEN;
1734 schednetisr(NETISR_IPV6);
1735 inq = &ip6intrq;
1736 break;
1737 #endif
1738
1739 default:
1740 /*
1741 * Some other protocol - place on input queue for read().
1742 */
1743 inq = &sc->sc_inq;
1744 rv = 1;
1745 break;
1746 }
1747
1748 /*
1749 * Put the packet on the appropriate input queue.
1750 */
1751 s = splnet();
1752 if (IF_QFULL(inq)) {
1753 IF_DROP(inq);
1754 splx(s);
1755 if (sc->sc_flags & SC_DEBUG)
1756 printf("%s: input queue full\n", ifp->if_xname);
1757 ifp->if_iqdrops++;
1758 goto bad;
1759 }
1760 IF_ENQUEUE(inq, m);
1761 splx(s);
1762 ifp->if_ipackets++;
1763 ifp->if_ibytes += ilen;
1764
1765 if (rv)
1766 (*sc->sc_ctlp)(sc);
1767
1768 return;
1769
1770 bad:
1771 m_freem(m);
1772 sc->sc_if.if_ierrors++;
1773 sc->sc_stats.ppp_ierrors++;
1774 }
1775
1776 #define MAX_DUMP_BYTES 128
1777
1778 static void
1779 pppdumpm(m0)
1780 struct mbuf *m0;
1781 {
1782 char buf[3*MAX_DUMP_BYTES+4];
1783 char *bp = buf;
1784 struct mbuf *m;
1785 static char digits[] = "0123456789abcdef";
1786
1787 for (m = m0; m; m = m->m_next) {
1788 int l = m->m_len;
1789 u_char *rptr = (u_char *)m->m_data;
1790
1791 while (l--) {
1792 if (bp > buf + sizeof(buf) - 4)
1793 goto done;
1794 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1795 *bp++ = digits[*rptr++ & 0xf];
1796 }
1797
1798 if (m->m_next) {
1799 if (bp > buf + sizeof(buf) - 3)
1800 goto done;
1801 *bp++ = '|';
1802 } else
1803 *bp++ = ' ';
1804 }
1805 done:
1806 if (m)
1807 *bp++ = '>';
1808 *bp = 0;
1809 printf("%s\n", buf);
1810 }
1811
1812 #ifdef ALTQ
1813 /*
1814 * a wrapper to transmit a packet from if_start since ALTQ uses
1815 * if_start to send a packet.
1816 */
1817 static void
1818 ppp_ifstart(ifp)
1819 struct ifnet *ifp;
1820 {
1821 struct ppp_softc *sc;
1822
1823 sc = ifp->if_softc;
1824 (*sc->sc_start)(sc);
1825 }
1826 #endif
1827