if_ppp.c 38 KB

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