if_ppp.c revision 1.105.4.1 1 /* $NetBSD: if_ppp.c,v 1.105.4.1 2006/02/04 14:18:52 simonb Exp $ */
2 /* Id: if_ppp.c,v 1.6 1997/03/04 03:33:00 paulus Exp */
3
4 /*
5 * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
6 *
7 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The name "Carnegie Mellon University" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For permission or any legal
24 * details, please contact
25 * Office of Technology Transfer
26 * Carnegie Mellon University
27 * 5000 Forbes Avenue
28 * Pittsburgh, PA 15213-3890
29 * (412) 268-4387, fax: (412) 268-7395
30 * tech-transfer (at) andrew.cmu.edu
31 *
32 * 4. Redistributions of any form whatsoever must retain the following
33 * acknowledgment:
34 * "This product includes software developed by Computing Services
35 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
36 *
37 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
38 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
39 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
40 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
41 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
42 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
43 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
44 *
45 * Based on:
46 * @(#)if_sl.c 7.6.1.2 (Berkeley) 2/15/89
47 *
48 * Copyright (c) 1987 Regents of the University of California.
49 * All rights reserved.
50 *
51 * Redistribution and use in source and binary forms are permitted
52 * provided that the above copyright notice and this paragraph are
53 * duplicated in all such forms and that any documentation,
54 * advertising materials, and other materials related to such
55 * distribution and use acknowledge that the software was developed
56 * by the University of California, Berkeley. The name of the
57 * University may not be used to endorse or promote products derived
58 * from this software without specific prior written permission.
59 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
60 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
61 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
62 *
63 * Serial Line interface
64 *
65 * Rick Adams
66 * Center for Seismic Studies
67 * 1300 N 17th Street, Suite 1450
68 * Arlington, Virginia 22209
69 * (703)276-7900
70 * rick (at) seismo.ARPA
71 * seismo!rick
72 *
73 * Pounded on heavily by Chris Torek (chris (at) mimsy.umd.edu, umcp-cs!chris).
74 * Converted to 4.3BSD Beta by Chris Torek.
75 * Other changes made at Berkeley, based in part on code by Kirk Smith.
76 *
77 * Converted to 4.3BSD+ 386BSD by Brad Parker (brad (at) cayman.com)
78 * Added VJ tcp header compression; more unified ioctls
79 *
80 * Extensively modified by Paul Mackerras (paulus (at) cs.anu.edu.au).
81 * Cleaned up a lot of the mbuf-related code to fix bugs that
82 * caused system crashes and packet corruption. Changed pppstart
83 * so that it doesn't just give up with a collision if the whole
84 * packet doesn't fit in the output ring buffer.
85 *
86 * Added priority queueing for interactive IP packets, following
87 * the model of if_sl.c, plus hooks for bpf.
88 * Paul Mackerras (paulus (at) cs.anu.edu.au).
89 */
90
91 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
92 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
93
94 /*
95 * XXX IMP ME HARDER
96 *
97 * This is an explanation of that comment. This code used to use
98 * splimp() to block both network and tty interrupts. However,
99 * that call is deprecated. So, we have replaced the uses of
100 * splimp() with splhigh() in order to applomplish what it needs
101 * to accomplish, and added that happy little comment.
102 */
103
104 #include <sys/cdefs.h>
105 __KERNEL_RCSID(0, "$NetBSD: if_ppp.c,v 1.105.4.1 2006/02/04 14:18:52 simonb 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
129 #include <net/if.h>
130 #include <net/if_types.h>
131 #include <net/netisr.h>
132 #include <net/route.h>
133 #ifdef PPP_FILTER
134 #include <net/bpf.h>
135 #endif
136
137 #include <machine/intr.h>
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, caddr_t);
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);
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 = softintr_establish(IPL_SOFTNET, 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 softintr_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, caddr_t data, 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_second;
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(struct ifnet *ifp, u_long cmd, caddr_t data)
746 {
747 struct proc *p = curproc; /* XXX */
748 struct ppp_softc *sc = ifp->if_softc;
749 struct ifaddr *ifa = (struct ifaddr *)data;
750 struct ifreq *ifr = (struct ifreq *)data;
751 struct ppp_stats *psp;
752 #ifdef PPP_COMPRESS
753 struct ppp_comp_stats *pcp;
754 #endif
755 int s = splnet(), error = 0;
756
757 switch (cmd) {
758 case SIOCSIFFLAGS:
759 if ((ifp->if_flags & IFF_RUNNING) == 0)
760 ifp->if_flags &= ~IFF_UP;
761 break;
762
763 case SIOCSIFADDR:
764 switch (ifa->ifa_addr->sa_family) {
765 #ifdef INET
766 case AF_INET:
767 break;
768 #endif
769 #ifdef INET6
770 case AF_INET6:
771 break;
772 #endif
773 default:
774 error = EAFNOSUPPORT;
775 break;
776 }
777 break;
778
779 case SIOCSIFDSTADDR:
780 switch (ifa->ifa_addr->sa_family) {
781 #ifdef INET
782 case AF_INET:
783 break;
784 #endif
785 #ifdef INET6
786 case AF_INET6:
787 break;
788 #endif
789 default:
790 error = EAFNOSUPPORT;
791 break;
792 }
793 break;
794
795 case SIOCSIFMTU:
796 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
797 break;
798 sc->sc_if.if_mtu = ifr->ifr_mtu;
799 break;
800
801 case SIOCGIFMTU:
802 ifr->ifr_mtu = sc->sc_if.if_mtu;
803 break;
804
805 case SIOCADDMULTI:
806 case SIOCDELMULTI:
807 if (ifr == 0) {
808 error = EAFNOSUPPORT;
809 break;
810 }
811 switch (ifr->ifr_addr.sa_family) {
812 #ifdef INET
813 case AF_INET:
814 break;
815 #endif
816 #ifdef INET6
817 case AF_INET6:
818 break;
819 #endif
820 default:
821 error = EAFNOSUPPORT;
822 break;
823 }
824 break;
825
826 case SIOCGPPPSTATS:
827 psp = &((struct ifpppstatsreq *) data)->stats;
828 memset(psp, 0, sizeof(*psp));
829 psp->p = sc->sc_stats;
830 #if defined(VJC) && !defined(SL_NO_STATS)
831 if (sc->sc_comp) {
832 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
833 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
834 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
835 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
836 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
837 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
838 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
839 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
840 }
841 #endif /* VJC */
842 break;
843
844 #ifdef PPP_COMPRESS
845 case SIOCGPPPCSTATS:
846 pcp = &((struct ifpppcstatsreq *) data)->stats;
847 memset(pcp, 0, sizeof(*pcp));
848 if (sc->sc_xc_state != NULL)
849 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
850 if (sc->sc_rc_state != NULL)
851 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
852 break;
853 #endif /* PPP_COMPRESS */
854
855 default:
856 error = EINVAL;
857 }
858 splx(s);
859 return (error);
860 }
861
862 /*
863 * Queue a packet. Start transmission if not active.
864 * Packet is placed in Information field of PPP frame.
865 */
866 int
867 pppoutput(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
868 struct rtentry *rtp)
869 {
870 struct ppp_softc *sc = ifp->if_softc;
871 int protocol, address, control;
872 u_char *cp;
873 int s, error;
874 #ifdef INET
875 struct ip *ip;
876 #endif
877 struct ifqueue *ifq;
878 enum NPmode mode;
879 int len;
880 ALTQ_DECL(struct altq_pktattr pktattr;)
881
882 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
883 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
884 error = ENETDOWN; /* sort of */
885 goto bad;
886 }
887
888 IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family, &pktattr);
889
890 /*
891 * Compute PPP header.
892 */
893 m0->m_flags &= ~M_HIGHPRI;
894 switch (dst->sa_family) {
895 #ifdef INET
896 case AF_INET:
897 address = PPP_ALLSTATIONS;
898 control = PPP_UI;
899 protocol = PPP_IP;
900 mode = sc->sc_npmode[NP_IP];
901
902 /*
903 * If this packet has the "low delay" bit set in the IP header,
904 * put it on the fastq instead.
905 */
906 ip = mtod(m0, struct ip *);
907 if (ip->ip_tos & IPTOS_LOWDELAY)
908 m0->m_flags |= M_HIGHPRI;
909 break;
910 #endif
911 #ifdef INET6
912 case AF_INET6:
913 address = PPP_ALLSTATIONS; /*XXX*/
914 control = PPP_UI; /*XXX*/
915 protocol = PPP_IPV6;
916 mode = sc->sc_npmode[NP_IPV6];
917
918 #if 0 /* XXX flowinfo/traffic class, maybe? */
919 /*
920 * If this packet has the "low delay" bit set in the IP header,
921 * put it on the fastq instead.
922 */
923 ip = mtod(m0, struct ip *);
924 if (ip->ip_tos & IPTOS_LOWDELAY)
925 m0->m_flags |= M_HIGHPRI;
926 #endif
927 break;
928 #endif
929 case AF_UNSPEC:
930 address = PPP_ADDRESS(dst->sa_data);
931 control = PPP_CONTROL(dst->sa_data);
932 protocol = PPP_PROTOCOL(dst->sa_data);
933 mode = NPMODE_PASS;
934 break;
935 default:
936 printf("%s: af%d not supported\n", ifp->if_xname, dst->sa_family);
937 error = EAFNOSUPPORT;
938 goto bad;
939 }
940
941 /*
942 * Drop this packet, or return an error, if necessary.
943 */
944 if (mode == NPMODE_ERROR) {
945 error = ENETDOWN;
946 goto bad;
947 }
948 if (mode == NPMODE_DROP) {
949 error = 0;
950 goto bad;
951 }
952
953 /*
954 * Add PPP header.
955 */
956 M_PREPEND(m0, PPP_HDRLEN, M_DONTWAIT);
957 if (m0 == NULL) {
958 error = ENOBUFS;
959 goto bad;
960 }
961
962 cp = mtod(m0, u_char *);
963 *cp++ = address;
964 *cp++ = control;
965 *cp++ = protocol >> 8;
966 *cp++ = protocol & 0xff;
967
968 len = m_length(m0);
969
970 if (sc->sc_flags & SC_LOG_OUTPKT) {
971 printf("%s output: ", ifp->if_xname);
972 pppdumpm(m0);
973 }
974
975 if ((protocol & 0x8000) == 0) {
976 #ifdef PPP_FILTER
977 /*
978 * Apply the pass and active filters to the packet,
979 * but only if it is a data packet.
980 */
981 if (sc->sc_pass_filt_out.bf_insns != 0
982 && bpf_filter(sc->sc_pass_filt_out.bf_insns, (u_char *) m0,
983 len, 0) == 0) {
984 error = 0; /* drop this packet */
985 goto bad;
986 }
987
988 /*
989 * Update the time we sent the most recent packet.
990 */
991 if (sc->sc_active_filt_out.bf_insns == 0
992 || bpf_filter(sc->sc_active_filt_out.bf_insns, (u_char *) m0,
993 len, 0))
994 sc->sc_last_sent = time_second;
995 #else
996 /*
997 * Update the time we sent the most recent packet.
998 */
999 sc->sc_last_sent = time_second;
1000 #endif /* PPP_FILTER */
1001 }
1002
1003 #if NBPFILTER > 0
1004 /*
1005 * See if bpf wants to look at the packet.
1006 */
1007 if (sc->sc_if.if_bpf)
1008 bpf_mtap(sc->sc_if.if_bpf, m0);
1009 #endif
1010
1011 /*
1012 * Put the packet on the appropriate queue.
1013 */
1014 s = splnet();
1015 if (mode == NPMODE_QUEUE) {
1016 /* XXX we should limit the number of packets on this queue */
1017 *sc->sc_npqtail = m0;
1018 m0->m_nextpkt = NULL;
1019 sc->sc_npqtail = &m0->m_nextpkt;
1020 } else {
1021 ifq = (m0->m_flags & M_HIGHPRI) ? &sc->sc_fastq : NULL;
1022 if ((error = ifq_enqueue2(&sc->sc_if, ifq, m0
1023 ALTQ_COMMA ALTQ_DECL(&pktattr))) != 0) {
1024 splx(s);
1025 sc->sc_if.if_oerrors++;
1026 sc->sc_stats.ppp_oerrors++;
1027 return (error);
1028 }
1029 ppp_restart(sc);
1030 }
1031 ifp->if_opackets++;
1032 ifp->if_obytes += len;
1033
1034 splx(s);
1035 return (0);
1036
1037 bad:
1038 m_freem(m0);
1039 return (error);
1040 }
1041
1042 /*
1043 * After a change in the NPmode for some NP, move packets from the
1044 * npqueue to the send queue or the fast queue as appropriate.
1045 * Should be called at splnet, since we muck with the queues.
1046 */
1047 static void
1048 ppp_requeue(struct ppp_softc *sc)
1049 {
1050 struct mbuf *m, **mpp;
1051 struct ifqueue *ifq;
1052 enum NPmode mode;
1053 int error;
1054
1055 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
1056 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
1057 case PPP_IP:
1058 mode = sc->sc_npmode[NP_IP];
1059 break;
1060 case PPP_IPV6:
1061 mode = sc->sc_npmode[NP_IPV6];
1062 break;
1063 default:
1064 mode = NPMODE_PASS;
1065 }
1066
1067 switch (mode) {
1068 case NPMODE_PASS:
1069 /*
1070 * This packet can now go on one of the queues to be sent.
1071 */
1072 *mpp = m->m_nextpkt;
1073 m->m_nextpkt = NULL;
1074 ifq = (m->m_flags & M_HIGHPRI) ? &sc->sc_fastq : NULL;
1075 if ((error = ifq_enqueue2(&sc->sc_if, ifq, m ALTQ_COMMA
1076 ALTQ_DECL(NULL))) != 0) {
1077 sc->sc_if.if_oerrors++;
1078 sc->sc_stats.ppp_oerrors++;
1079 }
1080 break;
1081
1082 case NPMODE_DROP:
1083 case NPMODE_ERROR:
1084 *mpp = m->m_nextpkt;
1085 m_freem(m);
1086 break;
1087
1088 case NPMODE_QUEUE:
1089 mpp = &m->m_nextpkt;
1090 break;
1091 }
1092 }
1093 sc->sc_npqtail = mpp;
1094 }
1095
1096 /*
1097 * Transmitter has finished outputting some stuff;
1098 * remember to call sc->sc_start later at splsoftnet.
1099 */
1100 void
1101 ppp_restart(struct ppp_softc *sc)
1102 {
1103 int s = splhigh(); /* XXX IMP ME HARDER */
1104
1105 sc->sc_flags &= ~SC_TBUSY;
1106 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
1107 softintr_schedule(sc->sc_si);
1108 #else
1109 schednetisr(NETISR_PPP);
1110 #endif
1111 splx(s);
1112 }
1113
1114 /*
1115 * Get a packet to send. This procedure is intended to be called at
1116 * splsoftnet, since it may involve time-consuming operations such as
1117 * applying VJ compression, packet compression, address/control and/or
1118 * protocol field compression to the packet.
1119 */
1120 struct mbuf *
1121 ppp_dequeue(struct ppp_softc *sc)
1122 {
1123 struct mbuf *m, *mp;
1124 u_char *cp;
1125 int address, control, protocol;
1126 int s;
1127
1128 /*
1129 * Grab a packet to send: first try the fast queue, then the
1130 * normal queue.
1131 */
1132 s = splnet();
1133 if (sc->sc_nfastq < sc->sc_maxfastq) {
1134 IF_DEQUEUE(&sc->sc_fastq, m);
1135 if (m != NULL)
1136 sc->sc_nfastq++;
1137 else
1138 IFQ_DEQUEUE(&sc->sc_if.if_snd, m);
1139 } else {
1140 sc->sc_nfastq = 0;
1141 IFQ_DEQUEUE(&sc->sc_if.if_snd, m);
1142 if (m == NULL) {
1143 IF_DEQUEUE(&sc->sc_fastq, m);
1144 if (m != NULL)
1145 sc->sc_nfastq++;
1146 }
1147 }
1148 splx(s);
1149
1150 if (m == NULL)
1151 return NULL;
1152
1153 ++sc->sc_stats.ppp_opackets;
1154
1155 /*
1156 * Extract the ppp header of the new packet.
1157 * The ppp header will be in one mbuf.
1158 */
1159 cp = mtod(m, u_char *);
1160 address = PPP_ADDRESS(cp);
1161 control = PPP_CONTROL(cp);
1162 protocol = PPP_PROTOCOL(cp);
1163
1164 switch (protocol) {
1165 case PPP_IP:
1166 #ifdef VJC
1167 /*
1168 * If the packet is a TCP/IP packet, see if we can compress it.
1169 */
1170 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
1171 struct ip *ip;
1172 int type;
1173
1174 mp = m;
1175 ip = (struct ip *) (cp + PPP_HDRLEN);
1176 if (mp->m_len <= PPP_HDRLEN) {
1177 mp = mp->m_next;
1178 if (mp == NULL)
1179 break;
1180 ip = mtod(mp, struct ip *);
1181 }
1182 /* this code assumes the IP/TCP header is in one non-shared mbuf */
1183 if (ip->ip_p == IPPROTO_TCP) {
1184 type = sl_compress_tcp(mp, ip, sc->sc_comp,
1185 !(sc->sc_flags & SC_NO_TCP_CCID));
1186 switch (type) {
1187 case TYPE_UNCOMPRESSED_TCP:
1188 protocol = PPP_VJC_UNCOMP;
1189 break;
1190 case TYPE_COMPRESSED_TCP:
1191 protocol = PPP_VJC_COMP;
1192 cp = mtod(m, u_char *);
1193 cp[0] = address; /* header has moved */
1194 cp[1] = control;
1195 cp[2] = 0;
1196 break;
1197 }
1198 cp[3] = protocol; /* update protocol in PPP header */
1199 }
1200 }
1201 #endif /* VJC */
1202 break;
1203
1204 #ifdef PPP_COMPRESS
1205 case PPP_CCP:
1206 ppp_ccp(sc, m, 0);
1207 break;
1208 #endif /* PPP_COMPRESS */
1209 }
1210
1211 #ifdef PPP_COMPRESS
1212 if (protocol != PPP_LCP && protocol != PPP_CCP
1213 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1214 struct mbuf *mcomp = NULL;
1215 int slen;
1216
1217 slen = 0;
1218 for (mp = m; mp != NULL; mp = mp->m_next)
1219 slen += mp->m_len;
1220 (*sc->sc_xcomp->compress)
1221 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
1222 if (mcomp != NULL) {
1223 if (sc->sc_flags & SC_CCP_UP) {
1224 /* Send the compressed packet instead of the original. */
1225 m_freem(m);
1226 m = mcomp;
1227 cp = mtod(m, u_char *);
1228 protocol = cp[3];
1229 } else {
1230 /* Can't transmit compressed packets until CCP is up. */
1231 m_freem(mcomp);
1232 }
1233 }
1234 }
1235 #endif /* PPP_COMPRESS */
1236
1237 /*
1238 * Compress the address/control and protocol, if possible.
1239 */
1240 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1241 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1242 protocol != PPP_LCP) {
1243 /* can compress address/control */
1244 m->m_data += 2;
1245 m->m_len -= 2;
1246 }
1247 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1248 /* can compress protocol */
1249 if (mtod(m, u_char *) == cp) {
1250 cp[2] = cp[1]; /* move address/control up */
1251 cp[1] = cp[0];
1252 }
1253 ++m->m_data;
1254 --m->m_len;
1255 }
1256
1257 return m;
1258 }
1259
1260 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
1261 void
1262 pppnetisr(void)
1263 {
1264 struct ppp_softc *sc;
1265
1266 for (sc = LIST_FIRST(&ppp_softc_list); sc != NULL;
1267 sc = LIST_NEXT(sc, sc_iflist))
1268 pppintr(sc);
1269 }
1270 #endif
1271
1272 /*
1273 * Software interrupt routine, called at splsoftnet.
1274 */
1275 static void
1276 pppintr(void *arg)
1277 {
1278 struct ppp_softc *sc = arg;
1279 struct mbuf *m;
1280 int s;
1281
1282 if (!(sc->sc_flags & SC_TBUSY)
1283 && (IFQ_IS_EMPTY(&sc->sc_if.if_snd) == 0 || sc->sc_fastq.ifq_head
1284 || sc->sc_outm)) {
1285 s = splhigh(); /* XXX IMP ME HARDER */
1286 sc->sc_flags |= SC_TBUSY;
1287 splx(s);
1288 (*sc->sc_start)(sc);
1289 }
1290 for (;;) {
1291 s = splnet();
1292 IF_DEQUEUE(&sc->sc_rawq, m);
1293 splx(s);
1294 if (m == NULL)
1295 break;
1296 ppp_inproc(sc, m);
1297 }
1298 }
1299
1300 #ifdef PPP_COMPRESS
1301 /*
1302 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1303 * 0 if it is about to be transmitted.
1304 */
1305 static void
1306 ppp_ccp(struct ppp_softc *sc, struct mbuf *m, 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(struct ppp_softc *sc)
1401 {
1402 if (sc->sc_xc_state) {
1403 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1404 sc->sc_xc_state = NULL;
1405 }
1406 if (sc->sc_rc_state) {
1407 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1408 sc->sc_rc_state = NULL;
1409 }
1410 }
1411 #endif /* PPP_COMPRESS */
1412
1413 /*
1414 * PPP packet input routine.
1415 * The caller has checked and removed the FCS and has inserted
1416 * the address/control bytes and the protocol high byte if they
1417 * were omitted.
1418 */
1419 void
1420 ppppktin(struct ppp_softc *sc, struct mbuf *m, int lost)
1421 {
1422 int s = splhigh(); /* XXX IMP ME HARDER */
1423
1424 if (lost)
1425 m->m_flags |= M_ERRMARK;
1426 IF_ENQUEUE(&sc->sc_rawq, m);
1427 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
1428 softintr_schedule(sc->sc_si);
1429 #else
1430 schednetisr(NETISR_PPP);
1431 #endif
1432 splx(s);
1433 }
1434
1435 /*
1436 * Process a received PPP packet, doing decompression as necessary.
1437 * Should be called at splsoftnet.
1438 */
1439 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1440 TYPE_UNCOMPRESSED_TCP)
1441
1442 static void
1443 ppp_inproc(struct ppp_softc *sc, struct mbuf *m)
1444 {
1445 struct ifnet *ifp = &sc->sc_if;
1446 struct ifqueue *inq;
1447 int s, ilen, proto, rv;
1448 u_char *cp, adrs, ctrl;
1449 struct mbuf *mp, *dmp = NULL;
1450 #ifdef VJC
1451 int xlen;
1452 u_char *iphdr;
1453 u_int hlen;
1454 #endif
1455
1456 sc->sc_stats.ppp_ipackets++;
1457
1458 if (sc->sc_flags & SC_LOG_INPKT) {
1459 ilen = 0;
1460 for (mp = m; mp != NULL; mp = mp->m_next)
1461 ilen += mp->m_len;
1462 printf("%s: got %d bytes\n", ifp->if_xname, ilen);
1463 pppdumpm(m);
1464 }
1465
1466 cp = mtod(m, u_char *);
1467 adrs = PPP_ADDRESS(cp);
1468 ctrl = PPP_CONTROL(cp);
1469 proto = PPP_PROTOCOL(cp);
1470
1471 if (m->m_flags & M_ERRMARK) {
1472 m->m_flags &= ~M_ERRMARK;
1473 s = splhigh(); /* XXX IMP ME HARDER */
1474 sc->sc_flags |= SC_VJ_RESET;
1475 splx(s);
1476 }
1477
1478 #ifdef PPP_COMPRESS
1479 /*
1480 * Decompress this packet if necessary, update the receiver's
1481 * dictionary, or take appropriate action on a CCP packet.
1482 */
1483 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1484 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1485 /* decompress this packet */
1486 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1487 if (rv == DECOMP_OK) {
1488 m_freem(m);
1489 if (dmp == NULL) {
1490 /* no error, but no decompressed packet produced */
1491 return;
1492 }
1493 m = dmp;
1494 cp = mtod(m, u_char *);
1495 proto = PPP_PROTOCOL(cp);
1496
1497 } else {
1498 /*
1499 * An error has occurred in decompression.
1500 * Pass the compressed packet up to pppd, which may take
1501 * CCP down or issue a Reset-Req.
1502 */
1503 if (sc->sc_flags & SC_DEBUG)
1504 printf("%s: decompress failed %d\n", ifp->if_xname, rv);
1505 s = splhigh(); /* XXX IMP ME HARDER */
1506 sc->sc_flags |= SC_VJ_RESET;
1507 if (rv == DECOMP_ERROR)
1508 sc->sc_flags |= SC_DC_ERROR;
1509 else
1510 sc->sc_flags |= SC_DC_FERROR;
1511 splx(s);
1512 }
1513
1514 } else {
1515 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1516 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1517 }
1518 if (proto == PPP_CCP) {
1519 ppp_ccp(sc, m, 1);
1520 }
1521 }
1522 #endif
1523
1524 ilen = 0;
1525 for (mp = m; mp != NULL; mp = mp->m_next)
1526 ilen += mp->m_len;
1527
1528 #ifdef VJC
1529 if (sc->sc_flags & SC_VJ_RESET) {
1530 /*
1531 * If we've missed a packet, we must toss subsequent compressed
1532 * packets which don't have an explicit connection ID.
1533 */
1534 if (sc->sc_comp)
1535 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1536 s = splhigh(); /* XXX IMP ME HARDER */
1537 sc->sc_flags &= ~SC_VJ_RESET;
1538 splx(s);
1539 }
1540
1541 /*
1542 * See if we have a VJ-compressed packet to uncompress.
1543 */
1544 if (proto == PPP_VJC_COMP) {
1545 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1546 goto bad;
1547
1548 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1549 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1550 sc->sc_comp, &iphdr, &hlen);
1551
1552 if (xlen <= 0) {
1553 if (sc->sc_flags & SC_DEBUG)
1554 printf("%s: VJ uncompress failed on type comp\n",
1555 ifp->if_xname);
1556 goto bad;
1557 }
1558
1559 /* Copy the PPP and IP headers into a new mbuf. */
1560 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1561 if (mp == NULL)
1562 goto bad;
1563 mp->m_len = 0;
1564 mp->m_next = NULL;
1565 if (hlen + PPP_HDRLEN > MHLEN) {
1566 MCLGET(mp, M_DONTWAIT);
1567 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1568 m_freem(mp);
1569 goto bad; /* lose if big headers and no clusters */
1570 }
1571 }
1572 cp = mtod(mp, u_char *);
1573 cp[0] = adrs;
1574 cp[1] = ctrl;
1575 cp[2] = 0;
1576 cp[3] = PPP_IP;
1577 proto = PPP_IP;
1578 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1579 mp->m_len = hlen + PPP_HDRLEN;
1580
1581 /*
1582 * Trim the PPP and VJ headers off the old mbuf
1583 * and stick the new and old mbufs together.
1584 */
1585 m->m_data += PPP_HDRLEN + xlen;
1586 m->m_len -= PPP_HDRLEN + xlen;
1587 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1588 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1589 mp->m_len += m->m_len;
1590 MFREE(m, mp->m_next);
1591 } else
1592 mp->m_next = m;
1593 m = mp;
1594 ilen += hlen - xlen;
1595
1596 } else if (proto == PPP_VJC_UNCOMP) {
1597 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1598 goto bad;
1599
1600 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1601 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1602 sc->sc_comp, &iphdr, &hlen);
1603
1604 if (xlen < 0) {
1605 if (sc->sc_flags & SC_DEBUG)
1606 printf("%s: VJ uncompress failed on type uncomp\n",
1607 ifp->if_xname);
1608 goto bad;
1609 }
1610
1611 proto = PPP_IP;
1612 cp[3] = PPP_IP;
1613 }
1614 #endif /* VJC */
1615
1616 /*
1617 * If the packet will fit in a header mbuf, don't waste a
1618 * whole cluster on it.
1619 */
1620 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1621 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1622 if (mp != NULL) {
1623 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1624 m_freem(m);
1625 m = mp;
1626 m->m_len = ilen;
1627 }
1628 }
1629 m->m_pkthdr.len = ilen;
1630 m->m_pkthdr.rcvif = ifp;
1631
1632 if ((proto & 0x8000) == 0) {
1633 #ifdef PPP_FILTER
1634 /*
1635 * See whether we want to pass this packet, and
1636 * if it counts as link activity.
1637 */
1638 if (sc->sc_pass_filt_in.bf_insns != 0
1639 && bpf_filter(sc->sc_pass_filt_in.bf_insns, (u_char *) m,
1640 ilen, 0) == 0) {
1641 /* drop this packet */
1642 m_freem(m);
1643 return;
1644 }
1645 if (sc->sc_active_filt_in.bf_insns == 0
1646 || bpf_filter(sc->sc_active_filt_in.bf_insns, (u_char *) m,
1647 ilen, 0))
1648 sc->sc_last_recv = time_second;
1649 #else
1650 /*
1651 * Record the time that we received this packet.
1652 */
1653 sc->sc_last_recv = time_second;
1654 #endif /* PPP_FILTER */
1655 }
1656
1657 #if NBPFILTER > 0
1658 /* See if bpf wants to look at the packet. */
1659 if (sc->sc_if.if_bpf)
1660 bpf_mtap(sc->sc_if.if_bpf, m);
1661 #endif
1662
1663 rv = 0;
1664 switch (proto) {
1665 #ifdef INET
1666 case PPP_IP:
1667 /*
1668 * IP packet - take off the ppp header and pass it up to IP.
1669 */
1670 if ((ifp->if_flags & IFF_UP) == 0
1671 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1672 /* interface is down - drop the packet. */
1673 m_freem(m);
1674 return;
1675 }
1676 m->m_pkthdr.len -= PPP_HDRLEN;
1677 m->m_data += PPP_HDRLEN;
1678 m->m_len -= PPP_HDRLEN;
1679 #ifdef GATEWAY
1680 if (ipflow_fastforward(m))
1681 return;
1682 #endif
1683 schednetisr(NETISR_IP);
1684 inq = &ipintrq;
1685 break;
1686 #endif
1687
1688 #ifdef INET6
1689 case PPP_IPV6:
1690 /*
1691 * IPv6 packet - take off the ppp header and pass it up to IPv6.
1692 */
1693 if ((ifp->if_flags & IFF_UP) == 0
1694 || sc->sc_npmode[NP_IPV6] != NPMODE_PASS) {
1695 /* interface is down - drop the packet. */
1696 m_freem(m);
1697 return;
1698 }
1699 m->m_pkthdr.len -= PPP_HDRLEN;
1700 m->m_data += PPP_HDRLEN;
1701 m->m_len -= PPP_HDRLEN;
1702 schednetisr(NETISR_IPV6);
1703 inq = &ip6intrq;
1704 break;
1705 #endif
1706
1707 default:
1708 /*
1709 * Some other protocol - place on input queue for read().
1710 */
1711 inq = &sc->sc_inq;
1712 rv = 1;
1713 break;
1714 }
1715
1716 /*
1717 * Put the packet on the appropriate input queue.
1718 */
1719 s = splnet();
1720 if (IF_QFULL(inq)) {
1721 IF_DROP(inq);
1722 splx(s);
1723 if (sc->sc_flags & SC_DEBUG)
1724 printf("%s: input queue full\n", ifp->if_xname);
1725 ifp->if_iqdrops++;
1726 goto bad;
1727 }
1728 IF_ENQUEUE(inq, m);
1729 splx(s);
1730 ifp->if_ipackets++;
1731 ifp->if_ibytes += ilen;
1732
1733 if (rv)
1734 (*sc->sc_ctlp)(sc);
1735
1736 return;
1737
1738 bad:
1739 m_freem(m);
1740 sc->sc_if.if_ierrors++;
1741 sc->sc_stats.ppp_ierrors++;
1742 }
1743
1744 #define MAX_DUMP_BYTES 128
1745
1746 static void
1747 pppdumpm(struct mbuf *m0)
1748 {
1749 char buf[3*MAX_DUMP_BYTES+4];
1750 char *bp = buf;
1751 struct mbuf *m;
1752
1753 for (m = m0; m; m = m->m_next) {
1754 int l = m->m_len;
1755 u_char *rptr = (u_char *)m->m_data;
1756
1757 while (l--) {
1758 if (bp > buf + sizeof(buf) - 4)
1759 goto done;
1760 *bp++ = hexdigits[*rptr >> 4]; /* convert byte to ascii hex */
1761 *bp++ = hexdigits[*rptr++ & 0xf];
1762 }
1763
1764 if (m->m_next) {
1765 if (bp > buf + sizeof(buf) - 3)
1766 goto done;
1767 *bp++ = '|';
1768 } else
1769 *bp++ = ' ';
1770 }
1771 done:
1772 if (m)
1773 *bp++ = '>';
1774 *bp = 0;
1775 printf("%s\n", buf);
1776 }
1777
1778 #ifdef ALTQ
1779 /*
1780 * a wrapper to transmit a packet from if_start since ALTQ uses
1781 * if_start to send a packet.
1782 */
1783 static void
1784 ppp_ifstart(struct ifnet *ifp)
1785 {
1786 struct ppp_softc *sc;
1787
1788 sc = ifp->if_softc;
1789 (*sc->sc_start)(sc);
1790 }
1791 #endif
1792