net_if.h 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501
  1. /*
  2. * Copyright (c) 2016 Intel Corporation.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief Public API for network interface
  9. */
  10. #ifndef ZEPHYR_INCLUDE_NET_NET_IF_H_
  11. #define ZEPHYR_INCLUDE_NET_NET_IF_H_
  12. /**
  13. * @brief Network Interface abstraction layer
  14. * @defgroup net_if Network Interface abstraction layer
  15. * @ingroup networking
  16. * @{
  17. */
  18. #include <device.h>
  19. #include <sys/slist.h>
  20. #include <net/net_core.h>
  21. #include <net/hostname.h>
  22. #include <net/net_linkaddr.h>
  23. #include <net/net_ip.h>
  24. #include <net/net_l2.h>
  25. #include <net/net_stats.h>
  26. #include <net/net_timeout.h>
  27. #if defined(CONFIG_NET_DHCPV4) && defined(CONFIG_NET_NATIVE_IPV4)
  28. #include <net/dhcpv4.h>
  29. #endif
  30. #if defined(CONFIG_NET_IPV4_AUTO) && defined(CONFIG_NET_NATIVE_IPV4)
  31. #include <net/ipv4_autoconf.h>
  32. #endif
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /**
  37. * @brief Network Interface unicast IP addresses
  38. *
  39. * Stores the unicast IP addresses assigned to this network interface.
  40. */
  41. struct net_if_addr {
  42. /** IP address */
  43. struct net_addr address;
  44. #if defined(CONFIG_NET_NATIVE_IPV6)
  45. struct net_timeout lifetime;
  46. #endif
  47. #if defined(CONFIG_NET_IPV6_DAD) && defined(CONFIG_NET_NATIVE_IPV6)
  48. /** Duplicate address detection (DAD) timer */
  49. sys_snode_t dad_node;
  50. uint32_t dad_start;
  51. #endif
  52. /** How the IP address was set */
  53. enum net_addr_type addr_type;
  54. /** What is the current state of the address */
  55. enum net_addr_state addr_state;
  56. #if defined(CONFIG_NET_IPV6_DAD) && defined(CONFIG_NET_NATIVE_IPV6)
  57. /** How many times we have done DAD */
  58. uint8_t dad_count;
  59. #endif
  60. /** Is the IP address valid forever */
  61. uint8_t is_infinite : 1;
  62. /** Is this IP address used or not */
  63. uint8_t is_used : 1;
  64. /** Is this IP address usage limited to the subnet (mesh) or not */
  65. uint8_t is_mesh_local : 1;
  66. uint8_t _unused : 5;
  67. };
  68. /**
  69. * @brief Network Interface multicast IP addresses
  70. *
  71. * Stores the multicast IP addresses assigned to this network interface.
  72. */
  73. struct net_if_mcast_addr {
  74. /** IP address */
  75. struct net_addr address;
  76. /** Is this multicast IP address used or not */
  77. uint8_t is_used : 1;
  78. /** Did we join to this group */
  79. uint8_t is_joined : 1;
  80. uint8_t _unused : 6;
  81. };
  82. /**
  83. * @brief Network Interface IPv6 prefixes
  84. *
  85. * Stores the multicast IP addresses assigned to this network interface.
  86. */
  87. struct net_if_ipv6_prefix {
  88. /** Prefix lifetime */
  89. struct net_timeout lifetime;
  90. /** IPv6 prefix */
  91. struct in6_addr prefix;
  92. /** Backpointer to network interface where this prefix is used */
  93. struct net_if *iface;
  94. /** Prefix length */
  95. uint8_t len;
  96. /** Is the IP prefix valid forever */
  97. uint8_t is_infinite : 1;
  98. /** Is this prefix used or not */
  99. uint8_t is_used : 1;
  100. uint8_t _unused : 6;
  101. };
  102. /**
  103. * @brief Information about routers in the system.
  104. *
  105. * Stores the router information.
  106. */
  107. struct net_if_router {
  108. /** Slist lifetime timer node */
  109. sys_snode_t node;
  110. /** IP address */
  111. struct net_addr address;
  112. /** Network interface the router is connected to */
  113. struct net_if *iface;
  114. /** Router life timer start */
  115. uint32_t life_start;
  116. /** Router lifetime */
  117. uint16_t lifetime;
  118. /** Is this router used or not */
  119. uint8_t is_used : 1;
  120. /** Is default router */
  121. uint8_t is_default : 1;
  122. /** Is the router valid forever */
  123. uint8_t is_infinite : 1;
  124. uint8_t _unused : 5;
  125. };
  126. enum net_if_flag {
  127. /** Interface is up/ready to receive and transmit */
  128. NET_IF_UP,
  129. /** Interface is pointopoint */
  130. NET_IF_POINTOPOINT,
  131. /** Interface is in promiscuous mode */
  132. NET_IF_PROMISC,
  133. /** Do not start the interface immediately after initialization.
  134. * This requires that either the device driver or some other entity
  135. * will need to manually take the interface up when needed.
  136. * For example for Ethernet this will happen when the driver calls
  137. * the net_eth_carrier_on() function.
  138. */
  139. NET_IF_NO_AUTO_START,
  140. /** Power management specific: interface is being suspended */
  141. NET_IF_SUSPENDED,
  142. /** Flag defines if received multicasts of other interface are
  143. * forwarded on this interface. This activates multicast
  144. * routing / forwarding for this interface.
  145. */
  146. NET_IF_FORWARD_MULTICASTS,
  147. /** Interface supports IPv4 */
  148. NET_IF_IPV4,
  149. /** Interface supports IPv6 */
  150. NET_IF_IPV6,
  151. /** @cond INTERNAL_HIDDEN */
  152. /* Total number of flags - must be at the end of the enum */
  153. NET_IF_NUM_FLAGS
  154. /** @endcond */
  155. };
  156. #if defined(CONFIG_NET_OFFLOAD)
  157. struct net_offload;
  158. #endif /* CONFIG_NET_OFFLOAD */
  159. /** @cond INTERNAL_HIDDEN */
  160. #if defined(CONFIG_NET_NATIVE_IPV6)
  161. #define NET_IF_MAX_IPV6_ADDR CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT
  162. #define NET_IF_MAX_IPV6_MADDR CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT
  163. #define NET_IF_MAX_IPV6_PREFIX CONFIG_NET_IF_IPV6_PREFIX_COUNT
  164. #else
  165. #define NET_IF_MAX_IPV6_ADDR 0
  166. #define NET_IF_MAX_IPV6_MADDR 0
  167. #define NET_IF_MAX_IPV6_PREFIX 0
  168. #endif
  169. /* @endcond */
  170. struct net_if_ipv6 {
  171. /** Unicast IP addresses */
  172. struct net_if_addr unicast[NET_IF_MAX_IPV6_ADDR];
  173. /** Multicast IP addresses */
  174. struct net_if_mcast_addr mcast[NET_IF_MAX_IPV6_MADDR];
  175. /** Prefixes */
  176. struct net_if_ipv6_prefix prefix[NET_IF_MAX_IPV6_PREFIX];
  177. /** Default reachable time (RFC 4861, page 52) */
  178. uint32_t base_reachable_time;
  179. /** Reachable time (RFC 4861, page 20) */
  180. uint32_t reachable_time;
  181. /** Retransmit timer (RFC 4861, page 52) */
  182. uint32_t retrans_timer;
  183. #if defined(CONFIG_NET_IPV6_ND) && defined(CONFIG_NET_NATIVE_IPV6)
  184. /** Router solicitation timer node */
  185. sys_snode_t rs_node;
  186. /* RS start time */
  187. uint32_t rs_start;
  188. /** RS count */
  189. uint8_t rs_count;
  190. #endif
  191. /** IPv6 hop limit */
  192. uint8_t hop_limit;
  193. };
  194. /** @cond INTERNAL_HIDDEN */
  195. #if defined(CONFIG_NET_NATIVE_IPV4)
  196. #define NET_IF_MAX_IPV4_ADDR CONFIG_NET_IF_UNICAST_IPV4_ADDR_COUNT
  197. #define NET_IF_MAX_IPV4_MADDR CONFIG_NET_IF_MCAST_IPV4_ADDR_COUNT
  198. #else
  199. #define NET_IF_MAX_IPV4_ADDR 0
  200. #define NET_IF_MAX_IPV4_MADDR 0
  201. #endif
  202. /** @endcond */
  203. struct net_if_ipv4 {
  204. /** Unicast IP addresses */
  205. struct net_if_addr unicast[NET_IF_MAX_IPV4_ADDR];
  206. /** Multicast IP addresses */
  207. struct net_if_mcast_addr mcast[NET_IF_MAX_IPV4_MADDR];
  208. /** Gateway */
  209. struct in_addr gw;
  210. /** Netmask */
  211. struct in_addr netmask;
  212. /** IPv4 time-to-live */
  213. uint8_t ttl;
  214. };
  215. #if defined(CONFIG_NET_DHCPV4) && defined(CONFIG_NET_NATIVE_IPV4)
  216. struct net_if_dhcpv4 {
  217. /** Used for timer lists */
  218. sys_snode_t node;
  219. /** Timer start */
  220. int64_t timer_start;
  221. /** Time for INIT, DISCOVER, REQUESTING, RENEWAL */
  222. uint32_t request_time;
  223. uint32_t xid;
  224. /** IP address Lease time */
  225. uint32_t lease_time;
  226. /** IP address Renewal time */
  227. uint32_t renewal_time;
  228. /** IP address Rebinding time */
  229. uint32_t rebinding_time;
  230. /** Server ID */
  231. struct in_addr server_id;
  232. /** Requested IP addr */
  233. struct in_addr requested_ip;
  234. /**
  235. * DHCPv4 client state in the process of network
  236. * address allocation.
  237. */
  238. enum net_dhcpv4_state state;
  239. /** Number of attempts made for REQUEST and RENEWAL messages */
  240. uint8_t attempts;
  241. };
  242. #endif /* CONFIG_NET_DHCPV4 */
  243. #if defined(CONFIG_NET_IPV4_AUTO) && defined(CONFIG_NET_NATIVE_IPV4)
  244. struct net_if_ipv4_autoconf {
  245. /** Used for timer lists */
  246. sys_snode_t node;
  247. /** Backpointer to correct network interface */
  248. struct net_if *iface;
  249. /** Timer start */
  250. int64_t timer_start;
  251. /** Time for INIT, DISCOVER, REQUESTING, RENEWAL */
  252. uint32_t timer_timeout;
  253. /** Current IP addr */
  254. struct in_addr current_ip;
  255. /** Requested IP addr */
  256. struct in_addr requested_ip;
  257. /** IPV4 Autoconf state in the process of network address allocation.
  258. */
  259. enum net_ipv4_autoconf_state state;
  260. /** Number of sent probe requests */
  261. uint8_t probe_cnt;
  262. /** Number of sent announcements */
  263. uint8_t announce_cnt;
  264. /** Incoming conflict count */
  265. uint8_t conflict_cnt;
  266. };
  267. #endif /* CONFIG_NET_IPV4_AUTO */
  268. /** @cond INTERNAL_HIDDEN */
  269. /* We always need to have at least one IP config */
  270. #define NET_IF_MAX_CONFIGS 1
  271. /** @endcond */
  272. /**
  273. * @brief Network interface IP address configuration.
  274. */
  275. struct net_if_ip {
  276. #if defined(CONFIG_NET_NATIVE_IPV6)
  277. struct net_if_ipv6 *ipv6;
  278. #endif /* CONFIG_NET_IPV6 */
  279. #if defined(CONFIG_NET_NATIVE_IPV4)
  280. struct net_if_ipv4 *ipv4;
  281. #endif /* CONFIG_NET_IPV4 */
  282. };
  283. /**
  284. * @brief IP and other configuration related data for network interface.
  285. */
  286. struct net_if_config {
  287. /** IP address configuration setting */
  288. struct net_if_ip ip;
  289. #if defined(CONFIG_NET_DHCPV4) && defined(CONFIG_NET_NATIVE_IPV4)
  290. struct net_if_dhcpv4 dhcpv4;
  291. #endif /* CONFIG_NET_DHCPV4 */
  292. #if defined(CONFIG_NET_IPV4_AUTO) && defined(CONFIG_NET_NATIVE_IPV4)
  293. struct net_if_ipv4_autoconf ipv4auto;
  294. #endif /* CONFIG_NET_IPV4_AUTO */
  295. #if defined(CONFIG_NET_L2_VIRTUAL)
  296. /**
  297. * This list keeps track of the virtual network interfaces
  298. * that are attached to this network interface.
  299. */
  300. sys_slist_t virtual_interfaces;
  301. #endif /* CONFIG_NET_L2_VIRTUAL */
  302. };
  303. /**
  304. * @brief Network traffic class.
  305. *
  306. * Traffic classes are used when sending or receiving data that is classified
  307. * with different priorities. So some traffic can be marked as high priority
  308. * and it will be sent or received first. Each network packet that is
  309. * transmitted or received goes through a fifo to a thread that will transmit
  310. * it.
  311. */
  312. struct net_traffic_class {
  313. /** Fifo for handling this Tx or Rx packet */
  314. struct k_fifo fifo;
  315. /** Traffic class handler thread */
  316. struct k_thread handler;
  317. /** Stack for this handler */
  318. k_thread_stack_t *stack;
  319. };
  320. /**
  321. * @brief Network Interface Device structure
  322. *
  323. * Used to handle a network interface on top of a device driver instance.
  324. * There can be many net_if_dev instance against the same device.
  325. *
  326. * Such interface is mainly to be used by the link layer, but is also tight
  327. * to a network context: it then makes the relation with a network context
  328. * and the network device.
  329. *
  330. * Because of the strong relationship between a device driver and such
  331. * network interface, each net_if_dev should be instantiated by
  332. */
  333. struct net_if_dev {
  334. /** The actually device driver instance the net_if is related to */
  335. const struct device *dev;
  336. /** Interface's L2 layer */
  337. const struct net_l2 * const l2;
  338. /** Interface's private L2 data pointer */
  339. void *l2_data;
  340. /* For internal use */
  341. ATOMIC_DEFINE(flags, NET_IF_NUM_FLAGS);
  342. /** The hardware link address */
  343. struct net_linkaddr link_addr;
  344. #if defined(CONFIG_NET_OFFLOAD)
  345. /** TCP/IP Offload functions.
  346. * If non-NULL, then the TCP/IP stack is located
  347. * in the communication chip that is accessed via this
  348. * network interface.
  349. */
  350. struct net_offload *offload;
  351. #endif /* CONFIG_NET_OFFLOAD */
  352. /** The hardware MTU */
  353. uint16_t mtu;
  354. #if defined(CONFIG_NET_SOCKETS_OFFLOAD)
  355. /** Indicate whether interface is offloaded at socket level. */
  356. bool offloaded;
  357. #endif /* CONFIG_NET_SOCKETS_OFFLOAD */
  358. };
  359. /**
  360. * @brief Network Interface structure
  361. *
  362. * Used to handle a network interface on top of a net_if_dev instance.
  363. * There can be many net_if instance against the same net_if_dev instance.
  364. *
  365. */
  366. struct net_if {
  367. /** The net_if_dev instance the net_if is related to */
  368. struct net_if_dev *if_dev;
  369. #if defined(CONFIG_NET_STATISTICS_PER_INTERFACE)
  370. /** Network statistics related to this network interface */
  371. struct net_stats stats;
  372. #endif /* CONFIG_NET_STATISTICS_PER_INTERFACE */
  373. /** Network interface instance configuration */
  374. struct net_if_config config;
  375. #if defined(CONFIG_NET_POWER_MANAGEMENT)
  376. /** Keep track of packets pending in traffic queues. This is
  377. * needed to avoid putting network device driver to sleep if
  378. * there are packets waiting to be sent.
  379. */
  380. int tx_pending;
  381. #endif
  382. };
  383. /**
  384. * @brief Set a value in network interface flags
  385. *
  386. * @param iface Pointer to network interface
  387. * @param value Flag value
  388. */
  389. static inline void net_if_flag_set(struct net_if *iface,
  390. enum net_if_flag value)
  391. {
  392. NET_ASSERT(iface);
  393. atomic_set_bit(iface->if_dev->flags, value);
  394. }
  395. /**
  396. * @brief Test and set a value in network interface flags
  397. *
  398. * @param iface Pointer to network interface
  399. * @param value Flag value
  400. *
  401. * @return true if the bit was set, false if it wasn't.
  402. */
  403. static inline bool net_if_flag_test_and_set(struct net_if *iface,
  404. enum net_if_flag value)
  405. {
  406. NET_ASSERT(iface);
  407. return atomic_test_and_set_bit(iface->if_dev->flags, value);
  408. }
  409. /**
  410. * @brief Clear a value in network interface flags
  411. *
  412. * @param iface Pointer to network interface
  413. * @param value Flag value
  414. */
  415. static inline void net_if_flag_clear(struct net_if *iface,
  416. enum net_if_flag value)
  417. {
  418. NET_ASSERT(iface);
  419. atomic_clear_bit(iface->if_dev->flags, value);
  420. }
  421. /**
  422. * @brief Check if a value in network interface flags is set
  423. *
  424. * @param iface Pointer to network interface
  425. * @param value Flag value
  426. *
  427. * @return True if the value is set, false otherwise
  428. */
  429. static inline bool net_if_flag_is_set(struct net_if *iface,
  430. enum net_if_flag value)
  431. {
  432. if (iface == NULL) {
  433. return false;
  434. }
  435. return atomic_test_bit(iface->if_dev->flags, value);
  436. }
  437. /**
  438. * @brief Send a packet through a net iface
  439. *
  440. * @param iface Pointer to a network interface structure
  441. * @param pkt Pointer to a net packet to send
  442. *
  443. * return verdict about the packet
  444. */
  445. enum net_verdict net_if_send_data(struct net_if *iface, struct net_pkt *pkt);
  446. /**
  447. * @brief Get a pointer to the interface L2
  448. *
  449. * @param iface a valid pointer to a network interface structure
  450. *
  451. * @return a pointer to the iface L2
  452. */
  453. static inline const struct net_l2 * const net_if_l2(struct net_if *iface)
  454. {
  455. if (!iface || !iface->if_dev) {
  456. return NULL;
  457. }
  458. return iface->if_dev->l2;
  459. }
  460. /**
  461. * @brief Input a packet through a net iface
  462. *
  463. * @param iface Pointer to a network interface structure
  464. * @param pkt Pointer to a net packet to input
  465. *
  466. * @return verdict about the packet
  467. */
  468. enum net_verdict net_if_recv_data(struct net_if *iface, struct net_pkt *pkt);
  469. /**
  470. * @brief Get a pointer to the interface L2 private data
  471. *
  472. * @param iface a valid pointer to a network interface structure
  473. *
  474. * @return a pointer to the iface L2 data
  475. */
  476. static inline void *net_if_l2_data(struct net_if *iface)
  477. {
  478. return iface->if_dev->l2_data;
  479. }
  480. /**
  481. * @brief Get an network interface's device
  482. *
  483. * @param iface Pointer to a network interface structure
  484. *
  485. * @return a pointer to the device driver instance
  486. */
  487. static inline const struct device *net_if_get_device(struct net_if *iface)
  488. {
  489. return iface->if_dev->dev;
  490. }
  491. /**
  492. * @brief Queue a packet to the net interface TX queue
  493. *
  494. * @param iface Pointer to a network interface structure
  495. * @param pkt Pointer to a net packet to queue
  496. */
  497. void net_if_queue_tx(struct net_if *iface, struct net_pkt *pkt);
  498. /**
  499. * @brief Return the IP offload status
  500. *
  501. * @param iface Network interface
  502. *
  503. * @return True if IP offlining is active, false otherwise.
  504. */
  505. static inline bool net_if_is_ip_offloaded(struct net_if *iface)
  506. {
  507. #if defined(CONFIG_NET_OFFLOAD)
  508. return (iface->if_dev->offload != NULL);
  509. #else
  510. ARG_UNUSED(iface);
  511. return false;
  512. #endif
  513. }
  514. /**
  515. * @brief Return the IP offload plugin
  516. *
  517. * @param iface Network interface
  518. *
  519. * @return NULL if there is no offload plugin defined, valid pointer otherwise
  520. */
  521. static inline struct net_offload *net_if_offload(struct net_if *iface)
  522. {
  523. #if defined(CONFIG_NET_OFFLOAD)
  524. return iface->if_dev->offload;
  525. #else
  526. ARG_UNUSED(iface);
  527. return NULL;
  528. #endif
  529. }
  530. /**
  531. * @brief Return the socket offload status
  532. *
  533. * @param iface Network interface
  534. *
  535. * @return True if socket offloading is active, false otherwise.
  536. */
  537. static inline bool net_if_is_socket_offloaded(struct net_if *iface)
  538. {
  539. #if defined(CONFIG_NET_SOCKETS_OFFLOAD)
  540. return iface->if_dev->offloaded;
  541. #else
  542. ARG_UNUSED(iface);
  543. return false;
  544. #endif
  545. }
  546. /**
  547. * @brief Get an network interface's link address
  548. *
  549. * @param iface Pointer to a network interface structure
  550. *
  551. * @return a pointer to the network link address
  552. */
  553. static inline struct net_linkaddr *net_if_get_link_addr(struct net_if *iface)
  554. {
  555. return &iface->if_dev->link_addr;
  556. }
  557. /**
  558. * @brief Return network configuration for this network interface
  559. *
  560. * @param iface Pointer to a network interface structure
  561. *
  562. * @return Pointer to configuration
  563. */
  564. static inline struct net_if_config *net_if_get_config(struct net_if *iface)
  565. {
  566. return &iface->config;
  567. }
  568. /**
  569. * @brief Start duplicate address detection procedure.
  570. *
  571. * @param iface Pointer to a network interface structure
  572. */
  573. #if defined(CONFIG_NET_IPV6_DAD) && defined(CONFIG_NET_NATIVE_IPV6)
  574. void net_if_start_dad(struct net_if *iface);
  575. #else
  576. static inline void net_if_start_dad(struct net_if *iface)
  577. {
  578. ARG_UNUSED(iface);
  579. }
  580. #endif
  581. /**
  582. * @brief Start neighbor discovery and send router solicitation message.
  583. *
  584. * @param iface Pointer to a network interface structure
  585. */
  586. void net_if_start_rs(struct net_if *iface);
  587. /**
  588. * @brief Stop neighbor discovery.
  589. *
  590. * @param iface Pointer to a network interface structure
  591. */
  592. #if defined(CONFIG_NET_IPV6_ND) && defined(CONFIG_NET_NATIVE_IPV6)
  593. void net_if_stop_rs(struct net_if *iface);
  594. #else
  595. static inline void net_if_stop_rs(struct net_if *iface)
  596. {
  597. ARG_UNUSED(iface);
  598. }
  599. #endif /* CONFIG_NET_IPV6_ND */
  600. /** @cond INTERNAL_HIDDEN */
  601. static inline int net_if_set_link_addr_unlocked(struct net_if *iface,
  602. uint8_t *addr, uint8_t len,
  603. enum net_link_type type)
  604. {
  605. if (net_if_flag_is_set(iface, NET_IF_UP)) {
  606. return -EPERM;
  607. }
  608. net_if_get_link_addr(iface)->addr = addr;
  609. net_if_get_link_addr(iface)->len = len;
  610. net_if_get_link_addr(iface)->type = type;
  611. net_hostname_set_postfix(addr, len);
  612. return 0;
  613. }
  614. int net_if_set_link_addr_locked(struct net_if *iface,
  615. uint8_t *addr, uint8_t len,
  616. enum net_link_type type);
  617. /** @endcond */
  618. /**
  619. * @brief Set a network interface's link address
  620. *
  621. * @param iface Pointer to a network interface structure
  622. * @param addr A pointer to a uint8_t buffer representing the address.
  623. * The buffer must remain valid throughout interface lifetime.
  624. * @param len length of the address buffer
  625. * @param type network bearer type of this link address
  626. *
  627. * @return 0 on success
  628. */
  629. static inline int net_if_set_link_addr(struct net_if *iface,
  630. uint8_t *addr, uint8_t len,
  631. enum net_link_type type)
  632. {
  633. #if defined(CONFIG_NET_RAW_MODE)
  634. return net_if_set_link_addr_unlocked(iface, addr, len, type);
  635. #else
  636. return net_if_set_link_addr_locked(iface, addr, len, type);
  637. #endif
  638. }
  639. /**
  640. * @brief Get an network interface's MTU
  641. *
  642. * @param iface Pointer to a network interface structure
  643. *
  644. * @return the MTU
  645. */
  646. static inline uint16_t net_if_get_mtu(struct net_if *iface)
  647. {
  648. if (iface == NULL) {
  649. return 0U;
  650. }
  651. return iface->if_dev->mtu;
  652. }
  653. /**
  654. * @brief Set an network interface's MTU
  655. *
  656. * @param iface Pointer to a network interface structure
  657. * @param mtu New MTU, note that we store only 16 bit mtu value.
  658. */
  659. static inline void net_if_set_mtu(struct net_if *iface,
  660. uint16_t mtu)
  661. {
  662. if (iface == NULL) {
  663. return;
  664. }
  665. iface->if_dev->mtu = mtu;
  666. }
  667. /**
  668. * @brief Set the infinite status of the network interface address
  669. *
  670. * @param ifaddr IP address for network interface
  671. * @param is_infinite Infinite status
  672. */
  673. static inline void net_if_addr_set_lf(struct net_if_addr *ifaddr,
  674. bool is_infinite)
  675. {
  676. ifaddr->is_infinite = is_infinite;
  677. }
  678. /**
  679. * @brief Get an interface according to link layer address.
  680. *
  681. * @param ll_addr Link layer address.
  682. *
  683. * @return Network interface or NULL if not found.
  684. */
  685. struct net_if *net_if_get_by_link_addr(struct net_linkaddr *ll_addr);
  686. /**
  687. * @brief Find an interface from it's related device
  688. *
  689. * @param dev A valid struct device pointer to relate with an interface
  690. *
  691. * @return a valid struct net_if pointer on success, NULL otherwise
  692. */
  693. struct net_if *net_if_lookup_by_dev(const struct device *dev);
  694. /**
  695. * @brief Get network interface IP config
  696. *
  697. * @param iface Interface to use.
  698. *
  699. * @return NULL if not found or pointer to correct config settings.
  700. */
  701. static inline struct net_if_config *net_if_config_get(struct net_if *iface)
  702. {
  703. return &iface->config;
  704. }
  705. /**
  706. * @brief Remove a router from the system
  707. *
  708. * @param router Pointer to existing router
  709. */
  710. void net_if_router_rm(struct net_if_router *router);
  711. /**
  712. * @brief Get the default network interface.
  713. *
  714. * @return Default interface or NULL if no interfaces are configured.
  715. */
  716. struct net_if *net_if_get_default(void);
  717. /**
  718. * @brief Get the first network interface according to its type.
  719. *
  720. * @param l2 Layer 2 type of the network interface.
  721. *
  722. * @return First network interface of a given type or NULL if no such
  723. * interfaces was found.
  724. */
  725. struct net_if *net_if_get_first_by_type(const struct net_l2 *l2);
  726. #if defined(CONFIG_NET_L2_IEEE802154)
  727. /**
  728. * @brief Get the first IEEE 802.15.4 network interface.
  729. *
  730. * @return First IEEE 802.15.4 network interface or NULL if no such
  731. * interfaces was found.
  732. */
  733. static inline struct net_if *net_if_get_ieee802154(void)
  734. {
  735. return net_if_get_first_by_type(&NET_L2_GET_NAME(IEEE802154));
  736. }
  737. #endif /* CONFIG_NET_L2_IEEE802154 */
  738. /**
  739. * @brief Allocate network interface IPv6 config.
  740. *
  741. * @details This function will allocate new IPv6 config.
  742. *
  743. * @param iface Interface to use.
  744. * @param ipv6 Pointer to allocated IPv6 struct is returned to caller.
  745. *
  746. * @return 0 if ok, <0 if error
  747. */
  748. int net_if_config_ipv6_get(struct net_if *iface,
  749. struct net_if_ipv6 **ipv6);
  750. /**
  751. * @brief Release network interface IPv6 config.
  752. *
  753. * @param iface Interface to use.
  754. *
  755. * @return 0 if ok, <0 if error
  756. */
  757. int net_if_config_ipv6_put(struct net_if *iface);
  758. /**
  759. * @brief Check if this IPv6 address belongs to one of the interfaces.
  760. *
  761. * @param addr IPv6 address
  762. * @param iface Pointer to interface is returned
  763. *
  764. * @return Pointer to interface address, NULL if not found.
  765. */
  766. struct net_if_addr *net_if_ipv6_addr_lookup(const struct in6_addr *addr,
  767. struct net_if **iface);
  768. /**
  769. * @brief Check if this IPv6 address belongs to this specific interfaces.
  770. *
  771. * @param iface Network interface
  772. * @param addr IPv6 address
  773. *
  774. * @return Pointer to interface address, NULL if not found.
  775. */
  776. struct net_if_addr *net_if_ipv6_addr_lookup_by_iface(struct net_if *iface,
  777. struct in6_addr *addr);
  778. /**
  779. * @brief Check if this IPv6 address belongs to one of the interface indices.
  780. *
  781. * @param addr IPv6 address
  782. *
  783. * @return >0 if address was found in given network interface index,
  784. * all other values mean address was not found
  785. */
  786. __syscall int net_if_ipv6_addr_lookup_by_index(const struct in6_addr *addr);
  787. /**
  788. * @brief Add a IPv6 address to an interface
  789. *
  790. * @param iface Network interface
  791. * @param addr IPv6 address
  792. * @param addr_type IPv6 address type
  793. * @param vlifetime Validity time for this address
  794. *
  795. * @return Pointer to interface address, NULL if cannot be added
  796. */
  797. struct net_if_addr *net_if_ipv6_addr_add(struct net_if *iface,
  798. struct in6_addr *addr,
  799. enum net_addr_type addr_type,
  800. uint32_t vlifetime);
  801. /**
  802. * @brief Add a IPv6 address to an interface by index
  803. *
  804. * @param index Network interface index
  805. * @param addr IPv6 address
  806. * @param addr_type IPv6 address type
  807. * @param vlifetime Validity time for this address
  808. *
  809. * @return True if ok, false if address could not be added
  810. */
  811. __syscall bool net_if_ipv6_addr_add_by_index(int index,
  812. struct in6_addr *addr,
  813. enum net_addr_type addr_type,
  814. uint32_t vlifetime);
  815. /**
  816. * @brief Update validity lifetime time of an IPv6 address.
  817. *
  818. * @param ifaddr Network IPv6 address
  819. * @param vlifetime Validity time for this address
  820. */
  821. void net_if_ipv6_addr_update_lifetime(struct net_if_addr *ifaddr,
  822. uint32_t vlifetime);
  823. /**
  824. * @brief Remove an IPv6 address from an interface
  825. *
  826. * @param iface Network interface
  827. * @param addr IPv6 address
  828. *
  829. * @return True if successfully removed, false otherwise
  830. */
  831. bool net_if_ipv6_addr_rm(struct net_if *iface, const struct in6_addr *addr);
  832. /**
  833. * @brief Remove an IPv6 address from an interface by index
  834. *
  835. * @param index Network interface index
  836. * @param addr IPv6 address
  837. *
  838. * @return True if successfully removed, false otherwise
  839. */
  840. __syscall bool net_if_ipv6_addr_rm_by_index(int index,
  841. const struct in6_addr *addr);
  842. /**
  843. * @brief Add a IPv6 multicast address to an interface
  844. *
  845. * @param iface Network interface
  846. * @param addr IPv6 multicast address
  847. *
  848. * @return Pointer to interface multicast address, NULL if cannot be added
  849. */
  850. struct net_if_mcast_addr *net_if_ipv6_maddr_add(struct net_if *iface,
  851. const struct in6_addr *addr);
  852. /**
  853. * @brief Remove an IPv6 multicast address from an interface
  854. *
  855. * @param iface Network interface
  856. * @param addr IPv6 multicast address
  857. *
  858. * @return True if successfully removed, false otherwise
  859. */
  860. bool net_if_ipv6_maddr_rm(struct net_if *iface, const struct in6_addr *addr);
  861. /**
  862. * @brief Check if this IPv6 multicast address belongs to a specific interface
  863. * or one of the interfaces.
  864. *
  865. * @param addr IPv6 address
  866. * @param iface If *iface is null, then pointer to interface is returned,
  867. * otherwise the *iface value needs to be matched.
  868. *
  869. * @return Pointer to interface multicast address, NULL if not found.
  870. */
  871. struct net_if_mcast_addr *net_if_ipv6_maddr_lookup(const struct in6_addr *addr,
  872. struct net_if **iface);
  873. /**
  874. * @typedef net_if_mcast_callback_t
  875. * @brief Define callback that is called whenever IPv6 multicast address group
  876. * is joined or left.
  877. * @param iface A pointer to a struct net_if to which the multicast address is
  878. * attached.
  879. * @param addr IPv6 multicast address.
  880. * @param is_joined True if the address is joined, false if left.
  881. */
  882. typedef void (*net_if_mcast_callback_t)(struct net_if *iface,
  883. const struct in6_addr *addr,
  884. bool is_joined);
  885. /**
  886. * @brief Multicast monitor handler struct.
  887. *
  888. * Stores the multicast callback information. Caller must make sure that
  889. * the variable pointed by this is valid during the lifetime of
  890. * registration. Typically this means that the variable cannot be
  891. * allocated from stack.
  892. */
  893. struct net_if_mcast_monitor {
  894. /** Node information for the slist. */
  895. sys_snode_t node;
  896. /** Network interface */
  897. struct net_if *iface;
  898. /** Multicast callback */
  899. net_if_mcast_callback_t cb;
  900. };
  901. /**
  902. * @brief Register a multicast monitor
  903. *
  904. * @param mon Monitor handle. This is a pointer to a monitor storage structure
  905. * which should be allocated by caller, but does not need to be initialized.
  906. * @param iface Network interface
  907. * @param cb Monitor callback
  908. */
  909. void net_if_mcast_mon_register(struct net_if_mcast_monitor *mon,
  910. struct net_if *iface,
  911. net_if_mcast_callback_t cb);
  912. /**
  913. * @brief Unregister a multicast monitor
  914. *
  915. * @param mon Monitor handle
  916. */
  917. void net_if_mcast_mon_unregister(struct net_if_mcast_monitor *mon);
  918. /**
  919. * @brief Call registered multicast monitors
  920. *
  921. * @param iface Network interface
  922. * @param addr Multicast address
  923. * @param is_joined Is this multicast address joined (true) or not (false)
  924. */
  925. void net_if_mcast_monitor(struct net_if *iface, const struct in6_addr *addr,
  926. bool is_joined);
  927. /**
  928. * @brief Mark a given multicast address to be joined.
  929. *
  930. * @param addr IPv6 multicast address
  931. */
  932. void net_if_ipv6_maddr_join(struct net_if_mcast_addr *addr);
  933. /**
  934. * @brief Check if given multicast address is joined or not.
  935. *
  936. * @param addr IPv6 multicast address
  937. *
  938. * @return True if address is joined, False otherwise.
  939. */
  940. static inline bool net_if_ipv6_maddr_is_joined(struct net_if_mcast_addr *addr)
  941. {
  942. NET_ASSERT(addr);
  943. return addr->is_joined;
  944. }
  945. /**
  946. * @brief Mark a given multicast address to be left.
  947. *
  948. * @param addr IPv6 multicast address
  949. */
  950. void net_if_ipv6_maddr_leave(struct net_if_mcast_addr *addr);
  951. /**
  952. * @brief Return prefix that corresponds to this IPv6 address.
  953. *
  954. * @param iface Network interface
  955. * @param addr IPv6 address
  956. *
  957. * @return Pointer to prefix, NULL if not found.
  958. */
  959. struct net_if_ipv6_prefix *net_if_ipv6_prefix_get(struct net_if *iface,
  960. struct in6_addr *addr);
  961. /**
  962. * @brief Check if this IPv6 prefix belongs to this interface
  963. *
  964. * @param iface Network interface
  965. * @param addr IPv6 address
  966. * @param len Prefix length
  967. *
  968. * @return Pointer to prefix, NULL if not found.
  969. */
  970. struct net_if_ipv6_prefix *net_if_ipv6_prefix_lookup(struct net_if *iface,
  971. struct in6_addr *addr,
  972. uint8_t len);
  973. /**
  974. * @brief Add a IPv6 prefix to an network interface.
  975. *
  976. * @param iface Network interface
  977. * @param prefix IPv6 address
  978. * @param len Prefix length
  979. * @param lifetime Prefix lifetime in seconds
  980. *
  981. * @return Pointer to prefix, NULL if the prefix was not added.
  982. */
  983. struct net_if_ipv6_prefix *net_if_ipv6_prefix_add(struct net_if *iface,
  984. struct in6_addr *prefix,
  985. uint8_t len,
  986. uint32_t lifetime);
  987. /**
  988. * @brief Remove an IPv6 prefix from an interface
  989. *
  990. * @param iface Network interface
  991. * @param addr IPv6 prefix address
  992. * @param len Prefix length
  993. *
  994. * @return True if successfully removed, false otherwise
  995. */
  996. bool net_if_ipv6_prefix_rm(struct net_if *iface, struct in6_addr *addr,
  997. uint8_t len);
  998. /**
  999. * @brief Set the infinite status of the prefix
  1000. *
  1001. * @param prefix IPv6 address
  1002. * @param is_infinite Infinite status
  1003. */
  1004. static inline void net_if_ipv6_prefix_set_lf(struct net_if_ipv6_prefix *prefix,
  1005. bool is_infinite)
  1006. {
  1007. prefix->is_infinite = is_infinite;
  1008. }
  1009. /**
  1010. * @brief Set the prefix lifetime timer.
  1011. *
  1012. * @param prefix IPv6 address
  1013. * @param lifetime Prefix lifetime in seconds
  1014. */
  1015. void net_if_ipv6_prefix_set_timer(struct net_if_ipv6_prefix *prefix,
  1016. uint32_t lifetime);
  1017. /**
  1018. * @brief Unset the prefix lifetime timer.
  1019. *
  1020. * @param prefix IPv6 address
  1021. */
  1022. void net_if_ipv6_prefix_unset_timer(struct net_if_ipv6_prefix *prefix);
  1023. /**
  1024. * @brief Check if this IPv6 address is part of the subnet of our
  1025. * network interface.
  1026. *
  1027. * @param iface Network interface. This is returned to the caller.
  1028. * The iface can be NULL in which case we check all the interfaces.
  1029. * @param addr IPv6 address
  1030. *
  1031. * @return True if address is part of our subnet, false otherwise
  1032. */
  1033. bool net_if_ipv6_addr_onlink(struct net_if **iface, struct in6_addr *addr);
  1034. /**
  1035. * @brief Get the IPv6 address of the given router
  1036. * @param router a network router
  1037. *
  1038. * @return pointer to the IPv6 address, or NULL if none
  1039. */
  1040. #if defined(CONFIG_NET_NATIVE_IPV6)
  1041. static inline struct in6_addr *net_if_router_ipv6(struct net_if_router *router)
  1042. {
  1043. return &router->address.in6_addr;
  1044. }
  1045. #else
  1046. static inline struct in6_addr *net_if_router_ipv6(struct net_if_router *router)
  1047. {
  1048. static struct in6_addr addr;
  1049. ARG_UNUSED(router);
  1050. return &addr;
  1051. }
  1052. #endif
  1053. /**
  1054. * @brief Check if IPv6 address is one of the routers configured
  1055. * in the system.
  1056. *
  1057. * @param iface Network interface
  1058. * @param addr IPv6 address
  1059. *
  1060. * @return Pointer to router information, NULL if cannot be found
  1061. */
  1062. struct net_if_router *net_if_ipv6_router_lookup(struct net_if *iface,
  1063. struct in6_addr *addr);
  1064. /**
  1065. * @brief Find default router for this IPv6 address.
  1066. *
  1067. * @param iface Network interface. This can be NULL in which case we
  1068. * go through all the network interfaces to find a suitable router.
  1069. * @param addr IPv6 address
  1070. *
  1071. * @return Pointer to router information, NULL if cannot be found
  1072. */
  1073. struct net_if_router *net_if_ipv6_router_find_default(struct net_if *iface,
  1074. struct in6_addr *addr);
  1075. /**
  1076. * @brief Update validity lifetime time of a router.
  1077. *
  1078. * @param router Network IPv6 address
  1079. * @param lifetime Lifetime of this router.
  1080. */
  1081. void net_if_ipv6_router_update_lifetime(struct net_if_router *router,
  1082. uint16_t lifetime);
  1083. /**
  1084. * @brief Add IPv6 router to the system.
  1085. *
  1086. * @param iface Network interface
  1087. * @param addr IPv6 address
  1088. * @param router_lifetime Lifetime of the router
  1089. *
  1090. * @return Pointer to router information, NULL if could not be added
  1091. */
  1092. struct net_if_router *net_if_ipv6_router_add(struct net_if *iface,
  1093. struct in6_addr *addr,
  1094. uint16_t router_lifetime);
  1095. /**
  1096. * @brief Remove IPv6 router from the system.
  1097. *
  1098. * @param router Router information.
  1099. *
  1100. * @return True if successfully removed, false otherwise
  1101. */
  1102. bool net_if_ipv6_router_rm(struct net_if_router *router);
  1103. /**
  1104. * @brief Get IPv6 hop limit specified for a given interface. This is the
  1105. * default value but can be overridden by the user.
  1106. *
  1107. * @param iface Network interface
  1108. *
  1109. * @return Hop limit
  1110. */
  1111. uint8_t net_if_ipv6_get_hop_limit(struct net_if *iface);
  1112. /**
  1113. * @brief Set the default IPv6 hop limit of a given interface.
  1114. *
  1115. * @param iface Network interface
  1116. * @param hop_limit New hop limit
  1117. */
  1118. void net_ipv6_set_hop_limit(struct net_if *iface, uint8_t hop_limit);
  1119. /**
  1120. * @brief Set IPv6 reachable time for a given interface
  1121. *
  1122. * @param iface Network interface
  1123. * @param reachable_time New reachable time
  1124. */
  1125. static inline void net_if_ipv6_set_base_reachable_time(struct net_if *iface,
  1126. uint32_t reachable_time)
  1127. {
  1128. #if defined(CONFIG_NET_NATIVE_IPV6)
  1129. if (!iface->config.ip.ipv6) {
  1130. return;
  1131. }
  1132. iface->config.ip.ipv6->base_reachable_time = reachable_time;
  1133. #endif
  1134. }
  1135. /**
  1136. * @brief Get IPv6 reachable timeout specified for a given interface
  1137. *
  1138. * @param iface Network interface
  1139. *
  1140. * @return Reachable timeout
  1141. */
  1142. static inline uint32_t net_if_ipv6_get_reachable_time(struct net_if *iface)
  1143. {
  1144. #if defined(CONFIG_NET_NATIVE_IPV6)
  1145. if (!iface->config.ip.ipv6) {
  1146. return 0;
  1147. }
  1148. return iface->config.ip.ipv6->reachable_time;
  1149. #else
  1150. return 0;
  1151. #endif
  1152. }
  1153. /**
  1154. * @brief Calculate next reachable time value for IPv6 reachable time
  1155. *
  1156. * @param ipv6 IPv6 address configuration
  1157. *
  1158. * @return Reachable time
  1159. */
  1160. uint32_t net_if_ipv6_calc_reachable_time(struct net_if_ipv6 *ipv6);
  1161. /**
  1162. * @brief Set IPv6 reachable time for a given interface. This requires
  1163. * that base reachable time is set for the interface.
  1164. *
  1165. * @param ipv6 IPv6 address configuration
  1166. */
  1167. static inline void net_if_ipv6_set_reachable_time(struct net_if_ipv6 *ipv6)
  1168. {
  1169. #if defined(CONFIG_NET_NATIVE_IPV6)
  1170. ipv6->reachable_time = net_if_ipv6_calc_reachable_time(ipv6);
  1171. #endif
  1172. }
  1173. /**
  1174. * @brief Set IPv6 retransmit timer for a given interface
  1175. *
  1176. * @param iface Network interface
  1177. * @param retrans_timer New retransmit timer
  1178. */
  1179. static inline void net_if_ipv6_set_retrans_timer(struct net_if *iface,
  1180. uint32_t retrans_timer)
  1181. {
  1182. #if defined(CONFIG_NET_NATIVE_IPV6)
  1183. if (!iface->config.ip.ipv6) {
  1184. return;
  1185. }
  1186. iface->config.ip.ipv6->retrans_timer = retrans_timer;
  1187. #endif
  1188. }
  1189. /**
  1190. * @brief Get IPv6 retransmit timer specified for a given interface
  1191. *
  1192. * @param iface Network interface
  1193. *
  1194. * @return Retransmit timer
  1195. */
  1196. static inline uint32_t net_if_ipv6_get_retrans_timer(struct net_if *iface)
  1197. {
  1198. #if defined(CONFIG_NET_NATIVE_IPV6)
  1199. if (!iface->config.ip.ipv6) {
  1200. return 0;
  1201. }
  1202. return iface->config.ip.ipv6->retrans_timer;
  1203. #else
  1204. return 0;
  1205. #endif
  1206. }
  1207. /**
  1208. * @brief Get a IPv6 source address that should be used when sending
  1209. * network data to destination.
  1210. *
  1211. * @param iface Interface that was used when packet was received.
  1212. * If the interface is not known, then NULL can be given.
  1213. * @param dst IPv6 destination address
  1214. *
  1215. * @return Pointer to IPv6 address to use, NULL if no IPv6 address
  1216. * could be found.
  1217. */
  1218. #if defined(CONFIG_NET_NATIVE_IPV6)
  1219. const struct in6_addr *net_if_ipv6_select_src_addr(struct net_if *iface,
  1220. const struct in6_addr *dst);
  1221. #else
  1222. static inline const struct in6_addr *net_if_ipv6_select_src_addr(
  1223. struct net_if *iface, const struct in6_addr *dst)
  1224. {
  1225. ARG_UNUSED(iface);
  1226. ARG_UNUSED(dst);
  1227. return NULL;
  1228. }
  1229. #endif
  1230. /**
  1231. * @brief Get a network interface that should be used when sending
  1232. * IPv6 network data to destination.
  1233. *
  1234. * @param dst IPv6 destination address
  1235. *
  1236. * @return Pointer to network interface to use, NULL if no suitable interface
  1237. * could be found.
  1238. */
  1239. #if defined(CONFIG_NET_NATIVE_IPV6)
  1240. struct net_if *net_if_ipv6_select_src_iface(const struct in6_addr *dst);
  1241. #else
  1242. static inline struct net_if *net_if_ipv6_select_src_iface(
  1243. const struct in6_addr *dst)
  1244. {
  1245. ARG_UNUSED(dst);
  1246. return NULL;
  1247. }
  1248. #endif
  1249. /**
  1250. * @brief Get a IPv6 link local address in a given state.
  1251. *
  1252. * @param iface Interface to use. Must be a valid pointer to an interface.
  1253. * @param addr_state IPv6 address state (preferred, tentative, deprecated)
  1254. *
  1255. * @return Pointer to link local IPv6 address, NULL if no proper IPv6 address
  1256. * could be found.
  1257. */
  1258. struct in6_addr *net_if_ipv6_get_ll(struct net_if *iface,
  1259. enum net_addr_state addr_state);
  1260. /**
  1261. * @brief Return link local IPv6 address from the first interface that has
  1262. * a link local address matching give state.
  1263. *
  1264. * @param state IPv6 address state (ANY, TENTATIVE, PREFERRED, DEPRECATED)
  1265. * @param iface Pointer to interface is returned
  1266. *
  1267. * @return Pointer to IPv6 address, NULL if not found.
  1268. */
  1269. struct in6_addr *net_if_ipv6_get_ll_addr(enum net_addr_state state,
  1270. struct net_if **iface);
  1271. /**
  1272. * @brief Stop IPv6 Duplicate Address Detection (DAD) procedure if
  1273. * we find out that our IPv6 address is already in use.
  1274. *
  1275. * @param iface Interface where the DAD was running.
  1276. * @param addr IPv6 address that failed DAD
  1277. */
  1278. void net_if_ipv6_dad_failed(struct net_if *iface, const struct in6_addr *addr);
  1279. /**
  1280. * @brief Return global IPv6 address from the first interface that has
  1281. * a global IPv6 address matching the given state.
  1282. *
  1283. * @param state IPv6 address state (ANY, TENTATIVE, PREFERRED, DEPRECATED)
  1284. * @param iface Caller can give an interface to check. If iface is set to NULL,
  1285. * then all the interfaces are checked. Pointer to interface where the IPv6
  1286. * address is defined is returned to the caller.
  1287. *
  1288. * @return Pointer to IPv6 address, NULL if not found.
  1289. */
  1290. struct in6_addr *net_if_ipv6_get_global_addr(enum net_addr_state state,
  1291. struct net_if **iface);
  1292. /**
  1293. * @brief Allocate network interface IPv4 config.
  1294. *
  1295. * @details This function will allocate new IPv4 config.
  1296. *
  1297. * @param iface Interface to use.
  1298. * @param ipv4 Pointer to allocated IPv4 struct is returned to caller.
  1299. *
  1300. * @return 0 if ok, <0 if error
  1301. */
  1302. int net_if_config_ipv4_get(struct net_if *iface,
  1303. struct net_if_ipv4 **ipv4);
  1304. /**
  1305. * @brief Release network interface IPv4 config.
  1306. *
  1307. * @param iface Interface to use.
  1308. *
  1309. * @return 0 if ok, <0 if error
  1310. */
  1311. int net_if_config_ipv4_put(struct net_if *iface);
  1312. /**
  1313. * @brief Get IPv4 time-to-live value specified for a given interface
  1314. *
  1315. * @param iface Network interface
  1316. *
  1317. * @return Time-to-live
  1318. */
  1319. uint8_t net_if_ipv4_get_ttl(struct net_if *iface);
  1320. /**
  1321. * @brief Set IPv4 time-to-live value specified to a given interface
  1322. *
  1323. * @param iface Network interface
  1324. * @param ttl Time-to-live value
  1325. */
  1326. void net_if_ipv4_set_ttl(struct net_if *iface, uint8_t ttl);
  1327. /**
  1328. * @brief Check if this IPv4 address belongs to one of the interfaces.
  1329. *
  1330. * @param addr IPv4 address
  1331. * @param iface Interface is returned
  1332. *
  1333. * @return Pointer to interface address, NULL if not found.
  1334. */
  1335. struct net_if_addr *net_if_ipv4_addr_lookup(const struct in_addr *addr,
  1336. struct net_if **iface);
  1337. /**
  1338. * @brief Add a IPv4 address to an interface
  1339. *
  1340. * @param iface Network interface
  1341. * @param addr IPv4 address
  1342. * @param addr_type IPv4 address type
  1343. * @param vlifetime Validity time for this address
  1344. *
  1345. * @return Pointer to interface address, NULL if cannot be added
  1346. */
  1347. struct net_if_addr *net_if_ipv4_addr_add(struct net_if *iface,
  1348. struct in_addr *addr,
  1349. enum net_addr_type addr_type,
  1350. uint32_t vlifetime);
  1351. /**
  1352. * @brief Remove a IPv4 address from an interface
  1353. *
  1354. * @param iface Network interface
  1355. * @param addr IPv4 address
  1356. *
  1357. * @return True if successfully removed, false otherwise
  1358. */
  1359. bool net_if_ipv4_addr_rm(struct net_if *iface, const struct in_addr *addr);
  1360. /**
  1361. * @brief Check if this IPv4 address belongs to one of the interface indices.
  1362. *
  1363. * @param addr IPv4 address
  1364. *
  1365. * @return >0 if address was found in given network interface index,
  1366. * all other values mean address was not found
  1367. */
  1368. __syscall int net_if_ipv4_addr_lookup_by_index(const struct in_addr *addr);
  1369. /**
  1370. * @brief Add a IPv4 address to an interface by network interface index
  1371. *
  1372. * @param index Network interface index
  1373. * @param addr IPv4 address
  1374. * @param addr_type IPv4 address type
  1375. * @param vlifetime Validity time for this address
  1376. *
  1377. * @return True if ok, false if the address could not be added
  1378. */
  1379. __syscall bool net_if_ipv4_addr_add_by_index(int index,
  1380. struct in_addr *addr,
  1381. enum net_addr_type addr_type,
  1382. uint32_t vlifetime);
  1383. /**
  1384. * @brief Remove a IPv4 address from an interface by interface index
  1385. *
  1386. * @param index Network interface index
  1387. * @param addr IPv4 address
  1388. *
  1389. * @return True if successfully removed, false otherwise
  1390. */
  1391. __syscall bool net_if_ipv4_addr_rm_by_index(int index,
  1392. const struct in_addr *addr);
  1393. /**
  1394. * @brief Add a IPv4 multicast address to an interface
  1395. *
  1396. * @param iface Network interface
  1397. * @param addr IPv4 multicast address
  1398. *
  1399. * @return Pointer to interface multicast address, NULL if cannot be added
  1400. */
  1401. struct net_if_mcast_addr *net_if_ipv4_maddr_add(struct net_if *iface,
  1402. const struct in_addr *addr);
  1403. /**
  1404. * @brief Remove an IPv4 multicast address from an interface
  1405. *
  1406. * @param iface Network interface
  1407. * @param addr IPv4 multicast address
  1408. *
  1409. * @return True if successfully removed, false otherwise
  1410. */
  1411. bool net_if_ipv4_maddr_rm(struct net_if *iface, const struct in_addr *addr);
  1412. /**
  1413. * @brief Check if this IPv4 multicast address belongs to a specific interface
  1414. * or one of the interfaces.
  1415. *
  1416. * @param addr IPv4 address
  1417. * @param iface If *iface is null, then pointer to interface is returned,
  1418. * otherwise the *iface value needs to be matched.
  1419. *
  1420. * @return Pointer to interface multicast address, NULL if not found.
  1421. */
  1422. struct net_if_mcast_addr *net_if_ipv4_maddr_lookup(const struct in_addr *addr,
  1423. struct net_if **iface);
  1424. /**
  1425. * @brief Mark a given multicast address to be joined.
  1426. *
  1427. * @param addr IPv4 multicast address
  1428. */
  1429. void net_if_ipv4_maddr_join(struct net_if_mcast_addr *addr);
  1430. /**
  1431. * @brief Check if given multicast address is joined or not.
  1432. *
  1433. * @param addr IPv4 multicast address
  1434. *
  1435. * @return True if address is joined, False otherwise.
  1436. */
  1437. static inline bool net_if_ipv4_maddr_is_joined(struct net_if_mcast_addr *addr)
  1438. {
  1439. NET_ASSERT(addr);
  1440. return addr->is_joined;
  1441. }
  1442. /**
  1443. * @brief Mark a given multicast address to be left.
  1444. *
  1445. * @param addr IPv4 multicast address
  1446. */
  1447. void net_if_ipv4_maddr_leave(struct net_if_mcast_addr *addr);
  1448. /**
  1449. * @brief Get the IPv4 address of the given router
  1450. * @param router a network router
  1451. *
  1452. * @return pointer to the IPv4 address, or NULL if none
  1453. */
  1454. #if defined(CONFIG_NET_NATIVE_IPV4)
  1455. static inline struct in_addr *net_if_router_ipv4(struct net_if_router *router)
  1456. {
  1457. return &router->address.in_addr;
  1458. }
  1459. #else
  1460. static inline struct in_addr *net_if_router_ipv4(struct net_if_router *router)
  1461. {
  1462. static struct in_addr addr;
  1463. ARG_UNUSED(router);
  1464. return &addr;
  1465. }
  1466. #endif
  1467. /**
  1468. * @brief Check if IPv4 address is one of the routers configured
  1469. * in the system.
  1470. *
  1471. * @param iface Network interface
  1472. * @param addr IPv4 address
  1473. *
  1474. * @return Pointer to router information, NULL if cannot be found
  1475. */
  1476. struct net_if_router *net_if_ipv4_router_lookup(struct net_if *iface,
  1477. struct in_addr *addr);
  1478. /**
  1479. * @brief Find default router for this IPv4 address.
  1480. *
  1481. * @param iface Network interface. This can be NULL in which case we
  1482. * go through all the network interfaces to find a suitable router.
  1483. * @param addr IPv4 address
  1484. *
  1485. * @return Pointer to router information, NULL if cannot be found
  1486. */
  1487. struct net_if_router *net_if_ipv4_router_find_default(struct net_if *iface,
  1488. struct in_addr *addr);
  1489. /**
  1490. * @brief Add IPv4 router to the system.
  1491. *
  1492. * @param iface Network interface
  1493. * @param addr IPv4 address
  1494. * @param is_default Is this router the default one
  1495. * @param router_lifetime Lifetime of the router
  1496. *
  1497. * @return Pointer to router information, NULL if could not be added
  1498. */
  1499. struct net_if_router *net_if_ipv4_router_add(struct net_if *iface,
  1500. struct in_addr *addr,
  1501. bool is_default,
  1502. uint16_t router_lifetime);
  1503. /**
  1504. * @brief Remove IPv4 router from the system.
  1505. *
  1506. * @param router Router information.
  1507. *
  1508. * @return True if successfully removed, false otherwise
  1509. */
  1510. bool net_if_ipv4_router_rm(struct net_if_router *router);
  1511. /**
  1512. * @brief Check if the given IPv4 address belongs to local subnet.
  1513. *
  1514. * @param iface Interface to use. Must be a valid pointer to an interface.
  1515. * @param addr IPv4 address
  1516. *
  1517. * @return True if address is part of local subnet, false otherwise.
  1518. */
  1519. bool net_if_ipv4_addr_mask_cmp(struct net_if *iface,
  1520. const struct in_addr *addr);
  1521. /**
  1522. * @brief Check if the given IPv4 address is a broadcast address.
  1523. *
  1524. * @param iface Interface to use. Must be a valid pointer to an interface.
  1525. * @param addr IPv4 address, this should be in network byte order
  1526. *
  1527. * @return True if address is a broadcast address, false otherwise.
  1528. */
  1529. bool net_if_ipv4_is_addr_bcast(struct net_if *iface,
  1530. const struct in_addr *addr);
  1531. /**
  1532. * @brief Get a network interface that should be used when sending
  1533. * IPv4 network data to destination.
  1534. *
  1535. * @param dst IPv4 destination address
  1536. *
  1537. * @return Pointer to network interface to use, NULL if no suitable interface
  1538. * could be found.
  1539. */
  1540. #if defined(CONFIG_NET_NATIVE_IPV4)
  1541. struct net_if *net_if_ipv4_select_src_iface(const struct in_addr *dst);
  1542. #else
  1543. static inline struct net_if *net_if_ipv4_select_src_iface(
  1544. const struct in_addr *dst)
  1545. {
  1546. ARG_UNUSED(dst);
  1547. return NULL;
  1548. }
  1549. #endif
  1550. /**
  1551. * @brief Get a IPv4 source address that should be used when sending
  1552. * network data to destination.
  1553. *
  1554. * @param iface Interface to use when sending the packet.
  1555. * If the interface is not known, then NULL can be given.
  1556. * @param dst IPv4 destination address
  1557. *
  1558. * @return Pointer to IPv4 address to use, NULL if no IPv4 address
  1559. * could be found.
  1560. */
  1561. #if defined(CONFIG_NET_NATIVE_IPV4)
  1562. const struct in_addr *net_if_ipv4_select_src_addr(struct net_if *iface,
  1563. const struct in_addr *dst);
  1564. #else
  1565. static inline const struct in_addr *net_if_ipv4_select_src_addr(
  1566. struct net_if *iface, const struct in_addr *dst)
  1567. {
  1568. ARG_UNUSED(iface);
  1569. ARG_UNUSED(dst);
  1570. return NULL;
  1571. }
  1572. #endif
  1573. /**
  1574. * @brief Get a IPv4 link local address in a given state.
  1575. *
  1576. * @param iface Interface to use. Must be a valid pointer to an interface.
  1577. * @param addr_state IPv4 address state (preferred, tentative, deprecated)
  1578. *
  1579. * @return Pointer to link local IPv4 address, NULL if no proper IPv4 address
  1580. * could be found.
  1581. */
  1582. struct in_addr *net_if_ipv4_get_ll(struct net_if *iface,
  1583. enum net_addr_state addr_state);
  1584. /**
  1585. * @brief Get a IPv4 global address in a given state.
  1586. *
  1587. * @param iface Interface to use. Must be a valid pointer to an interface.
  1588. * @param addr_state IPv4 address state (preferred, tentative, deprecated)
  1589. *
  1590. * @return Pointer to link local IPv4 address, NULL if no proper IPv4 address
  1591. * could be found.
  1592. */
  1593. struct in_addr *net_if_ipv4_get_global_addr(struct net_if *iface,
  1594. enum net_addr_state addr_state);
  1595. /**
  1596. * @brief Set IPv4 netmask for an interface.
  1597. *
  1598. * @param iface Interface to use.
  1599. * @param netmask IPv4 netmask
  1600. */
  1601. void net_if_ipv4_set_netmask(struct net_if *iface,
  1602. const struct in_addr *netmask);
  1603. /**
  1604. * @brief Set IPv4 netmask for an interface index.
  1605. *
  1606. * @param index Network interface index
  1607. * @param netmask IPv4 netmask
  1608. *
  1609. * @return True if netmask was added, false otherwise.
  1610. */
  1611. __syscall bool net_if_ipv4_set_netmask_by_index(int index,
  1612. const struct in_addr *netmask);
  1613. /**
  1614. * @brief Set IPv4 gateway for an interface.
  1615. *
  1616. * @param iface Interface to use.
  1617. * @param gw IPv4 address of an gateway
  1618. */
  1619. void net_if_ipv4_set_gw(struct net_if *iface, const struct in_addr *gw);
  1620. /**
  1621. * @brief Set IPv4 gateway for an interface index.
  1622. *
  1623. * @param index Network interface index
  1624. * @param gw IPv4 address of an gateway
  1625. *
  1626. * @return True if gateway was added, false otherwise.
  1627. */
  1628. __syscall bool net_if_ipv4_set_gw_by_index(int index, const struct in_addr *gw);
  1629. /**
  1630. * @brief Get a network interface that should be used when sending
  1631. * IPv6 or IPv4 network data to destination.
  1632. *
  1633. * @param dst IPv6 or IPv4 destination address
  1634. *
  1635. * @return Pointer to network interface to use. Note that the function
  1636. * will return the default network interface if the best network interface
  1637. * is not found.
  1638. */
  1639. struct net_if *net_if_select_src_iface(const struct sockaddr *dst);
  1640. /**
  1641. * @typedef net_if_link_callback_t
  1642. * @brief Define callback that is called after a network packet
  1643. * has been sent.
  1644. * @param iface A pointer to a struct net_if to which the the net_pkt was sent to.
  1645. * @param dst Link layer address of the destination where the network packet was sent.
  1646. * @param status Send status, 0 is ok, < 0 error.
  1647. */
  1648. typedef void (*net_if_link_callback_t)(struct net_if *iface,
  1649. struct net_linkaddr *dst,
  1650. int status);
  1651. /**
  1652. * @brief Link callback handler struct.
  1653. *
  1654. * Stores the link callback information. Caller must make sure that
  1655. * the variable pointed by this is valid during the lifetime of
  1656. * registration. Typically this means that the variable cannot be
  1657. * allocated from stack.
  1658. */
  1659. struct net_if_link_cb {
  1660. /** Node information for the slist. */
  1661. sys_snode_t node;
  1662. /** Link callback */
  1663. net_if_link_callback_t cb;
  1664. };
  1665. /**
  1666. * @brief Register a link callback.
  1667. *
  1668. * @param link Caller specified handler for the callback.
  1669. * @param cb Callback to register.
  1670. */
  1671. void net_if_register_link_cb(struct net_if_link_cb *link,
  1672. net_if_link_callback_t cb);
  1673. /**
  1674. * @brief Unregister a link callback.
  1675. *
  1676. * @param link Caller specified handler for the callback.
  1677. */
  1678. void net_if_unregister_link_cb(struct net_if_link_cb *link);
  1679. /**
  1680. * @brief Call a link callback function.
  1681. *
  1682. * @param iface Network interface.
  1683. * @param lladdr Destination link layer address
  1684. * @param status 0 is ok, < 0 error
  1685. */
  1686. void net_if_call_link_cb(struct net_if *iface, struct net_linkaddr *lladdr,
  1687. int status);
  1688. /**
  1689. * @brief Check if received network packet checksum calculation can be avoided
  1690. * or not. For example many ethernet devices support network packet offloading
  1691. * in which case the IP stack does not need to calculate the checksum.
  1692. *
  1693. * @param iface Network interface
  1694. *
  1695. * @return True if checksum needs to be calculated, false otherwise.
  1696. */
  1697. bool net_if_need_calc_rx_checksum(struct net_if *iface);
  1698. /**
  1699. * @brief Check if network packet checksum calculation can be avoided or not
  1700. * when sending the packet. For example many ethernet devices support network
  1701. * packet offloading in which case the IP stack does not need to calculate the
  1702. * checksum.
  1703. *
  1704. * @param iface Network interface
  1705. *
  1706. * @return True if checksum needs to be calculated, false otherwise.
  1707. */
  1708. bool net_if_need_calc_tx_checksum(struct net_if *iface);
  1709. /**
  1710. * @brief Get interface according to index
  1711. *
  1712. * @details This is a syscall only to provide access to the object for purposes
  1713. * of assigning permissions.
  1714. *
  1715. * @param index Interface index
  1716. *
  1717. * @return Pointer to interface or NULL if not found.
  1718. */
  1719. __syscall struct net_if *net_if_get_by_index(int index);
  1720. /**
  1721. * @brief Get interface index according to pointer
  1722. *
  1723. * @param iface Pointer to network interface
  1724. *
  1725. * @return Interface index
  1726. */
  1727. int net_if_get_by_iface(struct net_if *iface);
  1728. /**
  1729. * @typedef net_if_cb_t
  1730. * @brief Callback used while iterating over network interfaces
  1731. *
  1732. * @param iface Pointer to current network interface
  1733. * @param user_data A valid pointer to user data or NULL
  1734. */
  1735. typedef void (*net_if_cb_t)(struct net_if *iface, void *user_data);
  1736. /**
  1737. * @brief Go through all the network interfaces and call callback
  1738. * for each interface.
  1739. *
  1740. * @param cb User-supplied callback function to call
  1741. * @param user_data User specified data
  1742. */
  1743. void net_if_foreach(net_if_cb_t cb, void *user_data);
  1744. /**
  1745. * @brief Bring interface up
  1746. *
  1747. * @param iface Pointer to network interface
  1748. *
  1749. * @return 0 on success
  1750. */
  1751. int net_if_up(struct net_if *iface);
  1752. /**
  1753. * @brief Check if interface is up.
  1754. *
  1755. * @param iface Pointer to network interface
  1756. *
  1757. * @return True if interface is up, False if it is down.
  1758. */
  1759. static inline bool net_if_is_up(struct net_if *iface)
  1760. {
  1761. NET_ASSERT(iface);
  1762. return net_if_flag_is_set(iface, NET_IF_UP);
  1763. }
  1764. /**
  1765. * @brief Bring interface down
  1766. *
  1767. * @param iface Pointer to network interface
  1768. *
  1769. * @return 0 on success
  1770. */
  1771. int net_if_down(struct net_if *iface);
  1772. #if defined(CONFIG_NET_PKT_TIMESTAMP) && defined(CONFIG_NET_NATIVE)
  1773. /**
  1774. * @typedef net_if_timestamp_callback_t
  1775. * @brief Define callback that is called after a network packet
  1776. * has been timestamped.
  1777. * @param "struct net_pkt *pkt" A pointer on a struct net_pkt which has
  1778. * been timestamped after being sent.
  1779. */
  1780. typedef void (*net_if_timestamp_callback_t)(struct net_pkt *pkt);
  1781. /**
  1782. * @brief Timestamp callback handler struct.
  1783. *
  1784. * Stores the timestamp callback information. Caller must make sure that
  1785. * the variable pointed by this is valid during the lifetime of
  1786. * registration. Typically this means that the variable cannot be
  1787. * allocated from stack.
  1788. */
  1789. struct net_if_timestamp_cb {
  1790. /** Node information for the slist. */
  1791. sys_snode_t node;
  1792. /** Packet for which the callback is needed.
  1793. * A NULL value means all packets.
  1794. */
  1795. struct net_pkt *pkt;
  1796. /** Net interface for which the callback is needed.
  1797. * A NULL value means all interfaces.
  1798. */
  1799. struct net_if *iface;
  1800. /** Timestamp callback */
  1801. net_if_timestamp_callback_t cb;
  1802. };
  1803. /**
  1804. * @brief Register a timestamp callback.
  1805. *
  1806. * @param handle Caller specified handler for the callback.
  1807. * @param pkt Net packet for which the callback is registered. NULL for all
  1808. * packets.
  1809. * @param iface Net interface for which the callback is. NULL for all
  1810. * interfaces.
  1811. * @param cb Callback to register.
  1812. */
  1813. void net_if_register_timestamp_cb(struct net_if_timestamp_cb *handle,
  1814. struct net_pkt *pkt,
  1815. struct net_if *iface,
  1816. net_if_timestamp_callback_t cb);
  1817. /**
  1818. * @brief Unregister a timestamp callback.
  1819. *
  1820. * @param handle Caller specified handler for the callback.
  1821. */
  1822. void net_if_unregister_timestamp_cb(struct net_if_timestamp_cb *handle);
  1823. /**
  1824. * @brief Call a timestamp callback function.
  1825. *
  1826. * @param pkt Network buffer.
  1827. */
  1828. void net_if_call_timestamp_cb(struct net_pkt *pkt);
  1829. /*
  1830. * @brief Add timestamped TX buffer to be handled
  1831. *
  1832. * @param pkt Timestamped buffer
  1833. */
  1834. void net_if_add_tx_timestamp(struct net_pkt *pkt);
  1835. #endif /* CONFIG_NET_PKT_TIMESTAMP */
  1836. /**
  1837. * @brief Set network interface into promiscuous mode
  1838. *
  1839. * @details Note that not all network technologies will support this.
  1840. *
  1841. * @param iface Pointer to network interface
  1842. *
  1843. * @return 0 on success, <0 if error
  1844. */
  1845. int net_if_set_promisc(struct net_if *iface);
  1846. /**
  1847. * @brief Set network interface into normal mode
  1848. *
  1849. * @param iface Pointer to network interface
  1850. */
  1851. void net_if_unset_promisc(struct net_if *iface);
  1852. /**
  1853. * @brief Check if promiscuous mode is set or not.
  1854. *
  1855. * @param iface Pointer to network interface
  1856. *
  1857. * @return True if interface is in promisc mode,
  1858. * False if interface is not in in promiscuous mode.
  1859. */
  1860. bool net_if_is_promisc(struct net_if *iface);
  1861. /**
  1862. * @brief Check if there are any pending TX network data for a given network
  1863. * interface.
  1864. *
  1865. * @param iface Pointer to network interface
  1866. *
  1867. * @return True if there are pending TX network packets for this network
  1868. * interface, False otherwise.
  1869. */
  1870. static inline bool net_if_are_pending_tx_packets(struct net_if *iface)
  1871. {
  1872. #if defined(CONFIG_NET_POWER_MANAGEMENT)
  1873. return !!iface->tx_pending;
  1874. #else
  1875. ARG_UNUSED(iface);
  1876. return false;
  1877. #endif
  1878. }
  1879. #ifdef CONFIG_NET_POWER_MANAGEMENT
  1880. /**
  1881. * @brief Suspend a network interface from a power management perspective
  1882. *
  1883. * @param iface Pointer to network interface
  1884. *
  1885. * @return 0 on success, or -EALREADY/-EBUSY as possible errors.
  1886. */
  1887. int net_if_suspend(struct net_if *iface);
  1888. /**
  1889. * @brief Resume a network interface from a power management perspective
  1890. *
  1891. * @param iface Pointer to network interface
  1892. *
  1893. * @return 0 on success, or -EALREADY as a possible error.
  1894. */
  1895. int net_if_resume(struct net_if *iface);
  1896. /**
  1897. * @brief Check if the network interface is suspended or not.
  1898. *
  1899. * @param iface Pointer to network interface
  1900. *
  1901. * @return True if interface is suspended, False otherwise.
  1902. */
  1903. bool net_if_is_suspended(struct net_if *iface);
  1904. #endif /* CONFIG_NET_POWER_MANAGEMENT */
  1905. /** @cond INTERNAL_HIDDEN */
  1906. struct net_if_api {
  1907. void (*init)(struct net_if *iface);
  1908. };
  1909. #if defined(CONFIG_NET_DHCPV4) && defined(CONFIG_NET_NATIVE_IPV4)
  1910. #define NET_IF_DHCPV4_INIT .dhcpv4.state = NET_DHCPV4_DISABLED,
  1911. #else
  1912. #define NET_IF_DHCPV4_INIT
  1913. #endif
  1914. #define NET_IF_CONFIG_INIT \
  1915. .config = { \
  1916. .ip = { \
  1917. }, \
  1918. NET_IF_DHCPV4_INIT \
  1919. }
  1920. #define NET_IF_GET_NAME(dev_name, sfx) __net_if_##dev_name##_##sfx
  1921. #define NET_IF_DEV_GET_NAME(dev_name, sfx) __net_if_dev_##dev_name##_##sfx
  1922. #define NET_IF_GET(dev_name, sfx) \
  1923. ((struct net_if *)&NET_IF_GET_NAME(dev_name, sfx))
  1924. #define NET_IF_INIT(dev_name, sfx, _l2, _mtu, _num_configs) \
  1925. static STRUCT_SECTION_ITERABLE(net_if_dev, \
  1926. NET_IF_DEV_GET_NAME(dev_name, sfx)) = { \
  1927. .dev = &(DEVICE_NAME_GET(dev_name)), \
  1928. .l2 = &(NET_L2_GET_NAME(_l2)), \
  1929. .l2_data = &(NET_L2_GET_DATA(dev_name, sfx)), \
  1930. .mtu = _mtu, \
  1931. }; \
  1932. static Z_DECL_ALIGN(struct net_if) \
  1933. NET_IF_GET_NAME(dev_name, sfx)[_num_configs] \
  1934. __used __in_section(_net_if, static, \
  1935. dev_name) = { \
  1936. [0 ... (_num_configs - 1)] = { \
  1937. .if_dev = &(NET_IF_DEV_GET_NAME(dev_name, sfx)), \
  1938. NET_IF_CONFIG_INIT \
  1939. } \
  1940. }
  1941. #define NET_IF_OFFLOAD_INIT(dev_name, sfx, _mtu) \
  1942. static STRUCT_SECTION_ITERABLE(net_if_dev, \
  1943. NET_IF_DEV_GET_NAME(dev_name, sfx)) = { \
  1944. .dev = &(DEVICE_NAME_GET(dev_name)), \
  1945. .mtu = _mtu, \
  1946. }; \
  1947. static Z_DECL_ALIGN(struct net_if) \
  1948. NET_IF_GET_NAME(dev_name, sfx)[NET_IF_MAX_CONFIGS] \
  1949. __used __in_section(_net_if, static, \
  1950. dev_name) = { \
  1951. [0 ... (NET_IF_MAX_CONFIGS - 1)] = { \
  1952. .if_dev = &(NET_IF_DEV_GET_NAME(dev_name, sfx)), \
  1953. NET_IF_CONFIG_INIT \
  1954. } \
  1955. }
  1956. /** @endcond */
  1957. /* Network device initialization macros */
  1958. #define Z_NET_DEVICE_INIT(node_id, dev_name, drv_name, init_fn, \
  1959. pm_control_fn, data, cfg, prio, api, l2, \
  1960. l2_ctx_type, mtu) \
  1961. Z_DEVICE_DEFINE(node_id, dev_name, drv_name, init_fn, \
  1962. pm_control_fn, data, \
  1963. cfg, POST_KERNEL, prio, api); \
  1964. NET_L2_DATA_INIT(dev_name, 0, l2_ctx_type); \
  1965. NET_IF_INIT(dev_name, 0, l2, mtu, NET_IF_MAX_CONFIGS)
  1966. /**
  1967. * @def NET_DEVICE_INIT
  1968. *
  1969. * @brief Create a network interface and bind it to network device.
  1970. *
  1971. * @param dev_name Network device name.
  1972. * @param drv_name The name this instance of the driver exposes to
  1973. * the system.
  1974. * @param init_fn Address to the init function of the driver.
  1975. * @param pm_control_fn Pointer to pm_control function.
  1976. * Can be NULL if not implemented.
  1977. * @param data Pointer to the device's private data.
  1978. * @param cfg The address to the structure containing the
  1979. * configuration information for this instance of the driver.
  1980. * @param prio The initialization level at which configuration occurs.
  1981. * @param api Provides an initial pointer to the API function struct
  1982. * used by the driver. Can be NULL.
  1983. * @param l2 Network L2 layer for this network interface.
  1984. * @param l2_ctx_type Type of L2 context data.
  1985. * @param mtu Maximum transfer unit in bytes for this network interface.
  1986. */
  1987. #define NET_DEVICE_INIT(dev_name, drv_name, init_fn, pm_control_fn, \
  1988. data, cfg, prio, api, l2, \
  1989. l2_ctx_type, mtu) \
  1990. Z_NET_DEVICE_INIT(DT_INVALID_NODE, dev_name, drv_name, init_fn, \
  1991. pm_control_fn, data, cfg, prio, api, l2, \
  1992. l2_ctx_type, mtu)
  1993. /**
  1994. * @def NET_DEVICE_DT_DEFINE
  1995. *
  1996. * @brief Like NET_DEVICE_INIT but taking metadata from a devicetree node.
  1997. * Create a network interface and bind it to network device.
  1998. *
  1999. * @param node_id The devicetree node identifier.
  2000. * @param init_fn Address to the init function of the driver.
  2001. * @param pm_control_fn Pointer to pm_control function.
  2002. * Can be NULL if not implemented.
  2003. * @param data Pointer to the device's private data.
  2004. * @param cfg The address to the structure containing the
  2005. * configuration information for this instance of the driver.
  2006. * @param prio The initialization level at which configuration occurs.
  2007. * @param api Provides an initial pointer to the API function struct
  2008. * used by the driver. Can be NULL.
  2009. * @param l2 Network L2 layer for this network interface.
  2010. * @param l2_ctx_type Type of L2 context data.
  2011. * @param mtu Maximum transfer unit in bytes for this network interface.
  2012. */
  2013. #define NET_DEVICE_DT_DEFINE(node_id, init_fn, pm_control_fn, data, cfg, \
  2014. prio, api, l2, l2_ctx_type, mtu) \
  2015. Z_NET_DEVICE_INIT(node_id, Z_DEVICE_DT_DEV_NAME(node_id), \
  2016. DT_PROP_OR(node_id, label, ""), init_fn, \
  2017. pm_control_fn, data, cfg, prio, api, l2, \
  2018. l2_ctx_type, mtu)
  2019. /**
  2020. * @def NET_DEVICE_DT_INST_DEFINE
  2021. *
  2022. * @brief Like NET_DEVICE_DT_DEFINE for an instance of a DT_DRV_COMPAT compatible
  2023. *
  2024. * @param inst instance number. This is replaced by
  2025. * <tt>DT_DRV_COMPAT(inst)</tt> in the call to NET_DEVICE_DT_DEFINE.
  2026. *
  2027. * @param ... other parameters as expected by NET_DEVICE_DT_DEFINE.
  2028. */
  2029. #define NET_DEVICE_DT_INST_DEFINE(inst, ...) \
  2030. NET_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
  2031. #define Z_NET_DEVICE_INIT_INSTANCE(node_id, dev_name, drv_name, \
  2032. instance, init_fn, pm_control_fn, \
  2033. data, cfg, prio, api, l2, \
  2034. l2_ctx_type, mtu) \
  2035. Z_DEVICE_DEFINE(node_id, dev_name, drv_name, init_fn, \
  2036. pm_control_fn, data, cfg, POST_KERNEL, \
  2037. prio, api); \
  2038. NET_L2_DATA_INIT(dev_name, instance, l2_ctx_type); \
  2039. NET_IF_INIT(dev_name, instance, l2, mtu, NET_IF_MAX_CONFIGS)
  2040. /**
  2041. * @def NET_DEVICE_INIT_INSTANCE
  2042. *
  2043. * @brief Create multiple network interfaces and bind them to network device.
  2044. * If your network device needs more than one instance of a network interface,
  2045. * use this macro below and provide a different instance suffix each time
  2046. * (0, 1, 2, ... or a, b, c ... whatever works for you)
  2047. *
  2048. * @param dev_name Network device name.
  2049. * @param drv_name The name this instance of the driver exposes to
  2050. * the system.
  2051. * @param instance Instance identifier.
  2052. * @param init_fn Address to the init function of the driver.
  2053. * @param pm_control_fn Pointer to pm_control function.
  2054. * Can be NULL if not implemented.
  2055. * @param data Pointer to the device's private data.
  2056. * @param cfg The address to the structure containing the
  2057. * configuration information for this instance of the driver.
  2058. * @param prio The initialization level at which configuration occurs.
  2059. * @param api Provides an initial pointer to the API function struct
  2060. * used by the driver. Can be NULL.
  2061. * @param l2 Network L2 layer for this network interface.
  2062. * @param l2_ctx_type Type of L2 context data.
  2063. * @param mtu Maximum transfer unit in bytes for this network interface.
  2064. */
  2065. #define NET_DEVICE_INIT_INSTANCE(dev_name, drv_name, instance, init_fn, \
  2066. pm_control_fn, data, cfg, prio, \
  2067. api, l2, l2_ctx_type, mtu) \
  2068. Z_NET_DEVICE_INIT_INSTANCE(DT_INVALID_NODE, dev_name, drv_name, \
  2069. instance, init_fn, pm_control_fn, \
  2070. data, cfg, prio, api, l2, \
  2071. l2_ctx_type, mtu)
  2072. /**
  2073. * @def NET_DEVICE_DT_DEFINE_INSTANCE
  2074. *
  2075. * @brief Like NET_DEVICE_OFFLOAD_INIT but taking metadata from a devicetree.
  2076. * Create multiple network interfaces and bind them to network device.
  2077. * If your network device needs more than one instance of a network interface,
  2078. * use this macro below and provide a different instance suffix each time
  2079. * (0, 1, 2, ... or a, b, c ... whatever works for you)
  2080. *
  2081. * @param node_id The devicetree node identifier.
  2082. * @param instance Instance identifier.
  2083. * @param init_fn Address to the init function of the driver.
  2084. * @param pm_control_fn Pointer to pm_control function.
  2085. * Can be NULL if not implemented.
  2086. * @param data Pointer to the device's private data.
  2087. * @param cfg The address to the structure containing the
  2088. * configuration information for this instance of the driver.
  2089. * @param prio The initialization level at which configuration occurs.
  2090. * @param api Provides an initial pointer to the API function struct
  2091. * used by the driver. Can be NULL.
  2092. * @param l2 Network L2 layer for this network interface.
  2093. * @param l2_ctx_type Type of L2 context data.
  2094. * @param mtu Maximum transfer unit in bytes for this network interface.
  2095. */
  2096. #define NET_DEVICE_DT_DEFINE_INSTANCE(node_id, instance, init_fn, \
  2097. pm_control_fn, data, cfg, prio, \
  2098. api, l2, l2_ctx_type, mtu) \
  2099. Z_NET_DEVICE_INIT_INSTANCE(node_id, node_id, DT_LABEL(node_id), \
  2100. instance, init_fn, pm_control_fn, \
  2101. data, cfg, prio, api, l2, \
  2102. l2_ctx_type, mtu)
  2103. /**
  2104. * @def NET_DEVICE_DT_INST_DEFINE_INSTANCE
  2105. *
  2106. * @brief Like NET_DEVICE_DT_DEFINE_INSTANCE for an instance of a DT_DRV_COMPAT
  2107. * compatible
  2108. *
  2109. * @param inst instance number. This is replaced by
  2110. * <tt>DT_DRV_COMPAT(inst)</tt> in the call to NET_DEVICE_DT_DEFINE_INSTANCE.
  2111. *
  2112. * @param ... other parameters as expected by NET_DEVICE_DT_DEFINE_INSTANCE.
  2113. */
  2114. #define NET_DEVICE_DT_INST_DEFINE_INSTANCE(inst, ...) \
  2115. NET_DEVICE_DT_DEFINE_INSTANCE(DT_DRV_INST(inst), __VA_ARGS__)
  2116. #define Z_NET_DEVICE_OFFLOAD_INIT(node_id, dev_name, drv_name, init_fn, \
  2117. pm_control_fn, data, cfg, prio, \
  2118. api, mtu) \
  2119. Z_DEVICE_DEFINE(node_id, dev_name, drv_name, init_fn, \
  2120. pm_control_fn, data, cfg, POST_KERNEL, prio, api);\
  2121. NET_IF_OFFLOAD_INIT(dev_name, 0, mtu)
  2122. /**
  2123. * @def NET_DEVICE_OFFLOAD_INIT
  2124. *
  2125. * @brief Create a offloaded network interface and bind it to network device.
  2126. * The offloaded network interface is implemented by a device vendor HAL or
  2127. * similar.
  2128. *
  2129. * @param dev_name Network device name.
  2130. * @param drv_name The name this instance of the driver exposes to
  2131. * the system.
  2132. * @param init_fn Address to the init function of the driver.
  2133. * @param pm_control_fn Pointer to pm_control function.
  2134. * Can be NULL if not implemented.
  2135. * @param data Pointer to the device's private data.
  2136. * @param cfg The address to the structure containing the
  2137. * configuration information for this instance of the driver.
  2138. * @param prio The initialization level at which configuration occurs.
  2139. * @param api Provides an initial pointer to the API function struct
  2140. * used by the driver. Can be NULL.
  2141. * @param mtu Maximum transfer unit in bytes for this network interface.
  2142. */
  2143. #define NET_DEVICE_OFFLOAD_INIT(dev_name, drv_name, init_fn, \
  2144. pm_control_fn, data, cfg, prio, api, mtu)\
  2145. Z_NET_DEVICE_OFFLOAD_INIT(DT_INVALID_NODE, dev_name, drv_name, \
  2146. init_fn, pm_control_fn, data, cfg, prio,\
  2147. api, mtu)
  2148. /**
  2149. * @def NET_DEVICE_DT_OFFLOAD_DEFINE
  2150. *
  2151. * @brief Like NET_DEVICE_OFFLOAD_INIT but taking metadata from a devicetree
  2152. * node. Create a offloaded network interface and bind it to network device.
  2153. * The offloaded network interface is implemented by a device vendor HAL or
  2154. * similar.
  2155. *
  2156. * @param node_id The devicetree node identifier.
  2157. * @param init_fn Address to the init function of the driver.
  2158. * @param pm_control_fn Pointer to pm_control function.
  2159. * Can be NULL if not implemented.
  2160. * @param data Pointer to the device's private data.
  2161. * @param cfg The address to the structure containing the
  2162. * configuration information for this instance of the driver.
  2163. * @param prio The initialization level at which configuration occurs.
  2164. * @param api Provides an initial pointer to the API function struct
  2165. * used by the driver. Can be NULL.
  2166. * @param mtu Maximum transfer unit in bytes for this network interface.
  2167. */
  2168. #define NET_DEVICE_DT_OFFLOAD_DEFINE(node_id, init_fn, pm_control_fn, \
  2169. data, cfg, prio, api, mtu) \
  2170. Z_NET_DEVICE_OFFLOAD_INIT(node_id, Z_DEVICE_DT_DEV_NAME(node_id), \
  2171. DT_PROP_OR(node_id, label, NULL), \
  2172. init_fn, pm_control_fn, data, cfg, \
  2173. prio, api, mtu)
  2174. /**
  2175. * @def NET_DEVICE_DT_INST_OFFLOAD_DEFINE
  2176. *
  2177. * @brief Like NET_DEVICE_DT_OFFLOAD_DEFINE for an instance of a DT_DRV_COMPAT
  2178. * compatible
  2179. *
  2180. * @param inst instance number. This is replaced by
  2181. * <tt>DT_DRV_COMPAT(inst)</tt> in the call to NET_DEVICE_DT_OFFLOAD_DEFINE.
  2182. *
  2183. * @param ... other parameters as expected by NET_DEVICE_DT_OFFLOAD_DEFINE.
  2184. */
  2185. #define NET_DEVICE_DT_INST_OFFLOAD_DEFINE(inst, ...) \
  2186. NET_DEVICE_DT_OFFLOAD_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
  2187. #ifdef __cplusplus
  2188. }
  2189. #endif
  2190. #include <syscalls/net_if.h>
  2191. /**
  2192. * @}
  2193. */
  2194. #endif /* ZEPHYR_INCLUDE_NET_NET_IF_H_ */