netif.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  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>
  51. #include "lwip/def.h"
  52. #include "lwip/ip_addr.h"
  53. #include "lwip/ip6_addr.h"
  54. #include "lwip/netif.h"
  55. #include "lwip/priv/tcp_priv.h"
  56. #include "lwip/udp.h"
  57. #include "lwip/raw.h"
  58. #include "lwip/snmp.h"
  59. #include "lwip/igmp.h"
  60. #include "lwip/etharp.h"
  61. #include "lwip/stats.h"
  62. #include "lwip/sys.h"
  63. #include "lwip/ip.h"
  64. #if ENABLE_LOOPBACK
  65. #if LWIP_NETIF_LOOPBACK_MULTITHREADING
  66. #include "lwip/tcpip.h"
  67. #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
  68. #endif /* ENABLE_LOOPBACK */
  69. #include "netif/ethernet.h"
  70. #if LWIP_AUTOIP
  71. #include "lwip/autoip.h"
  72. #endif /* LWIP_AUTOIP */
  73. #if LWIP_DHCP
  74. #include "lwip/dhcp.h"
  75. #endif /* LWIP_DHCP */
  76. #if LWIP_IPV6_DHCP6
  77. #include "lwip/dhcp6.h"
  78. #endif /* LWIP_IPV6_DHCP6 */
  79. #if LWIP_IPV6_MLD
  80. #include "lwip/mld6.h"
  81. #endif /* LWIP_IPV6_MLD */
  82. #if LWIP_IPV6
  83. #include "lwip/nd6.h"
  84. #endif
  85. #include <rtthread.h>
  86. #ifdef RT_USING_NETDEV
  87. #include "lwip/netdb.h"
  88. #include <netdev.h>
  89. #endif /* RT_USING_NETDEV */
  90. #if LWIP_NETIF_STATUS_CALLBACK
  91. #define NETIF_STATUS_CALLBACK(n) do{ if (n->status_callback) { (n->status_callback)(n); }}while(0)
  92. #else
  93. #define NETIF_STATUS_CALLBACK(n)
  94. #endif /* LWIP_NETIF_STATUS_CALLBACK */
  95. #if LWIP_NETIF_LINK_CALLBACK
  96. #define NETIF_LINK_CALLBACK(n) do{ if (n->link_callback) { (n->link_callback)(n); }}while(0)
  97. #else
  98. #define NETIF_LINK_CALLBACK(n)
  99. #endif /* LWIP_NETIF_LINK_CALLBACK */
  100. struct netif *netif_list;
  101. struct netif *netif_default;
  102. static u8_t netif_num;
  103. #if LWIP_NUM_NETIF_CLIENT_DATA > 0
  104. static u8_t netif_client_id;
  105. #endif
  106. #define NETIF_REPORT_TYPE_IPV4 0x01
  107. #define NETIF_REPORT_TYPE_IPV6 0x02
  108. static void netif_issue_reports(struct netif* netif, u8_t report_type);
  109. #if LWIP_IPV6
  110. static err_t netif_null_output_ip6(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr);
  111. #endif /* LWIP_IPV6 */
  112. #if LWIP_HAVE_LOOPIF
  113. #if LWIP_IPV4
  114. static err_t netif_loop_output_ipv4(struct netif *netif, struct pbuf *p, const ip4_addr_t* addr);
  115. #endif
  116. #if LWIP_IPV6
  117. static err_t netif_loop_output_ipv6(struct netif *netif, struct pbuf *p, const ip6_addr_t* addr);
  118. #endif
  119. static struct netif loop_netif;
  120. /**
  121. * Initialize a lwip network interface structure for a loopback interface
  122. *
  123. * @param netif the lwip network interface structure for this loopif
  124. * @return ERR_OK if the loopif is initialized
  125. * ERR_MEM if private data couldn't be allocated
  126. */
  127. static err_t
  128. netif_loopif_init(struct netif *netif)
  129. {
  130. /* initialize the snmp variables and counters inside the struct netif
  131. * ifSpeed: no assumption can be made!
  132. */
  133. MIB2_INIT_NETIF(netif, snmp_ifType_softwareLoopback, 0);
  134. netif->name[0] = 'l';
  135. netif->name[1] = 'o';
  136. #if LWIP_IPV4
  137. netif->output = netif_loop_output_ipv4;
  138. #endif
  139. #if LWIP_IPV6
  140. netif->output_ip6 = netif_loop_output_ipv6;
  141. #endif
  142. #if LWIP_LOOPIF_MULTICAST
  143. netif->flags |= NETIF_FLAG_IGMP;
  144. #endif
  145. return ERR_OK;
  146. }
  147. #endif /* LWIP_HAVE_LOOPIF */
  148. void
  149. netif_init(void)
  150. {
  151. #if LWIP_HAVE_LOOPIF
  152. #if LWIP_IPV4
  153. #define LOOPIF_ADDRINIT &loop_ipaddr, &loop_netmask, &loop_gw,
  154. ip4_addr_t loop_ipaddr, loop_netmask, loop_gw;
  155. IP4_ADDR(&loop_gw, 127,0,0,1);
  156. IP4_ADDR(&loop_ipaddr, 127,0,0,1);
  157. IP4_ADDR(&loop_netmask, 255,0,0,0);
  158. #else /* LWIP_IPV4 */
  159. #define LOOPIF_ADDRINIT
  160. #endif /* LWIP_IPV4 */
  161. #if NO_SYS
  162. netif_add(&loop_netif, LOOPIF_ADDRINIT NULL, netif_loopif_init, ip_input);
  163. #else /* NO_SYS */
  164. netif_add(&loop_netif, LOOPIF_ADDRINIT NULL, netif_loopif_init, tcpip_input);
  165. #endif /* NO_SYS */
  166. #if LWIP_IPV6
  167. IP_ADDR6_HOST(loop_netif.ip6_addr, 0, 0, 0, 0x00000001UL);
  168. loop_netif.ip6_addr_state[0] = IP6_ADDR_VALID;
  169. #endif /* LWIP_IPV6 */
  170. netif_set_link_up(&loop_netif);
  171. netif_set_up(&loop_netif);
  172. #endif /* LWIP_HAVE_LOOPIF */
  173. }
  174. /**
  175. * @ingroup lwip_nosys
  176. * Forwards a received packet for input processing with
  177. * ethernet_input() or ip_input() depending on netif flags.
  178. * Don't call directly, pass to netif_add() and call
  179. * netif->input().
  180. * Only works if the netif driver correctly sets
  181. * NETIF_FLAG_ETHARP and/or NETIF_FLAG_ETHERNET flag!
  182. */
  183. err_t
  184. netif_input(struct pbuf *p, struct netif *inp)
  185. {
  186. #if LWIP_ETHERNET
  187. if (inp->flags & (NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET)) {
  188. return ethernet_input(p, inp);
  189. } else
  190. #endif /* LWIP_ETHERNET */
  191. return ip_input(p, inp);
  192. }
  193. /**
  194. * @ingroup netif
  195. * Add a network interface to the list of lwIP netifs.
  196. *
  197. * @param netif a pre-allocated netif structure
  198. * @param ipaddr IP address for the new netif
  199. * @param netmask network mask for the new netif
  200. * @param gw default gateway IP address for the new netif
  201. * @param state opaque data passed to the new netif
  202. * @param init callback function that initializes the interface
  203. * @param input callback function that is called to pass
  204. * ingress packets up in the protocol layer stack.\n
  205. * It is recommended to use a function that passes the input directly
  206. * to the stack (netif_input(), NO_SYS=1 mode) or via sending a
  207. * message to TCPIP thread (tcpip_input(), NO_SYS=0 mode).\n
  208. * These functions use netif flags NETIF_FLAG_ETHARP and NETIF_FLAG_ETHERNET
  209. * to decide whether to forward to ethernet_input() or ip_input().
  210. * In other words, the functions only work when the netif
  211. * driver is implemented correctly!\n
  212. * Most members of struct netif should be be initialized by the
  213. * netif init function = netif driver (init parameter of this function).\n
  214. * IPv6: Don't forget to call netif_create_ip6_linklocal_address() after
  215. * setting the MAC address in struct netif.hwaddr
  216. * (IPv6 requires a link-local address).
  217. *
  218. * @return netif, or NULL if failed.
  219. */
  220. struct netif *
  221. netif_add(struct netif *netif,
  222. #if LWIP_IPV4
  223. const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, const ip4_addr_t *gw,
  224. #endif /* LWIP_IPV4 */
  225. void *state, netif_init_fn init, netif_input_fn input)
  226. {
  227. #if LWIP_IPV6
  228. s8_t i;
  229. #endif
  230. LWIP_ASSERT("No init function given", init != NULL);
  231. /* reset new interface configuration state */
  232. #if LWIP_IPV4
  233. ip_addr_set_zero_ip4(&netif->ip_addr);
  234. ip_addr_set_zero_ip4(&netif->netmask);
  235. ip_addr_set_zero_ip4(&netif->gw);
  236. #endif /* LWIP_IPV4 */
  237. #if LWIP_IPV6
  238. for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
  239. ip_addr_set_zero_ip6(&netif->ip6_addr[i]);
  240. netif->ip6_addr_state[i] = IP6_ADDR_INVALID;
  241. }
  242. netif->output_ip6 = netif_null_output_ip6;
  243. #endif /* LWIP_IPV6 */
  244. NETIF_SET_CHECKSUM_CTRL(netif, NETIF_CHECKSUM_ENABLE_ALL);
  245. netif->flags = 0;
  246. #ifdef netif_get_client_data
  247. memset(netif->client_data, 0, sizeof(netif->client_data));
  248. #endif /* LWIP_NUM_NETIF_CLIENT_DATA */
  249. #if LWIP_IPV6_AUTOCONFIG
  250. /* IPv6 address autoconfiguration not enabled by default */
  251. netif->ip6_autoconfig_enabled = 0;
  252. #endif /* LWIP_IPV6_AUTOCONFIG */
  253. #if LWIP_IPV6_SEND_ROUTER_SOLICIT
  254. netif->rs_count = LWIP_ND6_MAX_MULTICAST_SOLICIT;
  255. #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
  256. #if LWIP_NETIF_STATUS_CALLBACK
  257. netif->status_callback = NULL;
  258. #endif /* LWIP_NETIF_STATUS_CALLBACK */
  259. #if LWIP_NETIF_LINK_CALLBACK
  260. netif->link_callback = NULL;
  261. #endif /* LWIP_NETIF_LINK_CALLBACK */
  262. #if LWIP_IGMP
  263. netif->igmp_mac_filter = NULL;
  264. #endif /* LWIP_IGMP */
  265. #if LWIP_IPV6 && LWIP_IPV6_MLD
  266. netif->mld_mac_filter = NULL;
  267. #endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
  268. #if ENABLE_LOOPBACK
  269. netif->loop_first = NULL;
  270. netif->loop_last = NULL;
  271. #endif /* ENABLE_LOOPBACK */
  272. /* remember netif specific state information data */
  273. netif->state = state;
  274. netif->num = netif_num++;
  275. netif->input = input;
  276. NETIF_SET_HWADDRHINT(netif, NULL);
  277. #if ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS
  278. netif->loop_cnt_current = 0;
  279. #endif /* ENABLE_LOOPBACK && LWIP_LOOPBACK_MAX_PBUFS */
  280. #if LWIP_IPV4
  281. netif_set_addr(netif, ipaddr, netmask, gw);
  282. #endif /* LWIP_IPV4 */
  283. /* call user specified initialization function for netif */
  284. if (init(netif) != ERR_OK) {
  285. return NULL;
  286. }
  287. /* add this netif to the list */
  288. netif->next = netif_list;
  289. netif_list = netif;
  290. mib2_netif_added(netif);
  291. #if LWIP_IGMP
  292. /* start IGMP processing */
  293. if (netif->flags & NETIF_FLAG_IGMP) {
  294. igmp_start(netif);
  295. }
  296. #endif /* LWIP_IGMP */
  297. LWIP_DEBUGF(NETIF_DEBUG, ("netif: added interface %c%c IP",
  298. netif->name[0], netif->name[1]));
  299. #if LWIP_IPV4
  300. LWIP_DEBUGF(NETIF_DEBUG, (" addr "));
  301. ip4_addr_debug_print(NETIF_DEBUG, ipaddr);
  302. LWIP_DEBUGF(NETIF_DEBUG, (" netmask "));
  303. ip4_addr_debug_print(NETIF_DEBUG, netmask);
  304. LWIP_DEBUGF(NETIF_DEBUG, (" gw "));
  305. ip4_addr_debug_print(NETIF_DEBUG, gw);
  306. #endif /* LWIP_IPV4 */
  307. LWIP_DEBUGF(NETIF_DEBUG, ("\n"));
  308. return netif;
  309. }
  310. #if LWIP_IPV4
  311. /**
  312. * @ingroup netif_ip4
  313. * Change IP address configuration for a network interface (including netmask
  314. * and default gateway).
  315. *
  316. * @param netif the network interface to change
  317. * @param ipaddr the new IP address
  318. * @param netmask the new netmask
  319. * @param gw the new default gateway
  320. */
  321. void
  322. netif_set_addr(struct netif *netif, const ip4_addr_t *ipaddr, const ip4_addr_t *netmask,
  323. const ip4_addr_t *gw)
  324. {
  325. if (ip4_addr_isany(ipaddr)) {
  326. /* when removing an address, we have to remove it *before* changing netmask/gw
  327. to ensure that tcp RST segment can be sent correctly */
  328. netif_set_ipaddr(netif, ipaddr);
  329. netif_set_netmask(netif, netmask);
  330. netif_set_gw(netif, gw);
  331. } else {
  332. netif_set_netmask(netif, netmask);
  333. netif_set_gw(netif, gw);
  334. /* set ipaddr last to ensure netmask/gw have been set when status callback is called */
  335. netif_set_ipaddr(netif, ipaddr);
  336. }
  337. }
  338. #endif /* LWIP_IPV4*/
  339. /**
  340. * @ingroup netif
  341. * Remove a network interface from the list of lwIP netifs.
  342. *
  343. * @param netif the network interface to remove
  344. */
  345. void
  346. netif_remove(struct netif *netif)
  347. {
  348. #if LWIP_IPV6
  349. int i;
  350. #endif
  351. if (netif == NULL) {
  352. return;
  353. }
  354. #if LWIP_IPV4
  355. if (!ip4_addr_isany_val(*netif_ip4_addr(netif))) {
  356. #if LWIP_TCP
  357. tcp_netif_ip_addr_changed(netif_ip_addr4(netif), NULL);
  358. #endif /* LWIP_TCP */
  359. #if LWIP_UDP
  360. udp_netif_ip_addr_changed(netif_ip_addr4(netif), NULL);
  361. #endif /* LWIP_UDP */
  362. #if LWIP_RAW
  363. raw_netif_ip_addr_changed(netif_ip_addr4(netif), NULL);
  364. #endif /* LWIP_RAW */
  365. }
  366. #if LWIP_IGMP
  367. /* stop IGMP processing */
  368. if (netif->flags & NETIF_FLAG_IGMP) {
  369. igmp_stop(netif);
  370. }
  371. #endif /* LWIP_IGMP */
  372. #endif /* LWIP_IPV4*/
  373. #if LWIP_IPV6
  374. for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
  375. if (ip6_addr_isvalid(netif_ip6_addr_state(netif, i))) {
  376. #if LWIP_TCP
  377. tcp_netif_ip_addr_changed(netif_ip_addr6(netif, i), NULL);
  378. #endif /* LWIP_TCP */
  379. #if LWIP_UDP
  380. udp_netif_ip_addr_changed(netif_ip_addr6(netif, i), NULL);
  381. #endif /* LWIP_UDP */
  382. #if LWIP_RAW
  383. raw_netif_ip_addr_changed(netif_ip_addr6(netif, i), NULL);
  384. #endif /* LWIP_RAW */
  385. }
  386. }
  387. #if LWIP_IPV6_MLD
  388. /* stop MLD processing */
  389. mld6_stop(netif);
  390. #endif /* LWIP_IPV6_MLD */
  391. #endif /* LWIP_IPV6 */
  392. if (netif_is_up(netif)) {
  393. /* set netif down before removing (call callback function) */
  394. netif_set_down(netif);
  395. }
  396. mib2_remove_ip4(netif);
  397. /* this netif is default? */
  398. if (netif_default == netif) {
  399. /* reset default netif */
  400. netif_set_default(NULL);
  401. }
  402. /* is it the first netif? */
  403. if (netif_list == netif) {
  404. netif_list = netif->next;
  405. } else {
  406. /* look for netif further down the list */
  407. struct netif * tmp_netif;
  408. for (tmp_netif = netif_list; tmp_netif != NULL; tmp_netif = tmp_netif->next) {
  409. if (tmp_netif->next == netif) {
  410. tmp_netif->next = netif->next;
  411. break;
  412. }
  413. }
  414. if (tmp_netif == NULL) {
  415. return; /* netif is not on the list */
  416. }
  417. }
  418. mib2_netif_removed(netif);
  419. #if LWIP_NETIF_REMOVE_CALLBACK
  420. if (netif->remove_callback) {
  421. netif->remove_callback(netif);
  422. }
  423. #endif /* LWIP_NETIF_REMOVE_CALLBACK */
  424. LWIP_DEBUGF( NETIF_DEBUG, ("netif_remove: removed netif\n") );
  425. }
  426. /**
  427. * @ingroup netif
  428. * Find a network interface by searching for its name
  429. *
  430. * @param name the name of the netif (like netif->name) plus concatenated number
  431. * in ascii representation (e.g. 'en0')
  432. */
  433. struct netif *
  434. netif_find(const char *name)
  435. {
  436. struct netif *netif;
  437. u8_t num;
  438. if (name == NULL) {
  439. return NULL;
  440. }
  441. num = (u8_t)(name[2] - '0');
  442. for (netif = netif_list; netif != NULL; netif = netif->next) {
  443. if (num == netif->num &&
  444. name[0] == netif->name[0] &&
  445. name[1] == netif->name[1]) {
  446. LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: found %c%c\n", name[0], name[1]));
  447. return netif;
  448. }
  449. }
  450. LWIP_DEBUGF(NETIF_DEBUG, ("netif_find: didn't find %c%c\n", name[0], name[1]));
  451. return NULL;
  452. }
  453. #if LWIP_IPV4
  454. /**
  455. * @ingroup netif_ip4
  456. * Change the IP address of a network interface
  457. *
  458. * @param netif the network interface to change
  459. * @param ipaddr the new IP address
  460. *
  461. * @note call netif_set_addr() if you also want to change netmask and
  462. * default gateway
  463. */
  464. void
  465. netif_set_ipaddr(struct netif *netif, const ip4_addr_t *ipaddr)
  466. {
  467. ip_addr_t new_addr;
  468. *ip_2_ip4(&new_addr) = (ipaddr ? *ipaddr : *IP4_ADDR_ANY4);
  469. IP_SET_TYPE_VAL(new_addr, IPADDR_TYPE_V4);
  470. /* address is actually being changed? */
  471. if (ip4_addr_cmp(ip_2_ip4(&new_addr), netif_ip4_addr(netif)) == 0) {
  472. LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: netif address being changed\n"));
  473. #if LWIP_TCP
  474. tcp_netif_ip_addr_changed(netif_ip_addr4(netif), &new_addr);
  475. #endif /* LWIP_TCP */
  476. #if LWIP_UDP
  477. udp_netif_ip_addr_changed(netif_ip_addr4(netif), &new_addr);
  478. #endif /* LWIP_UDP */
  479. #if LWIP_RAW
  480. raw_netif_ip_addr_changed(netif_ip_addr4(netif), &new_addr);
  481. #endif /* LWIP_RAW */
  482. mib2_remove_ip4(netif);
  483. mib2_remove_route_ip4(0, netif);
  484. /* set new IP address to netif */
  485. ip4_addr_set(ip_2_ip4(&netif->ip_addr), ipaddr);
  486. IP_SET_TYPE_VAL(netif->ip_addr, IPADDR_TYPE_V4);
  487. mib2_add_ip4(netif);
  488. mib2_add_route_ip4(0, netif);
  489. netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV4);
  490. NETIF_STATUS_CALLBACK(netif);
  491. #ifdef RT_USING_NETDEV
  492. /* rt-thread sal network interface device set IP address operations */
  493. netdev_low_level_set_ipaddr(netdev_get_by_name(netif->name), &netif->ip_addr);
  494. #endif /* RT_USING_NETDEV */
  495. }
  496. 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",
  497. netif->name[0], netif->name[1],
  498. ip4_addr1_16(netif_ip4_addr(netif)),
  499. ip4_addr2_16(netif_ip4_addr(netif)),
  500. ip4_addr3_16(netif_ip4_addr(netif)),
  501. ip4_addr4_16(netif_ip4_addr(netif))));
  502. }
  503. /**
  504. * @ingroup netif_ip4
  505. * Change the default gateway for a network interface
  506. *
  507. * @param netif the network interface to change
  508. * @param gw the new default gateway
  509. *
  510. * @note call netif_set_addr() if you also want to change ip address and netmask
  511. */
  512. void
  513. netif_set_gw(struct netif *netif, const ip4_addr_t *gw)
  514. {
  515. ip4_addr_set(ip_2_ip4(&netif->gw), gw);
  516. IP_SET_TYPE_VAL(netif->gw, IPADDR_TYPE_V4);
  517. 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",
  518. netif->name[0], netif->name[1],
  519. ip4_addr1_16(netif_ip4_gw(netif)),
  520. ip4_addr2_16(netif_ip4_gw(netif)),
  521. ip4_addr3_16(netif_ip4_gw(netif)),
  522. ip4_addr4_16(netif_ip4_gw(netif))));
  523. #ifdef RT_USING_NETDEV
  524. /* rt_thread network interface device set gateway address */
  525. netdev_low_level_set_gw(netdev_get_by_name(netif->name), &netif->gw);
  526. #endif /* RT_USING_NETDEV */
  527. }
  528. /**
  529. * @ingroup netif_ip4
  530. * Change the netmask of a network interface
  531. *
  532. * @param netif the network interface to change
  533. * @param netmask the new netmask
  534. *
  535. * @note call netif_set_addr() if you also want to change ip address and
  536. * default gateway
  537. */
  538. void
  539. netif_set_netmask(struct netif *netif, const ip4_addr_t *netmask)
  540. {
  541. mib2_remove_route_ip4(0, netif);
  542. /* set new netmask to netif */
  543. ip4_addr_set(ip_2_ip4(&netif->netmask), netmask);
  544. IP_SET_TYPE_VAL(netif->netmask, IPADDR_TYPE_V4);
  545. mib2_add_route_ip4(0, netif);
  546. 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",
  547. netif->name[0], netif->name[1],
  548. ip4_addr1_16(netif_ip4_netmask(netif)),
  549. ip4_addr2_16(netif_ip4_netmask(netif)),
  550. ip4_addr3_16(netif_ip4_netmask(netif)),
  551. ip4_addr4_16(netif_ip4_netmask(netif))));
  552. #ifdef RT_USING_NETDEV
  553. /* rt-thread network interface device set netmask address */
  554. netdev_low_level_set_netmask(netdev_get_by_name(netif->name), &netif->netmask);
  555. #endif /* RT_USING_NETDEV */
  556. }
  557. #endif /* LWIP_IPV4 */
  558. /**
  559. * @ingroup netif
  560. * Set a network interface as the default network interface
  561. * (used to output all packets for which no specific route is found)
  562. *
  563. * @param netif the default network interface
  564. */
  565. void
  566. netif_set_default(struct netif *netif)
  567. {
  568. if (netif == NULL) {
  569. /* remove default route */
  570. mib2_remove_route_ip4(1, netif);
  571. } else {
  572. /* install default route */
  573. mib2_add_route_ip4(1, netif);
  574. }
  575. netif_default = netif;
  576. LWIP_DEBUGF(NETIF_DEBUG, ("netif: setting default interface %c%c\n",
  577. netif ? netif->name[0] : '\'', netif ? netif->name[1] : '\''));
  578. }
  579. /**
  580. * @ingroup netif
  581. * Bring an interface up, available for processing
  582. * traffic.
  583. */
  584. void
  585. netif_set_up(struct netif *netif)
  586. {
  587. if (!(netif->flags & NETIF_FLAG_UP)) {
  588. netif->flags |= NETIF_FLAG_UP;
  589. MIB2_COPY_SYSUPTIME_TO(&netif->ts);
  590. NETIF_STATUS_CALLBACK(netif);
  591. if (netif->flags & NETIF_FLAG_LINK_UP) {
  592. netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV4|NETIF_REPORT_TYPE_IPV6);
  593. }
  594. #ifdef RT_USING_NETDEV
  595. /* rt-thread network interface device set up status */
  596. netdev_low_level_set_status(netdev_get_by_name(netif->name), RT_TRUE);
  597. #endif /* RT_USING_NETDEV */
  598. }
  599. }
  600. /** Send ARP/IGMP/MLD/RS events, e.g. on link-up/netif-up or addr-change
  601. */
  602. static void
  603. netif_issue_reports(struct netif* netif, u8_t report_type)
  604. {
  605. #if LWIP_IPV4
  606. if ((report_type & NETIF_REPORT_TYPE_IPV4) &&
  607. !ip4_addr_isany_val(*netif_ip4_addr(netif))) {
  608. #if LWIP_ARP
  609. /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */
  610. if (netif->flags & (NETIF_FLAG_ETHARP)) {
  611. etharp_gratuitous(netif);
  612. }
  613. #endif /* LWIP_ARP */
  614. #if LWIP_IGMP
  615. /* resend IGMP memberships */
  616. if (netif->flags & NETIF_FLAG_IGMP) {
  617. igmp_report_groups(netif);
  618. }
  619. #endif /* LWIP_IGMP */
  620. }
  621. #endif /* LWIP_IPV4 */
  622. #if LWIP_IPV6
  623. if (report_type & NETIF_REPORT_TYPE_IPV6) {
  624. #if LWIP_IPV6_MLD
  625. /* send mld memberships */
  626. mld6_report_groups(netif);
  627. #endif /* LWIP_IPV6_MLD */
  628. #if LWIP_IPV6_SEND_ROUTER_SOLICIT
  629. /* Send Router Solicitation messages. */
  630. netif->rs_count = LWIP_ND6_MAX_MULTICAST_SOLICIT;
  631. #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */
  632. }
  633. #endif /* LWIP_IPV6 */
  634. }
  635. /**
  636. * @ingroup netif
  637. * Bring an interface down, disabling any traffic processing.
  638. */
  639. void
  640. netif_set_down(struct netif *netif)
  641. {
  642. if (netif->flags & NETIF_FLAG_UP) {
  643. netif->flags &= ~NETIF_FLAG_UP;
  644. MIB2_COPY_SYSUPTIME_TO(&netif->ts);
  645. #if LWIP_IPV4 && LWIP_ARP
  646. if (netif->flags & NETIF_FLAG_ETHARP) {
  647. etharp_cleanup_netif(netif);
  648. }
  649. #endif /* LWIP_IPV4 && LWIP_ARP */
  650. #if LWIP_IPV6
  651. nd6_cleanup_netif(netif);
  652. #endif /* LWIP_IPV6 */
  653. NETIF_STATUS_CALLBACK(netif);
  654. #ifdef RT_USING_NETDEV
  655. /* rt-thread network interface device set down status */
  656. netdev_low_level_set_status(netdev_get_by_name(netif->name), RT_FALSE);
  657. #endif /* RT_USING_NETDEV */
  658. }
  659. }
  660. #if LWIP_NETIF_STATUS_CALLBACK
  661. /**
  662. * @ingroup netif
  663. * Set callback to be called when interface is brought up/down or address is changed while up
  664. */
  665. void
  666. netif_set_status_callback(struct netif *netif, netif_status_callback_fn status_callback)
  667. {
  668. if (netif) {
  669. netif->status_callback = status_callback;
  670. }
  671. }
  672. #endif /* LWIP_NETIF_STATUS_CALLBACK */
  673. #if LWIP_NETIF_REMOVE_CALLBACK
  674. /**
  675. * @ingroup netif
  676. * Set callback to be called when the interface has been removed
  677. */
  678. void
  679. netif_set_remove_callback(struct netif *netif, netif_status_callback_fn remove_callback)
  680. {
  681. if (netif) {
  682. netif->remove_callback = remove_callback;
  683. }
  684. }
  685. #endif /* LWIP_NETIF_REMOVE_CALLBACK */
  686. /**
  687. * @ingroup netif
  688. * Called by a driver when its link goes up
  689. */
  690. void
  691. netif_set_link_up(struct netif *netif)
  692. {
  693. if (!(netif->flags & NETIF_FLAG_LINK_UP)) {
  694. netif->flags |= NETIF_FLAG_LINK_UP;
  695. #if LWIP_DHCP
  696. dhcp_network_changed(netif);
  697. #endif /* LWIP_DHCP */
  698. #if LWIP_AUTOIP
  699. autoip_network_changed(netif);
  700. #endif /* LWIP_AUTOIP */
  701. if (netif->flags & NETIF_FLAG_UP) {
  702. netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV4|NETIF_REPORT_TYPE_IPV6);
  703. }
  704. NETIF_LINK_CALLBACK(netif);
  705. #ifdef RT_USING_NETDEV
  706. /* rt-thread network interface device set link up status */
  707. netdev_low_level_set_link_status(netdev_get_by_name(netif->name), RT_TRUE);
  708. #endif /* RT_USING_NETDEV */
  709. }
  710. }
  711. /**
  712. * @ingroup netif
  713. * Called by a driver when its link goes down
  714. */
  715. void
  716. netif_set_link_down(struct netif *netif )
  717. {
  718. if (netif->flags & NETIF_FLAG_LINK_UP) {
  719. netif->flags &= ~NETIF_FLAG_LINK_UP;
  720. NETIF_LINK_CALLBACK(netif);
  721. #ifdef RT_USING_NETDEV
  722. /* rt-thread network interface device set link down status */
  723. netdev_low_level_set_link_status(netdev_get_by_name(netif->name), RT_FALSE);
  724. #endif /* RT_USING_NETDEV */
  725. }
  726. }
  727. #if LWIP_NETIF_LINK_CALLBACK
  728. /**
  729. * @ingroup netif
  730. * Set callback to be called when link is brought up/down
  731. */
  732. void
  733. netif_set_link_callback(struct netif *netif, netif_status_callback_fn link_callback)
  734. {
  735. if (netif) {
  736. netif->link_callback = link_callback;
  737. }
  738. }
  739. #endif /* LWIP_NETIF_LINK_CALLBACK */
  740. #if ENABLE_LOOPBACK
  741. /**
  742. * @ingroup netif
  743. * Send an IP packet to be received on the same netif (loopif-like).
  744. * The pbuf is simply copied and handed back to netif->input.
  745. * In multithreaded mode, this is done directly since netif->input must put
  746. * the packet on a queue.
  747. * In callback mode, the packet is put on an internal queue and is fed to
  748. * netif->input by netif_poll().
  749. *
  750. * @param netif the lwip network interface structure
  751. * @param p the (IP) packet to 'send'
  752. * @return ERR_OK if the packet has been sent
  753. * ERR_MEM if the pbuf used to copy the packet couldn't be allocated
  754. */
  755. err_t
  756. netif_loop_output(struct netif *netif, struct pbuf *p)
  757. {
  758. struct pbuf *r;
  759. err_t err;
  760. struct pbuf *last;
  761. #if LWIP_LOOPBACK_MAX_PBUFS
  762. u16_t clen = 0;
  763. #endif /* LWIP_LOOPBACK_MAX_PBUFS */
  764. /* If we have a loopif, SNMP counters are adjusted for it,
  765. * if not they are adjusted for 'netif'. */
  766. #if MIB2_STATS
  767. #if LWIP_HAVE_LOOPIF
  768. struct netif *stats_if = &loop_netif;
  769. #else /* LWIP_HAVE_LOOPIF */
  770. struct netif *stats_if = netif;
  771. #endif /* LWIP_HAVE_LOOPIF */
  772. #endif /* MIB2_STATS */
  773. SYS_ARCH_DECL_PROTECT(lev);
  774. /* Allocate a new pbuf */
  775. r = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
  776. if (r == NULL) {
  777. LINK_STATS_INC(link.memerr);
  778. LINK_STATS_INC(link.drop);
  779. MIB2_STATS_NETIF_INC(stats_if, ifoutdiscards);
  780. return ERR_MEM;
  781. }
  782. #if LWIP_LOOPBACK_MAX_PBUFS
  783. clen = pbuf_clen(r);
  784. /* check for overflow or too many pbuf on queue */
  785. if (((netif->loop_cnt_current + clen) < netif->loop_cnt_current) ||
  786. ((netif->loop_cnt_current + clen) > LWIP_LOOPBACK_MAX_PBUFS)) {
  787. pbuf_free(r);
  788. LINK_STATS_INC(link.memerr);
  789. LINK_STATS_INC(link.drop);
  790. MIB2_STATS_NETIF_INC(stats_if, ifoutdiscards);
  791. return ERR_MEM;
  792. }
  793. netif->loop_cnt_current += clen;
  794. #endif /* LWIP_LOOPBACK_MAX_PBUFS */
  795. /* Copy the whole pbuf queue p into the single pbuf r */
  796. if ((err = pbuf_copy(r, p)) != ERR_OK) {
  797. pbuf_free(r);
  798. LINK_STATS_INC(link.memerr);
  799. LINK_STATS_INC(link.drop);
  800. MIB2_STATS_NETIF_INC(stats_if, ifoutdiscards);
  801. return err;
  802. }
  803. /* Put the packet on a linked list which gets emptied through calling
  804. netif_poll(). */
  805. /* let last point to the last pbuf in chain r */
  806. for (last = r; last->next != NULL; last = last->next);
  807. SYS_ARCH_PROTECT(lev);
  808. if (netif->loop_first != NULL) {
  809. LWIP_ASSERT("if first != NULL, last must also be != NULL", netif->loop_last != NULL);
  810. netif->loop_last->next = r;
  811. netif->loop_last = last;
  812. } else {
  813. netif->loop_first = r;
  814. netif->loop_last = last;
  815. }
  816. SYS_ARCH_UNPROTECT(lev);
  817. LINK_STATS_INC(link.xmit);
  818. MIB2_STATS_NETIF_ADD(stats_if, ifoutoctets, p->tot_len);
  819. MIB2_STATS_NETIF_INC(stats_if, ifoutucastpkts);
  820. #if LWIP_NETIF_LOOPBACK_MULTITHREADING
  821. /* For multithreading environment, schedule a call to netif_poll */
  822. tcpip_callback_with_block((tcpip_callback_fn)netif_poll, netif, 0);
  823. #endif /* LWIP_NETIF_LOOPBACK_MULTITHREADING */
  824. return ERR_OK;
  825. }
  826. #if LWIP_HAVE_LOOPIF
  827. #if LWIP_IPV4
  828. static err_t
  829. netif_loop_output_ipv4(struct netif *netif, struct pbuf *p, const ip4_addr_t* addr)
  830. {
  831. LWIP_UNUSED_ARG(addr);
  832. return netif_loop_output(netif, p);
  833. }
  834. #endif /* LWIP_IPV4 */
  835. #if LWIP_IPV6
  836. static err_t
  837. netif_loop_output_ipv6(struct netif *netif, struct pbuf *p, const ip6_addr_t* addr)
  838. {
  839. LWIP_UNUSED_ARG(addr);
  840. return netif_loop_output(netif, p);
  841. }
  842. #endif /* LWIP_IPV6 */
  843. #endif /* LWIP_HAVE_LOOPIF */
  844. /**
  845. * Call netif_poll() in the main loop of your application. This is to prevent
  846. * reentering non-reentrant functions like tcp_input(). Packets passed to
  847. * netif_loop_output() are put on a list that is passed to netif->input() by
  848. * netif_poll().
  849. */
  850. void
  851. netif_poll(struct netif *netif)
  852. {
  853. /* If we have a loopif, SNMP counters are adjusted for it,
  854. * if not they are adjusted for 'netif'. */
  855. #if MIB2_STATS
  856. #if LWIP_HAVE_LOOPIF
  857. struct netif *stats_if = &loop_netif;
  858. #else /* LWIP_HAVE_LOOPIF */
  859. struct netif *stats_if = netif;
  860. #endif /* LWIP_HAVE_LOOPIF */
  861. #endif /* MIB2_STATS */
  862. SYS_ARCH_DECL_PROTECT(lev);
  863. /* Get a packet from the list. With SYS_LIGHTWEIGHT_PROT=1, this is protected */
  864. SYS_ARCH_PROTECT(lev);
  865. while (netif->loop_first != NULL) {
  866. struct pbuf *in, *in_end;
  867. #if LWIP_LOOPBACK_MAX_PBUFS
  868. u8_t clen = 1;
  869. #endif /* LWIP_LOOPBACK_MAX_PBUFS */
  870. in = in_end = netif->loop_first;
  871. while (in_end->len != in_end->tot_len) {
  872. LWIP_ASSERT("bogus pbuf: len != tot_len but next == NULL!", in_end->next != NULL);
  873. in_end = in_end->next;
  874. #if LWIP_LOOPBACK_MAX_PBUFS
  875. clen++;
  876. #endif /* LWIP_LOOPBACK_MAX_PBUFS */
  877. }
  878. #if LWIP_LOOPBACK_MAX_PBUFS
  879. /* adjust the number of pbufs on queue */
  880. LWIP_ASSERT("netif->loop_cnt_current underflow",
  881. ((netif->loop_cnt_current - clen) < netif->loop_cnt_current));
  882. netif->loop_cnt_current -= clen;
  883. #endif /* LWIP_LOOPBACK_MAX_PBUFS */
  884. /* 'in_end' now points to the last pbuf from 'in' */
  885. if (in_end == netif->loop_last) {
  886. /* this was the last pbuf in the list */
  887. netif->loop_first = netif->loop_last = NULL;
  888. } else {
  889. /* pop the pbuf off the list */
  890. netif->loop_first = in_end->next;
  891. LWIP_ASSERT("should not be null since first != last!", netif->loop_first != NULL);
  892. }
  893. /* De-queue the pbuf from its successors on the 'loop_' list. */
  894. in_end->next = NULL;
  895. SYS_ARCH_UNPROTECT(lev);
  896. LINK_STATS_INC(link.recv);
  897. MIB2_STATS_NETIF_ADD(stats_if, ifinoctets, in->tot_len);
  898. MIB2_STATS_NETIF_INC(stats_if, ifinucastpkts);
  899. /* loopback packets are always IP packets! */
  900. if (ip_input(in, netif) != ERR_OK) {
  901. pbuf_free(in);
  902. }
  903. SYS_ARCH_PROTECT(lev);
  904. }
  905. SYS_ARCH_UNPROTECT(lev);
  906. }
  907. #if !LWIP_NETIF_LOOPBACK_MULTITHREADING
  908. /**
  909. * Calls netif_poll() for every netif on the netif_list.
  910. */
  911. void
  912. netif_poll_all(void)
  913. {
  914. struct netif *netif = netif_list;
  915. /* loop through netifs */
  916. while (netif != NULL) {
  917. netif_poll(netif);
  918. /* proceed to next network interface */
  919. netif = netif->next;
  920. }
  921. }
  922. #endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */
  923. #endif /* ENABLE_LOOPBACK */
  924. #if LWIP_NUM_NETIF_CLIENT_DATA > 0
  925. /**
  926. * @ingroup netif_cd
  927. * Allocate an index to store data in client_data member of struct netif.
  928. * Returned value is an index in mentioned array.
  929. * @see LWIP_NUM_NETIF_CLIENT_DATA
  930. */
  931. u8_t
  932. netif_alloc_client_data_id(void)
  933. {
  934. u8_t result = netif_client_id;
  935. netif_client_id++;
  936. LWIP_ASSERT("Increase LWIP_NUM_NETIF_CLIENT_DATA in lwipopts.h", result < LWIP_NUM_NETIF_CLIENT_DATA);
  937. return result + LWIP_NETIF_CLIENT_DATA_INDEX_MAX;
  938. }
  939. #endif
  940. #if LWIP_IPV6
  941. /**
  942. * @ingroup netif_ip6
  943. * Change an IPv6 address of a network interface
  944. *
  945. * @param netif the network interface to change
  946. * @param addr_idx index of the IPv6 address
  947. * @param addr6 the new IPv6 address
  948. *
  949. * @note call netif_ip6_addr_set_state() to set the address valid/temptative
  950. */
  951. void
  952. netif_ip6_addr_set(struct netif *netif, s8_t addr_idx, const ip6_addr_t *addr6)
  953. {
  954. LWIP_ASSERT("addr6 != NULL", addr6 != NULL);
  955. netif_ip6_addr_set_parts(netif, addr_idx, addr6->addr[0], addr6->addr[1],
  956. addr6->addr[2], addr6->addr[3]);
  957. }
  958. /*
  959. * Change an IPv6 address of a network interface (internal version taking 4 * u32_t)
  960. *
  961. * @param netif the network interface to change
  962. * @param addr_idx index of the IPv6 address
  963. * @param i0 word0 of the new IPv6 address
  964. * @param i1 word1 of the new IPv6 address
  965. * @param i2 word2 of the new IPv6 address
  966. * @param i3 word3 of the new IPv6 address
  967. */
  968. void
  969. netif_ip6_addr_set_parts(struct netif *netif, s8_t addr_idx, u32_t i0, u32_t i1, u32_t i2, u32_t i3)
  970. {
  971. const ip6_addr_t *old_addr;
  972. LWIP_ASSERT("netif != NULL", netif != NULL);
  973. LWIP_ASSERT("invalid index", addr_idx < LWIP_IPV6_NUM_ADDRESSES);
  974. old_addr = netif_ip6_addr(netif, addr_idx);
  975. /* address is actually being changed? */
  976. if ((old_addr->addr[0] != i0) || (old_addr->addr[1] != i1) ||
  977. (old_addr->addr[2] != i2) || (old_addr->addr[3] != i3)) {
  978. LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_ip6_addr_set: netif address being changed\n"));
  979. if (netif_ip6_addr_state(netif, addr_idx) & IP6_ADDR_VALID) {
  980. #if LWIP_TCP || LWIP_UDP
  981. ip_addr_t new_ipaddr;
  982. IP_ADDR6(&new_ipaddr, i0, i1, i2, i3);
  983. #endif /* LWIP_TCP || LWIP_UDP */
  984. #if LWIP_TCP
  985. tcp_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), &new_ipaddr);
  986. #endif /* LWIP_TCP */
  987. #if LWIP_UDP
  988. udp_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), &new_ipaddr);
  989. #endif /* LWIP_UDP */
  990. #if LWIP_RAW
  991. raw_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), &new_ipaddr);
  992. #endif /* LWIP_RAW */
  993. }
  994. /* @todo: remove/readd mib2 ip6 entries? */
  995. IP6_ADDR(ip_2_ip6(&(netif->ip6_addr[addr_idx])), i0, i1, i2, i3);
  996. IP_SET_TYPE_VAL(netif->ip6_addr[addr_idx], IPADDR_TYPE_V6);
  997. if (netif_ip6_addr_state(netif, addr_idx) & IP6_ADDR_VALID) {
  998. netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV6);
  999. NETIF_STATUS_CALLBACK(netif);
  1000. }
  1001. }
  1002. 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",
  1003. addr_idx, netif->name[0], netif->name[1], ip6addr_ntoa(netif_ip6_addr(netif, addr_idx)),
  1004. netif_ip6_addr_state(netif, addr_idx)));
  1005. }
  1006. /**
  1007. * @ingroup netif_ip6
  1008. * Change the state of an IPv6 address of a network interface
  1009. * (INVALID, TEMPTATIVE, PREFERRED, DEPRECATED, where TEMPTATIVE
  1010. * includes the number of checks done, see ip6_addr.h)
  1011. *
  1012. * @param netif the network interface to change
  1013. * @param addr_idx index of the IPv6 address
  1014. * @param state the new IPv6 address state
  1015. */
  1016. void
  1017. netif_ip6_addr_set_state(struct netif* netif, s8_t addr_idx, u8_t state)
  1018. {
  1019. u8_t old_state;
  1020. LWIP_ASSERT("netif != NULL", netif != NULL);
  1021. LWIP_ASSERT("invalid index", addr_idx < LWIP_IPV6_NUM_ADDRESSES);
  1022. old_state = netif_ip6_addr_state(netif, addr_idx);
  1023. /* state is actually being changed? */
  1024. if (old_state != state) {
  1025. u8_t old_valid = old_state & IP6_ADDR_VALID;
  1026. u8_t new_valid = state & IP6_ADDR_VALID;
  1027. LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_ip6_addr_set_state: netif address state being changed\n"));
  1028. #if LWIP_IPV6_MLD
  1029. /* Reevaluate solicited-node multicast group membership. */
  1030. if (netif->flags & NETIF_FLAG_MLD6) {
  1031. nd6_adjust_mld_membership(netif, addr_idx, state);
  1032. }
  1033. #endif /* LWIP_IPV6_MLD */
  1034. if (old_valid && !new_valid) {
  1035. /* address about to be removed by setting invalid */
  1036. #if LWIP_TCP
  1037. tcp_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), NULL);
  1038. #endif /* LWIP_TCP */
  1039. #if LWIP_UDP
  1040. udp_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), NULL);
  1041. #endif /* LWIP_UDP */
  1042. #if LWIP_RAW
  1043. raw_netif_ip_addr_changed(netif_ip_addr6(netif, addr_idx), NULL);
  1044. #endif /* LWIP_RAW */
  1045. /* @todo: remove mib2 ip6 entries? */
  1046. }
  1047. netif->ip6_addr_state[addr_idx] = state;
  1048. if (!old_valid && new_valid) {
  1049. /* address added by setting valid */
  1050. /* @todo: add mib2 ip6 entries? */
  1051. netif_issue_reports(netif, NETIF_REPORT_TYPE_IPV6);
  1052. }
  1053. if ((old_state & IP6_ADDR_PREFERRED) != (state & IP6_ADDR_PREFERRED)) {
  1054. /* address state has changed (valid flag changed or switched between
  1055. preferred and deprecated) -> call the callback function */
  1056. NETIF_STATUS_CALLBACK(netif);
  1057. }
  1058. }
  1059. 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",
  1060. addr_idx, netif->name[0], netif->name[1], ip6addr_ntoa(netif_ip6_addr(netif, addr_idx)),
  1061. netif_ip6_addr_state(netif, addr_idx)));
  1062. }
  1063. /**
  1064. * Checks if a specific address is assigned to the netif and returns its
  1065. * index.
  1066. *
  1067. * @param netif the netif to check
  1068. * @param ip6addr the IPv6 address to find
  1069. * @return >= 0: address found, this is its index
  1070. * -1: address not found on this netif
  1071. */
  1072. s8_t
  1073. netif_get_ip6_addr_match(struct netif *netif, const ip6_addr_t *ip6addr)
  1074. {
  1075. s8_t i;
  1076. for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
  1077. if (!ip6_addr_isinvalid(netif_ip6_addr_state(netif, i)) &&
  1078. ip6_addr_cmp(netif_ip6_addr(netif, i), ip6addr)) {
  1079. return i;
  1080. }
  1081. }
  1082. return -1;
  1083. }
  1084. /**
  1085. * @ingroup netif_ip6
  1086. * Create a link-local IPv6 address on a netif (stored in slot 0)
  1087. *
  1088. * @param netif the netif to create the address on
  1089. * @param from_mac_48bit if != 0, assume hwadr is a 48-bit MAC address (std conversion)
  1090. * if == 0, use hwaddr directly as interface ID
  1091. */
  1092. void
  1093. netif_create_ip6_linklocal_address(struct netif *netif, u8_t from_mac_48bit)
  1094. {
  1095. u8_t i, addr_index;
  1096. /* Link-local prefix. */
  1097. ip_2_ip6(&netif->ip6_addr[0])->addr[0] = PP_HTONL(0xfe800000ul);
  1098. ip_2_ip6(&netif->ip6_addr[0])->addr[1] = 0;
  1099. /* Generate interface ID. */
  1100. if (from_mac_48bit) {
  1101. /* Assume hwaddr is a 48-bit IEEE 802 MAC. Convert to EUI-64 address. Complement Group bit. */
  1102. ip_2_ip6(&netif->ip6_addr[0])->addr[2] = lwip_htonl((((u32_t)(netif->hwaddr[0] ^ 0x02)) << 24) |
  1103. ((u32_t)(netif->hwaddr[1]) << 16) |
  1104. ((u32_t)(netif->hwaddr[2]) << 8) |
  1105. (0xff));
  1106. ip_2_ip6(&netif->ip6_addr[0])->addr[3] = lwip_htonl((0xfeul << 24) |
  1107. ((u32_t)(netif->hwaddr[3]) << 16) |
  1108. ((u32_t)(netif->hwaddr[4]) << 8) |
  1109. (netif->hwaddr[5]));
  1110. } else {
  1111. /* Use hwaddr directly as interface ID. */
  1112. ip_2_ip6(&netif->ip6_addr[0])->addr[2] = 0;
  1113. ip_2_ip6(&netif->ip6_addr[0])->addr[3] = 0;
  1114. addr_index = 3;
  1115. for (i = 0; (i < 8) && (i < netif->hwaddr_len); i++) {
  1116. if (i == 4) {
  1117. addr_index--;
  1118. }
  1119. ip_2_ip6(&netif->ip6_addr[0])->addr[addr_index] |= ((u32_t)(netif->hwaddr[netif->hwaddr_len - i - 1])) << (8 * (i & 0x03));
  1120. }
  1121. }
  1122. #ifdef RT_USING_NETDEV
  1123. /* rt-thread network interface device set ipv6 address */
  1124. ip_addr_copy(netdev_get_by_name(netif->name)->ip6_addr[0], netif->ip6_addr[0]);
  1125. #endif /* RT_USING_NETDEV */
  1126. /* Set address state. */
  1127. #if LWIP_IPV6_DUP_DETECT_ATTEMPTS
  1128. /* Will perform duplicate address detection (DAD). */
  1129. netif_ip6_addr_set_state(netif, 0, IP6_ADDR_TENTATIVE);
  1130. #else
  1131. /* Consider address valid. */
  1132. netif_ip6_addr_set_state(netif, 0, IP6_ADDR_PREFERRED);
  1133. #endif /* LWIP_IPV6_AUTOCONFIG */
  1134. }
  1135. /**
  1136. * @ingroup netif_ip6
  1137. * This function allows for the easy addition of a new IPv6 address to an interface.
  1138. * It takes care of finding an empty slot and then sets the address tentative
  1139. * (to make sure that all the subsequent processing happens).
  1140. *
  1141. * @param netif netif to add the address on
  1142. * @param ip6addr address to add
  1143. * @param chosen_idx if != NULL, the chosen IPv6 address index will be stored here
  1144. */
  1145. err_t
  1146. netif_add_ip6_address(struct netif *netif, const ip6_addr_t *ip6addr, s8_t *chosen_idx)
  1147. {
  1148. s8_t i;
  1149. i = netif_get_ip6_addr_match(netif, ip6addr);
  1150. if (i >= 0) {
  1151. /* Address already added */
  1152. if (chosen_idx != NULL) {
  1153. *chosen_idx = i;
  1154. }
  1155. return ERR_OK;
  1156. }
  1157. /* Find a free slot -- musn't be the first one (reserved for link local) */
  1158. for (i = 1; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
  1159. if (ip6_addr_isinvalid(netif_ip6_addr_state(netif, i))) {
  1160. ip_addr_copy_from_ip6(netif->ip6_addr[i], *ip6addr);
  1161. #ifdef RT_USING_NETDEV
  1162. /* rt-thread network interface device set ipv6 address */
  1163. ip_addr_copy(netdev_get_by_name(netif->name)->ip6_addr[i], netif->ip6_addr[i]);
  1164. #endif /* RT_USING_NETDEV */
  1165. netif_ip6_addr_set_state(netif, i, IP6_ADDR_TENTATIVE);
  1166. if (chosen_idx != NULL) {
  1167. *chosen_idx = i;
  1168. }
  1169. return ERR_OK;
  1170. }
  1171. }
  1172. if (chosen_idx != NULL) {
  1173. *chosen_idx = -1;
  1174. }
  1175. return ERR_VAL;
  1176. }
  1177. /** Dummy IPv6 output function for netifs not supporting IPv6
  1178. */
  1179. static err_t
  1180. netif_null_output_ip6(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr)
  1181. {
  1182. LWIP_UNUSED_ARG(netif);
  1183. LWIP_UNUSED_ARG(p);
  1184. LWIP_UNUSED_ARG(ipaddr);
  1185. return ERR_IF;
  1186. }
  1187. #endif /* LWIP_IPV6 */