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 <zephyr/sys/slist.h>
17 #include <zephyr/types.h>
18
19 #include <zephyr/net/net_ip.h>
20 #include <zephyr/net/net_pkt.h>
21
22 struct net_icmpv6_ns_hdr {
23 uint32_t reserved;
24 uint8_t tgt[NET_IPV6_ADDR_SIZE];
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 uint8_t tgt[NET_IPV6_ADDR_SIZE];
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 uint8_t prefix[NET_IPV6_ADDR_SIZE];
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 uint8_t prefix[NET_IPV6_ADDR_SIZE];
70 } __packed;
71
72 /* RFC 4191, ch. 2.3 */
73 struct net_icmpv6_nd_opt_route_info {
74 uint8_t prefix_len;
75 struct {
76 #ifdef CONFIG_LITTLE_ENDIAN
77 uint8_t reserved_2 :3;
78 uint8_t prf :2;
79 uint8_t reserved_1 :3;
80 #else
81 uint8_t reserved_1 :3;
82 uint8_t prf :2;
83 uint8_t reserved_2 :3;
84 #endif
85 } flags;
86 uint32_t route_lifetime;
87 /* Variable-length prefix field follows, can be 0, 8 or 16 bytes
88 * depending on the option length.
89 */
90 } __packed;
91
92 struct net_icmpv6_nd_opt_rdnss {
93 uint16_t reserved;
94 uint32_t lifetime;
95 /* Variable-length DNS server address follows,
96 * depending on the option length.
97 */
98 } __packed;
99
100 struct net_icmpv6_echo_req {
101 uint16_t identifier;
102 uint16_t sequence;
103 } __packed;
104
105 struct net_icmpv6_mld_query {
106 uint16_t max_response_code;
107 uint16_t reserved;
108 uint8_t mcast_address[NET_IPV6_ADDR_SIZE];
109 uint16_t flagg; /*S, QRV & QQIC */
110 uint16_t num_sources;
111 } __packed;
112
113 struct net_icmpv6_mld_mcast_record {
114 uint8_t record_type;
115 uint8_t aux_data_len;
116 uint16_t num_sources;
117 uint8_t mcast_address[NET_IPV6_ADDR_SIZE];
118 } __packed;
119
120
121 #define NET_ICMPV6_ND_O_FLAG(flag) ((flag) & 0x40)
122 #define NET_ICMPV6_ND_M_FLAG(flag) ((flag) & 0x80)
123
124 #define NET_ICMPV6_ND_OPT_SLLAO 1
125 #define NET_ICMPV6_ND_OPT_TLLAO 2
126 #define NET_ICMPV6_ND_OPT_PREFIX_INFO 3
127 #define NET_ICMPV6_ND_OPT_MTU 5
128 #define NET_ICMPV6_ND_OPT_ROUTE 24
129 #define NET_ICMPV6_ND_OPT_RDNSS 25
130 #define NET_ICMPV6_ND_OPT_DNSSL 31
131 #define NET_ICMPV6_ND_OPT_6CO 34
132
133 #define NET_ICMPV6_OPT_TYPE_OFFSET 0
134 #define NET_ICMPV6_OPT_LEN_OFFSET 1
135 #define NET_ICMPV6_OPT_DATA_OFFSET 2
136
137 #define NET_ICMPV6_NA_FLAG_ROUTER 0x80
138 #define NET_ICMPV6_NA_FLAG_SOLICITED 0x40
139 #define NET_ICMPV6_NA_FLAG_OVERRIDE 0x20
140 #define NET_ICMPV6_RA_FLAG_ONLINK 0x80
141 #define NET_ICMPV6_RA_FLAG_AUTONOMOUS 0x40
142
143 #define NET_ICMPV6_DST_UNREACH 1 /* Destination unreachable */
144 #define NET_ICMPV6_PACKET_TOO_BIG 2 /* Packet too big */
145 #define NET_ICMPV6_TIME_EXCEEDED 3 /* Time exceeded */
146 #define NET_ICMPV6_PARAM_PROBLEM 4 /* IPv6 header is bad */
147 #define NET_ICMPV6_ECHO_REQUEST 128
148 #define NET_ICMPV6_ECHO_REPLY 129
149 #define NET_ICMPV6_MLD_QUERY 130 /* Multicast Listener Query */
150 #define NET_ICMPV6_RS 133 /* Router Solicitation */
151 #define NET_ICMPV6_RA 134 /* Router Advertisement */
152 #define NET_ICMPV6_NS 135 /* Neighbor Solicitation */
153 #define NET_ICMPV6_NA 136 /* Neighbor Advertisement */
154 #define NET_ICMPV6_MLDv2 143 /* Multicast Listener Report v2 */
155
156 /* Codes for ICMPv6 Destination Unreachable message */
157 #define NET_ICMPV6_DST_UNREACH_NO_ROUTE 0 /* No route to destination */
158 #define NET_ICMPV6_DST_UNREACH_ADMIN 1 /* Admin prohibited communication */
159 #define NET_ICMPV6_DST_UNREACH_SCOPE 2 /* Beyond scope of source address */
160 #define NET_ICMPV6_DST_UNREACH_NO_ADDR 3 /* Address unreachable */
161 #define NET_ICMPV6_DST_UNREACH_NO_PORT 4 /* Port unreachable */
162 #define NET_ICMPV6_DST_UNREACH_SRC_ADDR 5 /* Source address failed */
163 #define NET_ICMPV6_DST_UNREACH_REJ_ROUTE 6 /* Reject route to destination */
164
165 /* Codes for ICMPv6 Parameter Problem message */
166 #define NET_ICMPV6_PARAM_PROB_HEADER 0 /* Erroneous header field */
167 #define NET_ICMPV6_PARAM_PROB_NEXTHEADER 1 /* Unrecognized next header */
168 #define NET_ICMPV6_PARAM_PROB_OPTION 2 /* Unrecognized option */
169
170 /* ICMPv6 header has 4 unused bytes that must be zero, RFC 4443 ch 3.1 */
171 #define NET_ICMPV6_UNUSED_LEN 4
172
173 typedef enum net_verdict (*icmpv6_callback_handler_t)(
174 struct net_pkt *pkt,
175 struct net_ipv6_hdr *ip_hdr,
176 struct net_icmp_hdr *icmp_hdr);
177
178 const char *net_icmpv6_type2str(int icmpv6_type);
179
180 struct net_icmpv6_handler {
181 sys_snode_t node;
182 icmpv6_callback_handler_t handler;
183 uint8_t type;
184 uint8_t code;
185 };
186
187 /**
188 * @brief Send ICMPv6 error message.
189 * @param pkt Network packet that this error is related to.
190 * @param type Type of the error message.
191 * @param code Code of the type of the error message.
192 * @param param Optional parameter value for this error. Depending on type
193 * and code this gives extra information to the recipient. Set 0 if unsure
194 * what value to use.
195 * @return Return 0 if the sending succeed, <0 otherwise.
196 */
197 int net_icmpv6_send_error(struct net_pkt *pkt, uint8_t type, uint8_t code,
198 uint32_t param);
199
200 /**
201 * @brief Send ICMPv6 echo request message.
202 *
203 * @param iface Network interface.
204 * @param dst IPv6 address of the target host.
205 * @param identifier An identifier to aid in matching Echo Replies
206 * to this Echo Request. May be zero.
207 * @param sequence A sequence number to aid in matching Echo Replies
208 * to this Echo Request. May be zero.
209 * @param tc IPv6 Traffic Class field value. Represents combined DSCP and
210 * ECN values.
211 * @param data Arbitrary payload data that will be included in the
212 * Echo Reply verbatim. May be NULL.
213 * @param data_size Size of the Payload Data in bytes. May be zero. In case data
214 * pointer is NULL, the function will generate the payload up to the requested
215 * size.
216 *
217 * @return Return 0 if the sending succeed, <0 otherwise.
218 */
219 #if defined(CONFIG_NET_NATIVE_IPV6)
220 int net_icmpv6_send_echo_request(struct net_if *iface,
221 struct in6_addr *dst,
222 uint16_t identifier,
223 uint16_t sequence,
224 uint8_t tc,
225 const void *data,
226 size_t data_size);
227 #else
net_icmpv6_send_echo_request(struct net_if * iface,struct in6_addr * dst,uint16_t identifier,uint16_t sequence,uint8_t tc,const void * data,size_t data_size)228 static inline int net_icmpv6_send_echo_request(struct net_if *iface,
229 struct in6_addr *dst,
230 uint16_t identifier,
231 uint16_t sequence,
232 uint8_t tc,
233 const void *data,
234 size_t data_size)
235 {
236 ARG_UNUSED(iface);
237 ARG_UNUSED(dst);
238 ARG_UNUSED(identifier);
239 ARG_UNUSED(sequence);
240 ARG_UNUSED(tc);
241 ARG_UNUSED(data);
242 ARG_UNUSED(data_size);
243
244 return -ENOTSUP;
245 }
246 #endif
247
248 #if defined(CONFIG_NET_NATIVE_IPV6)
249 void net_icmpv6_register_handler(struct net_icmpv6_handler *handler);
250 void net_icmpv6_unregister_handler(struct net_icmpv6_handler *handler);
251 enum net_verdict net_icmpv6_input(struct net_pkt *pkt,
252 struct net_ipv6_hdr *ip_hdr);
253
254 int net_icmpv6_create(struct net_pkt *pkt, uint8_t icmp_type, uint8_t icmp_code);
255 int net_icmpv6_finalize(struct net_pkt *pkt);
256
257 void net_icmpv6_init(void);
258 #else
259 #define net_icmpv6_init(...)
260 #define net_icmpv6_register_handler(...)
261 #define net_icmpv6_unregister_handler(...)
262 #endif
263
264 #endif /* __ICMPV6_H */
265