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