1 | /* |
---|
2 | * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver. |
---|
3 | * |
---|
4 | * Copyright (c) 1989 Carnegie Mellon University. |
---|
5 | * All rights reserved. |
---|
6 | * |
---|
7 | * Redistribution and use in source and binary forms are permitted |
---|
8 | * provided that the above copyright notice and this paragraph are |
---|
9 | * duplicated in all such forms and that any documentation, |
---|
10 | * advertising materials, and other materials related to such |
---|
11 | * distribution and use acknowledge that the software was developed |
---|
12 | * by Carnegie Mellon University. The name of the |
---|
13 | * University may not be used to endorse or promote products derived |
---|
14 | * from this software without specific prior written permission. |
---|
15 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
---|
16 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
---|
17 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
---|
18 | * |
---|
19 | * Drew D. Perkins |
---|
20 | * Carnegie Mellon University |
---|
21 | * 4910 Forbes Ave. |
---|
22 | * Pittsburgh, PA 15213 |
---|
23 | * (412) 268-8576 |
---|
24 | * ddp@andrew.cmu.edu |
---|
25 | * |
---|
26 | * Based on: |
---|
27 | * @(#)if_sl.c 7.6.1.2 (Berkeley) 2/15/89 |
---|
28 | * |
---|
29 | * Copyright (c) 1987 Regents of the University of California. |
---|
30 | * All rights reserved. |
---|
31 | * |
---|
32 | * Redistribution and use in source and binary forms are permitted |
---|
33 | * provided that the above copyright notice and this paragraph are |
---|
34 | * duplicated in all such forms and that any documentation, |
---|
35 | * advertising materials, and other materials related to such |
---|
36 | * distribution and use acknowledge that the software was developed |
---|
37 | * by the University of California, Berkeley. The name of the |
---|
38 | * University may not be used to endorse or promote products derived |
---|
39 | * from this software without specific prior written permission. |
---|
40 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
---|
41 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
---|
42 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
---|
43 | * |
---|
44 | * Serial Line interface |
---|
45 | * |
---|
46 | * Rick Adams |
---|
47 | * Center for Seismic Studies |
---|
48 | * 1300 N 17th Street, Suite 1450 |
---|
49 | * Arlington, Virginia 22209 |
---|
50 | * (703)276-7900 |
---|
51 | * rick@seismo.ARPA |
---|
52 | * seismo!rick |
---|
53 | * |
---|
54 | * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris). |
---|
55 | * Converted to 4.3BSD Beta by Chris Torek. |
---|
56 | * Other changes made at Berkeley, based in part on code by Kirk Smith. |
---|
57 | * |
---|
58 | * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com) |
---|
59 | * Added VJ tcp header compression; more unified ioctls |
---|
60 | * |
---|
61 | * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au). |
---|
62 | * Cleaned up a lot of the mbuf-related code to fix bugs that |
---|
63 | * caused system crashes and packet corruption. Changed pppstart |
---|
64 | * so that it doesn't just give up with a collision if the whole |
---|
65 | * packet doesn't fit in the output ring buffer. |
---|
66 | * |
---|
67 | * Added priority queueing for interactive IP packets, following |
---|
68 | * the model of if_sl.c, plus hooks for bpf. |
---|
69 | * Paul Mackerras (paulus@cs.anu.edu.au). |
---|
70 | */ |
---|
71 | |
---|
72 | /* $Id$ */ |
---|
73 | /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */ |
---|
74 | /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */ |
---|
75 | |
---|
76 | #include "ppp.h" |
---|
77 | #if NPPP > 0 |
---|
78 | /* temporarily we switch off the compression */ |
---|
79 | |
---|
80 | #include <rtems/rtems_bsdnet.h> /* to get right defines */ |
---|
81 | |
---|
82 | #include <sys/param.h> |
---|
83 | #include <sys/systm.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 | #include <sys/time.h> |
---|
90 | #include <sys/malloc.h> |
---|
91 | |
---|
92 | #include <rtems/rtems_bsdnet.h> |
---|
93 | #include <net/if.h> |
---|
94 | #include <sys/errno.h> |
---|
95 | #include <net/if_types.h> |
---|
96 | #include <net/netisr.h> |
---|
97 | #include <net/route.h> |
---|
98 | #ifdef PPP_FILTER |
---|
99 | #include <net/bpf.h> |
---|
100 | #endif |
---|
101 | |
---|
102 | #ifdef INET |
---|
103 | #include <netinet/in.h> |
---|
104 | #include <netinet/in_systm.h> |
---|
105 | #include <netinet/in_var.h> |
---|
106 | #include <netinet/ip.h> |
---|
107 | #endif |
---|
108 | |
---|
109 | #include <bpfilter.h> |
---|
110 | #if NBPFILTER > 0 |
---|
111 | #include <net/bpf.h> |
---|
112 | #endif |
---|
113 | |
---|
114 | #ifdef VJC |
---|
115 | #include <net/pppcompress.h> |
---|
116 | #endif |
---|
117 | |
---|
118 | #include <net/ppp_defs.h> |
---|
119 | #include <net/if_ppp.h> |
---|
120 | #include <net/if_pppvar.h> |
---|
121 | #include <machine/cpu.h> |
---|
122 | |
---|
123 | #define splsoftnet splnet |
---|
124 | |
---|
125 | #ifndef NETISR_PPP |
---|
126 | /* This definition should be moved to net/netisr.h */ |
---|
127 | #define NETISR_PPP 26 /* PPP software interrupt */ |
---|
128 | #endif |
---|
129 | |
---|
130 | #ifdef PPP_COMPRESS |
---|
131 | #define PACKETPTR struct mbuf * |
---|
132 | #include <net/ppp-comp.h> |
---|
133 | #endif |
---|
134 | extern struct ifqueue ipintrq; |
---|
135 | static int pppsioctl __P((struct ifnet *, int, caddr_t)); |
---|
136 | static void ppp_requeue __P((struct ppp_softc *)); |
---|
137 | #ifdef PPP_COMPRESS |
---|
138 | static void ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd)); |
---|
139 | static void ppp_ccp_closed __P((struct ppp_softc *)); |
---|
140 | #endif |
---|
141 | static void ppp_inproc __P((struct ppp_softc *, struct mbuf *)); |
---|
142 | static void pppdumpm __P((struct mbuf *m0)); |
---|
143 | |
---|
144 | /* |
---|
145 | * Some useful mbuf macros not in mbuf.h. |
---|
146 | */ |
---|
147 | #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT) |
---|
148 | |
---|
149 | #define M_DATASTART(m) \ |
---|
150 | (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \ |
---|
151 | (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat) |
---|
152 | |
---|
153 | #define M_DATASIZE(m) \ |
---|
154 | (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \ |
---|
155 | (m)->m_flags & M_PKTHDR ? MHLEN: MLEN) |
---|
156 | |
---|
157 | /* |
---|
158 | * We steal two bits in the mbuf m_flags, to mark high-priority packets |
---|
159 | * for output, and received packets following lost/corrupted packets. |
---|
160 | */ |
---|
161 | #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */ |
---|
162 | #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */ |
---|
163 | |
---|
164 | |
---|
165 | #ifdef PPP_COMPRESS |
---|
166 | /* |
---|
167 | * List of compressors we know about. |
---|
168 | * We leave some space so maybe we can modload compressors. |
---|
169 | */ |
---|
170 | |
---|
171 | extern struct compressor ppp_bsd_compress; |
---|
172 | extern struct compressor ppp_deflate, ppp_deflate_draft; |
---|
173 | |
---|
174 | |
---|
175 | struct compressor *ppp_compressors[8] = { |
---|
176 | #if DO_BSD_COMPRESS |
---|
177 | &ppp_bsd_compress, |
---|
178 | #endif |
---|
179 | #if DO_DEFLATE |
---|
180 | &ppp_deflate, |
---|
181 | &ppp_deflate_draft, |
---|
182 | #endif |
---|
183 | NULL |
---|
184 | }; |
---|
185 | #endif /* PPP_COMPRESS */ |
---|
186 | static struct timeval ppp_time; |
---|
187 | |
---|
188 | TEXT_SET(pseudo_set, pppattach); |
---|
189 | |
---|
190 | /* |
---|
191 | * Called from boot code to establish ppp interfaces. |
---|
192 | */ |
---|
193 | int rtems_ppp_driver_attach (struct rtems_bsdnet_ifconfig *config, |
---|
194 | int attaching) |
---|
195 | { |
---|
196 | register struct ppp_softc *sc; |
---|
197 | register int i = 0; |
---|
198 | /* XXX unused in rtems |
---|
199 | extern void (*netisrs[])__P((void)); */ |
---|
200 | |
---|
201 | for (sc = ppp_softc; i < NPPP; sc++) { |
---|
202 | sc->sc_if.if_name = "ppp"; |
---|
203 | sc->sc_if.if_unit = i++; |
---|
204 | sc->sc_if.if_mtu = PPP_MTU; |
---|
205 | sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST; |
---|
206 | sc->sc_if.if_type = IFT_PPP; |
---|
207 | sc->sc_if.if_hdrlen = PPP_HDRLEN; |
---|
208 | sc->sc_if.if_ioctl = pppsioctl; |
---|
209 | sc->sc_if.if_output = pppoutput; |
---|
210 | sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN; |
---|
211 | sc->sc_inq.ifq_maxlen = IFQ_MAXLEN; |
---|
212 | sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN; |
---|
213 | sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN; |
---|
214 | if_attach(&sc->sc_if); |
---|
215 | #if NBPFILTER > 0 |
---|
216 | bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_PPP, PPP_HDRLEN); |
---|
217 | #endif |
---|
218 | } |
---|
219 | /* hardcoded in rtems_glue.c |
---|
220 | netisrs[NETISR_PPP] = pppintr; */ |
---|
221 | return 1; |
---|
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 | for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++) |
---|
234 | if (sc->sc_xfer == pid) { |
---|
235 | sc->sc_xfer = 0; |
---|
236 | return sc; |
---|
237 | } |
---|
238 | for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++) |
---|
239 | if (sc->sc_devp == NULL) |
---|
240 | break; |
---|
241 | if (nppp >= NPPP) |
---|
242 | return NULL; |
---|
243 | |
---|
244 | sc->sc_flags = 0; |
---|
245 | sc->sc_mru = PPP_MRU; |
---|
246 | sc->sc_relinq = NULL; |
---|
247 | bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats)); |
---|
248 | #ifdef VJC |
---|
249 | sc->sc_comp=malloc(sizeof(struct vjcompress),M_DEVBUF, M_NOWAIT); |
---|
250 | if (sc->sc_comp) |
---|
251 | vj_compress_init(sc->sc_comp, -1); |
---|
252 | #endif |
---|
253 | #ifdef PPP_COMPRESS |
---|
254 | sc->sc_xc_state = NULL; |
---|
255 | sc->sc_rc_state = NULL; |
---|
256 | #endif /* PPP_COMPRESS */ |
---|
257 | for (i = 0; i < NUM_NP; ++i) |
---|
258 | sc->sc_npmode[i] = NPMODE_ERROR; |
---|
259 | sc->sc_npqueue = NULL; |
---|
260 | sc->sc_npqtail = &sc->sc_npqueue; |
---|
261 | microtime(&ppp_time); |
---|
262 | sc->sc_last_sent = sc->sc_last_recv = ppp_time.tv_sec; |
---|
263 | |
---|
264 | return sc; |
---|
265 | } |
---|
266 | |
---|
267 | /* |
---|
268 | * Deallocate a ppp unit. Must be called at splsoftnet or higher. |
---|
269 | */ |
---|
270 | void |
---|
271 | pppdealloc(sc) |
---|
272 | struct ppp_softc *sc; |
---|
273 | { |
---|
274 | struct mbuf *m; |
---|
275 | |
---|
276 | if_down(&sc->sc_if); |
---|
277 | sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING); |
---|
278 | sc->sc_devp = NULL; |
---|
279 | sc->sc_xfer = 0; |
---|
280 | for (;;) { |
---|
281 | IF_DEQUEUE(&sc->sc_rawq, m); |
---|
282 | if (m == NULL) |
---|
283 | break; |
---|
284 | m_freem(m); |
---|
285 | } |
---|
286 | for (;;) { |
---|
287 | IF_DEQUEUE(&sc->sc_inq, m); |
---|
288 | if (m == NULL) |
---|
289 | break; |
---|
290 | m_freem(m); |
---|
291 | } |
---|
292 | for (;;) { |
---|
293 | IF_DEQUEUE(&sc->sc_fastq, m); |
---|
294 | if (m == NULL) |
---|
295 | break; |
---|
296 | m_freem(m); |
---|
297 | } |
---|
298 | while ((m = sc->sc_npqueue) != NULL) { |
---|
299 | sc->sc_npqueue = m->m_nextpkt; |
---|
300 | m_freem(m); |
---|
301 | } |
---|
302 | if (sc->sc_togo != NULL) { |
---|
303 | m_freem(sc->sc_togo); |
---|
304 | sc->sc_togo = NULL; |
---|
305 | } |
---|
306 | #ifdef PPP_COMPRESS |
---|
307 | ppp_ccp_closed(sc); |
---|
308 | sc->sc_xc_state = NULL; |
---|
309 | sc->sc_rc_state = NULL; |
---|
310 | #endif /* PPP_COMPRESS */ |
---|
311 | #ifdef PPP_FILTER |
---|
312 | if (sc->sc_pass_filt.bf_insns != 0) { |
---|
313 | free(sc->sc_pass_filt.bf_insns, M_DEVBUF); |
---|
314 | sc->sc_pass_filt.bf_insns = 0; |
---|
315 | sc->sc_pass_filt.bf_len = 0; |
---|
316 | } |
---|
317 | if (sc->sc_active_filt.bf_insns != 0) { |
---|
318 | free(sc->sc_active_filt.bf_insns, M_DEVBUF); |
---|
319 | sc->sc_active_filt.bf_insns = 0; |
---|
320 | sc->sc_active_filt.bf_len = 0; |
---|
321 | } |
---|
322 | #endif /* PPP_FILTER */ |
---|
323 | #ifdef VJC |
---|
324 | if (sc->sc_comp != 0) { |
---|
325 | free(sc->sc_comp, M_DEVBUF); |
---|
326 | sc->sc_comp = 0; |
---|
327 | } |
---|
328 | #endif |
---|
329 | } |
---|
330 | |
---|
331 | /* |
---|
332 | * Ioctl routine for generic ppp devices. |
---|
333 | */ |
---|
334 | int |
---|
335 | pppioctl(sc, cmd, data, flag, p) |
---|
336 | struct ppp_softc *sc; |
---|
337 | int cmd; |
---|
338 | caddr_t data; |
---|
339 | int flag; |
---|
340 | struct proc *p; |
---|
341 | { |
---|
342 | int s, flags, mru, npx; |
---|
343 | struct npioctl *npi; |
---|
344 | time_t t; |
---|
345 | #ifdef PPP_FILTER |
---|
346 | int error; |
---|
347 | struct bpf_program *bp, *nbp; |
---|
348 | struct bpf_insn *newcode, *oldcode; |
---|
349 | int newcodelen; |
---|
350 | #endif /* PPP_FILTER */ |
---|
351 | #ifdef PPP_COMPRESS |
---|
352 | int nb; |
---|
353 | struct ppp_option_data *odp; |
---|
354 | struct compressor **cp; |
---|
355 | u_char ccp_option[CCP_MAX_OPTION_LENGTH]; |
---|
356 | #endif |
---|
357 | |
---|
358 | switch (cmd) { |
---|
359 | case FIONREAD: |
---|
360 | *(int *)data = sc->sc_inq.ifq_len; |
---|
361 | break; |
---|
362 | |
---|
363 | case PPPIOCGUNIT: |
---|
364 | *(int *)data = sc->sc_if.if_unit; |
---|
365 | break; |
---|
366 | |
---|
367 | case PPPIOCGFLAGS: |
---|
368 | *(u_int *)data = sc->sc_flags; |
---|
369 | break; |
---|
370 | |
---|
371 | case PPPIOCSFLAGS: |
---|
372 | flags = *(int *)data & SC_MASK; |
---|
373 | s = splsoftnet(); |
---|
374 | #ifdef PPP_COMPRESS |
---|
375 | if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN)) |
---|
376 | ppp_ccp_closed(sc); |
---|
377 | #endif |
---|
378 | splimp(); |
---|
379 | sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags; |
---|
380 | splx(s); |
---|
381 | break; |
---|
382 | |
---|
383 | case PPPIOCSMRU: |
---|
384 | mru = *(int *)data; |
---|
385 | if (mru >= PPP_MRU && mru <= PPP_MAXMRU) |
---|
386 | sc->sc_mru = mru; |
---|
387 | break; |
---|
388 | |
---|
389 | case PPPIOCGMRU: |
---|
390 | *(int *)data = sc->sc_mru; |
---|
391 | break; |
---|
392 | |
---|
393 | #ifdef VJC |
---|
394 | case PPPIOCSMAXCID: |
---|
395 | if (sc->sc_comp) { |
---|
396 | s = splsoftnet(); |
---|
397 | vj_compress_init(sc->sc_comp, *(int *)data); |
---|
398 | splx(s); |
---|
399 | } |
---|
400 | break; |
---|
401 | #endif |
---|
402 | |
---|
403 | case PPPIOCXFERUNIT: |
---|
404 | sc->sc_xfer = 0; /* Always root p->p_pid;*/ |
---|
405 | break; |
---|
406 | |
---|
407 | #ifdef PPP_COMPRESS |
---|
408 | case PPPIOCSCOMPRESS: |
---|
409 | odp = (struct ppp_option_data *) data; |
---|
410 | nb = odp->length; |
---|
411 | if (nb > sizeof(ccp_option)) |
---|
412 | nb = sizeof(ccp_option); |
---|
413 | if ((error = copyin(odp->ptr, ccp_option, nb)) != 0) |
---|
414 | return (error); |
---|
415 | if (ccp_option[1] < 2) /* preliminary check on the length byte */ |
---|
416 | return (EINVAL); |
---|
417 | for (cp = ppp_compressors; *cp != NULL; ++cp) |
---|
418 | if ((*cp)->compress_proto == ccp_option[0]) { |
---|
419 | /* |
---|
420 | * Found a handler for the protocol - try to allocate |
---|
421 | * a compressor or decompressor. |
---|
422 | */ |
---|
423 | error = 0; |
---|
424 | if (odp->transmit) { |
---|
425 | s = splsoftnet(); |
---|
426 | if (sc->sc_xc_state != NULL) |
---|
427 | (*sc->sc_xcomp->comp_free)(sc->sc_xc_state); |
---|
428 | sc->sc_xcomp = *cp; |
---|
429 | sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb); |
---|
430 | if (sc->sc_xc_state == NULL) { |
---|
431 | if (sc->sc_flags & SC_DEBUG) |
---|
432 | printf("ppp%d: comp_alloc failed\n", |
---|
433 | sc->sc_if.if_unit); |
---|
434 | error = ENOBUFS; |
---|
435 | } |
---|
436 | splimp(); |
---|
437 | sc->sc_flags &= ~SC_COMP_RUN; |
---|
438 | splx(s); |
---|
439 | } else { |
---|
440 | s = splsoftnet(); |
---|
441 | if (sc->sc_rc_state != NULL) |
---|
442 | (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state); |
---|
443 | sc->sc_rcomp = *cp; |
---|
444 | sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb); |
---|
445 | if (sc->sc_rc_state == NULL) { |
---|
446 | if (sc->sc_flags & SC_DEBUG) |
---|
447 | printf("ppp%d: decomp_alloc failed\n", |
---|
448 | sc->sc_if.if_unit); |
---|
449 | error = ENOBUFS; |
---|
450 | } |
---|
451 | splimp(); |
---|
452 | sc->sc_flags &= ~SC_DECOMP_RUN; |
---|
453 | splx(s); |
---|
454 | } |
---|
455 | return (error); |
---|
456 | } |
---|
457 | if (sc->sc_flags & SC_DEBUG) |
---|
458 | printf("ppp%d: no compressor for [%x %x %x], %x\n", |
---|
459 | sc->sc_if.if_unit, ccp_option[0], ccp_option[1], |
---|
460 | ccp_option[2], nb); |
---|
461 | return (EINVAL); /* no handler found */ |
---|
462 | #endif /* PPP_COMPRESS */ |
---|
463 | |
---|
464 | case PPPIOCGNPMODE: |
---|
465 | case PPPIOCSNPMODE: |
---|
466 | npi = (struct npioctl *) data; |
---|
467 | switch (npi->protocol) { |
---|
468 | case PPP_IP: |
---|
469 | npx = NP_IP; |
---|
470 | break; |
---|
471 | default: |
---|
472 | return EINVAL; |
---|
473 | } |
---|
474 | if (cmd == PPPIOCGNPMODE) { |
---|
475 | npi->mode = sc->sc_npmode[npx]; |
---|
476 | } else { |
---|
477 | if (npi->mode != sc->sc_npmode[npx]) { |
---|
478 | s = splsoftnet(); |
---|
479 | sc->sc_npmode[npx] = npi->mode; |
---|
480 | if (npi->mode != NPMODE_QUEUE) { |
---|
481 | ppp_requeue(sc); |
---|
482 | (*sc->sc_start)(sc); |
---|
483 | } |
---|
484 | splx(s); |
---|
485 | } |
---|
486 | } |
---|
487 | break; |
---|
488 | |
---|
489 | case PPPIOCGIDLE: |
---|
490 | s = splsoftnet(); |
---|
491 | microtime(&ppp_time); |
---|
492 | t = ppp_time.tv_sec; |
---|
493 | ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent; |
---|
494 | ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv; |
---|
495 | splx(s); |
---|
496 | break; |
---|
497 | |
---|
498 | #ifdef PPP_FILTER |
---|
499 | case PPPIOCSPASS: |
---|
500 | case PPPIOCSACTIVE: |
---|
501 | nbp = (struct bpf_program *) data; |
---|
502 | if ((unsigned) nbp->bf_len > BPF_MAXINSNS) |
---|
503 | return EINVAL; |
---|
504 | newcodelen = nbp->bf_len * sizeof(struct bpf_insn); |
---|
505 | if (newcodelen != 0) { |
---|
506 | newcode=(struct bpf_insn *) malloc (newcodelen, M_DEVBUF, M_WAITOK); |
---|
507 | if (newcode == 0) { |
---|
508 | return EINVAL; /* or sumpin */ |
---|
509 | } |
---|
510 | if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode, |
---|
511 | newcodelen)) != 0) { |
---|
512 | free(newcode, M_DEVBUF); |
---|
513 | return error; |
---|
514 | } |
---|
515 | if (!bpf_validate(newcode, nbp->bf_len)) { |
---|
516 | free(newcode, M_DEVBUF); |
---|
517 | return EINVAL; |
---|
518 | } |
---|
519 | } else |
---|
520 | newcode = 0; |
---|
521 | bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt; |
---|
522 | oldcode = bp->bf_insns; |
---|
523 | s = splimp(); |
---|
524 | bp->bf_len = nbp->bf_len; |
---|
525 | bp->bf_insns = newcode; |
---|
526 | splx(s); |
---|
527 | if (oldcode != 0) |
---|
528 | free(oldcode, M_DEVBUF); |
---|
529 | break; |
---|
530 | #endif |
---|
531 | |
---|
532 | default: |
---|
533 | return (-1); |
---|
534 | } |
---|
535 | return (0); |
---|
536 | } |
---|
537 | |
---|
538 | /* |
---|
539 | * Process an ioctl request to the ppp network interface. |
---|
540 | */ |
---|
541 | static int |
---|
542 | pppsioctl(ifp, cmd, data) |
---|
543 | register struct ifnet *ifp; |
---|
544 | int cmd; |
---|
545 | caddr_t data; |
---|
546 | { |
---|
547 | /* struct proc *p = curproc; *//* XXX */ |
---|
548 | register struct ppp_softc *sc = &ppp_softc[ifp->if_unit]; |
---|
549 | register struct ifaddr *ifa = (struct ifaddr *)data; |
---|
550 | register struct ifreq *ifr = (struct ifreq *)data; |
---|
551 | struct ppp_stats *psp; |
---|
552 | #ifdef PPP_COMPRESS |
---|
553 | struct ppp_comp_stats *pcp; |
---|
554 | #endif |
---|
555 | int s = splimp(), error = 0; |
---|
556 | |
---|
557 | switch (cmd) { |
---|
558 | case SIOCSIFFLAGS: |
---|
559 | if ((ifp->if_flags & IFF_RUNNING) == 0) |
---|
560 | ifp->if_flags &= ~IFF_UP; |
---|
561 | break; |
---|
562 | |
---|
563 | case SIOCSIFADDR: |
---|
564 | if (ifa->ifa_addr->sa_family != AF_INET) |
---|
565 | error = EAFNOSUPPORT; |
---|
566 | break; |
---|
567 | |
---|
568 | case SIOCSIFDSTADDR: |
---|
569 | if (ifa->ifa_addr->sa_family != AF_INET) |
---|
570 | error = EAFNOSUPPORT; |
---|
571 | break; |
---|
572 | |
---|
573 | case SIOCSIFMTU: |
---|
574 | sc->sc_if.if_mtu = ifr->ifr_mtu; |
---|
575 | break; |
---|
576 | |
---|
577 | case SIOCGIFMTU: |
---|
578 | ifr->ifr_mtu = sc->sc_if.if_mtu; |
---|
579 | break; |
---|
580 | |
---|
581 | case SIOCADDMULTI: |
---|
582 | case SIOCDELMULTI: |
---|
583 | if (ifr == 0) { |
---|
584 | error = EAFNOSUPPORT; |
---|
585 | break; |
---|
586 | } |
---|
587 | switch(ifr->ifr_addr.sa_family) { |
---|
588 | #ifdef INET |
---|
589 | case AF_INET: |
---|
590 | break; |
---|
591 | #endif |
---|
592 | default: |
---|
593 | error = EAFNOSUPPORT; |
---|
594 | break; |
---|
595 | } |
---|
596 | break; |
---|
597 | case SIO_RTEMS_SHOW_STATS: |
---|
598 | printf (" MRU:%-8lu", sc->sc_mru); |
---|
599 | printf (" Bytes received:%-8lu", sc->sc_stats.ppp_ibytes); |
---|
600 | printf (" Packets received:%-8lu", sc->sc_stats.ppp_ipackets); |
---|
601 | printf (" Receive errors:%-8lu\n", sc->sc_stats.ppp_ierrors); |
---|
602 | printf (" Bytes sent:%-8lu", sc->sc_stats.ppp_obytes); |
---|
603 | printf (" Packets sent:%-8lu", sc->sc_stats.ppp_opackets); |
---|
604 | printf (" Transmit errors:%-8lu\n", sc->sc_stats.ppp_oerrors); |
---|
605 | |
---|
606 | break; |
---|
607 | |
---|
608 | |
---|
609 | case SIOCGPPPSTATS: |
---|
610 | psp = &((struct ifpppstatsreq *) data)->stats; |
---|
611 | bzero(psp, sizeof(*psp)); |
---|
612 | psp->p = sc->sc_stats; |
---|
613 | #if defined(VJC) && !defined(SL_NO_STATS) |
---|
614 | if (sc->sc_comp) { |
---|
615 | psp->vj.vjs_packets = sc->sc_comp->sls_packets; |
---|
616 | psp->vj.vjs_compressed = sc->sc_comp->sls_compressed; |
---|
617 | psp->vj.vjs_searches = sc->sc_comp->sls_searches; |
---|
618 | psp->vj.vjs_misses = sc->sc_comp->sls_misses; |
---|
619 | psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin; |
---|
620 | psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin; |
---|
621 | psp->vj.vjs_errorin = sc->sc_comp->sls_errorin; |
---|
622 | psp->vj.vjs_tossed = sc->sc_comp->sls_tossed; |
---|
623 | } |
---|
624 | #endif /* VJC */ |
---|
625 | break; |
---|
626 | |
---|
627 | #ifdef PPP_COMPRESS |
---|
628 | case SIOCGPPPCSTATS: |
---|
629 | pcp = &((struct ifpppcstatsreq *) data)->stats; |
---|
630 | bzero(pcp, sizeof(*pcp)); |
---|
631 | if (sc->sc_xc_state != NULL) |
---|
632 | (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c); |
---|
633 | if (sc->sc_rc_state != NULL) |
---|
634 | (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d); |
---|
635 | break; |
---|
636 | #endif /* PPP_COMPRESS */ |
---|
637 | |
---|
638 | default: |
---|
639 | error = EINVAL; |
---|
640 | } |
---|
641 | splx(s); |
---|
642 | return (error); |
---|
643 | } |
---|
644 | |
---|
645 | /* |
---|
646 | * Queue a packet. Start transmission if not active. |
---|
647 | * Packet is placed in Information field of PPP frame. |
---|
648 | */ |
---|
649 | int |
---|
650 | pppoutput(ifp, m0, dst, rtp) |
---|
651 | struct ifnet *ifp; |
---|
652 | struct mbuf *m0; |
---|
653 | struct sockaddr *dst; |
---|
654 | struct rtentry *rtp; |
---|
655 | { |
---|
656 | register struct ppp_softc *sc = &ppp_softc[ifp->if_unit]; |
---|
657 | int protocol, address, control; |
---|
658 | u_char *cp; |
---|
659 | int s, error; |
---|
660 | struct ip *ip; |
---|
661 | struct ifqueue *ifq; |
---|
662 | enum NPmode mode; |
---|
663 | int len; |
---|
664 | struct mbuf *m; |
---|
665 | if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0 |
---|
666 | || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) { |
---|
667 | error = ENETDOWN; /* sort of */ |
---|
668 | goto bad; |
---|
669 | } |
---|
670 | |
---|
671 | /* |
---|
672 | * Compute PPP header. |
---|
673 | */ |
---|
674 | m0->m_flags &= ~M_HIGHPRI; |
---|
675 | switch (dst->sa_family) { |
---|
676 | #ifdef INET |
---|
677 | case AF_INET: |
---|
678 | address = PPP_ALLSTATIONS; |
---|
679 | control = PPP_UI; |
---|
680 | protocol = PPP_IP; |
---|
681 | mode = sc->sc_npmode[NP_IP]; |
---|
682 | |
---|
683 | /* |
---|
684 | * If this packet has the "low delay" bit set in the IP header, |
---|
685 | * put it on the fastq instead. |
---|
686 | */ |
---|
687 | ip = mtod(m0, struct ip *); |
---|
688 | if (ip->ip_tos & IPTOS_LOWDELAY) |
---|
689 | m0->m_flags |= M_HIGHPRI; |
---|
690 | break; |
---|
691 | #endif |
---|
692 | case AF_UNSPEC: |
---|
693 | address = PPP_ADDRESS(dst->sa_data); |
---|
694 | control = PPP_CONTROL(dst->sa_data); |
---|
695 | protocol = PPP_PROTOCOL(dst->sa_data); |
---|
696 | mode = NPMODE_PASS; |
---|
697 | break; |
---|
698 | default: |
---|
699 | printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family); |
---|
700 | error = EAFNOSUPPORT; |
---|
701 | goto bad; |
---|
702 | } |
---|
703 | |
---|
704 | /* |
---|
705 | * Drop this packet, or return an error, if necessary. |
---|
706 | */ |
---|
707 | if (mode == NPMODE_ERROR) { |
---|
708 | error = ENETDOWN; |
---|
709 | goto bad; |
---|
710 | } |
---|
711 | if (mode == NPMODE_DROP) { |
---|
712 | error = 0; |
---|
713 | goto bad; |
---|
714 | } |
---|
715 | |
---|
716 | /* |
---|
717 | * Add PPP header. If no space in first mbuf, allocate another. |
---|
718 | * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.) |
---|
719 | */ |
---|
720 | if (M_LEADINGSPACE(m0) < PPP_HDRLEN) { |
---|
721 | m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT); |
---|
722 | if (m0 == 0) { |
---|
723 | error = ENOBUFS; |
---|
724 | goto bad; |
---|
725 | } |
---|
726 | m0->m_len = 0; |
---|
727 | } else |
---|
728 | m0->m_data -= PPP_HDRLEN; |
---|
729 | |
---|
730 | cp = mtod(m0, u_char *); |
---|
731 | *cp++ = address; |
---|
732 | *cp++ = control; |
---|
733 | *cp++ = protocol >> 8; |
---|
734 | *cp++ = protocol & 0xff; |
---|
735 | m0->m_len += PPP_HDRLEN; |
---|
736 | |
---|
737 | len = 0; |
---|
738 | for (m = m0; m != 0; m = m->m_next) |
---|
739 | len += m->m_len; |
---|
740 | |
---|
741 | if (sc->sc_flags & SC_LOG_OUTPKT) { |
---|
742 | printf("ppp%d output: ", ifp->if_unit); |
---|
743 | pppdumpm(m0); |
---|
744 | } |
---|
745 | |
---|
746 | if ((protocol & 0x8000) == 0) { |
---|
747 | #ifdef PPP_FILTER |
---|
748 | /* |
---|
749 | * Apply the pass and active filters to the packet, |
---|
750 | * but only if it is a data packet. |
---|
751 | */ |
---|
752 | *mtod(m0, u_char *) = 1; /* indicates outbound */ |
---|
753 | if (sc->sc_pass_filt.bf_insns != 0 |
---|
754 | && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0, |
---|
755 | len, 0) == 0) { |
---|
756 | error = 0; /* drop this packet */ |
---|
757 | goto bad; |
---|
758 | } |
---|
759 | |
---|
760 | /* |
---|
761 | * Update the time we sent the most recent packet. |
---|
762 | */ |
---|
763 | if (sc->sc_active_filt.bf_insns == 0 |
---|
764 | || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0)) |
---|
765 | { |
---|
766 | microtime(&ppp_time); |
---|
767 | sc->sc_last_sent = ppp_time.tv_sec; |
---|
768 | } |
---|
769 | |
---|
770 | *mtod(m0, u_char *) = address; |
---|
771 | #else |
---|
772 | /* |
---|
773 | * Update the time we sent the most recent data packet. |
---|
774 | */ |
---|
775 | microtime(&ppp_time); |
---|
776 | sc->sc_last_sent = ppp_time.tv_sec; |
---|
777 | #endif /* PPP_FILTER */ |
---|
778 | } |
---|
779 | |
---|
780 | #if NBPFILTER > 0 |
---|
781 | /* |
---|
782 | * See if bpf wants to look at the packet. |
---|
783 | */ |
---|
784 | if (sc->sc_bpf) |
---|
785 | bpf_mtap(sc->sc_bpf, m0); |
---|
786 | #endif |
---|
787 | |
---|
788 | /* |
---|
789 | * Put the packet on the appropriate queue. |
---|
790 | */ |
---|
791 | if (mode == NPMODE_QUEUE) { |
---|
792 | /* XXX we should limit the number of packets on this queue */ |
---|
793 | *sc->sc_npqtail = m0; |
---|
794 | m0->m_nextpkt = NULL; |
---|
795 | sc->sc_npqtail = &m0->m_nextpkt; |
---|
796 | } else { |
---|
797 | ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd; |
---|
798 | if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) { |
---|
799 | IF_DROP(ifq); |
---|
800 | splx(s); |
---|
801 | sc->sc_if.if_oerrors++; |
---|
802 | sc->sc_stats.ppp_oerrors++; |
---|
803 | error = ENOBUFS; |
---|
804 | goto bad; |
---|
805 | } |
---|
806 | IF_ENQUEUE(ifq, m0); |
---|
807 | (*sc->sc_start)(sc); |
---|
808 | } |
---|
809 | microtime(&ppp_time); |
---|
810 | ifp->if_lastchange = ppp_time; |
---|
811 | ifp->if_opackets++; |
---|
812 | ifp->if_obytes += len; |
---|
813 | |
---|
814 | return (0); |
---|
815 | |
---|
816 | bad: |
---|
817 | m_freem(m0); |
---|
818 | return (error); |
---|
819 | } |
---|
820 | |
---|
821 | /* |
---|
822 | * After a change in the NPmode for some NP, move packets from the |
---|
823 | * npqueue to the send queue or the fast queue as appropriate. |
---|
824 | * Should be called at splsoftnet. |
---|
825 | */ |
---|
826 | static void |
---|
827 | ppp_requeue(sc) |
---|
828 | struct ppp_softc *sc; |
---|
829 | { |
---|
830 | struct mbuf *m, **mpp; |
---|
831 | struct ifqueue *ifq; |
---|
832 | enum NPmode mode; |
---|
833 | |
---|
834 | for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) { |
---|
835 | switch (PPP_PROTOCOL(mtod(m, u_char *))) { |
---|
836 | case PPP_IP: |
---|
837 | mode = sc->sc_npmode[NP_IP]; |
---|
838 | break; |
---|
839 | default: |
---|
840 | mode = NPMODE_PASS; |
---|
841 | } |
---|
842 | |
---|
843 | switch (mode) { |
---|
844 | case NPMODE_PASS: |
---|
845 | /* |
---|
846 | * This packet can now go on one of the queues to be sent. |
---|
847 | */ |
---|
848 | *mpp = m->m_nextpkt; |
---|
849 | m->m_nextpkt = NULL; |
---|
850 | ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd; |
---|
851 | if (IF_QFULL(ifq)) { |
---|
852 | IF_DROP(ifq); |
---|
853 | sc->sc_if.if_oerrors++; |
---|
854 | sc->sc_stats.ppp_oerrors++; |
---|
855 | } else |
---|
856 | IF_ENQUEUE(ifq, m); |
---|
857 | break; |
---|
858 | |
---|
859 | case NPMODE_DROP: |
---|
860 | case NPMODE_ERROR: |
---|
861 | *mpp = m->m_nextpkt; |
---|
862 | m_freem(m); |
---|
863 | break; |
---|
864 | |
---|
865 | case NPMODE_QUEUE: |
---|
866 | mpp = &m->m_nextpkt; |
---|
867 | break; |
---|
868 | } |
---|
869 | } |
---|
870 | sc->sc_npqtail = mpp; |
---|
871 | } |
---|
872 | |
---|
873 | /* |
---|
874 | * Transmitter has finished outputting some stuff; |
---|
875 | * remember to call sc->sc_start later at splsoftnet. |
---|
876 | */ |
---|
877 | void |
---|
878 | ppp_restart(sc) |
---|
879 | struct ppp_softc *sc; |
---|
880 | { |
---|
881 | |
---|
882 | sc->sc_flags &= ~SC_TBUSY; |
---|
883 | /* schednetisr(NETISR_PPP); |
---|
884 | */} |
---|
885 | |
---|
886 | /* |
---|
887 | * Get a packet to send. This procedure is intended to be called at |
---|
888 | * splsoftnet, since it may involve time-consuming operations such as |
---|
889 | * applying VJ compression, packet compression, address/control and/or |
---|
890 | * protocol field compression to the packet. |
---|
891 | */ |
---|
892 | struct mbuf * |
---|
893 | ppp_dequeue(sc) |
---|
894 | struct ppp_softc *sc; |
---|
895 | { |
---|
896 | struct mbuf *m, *mp; |
---|
897 | u_char *cp; |
---|
898 | int address, control, protocol; |
---|
899 | |
---|
900 | /* |
---|
901 | * Grab a packet to send: first try the fast queue, then the |
---|
902 | * normal queue. |
---|
903 | */ |
---|
904 | IF_DEQUEUE(&sc->sc_fastq, m); |
---|
905 | if (m == NULL) |
---|
906 | IF_DEQUEUE(&sc->sc_if.if_snd, m); |
---|
907 | if (m == NULL) |
---|
908 | return NULL; |
---|
909 | |
---|
910 | ++sc->sc_stats.ppp_opackets; |
---|
911 | |
---|
912 | /* |
---|
913 | * Extract the ppp header of the new packet. |
---|
914 | * The ppp header will be in one mbuf. |
---|
915 | */ |
---|
916 | cp = mtod(m, u_char *); |
---|
917 | address = PPP_ADDRESS(cp); |
---|
918 | control = PPP_CONTROL(cp); |
---|
919 | protocol = PPP_PROTOCOL(cp); |
---|
920 | |
---|
921 | switch (protocol) { |
---|
922 | case PPP_IP: |
---|
923 | #ifdef VJC |
---|
924 | /* |
---|
925 | * If the packet is a TCP/IP packet, see if we can compress it. |
---|
926 | */ |
---|
927 | if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) { |
---|
928 | struct ip *ip; |
---|
929 | int type; |
---|
930 | |
---|
931 | mp = m; |
---|
932 | ip = (struct ip *) (cp + PPP_HDRLEN); |
---|
933 | if (mp->m_len <= PPP_HDRLEN) { |
---|
934 | mp = mp->m_next; |
---|
935 | if (mp == NULL) |
---|
936 | break; |
---|
937 | ip = mtod(mp, struct ip *); |
---|
938 | } |
---|
939 | /* this code assumes the IP/TCP header is in one non-shared mbuf */ |
---|
940 | if (ip->ip_p == IPPROTO_TCP) { |
---|
941 | type = vj_compress_tcp(mp, ip, sc->sc_comp, |
---|
942 | !(sc->sc_flags & SC_NO_TCP_CCID)); |
---|
943 | switch (type) { |
---|
944 | case TYPE_UNCOMPRESSED_TCP: |
---|
945 | protocol = PPP_VJC_UNCOMP; |
---|
946 | break; |
---|
947 | case TYPE_COMPRESSED_TCP: |
---|
948 | protocol = PPP_VJC_COMP; |
---|
949 | cp = mtod(m, u_char *); |
---|
950 | cp[0] = address; /* header has moved */ |
---|
951 | cp[1] = control; |
---|
952 | cp[2] = 0; |
---|
953 | break; |
---|
954 | } |
---|
955 | cp[3] = protocol; /* update protocol in PPP header */ |
---|
956 | } |
---|
957 | } |
---|
958 | #endif /* VJC */ |
---|
959 | break; |
---|
960 | |
---|
961 | #ifdef PPP_COMPRESS |
---|
962 | case PPP_CCP: |
---|
963 | ppp_ccp(sc, m, 0); |
---|
964 | break; |
---|
965 | #endif /* PPP_COMPRESS */ |
---|
966 | } |
---|
967 | |
---|
968 | #ifdef PPP_COMPRESS |
---|
969 | if (protocol != PPP_LCP && protocol != PPP_CCP |
---|
970 | && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) { |
---|
971 | struct mbuf *mcomp = NULL; |
---|
972 | int slen, clen; |
---|
973 | |
---|
974 | slen = 0; |
---|
975 | for (mp = m; mp != NULL; mp = mp->m_next) |
---|
976 | slen += mp->m_len; |
---|
977 | clen = (*sc->sc_xcomp->compress) |
---|
978 | (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN); |
---|
979 | if (mcomp != NULL) { |
---|
980 | if (sc->sc_flags & SC_CCP_UP) { |
---|
981 | /* Send the compressed packet instead of the original. */ |
---|
982 | m_freem(m); |
---|
983 | m = mcomp; |
---|
984 | cp = mtod(m, u_char *); |
---|
985 | protocol = cp[3]; |
---|
986 | } else { |
---|
987 | /* Can't transmit compressed packets until CCP is up. */ |
---|
988 | m_freem(mcomp); |
---|
989 | } |
---|
990 | } |
---|
991 | } |
---|
992 | #endif /* PPP_COMPRESS */ |
---|
993 | |
---|
994 | /* |
---|
995 | * Compress the address/control and protocol, if possible. |
---|
996 | */ |
---|
997 | if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS && |
---|
998 | control == PPP_UI && protocol != PPP_ALLSTATIONS && |
---|
999 | protocol != PPP_LCP) { |
---|
1000 | /* can compress address/control */ |
---|
1001 | m->m_data += 2; |
---|
1002 | m->m_len -= 2; |
---|
1003 | } |
---|
1004 | if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) { |
---|
1005 | /* can compress protocol */ |
---|
1006 | if (mtod(m, u_char *) == cp) { |
---|
1007 | cp[2] = cp[1]; /* move address/control up */ |
---|
1008 | cp[1] = cp[0]; |
---|
1009 | } |
---|
1010 | ++m->m_data; |
---|
1011 | --m->m_len; |
---|
1012 | } |
---|
1013 | |
---|
1014 | return m; |
---|
1015 | } |
---|
1016 | |
---|
1017 | /* |
---|
1018 | * Software interrupt routine, called at splsoftnet. |
---|
1019 | */ |
---|
1020 | void |
---|
1021 | pppintr() |
---|
1022 | { |
---|
1023 | struct ppp_softc *sc; |
---|
1024 | int i, s2; |
---|
1025 | struct mbuf *m; |
---|
1026 | |
---|
1027 | sc = ppp_softc; |
---|
1028 | for (i = 0; i < NPPP; ++i, ++sc) { |
---|
1029 | if (!(sc->sc_flags & SC_TBUSY) |
---|
1030 | && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head)) { |
---|
1031 | s2 = splimp(); |
---|
1032 | sc->sc_flags |= SC_TBUSY; |
---|
1033 | splx(s2); |
---|
1034 | (*sc->sc_start)(sc); |
---|
1035 | } |
---|
1036 | for (;;) { |
---|
1037 | IF_DEQUEUE(&sc->sc_rawq, m); |
---|
1038 | if (m == NULL) |
---|
1039 | break; |
---|
1040 | ppp_inproc(sc, m); |
---|
1041 | } |
---|
1042 | } |
---|
1043 | } |
---|
1044 | |
---|
1045 | #ifdef PPP_COMPRESS |
---|
1046 | /* |
---|
1047 | * Handle a CCP packet. `rcvd' is 1 if the packet was received, |
---|
1048 | * 0 if it is about to be transmitted. |
---|
1049 | */ |
---|
1050 | static void |
---|
1051 | ppp_ccp(sc, m, rcvd) |
---|
1052 | struct ppp_softc *sc; |
---|
1053 | struct mbuf *m; |
---|
1054 | int rcvd; |
---|
1055 | { |
---|
1056 | u_char *dp, *ep; |
---|
1057 | struct mbuf *mp; |
---|
1058 | int slen, s; |
---|
1059 | |
---|
1060 | /* |
---|
1061 | * Get a pointer to the data after the PPP header. |
---|
1062 | */ |
---|
1063 | if (m->m_len <= PPP_HDRLEN) { |
---|
1064 | mp = m->m_next; |
---|
1065 | if (mp == NULL) |
---|
1066 | return; |
---|
1067 | dp = (mp != NULL)? mtod(mp, u_char *): NULL; |
---|
1068 | } else { |
---|
1069 | mp = m; |
---|
1070 | dp = mtod(mp, u_char *) + PPP_HDRLEN; |
---|
1071 | } |
---|
1072 | |
---|
1073 | ep = mtod(mp, u_char *) + mp->m_len; |
---|
1074 | if (dp + CCP_HDRLEN > ep) |
---|
1075 | return; |
---|
1076 | slen = CCP_LENGTH(dp); |
---|
1077 | if (dp + slen > ep) { |
---|
1078 | if (sc->sc_flags & SC_DEBUG) |
---|
1079 | printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n", |
---|
1080 | dp, slen, mtod(mp, u_char *), mp->m_len); |
---|
1081 | return; |
---|
1082 | } |
---|
1083 | |
---|
1084 | switch (CCP_CODE(dp)) { |
---|
1085 | case CCP_CONFREQ: |
---|
1086 | case CCP_TERMREQ: |
---|
1087 | case CCP_TERMACK: |
---|
1088 | /* CCP must be going down - disable compression */ |
---|
1089 | if (sc->sc_flags & SC_CCP_UP) { |
---|
1090 | s = splimp(); |
---|
1091 | sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN); |
---|
1092 | splx(s); |
---|
1093 | } |
---|
1094 | break; |
---|
1095 | |
---|
1096 | case CCP_CONFACK: |
---|
1097 | if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP) |
---|
1098 | && slen >= CCP_HDRLEN + CCP_OPT_MINLEN |
---|
1099 | && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) { |
---|
1100 | if (!rcvd) { |
---|
1101 | /* we're agreeing to send compressed packets. */ |
---|
1102 | if (sc->sc_xc_state != NULL |
---|
1103 | && (*sc->sc_xcomp->comp_init) |
---|
1104 | (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN, |
---|
1105 | sc->sc_if.if_unit, 0, sc->sc_flags & SC_DEBUG)) { |
---|
1106 | s = splimp(); |
---|
1107 | sc->sc_flags |= SC_COMP_RUN; |
---|
1108 | splx(s); |
---|
1109 | } |
---|
1110 | } else { |
---|
1111 | /* peer is agreeing to send compressed packets. */ |
---|
1112 | if (sc->sc_rc_state != NULL |
---|
1113 | && (*sc->sc_rcomp->decomp_init) |
---|
1114 | (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN, |
---|
1115 | sc->sc_if.if_unit, 0, sc->sc_mru, |
---|
1116 | sc->sc_flags & SC_DEBUG)) { |
---|
1117 | s = splimp(); |
---|
1118 | sc->sc_flags |= SC_DECOMP_RUN; |
---|
1119 | sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR); |
---|
1120 | splx(s); |
---|
1121 | } |
---|
1122 | } |
---|
1123 | } |
---|
1124 | break; |
---|
1125 | |
---|
1126 | case CCP_RESETACK: |
---|
1127 | if (sc->sc_flags & SC_CCP_UP) { |
---|
1128 | if (!rcvd) { |
---|
1129 | if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) |
---|
1130 | (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state); |
---|
1131 | } else { |
---|
1132 | if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) { |
---|
1133 | (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state); |
---|
1134 | s = splimp(); |
---|
1135 | sc->sc_flags &= ~SC_DC_ERROR; |
---|
1136 | splx(s); |
---|
1137 | } |
---|
1138 | } |
---|
1139 | } |
---|
1140 | break; |
---|
1141 | } |
---|
1142 | } |
---|
1143 | |
---|
1144 | /* |
---|
1145 | * CCP is down; free (de)compressor state if necessary. |
---|
1146 | */ |
---|
1147 | static void |
---|
1148 | ppp_ccp_closed(sc) |
---|
1149 | struct ppp_softc *sc; |
---|
1150 | { |
---|
1151 | if (sc->sc_xc_state) { |
---|
1152 | (*sc->sc_xcomp->comp_free)(sc->sc_xc_state); |
---|
1153 | sc->sc_xc_state = NULL; |
---|
1154 | } |
---|
1155 | if (sc->sc_rc_state) { |
---|
1156 | (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state); |
---|
1157 | sc->sc_rc_state = NULL; |
---|
1158 | } |
---|
1159 | } |
---|
1160 | #endif /* PPP_COMPRESS */ |
---|
1161 | |
---|
1162 | /* |
---|
1163 | * PPP packet input routine. |
---|
1164 | * The caller has checked and removed the FCS and has inserted |
---|
1165 | * the address/control bytes and the protocol high byte if they |
---|
1166 | * were omitted. |
---|
1167 | */ |
---|
1168 | void |
---|
1169 | ppppktin(sc, m, lost) |
---|
1170 | struct ppp_softc *sc; |
---|
1171 | struct mbuf *m; |
---|
1172 | int lost; |
---|
1173 | { |
---|
1174 | |
---|
1175 | if (lost) |
---|
1176 | m->m_flags |= M_ERRMARK; |
---|
1177 | IF_ENQUEUE(&sc->sc_rawq, m); |
---|
1178 | pppintr(); |
---|
1179 | } |
---|
1180 | |
---|
1181 | /* |
---|
1182 | * Process a received PPP packet, doing decompression as necessary. |
---|
1183 | * Should be called at splsoftnet. |
---|
1184 | */ |
---|
1185 | #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \ |
---|
1186 | TYPE_UNCOMPRESSED_TCP) |
---|
1187 | |
---|
1188 | static void |
---|
1189 | ppp_inproc(sc, m) |
---|
1190 | struct ppp_softc *sc; |
---|
1191 | struct mbuf *m; |
---|
1192 | { |
---|
1193 | struct ifnet *ifp = &sc->sc_if; |
---|
1194 | struct ifqueue *inq; |
---|
1195 | int s, ilen, xlen, proto, rv; |
---|
1196 | u_char *cp, adrs, ctrl; |
---|
1197 | struct mbuf *mp; |
---|
1198 | #ifdef PPP_COMPRESS |
---|
1199 | *dmp = NULL; |
---|
1200 | #endif |
---|
1201 | u_char *iphdr; |
---|
1202 | u_int hlen; |
---|
1203 | |
---|
1204 | sc->sc_stats.ppp_ipackets++; |
---|
1205 | |
---|
1206 | if (sc->sc_flags & SC_LOG_INPKT) { |
---|
1207 | ilen = 0; |
---|
1208 | for (mp = m; mp != NULL; mp = mp->m_next) |
---|
1209 | ilen += mp->m_len; |
---|
1210 | printf("ppp%d: got %d bytes\n", ifp->if_unit, ilen); |
---|
1211 | pppdumpm(m); |
---|
1212 | } |
---|
1213 | |
---|
1214 | cp = mtod(m, u_char *); |
---|
1215 | adrs = PPP_ADDRESS(cp); |
---|
1216 | ctrl = PPP_CONTROL(cp); |
---|
1217 | proto = PPP_PROTOCOL(cp); |
---|
1218 | |
---|
1219 | if (m->m_flags & M_ERRMARK) { |
---|
1220 | m->m_flags &= ~M_ERRMARK; |
---|
1221 | s = splimp(); |
---|
1222 | sc->sc_flags |= SC_VJ_RESET; |
---|
1223 | splx(s); |
---|
1224 | } |
---|
1225 | |
---|
1226 | #ifdef PPP_COMPRESS |
---|
1227 | /* |
---|
1228 | * Decompress this packet if necessary, update the receiver's |
---|
1229 | * dictionary, or take appropriate action on a CCP packet. |
---|
1230 | */ |
---|
1231 | if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN) |
---|
1232 | && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) { |
---|
1233 | /* decompress this packet */ |
---|
1234 | rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp); |
---|
1235 | if (rv == DECOMP_OK) { |
---|
1236 | m_freem(m); |
---|
1237 | if (dmp == NULL) { |
---|
1238 | /* no error, but no decompressed packet produced */ |
---|
1239 | return; |
---|
1240 | } |
---|
1241 | m = dmp; |
---|
1242 | cp = mtod(m, u_char *); |
---|
1243 | proto = PPP_PROTOCOL(cp); |
---|
1244 | |
---|
1245 | } else { |
---|
1246 | /* |
---|
1247 | * An error has occurred in decompression. |
---|
1248 | * Pass the compressed packet up to pppd, which may take |
---|
1249 | * CCP down or issue a Reset-Req. |
---|
1250 | */ |
---|
1251 | if (sc->sc_flags & SC_DEBUG) |
---|
1252 | printf("ppp%d: decompress failed %d\n", ifp->if_unit, rv); |
---|
1253 | s = splimp(); |
---|
1254 | sc->sc_flags |= SC_VJ_RESET; |
---|
1255 | if (rv == DECOMP_ERROR) |
---|
1256 | sc->sc_flags |= SC_DC_ERROR; |
---|
1257 | else |
---|
1258 | sc->sc_flags |= SC_DC_FERROR; |
---|
1259 | splx(s); |
---|
1260 | } |
---|
1261 | |
---|
1262 | } else { |
---|
1263 | if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) { |
---|
1264 | (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m); |
---|
1265 | } |
---|
1266 | if (proto == PPP_CCP) { |
---|
1267 | ppp_ccp(sc, m, 1); |
---|
1268 | } |
---|
1269 | } |
---|
1270 | #endif |
---|
1271 | |
---|
1272 | ilen = 0; |
---|
1273 | for (mp = m; mp != NULL; mp = mp->m_next) |
---|
1274 | ilen += mp->m_len; |
---|
1275 | |
---|
1276 | #ifdef VJC |
---|
1277 | if (sc->sc_flags & SC_VJ_RESET) { |
---|
1278 | /* |
---|
1279 | * If we've missed a packet, we must toss subsequent compressed |
---|
1280 | * packets which don't have an explicit connection ID. |
---|
1281 | */ |
---|
1282 | if (sc->sc_comp) |
---|
1283 | vj_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp); |
---|
1284 | s = splimp(); |
---|
1285 | sc->sc_flags &= ~SC_VJ_RESET; |
---|
1286 | splx(s); |
---|
1287 | } |
---|
1288 | |
---|
1289 | /* |
---|
1290 | * See if we have a VJ-compressed packet to uncompress. |
---|
1291 | */ |
---|
1292 | if (proto == PPP_VJC_COMP) { |
---|
1293 | if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0) |
---|
1294 | goto bad; |
---|
1295 | |
---|
1296 | xlen = vj_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN, |
---|
1297 | ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP, |
---|
1298 | sc->sc_comp, &iphdr, &hlen); |
---|
1299 | |
---|
1300 | if (xlen <= 0) { |
---|
1301 | if (sc->sc_flags & SC_DEBUG) |
---|
1302 | printf("ppp%d: VJ uncompress failed on type comp\n", |
---|
1303 | ifp->if_unit); |
---|
1304 | goto bad; |
---|
1305 | } |
---|
1306 | |
---|
1307 | /* Copy the PPP and IP headers into a new mbuf. */ |
---|
1308 | MGETHDR(mp, M_DONTWAIT, MT_DATA); |
---|
1309 | if (mp == NULL) |
---|
1310 | goto bad; |
---|
1311 | mp->m_len = 0; |
---|
1312 | mp->m_next = NULL; |
---|
1313 | if (hlen + PPP_HDRLEN > MHLEN) { |
---|
1314 | MCLGET(mp, M_DONTWAIT); |
---|
1315 | if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) { |
---|
1316 | m_freem(mp); |
---|
1317 | goto bad; /* lose if big headers and no clusters */ |
---|
1318 | } |
---|
1319 | } |
---|
1320 | cp = mtod(mp, u_char *); |
---|
1321 | cp[0] = adrs; |
---|
1322 | cp[1] = ctrl; |
---|
1323 | cp[2] = 0; |
---|
1324 | cp[3] = PPP_IP; |
---|
1325 | proto = PPP_IP; |
---|
1326 | bcopy(iphdr, cp + PPP_HDRLEN, hlen); |
---|
1327 | mp->m_len = hlen + PPP_HDRLEN; |
---|
1328 | |
---|
1329 | /* |
---|
1330 | * Trim the PPP and VJ headers off the old mbuf |
---|
1331 | * and stick the new and old mbufs together. |
---|
1332 | */ |
---|
1333 | m->m_data += PPP_HDRLEN + xlen; |
---|
1334 | m->m_len -= PPP_HDRLEN + xlen; |
---|
1335 | if (m->m_len <= M_TRAILINGSPACE(mp)) { |
---|
1336 | bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len); |
---|
1337 | mp->m_len += m->m_len; |
---|
1338 | MFREE(m, mp->m_next); |
---|
1339 | } else |
---|
1340 | mp->m_next = m; |
---|
1341 | m = mp; |
---|
1342 | ilen += hlen - xlen; |
---|
1343 | |
---|
1344 | } else if (proto == PPP_VJC_UNCOMP) { |
---|
1345 | if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0) |
---|
1346 | goto bad; |
---|
1347 | |
---|
1348 | xlen = vj_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN, |
---|
1349 | ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP, |
---|
1350 | sc->sc_comp, &iphdr, &hlen); |
---|
1351 | |
---|
1352 | if (xlen < 0) { |
---|
1353 | if (sc->sc_flags & SC_DEBUG) |
---|
1354 | printf("ppp%d: VJ uncompress failed on type uncomp\n", |
---|
1355 | ifp->if_unit); |
---|
1356 | goto bad; |
---|
1357 | } |
---|
1358 | |
---|
1359 | proto = PPP_IP; |
---|
1360 | cp[3] = PPP_IP; |
---|
1361 | } |
---|
1362 | #endif /* VJC */ |
---|
1363 | |
---|
1364 | /* |
---|
1365 | * If the packet will fit in a header mbuf, don't waste a |
---|
1366 | * whole cluster on it. |
---|
1367 | */ |
---|
1368 | if (ilen <= MHLEN && M_IS_CLUSTER(m)) { |
---|
1369 | MGETHDR(mp, M_DONTWAIT, MT_DATA); |
---|
1370 | if (mp != NULL) { |
---|
1371 | m_copydata(m, 0, ilen, mtod(mp, caddr_t)); |
---|
1372 | m_freem(m); |
---|
1373 | m = mp; |
---|
1374 | m->m_len = ilen; |
---|
1375 | } |
---|
1376 | } |
---|
1377 | m->m_pkthdr.len = ilen; |
---|
1378 | m->m_pkthdr.rcvif = ifp; |
---|
1379 | |
---|
1380 | if ((proto & 0x8000) == 0) { |
---|
1381 | #ifdef PPP_FILTER |
---|
1382 | /* |
---|
1383 | * See whether we want to pass this packet, and |
---|
1384 | * if it counts as link activity. |
---|
1385 | */ |
---|
1386 | adrs = *mtod(m, u_char *); /* save address field */ |
---|
1387 | *mtod(m, u_char *) = 0; /* indicate inbound */ |
---|
1388 | if (sc->sc_pass_filt.bf_insns != 0 |
---|
1389 | && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m, |
---|
1390 | ilen, 0) == 0) { |
---|
1391 | /* drop this packet */ |
---|
1392 | m_freem(m); |
---|
1393 | return; |
---|
1394 | } |
---|
1395 | if (sc->sc_active_filt.bf_insns == 0 |
---|
1396 | || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0)) |
---|
1397 | { |
---|
1398 | microtime(&ppp_time); |
---|
1399 | |
---|
1400 | sc->sc_last_recv = ppp_time.tv_sec; |
---|
1401 | } |
---|
1402 | |
---|
1403 | *mtod(m, u_char *) = adrs; |
---|
1404 | #else |
---|
1405 | /* |
---|
1406 | * Record the time that we received this packet. |
---|
1407 | */ |
---|
1408 | microtime(&ppp_time); |
---|
1409 | sc->sc_last_recv = ppp_time.tv_sec; |
---|
1410 | #endif /* PPP_FILTER */ |
---|
1411 | } |
---|
1412 | |
---|
1413 | #if NBPFILTER > 0 |
---|
1414 | /* See if bpf wants to look at the packet. */ |
---|
1415 | if (sc->sc_bpf) |
---|
1416 | bpf_mtap(sc->sc_bpf, m); |
---|
1417 | #endif |
---|
1418 | |
---|
1419 | rv = 0; |
---|
1420 | switch (proto) { |
---|
1421 | #ifdef INET |
---|
1422 | case PPP_IP: |
---|
1423 | /* |
---|
1424 | * IP packet - take off the ppp header and pass it up to IP. |
---|
1425 | */ |
---|
1426 | if ((ifp->if_flags & IFF_UP) == 0 |
---|
1427 | || sc->sc_npmode[NP_IP] != NPMODE_PASS) { |
---|
1428 | /* interface is down - drop the packet. */ |
---|
1429 | m_freem(m); |
---|
1430 | return; |
---|
1431 | } |
---|
1432 | m->m_pkthdr.len -= PPP_HDRLEN; |
---|
1433 | m->m_data += PPP_HDRLEN; |
---|
1434 | m->m_len -= PPP_HDRLEN; |
---|
1435 | schednetisr(NETISR_IP); |
---|
1436 | inq = &ipintrq; |
---|
1437 | break; |
---|
1438 | #endif |
---|
1439 | |
---|
1440 | default: |
---|
1441 | /* |
---|
1442 | * Some other protocol - place on input queue for read(). |
---|
1443 | */ |
---|
1444 | inq = &sc->sc_inq; |
---|
1445 | rv = 1; |
---|
1446 | break; |
---|
1447 | } |
---|
1448 | |
---|
1449 | /* |
---|
1450 | * Put the packet on the appropriate input queue. |
---|
1451 | */ |
---|
1452 | s = splimp(); |
---|
1453 | if (IF_QFULL(inq)) { |
---|
1454 | IF_DROP(inq); |
---|
1455 | splx(s); |
---|
1456 | if (sc->sc_flags & SC_DEBUG) |
---|
1457 | printf("ppp%d: input queue full\n", ifp->if_unit); |
---|
1458 | ifp->if_iqdrops++; |
---|
1459 | goto bad; |
---|
1460 | } |
---|
1461 | IF_ENQUEUE(inq, m); |
---|
1462 | splx(s); |
---|
1463 | ifp->if_ipackets++; |
---|
1464 | ifp->if_ibytes += ilen; |
---|
1465 | microtime(&ppp_time); |
---|
1466 | ifp->if_lastchange = ppp_time; |
---|
1467 | |
---|
1468 | if (rv) |
---|
1469 | (*sc->sc_ctlp)(sc); |
---|
1470 | |
---|
1471 | return; |
---|
1472 | |
---|
1473 | bad: |
---|
1474 | m_freem(m); |
---|
1475 | sc->sc_if.if_ierrors++; |
---|
1476 | sc->sc_stats.ppp_ierrors++; |
---|
1477 | } |
---|
1478 | |
---|
1479 | #define MAX_DUMP_BYTES 128 |
---|
1480 | |
---|
1481 | static void |
---|
1482 | pppdumpm(m0) |
---|
1483 | struct mbuf *m0; |
---|
1484 | { |
---|
1485 | char buf[3*MAX_DUMP_BYTES+4]; |
---|
1486 | char *bp = buf; |
---|
1487 | struct mbuf *m; |
---|
1488 | static char digits[] = "0123456789abcdef"; |
---|
1489 | |
---|
1490 | for (m = m0; m; m = m->m_next) { |
---|
1491 | int l = m->m_len; |
---|
1492 | u_char *rptr = (u_char *)m->m_data; |
---|
1493 | |
---|
1494 | while (l--) { |
---|
1495 | if (bp > buf + sizeof(buf) - 4) |
---|
1496 | goto done; |
---|
1497 | *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */ |
---|
1498 | *bp++ = digits[*rptr++ & 0xf]; |
---|
1499 | } |
---|
1500 | |
---|
1501 | if (m->m_next) { |
---|
1502 | if (bp > buf + sizeof(buf) - 3) |
---|
1503 | goto done; |
---|
1504 | *bp++ = '|'; |
---|
1505 | } else |
---|
1506 | *bp++ = ' '; |
---|
1507 | } |
---|
1508 | done: |
---|
1509 | if (m) |
---|
1510 | *bp++ = '>'; |
---|
1511 | *bp = 0; |
---|
1512 | printf("%s\n", buf); |
---|
1513 | } |
---|
1514 | |
---|
1515 | #endif /* NPPP > 0 */ |
---|