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