netif.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /**
  2. * @file
  3. * lwIP network interface abstraction
  4. *
  5. */
  6. /*
  7. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without modification,
  11. * are permitted provided that the following conditions are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. The name of the author may not be used to endorse or promote products
  19. * derived from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  24. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  26. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  29. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. * OF SUCH DAMAGE.
  31. *
  32. * This file is part of the lwIP TCP/IP stack.
  33. *
  34. * Author: Adam Dunkels <adam@sics.se>
  35. *
  36. */
  37. #include "lwip/opt.h"
  38. #include "lwip/def.h"
  39. #include "lwip/ip_addr.h"
  40. #include "lwip/netif.h"
  41. #include "lwip/tcp.h"
  42. #include "lwip/snmp.h"
  43. #include "lwip/igmp.h"
  44. #include "netif/etharp.h"
  45. #if ENABLE_LOOPBACK
  46. #include "lwip/sys.h"
  47. #if LWIP_NETIF_LOOPBACK_MULTITHREADING
  48. #include "lwip/tcpip.h"
  49. #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
  50. #endif /* ENABLE_LOOPBACK */
  51. #if LWIP_AUTOIP
  52. #include "lwip/autoip.h"
  53. #endif /* LWIP_AUTOIP */
  54. #if LWIP_DHCP
  55. #include "lwip/dhcp.h"
  56. #endif /* LWIP_DHCP */
  57. #if LWIP_NETIF_STATUS_CALLBACK
  58. #define NETIF_STATUS_CALLBACK(n) { if (n->status_callback) (n->status_callback)(n); }
  59. #else
  60. #define NETIF_STATUS_CALLBACK(n) { /* NOP */ }
  61. #endif /* LWIP_NETIF_STATUS_CALLBACK */
  62. #if LWIP_NETIF_LINK_CALLBACK
  63. #define NETIF_LINK_CALLBACK(n) { if (n->link_callback) (n->link_callback)(n); }
  64. #else
  65. #define NETIF_LINK_CALLBACK(n) { /* NOP */ }
  66. #endif /* LWIP_NETIF_LINK_CALLBACK */
  67. struct netif *netif_list;
  68. struct netif *netif_default;
  69. /**
  70. * Add a network interface to the list of lwIP netifs.
  71. *
  72. * @param netif a pre-allocated netif structure
  73. * @param ipaddr IP address for the new netif
  74. * @param netmask network mask for the new netif
  75. * @param gw default gateway IP address for the new netif
  76. * @param state opaque data passed to the new netif
  77. * @param init callback function that initializes the interface
  78. * @param input callback function that is called to pass
  79. * ingress packets up in the protocol layer stack.
  80. *
  81. * @return netif, or NULL if failed.
  82. */
  83. struct netif *
  84. netif_add(struct netif *netif, struct ip_addr *ipaddr, struct ip_addr *netmask,
  85. struct ip_addr *gw,
  86. void *state,
  87. err_t (* init)(struct netif *netif),
  88. err_t (* input)(struct pbuf *p, struct netif *netif))
  89. {
  90. static u8_t netifnum = 0;
  91. /* reset new interface configuration state */
  92. netif->ip_addr.addr = 0;
  93. netif->netmask.addr = 0;
  94. netif->gw.addr = 0;
  95. /* netif->flags = 0; */
  96. #if LWIP_DHCP
  97. /* netif not under DHCP control by default */
  98. netif->dhcp = NULL;
  99. #endif /* LWIP_DHCP */
  100. #if LWIP_AUTOIP
  101. /* netif not under AutoIP control by default */
  102. netif->autoip = NULL;
  103. #endif /* LWIP_AUTOIP */
  104. #if LWIP_NETIF_STATUS_CALLBACK
  105. netif->status_callback = NULL;
  106. #endif /* LWIP_NETIF_STATUS_CALLBACK */
  107. #if LWIP_NETIF_LINK_CALLBACK
  108. netif->link_callback = NULL;
  109. #endif /* LWIP_NETIF_LINK_CALLBACK */
  110. #if LWIP_IGMP
  111. netif->igmp_mac_filter = NULL;
  112. #endif /* LWIP_IGMP */
  113. #if ENABLE_LOOPBACK
  114. netif->loop_first = NULL;
  115. netif->loop_last = NULL;
  116. #endif /* ENABLE_LOOPBACK */
  117. /* remember netif specific state information data */
  118. netif->state = state;
  119. netif->num = netifnum++;
  120. netif->input = input;
  121. #if LWIP_NETIF_HWADDRHINT
  122. netif->addr_hint = NULL;
  123. #endif /* LWIP_NETIF_HWADDRHINT*/
  124. #if ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS
  125. netif->loop_cnt_current = 0;
  126. #endif /* ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS */
  127. netif_set_addr(netif, ipaddr, netmask, gw);
  128. /* call user specified initialization function for netif */
  129. if (init(netif) != ERR_OK) {
  130. return NULL;
  131. }
  132. /* add this netif to the list */
  133. netif->next = netif_list;
  134. netif_list = netif;
  135. snmp_inc_iflist();
  136. #if LWIP_IGMP
  137. /* start IGMP processing */
  138. if (netif->flags & NETIF_FLAG_IGMP) {
  139. igmp_start( netif);
  140. }
  141. #endif /* LWIP_IGMP */
  142. LWIP_DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP addr ",
  143. netif->name[0], netif->name[1]));
  144. ip_addr_debug_print(NETIF_DEBUG, ipaddr);
  145. LWIP_DEBUGF(NETIF_DEBUG, (" netmask "));
  146. ip_addr_debug_print(NETIF_DEBUG, netmask);
  147. LWIP_DEBUGF(NETIF_DEBUG, (" gw "));
  148. ip_addr_debug_print(NETIF_DEBUG, gw);
  149. LWIP_DEBUGF(NETIF_DEBUG, ("\n"));
  150. return netif;
  151. }
  152. /**
  153. * Change IP address configuration for a network interface (including netmask
  154. * and default gateway).
  155. *
  156. * @param netif the network interface to change
  157. * @param ipaddr the new IP address
  158. * @param netmask the new netmask
  159. * @param gw the new default gateway
  160. */
  161. void
  162. netif_set_addr(struct netif *netif, struct ip_addr *ipaddr, struct ip_addr *netmask,
  163. struct ip_addr *gw)
  164. {
  165. netif_set_ipaddr(netif, ipaddr);
  166. netif_set_netmask(netif, netmask);
  167. netif_set_gw(netif, gw);
  168. }
  169. /**
  170. * Remove a network interface from the list of lwIP netifs.
  171. *
  172. * @param netif the network interface to remove
  173. */
  174. void netif_remove(struct netif * netif)
  175. {
  176. if ( netif == NULL ) return;
  177. #if LWIP_IGMP
  178. /* stop IGMP processing */
  179. if (netif->flags & NETIF_FLAG_IGMP) {
  180. igmp_stop( netif);
  181. }
  182. #endif /* LWIP_IGMP */
  183. snmp_delete_ipaddridx_tree(netif);
  184. /* is it the first netif? */
  185. if (netif_list == netif) {
  186. netif_list = netif->next;
  187. snmp_dec_iflist();
  188. }
  189. else {
  190. /* look for netif further down the list */
  191. struct netif * tmpNetif;
  192. for (tmpNetif = netif_list; tmpNetif != NULL; tmpNetif = tmpNetif->next) {
  193. if (tmpNetif->next == netif) {
  194. tmpNetif->next = netif->next;
  195. snmp_dec_iflist();
  196. break;
  197. }
  198. }
  199. if (tmpNetif == NULL)
  200. return; /* we didn't find any netif today */
  201. }
  202. /* this netif is default? */
  203. if (netif_default == netif)
  204. /* reset default netif */
  205. netif_set_default(NULL);
  206. LWIP_DEBUGF( NETIF_DEBUG, ("netif_remove: removed netif\n") );
  207. }
  208. /**
  209. * Find a network interface by searching for its name
  210. *
  211. * @param name the name of the netif (like netif->name) plus concatenated number
  212. * in ascii representation (e.g. 'en0')
  213. */
  214. struct netif *
  215. netif_find(char *name)
  216. {
  217. struct netif *netif;
  218. u8_t num;
  219. if (name == NULL) {
  220. return NULL;
  221. }
  222. num = name[2] - '0';
  223. for(netif = netif_list; netif != NULL; netif = netif->next) {
  224. if (num == netif->num &&
  225. name[0] == netif->name[0] &&
  226. name[1] == netif->name[1]) {
  227. LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: found %c%c\n", name[0], name[1]));
  228. return netif;
  229. }
  230. }
  231. LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
  232. return NULL;
  233. }
  234. /**
  235. * Change the IP address of a network interface
  236. *
  237. * @param netif the network interface to change
  238. * @param ipaddr the new IP address
  239. *
  240. * @note call netif_set_addr() if you also want to change netmask and
  241. * default gateway
  242. */
  243. void
  244. netif_set_ipaddr(struct netif *netif, struct ip_addr *ipaddr)
  245. {
  246. /* TODO: Handling of obsolete pcbs */
  247. /* See: http://mail.gnu.org/archive/html/lwip-users/2003-03/msg00118.html */
  248. #if LWIP_TCP
  249. struct tcp_pcb *pcb;
  250. struct tcp_pcb_listen *lpcb;
  251. /* address is actually being changed? */
  252. if ((ip_addr_cmp(ipaddr, &(netif->ip_addr))) == 0)
  253. {
  254. /* extern struct tcp_pcb *tcp_active_pcbs; defined by tcp.h */
  255. LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: netif address being changed\n"));
  256. pcb = tcp_active_pcbs;
  257. while (pcb != NULL) {
  258. /* PCB bound to current local interface address? */
  259. if (ip_addr_cmp(&(pcb->local_ip), &(netif->ip_addr))) {
  260. /* this connection must be aborted */
  261. struct tcp_pcb *next = pcb->next;
  262. LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: aborting TCP pcb %p\n", (void *)pcb));
  263. tcp_abort(pcb);
  264. pcb = next;
  265. } else {
  266. pcb = pcb->next;
  267. }
  268. }
  269. for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
  270. /* PCB bound to current local interface address? */
  271. if ((!(ip_addr_isany(&(lpcb->local_ip)))) &&
  272. (ip_addr_cmp(&(lpcb->local_ip), &(netif->ip_addr)))) {
  273. /* The PCB is listening to the old ipaddr and
  274. * is set to listen to the new one instead */
  275. ip_addr_set(&(lpcb->local_ip), ipaddr);
  276. }
  277. }
  278. }
  279. #endif
  280. snmp_delete_ipaddridx_tree(netif);
  281. snmp_delete_iprteidx_tree(0,netif);
  282. /* set new IP address to netif */
  283. ip_addr_set(&(netif->ip_addr), ipaddr);
  284. snmp_insert_ipaddridx_tree(netif);
  285. snmp_insert_iprteidx_tree(0,netif);
  286. LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: IP address of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
  287. netif->name[0], netif->name[1],
  288. ip4_addr1(&netif->ip_addr),
  289. ip4_addr2(&netif->ip_addr),
  290. ip4_addr3(&netif->ip_addr),
  291. ip4_addr4(&netif->ip_addr)));
  292. }
  293. /**
  294. * Change the default gateway for a network interface
  295. *
  296. * @param netif the network interface to change
  297. * @param gw the new default gateway
  298. *
  299. * @note call netif_set_addr() if you also want to change ip address and netmask
  300. */
  301. void
  302. netif_set_gw(struct netif *netif, struct ip_addr *gw)
  303. {
  304. ip_addr_set(&(netif->gw), gw);
  305. LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: GW address of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
  306. netif->name[0], netif->name[1],
  307. ip4_addr1(&netif->gw),
  308. ip4_addr2(&netif->gw),
  309. ip4_addr3(&netif->gw),
  310. ip4_addr4(&netif->gw)));
  311. }
  312. /**
  313. * Change the netmask of a network interface
  314. *
  315. * @param netif the network interface to change
  316. * @param netmask the new netmask
  317. *
  318. * @note call netif_set_addr() if you also want to change ip address and
  319. * default gateway
  320. */
  321. void
  322. netif_set_netmask(struct netif *netif, struct ip_addr *netmask)
  323. {
  324. snmp_delete_iprteidx_tree(0, netif);
  325. /* set new netmask to netif */
  326. ip_addr_set(&(netif->netmask), netmask);
  327. snmp_insert_iprteidx_tree(0, netif);
  328. LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: netmask of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
  329. netif->name[0], netif->name[1],
  330. ip4_addr1(&netif->netmask),
  331. ip4_addr2(&netif->netmask),
  332. ip4_addr3(&netif->netmask),
  333. ip4_addr4(&netif->netmask)));
  334. }
  335. /**
  336. * Set a network interface as the default network interface
  337. * (used to output all packets for which no specific route is found)
  338. *
  339. * @param netif the default network interface
  340. */
  341. void
  342. netif_set_default(struct netif *netif)
  343. {
  344. if (netif == NULL)
  345. {
  346. /* remove default route */
  347. snmp_delete_iprteidx_tree(1, netif);
  348. }
  349. else
  350. {
  351. /* install default route */
  352. snmp_insert_iprteidx_tree(1, netif);
  353. }
  354. netif_default = netif;
  355. LWIP_DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
  356. netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
  357. }
  358. /**
  359. * Bring an interface up, available for processing
  360. * traffic.
  361. *
  362. * @note: Enabling DHCP on a down interface will make it come
  363. * up once configured.
  364. *
  365. * @see dhcp_start()
  366. */
  367. void netif_set_up(struct netif *netif)
  368. {
  369. if ( !(netif->flags & NETIF_FLAG_UP )) {
  370. netif->flags |= NETIF_FLAG_UP;
  371. #if LWIP_SNMP
  372. snmp_get_sysuptime(&netif->ts);
  373. #endif /* LWIP_SNMP */
  374. NETIF_LINK_CALLBACK(netif);
  375. NETIF_STATUS_CALLBACK(netif);
  376. #if LWIP_ARP
  377. /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */
  378. if (netif->flags & NETIF_FLAG_ETHARP) {
  379. etharp_gratuitous(netif);
  380. }
  381. #endif /* LWIP_ARP */
  382. #if LWIP_IGMP
  383. /* resend IGMP memberships */
  384. if (netif->flags & NETIF_FLAG_IGMP) {
  385. igmp_report_groups( netif);
  386. }
  387. #endif /* LWIP_IGMP */
  388. }
  389. }
  390. /**
  391. * Bring an interface down, disabling any traffic processing.
  392. *
  393. * @note: Enabling DHCP on a down interface will make it come
  394. * up once configured.
  395. *
  396. * @see dhcp_start()
  397. */
  398. void netif_set_down(struct netif *netif)
  399. {
  400. if ( netif->flags & NETIF_FLAG_UP )
  401. {
  402. netif->flags &= ~NETIF_FLAG_UP;
  403. #if LWIP_SNMP
  404. snmp_get_sysuptime(&netif->ts);
  405. #endif
  406. NETIF_LINK_CALLBACK(netif);
  407. NETIF_STATUS_CALLBACK(netif);
  408. }
  409. }
  410. /**
  411. * Ask if an interface is up
  412. */
  413. u8_t netif_is_up(struct netif *netif)
  414. {
  415. return (netif->flags & NETIF_FLAG_UP)?1:0;
  416. }
  417. #if LWIP_NETIF_STATUS_CALLBACK
  418. /**
  419. * Set callback to be called when interface is brought up/down
  420. */
  421. void netif_set_status_callback(struct netif *netif, void (* status_callback)(struct netif *netif ))
  422. {
  423. if ( netif )
  424. netif->status_callback = status_callback;
  425. }
  426. #endif /* LWIP_NETIF_STATUS_CALLBACK */
  427. #if LWIP_NETIF_LINK_CALLBACK
  428. /**
  429. * Called by a driver when its link goes up
  430. */
  431. void netif_set_link_up(struct netif *netif )
  432. {
  433. /* not notify link up anymore */
  434. if (netif->flags & NETIF_FLAG_LINK_UP) return;
  435. netif->flags |= NETIF_FLAG_LINK_UP;
  436. #if LWIP_DHCP
  437. if (netif->dhcp) {
  438. dhcp_network_changed(netif);
  439. }
  440. #endif /* LWIP_DHCP */
  441. #if LWIP_AUTOIP
  442. if (netif->autoip) {
  443. autoip_network_changed(netif);
  444. }
  445. #endif /* LWIP_AUTOIP */
  446. if (netif->flags & NETIF_FLAG_UP) {
  447. #if LWIP_ARP
  448. /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */
  449. if (netif->flags & NETIF_FLAG_ETHARP) {
  450. etharp_gratuitous(netif);
  451. }
  452. #endif /* LWIP_ARP */
  453. #if LWIP_IGMP
  454. /* resend IGMP memberships */
  455. if (netif->flags & NETIF_FLAG_IGMP) {
  456. igmp_report_groups( netif);
  457. }
  458. #endif /* LWIP_IGMP */
  459. }
  460. NETIF_LINK_CALLBACK(netif);
  461. }
  462. /**
  463. * Called by a driver when its link goes down
  464. */
  465. void netif_set_link_down(struct netif *netif )
  466. {
  467. netif->flags &= ~NETIF_FLAG_LINK_UP;
  468. NETIF_LINK_CALLBACK(netif);
  469. }
  470. /**
  471. * Ask if a link is up
  472. */
  473. u8_t netif_is_link_up(struct netif *netif)
  474. {
  475. return (netif->flags & NETIF_FLAG_LINK_UP) ? 1 : 0;
  476. }
  477. /**
  478. * Set callback to be called when link is brought up/down
  479. */
  480. void netif_set_link_callback(struct netif *netif, void (* link_callback)(struct netif *netif ))
  481. {
  482. if (netif) {
  483. netif->link_callback = link_callback;
  484. }
  485. }
  486. #endif /* LWIP_NETIF_LINK_CALLBACK */
  487. #if ENABLE_LOOPBACK
  488. /**
  489. * Send an IP packet to be received on the same netif (loopif-like).
  490. * The pbuf is simply copied and handed back to netif->input.
  491. * In multithreaded mode, this is done directly since netif->input must put
  492. * the packet on a queue.
  493. * In callback mode, the packet is put on an internal queue and is fed to
  494. * netif->input by netif_poll().
  495. *
  496. * @param netif the lwip network interface structure
  497. * @param p the (IP) packet to 'send'
  498. * @param ipaddr the ip address to send the packet to (not used)
  499. * @return ERR_OK if the packet has been sent
  500. * ERR_MEM if the pbuf used to copy the packet couldn't be allocated
  501. */
  502. err_t
  503. netif_loop_output(struct netif *netif, struct pbuf *p,
  504. struct ip_addr *ipaddr)
  505. {
  506. struct pbuf *r;
  507. err_t err;
  508. struct pbuf *last;
  509. #if LWIP_LOOPBACK_MAX_PBUFS
  510. u8_t clen = 0;
  511. #endif /* LWIP_LOOPBACK_MAX_PBUFS */
  512. SYS_ARCH_DECL_PROTECT(lev);
  513. LWIP_UNUSED_ARG(ipaddr);
  514. /* Allocate a new pbuf */
  515. r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
  516. if (r == NULL) {
  517. return ERR_MEM;
  518. }
  519. #if LWIP_LOOPBACK_MAX_PBUFS
  520. clen = pbuf_clen(r);
  521. /* check for overflow or too many pbuf on queue */
  522. if(((netif->loop_cnt_current + clen) < netif->loop_cnt_current) ||
  523. ((netif->loop_cnt_current + clen) > LWIP_LOOPBACK_MAX_PBUFS)) {
  524. pbuf_free(r);
  525. r = NULL;
  526. return ERR_MEM;
  527. }
  528. netif->loop_cnt_current += clen;
  529. #endif /* LWIP_LOOPBACK_MAX_PBUFS */
  530. /* Copy the whole pbuf queue p into the single pbuf r */
  531. if ((err = pbuf_copy(r, p)) != ERR_OK) {
  532. pbuf_free(r);
  533. r = NULL;
  534. return err;
  535. }
  536. /* Put the packet on a linked list which gets emptied through calling
  537. netif_poll(). */
  538. /* let last point to the last pbuf in chain r */
  539. for (last = r; last->next != NULL; last = last->next);
  540. SYS_ARCH_PROTECT(lev);
  541. if(netif->loop_first != NULL) {
  542. LWIP_ASSERT("if first != NULL, last must also be != NULL", netif->loop_last != NULL);
  543. netif->loop_last->next = r;
  544. netif->loop_last = last;
  545. } else {
  546. netif->loop_first = r;
  547. netif->loop_last = last;
  548. }
  549. SYS_ARCH_UNPROTECT(lev);
  550. #if LWIP_NETIF_LOOPBACK_MULTITHREADING
  551. /* For multithreading environment, schedule a call to netif_poll */
  552. tcpip_callback((void (*)(void *))(netif_poll), netif);
  553. #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
  554. return ERR_OK;
  555. }
  556. /**
  557. * Call netif_poll() in the main loop of your application. This is to prevent
  558. * reentering non-reentrant functions like tcp_input(). Packets passed to
  559. * netif_loop_output() are put on a list that is passed to netif->input() by
  560. * netif_poll().
  561. */
  562. void
  563. netif_poll(struct netif *netif)
  564. {
  565. struct pbuf *in;
  566. SYS_ARCH_DECL_PROTECT(lev);
  567. do {
  568. /* Get a packet from the list. With SYS_LIGHTWEIGHT_PROT=1, this is protected */
  569. SYS_ARCH_PROTECT(lev);
  570. in = netif->loop_first;
  571. if(in != NULL) {
  572. struct pbuf *in_end = in;
  573. #if LWIP_LOOPBACK_MAX_PBUFS
  574. u8_t clen = pbuf_clen(in);
  575. /* adjust the number of pbufs on queue */
  576. LWIP_ASSERT("netif->loop_cnt_current underflow",
  577. ((netif->loop_cnt_current - clen) < netif->loop_cnt_current));
  578. netif->loop_cnt_current -= clen;
  579. #endif /* LWIP_LOOPBACK_MAX_PBUFS */
  580. while(in_end->len != in_end->tot_len) {
  581. LWIP_ASSERT("bogus pbuf: len != tot_len but next == NULL!", in_end->next != NULL);
  582. in_end = in_end->next;
  583. }
  584. /* 'in_end' now points to the last pbuf from 'in' */
  585. if(in_end == netif->loop_last) {
  586. /* this was the last pbuf in the list */
  587. netif->loop_first = netif->loop_last = NULL;
  588. } else {
  589. /* pop the pbuf off the list */
  590. netif->loop_first = in_end->next;
  591. LWIP_ASSERT("should not be null since first != last!", netif->loop_first != NULL);
  592. }
  593. /* De-queue the pbuf from its successors on the 'loop_' list. */
  594. in_end->next = NULL;
  595. }
  596. SYS_ARCH_UNPROTECT(lev);
  597. if(in != NULL) {
  598. /* loopback packets are always IP packets! */
  599. if(ip_input(in, netif) != ERR_OK) {
  600. pbuf_free(in);
  601. }
  602. /* Don't reference the packet any more! */
  603. in = NULL;
  604. }
  605. /* go on while there is a packet on the list */
  606. } while(netif->loop_first != NULL);
  607. }
  608. #if !LWIP_NETIF_LOOPBACK_MULTITHREADING
  609. /**
  610. * Calls netif_poll() for every netif on the netif_list.
  611. */
  612. void
  613. netif_poll_all(void)
  614. {
  615. struct netif *netif = netif_list;
  616. /* loop through netifs */
  617. while (netif != NULL) {
  618. netif_poll(netif);
  619. /* proceed to next network interface */
  620. netif = netif->next;
  621. }
  622. }
  623. #endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */
  624. #endif /* ENABLE_LOOPBACK */