if_ppp.c revision 1.23 1 /* $NetBSD: if_ppp.c,v 1.23 1995/08/12 23:59:21 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
691 splx(s);
692 return (0);
693
694 bad:
695 m_freem(m0);
696 return (error);
697 }
698
699 /*
700 * After a change in the NPmode for some NP, move packets from the
701 * npqueue to the send queue or the fast queue as appropriate.
702 * Should be called at splsoftnet.
703 */
704 static void
705 ppp_requeue(sc)
706 struct ppp_softc *sc;
707 {
708 struct mbuf *m, **mpp;
709 struct ifqueue *ifq;
710 enum NPmode mode;
711
712 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
713 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
714 case PPP_IP:
715 mode = sc->sc_npmode[NP_IP];
716 break;
717 default:
718 mode = NPMODE_PASS;
719 }
720
721 switch (mode) {
722 case NPMODE_PASS:
723 /*
724 * This packet can now go on one of the queues to be sent.
725 */
726 *mpp = m->m_nextpkt;
727 m->m_nextpkt = NULL;
728 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
729 if (IF_QFULL(ifq)) {
730 IF_DROP(ifq);
731 sc->sc_if.if_oerrors++;
732 } else
733 IF_ENQUEUE(ifq, m);
734 break;
735
736 case NPMODE_DROP:
737 case NPMODE_ERROR:
738 *mpp = m->m_nextpkt;
739 m_freem(m);
740 break;
741
742 case NPMODE_QUEUE:
743 mpp = &m->m_nextpkt;
744 break;
745 }
746 }
747 sc->sc_npqtail = mpp;
748 }
749
750 /*
751 * Get a packet to send. This procedure is intended to be called at
752 * spltty or splimp, so it takes little time. If there isn't a packet
753 * waiting to go out, it schedules a software interrupt to prepare a
754 * new packet; the device start routine gets called again when a
755 * packet is ready.
756 */
757 struct mbuf *
758 ppp_dequeue(sc)
759 struct ppp_softc *sc;
760 {
761 struct mbuf *m;
762 int s = splhigh();
763
764 m = sc->sc_togo;
765 if (m) {
766 /*
767 * Had a packet waiting - send it.
768 */
769 sc->sc_togo = NULL;
770 sc->sc_flags |= SC_TBUSY;
771 splx(s);
772 return m;
773 }
774 /*
775 * Remember we wanted a packet and schedule a software interrupt.
776 */
777 sc->sc_flags &= ~SC_TBUSY;
778 schednetisr(NETISR_PPP);
779 splx(s);
780 return NULL;
781 }
782
783 /*
784 * Software interrupt routine, called at splsoftnet.
785 */
786 void
787 pppintr()
788 {
789 struct ppp_softc *sc;
790 int i, s;
791 struct mbuf *m;
792
793 sc = ppp_softc;
794 for (i = 0; i < NPPP; ++i, ++sc) {
795 if (!(sc->sc_flags & SC_TBUSY) && sc->sc_togo == NULL
796 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head))
797 ppp_outpkt(sc);
798 for (;;) {
799 s = splhigh();
800 IF_DEQUEUE(&sc->sc_rawq, m);
801 splx(s);
802 if (m == NULL)
803 break;
804 ppp_inproc(sc, m);
805 }
806 }
807 }
808
809 /*
810 * Grab another packet off a queue and apply VJ compression,
811 * packet compression, address/control and/or protocol compression
812 * if enabled. Should be called at splsoftnet.
813 */
814 static void
815 ppp_outpkt(sc)
816 struct ppp_softc *sc;
817 {
818 int s;
819 struct mbuf *m, *mp;
820 u_char *cp;
821 int address, control, protocol;
822 enum NPmode mode;
823
824 /*
825 * Grab a packet to send: first try the fast queue, then the
826 * normal queue.
827 */
828 IF_DEQUEUE(&sc->sc_fastq, m);
829 if (m == NULL)
830 IF_DEQUEUE(&sc->sc_if.if_snd, m);
831 if (m == NULL)
832 return;
833
834 /*
835 * Extract the ppp header of the new packet.
836 * The ppp header will be in one mbuf.
837 */
838 cp = mtod(m, u_char *);
839 address = PPP_ADDRESS(cp);
840 control = PPP_CONTROL(cp);
841 protocol = PPP_PROTOCOL(cp);
842
843 switch (protocol) {
844 case PPP_IP:
845 /*
846 * Update the time we sent the most recent packet.
847 */
848 sc->sc_last_sent = time.tv_sec;
849
850 #ifdef VJC
851 /*
852 * If the packet is a TCP/IP packet, see if we can compress it.
853 */
854 if (sc->sc_flags & SC_COMP_TCP) {
855 struct ip *ip;
856 int type;
857
858 mp = m;
859 ip = (struct ip *) (cp + PPP_HDRLEN);
860 if (mp->m_len <= PPP_HDRLEN) {
861 mp = mp->m_next;
862 if (mp == NULL)
863 break;
864 ip = mtod(mp, struct ip *);
865 }
866 /* this code assumes the IP/TCP header is in one non-shared mbuf */
867 if (ip->ip_p == IPPROTO_TCP) {
868 type = sl_compress_tcp(mp, ip, &sc->sc_comp,
869 !(sc->sc_flags & SC_NO_TCP_CCID));
870 switch (type) {
871 case TYPE_UNCOMPRESSED_TCP:
872 protocol = PPP_VJC_UNCOMP;
873 break;
874 case TYPE_COMPRESSED_TCP:
875 protocol = PPP_VJC_COMP;
876 cp = mtod(m, u_char *);
877 cp[0] = address; /* header has moved */
878 cp[1] = control;
879 cp[2] = 0;
880 break;
881 }
882 cp[3] = protocol; /* update protocol in PPP header */
883 }
884 }
885 #endif /* VJC */
886 break;
887
888 #ifdef PPP_COMPRESS
889 case PPP_CCP:
890 ppp_ccp(sc, m, 0);
891 break;
892 #endif /* PPP_COMPRESS */
893 }
894
895 #ifdef PPP_COMPRESS
896 if (protocol != PPP_LCP && protocol != PPP_CCP
897 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
898 struct mbuf *mcomp;
899 int slen, clen;
900
901 slen = 0;
902 for (mp = m; mp != NULL; mp = mp->m_next)
903 slen += mp->m_len;
904 clen = (*sc->sc_xcomp->compress)
905 (sc->sc_xc_state, &mcomp, m, slen,
906 (sc->sc_flags & SC_CCP_UP? sc->sc_if.if_mtu: 0));
907 if (mcomp != NULL) {
908 m_freem(m);
909 m = mcomp;
910 cp = mtod(m, u_char *);
911 protocol = cp[3];
912 }
913 }
914 #endif /* PPP_COMPRESS */
915
916 /*
917 * Compress the address/control and protocol, if possible.
918 */
919 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
920 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
921 protocol != PPP_LCP) {
922 /* can compress address/control */
923 m->m_data += 2;
924 m->m_len -= 2;
925 }
926 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
927 /* can compress protocol */
928 if (mtod(m, u_char *) == cp) {
929 cp[2] = cp[1]; /* move address/control up */
930 cp[1] = cp[0];
931 }
932 ++m->m_data;
933 --m->m_len;
934 }
935
936 sc->sc_togo = m;
937 (*sc->sc_start)(sc);
938 }
939
940 #ifdef PPP_COMPRESS
941 /*
942 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
943 * 0 if it is about to be transmitted.
944 */
945 static int
946 ppp_ccp(sc, m, rcvd)
947 struct ppp_softc *sc;
948 struct mbuf *m;
949 int rcvd;
950 {
951 u_char *dp, *ep;
952 struct mbuf *mp;
953 int slen, s;
954
955 /*
956 * Get a pointer to the data after the PPP header.
957 */
958 if (m->m_len <= PPP_HDRLEN) {
959 mp = m->m_next;
960 if (mp == NULL)
961 return;
962 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
963 } else {
964 mp = m;
965 dp = mtod(mp, u_char *) + PPP_HDRLEN;
966 }
967
968 ep = mtod(mp, u_char *) + mp->m_len;
969 if (dp + CCP_HDRLEN > ep)
970 return;
971 slen = CCP_LENGTH(dp);
972 if (dp + slen > ep) {
973 if (sc->sc_flags & SC_DEBUG)
974 printf("if_ppp/ccp: not enough data in mbuf (%x+%x > %x+%x)\n",
975 dp, slen, mtod(mp, u_char *), mp->m_len);
976 return;
977 }
978
979 switch (CCP_CODE(dp)) {
980 case CCP_CONFREQ:
981 case CCP_TERMREQ:
982 case CCP_TERMACK:
983 /* CCP must be going down - disable compression */
984 if (sc->sc_flags & SC_CCP_UP) {
985 s = splhigh();
986 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
987 splx(s);
988 }
989 break;
990
991 case CCP_CONFACK:
992 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
993 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
994 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
995 if (!rcvd) {
996 /* we're agreeing to send compressed packets. */
997 if (sc->sc_xc_state != NULL
998 && (*sc->sc_xcomp->comp_init)
999 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1000 sc->sc_if.if_unit, 0, sc->sc_flags & SC_DEBUG)) {
1001 s = splhigh();
1002 sc->sc_flags |= SC_COMP_RUN;
1003 splx(s);
1004 }
1005 } else {
1006 /* peer is agreeing to send compressed packets. */
1007 if (sc->sc_rc_state != NULL
1008 && (*sc->sc_rcomp->decomp_init)
1009 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1010 sc->sc_if.if_unit, 0, sc->sc_mru,
1011 sc->sc_flags & SC_DEBUG)) {
1012 s = splhigh();
1013 sc->sc_flags |= SC_DECOMP_RUN;
1014 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1015 splx(s);
1016 }
1017 }
1018 }
1019 break;
1020
1021 case CCP_RESETACK:
1022 if (sc->sc_flags & SC_CCP_UP) {
1023 if (!rcvd) {
1024 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1025 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1026 } else {
1027 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1028 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1029 s = splhigh();
1030 sc->sc_flags &= ~SC_DC_ERROR;
1031 splx(s);
1032 }
1033 }
1034 }
1035 break;
1036 }
1037 }
1038
1039 /*
1040 * CCP is down; free (de)compressor state if necessary.
1041 */
1042 static void
1043 ppp_ccp_closed(sc)
1044 struct ppp_softc *sc;
1045 {
1046 if (sc->sc_xc_state) {
1047 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1048 sc->sc_xc_state = NULL;
1049 }
1050 if (sc->sc_rc_state) {
1051 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1052 sc->sc_rc_state = NULL;
1053 }
1054 }
1055 #endif /* PPP_COMPRESS */
1056
1057 /*
1058 * PPP packet input routine.
1059 * The caller has checked and removed the FCS and has inserted
1060 * the address/control bytes and the protocol high byte if they
1061 * were omitted.
1062 */
1063 void
1064 ppppktin(sc, m, lost)
1065 struct ppp_softc *sc;
1066 struct mbuf *m;
1067 int lost;
1068 {
1069 int s = splhigh();
1070
1071 if (lost)
1072 m->m_flags |= M_ERRMARK;
1073 IF_ENQUEUE(&sc->sc_rawq, m);
1074 schednetisr(NETISR_PPP);
1075 splx(s);
1076 }
1077
1078 /*
1079 * Process a received PPP packet, doing decompression as necessary.
1080 * Should be called at splsoftnet.
1081 */
1082 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1083 TYPE_UNCOMPRESSED_TCP)
1084
1085 static void
1086 ppp_inproc(sc, m)
1087 struct ppp_softc *sc;
1088 struct mbuf *m;
1089 {
1090 struct ifqueue *inq;
1091 int s, ilen, xlen, proto, rv;
1092 u_char *cp, adrs, ctrl;
1093 struct mbuf *mp, *dmp;
1094 u_char *iphdr;
1095 u_int hlen;
1096
1097 sc->sc_if.if_ipackets++;
1098
1099 if (sc->sc_flags & SC_LOG_INPKT) {
1100 printf("ppp%d: got %d bytes\n", sc->sc_if.if_unit, ilen);
1101 pppdumpm(m);
1102 }
1103
1104 cp = mtod(m, u_char *);
1105 adrs = PPP_ADDRESS(cp);
1106 ctrl = PPP_CONTROL(cp);
1107 proto = PPP_PROTOCOL(cp);
1108
1109 if (m->m_flags & M_ERRMARK) {
1110 m->m_flags &= ~M_ERRMARK;
1111 s = splhigh();
1112 sc->sc_flags |= SC_VJ_RESET;
1113 splx(s);
1114 }
1115
1116 #ifdef PPP_COMPRESS
1117 /*
1118 * Decompress this packet if necessary, update the receiver's
1119 * dictionary, or take appropriate action on a CCP packet.
1120 */
1121 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1122 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1123 /* decompress this packet */
1124 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1125 if (rv == DECOMP_OK) {
1126 m_freem(m);
1127 if (dmp == NULL) {
1128 /* no error, but no decompressed packet produced */
1129 return;
1130 }
1131 m = dmp;
1132 cp = mtod(m, u_char *);
1133 proto = PPP_PROTOCOL(cp);
1134
1135 } else {
1136 /*
1137 * An error has occurred in decompression.
1138 * Pass the compressed packet up to pppd, which may take
1139 * CCP down or issue a Reset-Req.
1140 */
1141 if (sc->sc_flags & SC_DEBUG)
1142 printf("ppp%d: decompress failed %d\n", sc->sc_if.if_unit, rv);
1143 s = splhigh();
1144 sc->sc_flags |= SC_VJ_RESET;
1145 if (rv == DECOMP_ERROR)
1146 sc->sc_flags |= SC_DC_ERROR;
1147 else
1148 sc->sc_flags |= SC_DC_FERROR;
1149 splx(s);
1150 }
1151
1152 } else {
1153 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1154 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1155 }
1156 if (proto == PPP_CCP) {
1157 ppp_ccp(sc, m, 1);
1158 }
1159 }
1160 #endif
1161
1162 ilen = 0;
1163 for (mp = m; mp != NULL; mp = mp->m_next)
1164 ilen += mp->m_len;
1165
1166 #ifdef VJC
1167 if (sc->sc_flags & SC_VJ_RESET) {
1168 /*
1169 * If we've missed a packet, we must toss subsequent compressed
1170 * packets which don't have an explicit connection ID.
1171 */
1172 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, &sc->sc_comp);
1173 s = splhigh();
1174 sc->sc_flags &= ~SC_VJ_RESET;
1175 splx(s);
1176 }
1177
1178 /*
1179 * See if we have a VJ-compressed packet to uncompress.
1180 */
1181 if (proto == PPP_VJC_COMP) {
1182 if (sc->sc_flags & SC_REJ_COMP_TCP)
1183 goto bad;
1184
1185 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1186 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1187 &sc->sc_comp, &iphdr, &hlen);
1188
1189 if (xlen <= 0) {
1190 if (sc->sc_flags & SC_DEBUG)
1191 printf("ppp%d: VJ uncompress failed on type comp\n",
1192 sc->sc_if.if_unit);
1193 goto bad;
1194 }
1195
1196 /* Copy the PPP and IP headers into a new mbuf. */
1197 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1198 if (mp == NULL)
1199 goto bad;
1200 mp->m_len = 0;
1201 mp->m_next = NULL;
1202 if (hlen + PPP_HDRLEN > MHLEN) {
1203 MCLGET(mp, M_DONTWAIT);
1204 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1205 m_freem(mp);
1206 goto bad; /* lose if big headers and no clusters */
1207 }
1208 }
1209 cp = mtod(mp, u_char *);
1210 cp[0] = adrs;
1211 cp[1] = ctrl;
1212 cp[2] = 0;
1213 cp[3] = PPP_IP;
1214 proto = PPP_IP;
1215 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1216 mp->m_len = hlen + PPP_HDRLEN;
1217
1218 /*
1219 * Trim the PPP and VJ headers off the old mbuf
1220 * and stick the new and old mbufs together.
1221 */
1222 m->m_data += PPP_HDRLEN + xlen;
1223 m->m_len -= PPP_HDRLEN + xlen;
1224 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1225 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1226 mp->m_len += m->m_len;
1227 MFREE(m, mp->m_next);
1228 } else
1229 mp->m_next = m;
1230 m = mp;
1231 ilen += hlen - xlen;
1232
1233 } else if (proto == PPP_VJC_UNCOMP) {
1234 if (sc->sc_flags & SC_REJ_COMP_TCP)
1235 goto bad;
1236
1237 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1238 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1239 &sc->sc_comp, &iphdr, &hlen);
1240
1241 if (xlen < 0) {
1242 if (sc->sc_flags & SC_DEBUG)
1243 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1244 sc->sc_if.if_unit);
1245 goto bad;
1246 }
1247
1248 proto = PPP_IP;
1249 cp[3] = PPP_IP;
1250 }
1251 #endif /* VJC */
1252
1253 /*
1254 * If the packet will fit in a header mbuf, don't waste a
1255 * whole cluster on it.
1256 */
1257 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1258 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1259 if (mp != NULL) {
1260 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1261 m_freem(m);
1262 m = mp;
1263 m->m_len = ilen;
1264 }
1265 }
1266 m->m_pkthdr.len = ilen;
1267 m->m_pkthdr.rcvif = &sc->sc_if;
1268
1269 #if NBPFILTER > 0
1270 /* See if bpf wants to look at the packet. */
1271 if (sc->sc_bpf)
1272 bpf_mtap(sc->sc_bpf, m);
1273 #endif
1274
1275 rv = 0;
1276 switch (proto) {
1277 #ifdef INET
1278 case PPP_IP:
1279 /*
1280 * IP packet - take off the ppp header and pass it up to IP.
1281 */
1282 if ((sc->sc_if.if_flags & IFF_UP) == 0
1283 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1284 /* interface is down - drop the packet. */
1285 m_freem(m);
1286 return;
1287 }
1288 m->m_pkthdr.len -= PPP_HDRLEN;
1289 m->m_data += PPP_HDRLEN;
1290 m->m_len -= PPP_HDRLEN;
1291 schednetisr(NETISR_IP);
1292 inq = &ipintrq;
1293 sc->sc_last_recv = time.tv_sec; /* update time of last pkt rcvd */
1294 break;
1295 #endif
1296
1297 default:
1298 /*
1299 * Some other protocol - place on input queue for read().
1300 */
1301 inq = &sc->sc_inq;
1302 rv = 1;
1303 break;
1304 }
1305
1306 /*
1307 * Put the packet on the appropriate input queue.
1308 */
1309 s = splhigh();
1310 if (IF_QFULL(inq)) {
1311 IF_DROP(inq);
1312 splx(s);
1313 if (sc->sc_flags & SC_DEBUG)
1314 printf("ppp%d: input queue full\n", sc->sc_if.if_unit);
1315 sc->sc_if.if_iqdrops++;
1316 goto bad;
1317 }
1318 IF_ENQUEUE(inq, m);
1319 splx(s);
1320
1321 if (rv)
1322 (*sc->sc_ctlp)(sc);
1323
1324 return;
1325
1326 bad:
1327 m_freem(m);
1328 sc->sc_if.if_ierrors++;
1329 }
1330
1331 #define MAX_DUMP_BYTES 128
1332
1333 static void
1334 pppdumpm(m0)
1335 struct mbuf *m0;
1336 {
1337 char buf[3*MAX_DUMP_BYTES+4];
1338 char *bp = buf;
1339 struct mbuf *m;
1340 static char digits[] = "0123456789abcdef";
1341
1342 for (m = m0; m; m = m->m_next) {
1343 int l = m->m_len;
1344 u_char *rptr = (u_char *)m->m_data;
1345
1346 while (l--) {
1347 if (bp > buf + sizeof(buf) - 4)
1348 goto done;
1349 *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1350 *bp++ = digits[*rptr++ & 0xf];
1351 }
1352
1353 if (m->m_next) {
1354 if (bp > buf + sizeof(buf) - 3)
1355 goto done;
1356 *bp++ = '|';
1357 } else
1358 *bp++ = ' ';
1359 }
1360 done:
1361 if (m)
1362 *bp++ = '>';
1363 *bp = 0;
1364 printf("%s\n", buf);
1365 }
1366
1367 #endif /* NPPP > 0 */
1368