1 /** @file
2 @brief IPv6 data handler
3
4 This is not to be included by the application.
5 */
6
7 /*
8 * Copyright (c) 2016 Intel Corporation
9 *
10 * SPDX-License-Identifier: Apache-2.0
11 */
12
13 #ifndef __IPV6_H
14 #define __IPV6_H
15
16 #include <zephyr/types.h>
17
18 #include <net/net_ip.h>
19 #include <net/net_pkt.h>
20 #include <net/net_if.h>
21 #include <net/net_context.h>
22
23 #include "icmpv6.h"
24 #include "nbr.h"
25
26 #define NET_IPV6_ND_HOP_LIMIT 255
27 #define NET_IPV6_ND_INFINITE_LIFETIME 0xFFFFFFFF
28
29 #define NET_IPV6_DEFAULT_PREFIX_LEN 64
30
31 #define NET_MAX_RS_COUNT 3
32
33 /**
34 * @brief Bitmaps for IPv6 extension header processing
35 *
36 * When processing extension headers, we record which one we have seen.
37 * This is done as the network packet cannot have twice the same header,
38 * except for destination option.
39 * This information is stored in bitfield variable.
40 * The order of the bitmap is the order recommended in RFC 2460.
41 */
42 #define NET_IPV6_EXT_HDR_BITMAP_HBHO 0x01
43 #define NET_IPV6_EXT_HDR_BITMAP_DESTO1 0x02
44 #define NET_IPV6_EXT_HDR_BITMAP_ROUTING 0x04
45 #define NET_IPV6_EXT_HDR_BITMAP_FRAG 0x08
46 #define NET_IPV6_EXT_HDR_BITMAP_AH 0x10
47 #define NET_IPV6_EXT_HDR_BITMAP_ESP 0x20
48 #define NET_IPV6_EXT_HDR_BITMAP_DESTO2 0x40
49
50 /**
51 * @brief Destination and Hop By Hop extension headers option types
52 */
53 #define NET_IPV6_EXT_HDR_OPT_PAD1 0
54 #define NET_IPV6_EXT_HDR_OPT_PADN 1
55 #define NET_IPV6_EXT_HDR_OPT_RPL 0x63
56
57 /**
58 * @brief Multicast Listener Record v2 record types.
59 */
60 #define NET_IPV6_MLDv2_MODE_IS_INCLUDE 1
61 #define NET_IPV6_MLDv2_MODE_IS_EXCLUDE 2
62 #define NET_IPV6_MLDv2_CHANGE_TO_INCLUDE_MODE 3
63 #define NET_IPV6_MLDv2_CHANGE_TO_EXCLUDE_MODE 4
64 #define NET_IPV6_MLDv2_ALLOW_NEW_SOURCES 5
65 #define NET_IPV6_MLDv2_BLOCK_OLD_SOURCES 6
66
67 /* State of the neighbor */
68 enum net_ipv6_nbr_state {
69 NET_IPV6_NBR_STATE_INCOMPLETE,
70 NET_IPV6_NBR_STATE_REACHABLE,
71 NET_IPV6_NBR_STATE_STALE,
72 NET_IPV6_NBR_STATE_DELAY,
73 NET_IPV6_NBR_STATE_PROBE,
74 NET_IPV6_NBR_STATE_STATIC,
75 };
76
77 const char *net_ipv6_nbr_state2str(enum net_ipv6_nbr_state state);
78
79 /**
80 * @brief IPv6 neighbor information.
81 */
82 struct net_ipv6_nbr_data {
83 /** Any pending packet waiting ND to finish. */
84 struct net_pkt *pending;
85
86 /** IPv6 address. */
87 struct in6_addr addr;
88
89 /** Reachable timer. */
90 int64_t reachable;
91
92 /** Reachable timeout */
93 int32_t reachable_timeout;
94
95 /** Neighbor Solicitation reply timer */
96 int64_t send_ns;
97
98 /** State of the neighbor discovery */
99 enum net_ipv6_nbr_state state;
100
101 /** Link metric for the neighbor */
102 uint16_t link_metric;
103
104 /** How many times we have sent NS */
105 uint8_t ns_count;
106
107 /** Is the neighbor a router */
108 bool is_router;
109
110 #if defined(CONFIG_NET_IPV6_NBR_CACHE) || defined(CONFIG_NET_IPV6_ND)
111 /** Stale counter used to removed oldest nbr in STALE state,
112 * when table is full.
113 */
114 uint32_t stale_counter;
115 #endif
116 };
117
net_ipv6_nbr_data(struct net_nbr * nbr)118 static inline struct net_ipv6_nbr_data *net_ipv6_nbr_data(struct net_nbr *nbr)
119 {
120 return (struct net_ipv6_nbr_data *)nbr->data;
121 }
122
123 #if defined(CONFIG_NET_IPV6_DAD)
124 int net_ipv6_start_dad(struct net_if *iface, struct net_if_addr *ifaddr);
125 #endif
126
127 int net_ipv6_send_ns(struct net_if *iface, struct net_pkt *pending,
128 const struct in6_addr *src, const struct in6_addr *dst,
129 const struct in6_addr *tgt, bool is_my_address);
130
131 int net_ipv6_send_rs(struct net_if *iface);
132 int net_ipv6_start_rs(struct net_if *iface);
133
134 int net_ipv6_send_na(struct net_if *iface, const struct in6_addr *src,
135 const struct in6_addr *dst, const struct in6_addr *tgt,
136 uint8_t flags);
137
138
net_ipv6_is_nexthdr_upper_layer(uint8_t nexthdr)139 static inline bool net_ipv6_is_nexthdr_upper_layer(uint8_t nexthdr)
140 {
141 return (nexthdr == IPPROTO_ICMPV6 || nexthdr == IPPROTO_UDP ||
142 nexthdr == IPPROTO_TCP ||
143 (IS_ENABLED(CONFIG_NET_L2_VIRTUAL) &&
144 ((nexthdr == IPPROTO_IPV6) || (nexthdr == IPPROTO_IPIP))));
145 }
146
147 /**
148 * @brief Create IPv6 packet in provided net_pkt.
149 *
150 * @param pkt Network packet
151 * @param src Source IPv6 address
152 * @param dst Destination IPv6 address
153 *
154 * @return 0 on success, negative errno otherwise.
155 */
156 #if defined(CONFIG_NET_NATIVE_IPV6)
157 int net_ipv6_create(struct net_pkt *pkt,
158 const struct in6_addr *src,
159 const struct in6_addr *dst);
160 #else
net_ipv6_create(struct net_pkt * pkt,const struct in6_addr * src,const struct in6_addr * dst)161 static inline int net_ipv6_create(struct net_pkt *pkt,
162 const struct in6_addr *src,
163 const struct in6_addr *dst)
164 {
165 ARG_UNUSED(pkt);
166 ARG_UNUSED(src);
167 ARG_UNUSED(dst);
168
169 return -ENOTSUP;
170 }
171 #endif
172
173 /**
174 * @brief Finalize IPv6 packet. It should be called right before
175 * sending the packet and after all the data has been added into
176 * the packet. This function will set the length of the
177 * packet and calculate the higher protocol checksum if needed.
178 *
179 * @param pkt Network packet
180 * @param next_header_proto Protocol type of the next header after IPv6 header.
181 *
182 * @return 0 on success, negative errno otherwise.
183 */
184 #if defined(CONFIG_NET_NATIVE_IPV6)
185 int net_ipv6_finalize(struct net_pkt *pkt, uint8_t next_header_proto);
186 #else
net_ipv6_finalize(struct net_pkt * pkt,uint8_t next_header_proto)187 static inline int net_ipv6_finalize(struct net_pkt *pkt,
188 uint8_t next_header_proto)
189 {
190 ARG_UNUSED(pkt);
191 ARG_UNUSED(next_header_proto);
192
193 return -ENOTSUP;
194 }
195 #endif
196
197 /**
198 * @brief Join a given multicast group.
199 *
200 * @param iface Network interface where join message is sent
201 * @param addr Multicast group to join
202 *
203 * @return Return 0 if joining was done, <0 otherwise.
204 */
205 #if defined(CONFIG_NET_IPV6_MLD)
206 int net_ipv6_mld_join(struct net_if *iface, const struct in6_addr *addr);
207 #else
208 #define net_ipv6_mld_join(...)
209 #endif /* CONFIG_NET_IPV6_MLD */
210
211 /**
212 * @brief Leave a given multicast group.
213 *
214 * @param iface Network interface where leave message is sent
215 * @param addr Multicast group to leave
216 *
217 * @return Return 0 if leaving is done, <0 otherwise.
218 */
219 #if defined(CONFIG_NET_IPV6_MLD)
220 int net_ipv6_mld_leave(struct net_if *iface, const struct in6_addr *addr);
221 #else
222 #define net_ipv6_mld_leave(...)
223 #endif /* CONFIG_NET_IPV6_MLD */
224
225 /**
226 * @typedef net_nbr_cb_t
227 * @brief Callback used while iterating over neighbors.
228 *
229 * @param nbr A valid pointer on current neighbor.
230 * @param user_data A valid pointer on some user data or NULL
231 */
232 typedef void (*net_nbr_cb_t)(struct net_nbr *nbr, void *user_data);
233
234 /**
235 * @brief Make sure the link layer address is set according to
236 * destination address. If the ll address is not yet known, then
237 * start neighbor discovery to find it out. If ND needs to be done
238 * then the returned packet is the Neighbor Solicitation message
239 * and the original message is sent after Neighbor Advertisement
240 * message is received.
241 *
242 * @param pkt Network packet
243 *
244 * @return Return a verdict.
245 */
246 #if defined(CONFIG_NET_IPV6_NBR_CACHE) && defined(CONFIG_NET_NATIVE_IPV6)
247 enum net_verdict net_ipv6_prepare_for_send(struct net_pkt *pkt);
248 #else
net_ipv6_prepare_for_send(struct net_pkt * pkt)249 static inline enum net_verdict net_ipv6_prepare_for_send(struct net_pkt *pkt)
250 {
251 return NET_OK;
252 }
253 #endif
254
255 /**
256 * @brief Look for a neighbor from it's address on an iface
257 *
258 * @param iface A valid pointer on a network interface
259 * @param addr The IPv6 address to match
260 *
261 * @return A valid pointer on a neighbor on success, NULL otherwise
262 */
263 #if defined(CONFIG_NET_IPV6_NBR_CACHE) && defined(CONFIG_NET_NATIVE_IPV6)
264 struct net_nbr *net_ipv6_nbr_lookup(struct net_if *iface,
265 struct in6_addr *addr);
266 #else
net_ipv6_nbr_lookup(struct net_if * iface,struct in6_addr * addr)267 static inline struct net_nbr *net_ipv6_nbr_lookup(struct net_if *iface,
268 struct in6_addr *addr)
269 {
270 return NULL;
271 }
272 #endif
273
274 /**
275 * @brief Get neighbor from its index.
276 *
277 * @param iface Network interface to match. If NULL, then use
278 * whatever interface there is configured for the neighbor address.
279 * @param idx Index of the link layer address in the address array
280 *
281 * @return A valid pointer on a neighbor on success, NULL otherwise
282 */
283 struct net_nbr *net_ipv6_get_nbr(struct net_if *iface, uint8_t idx);
284
285 /**
286 * @brief Look for a neighbor from it's link local address index
287 *
288 * @param iface Network interface to match. If NULL, then use
289 * whatever interface there is configured for the neighbor address.
290 * @param idx Index of the link layer address in the address array
291 *
292 * @return A valid pointer on a neighbor on success, NULL otherwise
293 */
294 #if defined(CONFIG_NET_IPV6_NBR_CACHE) && defined(CONFIG_NET_NATIVE_IPV6)
295 struct in6_addr *net_ipv6_nbr_lookup_by_index(struct net_if *iface,
296 uint8_t idx);
297 #else
298 static inline
net_ipv6_nbr_lookup_by_index(struct net_if * iface,uint8_t idx)299 struct in6_addr *net_ipv6_nbr_lookup_by_index(struct net_if *iface,
300 uint8_t idx)
301 {
302 return NULL;
303 }
304 #endif
305
306 /**
307 * @brief Add a neighbor to neighbor cache
308 *
309 * Add a neighbor to the cache after performing a lookup and in case
310 * there exists an entry in the cache update its state and lladdr.
311 *
312 * @param iface A valid pointer on a network interface
313 * @param addr Neighbor IPv6 address
314 * @param lladdr Neighbor link layer address
315 * @param is_router Set to true if the neighbor is a router, false
316 * otherwise
317 * @param state Initial state of the neighbor entry in the cache
318 *
319 * @return A valid pointer on a neighbor on success, NULL otherwise
320 */
321 #if defined(CONFIG_NET_IPV6_NBR_CACHE) && defined(CONFIG_NET_NATIVE_IPV6)
322 struct net_nbr *net_ipv6_nbr_add(struct net_if *iface,
323 const struct in6_addr *addr,
324 const struct net_linkaddr *lladdr,
325 bool is_router,
326 enum net_ipv6_nbr_state state);
327 #else
net_ipv6_nbr_add(struct net_if * iface,const struct in6_addr * addr,const struct net_linkaddr * lladdr,bool is_router,enum net_ipv6_nbr_state state)328 static inline struct net_nbr *net_ipv6_nbr_add(struct net_if *iface,
329 const struct in6_addr *addr,
330 const struct net_linkaddr *lladdr,
331 bool is_router,
332 enum net_ipv6_nbr_state state)
333 {
334 return NULL;
335 }
336 #endif
337
338 /**
339 * @brief Remove a neighbor from neighbor cache.
340 *
341 * @param iface A valid pointer on a network interface
342 * @param addr Neighbor IPv6 address
343 *
344 * @return True if neighbor could be removed, False otherwise
345 */
346 #if defined(CONFIG_NET_IPV6_NBR_CACHE) && defined(CONFIG_NET_NATIVE_IPV6)
347 bool net_ipv6_nbr_rm(struct net_if *iface, struct in6_addr *addr);
348 #else
net_ipv6_nbr_rm(struct net_if * iface,struct in6_addr * addr)349 static inline bool net_ipv6_nbr_rm(struct net_if *iface, struct in6_addr *addr)
350 {
351 return true;
352 }
353 #endif
354
355 /**
356 * @brief Go through all the neighbors and call callback for each of them.
357 *
358 * @param cb User supplied callback function to call.
359 * @param user_data User specified data.
360 */
361 #if defined(CONFIG_NET_IPV6_NBR_CACHE) && defined(CONFIG_NET_NATIVE_IPV6)
362 void net_ipv6_nbr_foreach(net_nbr_cb_t cb, void *user_data);
363 #else /* CONFIG_NET_IPV6_NBR_CACHE */
net_ipv6_nbr_foreach(net_nbr_cb_t cb,void * user_data)364 static inline void net_ipv6_nbr_foreach(net_nbr_cb_t cb, void *user_data)
365 {
366 return;
367 }
368 #endif /* CONFIG_NET_IPV6_NBR_CACHE */
369
370 /**
371 * @brief Set the neighbor reachable timer.
372 *
373 * @param iface A valid pointer on a network interface
374 * @param nbr Neighbor struct pointer
375 */
376 #if defined(CONFIG_NET_IPV6_ND) && defined(CONFIG_NET_NATIVE_IPV6)
377 void net_ipv6_nbr_set_reachable_timer(struct net_if *iface,
378 struct net_nbr *nbr);
379
380 #else /* CONFIG_NET_IPV6_ND */
net_ipv6_nbr_set_reachable_timer(struct net_if * iface,struct net_nbr * nbr)381 static inline void net_ipv6_nbr_set_reachable_timer(struct net_if *iface,
382 struct net_nbr *nbr)
383 {
384 }
385 #endif
386
387 #if defined(CONFIG_NET_IPV6_FRAGMENT)
388 /** Store pending IPv6 fragment information that is needed for reassembly. */
389 struct net_ipv6_reassembly {
390 /** IPv6 source address of the fragment */
391 struct in6_addr src;
392
393 /** IPv6 destination address of the fragment */
394 struct in6_addr dst;
395
396 /**
397 * Timeout for cancelling the reassembly. The timer is used
398 * also to detect if this reassembly slot is used or not.
399 */
400 struct k_work_delayable timer;
401
402 /** Pointers to pending fragments */
403 struct net_pkt *pkt[CONFIG_NET_IPV6_FRAGMENT_MAX_PKT];
404
405 /** IPv6 fragment identification */
406 uint32_t id;
407 };
408 #else
409 struct net_ipv6_reassembly;
410 #endif
411
412 /**
413 * @typedef net_ipv6_frag_cb_t
414 * @brief Callback used while iterating over pending IPv6 fragments.
415 *
416 * @param reass IPv6 fragment reassembly struct
417 * @param user_data A valid pointer on some user data or NULL
418 */
419 typedef void (*net_ipv6_frag_cb_t)(struct net_ipv6_reassembly *reass,
420 void *user_data);
421
422 /**
423 * @brief Go through all the currently pending IPv6 fragments.
424 *
425 * @param cb Callback to call for each pending IPv6 fragment.
426 * @param user_data User specified data or NULL.
427 */
428 void net_ipv6_frag_foreach(net_ipv6_frag_cb_t cb, void *user_data);
429
430 /**
431 * @brief Find the last IPv6 extension header in the network packet.
432 *
433 * @param pkt Network head packet.
434 * @param next_hdr_off Offset of the next header field that points
435 * to last header. This is returned to caller.
436 * @param last_hdr_off Offset of the last header field in the packet.
437 * This is returned to caller.
438 *
439 * @return 0 on success, a negative errno otherwise.
440 */
441 int net_ipv6_find_last_ext_hdr(struct net_pkt *pkt, uint16_t *next_hdr_off,
442 uint16_t *last_hdr_off);
443
444 /**
445 * @brief Handles IPv6 fragmented packets.
446 *
447 * @param pkt Network head packet.
448 * @param hdr The IPv6 header of the current packet
449 * @param nexthdr IPv6 next header after fragment header part
450 *
451 * @return Return verdict about the packet
452 */
453 #if defined(CONFIG_NET_IPV6_FRAGMENT) && defined(CONFIG_NET_NATIVE_IPV6)
454 enum net_verdict net_ipv6_handle_fragment_hdr(struct net_pkt *pkt,
455 struct net_ipv6_hdr *hdr,
456 uint8_t nexthdr);
457 #else
458 static inline
net_ipv6_handle_fragment_hdr(struct net_pkt * pkt,struct net_ipv6_hdr * hdr,uint8_t nexthdr)459 enum net_verdict net_ipv6_handle_fragment_hdr(struct net_pkt *pkt,
460 struct net_ipv6_hdr *hdr,
461 uint8_t nexthdr)
462 {
463 ARG_UNUSED(pkt);
464 ARG_UNUSED(hdr);
465 ARG_UNUSED(nexthdr);
466
467 return NET_DROP;
468 }
469 #endif /* CONFIG_NET_IPV6_FRAGMENT */
470
471 #if defined(CONFIG_NET_NATIVE_IPV6)
472 void net_ipv6_init(void);
473 void net_ipv6_nbr_init(void);
474 #if defined(CONFIG_NET_IPV6_MLD)
475 void net_ipv6_mld_init(void);
476 #else
477 #define net_ipv6_mld_init(...)
478 #endif
479 #else
480 #define net_ipv6_init(...)
481 #define net_ipv6_nbr_init(...)
482 #endif
483
484 #endif /* __IPV6_H */
485