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