netif.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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_impl.h"
  42. #include "lwip/snmp.h"
  43. #include "lwip/igmp.h"
  44. #include "netif/etharp.h"
  45. #include "lwip/stats.h"
  46. #if ENABLE_LOOPBACK
  47. #include "lwip/sys.h"
  48. #if LWIP_NETIF_LOOPBACK_MULTITHREADING
  49. #include "lwip/tcpip.h"
  50. #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
  51. #endif /* ENABLE_LOOPBACK */
  52. #if LWIP_AUTOIP
  53. #include "lwip/autoip.h"
  54. #endif /* LWIP_AUTOIP */
  55. #if LWIP_DHCP
  56. #include "lwip/dhcp.h"
  57. #endif /* LWIP_DHCP */
  58. #if LWIP_NETIF_STATUS_CALLBACK
  59. #define NETIF_STATUS_CALLBACK(n) do{ if (n->status_callback) { (n->status_callback)(n); }}while(0)
  60. #else
  61. #define NETIF_STATUS_CALLBACK(n)
  62. #endif /* LWIP_NETIF_STATUS_CALLBACK */
  63. #if LWIP_NETIF_LINK_CALLBACK
  64. #define NETIF_LINK_CALLBACK(n) do{ if (n->link_callback) { (n->link_callback)(n); }}while(0)
  65. #else
  66. #define NETIF_LINK_CALLBACK(n)
  67. #endif /* LWIP_NETIF_LINK_CALLBACK */
  68. struct netif *netif_list;
  69. struct netif *netif_default;
  70. #if LWIP_HAVE_LOOPIF
  71. static struct netif loop_netif;
  72. /**
  73. * Initialize a lwip network interface structure for a loopback interface
  74. *
  75. * @param netif the lwip network interface structure for this loopif
  76. * @return ERR_OK if the loopif is initialized
  77. * ERR_MEM if private data couldn't be allocated
  78. */
  79. static err_t ICACHE_FLASH_ATTR
  80. netif_loopif_init(struct netif *netif)
  81. {
  82. /* initialize the snmp variables and counters inside the struct netif
  83. * ifSpeed: no assumption can be made!
  84. */
  85. NETIF_INIT_SNMP(netif, snmp_ifType_softwareLoopback, 0);
  86. netif->name[0] = 'l';
  87. netif->name[1] = 'o';
  88. netif->output = netif_loop_output;
  89. return ERR_OK;
  90. }
  91. #endif /* LWIP_HAVE_LOOPIF */
  92. void
  93. netif_init(void)
  94. {
  95. #if LWIP_HAVE_LOOPIF
  96. ip_addr_t loop_ipaddr, loop_netmask, loop_gw;
  97. IP4_ADDR(&loop_gw, 127,0,0,1);
  98. IP4_ADDR(&loop_ipaddr, 127,0,0,1);
  99. IP4_ADDR(&loop_netmask, 255,0,0,0);
  100. #if NO_SYS
  101. netif_add(&loop_netif, &loop_ipaddr, &loop_netmask, &loop_gw, NULL, netif_loopif_init, ip_input);
  102. #else /* NO_SYS */
  103. netif_add(&loop_netif, &loop_ipaddr, &loop_netmask, &loop_gw, NULL, netif_loopif_init, tcpip_input);
  104. #endif /* NO_SYS */
  105. netif_set_up(&loop_netif);
  106. #endif /* LWIP_HAVE_LOOPIF */
  107. }
  108. /**
  109. * Add a network interface to the list of lwIP netifs.
  110. *
  111. * @param netif a pre-allocated netif structure
  112. * @param ipaddr IP address for the new netif
  113. * @param netmask network mask for the new netif
  114. * @param gw default gateway IP address for the new netif
  115. * @param state opaque data passed to the new netif
  116. * @param init callback function that initializes the interface
  117. * @param input callback function that is called to pass
  118. * ingress packets up in the protocol layer stack.
  119. *
  120. * @return netif, or NULL if failed.
  121. */
  122. struct netif *
  123. netif_add(struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask,
  124. ip_addr_t *gw, void *state, netif_init_fn init, netif_input_fn input)
  125. {
  126. static u8_t netifnum = 0;
  127. LWIP_ASSERT("No init function given", init != NULL);
  128. /* reset new interface configuration state */
  129. ip_addr_set_zero(&netif->ip_addr);
  130. ip_addr_set_zero(&netif->netmask);
  131. ip_addr_set_zero(&netif->gw);
  132. netif->flags = 0;
  133. #if LWIP_DHCP
  134. /* netif not under DHCP control by default */
  135. netif->dhcp = NULL;
  136. netif->dhcps_pcb = NULL;
  137. #endif /* LWIP_DHCP */
  138. #if LWIP_AUTOIP
  139. /* netif not under AutoIP control by default */
  140. netif->autoip = NULL;
  141. #endif /* LWIP_AUTOIP */
  142. #if LWIP_NETIF_STATUS_CALLBACK
  143. netif->status_callback = NULL;
  144. #endif /* LWIP_NETIF_STATUS_CALLBACK */
  145. #if LWIP_NETIF_LINK_CALLBACK
  146. netif->link_callback = NULL;
  147. #endif /* LWIP_NETIF_LINK_CALLBACK */
  148. #if LWIP_IGMP
  149. netif->igmp_mac_filter = NULL;
  150. #endif /* LWIP_IGMP */
  151. #if ENABLE_LOOPBACK
  152. netif->loop_first = NULL;
  153. netif->loop_last = NULL;
  154. #endif /* ENABLE_LOOPBACK */
  155. /* remember netif specific state information data */
  156. netif->state = state;
  157. netif->num = netifnum++;
  158. netif->input = input;
  159. #if LWIP_NETIF_HWADDRHINT
  160. netif->addr_hint = NULL;
  161. #endif /* LWIP_NETIF_HWADDRHINT*/
  162. #if ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS
  163. netif->loop_cnt_current = 0;
  164. #endif /* ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS */
  165. netif_set_addr(netif, ipaddr, netmask, gw);
  166. /* call user specified initialization function for netif */
  167. if (init(netif) != ERR_OK) {
  168. return NULL;
  169. }
  170. /* add this netif to the list */
  171. netif->next = netif_list;
  172. netif_list = netif;
  173. snmp_inc_iflist();
  174. #if LWIP_IGMP
  175. /* start IGMP processing */
  176. if (netif->flags & NETIF_FLAG_IGMP) {
  177. igmp_start(netif);
  178. }
  179. #endif /* LWIP_IGMP */
  180. LWIP_DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP addr ",
  181. netif->name[0], netif->name[1]));
  182. ip_addr_debug_print(NETIF_DEBUG, ipaddr);
  183. LWIP_DEBUGF(NETIF_DEBUG, (" netmask "));
  184. ip_addr_debug_print(NETIF_DEBUG, netmask);
  185. LWIP_DEBUGF(NETIF_DEBUG, (" gw "));
  186. ip_addr_debug_print(NETIF_DEBUG, gw);
  187. LWIP_DEBUGF(NETIF_DEBUG, ("\n"));
  188. return netif;
  189. }
  190. /**
  191. * Change IP address configuration for a network interface (including netmask
  192. * and default gateway).
  193. *
  194. * @param netif the network interface to change
  195. * @param ipaddr the new IP address
  196. * @param netmask the new netmask
  197. * @param gw the new default gateway
  198. */
  199. void
  200. netif_set_addr(struct netif *netif, ip_addr_t *ipaddr, ip_addr_t *netmask,
  201. ip_addr_t *gw)
  202. {
  203. netif_set_ipaddr(netif, ipaddr);
  204. netif_set_netmask(netif, netmask);
  205. netif_set_gw(netif, gw);
  206. }
  207. /**
  208. * Remove a network interface from the list of lwIP netifs.
  209. *
  210. * @param netif the network interface to remove
  211. */
  212. void
  213. netif_remove(struct netif *netif)
  214. {
  215. if (netif == NULL) {
  216. return;
  217. }
  218. #if LWIP_IGMP
  219. /* stop IGMP processing */
  220. if (netif->flags & NETIF_FLAG_IGMP) {
  221. igmp_stop(netif);
  222. }
  223. #endif /* LWIP_IGMP */
  224. if (netif_is_up(netif)) {
  225. /* set netif down before removing (call callback function) */
  226. netif_set_down(netif);
  227. }
  228. snmp_delete_ipaddridx_tree(netif);
  229. /* is it the first netif? */
  230. if (netif_list == netif) {
  231. netif_list = netif->next;
  232. } else {
  233. /* look for netif further down the list */
  234. struct netif * tmpNetif;
  235. for (tmpNetif = netif_list; tmpNetif != NULL; tmpNetif = tmpNetif->next) {
  236. if (tmpNetif->next == netif) {
  237. tmpNetif->next = netif->next;
  238. break;
  239. }
  240. }
  241. if (tmpNetif == NULL)
  242. return; /* we didn't find any netif today */
  243. }
  244. snmp_dec_iflist();
  245. /* this netif is default? */
  246. if (netif_default == netif) {
  247. /* reset default netif */
  248. netif_set_default(NULL);
  249. }
  250. LWIP_DEBUGF( NETIF_DEBUG, ("netif_remove: removed netif\n") );
  251. }
  252. /**
  253. * Find a network interface by searching for its name
  254. *
  255. * @param name the name of the netif (like netif->name) plus concatenated number
  256. * in ascii representation (e.g. 'en0')
  257. */
  258. struct netif *
  259. netif_find(char *name)
  260. {
  261. struct netif *netif;
  262. u8_t num;
  263. if (name == NULL) {
  264. return NULL;
  265. }
  266. num = name[2] - '0';
  267. for(netif = netif_list; netif != NULL; netif = netif->next) {
  268. if (num == netif->num &&
  269. name[0] == netif->name[0] &&
  270. name[1] == netif->name[1]) {
  271. LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: found %c%c\n", name[0], name[1]));
  272. return netif;
  273. }
  274. }
  275. LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
  276. return NULL;
  277. }
  278. /**
  279. * Change the IP address of a network interface
  280. *
  281. * @param netif the network interface to change
  282. * @param ipaddr the new IP address
  283. *
  284. * @note call netif_set_addr() if you also want to change netmask and
  285. * default gateway
  286. */
  287. void
  288. netif_set_ipaddr(struct netif *netif, ip_addr_t *ipaddr)
  289. {
  290. /* TODO: Handling of obsolete pcbs */
  291. /* See: http://mail.gnu.org/archive/html/lwip-users/2003-03/msg00118.html */
  292. #if LWIP_TCP
  293. struct tcp_pcb *pcb;
  294. struct tcp_pcb_listen *lpcb;
  295. /* address is actually being changed? */
  296. if (ipaddr && (ip_addr_cmp(ipaddr, &(netif->ip_addr))) == 0) {
  297. /* extern struct tcp_pcb *tcp_active_pcbs; defined by tcp.h */
  298. LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: netif address being changed\n"));
  299. pcb = tcp_active_pcbs;
  300. while (pcb != NULL) {
  301. /* PCB bound to current local interface address? */
  302. if (ip_addr_cmp(&(pcb->local_ip), &(netif->ip_addr))
  303. #if LWIP_AUTOIP
  304. /* connections to link-local addresses must persist (RFC3927 ch. 1.9) */
  305. && !ip_addr_islinklocal(&(pcb->local_ip))
  306. #endif /* LWIP_AUTOIP */
  307. ) {
  308. /* this connection must be aborted */
  309. struct tcp_pcb *next = pcb->next;
  310. LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: aborting TCP pcb %p\n", (void *)pcb));
  311. tcp_abort(pcb);
  312. pcb = next;
  313. } else {
  314. pcb = pcb->next;
  315. }
  316. }
  317. for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
  318. /* PCB bound to current local interface address? */
  319. if ((!(ip_addr_isany(&(lpcb->local_ip)))) &&
  320. (ip_addr_cmp(&(lpcb->local_ip), &(netif->ip_addr)))) {
  321. /* The PCB is listening to the old ipaddr and
  322. * is set to listen to the new one instead */
  323. ip_addr_set(&(lpcb->local_ip), ipaddr);
  324. }
  325. }
  326. }
  327. #endif
  328. snmp_delete_ipaddridx_tree(netif);
  329. snmp_delete_iprteidx_tree(0,netif);
  330. /* set new IP address to netif */
  331. ip_addr_set(&(netif->ip_addr), ipaddr);
  332. snmp_insert_ipaddridx_tree(netif);
  333. snmp_insert_iprteidx_tree(0,netif);
  334. 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",
  335. netif->name[0], netif->name[1],
  336. ip4_addr1_16(&netif->ip_addr),
  337. ip4_addr2_16(&netif->ip_addr),
  338. ip4_addr3_16(&netif->ip_addr),
  339. ip4_addr4_16(&netif->ip_addr)));
  340. }
  341. /**
  342. * Change the default gateway for a network interface
  343. *
  344. * @param netif the network interface to change
  345. * @param gw the new default gateway
  346. *
  347. * @note call netif_set_addr() if you also want to change ip address and netmask
  348. */
  349. void
  350. netif_set_gw(struct netif *netif, ip_addr_t *gw)
  351. {
  352. ip_addr_set(&(netif->gw), gw);
  353. 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",
  354. netif->name[0], netif->name[1],
  355. ip4_addr1_16(&netif->gw),
  356. ip4_addr2_16(&netif->gw),
  357. ip4_addr3_16(&netif->gw),
  358. ip4_addr4_16(&netif->gw)));
  359. }
  360. /**
  361. * Change the netmask of a network interface
  362. *
  363. * @param netif the network interface to change
  364. * @param netmask the new netmask
  365. *
  366. * @note call netif_set_addr() if you also want to change ip address and
  367. * default gateway
  368. */
  369. void
  370. netif_set_netmask(struct netif *netif, ip_addr_t *netmask)
  371. {
  372. snmp_delete_iprteidx_tree(0, netif);
  373. /* set new netmask to netif */
  374. ip_addr_set(&(netif->netmask), netmask);
  375. snmp_insert_iprteidx_tree(0, netif);
  376. 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",
  377. netif->name[0], netif->name[1],
  378. ip4_addr1_16(&netif->netmask),
  379. ip4_addr2_16(&netif->netmask),
  380. ip4_addr3_16(&netif->netmask),
  381. ip4_addr4_16(&netif->netmask)));
  382. }
  383. /**
  384. * Set a network interface as the default network interface
  385. * (used to output all packets for which no specific route is found)
  386. *
  387. * @param netif the default network interface
  388. */
  389. void
  390. netif_set_default(struct netif *netif)
  391. {
  392. if (netif == NULL) {
  393. /* remove default route */
  394. snmp_delete_iprteidx_tree(1, netif);
  395. } else {
  396. /* install default route */
  397. snmp_insert_iprteidx_tree(1, netif);
  398. }
  399. netif_default = netif;
  400. LWIP_DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
  401. netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
  402. }
  403. /**
  404. * Bring an interface up, available for processing
  405. * traffic.
  406. *
  407. * @note: Enabling DHCP on a down interface will make it come
  408. * up once configured.
  409. *
  410. * @see dhcp_start()
  411. */
  412. void netif_set_up(struct netif *netif)
  413. {
  414. if (!(netif->flags & NETIF_FLAG_UP)) {
  415. netif->flags |= NETIF_FLAG_UP;
  416. #if LWIP_SNMP
  417. snmp_get_sysuptime(&netif->ts);
  418. #endif /* LWIP_SNMP */
  419. NETIF_STATUS_CALLBACK(netif);
  420. if (netif->flags & NETIF_FLAG_LINK_UP) {
  421. #if LWIP_ARP
  422. /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */
  423. if (netif->flags & (NETIF_FLAG_ETHARP)) {
  424. etharp_gratuitous(netif);
  425. }
  426. #endif /* LWIP_ARP */
  427. #if LWIP_IGMP
  428. /* resend IGMP memberships */
  429. if (netif->flags & NETIF_FLAG_IGMP) {
  430. igmp_report_groups( netif);
  431. }
  432. #endif /* LWIP_IGMP */
  433. }
  434. }
  435. }
  436. /**
  437. * Bring an interface down, disabling any traffic processing.
  438. *
  439. * @note: Enabling DHCP on a down interface will make it come
  440. * up once configured.
  441. *
  442. * @see dhcp_start()
  443. */
  444. void netif_set_down(struct netif *netif)
  445. {
  446. if (netif == NULL) {
  447. return;
  448. }
  449. if (netif->flags & NETIF_FLAG_UP) {
  450. netif->flags &= ~NETIF_FLAG_UP;
  451. #if LWIP_SNMP
  452. snmp_get_sysuptime(&netif->ts);
  453. #endif
  454. #if LWIP_ARP
  455. if (netif->flags & NETIF_FLAG_ETHARP) {
  456. etharp_cleanup_netif(netif);
  457. }
  458. #endif /* LWIP_ARP */
  459. NETIF_STATUS_CALLBACK(netif);
  460. }
  461. }
  462. #if LWIP_NETIF_STATUS_CALLBACK
  463. /**
  464. * Set callback to be called when interface is brought up/down
  465. */
  466. void netif_set_status_callback(struct netif *netif, netif_status_callback_fn status_callback)
  467. {
  468. if (netif) {
  469. netif->status_callback = status_callback;
  470. }
  471. }
  472. #endif /* LWIP_NETIF_STATUS_CALLBACK */
  473. /**
  474. * Called by a driver when its link goes up
  475. */
  476. void netif_set_link_up(struct netif *netif )
  477. {
  478. if (!(netif->flags & NETIF_FLAG_LINK_UP)) {
  479. netif->flags |= NETIF_FLAG_LINK_UP;
  480. #if LWIP_DHCP
  481. if (netif->dhcp) {
  482. dhcp_network_changed(netif);
  483. }
  484. #endif /* LWIP_DHCP */
  485. #if LWIP_AUTOIP
  486. if (netif->autoip) {
  487. autoip_network_changed(netif);
  488. }
  489. #endif /* LWIP_AUTOIP */
  490. if (netif->flags & NETIF_FLAG_UP) {
  491. #if LWIP_ARP
  492. /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */
  493. if (netif->flags & NETIF_FLAG_ETHARP) {
  494. etharp_gratuitous(netif);
  495. }
  496. #endif /* LWIP_ARP */
  497. #if LWIP_IGMP
  498. /* resend IGMP memberships */
  499. if (netif->flags & NETIF_FLAG_IGMP) {
  500. igmp_report_groups( netif);
  501. }
  502. #endif /* LWIP_IGMP */
  503. }
  504. NETIF_LINK_CALLBACK(netif);
  505. }
  506. }
  507. /**
  508. * Called by a driver when its link goes down
  509. */
  510. void netif_set_link_down(struct netif *netif )
  511. {
  512. if (netif->flags & NETIF_FLAG_LINK_UP) {
  513. netif->flags &= ~NETIF_FLAG_LINK_UP;
  514. NETIF_LINK_CALLBACK(netif);
  515. }
  516. }
  517. #if LWIP_NETIF_LINK_CALLBACK
  518. /**
  519. * Set callback to be called when link is brought up/down
  520. */
  521. void netif_set_link_callback(struct netif *netif, netif_status_callback_fn link_callback)
  522. {
  523. if (netif) {
  524. netif->link_callback = link_callback;
  525. }
  526. }
  527. #endif /* LWIP_NETIF_LINK_CALLBACK */
  528. #if ENABLE_LOOPBACK
  529. /**
  530. * Send an IP packet to be received on the same netif (loopif-like).
  531. * The pbuf is simply copied and handed back to netif->input.
  532. * In multithreaded mode, this is done directly since netif->input must put
  533. * the packet on a queue.
  534. * In callback mode, the packet is put on an internal queue and is fed to
  535. * netif->input by netif_poll().
  536. *
  537. * @param netif the lwip network interface structure
  538. * @param p the (IP) packet to 'send'
  539. * @param ipaddr the ip address to send the packet to (not used)
  540. * @return ERR_OK if the packet has been sent
  541. * ERR_MEM if the pbuf used to copy the packet couldn't be allocated
  542. */
  543. err_t
  544. netif_loop_output(struct netif *netif, struct pbuf *p,
  545. ip_addr_t *ipaddr)
  546. {
  547. struct pbuf *r;
  548. err_t err;
  549. struct pbuf *last;
  550. #if LWIP_LOOPBACK_MAX_PBUFS
  551. u8_t clen = 0;
  552. #endif /* LWIP_LOOPBACK_MAX_PBUFS */
  553. /* If we have a loopif, SNMP counters are adjusted for it,
  554. * if not they are adjusted for 'netif'. */
  555. #if LWIP_SNMP
  556. #if LWIP_HAVE_LOOPIF
  557. struct netif *stats_if = &loop_netif;
  558. #else /* LWIP_HAVE_LOOPIF */
  559. struct netif *stats_if = netif;
  560. #endif /* LWIP_HAVE_LOOPIF */
  561. #endif /* LWIP_SNMP */
  562. SYS_ARCH_DECL_PROTECT(lev);
  563. LWIP_UNUSED_ARG(ipaddr);
  564. /* Allocate a new pbuf */
  565. r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
  566. if (r == NULL) {
  567. LINK_STATS_INC(link.memerr);
  568. LINK_STATS_INC(link.drop);
  569. snmp_inc_ifoutdiscards(stats_if);
  570. return ERR_MEM;
  571. }
  572. #if LWIP_LOOPBACK_MAX_PBUFS
  573. clen = pbuf_clen(r);
  574. /* check for overflow or too many pbuf on queue */
  575. if(((netif->loop_cnt_current + clen) < netif->loop_cnt_current) ||
  576. ((netif->loop_cnt_current + clen) > LWIP_LOOPBACK_MAX_PBUFS)) {
  577. pbuf_free(r);
  578. LINK_STATS_INC(link.memerr);
  579. LINK_STATS_INC(link.drop);
  580. snmp_inc_ifoutdiscards(stats_if);
  581. return ERR_MEM;
  582. }
  583. netif->loop_cnt_current += clen;
  584. #endif /* LWIP_LOOPBACK_MAX_PBUFS */
  585. /* Copy the whole pbuf queue p into the single pbuf r */
  586. if ((err = pbuf_copy(r, p)) != ERR_OK) {
  587. pbuf_free(r);
  588. LINK_STATS_INC(link.memerr);
  589. LINK_STATS_INC(link.drop);
  590. snmp_inc_ifoutdiscards(stats_if);
  591. return err;
  592. }
  593. /* Put the packet on a linked list which gets emptied through calling
  594. netif_poll(). */
  595. /* let last point to the last pbuf in chain r */
  596. for (last = r; last->next != NULL; last = last->next);
  597. SYS_ARCH_PROTECT(lev);
  598. if(netif->loop_first != NULL) {
  599. LWIP_ASSERT("if first != NULL, last must also be != NULL", netif->loop_last != NULL);
  600. netif->loop_last->next = r;
  601. netif->loop_last = last;
  602. } else {
  603. netif->loop_first = r;
  604. netif->loop_last = last;
  605. }
  606. SYS_ARCH_UNPROTECT(lev);
  607. LINK_STATS_INC(link.xmit);
  608. snmp_add_ifoutoctets(stats_if, p->tot_len);
  609. snmp_inc_ifoutucastpkts(stats_if);
  610. #if LWIP_NETIF_LOOPBACK_MULTITHREADING
  611. /* For multithreading environment, schedule a call to netif_poll */
  612. tcpip_callback((tcpip_callback_fn)netif_poll, netif);
  613. #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
  614. return ERR_OK;
  615. }
  616. /**
  617. * Call netif_poll() in the main loop of your application. This is to prevent
  618. * reentering non-reentrant functions like tcp_input(). Packets passed to
  619. * netif_loop_output() are put on a list that is passed to netif->input() by
  620. * netif_poll().
  621. */
  622. void
  623. netif_poll(struct netif *netif)
  624. {
  625. struct pbuf *in;
  626. /* If we have a loopif, SNMP counters are adjusted for it,
  627. * if not they are adjusted for 'netif'. */
  628. #if LWIP_SNMP
  629. #if LWIP_HAVE_LOOPIF
  630. struct netif *stats_if = &loop_netif;
  631. #else /* LWIP_HAVE_LOOPIF */
  632. struct netif *stats_if = netif;
  633. #endif /* LWIP_HAVE_LOOPIF */
  634. #endif /* LWIP_SNMP */
  635. SYS_ARCH_DECL_PROTECT(lev);
  636. do {
  637. /* Get a packet from the list. With SYS_LIGHTWEIGHT_PROT=1, this is protected */
  638. SYS_ARCH_PROTECT(lev);
  639. in = netif->loop_first;
  640. if (in != NULL) {
  641. struct pbuf *in_end = in;
  642. #if LWIP_LOOPBACK_MAX_PBUFS
  643. u8_t clen = pbuf_clen(in);
  644. /* adjust the number of pbufs on queue */
  645. LWIP_ASSERT("netif->loop_cnt_current underflow",
  646. ((netif->loop_cnt_current - clen) < netif->loop_cnt_current));
  647. netif->loop_cnt_current -= clen;
  648. #endif /* LWIP_LOOPBACK_MAX_PBUFS */
  649. while (in_end->len != in_end->tot_len) {
  650. LWIP_ASSERT("bogus pbuf: len != tot_len but next == NULL!", in_end->next != NULL);
  651. in_end = in_end->next;
  652. }
  653. /* 'in_end' now points to the last pbuf from 'in' */
  654. if (in_end == netif->loop_last) {
  655. /* this was the last pbuf in the list */
  656. netif->loop_first = netif->loop_last = NULL;
  657. } else {
  658. /* pop the pbuf off the list */
  659. netif->loop_first = in_end->next;
  660. LWIP_ASSERT("should not be null since first != last!", netif->loop_first != NULL);
  661. }
  662. /* De-queue the pbuf from its successors on the 'loop_' list. */
  663. in_end->next = NULL;
  664. }
  665. SYS_ARCH_UNPROTECT(lev);
  666. if (in != NULL) {
  667. LINK_STATS_INC(link.recv);
  668. snmp_add_ifinoctets(stats_if, in->tot_len);
  669. snmp_inc_ifinucastpkts(stats_if);
  670. /* loopback packets are always IP packets! */
  671. if (ip_input(in, netif) != ERR_OK) {
  672. pbuf_free(in);
  673. }
  674. /* Don't reference the packet any more! */
  675. in = NULL;
  676. }
  677. /* go on while there is a packet on the list */
  678. } while (netif->loop_first != NULL);
  679. }
  680. #if !LWIP_NETIF_LOOPBACK_MULTITHREADING
  681. /**
  682. * Calls netif_poll() for every netif on the netif_list.
  683. */
  684. void
  685. netif_poll_all(void)
  686. {
  687. struct netif *netif = netif_list;
  688. /* loop through netifs */
  689. while (netif != NULL) {
  690. netif_poll(netif);
  691. /* proceed to next network interface */
  692. netif = netif->next;
  693. }
  694. }
  695. #endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */
  696. #endif /* ENABLE_LOOPBACK */