if_ppp.c revision 1.135 1 /* $NetBSD: if_ppp.c,v 1.135 2011/10/28 20:13:32 dyoung Exp $ */
2 /* Id: if_ppp.c,v 1.6 1997/03/04 03:33:00 paulus Exp */
3
4 /*
5 * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
6 *
7 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The name "Carnegie Mellon University" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For permission or any legal
24 * details, please contact
25 * Office of Technology Transfer
26 * Carnegie Mellon University
27 * 5000 Forbes Avenue
28 * Pittsburgh, PA 15213-3890
29 * (412) 268-4387, fax: (412) 268-7395
30 * tech-transfer (at) andrew.cmu.edu
31 *
32 * 4. Redistributions of any form whatsoever must retain the following
33 * acknowledgment:
34 * "This product includes software developed by Computing Services
35 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
36 *
37 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
38 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
39 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
40 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
41 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
42 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
43 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
44 *
45 * Based on:
46 * @(#)if_sl.c 7.6.1.2 (Berkeley) 2/15/89
47 *
48 * Copyright (c) 1987 Regents of the University of California.
49 * All rights reserved.
50 *
51 * Redistribution and use in source and binary forms are permitted
52 * provided that the above copyright notice and this paragraph are
53 * duplicated in all such forms and that any documentation,
54 * advertising materials, and other materials related to such
55 * distribution and use acknowledge that the software was developed
56 * by the University of California, Berkeley. The name of the
57 * University may not be used to endorse or promote products derived
58 * from this software without specific prior written permission.
59 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
60 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
61 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
62 *
63 * Serial Line interface
64 *
65 * Rick Adams
66 * Center for Seismic Studies
67 * 1300 N 17th Street, Suite 1450
68 * Arlington, Virginia 22209
69 * (703)276-7900
70 * rick (at) seismo.ARPA
71 * seismo!rick
72 *
73 * Pounded on heavily by Chris Torek (chris (at) mimsy.umd.edu, umcp-cs!chris).
74 * Converted to 4.3BSD Beta by Chris Torek.
75 * Other changes made at Berkeley, based in part on code by Kirk Smith.
76 *
77 * Converted to 4.3BSD+ 386BSD by Brad Parker (brad (at) cayman.com)
78 * Added VJ tcp header compression; more unified ioctls
79 *
80 * Extensively modified by Paul Mackerras (paulus (at) cs.anu.edu.au).
81 * Cleaned up a lot of the mbuf-related code to fix bugs that
82 * caused system crashes and packet corruption. Changed pppstart
83 * so that it doesn't just give up with a collision if the whole
84 * packet doesn't fit in the output ring buffer.
85 *
86 * Added priority queueing for interactive IP packets, following
87 * the model of if_sl.c, plus hooks for bpf.
88 * Paul Mackerras (paulus (at) cs.anu.edu.au).
89 */
90
91 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
92 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
93
94 /*
95 * XXX IMP ME HARDER
96 *
97 * This is an explanation of that comment. This code used to use
98 * splimp() to block both network and tty interrupts. However,
99 * that call is deprecated. So, we have replaced the uses of
100 * splimp() with splhigh() in order to applomplish what it needs
101 * to accomplish, and added that happy little comment.
102 */
103
104 #include <sys/cdefs.h>
105 __KERNEL_RCSID(0, "$NetBSD: if_ppp.c,v 1.135 2011/10/28 20:13:32 dyoung Exp $");
106
107 #include "ppp.h"
108
109 #include "opt_inet.h"
110 #include "opt_gateway.h"
111 #include "opt_ppp.h"
112
113 #ifdef INET
114 #define VJC
115 #endif
116 #define PPP_COMPRESS
117
118 #include <sys/param.h>
119 #include <sys/proc.h>
120 #include <sys/mbuf.h>
121 #include <sys/socket.h>
122 #include <sys/ioctl.h>
123 #include <sys/kernel.h>
124 #include <sys/systm.h>
125 #include <sys/time.h>
126 #include <sys/malloc.h>
127 #include <sys/module.h>
128 #include <sys/mutex.h>
129 #include <sys/once.h>
130 #include <sys/conf.h>
131 #include <sys/kauth.h>
132 #include <sys/intr.h>
133 #include <sys/socketvar.h>
134
135 #include <net/if.h>
136 #include <net/if_types.h>
137 #include <net/netisr.h>
138 #include <net/route.h>
139 #ifdef PPP_FILTER
140 #include <net/bpf.h>
141 #endif
142
143 #include <netinet/in.h>
144 #include <netinet/in_systm.h>
145 #include <netinet/in_var.h>
146 #ifdef INET
147 #include <netinet/ip.h>
148 #endif
149
150 #include <net/bpf.h>
151
152 #include <net/slip.h>
153
154 #ifdef VJC
155 #include <net/slcompress.h>
156 #endif
157
158 #include <net/ppp_defs.h>
159 #include <net/if_ppp.h>
160 #include <net/if_pppvar.h>
161 #include <sys/cpu.h>
162
163 #ifdef PPP_COMPRESS
164 #define PACKETPTR struct mbuf *
165 #include <net/ppp-comp.h>
166 #endif
167
168 static int pppsioctl(struct ifnet *, u_long, void *);
169 static void ppp_requeue(struct ppp_softc *);
170 static void ppp_ccp(struct ppp_softc *, struct mbuf *m, int rcvd);
171 static void ppp_ccp_closed(struct ppp_softc *);
172 static void ppp_inproc(struct ppp_softc *, struct mbuf *);
173 static void pppdumpm(struct mbuf *m0);
174 #ifdef ALTQ
175 static void ppp_ifstart(struct ifnet *ifp);
176 #endif
177
178 static void pppintr(void *);
179
180 /*
181 * Some useful mbuf macros not in mbuf.h.
182 */
183 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
184
185 #define M_DATASTART(m) \
186 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
187 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
188
189 #define M_DATASIZE(m) \
190 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
191 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
192
193 /*
194 * We define two link layer specific mbuf flags, to mark high-priority
195 * packets for output, and received packets following lost/corrupted
196 * packets.
197 */
198 #define M_HIGHPRI M_LINK0 /* output packet for sc_fastq */
199 #define M_ERRMARK M_LINK1 /* rx packet following lost/corrupted pkt */
200
201 static int ppp_clone_create(struct if_clone *, int);
202 static int ppp_clone_destroy(struct ifnet *);
203
204 static struct ppp_softc *ppp_create(const char *, int);
205
206 static LIST_HEAD(, ppp_softc) ppp_softc_list;
207 static kmutex_t ppp_list_lock;
208
209 struct if_clone ppp_cloner =
210 IF_CLONE_INITIALIZER("ppp", ppp_clone_create, ppp_clone_destroy);
211
212 #ifdef PPP_COMPRESS
213 ONCE_DECL(ppp_compressor_mtx_init);
214 static LIST_HEAD(, compressor) ppp_compressors = { NULL };
215 static kmutex_t ppp_compressors_mtx;
216
217 static int ppp_compressor_init(void);
218 static struct compressor *ppp_get_compressor(uint8_t);
219 static void ppp_compressor_rele(struct compressor *);
220 #endif /* PPP_COMPRESS */
221
222
223 /*
224 * Called from boot code to establish ppp interfaces.
225 */
226 void
227 pppattach(void)
228 {
229 extern struct linesw ppp_disc;
230
231 if (ttyldisc_attach(&ppp_disc) != 0)
232 panic("pppattach");
233
234 mutex_init(&ppp_list_lock, MUTEX_DEFAULT, IPL_NONE);
235 LIST_INIT(&ppp_softc_list);
236 if_clone_attach(&ppp_cloner);
237 RUN_ONCE(&ppp_compressor_mtx_init, ppp_compressor_init);
238 }
239
240 static struct ppp_softc *
241 ppp_create(const char *name, int unit)
242 {
243 struct ppp_softc *sc, *sci, *scl = NULL;
244
245 sc = malloc(sizeof(*sc), M_DEVBUF, M_WAIT|M_ZERO);
246
247 mutex_enter(&ppp_list_lock);
248 if (unit == -1) {
249 int i = 0;
250 LIST_FOREACH(sci, &ppp_softc_list, sc_iflist) {
251 scl = sci;
252 if (i < sci->sc_unit) {
253 unit = i;
254 break;
255 } else {
256 #ifdef DIAGNOSTIC
257 KASSERT(i == sci->sc_unit);
258 #endif
259 i++;
260 }
261 }
262 if (unit == -1)
263 unit = i;
264 } else {
265 LIST_FOREACH(sci, &ppp_softc_list, sc_iflist) {
266 scl = sci;
267 if (unit < sci->sc_unit)
268 break;
269 else if (unit == sci->sc_unit) {
270 free(sc, M_DEVBUF);
271 return NULL;
272 }
273 }
274 }
275
276 if (sci != NULL)
277 LIST_INSERT_BEFORE(sci, sc, sc_iflist);
278 else if (scl != NULL)
279 LIST_INSERT_AFTER(scl, sc, sc_iflist);
280 else
281 LIST_INSERT_HEAD(&ppp_softc_list, sc, sc_iflist);
282
283 mutex_exit(&ppp_list_lock);
284
285 if_initname(&sc->sc_if, name, sc->sc_unit = unit);
286 callout_init(&sc->sc_timo_ch, 0);
287 sc->sc_if.if_softc = sc;
288 sc->sc_if.if_mtu = PPP_MTU;
289 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
290 sc->sc_if.if_type = IFT_PPP;
291 sc->sc_if.if_hdrlen = PPP_HDRLEN;
292 sc->sc_if.if_dlt = DLT_NULL;
293 sc->sc_if.if_ioctl = pppsioctl;
294 sc->sc_if.if_output = pppoutput;
295 #ifdef ALTQ
296 sc->sc_if.if_start = ppp_ifstart;
297 #endif
298 IFQ_SET_MAXLEN(&sc->sc_if.if_snd, IFQ_MAXLEN);
299 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
300 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
301 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
302 /* Ratio of 1:2 packets between the regular and the fast queue */
303 sc->sc_maxfastq = 2;
304 IFQ_SET_READY(&sc->sc_if.if_snd);
305 if_attach(&sc->sc_if);
306 if_alloc_sadl(&sc->sc_if);
307 bpf_attach(&sc->sc_if, DLT_NULL, 0);
308 return sc;
309 }
310
311 static int
312 ppp_clone_create(struct if_clone *ifc, int unit)
313 {
314 return ppp_create(ifc->ifc_name, unit) == NULL ? EEXIST : 0;
315 }
316
317 static int
318 ppp_clone_destroy(struct ifnet *ifp)
319 {
320 struct ppp_softc *sc = (struct ppp_softc *)ifp->if_softc;
321
322 if (sc->sc_devp != NULL)
323 return EBUSY; /* Not removing it */
324
325 mutex_enter(&ppp_list_lock);
326 LIST_REMOVE(sc, sc_iflist);
327 mutex_exit(&ppp_list_lock);
328
329 bpf_detach(ifp);
330 if_detach(ifp);
331
332 free(sc, M_DEVBUF);
333 return 0;
334 }
335
336 /*
337 * Allocate a ppp interface unit and initialize it.
338 */
339 struct ppp_softc *
340 pppalloc(pid_t pid)
341 {
342 struct ppp_softc *sc = NULL, *scf;
343 int i;
344
345 mutex_enter(&ppp_list_lock);
346 LIST_FOREACH(scf, &ppp_softc_list, sc_iflist) {
347 if (scf->sc_xfer == pid) {
348 scf->sc_xfer = 0;
349 mutex_exit(&ppp_list_lock);
350 return scf;
351 }
352 if (scf->sc_devp == NULL && sc == NULL)
353 sc = scf;
354 }
355 mutex_exit(&ppp_list_lock);
356
357 if (sc == NULL)
358 sc = ppp_create(ppp_cloner.ifc_name, -1);
359
360 sc->sc_si = softint_establish(SOFTINT_NET, pppintr, sc);
361 if (sc->sc_si == NULL) {
362 printf("%s: unable to establish softintr\n", sc->sc_if.if_xname);
363 return (NULL);
364 }
365 sc->sc_flags = 0;
366 sc->sc_mru = PPP_MRU;
367 sc->sc_relinq = NULL;
368 (void)memset(&sc->sc_stats, 0, sizeof(sc->sc_stats));
369 #ifdef VJC
370 sc->sc_comp = malloc(sizeof(struct slcompress), M_DEVBUF, M_NOWAIT);
371 if (sc->sc_comp)
372 sl_compress_init(sc->sc_comp);
373 #endif
374 #ifdef PPP_COMPRESS
375 sc->sc_xc_state = NULL;
376 sc->sc_rc_state = NULL;
377 #endif /* PPP_COMPRESS */
378 for (i = 0; i < NUM_NP; ++i)
379 sc->sc_npmode[i] = NPMODE_ERROR;
380 sc->sc_npqueue = NULL;
381 sc->sc_npqtail = &sc->sc_npqueue;
382 sc->sc_last_sent = sc->sc_last_recv = time_second;
383
384 return sc;
385 }
386
387 /*
388 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
389 */
390 void
391 pppdealloc(struct ppp_softc *sc)
392 {
393 struct mbuf *m;
394
395 softint_disestablish(sc->sc_si);
396 if_down(&sc->sc_if);
397 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
398 sc->sc_devp = NULL;
399 sc->sc_xfer = 0;
400 for (;;) {
401 IF_DEQUEUE(&sc->sc_rawq, m);
402 if (m == NULL)
403 break;
404 m_freem(m);
405 }
406 for (;;) {
407 IF_DEQUEUE(&sc->sc_inq, m);
408 if (m == NULL)
409 break;
410 m_freem(m);
411 }
412 for (;;) {
413 IF_DEQUEUE(&sc->sc_fastq, m);
414 if (m == NULL)
415 break;
416 m_freem(m);
417 }
418 while ((m = sc->sc_npqueue) != NULL) {
419 sc->sc_npqueue = m->m_nextpkt;
420 m_freem(m);
421 }
422 if (sc->sc_togo != NULL) {
423 m_freem(sc->sc_togo);
424 sc->sc_togo = NULL;
425 }
426 #ifdef PPP_COMPRESS
427 ppp_ccp_closed(sc);
428 sc->sc_xc_state = NULL;
429 sc->sc_rc_state = NULL;
430 #endif /* PPP_COMPRESS */
431 #ifdef PPP_FILTER
432 if (sc->sc_pass_filt_in.bf_insns != 0) {
433 free(sc->sc_pass_filt_in.bf_insns, M_DEVBUF);
434 sc->sc_pass_filt_in.bf_insns = 0;
435 sc->sc_pass_filt_in.bf_len = 0;
436 }
437 if (sc->sc_pass_filt_out.bf_insns != 0) {
438 free(sc->sc_pass_filt_out.bf_insns, M_DEVBUF);
439 sc->sc_pass_filt_out.bf_insns = 0;
440 sc->sc_pass_filt_out.bf_len = 0;
441 }
442 if (sc->sc_active_filt_in.bf_insns != 0) {
443 free(sc->sc_active_filt_in.bf_insns, M_DEVBUF);
444 sc->sc_active_filt_in.bf_insns = 0;
445 sc->sc_active_filt_in.bf_len = 0;
446 }
447 if (sc->sc_active_filt_out.bf_insns != 0) {
448 free(sc->sc_active_filt_out.bf_insns, M_DEVBUF);
449 sc->sc_active_filt_out.bf_insns = 0;
450 sc->sc_active_filt_out.bf_len = 0;
451 }
452 #endif /* PPP_FILTER */
453 #ifdef VJC
454 if (sc->sc_comp != 0) {
455 free(sc->sc_comp, M_DEVBUF);
456 sc->sc_comp = 0;
457 }
458 #endif
459 (void)ppp_clone_destroy(&sc->sc_if);
460 }
461
462 /*
463 * Ioctl routine for generic ppp devices.
464 */
465 int
466 pppioctl(struct ppp_softc *sc, u_long cmd, void *data, int flag,
467 struct lwp *l)
468 {
469 int s, error, flags, mru, npx;
470 u_int nb;
471 struct ppp_option_data *odp;
472 struct compressor *cp;
473 struct npioctl *npi;
474 time_t t;
475 #ifdef PPP_FILTER
476 struct bpf_program *bp, *nbp;
477 struct bpf_insn *newcode, *oldcode;
478 int newcodelen;
479 #endif /* PPP_FILTER */
480 #ifdef PPP_COMPRESS
481 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
482 #endif
483
484 switch (cmd) {
485 case PPPIOCSFLAGS:
486 case PPPIOCSMRU:
487 case PPPIOCSMAXCID:
488 case PPPIOCSCOMPRESS:
489 case PPPIOCSNPMODE:
490 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
491 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, &sc->sc_if, KAUTH_ARG(cmd),
492 NULL) != 0)
493 return (EPERM);
494 break;
495 case PPPIOCXFERUNIT:
496 /* XXX: Why is this privileged?! */
497 if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
498 KAUTH_REQ_NETWORK_INTERFACE_GETPRIV, &sc->sc_if, KAUTH_ARG(cmd),
499 NULL) != 0)
500 return (EPERM);
501 break;
502 default:
503 break;
504 }
505
506 switch (cmd) {
507 case FIONREAD:
508 *(int *)data = sc->sc_inq.ifq_len;
509 break;
510
511 case PPPIOCGUNIT:
512 *(int *)data = sc->sc_unit;
513 break;
514
515 case PPPIOCGFLAGS:
516 *(u_int *)data = sc->sc_flags;
517 break;
518
519 case PPPIOCGRAWIN:
520 {
521 struct ppp_rawin *rwin = (struct ppp_rawin *)data;
522 u_char c, q = 0;
523
524 for (c = sc->sc_rawin_start; c < sizeof(sc->sc_rawin.buf);)
525 rwin->buf[q++] = sc->sc_rawin.buf[c++];
526
527 for (c = 0; c < sc->sc_rawin_start;)
528 rwin->buf[q++] = sc->sc_rawin.buf[c++];
529
530 rwin->count = sc->sc_rawin.count;
531 }
532 break;
533
534 case PPPIOCSFLAGS:
535 flags = *(int *)data & SC_MASK;
536 s = splsoftnet();
537 #ifdef PPP_COMPRESS
538 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
539 ppp_ccp_closed(sc);
540 #endif
541 splhigh(); /* XXX IMP ME HARDER */
542 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
543 splx(s);
544 break;
545
546 case PPPIOCSMRU:
547 mru = *(int *)data;
548 if (mru >= PPP_MINMRU && mru <= PPP_MAXMRU)
549 sc->sc_mru = mru;
550 break;
551
552 case PPPIOCGMRU:
553 *(int *)data = sc->sc_mru;
554 break;
555
556 #ifdef VJC
557 case PPPIOCSMAXCID:
558 if (sc->sc_comp) {
559 s = splsoftnet();
560 sl_compress_setup(sc->sc_comp, *(int *)data);
561 splx(s);
562 }
563 break;
564 #endif
565
566 case PPPIOCXFERUNIT:
567 sc->sc_xfer = l->l_proc->p_pid;
568 break;
569
570 #ifdef PPP_COMPRESS
571 case PPPIOCSCOMPRESS:
572 odp = (struct ppp_option_data *) data;
573 nb = odp->length;
574 if (nb > sizeof(ccp_option))
575 nb = sizeof(ccp_option);
576 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
577 return (error);
578 if (ccp_option[1] < 2) /* preliminary check on the length byte */
579 return (EINVAL);
580 cp = ppp_get_compressor(ccp_option[0]);
581 if (cp == NULL) {
582 if (sc->sc_flags & SC_DEBUG)
583 printf("%s: no compressor for [%x %x %x], %x\n",
584 sc->sc_if.if_xname, ccp_option[0], ccp_option[1],
585 ccp_option[2], nb);
586 return (EINVAL); /* no handler found */
587 }
588 /*
589 * Found a handler for the protocol - try to allocate
590 * a compressor or decompressor.
591 */
592 error = 0;
593 if (odp->transmit) {
594 s = splsoftnet();
595 if (sc->sc_xc_state != NULL) {
596 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
597 ppp_compressor_rele(sc->sc_xcomp);
598 }
599 sc->sc_xcomp = cp;
600 sc->sc_xc_state = cp->comp_alloc(ccp_option, nb);
601 if (sc->sc_xc_state == NULL) {
602 if (sc->sc_flags & SC_DEBUG)
603 printf("%s: comp_alloc failed\n",
604 sc->sc_if.if_xname);
605 error = ENOBUFS;
606 }
607 splhigh(); /* XXX IMP ME HARDER */
608 sc->sc_flags &= ~SC_COMP_RUN;
609 splx(s);
610 } else {
611 s = splsoftnet();
612 if (sc->sc_rc_state != NULL) {
613 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
614 ppp_compressor_rele(sc->sc_rcomp);
615 }
616 sc->sc_rcomp = cp;
617 sc->sc_rc_state = cp->decomp_alloc(ccp_option, nb);
618 if (sc->sc_rc_state == NULL) {
619 if (sc->sc_flags & SC_DEBUG)
620 printf("%s: decomp_alloc failed\n",
621 sc->sc_if.if_xname);
622 error = ENOBUFS;
623 }
624 splhigh(); /* XXX IMP ME HARDER */
625 sc->sc_flags &= ~SC_DECOMP_RUN;
626 splx(s);
627 }
628 return (error);
629 #endif /* PPP_COMPRESS */
630
631 case PPPIOCGNPMODE:
632 case PPPIOCSNPMODE:
633 npi = (struct npioctl *) data;
634 switch (npi->protocol) {
635 case PPP_IP:
636 npx = NP_IP;
637 break;
638 case PPP_IPV6:
639 npx = NP_IPV6;
640 break;
641 default:
642 return EINVAL;
643 }
644 if (cmd == PPPIOCGNPMODE) {
645 npi->mode = sc->sc_npmode[npx];
646 } else {
647 if (npi->mode != sc->sc_npmode[npx]) {
648 s = splnet();
649 sc->sc_npmode[npx] = npi->mode;
650 if (npi->mode != NPMODE_QUEUE) {
651 ppp_requeue(sc);
652 ppp_restart(sc);
653 }
654 splx(s);
655 }
656 }
657 break;
658
659 case PPPIOCGIDLE:
660 s = splsoftnet();
661 t = time_second;
662 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
663 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
664 splx(s);
665 break;
666
667 #ifdef PPP_FILTER
668 case PPPIOCSPASS:
669 case PPPIOCSACTIVE:
670 /* These are no longer supported. */
671 return EOPNOTSUPP;
672
673 case PPPIOCSIPASS:
674 case PPPIOCSOPASS:
675 case PPPIOCSIACTIVE:
676 case PPPIOCSOACTIVE:
677 nbp = (struct bpf_program *) data;
678 if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
679 return EINVAL;
680 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
681 if (newcodelen != 0) {
682 newcode = malloc(newcodelen, M_DEVBUF, M_WAITOK);
683 /* WAITOK -- malloc() never fails. */
684 if ((error = copyin((void *)nbp->bf_insns, (void *)newcode,
685 newcodelen)) != 0) {
686 free(newcode, M_DEVBUF);
687 return error;
688 }
689 if (!bpf_validate(newcode, nbp->bf_len)) {
690 free(newcode, M_DEVBUF);
691 return EINVAL;
692 }
693 } else
694 newcode = 0;
695 switch (cmd) {
696 case PPPIOCSIPASS:
697 bp = &sc->sc_pass_filt_in;
698 break;
699
700 case PPPIOCSOPASS:
701 bp = &sc->sc_pass_filt_out;
702 break;
703
704 case PPPIOCSIACTIVE:
705 bp = &sc->sc_active_filt_in;
706 break;
707
708 case PPPIOCSOACTIVE:
709 bp = &sc->sc_active_filt_out;
710 break;
711 default:
712 free(newcode, M_DEVBUF);
713 return (EPASSTHROUGH);
714 }
715 oldcode = bp->bf_insns;
716 s = splnet();
717 bp->bf_len = nbp->bf_len;
718 bp->bf_insns = newcode;
719 splx(s);
720 if (oldcode != 0)
721 free(oldcode, M_DEVBUF);
722 break;
723 #endif /* PPP_FILTER */
724
725 default:
726 return (EPASSTHROUGH);
727 }
728 return (0);
729 }
730
731 /*
732 * Process an ioctl request to the ppp network interface.
733 */
734 static int
735 pppsioctl(struct ifnet *ifp, u_long cmd, void *data)
736 {
737 struct ppp_softc *sc = ifp->if_softc;
738 struct ifaddr *ifa = (struct ifaddr *)data;
739 struct ifreq *ifr = (struct ifreq *)data;
740 struct ppp_stats *psp;
741 #ifdef PPP_COMPRESS
742 struct ppp_comp_stats *pcp;
743 #endif
744 int s = splnet(), error = 0;
745
746 switch (cmd) {
747 case SIOCSIFFLAGS:
748 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
749 break;
750 if ((ifp->if_flags & IFF_RUNNING) == 0)
751 ifp->if_flags &= ~IFF_UP;
752 break;
753
754 case SIOCINITIFADDR:
755 switch (ifa->ifa_addr->sa_family) {
756 #ifdef INET
757 case AF_INET:
758 break;
759 #endif
760 #ifdef INET6
761 case AF_INET6:
762 break;
763 #endif
764 default:
765 error = EAFNOSUPPORT;
766 break;
767 }
768 break;
769
770 case SIOCSIFDSTADDR:
771 switch (ifa->ifa_addr->sa_family) {
772 #ifdef INET
773 case AF_INET:
774 break;
775 #endif
776 #ifdef INET6
777 case AF_INET6:
778 break;
779 #endif
780 default:
781 error = EAFNOSUPPORT;
782 break;
783 }
784 break;
785
786 case SIOCADDMULTI:
787 case SIOCDELMULTI:
788 if (ifr == NULL) {
789 error = EAFNOSUPPORT;
790 break;
791 }
792 switch (ifreq_getaddr(cmd, ifr)->sa_family) {
793 #ifdef INET
794 case AF_INET:
795 break;
796 #endif
797 #ifdef INET6
798 case AF_INET6:
799 break;
800 #endif
801 default:
802 error = EAFNOSUPPORT;
803 break;
804 }
805 break;
806
807 case SIOCGPPPSTATS:
808 psp = &((struct ifpppstatsreq *) data)->stats;
809 memset(psp, 0, sizeof(*psp));
810 psp->p = sc->sc_stats;
811 #if defined(VJC) && !defined(SL_NO_STATS)
812 if (sc->sc_comp) {
813 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
814 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
815 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
816 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
817 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
818 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
819 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
820 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
821 }
822 #endif /* VJC */
823 break;
824
825 #ifdef PPP_COMPRESS
826 case SIOCGPPPCSTATS:
827 pcp = &((struct ifpppcstatsreq *) data)->stats;
828 memset(pcp, 0, sizeof(*pcp));
829 if (sc->sc_xc_state != NULL)
830 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
831 if (sc->sc_rc_state != NULL)
832 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
833 break;
834 #endif /* PPP_COMPRESS */
835
836 default:
837 if ((error = ifioctl_common(&sc->sc_if, cmd, data)) == ENETRESET)
838 error = 0;
839 break;
840 }
841 splx(s);
842 return (error);
843 }
844
845 /*
846 * Queue a packet. Start transmission if not active.
847 * Packet is placed in Information field of PPP frame.
848 */
849 int
850 pppoutput(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
851 struct rtentry *rtp)
852 {
853 struct ppp_softc *sc = ifp->if_softc;
854 int protocol, address, control;
855 u_char *cp;
856 int s, error;
857 #ifdef INET
858 struct ip *ip;
859 #endif
860 struct ifqueue *ifq;
861 enum NPmode mode;
862 int len;
863 ALTQ_DECL(struct altq_pktattr pktattr;)
864
865 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
866 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
867 error = ENETDOWN; /* sort of */
868 goto bad;
869 }
870
871 IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family, &pktattr);
872
873 /*
874 * Compute PPP header.
875 */
876 m0->m_flags &= ~M_HIGHPRI;
877 switch (dst->sa_family) {
878 #ifdef INET
879 case AF_INET:
880 address = PPP_ALLSTATIONS;
881 control = PPP_UI;
882 protocol = PPP_IP;
883 mode = sc->sc_npmode[NP_IP];
884
885 /*
886 * If this packet has the "low delay" bit set in the IP header,
887 * put it on the fastq instead.
888 */
889 ip = mtod(m0, struct ip *);
890 if (ip->ip_tos & IPTOS_LOWDELAY)
891 m0->m_flags |= M_HIGHPRI;
892 break;
893 #endif
894 #ifdef INET6
895 case AF_INET6:
896 address = PPP_ALLSTATIONS; /*XXX*/
897 control = PPP_UI; /*XXX*/
898 protocol = PPP_IPV6;
899 mode = sc->sc_npmode[NP_IPV6];
900
901 #if 0 /* XXX flowinfo/traffic class, maybe? */
902 /*
903 * If this packet has the "low delay" bit set in the IP header,
904 * put it on the fastq instead.
905 */
906 ip = mtod(m0, struct ip *);
907 if (ip->ip_tos & IPTOS_LOWDELAY)
908 m0->m_flags |= M_HIGHPRI;
909 #endif
910 break;
911 #endif
912 case AF_UNSPEC:
913 address = PPP_ADDRESS(dst->sa_data);
914 control = PPP_CONTROL(dst->sa_data);
915 protocol = PPP_PROTOCOL(dst->sa_data);
916 mode = NPMODE_PASS;
917 break;
918 default:
919 printf("%s: af%d not supported\n", ifp->if_xname, dst->sa_family);
920 error = EAFNOSUPPORT;
921 goto bad;
922 }
923
924 /*
925 * Drop this packet, or return an error, if necessary.
926 */
927 if (mode == NPMODE_ERROR) {
928 error = ENETDOWN;
929 goto bad;
930 }
931 if (mode == NPMODE_DROP) {
932 error = 0;
933 goto bad;
934 }
935
936 /*
937 * Add PPP header.
938 */
939 M_PREPEND(m0, PPP_HDRLEN, M_DONTWAIT);
940 if (m0 == NULL) {
941 error = ENOBUFS;
942 goto bad;
943 }
944
945 cp = mtod(m0, u_char *);
946 *cp++ = address;
947 *cp++ = control;
948 *cp++ = protocol >> 8;
949 *cp++ = protocol & 0xff;
950
951 len = m_length(m0);
952
953 if (sc->sc_flags & SC_LOG_OUTPKT) {
954 printf("%s output: ", ifp->if_xname);
955 pppdumpm(m0);
956 }
957
958 if ((protocol & 0x8000) == 0) {
959 #ifdef PPP_FILTER
960 /*
961 * Apply the pass and active filters to the packet,
962 * but only if it is a data packet.
963 */
964 if (sc->sc_pass_filt_out.bf_insns != 0
965 && bpf_filter(sc->sc_pass_filt_out.bf_insns, (u_char *) m0,
966 len, 0) == 0) {
967 error = 0; /* drop this packet */
968 goto bad;
969 }
970
971 /*
972 * Update the time we sent the most recent packet.
973 */
974 if (sc->sc_active_filt_out.bf_insns == 0
975 || bpf_filter(sc->sc_active_filt_out.bf_insns, (u_char *) m0,
976 len, 0))
977 sc->sc_last_sent = time_second;
978 #else
979 /*
980 * Update the time we sent the most recent packet.
981 */
982 sc->sc_last_sent = time_second;
983 #endif /* PPP_FILTER */
984 }
985
986 /*
987 * See if bpf wants to look at the packet.
988 */
989 bpf_mtap(&sc->sc_if, m0);
990
991 /*
992 * Put the packet on the appropriate queue.
993 */
994 s = splnet();
995 if (mode == NPMODE_QUEUE) {
996 /* XXX we should limit the number of packets on this queue */
997 *sc->sc_npqtail = m0;
998 m0->m_nextpkt = NULL;
999 sc->sc_npqtail = &m0->m_nextpkt;
1000 } else {
1001 ifq = (m0->m_flags & M_HIGHPRI) ? &sc->sc_fastq : NULL;
1002 if ((error = ifq_enqueue2(&sc->sc_if, ifq, m0
1003 ALTQ_COMMA ALTQ_DECL(&pktattr))) != 0) {
1004 splx(s);
1005 sc->sc_if.if_oerrors++;
1006 sc->sc_stats.ppp_oerrors++;
1007 return (error);
1008 }
1009 ppp_restart(sc);
1010 }
1011 ifp->if_opackets++;
1012 ifp->if_obytes += len;
1013
1014 splx(s);
1015 return (0);
1016
1017 bad:
1018 m_freem(m0);
1019 return (error);
1020 }
1021
1022 /*
1023 * After a change in the NPmode for some NP, move packets from the
1024 * npqueue to the send queue or the fast queue as appropriate.
1025 * Should be called at splnet, since we muck with the queues.
1026 */
1027 static void
1028 ppp_requeue(struct ppp_softc *sc)
1029 {
1030 struct mbuf *m, **mpp;
1031 struct ifqueue *ifq;
1032 enum NPmode mode;
1033 int error;
1034
1035 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
1036 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
1037 case PPP_IP:
1038 mode = sc->sc_npmode[NP_IP];
1039 break;
1040 case PPP_IPV6:
1041 mode = sc->sc_npmode[NP_IPV6];
1042 break;
1043 default:
1044 mode = NPMODE_PASS;
1045 }
1046
1047 switch (mode) {
1048 case NPMODE_PASS:
1049 /*
1050 * This packet can now go on one of the queues to be sent.
1051 */
1052 *mpp = m->m_nextpkt;
1053 m->m_nextpkt = NULL;
1054 ifq = (m->m_flags & M_HIGHPRI) ? &sc->sc_fastq : NULL;
1055 if ((error = ifq_enqueue2(&sc->sc_if, ifq, m ALTQ_COMMA
1056 ALTQ_DECL(NULL))) != 0) {
1057 sc->sc_if.if_oerrors++;
1058 sc->sc_stats.ppp_oerrors++;
1059 }
1060 break;
1061
1062 case NPMODE_DROP:
1063 case NPMODE_ERROR:
1064 *mpp = m->m_nextpkt;
1065 m_freem(m);
1066 break;
1067
1068 case NPMODE_QUEUE:
1069 mpp = &m->m_nextpkt;
1070 break;
1071 }
1072 }
1073 sc->sc_npqtail = mpp;
1074 }
1075
1076 /*
1077 * Transmitter has finished outputting some stuff;
1078 * remember to call sc->sc_start later at splsoftnet.
1079 */
1080 void
1081 ppp_restart(struct ppp_softc *sc)
1082 {
1083 int s = splhigh(); /* XXX IMP ME HARDER */
1084
1085 sc->sc_flags &= ~SC_TBUSY;
1086 softint_schedule(sc->sc_si);
1087 splx(s);
1088 }
1089
1090 /*
1091 * Get a packet to send. This procedure is intended to be called at
1092 * splsoftnet, since it may involve time-consuming operations such as
1093 * applying VJ compression, packet compression, address/control and/or
1094 * protocol field compression to the packet.
1095 */
1096 struct mbuf *
1097 ppp_dequeue(struct ppp_softc *sc)
1098 {
1099 struct mbuf *m, *mp;
1100 u_char *cp;
1101 int address, control, protocol;
1102 int s;
1103
1104 /*
1105 * Grab a packet to send: first try the fast queue, then the
1106 * normal queue.
1107 */
1108 s = splnet();
1109 if (sc->sc_nfastq < sc->sc_maxfastq) {
1110 IF_DEQUEUE(&sc->sc_fastq, m);
1111 if (m != NULL)
1112 sc->sc_nfastq++;
1113 else
1114 IFQ_DEQUEUE(&sc->sc_if.if_snd, m);
1115 } else {
1116 sc->sc_nfastq = 0;
1117 IFQ_DEQUEUE(&sc->sc_if.if_snd, m);
1118 if (m == NULL) {
1119 IF_DEQUEUE(&sc->sc_fastq, m);
1120 if (m != NULL)
1121 sc->sc_nfastq++;
1122 }
1123 }
1124 splx(s);
1125
1126 if (m == NULL)
1127 return NULL;
1128
1129 ++sc->sc_stats.ppp_opackets;
1130
1131 /*
1132 * Extract the ppp header of the new packet.
1133 * The ppp header will be in one mbuf.
1134 */
1135 cp = mtod(m, u_char *);
1136 address = PPP_ADDRESS(cp);
1137 control = PPP_CONTROL(cp);
1138 protocol = PPP_PROTOCOL(cp);
1139
1140 switch (protocol) {
1141 case PPP_IP:
1142 #ifdef VJC
1143 /*
1144 * If the packet is a TCP/IP packet, see if we can compress it.
1145 */
1146 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
1147 struct ip *ip;
1148 int type;
1149
1150 mp = m;
1151 ip = (struct ip *) (cp + PPP_HDRLEN);
1152 if (mp->m_len <= PPP_HDRLEN) {
1153 mp = mp->m_next;
1154 if (mp == NULL)
1155 break;
1156 ip = mtod(mp, struct ip *);
1157 }
1158 /* this code assumes the IP/TCP header is in one non-shared mbuf */
1159 if (ip->ip_p == IPPROTO_TCP) {
1160 type = sl_compress_tcp(mp, ip, sc->sc_comp,
1161 !(sc->sc_flags & SC_NO_TCP_CCID));
1162 switch (type) {
1163 case TYPE_UNCOMPRESSED_TCP:
1164 protocol = PPP_VJC_UNCOMP;
1165 break;
1166 case TYPE_COMPRESSED_TCP:
1167 protocol = PPP_VJC_COMP;
1168 cp = mtod(m, u_char *);
1169 cp[0] = address; /* header has moved */
1170 cp[1] = control;
1171 cp[2] = 0;
1172 break;
1173 }
1174 cp[3] = protocol; /* update protocol in PPP header */
1175 }
1176 }
1177 #endif /* VJC */
1178 break;
1179
1180 #ifdef PPP_COMPRESS
1181 case PPP_CCP:
1182 ppp_ccp(sc, m, 0);
1183 break;
1184 #endif /* PPP_COMPRESS */
1185 }
1186
1187 #ifdef PPP_COMPRESS
1188 if (protocol != PPP_LCP && protocol != PPP_CCP
1189 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1190 struct mbuf *mcomp = NULL;
1191 int slen;
1192
1193 slen = 0;
1194 for (mp = m; mp != NULL; mp = mp->m_next)
1195 slen += mp->m_len;
1196 (*sc->sc_xcomp->compress)
1197 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
1198 if (mcomp != NULL) {
1199 if (sc->sc_flags & SC_CCP_UP) {
1200 /* Send the compressed packet instead of the original. */
1201 m_freem(m);
1202 m = mcomp;
1203 cp = mtod(m, u_char *);
1204 protocol = cp[3];
1205 } else {
1206 /* Can't transmit compressed packets until CCP is up. */
1207 m_freem(mcomp);
1208 }
1209 }
1210 }
1211 #endif /* PPP_COMPRESS */
1212
1213 /*
1214 * Compress the address/control and protocol, if possible.
1215 */
1216 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1217 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1218 protocol != PPP_LCP) {
1219 /* can compress address/control */
1220 m->m_data += 2;
1221 m->m_len -= 2;
1222 }
1223 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1224 /* can compress protocol */
1225 if (mtod(m, u_char *) == cp) {
1226 cp[2] = cp[1]; /* move address/control up */
1227 cp[1] = cp[0];
1228 }
1229 ++m->m_data;
1230 --m->m_len;
1231 }
1232
1233 return m;
1234 }
1235
1236 /*
1237 * Software interrupt routine, called at splsoftnet.
1238 */
1239 static void
1240 pppintr(void *arg)
1241 {
1242 struct ppp_softc *sc = arg;
1243 struct mbuf *m;
1244 int s;
1245
1246 mutex_enter(softnet_lock);
1247 if (!(sc->sc_flags & SC_TBUSY)
1248 && (IFQ_IS_EMPTY(&sc->sc_if.if_snd) == 0 || sc->sc_fastq.ifq_head
1249 || sc->sc_outm)) {
1250 s = splhigh(); /* XXX IMP ME HARDER */
1251 sc->sc_flags |= SC_TBUSY;
1252 splx(s);
1253 (*sc->sc_start)(sc);
1254 }
1255 for (;;) {
1256 s = splnet();
1257 IF_DEQUEUE(&sc->sc_rawq, m);
1258 splx(s);
1259 if (m == NULL)
1260 break;
1261 ppp_inproc(sc, m);
1262 }
1263 mutex_exit(softnet_lock);
1264 }
1265
1266 #ifdef PPP_COMPRESS
1267 /*
1268 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1269 * 0 if it is about to be transmitted.
1270 */
1271 static void
1272 ppp_ccp(struct ppp_softc *sc, struct mbuf *m, int rcvd)
1273 {
1274 u_char *dp, *ep;
1275 struct mbuf *mp;
1276 int slen, s;
1277
1278 /*
1279 * Get a pointer to the data after the PPP header.
1280 */
1281 if (m->m_len <= PPP_HDRLEN) {
1282 mp = m->m_next;
1283 if (mp == NULL)
1284 return;
1285 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1286 } else {
1287 mp = m;
1288 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1289 }
1290
1291 ep = mtod(mp, u_char *) + mp->m_len;
1292 if (dp + CCP_HDRLEN > ep)
1293 return;
1294 slen = CCP_LENGTH(dp);
1295 if (dp + slen > ep) {
1296 if (sc->sc_flags & SC_DEBUG)
1297 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1298 dp, slen, mtod(mp, u_char *), mp->m_len);
1299 return;
1300 }
1301
1302 switch (CCP_CODE(dp)) {
1303 case CCP_CONFREQ:
1304 case CCP_TERMREQ:
1305 case CCP_TERMACK:
1306 /* CCP must be going down - disable compression */
1307 if (sc->sc_flags & SC_CCP_UP) {
1308 s = splhigh(); /* XXX IMP ME HARDER */
1309 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1310 splx(s);
1311 }
1312 break;
1313
1314 case CCP_CONFACK:
1315 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1316 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1317 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1318 if (!rcvd) {
1319 /* we're agreeing to send compressed packets. */
1320 if (sc->sc_xc_state != NULL
1321 && (*sc->sc_xcomp->comp_init)
1322 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1323 sc->sc_unit, 0, sc->sc_flags & SC_DEBUG)) {
1324 s = splhigh(); /* XXX IMP ME HARDER */
1325 sc->sc_flags |= SC_COMP_RUN;
1326 splx(s);
1327 }
1328 } else {
1329 /* peer is agreeing to send compressed packets. */
1330 if (sc->sc_rc_state != NULL
1331 && (*sc->sc_rcomp->decomp_init)
1332 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1333 sc->sc_unit, 0, sc->sc_mru,
1334 sc->sc_flags & SC_DEBUG)) {
1335 s = splhigh(); /* XXX IMP ME HARDER */
1336 sc->sc_flags |= SC_DECOMP_RUN;
1337 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1338 splx(s);
1339 }
1340 }
1341 }
1342 break;
1343
1344 case CCP_RESETACK:
1345 if (sc->sc_flags & SC_CCP_UP) {
1346 if (!rcvd) {
1347 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1348 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1349 } else {
1350 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1351 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1352 s = splhigh(); /* XXX IMP ME HARDER */
1353 sc->sc_flags &= ~SC_DC_ERROR;
1354 splx(s);
1355 }
1356 }
1357 }
1358 break;
1359 }
1360 }
1361
1362 /*
1363 * CCP is down; free (de)compressor state if necessary.
1364 */
1365 static void
1366 ppp_ccp_closed(struct ppp_softc *sc)
1367 {
1368 if (sc->sc_xc_state) {
1369 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1370 ppp_compressor_rele(sc->sc_xcomp);
1371 sc->sc_xc_state = NULL;
1372 }
1373 if (sc->sc_rc_state) {
1374 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1375 ppp_compressor_rele(sc->sc_rcomp);
1376 sc->sc_rc_state = NULL;
1377 }
1378 }
1379 #endif /* PPP_COMPRESS */
1380
1381 /*
1382 * PPP packet input routine.
1383 * The caller has checked and removed the FCS and has inserted
1384 * the address/control bytes and the protocol high byte if they
1385 * were omitted.
1386 */
1387 void
1388 ppppktin(struct ppp_softc *sc, struct mbuf *m, int lost)
1389 {
1390 int s = splhigh(); /* XXX IMP ME HARDER */
1391
1392 if (lost)
1393 m->m_flags |= M_ERRMARK;
1394 IF_ENQUEUE(&sc->sc_rawq, m);
1395 softint_schedule(sc->sc_si);
1396 splx(s);
1397 }
1398
1399 /*
1400 * Process a received PPP packet, doing decompression as necessary.
1401 * Should be called at splsoftnet.
1402 */
1403 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1404 TYPE_UNCOMPRESSED_TCP)
1405
1406 static void
1407 ppp_inproc(struct ppp_softc *sc, struct mbuf *m)
1408 {
1409 struct ifnet *ifp = &sc->sc_if;
1410 struct ifqueue *inq;
1411 int s, ilen, proto, rv;
1412 u_char *cp, adrs, ctrl;
1413 struct mbuf *mp, *dmp = NULL;
1414 #ifdef VJC
1415 int xlen;
1416 u_char *iphdr;
1417 u_int hlen;
1418 #endif
1419
1420 sc->sc_stats.ppp_ipackets++;
1421
1422 if (sc->sc_flags & SC_LOG_INPKT) {
1423 ilen = 0;
1424 for (mp = m; mp != NULL; mp = mp->m_next)
1425 ilen += mp->m_len;
1426 printf("%s: got %d bytes\n", ifp->if_xname, ilen);
1427 pppdumpm(m);
1428 }
1429
1430 cp = mtod(m, u_char *);
1431 adrs = PPP_ADDRESS(cp);
1432 ctrl = PPP_CONTROL(cp);
1433 proto = PPP_PROTOCOL(cp);
1434
1435 if (m->m_flags & M_ERRMARK) {
1436 m->m_flags &= ~M_ERRMARK;
1437 s = splhigh(); /* XXX IMP ME HARDER */
1438 sc->sc_flags |= SC_VJ_RESET;
1439 splx(s);
1440 }
1441
1442 #ifdef PPP_COMPRESS
1443 /*
1444 * Decompress this packet if necessary, update the receiver's
1445 * dictionary, or take appropriate action on a CCP packet.
1446 */
1447 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1448 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1449 /* decompress this packet */
1450 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1451 if (rv == DECOMP_OK) {
1452 m_freem(m);
1453 if (dmp == NULL) {
1454 /* no error, but no decompressed packet produced */
1455 return;
1456 }
1457 m = dmp;
1458 cp = mtod(m, u_char *);
1459 proto = PPP_PROTOCOL(cp);
1460
1461 } else {
1462 /*
1463 * An error has occurred in decompression.
1464 * Pass the compressed packet up to pppd, which may take
1465 * CCP down or issue a Reset-Req.
1466 */
1467 if (sc->sc_flags & SC_DEBUG)
1468 printf("%s: decompress failed %d\n", ifp->if_xname, rv);
1469 s = splhigh(); /* XXX IMP ME HARDER */
1470 sc->sc_flags |= SC_VJ_RESET;
1471 if (rv == DECOMP_ERROR)
1472 sc->sc_flags |= SC_DC_ERROR;
1473 else
1474 sc->sc_flags |= SC_DC_FERROR;
1475 splx(s);
1476 }
1477
1478 } else {
1479 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1480 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1481 }
1482 if (proto == PPP_CCP) {
1483 ppp_ccp(sc, m, 1);
1484 }
1485 }
1486 #endif
1487
1488 ilen = 0;
1489 for (mp = m; mp != NULL; mp = mp->m_next)
1490 ilen += mp->m_len;
1491
1492 #ifdef VJC
1493 if (sc->sc_flags & SC_VJ_RESET) {
1494 /*
1495 * If we've missed a packet, we must toss subsequent compressed
1496 * packets which don't have an explicit connection ID.
1497 */
1498 if (sc->sc_comp)
1499 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1500 s = splhigh(); /* XXX IMP ME HARDER */
1501 sc->sc_flags &= ~SC_VJ_RESET;
1502 splx(s);
1503 }
1504
1505 /*
1506 * See if we have a VJ-compressed packet to uncompress.
1507 */
1508 if (proto == PPP_VJC_COMP) {
1509 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1510 goto bad;
1511
1512 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1513 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1514 sc->sc_comp, &iphdr, &hlen);
1515
1516 if (xlen <= 0) {
1517 if (sc->sc_flags & SC_DEBUG)
1518 printf("%s: VJ uncompress failed on type comp\n",
1519 ifp->if_xname);
1520 goto bad;
1521 }
1522
1523 /* Copy the PPP and IP headers into a new mbuf. */
1524 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1525 if (mp == NULL)
1526 goto bad;
1527 mp->m_len = 0;
1528 mp->m_next = NULL;
1529 if (hlen + PPP_HDRLEN > MHLEN) {
1530 MCLGET(mp, M_DONTWAIT);
1531 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1532 m_freem(mp);
1533 goto bad; /* lose if big headers and no clusters */
1534 }
1535 }
1536 cp = mtod(mp, u_char *);
1537 cp[0] = adrs;
1538 cp[1] = ctrl;
1539 cp[2] = 0;
1540 cp[3] = PPP_IP;
1541 proto = PPP_IP;
1542 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1543 mp->m_len = hlen + PPP_HDRLEN;
1544
1545 /*
1546 * Trim the PPP and VJ headers off the old mbuf
1547 * and stick the new and old mbufs together.
1548 */
1549 m->m_data += PPP_HDRLEN + xlen;
1550 m->m_len -= PPP_HDRLEN + xlen;
1551 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1552 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1553 mp->m_len += m->m_len;
1554 MFREE(m, mp->m_next);
1555 } else
1556 mp->m_next = m;
1557 m = mp;
1558 ilen += hlen - xlen;
1559
1560 } else if (proto == PPP_VJC_UNCOMP) {
1561 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1562 goto bad;
1563
1564 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1565 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1566 sc->sc_comp, &iphdr, &hlen);
1567
1568 if (xlen < 0) {
1569 if (sc->sc_flags & SC_DEBUG)
1570 printf("%s: VJ uncompress failed on type uncomp\n",
1571 ifp->if_xname);
1572 goto bad;
1573 }
1574
1575 proto = PPP_IP;
1576 cp[3] = PPP_IP;
1577 }
1578 #endif /* VJC */
1579
1580 /*
1581 * If the packet will fit in a header mbuf, don't waste a
1582 * whole cluster on it.
1583 */
1584 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1585 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1586 if (mp != NULL) {
1587 m_copydata(m, 0, ilen, mtod(mp, void *));
1588 m_freem(m);
1589 m = mp;
1590 m->m_len = ilen;
1591 }
1592 }
1593 m->m_pkthdr.len = ilen;
1594 m->m_pkthdr.rcvif = ifp;
1595
1596 if ((proto & 0x8000) == 0) {
1597 #ifdef PPP_FILTER
1598 /*
1599 * See whether we want to pass this packet, and
1600 * if it counts as link activity.
1601 */
1602 if (sc->sc_pass_filt_in.bf_insns != 0
1603 && bpf_filter(sc->sc_pass_filt_in.bf_insns, (u_char *) m,
1604 ilen, 0) == 0) {
1605 /* drop this packet */
1606 m_freem(m);
1607 return;
1608 }
1609 if (sc->sc_active_filt_in.bf_insns == 0
1610 || bpf_filter(sc->sc_active_filt_in.bf_insns, (u_char *) m,
1611 ilen, 0))
1612 sc->sc_last_recv = time_second;
1613 #else
1614 /*
1615 * Record the time that we received this packet.
1616 */
1617 sc->sc_last_recv = time_second;
1618 #endif /* PPP_FILTER */
1619 }
1620
1621 /* See if bpf wants to look at the packet. */
1622 bpf_mtap(&sc->sc_if, m);
1623
1624 rv = 0;
1625 switch (proto) {
1626 #ifdef INET
1627 case PPP_IP:
1628 /*
1629 * IP packet - take off the ppp header and pass it up to IP.
1630 */
1631 if ((ifp->if_flags & IFF_UP) == 0
1632 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1633 /* interface is down - drop the packet. */
1634 m_freem(m);
1635 return;
1636 }
1637 m->m_pkthdr.len -= PPP_HDRLEN;
1638 m->m_data += PPP_HDRLEN;
1639 m->m_len -= PPP_HDRLEN;
1640 #ifdef GATEWAY
1641 if (ipflow_fastforward(m))
1642 return;
1643 #endif
1644 schednetisr(NETISR_IP);
1645 inq = &ipintrq;
1646 break;
1647 #endif
1648
1649 #ifdef INET6
1650 case PPP_IPV6:
1651 /*
1652 * IPv6 packet - take off the ppp header and pass it up to IPv6.
1653 */
1654 if ((ifp->if_flags & IFF_UP) == 0
1655 || sc->sc_npmode[NP_IPV6] != NPMODE_PASS) {
1656 /* interface is down - drop the packet. */
1657 m_freem(m);
1658 return;
1659 }
1660 m->m_pkthdr.len -= PPP_HDRLEN;
1661 m->m_data += PPP_HDRLEN;
1662 m->m_len -= PPP_HDRLEN;
1663 #ifdef GATEWAY
1664 if (ip6flow_fastforward(m))
1665 return;
1666 #endif
1667 schednetisr(NETISR_IPV6);
1668 inq = &ip6intrq;
1669 break;
1670 #endif
1671
1672 default:
1673 /*
1674 * Some other protocol - place on input queue for read().
1675 */
1676 inq = &sc->sc_inq;
1677 rv = 1;
1678 break;
1679 }
1680
1681 /*
1682 * Put the packet on the appropriate input queue.
1683 */
1684 s = splnet();
1685 if (IF_QFULL(inq)) {
1686 IF_DROP(inq);
1687 splx(s);
1688 if (sc->sc_flags & SC_DEBUG)
1689 printf("%s: input queue full\n", ifp->if_xname);
1690 ifp->if_iqdrops++;
1691 goto bad;
1692 }
1693 IF_ENQUEUE(inq, m);
1694 splx(s);
1695 ifp->if_ipackets++;
1696 ifp->if_ibytes += ilen;
1697
1698 if (rv)
1699 (*sc->sc_ctlp)(sc);
1700
1701 return;
1702
1703 bad:
1704 m_freem(m);
1705 sc->sc_if.if_ierrors++;
1706 sc->sc_stats.ppp_ierrors++;
1707 }
1708
1709 #define MAX_DUMP_BYTES 128
1710
1711 static void
1712 pppdumpm(struct mbuf *m0)
1713 {
1714 char buf[3*MAX_DUMP_BYTES+4];
1715 char *bp = buf;
1716 struct mbuf *m;
1717
1718 for (m = m0; m; m = m->m_next) {
1719 int l = m->m_len;
1720 u_char *rptr = (u_char *)m->m_data;
1721
1722 while (l--) {
1723 if (bp > buf + sizeof(buf) - 4)
1724 goto done;
1725 *bp++ = hexdigits[*rptr >> 4]; /* convert byte to ascii hex */
1726 *bp++ = hexdigits[*rptr++ & 0xf];
1727 }
1728
1729 if (m->m_next) {
1730 if (bp > buf + sizeof(buf) - 3)
1731 goto done;
1732 *bp++ = '|';
1733 } else
1734 *bp++ = ' ';
1735 }
1736 done:
1737 if (m)
1738 *bp++ = '>';
1739 *bp = 0;
1740 printf("%s\n", buf);
1741 }
1742
1743 #ifdef ALTQ
1744 /*
1745 * a wrapper to transmit a packet from if_start since ALTQ uses
1746 * if_start to send a packet.
1747 */
1748 static void
1749 ppp_ifstart(struct ifnet *ifp)
1750 {
1751 struct ppp_softc *sc;
1752
1753 sc = ifp->if_softc;
1754 (*sc->sc_start)(sc);
1755 }
1756 #endif
1757
1758 static const struct ppp_known_compressor {
1759 uint8_t code;
1760 const char *module;
1761 } ppp_known_compressors[] = {
1762 { CI_DEFLATE, "ppp_deflate" },
1763 { CI_DEFLATE_DRAFT, "ppp_deflate" },
1764 { CI_BSD_COMPRESS, "ppp_bsdcomp" },
1765 { CI_MPPE, "ppp_mppe" },
1766 { 0, NULL }
1767 };
1768
1769 static int
1770 ppp_compressor_init(void)
1771 {
1772
1773 mutex_init(&ppp_compressors_mtx, MUTEX_DEFAULT, IPL_NONE);
1774 return 0;
1775 }
1776
1777 static void
1778 ppp_compressor_rele(struct compressor *cp)
1779 {
1780
1781 mutex_enter(&ppp_compressors_mtx);
1782 --cp->comp_refcnt;
1783 mutex_exit(&ppp_compressors_mtx);
1784 }
1785
1786 static struct compressor *
1787 ppp_get_compressor_noload(uint8_t ci, bool hold)
1788 {
1789 struct compressor *cp;
1790
1791 KASSERT(mutex_owned(&ppp_compressors_mtx));
1792 LIST_FOREACH(cp, &ppp_compressors, comp_list) {
1793 if (cp->compress_proto == ci) {
1794 if (hold)
1795 ++cp->comp_refcnt;
1796 return cp;
1797 }
1798 }
1799
1800 return NULL;
1801 }
1802
1803 static struct compressor *
1804 ppp_get_compressor(uint8_t ci)
1805 {
1806 struct compressor *cp = NULL;
1807 const struct ppp_known_compressor *pkc;
1808
1809 mutex_enter(&ppp_compressors_mtx);
1810 cp = ppp_get_compressor_noload(ci, true);
1811 mutex_exit(&ppp_compressors_mtx);
1812 if (cp != NULL)
1813 return cp;
1814
1815 kernconfig_lock();
1816 mutex_enter(&ppp_compressors_mtx);
1817 cp = ppp_get_compressor_noload(ci, true);
1818 mutex_exit(&ppp_compressors_mtx);
1819 if (cp == NULL) {
1820 /* Not found, so try to autoload a module */
1821 for (pkc = ppp_known_compressors; pkc->module != NULL; pkc++) {
1822 if (pkc->code == ci) {
1823 if (module_autoload(pkc->module,
1824 MODULE_CLASS_MISC) != 0)
1825 break;
1826 mutex_enter(&ppp_compressors_mtx);
1827 cp = ppp_get_compressor_noload(ci, true);
1828 mutex_exit(&ppp_compressors_mtx);
1829 break;
1830 }
1831 }
1832 }
1833 kernconfig_unlock();
1834
1835 return cp;
1836 }
1837
1838 int
1839 ppp_register_compressor(struct compressor *pc, size_t ncomp)
1840 {
1841 int error = 0;
1842 size_t i;
1843
1844 RUN_ONCE(&ppp_compressor_mtx_init, ppp_compressor_init);
1845
1846 mutex_enter(&ppp_compressors_mtx);
1847 for (i = 0; i < ncomp; i++) {
1848 if (ppp_get_compressor_noload(pc[i].compress_proto,
1849 false) != NULL)
1850 error = EEXIST;
1851 }
1852 if (!error) {
1853 for (i = 0; i < ncomp; i++) {
1854 pc[i].comp_refcnt = 0;
1855 LIST_INSERT_HEAD(&ppp_compressors, &pc[i], comp_list);
1856 }
1857 }
1858 mutex_exit(&ppp_compressors_mtx);
1859
1860 return error;
1861 }
1862
1863 int
1864 ppp_unregister_compressor(struct compressor *pc, size_t ncomp)
1865 {
1866 int error = 0;
1867 size_t i;
1868
1869 mutex_enter(&ppp_compressors_mtx);
1870 for (i = 0; i < ncomp; i++) {
1871 if (ppp_get_compressor_noload(pc[i].compress_proto,
1872 false) != &pc[i])
1873 error = ENOENT;
1874 else if (pc[i].comp_refcnt != 0)
1875 error = EBUSY;
1876 }
1877 if (!error) {
1878 for (i = 0; i < ncomp; i++) {
1879 LIST_REMOVE(&pc[i], comp_list);
1880 }
1881 }
1882 mutex_exit(&ppp_compressors_mtx);
1883
1884 return error;
1885 }
1886