FreeBSD/Linux Kernel Cross Reference
sys/net/if_ppp.c
1 /*
2 * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
3 *
4 * Copyright (c) 1989 Carnegie Mellon University.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by Carnegie Mellon University. The name of the
13 * University may not be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * Drew D. Perkins
20 * Carnegie Mellon University
21 * 4910 Forbes Ave.
22 * Pittsburgh, PA 15213
23 * (412) 268-8576
24 * ddp@andrew.cmu.edu
25 *
26 * Based on:
27 * @(#)if_sl.c 7.6.1.2 (Berkeley) 2/15/89
28 *
29 * Copyright (c) 1987 Regents of the University of California.
30 * All rights reserved.
31 *
32 * Redistribution and use in source and binary forms are permitted
33 * provided that the above copyright notice and this paragraph are
34 * duplicated in all such forms and that any documentation,
35 * advertising materials, and other materials related to such
36 * distribution and use acknowledge that the software was developed
37 * by the University of California, Berkeley. The name of the
38 * University may not be used to endorse or promote products derived
39 * from this software without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
41 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
42 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
43 *
44 * Serial Line interface
45 *
46 * Rick Adams
47 * Center for Seismic Studies
48 * 1300 N 17th Street, Suite 1450
49 * Arlington, Virginia 22209
50 * (703)276-7900
51 * rick@seismo.ARPA
52 * seismo!rick
53 *
54 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
55 * Converted to 4.3BSD Beta by Chris Torek.
56 * Other changes made at Berkeley, based in part on code by Kirk Smith.
57 *
58 * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com)
59 * Added VJ tcp header compression; more unified ioctls
60 *
61 * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au).
62 * Cleaned up a lot of the mbuf-related code to fix bugs that
63 * caused system crashes and packet corruption. Changed pppstart
64 * so that it doesn't just give up with a collision if the whole
65 * packet doesn't fit in the output ring buffer.
66 *
67 * Added priority queueing for interactive IP packets, following
68 * the model of if_sl.c, plus hooks for bpf.
69 * Paul Mackerras (paulus@cs.anu.edu.au).
70 */
71
72 /* $FreeBSD: releng/5.1/sys/net/if_ppp.c 112707 2003-03-27 12:52:57Z maxim $ */
73 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
74 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
75
76 #include "opt_inet.h"
77 #include "opt_ipx.h"
78 #include "opt_mac.h"
79 #include "opt_ppp.h"
80
81 #ifdef INET
82 #define VJC
83 #endif
84 #define PPP_COMPRESS
85
86 #include <sys/param.h>
87 #include <sys/systm.h>
88 #include <sys/proc.h>
89 #include <sys/mac.h>
90 #include <sys/mbuf.h>
91 #include <sys/socket.h>
92 #include <sys/filio.h>
93 #include <sys/sockio.h>
94 #include <sys/kernel.h>
95 #include <sys/time.h>
96 #include <sys/malloc.h>
97 #include <sys/module.h>
98
99 #include <net/if.h>
100 #include <net/if_types.h>
101 #include <net/netisr.h>
102 #include <net/bpf.h>
103
104 #if INET
105 #include <netinet/in.h>
106 #include <netinet/in_systm.h>
107 #include <netinet/in_var.h>
108 #include <netinet/ip.h>
109 #endif
110
111 #if IPX
112 #include <netipx/ipx.h>
113 #include <netipx/ipx_if.h>
114 #endif
115
116 #ifdef VJC
117 #include <net/slcompress.h>
118 #endif
119
120 #include <net/if_ppp.h>
121 #include <net/if_pppvar.h>
122
123 /* minimise diffs */
124 #ifndef splsoftnet
125 #define splsoftnet splnet
126 #endif
127
128 #ifdef PPP_COMPRESS
129 #define PACKETPTR struct mbuf *
130 #include <net/ppp_comp.h>
131 #endif
132
133 #define PPPNAME "ppp"
134 static MALLOC_DEFINE(M_PPP, PPPNAME, "PPP interface");
135 static LIST_HEAD(, ppp_softc) ppp_softc_list;
136
137 /* XXX layering violation */
138 extern void pppasyncattach(void *);
139 extern void pppasyncdetach(void);
140
141 static int pppsioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
142 static void pppintr(void);
143
144 static void ppp_requeue(struct ppp_softc *);
145 static void ppp_ccp(struct ppp_softc *, struct mbuf *m, int rcvd);
146 static void ppp_ccp_closed(struct ppp_softc *);
147 static void ppp_inproc(struct ppp_softc *, struct mbuf *);
148 static void pppdumpm(struct mbuf *m0);
149 static int ppp_clone_create(struct if_clone *, int);
150 static void ppp_clone_destroy(struct ifnet *);
151
152 static struct if_clone ppp_cloner = IF_CLONE_INITIALIZER(PPPNAME,
153 ppp_clone_create, ppp_clone_destroy, 0, IF_MAXUNIT);
154
155 /*
156 * Some useful mbuf macros not in mbuf.h.
157 */
158 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
159
160 #define M_DATASTART(m) \
161 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
162 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
163
164 #define M_DATASIZE(m) \
165 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
166 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
167
168 /*
169 * We steal two bits in the mbuf m_flags, to mark high-priority packets
170 * for output, and received packets following lost/corrupted packets.
171 */
172 #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */
173 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
174
175
176 #ifdef PPP_COMPRESS
177 /*
178 * List of compressors we know about.
179 * We leave some space so maybe we can modload compressors.
180 */
181
182 extern struct compressor ppp_bsd_compress;
183 extern struct compressor ppp_deflate, ppp_deflate_draft;
184
185 static struct compressor *ppp_compressors[8] = {
186 #if DO_BSD_COMPRESS && defined(PPP_BSDCOMP)
187 &ppp_bsd_compress,
188 #endif
189 #if DO_DEFLATE && defined(PPP_DEFLATE)
190 &ppp_deflate,
191 &ppp_deflate_draft,
192 #endif
193 NULL
194 };
195 #endif /* PPP_COMPRESS */
196
197 static int
198 ppp_clone_create(struct if_clone *ifc, int unit)
199 {
200 struct ppp_softc *sc;
201
202 sc = malloc(sizeof(struct ppp_softc), M_PPP, M_WAITOK | M_ZERO);
203 sc->sc_if.if_softc = sc;
204 sc->sc_if.if_name = PPPNAME;
205 sc->sc_if.if_unit = unit;
206 sc->sc_if.if_mtu = PPP_MTU;
207 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
208 sc->sc_if.if_type = IFT_PPP;
209 sc->sc_if.if_hdrlen = PPP_HDRLEN;
210 sc->sc_if.if_ioctl = pppsioctl;
211 sc->sc_if.if_output = pppoutput;
212 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
213 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
214 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
215 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
216 mtx_init(&sc->sc_inq.ifq_mtx, "ppp_inq", NULL, MTX_DEF);
217 mtx_init(&sc->sc_fastq.ifq_mtx, "ppp_fastq", NULL, MTX_DEF);
218 mtx_init(&sc->sc_rawq.ifq_mtx, "ppp_rawq", NULL, MTX_DEF);
219 if_attach(&sc->sc_if);
220 bpfattach(&sc->sc_if, DLT_PPP, PPP_HDRLEN);
221 LIST_INSERT_HEAD(&ppp_softc_list, sc, sc_list);
222
223 return (0);
224 }
225
226 static void
227 ppp_clone_destroy(struct ifnet *ifp)
228 {
229 struct ppp_softc *sc;
230
231 sc = ifp->if_softc;
232
233 LIST_REMOVE(sc, sc_list);
234 bpfdetach(ifp);
235 if_detach(ifp);
236 mtx_destroy(&sc->sc_rawq.ifq_mtx);
237 mtx_destroy(&sc->sc_fastq.ifq_mtx);
238 mtx_destroy(&sc->sc_inq.ifq_mtx);
239 }
240
241 static int
242 ppp_modevent(module_t mod, int type, void *data)
243 {
244 switch (type) {
245 case MOD_LOAD:
246 if_clone_attach(&ppp_cloner);
247
248 netisr_register(NETISR_PPP, (netisr_t *)pppintr, NULL);
249 /*
250 * XXX layering violation - if_ppp can work over any lower
251 * level transport that cares to attach to it.
252 */
253 pppasyncattach(NULL);
254 break;
255 case MOD_UNLOAD:
256 /* XXX: layering violation */
257 pppasyncdetach();
258
259 netisr_unregister(NETISR_PPP);
260
261 if_clone_detach(&ppp_cloner);
262
263 while (!LIST_EMPTY(&ppp_softc_list))
264 ppp_clone_destroy(
265 &LIST_FIRST(&ppp_softc_list)->sc_if);
266 break;
267 }
268 return 0;
269 }
270
271 static moduledata_t ppp_mod = {
272 "if_ppp",
273 ppp_modevent,
274 0
275 };
276
277 DECLARE_MODULE(if_ppp, ppp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
278
279 /*
280 * Allocate a ppp interface unit and initialize it.
281 */
282 struct ppp_softc *
283 pppalloc(pid)
284 pid_t pid;
285 {
286 int i;
287 char tmpname[IFNAMSIZ];
288 struct ifnet *ifp;
289 struct ppp_softc *sc;
290
291 LIST_FOREACH(sc, &ppp_softc_list, sc_list) {
292 if (sc->sc_xfer == pid) {
293 sc->sc_xfer = 0;
294 return sc;
295 }
296 }
297 LIST_FOREACH(sc, &ppp_softc_list, sc_list) {
298 if (sc->sc_devp == NULL)
299 break;
300 }
301 /* Try to clone an interface if we don't have a free one */
302 if (sc == NULL) {
303 strcpy(tmpname, PPPNAME);
304 if (if_clone_create(tmpname, sizeof(tmpname)) != 0)
305 return NULL;
306 ifp = ifunit(tmpname);
307 if (ifp == NULL)
308 return NULL;
309 sc = ifp->if_softc;
310 }
311 if (sc == NULL || sc->sc_devp != NULL)
312 return NULL;
313
314 sc->sc_flags = 0;
315 sc->sc_mru = PPP_MRU;
316 sc->sc_relinq = NULL;
317 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
318 #ifdef VJC
319 MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
320 M_DEVBUF, M_NOWAIT);
321 if (sc->sc_comp)
322 sl_compress_init(sc->sc_comp, -1);
323 #endif
324 #ifdef PPP_COMPRESS
325 sc->sc_xc_state = NULL;
326 sc->sc_rc_state = NULL;
327 #endif /* PPP_COMPRESS */
328 for (i = 0; i < NUM_NP; ++i)
329 sc->sc_npmode[i] = NPMODE_ERROR;
330 sc->sc_npqueue = NULL;
331 sc->sc_npqtail = &sc->sc_npqueue;
332 sc->sc_last_sent = sc->sc_last_recv = time_second;
333
334 return sc;
335 }
336
337 /*
338 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
339 */
340 void
341 pppdealloc(sc)
342 struct ppp_softc *sc;
343 {
344 struct mbuf *m;
345
346 if_down(&sc->sc_if);
347 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
348 getmicrotime(&sc->sc_if.if_lastchange);
349 sc->sc_devp = NULL;
350 sc->sc_xfer = 0;
351 IF_DRAIN(&sc->sc_rawq);
352 IF_DRAIN(&sc->sc_inq);
353 IF_DRAIN(&sc->sc_fastq);
354 while ((m = sc->sc_npqueue) != NULL) {
355 sc->sc_npqueue = m->m_nextpkt;
356 m_freem(m);
357 }
358 #ifdef PPP_COMPRESS
359 ppp_ccp_closed(sc);
360 sc->sc_xc_state = NULL;
361 sc->sc_rc_state = NULL;
362 #endif /* PPP_COMPRESS */
363 #ifdef PPP_FILTER
364 if (sc->sc_pass_filt.bf_insns != 0) {
365 free(sc->sc_pass_filt.bf_insns, M_DEVBUF);
366 sc->sc_pass_filt.bf_insns = 0;
367 sc->sc_pass_filt.bf_len = 0;
368 }
369 if (sc->sc_active_filt.bf_insns != 0) {
370 free(sc->sc_active_filt.bf_insns, M_DEVBUF);
371 sc->sc_active_filt.bf_insns = 0;
372 sc->sc_active_filt.bf_len = 0;
373 }
374 #endif /* PPP_FILTER */
375 #ifdef VJC
376 if (sc->sc_comp != 0) {
377 free(sc->sc_comp, M_DEVBUF);
378 sc->sc_comp = 0;
379 }
380 #endif
381 }
382
383 /*
384 * Ioctl routine for generic ppp devices.
385 */
386 int
387 pppioctl(sc, cmd, data, flag, td)
388 struct ppp_softc *sc;
389 u_long cmd;
390 caddr_t data;
391 int flag;
392 struct thread *td;
393 {
394 struct proc *p = td->td_proc;
395 int s, flags, mru, npx;
396 u_int nb;
397 int error = 0;
398 struct ppp_option_data *odp;
399 struct compressor **cp;
400 struct npioctl *npi;
401 time_t t;
402 #ifdef PPP_FILTER
403 struct bpf_program *bp, *nbp;
404 struct bpf_insn *newcode, *oldcode;
405 int newcodelen;
406 #endif /* PPP_FILTER */
407 #ifdef PPP_COMPRESS
408 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
409 #endif
410
411 switch (cmd) {
412 case FIONREAD:
413 *(int *)data = sc->sc_inq.ifq_len;
414 break;
415
416 case PPPIOCGUNIT:
417 *(int *)data = sc->sc_if.if_unit;
418 break;
419
420 case PPPIOCGFLAGS:
421 *(u_int *)data = sc->sc_flags;
422 break;
423
424 case PPPIOCSFLAGS:
425 if ((error = suser(td)) != 0)
426 break;
427 flags = *(int *)data & SC_MASK;
428 s = splsoftnet();
429 #ifdef PPP_COMPRESS
430 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
431 ppp_ccp_closed(sc);
432 #endif
433 splimp();
434 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
435 splx(s);
436 break;
437
438 case PPPIOCSMRU:
439 if ((error = suser(td)) != 0)
440 return (error);
441 mru = *(int *)data;
442 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
443 sc->sc_mru = mru;
444 break;
445
446 case PPPIOCGMRU:
447 *(int *)data = sc->sc_mru;
448 break;
449
450 #ifdef VJC
451 case PPPIOCSMAXCID:
452 if ((error = suser(td)) != 0)
453 break;
454 if (sc->sc_comp) {
455 s = splsoftnet();
456 sl_compress_init(sc->sc_comp, *(int *)data);
457 splx(s);
458 }
459 break;
460 #endif
461
462 case PPPIOCXFERUNIT:
463 if ((error = suser(td)) != 0)
464 break;
465 sc->sc_xfer = p->p_pid;
466 break;
467
468 #ifdef PPP_COMPRESS
469 case PPPIOCSCOMPRESS:
470 if ((error = suser(td)) != 0)
471 break;
472 odp = (struct ppp_option_data *) data;
473 nb = odp->length;
474 if (nb > sizeof(ccp_option))
475 nb = sizeof(ccp_option);
476 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
477 break;
478 if (ccp_option[1] < 2) { /* preliminary check on the length byte */
479 error = EINVAL;
480 break;
481 }
482 for (cp = ppp_compressors; *cp != NULL; ++cp)
483 if ((*cp)->compress_proto == ccp_option[0]) {
484 /*
485 * Found a handler for the protocol - try to allocate
486 * a compressor or decompressor.
487 */
488 error = 0;
489 if (odp->transmit) {
490 s = splsoftnet();
491 if (sc->sc_xc_state != NULL)
492 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
493 sc->sc_xcomp = *cp;
494 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
495 if (sc->sc_xc_state == NULL) {
496 if (sc->sc_flags & SC_DEBUG)
497 if_printf(&sc->sc_if, "comp_alloc failed\n");
498 error = ENOBUFS;
499 }
500 splimp();
501 sc->sc_flags &= ~SC_COMP_RUN;
502 splx(s);
503 } else {
504 s = splsoftnet();
505 if (sc->sc_rc_state != NULL)
506 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
507 sc->sc_rcomp = *cp;
508 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
509 if (sc->sc_rc_state == NULL) {
510 if (sc->sc_flags & SC_DEBUG)
511 if_printf(&sc->sc_if, "decomp_alloc failed\n");
512 error = ENOBUFS;
513 }
514 splimp();
515 sc->sc_flags &= ~SC_DECOMP_RUN;
516 splx(s);
517 }
518 break;
519 }
520 if (sc->sc_flags & SC_DEBUG)
521 if_printf(&sc->sc_if, "no compressor for [%x %x %x], %x\n",
522 ccp_option[0], ccp_option[1], ccp_option[2], nb);
523 error = EINVAL; /* no handler found */
524 break;
525 #endif /* PPP_COMPRESS */
526
527 case PPPIOCGNPMODE:
528 case PPPIOCSNPMODE:
529 npi = (struct npioctl *) data;
530 npx = 0; /* XXX: quiet gcc */
531 switch (npi->protocol) {
532 case PPP_IP:
533 npx = NP_IP;
534 break;
535 default:
536 error = EINVAL;
537 }
538 if (error)
539 break;
540 if (cmd == PPPIOCGNPMODE) {
541 npi->mode = sc->sc_npmode[npx];
542 } else {
543 if ((error = suser(td)) != 0)
544 break;
545 if (npi->mode != sc->sc_npmode[npx]) {
546 s = splsoftnet();
547 sc->sc_npmode[npx] = npi->mode;
548 if (npi->mode != NPMODE_QUEUE) {
549 ppp_requeue(sc);
550 (*sc->sc_start)(sc);
551 }
552 splx(s);
553 }
554 }
555 break;
556
557 case PPPIOCGIDLE:
558 s = splsoftnet();
559 t = time_second;
560 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
561 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
562 splx(s);
563 break;
564
565 #ifdef PPP_FILTER
566 case PPPIOCSPASS:
567 case PPPIOCSACTIVE:
568 nbp = (struct bpf_program *) data;
569 if ((unsigned) nbp->bf_len > BPF_MAXINSNS) {
570 error = EINVAL;
571 break;
572 }
573 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
574 if (newcodelen != 0) {
575 MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
576 if (newcode == 0) {
577 error = EINVAL; /* or sumpin */
578 break;
579 }
580 if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
581 newcodelen)) != 0) {
582 free(newcode, M_DEVBUF);
583 break;
584 }
585 if (!bpf_validate(newcode, nbp->bf_len)) {
586 free(newcode, M_DEVBUF);
587 error = EINVAL;
588 break;
589 }
590 } else
591 newcode = 0;
592 bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
593 oldcode = bp->bf_insns;
594 s = splimp();
595 bp->bf_len = nbp->bf_len;
596 bp->bf_insns = newcode;
597 splx(s);
598 if (oldcode != 0)
599 free(oldcode, M_DEVBUF);
600 break;
601 #endif
602
603 default:
604 error = ENOIOCTL;
605 break;
606 }
607 return (error);
608 }
609
610 /*
611 * Process an ioctl request to the ppp network interface.
612 */
613 static int
614 pppsioctl(ifp, cmd, data)
615 register struct ifnet *ifp;
616 u_long cmd;
617 caddr_t data;
618 {
619 struct thread *td = curthread; /* XXX */
620 register struct ppp_softc *sc = ifp->if_softc;
621 register struct ifaddr *ifa = (struct ifaddr *)data;
622 register struct ifreq *ifr = (struct ifreq *)data;
623 struct ppp_stats *psp;
624 #ifdef PPP_COMPRESS
625 struct ppp_comp_stats *pcp;
626 #endif
627 int s = splimp(), error = 0;
628
629 switch (cmd) {
630 case SIOCSIFFLAGS:
631 if ((ifp->if_flags & IFF_RUNNING) == 0)
632 ifp->if_flags &= ~IFF_UP;
633 break;
634
635 case SIOCSIFADDR:
636 case SIOCAIFADDR:
637 switch(ifa->ifa_addr->sa_family) {
638 #ifdef INET
639 case AF_INET:
640 break;
641 #endif
642 #ifdef IPX
643 case AF_IPX:
644 break;
645 #endif
646 default:
647 error = EAFNOSUPPORT;
648 break;
649 }
650 break;
651
652 case SIOCSIFDSTADDR:
653 switch(ifa->ifa_addr->sa_family) {
654 #ifdef INET
655 case AF_INET:
656 break;
657 #endif
658 #ifdef IPX
659 case AF_IPX:
660 break;
661 #endif
662 default:
663 error = EAFNOSUPPORT;
664 break;
665 }
666 break;
667
668 case SIOCSIFMTU:
669 if ((error = suser(td)) != 0)
670 break;
671 if (ifr->ifr_mtu > PPP_MAXMTU)
672 error = EINVAL;
673 else {
674 sc->sc_if.if_mtu = ifr->ifr_mtu;
675 if (sc->sc_setmtu)
676 (*sc->sc_setmtu)(sc);
677 }
678 break;
679
680 case SIOCGIFMTU:
681 ifr->ifr_mtu = sc->sc_if.if_mtu;
682 break;
683
684 case SIOCADDMULTI:
685 case SIOCDELMULTI:
686 if (ifr == 0) {
687 error = EAFNOSUPPORT;
688 break;
689 }
690 switch(ifr->ifr_addr.sa_family) {
691 #ifdef INET
692 case AF_INET:
693 break;
694 #endif
695 default:
696 error = EAFNOSUPPORT;
697 break;
698 }
699 break;
700
701 case SIOCGPPPSTATS:
702 psp = &((struct ifpppstatsreq *) data)->stats;
703 bzero(psp, sizeof(*psp));
704 psp->p = sc->sc_stats;
705 #if defined(VJC) && !defined(SL_NO_STATS)
706 if (sc->sc_comp) {
707 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
708 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
709 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
710 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
711 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
712 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
713 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
714 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
715 }
716 #endif /* VJC */
717 break;
718
719 #ifdef PPP_COMPRESS
720 case SIOCGPPPCSTATS:
721 pcp = &((struct ifpppcstatsreq *) data)->stats;
722 bzero(pcp, sizeof(*pcp));
723 if (sc->sc_xc_state != NULL)
724 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
725 if (sc->sc_rc_state != NULL)
726 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
727 break;
728 #endif /* PPP_COMPRESS */
729
730 default:
731 error = ENOTTY;
732 }
733 splx(s);
734 return (error);
735 }
736
737 /*
738 * Queue a packet. Start transmission if not active.
739 * Packet is placed in Information field of PPP frame.
740 * Called at splnet as the if->if_output handler.
741 * Called at splnet from pppwrite().
742 */
743 int
744 pppoutput(ifp, m0, dst, rtp)
745 struct ifnet *ifp;
746 struct mbuf *m0;
747 struct sockaddr *dst;
748 struct rtentry *rtp;
749 {
750 register struct ppp_softc *sc = ifp->if_softc;
751 int protocol, address, control;
752 u_char *cp;
753 int s, error;
754 struct ip *ip;
755 struct ifqueue *ifq;
756 enum NPmode mode;
757 int len;
758
759 #ifdef MAC
760 error = mac_check_ifnet_transmit(ifp, m0);
761 if (error)
762 goto bad;
763 #endif
764
765 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
766 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
767 error = ENETDOWN; /* sort of */
768 goto bad;
769 }
770
771 /*
772 * Compute PPP header.
773 */
774 m0->m_flags &= ~M_HIGHPRI;
775 switch (dst->sa_family) {
776 #ifdef INET
777 case AF_INET:
778 address = PPP_ALLSTATIONS;
779 control = PPP_UI;
780 protocol = PPP_IP;
781 mode = sc->sc_npmode[NP_IP];
782
783 /*
784 * If this packet has the "low delay" bit set in the IP header,
785 * put it on the fastq instead.
786 */
787 ip = mtod(m0, struct ip *);
788 if (ip->ip_tos & IPTOS_LOWDELAY)
789 m0->m_flags |= M_HIGHPRI;
790 break;
791 #endif
792 #ifdef IPX
793 case AF_IPX:
794 /*
795 * This is pretty bogus.. We dont have an ipxcp module in pppd
796 * yet to configure the link parameters. Sigh. I guess a
797 * manual ifconfig would do.... -Peter
798 */
799 address = PPP_ALLSTATIONS;
800 control = PPP_UI;
801 protocol = PPP_IPX;
802 mode = NPMODE_PASS;
803 break;
804 #endif
805 case AF_UNSPEC:
806 address = PPP_ADDRESS(dst->sa_data);
807 control = PPP_CONTROL(dst->sa_data);
808 protocol = PPP_PROTOCOL(dst->sa_data);
809 mode = NPMODE_PASS;
810 break;
811 default:
812 if_printf(ifp, "af%d not supported\n", dst->sa_family);
813 error = EAFNOSUPPORT;
814 goto bad;
815 }
816
817 /*
818 * Drop this packet, or return an error, if necessary.
819 */
820 if (mode == NPMODE_ERROR) {
821 error = ENETDOWN;
822 goto bad;
823 }
824 if (mode == NPMODE_DROP) {
825 error = 0;
826 goto bad;
827 }
828
829 /*
830 * Add PPP header. If no space in first mbuf, allocate another.
831 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
832 */
833 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
834 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
835 if (m0 == 0) {
836 error = ENOBUFS;
837 goto bad;
838 }
839 m0->m_len = 0;
840 } else
841 m0->m_data -= PPP_HDRLEN;
842
843 cp = mtod(m0, u_char *);
844 *cp++ = address;
845 *cp++ = control;
846 *cp++ = protocol >> 8;
847 *cp++ = protocol & 0xff;
848 m0->m_len += PPP_HDRLEN;
849
850 len = m_length(m0, NULL);
851
852 if (sc->sc_flags & SC_LOG_OUTPKT) {
853 printf("ppp%d output: ", ifp->if_unit);
854 pppdumpm(m0);
855 }
856
857 if ((protocol & 0x8000) == 0) {
858 #ifdef PPP_FILTER
859 /*
860 * Apply the pass and active filters to the packet,
861 * but only if it is a data packet.
862 */
863 *mtod(m0, u_char *) = 1; /* indicates outbound */
864 if (sc->sc_pass_filt.bf_insns != 0
865 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
866 len, 0) == 0) {
867 error = 0; /* drop this packet */
868 goto bad;
869 }
870
871 /*
872 * Update the time we sent the most recent packet.
873 */
874 if (sc->sc_active_filt.bf_insns == 0
875 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
876 sc->sc_last_sent = time_second;
877
878 *mtod(m0, u_char *) = address;
879 #else
880 /*
881 * Update the time we sent the most recent data packet.
882 */
883 sc->sc_last_sent = time_second;
884 #endif /* PPP_FILTER */
885 }
886
887 /*
888 * See if bpf wants to look at the packet.
889 */
890 BPF_MTAP(ifp, m0);
891
892 /*
893 * Put the packet on the appropriate queue.
894 */
895 s = splsoftnet(); /* redundant */
896 if (mode == NPMODE_QUEUE) {
897 /* XXX we should limit the number of packets on this queue */
898 *sc->sc_npqtail = m0;
899 m0->m_nextpkt = NULL;
900 sc->sc_npqtail = &m0->m_nextpkt;
901 } else {
902 /* fastq and if_snd are emptied at spl[soft]net now */
903 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
904 IF_LOCK(ifq);
905 if (_IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
906 _IF_DROP(ifq);
907 IF_UNLOCK(ifq);
908 sc->sc_if.if_oerrors++;
909 sc->sc_stats.ppp_oerrors++;
910 error = ENOBUFS;
911 goto bad;
912 }
913 _IF_ENQUEUE(ifq, m0);
914 IF_UNLOCK(ifq);
915 (*sc->sc_start)(sc);
916 }
917 getmicrotime(&ifp->if_lastchange);
918 ifp->if_opackets++;
919 ifp->if_obytes += len;
920
921 splx(s);
922 return (0);
923
924 bad:
925 m_freem(m0);
926 return (error);
927 }
928
929 /*
930 * After a change in the NPmode for some NP, move packets from the
931 * npqueue to the send queue or the fast queue as appropriate.
932 * Should be called at spl[soft]net.
933 */
934 static void
935 ppp_requeue(sc)
936 struct ppp_softc *sc;
937 {
938 struct mbuf *m, **mpp;
939 struct ifqueue *ifq;
940 enum NPmode mode;
941
942 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
943 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
944 case PPP_IP:
945 mode = sc->sc_npmode[NP_IP];
946 break;
947 default:
948 mode = NPMODE_PASS;
949 }
950
951 switch (mode) {
952 case NPMODE_PASS:
953 /*
954 * This packet can now go on one of the queues to be sent.
955 */
956 *mpp = m->m_nextpkt;
957 m->m_nextpkt = NULL;
958 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
959 if (! IF_HANDOFF(ifq, m, NULL)) {
960 sc->sc_if.if_oerrors++;
961 sc->sc_stats.ppp_oerrors++;
962 }
963 break;
964
965 case NPMODE_DROP:
966 case NPMODE_ERROR:
967 *mpp = m->m_nextpkt;
968 m_freem(m);
969 break;
970
971 case NPMODE_QUEUE:
972 mpp = &m->m_nextpkt;
973 break;
974 }
975 }
976 sc->sc_npqtail = mpp;
977 }
978
979 /*
980 * Transmitter has finished outputting some stuff;
981 * remember to call sc->sc_start later at splsoftnet.
982 */
983 void
984 ppp_restart(sc)
985 struct ppp_softc *sc;
986 {
987 int s = splimp();
988
989 sc->sc_flags &= ~SC_TBUSY;
990 schednetisr(NETISR_PPP);
991 splx(s);
992 }
993
994
995 /*
996 * Get a packet to send. This procedure is intended to be called at
997 * splsoftnet, since it may involve time-consuming operations such as
998 * applying VJ compression, packet compression, address/control and/or
999 * protocol field compression to the packet.
1000 */
1001 struct mbuf *
1002 ppp_dequeue(sc)
1003 struct ppp_softc *sc;
1004 {
1005 struct mbuf *m, *mp;
1006 u_char *cp;
1007 int address, control, protocol;
1008
1009 /*
1010 * Grab a packet to send: first try the fast queue, then the
1011 * normal queue.
1012 */
1013 IF_DEQUEUE(&sc->sc_fastq, m);
1014 if (m == NULL)
1015 IF_DEQUEUE(&sc->sc_if.if_snd, m);
1016 if (m == NULL)
1017 return NULL;
1018
1019 ++sc->sc_stats.ppp_opackets;
1020
1021 /*
1022 * Extract the ppp header of the new packet.
1023 * The ppp header will be in one mbuf.
1024 */
1025 cp = mtod(m, u_char *);
1026 address = PPP_ADDRESS(cp);
1027 control = PPP_CONTROL(cp);
1028 protocol = PPP_PROTOCOL(cp);
1029
1030 switch (protocol) {
1031 case PPP_IP:
1032 #ifdef VJC
1033 /*
1034 * If the packet is a TCP/IP packet, see if we can compress it.
1035 */
1036 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
1037 struct ip *ip;
1038 int type;
1039
1040 mp = m;
1041 ip = (struct ip *) (cp + PPP_HDRLEN);
1042 if (mp->m_len <= PPP_HDRLEN) {
1043 mp = mp->m_next;
1044 if (mp == NULL)
1045 break;
1046 ip = mtod(mp, struct ip *);
1047 }
1048 /* this code assumes the IP/TCP header is in one non-shared mbuf */
1049 if (ip->ip_p == IPPROTO_TCP) {
1050 type = sl_compress_tcp(mp, ip, sc->sc_comp,
1051 !(sc->sc_flags & SC_NO_TCP_CCID));
1052 switch (type) {
1053 case TYPE_UNCOMPRESSED_TCP:
1054 protocol = PPP_VJC_UNCOMP;
1055 break;
1056 case TYPE_COMPRESSED_TCP:
1057 protocol = PPP_VJC_COMP;
1058 cp = mtod(m, u_char *);
1059 cp[0] = address; /* header has moved */
1060 cp[1] = control;
1061 cp[2] = 0;
1062 break;
1063 }
1064 cp[3] = protocol; /* update protocol in PPP header */
1065 }
1066 }
1067 #endif /* VJC */
1068 break;
1069
1070 #ifdef PPP_COMPRESS
1071 case PPP_CCP:
1072 ppp_ccp(sc, m, 0);
1073 break;
1074 #endif /* PPP_COMPRESS */
1075 }
1076
1077 #ifdef PPP_COMPRESS
1078 if (protocol != PPP_LCP && protocol != PPP_CCP
1079 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1080 struct mbuf *mcomp = NULL;
1081 int slen, clen;
1082
1083 slen = m_length(m, NULL);
1084 clen = (*sc->sc_xcomp->compress)
1085 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
1086 if (mcomp != NULL) {
1087 if (sc->sc_flags & SC_CCP_UP) {
1088 /* Send the compressed packet instead of the original. */
1089 m_freem(m);
1090 m = mcomp;
1091 cp = mtod(m, u_char *);
1092 protocol = cp[3];
1093 } else {
1094 /* Can't transmit compressed packets until CCP is up. */
1095 m_freem(mcomp);
1096 }
1097 }
1098 }
1099 #endif /* PPP_COMPRESS */
1100
1101 /*
1102 * Compress the address/control and protocol, if possible.
1103 */
1104 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1105 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1106 protocol != PPP_LCP) {
1107 /* can compress address/control */
1108 m->m_data += 2;
1109 m->m_len -= 2;
1110 }
1111 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1112 /* can compress protocol */
1113 if (mtod(m, u_char *) == cp) {
1114 cp[2] = cp[1]; /* move address/control up */
1115 cp[1] = cp[0];
1116 }
1117 ++m->m_data;
1118 --m->m_len;
1119 }
1120
1121 return m;
1122 }
1123
1124 /*
1125 * Software interrupt routine, called at spl[soft]net.
1126 */
1127 static void
1128 pppintr()
1129 {
1130 struct ppp_softc *sc;
1131 int s;
1132 struct mbuf *m;
1133
1134 LIST_FOREACH(sc, &ppp_softc_list, sc_list) {
1135 s = splimp();
1136 if (!(sc->sc_flags & SC_TBUSY)
1137 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
1138 sc->sc_flags |= SC_TBUSY;
1139 splx(s);
1140 (*sc->sc_start)(sc);
1141 } else
1142 splx(s);
1143 for (;;) {
1144 s = splimp();
1145 IF_DEQUEUE(&sc->sc_rawq, m);
1146 splx(s);
1147 if (m == NULL)
1148 break;
1149 #ifdef MAC
1150 mac_create_mbuf_from_ifnet(&sc->sc_if, m);
1151 #endif
1152 ppp_inproc(sc, m);
1153 }
1154 }
1155 }
1156
1157 #ifdef PPP_COMPRESS
1158 /*
1159 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1160 * 0 if it is about to be transmitted.
1161 */
1162 static void
1163 ppp_ccp(sc, m, rcvd)
1164 struct ppp_softc *sc;
1165 struct mbuf *m;
1166 int rcvd;
1167 {
1168 u_char *dp, *ep;
1169 struct mbuf *mp;
1170 int slen, s;
1171
1172 /*
1173 * Get a pointer to the data after the PPP header.
1174 */
1175 if (m->m_len <= PPP_HDRLEN) {
1176 mp = m->m_next;
1177 if (mp == NULL)
1178 return;
1179 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1180 } else {
1181 mp = m;
1182 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1183 }
1184
1185 ep = mtod(mp, u_char *) + mp->m_len;
1186 if (dp + CCP_HDRLEN > ep)
1187 return;
1188 slen = CCP_LENGTH(dp);
1189 if (dp + slen > ep) {
1190 if (sc->sc_flags & SC_DEBUG)
1191 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1192 dp, slen, mtod(mp, u_char *), mp->m_len);
1193 return;
1194 }
1195
1196 switch (CCP_CODE(dp)) {
1197 case CCP_CONFREQ:
1198 case CCP_TERMREQ:
1199 case CCP_TERMACK:
1200 /* CCP must be going down - disable compression */
1201 if (sc->sc_flags & SC_CCP_UP) {
1202 s = splimp();
1203 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1204 splx(s);
1205 }
1206 break;
1207
1208 case CCP_CONFACK:
1209 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1210 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1211 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1212 if (!rcvd) {
1213 /* we're agreeing to send compressed packets. */
1214 if (sc->sc_xc_state != NULL
1215 && (*sc->sc_xcomp->comp_init)
1216 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1217 sc->sc_if.if_unit, 0, sc->sc_flags & SC_DEBUG)) {
1218 s = splimp();
1219 sc->sc_flags |= SC_COMP_RUN;
1220 splx(s);
1221 }
1222 } else {
1223 /* peer is agreeing to send compressed packets. */
1224 if (sc->sc_rc_state != NULL
1225 && (*sc->sc_rcomp->decomp_init)
1226 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1227 sc->sc_if.if_unit, 0, sc->sc_mru,
1228 sc->sc_flags & SC_DEBUG)) {
1229 s = splimp();
1230 sc->sc_flags |= SC_DECOMP_RUN;
1231 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1232 splx(s);
1233 }
1234 }
1235 }
1236 break;
1237
1238 case CCP_RESETACK:
1239 if (sc->sc_flags & SC_CCP_UP) {
1240 if (!rcvd) {
1241 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1242 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1243 } else {
1244 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1245 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1246 s = splimp();
1247 sc->sc_flags &= ~SC_DC_ERROR;
1248 splx(s);
1249 }
1250 }
1251 }
1252 break;
1253 }
1254 }
1255
1256 /*
1257 * CCP is down; free (de)compressor state if necessary.
1258 */
1259 static void
1260 ppp_ccp_closed(sc)
1261 struct ppp_softc *sc;
1262 {
1263 if (sc->sc_xc_state) {
1264 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1265 sc->sc_xc_state = NULL;
1266 }
1267 if (sc->sc_rc_state) {
1268 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1269 sc->sc_rc_state = NULL;
1270 }
1271 }
1272 #endif /* PPP_COMPRESS */
1273
1274 /*
1275 * PPP packet input routine.
1276 * The caller has checked and removed the FCS and has inserted
1277 * the address/control bytes and the protocol high byte if they
1278 * were omitted.
1279 */
1280 void
1281 ppppktin(sc, m, lost)
1282 struct ppp_softc *sc;
1283 struct mbuf *m;
1284 int lost;
1285 {
1286 int s = splimp();
1287
1288 if (lost)
1289 m->m_flags |= M_ERRMARK;
1290 IF_ENQUEUE(&sc->sc_rawq, m);
1291 schednetisr(NETISR_PPP);
1292 splx(s);
1293 }
1294
1295 /*
1296 * Process a received PPP packet, doing decompression as necessary.
1297 * Should be called at splsoftnet.
1298 */
1299 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1300 TYPE_UNCOMPRESSED_TCP)
1301
1302 static void
1303 ppp_inproc(sc, m)
1304 struct ppp_softc *sc;
1305 struct mbuf *m;
1306 {
1307 struct ifnet *ifp = &sc->sc_if;
1308 int isr;
1309 int s, ilen = 0, xlen, proto, rv;
1310 u_char *cp, adrs, ctrl;
1311 struct mbuf *mp, *dmp = NULL;
1312 u_char *iphdr;
1313 u_int hlen;
1314
1315 sc->sc_stats.ppp_ipackets++;
1316
1317 if (sc->sc_flags & SC_LOG_INPKT) {
1318 ilen = m_length(m, NULL);
1319 if_printf(ifp, "got %d bytes\n", ilen);
1320 pppdumpm(m);
1321 }
1322
1323 cp = mtod(m, u_char *);
1324 adrs = PPP_ADDRESS(cp);
1325 ctrl = PPP_CONTROL(cp);
1326 proto = PPP_PROTOCOL(cp);
1327
1328 if (m->m_flags & M_ERRMARK) {
1329 m->m_flags &= ~M_ERRMARK;
1330 s = splimp();
1331 sc->sc_flags |= SC_VJ_RESET;
1332 splx(s);
1333 }
1334
1335 #ifdef PPP_COMPRESS
1336 /*
1337 * Decompress this packet if necessary, update the receiver's
1338 * dictionary, or take appropriate action on a CCP packet.
1339 */
1340 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1341 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1342 /* decompress this packet */
1343 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1344 if (rv == DECOMP_OK) {
1345 m_freem(m);
1346 if (dmp == NULL) {
1347 /* no error, but no decompressed packet produced */
1348 return;
1349 }
1350 m = dmp;
1351 cp = mtod(m, u_char *);
1352 proto = PPP_PROTOCOL(cp);
1353
1354 } else {
1355 /*
1356 * An error has occurred in decompression.
1357 * Pass the compressed packet up to pppd, which may take
1358 * CCP down or issue a Reset-Req.
1359 */
1360 if (sc->sc_flags & SC_DEBUG)
1361 if_printf(ifp, "decompress failed %d\n", rv);
1362 s = splimp();
1363 sc->sc_flags |= SC_VJ_RESET;
1364 if (rv == DECOMP_ERROR)
1365 sc->sc_flags |= SC_DC_ERROR;
1366 else
1367 sc->sc_flags |= SC_DC_FERROR;
1368 splx(s);
1369 }
1370
1371 } else {
1372 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1373 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1374 }
1375 if (proto == PPP_CCP) {
1376 ppp_ccp(sc, m, 1);
1377 }
1378 }
1379 #endif
1380
1381 ilen = m_length(m, NULL);
1382
1383 #ifdef VJC
1384 if (sc->sc_flags & SC_VJ_RESET) {
1385 /*
1386 * If we've missed a packet, we must toss subsequent compressed
1387 * packets which don't have an explicit connection ID.
1388 */
1389 if (sc->sc_comp)
1390 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1391 s = splimp();
1392 sc->sc_flags &= ~SC_VJ_RESET;
1393 splx(s);
1394 }
1395
1396 /*
1397 * See if we have a VJ-compressed packet to uncompress.
1398 */
1399 if (proto == PPP_VJC_COMP) {
1400 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1401 goto bad;
1402
1403 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1404 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1405 sc->sc_comp, &iphdr, &hlen);
1406
1407 if (xlen <= 0) {
1408 if (sc->sc_flags & SC_DEBUG)
1409 if_printf(ifp, "VJ uncompress failed on type comp\n");
1410 goto bad;
1411 }
1412
1413 /* Copy the PPP and IP headers into a new mbuf. */
1414 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1415 if (mp == NULL)
1416 goto bad;
1417 mp->m_len = 0;
1418 mp->m_next = NULL;
1419 if (hlen + PPP_HDRLEN > MHLEN) {
1420 MCLGET(mp, M_DONTWAIT);
1421 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1422 m_freem(mp);
1423 goto bad; /* lose if big headers and no clusters */
1424 }
1425 }
1426 #ifdef MAC
1427 mac_create_mbuf_from_mbuf(m, mp);
1428 #endif
1429 cp = mtod(mp, u_char *);
1430 cp[0] = adrs;
1431 cp[1] = ctrl;
1432 cp[2] = 0;
1433 cp[3] = PPP_IP;
1434 proto = PPP_IP;
1435 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1436 mp->m_len = hlen + PPP_HDRLEN;
1437
1438 /*
1439 * Trim the PPP and VJ headers off the old mbuf
1440 * and stick the new and old mbufs together.
1441 */
1442 m->m_data += PPP_HDRLEN + xlen;
1443 m->m_len -= PPP_HDRLEN + xlen;
1444 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1445 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1446 mp->m_len += m->m_len;
1447 mp->m_next = m_free(m);
1448 } else {
1449 mp->m_next = m;
1450 }
1451 m = mp;
1452 ilen += hlen - xlen;
1453
1454 } else if (proto == PPP_VJC_UNCOMP) {
1455 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1456 goto bad;
1457
1458 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1459 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1460 sc->sc_comp, &iphdr, &hlen);
1461
1462 if (xlen < 0) {
1463 if (sc->sc_flags & SC_DEBUG)
1464 if_printf(ifp, "VJ uncompress failed on type uncomp\n");
1465 goto bad;
1466 }
1467
1468 proto = PPP_IP;
1469 cp[3] = PPP_IP;
1470 }
1471 #endif /* VJC */
1472
1473 /*
1474 * If the packet will fit in a header mbuf, don't waste a
1475 * whole cluster on it.
1476 */
1477 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1478 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1479 if (mp != NULL) {
1480 #ifdef MAC
1481 mac_create_mbuf_from_mbuf(m, mp);
1482 #endif
1483 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1484 m_freem(m);
1485 m = mp;
1486 m->m_len = ilen;
1487 }
1488 }
1489 m->m_pkthdr.len = ilen;
1490 m->m_pkthdr.rcvif = ifp;
1491
1492 if ((proto & 0x8000) == 0) {
1493 #ifdef PPP_FILTER
1494 /*
1495 * See whether we want to pass this packet, and
1496 * if it counts as link activity.
1497 */
1498 adrs = *mtod(m, u_char *); /* save address field */
1499 *mtod(m, u_char *) = 0; /* indicate inbound */
1500 if (sc->sc_pass_filt.bf_insns != 0
1501 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1502 ilen, 0) == 0) {
1503 /* drop this packet */
1504 m_freem(m);
1505 return;
1506 }
1507 if (sc->sc_active_filt.bf_insns == 0
1508 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1509 sc->sc_last_recv = time_second;
1510
1511 *mtod(m, u_char *) = adrs;
1512 #else
1513 /*
1514 * Record the time that we received this packet.
1515 */
1516 sc->sc_last_recv = time_second;
1517 #endif /* PPP_FILTER */
1518 }
1519
1520 /* See if bpf wants to look at the packet. */
1521 BPF_MTAP(&sc->sc_if, m);
1522
1523 isr = -1;
1524 switch (proto) {
1525 #ifdef INET
1526 case PPP_IP:
1527 /*
1528 * IP packet - take off the ppp header and pass it up to IP.
1529 */
1530 if ((ifp->if_flags & IFF_UP) == 0
1531 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1532 /* interface is down - drop the packet. */
1533 m_freem(m);
1534 return;
1535 }
1536 m->m_pkthdr.len -= PPP_HDRLEN;
1537 m->m_data += PPP_HDRLEN;
1538 m->m_len -= PPP_HDRLEN;
1539 if (ipflow_fastforward(m))
1540 return;
1541 isr = NETISR_IP;
1542 break;
1543 #endif
1544 #ifdef IPX
1545 case PPP_IPX:
1546 /*
1547 * IPX packet - take off the ppp header and pass it up to IPX.
1548 */
1549 if ((sc->sc_if.if_flags & IFF_UP) == 0
1550 /* XXX: || sc->sc_npmode[NP_IPX] != NPMODE_PASS*/) {
1551 /* interface is down - drop the packet. */
1552 m_freem(m);
1553 return;
1554 }
1555 m->m_pkthdr.len -= PPP_HDRLEN;
1556 m->m_data += PPP_HDRLEN;
1557 m->m_len -= PPP_HDRLEN;
1558 isr = NETISR_IPX;
1559 sc->sc_last_recv = time_second; /* update time of last pkt rcvd */
1560 break;
1561 #endif
1562
1563 default:
1564 /*
1565 * Some other protocol - place on input queue for read().
1566 */
1567 break;
1568 }
1569
1570 if (isr == -1)
1571 rv = IF_HANDOFF(&sc->sc_inq, m, NULL);
1572 else
1573 rv = netisr_queue(isr, m);
1574 if (!rv) {
1575 if (sc->sc_flags & SC_DEBUG)
1576 if_printf(ifp, "input queue full\n");
1577 ifp->if_iqdrops++;
1578 goto bad;
1579 }
1580 ifp->if_ipackets++;
1581 ifp->if_ibytes += ilen;
1582 getmicrotime(&ifp->if_lastchange);
1583
1584 if (isr == -1)
1585 (*sc->sc_ctlp)(sc);
1586
1587 return;
1588
1589 bad:
1590 if (m)
1591 m_freem(m);
1592 sc->sc_if.if_ierrors++;
1593 sc->sc_stats.ppp_ierrors++;
1594 }
1595
1596 #define MAX_DUMP_BYTES 128
1597
1598 static void
1599 pppdumpm(m0)
1600 struct mbuf *m0;
1601 {
1602 char buf[3*MAX_DUMP_BYTES+4];
1603 char *bp = buf;
1604 struct mbuf *m;
1605
1606 for (m = m0; m; m = m->m_next) {
1607 int l = m->m_len;
1608 u_char *rptr = (u_char *)m->m_data;
1609
1610 while (l--) {
1611 if (bp > buf + (sizeof(buf) - 4))
1612 goto done;
1613 *bp++ = hex2ascii(*rptr >> 4);
1614 *bp++ = hex2ascii(*rptr++ & 0xf);
1615 }
1616
1617 if (m->m_next) {
1618 if (bp > buf + (sizeof(buf) - 3))
1619 goto done;
1620 *bp++ = '|';
1621 } else
1622 *bp++ = ' ';
1623 }
1624 done:
1625 if (m)
1626 *bp++ = '>';
1627 *bp = 0;
1628 printf("%s\n", buf);
1629 }
Cache object: 4e7e2de539a00e8431c181bca0b05b6b
|