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