netif.c 22 KB

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