3 * Guillaume Subiron, Yann Bordenave, Serigne Modou Wagne.
6 #include "qemu/osdep.h"
9 #include "qemu/timer.h"
10 #include "qemu/error-report.h"
13 #define NDP_Interval g_rand_int_range(slirp->grand, \
14 NDP_MinRtrAdvInterval, NDP_MaxRtrAdvInterval)
16 static void ra_timer_handler(void *opaque
)
18 Slirp
*slirp
= opaque
;
19 timer_mod(slirp
->ra_timer
,
20 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + NDP_Interval
);
24 void icmp6_init(Slirp
*slirp
)
26 if (!slirp
->in6_enabled
) {
30 slirp
->ra_timer
= timer_new_ms(QEMU_CLOCK_VIRTUAL
, ra_timer_handler
, slirp
);
31 timer_mod(slirp
->ra_timer
,
32 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL
) + NDP_Interval
);
35 void icmp6_cleanup(Slirp
*slirp
)
37 if (!slirp
->in6_enabled
) {
41 timer_del(slirp
->ra_timer
);
42 timer_free(slirp
->ra_timer
);
45 static void icmp6_send_echoreply(struct mbuf
*m
, Slirp
*slirp
, struct ip6
*ip
,
48 struct mbuf
*t
= m_get(slirp
);
49 t
->m_len
= sizeof(struct ip6
) + ntohs(ip
->ip_pl
);
50 memcpy(t
->m_data
, m
->m_data
, t
->m_len
);
53 struct ip6
*rip
= mtod(t
, struct ip6
*);
54 rip
->ip_dst
= ip
->ip_src
;
55 rip
->ip_src
= ip
->ip_dst
;
58 t
->m_data
+= sizeof(struct ip6
);
59 struct icmp6
*ricmp
= mtod(t
, struct icmp6
*);
60 ricmp
->icmp6_type
= ICMP6_ECHO_REPLY
;
61 ricmp
->icmp6_cksum
= 0;
64 t
->m_data
-= sizeof(struct ip6
);
65 ricmp
->icmp6_cksum
= ip6_cksum(t
);
67 ip6_output(NULL
, t
, 0);
70 void icmp6_send_error(struct mbuf
*m
, uint8_t type
, uint8_t code
)
72 Slirp
*slirp
= m
->slirp
;
74 struct ip6
*ip
= mtod(m
, struct ip6
*);
76 DEBUG_CALL("icmp6_send_error");
77 DEBUG_ARGS((dfd
, " type = %d, code = %d\n", type
, code
));
79 if (IN6_IS_ADDR_MULTICAST(&ip
->ip_src
) ||
80 IN6_IS_ADDR_UNSPECIFIED(&ip
->ip_src
)) {
81 /* TODO icmp error? */
88 struct ip6
*rip
= mtod(t
, struct ip6
*);
89 rip
->ip_src
= (struct in6_addr
)LINKLOCAL_ADDR
;
90 rip
->ip_dst
= ip
->ip_src
;
91 #if !defined(_WIN32) || (_WIN32_WINNT >= 0x0600)
92 char addrstr
[INET6_ADDRSTRLEN
];
93 inet_ntop(AF_INET6
, &rip
->ip_dst
, addrstr
, INET6_ADDRSTRLEN
);
94 DEBUG_ARG("target = %s", addrstr
);
97 rip
->ip_nh
= IPPROTO_ICMPV6
;
98 const int error_data_len
= min(m
->m_len
,
99 IF_MTU
- (sizeof(struct ip6
) + ICMP6_ERROR_MINLEN
));
100 rip
->ip_pl
= htons(ICMP6_ERROR_MINLEN
+ error_data_len
);
101 t
->m_len
= sizeof(struct ip6
) + ntohs(rip
->ip_pl
);
104 t
->m_data
+= sizeof(struct ip6
);
105 struct icmp6
*ricmp
= mtod(t
, struct icmp6
*);
106 ricmp
->icmp6_type
= type
;
107 ricmp
->icmp6_code
= code
;
108 ricmp
->icmp6_cksum
= 0;
113 ricmp
->icmp6_err
.unused
= 0;
116 ricmp
->icmp6_err
.mtu
= htonl(IF_MTU
);
118 case ICMP6_PARAMPROB
:
119 /* TODO: Handle this case */
122 g_assert_not_reached();
125 t
->m_data
+= ICMP6_ERROR_MINLEN
;
126 memcpy(t
->m_data
, m
->m_data
, error_data_len
);
129 t
->m_data
-= ICMP6_ERROR_MINLEN
;
130 t
->m_data
-= sizeof(struct ip6
);
131 ricmp
->icmp6_cksum
= ip6_cksum(t
);
133 ip6_output(NULL
, t
, 0);
137 * Send NDP Router Advertisement
139 void ndp_send_ra(Slirp
*slirp
)
141 DEBUG_CALL("ndp_send_ra");
143 /* Build IPv6 packet */
144 struct mbuf
*t
= m_get(slirp
);
145 struct ip6
*rip
= mtod(t
, struct ip6
*);
146 rip
->ip_src
= (struct in6_addr
)LINKLOCAL_ADDR
;
147 rip
->ip_dst
= (struct in6_addr
)ALLNODES_MULTICAST
;
148 rip
->ip_nh
= IPPROTO_ICMPV6
;
149 rip
->ip_pl
= htons(ICMP6_NDP_RA_MINLEN
150 + NDPOPT_LINKLAYER_LEN
151 + NDPOPT_PREFIXINFO_LEN
);
152 t
->m_len
= sizeof(struct ip6
) + ntohs(rip
->ip_pl
);
154 /* Build ICMPv6 packet */
155 t
->m_data
+= sizeof(struct ip6
);
156 struct icmp6
*ricmp
= mtod(t
, struct icmp6
*);
157 ricmp
->icmp6_type
= ICMP6_NDP_RA
;
158 ricmp
->icmp6_code
= 0;
159 ricmp
->icmp6_cksum
= 0;
162 ricmp
->icmp6_nra
.chl
= NDP_AdvCurHopLimit
;
163 ricmp
->icmp6_nra
.M
= NDP_AdvManagedFlag
;
164 ricmp
->icmp6_nra
.O
= NDP_AdvOtherConfigFlag
;
165 ricmp
->icmp6_nra
.reserved
= 0;
166 ricmp
->icmp6_nra
.lifetime
= htons(NDP_AdvDefaultLifetime
);
167 ricmp
->icmp6_nra
.reach_time
= htonl(NDP_AdvReachableTime
);
168 ricmp
->icmp6_nra
.retrans_time
= htonl(NDP_AdvRetransTime
);
170 /* Source link-layer address (NDP option) */
171 t
->m_data
+= ICMP6_NDP_RA_MINLEN
;
172 struct ndpopt
*opt
= mtod(t
, struct ndpopt
*);
173 opt
->ndpopt_type
= NDPOPT_LINKLAYER_SOURCE
;
174 opt
->ndpopt_len
= NDPOPT_LINKLAYER_LEN
/ 8;
175 in6_compute_ethaddr(rip
->ip_src
, opt
->ndpopt_linklayer
);
177 /* Prefix information (NDP option) */
178 t
->m_data
+= NDPOPT_LINKLAYER_LEN
;
179 struct ndpopt
*opt2
= mtod(t
, struct ndpopt
*);
180 opt2
->ndpopt_type
= NDPOPT_PREFIX_INFO
;
181 opt2
->ndpopt_len
= NDPOPT_PREFIXINFO_LEN
/ 8;
182 opt2
->ndpopt_prefixinfo
.prefix_length
= slirp
->vprefix_len
;
183 opt2
->ndpopt_prefixinfo
.L
= 1;
184 opt2
->ndpopt_prefixinfo
.A
= 1;
185 opt2
->ndpopt_prefixinfo
.reserved1
= 0;
186 opt2
->ndpopt_prefixinfo
.valid_lt
= htonl(NDP_AdvValidLifetime
);
187 opt2
->ndpopt_prefixinfo
.pref_lt
= htonl(NDP_AdvPrefLifetime
);
188 opt2
->ndpopt_prefixinfo
.reserved2
= 0;
189 opt2
->ndpopt_prefixinfo
.prefix
= slirp
->vprefix_addr6
;
191 /* ICMPv6 Checksum */
192 t
->m_data
-= NDPOPT_LINKLAYER_LEN
;
193 t
->m_data
-= ICMP6_NDP_RA_MINLEN
;
194 t
->m_data
-= sizeof(struct ip6
);
195 ricmp
->icmp6_cksum
= ip6_cksum(t
);
197 ip6_output(NULL
, t
, 0);
201 * Send NDP Neighbor Solitication
203 void ndp_send_ns(Slirp
*slirp
, struct in6_addr addr
)
205 DEBUG_CALL("ndp_send_ns");
206 #if !defined(_WIN32) || (_WIN32_WINNT >= 0x0600)
207 char addrstr
[INET6_ADDRSTRLEN
];
208 inet_ntop(AF_INET6
, &addr
, addrstr
, INET6_ADDRSTRLEN
);
209 DEBUG_ARG("target = %s", addrstr
);
212 /* Build IPv6 packet */
213 struct mbuf
*t
= m_get(slirp
);
214 struct ip6
*rip
= mtod(t
, struct ip6
*);
215 rip
->ip_src
= slirp
->vhost_addr6
;
216 rip
->ip_dst
= (struct in6_addr
)SOLICITED_NODE_PREFIX
;
217 memcpy(&rip
->ip_dst
.s6_addr
[13], &addr
.s6_addr
[13], 3);
218 rip
->ip_nh
= IPPROTO_ICMPV6
;
219 rip
->ip_pl
= htons(ICMP6_NDP_NS_MINLEN
+ NDPOPT_LINKLAYER_LEN
);
220 t
->m_len
= sizeof(struct ip6
) + ntohs(rip
->ip_pl
);
222 /* Build ICMPv6 packet */
223 t
->m_data
+= sizeof(struct ip6
);
224 struct icmp6
*ricmp
= mtod(t
, struct icmp6
*);
225 ricmp
->icmp6_type
= ICMP6_NDP_NS
;
226 ricmp
->icmp6_code
= 0;
227 ricmp
->icmp6_cksum
= 0;
230 ricmp
->icmp6_nns
.reserved
= 0;
231 ricmp
->icmp6_nns
.target
= addr
;
233 /* Build NDP option */
234 t
->m_data
+= ICMP6_NDP_NS_MINLEN
;
235 struct ndpopt
*opt
= mtod(t
, struct ndpopt
*);
236 opt
->ndpopt_type
= NDPOPT_LINKLAYER_SOURCE
;
237 opt
->ndpopt_len
= NDPOPT_LINKLAYER_LEN
/ 8;
238 in6_compute_ethaddr(slirp
->vhost_addr6
, opt
->ndpopt_linklayer
);
240 /* ICMPv6 Checksum */
241 t
->m_data
-= ICMP6_NDP_NA_MINLEN
;
242 t
->m_data
-= sizeof(struct ip6
);
243 ricmp
->icmp6_cksum
= ip6_cksum(t
);
245 ip6_output(NULL
, t
, 1);
249 * Send NDP Neighbor Advertisement
251 static void ndp_send_na(Slirp
*slirp
, struct ip6
*ip
, struct icmp6
*icmp
)
253 /* Build IPv6 packet */
254 struct mbuf
*t
= m_get(slirp
);
255 struct ip6
*rip
= mtod(t
, struct ip6
*);
256 rip
->ip_src
= icmp
->icmp6_nns
.target
;
257 if (IN6_IS_ADDR_UNSPECIFIED(&ip
->ip_src
)) {
258 rip
->ip_dst
= (struct in6_addr
)ALLNODES_MULTICAST
;
260 rip
->ip_dst
= ip
->ip_src
;
262 rip
->ip_nh
= IPPROTO_ICMPV6
;
263 rip
->ip_pl
= htons(ICMP6_NDP_NA_MINLEN
264 + NDPOPT_LINKLAYER_LEN
);
265 t
->m_len
= sizeof(struct ip6
) + ntohs(rip
->ip_pl
);
267 /* Build ICMPv6 packet */
268 t
->m_data
+= sizeof(struct ip6
);
269 struct icmp6
*ricmp
= mtod(t
, struct icmp6
*);
270 ricmp
->icmp6_type
= ICMP6_NDP_NA
;
271 ricmp
->icmp6_code
= 0;
272 ricmp
->icmp6_cksum
= 0;
275 ricmp
->icmp6_nna
.R
= NDP_IsRouter
;
276 ricmp
->icmp6_nna
.S
= !IN6_IS_ADDR_MULTICAST(&rip
->ip_dst
);
277 ricmp
->icmp6_nna
.O
= 1;
278 ricmp
->icmp6_nna
.reserved_hi
= 0;
279 ricmp
->icmp6_nna
.reserved_lo
= 0;
280 ricmp
->icmp6_nna
.target
= icmp
->icmp6_nns
.target
;
282 /* Build NDP option */
283 t
->m_data
+= ICMP6_NDP_NA_MINLEN
;
284 struct ndpopt
*opt
= mtod(t
, struct ndpopt
*);
285 opt
->ndpopt_type
= NDPOPT_LINKLAYER_TARGET
;
286 opt
->ndpopt_len
= NDPOPT_LINKLAYER_LEN
/ 8;
287 in6_compute_ethaddr(ricmp
->icmp6_nna
.target
,
288 opt
->ndpopt_linklayer
);
290 /* ICMPv6 Checksum */
291 t
->m_data
-= ICMP6_NDP_NA_MINLEN
;
292 t
->m_data
-= sizeof(struct ip6
);
293 ricmp
->icmp6_cksum
= ip6_cksum(t
);
295 ip6_output(NULL
, t
, 0);
299 * Process a NDP message
301 static void ndp_input(struct mbuf
*m
, Slirp
*slirp
, struct ip6
*ip
,
304 m
->m_len
+= ETH_HLEN
;
305 m
->m_data
-= ETH_HLEN
;
306 struct ethhdr
*eth
= mtod(m
, struct ethhdr
*);
307 m
->m_len
-= ETH_HLEN
;
308 m
->m_data
+= ETH_HLEN
;
310 switch (icmp
->icmp6_type
) {
312 DEBUG_CALL(" type = Router Solicitation");
314 && icmp
->icmp6_code
== 0
315 && ntohs(ip
->ip_pl
) >= ICMP6_NDP_RS_MINLEN
) {
317 ndp_table_add(slirp
, ip
->ip_src
, eth
->h_source
);
324 DEBUG_CALL(" type = Router Advertisement");
325 qemu_log_mask(LOG_GUEST_ERROR
,
326 "Warning: guest sent NDP RA, but shouldn't");
330 DEBUG_CALL(" type = Neighbor Solicitation");
332 && icmp
->icmp6_code
== 0
333 && !IN6_IS_ADDR_MULTICAST(&icmp
->icmp6_nns
.target
)
334 && ntohs(ip
->ip_pl
) >= ICMP6_NDP_NS_MINLEN
335 && (!IN6_IS_ADDR_UNSPECIFIED(&ip
->ip_src
)
336 || in6_solicitednode_multicast(&ip
->ip_dst
))) {
337 if (in6_equal_host(&icmp
->icmp6_nns
.target
)) {
339 ndp_table_add(slirp
, ip
->ip_src
, eth
->h_source
);
340 ndp_send_na(slirp
, ip
, icmp
);
346 DEBUG_CALL(" type = Neighbor Advertisement");
348 && icmp
->icmp6_code
== 0
349 && ntohs(ip
->ip_pl
) >= ICMP6_NDP_NA_MINLEN
350 && !IN6_IS_ADDR_MULTICAST(&icmp
->icmp6_nna
.target
)
351 && (!IN6_IS_ADDR_MULTICAST(&ip
->ip_dst
)
352 || icmp
->icmp6_nna
.S
== 0)) {
353 ndp_table_add(slirp
, ip
->ip_src
, eth
->h_source
);
357 case ICMP6_NDP_REDIRECT
:
358 DEBUG_CALL(" type = Redirect");
359 qemu_log_mask(LOG_GUEST_ERROR
,
360 "Warning: guest sent NDP REDIRECT, but shouldn't");
366 * Process a received ICMPv6 message.
368 void icmp6_input(struct mbuf
*m
)
371 struct ip6
*ip
= mtod(m
, struct ip6
*);
372 Slirp
*slirp
= m
->slirp
;
373 int hlen
= sizeof(struct ip6
);
375 DEBUG_CALL("icmp6_input");
376 DEBUG_ARG("m = %lx", (long) m
);
377 DEBUG_ARG("m_len = %d", m
->m_len
);
379 if (ntohs(ip
->ip_pl
) < ICMP6_MINLEN
) {
389 icmp
= mtod(m
, struct icmp6
*);
393 DEBUG_ARG("icmp6_type = %d", icmp
->icmp6_type
);
394 switch (icmp
->icmp6_type
) {
395 case ICMP6_ECHO_REQUEST
:
396 if (in6_equal_host(&ip
->ip_dst
)) {
397 icmp6_send_echoreply(m
, slirp
, ip
, icmp
);
400 error_report("external icmpv6 not supported yet");
408 case ICMP6_NDP_REDIRECT
:
409 ndp_input(m
, slirp
, ip
, icmp
);
415 case ICMP6_PARAMPROB
:
416 /* XXX? report error? close socket? */