1 /** @file
2  @brief ICMPv6 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 __ICMPV6_H
14 #define __ICMPV6_H
15 
16 #include <sys/slist.h>
17 #include <zephyr/types.h>
18 
19 #include <net/net_ip.h>
20 #include <net/net_pkt.h>
21 
22 struct net_icmpv6_ns_hdr {
23 	uint32_t reserved;
24 	struct in6_addr tgt;
25 } __packed;
26 
27 struct net_icmpv6_nd_opt_hdr {
28 	uint8_t type;
29 	uint8_t len;
30 } __packed;
31 
32 struct net_icmpv6_na_hdr {
33 	uint8_t flags;
34 	uint8_t reserved[3];
35 	struct in6_addr tgt;
36 } __packed;
37 
38 struct net_icmpv6_rs_hdr {
39 	uint32_t reserved;
40 } __packed;
41 
42 struct net_icmpv6_ra_hdr {
43 	uint8_t cur_hop_limit;
44 	uint8_t flags;
45 	uint16_t router_lifetime;
46 	uint32_t reachable_time;
47 	uint32_t retrans_timer;
48 } __packed;
49 
50 struct net_icmpv6_nd_opt_mtu {
51 	uint16_t reserved;
52 	uint32_t mtu;
53 } __packed;
54 
55 struct net_icmpv6_nd_opt_prefix_info {
56 	uint8_t prefix_len;
57 	uint8_t flags;
58 	uint32_t valid_lifetime;
59 	uint32_t preferred_lifetime;
60 	uint32_t reserved;
61 	struct in6_addr prefix;
62 } __packed;
63 
64 struct net_icmpv6_nd_opt_6co {
65 	uint8_t context_len;
66 	uint8_t flag; /*res:3,c:1,cid:4 */
67 	uint16_t reserved;
68 	uint16_t lifetime;
69 	struct in6_addr prefix;
70 } __packed;
71 
72 struct net_icmpv6_echo_req {
73 	uint16_t identifier;
74 	uint16_t sequence;
75 } __packed;
76 
77 struct net_icmpv6_mld_query {
78 	uint16_t max_response_code;
79 	uint16_t reserved;
80 	struct in6_addr mcast_address;
81 	uint16_t flagg; /*S, QRV & QQIC */
82 	uint16_t num_sources;
83 } __packed;
84 
85 struct net_icmpv6_mld_mcast_record {
86 	uint8_t record_type;
87 	uint8_t aux_data_len;
88 	uint16_t num_sources;
89 	struct in6_addr mcast_address;
90 } __packed;
91 
92 
93 #define NET_ICMPV6_ND_O_FLAG(flag) ((flag) & 0x40)
94 #define NET_ICMPV6_ND_M_FLAG(flag) ((flag) & 0x80)
95 
96 #define NET_ICMPV6_ND_OPT_SLLAO       1
97 #define NET_ICMPV6_ND_OPT_TLLAO       2
98 #define NET_ICMPV6_ND_OPT_PREFIX_INFO 3
99 #define NET_ICMPV6_ND_OPT_MTU         5
100 #define NET_ICMPV6_ND_OPT_ROUTE       24
101 #define NET_ICMPV6_ND_OPT_RDNSS       25
102 #define NET_ICMPV6_ND_OPT_DNSSL       31
103 #define NET_ICMPV6_ND_OPT_6CO         34
104 
105 #define NET_ICMPV6_OPT_TYPE_OFFSET   0
106 #define NET_ICMPV6_OPT_LEN_OFFSET    1
107 #define NET_ICMPV6_OPT_DATA_OFFSET   2
108 
109 #define NET_ICMPV6_NA_FLAG_ROUTER     0x80
110 #define NET_ICMPV6_NA_FLAG_SOLICITED  0x40
111 #define NET_ICMPV6_NA_FLAG_OVERRIDE   0x20
112 #define NET_ICMPV6_RA_FLAG_ONLINK     0x80
113 #define NET_ICMPV6_RA_FLAG_AUTONOMOUS 0x40
114 
115 #define NET_ICMPV6_DST_UNREACH    1	/* Destination unreachable */
116 #define NET_ICMPV6_PACKET_TOO_BIG 2	/* Packet too big */
117 #define NET_ICMPV6_TIME_EXCEEDED  3	/* Time exceeded */
118 #define NET_ICMPV6_PARAM_PROBLEM  4	/* IPv6 header is bad */
119 #define NET_ICMPV6_ECHO_REQUEST 128
120 #define NET_ICMPV6_ECHO_REPLY   129
121 #define NET_ICMPV6_MLD_QUERY    130	/* Multicast Listener Query */
122 #define NET_ICMPV6_RS           133	/* Router Solicitation */
123 #define NET_ICMPV6_RA           134	/* Router Advertisement */
124 #define NET_ICMPV6_NS           135	/* Neighbor Solicitation */
125 #define NET_ICMPV6_NA           136	/* Neighbor Advertisement */
126 #define NET_ICMPV6_MLDv2        143	/* Multicast Listener Report v2 */
127 
128 /* Codes for ICMPv6 Destination Unreachable message */
129 #define NET_ICMPV6_DST_UNREACH_NO_ROUTE  0 /* No route to destination */
130 #define NET_ICMPV6_DST_UNREACH_ADMIN     1 /* Admin prohibited communication */
131 #define NET_ICMPV6_DST_UNREACH_SCOPE     2 /* Beoynd scope of source address */
132 #define NET_ICMPV6_DST_UNREACH_NO_ADDR   3 /* Address unrechable */
133 #define NET_ICMPV6_DST_UNREACH_NO_PORT   4 /* Port unreachable */
134 #define NET_ICMPV6_DST_UNREACH_SRC_ADDR  5 /* Source address failed */
135 #define NET_ICMPV6_DST_UNREACH_REJ_ROUTE 6 /* Reject route to destination */
136 
137 /* Codes for ICMPv6 Parameter Problem message */
138 #define NET_ICMPV6_PARAM_PROB_HEADER     0 /* Erroneous header field */
139 #define NET_ICMPV6_PARAM_PROB_NEXTHEADER 1 /* Unrecognized next header */
140 #define NET_ICMPV6_PARAM_PROB_OPTION     2 /* Unrecognized option */
141 
142 /* ICMPv6 header has 4 unused bytes that must be zero, RFC 4443 ch 3.1 */
143 #define NET_ICMPV6_UNUSED_LEN 4
144 
145 typedef enum net_verdict (*icmpv6_callback_handler_t)(
146 						struct net_pkt *pkt,
147 						struct net_ipv6_hdr *ip_hdr,
148 						struct net_icmp_hdr *icmp_hdr);
149 
150 const char *net_icmpv6_type2str(int icmpv6_type);
151 
152 struct net_icmpv6_handler {
153 	sys_snode_t node;
154 	icmpv6_callback_handler_t handler;
155 	uint8_t type;
156 	uint8_t code;
157 };
158 
159 /**
160  * @brief Send ICMPv6 error message.
161  * @param pkt Network packet that this error is related to.
162  * @param type Type of the error message.
163  * @param code Code of the type of the error message.
164  * @param param Optional parameter value for this error. Depending on type
165  * and code this gives extra information to the recipient. Set 0 if unsure
166  * what value to use.
167  * @return Return 0 if the sending succeed, <0 otherwise.
168  */
169 int net_icmpv6_send_error(struct net_pkt *pkt, uint8_t type, uint8_t code,
170 			  uint32_t param);
171 
172 /**
173  * @brief Send ICMPv6 echo request message.
174  *
175  * @param iface Network interface.
176  * @param dst IPv6 address of the target host.
177  * @param identifier An identifier to aid in matching Echo Replies
178  * to this Echo Request. May be zero.
179  * @param sequence A sequence number to aid in matching Echo Replies
180  * to this Echo Request. May be zero.
181  * @param data Arbitrary payload data that will be included in the
182  * Echo Reply verbatim. May be zero.
183  * @param data_size Size of the Payload Data in bytes. May be zero.
184  *
185  * @return Return 0 if the sending succeed, <0 otherwise.
186  */
187 #if defined(CONFIG_NET_NATIVE_IPV6)
188 int net_icmpv6_send_echo_request(struct net_if *iface,
189 				 struct in6_addr *dst,
190 				 uint16_t identifier,
191 				 uint16_t sequence,
192 				 const void *data,
193 				 size_t data_size);
194 #else
net_icmpv6_send_echo_request(struct net_if * iface,struct in6_addr * dst,uint16_t identifier,uint16_t sequence,const void * data,size_t data_size)195 static inline int net_icmpv6_send_echo_request(struct net_if *iface,
196 					       struct in6_addr *dst,
197 					       uint16_t identifier,
198 					       uint16_t sequence,
199 					       const void *data,
200 					       size_t data_size)
201 {
202 	ARG_UNUSED(iface);
203 	ARG_UNUSED(dst);
204 	ARG_UNUSED(identifier);
205 	ARG_UNUSED(sequence);
206 	ARG_UNUSED(data);
207 	ARG_UNUSED(data_size);
208 
209 	return -ENOTSUP;
210 }
211 #endif
212 
213 #if defined(CONFIG_NET_NATIVE_IPV6)
214 void net_icmpv6_register_handler(struct net_icmpv6_handler *handler);
215 void net_icmpv6_unregister_handler(struct net_icmpv6_handler *handler);
216 enum net_verdict net_icmpv6_input(struct net_pkt *pkt,
217 				  struct net_ipv6_hdr *ip_hdr);
218 
219 int net_icmpv6_create(struct net_pkt *pkt, uint8_t icmp_type, uint8_t icmp_code);
220 int net_icmpv6_finalize(struct net_pkt *pkt);
221 
222 void net_icmpv6_init(void);
223 #else
224 #define net_icmpv6_init(...)
225 #define net_icmpv6_register_handler(...)
226 #define net_icmpv6_unregister_handler(...)
227 #endif
228 
229 #endif /* __ICMPV6_H */
230