ppp.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045
  1. /*****************************************************************************
  2. * ppp.c - Network Point to Point Protocol program file.
  3. *
  4. * Copyright (c) 2003 by Marc Boucher, Services Informatiques (MBSI) inc.
  5. * portions Copyright (c) 1997 by Global Election Systems Inc.
  6. *
  7. * The authors hereby grant permission to use, copy, modify, distribute,
  8. * and license this software and its documentation for any purpose, provided
  9. * that existing copyright notices are retained in all copies and that this
  10. * notice and the following disclaimer are included verbatim in any
  11. * distributions. No written agreement, license, or royalty fee is required
  12. * for any of the authorized uses.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. *
  25. ******************************************************************************
  26. * REVISION HISTORY
  27. *
  28. * 03-01-01 Marc Boucher <marc@mbsi.ca>
  29. * Ported to lwIP.
  30. * 97-11-05 Guy Lancaster <lancasterg@acm.org>, Global Election Systems Inc.
  31. * Original.
  32. *****************************************************************************/
  33. /*
  34. * ppp_defs.h - PPP definitions.
  35. *
  36. * if_pppvar.h - private structures and declarations for PPP.
  37. *
  38. * Copyright (c) 1994 The Australian National University.
  39. * All rights reserved.
  40. *
  41. * Permission to use, copy, modify, and distribute this software and its
  42. * documentation is hereby granted, provided that the above copyright
  43. * notice appears in all copies. This software is provided without any
  44. * warranty, express or implied. The Australian National University
  45. * makes no representations about the suitability of this software for
  46. * any purpose.
  47. *
  48. * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
  49. * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  50. * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
  51. * THE AUSTRALIAN NATIONAL UNIVERSITY HAVE BEEN ADVISED OF THE POSSIBILITY
  52. * OF SUCH DAMAGE.
  53. *
  54. * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  55. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  56. * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
  57. * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
  58. * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
  59. * OR MODIFICATIONS.
  60. */
  61. /*
  62. * if_ppp.h - Point-to-Point Protocol definitions.
  63. *
  64. * Copyright (c) 1989 Carnegie Mellon University.
  65. * All rights reserved.
  66. *
  67. * Redistribution and use in source and binary forms are permitted
  68. * provided that the above copyright notice and this paragraph are
  69. * duplicated in all such forms and that any documentation,
  70. * advertising materials, and other materials related to such
  71. * distribution and use acknowledge that the software was developed
  72. * by Carnegie Mellon University. The name of the
  73. * University may not be used to endorse or promote products derived
  74. * from this software without specific prior written permission.
  75. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  76. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  77. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  78. */
  79. #include "lwip/opt.h"
  80. #if PPP_SUPPORT /* don't build if not configured for use in lwipopts.h */
  81. #include "ppp_impl.h"
  82. #include "lwip/ip.h" /* for ip_input() */
  83. #include "pppdebug.h"
  84. #include "randm.h"
  85. #include "fsm.h"
  86. #if PAP_SUPPORT
  87. #include "pap.h"
  88. #endif /* PAP_SUPPORT */
  89. #if CHAP_SUPPORT
  90. #include "chap.h"
  91. #endif /* CHAP_SUPPORT */
  92. #include "ipcp.h"
  93. #include "lcp.h"
  94. #include "magic.h"
  95. #include "auth.h"
  96. #if VJ_SUPPORT
  97. #include "vj.h"
  98. #endif /* VJ_SUPPORT */
  99. #if PPPOE_SUPPORT
  100. #include "netif/ppp_oe.h"
  101. #endif /* PPPOE_SUPPORT */
  102. #include "lwip/tcpip.h"
  103. #include "lwip/api.h"
  104. #include "lwip/snmp.h"
  105. #include <string.h>
  106. /*************************/
  107. /*** LOCAL DEFINITIONS ***/
  108. /*************************/
  109. /** PPP_INPROC_MULTITHREADED==1 call pppInput using tcpip_callback().
  110. * Set this to 0 if pppInProc is called inside tcpip_thread or with NO_SYS==1.
  111. * Default is 1 for NO_SYS==0 (multithreaded) and 0 for NO_SYS==1 (single-threaded).
  112. */
  113. #ifndef PPP_INPROC_MULTITHREADED
  114. #define PPP_INPROC_MULTITHREADED (NO_SYS==0)
  115. #endif
  116. /** PPP_INPROC_OWNTHREAD==1: start a dedicated RX thread per PPP session.
  117. * Default is 0: call pppos_input() for received raw characters, charcater
  118. * reception is up to the port */
  119. #ifndef PPP_INPROC_OWNTHREAD
  120. #define PPP_INPROC_OWNTHREAD PPP_INPROC_MULTITHREADED
  121. #endif
  122. #if PPP_INPROC_OWNTHREAD && !PPP_INPROC_MULTITHREADED
  123. #error "PPP_INPROC_OWNTHREAD needs PPP_INPROC_MULTITHREADED==1"
  124. #endif
  125. /*
  126. * The basic PPP frame.
  127. */
  128. #define PPP_ADDRESS(p) (((u_char *)(p))[0])
  129. #define PPP_CONTROL(p) (((u_char *)(p))[1])
  130. #define PPP_PROTOCOL(p) ((((u_char *)(p))[2] << 8) + ((u_char *)(p))[3])
  131. /* PPP packet parser states. Current state indicates operation yet to be
  132. * completed. */
  133. typedef enum {
  134. PDIDLE = 0, /* Idle state - waiting. */
  135. PDSTART, /* Process start flag. */
  136. PDADDRESS, /* Process address field. */
  137. PDCONTROL, /* Process control field. */
  138. PDPROTOCOL1, /* Process protocol field 1. */
  139. PDPROTOCOL2, /* Process protocol field 2. */
  140. PDDATA /* Process data byte. */
  141. } PPPDevStates;
  142. #define ESCAPE_P(accm, c) ((accm)[(c) >> 3] & pppACCMMask[c & 0x07])
  143. /************************/
  144. /*** LOCAL DATA TYPES ***/
  145. /************************/
  146. /** RX buffer size: this may be configured smaller! */
  147. #ifndef PPPOS_RX_BUFSIZE
  148. #define PPPOS_RX_BUFSIZE (PPP_MRU + PPP_HDRLEN)
  149. #endif
  150. typedef struct PPPControlRx_s {
  151. /** unit number / ppp descriptor */
  152. int pd;
  153. /** the rx file descriptor */
  154. sio_fd_t fd;
  155. /** receive buffer - encoded data is stored here */
  156. #if PPP_INPROC_OWNTHREAD
  157. u_char rxbuf[PPPOS_RX_BUFSIZE];
  158. #endif /* PPP_INPROC_OWNTHREAD */
  159. /* The input packet. */
  160. struct pbuf *inHead, *inTail;
  161. #if PPPOS_SUPPORT
  162. u16_t inProtocol; /* The input protocol code. */
  163. u16_t inFCS; /* Input Frame Check Sequence value. */
  164. #endif /* PPPOS_SUPPORT */
  165. PPPDevStates inState; /* The input process state. */
  166. char inEscaped; /* Escape next character. */
  167. ext_accm inACCM; /* Async-Ctl-Char-Map for input. */
  168. } PPPControlRx;
  169. /*
  170. * PPP interface control block.
  171. */
  172. typedef struct PPPControl_s {
  173. PPPControlRx rx;
  174. char openFlag; /* True when in use. */
  175. #if PPPOE_SUPPORT
  176. struct netif *ethif;
  177. struct pppoe_softc *pppoe_sc;
  178. #endif /* PPPOE_SUPPORT */
  179. int if_up; /* True when the interface is up. */
  180. int errCode; /* Code indicating why interface is down. */
  181. #if PPPOS_SUPPORT
  182. sio_fd_t fd; /* File device ID of port. */
  183. #endif /* PPPOS_SUPPORT */
  184. u16_t mtu; /* Peer's mru */
  185. int pcomp; /* Does peer accept protocol compression? */
  186. int accomp; /* Does peer accept addr/ctl compression? */
  187. u_long lastXMit; /* Time of last transmission. */
  188. ext_accm outACCM; /* Async-Ctl-Char-Map for output. */
  189. #if PPPOS_SUPPORT && VJ_SUPPORT
  190. int vjEnabled; /* Flag indicating VJ compression enabled. */
  191. struct vjcompress vjComp; /* Van Jacobson compression header. */
  192. #endif /* PPPOS_SUPPORT && VJ_SUPPORT */
  193. struct netif netif;
  194. struct ppp_addrs addrs;
  195. void (*linkStatusCB)(void *ctx, int errCode, void *arg);
  196. void *linkStatusCtx;
  197. } PPPControl;
  198. /*
  199. * Ioctl definitions.
  200. */
  201. struct npioctl {
  202. int protocol; /* PPP procotol, e.g. PPP_IP */
  203. enum NPmode mode;
  204. };
  205. /***********************************/
  206. /*** LOCAL FUNCTION DECLARATIONS ***/
  207. /***********************************/
  208. #if PPPOS_SUPPORT
  209. #if PPP_INPROC_OWNTHREAD
  210. static void pppInputThread(void *arg);
  211. #endif /* PPP_INPROC_OWNTHREAD */
  212. static void pppDrop(PPPControlRx *pcrx);
  213. static void pppInProc(PPPControlRx *pcrx, u_char *s, int l);
  214. static void pppFreeCurrentInputPacket(PPPControlRx *pcrx);
  215. #endif /* PPPOS_SUPPORT */
  216. /******************************/
  217. /*** PUBLIC DATA STRUCTURES ***/
  218. /******************************/
  219. u_long subnetMask;
  220. static PPPControl pppControl[NUM_PPP]; /* The PPP interface control blocks. */
  221. /*
  222. * PPP Data Link Layer "protocol" table.
  223. * One entry per supported protocol.
  224. * The last entry must be NULL.
  225. */
  226. struct protent *ppp_protocols[] = {
  227. &lcp_protent,
  228. #if PAP_SUPPORT
  229. &pap_protent,
  230. #endif /* PAP_SUPPORT */
  231. #if CHAP_SUPPORT
  232. &chap_protent,
  233. #endif /* CHAP_SUPPORT */
  234. #if CBCP_SUPPORT
  235. &cbcp_protent,
  236. #endif /* CBCP_SUPPORT */
  237. &ipcp_protent,
  238. #if CCP_SUPPORT
  239. &ccp_protent,
  240. #endif /* CCP_SUPPORT */
  241. NULL
  242. };
  243. /*
  244. * Buffers for outgoing packets. This must be accessed only from the appropriate
  245. * PPP task so that it doesn't need to be protected to avoid collisions.
  246. */
  247. u_char outpacket_buf[NUM_PPP][PPP_MRU+PPP_HDRLEN];
  248. /*****************************/
  249. /*** LOCAL DATA STRUCTURES ***/
  250. /*****************************/
  251. #if PPPOS_SUPPORT
  252. /*
  253. * FCS lookup table as calculated by genfcstab.
  254. * @todo: smaller, slower implementation for lower memory footprint?
  255. */
  256. static const u_short fcstab[256] = {
  257. 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
  258. 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
  259. 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
  260. 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
  261. 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
  262. 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
  263. 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
  264. 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
  265. 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
  266. 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
  267. 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
  268. 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
  269. 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
  270. 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
  271. 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
  272. 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
  273. 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
  274. 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
  275. 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
  276. 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
  277. 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
  278. 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
  279. 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
  280. 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
  281. 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
  282. 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
  283. 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
  284. 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
  285. 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
  286. 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
  287. 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
  288. 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
  289. };
  290. /* PPP's Asynchronous-Control-Character-Map. The mask array is used
  291. * to select the specific bit for a character. */
  292. static u_char pppACCMMask[] = {
  293. 0x01,
  294. 0x02,
  295. 0x04,
  296. 0x08,
  297. 0x10,
  298. 0x20,
  299. 0x40,
  300. 0x80
  301. };
  302. #if PPP_INPROC_OWNTHREAD
  303. /** Wake up the task blocked in reading from serial line (if any) */
  304. static void
  305. pppRecvWakeup(int pd)
  306. {
  307. PPPDEBUG(LOG_DEBUG, ("pppRecvWakeup: unit %d\n", pd));
  308. if (pppControl[pd].openFlag != 0) {
  309. sio_read_abort(pppControl[pd].fd);
  310. }
  311. }
  312. #endif /* PPP_INPROC_OWNTHREAD */
  313. #endif /* PPPOS_SUPPORT */
  314. void
  315. pppLinkTerminated(int pd)
  316. {
  317. PPPDEBUG(LOG_DEBUG, ("pppLinkTerminated: unit %d\n", pd));
  318. #if PPPOE_SUPPORT
  319. if (pppControl[pd].ethif) {
  320. pppoe_disconnect(pppControl[pd].pppoe_sc);
  321. } else
  322. #endif /* PPPOE_SUPPORT */
  323. {
  324. #if PPPOS_SUPPORT
  325. PPPControl* pc;
  326. #if PPP_INPROC_OWNTHREAD
  327. pppRecvWakeup(pd);
  328. #endif /* PPP_INPROC_OWNTHREAD */
  329. pc = &pppControl[pd];
  330. PPPDEBUG(LOG_DEBUG, ("pppLinkTerminated: unit %d: linkStatusCB=%p errCode=%d\n", pd, pc->linkStatusCB, pc->errCode));
  331. if (pc->linkStatusCB) {
  332. pc->linkStatusCB(pc->linkStatusCtx, pc->errCode ? pc->errCode : PPPERR_PROTOCOL, NULL);
  333. }
  334. pc->openFlag = 0;/**/
  335. #endif /* PPPOS_SUPPORT */
  336. }
  337. PPPDEBUG(LOG_DEBUG, ("pppLinkTerminated: finished.\n"));
  338. }
  339. void
  340. pppLinkDown(int pd)
  341. {
  342. PPPDEBUG(LOG_DEBUG, ("pppLinkDown: unit %d\n", pd));
  343. #if PPPOE_SUPPORT
  344. if (pppControl[pd].ethif) {
  345. pppoe_disconnect(pppControl[pd].pppoe_sc);
  346. } else
  347. #endif /* PPPOE_SUPPORT */
  348. {
  349. #if PPPOS_SUPPORT && PPP_INPROC_OWNTHREAD
  350. pppRecvWakeup(pd);
  351. #endif /* PPPOS_SUPPORT && PPP_INPROC_OWNTHREAD*/
  352. }
  353. }
  354. /** Initiate LCP open request */
  355. static void
  356. pppStart(int pd)
  357. {
  358. PPPDEBUG(LOG_DEBUG, ("pppStart: unit %d\n", pd));
  359. lcp_lowerup(pd);
  360. lcp_open(pd); /* Start protocol */
  361. PPPDEBUG(LOG_DEBUG, ("pppStart: finished\n"));
  362. }
  363. /** LCP close request */
  364. static void
  365. pppStop(int pd)
  366. {
  367. PPPDEBUG(LOG_DEBUG, ("pppStop: unit %d\n", pd));
  368. lcp_close(pd, "User request");
  369. }
  370. /** Called when carrier/link is lost */
  371. static void
  372. pppHup(int pd)
  373. {
  374. PPPDEBUG(LOG_DEBUG, ("pppHupCB: unit %d\n", pd));
  375. lcp_lowerdown(pd);
  376. link_terminated(pd);
  377. }
  378. /***********************************/
  379. /*** PUBLIC FUNCTION DEFINITIONS ***/
  380. /***********************************/
  381. /* Initialize the PPP subsystem. */
  382. struct ppp_settings ppp_settings;
  383. void
  384. pppInit(void)
  385. {
  386. struct protent *protp;
  387. int i, j;
  388. memset(&ppp_settings, 0, sizeof(ppp_settings));
  389. ppp_settings.usepeerdns = 1;
  390. pppSetAuth(PPPAUTHTYPE_NONE, NULL, NULL);
  391. magicInit();
  392. subnetMask = PP_HTONL(0xffffff00UL);
  393. for (i = 0; i < NUM_PPP; i++) {
  394. /* Initialize each protocol to the standard option set. */
  395. for (j = 0; (protp = ppp_protocols[j]) != NULL; ++j) {
  396. (*protp->init)(i);
  397. }
  398. }
  399. }
  400. void
  401. pppSetAuth(enum pppAuthType authType, const char *user, const char *passwd)
  402. {
  403. switch(authType) {
  404. case PPPAUTHTYPE_NONE:
  405. default:
  406. #ifdef LWIP_PPP_STRICT_PAP_REJECT
  407. ppp_settings.refuse_pap = 1;
  408. #else /* LWIP_PPP_STRICT_PAP_REJECT */
  409. /* some providers request pap and accept an empty login/pw */
  410. ppp_settings.refuse_pap = 0;
  411. #endif /* LWIP_PPP_STRICT_PAP_REJECT */
  412. ppp_settings.refuse_chap = 1;
  413. break;
  414. case PPPAUTHTYPE_ANY:
  415. /* Warning: Using PPPAUTHTYPE_ANY might have security consequences.
  416. * RFC 1994 says:
  417. *
  418. * In practice, within or associated with each PPP server, there is a
  419. * database which associates "user" names with authentication
  420. * information ("secrets"). It is not anticipated that a particular
  421. * named user would be authenticated by multiple methods. This would
  422. * make the user vulnerable to attacks which negotiate the least secure
  423. * method from among a set (such as PAP rather than CHAP). If the same
  424. * secret was used, PAP would reveal the secret to be used later with
  425. * CHAP.
  426. *
  427. * Instead, for each user name there should be an indication of exactly
  428. * one method used to authenticate that user name. If a user needs to
  429. * make use of different authentication methods under different
  430. * circumstances, then distinct user names SHOULD be employed, each of
  431. * which identifies exactly one authentication method.
  432. *
  433. */
  434. ppp_settings.refuse_pap = 0;
  435. ppp_settings.refuse_chap = 0;
  436. break;
  437. case PPPAUTHTYPE_PAP:
  438. ppp_settings.refuse_pap = 0;
  439. ppp_settings.refuse_chap = 1;
  440. break;
  441. case PPPAUTHTYPE_CHAP:
  442. ppp_settings.refuse_pap = 1;
  443. ppp_settings.refuse_chap = 0;
  444. break;
  445. }
  446. if(user) {
  447. strncpy(ppp_settings.user, user, sizeof(ppp_settings.user)-1);
  448. ppp_settings.user[sizeof(ppp_settings.user)-1] = '\0';
  449. } else {
  450. ppp_settings.user[0] = '\0';
  451. }
  452. if(passwd) {
  453. strncpy(ppp_settings.passwd, passwd, sizeof(ppp_settings.passwd)-1);
  454. ppp_settings.passwd[sizeof(ppp_settings.passwd)-1] = '\0';
  455. } else {
  456. ppp_settings.passwd[0] = '\0';
  457. }
  458. }
  459. #if PPPOS_SUPPORT
  460. /** Open a new PPP connection using the given I/O device.
  461. * This initializes the PPP control block but does not
  462. * attempt to negotiate the LCP session. If this port
  463. * connects to a modem, the modem connection must be
  464. * established before calling this.
  465. * Return a new PPP connection descriptor on success or
  466. * an error code (negative) on failure.
  467. *
  468. * pppOpen() is directly defined to this function.
  469. */
  470. int
  471. pppOverSerialOpen(sio_fd_t fd, pppLinkStatusCB_fn linkStatusCB, void *linkStatusCtx)
  472. {
  473. PPPControl *pc;
  474. int pd;
  475. if (linkStatusCB == NULL) {
  476. /* PPP is single-threaded: without a callback,
  477. * there is no way to know when the link is up. */
  478. return PPPERR_PARAM;
  479. }
  480. /* Find a free PPP session descriptor. */
  481. for (pd = 0; pd < NUM_PPP && pppControl[pd].openFlag != 0; pd++);
  482. if (pd >= NUM_PPP) {
  483. pd = PPPERR_OPEN;
  484. } else {
  485. pc = &pppControl[pd];
  486. /* input pbuf left over from last session? */
  487. pppFreeCurrentInputPacket(&pc->rx);
  488. /* @todo: is this correct or do I overwrite something? */
  489. memset(pc, 0, sizeof(PPPControl));
  490. pc->rx.pd = pd;
  491. pc->rx.fd = fd;
  492. pc->openFlag = 1;
  493. pc->fd = fd;
  494. #if VJ_SUPPORT
  495. vj_compress_init(&pc->vjComp);
  496. #endif /* VJ_SUPPORT */
  497. /*
  498. * Default the in and out accm so that escape and flag characters
  499. * are always escaped.
  500. */
  501. pc->rx.inACCM[15] = 0x60; /* no need to protect since RX is not running */
  502. pc->outACCM[15] = 0x60;
  503. pc->linkStatusCB = linkStatusCB;
  504. pc->linkStatusCtx = linkStatusCtx;
  505. /*
  506. * Start the connection and handle incoming events (packet or timeout).
  507. */
  508. PPPDEBUG(LOG_INFO, ("pppOverSerialOpen: unit %d: Connecting\n", pd));
  509. pppStart(pd);
  510. #if PPP_INPROC_OWNTHREAD
  511. sys_thread_new(PPP_THREAD_NAME, pppInputThread, (void*)&pc->rx, PPP_THREAD_STACKSIZE, PPP_THREAD_PRIO);
  512. #endif /* PPP_INPROC_OWNTHREAD */
  513. }
  514. return pd;
  515. }
  516. #endif /* PPPOS_SUPPORT */
  517. #if PPPOE_SUPPORT
  518. static void pppOverEthernetLinkStatusCB(int pd, int up);
  519. void
  520. pppOverEthernetClose(int pd)
  521. {
  522. PPPControl* pc = &pppControl[pd];
  523. /* *TJL* There's no lcp_deinit */
  524. lcp_close(pd, NULL);
  525. pppoe_destroy(&pc->netif);
  526. }
  527. int pppOverEthernetOpen(struct netif *ethif, const char *service_name, const char *concentrator_name,
  528. pppLinkStatusCB_fn linkStatusCB, void *linkStatusCtx)
  529. {
  530. PPPControl *pc;
  531. int pd;
  532. LWIP_UNUSED_ARG(service_name);
  533. LWIP_UNUSED_ARG(concentrator_name);
  534. if (linkStatusCB == NULL) {
  535. /* PPP is single-threaded: without a callback,
  536. * there is no way to know when the link is up. */
  537. return PPPERR_PARAM;
  538. }
  539. /* Find a free PPP session descriptor. Critical region? */
  540. for (pd = 0; pd < NUM_PPP && pppControl[pd].openFlag != 0; pd++);
  541. if (pd >= NUM_PPP) {
  542. pd = PPPERR_OPEN;
  543. } else {
  544. pc = &pppControl[pd];
  545. memset(pc, 0, sizeof(PPPControl));
  546. pc->openFlag = 1;
  547. pc->ethif = ethif;
  548. pc->linkStatusCB = linkStatusCB;
  549. pc->linkStatusCtx = linkStatusCtx;
  550. lcp_wantoptions[pd].mru = PPPOE_MAXMTU;
  551. lcp_wantoptions[pd].neg_asyncmap = 0;
  552. lcp_wantoptions[pd].neg_pcompression = 0;
  553. lcp_wantoptions[pd].neg_accompression = 0;
  554. lcp_allowoptions[pd].mru = PPPOE_MAXMTU;
  555. lcp_allowoptions[pd].neg_asyncmap = 0;
  556. lcp_allowoptions[pd].neg_pcompression = 0;
  557. lcp_allowoptions[pd].neg_accompression = 0;
  558. if(pppoe_create(ethif, pd, pppOverEthernetLinkStatusCB, &pc->pppoe_sc) != ERR_OK) {
  559. pc->openFlag = 0;
  560. return PPPERR_OPEN;
  561. }
  562. pppoe_connect(pc->pppoe_sc);
  563. }
  564. return pd;
  565. }
  566. #endif /* PPPOE_SUPPORT */
  567. /* Close a PPP connection and release the descriptor.
  568. * Any outstanding packets in the queues are dropped.
  569. * Return 0 on success, an error code on failure. */
  570. int
  571. pppClose(int pd)
  572. {
  573. PPPControl *pc = &pppControl[pd];
  574. int st = 0;
  575. PPPDEBUG(LOG_DEBUG, ("pppClose() called\n"));
  576. /* Disconnect */
  577. #if PPPOE_SUPPORT
  578. if(pc->ethif) {
  579. PPPDEBUG(LOG_DEBUG, ("pppClose: unit %d kill_link -> pppStop\n", pd));
  580. pc->errCode = PPPERR_USER;
  581. /* This will leave us at PHASE_DEAD. */
  582. pppStop(pd);
  583. } else
  584. #endif /* PPPOE_SUPPORT */
  585. {
  586. #if PPPOS_SUPPORT
  587. PPPDEBUG(LOG_DEBUG, ("pppClose: unit %d kill_link -> pppStop\n", pd));
  588. pc->errCode = PPPERR_USER;
  589. /* This will leave us at PHASE_DEAD. */
  590. pppStop(pd);
  591. #if PPP_INPROC_OWNTHREAD
  592. pppRecvWakeup(pd);
  593. #endif /* PPP_INPROC_OWNTHREAD */
  594. #endif /* PPPOS_SUPPORT */
  595. }
  596. return st;
  597. }
  598. /* This function is called when carrier is lost on the PPP channel. */
  599. void
  600. pppSigHUP(int pd)
  601. {
  602. PPPDEBUG(LOG_DEBUG, ("pppSigHUP: unit %d sig_hup -> pppHupCB\n", pd));
  603. pppHup(pd);
  604. }
  605. #if PPPOS_SUPPORT
  606. static void
  607. nPut(PPPControl *pc, struct pbuf *nb)
  608. {
  609. struct pbuf *b;
  610. int c;
  611. for(b = nb; b != NULL; b = b->next) {
  612. if((c = sio_write(pc->fd, b->payload, b->len)) != b->len) {
  613. PPPDEBUG(LOG_WARNING,
  614. ("PPP nPut: incomplete sio_write(fd:%"SZT_F", len:%d, c: 0x%"X8_F") c = %d\n", (size_t)pc->fd, b->len, c, c));
  615. LINK_STATS_INC(link.err);
  616. pc->lastXMit = 0; /* prepend PPP_FLAG to next packet */
  617. snmp_inc_ifoutdiscards(&pc->netif);
  618. pbuf_free(nb);
  619. return;
  620. }
  621. }
  622. snmp_add_ifoutoctets(&pc->netif, nb->tot_len);
  623. snmp_inc_ifoutucastpkts(&pc->netif);
  624. pbuf_free(nb);
  625. LINK_STATS_INC(link.xmit);
  626. }
  627. /*
  628. * pppAppend - append given character to end of given pbuf. If outACCM
  629. * is not NULL and the character needs to be escaped, do so.
  630. * If pbuf is full, append another.
  631. * Return the current pbuf.
  632. */
  633. static struct pbuf *
  634. pppAppend(u_char c, struct pbuf *nb, ext_accm *outACCM)
  635. {
  636. struct pbuf *tb = nb;
  637. /* Make sure there is room for the character and an escape code.
  638. * Sure we don't quite fill the buffer if the character doesn't
  639. * get escaped but is one character worth complicating this? */
  640. /* Note: We assume no packet header. */
  641. if (nb && (PBUF_POOL_BUFSIZE - nb->len) < 2) {
  642. tb = pbuf_alloc(PBUF_RAW, 0, PBUF_POOL);
  643. if (tb) {
  644. nb->next = tb;
  645. } else {
  646. LINK_STATS_INC(link.memerr);
  647. }
  648. nb = tb;
  649. }
  650. if (nb) {
  651. if (outACCM && ESCAPE_P(*outACCM, c)) {
  652. *((u_char*)nb->payload + nb->len++) = PPP_ESCAPE;
  653. *((u_char*)nb->payload + nb->len++) = c ^ PPP_TRANS;
  654. } else {
  655. *((u_char*)nb->payload + nb->len++) = c;
  656. }
  657. }
  658. return tb;
  659. }
  660. #endif /* PPPOS_SUPPORT */
  661. #if PPPOE_SUPPORT
  662. static err_t
  663. pppifOutputOverEthernet(int pd, struct pbuf *p)
  664. {
  665. PPPControl *pc = &pppControl[pd];
  666. struct pbuf *pb;
  667. u_short protocol = PPP_IP;
  668. int i=0;
  669. u16_t tot_len;
  670. /* @todo: try to use pbuf_header() here! */
  671. pb = pbuf_alloc(PBUF_LINK, PPPOE_HDRLEN + sizeof(protocol), PBUF_RAM);
  672. if(!pb) {
  673. LINK_STATS_INC(link.memerr);
  674. LINK_STATS_INC(link.proterr);
  675. snmp_inc_ifoutdiscards(&pc->netif);
  676. return ERR_MEM;
  677. }
  678. pbuf_header(pb, -(s16_t)PPPOE_HDRLEN);
  679. pc->lastXMit = sys_jiffies();
  680. if (!pc->pcomp || protocol > 0xFF) {
  681. *((u_char*)pb->payload + i++) = (protocol >> 8) & 0xFF;
  682. }
  683. *((u_char*)pb->payload + i) = protocol & 0xFF;
  684. pbuf_chain(pb, p);
  685. tot_len = pb->tot_len;
  686. if(pppoe_xmit(pc->pppoe_sc, pb) != ERR_OK) {
  687. LINK_STATS_INC(link.err);
  688. snmp_inc_ifoutdiscards(&pc->netif);
  689. return PPPERR_DEVICE;
  690. }
  691. snmp_add_ifoutoctets(&pc->netif, tot_len);
  692. snmp_inc_ifoutucastpkts(&pc->netif);
  693. LINK_STATS_INC(link.xmit);
  694. return ERR_OK;
  695. }
  696. #endif /* PPPOE_SUPPORT */
  697. /* Send a packet on the given connection. */
  698. static err_t
  699. pppifOutput(struct netif *netif, struct pbuf *pb, ip_addr_t *ipaddr)
  700. {
  701. int pd = (int)(size_t)netif->state;
  702. PPPControl *pc = &pppControl[pd];
  703. #if PPPOS_SUPPORT
  704. u_short protocol = PPP_IP;
  705. u_int fcsOut = PPP_INITFCS;
  706. struct pbuf *headMB = NULL, *tailMB = NULL, *p;
  707. u_char c;
  708. #endif /* PPPOS_SUPPORT */
  709. LWIP_UNUSED_ARG(ipaddr);
  710. /* Validate parameters. */
  711. /* We let any protocol value go through - it can't hurt us
  712. * and the peer will just drop it if it's not accepting it. */
  713. if (pd < 0 || pd >= NUM_PPP || !pc->openFlag || !pb) {
  714. PPPDEBUG(LOG_WARNING, ("pppifOutput[%d]: bad parms prot=%d pb=%p\n",
  715. pd, PPP_IP, pb));
  716. LINK_STATS_INC(link.opterr);
  717. LINK_STATS_INC(link.drop);
  718. snmp_inc_ifoutdiscards(netif);
  719. return ERR_ARG;
  720. }
  721. /* Check that the link is up. */
  722. if (lcp_phase[pd] == PHASE_DEAD) {
  723. PPPDEBUG(LOG_ERR, ("pppifOutput[%d]: link not up\n", pd));
  724. LINK_STATS_INC(link.rterr);
  725. LINK_STATS_INC(link.drop);
  726. snmp_inc_ifoutdiscards(netif);
  727. return ERR_RTE;
  728. }
  729. #if PPPOE_SUPPORT
  730. if(pc->ethif) {
  731. return pppifOutputOverEthernet(pd, pb);
  732. }
  733. #endif /* PPPOE_SUPPORT */
  734. #if PPPOS_SUPPORT
  735. /* Grab an output buffer. */
  736. headMB = pbuf_alloc(PBUF_RAW, 0, PBUF_POOL);
  737. if (headMB == NULL) {
  738. PPPDEBUG(LOG_WARNING, ("pppifOutput[%d]: first alloc fail\n", pd));
  739. LINK_STATS_INC(link.memerr);
  740. LINK_STATS_INC(link.drop);
  741. snmp_inc_ifoutdiscards(netif);
  742. return ERR_MEM;
  743. }
  744. #if VJ_SUPPORT
  745. /*
  746. * Attempt Van Jacobson header compression if VJ is configured and
  747. * this is an IP packet.
  748. */
  749. if (protocol == PPP_IP && pc->vjEnabled) {
  750. switch (vj_compress_tcp(&pc->vjComp, pb)) {
  751. case TYPE_IP:
  752. /* No change...
  753. protocol = PPP_IP_PROTOCOL; */
  754. break;
  755. case TYPE_COMPRESSED_TCP:
  756. protocol = PPP_VJC_COMP;
  757. break;
  758. case TYPE_UNCOMPRESSED_TCP:
  759. protocol = PPP_VJC_UNCOMP;
  760. break;
  761. default:
  762. PPPDEBUG(LOG_WARNING, ("pppifOutput[%d]: bad IP packet\n", pd));
  763. LINK_STATS_INC(link.proterr);
  764. LINK_STATS_INC(link.drop);
  765. snmp_inc_ifoutdiscards(netif);
  766. pbuf_free(headMB);
  767. return ERR_VAL;
  768. }
  769. }
  770. #endif /* VJ_SUPPORT */
  771. tailMB = headMB;
  772. /* Build the PPP header. */
  773. if ((sys_jiffies() - pc->lastXMit) >= PPP_MAXIDLEFLAG) {
  774. tailMB = pppAppend(PPP_FLAG, tailMB, NULL);
  775. }
  776. pc->lastXMit = sys_jiffies();
  777. if (!pc->accomp) {
  778. fcsOut = PPP_FCS(fcsOut, PPP_ALLSTATIONS);
  779. tailMB = pppAppend(PPP_ALLSTATIONS, tailMB, &pc->outACCM);
  780. fcsOut = PPP_FCS(fcsOut, PPP_UI);
  781. tailMB = pppAppend(PPP_UI, tailMB, &pc->outACCM);
  782. }
  783. if (!pc->pcomp || protocol > 0xFF) {
  784. c = (protocol >> 8) & 0xFF;
  785. fcsOut = PPP_FCS(fcsOut, c);
  786. tailMB = pppAppend(c, tailMB, &pc->outACCM);
  787. }
  788. c = protocol & 0xFF;
  789. fcsOut = PPP_FCS(fcsOut, c);
  790. tailMB = pppAppend(c, tailMB, &pc->outACCM);
  791. /* Load packet. */
  792. for(p = pb; p; p = p->next) {
  793. int n;
  794. u_char *sPtr;
  795. sPtr = (u_char*)p->payload;
  796. n = p->len;
  797. while (n-- > 0) {
  798. c = *sPtr++;
  799. /* Update FCS before checking for special characters. */
  800. fcsOut = PPP_FCS(fcsOut, c);
  801. /* Copy to output buffer escaping special characters. */
  802. tailMB = pppAppend(c, tailMB, &pc->outACCM);
  803. }
  804. }
  805. /* Add FCS and trailing flag. */
  806. c = ~fcsOut & 0xFF;
  807. tailMB = pppAppend(c, tailMB, &pc->outACCM);
  808. c = (~fcsOut >> 8) & 0xFF;
  809. tailMB = pppAppend(c, tailMB, &pc->outACCM);
  810. tailMB = pppAppend(PPP_FLAG, tailMB, NULL);
  811. /* If we failed to complete the packet, throw it away. */
  812. if (!tailMB) {
  813. PPPDEBUG(LOG_WARNING,
  814. ("pppifOutput[%d]: Alloc err - dropping proto=%d\n",
  815. pd, protocol));
  816. pbuf_free(headMB);
  817. LINK_STATS_INC(link.memerr);
  818. LINK_STATS_INC(link.drop);
  819. snmp_inc_ifoutdiscards(netif);
  820. return ERR_MEM;
  821. }
  822. /* Send it. */
  823. PPPDEBUG(LOG_INFO, ("pppifOutput[%d]: proto=0x%"X16_F"\n", pd, protocol));
  824. nPut(pc, headMB);
  825. #endif /* PPPOS_SUPPORT */
  826. return ERR_OK;
  827. }
  828. /* Get and set parameters for the given connection.
  829. * Return 0 on success, an error code on failure. */
  830. int
  831. pppIOCtl(int pd, int cmd, void *arg)
  832. {
  833. PPPControl *pc = &pppControl[pd];
  834. int st = 0;
  835. if (pd < 0 || pd >= NUM_PPP) {
  836. st = PPPERR_PARAM;
  837. } else {
  838. switch(cmd) {
  839. case PPPCTLG_UPSTATUS: /* Get the PPP up status. */
  840. if (arg) {
  841. *(int *)arg = (int)(pc->if_up);
  842. } else {
  843. st = PPPERR_PARAM;
  844. }
  845. break;
  846. case PPPCTLS_ERRCODE: /* Set the PPP error code. */
  847. if (arg) {
  848. pc->errCode = *(int *)arg;
  849. } else {
  850. st = PPPERR_PARAM;
  851. }
  852. break;
  853. case PPPCTLG_ERRCODE: /* Get the PPP error code. */
  854. if (arg) {
  855. *(int *)arg = (int)(pc->errCode);
  856. } else {
  857. st = PPPERR_PARAM;
  858. }
  859. break;
  860. #if PPPOS_SUPPORT
  861. case PPPCTLG_FD: /* Get the fd associated with the ppp */
  862. if (arg) {
  863. *(sio_fd_t *)arg = pc->fd;
  864. } else {
  865. st = PPPERR_PARAM;
  866. }
  867. break;
  868. #endif /* PPPOS_SUPPORT */
  869. default:
  870. st = PPPERR_PARAM;
  871. break;
  872. }
  873. }
  874. return st;
  875. }
  876. /*
  877. * Return the Maximum Transmission Unit for the given PPP connection.
  878. */
  879. u_short
  880. pppMTU(int pd)
  881. {
  882. PPPControl *pc = &pppControl[pd];
  883. u_short st;
  884. /* Validate parameters. */
  885. if (pd < 0 || pd >= NUM_PPP || !pc->openFlag) {
  886. st = 0;
  887. } else {
  888. st = pc->mtu;
  889. }
  890. return st;
  891. }
  892. #if PPPOE_SUPPORT
  893. int
  894. pppWriteOverEthernet(int pd, const u_char *s, int n)
  895. {
  896. PPPControl *pc = &pppControl[pd];
  897. struct pbuf *pb;
  898. /* skip address & flags */
  899. s += 2;
  900. n -= 2;
  901. LWIP_ASSERT("PPPOE_HDRLEN + n <= 0xffff", PPPOE_HDRLEN + n <= 0xffff);
  902. pb = pbuf_alloc(PBUF_LINK, (u16_t)(PPPOE_HDRLEN + n), PBUF_RAM);
  903. if(!pb) {
  904. LINK_STATS_INC(link.memerr);
  905. LINK_STATS_INC(link.proterr);
  906. snmp_inc_ifoutdiscards(&pc->netif);
  907. return PPPERR_ALLOC;
  908. }
  909. pbuf_header(pb, -(s16_t)PPPOE_HDRLEN);
  910. pc->lastXMit = sys_jiffies();
  911. MEMCPY(pb->payload, s, n);
  912. if(pppoe_xmit(pc->pppoe_sc, pb) != ERR_OK) {
  913. LINK_STATS_INC(link.err);
  914. snmp_inc_ifoutdiscards(&pc->netif);
  915. return PPPERR_DEVICE;
  916. }
  917. snmp_add_ifoutoctets(&pc->netif, (u16_t)n);
  918. snmp_inc_ifoutucastpkts(&pc->netif);
  919. LINK_STATS_INC(link.xmit);
  920. return PPPERR_NONE;
  921. }
  922. #endif /* PPPOE_SUPPORT */
  923. /*
  924. * Write n characters to a ppp link.
  925. * RETURN: >= 0 Number of characters written
  926. * -1 Failed to write to device
  927. */
  928. int
  929. pppWrite(int pd, const u_char *s, int n)
  930. {
  931. PPPControl *pc = &pppControl[pd];
  932. #if PPPOS_SUPPORT
  933. u_char c;
  934. u_int fcsOut;
  935. struct pbuf *headMB, *tailMB;
  936. #endif /* PPPOS_SUPPORT */
  937. #if PPPOE_SUPPORT
  938. if(pc->ethif) {
  939. return pppWriteOverEthernet(pd, s, n);
  940. }
  941. #endif /* PPPOE_SUPPORT */
  942. #if PPPOS_SUPPORT
  943. headMB = pbuf_alloc(PBUF_RAW, 0, PBUF_POOL);
  944. if (headMB == NULL) {
  945. LINK_STATS_INC(link.memerr);
  946. LINK_STATS_INC(link.proterr);
  947. snmp_inc_ifoutdiscards(&pc->netif);
  948. return PPPERR_ALLOC;
  949. }
  950. tailMB = headMB;
  951. /* If the link has been idle, we'll send a fresh flag character to
  952. * flush any noise. */
  953. if ((sys_jiffies() - pc->lastXMit) >= PPP_MAXIDLEFLAG) {
  954. tailMB = pppAppend(PPP_FLAG, tailMB, NULL);
  955. }
  956. pc->lastXMit = sys_jiffies();
  957. fcsOut = PPP_INITFCS;
  958. /* Load output buffer. */
  959. while (n-- > 0) {
  960. c = *s++;
  961. /* Update FCS before checking for special characters. */
  962. fcsOut = PPP_FCS(fcsOut, c);
  963. /* Copy to output buffer escaping special characters. */
  964. tailMB = pppAppend(c, tailMB, &pc->outACCM);
  965. }
  966. /* Add FCS and trailing flag. */
  967. c = ~fcsOut & 0xFF;
  968. tailMB = pppAppend(c, tailMB, &pc->outACCM);
  969. c = (~fcsOut >> 8) & 0xFF;
  970. tailMB = pppAppend(c, tailMB, &pc->outACCM);
  971. tailMB = pppAppend(PPP_FLAG, tailMB, NULL);
  972. /* If we failed to complete the packet, throw it away.
  973. * Otherwise send it. */
  974. if (!tailMB) {
  975. PPPDEBUG(LOG_WARNING,
  976. ("pppWrite[%d]: Alloc err - dropping pbuf len=%d\n", pd, headMB->len));
  977. /*"pppWrite[%d]: Alloc err - dropping %d:%.*H", pd, headMB->len, LWIP_MIN(headMB->len * 2, 40), headMB->payload)); */
  978. pbuf_free(headMB);
  979. LINK_STATS_INC(link.memerr);
  980. LINK_STATS_INC(link.proterr);
  981. snmp_inc_ifoutdiscards(&pc->netif);
  982. return PPPERR_ALLOC;
  983. }
  984. PPPDEBUG(LOG_INFO, ("pppWrite[%d]: len=%d\n", pd, headMB->len));
  985. /* "pppWrite[%d]: %d:%.*H", pd, headMB->len, LWIP_MIN(headMB->len * 2, 40), headMB->payload)); */
  986. nPut(pc, headMB);
  987. #endif /* PPPOS_SUPPORT */
  988. return PPPERR_NONE;
  989. }
  990. /*
  991. * ppp_send_config - configure the transmit characteristics of
  992. * the ppp interface.
  993. */
  994. void
  995. ppp_send_config( int unit, u16_t mtu, u32_t asyncmap, int pcomp, int accomp)
  996. {
  997. PPPControl *pc = &pppControl[unit];
  998. int i;
  999. pc->mtu = mtu;
  1000. pc->pcomp = pcomp;
  1001. pc->accomp = accomp;
  1002. /* Load the ACCM bits for the 32 control codes. */
  1003. for (i = 0; i < 32/8; i++) {
  1004. pc->outACCM[i] = (u_char)((asyncmap >> (8 * i)) & 0xFF);
  1005. }
  1006. PPPDEBUG(LOG_INFO, ("ppp_send_config[%d]: outACCM=%X %X %X %X\n",
  1007. unit,
  1008. pc->outACCM[0], pc->outACCM[1], pc->outACCM[2], pc->outACCM[3]));
  1009. }
  1010. /*
  1011. * ppp_set_xaccm - set the extended transmit ACCM for the interface.
  1012. */
  1013. void
  1014. ppp_set_xaccm(int unit, ext_accm *accm)
  1015. {
  1016. SMEMCPY(pppControl[unit].outACCM, accm, sizeof(ext_accm));
  1017. PPPDEBUG(LOG_INFO, ("ppp_set_xaccm[%d]: outACCM=%X %X %X %X\n",
  1018. unit,
  1019. pppControl[unit].outACCM[0],
  1020. pppControl[unit].outACCM[1],
  1021. pppControl[unit].outACCM[2],
  1022. pppControl[unit].outACCM[3]));
  1023. }
  1024. /*
  1025. * ppp_recv_config - configure the receive-side characteristics of
  1026. * the ppp interface.
  1027. */
  1028. void
  1029. ppp_recv_config( int unit, int mru, u32_t asyncmap, int pcomp, int accomp)
  1030. {
  1031. PPPControl *pc = &pppControl[unit];
  1032. int i;
  1033. SYS_ARCH_DECL_PROTECT(lev);
  1034. LWIP_UNUSED_ARG(accomp);
  1035. LWIP_UNUSED_ARG(pcomp);
  1036. LWIP_UNUSED_ARG(mru);
  1037. /* Load the ACCM bits for the 32 control codes. */
  1038. SYS_ARCH_PROTECT(lev);
  1039. for (i = 0; i < 32 / 8; i++) {
  1040. /* @todo: does this work? ext_accm has been modified from pppd! */
  1041. pc->rx.inACCM[i] = (u_char)(asyncmap >> (i * 8));
  1042. }
  1043. SYS_ARCH_UNPROTECT(lev);
  1044. PPPDEBUG(LOG_INFO, ("ppp_recv_config[%d]: inACCM=%X %X %X %X\n",
  1045. unit,
  1046. pc->rx.inACCM[0], pc->rx.inACCM[1], pc->rx.inACCM[2], pc->rx.inACCM[3]));
  1047. }
  1048. #if 0
  1049. /*
  1050. * ccp_test - ask kernel whether a given compression method
  1051. * is acceptable for use. Returns 1 if the method and parameters
  1052. * are OK, 0 if the method is known but the parameters are not OK
  1053. * (e.g. code size should be reduced), or -1 if the method is unknown.
  1054. */
  1055. int
  1056. ccp_test( int unit, int opt_len, int for_transmit, u_char *opt_ptr)
  1057. {
  1058. return 0; /* XXX Currently no compression. */
  1059. }
  1060. /*
  1061. * ccp_flags_set - inform kernel about the current state of CCP.
  1062. */
  1063. void
  1064. ccp_flags_set(int unit, int isopen, int isup)
  1065. {
  1066. /* XXX */
  1067. }
  1068. /*
  1069. * ccp_fatal_error - returns 1 if decompression was disabled as a
  1070. * result of an error detected after decompression of a packet,
  1071. * 0 otherwise. This is necessary because of patent nonsense.
  1072. */
  1073. int
  1074. ccp_fatal_error(int unit)
  1075. {
  1076. /* XXX */
  1077. return 0;
  1078. }
  1079. #endif
  1080. /*
  1081. * get_idle_time - return how long the link has been idle.
  1082. */
  1083. int
  1084. get_idle_time(int u, struct ppp_idle *ip)
  1085. {
  1086. /* XXX */
  1087. LWIP_UNUSED_ARG(u);
  1088. LWIP_UNUSED_ARG(ip);
  1089. return 0;
  1090. }
  1091. /*
  1092. * Return user specified netmask, modified by any mask we might determine
  1093. * for address `addr' (in network byte order).
  1094. * Here we scan through the system's list of interfaces, looking for
  1095. * any non-point-to-point interfaces which might appear to be on the same
  1096. * network as `addr'. If we find any, we OR in their netmask to the
  1097. * user-specified netmask.
  1098. */
  1099. u32_t
  1100. GetMask(u32_t addr)
  1101. {
  1102. u32_t mask, nmask;
  1103. addr = htonl(addr);
  1104. if (IP_CLASSA(addr)) { /* determine network mask for address class */
  1105. nmask = IP_CLASSA_NET;
  1106. } else if (IP_CLASSB(addr)) {
  1107. nmask = IP_CLASSB_NET;
  1108. } else {
  1109. nmask = IP_CLASSC_NET;
  1110. }
  1111. /* class D nets are disallowed by bad_ip_adrs */
  1112. mask = subnetMask | htonl(nmask);
  1113. /* XXX
  1114. * Scan through the system's network interfaces.
  1115. * Get each netmask and OR them into our mask.
  1116. */
  1117. return mask;
  1118. }
  1119. /*
  1120. * sifvjcomp - config tcp header compression
  1121. */
  1122. int
  1123. sifvjcomp(int pd, int vjcomp, u8_t cidcomp, u8_t maxcid)
  1124. {
  1125. #if PPPOS_SUPPORT && VJ_SUPPORT
  1126. PPPControl *pc = &pppControl[pd];
  1127. pc->vjEnabled = vjcomp;
  1128. pc->vjComp.compressSlot = cidcomp;
  1129. pc->vjComp.maxSlotIndex = maxcid;
  1130. PPPDEBUG(LOG_INFO, ("sifvjcomp: VJ compress enable=%d slot=%d max slot=%d\n",
  1131. vjcomp, cidcomp, maxcid));
  1132. #else /* PPPOS_SUPPORT && VJ_SUPPORT */
  1133. LWIP_UNUSED_ARG(pd);
  1134. LWIP_UNUSED_ARG(vjcomp);
  1135. LWIP_UNUSED_ARG(cidcomp);
  1136. LWIP_UNUSED_ARG(maxcid);
  1137. #endif /* PPPOS_SUPPORT && VJ_SUPPORT */
  1138. return 0;
  1139. }
  1140. /*
  1141. * pppifNetifInit - netif init callback
  1142. */
  1143. static err_t
  1144. pppifNetifInit(struct netif *netif)
  1145. {
  1146. netif->name[0] = 'p';
  1147. netif->name[1] = 'p';
  1148. netif->output = pppifOutput;
  1149. netif->mtu = pppMTU((int)(size_t)netif->state);
  1150. netif->flags = NETIF_FLAG_POINTTOPOINT | NETIF_FLAG_LINK_UP;
  1151. #if LWIP_NETIF_HOSTNAME
  1152. /* @todo: Initialize interface hostname */
  1153. /* netif_set_hostname(netif, "lwip"); */
  1154. #endif /* LWIP_NETIF_HOSTNAME */
  1155. return ERR_OK;
  1156. }
  1157. /*
  1158. * sifup - Config the interface up and enable IP packets to pass.
  1159. */
  1160. int
  1161. sifup(int pd)
  1162. {
  1163. PPPControl *pc = &pppControl[pd];
  1164. int st = 1;
  1165. if (pd < 0 || pd >= NUM_PPP || !pc->openFlag) {
  1166. st = 0;
  1167. PPPDEBUG(LOG_WARNING, ("sifup[%d]: bad parms\n", pd));
  1168. } else {
  1169. netif_remove(&pc->netif);
  1170. if (netif_add(&pc->netif, &pc->addrs.our_ipaddr, &pc->addrs.netmask,
  1171. &pc->addrs.his_ipaddr, (void *)(size_t)pd, pppifNetifInit, ip_input)) {
  1172. netif_set_up(&pc->netif);
  1173. pc->if_up = 1;
  1174. pc->errCode = PPPERR_NONE;
  1175. PPPDEBUG(LOG_DEBUG, ("sifup: unit %d: linkStatusCB=%p errCode=%d\n", pd, pc->linkStatusCB, pc->errCode));
  1176. if (pc->linkStatusCB) {
  1177. pc->linkStatusCB(pc->linkStatusCtx, pc->errCode, &pc->addrs);
  1178. }
  1179. } else {
  1180. st = 0;
  1181. PPPDEBUG(LOG_ERR, ("sifup[%d]: netif_add failed\n", pd));
  1182. }
  1183. }
  1184. return st;
  1185. }
  1186. /*
  1187. * sifnpmode - Set the mode for handling packets for a given NP.
  1188. */
  1189. int
  1190. sifnpmode(int u, int proto, enum NPmode mode)
  1191. {
  1192. LWIP_UNUSED_ARG(u);
  1193. LWIP_UNUSED_ARG(proto);
  1194. LWIP_UNUSED_ARG(mode);
  1195. return 0;
  1196. }
  1197. /*
  1198. * sifdown - Config the interface down and disable IP.
  1199. */
  1200. int
  1201. sifdown(int pd)
  1202. {
  1203. PPPControl *pc = &pppControl[pd];
  1204. int st = 1;
  1205. if (pd < 0 || pd >= NUM_PPP || !pc->openFlag) {
  1206. st = 0;
  1207. PPPDEBUG(LOG_WARNING, ("sifdown[%d]: bad parms\n", pd));
  1208. } else {
  1209. pc->if_up = 0;
  1210. /* make sure the netif status callback is called */
  1211. netif_set_down(&pc->netif);
  1212. netif_remove(&pc->netif);
  1213. PPPDEBUG(LOG_DEBUG, ("sifdown: unit %d: linkStatusCB=%p errCode=%d\n", pd, pc->linkStatusCB, pc->errCode));
  1214. if (pc->linkStatusCB) {
  1215. pc->linkStatusCB(pc->linkStatusCtx, PPPERR_CONNECT, NULL);
  1216. }
  1217. }
  1218. return st;
  1219. }
  1220. /**
  1221. * sifaddr - Config the interface IP addresses and netmask.
  1222. * @param pd Interface unit ???
  1223. * @param o Our IP address ???
  1224. * @param h His IP address ???
  1225. * @param m IP subnet mask ???
  1226. * @param ns1 Primary DNS
  1227. * @param ns2 Secondary DNS
  1228. */
  1229. int
  1230. sifaddr( int pd, u32_t o, u32_t h, u32_t m, u32_t ns1, u32_t ns2)
  1231. {
  1232. PPPControl *pc = &pppControl[pd];
  1233. int st = 1;
  1234. if (pd < 0 || pd >= NUM_PPP || !pc->openFlag) {
  1235. st = 0;
  1236. PPPDEBUG(LOG_WARNING, ("sifup[%d]: bad parms\n", pd));
  1237. } else {
  1238. SMEMCPY(&pc->addrs.our_ipaddr, &o, sizeof(o));
  1239. SMEMCPY(&pc->addrs.his_ipaddr, &h, sizeof(h));
  1240. SMEMCPY(&pc->addrs.netmask, &m, sizeof(m));
  1241. SMEMCPY(&pc->addrs.dns1, &ns1, sizeof(ns1));
  1242. SMEMCPY(&pc->addrs.dns2, &ns2, sizeof(ns2));
  1243. }
  1244. return st;
  1245. }
  1246. /**
  1247. * cifaddr - Clear the interface IP addresses, and delete routes
  1248. * through the interface if possible.
  1249. * @param pd Interface unit ???
  1250. * @param o Our IP address ???
  1251. * @param h IP broadcast address ???
  1252. */
  1253. int
  1254. cifaddr( int pd, u32_t o, u32_t h)
  1255. {
  1256. PPPControl *pc = &pppControl[pd];
  1257. int st = 1;
  1258. LWIP_UNUSED_ARG(o);
  1259. LWIP_UNUSED_ARG(h);
  1260. if (pd < 0 || pd >= NUM_PPP || !pc->openFlag) {
  1261. st = 0;
  1262. PPPDEBUG(LOG_WARNING, ("sifup[%d]: bad parms\n", pd));
  1263. } else {
  1264. IP4_ADDR(&pc->addrs.our_ipaddr, 0,0,0,0);
  1265. IP4_ADDR(&pc->addrs.his_ipaddr, 0,0,0,0);
  1266. IP4_ADDR(&pc->addrs.netmask, 255,255,255,0);
  1267. IP4_ADDR(&pc->addrs.dns1, 0,0,0,0);
  1268. IP4_ADDR(&pc->addrs.dns2, 0,0,0,0);
  1269. }
  1270. return st;
  1271. }
  1272. /*
  1273. * sifdefaultroute - assign a default route through the address given.
  1274. */
  1275. int
  1276. sifdefaultroute(int pd, u32_t l, u32_t g)
  1277. {
  1278. PPPControl *pc = &pppControl[pd];
  1279. int st = 1;
  1280. LWIP_UNUSED_ARG(l);
  1281. LWIP_UNUSED_ARG(g);
  1282. if (pd < 0 || pd >= NUM_PPP || !pc->openFlag) {
  1283. st = 0;
  1284. PPPDEBUG(LOG_WARNING, ("sifup[%d]: bad parms\n", pd));
  1285. } else {
  1286. netif_set_default(&pc->netif);
  1287. }
  1288. /* TODO: check how PPP handled the netMask, previously not set by ipSetDefault */
  1289. return st;
  1290. }
  1291. /*
  1292. * cifdefaultroute - delete a default route through the address given.
  1293. */
  1294. int
  1295. cifdefaultroute(int pd, u32_t l, u32_t g)
  1296. {
  1297. PPPControl *pc = &pppControl[pd];
  1298. int st = 1;
  1299. LWIP_UNUSED_ARG(l);
  1300. LWIP_UNUSED_ARG(g);
  1301. if (pd < 0 || pd >= NUM_PPP || !pc->openFlag) {
  1302. st = 0;
  1303. PPPDEBUG(LOG_WARNING, ("sifup[%d]: bad parms\n", pd));
  1304. } else {
  1305. netif_set_default(NULL);
  1306. }
  1307. return st;
  1308. }
  1309. /**********************************/
  1310. /*** LOCAL FUNCTION DEFINITIONS ***/
  1311. /**********************************/
  1312. #if PPPOS_SUPPORT && PPP_INPROC_OWNTHREAD
  1313. /* The main PPP process function. This implements the state machine according
  1314. * to section 4 of RFC 1661: The Point-To-Point Protocol. */
  1315. static void
  1316. pppInputThread(void *arg)
  1317. {
  1318. int count;
  1319. PPPControlRx *pcrx = arg;
  1320. while (lcp_phase[pcrx->pd] != PHASE_DEAD) {
  1321. count = sio_read(pcrx->fd, pcrx->rxbuf, PPPOS_RX_BUFSIZE);
  1322. if(count > 0) {
  1323. pppInProc(pcrx, pcrx->rxbuf, count);
  1324. } else {
  1325. /* nothing received, give other tasks a chance to run */
  1326. sys_msleep(1);
  1327. }
  1328. }
  1329. }
  1330. #endif /* PPPOS_SUPPORT && PPP_INPROC_OWNTHREAD */
  1331. #if PPPOE_SUPPORT
  1332. void
  1333. pppOverEthernetInitFailed(int pd)
  1334. {
  1335. PPPControl* pc;
  1336. pppHup(pd);
  1337. pppStop(pd);
  1338. pc = &pppControl[pd];
  1339. pppoe_destroy(&pc->netif);
  1340. pc->openFlag = 0;
  1341. if(pc->linkStatusCB) {
  1342. pc->linkStatusCB(pc->linkStatusCtx, pc->errCode ? pc->errCode : PPPERR_PROTOCOL, NULL);
  1343. }
  1344. }
  1345. static void
  1346. pppOverEthernetLinkStatusCB(int pd, int up)
  1347. {
  1348. if(up) {
  1349. PPPDEBUG(LOG_INFO, ("pppOverEthernetLinkStatusCB: unit %d: Connecting\n", pd));
  1350. pppStart(pd);
  1351. } else {
  1352. pppOverEthernetInitFailed(pd);
  1353. }
  1354. }
  1355. #endif /* PPPOE_SUPPORT */
  1356. struct pbuf *
  1357. pppSingleBuf(struct pbuf *p)
  1358. {
  1359. struct pbuf *q, *b;
  1360. u_char *pl;
  1361. if(p->tot_len == p->len) {
  1362. return p;
  1363. }
  1364. q = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
  1365. if(!q) {
  1366. PPPDEBUG(LOG_ERR,
  1367. ("pppSingleBuf: unable to alloc new buf (%d)\n", p->tot_len));
  1368. return p; /* live dangerously */
  1369. }
  1370. for(b = p, pl = q->payload; b != NULL; b = b->next) {
  1371. MEMCPY(pl, b->payload, b->len);
  1372. pl += b->len;
  1373. }
  1374. pbuf_free(p);
  1375. return q;
  1376. }
  1377. struct pppInputHeader {
  1378. int unit;
  1379. u16_t proto;
  1380. };
  1381. /*
  1382. * Pass the processed input packet to the appropriate handler.
  1383. * This function and all handlers run in the context of the tcpip_thread
  1384. */
  1385. static void
  1386. pppInput(void *arg)
  1387. {
  1388. struct pbuf *nb = (struct pbuf *)arg;
  1389. u16_t protocol;
  1390. int pd;
  1391. pd = ((struct pppInputHeader *)nb->payload)->unit;
  1392. protocol = ((struct pppInputHeader *)nb->payload)->proto;
  1393. if(pbuf_header(nb, -(int)sizeof(struct pppInputHeader))) {
  1394. LWIP_ASSERT("pbuf_header failed\n", 0);
  1395. goto drop;
  1396. }
  1397. LINK_STATS_INC(link.recv);
  1398. snmp_inc_ifinucastpkts(&pppControl[pd].netif);
  1399. snmp_add_ifinoctets(&pppControl[pd].netif, nb->tot_len);
  1400. /*
  1401. * Toss all non-LCP packets unless LCP is OPEN.
  1402. * Until we get past the authentication phase, toss all packets
  1403. * except LCP, LQR and authentication packets.
  1404. */
  1405. if((lcp_phase[pd] <= PHASE_AUTHENTICATE) && (protocol != PPP_LCP)) {
  1406. if(!((protocol == PPP_LQR) || (protocol == PPP_PAP) || (protocol == PPP_CHAP)) ||
  1407. (lcp_phase[pd] != PHASE_AUTHENTICATE)) {
  1408. PPPDEBUG(LOG_INFO, ("pppInput: discarding proto 0x%"X16_F" in phase %d\n", protocol, lcp_phase[pd]));
  1409. goto drop;
  1410. }
  1411. }
  1412. switch(protocol) {
  1413. case PPP_VJC_COMP: /* VJ compressed TCP */
  1414. #if PPPOS_SUPPORT && VJ_SUPPORT
  1415. PPPDEBUG(LOG_INFO, ("pppInput[%d]: vj_comp in pbuf len=%d\n", pd, nb->len));
  1416. /*
  1417. * Clip off the VJ header and prepend the rebuilt TCP/IP header and
  1418. * pass the result to IP.
  1419. */
  1420. if ((vj_uncompress_tcp(&nb, &pppControl[pd].vjComp) >= 0) && (pppControl[pd].netif.input)) {
  1421. pppControl[pd].netif.input(nb, &pppControl[pd].netif);
  1422. return;
  1423. }
  1424. /* Something's wrong so drop it. */
  1425. PPPDEBUG(LOG_WARNING, ("pppInput[%d]: Dropping VJ compressed\n", pd));
  1426. #else /* PPPOS_SUPPORT && VJ_SUPPORT */
  1427. /* No handler for this protocol so drop the packet. */
  1428. PPPDEBUG(LOG_INFO, ("pppInput[%d]: drop VJ Comp in %d:%s\n", pd, nb->len, nb->payload));
  1429. #endif /* PPPOS_SUPPORT && VJ_SUPPORT */
  1430. break;
  1431. case PPP_VJC_UNCOMP: /* VJ uncompressed TCP */
  1432. #if PPPOS_SUPPORT && VJ_SUPPORT
  1433. PPPDEBUG(LOG_INFO, ("pppInput[%d]: vj_un in pbuf len=%d\n", pd, nb->len));
  1434. /*
  1435. * Process the TCP/IP header for VJ header compression and then pass
  1436. * the packet to IP.
  1437. */
  1438. if ((vj_uncompress_uncomp(nb, &pppControl[pd].vjComp) >= 0) && pppControl[pd].netif.input) {
  1439. pppControl[pd].netif.input(nb, &pppControl[pd].netif);
  1440. return;
  1441. }
  1442. /* Something's wrong so drop it. */
  1443. PPPDEBUG(LOG_WARNING, ("pppInput[%d]: Dropping VJ uncompressed\n", pd));
  1444. #else /* PPPOS_SUPPORT && VJ_SUPPORT */
  1445. /* No handler for this protocol so drop the packet. */
  1446. PPPDEBUG(LOG_INFO,
  1447. ("pppInput[%d]: drop VJ UnComp in %d:.*H\n",
  1448. pd, nb->len, LWIP_MIN(nb->len * 2, 40), nb->payload));
  1449. #endif /* PPPOS_SUPPORT && VJ_SUPPORT */
  1450. break;
  1451. case PPP_IP: /* Internet Protocol */
  1452. PPPDEBUG(LOG_INFO, ("pppInput[%d]: ip in pbuf len=%d\n", pd, nb->len));
  1453. if (pppControl[pd].netif.input) {
  1454. pppControl[pd].netif.input(nb, &pppControl[pd].netif);
  1455. return;
  1456. }
  1457. break;
  1458. default: {
  1459. struct protent *protp;
  1460. int i;
  1461. /*
  1462. * Upcall the proper protocol input routine.
  1463. */
  1464. for (i = 0; (protp = ppp_protocols[i]) != NULL; ++i) {
  1465. if (protp->protocol == protocol && protp->enabled_flag) {
  1466. PPPDEBUG(LOG_INFO, ("pppInput[%d]: %s len=%d\n", pd, protp->name, nb->len));
  1467. nb = pppSingleBuf(nb);
  1468. (*protp->input)(pd, nb->payload, nb->len);
  1469. PPPDEBUG(LOG_DETAIL, ("pppInput[%d]: packet processed\n", pd));
  1470. goto out;
  1471. }
  1472. }
  1473. /* No handler for this protocol so reject the packet. */
  1474. PPPDEBUG(LOG_INFO, ("pppInput[%d]: rejecting unsupported proto 0x%"X16_F" len=%d\n", pd, protocol, nb->len));
  1475. if (pbuf_header(nb, sizeof(protocol))) {
  1476. LWIP_ASSERT("pbuf_header failed\n", 0);
  1477. goto drop;
  1478. }
  1479. #if BYTE_ORDER == LITTLE_ENDIAN
  1480. protocol = htons(protocol);
  1481. #endif /* BYTE_ORDER == LITTLE_ENDIAN */
  1482. SMEMCPY(nb->payload, &protocol, sizeof(protocol));
  1483. lcp_sprotrej(pd, nb->payload, nb->len);
  1484. }
  1485. break;
  1486. }
  1487. drop:
  1488. LINK_STATS_INC(link.drop);
  1489. snmp_inc_ifindiscards(&pppControl[pd].netif);
  1490. out:
  1491. pbuf_free(nb);
  1492. return;
  1493. }
  1494. #if PPPOS_SUPPORT
  1495. /*
  1496. * Drop the input packet.
  1497. */
  1498. static void
  1499. pppFreeCurrentInputPacket(PPPControlRx *pcrx)
  1500. {
  1501. if (pcrx->inHead != NULL) {
  1502. if (pcrx->inTail && (pcrx->inTail != pcrx->inHead)) {
  1503. pbuf_free(pcrx->inTail);
  1504. }
  1505. pbuf_free(pcrx->inHead);
  1506. pcrx->inHead = NULL;
  1507. }
  1508. pcrx->inTail = NULL;
  1509. }
  1510. /*
  1511. * Drop the input packet and increase error counters.
  1512. */
  1513. static void
  1514. pppDrop(PPPControlRx *pcrx)
  1515. {
  1516. if (pcrx->inHead != NULL) {
  1517. #if 0
  1518. PPPDEBUG(LOG_INFO, ("pppDrop: %d:%.*H\n", pcrx->inHead->len, min(60, pcrx->inHead->len * 2), pcrx->inHead->payload));
  1519. #endif
  1520. PPPDEBUG(LOG_INFO, ("pppDrop: pbuf len=%d, addr %p\n", pcrx->inHead->len, (void*)pcrx->inHead));
  1521. }
  1522. pppFreeCurrentInputPacket(pcrx);
  1523. #if VJ_SUPPORT
  1524. vj_uncompress_err(&pppControl[pcrx->pd].vjComp);
  1525. #endif /* VJ_SUPPORT */
  1526. LINK_STATS_INC(link.drop);
  1527. snmp_inc_ifindiscards(&pppControl[pcrx->pd].netif);
  1528. }
  1529. #if !PPP_INPROC_OWNTHREAD
  1530. /** Pass received raw characters to PPPoS to be decoded. This function is
  1531. * thread-safe and can be called from a dedicated RX-thread or from a main-loop.
  1532. *
  1533. * @param pd PPP descriptor index, returned by pppOpen()
  1534. * @param data received data
  1535. * @param len length of received data
  1536. */
  1537. void
  1538. pppos_input(int pd, u_char* data, int len)
  1539. {
  1540. pppInProc(&pppControl[pd].rx, data, len);
  1541. }
  1542. #endif
  1543. /**
  1544. * Process a received octet string.
  1545. */
  1546. static void
  1547. pppInProc(PPPControlRx *pcrx, u_char *s, int l)
  1548. {
  1549. struct pbuf *nextNBuf;
  1550. u_char curChar;
  1551. u_char escaped;
  1552. SYS_ARCH_DECL_PROTECT(lev);
  1553. PPPDEBUG(LOG_DEBUG, ("pppInProc[%d]: got %d bytes\n", pcrx->pd, l));
  1554. while (l-- > 0) {
  1555. curChar = *s++;
  1556. SYS_ARCH_PROTECT(lev);
  1557. escaped = ESCAPE_P(pcrx->inACCM, curChar);
  1558. SYS_ARCH_UNPROTECT(lev);
  1559. /* Handle special characters. */
  1560. if (escaped) {
  1561. /* Check for escape sequences. */
  1562. /* XXX Note that this does not handle an escaped 0x5d character which
  1563. * would appear as an escape character. Since this is an ASCII ']'
  1564. * and there is no reason that I know of to escape it, I won't complicate
  1565. * the code to handle this case. GLL */
  1566. if (curChar == PPP_ESCAPE) {
  1567. pcrx->inEscaped = 1;
  1568. /* Check for the flag character. */
  1569. } else if (curChar == PPP_FLAG) {
  1570. /* If this is just an extra flag character, ignore it. */
  1571. if (pcrx->inState <= PDADDRESS) {
  1572. /* ignore it */;
  1573. /* If we haven't received the packet header, drop what has come in. */
  1574. } else if (pcrx->inState < PDDATA) {
  1575. PPPDEBUG(LOG_WARNING,
  1576. ("pppInProc[%d]: Dropping incomplete packet %d\n",
  1577. pcrx->pd, pcrx->inState));
  1578. LINK_STATS_INC(link.lenerr);
  1579. pppDrop(pcrx);
  1580. /* If the fcs is invalid, drop the packet. */
  1581. } else if (pcrx->inFCS != PPP_GOODFCS) {
  1582. PPPDEBUG(LOG_INFO,
  1583. ("pppInProc[%d]: Dropping bad fcs 0x%"X16_F" proto=0x%"X16_F"\n",
  1584. pcrx->pd, pcrx->inFCS, pcrx->inProtocol));
  1585. /* Note: If you get lots of these, check for UART frame errors or try different baud rate */
  1586. LINK_STATS_INC(link.chkerr);
  1587. pppDrop(pcrx);
  1588. /* Otherwise it's a good packet so pass it on. */
  1589. } else {
  1590. struct pbuf *inp;
  1591. /* Trim off the checksum. */
  1592. if(pcrx->inTail->len >= 2) {
  1593. pcrx->inTail->len -= 2;
  1594. pcrx->inTail->tot_len = pcrx->inTail->len;
  1595. if (pcrx->inTail != pcrx->inHead) {
  1596. pbuf_cat(pcrx->inHead, pcrx->inTail);
  1597. }
  1598. } else {
  1599. pcrx->inTail->tot_len = pcrx->inTail->len;
  1600. if (pcrx->inTail != pcrx->inHead) {
  1601. pbuf_cat(pcrx->inHead, pcrx->inTail);
  1602. }
  1603. pbuf_realloc(pcrx->inHead, pcrx->inHead->tot_len - 2);
  1604. }
  1605. /* Dispatch the packet thereby consuming it. */
  1606. inp = pcrx->inHead;
  1607. /* Packet consumed, release our references. */
  1608. pcrx->inHead = NULL;
  1609. pcrx->inTail = NULL;
  1610. #if PPP_INPROC_MULTITHREADED
  1611. if(tcpip_callback_with_block(pppInput, inp, 0) != ERR_OK) {
  1612. PPPDEBUG(LOG_ERR, ("pppInProc[%d]: tcpip_callback() failed, dropping packet\n", pcrx->pd));
  1613. pbuf_free(inp);
  1614. LINK_STATS_INC(link.drop);
  1615. snmp_inc_ifindiscards(&pppControl[pcrx->pd].netif);
  1616. }
  1617. #else /* PPP_INPROC_MULTITHREADED */
  1618. pppInput(inp);
  1619. #endif /* PPP_INPROC_MULTITHREADED */
  1620. }
  1621. /* Prepare for a new packet. */
  1622. pcrx->inFCS = PPP_INITFCS;
  1623. pcrx->inState = PDADDRESS;
  1624. pcrx->inEscaped = 0;
  1625. /* Other characters are usually control characters that may have
  1626. * been inserted by the physical layer so here we just drop them. */
  1627. } else {
  1628. PPPDEBUG(LOG_WARNING,
  1629. ("pppInProc[%d]: Dropping ACCM char <%d>\n", pcrx->pd, curChar));
  1630. }
  1631. /* Process other characters. */
  1632. } else {
  1633. /* Unencode escaped characters. */
  1634. if (pcrx->inEscaped) {
  1635. pcrx->inEscaped = 0;
  1636. curChar ^= PPP_TRANS;
  1637. }
  1638. /* Process character relative to current state. */
  1639. switch(pcrx->inState) {
  1640. case PDIDLE: /* Idle state - waiting. */
  1641. /* Drop the character if it's not 0xff
  1642. * we would have processed a flag character above. */
  1643. if (curChar != PPP_ALLSTATIONS) {
  1644. break;
  1645. }
  1646. /* Fall through */
  1647. case PDSTART: /* Process start flag. */
  1648. /* Prepare for a new packet. */
  1649. pcrx->inFCS = PPP_INITFCS;
  1650. /* Fall through */
  1651. case PDADDRESS: /* Process address field. */
  1652. if (curChar == PPP_ALLSTATIONS) {
  1653. pcrx->inState = PDCONTROL;
  1654. break;
  1655. }
  1656. /* Else assume compressed address and control fields so
  1657. * fall through to get the protocol... */
  1658. case PDCONTROL: /* Process control field. */
  1659. /* If we don't get a valid control code, restart. */
  1660. if (curChar == PPP_UI) {
  1661. pcrx->inState = PDPROTOCOL1;
  1662. break;
  1663. }
  1664. #if 0
  1665. else {
  1666. PPPDEBUG(LOG_WARNING,
  1667. ("pppInProc[%d]: Invalid control <%d>\n", pcrx->pd, curChar));
  1668. pcrx->inState = PDSTART;
  1669. }
  1670. #endif
  1671. case PDPROTOCOL1: /* Process protocol field 1. */
  1672. /* If the lower bit is set, this is the end of the protocol
  1673. * field. */
  1674. if (curChar & 1) {
  1675. pcrx->inProtocol = curChar;
  1676. pcrx->inState = PDDATA;
  1677. } else {
  1678. pcrx->inProtocol = (u_int)curChar << 8;
  1679. pcrx->inState = PDPROTOCOL2;
  1680. }
  1681. break;
  1682. case PDPROTOCOL2: /* Process protocol field 2. */
  1683. pcrx->inProtocol |= curChar;
  1684. pcrx->inState = PDDATA;
  1685. break;
  1686. case PDDATA: /* Process data byte. */
  1687. /* Make space to receive processed data. */
  1688. if (pcrx->inTail == NULL || pcrx->inTail->len == PBUF_POOL_BUFSIZE) {
  1689. if (pcrx->inTail != NULL) {
  1690. pcrx->inTail->tot_len = pcrx->inTail->len;
  1691. if (pcrx->inTail != pcrx->inHead) {
  1692. pbuf_cat(pcrx->inHead, pcrx->inTail);
  1693. /* give up the inTail reference now */
  1694. pcrx->inTail = NULL;
  1695. }
  1696. }
  1697. /* If we haven't started a packet, we need a packet header. */
  1698. nextNBuf = pbuf_alloc(PBUF_RAW, 0, PBUF_POOL);
  1699. if (nextNBuf == NULL) {
  1700. /* No free buffers. Drop the input packet and let the
  1701. * higher layers deal with it. Continue processing
  1702. * the received pbuf chain in case a new packet starts. */
  1703. PPPDEBUG(LOG_ERR, ("pppInProc[%d]: NO FREE MBUFS!\n", pcrx->pd));
  1704. LINK_STATS_INC(link.memerr);
  1705. pppDrop(pcrx);
  1706. pcrx->inState = PDSTART; /* Wait for flag sequence. */
  1707. break;
  1708. }
  1709. if (pcrx->inHead == NULL) {
  1710. struct pppInputHeader *pih = nextNBuf->payload;
  1711. pih->unit = pcrx->pd;
  1712. pih->proto = pcrx->inProtocol;
  1713. nextNBuf->len += sizeof(*pih);
  1714. pcrx->inHead = nextNBuf;
  1715. }
  1716. pcrx->inTail = nextNBuf;
  1717. }
  1718. /* Load character into buffer. */
  1719. ((u_char*)pcrx->inTail->payload)[pcrx->inTail->len++] = curChar;
  1720. break;
  1721. }
  1722. /* update the frame check sequence number. */
  1723. pcrx->inFCS = PPP_FCS(pcrx->inFCS, curChar);
  1724. }
  1725. } /* while (l-- > 0), all bytes processed */
  1726. avRandomize();
  1727. }
  1728. #endif /* PPPOS_SUPPORT */
  1729. #if PPPOE_SUPPORT
  1730. void
  1731. pppInProcOverEthernet(int pd, struct pbuf *pb)
  1732. {
  1733. struct pppInputHeader *pih;
  1734. u16_t inProtocol;
  1735. if(pb->len < sizeof(inProtocol)) {
  1736. PPPDEBUG(LOG_ERR, ("pppInProcOverEthernet: too small for protocol field\n"));
  1737. goto drop;
  1738. }
  1739. inProtocol = (((u8_t *)pb->payload)[0] << 8) | ((u8_t*)pb->payload)[1];
  1740. /* make room for pppInputHeader - should not fail */
  1741. if (pbuf_header(pb, sizeof(*pih) - sizeof(inProtocol)) != 0) {
  1742. PPPDEBUG(LOG_ERR, ("pppInProcOverEthernet: could not allocate room for header\n"));
  1743. goto drop;
  1744. }
  1745. pih = pb->payload;
  1746. pih->unit = pd;
  1747. pih->proto = inProtocol;
  1748. /* Dispatch the packet thereby consuming it. */
  1749. pppInput(pb);
  1750. return;
  1751. drop:
  1752. LINK_STATS_INC(link.drop);
  1753. snmp_inc_ifindiscards(&pppControl[pd].netif);
  1754. pbuf_free(pb);
  1755. return;
  1756. }
  1757. #endif /* PPPOE_SUPPORT */
  1758. #if LWIP_NETIF_STATUS_CALLBACK
  1759. /** Set the status callback of a PPP's netif
  1760. *
  1761. * @param pd The PPP descriptor returned by pppOpen()
  1762. * @param status_callback pointer to the status callback function
  1763. *
  1764. * @see netif_set_status_callback
  1765. */
  1766. void
  1767. ppp_set_netif_statuscallback(int pd, netif_status_callback_fn status_callback)
  1768. {
  1769. netif_set_status_callback(&pppControl[pd].netif, status_callback);
  1770. }
  1771. #endif /* LWIP_NETIF_STATUS_CALLBACK */
  1772. #if LWIP_NETIF_LINK_CALLBACK
  1773. /** Set the link callback of a PPP's netif
  1774. *
  1775. * @param pd The PPP descriptor returned by pppOpen()
  1776. * @param link_callback pointer to the link callback function
  1777. *
  1778. * @see netif_set_link_callback
  1779. */
  1780. void
  1781. ppp_set_netif_linkcallback(int pd, netif_status_callback_fn link_callback)
  1782. {
  1783. netif_set_link_callback(&pppControl[pd].netif, link_callback);
  1784. }
  1785. #endif /* LWIP_NETIF_LINK_CALLBACK */
  1786. #endif /* PPP_SUPPORT */