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 Look for a neighbor from it's address on an iface
275 *
276 * @param iface A valid pointer on a network interface
277 * @param addr The IPv6 address to match
278 *
279 * @return A valid pointer on a neighbor on success, NULL otherwise
280 */
281 #if defined(CONFIG_NET_IPV6_NBR_CACHE) && defined(CONFIG_NET_NATIVE_IPV6)
282 struct net_nbr *net_ipv6_nbr_lookup(struct net_if *iface,
283 struct in6_addr *addr);
284 #else
net_ipv6_nbr_lookup(struct net_if * iface,struct in6_addr * addr)285 static inline struct net_nbr *net_ipv6_nbr_lookup(struct net_if *iface,
286 struct in6_addr *addr)
287 {
288 return NULL;
289 }
290 #endif
291
292 /**
293 * @brief Get neighbor from its index.
294 *
295 * @param iface Network interface to match. If NULL, then use
296 * whatever interface there is configured for the neighbor address.
297 * @param idx Index of the link layer address in the address array
298 *
299 * @return A valid pointer on a neighbor on success, NULL otherwise
300 */
301 struct net_nbr *net_ipv6_get_nbr(struct net_if *iface, uint8_t idx);
302
303 /**
304 * @brief Look for a neighbor from it's link local address index
305 *
306 * @param iface Network interface to match. If NULL, then use
307 * whatever interface there is configured for the neighbor address.
308 * @param idx Index of the link layer address in the address array
309 *
310 * @return A valid pointer on a neighbor on success, NULL otherwise
311 */
312 #if defined(CONFIG_NET_IPV6_NBR_CACHE) && defined(CONFIG_NET_NATIVE_IPV6)
313 struct in6_addr *net_ipv6_nbr_lookup_by_index(struct net_if *iface,
314 uint8_t idx);
315 #else
316 static inline
net_ipv6_nbr_lookup_by_index(struct net_if * iface,uint8_t idx)317 struct in6_addr *net_ipv6_nbr_lookup_by_index(struct net_if *iface,
318 uint8_t idx)
319 {
320 return NULL;
321 }
322 #endif
323
324 /**
325 * @brief Add a neighbor to neighbor cache
326 *
327 * Add a neighbor to the cache after performing a lookup and in case
328 * there exists an entry in the cache update its state and lladdr.
329 *
330 * @param iface A valid pointer on a network interface
331 * @param addr Neighbor IPv6 address
332 * @param lladdr Neighbor link layer address
333 * @param is_router Set to true if the neighbor is a router, false
334 * otherwise
335 * @param state Initial state of the neighbor entry in the cache
336 *
337 * @return A valid pointer on a neighbor on success, NULL otherwise
338 */
339 #if defined(CONFIG_NET_IPV6_NBR_CACHE) && defined(CONFIG_NET_NATIVE_IPV6)
340 struct net_nbr *net_ipv6_nbr_add(struct net_if *iface,
341 const struct in6_addr *addr,
342 const struct net_linkaddr *lladdr,
343 bool is_router,
344 enum net_ipv6_nbr_state state);
345 #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)346 static inline struct net_nbr *net_ipv6_nbr_add(struct net_if *iface,
347 const struct in6_addr *addr,
348 const struct net_linkaddr *lladdr,
349 bool is_router,
350 enum net_ipv6_nbr_state state)
351 {
352 return NULL;
353 }
354 #endif
355
356 /**
357 * @brief Remove a neighbor from neighbor cache.
358 *
359 * @param iface A valid pointer on a network interface
360 * @param addr Neighbor IPv6 address
361 *
362 * @return True if neighbor could be removed, False otherwise
363 */
364 #if defined(CONFIG_NET_IPV6_NBR_CACHE) && defined(CONFIG_NET_NATIVE_IPV6)
365 bool net_ipv6_nbr_rm(struct net_if *iface, struct in6_addr *addr);
366 #else
net_ipv6_nbr_rm(struct net_if * iface,struct in6_addr * addr)367 static inline bool net_ipv6_nbr_rm(struct net_if *iface, struct in6_addr *addr)
368 {
369 return true;
370 }
371 #endif
372
373 /**
374 * @brief Go through all the neighbors and call callback for each of them.
375 *
376 * @param cb User supplied callback function to call.
377 * @param user_data User specified data.
378 */
379 #if defined(CONFIG_NET_IPV6_NBR_CACHE) && defined(CONFIG_NET_NATIVE_IPV6)
380 void net_ipv6_nbr_foreach(net_nbr_cb_t cb, void *user_data);
381 #else /* CONFIG_NET_IPV6_NBR_CACHE */
net_ipv6_nbr_foreach(net_nbr_cb_t cb,void * user_data)382 static inline void net_ipv6_nbr_foreach(net_nbr_cb_t cb, void *user_data)
383 {
384 return;
385 }
386 #endif /* CONFIG_NET_IPV6_NBR_CACHE */
387
388 /**
389 * @brief Set the neighbor reachable timer.
390 *
391 * @param iface A valid pointer on a network interface
392 * @param nbr Neighbor struct pointer
393 */
394 #if defined(CONFIG_NET_IPV6_ND) && defined(CONFIG_NET_NATIVE_IPV6)
395 void net_ipv6_nbr_set_reachable_timer(struct net_if *iface,
396 struct net_nbr *nbr);
397
398 #else /* CONFIG_NET_IPV6_ND */
net_ipv6_nbr_set_reachable_timer(struct net_if * iface,struct net_nbr * nbr)399 static inline void net_ipv6_nbr_set_reachable_timer(struct net_if *iface,
400 struct net_nbr *nbr)
401 {
402 }
403 #endif
404
405 #if defined(CONFIG_NET_IPV6_FRAGMENT)
406 /** Store pending IPv6 fragment information that is needed for reassembly. */
407 struct net_ipv6_reassembly {
408 /** IPv6 source address of the fragment */
409 struct in6_addr src;
410
411 /** IPv6 destination address of the fragment */
412 struct in6_addr dst;
413
414 /**
415 * Timeout for cancelling the reassembly. The timer is used
416 * also to detect if this reassembly slot is used or not.
417 */
418 struct k_work_delayable timer;
419
420 /** Pointers to pending fragments */
421 struct net_pkt *pkt[CONFIG_NET_IPV6_FRAGMENT_MAX_PKT];
422
423 /** IPv6 fragment identification */
424 uint32_t id;
425 };
426 #else
427 struct net_ipv6_reassembly;
428 #endif
429
430 /**
431 * @typedef net_ipv6_frag_cb_t
432 * @brief Callback used while iterating over pending IPv6 fragments.
433 *
434 * @param reass IPv6 fragment reassembly struct
435 * @param user_data A valid pointer on some user data or NULL
436 */
437 typedef void (*net_ipv6_frag_cb_t)(struct net_ipv6_reassembly *reass,
438 void *user_data);
439
440 /**
441 * @brief Go through all the currently pending IPv6 fragments.
442 *
443 * @param cb Callback to call for each pending IPv6 fragment.
444 * @param user_data User specified data or NULL.
445 */
446 void net_ipv6_frag_foreach(net_ipv6_frag_cb_t cb, void *user_data);
447
448 /**
449 * @brief Find the last IPv6 extension header in the network packet.
450 *
451 * @param pkt Network head packet.
452 * @param next_hdr_off Offset of the next header field that points
453 * to last header. This is returned to caller.
454 * @param last_hdr_off Offset of the last header field in the packet.
455 * This is returned to caller.
456 *
457 * @return 0 on success, a negative errno otherwise.
458 */
459 int net_ipv6_find_last_ext_hdr(struct net_pkt *pkt, uint16_t *next_hdr_off,
460 uint16_t *last_hdr_off);
461
462 /**
463 * @brief Handles IPv6 fragmented packets.
464 *
465 * @param pkt Network head packet.
466 * @param hdr The IPv6 header of the current packet
467 * @param nexthdr IPv6 next header after fragment header part
468 *
469 * @return Return verdict about the packet
470 */
471 #if defined(CONFIG_NET_IPV6_FRAGMENT) && defined(CONFIG_NET_NATIVE_IPV6)
472 enum net_verdict net_ipv6_handle_fragment_hdr(struct net_pkt *pkt,
473 struct net_ipv6_hdr *hdr,
474 uint8_t nexthdr);
475 #else
476 static inline
net_ipv6_handle_fragment_hdr(struct net_pkt * pkt,struct net_ipv6_hdr * hdr,uint8_t nexthdr)477 enum net_verdict net_ipv6_handle_fragment_hdr(struct net_pkt *pkt,
478 struct net_ipv6_hdr *hdr,
479 uint8_t nexthdr)
480 {
481 ARG_UNUSED(pkt);
482 ARG_UNUSED(hdr);
483 ARG_UNUSED(nexthdr);
484
485 return NET_DROP;
486 }
487 #endif /* CONFIG_NET_IPV6_FRAGMENT */
488
489 #if defined(CONFIG_NET_NATIVE_IPV6)
490 void net_ipv6_init(void);
491 void net_ipv6_nbr_init(void);
492 #if defined(CONFIG_NET_IPV6_MLD)
493 void net_ipv6_mld_init(void);
494 #else
495 #define net_ipv6_mld_init(...)
496 #endif
497 #else
498 #define net_ipv6_init(...)
499 #define net_ipv6_nbr_init(...)
500 #endif
501
502 /**
503 * @brief Decode DSCP value from TC field.
504 *
505 * @param tc TC field value from the IPv6 header.
506 *
507 * @return Decoded DSCP value.
508 */
net_ipv6_get_dscp(uint8_t tc)509 static inline uint8_t net_ipv6_get_dscp(uint8_t tc)
510 {
511 return (tc & NET_IPV6_DSCP_MASK) >> NET_IPV6_DSCP_OFFSET;
512 }
513
514 /**
515 * @brief Encode DSCP value into TC field.
516 *
517 * @param tc A pointer to the TC field.
518 * @param dscp DSCP value to set.
519 */
net_ipv6_set_dscp(uint8_t * tc,uint8_t dscp)520 static inline void net_ipv6_set_dscp(uint8_t *tc, uint8_t dscp)
521 {
522 *tc &= ~NET_IPV6_DSCP_MASK;
523 *tc |= (dscp << NET_IPV6_DSCP_OFFSET) & NET_IPV6_DSCP_MASK;
524 }
525
526 /**
527 * @brief Convert DSCP value to priority.
528 *
529 * @param dscp DSCP value.
530 */
net_ipv6_dscp_to_priority(uint8_t dscp)531 static inline uint8_t net_ipv6_dscp_to_priority(uint8_t dscp)
532 {
533 return dscp >> 3;
534 }
535
536 /**
537 * @brief Decode ECN value from TC field.
538 *
539 * @param tc TC field value from the IPv6 header.
540 *
541 * @return Decoded ECN value.
542 */
net_ipv6_get_ecn(uint8_t tc)543 static inline uint8_t net_ipv6_get_ecn(uint8_t tc)
544 {
545 return tc & NET_IPV6_ECN_MASK;
546 }
547
548 /**
549 * @brief Encode ECN value into TC field.
550 *
551 * @param tc A pointer to the TC field.
552 * @param ecn ECN value to set.
553 */
net_ipv6_set_ecn(uint8_t * tc,uint8_t ecn)554 static inline void net_ipv6_set_ecn(uint8_t *tc, uint8_t ecn)
555 {
556 *tc &= ~NET_IPV6_ECN_MASK;
557 *tc |= ecn & NET_IPV6_ECN_MASK;
558 }
559
560
561 #endif /* __IPV6_H */
562