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