Skip to content

Commit 0d6ff71

Browse files
Guillaume Subironsthibaul
Guillaume Subiron
authored andcommitted
slirp: Adding IPv6, ICMPv6 Echo and NDP autoconfiguration
This patch adds the functions needed to handle IPv6 packets. ICMPv6 and NDP headers are implemented. Slirp is now able to send NDP Router or Neighbor Advertisement when it receives Router or Neighbor Solicitation. Using a 64bit-sized IPv6 prefix, the guest is now able to perform stateless autoconfiguration (SLAAC) and to compute its IPv6 address. This patch adds an ndp_table, mainly inspired by arp_table, to keep an NDP cache and manage network address resolution. Slirp regularly sends NDP Neighbor Advertisement, as recommended by the RFC, to make the guest refresh its route. This also adds ip6_cksum() to compute ICMPv6 checksums using IPv6 pseudo-header. Some #define ETH_* are moved upper in slirp.h to make them accessible to other slirp/*.h Signed-off-by: Guillaume Subiron <[email protected]> Signed-off-by: Samuel Thibault <[email protected]> Reviewed-by: Thomas Huth <[email protected]>
1 parent 618a5a8 commit 0d6ff71

12 files changed

+1019
-6
lines changed

slirp/Makefile.objs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
common-obj-y = cksum.o if.o ip_icmp.o ip_input.o ip_output.o dnssearch.o
1+
common-obj-y = cksum.o if.o ip_icmp.o ip6_icmp.o ip6_input.o ip6_output.o \
2+
ip_input.o ip_output.o dnssearch.o
23
common-obj-y += slirp.o mbuf.o misc.o sbuf.o socket.o tcp_input.o tcp_output.o
3-
common-obj-y += tcp_subr.o tcp_timer.o udp.o bootp.o tftp.o arp_table.o
4+
common-obj-y += tcp_subr.o tcp_timer.o udp.o bootp.o tftp.o arp_table.o \
5+
ndp_table.o

slirp/cksum.c

+25
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,28 @@ int cksum(struct mbuf *m, int len)
138138
REDUCE;
139139
return (~sum & 0xffff);
140140
}
141+
142+
int ip6_cksum(struct mbuf *m)
143+
{
144+
/* TODO: Optimize this by being able to pass the ip6_pseudohdr to cksum
145+
* separately from the mbuf */
146+
struct ip6 save_ip, *ip = mtod(m, struct ip6 *);
147+
struct ip6_pseudohdr *ih = mtod(m, struct ip6_pseudohdr *);
148+
int sum;
149+
150+
save_ip = *ip;
151+
152+
ih->ih_src = save_ip.ip_src;
153+
ih->ih_dst = save_ip.ip_dst;
154+
ih->ih_pl = htonl((uint32_t)ntohs(save_ip.ip_pl));
155+
ih->ih_zero_hi = 0;
156+
ih->ih_zero_lo = 0;
157+
ih->ih_nh = save_ip.ip_nh;
158+
159+
sum = cksum(m, ((int)sizeof(struct ip6_pseudohdr))
160+
+ ntohl(ih->ih_pl));
161+
162+
*ip = save_ip;
163+
164+
return sum;
165+
}

slirp/if.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ void if_start(Slirp *slirp)
194194

195195
/* Try to send packet unless it already expired */
196196
if (ifm->expiration_date >= now && !if_encap(slirp, ifm)) {
197-
/* Packet is delayed due to pending ARP resolution */
197+
/* Packet is delayed due to pending ARP or NDP resolution */
198198
continue;
199199
}
200200

