ipcpin.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*
  2. * Copyright (C) 2001-2004 by egnite Software GmbH
  3. * Copyright (c) 1989 by Carnegie Mellon University
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. */
  35. /*!
  36. * \file net/ipcin.c
  37. * \brief PPP IPC input functions.
  38. *
  39. * \verbatim
  40. * $Id: ipcpin.c 5505 2014-01-01 11:15:16Z mifi $
  41. * \endverbatim
  42. */
  43. #include <sys/event.h>
  44. #include <sys/types.h>
  45. #include <net/if_var.h>
  46. #include <dev/ppp.h>
  47. #include <netinet/if_ppp.h>
  48. #include <netinet/ppp_fsm.h>
  49. #include <netinet/in.h>
  50. /*!
  51. * \addtogroup xgIPCP
  52. */
  53. /*@{*/
  54. static uint16_t IpcpValidateIpReq(uint32_t *expected_ip, uint32_t *requested_ip)
  55. {
  56. if (*expected_ip == 0 && *requested_ip) {
  57. *expected_ip = *requested_ip;
  58. }
  59. else if (*expected_ip != *requested_ip) {
  60. *requested_ip = *expected_ip;
  61. return 6;
  62. }
  63. return 0;
  64. }
  65. /*
  66. * Received Configure-Request.
  67. */
  68. static void IpcpRxConfReq(NUTDEVICE * dev, uint8_t id, NETBUF * nb)
  69. {
  70. PPPDCB *dcb = dev->dev_dcb;
  71. int rc = XCP_CONFACK;
  72. XCPOPT *xcpo;
  73. uint16_t xcpl;
  74. XCPOPT *xcpr;
  75. uint16_t xcps;
  76. uint16_t len = 0;
  77. uint_fast8_t i;
  78. uint32_t ip;
  79. switch (dcb->dcb_ipcp_state) {
  80. case PPPS_CLOSED:
  81. /*
  82. * Go away, we're closed.
  83. */
  84. NutNetBufFree(nb);
  85. NutIpcpOutput(dev, XCP_TERMACK, id, 0);
  86. return;
  87. case PPPS_CLOSING:
  88. case PPPS_STOPPING:
  89. /*
  90. * Ignore if we are going down.
  91. */
  92. NutNetBufFree(nb);
  93. return;
  94. case PPPS_OPENED:
  95. /*
  96. * Go down and restart negotiation.
  97. */
  98. IpcpLowerDown(dev);
  99. IpcpTxConfReq(dev, ++dcb->dcb_reqid);
  100. break;
  101. case PPPS_STOPPED:
  102. /*
  103. * Negotiation started by our peer.
  104. */
  105. IpcpTxConfReq(dev, ++dcb->dcb_reqid);
  106. dcb->dcb_ipcp_state = PPPS_REQSENT;
  107. break;
  108. }
  109. /*
  110. * Check if there is anything to reject.
  111. */
  112. xcpo = nb->nb_ap.vp;
  113. xcpl = nb->nb_ap.sz;
  114. xcpr = nb->nb_ap.vp;
  115. xcps = 0;
  116. while (xcpl >= 2) {
  117. len = xcpo->xcpo_len;
  118. if (len > xcpl)
  119. len = xcpl;
  120. else {
  121. switch (xcpo->xcpo_type) {
  122. case IPCP_MS_DNS1:
  123. if (xcpo->xcpo_.ul == 0 && dcb->dcb_ip_dns1 == 0) {
  124. break;
  125. }
  126. case IPCP_MS_DNS2:
  127. if (xcpo->xcpo_.ul == 0 && dcb->dcb_ip_dns2 == 0) {
  128. break;
  129. }
  130. case IPCP_ADDR:
  131. if (xcpo->xcpo_len == 6)
  132. len = 0;
  133. break;
  134. }
  135. }
  136. if (len) {
  137. if (xcpr != xcpo) {
  138. xcpr->xcpo_type = xcpo->xcpo_type;
  139. xcpr->xcpo_len = len;
  140. for (i = 0; i < len - 2; i++)
  141. xcpr->xcpo_.uc[i] = xcpo->xcpo_.uc[i];
  142. }
  143. xcpr = (XCPOPT *) ((char *) xcpr + len);
  144. xcps += len;
  145. }
  146. xcpl -= xcpo->xcpo_len;
  147. xcpo = (XCPOPT *) ((char *) xcpo + xcpo->xcpo_len);
  148. }
  149. if (xcps) {
  150. nb->nb_ap.sz = xcps;
  151. rc = XCP_CONFREJ;
  152. }
  153. /*
  154. * Check if there is anything to negotiate.
  155. */
  156. else {
  157. xcpo = nb->nb_ap.vp;
  158. xcpl = nb->nb_ap.sz;
  159. xcpr = nb->nb_ap.vp;
  160. xcps = 0;
  161. len = 0;
  162. while (xcpl >= 2) {
  163. ip = xcpo->xcpo_.ul;
  164. switch (xcpo->xcpo_type) {
  165. case IPCP_ADDR:
  166. len = IpcpValidateIpReq(&dcb->dcb_remote_ip, &ip);
  167. break;
  168. case IPCP_MS_DNS1:
  169. len = IpcpValidateIpReq(&dcb->dcb_ip_dns1, &ip);
  170. break;
  171. case IPCP_MS_DNS2:
  172. len = IpcpValidateIpReq(&dcb->dcb_ip_dns2, &ip);
  173. break;
  174. }
  175. if (len) {
  176. if (xcpr != xcpo) {
  177. xcpr->xcpo_type = xcpo->xcpo_type;
  178. xcpr->xcpo_len = len;
  179. }
  180. xcpr->xcpo_.ul = ip;
  181. xcpr = (XCPOPT *) ((char *) xcpr + len);
  182. xcps += len;
  183. len = 0;
  184. }
  185. xcpl -= xcpo->xcpo_len;
  186. xcpo = (XCPOPT *) ((char *) xcpo + xcpo->xcpo_len);
  187. }
  188. if (xcps) {
  189. nb->nb_ap.sz = xcps;
  190. rc = XCP_CONFNAK;
  191. }
  192. }
  193. NutIpcpOutput(dev, rc, id, nb);
  194. if (rc == XCP_CONFACK) {
  195. if (dcb->dcb_ipcp_state == PPPS_ACKRCVD) {
  196. dcb->dcb_ipcp_state = PPPS_OPENED;
  197. NutEventPost(&dcb->dcb_state_chg);
  198. } else
  199. dcb->dcb_ipcp_state = PPPS_ACKSENT;
  200. dcb->dcb_ipcp_naks = 0;
  201. } else if (dcb->dcb_ipcp_state != PPPS_ACKRCVD)
  202. dcb->dcb_ipcp_state = PPPS_REQSENT;
  203. }
  204. /*
  205. * Configure-Ack received.
  206. */
  207. static void IpcpRxConfAck(NUTDEVICE * dev, uint8_t id, NETBUF * nb)
  208. {
  209. PPPDCB *dcb = dev->dev_dcb;
  210. XCPOPT *xcpo;
  211. uint16_t xcpl;
  212. /*
  213. * Ignore, if we are not expecting this id.
  214. */
  215. if (id != dcb->dcb_reqid || dcb->dcb_acked) {
  216. return;
  217. }
  218. switch (dcb->dcb_ipcp_state) {
  219. case PPPS_CLOSED:
  220. case PPPS_STOPPED:
  221. /*
  222. * Go away, we're closed.
  223. */
  224. NutNetBufFree(nb);
  225. NutIpcpOutput(dev, XCP_TERMACK, id, 0);
  226. return;
  227. case PPPS_REQSENT:
  228. dcb->dcb_ipcp_state = PPPS_ACKRCVD;
  229. dcb->dcb_retries = 0;
  230. break;
  231. case PPPS_ACKRCVD:
  232. /* Huh? an extra valid Ack? oh well... */
  233. IpcpTxConfReq(dev, ++dcb->dcb_reqid);
  234. dcb->dcb_ipcp_state = PPPS_REQSENT;
  235. break;
  236. case PPPS_ACKSENT:
  237. dcb->dcb_ipcp_state = PPPS_OPENED;
  238. dcb->dcb_retries = 0;
  239. NutEventPost(&dcb->dcb_state_chg);
  240. break;
  241. case PPPS_OPENED:
  242. /* Go down and restart negotiation */
  243. IpcpLowerDown(dev);
  244. IpcpTxConfReq(dev, ++dcb->dcb_reqid);
  245. dcb->dcb_ipcp_state = PPPS_REQSENT;
  246. break;
  247. }
  248. xcpo = nb->nb_ap.vp;
  249. xcpl = nb->nb_ap.sz;
  250. while (xcpl >= 2) {
  251. switch (xcpo->xcpo_type) {
  252. case IPCP_ADDR:
  253. if (xcpo->xcpo_.ul)
  254. dcb->dcb_local_ip = xcpo->xcpo_.ul;
  255. break;
  256. case IPCP_COMPRESSTYPE:
  257. break;
  258. case IPCP_MS_DNS1:
  259. if (xcpo->xcpo_.ul)
  260. dcb->dcb_ip_dns1 = xcpo->xcpo_.ul;
  261. break;
  262. case IPCP_MS_DNS2:
  263. /* Fixed secondary DNS bug, thanks to Tarmo Fimberg
  264. and Jelle Martijn Kok. */
  265. if (xcpo->xcpo_.ul)
  266. dcb->dcb_ip_dns2 = xcpo->xcpo_.ul;
  267. break;
  268. }
  269. xcpl -= xcpo->xcpo_len;
  270. xcpo = (XCPOPT *) ((char *) xcpo + xcpo->xcpo_len);
  271. }
  272. dcb->dcb_acked = 1;
  273. NutNetBufFree(nb);
  274. }
  275. /*
  276. * Configure-Nak or Configure-Reject received.
  277. */
  278. static void IpcpRxConfNakRej(NUTDEVICE * dev, uint8_t id, NETBUF * nb, uint8_t rejected)
  279. {
  280. PPPDCB *dcb = dev->dev_dcb;
  281. XCPOPT *xcpo;
  282. uint16_t xcpl;
  283. /*
  284. * Ignore, if we are not expecting this id.
  285. */
  286. if (id != dcb->dcb_reqid || dcb->dcb_acked) {
  287. NutNetBufFree(nb);
  288. return;
  289. }
  290. switch (dcb->dcb_ipcp_state) {
  291. case PPPS_CLOSED:
  292. case PPPS_STOPPED:
  293. /*
  294. * Go away, we're closed.
  295. */
  296. NutNetBufFree(nb);
  297. NutIpcpOutput(dev, XCP_TERMACK, id, 0);
  298. return;
  299. case PPPS_REQSENT:
  300. case PPPS_ACKSENT:
  301. case PPPS_ACKRCVD:
  302. case PPPS_OPENED:
  303. break;
  304. default:
  305. NutNetBufFree(nb);
  306. return;
  307. }
  308. dcb->dcb_acked = 1;
  309. xcpo = nb->nb_ap.vp;
  310. xcpl = nb->nb_ap.sz;
  311. while (xcpl >= 2) {
  312. switch (xcpo->xcpo_type) {
  313. case IPCP_ADDR:
  314. if (xcpo->xcpo_.ul)
  315. dcb->dcb_local_ip = xcpo->xcpo_.ul;
  316. break;
  317. case IPCP_COMPRESSTYPE:
  318. break;
  319. case IPCP_MS_DNS1:
  320. if (rejected)
  321. dcb->dcb_rejects |= REJ_IPCP_DNS1;
  322. else if (xcpo->xcpo_.ul)
  323. dcb->dcb_ip_dns1 = xcpo->xcpo_.ul;
  324. break;
  325. case IPCP_MS_DNS2:
  326. if (rejected)
  327. dcb->dcb_rejects |= REJ_IPCP_DNS2;
  328. else if (xcpo->xcpo_.ul)
  329. dcb->dcb_ip_dns2 = xcpo->xcpo_.ul;
  330. break;
  331. }
  332. xcpl -= xcpo->xcpo_len;
  333. xcpo = (XCPOPT *) ((char *) xcpo + xcpo->xcpo_len);
  334. }
  335. NutNetBufFree(nb);
  336. switch (dcb->dcb_ipcp_state) {
  337. case PPPS_REQSENT:
  338. case PPPS_ACKSENT:
  339. /* They didn't agree to what we wanted - try another request */
  340. IpcpTxConfReq(dev, ++dcb->dcb_reqid);
  341. break;
  342. case PPPS_ACKRCVD:
  343. /* Got a Nak/reject when we had already had an Ack?? oh well... */
  344. IpcpTxConfReq(dev, ++dcb->dcb_reqid);
  345. dcb->dcb_ipcp_state = PPPS_REQSENT;
  346. break;
  347. case PPPS_OPENED:
  348. /* Go down and restart negotiation */
  349. IpcpLowerDown(dev);
  350. IpcpTxConfReq(dev, ++dcb->dcb_reqid);
  351. dcb->dcb_ipcp_state = PPPS_REQSENT;
  352. break;
  353. }
  354. }
  355. /*
  356. * \brief Terminate request received.
  357. */
  358. static void IpcpRxTermReq(NUTDEVICE * dev, uint8_t id, NETBUF * nb)
  359. {
  360. PPPDCB *dcb = dev->dev_dcb;
  361. NutNetBufFree(nb);
  362. switch (dcb->dcb_ipcp_state) {
  363. case PPPS_ACKRCVD:
  364. case PPPS_ACKSENT:
  365. dcb->dcb_ipcp_state = PPPS_REQSENT;
  366. break;
  367. case PPPS_OPENED:
  368. IpcpLowerDown(dev);
  369. dcb->dcb_ipcp_state = PPPS_STOPPING;
  370. break;
  371. }
  372. NutIpcpOutput(dev, XCP_TERMACK, id, 0);
  373. }
  374. /*
  375. * Terminate-Ack received.
  376. */
  377. static void IpcpRxTermAck(NUTDEVICE * dev, uint8_t id, NETBUF * nb)
  378. {
  379. PPPDCB *dcb = dev->dev_dcb;
  380. (void)id;
  381. (void)nb;
  382. switch (dcb->dcb_ipcp_state) {
  383. case PPPS_CLOSING:
  384. dcb->dcb_ipcp_state = PPPS_CLOSED;
  385. break;
  386. case PPPS_STOPPING:
  387. dcb->dcb_ipcp_state = PPPS_STOPPED;
  388. break;
  389. case PPPS_ACKRCVD:
  390. dcb->dcb_ipcp_state = PPPS_REQSENT;
  391. break;
  392. case PPPS_OPENED:
  393. IpcpLowerDown(dev);
  394. IpcpTxConfReq(dev, ++dcb->dcb_reqid);
  395. break;
  396. }
  397. }
  398. /*
  399. * Peer doesn't speak this protocol.
  400. *
  401. * Treat this as a catastrophic error (RXJ-).
  402. */
  403. void IpcpRxProtRej(NUTDEVICE * dev)
  404. {
  405. PPPDCB *dcb = dev->dev_dcb;
  406. switch (dcb->dcb_ipcp_state) {
  407. case PPPS_CLOSING:
  408. case PPPS_CLOSED:
  409. dcb->dcb_ipcp_state = PPPS_CLOSED;
  410. break;
  411. case PPPS_STOPPING:
  412. case PPPS_REQSENT:
  413. case PPPS_ACKRCVD:
  414. case PPPS_ACKSENT:
  415. case PPPS_STOPPED:
  416. dcb->dcb_ipcp_state = PPPS_STOPPED;
  417. break;
  418. case PPPS_OPENED:
  419. IpcpLowerDown(dev);
  420. NutIpcpOutput(dev, XCP_TERMREQ, dcb->dcb_reqid, 0);
  421. dcb->dcb_ipcp_state = PPPS_STOPPING;
  422. break;
  423. }
  424. }
  425. /*
  426. * Receive an Code-Reject.
  427. */
  428. static void IpcpRxCodeRej(NUTDEVICE * dev, uint8_t id, NETBUF * nb)
  429. {
  430. PPPDCB *dcb = dev->dev_dcb;
  431. (void)id;
  432. NutNetBufFree(nb);
  433. if (dcb->dcb_ipcp_state == PPPS_ACKRCVD)
  434. dcb->dcb_ipcp_state = PPPS_REQSENT;
  435. }
  436. /*!
  437. * \brief Handle incoming IPCP packets.
  438. *
  439. * Packets not destined to us or packets with unsupported
  440. * address type or item length are silently discarded.
  441. *
  442. * \note This routine is called by the Ethernet layer on
  443. * incoming ARP packets. Applications typically do
  444. * not call this function.
  445. *
  446. * \param dev Identifies the device that received the packet.
  447. * \param nb Pointer to a network buffer structure containing
  448. * the ARP packet.
  449. */
  450. void NutIpcpInput(NUTDEVICE * dev, NETBUF * nb)
  451. {
  452. XCPHDR *xch;
  453. PPPDCB *dcb = dev->dev_dcb;
  454. uint16_t len;
  455. /*
  456. * Discard packets with illegal lengths.
  457. */
  458. if (nb->nb_nw.sz < sizeof(XCPHDR)) {
  459. NutNetBufFree(nb);
  460. return;
  461. }
  462. xch = (XCPHDR *) nb->nb_nw.vp;
  463. if ((len = htons(xch->xch_len)) < sizeof(XCPHDR) || len > nb->nb_nw.sz) {
  464. NutNetBufFree(nb);
  465. return;
  466. }
  467. /*
  468. * Discard all packets while we are in initial or starting state.
  469. */
  470. if (dcb->dcb_ipcp_state == PPPS_INITIAL || dcb->dcb_ipcp_state == PPPS_STARTING) {
  471. NutNetBufFree(nb);
  472. return;
  473. }
  474. /*
  475. * Split the IPCP packet.
  476. */
  477. nb->nb_ap.vp = xch + 1;
  478. nb->nb_ap.sz = htons(xch->xch_len) - sizeof(XCPHDR);
  479. /*
  480. * Action depends on code.
  481. */
  482. switch (xch->xch_code) {
  483. case XCP_CONFREQ:
  484. IpcpRxConfReq(dev, xch->xch_id, nb);
  485. break;
  486. case XCP_CONFACK:
  487. IpcpRxConfAck(dev, xch->xch_id, nb);
  488. break;
  489. case XCP_CONFNAK:
  490. IpcpRxConfNakRej(dev, xch->xch_id, nb, 0);
  491. break;
  492. case XCP_CONFREJ:
  493. IpcpRxConfNakRej(dev, xch->xch_id, nb, 1);
  494. break;
  495. case XCP_TERMREQ:
  496. IpcpRxTermReq(dev, xch->xch_id, nb);
  497. break;
  498. case XCP_TERMACK:
  499. IpcpRxTermAck(dev, xch->xch_id, nb);
  500. break;
  501. case XCP_CODEREJ:
  502. IpcpRxCodeRej(dev, xch->xch_id, nb);
  503. break;
  504. default:
  505. /*
  506. * TODO: Send code reject.
  507. */
  508. NutNetBufFree(nb);
  509. break;
  510. }
  511. }
  512. /*@}*/