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