slirp/ip6.h

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* Copyright (c) 2013
3+
* Guillaume Subiron, Yann Bordenave, Serigne Modou Wagne.
4+
*/
5+
6+
#ifndef SLIRP_IP6_H_
7+
#define SLIRP_IP6_H_
8+
9+
#include "net/eth.h"
10+
11+
#define ALLNODES_MULTICAST { .s6_addr = \
12+
{ 0xff, 0x02, 0x00, 0x00,\
13+
0x00, 0x00, 0x00, 0x00,\
14+
0x00, 0x00, 0x00, 0x00,\
15+
0x00, 0x00, 0x00, 0x01 } }
16+
17+
#define SOLICITED_NODE_PREFIX { .s6_addr = \
18+
{ 0xff, 0x02, 0x00, 0x00,\
19+
0x00, 0x00, 0x00, 0x00,\
20+
0x00, 0x00, 0x00, 0x01,\
21+
0xff, 0x00, 0x00, 0x00 } }
22+
23+
#define LINKLOCAL_ADDR { .s6_addr = \
24+
{ 0xfe, 0x80, 0x00, 0x00,\
25+
0x00, 0x00, 0x00, 0x00,\
26+
0x00, 0x00, 0x00, 0x00,\
27+
0x00, 0x00, 0x00, 0x02 } }
28+
29+
static inline bool in6_equal(const struct in6_addr *a, const struct in6_addr *b)
30+
{
31+
return memcmp(a, b, sizeof(*a)) == 0;
32+
}
33+
34+
static inline bool in6_equal_net(const struct in6_addr *a,
35+
const struct in6_addr *b,
36+
int prefix_len)
37+
{
38+
if (memcmp(a, b, prefix_len / 8) != 0) {
39+
return 0;
40+
}
41+
42+
if (prefix_len % 8 == 0) {
43+
return 1;
44+
}
45+
46+
return a->s6_addr[prefix_len / 8] >> (8 - (prefix_len % 8))
47+
== b->s6_addr[prefix_len / 8] >> (8 - (prefix_len % 8));
48+
}
49+
50+
static inline bool in6_equal_mach(const struct in6_addr *a,
51+
const struct in6_addr *b,
52+
int prefix_len)
53+
{
54+
if (memcmp(&(a->s6_addr[(prefix_len + 7) / 8]),
55+
&(b->s6_addr[(prefix_len + 7) / 8]),
56+
16 - (prefix_len + 7) / 8) != 0) {
57+
return 0;
58+
}
59+
60+
if (prefix_len % 8 == 0) {
61+
return 1;
62+
}
63+
64+
return (a->s6_addr[prefix_len / 8] & ((1U << (8 - (prefix_len % 8))) - 1))
65+
== (b->s6_addr[prefix_len / 8] & ((1U << (8 - (prefix_len % 8))) - 1));
66+
}
67+
68+
69+
#define in6_equal_router(a)\
70+
((in6_equal_net(a, &slirp->vprefix_addr6, slirp->vprefix_len)\
71+
&& in6_equal_mach(a, &slirp->vhost_addr6, slirp->vprefix_len))\
72+
|| (in6_equal_net(a, &(struct in6_addr)LINKLOCAL_ADDR, 64)\
73+
&& in6_equal_mach(a, &slirp->vhost_addr6, 64)))
74+
75+
#define in6_equal_dns(a) 0
76+
77+
#define in6_equal_host(a)\
78+
(in6_equal_router(a) || in6_equal_dns(a))
79+
80+
#define in6_solicitednode_multicast(a)\
81+
(in6_equal_net(a, &(struct in6_addr)SOLICITED_NODE_PREFIX, 104))
82+
83+
/* Compute emulated host MAC address from its ipv6 address */
84+
static inline void in6_compute_ethaddr(struct in6_addr ip,
85+
uint8_t eth[ETH_ALEN])
86+
{
87+
eth[0] = 0x52;
88+
eth[1] = 0x56;
89+
memcpy(&eth[2], &ip.s6_addr[16 - (ETH_ALEN - 2)], ETH_ALEN - 2);
90+
}
91+
92+
/*
93+
* Definitions for internet protocol version 6.
94+
* Per RFC 2460, December 1998.
95+
*/
96+
#define IP6VERSION 6
97+
#define IP6_HOP_LIMIT 255
98+
99+
/*
100+
* Structure of an internet header, naked of options.
101+
*/
102+
struct ip6 {
103+
#ifdef HOST_WORDS_BIGENDIAN
104+
uint32_t
105+
ip_v:4, /* version */
106+
ip_tc_hi:4, /* traffic class */
107+
ip_tc_lo:4,
108+
ip_fl_hi:4, /* flow label */
109+
ip_fl_lo:16;
110+
#else
111+
uint32_t
112+
ip_tc_hi:4,
113+
ip_v:4,
114+
ip_fl_hi:4,
115+
ip_tc_lo:4,
116+
ip_fl_lo:16;
117+
#endif
118+
uint16_t ip_pl; /* payload length */
119+
uint8_t ip_nh; /* next header */
120+
uint8_t ip_hl; /* hop limit */
121+
struct in6_addr ip_src, ip_dst; /* source and dest address */
122+
} QEMU_PACKED;
123+
124+
/*
125+
* IPv6 pseudo-header used by upper-layer protocols
126+
*/
127+
struct ip6_pseudohdr {
128+
struct in6_addr ih_src; /* source internet address */
129+
struct in6_addr ih_dst; /* destination internet address */
130+
uint32_t ih_pl; /* upper-layer packet length */
131+
uint16_t ih_zero_hi; /* zero */
132+
uint8_t ih_zero_lo; /* zero */
133+
uint8_t ih_nh; /* next header */
134+
} QEMU_PACKED;
135+
136+
137+
#endif

0 commit comments

Comments
 (0)