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