netif.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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/ip6_addr.h"
  41. #include "lwip/netif.h"
  42. #include "lwip/priv/tcp_priv.h"
  43. #include "lwip/udp.h"
  44. #include "lwip/snmp.h"
  45. #include "lwip/igmp.h"
  46. #include "netif/etharp.h"
  47. #include "lwip/stats.h"
  48. #include "lwip/sys.h"
  49. #if ENABLE_LOOPBACK
  50. #include "lwip/sys.h"
  51. #if LWIP_NETIF_LOOPBACK_MULTITHREADING
  52. #include "lwip/tcpip.h"
  53. #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
  54. #endif /* ENABLE_LOOPBACK */
  55. #if LWIP_AUTOIP
  56. #include "lwip/autoip.h"
  57. #endif /* LWIP_AUTOIP */
  58. #if LWIP_DHCP
  59. #include "lwip/dhcp.h"
  60. #endif /* LWIP_DHCP */
  61. #if LWIP_IPV6_DHCP6
  62. #include "lwip/dhcp6.h"
  63. #endif /* LWIP_IPV6_DHCP6 */
  64. #if LWIP_IPV6_MLD
  65. #include "lwip/mld6.h"
  66. #endif /* LWIP_IPV6_MLD */
  67. #if LWIP_NETIF_STATUS_CALLBACK
  68. #define NETIF_STATUS_CALLBACK(n) do{ if (n->status_callback) { (n->status_callback)(n); }}while(0)
  69. #else
  70. #define NETIF_STATUS_CALLBACK(n)
  71. #endif /* LWIP_NETIF_STATUS_CALLBACK */
  72. #if LWIP_NETIF_LINK_CALLBACK
  73. #define NETIF_LINK_CALLBACK(n) do{ if (n->link_callback) { (n->link_callback)(n); }}while(0)
  74. #else
  75. #define NETIF_LINK_CALLBACK(n)
  76. #endif /* LWIP_NETIF_LINK_CALLBACK */
  77. struct netif *netif_list;
  78. struct netif *netif_default;
  79. static u8_t netif_num;
  80. #define NETIF_REPORT_TYPE_IPV4 0x01
  81. #define NETIF_REPORT_TYPE_IPV6 0x02
  82. static void netif_issue_reports(struct netif* netif, u8_t report_type);
  83. #if LWIP_IPV6
  84. static err_t netif_null_output_ip6(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr);
  85. #endif /* LWIP_IPV6 */
  86. #if LWIP_HAVE_LOOPIF
  87. #if LWIP_IPV4
  88. static err_t netif_loop_output_ipv4(struct netif *netif, struct pbuf *p, const ip4_addr_t* addr);
  89. #endif
  90. #if LWIP_IPV6
  91. static err_t netif_loop_output_ipv6(struct netif *netif, struct pbuf *p, const ip6_addr_t* addr);
  92. #endif
  93. static struct netif loop_netif;
  94. /**
  95. * Initialize a lwip network interface structure for a loopback interface
  96. *
  97. * @param netif the lwip network interface structure for this loopif
  98. * @return ERR_OK if the loopif is initialized
  99. * ERR_MEM if private data couldn't be allocated
  100. */
  101. static err_t
  102. netif_loopif_init(struct netif *netif)
  103. {
  104. /* initialize the snmp variables and counters inside the struct netif
  105. * ifSpeed: no assumption can be made!
  106. */
  107. MIB2_INIT_NETIF(netif, snmp_ifType_softwareLoopback, 0);
  108. netif->name[0] = 'l';
  109. netif->name[1] = 'o';
  110. #if LWIP_IPV4
  111. netif->output = netif_loop_output_ipv4;
  112. #endif
  113. #if LWIP_IPV6
  114. netif->output_ip6 = netif_loop_output_ipv6;
  115. #endif
  116. #if LWIP_LOOPIF_MULTICAST
  117. netif->flags |= NETIF_FLAG_IGMP;
  118. #endif
  119. return ERR_OK;
  120. }
  121. #endif /* LWIP_HAVE_LOOPIF */
  122. void
  123. netif_init(void)
  124. {
  125. #if LWIP_HAVE_LOOPIF
  126. #if LWIP_IPV4
  127. #define LOOPIF_ADDRINIT &loop_ipaddr, &loop_netmask, &loop_gw,
  128. ip4_addr_t loop_ipaddr, loop_netmask, loop_gw;
  129. IP4_ADDR(&loop_gw, 127,0,0,1);
  130. IP4_ADDR(&loop_ipaddr, 127,0,0,1);
  131. IP4_ADDR(&loop_netmask, 255,0,0,0);
  132. #else /* LWIP_IPV4 */
  133. #define LOOPIF_ADDRINIT
  134. #endif /* LWIP_IPV4 */
  135. #if NO_SYS
  136. netif_add(&loop_netif, LOOPIF_ADDRINIT NULL, netif_loopif_init, ip_input);
  137. #else /* NO_SYS */
  138. netif_add(&loop_netif, LOOPIF_ADDRINIT NULL, netif_loopif_init, tcpip_input);
  139. #endif /* NO_SYS */
  140. #if LWIP_IPV6
  141. IP_ADDR6(loop_netif.ip6_addr, 0, 0, 0, PP_HTONL(0x00000001UL));
  142. loop_netif.ip6_addr_state[0] = IP6_ADDR_VALID;
  143. #endif /* LWIP_IPV6 */
  144. netif_set_link_up(&loop_netif);
  145. netif_set_up(&loop_netif);
  146. #endif /* LWIP_HAVE_LOOPIF */
  147. }
  148. /**
  149. * Add a network interface to the list of lwIP netifs.
  150. *
  151. * @param netif a pre-allocated netif structure
  152. * @param ipaddr IP address for the new netif
  153. * @param netmask network mask for the new netif
  154. * @param gw default gateway IP address for the new netif
  155. * @param state opaque data passed to the new netif
  156. * @param init callback function that initializes the interface
  157. * @param input callback function that is called to pass
  158. * ingress packets up in the protocol layer stack.
  159. *
  160. * @return netif, or NULL if failed.
  161. */
  162. struct netif *
  163. netif_add(struct netif *netif,
  164. #if LWIP_IPV4
  165. const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, const ip4_addr_t *gw,
  166. #endif /* LWIP_IPV4 */
  167. void *state, netif_init_fn init, netif_input_fn input)
  168. {
  169. #if LWIP_IPV6
  170. u32_t i;
  171. #endif
  172. LWIP_ASSERT("No init function given", init != NULL);
  173. /* reset new interface configuration state */
  174. #if LWIP_IPV4
  175. ip_addr_set_zero_ip4(&netif->ip_addr);
  176. ip_addr_set_zero_ip4(&netif->netmask);
  177. ip_addr_set_zero_ip4(&netif->gw);
  178. #endif /* LWIP_IPV4 */
  179. #if LWIP_IPV6
  180. for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
  181. ip_addr_set_zero_ip6(&netif->ip6_addr[i]);
  182. netif_ip6_addr_set_state(netif, i, IP6_ADDR_INVALID);
  183. }
  184. netif->output_ip6 = netif_null_output_ip6;
  185. #endif /* LWIP_IPV6 */
  186. NETIF_SET_CHECKSUM_CTRL(netif, NETIF_CHECKSUM_ENABLE_ALL);
  187. netif->flags = 0;
  188. #if LWIP_DHCP
  189. /* netif not under DHCP control by default */
  190. netif->dhcp = NULL;
  191. #if ESP_DHCP
  192. netif->dhcps_pcb = NULL;
  193. #endif
  194. #endif /* LWIP_DHCP */
  195. #if LWIP_AUTOIP
  196. /* netif not under AutoIP control by default */
  197. netif->autoip = NULL;
  198. #endif /* LWIP_AUTOIP */
  199. #if LWIP_IPV6_AUTOCONFIG
  200. #if ESP_IPV6_AUTOCONFIG
  201. netif->ip6_autoconfig_enabled = 1;
  202. #else
  203. /* IPv6 address autoconfiguration not enabled by default */
  204. netif->ip6_autoconfig_enabled = 0;
  205. #endif
  206. #endif /* LWIP_IPV6_AUTOCONFIG */
  207. #if LWIP_IPV6_SEND_ROUTER_SOLICIT
  208. netif->rs_count = LWIP_ND6_MAX_MULTICAST_SOLICIT;
  209. #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
  210. #if LWIP_IPV6_DHCP6
  211. /* netif not under DHCPv6 control by default */
  212. netif->dhcp6 = NULL;
  213. #endif /* LWIP_IPV6_DHCP6 */
  214. #if LWIP_NETIF_STATUS_CALLBACK
  215. netif->status_callback = NULL;
  216. #endif /* LWIP_NETIF_STATUS_CALLBACK */
  217. #if LWIP_NETIF_LINK_CALLBACK
  218. netif->link_callback = NULL;
  219. #endif /* LWIP_NETIF_LINK_CALLBACK */
  220. #if LWIP_IGMP
  221. netif->igmp_mac_filter = NULL;
  222. #endif /* LWIP_IGMP */
  223. #if LWIP_IPV6 && LWIP_IPV6_MLD
  224. netif->mld_mac_filter = NULL;
  225. #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
  226. #if ENABLE_LOOPBACK
  227. netif->loop_first = NULL;
  228. netif->loop_last = NULL;
  229. #endif /* ENABLE_LOOPBACK */
  230. /* remember netif specific state information data */
  231. netif->state = state;
  232. netif->num = netif_num++;
  233. netif->input = input;
  234. NETIF_SET_HWADDRHINT(netif, NULL);
  235. #if ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS
  236. netif->loop_cnt_current = 0;
  237. #endif /* ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS */
  238. #if LWIP_IPV4
  239. netif_set_addr(netif, ipaddr, netmask, gw);
  240. #endif /* LWIP_IPV4 */
  241. /* call user specified initialization function for netif */
  242. if (init(netif) != ERR_OK) {
  243. return NULL;
  244. }
  245. /* add this netif to the list */
  246. netif->next = netif_list;
  247. netif_list = netif;
  248. mib2_netif_added(netif);
  249. #if LWIP_IGMP
  250. /* start IGMP processing */
  251. if (netif->flags & NETIF_FLAG_IGMP) {
  252. igmp_start(netif);
  253. }
  254. #endif /* LWIP_IGMP */
  255. LWIP_DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP",
  256. netif->name[0], netif->name[1]));
  257. #if LWIP_IPV4
  258. LWIP_DEBUGF(NETIF_DEBUG, (" addr "));
  259. ip4_addr_debug_print(NETIF_DEBUG, ipaddr);
  260. LWIP_DEBUGF(NETIF_DEBUG, (" netmask "));
  261. ip4_addr_debug_print(NETIF_DEBUG, netmask);
  262. LWIP_DEBUGF(NETIF_DEBUG, (" gw "));
  263. ip4_addr_debug_print(NETIF_DEBUG, gw);
  264. #endif /* LWIP_IPV4 */
  265. LWIP_DEBUGF(NETIF_DEBUG, ("\n"));
  266. return netif;
  267. }
  268. #if LWIP_IPV4
  269. /**
  270. * Change IP address configuration for a network interface (including netmask
  271. * and default gateway).
  272. *
  273. * @param netif the network interface to change
  274. * @param ipaddr the new IP address
  275. * @param netmask the new netmask
  276. * @param gw the new default gateway
  277. */
  278. void
  279. netif_set_addr(struct netif *netif, const ip4_addr_t *ipaddr, const ip4_addr_t *netmask,
  280. const ip4_addr_t *gw)
  281. {
  282. netif_set_netmask(netif, netmask);
  283. netif_set_gw(netif, gw);
  284. /* set ipaddr last to ensure netmask/gw have been set when status callback is called */
  285. netif_set_ipaddr(netif, ipaddr);
  286. }
  287. #endif /* LWIP_IPV4*/
  288. /**
  289. * Remove a network interface from the list of lwIP netifs.
  290. *
  291. * @param netif the network interface to remove
  292. */
  293. void
  294. netif_remove(struct netif *netif)
  295. {
  296. if (netif == NULL) {
  297. return;
  298. }
  299. #if LWIP_IPV4
  300. if (!ip4_addr_isany_val(*netif_ip4_addr(netif))) {
  301. #if LWIP_TCP
  302. tcp_netif_ipv4_addr_changed(netif_ip4_addr(netif), NULL);
  303. #endif /* LWIP_TCP */
  304. /* cannot do this for UDP, as there is no 'err' callback in udp pcbs */
  305. }
  306. #if LWIP_IGMP
  307. /* stop IGMP processing */
  308. if (netif->flags & NETIF_FLAG_IGMP) {
  309. igmp_stop(netif);
  310. }
  311. #endif /* LWIP_IGMP */
  312. #endif /* LWIP_IPV4*/
  313. #if LWIP_IPV6 && LWIP_IPV6_MLD
  314. /* stop MLD processing */
  315. mld6_stop(netif);
  316. #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
  317. if (netif_is_up(netif)) {
  318. /* set netif down before removing (call callback function) */
  319. netif_set_down(netif);
  320. }
  321. mib2_remove_ip4(netif);
  322. /* this netif is default? */
  323. if (netif_default == netif) {
  324. /* reset default netif */
  325. netif_set_default(NULL);
  326. }
  327. /* is it the first netif? */
  328. if (netif_list == netif) {
  329. netif_list = netif->next;
  330. } else {
  331. /* look for netif further down the list */
  332. struct netif * tmp_netif;
  333. for (tmp_netif = netif_list; tmp_netif != NULL; tmp_netif = tmp_netif->next) {
  334. if (tmp_netif->next == netif) {
  335. tmp_netif->next = netif->next;
  336. break;
  337. }
  338. }
  339. if (tmp_netif == NULL) {
  340. return; /* netif is not on the list */
  341. }
  342. }
  343. mib2_netif_removed(netif);
  344. #if LWIP_NETIF_REMOVE_CALLBACK
  345. if (netif->remove_callback) {
  346. netif->remove_callback(netif);
  347. }
  348. #endif /* LWIP_NETIF_REMOVE_CALLBACK */
  349. LWIP_DEBUGF( NETIF_DEBUG, ("netif_remove: removed netif\n") );
  350. }
  351. /**
  352. * Find a network interface by searching for its name
  353. *
  354. * @param name the name of the netif (like netif->name) plus concatenated number
  355. * in ascii representation (e.g. 'en0')
  356. */
  357. struct netif *
  358. netif_find(const char *name)
  359. {
  360. struct netif *netif;
  361. u8_t num;
  362. if (name == NULL) {
  363. return NULL;
  364. }
  365. num = name[2] - '0';
  366. for (netif = netif_list; netif != NULL; netif = netif->next) {
  367. if (num == netif->num &&
  368. name[0] == netif->name[0] &&
  369. name[1] == netif->name[1]) {
  370. LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: found %c%c\n", name[0], name[1]));
  371. return netif;
  372. }
  373. }
  374. LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
  375. return NULL;
  376. }
  377. #if LWIP_IPV4
  378. /**
  379. * Change the IP address of a network interface
  380. *
  381. * @param netif the network interface to change
  382. * @param ipaddr the new IP address
  383. *
  384. * @note call netif_set_addr() if you also want to change netmask and
  385. * default gateway
  386. */
  387. void
  388. netif_set_ipaddr(struct netif *netif, const ip4_addr_t *ipaddr)
  389. {
  390. ip4_addr_t new_addr = (ipaddr ? *ipaddr : *IP4_ADDR_ANY);
  391. /* address is actually being changed? */
  392. if (ip4_addr_cmp(&new_addr, netif_ip4_addr(netif)) == 0) {
  393. LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: netif address being changed\n"));
  394. #if LWIP_TCP
  395. tcp_netif_ipv4_addr_changed(netif_ip4_addr(netif), ipaddr);
  396. #endif /* LWIP_TCP */
  397. #if LWIP_UDP
  398. udp_netif_ipv4_addr_changed(netif_ip4_addr(netif), ipaddr);
  399. #endif /* LWIP_UDP */
  400. mib2_remove_ip4(netif);
  401. mib2_remove_route_ip4(0, netif);
  402. /* set new IP address to netif */
  403. ip4_addr_set(ip_2_ip4(&netif->ip_addr), ipaddr);
  404. IP_SET_TYPE_VAL(netif->ip_addr, IPADDR_TYPE_V4);
  405. mib2_add_ip4(netif);
  406. mib2_add_route_ip4(0, netif);
  407. netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV4);
  408. NETIF_STATUS_CALLBACK(netif);
  409. }
  410. 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",
  411. netif->name[0], netif->name[1],
  412. ip4_addr1_16(netif_ip4_addr(netif)),
  413. ip4_addr2_16(netif_ip4_addr(netif)),
  414. ip4_addr3_16(netif_ip4_addr(netif)),
  415. ip4_addr4_16(netif_ip4_addr(netif))));
  416. }
  417. /**
  418. * Change the default gateway for a network interface
  419. *
  420. * @param netif the network interface to change
  421. * @param gw the new default gateway
  422. *
  423. * @note call netif_set_addr() if you also want to change ip address and netmask
  424. */
  425. void
  426. netif_set_gw(struct netif *netif, const ip4_addr_t *gw)
  427. {
  428. ip4_addr_set(ip_2_ip4(&netif->gw), gw);
  429. IP_SET_TYPE_VAL(netif->gw, IPADDR_TYPE_V4);
  430. 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",
  431. netif->name[0], netif->name[1],
  432. ip4_addr1_16(netif_ip4_gw(netif)),
  433. ip4_addr2_16(netif_ip4_gw(netif)),
  434. ip4_addr3_16(netif_ip4_gw(netif)),
  435. ip4_addr4_16(netif_ip4_gw(netif))));
  436. }
  437. /**
  438. * Change the netmask of a network interface
  439. *
  440. * @param netif the network interface to change
  441. * @param netmask the new netmask
  442. *
  443. * @note call netif_set_addr() if you also want to change ip address and
  444. * default gateway
  445. */
  446. void
  447. netif_set_netmask(struct netif *netif, const ip4_addr_t *netmask)
  448. {
  449. mib2_remove_route_ip4(0, netif);
  450. /* set new netmask to netif */
  451. ip4_addr_set(ip_2_ip4(&netif->netmask), netmask);
  452. IP_SET_TYPE_VAL(netif->netmask, IPADDR_TYPE_V4);
  453. mib2_add_route_ip4(0, netif);
  454. 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",
  455. netif->name[0], netif->name[1],
  456. ip4_addr1_16(netif_ip4_netmask(netif)),
  457. ip4_addr2_16(netif_ip4_netmask(netif)),
  458. ip4_addr3_16(netif_ip4_netmask(netif)),
  459. ip4_addr4_16(netif_ip4_netmask(netif))));
  460. }
  461. #endif /* LWIP_IPV4 */
  462. /**
  463. * Set a network interface as the default network interface
  464. * (used to output all packets for which no specific route is found)
  465. *
  466. * @param netif the default network interface
  467. */
  468. void
  469. netif_set_default(struct netif *netif)
  470. {
  471. if (netif == NULL) {
  472. /* remove default route */
  473. mib2_remove_route_ip4(1, netif);
  474. } else {
  475. /* install default route */
  476. mib2_add_route_ip4(1, netif);
  477. }
  478. netif_default = netif;
  479. LWIP_DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
  480. netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
  481. }
  482. /**
  483. * Bring an interface up, available for processing
  484. * traffic.
  485. */
  486. void
  487. netif_set_up(struct netif *netif)
  488. {
  489. if (!(netif->flags & NETIF_FLAG_UP)) {
  490. netif->flags |= NETIF_FLAG_UP;
  491. MIB2_COPY_SYSUPTIME_TO(&netif->ts);
  492. NETIF_STATUS_CALLBACK(netif);
  493. if (netif->flags & NETIF_FLAG_LINK_UP) {
  494. netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV4|NETIF_REPORT_TYPE_IPV6);
  495. }
  496. }
  497. }
  498. /** Send ARP/IGMP/MLD/RS events, e.g. on link-up/netif-up or addr-change
  499. */
  500. static void
  501. netif_issue_reports(struct netif* netif, u8_t report_type)
  502. {
  503. #if LWIP_IPV4
  504. if ((report_type & NETIF_REPORT_TYPE_IPV4) &&
  505. !ip4_addr_isany_val(*netif_ip4_addr(netif))) {
  506. #if LWIP_ARP
  507. /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */
  508. if (netif->flags & (NETIF_FLAG_ETHARP)) {
  509. etharp_gratuitous(netif);
  510. }
  511. #endif /* LWIP_ARP */
  512. #if LWIP_IGMP
  513. /* resend IGMP memberships */
  514. if (netif->flags & NETIF_FLAG_IGMP) {
  515. igmp_report_groups(netif);
  516. }
  517. #endif /* LWIP_IGMP */
  518. }
  519. #endif /* LWIP_IPV4 */
  520. #if LWIP_IPV6
  521. if (report_type & NETIF_REPORT_TYPE_IPV6) {
  522. #if LWIP_IPV6_MLD
  523. /* send mld memberships */
  524. mld6_report_groups(netif);
  525. #endif /* LWIP_IPV6_MLD */
  526. #if LWIP_IPV6_SEND_ROUTER_SOLICIT
  527. /* Send Router Solicitation messages. */
  528. netif->rs_count = LWIP_ND6_MAX_MULTICAST_SOLICIT;
  529. #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
  530. }
  531. #endif /* LWIP_IPV6 */
  532. }
  533. /**
  534. * Bring an interface down, disabling any traffic processing.
  535. */
  536. void
  537. netif_set_down(struct netif *netif)
  538. {
  539. if (netif->flags & NETIF_FLAG_UP) {
  540. netif->flags &= ~NETIF_FLAG_UP;
  541. MIB2_COPY_SYSUPTIME_TO(&netif->ts);
  542. #if LWIP_IPV4 && LWIP_ARP
  543. if (netif->flags & NETIF_FLAG_ETHARP) {
  544. etharp_cleanup_netif(netif);
  545. }
  546. #endif /* LWIP_IPV4 && LWIP_ARP */
  547. NETIF_STATUS_CALLBACK(netif);
  548. }
  549. }
  550. #if LWIP_NETIF_STATUS_CALLBACK
  551. /**
  552. * Set callback to be called when interface is brought up/down or address is changed while up
  553. */
  554. void
  555. netif_set_status_callback(struct netif *netif, netif_status_callback_fn status_callback)
  556. {
  557. if (netif) {
  558. netif->status_callback = status_callback;
  559. }
  560. }
  561. #endif /* LWIP_NETIF_STATUS_CALLBACK */
  562. #if LWIP_NETIF_REMOVE_CALLBACK
  563. /**
  564. * Set callback to be called when the interface has been removed
  565. */
  566. void
  567. netif_set_remove_callback(struct netif *netif, netif_status_callback_fn remove_callback)
  568. {
  569. if (netif) {
  570. netif->remove_callback = remove_callback;
  571. }
  572. }
  573. #endif /* LWIP_NETIF_REMOVE_CALLBACK */
  574. /**
  575. * Called by a driver when its link goes up
  576. */
  577. void
  578. netif_set_link_up(struct netif *netif)
  579. {
  580. if (!(netif->flags & NETIF_FLAG_LINK_UP)) {
  581. netif->flags |= NETIF_FLAG_LINK_UP;
  582. #if LWIP_DHCP
  583. if (netif->dhcp) {
  584. dhcp_network_changed(netif);
  585. }
  586. #endif /* LWIP_DHCP */
  587. #if LWIP_AUTOIP
  588. if (netif->autoip) {
  589. autoip_network_changed(netif);
  590. }
  591. #endif /* LWIP_AUTOIP */
  592. if (netif->flags & NETIF_FLAG_UP) {
  593. netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV4|NETIF_REPORT_TYPE_IPV6);
  594. }
  595. NETIF_LINK_CALLBACK(netif);
  596. }
  597. }
  598. /**
  599. * Called by a driver when its link goes down
  600. */
  601. void
  602. netif_set_link_down(struct netif *netif )
  603. {
  604. if (netif->flags & NETIF_FLAG_LINK_UP) {
  605. netif->flags &= ~NETIF_FLAG_LINK_UP;
  606. NETIF_LINK_CALLBACK(netif);
  607. }
  608. }
  609. #if LWIP_NETIF_LINK_CALLBACK
  610. /**
  611. * Set callback to be called when link is brought up/down
  612. */
  613. void
  614. netif_set_link_callback(struct netif *netif, netif_status_callback_fn link_callback)
  615. {
  616. if (netif) {
  617. netif->link_callback = link_callback;
  618. }
  619. }
  620. #endif /* LWIP_NETIF_LINK_CALLBACK */
  621. #if ENABLE_LOOPBACK
  622. /**
  623. * Send an IP packet to be received on the same netif (loopif-like).
  624. * The pbuf is simply copied and handed back to netif->input.
  625. * In multithreaded mode, this is done directly since netif->input must put
  626. * the packet on a queue.
  627. * In callback mode, the packet is put on an internal queue and is fed to
  628. * netif->input by netif_poll().
  629. *
  630. * @param netif the lwip network interface structure
  631. * @param p the (IP) packet to 'send'
  632. * @return ERR_OK if the packet has been sent
  633. * ERR_MEM if the pbuf used to copy the packet couldn't be allocated
  634. */
  635. err_t
  636. netif_loop_output(struct netif *netif, struct pbuf *p)
  637. {
  638. struct pbuf *r;
  639. err_t err;
  640. struct pbuf *last;
  641. #if LWIP_LOOPBACK_MAX_PBUFS
  642. u8_t clen = 0;
  643. #endif /* LWIP_LOOPBACK_MAX_PBUFS */
  644. /* If we have a loopif, SNMP counters are adjusted for it,
  645. * if not they are adjusted for 'netif'. */
  646. #if MIB2_STATS
  647. #if LWIP_HAVE_LOOPIF
  648. struct netif *stats_if = &loop_netif;
  649. #else /* LWIP_HAVE_LOOPIF */
  650. struct netif *stats_if = netif;
  651. #endif /* LWIP_HAVE_LOOPIF */
  652. #endif /* MIB2_STATS */
  653. SYS_ARCH_DECL_PROTECT(lev);
  654. /* Allocate a new pbuf */
  655. r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
  656. if (r == NULL) {
  657. LINK_STATS_INC(link.memerr);
  658. LINK_STATS_INC(link.drop);
  659. MIB2_STATS_NETIF_INC(stats_if, ifoutdiscards);
  660. return ERR_MEM;
  661. }
  662. #if LWIP_LOOPBACK_MAX_PBUFS
  663. clen = pbuf_clen(r);
  664. /* check for overflow or too many pbuf on queue */
  665. if (((netif->loop_cnt_current + clen) < netif->loop_cnt_current) ||
  666. ((netif->loop_cnt_current + clen) > LWIP_LOOPBACK_MAX_PBUFS)) {
  667. pbuf_free(r);
  668. LINK_STATS_INC(link.memerr);
  669. LINK_STATS_INC(link.drop);
  670. MIB2_STATS_NETIF_INC(stats_if, ifoutdiscards);
  671. return ERR_MEM;
  672. }
  673. netif->loop_cnt_current += clen;
  674. #endif /* LWIP_LOOPBACK_MAX_PBUFS */
  675. /* Copy the whole pbuf queue p into the single pbuf r */
  676. if ((err = pbuf_copy(r, p)) != ERR_OK) {
  677. pbuf_free(r);
  678. LINK_STATS_INC(link.memerr);
  679. LINK_STATS_INC(link.drop);
  680. MIB2_STATS_NETIF_INC(stats_if, ifoutdiscards);
  681. return err;
  682. }
  683. /* Put the packet on a linked list which gets emptied through calling
  684. netif_poll(). */
  685. /* let last point to the last pbuf in chain r */
  686. for (last = r; last->next != NULL; last = last->next);
  687. SYS_ARCH_PROTECT(lev);
  688. if (netif->loop_first != NULL) {
  689. LWIP_ASSERT("if first != NULL, last must also be != NULL", netif->loop_last != NULL);
  690. netif->loop_last->next = r;
  691. netif->loop_last = last;
  692. } else {
  693. netif->loop_first = r;
  694. netif->loop_last = last;
  695. }
  696. SYS_ARCH_UNPROTECT(lev);
  697. LINK_STATS_INC(link.xmit);
  698. MIB2_STATS_NETIF_ADD(stats_if, ifoutoctets, p->tot_len);
  699. MIB2_STATS_NETIF_INC(stats_if, ifoutucastpkts);
  700. #if LWIP_NETIF_LOOPBACK_MULTITHREADING
  701. /* For multithreading environment, schedule a call to netif_poll */
  702. tcpip_callback_with_block((tcpip_callback_fn)netif_poll, netif, 0);
  703. #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
  704. return ERR_OK;
  705. }
  706. #if LWIP_HAVE_LOOPIF
  707. #if LWIP_IPV4
  708. static err_t
  709. netif_loop_output_ipv4(struct netif *netif, struct pbuf *p, const ip4_addr_t* addr)
  710. {
  711. LWIP_UNUSED_ARG(addr);
  712. return netif_loop_output(netif, p);
  713. }
  714. #endif /* LWIP_IPV4 */
  715. #if LWIP_IPV6
  716. static err_t
  717. netif_loop_output_ipv6(struct netif *netif, struct pbuf *p, const ip6_addr_t* addr)
  718. {
  719. LWIP_UNUSED_ARG(addr);
  720. return netif_loop_output(netif, p);
  721. }
  722. #endif /* LWIP_IPV6 */
  723. #endif /* LWIP_HAVE_LOOPIF */
  724. /**
  725. * Call netif_poll() in the main loop of your application. This is to prevent
  726. * reentering non-reentrant functions like tcp_input(). Packets passed to
  727. * netif_loop_output() are put on a list that is passed to netif->input() by
  728. * netif_poll().
  729. */
  730. void
  731. netif_poll(struct netif *netif)
  732. {
  733. struct pbuf *in;
  734. /* If we have a loopif, SNMP counters are adjusted for it,
  735. * if not they are adjusted for 'netif'. */
  736. #if MIB2_STATS
  737. #if LWIP_HAVE_LOOPIF
  738. struct netif *stats_if = &loop_netif;
  739. #else /* LWIP_HAVE_LOOPIF */
  740. struct netif *stats_if = netif;
  741. #endif /* LWIP_HAVE_LOOPIF */
  742. #endif /* MIB2_STATS */
  743. SYS_ARCH_DECL_PROTECT(lev);
  744. do {
  745. /* Get a packet from the list. With SYS_LIGHTWEIGHT_PROT=1, this is protected */
  746. SYS_ARCH_PROTECT(lev);
  747. in = netif->loop_first;
  748. if (in != NULL) {
  749. struct pbuf *in_end = in;
  750. #if LWIP_LOOPBACK_MAX_PBUFS
  751. u8_t clen = 1;
  752. #endif /* LWIP_LOOPBACK_MAX_PBUFS */
  753. while (in_end->len != in_end->tot_len) {
  754. LWIP_ASSERT("bogus pbuf: len != tot_len but next == NULL!", in_end->next != NULL);
  755. in_end = in_end->next;
  756. #if LWIP_LOOPBACK_MAX_PBUFS
  757. clen++;
  758. #endif /* LWIP_LOOPBACK_MAX_PBUFS */
  759. }
  760. #if LWIP_LOOPBACK_MAX_PBUFS
  761. /* adjust the number of pbufs on queue */
  762. LWIP_ASSERT("netif->loop_cnt_current underflow",
  763. ((netif->loop_cnt_current - clen) < netif->loop_cnt_current));
  764. netif->loop_cnt_current -= clen;
  765. #endif /* LWIP_LOOPBACK_MAX_PBUFS */
  766. /* 'in_end' now points to the last pbuf from 'in' */
  767. if (in_end == netif->loop_last) {
  768. /* this was the last pbuf in the list */
  769. netif->loop_first = netif->loop_last = NULL;
  770. } else {
  771. /* pop the pbuf off the list */
  772. netif->loop_first = in_end->next;
  773. LWIP_ASSERT("should not be null since first != last!", netif->loop_first != NULL);
  774. }
  775. /* De-queue the pbuf from its successors on the 'loop_' list. */
  776. in_end->next = NULL;
  777. }
  778. SYS_ARCH_UNPROTECT(lev);
  779. if (in != NULL) {
  780. LINK_STATS_INC(link.recv);
  781. MIB2_STATS_NETIF_ADD(stats_if, ifinoctets, in->tot_len);
  782. MIB2_STATS_NETIF_INC(stats_if, ifinucastpkts);
  783. /* loopback packets are always IP packets! */
  784. if (ip_input(in, netif) != ERR_OK) {
  785. pbuf_free(in);
  786. }
  787. /* Don't reference the packet any more! */
  788. in = NULL;
  789. }
  790. /* go on while there is a packet on the list */
  791. } while (netif->loop_first != NULL);
  792. }
  793. #if !LWIP_NETIF_LOOPBACK_MULTITHREADING
  794. /**
  795. * Calls netif_poll() for every netif on the netif_list.
  796. */
  797. void
  798. netif_poll_all(void)
  799. {
  800. struct netif *netif = netif_list;
  801. /* loop through netifs */
  802. while (netif != NULL) {
  803. netif_poll(netif);
  804. /* proceed to next network interface */
  805. netif = netif->next;
  806. }
  807. }
  808. #endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */
  809. #endif /* ENABLE_LOOPBACK */
  810. #if LWIP_IPV6
  811. /** Checks if a specific address is assigned to the netif and returns its
  812. * index.
  813. *
  814. * @param netif the netif to check
  815. * @param ip6addr the IPv6 address to find
  816. * @return >= 0: address found, this is its index
  817. * -1: address not found on this netif
  818. */
  819. s8_t
  820. netif_get_ip6_addr_match(struct netif *netif, const ip6_addr_t *ip6addr)
  821. {
  822. s8_t i;
  823. for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
  824. if (!ip6_addr_isinvalid(netif_ip6_addr_state(netif, i)) &&
  825. ip6_addr_cmp(netif_ip6_addr(netif, i), ip6addr)) {
  826. return i;
  827. }
  828. }
  829. return -1;
  830. }
  831. /** Create a link-local IPv6 address on a netif (stored in slot 0)
  832. *
  833. * @param netif the netif to create the address on
  834. * @param from_mac_48bit if != 0, assume hwadr is a 48-bit MAC address (std conversion)
  835. * if == 0, use hwaddr directly as interface ID
  836. */
  837. void
  838. netif_create_ip6_linklocal_address(struct netif *netif, u8_t from_mac_48bit)
  839. {
  840. u8_t i, addr_index;
  841. /* Link-local prefix. */
  842. ip_2_ip6(&netif->ip6_addr[0])->addr[0] = PP_HTONL(0xfe800000ul);
  843. ip_2_ip6(&netif->ip6_addr[0])->addr[1] = 0;
  844. /* Generate interface ID. */
  845. if (from_mac_48bit) {
  846. /* Assume hwaddr is a 48-bit IEEE 802 MAC. Convert to EUI-64 address. Complement Group bit. */
  847. ip_2_ip6(&netif->ip6_addr[0])->addr[2] = htonl((((u32_t)(netif->hwaddr[0] ^ 0x02)) << 24) |
  848. ((u32_t)(netif->hwaddr[1]) << 16) |
  849. ((u32_t)(netif->hwaddr[2]) << 8) |
  850. (0xff));
  851. ip_2_ip6(&netif->ip6_addr[0])->addr[3] = htonl((0xfeul << 24) |
  852. ((u32_t)(netif->hwaddr[3]) << 16) |
  853. ((u32_t)(netif->hwaddr[4]) << 8) |
  854. (netif->hwaddr[5]));
  855. } else {
  856. /* Use hwaddr directly as interface ID. */
  857. ip_2_ip6(&netif->ip6_addr[0])->addr[2] = 0;
  858. ip_2_ip6(&netif->ip6_addr[0])->addr[3] = 0;
  859. addr_index = 3;
  860. for (i = 0; (i < 8) && (i < netif->hwaddr_len); i++) {
  861. if (i == 4) {
  862. addr_index--;
  863. }
  864. ip_2_ip6(&netif->ip6_addr[0])->addr[addr_index] |= ((u32_t)(netif->hwaddr[netif->hwaddr_len - i - 1])) << (8 * (i & 0x03));
  865. }
  866. }
  867. /* Set address state. */
  868. #if LWIP_IPV6_DUP_DETECT_ATTEMPTS
  869. /* Will perform duplicate address detection (DAD). */
  870. netif->ip6_addr_state[0] = IP6_ADDR_TENTATIVE;
  871. #else
  872. /* Consider address valid. */
  873. netif->ip6_addr_state[0] = IP6_ADDR_PREFERRED;
  874. #endif /* LWIP_IPV6_AUTOCONFIG */
  875. }
  876. /** This function allows for the easy addition of a new IPv6 address to an interface.
  877. * It takes care of finding an empty slot and then sets the address tentative
  878. * (to make sure that all the subsequent processing happens).
  879. *
  880. * @param netif netif to add the address on
  881. * @param ip6addr address to add
  882. * @param chosen_idx if != NULL, the chosen IPv6 address index will be stored here
  883. */
  884. err_t
  885. netif_add_ip6_address(struct netif *netif, const ip6_addr_t *ip6addr, s8_t *chosen_idx)
  886. {
  887. s8_t i;
  888. i = netif_get_ip6_addr_match(netif, ip6addr);
  889. if (i >= 0) {
  890. /* Address already added */
  891. if (chosen_idx != NULL) {
  892. *chosen_idx = i;
  893. }
  894. return ERR_OK;
  895. }
  896. /* Find a free slot -- musn't be the first one (reserved for link local) */
  897. for (i = 1; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
  898. if (!ip6_addr_isvalid(netif->ip6_addr_state[i])) {
  899. ip_addr_copy_from_ip6(netif->ip6_addr[i], *ip6addr);
  900. netif_ip6_addr_set_state(netif, i, IP6_ADDR_TENTATIVE);
  901. if (chosen_idx != NULL) {
  902. *chosen_idx = i;
  903. }
  904. return ERR_OK;
  905. }
  906. }
  907. if (chosen_idx != NULL) {
  908. *chosen_idx = -1;
  909. }
  910. return ERR_VAL;
  911. }
  912. /** Dummy IPv6 output function for netifs not supporting IPv6
  913. */
  914. static err_t
  915. netif_null_output_ip6(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr)
  916. {
  917. LWIP_UNUSED_ARG(netif);
  918. LWIP_UNUSED_ARG(p);
  919. LWIP_UNUSED_ARG(ipaddr);
  920. return ERR_IF;
  921. }
  922. #endif /* LWIP_IPV6 */