if_ppp.c revision 1.24 1 /* $NetBSD: if_ppp.c,v 1.24 1995/10/05 05:55:09 mycroft Exp $ */
2
3 /*
4 * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
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 (at) 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 (at) seismo.ARPA
54 * seismo!rick
55 *
56 * Pounded on heavily by Chris Torek (chris (at) 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 (at) cayman.com)
61 * Added VJ tcp header compression; more unified ioctls
62 *
63 * Extensively modified by Paul Mackerras (paulus (at) 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 (at) cs.anu.edu.au).
72 */
73
74 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
75 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
76
77 #include "ppp.h"
78 #if NPPP > 0
79
80 #define VJC
81 #define PPP_COMPRESS
82
83 #include <sys/param.h>
84 #include <sys/proc.h>
85 #include <sys/mbuf.h>
86 #include <sys/socket.h>
87 #include <sys/ioctl.h>
88 #include <sys/kernel.h>
89
90 #ifdef i386
91 #include <machine/psl.h>
92 #endif
93
94 #include <net/if.h>
95 #include <net/if_types.h>
96 #include <net/netisr.h>
97 #include <net/route.h>
98
99 #if INET
100 #include <netinet/in.h>
101 #include <netinet/in_systm.h>
102 #include <netinet/in_var.h>
103 #include <netinet/ip.h>
104 #endif
105
106 #include "bpfilter.h"
107 #if NBPFILTER > 0
108 #include <sys/time.h>
109 #include <net/bpf.h>
110 #endif
111
112 #ifdef VJC
113 #include <net/slcompress.h>
114 #endif
115
116 #include <net/ppp_defs.h>
117 #include <net/if_ppp.h>
118 #include <net/if_pppvar.h>
119 #include <machine/cpu.h>
120
121 #ifdef PPP_COMPRESS
122 #define PACKETPTR struct mbuf *
123 #include <net/ppp-comp.h>
124 #endif
125
126 void pppattach __P((void));
127 int pppioctl __P((struct ppp_softc *sc, u_long cmd, caddr_t data, int flag,
128 struct proc *));
129 int pppoutput __P((struct ifnet *ifp, struct mbuf *m0,
130 struct sockaddr *dst, struct rtentry *rtp));
131 int pppsioctl __P((struct ifnet *ifp, u_long cmd, caddr_t data));
132 void pppintr __P((void));
133
134 static void ppp_requeue __P((struct ppp_softc *));
135 static void ppp_outpkt __P((struct ppp_softc *));
136 static int ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd));
137 static void ppp_ccp_closed __P((struct ppp_softc *));
138 static void ppp_inproc __P((struct ppp_softc *, struct mbuf *));
139 static void pppdumpm __P((struct mbuf *m0));
140
141 /*
142 * Some useful mbuf macros not in mbuf.h.
143 */
144 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
145
146 #define M_DATASTART(m) \
147 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
148 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
149
150 #define M_DATASIZE(m) \
151 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
152 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
153
154 /*
155 * We steal two bits in the mbuf m_flags, to mark high-priority packets
156 * for output, and received packets following lost/corrupted packets.
157 */
158 #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */
159 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
160
161
162 #ifdef PPP_COMPRESS
163 /*
164 * List of compressors we know about.
165 * We leave some space so maybe we can modload compressors.
166 */
167
168 extern struct compressor ppp_bsd_compress;
169
170 struct compressor *ppp_compressors[8] = {
171 #if DO_BSD_COMPRESS
172 &ppp_bsd_compress,
173 #endif
174 NULL
175 };
176 #endif /* PPP_COMPRESS */
177
178 /*
179 * Called from boot code to establish ppp interfaces.
180 */
181 void
182 pppattach()
183 {
184 register struct ppp_softc *sc;
185 register int i = 0;
186
187 for (sc = ppp_softc; i < NPPP; sc++) {
188 sc->sc_if.if_name = "ppp";
189 sc->sc_if.if_unit = i++;
190 sc->sc_if.if_mtu = PPP_MTU;
191 sc->sc_if.if_flags = IFF_POINTOPOINT;
192 sc->sc_if.if_type = IFT_PPP;
193 sc->sc_if.if_hdrlen = PPP_HDRLEN;
194 sc->sc_if.if_ioctl = pppsioctl;
195 sc->sc_if.if_output = pppoutput;
196 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
197 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
198 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
199 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
200 if_attach(&sc->sc_if);
201 #if NBPFILTER > 0
202 bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_PPP, PPP_HDRLEN);
203 #endif
204 }
205
206 #ifdef i386
207 /*
208 * XXX kludge to fix the bug in the i386 interrupt handling code,
209 * where software interrupts could be taken while hardware
210 * interrupts were blocked.
211 */
212 if ((imask[IPL_TTY] & SIR_NETMASK) == 0) {
213 imask[IPL_TTY] |= SIR_NETMASK;
214 intr_calculatemasks();
215 }
216 #endif
217 }
218
219 /*
220 * Allocate a ppp interface unit and initialize it.
221 */
222 struct ppp_softc *
223 pppalloc(pid)
224 pid_t pid;
225 {
226 int nppp, i;
227 struct ppp_softc *sc;
228
229 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
230 if (sc->sc_xfer == pid) {
231 sc->sc_xfer = 0;
232 return sc;
233 }
234 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
235 if (sc->sc_devp == NULL)
236 break;
237 if (nppp >= NPPP)
238 return NULL;
239
240 sc->sc_flags = 0;
241 sc->sc_mru = PPP_MRU;
242 sc->sc_relinq = NULL;
243 #ifdef VJC
244 sl_compress_init(&sc->sc_comp, -1);
245 #endif
246 #ifdef PPP_COMPRESS
247 sc->sc_xc_state = NULL;
248 sc->sc_rc_state = NULL;
249 #endif /* PPP_COMPRESS */
250 for (i = 0; i < NUM_NP; ++i)
251 sc->sc_npmode[i] = NPMODE_ERROR;
252 sc->sc_npqueue = NULL;
253 sc->sc_npqtail = &sc->sc_npqueue;
254 sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;
255
256 return sc;
257 }
258
259 /*
260 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
261 */
262 void
263 pppdealloc(sc)
264 struct ppp_softc *sc;
265 {
266 struct mbuf *m;
267
268 if_down(&sc->sc_if);
269 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
270 sc->sc_devp = NULL;
271 sc->sc_xfer = 0;
272 for (;;) {
273 IF_DEQUEUE(&sc->sc_rawq, m);
274 if (m == NULL)
275 break;
276 m_freem(m);
277 }
278 for (;;) {
279 IF_DEQUEUE(&sc->sc_inq, m);
280 if (m == NULL)
281 break;
282 m_freem(m);
283 }
284 for (;;) {
285 IF_DEQUEUE(&sc->sc_fastq, m);
286 if (m == NULL)
287 break;
288 m_freem(m);
289 }
290 while ((m = sc->sc_npqueue) != NULL) {
291 sc->sc_npqueue = m->m_nextpkt;
292 m_freem(m);
293 }
294 if (sc->sc_togo != NULL) {
295 m_freem(sc->sc_togo);
296 sc->sc_togo = NULL;
297 }
298 #ifdef PPP_COMPRESS
299 ppp_ccp_closed(sc);
300 sc->sc_xc_state = NULL;
301 sc->sc_rc_state = NULL;
302 #endif /* PPP_COMPRESS */
303 }
304
305 /*
306 * Ioctl routine for generic ppp devices.
307 */
308 int
309 pppioctl(sc, cmd, data, flag, p)
310 struct ppp_softc *sc;
311 u_long cmd;
312 caddr_t data;
313 int flag;
314 struct proc *p;
315 {
316 int s, error, flags, mru, nb, npx;
317 struct ppp_option_data *odp;
318 struct compressor **cp;
319 struct npioctl *npi;
320 time_t t;
321 #ifdef PPP_COMPRESS
322 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
323 #endif
324
325 switch (cmd) {
326 case FIONREAD:
327 *(int *)data = sc->sc_inq.ifq_len;
328 break;
329
330 case PPPIOCGUNIT:
331 *(int *)data = sc->sc_if.if_unit;
332 break;
333
334 case PPPIOCGFLAGS:
335 *(u_int *)data = sc->sc_flags;
336 break;
337
338 case PPPIOCSFLAGS:
339 if (error = suser(p->p_ucred, &p->p_acflag))
340 return (error);
341 flags = *(int *)data & SC_MASK;
342 s = splsoftnet();
343 #ifdef PPP_COMPRESS
344 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
345 ppp_ccp_closed(sc);
346 #endif
347 splhigh();
348 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
349 splx(s);
350 break;
351
352 case PPPIOCSMRU:
353 if (error = suser(p->p_ucred, &p->p_acflag))
354 return (error);
355 mru = *(int *)data;
356 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
357 sc->sc_mru = mru;
358 break;
359
360 case PPPIOCGMRU:
361 *(int *)data = sc->sc_mru;
362 break;
363
364 #ifdef VJC
365 case PPPIOCSMAXCID:
366 if (error = suser(p->p_ucred, &p->p_acflag))
367 return (error);
368 s = splsoftnet();
369 sl_compress_init(&sc->sc_comp, *(int *)data);
370 splx(s);
371 break;
372 #endif
373
374 case PPPIOCXFERUNIT:
375 if (error = suser(p->p_ucred, &p->p_acflag))
376 return (error);
377 sc->sc_xfer = p->p_pid;
378 break;
379
380 #ifdef PPP_COMPRESS
381 case PPPIOCSCOMPRESS:
382 if (error = suser(p->p_ucred, &p->p_acflag))
383 return (error);
384 odp = (struct ppp_option_data *) data;
385 nb = odp->length;
386 if (nb > sizeof(ccp_option))
387 nb = sizeof(ccp_option);
388 if (error = copyin(odp->ptr, ccp_option, nb))
389 return (error);
390 if (ccp_option[1] < 2) /* preliminary check on the length byte */
391 return (EINVAL);
392 for (cp = ppp_compressors; *cp != NULL; ++cp)
393 if ((*cp)->compress_proto == ccp_option[0]) {
394 /*
395 * Found a handler for the protocol - try to allocate
396 * a compressor or decompressor.
397 */
398 error = 0;
399 if (odp->transmit) {
400 s = splsoftnet();
401 if (sc->sc_xc_state != NULL)
402 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
403 sc->sc_xcomp = *cp;
404 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
405 if (sc->sc_xc_state == NULL) {
406 if (sc->sc_flags & SC_DEBUG)
407 printf("ppp%d: comp_alloc failed\n",
408 sc->sc_if.if_unit);
409 error = ENOBUFS;
410 }
411 splhigh();
412 sc->sc_flags &= ~SC_COMP_RUN;
413 splx(s);
414 } else {
415 s = splsoftnet();
416 if (sc->sc_rc_state != NULL)
417 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
418 sc->sc_rcomp = *cp;
419 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
420 if (sc->sc_rc_state == NULL) {
421 if (sc->sc_flags & SC_DEBUG)
422 printf("ppp%d: decomp_alloc failed\n",
423 sc->sc_if.if_unit);
424 error = ENOBUFS;
425 }
426 splhigh();
427 sc->sc_flags &= ~SC_DECOMP_RUN;
428 splx(s);
429 }
430 return (error);
431 }
432 if (sc->sc_flags & SC_DEBUG)
433 printf("ppp%d: no compressor for [%x %x %x], %x\n",
434 sc->sc_if.if_unit, ccp_option[0], ccp_option[1],
435 ccp_option[2], nb);
436 return (EINVAL); /* no handler found */
437 #endif /* PPP_COMPRESS */
438
439 case PPPIOCGNPMODE:
440 case PPPIOCSNPMODE:
441 npi = (struct npioctl *) data;
442 switch (npi->protocol) {
443 case PPP_IP:
444 npx = NP_IP;
445 break;
446 default:
447 return EINVAL;
448 }
449 if (cmd == PPPIOCGNPMODE) {
450 npi->mode = sc->sc_npmode[npx];
451 } else {
452 if (error = suser(p->p_ucred, &p->p_acflag))
453 return (error);
454 if (npi->mode != sc->sc_npmode[npx]) {
455 s = splsoftnet();
456 sc->sc_npmode[npx] = npi->mode;
457 if (npi->mode != NPMODE_QUEUE) {
458 ppp_requeue(sc);
459 (*sc->sc_start)(sc);
460 }
461 splx(s);
462 }
463 }
464 break;
465
466 case PPPIOCGIDLE:
467 s = splsoftnet();
468 t = time.tv_sec;
469 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
470 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
471 splx(s);
472 break;
473
474 default:
475 return (-1);
476 }
477 return (0);
478 }
479
480 /*
481 * Process an ioctl request to the ppp network interface.
482 */
483 int
484 pppsioctl(ifp, cmd, data)
485 register struct ifnet *ifp;
486 u_long cmd;
487 caddr_t data;
488 {
489 struct proc *p = curproc; /* XXX */
490 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
491 register struct ifaddr *ifa = (struct ifaddr *)data;
492 register struct ifreq *ifr = (struct ifreq *)data;
493 struct ppp_stats *psp;
494 #ifdef PPP_COMPRESS
495 struct ppp_comp_stats *pcp;
496 #endif
497 int s = splimp(), error = 0;
498
499 switch (cmd) {
500 case SIOCSIFFLAGS:
501 if ((ifp->if_flags & IFF_RUNNING) == 0)
502 ifp->if_flags &= ~IFF_UP;
503 break;
504
505 case SIOCSIFADDR:
506 if (ifa->ifa_addr->sa_family != AF_INET)
507 error = EAFNOSUPPORT;
508 break;
509
510 case SIOCSIFDSTADDR:
511 if (ifa->ifa_addr->sa_family != AF_INET)
512 error = EAFNOSUPPORT;
513 break;
514
515 case SIOCSIFMTU:
516 if (error = suser(p->p_ucred, &p->p_acflag))
517 break;
518 sc->sc_if.if_mtu = ifr->ifr_mtu;
519 break;
520
521 case SIOCGIFMTU:
522 ifr->ifr_mtu = sc->sc_if.if_mtu;
523 break;
524
525 case SIOCGPPPSTATS:
526 psp = &((struct ifpppstatsreq *) data)->stats;
527 bzero(psp, sizeof(*psp));
528 psp->p.ppp_ibytes = sc->sc_bytesrcvd;
529 psp->p.ppp_ipackets = ifp->if_ipackets;
530 psp->p.ppp_ierrors = ifp->if_ierrors;
531 psp->p.ppp_obytes = sc->sc_bytessent;
532 psp->p.ppp_opackets = ifp->if_opackets;
533 psp->p.ppp_oerrors = ifp->if_oerrors;
534 #ifdef VJC
535 psp->vj.vjs_packets = sc->sc_comp.sls_packets;
536 psp->vj.vjs_compressed = sc->sc_comp.sls_compressed;
537 psp->vj.vjs_searches = sc->sc_comp.sls_searches;
538 psp->vj.vjs_misses = sc->sc_comp.sls_misses;
539 psp->vj.vjs_uncompressedin = sc->sc_comp.sls_uncompressedin;
540 psp->vj.vjs_compressedin = sc->sc_comp.sls_compressedin;
541 psp->vj.vjs_errorin = sc->sc_comp.sls_errorin;
542 psp->vj.vjs_tossed = sc->sc_comp.sls_tossed;
543 #endif /* VJC */
544 break;
545
546 #ifdef PPP_COMPRESS
547 case SIOCGPPPCSTATS:
548 pcp = &((struct ifpppcstatsreq *) data)->stats;
549 bzero(pcp, sizeof(*pcp));
550 if (sc->sc_xc_state != NULL)
551 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
552 if (sc->sc_rc_state != NULL)
553 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
554 break;
555 #endif /* PPP_COMPRESS */
556
557 default:
558 error = EINVAL;
559 }
560 splx(s);
561 return (error);
562 }
563
564 /*
565 * Queue a packet. Start transmission if not active.
566 * Packet is placed in Information field of PPP frame.
567 */
568 int
569 pppoutput(ifp, m0, dst, rtp)
570 struct ifnet *ifp;
571 struct mbuf *m0;
572 struct sockaddr *dst;
573 struct rtentry *rtp;
574 {
575 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
576 struct ppp_header *ph;
577 int protocol, address, control;
578 u_char *cp;
579 int s, error;
580 struct ip *ip;
581 struct ifqueue *ifq;
582 enum NPmode mode;
583
584 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
585 || (ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC) {
586 error = ENETDOWN; /* sort of */
587 goto bad;
588 }
589
590 /*
591 * Compute PPP header.
592 */
593 m0->m_flags &= ~M_HIGHPRI;
594 switch (dst->sa_family) {
595 #ifdef INET
596 case AF_INET:
597 address = PPP_ALLSTATIONS;
598 control = PPP_UI;
599 protocol = PPP_IP;
600 mode = sc->sc_npmode[NP_IP];
601
602 /*
603 * If this packet has the "low delay" bit set in the IP header,
604 * put it on the fastq instead.
605 */
606 ip = mtod(m0, struct ip *);
607 if (ip->ip_tos & IPTOS_LOWDELAY)
608 m0->m_flags |= M_HIGHPRI;
609 break;
610 #endif
611 case AF_UNSPEC:
612 address = PPP_ADDRESS(dst->sa_data);
613 control = PPP_CONTROL(dst->sa_data);
614 protocol = PPP_PROTOCOL(dst->sa_data);
615 mode = NPMODE_PASS;
616 break;
617 default:
618 printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family);
619 error = EAFNOSUPPORT;
620 goto bad;
621 }
622
623 /*
624 * Drop this packet, or return an error, if necessary.
625 */
626 if (mode == NPMODE_ERROR) {
627 error = ENETDOWN;
628 goto bad;
629 }
630 if (mode == NPMODE_DROP) {
631 error = 0;
632 goto bad;
633 }
634
635 /*
636 * Add PPP header. If no space in first mbuf, allocate another.
637 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
638 */
639 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
640 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
641 if (m0 == 0) {
642 error = ENOBUFS;
643 goto bad;
644 }
645 m0->m_len = 0;
646 } else
647 m0->m_data -= PPP_HDRLEN;
648
649 cp = mtod(m0, u_char *);
650 *cp++ = address;
651 *cp++ = control;
652 *cp++ = protocol >> 8;
653 *cp++ = protocol & 0xff;
654 m0->m_len += PPP_HDRLEN;
655
656 if (sc->sc_flags & SC_LOG_OUTPKT) {
657 printf("ppp%d output: ", ifp->if_unit);
658 pppdumpm(m0);
659 }
660
661 #if NBPFILTER > 0
662 /*
663 * See if bpf wants to look at the packet.
664 */
665 if (sc->sc_bpf)
666 bpf_mtap(sc->sc_bpf, m0);
667 #endif
668
669 /*
670 * Put the packet on the appropriate queue.
671 */
672 s = splsoftnet();
673 if (mode == NPMODE_QUEUE) {
674 /* XXX we should limit the number of packets on this queue */
675 *sc->sc_npqtail = m0;
676 m0->m_nextpkt = NULL;
677 sc->sc_npqtail = &m0->m_nextpkt;
678 } else {
679 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
680 if (IF_QFULL(ifq)) {
681 IF_DROP(ifq);
682 splx(s);
683 sc->sc_if.if_oerrors++;
684 error = ENOBUFS;
685 goto bad;
686 }
687 IF_ENQUEUE(ifq, m0);
688 (*sc->sc_start)(sc);
689 }
690 ifp->if_lastchange = time;
691
692 splx(s);
693 return (0);
694
695 bad:
696 m_freem(m0);
697 return (error);
698 }
699
700 /*
701 * After a change in the NPmode for some NP, move packets from the
702 * npqueue to the send queue or the fast queue as appropriate.
703 * Should be called at splsoftnet.
704 */
705 static void
706 ppp_requeue(sc)
707 struct ppp_softc *sc;
708 {
709 struct mbuf *m, **mpp;
710 struct ifqueue *ifq;
711 enum NPmode mode;
712
713 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
714 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
715 case PPP_IP:
716 mode = sc->sc_npmode[NP_IP];
717 break;
718 default:
719 mode = NPMODE_PASS;
720 }
721
722 switch (mode) {
723 case NPMODE_PASS:
724 /*
725 * This packet can now go on one of the queues to be sent.
726 */
727 *mpp = m->m_nextpkt;
728 m->m_nextpkt = NULL;
729 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
730 if (IF_QFULL(ifq)) {
731 IF_DROP(ifq);
732 sc->sc_if.if_oerrors++;
733 } else
734 IF_ENQUEUE(ifq, m);
735 break;
736
737 case NPMODE_DROP:
738 case NPMODE_ERROR:
739 *mpp = m->m_nextpkt;
740 m_freem(m);
741 break;
742
743 case NPMODE_QUEUE:
744 mpp = &m->m_nextpkt;
745 break;
746 }
747 }
748 sc->sc_npqtail = mpp;
749 }
750
751 /*
752 * Get a packet to send. This procedure is intended to be called at
753 * spltty or splimp, so it takes little time. If there isn't a packet
754 * waiting to go out, it schedules a software interrupt to prepare a
755 * new packet; the device start routine gets called again when a
756 * packet is ready.
757 */
758 struct mbuf *
759 ppp_dequeue(sc)
760 struct ppp_softc *sc;
761 {
762 struct mbuf *m;
763 int s = splhigh();
764
765 m = sc->sc_togo;
766 if (m) {
767 /*
768 * Had a packet waiting - send it.
769 */
770 sc->sc_togo = NULL;
771 sc->sc_flags |= SC_TBUSY;
772 splx(s);
773 return m;
774 }
775 /*
776 * Remember we wanted a packet and schedule a software interrupt.
777 */
778 sc->sc_flags &= ~SC_TBUSY;
779 schednetisr(NETISR_PPP);
780 splx(s);
781 return NULL;
782 }
783
784 /*
785 * Software interrupt routine, called at splsoftnet.
786 */
787 void
788 pppintr()
789 {
790 struct ppp_softc *sc;
791 int i, s;
792 struct mbuf *m;
793
794 sc = ppp_softc;
795 for (i = 0; i < NPPP; ++i, ++sc) {
796 if (!(sc->sc_flags & SC_TBUSY) && sc->sc_togo == NULL
797 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head))
798 ppp_outpkt(sc);
799 for (;;) {
800 s = splhigh();
801 IF_DEQUEUE(&sc->sc_rawq, m);
802 splx(s);
803 if (m == NULL)
804 break;
805 ppp_inproc(sc, m);
806 }
807 }
808 }
809
810 /*
811 * Grab another packet off a queue and apply VJ compression,
812 * packet compression, address/control and/or protocol compression
813 * if enabled. Should be called at splsoftnet.
814 */
815 static void
816 ppp_outpkt(sc)
817 struct ppp_softc *sc;
818 {
819 int s;
820 struct mbuf *m, *mp;
821 u_char *cp;
822 int address, control, protocol;
823 enum NPmode mode;
824
825 /*
826 * Grab a packet to send: first try the fast queue, then the
827 * normal queue.
828 */
829 IF_DEQUEUE(&sc->sc_fastq, m);
830 if (m == NULL)
831 IF_DEQUEUE(&sc->sc_if.if_snd, m);
832 if (m == NULL)
833 return;
834
835 /*
836 * Extract the ppp header of the new packet.
837 * The ppp header will be in one mbuf.
838 */
839 cp = mtod(m, u_char *);
840 address = PPP_ADDRESS(cp);
841 control = PPP_CONTROL(cp);
842 protocol = PPP_PROTOCOL(cp);
843
844 switch (protocol) {
845 case PPP_IP:
846 /*
847 * Update the time we sent the most recent packet.
848 */
849 sc->sc_last_sent = time.tv_sec;
850
851 #ifdef VJC
852 /*
853 * If the packet is a TCP/IP packet, see if we can compress it.
854 */
855 if (sc->sc_flags & SC_COMP_TCP) {
856 struct ip *ip;
857 int type;
858
859 mp = m;
860 ip = (struct ip *) (cp + PPP_HDRLEN);
861 if (mp->m_len <= PPP_HDRLEN) {
862 mp = mp->m_next;
863 if (mp == NULL)
864 break;
865 ip = mtod(mp, struct ip *);
866 }
867 /* this code assumes the IP/TCP header is in one non-shared mbuf */
868 if (ip->ip_p == IPPROTO_TCP) {
869 type = sl_compress_tcp(mp, ip, &sc->sc_comp,
870 !(sc->sc_flags & SC_NO_TCP_CCID));
871 switch (type) {
872 case TYPE_UNCOMPRESSED_TCP:
873 protocol = PPP_VJC_UNCOMP;
874 break;
875 case TYPE_COMPRESSED_TCP:
876 protocol = PPP_VJC_COMP;
877 cp = mtod(m, u_char *);
878 cp[0] = address; /* header has moved */
879 cp[1] = control;
880 cp[2] = 0;
881 break;
882 }
883 cp[3] = protocol; /* update protocol in PPP header */
884 }
885 }
886 #endif /* VJC */
887 break;
888
889 #ifdef PPP_COMPRESS
890 case PPP_CCP:
891 ppp_ccp(sc, m, 0);
892 break;
893 #endif /* PPP_COMPRESS */
894 }
895
896 #ifdef PPP_COMPRESS
897 if (protocol != PPP_LCP && protocol != PPP_CCP
898 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
899 struct mbuf *mcomp;
900 int slen, clen;
901
902 slen = 0;
903 for (mp = m; mp != NULL; mp = mp->m_next)
904 slen += mp->m_len;
905 clen = (*sc->sc_xcomp->compress)
906 (sc->sc_xc_state, &mcomp, m, slen,
907 (sc->sc_flags & SC_CCP_UP? sc->sc_if.if_mtu: 0));
908 if (mcomp != NULL) {
909 m_freem(m);
910 m = mcomp;
911 cp = mtod(m, u_char *);
912 protocol = cp[3];
913 }
914 }
915 #endif /* PPP_COMPRESS */
916
917 /*
918 * Compress the address/control and protocol, if possible.
919 */
920 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
921 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
922 protocol != PPP_LCP) {
923 /* can compress address/control */
924 m->m_data += 2;
925 m->m_len -= 2;
926 }
927 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
928 /* can compress protocol */
929 if (mtod(m, u_char *) == cp) {
930 cp[2] = cp[1]; /* move address/control up */
931 cp[1] = cp[0];
932 }
933 ++m->m_data;
934 --m->m_len;
935 }
936
937 sc->sc_togo = m;
938 (*sc->sc_start)(sc);
939 }
940
941 #ifdef PPP_COMPRESS
942 /*
943 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
944 * 0 if it is about to be transmitted.
945 */
946 static int
947 ppp_ccp(sc, m, rcvd)
948 struct ppp_softc *sc;
949 struct mbuf *m;
950 int rcvd;
951 {
952 u_char *dp, *ep;
953 struct mbuf *mp;
954 int slen, s;
955
956 /*
957 * Get a pointer to the data after the PPP header.
958 */
959 if (m->m_len <= PPP_HDRLEN) {
960 mp = m->m_next;
961 if (mp == NULL)
962 return;
963 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
964 } else {
965 mp = m;
966 dp = mtod(mp, u_char *) + PPP_HDRLEN;
967 }
968
969 ep = mtod(mp, u_char *) + mp->m_len;
970 if (dp + CCP_HDRLEN > ep)
971 return;
972 slen = CCP_LENGTH(dp);
973 if (dp + slen > ep) {
974 if (sc->sc_flags & SC_DEBUG)
975 printf("if_ppp/ccp: not enough data in mbuf (%x+%x > %x+%x)\n",
976 dp, slen, mtod(mp, u_char *), mp->m_len);
977 return;
978 }
979
980 switch (CCP_CODE(dp)) {
981 case CCP_CONFREQ:
982 case CCP_TERMREQ:
983 case CCP_TERMACK:
984 /* CCP must be going down - disable compression */
985 if (sc->sc_flags & SC_CCP_UP) {
986 s = splhigh();
987 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
988 splx(s);
989 }
990 break;
991
992 case CCP_CONFACK:
993 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
994 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
995 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
996 if (!rcvd) {
997 /* we're agreeing to send compressed packets. */
998 if (sc->sc_xc_state != NULL
999 && (*sc->sc_xcomp->comp_init)
1000 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1001 sc->sc_if.if_unit, 0, sc->sc_flags & SC_DEBUG)) {
1002 s = splhigh();
1003 sc->sc_flags |= SC_COMP_RUN;
1004 splx(s);
1005 }
1006 } else {
1007 /* peer is agreeing to send compressed packets. */
1008 if (sc->sc_rc_state != NULL
1009 && (*sc->sc_rcomp->decomp_init)
1010 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1011 sc->sc_if.if_unit, 0, sc->sc_mru,
1012 sc->sc_flags & SC_DEBUG)) {
1013 s = splhigh();
1014 sc->sc_flags |= SC_DECOMP_RUN;
1015 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1016 splx(s);
1017 }
1018 }
1019 }
1020 break;
1021
1022 case CCP_RESETACK:
1023 if (sc->sc_flags & SC_CCP_UP) {
1024 if (!rcvd) {
1025 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1026 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1027 } else {
1028 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1029 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1030 s = splhigh();
1031 sc->sc_flags &= ~SC_DC_ERROR;
1032 splx(s);
1033 }
1034 }
1035 }
1036 break;
1037 }
1038 }
1039
1040 /*
1041 * CCP is down; free (de)compressor state if necessary.
1042 */
1043 static void
1044 ppp_ccp_closed(sc)
1045 struct ppp_softc *sc;
1046 {
1047 if (sc->sc_xc_state) {
1048 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1049 sc->sc_xc_state = NULL;
1050 }
1051 if (sc->sc_rc_state) {
1052 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1053 sc->sc_rc_state = NULL;
1054 }
1055 }
1056 #endif /* PPP_COMPRESS */
1057
1058 /*
1059 * PPP packet input routine.
1060 * The caller has checked and removed the FCS and has inserted
1061 * the address/control bytes and the protocol high byte if they
1062 * were omitted.
1063 */
1064 void
1065 ppppktin(sc, m, lost)
1066 struct ppp_softc *sc;
1067 struct mbuf *m;
1068 int lost;
1069 {
1070 int s = splhigh();
1071
1072 if (lost)
1073 m->m_flags |= M_ERRMARK;
1074 IF_ENQUEUE(&sc->sc_rawq, m);
1075 schednetisr(NETISR_PPP);
1076 splx(s);
1077 }
1078
1079 /*
1080 * Process a received PPP packet, doing decompression as necessary.
1081 * Should be called at splsoftnet.
1082 */
1083 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1084 TYPE_UNCOMPRESSED_TCP)
1085
1086 static void
1087 ppp_inproc(sc, m)
1088 struct ppp_softc *sc;
1089 struct mbuf *m;
1090 {
1091 struct ifnet *ifp = &sc->sc_if;
1092 struct ifqueue *inq;
1093 int s, ilen, xlen, proto, rv;
1094 u_char *cp, adrs, ctrl;
1095 struct mbuf *mp, *dmp;
1096 u_char *iphdr;
1097 u_int hlen;
1098
1099 ifp->if_ipackets++;
1100 ifp->if_lastchange = time;
1101
1102 if (sc->sc_flags & SC_LOG_INPKT) {
1103 printf("ppp%d: got %d bytes\n", ifp->if_unit, ilen);
1104 pppdumpm(m);
1105 }
1106
1107 cp = mtod(m, u_char *);
1108 adrs = PPP_ADDRESS(cp);
1109 ctrl = PPP_CONTROL(cp);
1110 proto = PPP_PROTOCOL(cp);
1111
1112 if (m->m_flags & M_ERRMARK) {
1113 m->m_flags &= ~M_ERRMARK;
1114 s = splhigh();
1115 sc->sc_flags |= SC_VJ_RESET;
1116 splx(s);
1117 }
1118
1119 #ifdef PPP_COMPRESS
1120 /*
1121 * Decompress this packet if necessary, update the receiver's
1122 * dictionary, or take appropriate action on a CCP packet.
1123 */
1124 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1125 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1126 /* decompress this packet */
1127 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1128 if (rv == DECOMP_OK) {
1129 m_freem(m);
1130 if (dmp == NULL) {
1131 /* no error, but no decompressed packet produced */
1132 return;
1133 }
1134 m = dmp;
1135 cp = mtod(m, u_char *);
1136 proto = PPP_PROTOCOL(cp);
1137
1138 } else {
1139 /*
1140 * An error has occurred in decompression.
1141 * Pass the compressed packet up to pppd, which may take
1142 * CCP down or issue a Reset-Req.
1143 */
1144 if (sc->sc_flags & SC_DEBUG)
1145 printf("ppp%d: decompress failed %d\n", ifp->if_unit, rv);
1146 s = splhigh();
1147 sc->sc_flags |= SC_VJ_RESET;
1148 if (rv == DECOMP_ERROR)
1149 sc->sc_flags |= SC_DC_ERROR;
1150 else
1151 sc->sc_flags |= SC_DC_FERROR;
1152 splx(s);
1153 }
1154
1155 } else {
1156 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1157 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1158 }
1159 if (proto == PPP_CCP) {
1160 ppp_ccp(sc, m, 1);
1161 }
1162 }
1163 #endif
1164
1165 ilen = 0;
1166 for (mp = m; mp != NULL; mp = mp->m_next)
1167 ilen += mp->m_len;
1168
1169 #ifdef VJC
1170 if (sc->sc_flags & SC_VJ_RESET) {
1171 /*
1172 * If we've missed a packet, we must toss subsequent compressed
1173 * packets which don't have an explicit connection ID.
1174 */
1175 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, &sc->sc_comp);
1176 s = splhigh();
1177 sc->sc_flags &= ~SC_VJ_RESET;
1178 splx(s);
1179 }
1180
1181 /*
1182 * See if we have a VJ-compressed packet to uncompress.
1183 */
1184 if (proto == PPP_VJC_COMP) {
1185 if (sc->sc_flags & SC_REJ_COMP_TCP)
1186 goto bad;
1187
1188 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1189 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1190 &sc->sc_comp, &iphdr, &hlen);
1191
1192 if (xlen <= 0) {
1193 if (sc->sc_flags & SC_DEBUG)
1194 printf("ppp%d: VJ uncompress failed on type comp\n",
1195 ifp->if_unit);
1196 goto bad;
1197 }
1198
1199 /* Copy the PPP and IP headers into a new mbuf. */
1200 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1201 if (mp == NULL)
1202 goto bad;
1203 mp->m_len = 0;
1204 mp->m_next = NULL;
1205 if (hlen + PPP_HDRLEN > MHLEN) {
1206 MCLGET(mp, M_DONTWAIT);
1207 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1208 m_freem(mp);
1209 goto bad; /* lose if big headers and no clusters */
1210 }
1211 }
1212 cp = mtod(mp, u_char *);
1213 cp[0] = adrs;
1214 cp[1] = ctrl;
1215 cp[2] = 0;
1216 cp[3] = PPP_IP;
1217 proto = PPP_IP;
1218 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1219 mp->m_len = hlen + PPP_HDRLEN;
1220
1221 /*
1222 * Trim the PPP and VJ headers off the old mbuf
1223 * and stick the new and old mbufs together.
1224 */
1225 m->m_data += PPP_HDRLEN + xlen;
1226 m->m_len -= PPP_HDRLEN + xlen;
1227 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1228 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1229 mp->m_len += m->m_len;
1230 MFREE(m, mp->m_next);
1231 } else
1232 mp->m_next = m;
1233 m = mp;
1234 ilen += hlen - xlen;
1235
1236 } else if (proto == PPP_VJC_UNCOMP) {
1237 if (sc->sc_flags & SC_REJ_COMP_TCP)
1238 goto bad;
1239
1240 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1241 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1242 &sc->sc_comp, &iphdr, &hlen);
1243
1244 if (xlen < 0) {
1245 if (sc->sc_flags & SC_DEBUG)
1246 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1247 ifp->if_unit);
1248 goto bad;
1249 }
1250
1251 proto = PPP_IP;
1252 cp[3] = PPP_IP;
1253 }
1254 #endif /* VJC */
1255
1256 /*
1257 * If the packet will fit in a header mbuf, don't waste a
1258 * whole cluster on it.
1259 */
1260 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1261 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1262 if (mp != NULL) {
1263 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1264 m_freem(m);
1265 m = mp;
1266 m->m_len = ilen;
1267 }
1268 }
1269 m->m_pkthdr.len = ilen;
1270 m->m_pkthdr.rcvif = ifp;
1271
1272 #if NBPFILTER > 0
1273 /* See if bpf wants to look at the packet. */
1274 if (sc->sc_bpf)
1275 bpf_mtap(sc->sc_bpf, m);
1276 #endif
1277
1278 rv = 0;
1279 switch (proto) {
1280 #ifdef INET
1281 case PPP_IP:
1282 /*
1283 * IP packet - take off the ppp header and pass it up to IP.
1284 */
1285 if ((ifp->if_flags & IFF_UP) == 0
1286 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1287 /* interface is down - drop the packet. */
1288 m_freem(m);
1289 return;
1290 }
1291 m->m_pkthdr.len -= PPP_HDRLEN;
1292 m->m_data += PPP_HDRLEN;
1293 m->m_len -= PPP_HDRLEN;
1294 schednetisr(NETISR_IP);
1295 inq = &ipintrq;
1296 sc->sc_last_recv = time.tv_sec; /* update time of last pkt rcvd */
1297 break;
1298 #endif
1299
1300 default:
1301 /*
1302 * Some other protocol - place on input queue for read().
1303 */
1304 inq = &sc->sc_inq;
1305 rv = 1;
1306 break;
1307 }
1308
1309 /*
1310 * Put the packet on the appropriate input queue.
1311 */
1312 s = splhigh();
1313 if (IF_QFULL(inq)) {
1314 IF_DROP(inq);
1315 splx(s);
1316 if (sc->sc_flags & SC_DEBUG)
1317 printf("ppp%d: input queue full\n", ifp->if_unit);
1318 ifp->if_iqdrops++;
1319 goto bad;
1320 }
1321 IF_ENQUEUE(inq, m);
1322 splx(s);
1323
1324 if (rv)
1325 (*sc->sc_ctlp)(sc);
1326
1327 return;
1328
1329 bad:
1330 m_freem(m);
1331 sc->sc_if.if_ierrors++;
1332 }
1333
1334 #define MAX_DUMP_BYTES 128
1335
1336 static void
1337 pppdumpm(m0)
1338 struct mbuf *m0;
1339 {
1340 char buf[3*MAX_DUMP_BYTES+4];
1341 char *bp = buf;
1342 struct mbuf *m;
1343 static char digits[] = "0123456789abcdef";
1344
1345 for (m = m0; m; m = m->m_next) {
1346 int l = m->m_len;
1347 u_char *rptr = (u_char *)m->m_data;
1348
1349 while (l--) {
1350 if (bp > buf + sizeof(buf) - 4)
1351 goto done;
1352 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1353 *bp++ = digits[*rptr++ & 0xf];
1354 }
1355
1356 if (m->m_next) {
1357 if (bp > buf + sizeof(buf) - 3)
1358 goto done;
1359 *bp++ = '|';
1360 } else
1361 *bp++ = ' ';
1362 }
1363 done:
1364 if (m)
1365 *bp++ = '>';
1366 *bp = 0;
1367 printf("%s\n", buf);
1368 }
1369
1370 #endif /* NPPP > 0 */
1371