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 <zephyr/net/net_ip.h>
19 #include <zephyr/net/net_pkt.h>
20 #include <zephyr/net/net_if.h>
21 #include <zephyr/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 #define NET_IPV6_DSCP_MASK 0xFC
34 #define NET_IPV6_DSCP_OFFSET 2
35 #define NET_IPV6_ECN_MASK 0x03
36 
37 /**
38  * @brief Bitmaps for IPv6 extension header processing
39  *
40  * When processing extension headers, we record which one we have seen.
41  * This is done as the network packet cannot have twice the same header,
42  * except for destination option.
43  * This information is stored in bitfield variable.
44  * The order of the bitmap is the order recommended in RFC 2460.
45  */
46 #define NET_IPV6_EXT_HDR_BITMAP_HBHO   0x01
47 #define NET_IPV6_EXT_HDR_BITMAP_DESTO1 0x02
48 #define NET_IPV6_EXT_HDR_BITMAP_ROUTING        0x04
49 #define NET_IPV6_EXT_HDR_BITMAP_FRAG   0x08
50 #define NET_IPV6_EXT_HDR_BITMAP_AH     0x10
51 #define NET_IPV6_EXT_HDR_BITMAP_ESP    0x20
52 #define NET_IPV6_EXT_HDR_BITMAP_DESTO2 0x40
53 
54 /**
55  * @brief Destination and Hop By Hop extension headers option types
56  */
57 #define NET_IPV6_EXT_HDR_OPT_PAD1  0
58 #define NET_IPV6_EXT_HDR_OPT_PADN  1
59 #define NET_IPV6_EXT_HDR_OPT_RPL   0x63
60 
61 /**
62  * @brief Multicast Listener Record v2 record types.
63  */
64 #define NET_IPV6_MLDv2_MODE_IS_INCLUDE        1
65 #define NET_IPV6_MLDv2_MODE_IS_EXCLUDE        2
66 #define NET_IPV6_MLDv2_CHANGE_TO_INCLUDE_MODE 3
67 #define NET_IPV6_MLDv2_CHANGE_TO_EXCLUDE_MODE 4
68 #define NET_IPV6_MLDv2_ALLOW_NEW_SOURCES      5
69 #define NET_IPV6_MLDv2_BLOCK_OLD_SOURCES      6
70 
71 /* State of the neighbor */
72 enum net_ipv6_nbr_state {
73 	NET_IPV6_NBR_STATE_INCOMPLETE,
74 	NET_IPV6_NBR_STATE_REACHABLE,
75 	NET_IPV6_NBR_STATE_STALE,
76 	NET_IPV6_NBR_STATE_DELAY,
77 	NET_IPV6_NBR_STATE_PROBE,
78 	NET_IPV6_NBR_STATE_STATIC,
79 };
80 
81 const char *net_ipv6_nbr_state2str(enum net_ipv6_nbr_state state);
82 
83 /**
84  * @brief IPv6 neighbor information.
85  */
86 struct net_ipv6_nbr_data {
87 	/** Any pending packet waiting ND to finish. */
88 	struct net_pkt *pending;
89 
90 	/** IPv6 address. */
91 	struct in6_addr addr;
92 
93 	/** Reachable timer. */
94 	int64_t reachable;
95 
96 	/** Reachable timeout */
97 	int32_t reachable_timeout;
98 
99 	/** Neighbor Solicitation reply timer */
100 	int64_t send_ns;
101 
102 	/** State of the neighbor discovery */
103 	enum net_ipv6_nbr_state state;
104 
105 	/** Link metric for the neighbor */
106 	uint16_t link_metric;
107 
108 	/** How many times we have sent NS */
109 	uint8_t ns_count;
110 
111 	/** Is the neighbor a router */
112 	bool is_router;
113 
114 #if defined(CONFIG_NET_IPV6_NBR_CACHE) || defined(CONFIG_NET_IPV6_ND)
115 	/** Stale counter used to removed oldest nbr in STALE state,
116 	 *  when table is full.
117 	 */
118 	uint32_t stale_counter;
119 #endif
120 };
121 
net_ipv6_nbr_data(struct net_nbr * nbr)122 static inline struct net_ipv6_nbr_data *net_ipv6_nbr_data(struct net_nbr *nbr)
123 {
124 	return (struct net_ipv6_nbr_data *)nbr->data;
125 }
126 
127 #if defined(CONFIG_NET_IPV6_DAD)
128 int net_ipv6_start_dad(struct net_if *iface, struct net_if_addr *ifaddr);
129 #endif
130 
131 int net_ipv6_send_ns(struct net_if *iface, struct net_pkt *pending,
132 		     const struct in6_addr *src, const struct in6_addr *dst,
133 		     const struct in6_addr *tgt, bool is_my_address);
134 
135 int net_ipv6_send_rs(struct net_if *iface);
136 int net_ipv6_start_rs(struct net_if *iface);
137 
138 int net_ipv6_send_na(struct net_if *iface, const struct in6_addr *src,
139 		     const struct in6_addr *dst, const struct in6_addr *tgt,
140 		     uint8_t flags);
141 
142 
net_ipv6_is_nexthdr_upper_layer(uint8_t nexthdr)143 static inline bool net_ipv6_is_nexthdr_upper_layer(uint8_t nexthdr)
144 {
145 	return (nexthdr == IPPROTO_ICMPV6 || nexthdr == IPPROTO_UDP ||
146 		nexthdr == IPPROTO_TCP ||
147 		(IS_ENABLED(CONFIG_NET_L2_VIRTUAL) &&
148 		 ((nexthdr == IPPROTO_IPV6) || (nexthdr == IPPROTO_IPIP))));
149 }
150 
151 /**
152  * @brief Create IPv6 packet in provided net_pkt.
153  *
154  * @param pkt Network packet
155  * @param src Source IPv6 address
156  * @param dst Destination IPv6 address
157  *
158  * @return 0 on success, negative errno otherwise.
159  */
160 #if defined(CONFIG_NET_NATIVE_IPV6)
161 int net_ipv6_create(struct net_pkt *pkt,
162 		    const struct in6_addr *src,
163 		    const struct in6_addr *dst);
164 #else
net_ipv6_create(struct net_pkt * pkt,const struct in6_addr * src,const struct in6_addr * dst)165 static inline int net_ipv6_create(struct net_pkt *pkt,
166 				  const struct in6_addr *src,
167 				  const struct in6_addr *dst)
168 {
169 	ARG_UNUSED(pkt);
170 	ARG_UNUSED(src);
171 	ARG_UNUSED(dst);
172 
173 	return -ENOTSUP;
174 }
175 #endif
176 
177 /**
178  * @brief Finalize IPv6 packet. It should be called right before
179  * sending the packet and after all the data has been added into
180  * the packet. This function will set the length of the
181  * packet and calculate the higher protocol checksum if needed.
182  *
183  * @param pkt Network packet
184  * @param next_header_proto Protocol type of the next header after IPv6 header.
185  *
186  * @return 0 on success, negative errno otherwise.
187  */
188 #if defined(CONFIG_NET_NATIVE_IPV6)
189 int net_ipv6_finalize(struct net_pkt *pkt, uint8_t next_header_proto);
190 #else
net_ipv6_finalize(struct net_pkt * pkt,uint8_t next_header_proto)191 static inline int net_ipv6_finalize(struct net_pkt *pkt,
192 				    uint8_t next_header_proto)
193 {
194 	ARG_UNUSED(pkt);
195 	ARG_UNUSED(next_header_proto);
196 
197 	return -ENOTSUP;
198 }
199 #endif
200 
201 /**
202  * @brief Join a given multicast group.
203  *
204  * @param iface Network interface where join message is sent
205  * @param addr Multicast group to join
206  *
207  * @return Return 0 if joining was done, <0 otherwise.
208  */
209 #if defined(CONFIG_NET_IPV6_MLD)
210 int net_ipv6_mld_join(struct net_if *iface, const struct in6_addr *addr);
211 #else
212 static inline int
net_ipv6_mld_join(struct net_if * iface,const struct in6_addr * addr)213 net_ipv6_mld_join(struct net_if *iface, const struct in6_addr *addr)
214 {
215 	ARG_UNUSED(iface);
216 	ARG_UNUSED(addr);
217 
218 	return -ENOTSUP;
219 }
220 #endif /* CONFIG_NET_IPV6_MLD */
221 
222 /**
223  * @brief Leave a given multicast group.
224  *
225  * @param iface Network interface where leave message is sent
226  * @param addr Multicast group to leave
227  *
228  * @return Return 0 if leaving is done, <0 otherwise.
229  */
230 #if defined(CONFIG_NET_IPV6_MLD)
231 int net_ipv6_mld_leave(struct net_if *iface, const struct in6_addr *addr);
232 #else
233 static inline int
net_ipv6_mld_leave(struct net_if * iface,const struct in6_addr * addr)234 net_ipv6_mld_leave(struct net_if *iface, const struct in6_addr *addr)
235 {
236 	ARG_UNUSED(iface);
237 	ARG_UNUSED(addr);
238 
239 	return -ENOTSUP;
240 }
241 #endif /* CONFIG_NET_IPV6_MLD */
242 
243 /**
244  * @typedef net_nbr_cb_t
245  * @brief Callback used while iterating over neighbors.
246  *
247  * @param nbr A valid pointer on current neighbor.
248  * @param user_data A valid pointer on some user data or NULL
249  */
250 typedef void (*net_nbr_cb_t)(struct net_nbr *nbr, void *user_data);
251 
252 /**
253  * @brief Make sure the link layer address is set according to
254  * destination address. If the ll address is not yet known, then
255  * start neighbor discovery to find it out. If ND needs to be done
256  * then the returned packet is the Neighbor Solicitation message
257  * and the original message is sent after Neighbor Advertisement
258  * message is received.
259  *
260  * @param pkt Network packet
261  *
262  * @return Return a verdict.
263  */
264 #if defined(CONFIG_NET_IPV6_NBR_CACHE) && defined(CONFIG_NET_NATIVE_IPV6)
265 enum net_verdict net_ipv6_prepare_for_send(struct net_pkt *pkt);
266 #else
net_ipv6_prepare_for_send(struct net_pkt * pkt)267 static inline enum net_verdict net_ipv6_prepare_for_send(struct net_pkt *pkt)
268 {
269 	return NET_OK;
270 }
271 #endif
272 
273 /**
274  * @brief Lock IPv6 Neighbor table mutex
275  *
276  * Neighbor table mutex is used by IPv6 Neighbor cache and IPv6 Routing module.
277  * Mutex shall be held whenever accessing or manipulating neighbor or routing
278  * table entries (for example when obtaining a pointer to the neighbor table
279  * entry). Neighbor and Routing API functions will lock the mutex when called.
280  */
281 void net_ipv6_nbr_lock(void);
282 
283 /**
284  * @brief Unlock IPv6 Neighbor table mutex
285  */
286 void net_ipv6_nbr_unlock(void);
287 
288 /**
289  * @brief Look for a neighbor from it's address on an iface
290  *
291  * @param iface A valid pointer on a network interface
292  * @param addr The IPv6 address to match
293  *
294  * @return A valid pointer on a neighbor on success, NULL otherwise
295  */
296 #if defined(CONFIG_NET_IPV6_NBR_CACHE) && defined(CONFIG_NET_NATIVE_IPV6)
297 struct net_nbr *net_ipv6_nbr_lookup(struct net_if *iface,
298 				    struct in6_addr *addr);
299 #else
net_ipv6_nbr_lookup(struct net_if * iface,struct in6_addr * addr)300 static inline struct net_nbr *net_ipv6_nbr_lookup(struct net_if *iface,
301 						  struct in6_addr *addr)
302 {
303 	return NULL;
304 }
305 #endif
306 
307 /**
308  * @brief Get neighbor from its index.
309  *
310  * @param iface Network interface to match. If NULL, then use
311  * whatever interface there is configured for the neighbor address.
312  * @param idx Index of the link layer address in the address array
313  *
314  * @return A valid pointer on a neighbor on success, NULL otherwise
315  */
316 struct net_nbr *net_ipv6_get_nbr(struct net_if *iface, uint8_t idx);
317 
318 /**
319  * @brief Look for a neighbor from it's link local address index
320  *
321  * @param iface Network interface to match. If NULL, then use
322  * whatever interface there is configured for the neighbor address.
323  * @param idx Index of the link layer address in the address array
324  *
325  * @return A valid pointer on a neighbor on success, NULL otherwise
326  */
327 #if defined(CONFIG_NET_IPV6_NBR_CACHE) && defined(CONFIG_NET_NATIVE_IPV6)
328 struct in6_addr *net_ipv6_nbr_lookup_by_index(struct net_if *iface,
329 					      uint8_t idx);
330 #else
331 static inline
net_ipv6_nbr_lookup_by_index(struct net_if * iface,uint8_t idx)332 struct in6_addr *net_ipv6_nbr_lookup_by_index(struct net_if *iface,
333 					      uint8_t idx)
334 {
335 	return NULL;
336 }
337 #endif
338 
339 /**
340  * @brief Add a neighbor to neighbor cache
341  *
342  * Add a neighbor to the cache after performing a lookup and in case
343  * there exists an entry in the cache update its state and lladdr.
344  *
345  * @param iface A valid pointer on a network interface
346  * @param addr Neighbor IPv6 address
347  * @param lladdr Neighbor link layer address
348  * @param is_router Set to true if the neighbor is a router, false
349  * otherwise
350  * @param state Initial state of the neighbor entry in the cache
351  *
352  * @return A valid pointer on a neighbor on success, NULL otherwise
353  */
354 #if defined(CONFIG_NET_IPV6_NBR_CACHE) && defined(CONFIG_NET_NATIVE_IPV6)
355 struct net_nbr *net_ipv6_nbr_add(struct net_if *iface,
356 				 const struct in6_addr *addr,
357 				 const struct net_linkaddr *lladdr,
358 				 bool is_router,
359 				 enum net_ipv6_nbr_state state);
360 #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)361 static inline struct net_nbr *net_ipv6_nbr_add(struct net_if *iface,
362 					       const struct in6_addr *addr,
363 					       const struct net_linkaddr *lladdr,
364 					       bool is_router,
365 					       enum net_ipv6_nbr_state state)
366 {
367 	return NULL;
368 }
369 #endif
370 
371 /**
372  * @brief Remove a neighbor from neighbor cache.
373  *
374  * @param iface A valid pointer on a network interface
375  * @param addr Neighbor IPv6 address
376  *
377  * @return True if neighbor could be removed, False otherwise
378  */
379 #if defined(CONFIG_NET_IPV6_NBR_CACHE) && defined(CONFIG_NET_NATIVE_IPV6)
380 bool net_ipv6_nbr_rm(struct net_if *iface, struct in6_addr *addr);
381 #else
net_ipv6_nbr_rm(struct net_if * iface,struct in6_addr * addr)382 static inline bool net_ipv6_nbr_rm(struct net_if *iface, struct in6_addr *addr)
383 {
384 	return true;
385 }
386 #endif
387 
388 /**
389  * @brief Go through all the neighbors and call callback for each of them.
390  *
391  * @param cb User supplied callback function to call.
392  * @param user_data User specified data.
393  */
394 #if defined(CONFIG_NET_IPV6_NBR_CACHE) && defined(CONFIG_NET_NATIVE_IPV6)
395 void net_ipv6_nbr_foreach(net_nbr_cb_t cb, void *user_data);
396 #else /* CONFIG_NET_IPV6_NBR_CACHE */
net_ipv6_nbr_foreach(net_nbr_cb_t cb,void * user_data)397 static inline void net_ipv6_nbr_foreach(net_nbr_cb_t cb, void *user_data)
398 {
399 	return;
400 }
401 #endif /* CONFIG_NET_IPV6_NBR_CACHE */
402 
403 /**
404  * @brief Provide a reachability hint for IPv6 Neighbor Discovery.
405  *
406  * This function is intended for upper-layer protocols to inform the IPv6
407  * Neighbor Discovery process about the active link to a specific neighbor.
408  * By signaling recent "forward progress" event, such as the reception of
409  * an ACK, this function can help reducing unnecessary ND traffic as per the
410  * guidelines in RFC 4861 (section 7.3).
411  *
412  * @param iface A pointer to the network interface.
413  * @param ipv6_addr Pointer to the IPv6 address of the neighbor node.
414  */
415 #if defined(CONFIG_NET_IPV6_ND) && defined(CONFIG_NET_NATIVE_IPV6)
416 void net_ipv6_nbr_reachability_hint(struct net_if *iface, const struct in6_addr *ipv6_addr);
417 #else
net_ipv6_nbr_reachability_hint(struct net_if * iface,const struct in6_addr * ipv6_addr)418 static inline void net_ipv6_nbr_reachability_hint(struct net_if *iface,
419 						  const struct in6_addr *ipv6_addr)
420 {
421 	ARG_UNUSED(iface);
422 	ARG_UNUSED(ipv6_addr);
423 }
424 #endif
425 
426 /**
427  * @brief Set the neighbor reachable timer.
428  *
429  * @param iface A valid pointer on a network interface
430  * @param nbr Neighbor struct pointer
431  */
432 #if defined(CONFIG_NET_IPV6_ND) && defined(CONFIG_NET_NATIVE_IPV6)
433 void net_ipv6_nbr_set_reachable_timer(struct net_if *iface,
434 				      struct net_nbr *nbr);
435 
436 #else /* CONFIG_NET_IPV6_ND */
net_ipv6_nbr_set_reachable_timer(struct net_if * iface,struct net_nbr * nbr)437 static inline void net_ipv6_nbr_set_reachable_timer(struct net_if *iface,
438 						    struct net_nbr *nbr)
439 {
440 }
441 #endif
442 
443 #if defined(CONFIG_NET_IPV6_FRAGMENT)
444 /** Store pending IPv6 fragment information that is needed for reassembly. */
445 struct net_ipv6_reassembly {
446 	/** IPv6 source address of the fragment */
447 	struct in6_addr src;
448 
449 	/** IPv6 destination address of the fragment */
450 	struct in6_addr dst;
451 
452 	/**
453 	 * Timeout for cancelling the reassembly. The timer is used
454 	 * also to detect if this reassembly slot is used or not.
455 	 */
456 	struct k_work_delayable timer;
457 
458 	/** Pointers to pending fragments */
459 	struct net_pkt *pkt[CONFIG_NET_IPV6_FRAGMENT_MAX_PKT];
460 
461 	/** IPv6 fragment identification */
462 	uint32_t id;
463 };
464 #else
465 struct net_ipv6_reassembly;
466 #endif
467 
468 /**
469  * @typedef net_ipv6_frag_cb_t
470  * @brief Callback used while iterating over pending IPv6 fragments.
471  *
472  * @param reass IPv6 fragment reassembly struct
473  * @param user_data A valid pointer on some user data or NULL
474  */
475 typedef void (*net_ipv6_frag_cb_t)(struct net_ipv6_reassembly *reass,
476 				   void *user_data);
477 
478 /**
479  * @brief Go through all the currently pending IPv6 fragments.
480  *
481  * @param cb Callback to call for each pending IPv6 fragment.
482  * @param user_data User specified data or NULL.
483  */
484 void net_ipv6_frag_foreach(net_ipv6_frag_cb_t cb, void *user_data);
485 
486 /**
487  * @brief Find the last IPv6 extension header in the network packet.
488  *
489  * @param pkt Network head packet.
490  * @param next_hdr_off Offset of the next header field that points
491  * to last header. This is returned to caller.
492  * @param last_hdr_off Offset of the last header field in the packet.
493  * This is returned to caller.
494  *
495  * @return 0 on success, a negative errno otherwise.
496  */
497 int net_ipv6_find_last_ext_hdr(struct net_pkt *pkt, uint16_t *next_hdr_off,
498 			       uint16_t *last_hdr_off);
499 
500 /**
501  * @brief Handles IPv6 fragmented packets.
502  *
503  * @param pkt     Network head packet.
504  * @param hdr     The IPv6 header of the current packet
505  * @param nexthdr IPv6 next header after fragment header part
506  *
507  * @return Return verdict about the packet
508  */
509 #if defined(CONFIG_NET_IPV6_FRAGMENT) && defined(CONFIG_NET_NATIVE_IPV6)
510 enum net_verdict net_ipv6_handle_fragment_hdr(struct net_pkt *pkt,
511 					      struct net_ipv6_hdr *hdr,
512 					      uint8_t nexthdr);
513 #else
514 static inline
net_ipv6_handle_fragment_hdr(struct net_pkt * pkt,struct net_ipv6_hdr * hdr,uint8_t nexthdr)515 enum net_verdict net_ipv6_handle_fragment_hdr(struct net_pkt *pkt,
516 					      struct net_ipv6_hdr *hdr,
517 					      uint8_t nexthdr)
518 {
519 	ARG_UNUSED(pkt);
520 	ARG_UNUSED(hdr);
521 	ARG_UNUSED(nexthdr);
522 
523 	return NET_DROP;
524 }
525 #endif /* CONFIG_NET_IPV6_FRAGMENT */
526 
527 #if defined(CONFIG_NET_NATIVE_IPV6)
528 void net_ipv6_init(void);
529 void net_ipv6_nbr_init(void);
530 #if defined(CONFIG_NET_IPV6_MLD)
531 void net_ipv6_mld_init(void);
532 #else
533 #define net_ipv6_mld_init(...)
534 #endif
535 #else
536 #define net_ipv6_init(...)
537 #define net_ipv6_nbr_init(...)
538 #endif
539 
540 /**
541  * @brief Decode DSCP value from TC field.
542  *
543  * @param tc TC field value from the IPv6 header.
544  *
545  * @return Decoded DSCP value.
546  */
net_ipv6_get_dscp(uint8_t tc)547 static inline uint8_t net_ipv6_get_dscp(uint8_t tc)
548 {
549 	return (tc & NET_IPV6_DSCP_MASK) >> NET_IPV6_DSCP_OFFSET;
550 }
551 
552 /**
553  * @brief Encode DSCP value into TC field.
554  *
555  * @param tc A pointer to the TC field.
556  * @param dscp DSCP value to set.
557  */
net_ipv6_set_dscp(uint8_t * tc,uint8_t dscp)558 static inline void net_ipv6_set_dscp(uint8_t *tc, uint8_t dscp)
559 {
560 	*tc &= ~NET_IPV6_DSCP_MASK;
561 	*tc |= (dscp << NET_IPV6_DSCP_OFFSET) & NET_IPV6_DSCP_MASK;
562 }
563 
564 /**
565  * @brief Convert DSCP value to priority.
566  *
567  * @param dscp DSCP value.
568  */
net_ipv6_dscp_to_priority(uint8_t dscp)569 static inline uint8_t net_ipv6_dscp_to_priority(uint8_t dscp)
570 {
571 	return dscp >> 3;
572 }
573 
574 /**
575  * @brief Decode ECN value from TC field.
576  *
577  * @param tc TC field value from the IPv6 header.
578  *
579  * @return Decoded ECN value.
580  */
net_ipv6_get_ecn(uint8_t tc)581 static inline uint8_t net_ipv6_get_ecn(uint8_t tc)
582 {
583 	return tc & NET_IPV6_ECN_MASK;
584 }
585 
586 /**
587  * @brief Encode ECN value into TC field.
588  *
589  * @param tc A pointer to the TC field.
590  * @param ecn ECN value to set.
591  */
net_ipv6_set_ecn(uint8_t * tc,uint8_t ecn)592 static inline void net_ipv6_set_ecn(uint8_t *tc, uint8_t ecn)
593 {
594 	*tc &= ~NET_IPV6_ECN_MASK;
595 	*tc |= ecn & NET_IPV6_ECN_MASK;
596 }
597 
598 
599 #endif /* __IPV6_H */
600