netif.c 38 KB

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