netif.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855
  1. /**
  2. * @file
  3. * lwIP network interface abstraction
  4. *
  5. * @defgroup netif Network interface (NETIF)
  6. * @ingroup callbackstyle_api
  7. *
  8. * @defgroup netif_ip4 IPv4 address handling
  9. * @ingroup netif
  10. *
  11. * @defgroup netif_ip6 IPv6 address handling
  12. * @ingroup netif
  13. *
  14. * @defgroup netif_cd Client data handling
  15. * Store data (void*) on a netif for application usage.
  16. * @see @ref LWIP_NUM_NETIF_CLIENT_DATA
  17. * @ingroup netif
  18. */
  19. /*
  20. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  21. * All rights reserved.
  22. *
  23. * Redistribution and use in source and binary forms, with or without modification,
  24. * are permitted provided that the following conditions are met:
  25. *
  26. * 1. Redistributions of source code must retain the above copyright notice,
  27. * this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright notice,
  29. * this list of conditions and the following disclaimer in the documentation
  30. * and/or other materials provided with the distribution.
  31. * 3. The name of the author may not be used to endorse or promote products
  32. * derived from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  35. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  36. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  37. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  38. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  39. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  40. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  41. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  42. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  43. * OF SUCH DAMAGE.
  44. *
  45. * This file is part of the lwIP TCP/IP stack.
  46. *
  47. * Author: Adam Dunkels <adam@sics.se>
  48. */
  49. #include "lwip/opt.h"
  50. #include <string.h> /* memset */
  51. #include <stdlib.h> /* atoi */
  52. #include "lwip/def.h"
  53. #include "lwip/ip_addr.h"
  54. #include "lwip/ip6_addr.h"
  55. #include "lwip/netif.h"
  56. #include "lwip/priv/tcp_priv.h"
  57. #include "lwip/udp.h"
  58. #include "lwip/priv/raw_priv.h"
  59. #include "lwip/snmp.h"
  60. #include "lwip/igmp.h"
  61. #include "lwip/etharp.h"
  62. #include "lwip/stats.h"
  63. #include "lwip/sys.h"
  64. #include "lwip/ip.h"
  65. #if ENABLE_LOOPBACK
  66. #if LWIP_NETIF_LOOPBACK_MULTITHREADING
  67. #include "lwip/tcpip.h"
  68. #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
  69. #endif /* ENABLE_LOOPBACK */
  70. #include "netif/ethernet.h"
  71. #if LWIP_AUTOIP
  72. #include "lwip/autoip.h"
  73. #endif /* LWIP_AUTOIP */
  74. #if LWIP_DHCP
  75. #include "lwip/dhcp.h"
  76. #endif /* LWIP_DHCP */
  77. #if LWIP_ACD
  78. #include "lwip/acd.h"
  79. #endif /* LWIP_ACD */
  80. #if LWIP_IPV6_DHCP6
  81. #include "lwip/dhcp6.h"
  82. #endif /* LWIP_IPV6_DHCP6 */
  83. #if LWIP_IPV6_MLD
  84. #include "lwip/mld6.h"
  85. #endif /* LWIP_IPV6_MLD */
  86. #if LWIP_IPV6
  87. #include "lwip/nd6.h"
  88. #endif
  89. #if LWIP_NETIF_STATUS_CALLBACK
  90. #define NETIF_STATUS_CALLBACK(n) do{ if (n->status_callback) { (n->status_callback)(n); }}while(0)
  91. #else
  92. #define NETIF_STATUS_CALLBACK(n)
  93. #endif /* LWIP_NETIF_STATUS_CALLBACK */
  94. #if LWIP_NETIF_LINK_CALLBACK
  95. #define NETIF_LINK_CALLBACK(n) do{ if (n->link_callback) { (n->link_callback)(n); }}while(0)
  96. #else
  97. #define NETIF_LINK_CALLBACK(n)
  98. #endif /* LWIP_NETIF_LINK_CALLBACK */
  99. #if LWIP_NETIF_EXT_STATUS_CALLBACK
  100. static netif_ext_callback_t *ext_callback;
  101. #endif
  102. #if !LWIP_SINGLE_NETIF
  103. struct netif *netif_list;
  104. #endif /* !LWIP_SINGLE_NETIF */
  105. struct netif *netif_default;
  106. #define netif_index_to_num(index) ((index) - 1)
  107. static u8_t netif_num;
  108. #if LWIP_NUM_NETIF_CLIENT_DATA > 0
  109. static u8_t netif_client_id;
  110. #endif
  111. #define NETIF_REPORT_TYPE_IPV4 0x01
  112. #define NETIF_REPORT_TYPE_IPV6 0x02
  113. static void netif_issue_reports(struct netif *netif, u8_t report_type);
  114. #if LWIP_IPV6
  115. static err_t netif_null_output_ip6(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr);
  116. #endif /* LWIP_IPV6 */
  117. #if LWIP_IPV4
  118. static err_t netif_null_output_ip4(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr);
  119. #endif /* LWIP_IPV4 */
  120. #if LWIP_HAVE_LOOPIF
  121. #if LWIP_IPV4
  122. static err_t netif_loop_output_ipv4(struct netif *netif, struct pbuf *p, const ip4_addr_t *addr);
  123. #endif
  124. #if LWIP_IPV6
  125. static err_t netif_loop_output_ipv6(struct netif *netif, struct pbuf *p, const ip6_addr_t *addr);
  126. #endif
  127. static struct netif loop_netif;
  128. #if LWIP_TESTMODE
  129. struct netif* netif_get_loopif(void)
  130. {
  131. return &loop_netif;
  132. }
  133. #endif
  134. /**
  135. * Initialize a lwip network interface structure for a loopback interface
  136. *
  137. * @param netif the lwip network interface structure for this loopif
  138. * @return ERR_OK if the loopif is initialized
  139. * ERR_MEM if private data couldn't be allocated
  140. */
  141. static err_t
  142. netif_loopif_init(struct netif *netif)
  143. {
  144. LWIP_ASSERT("netif_loopif_init: invalid netif", netif != NULL);
  145. /* initialize the snmp variables and counters inside the struct netif
  146. * ifSpeed: no assumption can be made!
  147. */
  148. MIB2_INIT_NETIF(netif, snmp_ifType_softwareLoopback, 0);
  149. netif->name[0] = 'l';
  150. netif->name[1] = 'o';
  151. #if LWIP_IPV4
  152. netif->output = netif_loop_output_ipv4;
  153. #endif
  154. #if LWIP_IPV6
  155. netif->output_ip6 = netif_loop_output_ipv6;
  156. #endif
  157. #if LWIP_LOOPIF_MULTICAST
  158. netif_set_flags(netif, NETIF_FLAG_IGMP);
  159. #endif
  160. NETIF_SET_CHECKSUM_CTRL(netif, NETIF_CHECKSUM_DISABLE_ALL);
  161. return ERR_OK;
  162. }
  163. #endif /* LWIP_HAVE_LOOPIF */
  164. void
  165. netif_init(void)
  166. {
  167. #if LWIP_HAVE_LOOPIF
  168. #if LWIP_IPV4
  169. #define LOOPIF_ADDRINIT &loop_ipaddr, &loop_netmask, &loop_gw,
  170. ip4_addr_t loop_ipaddr, loop_netmask, loop_gw;
  171. IP4_ADDR(&loop_gw, 127, 0, 0, 1);
  172. IP4_ADDR(&loop_ipaddr, 127, 0, 0, 1);
  173. IP4_ADDR(&loop_netmask, 255, 0, 0, 0);
  174. #else /* LWIP_IPV4 */
  175. #define LOOPIF_ADDRINIT
  176. #endif /* LWIP_IPV4 */
  177. #if NO_SYS
  178. netif_add(&loop_netif, LOOPIF_ADDRINIT NULL, netif_loopif_init, ip_input);
  179. #else /* NO_SYS */
  180. netif_add(&loop_netif, LOOPIF_ADDRINIT NULL, netif_loopif_init, tcpip_input);
  181. #endif /* NO_SYS */
  182. #if LWIP_IPV6
  183. IP_ADDR6_HOST(loop_netif.ip6_addr, 0, 0, 0, 0x00000001UL);
  184. loop_netif.ip6_addr_state[0] = IP6_ADDR_VALID;
  185. #endif /* LWIP_IPV6 */
  186. netif_set_link_up(&loop_netif);
  187. netif_set_up(&loop_netif);
  188. #endif /* LWIP_HAVE_LOOPIF */
  189. }
  190. /**
  191. * @ingroup lwip_nosys
  192. * Forwards a received packet for input processing with
  193. * ethernet_input() or ip_input() depending on netif flags.
  194. * Don't call directly, pass to netif_add() and call
  195. * netif->input().
  196. * Only works if the netif driver correctly sets
  197. * NETIF_FLAG_ETHARP and/or NETIF_FLAG_ETHERNET flag!
  198. */
  199. err_t
  200. netif_input(struct pbuf *p, struct netif *inp)
  201. {
  202. LWIP_ASSERT_CORE_LOCKED();
  203. LWIP_ASSERT("netif_input: invalid pbuf", p != NULL);
  204. LWIP_ASSERT("netif_input: invalid netif", inp != NULL);
  205. #if LWIP_ETHERNET
  206. if (inp->flags & (NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET)) {
  207. return ethernet_input(p, inp);
  208. } else
  209. #endif /* LWIP_ETHERNET */
  210. return ip_input(p, inp);
  211. }
  212. /**
  213. * @ingroup netif
  214. * Add a network interface to the list of lwIP netifs.
  215. *
  216. * Same as @ref netif_add but without IPv4 addresses
  217. */
  218. struct netif *
  219. netif_add_noaddr(struct netif *netif, void *state, netif_init_fn init, netif_input_fn input)
  220. {
  221. return netif_add(netif,
  222. #if LWIP_IPV4
  223. NULL, NULL, NULL,
  224. #endif /* LWIP_IPV4*/
  225. state, init, input);
  226. }
  227. /**
  228. * @ingroup netif
  229. * Add a network interface to the list of lwIP netifs.
  230. *
  231. * @param netif a pre-allocated netif structure
  232. * @param ipaddr IP address for the new netif
  233. * @param netmask network mask for the new netif
  234. * @param gw default gateway IP address for the new netif
  235. * @param state opaque data passed to the new netif
  236. * @param init callback function that initializes the interface
  237. * @param input callback function that is called to pass
  238. * ingress packets up in the protocol layer stack.<br>
  239. * It is recommended to use a function that passes the input directly
  240. * to the stack (netif_input(), NO_SYS=1 mode) or via sending a
  241. * message to TCPIP thread (tcpip_input(), NO_SYS=0 mode).<br>
  242. * These functions use netif flags NETIF_FLAG_ETHARP and NETIF_FLAG_ETHERNET
  243. * to decide whether to forward to ethernet_input() or ip_input().
  244. * In other words, the functions only work when the netif
  245. * driver is implemented correctly!<br>
  246. * Most members of struct netif should be be initialized by the
  247. * netif init function = netif driver (init parameter of this function).<br>
  248. * IPv6: Don't forget to call netif_create_ip6_linklocal_address() after
  249. * setting the MAC address in struct netif.hwaddr
  250. * (IPv6 requires a link-local address).
  251. *
  252. * @return netif, or NULL if failed.
  253. */
  254. struct netif *
  255. netif_add(struct netif *netif,
  256. #if LWIP_IPV4
  257. const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, const ip4_addr_t *gw,
  258. #endif /* LWIP_IPV4 */
  259. void *state, netif_init_fn init, netif_input_fn input)
  260. {
  261. #if LWIP_IPV6
  262. s8_t i;
  263. #endif
  264. LWIP_ASSERT_CORE_LOCKED();
  265. #if LWIP_SINGLE_NETIF
  266. if (netif_default != NULL) {
  267. LWIP_ASSERT("single netif already set", 0);
  268. return NULL;
  269. }
  270. #endif
  271. LWIP_ERROR("netif_add: invalid netif", netif != NULL, return NULL);
  272. LWIP_ERROR("netif_add: No init function given", init != NULL, return NULL);
  273. #if LWIP_IPV4
  274. if (ipaddr == NULL) {
  275. ipaddr = ip_2_ip4(IP4_ADDR_ANY);
  276. }
  277. if (netmask == NULL) {
  278. netmask = ip_2_ip4(IP4_ADDR_ANY);
  279. }
  280. if (gw == NULL) {
  281. gw = ip_2_ip4(IP4_ADDR_ANY);
  282. }
  283. /* reset new interface configuration state */
  284. ip_addr_set_zero_ip4(&netif->ip_addr);
  285. ip_addr_set_zero_ip4(&netif->netmask);
  286. ip_addr_set_zero_ip4(&netif->gw);
  287. netif->output = netif_null_output_ip4;
  288. #endif /* LWIP_IPV4 */
  289. #if LWIP_IPV6
  290. for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
  291. ip_addr_set_zero_ip6(&netif->ip6_addr[i]);
  292. netif->ip6_addr_state[i] = IP6_ADDR_INVALID;
  293. #if LWIP_IPV6_ADDRESS_LIFETIMES
  294. netif->ip6_addr_valid_life[i] = IP6_ADDR_LIFE_STATIC;
  295. netif->ip6_addr_pref_life[i] = IP6_ADDR_LIFE_STATIC;
  296. #endif /* LWIP_IPV6_ADDRESS_LIFETIMES */
  297. }
  298. netif->output_ip6 = netif_null_output_ip6;
  299. #endif /* LWIP_IPV6 */
  300. NETIF_SET_CHECKSUM_CTRL(netif, NETIF_CHECKSUM_ENABLE_ALL);
  301. netif->mtu = 0;
  302. netif->flags = 0;
  303. #ifdef netif_get_client_data
  304. memset(netif->client_data, 0, sizeof(netif->client_data));
  305. #endif /* LWIP_NUM_NETIF_CLIENT_DATA */
  306. #if LWIP_IPV6
  307. #if LWIP_IPV6_AUTOCONFIG
  308. /* IPv6 address autoconfiguration should be enabled by default */
  309. netif->ip6_autoconfig_enabled = 1;
  310. #endif /* LWIP_IPV6_AUTOCONFIG */
  311. nd6_restart_netif(netif);
  312. #endif /* LWIP_IPV6 */
  313. #if LWIP_NETIF_STATUS_CALLBACK
  314. netif->status_callback = NULL;
  315. #endif /* LWIP_NETIF_STATUS_CALLBACK */
  316. #if LWIP_NETIF_LINK_CALLBACK
  317. netif->link_callback = NULL;
  318. #endif /* LWIP_NETIF_LINK_CALLBACK */
  319. #if LWIP_IGMP
  320. netif->igmp_mac_filter = NULL;
  321. #endif /* LWIP_IGMP */
  322. #if LWIP_IPV6 && LWIP_IPV6_MLD
  323. netif->mld_mac_filter = NULL;
  324. #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
  325. /* remember netif specific state information data */
  326. netif->state = state;
  327. netif->num = netif_num;
  328. netif->input = input;
  329. #if LWIP_ACD
  330. netif->acd_list = NULL;
  331. #endif /* LWIP_ACD */
  332. NETIF_RESET_HINTS(netif);
  333. #if ENABLE_LOOPBACK
  334. netif->loop_first = NULL;
  335. netif->loop_last = NULL;
  336. #if LWIP_LOOPBACK_MAX_PBUFS
  337. netif->loop_cnt_current = 0;
  338. #endif /* LWIP_LOOPBACK_MAX_PBUFS */
  339. #if LWIP_NETIF_LOOPBACK_MULTITHREADING
  340. netif->reschedule_poll = 0;
  341. #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
  342. #endif /* ENABLE_LOOPBACK */
  343. #if LWIP_IPV4
  344. netif_set_addr(netif, ipaddr, netmask, gw);
  345. #endif /* LWIP_IPV4 */
  346. /* call user specified initialization function for netif */
  347. if (init(netif) != ERR_OK) {
  348. return NULL;
  349. }
  350. #if LWIP_IPV6 && LWIP_ND6_ALLOW_RA_UPDATES
  351. /* Initialize the MTU for IPv6 to the one set by the netif driver.
  352. This can be updated later by RA. */
  353. netif->mtu6 = netif->mtu;
  354. #endif /* LWIP_IPV6 && LWIP_ND6_ALLOW_RA_UPDATES */
  355. #if !LWIP_SINGLE_NETIF
  356. /* Assign a unique netif number in the range [0..254], so that (num+1) can
  357. serve as an interface index that fits in a u8_t.
  358. We assume that the new netif has not yet been added to the list here.
  359. This algorithm is O(n^2), but that should be OK for lwIP.
  360. */
  361. {
  362. struct netif *netif2;
  363. int num_netifs;
  364. do {
  365. if (netif->num == 255) {
  366. netif->num = 0;
  367. }
  368. num_netifs = 0;
  369. for (netif2 = netif_list; netif2 != NULL; netif2 = netif2->next) {
  370. LWIP_ASSERT("netif already added", netif2 != netif);
  371. num_netifs++;
  372. LWIP_ASSERT("too many netifs, max. supported number is 255", num_netifs <= 255);
  373. if (netif2->num == netif->num) {
  374. netif->num++;
  375. break;
  376. }
  377. }
  378. } while (netif2 != NULL);
  379. }
  380. if (netif->num == 254) {
  381. netif_num = 0;
  382. } else {
  383. netif_num = (u8_t)(netif->num + 1);
  384. }
  385. /* add this netif to the list */
  386. netif->next = netif_list;
  387. netif_list = netif;
  388. #endif /* "LWIP_SINGLE_NETIF */
  389. mib2_netif_added(netif);
  390. #if LWIP_IGMP
  391. /* start IGMP processing */
  392. if (netif->flags & NETIF_FLAG_IGMP) {
  393. igmp_start(netif);
  394. }
  395. #endif /* LWIP_IGMP */
  396. LWIP_DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP",
  397. netif->name[0], netif->name[1]));
  398. #if LWIP_IPV4
  399. LWIP_DEBUGF(NETIF_DEBUG, (" addr "));
  400. ip4_addr_debug_print(NETIF_DEBUG, ipaddr);
  401. LWIP_DEBUGF(NETIF_DEBUG, (" netmask "));
  402. ip4_addr_debug_print(NETIF_DEBUG, netmask);
  403. LWIP_DEBUGF(NETIF_DEBUG, (" gw "));
  404. ip4_addr_debug_print(NETIF_DEBUG, gw);
  405. #endif /* LWIP_IPV4 */
  406. LWIP_DEBUGF(NETIF_DEBUG, ("\n"));
  407. netif_invoke_ext_callback(netif, LWIP_NSC_NETIF_ADDED, NULL);
  408. return netif;
  409. }
  410. static void
  411. netif_do_ip_addr_changed(const ip_addr_t *old_addr, const ip_addr_t *new_addr)
  412. {
  413. #if LWIP_TCP
  414. tcp_netif_ip_addr_changed(old_addr, new_addr);
  415. #endif /* LWIP_TCP */
  416. #if LWIP_UDP
  417. udp_netif_ip_addr_changed(old_addr, new_addr);
  418. #endif /* LWIP_UDP */
  419. #if LWIP_RAW
  420. raw_netif_ip_addr_changed(old_addr, new_addr);
  421. #endif /* LWIP_RAW */
  422. }
  423. #if LWIP_IPV4
  424. static int
  425. netif_do_set_ipaddr(struct netif *netif, const ip4_addr_t *ipaddr, ip_addr_t *old_addr)
  426. {
  427. LWIP_ASSERT("invalid pointer", ipaddr != NULL);
  428. LWIP_ASSERT("invalid pointer", old_addr != NULL);
  429. /* address is actually being changed? */
  430. if (ip4_addr_eq(ipaddr, netif_ip4_addr(netif)) == 0) {
  431. ip_addr_t new_addr;
  432. *ip_2_ip4(&new_addr) = *ipaddr;
  433. IP_SET_TYPE_VAL(new_addr, IPADDR_TYPE_V4);
  434. ip_addr_copy(*old_addr, *netif_ip_addr4(netif));
  435. LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: netif address being changed\n"));
  436. netif_do_ip_addr_changed(old_addr, &new_addr);
  437. #if LWIP_ACD
  438. acd_netif_ip_addr_changed(netif, old_addr, &new_addr);
  439. #endif /* LWIP_ACD */
  440. mib2_remove_ip4(netif);
  441. mib2_remove_route_ip4(0, netif);
  442. /* set new IP address to netif */
  443. ip4_addr_set(ip_2_ip4(&netif->ip_addr), ipaddr);
  444. IP_SET_TYPE_VAL(netif->ip_addr, IPADDR_TYPE_V4);
  445. mib2_add_ip4(netif);
  446. mib2_add_route_ip4(0, netif);
  447. netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV4);
  448. NETIF_STATUS_CALLBACK(netif);
  449. return 1; /* address changed */
  450. }
  451. return 0; /* address unchanged */
  452. }
  453. /**
  454. * @ingroup netif_ip4
  455. * Change the IP address of a network interface
  456. *
  457. * @param netif the network interface to change
  458. * @param ipaddr the new IP address
  459. *
  460. * @note call netif_set_addr() if you also want to change netmask and
  461. * default gateway
  462. */
  463. void
  464. netif_set_ipaddr(struct netif *netif, const ip4_addr_t *ipaddr)
  465. {
  466. ip_addr_t old_addr;
  467. LWIP_ERROR("netif_set_ipaddr: invalid netif", netif != NULL, return);
  468. /* Don't propagate NULL pointer (IPv4 ANY) to subsequent functions */
  469. if (ipaddr == NULL) {
  470. ipaddr = IP4_ADDR_ANY4;
  471. }
  472. LWIP_ASSERT_CORE_LOCKED();
  473. if (netif_do_set_ipaddr(netif, ipaddr, &old_addr)) {
  474. #if LWIP_NETIF_EXT_STATUS_CALLBACK
  475. netif_ext_callback_args_t args;
  476. args.ipv4_changed.old_address = &old_addr;
  477. netif_invoke_ext_callback(netif, LWIP_NSC_IPV4_ADDRESS_CHANGED, &args);
  478. #endif
  479. }
  480. }
  481. static int
  482. netif_do_set_netmask(struct netif *netif, const ip4_addr_t *netmask, ip_addr_t *old_nm)
  483. {
  484. /* address is actually being changed? */
  485. if (ip4_addr_eq(netmask, netif_ip4_netmask(netif)) == 0) {
  486. #if LWIP_NETIF_EXT_STATUS_CALLBACK
  487. LWIP_ASSERT("invalid pointer", old_nm != NULL);
  488. ip_addr_copy(*old_nm, *netif_ip_netmask4(netif));
  489. #else
  490. LWIP_UNUSED_ARG(old_nm);
  491. #endif
  492. mib2_remove_route_ip4(0, netif);
  493. /* set new netmask to netif */
  494. ip4_addr_set(ip_2_ip4(&netif->netmask), netmask);
  495. IP_SET_TYPE_VAL(netif->netmask, IPADDR_TYPE_V4);
  496. mib2_add_route_ip4(0, netif);
  497. 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",
  498. netif->name[0], netif->name[1],
  499. ip4_addr1_16(netif_ip4_netmask(netif)),
  500. ip4_addr2_16(netif_ip4_netmask(netif)),
  501. ip4_addr3_16(netif_ip4_netmask(netif)),
  502. ip4_addr4_16(netif_ip4_netmask(netif))));
  503. return 1; /* netmask changed */
  504. }
  505. return 0; /* netmask unchanged */
  506. }
  507. /**
  508. * @ingroup netif_ip4
  509. * Change the netmask of a network interface
  510. *
  511. * @param netif the network interface to change
  512. * @param netmask the new netmask
  513. *
  514. * @note call netif_set_addr() if you also want to change ip address and
  515. * default gateway
  516. */
  517. void
  518. netif_set_netmask(struct netif *netif, const ip4_addr_t *netmask)
  519. {
  520. #if LWIP_NETIF_EXT_STATUS_CALLBACK
  521. ip_addr_t old_nm_val;
  522. ip_addr_t *old_nm = &old_nm_val;
  523. #else
  524. ip_addr_t *old_nm = NULL;
  525. #endif
  526. LWIP_ASSERT_CORE_LOCKED();
  527. LWIP_ERROR("netif_set_netmask: invalid netif", netif != NULL, return);
  528. /* Don't propagate NULL pointer (IPv4 ANY) to subsequent functions */
  529. if (netmask == NULL) {
  530. netmask = IP4_ADDR_ANY4;
  531. }
  532. if (netif_do_set_netmask(netif, netmask, old_nm)) {
  533. #if LWIP_NETIF_EXT_STATUS_CALLBACK
  534. netif_ext_callback_args_t args;
  535. args.ipv4_changed.old_netmask = old_nm;
  536. netif_invoke_ext_callback(netif, LWIP_NSC_IPV4_NETMASK_CHANGED, &args);
  537. #endif
  538. }
  539. }
  540. static int
  541. netif_do_set_gw(struct netif *netif, const ip4_addr_t *gw, ip_addr_t *old_gw)
  542. {
  543. /* address is actually being changed? */
  544. if (ip4_addr_eq(gw, netif_ip4_gw(netif)) == 0) {
  545. #if LWIP_NETIF_EXT_STATUS_CALLBACK
  546. LWIP_ASSERT("invalid pointer", old_gw != NULL);
  547. ip_addr_copy(*old_gw, *netif_ip_gw4(netif));
  548. #else
  549. LWIP_UNUSED_ARG(old_gw);
  550. #endif
  551. ip4_addr_set(ip_2_ip4(&netif->gw), gw);
  552. IP_SET_TYPE_VAL(netif->gw, IPADDR_TYPE_V4);
  553. 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",
  554. netif->name[0], netif->name[1],
  555. ip4_addr1_16(netif_ip4_gw(netif)),
  556. ip4_addr2_16(netif_ip4_gw(netif)),
  557. ip4_addr3_16(netif_ip4_gw(netif)),
  558. ip4_addr4_16(netif_ip4_gw(netif))));
  559. return 1; /* gateway changed */
  560. }
  561. return 0; /* gateway unchanged */
  562. }
  563. /**
  564. * @ingroup netif_ip4
  565. * Change the default gateway for a network interface
  566. *
  567. * @param netif the network interface to change
  568. * @param gw the new default gateway
  569. *
  570. * @note call netif_set_addr() if you also want to change ip address and netmask
  571. */
  572. void
  573. netif_set_gw(struct netif *netif, const ip4_addr_t *gw)
  574. {
  575. #if LWIP_NETIF_EXT_STATUS_CALLBACK
  576. ip_addr_t old_gw_val;
  577. ip_addr_t *old_gw = &old_gw_val;
  578. #else
  579. ip_addr_t *old_gw = NULL;
  580. #endif
  581. LWIP_ASSERT_CORE_LOCKED();
  582. LWIP_ERROR("netif_set_gw: invalid netif", netif != NULL, return);
  583. /* Don't propagate NULL pointer (IPv4 ANY) to subsequent functions */
  584. if (gw == NULL) {
  585. gw = IP4_ADDR_ANY4;
  586. }
  587. if (netif_do_set_gw(netif, gw, old_gw)) {
  588. #if LWIP_NETIF_EXT_STATUS_CALLBACK
  589. netif_ext_callback_args_t args;
  590. args.ipv4_changed.old_gw = old_gw;
  591. netif_invoke_ext_callback(netif, LWIP_NSC_IPV4_GATEWAY_CHANGED, &args);
  592. #endif
  593. }
  594. }
  595. /**
  596. * @ingroup netif_ip4
  597. * Change IP address configuration for a network interface (including netmask
  598. * and default gateway).
  599. *
  600. * @param netif the network interface to change
  601. * @param ipaddr the new IP address
  602. * @param netmask the new netmask
  603. * @param gw the new default gateway
  604. */
  605. void
  606. netif_set_addr(struct netif *netif, const ip4_addr_t *ipaddr, const ip4_addr_t *netmask,
  607. const ip4_addr_t *gw)
  608. {
  609. #if LWIP_NETIF_EXT_STATUS_CALLBACK
  610. netif_nsc_reason_t change_reason = LWIP_NSC_NONE;
  611. netif_ext_callback_args_t cb_args;
  612. ip_addr_t old_nm_val;
  613. ip_addr_t old_gw_val;
  614. ip_addr_t *old_nm = &old_nm_val;
  615. ip_addr_t *old_gw = &old_gw_val;
  616. #else
  617. ip_addr_t *old_nm = NULL;
  618. ip_addr_t *old_gw = NULL;
  619. #endif
  620. ip_addr_t old_addr;
  621. int remove;
  622. LWIP_ASSERT_CORE_LOCKED();
  623. /* Don't propagate NULL pointer (IPv4 ANY) to subsequent functions */
  624. if (ipaddr == NULL) {
  625. ipaddr = IP4_ADDR_ANY4;
  626. }
  627. if (netmask == NULL) {
  628. netmask = IP4_ADDR_ANY4;
  629. }
  630. if (gw == NULL) {
  631. gw = IP4_ADDR_ANY4;
  632. }
  633. remove = ip4_addr_isany(ipaddr);
  634. if (remove) {
  635. /* when removing an address, we have to remove it *before* changing netmask/gw
  636. to ensure that tcp RST segment can be sent correctly */
  637. if (netif_do_set_ipaddr(netif, ipaddr, &old_addr)) {
  638. #if LWIP_NETIF_EXT_STATUS_CALLBACK
  639. change_reason |= LWIP_NSC_IPV4_ADDRESS_CHANGED;
  640. cb_args.ipv4_changed.old_address = &old_addr;
  641. #endif
  642. }
  643. }
  644. if (netif_do_set_netmask(netif, netmask, old_nm)) {
  645. #if LWIP_NETIF_EXT_STATUS_CALLBACK
  646. change_reason |= LWIP_NSC_IPV4_NETMASK_CHANGED;
  647. cb_args.ipv4_changed.old_netmask = old_nm;
  648. #endif
  649. }
  650. if (netif_do_set_gw(netif, gw, old_gw)) {
  651. #if LWIP_NETIF_EXT_STATUS_CALLBACK
  652. change_reason |= LWIP_NSC_IPV4_GATEWAY_CHANGED;
  653. cb_args.ipv4_changed.old_gw = old_gw;
  654. #endif
  655. }
  656. if (!remove) {
  657. /* set ipaddr last to ensure netmask/gw have been set when status callback is called */
  658. if (netif_do_set_ipaddr(netif, ipaddr, &old_addr)) {
  659. #if LWIP_NETIF_EXT_STATUS_CALLBACK
  660. change_reason |= LWIP_NSC_IPV4_ADDRESS_CHANGED;
  661. cb_args.ipv4_changed.old_address = &old_addr;
  662. #endif
  663. }
  664. }
  665. #if LWIP_NETIF_EXT_STATUS_CALLBACK
  666. if (change_reason != LWIP_NSC_NONE) {
  667. change_reason |= LWIP_NSC_IPV4_SETTINGS_CHANGED;
  668. }
  669. if (!remove) {
  670. /* Issue a callback even if the address hasn't changed, eg. DHCP reboot */
  671. change_reason |= LWIP_NSC_IPV4_ADDR_VALID;
  672. }
  673. if (change_reason != LWIP_NSC_NONE) {
  674. netif_invoke_ext_callback(netif, change_reason, &cb_args);
  675. }
  676. #endif
  677. }
  678. #endif /* LWIP_IPV4*/
  679. /**
  680. * @ingroup netif
  681. * Remove a network interface from the list of lwIP netifs.
  682. *
  683. * @param netif the network interface to remove
  684. */
  685. void
  686. netif_remove(struct netif *netif)
  687. {
  688. #if LWIP_IPV6
  689. int i;
  690. #endif
  691. LWIP_ASSERT_CORE_LOCKED();
  692. if (netif == NULL) {
  693. return;
  694. }
  695. netif_invoke_ext_callback(netif, LWIP_NSC_NETIF_REMOVED, NULL);
  696. #if LWIP_IPV4
  697. if (!ip4_addr_isany_val(*netif_ip4_addr(netif))) {
  698. netif_do_ip_addr_changed(netif_ip_addr4(netif), NULL);
  699. }
  700. #if LWIP_IGMP
  701. /* stop IGMP processing */
  702. if (netif->flags & NETIF_FLAG_IGMP) {
  703. igmp_stop(netif);
  704. }
  705. #endif /* LWIP_IGMP */
  706. #endif /* LWIP_IPV4*/
  707. #if LWIP_IPV6
  708. for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
  709. if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i))) {
  710. netif_do_ip_addr_changed(netif_ip_addr6(netif, i), NULL);
  711. }
  712. }
  713. #if LWIP_IPV6_MLD
  714. /* stop MLD processing */
  715. mld6_stop(netif);
  716. #endif /* LWIP_IPV6_MLD */
  717. #endif /* LWIP_IPV6 */
  718. if (netif_is_up(netif)) {
  719. /* set netif down before removing (call callback function) */
  720. netif_set_down(netif);
  721. }
  722. mib2_remove_ip4(netif);
  723. /* this netif is default? */
  724. if (netif_default == netif) {
  725. /* reset default netif */
  726. netif_set_default(NULL);
  727. }
  728. #if !LWIP_SINGLE_NETIF
  729. /* is it the first netif? */
  730. if (netif_list == netif) {
  731. netif_list = netif->next;
  732. } else {
  733. /* look for netif further down the list */
  734. struct netif *tmp_netif;
  735. NETIF_FOREACH(tmp_netif) {
  736. if (tmp_netif->next == netif) {
  737. tmp_netif->next = netif->next;
  738. break;
  739. }
  740. }
  741. if (tmp_netif == NULL) {
  742. return; /* netif is not on the list */
  743. }
  744. }
  745. #endif /* !LWIP_SINGLE_NETIF */
  746. mib2_netif_removed(netif);
  747. #if LWIP_NETIF_REMOVE_CALLBACK
  748. if (netif->remove_callback) {
  749. netif->remove_callback(netif);
  750. }
  751. #endif /* LWIP_NETIF_REMOVE_CALLBACK */
  752. LWIP_DEBUGF( NETIF_DEBUG, ("netif_remove: removed netif\n") );
  753. }
  754. /**
  755. * @ingroup netif
  756. * Set a network interface as the default network interface
  757. * (used to output all packets for which no specific route is found)
  758. *
  759. * @param netif the default network interface
  760. */
  761. void
  762. netif_set_default(struct netif *netif)
  763. {
  764. LWIP_ASSERT_CORE_LOCKED();
  765. if (netif == NULL) {
  766. /* remove default route */
  767. mib2_remove_route_ip4(1, netif);
  768. } else {
  769. /* install default route */
  770. mib2_add_route_ip4(1, netif);
  771. }
  772. netif_default = netif;
  773. LWIP_DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
  774. netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
  775. }
  776. /**
  777. * @ingroup netif
  778. * Bring an interface up, available for processing
  779. * traffic.
  780. */
  781. void
  782. netif_set_up(struct netif *netif)
  783. {
  784. LWIP_ASSERT_CORE_LOCKED();
  785. LWIP_ERROR("netif_set_up: invalid netif", netif != NULL, return);
  786. if (!(netif->flags & NETIF_FLAG_UP)) {
  787. netif_set_flags(netif, NETIF_FLAG_UP);
  788. MIB2_COPY_SYSUPTIME_TO(&netif->ts);
  789. NETIF_STATUS_CALLBACK(netif);
  790. #if LWIP_NETIF_EXT_STATUS_CALLBACK
  791. {
  792. netif_ext_callback_args_t args;
  793. args.status_changed.state = 1;
  794. netif_invoke_ext_callback(netif, LWIP_NSC_STATUS_CHANGED, &args);
  795. }
  796. #endif
  797. netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV4 | NETIF_REPORT_TYPE_IPV6);
  798. #if LWIP_IPV6
  799. nd6_restart_netif(netif);
  800. #endif /* LWIP_IPV6 */
  801. }
  802. }
  803. /** Send ARP/IGMP/MLD/RS events, e.g. on link-up/netif-up or addr-change
  804. */
  805. static void
  806. netif_issue_reports(struct netif *netif, u8_t report_type)
  807. {
  808. LWIP_ASSERT("netif_issue_reports: invalid netif", netif != NULL);
  809. /* Only send reports when both link and admin states are up */
  810. if (!(netif->flags & NETIF_FLAG_LINK_UP) ||
  811. !(netif->flags & NETIF_FLAG_UP)) {
  812. return;
  813. }
  814. #if LWIP_IPV4
  815. if ((report_type & NETIF_REPORT_TYPE_IPV4) &&
  816. !ip4_addr_isany_val(*netif_ip4_addr(netif))) {
  817. #if LWIP_ARP && !LWIP_ACD
  818. /* For Ethernet network interfaces:
  819. * we would like to send a "gratuitous ARP".
  820. * Only needs to be done here if ACD isn't configured.
  821. */
  822. if (netif->flags & (NETIF_FLAG_ETHARP)) {
  823. etharp_gratuitous(netif);
  824. }
  825. #endif /* LWIP_ARP */
  826. #if LWIP_IGMP
  827. /* resend IGMP memberships */
  828. if (netif->flags & NETIF_FLAG_IGMP) {
  829. igmp_report_groups(netif);
  830. }
  831. #endif /* LWIP_IGMP */
  832. }
  833. #endif /* LWIP_IPV4 */
  834. #if LWIP_IPV6
  835. if (report_type & NETIF_REPORT_TYPE_IPV6) {
  836. #if LWIP_IPV6_MLD
  837. /* send mld memberships */
  838. mld6_report_groups(netif);
  839. #endif /* LWIP_IPV6_MLD */
  840. }
  841. #endif /* LWIP_IPV6 */
  842. }
  843. /**
  844. * @ingroup netif
  845. * Bring an interface down, disabling any traffic processing.
  846. */
  847. void
  848. netif_set_down(struct netif *netif)
  849. {
  850. LWIP_ASSERT_CORE_LOCKED();
  851. LWIP_ERROR("netif_set_down: invalid netif", netif != NULL, return);
  852. if (netif->flags & NETIF_FLAG_UP) {
  853. #if LWIP_NETIF_EXT_STATUS_CALLBACK
  854. {
  855. netif_ext_callback_args_t args;
  856. args.status_changed.state = 0;
  857. netif_invoke_ext_callback(netif, LWIP_NSC_STATUS_CHANGED, &args);
  858. }
  859. #endif
  860. netif_clear_flags(netif, NETIF_FLAG_UP);
  861. MIB2_COPY_SYSUPTIME_TO(&netif->ts);
  862. #if LWIP_IPV4 && LWIP_ARP
  863. if (netif->flags & NETIF_FLAG_ETHARP) {
  864. etharp_cleanup_netif(netif);
  865. }
  866. #endif /* LWIP_IPV4 && LWIP_ARP */
  867. #if LWIP_IPV6
  868. nd6_cleanup_netif(netif);
  869. #endif /* LWIP_IPV6 */
  870. NETIF_STATUS_CALLBACK(netif);
  871. }
  872. }
  873. #if LWIP_NETIF_STATUS_CALLBACK
  874. /**
  875. * @ingroup netif
  876. * Set callback to be called when interface is brought up/down or address is changed while up
  877. */
  878. void
  879. netif_set_status_callback(struct netif *netif, netif_status_callback_fn status_callback)
  880. {
  881. LWIP_ASSERT_CORE_LOCKED();
  882. if (netif) {
  883. netif->status_callback = status_callback;
  884. }
  885. }
  886. #endif /* LWIP_NETIF_STATUS_CALLBACK */
  887. #if LWIP_NETIF_REMOVE_CALLBACK
  888. /**
  889. * @ingroup netif
  890. * Set callback to be called when the interface has been removed
  891. */
  892. void
  893. netif_set_remove_callback(struct netif *netif, netif_status_callback_fn remove_callback)
  894. {
  895. LWIP_ASSERT_CORE_LOCKED();
  896. if (netif) {
  897. netif->remove_callback = remove_callback;
  898. }
  899. }
  900. #endif /* LWIP_NETIF_REMOVE_CALLBACK */
  901. /**
  902. * @ingroup netif
  903. * Called by a driver when its link goes up
  904. */
  905. void
  906. netif_set_link_up(struct netif *netif)
  907. {
  908. LWIP_ASSERT_CORE_LOCKED();
  909. LWIP_ERROR("netif_set_link_up: invalid netif", netif != NULL, return);
  910. if (!(netif->flags & NETIF_FLAG_LINK_UP)) {
  911. netif_set_flags(netif, NETIF_FLAG_LINK_UP);
  912. #if LWIP_DHCP
  913. dhcp_network_changed_link_up(netif);
  914. #endif /* LWIP_DHCP */
  915. #if LWIP_AUTOIP
  916. autoip_network_changed_link_up(netif);
  917. #endif /* LWIP_AUTOIP */
  918. netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV4 | NETIF_REPORT_TYPE_IPV6);
  919. #if LWIP_IPV6
  920. nd6_restart_netif(netif);
  921. #endif /* LWIP_IPV6 */
  922. NETIF_LINK_CALLBACK(netif);
  923. #if LWIP_NETIF_EXT_STATUS_CALLBACK
  924. {
  925. netif_ext_callback_args_t args;
  926. args.link_changed.state = 1;
  927. netif_invoke_ext_callback(netif, LWIP_NSC_LINK_CHANGED, &args);
  928. }
  929. #endif
  930. }
  931. }
  932. /**
  933. * @ingroup netif
  934. * Called by a driver when its link goes down
  935. */
  936. void
  937. netif_set_link_down(struct netif *netif)
  938. {
  939. LWIP_ASSERT_CORE_LOCKED();
  940. LWIP_ERROR("netif_set_link_down: invalid netif", netif != NULL, return);
  941. if (netif->flags & NETIF_FLAG_LINK_UP) {
  942. netif_clear_flags(netif, NETIF_FLAG_LINK_UP);
  943. #if LWIP_AUTOIP
  944. autoip_network_changed_link_down(netif);
  945. #endif /* LWIP_AUTOIP */
  946. #if LWIP_ACD
  947. acd_network_changed_link_down(netif);
  948. #endif /* LWIP_ACD */
  949. #if LWIP_IPV6 && LWIP_ND6_ALLOW_RA_UPDATES
  950. netif->mtu6 = netif->mtu;
  951. #endif
  952. NETIF_LINK_CALLBACK(netif);
  953. #if LWIP_NETIF_EXT_STATUS_CALLBACK
  954. {
  955. netif_ext_callback_args_t args;
  956. args.link_changed.state = 0;
  957. netif_invoke_ext_callback(netif, LWIP_NSC_LINK_CHANGED, &args);
  958. }
  959. #endif
  960. }
  961. }
  962. #if LWIP_NETIF_LINK_CALLBACK
  963. /**
  964. * @ingroup netif
  965. * Set callback to be called when link is brought up/down
  966. */
  967. void
  968. netif_set_link_callback(struct netif *netif, netif_status_callback_fn link_callback)
  969. {
  970. LWIP_ASSERT_CORE_LOCKED();
  971. if (netif) {
  972. netif->link_callback = link_callback;
  973. }
  974. }
  975. #endif /* LWIP_NETIF_LINK_CALLBACK */
  976. #if ENABLE_LOOPBACK
  977. /**
  978. * @ingroup netif
  979. * Send an IP packet to be received on the same netif (loopif-like).
  980. * The pbuf is copied and added to an internal queue which is fed to
  981. * netif->input by netif_poll().
  982. * In multithreaded mode, the call to netif_poll() is queued to be done on the
  983. * TCP/IP thread.
  984. * In callback mode, the user has the responsibility to call netif_poll() in
  985. * the main loop of their application.
  986. *
  987. * @param netif the lwip network interface structure
  988. * @param p the (IP) packet to 'send'
  989. * @return ERR_OK if the packet has been sent
  990. * ERR_MEM if the pbuf used to copy the packet couldn't be allocated
  991. */
  992. err_t
  993. netif_loop_output(struct netif *netif, struct pbuf *p)
  994. {
  995. struct pbuf *r;
  996. err_t err;
  997. struct pbuf *last;
  998. #if LWIP_LOOPBACK_MAX_PBUFS
  999. u16_t clen = 0;
  1000. #endif /* LWIP_LOOPBACK_MAX_PBUFS */
  1001. /* If we have a loopif, SNMP counters are adjusted for it,
  1002. * if not they are adjusted for 'netif'. */
  1003. #if MIB2_STATS
  1004. #if LWIP_HAVE_LOOPIF
  1005. struct netif *stats_if = &loop_netif;
  1006. #else /* LWIP_HAVE_LOOPIF */
  1007. struct netif *stats_if = netif;
  1008. #endif /* LWIP_HAVE_LOOPIF */
  1009. #endif /* MIB2_STATS */
  1010. #if LWIP_NETIF_LOOPBACK_MULTITHREADING
  1011. u8_t schedule_poll = 0;
  1012. #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
  1013. SYS_ARCH_DECL_PROTECT(lev);
  1014. LWIP_ASSERT("netif_loop_output: invalid netif", netif != NULL);
  1015. LWIP_ASSERT("netif_loop_output: invalid pbuf", p != NULL);
  1016. /* Allocate a new pbuf */
  1017. r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
  1018. if (r == NULL) {
  1019. LINK_STATS_INC(link.memerr);
  1020. LINK_STATS_INC(link.drop);
  1021. MIB2_STATS_NETIF_INC(stats_if, ifoutdiscards);
  1022. return ERR_MEM;
  1023. }
  1024. #if LWIP_LOOPBACK_MAX_PBUFS
  1025. clen = pbuf_clen(r);
  1026. /* check for overflow or too many pbuf on queue */
  1027. if (((netif->loop_cnt_current + clen) < netif->loop_cnt_current) ||
  1028. ((netif->loop_cnt_current + clen) > LWIP_MIN(LWIP_LOOPBACK_MAX_PBUFS, 0xFFFF))) {
  1029. pbuf_free(r);
  1030. LINK_STATS_INC(link.memerr);
  1031. LINK_STATS_INC(link.drop);
  1032. MIB2_STATS_NETIF_INC(stats_if, ifoutdiscards);
  1033. return ERR_MEM;
  1034. }
  1035. netif->loop_cnt_current = (u16_t)(netif->loop_cnt_current + clen);
  1036. #endif /* LWIP_LOOPBACK_MAX_PBUFS */
  1037. /* Copy the whole pbuf queue p into the single pbuf r */
  1038. if ((err = pbuf_copy(r, p)) != ERR_OK) {
  1039. pbuf_free(r);
  1040. LINK_STATS_INC(link.memerr);
  1041. LINK_STATS_INC(link.drop);
  1042. MIB2_STATS_NETIF_INC(stats_if, ifoutdiscards);
  1043. return err;
  1044. }
  1045. /* Put the packet on a linked list which gets emptied through calling
  1046. netif_poll(). */
  1047. /* let last point to the last pbuf in chain r */
  1048. for (last = r; last->next != NULL; last = last->next) {
  1049. /* nothing to do here, just get to the last pbuf */
  1050. }
  1051. SYS_ARCH_PROTECT(lev);
  1052. if (netif->loop_first != NULL) {
  1053. LWIP_ASSERT("if first != NULL, last must also be != NULL", netif->loop_last != NULL);
  1054. netif->loop_last->next = r;
  1055. netif->loop_last = last;
  1056. #if LWIP_NETIF_LOOPBACK_MULTITHREADING
  1057. if (netif->reschedule_poll) {
  1058. schedule_poll = 1;
  1059. netif->reschedule_poll = 0;
  1060. }
  1061. #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
  1062. } else {
  1063. netif->loop_first = r;
  1064. netif->loop_last = last;
  1065. #if LWIP_NETIF_LOOPBACK_MULTITHREADING
  1066. /* No existing packets queued, schedule poll */
  1067. schedule_poll = 1;
  1068. #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
  1069. }
  1070. SYS_ARCH_UNPROTECT(lev);
  1071. LINK_STATS_INC(link.xmit);
  1072. MIB2_STATS_NETIF_ADD(stats_if, ifoutoctets, p->tot_len);
  1073. MIB2_STATS_NETIF_INC(stats_if, ifoutucastpkts);
  1074. #if LWIP_NETIF_LOOPBACK_MULTITHREADING
  1075. /* For multithreading environment, schedule a call to netif_poll */
  1076. if (schedule_poll) {
  1077. if (tcpip_try_callback((tcpip_callback_fn)netif_poll, netif) != ERR_OK) {
  1078. SYS_ARCH_PROTECT(lev);
  1079. netif->reschedule_poll = 1;
  1080. SYS_ARCH_UNPROTECT(lev);
  1081. }
  1082. }
  1083. #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
  1084. return ERR_OK;
  1085. }
  1086. #if LWIP_HAVE_LOOPIF
  1087. #if LWIP_IPV4
  1088. static err_t
  1089. netif_loop_output_ipv4(struct netif *netif, struct pbuf *p, const ip4_addr_t *addr)
  1090. {
  1091. LWIP_UNUSED_ARG(addr);
  1092. return netif_loop_output(netif, p);
  1093. }
  1094. #endif /* LWIP_IPV4 */
  1095. #if LWIP_IPV6
  1096. static err_t
  1097. netif_loop_output_ipv6(struct netif *netif, struct pbuf *p, const ip6_addr_t *addr)
  1098. {
  1099. LWIP_UNUSED_ARG(addr);
  1100. return netif_loop_output(netif, p);
  1101. }
  1102. #endif /* LWIP_IPV6 */
  1103. #endif /* LWIP_HAVE_LOOPIF */
  1104. /**
  1105. * Call netif_poll() in the main loop of your application. This is to prevent
  1106. * reentering non-reentrant functions like tcp_input(). Packets passed to
  1107. * netif_loop_output() are put on a list that is passed to netif->input() by
  1108. * netif_poll().
  1109. */
  1110. void
  1111. netif_poll(struct netif *netif)
  1112. {
  1113. /* If we have a loopif, SNMP counters are adjusted for it,
  1114. * if not they are adjusted for 'netif'. */
  1115. #if MIB2_STATS
  1116. #if LWIP_HAVE_LOOPIF
  1117. struct netif *stats_if = &loop_netif;
  1118. #else /* LWIP_HAVE_LOOPIF */
  1119. struct netif *stats_if = netif;
  1120. #endif /* LWIP_HAVE_LOOPIF */
  1121. #endif /* MIB2_STATS */
  1122. SYS_ARCH_DECL_PROTECT(lev);
  1123. LWIP_ASSERT("netif_poll: invalid netif", netif != NULL);
  1124. /* Get a packet from the list. With SYS_LIGHTWEIGHT_PROT=1, this is protected */
  1125. SYS_ARCH_PROTECT(lev);
  1126. while (netif->loop_first != NULL) {
  1127. struct pbuf *in, *in_end;
  1128. #if LWIP_LOOPBACK_MAX_PBUFS
  1129. u8_t clen = 1;
  1130. #endif /* LWIP_LOOPBACK_MAX_PBUFS */
  1131. in = in_end = netif->loop_first;
  1132. while (in_end->len != in_end->tot_len) {
  1133. LWIP_ASSERT("bogus pbuf: len != tot_len but next == NULL!", in_end->next != NULL);
  1134. in_end = in_end->next;
  1135. #if LWIP_LOOPBACK_MAX_PBUFS
  1136. clen++;
  1137. #endif /* LWIP_LOOPBACK_MAX_PBUFS */
  1138. }
  1139. #if LWIP_LOOPBACK_MAX_PBUFS
  1140. /* adjust the number of pbufs on queue */
  1141. LWIP_ASSERT("netif->loop_cnt_current underflow",
  1142. ((netif->loop_cnt_current - clen) < netif->loop_cnt_current));
  1143. netif->loop_cnt_current = (u16_t)(netif->loop_cnt_current - clen);
  1144. #endif /* LWIP_LOOPBACK_MAX_PBUFS */
  1145. /* 'in_end' now points to the last pbuf from 'in' */
  1146. if (in_end == netif->loop_last) {
  1147. /* this was the last pbuf in the list */
  1148. netif->loop_first = netif->loop_last = NULL;
  1149. } else {
  1150. /* pop the pbuf off the list */
  1151. netif->loop_first = in_end->next;
  1152. LWIP_ASSERT("should not be null since first != last!", netif->loop_first != NULL);
  1153. }
  1154. /* De-queue the pbuf from its successors on the 'loop_' list. */
  1155. in_end->next = NULL;
  1156. SYS_ARCH_UNPROTECT(lev);
  1157. in->if_idx = netif_get_index(netif);
  1158. LINK_STATS_INC(link.recv);
  1159. MIB2_STATS_NETIF_ADD(stats_if, ifinoctets, in->tot_len);
  1160. MIB2_STATS_NETIF_INC(stats_if, ifinucastpkts);
  1161. /* loopback packets are always IP packets! */
  1162. if (ip_input(in, netif) != ERR_OK) {
  1163. pbuf_free(in);
  1164. }
  1165. SYS_ARCH_PROTECT(lev);
  1166. }
  1167. SYS_ARCH_UNPROTECT(lev);
  1168. }
  1169. #if !LWIP_NETIF_LOOPBACK_MULTITHREADING
  1170. /**
  1171. * Calls netif_poll() for every netif on the netif_list.
  1172. */
  1173. void
  1174. netif_poll_all(void)
  1175. {
  1176. struct netif *netif;
  1177. /* loop through netifs */
  1178. NETIF_FOREACH(netif) {
  1179. netif_poll(netif);
  1180. }
  1181. }
  1182. #endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */
  1183. #endif /* ENABLE_LOOPBACK */
  1184. #if LWIP_NUM_NETIF_CLIENT_DATA > 0
  1185. /**
  1186. * @ingroup netif_cd
  1187. * Allocate an index to store data in client_data member of struct netif.
  1188. * Returned value is an index in mentioned array.
  1189. * @see LWIP_NUM_NETIF_CLIENT_DATA
  1190. */
  1191. u8_t
  1192. netif_alloc_client_data_id(void)
  1193. {
  1194. u8_t result = netif_client_id;
  1195. netif_client_id++;
  1196. LWIP_ASSERT_CORE_LOCKED();
  1197. #if LWIP_NUM_NETIF_CLIENT_DATA > 256
  1198. #error LWIP_NUM_NETIF_CLIENT_DATA must be <= 256
  1199. #endif
  1200. LWIP_ASSERT("Increase LWIP_NUM_NETIF_CLIENT_DATA in lwipopts.h", result < LWIP_NUM_NETIF_CLIENT_DATA);
  1201. return (u8_t)(result + LWIP_NETIF_CLIENT_DATA_INDEX_MAX);
  1202. }
  1203. #endif
  1204. #if LWIP_IPV6
  1205. /**
  1206. * @ingroup netif_ip6
  1207. * Change an IPv6 address of a network interface
  1208. *
  1209. * @param netif the network interface to change
  1210. * @param addr_idx index of the IPv6 address
  1211. * @param addr6 the new IPv6 address
  1212. *
  1213. * @note call netif_ip6_addr_set_state() to set the address valid/temptative
  1214. */
  1215. void
  1216. netif_ip6_addr_set(struct netif *netif, s8_t addr_idx, const ip6_addr_t *addr6)
  1217. {
  1218. LWIP_ASSERT_CORE_LOCKED();
  1219. LWIP_ASSERT("netif_ip6_addr_set: invalid netif", netif != NULL);
  1220. LWIP_ASSERT("netif_ip6_addr_set: invalid addr6", addr6 != NULL);
  1221. netif_ip6_addr_set_parts(netif, addr_idx, addr6->addr[0], addr6->addr[1],
  1222. addr6->addr[2], addr6->addr[3]);
  1223. }
  1224. /*
  1225. * Change an IPv6 address of a network interface (internal version taking 4 * u32_t)
  1226. *
  1227. * @param netif the network interface to change
  1228. * @param addr_idx index of the IPv6 address
  1229. * @param i0 word0 of the new IPv6 address
  1230. * @param i1 word1 of the new IPv6 address
  1231. * @param i2 word2 of the new IPv6 address
  1232. * @param i3 word3 of the new IPv6 address
  1233. */
  1234. void
  1235. netif_ip6_addr_set_parts(struct netif *netif, s8_t addr_idx, u32_t i0, u32_t i1, u32_t i2, u32_t i3)
  1236. {
  1237. ip_addr_t old_addr;
  1238. ip_addr_t new_ipaddr;
  1239. LWIP_ASSERT_CORE_LOCKED();
  1240. LWIP_ASSERT("netif != NULL", netif != NULL);
  1241. LWIP_ASSERT("invalid index", addr_idx < LWIP_IPV6_NUM_ADDRESSES);
  1242. ip6_addr_copy(*ip_2_ip6(&old_addr), *netif_ip6_addr(netif, addr_idx));
  1243. IP_SET_TYPE_VAL(old_addr, IPADDR_TYPE_V6);
  1244. /* address is actually being changed? */
  1245. if ((ip_2_ip6(&old_addr)->addr[0] != i0) || (ip_2_ip6(&old_addr)->addr[1] != i1) ||
  1246. (ip_2_ip6(&old_addr)->addr[2] != i2) || (ip_2_ip6(&old_addr)->addr[3] != i3)) {
  1247. LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_ip6_addr_set: netif address being changed\n"));
  1248. IP_ADDR6(&new_ipaddr, i0, i1, i2, i3);
  1249. ip6_addr_assign_zone(ip_2_ip6(&new_ipaddr), IP6_UNICAST, netif);
  1250. if (ip6_addr_isvalid(netif_ip6_addr_state(netif, addr_idx))) {
  1251. netif_do_ip_addr_changed(netif_ip_addr6(netif, addr_idx), &new_ipaddr);
  1252. }
  1253. /* @todo: remove/re-add mib2 ip6 entries? */
  1254. ip_addr_copy(netif->ip6_addr[addr_idx], new_ipaddr);
  1255. if (ip6_addr_isvalid(netif_ip6_addr_state(netif, addr_idx))) {
  1256. netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV6);
  1257. NETIF_STATUS_CALLBACK(netif);
  1258. }
  1259. #if LWIP_NETIF_EXT_STATUS_CALLBACK
  1260. {
  1261. netif_ext_callback_args_t args;
  1262. args.ipv6_set.addr_index = addr_idx;
  1263. args.ipv6_set.old_address = &old_addr;
  1264. netif_invoke_ext_callback(netif, LWIP_NSC_IPV6_SET, &args);
  1265. }
  1266. #endif
  1267. }
  1268. LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: IPv6 address %d of interface %c%c set to %s/0x%"X8_F"\n",
  1269. addr_idx, netif->name[0], netif->name[1], ip6addr_ntoa(netif_ip6_addr(netif, addr_idx)),
  1270. netif_ip6_addr_state(netif, addr_idx)));
  1271. }
  1272. /**
  1273. * @ingroup netif_ip6
  1274. * Change the state of an IPv6 address of a network interface
  1275. * (INVALID, TEMPTATIVE, PREFERRED, DEPRECATED, where TEMPTATIVE
  1276. * includes the number of checks done, see ip6_addr.h)
  1277. *
  1278. * @param netif the network interface to change
  1279. * @param addr_idx index of the IPv6 address
  1280. * @param state the new IPv6 address state
  1281. */
  1282. void
  1283. netif_ip6_addr_set_state(struct netif *netif, s8_t addr_idx, u8_t state)
  1284. {
  1285. u8_t old_state;
  1286. LWIP_ASSERT_CORE_LOCKED();
  1287. LWIP_ASSERT("netif != NULL", netif != NULL);
  1288. LWIP_ASSERT("invalid index", addr_idx < LWIP_IPV6_NUM_ADDRESSES);
  1289. old_state = netif_ip6_addr_state(netif, addr_idx);
  1290. /* state is actually being changed? */
  1291. if (old_state != state) {
  1292. u8_t old_valid = old_state & IP6_ADDR_VALID;
  1293. u8_t new_valid = state & IP6_ADDR_VALID;
  1294. LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_ip6_addr_set_state: netif address state being changed\n"));
  1295. #if LWIP_IPV6_MLD
  1296. /* Reevaluate solicited-node multicast group membership. */
  1297. if (netif->flags & NETIF_FLAG_MLD6) {
  1298. nd6_adjust_mld_membership(netif, addr_idx, state);
  1299. }
  1300. #endif /* LWIP_IPV6_MLD */
  1301. if (old_valid && !new_valid) {
  1302. /* address about to be removed by setting invalid */
  1303. netif_do_ip_addr_changed(netif_ip_addr6(netif, addr_idx), NULL);
  1304. /* @todo: remove mib2 ip6 entries? */
  1305. }
  1306. netif->ip6_addr_state[addr_idx] = state;
  1307. if (!old_valid && new_valid) {
  1308. /* address added by setting valid */
  1309. /* This is a good moment to check that the address is properly zoned. */
  1310. IP6_ADDR_ZONECHECK_NETIF(netif_ip6_addr(netif, addr_idx), netif);
  1311. /* @todo: add mib2 ip6 entries? */
  1312. netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV6);
  1313. }
  1314. if ((old_state & ~IP6_ADDR_TENTATIVE_COUNT_MASK) !=
  1315. (state & ~IP6_ADDR_TENTATIVE_COUNT_MASK)) {
  1316. /* address state has changed -> call the callback function */
  1317. NETIF_STATUS_CALLBACK(netif);
  1318. }
  1319. #if LWIP_NETIF_EXT_STATUS_CALLBACK
  1320. {
  1321. netif_ext_callback_args_t args;
  1322. args.ipv6_addr_state_changed.addr_index = addr_idx;
  1323. args.ipv6_addr_state_changed.old_state = old_state;
  1324. args.ipv6_addr_state_changed.address = netif_ip_addr6(netif, addr_idx);
  1325. netif_invoke_ext_callback(netif, LWIP_NSC_IPV6_ADDR_STATE_CHANGED, &args);
  1326. }
  1327. #endif
  1328. }
  1329. LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: IPv6 address %d of interface %c%c set to %s/0x%"X8_F"\n",
  1330. addr_idx, netif->name[0], netif->name[1], ip6addr_ntoa(netif_ip6_addr(netif, addr_idx)),
  1331. netif_ip6_addr_state(netif, addr_idx)));
  1332. }
  1333. /**
  1334. * Checks if a specific local address is present on the netif and returns its
  1335. * index. Depending on its state, it may or may not be assigned to the
  1336. * interface (as per RFC terminology).
  1337. *
  1338. * The given address may or may not be zoned (i.e., have a zone index other
  1339. * than IP6_NO_ZONE). If the address is zoned, it must have the correct zone
  1340. * for the given netif, or no match will be found.
  1341. *
  1342. * @param netif the netif to check
  1343. * @param ip6addr the IPv6 address to find
  1344. * @return >= 0: address found, this is its index
  1345. * -1: address not found on this netif
  1346. */
  1347. s8_t
  1348. netif_get_ip6_addr_match(struct netif *netif, const ip6_addr_t *ip6addr)
  1349. {
  1350. s8_t i;
  1351. LWIP_ASSERT_CORE_LOCKED();
  1352. LWIP_ASSERT("netif_get_ip6_addr_match: invalid netif", netif != NULL);
  1353. LWIP_ASSERT("netif_get_ip6_addr_match: invalid ip6addr", ip6addr != NULL);
  1354. #if LWIP_IPV6_SCOPES
  1355. if (ip6_addr_has_zone(ip6addr) && !ip6_addr_test_zone(ip6addr, netif)) {
  1356. return -1; /* wrong zone, no match */
  1357. }
  1358. #endif /* LWIP_IPV6_SCOPES */
  1359. for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
  1360. if (!ip6_addr_isinvalid(netif_ip6_addr_state(netif, i)) &&
  1361. ip6_addr_zoneless_eq(netif_ip6_addr(netif, i), ip6addr)) {
  1362. return i;
  1363. }
  1364. }
  1365. return -1;
  1366. }
  1367. /**
  1368. * @ingroup netif_ip6
  1369. * Create a link-local IPv6 address on a netif (stored in slot 0)
  1370. *
  1371. * @param netif the netif to create the address on
  1372. * @param from_mac_48bit if != 0, assume hwadr is a 48-bit MAC address (std conversion)
  1373. * if == 0, use hwaddr directly as interface ID
  1374. */
  1375. void
  1376. netif_create_ip6_linklocal_address(struct netif *netif, u8_t from_mac_48bit)
  1377. {
  1378. u8_t i, addr_index;
  1379. LWIP_ASSERT_CORE_LOCKED();
  1380. LWIP_ASSERT("netif_create_ip6_linklocal_address: invalid netif", netif != NULL);
  1381. /* Link-local prefix. */
  1382. ip_2_ip6(&netif->ip6_addr[0])->addr[0] = PP_HTONL(0xfe800000ul);
  1383. ip_2_ip6(&netif->ip6_addr[0])->addr[1] = 0;
  1384. /* Generate interface ID. */
  1385. if (from_mac_48bit) {
  1386. /* Assume hwaddr is a 48-bit IEEE 802 MAC. Convert to EUI-64 address. Complement Group bit. */
  1387. ip_2_ip6(&netif->ip6_addr[0])->addr[2] = lwip_htonl((((u32_t)(netif->hwaddr[0] ^ 0x02)) << 24) |
  1388. ((u32_t)(netif->hwaddr[1]) << 16) |
  1389. ((u32_t)(netif->hwaddr[2]) << 8) |
  1390. (0xff));
  1391. ip_2_ip6(&netif->ip6_addr[0])->addr[3] = lwip_htonl((u32_t)(0xfeul << 24) |
  1392. ((u32_t)(netif->hwaddr[3]) << 16) |
  1393. ((u32_t)(netif->hwaddr[4]) << 8) |
  1394. (netif->hwaddr[5]));
  1395. } else {
  1396. /* Use hwaddr directly as interface ID. */
  1397. ip_2_ip6(&netif->ip6_addr[0])->addr[2] = 0;
  1398. ip_2_ip6(&netif->ip6_addr[0])->addr[3] = 0;
  1399. addr_index = 3;
  1400. for (i = 0; (i < 8) && (i < netif->hwaddr_len); i++) {
  1401. if (i == 4) {
  1402. addr_index--;
  1403. }
  1404. ip_2_ip6(&netif->ip6_addr[0])->addr[addr_index] |= lwip_htonl(((u32_t)(netif->hwaddr[netif->hwaddr_len - i - 1])) << (8 * (i & 0x03)));
  1405. }
  1406. }
  1407. /* Set a link-local zone. Even though the zone is implied by the owning
  1408. * netif, setting the zone anyway has two important conceptual advantages:
  1409. * 1) it avoids the need for a ton of exceptions in internal code, allowing
  1410. * e.g. ip6_addr_eq() to be used on local addresses;
  1411. * 2) the properly zoned address is visible externally, e.g. when any outside
  1412. * code enumerates available addresses or uses one to bind a socket.
  1413. * Any external code unaware of address scoping is likely to just ignore the
  1414. * zone field, so this should not create any compatibility problems. */
  1415. ip6_addr_assign_zone(ip_2_ip6(&netif->ip6_addr[0]), IP6_UNICAST, netif);
  1416. /* Set address state. */
  1417. #if LWIP_IPV6_DUP_DETECT_ATTEMPTS
  1418. /* Will perform duplicate address detection (DAD). */
  1419. netif_ip6_addr_set_state(netif, 0, IP6_ADDR_TENTATIVE);
  1420. #else
  1421. /* Consider address valid. */
  1422. netif_ip6_addr_set_state(netif, 0, IP6_ADDR_PREFERRED);
  1423. #endif /* LWIP_IPV6_AUTOCONFIG */
  1424. }
  1425. /**
  1426. * @ingroup netif_ip6
  1427. * This function allows for the easy addition of a new IPv6 address to an interface.
  1428. * It takes care of finding an empty slot and then sets the address tentative
  1429. * (to make sure that all the subsequent processing happens).
  1430. *
  1431. * @param netif netif to add the address on
  1432. * @param ip6addr address to add
  1433. * @param chosen_idx if != NULL, the chosen IPv6 address index will be stored here
  1434. */
  1435. err_t
  1436. netif_add_ip6_address(struct netif *netif, const ip6_addr_t *ip6addr, s8_t *chosen_idx)
  1437. {
  1438. s8_t i;
  1439. LWIP_ASSERT_CORE_LOCKED();
  1440. LWIP_ASSERT("netif_add_ip6_address: invalid netif", netif != NULL);
  1441. LWIP_ASSERT("netif_add_ip6_address: invalid ip6addr", ip6addr != NULL);
  1442. i = netif_get_ip6_addr_match(netif, ip6addr);
  1443. if (i >= 0) {
  1444. /* Address already added */
  1445. if (chosen_idx != NULL) {
  1446. *chosen_idx = i;
  1447. }
  1448. return ERR_OK;
  1449. }
  1450. /* Find a free slot. The first one is reserved for link-local addresses. */
  1451. for (i = ip6_addr_islinklocal(ip6addr) ? 0 : 1; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
  1452. if (ip6_addr_isinvalid(netif_ip6_addr_state(netif, i))) {
  1453. ip_addr_copy_from_ip6(netif->ip6_addr[i], *ip6addr);
  1454. ip6_addr_assign_zone(ip_2_ip6(&netif->ip6_addr[i]), IP6_UNICAST, netif);
  1455. netif_ip6_addr_set_state(netif, i, IP6_ADDR_TENTATIVE);
  1456. if (chosen_idx != NULL) {
  1457. *chosen_idx = i;
  1458. }
  1459. return ERR_OK;
  1460. }
  1461. }
  1462. if (chosen_idx != NULL) {
  1463. *chosen_idx = -1;
  1464. }
  1465. return ERR_VAL;
  1466. }
  1467. /** Dummy IPv6 output function for netifs not supporting IPv6
  1468. */
  1469. static err_t
  1470. netif_null_output_ip6(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr)
  1471. {
  1472. LWIP_UNUSED_ARG(netif);
  1473. LWIP_UNUSED_ARG(p);
  1474. LWIP_UNUSED_ARG(ipaddr);
  1475. return ERR_IF;
  1476. }
  1477. #endif /* LWIP_IPV6 */
  1478. #if LWIP_IPV4
  1479. /** Dummy IPv4 output function for netifs not supporting IPv4
  1480. */
  1481. static err_t
  1482. netif_null_output_ip4(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr)
  1483. {
  1484. LWIP_UNUSED_ARG(netif);
  1485. LWIP_UNUSED_ARG(p);
  1486. LWIP_UNUSED_ARG(ipaddr);
  1487. return ERR_IF;
  1488. }
  1489. #endif /* LWIP_IPV4 */
  1490. /**
  1491. * @ingroup netif
  1492. * Return the interface index for the netif with name
  1493. * or NETIF_NO_INDEX if not found/on error
  1494. *
  1495. * @param name the name of the netif
  1496. */
  1497. u8_t
  1498. netif_name_to_index(const char *name)
  1499. {
  1500. struct netif *netif = netif_find(name);
  1501. if (netif != NULL) {
  1502. return netif_get_index(netif);
  1503. }
  1504. /* No name found, return invalid index */
  1505. return NETIF_NO_INDEX;
  1506. }
  1507. /**
  1508. * @ingroup netif
  1509. * Return the interface name for the netif matching index
  1510. * or NULL if not found/on error
  1511. *
  1512. * @param idx the interface index of the netif
  1513. * @param name char buffer of at least NETIF_NAMESIZE bytes
  1514. */
  1515. char *
  1516. netif_index_to_name(u8_t idx, char *name)
  1517. {
  1518. struct netif *netif = netif_get_by_index(idx);
  1519. if (netif != NULL) {
  1520. name[0] = netif->name[0];
  1521. name[1] = netif->name[1];
  1522. lwip_itoa(&name[2], NETIF_NAMESIZE - 2, netif_index_to_num(idx));
  1523. return name;
  1524. }
  1525. return NULL;
  1526. }
  1527. /**
  1528. * @ingroup netif
  1529. * Return the interface for the netif index
  1530. *
  1531. * @param idx index of netif to find
  1532. */
  1533. struct netif *
  1534. netif_get_by_index(u8_t idx)
  1535. {
  1536. struct netif *netif;
  1537. LWIP_ASSERT_CORE_LOCKED();
  1538. if (idx != NETIF_NO_INDEX) {
  1539. NETIF_FOREACH(netif) {
  1540. if (idx == netif_get_index(netif)) {
  1541. return netif; /* found! */
  1542. }
  1543. }
  1544. }
  1545. return NULL;
  1546. }
  1547. /**
  1548. * @ingroup netif
  1549. * Find a network interface by searching for its name
  1550. *
  1551. * @param name the name of the netif (like netif->name) plus concatenated number
  1552. * in ascii representation (e.g. 'en0')
  1553. */
  1554. struct netif *
  1555. netif_find(const char *name)
  1556. {
  1557. struct netif *netif;
  1558. u8_t num;
  1559. LWIP_ASSERT_CORE_LOCKED();
  1560. if (name == NULL) {
  1561. return NULL;
  1562. }
  1563. num = (u8_t)atoi(&name[2]);
  1564. if (!num && (name[2] != '0')) {
  1565. /* this means atoi has failed */
  1566. return NULL;
  1567. }
  1568. NETIF_FOREACH(netif) {
  1569. if (num == netif->num &&
  1570. name[0] == netif->name[0] &&
  1571. name[1] == netif->name[1]) {
  1572. LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: found %c%c\n", name[0], name[1]));
  1573. return netif;
  1574. }
  1575. }
  1576. LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
  1577. return NULL;
  1578. }
  1579. #if LWIP_NETIF_EXT_STATUS_CALLBACK
  1580. /**
  1581. * @ingroup netif
  1582. * Add extended netif events listener
  1583. * @param callback pointer to listener structure
  1584. * @param fn callback function
  1585. */
  1586. void
  1587. netif_add_ext_callback(netif_ext_callback_t *callback, netif_ext_callback_fn fn)
  1588. {
  1589. LWIP_ASSERT_CORE_LOCKED();
  1590. LWIP_ASSERT("callback must be != NULL", callback != NULL);
  1591. LWIP_ASSERT("fn must be != NULL", fn != NULL);
  1592. callback->callback_fn = fn;
  1593. callback->next = ext_callback;
  1594. ext_callback = callback;
  1595. }
  1596. /**
  1597. * @ingroup netif
  1598. * Remove extended netif events listener
  1599. * @param callback pointer to listener structure
  1600. */
  1601. void
  1602. netif_remove_ext_callback(netif_ext_callback_t* callback)
  1603. {
  1604. netif_ext_callback_t *last, *iter;
  1605. LWIP_ASSERT_CORE_LOCKED();
  1606. LWIP_ASSERT("callback must be != NULL", callback != NULL);
  1607. if (ext_callback == NULL) {
  1608. return;
  1609. }
  1610. if (callback == ext_callback) {
  1611. ext_callback = ext_callback->next;
  1612. } else {
  1613. last = ext_callback;
  1614. for (iter = ext_callback->next; iter != NULL; last = iter, iter = iter->next) {
  1615. if (iter == callback) {
  1616. LWIP_ASSERT("last != NULL", last != NULL);
  1617. last->next = callback->next;
  1618. break;
  1619. }
  1620. }
  1621. }
  1622. callback->next = NULL;
  1623. }
  1624. /**
  1625. * Invoke extended netif status event
  1626. * @param netif netif that is affected by change
  1627. * @param reason change reason
  1628. * @param args depends on reason, see reason description
  1629. */
  1630. void
  1631. netif_invoke_ext_callback(struct netif *netif, netif_nsc_reason_t reason, const netif_ext_callback_args_t *args)
  1632. {
  1633. netif_ext_callback_t *callback = ext_callback;
  1634. LWIP_ASSERT("netif must be != NULL", netif != NULL);
  1635. while (callback != NULL) {
  1636. /* cache next pointer: the callback might unregister itself */
  1637. netif_ext_callback_t *next = callback->next;
  1638. callback->callback_fn(netif, reason, args);
  1639. callback = next;
  1640. }
  1641. }
  1642. #endif /* LWIP_NETIF_EXT_STATUS_CALLBACK */