slirp: Add support for stateless DHCPv6
PATCH 1/1 slirp: Add support for stateless DHCPv6
Provide basic support for stateless DHCPv6 (see RFC 3736) so that guests can also automatically boot via IPv6 with SLIRP (for IPv6 network booting, see RFC 5970 for details).
Tested with:
qemu-system-ppc64 -nographic -vga none -boot n -net nic \
-net user,ipv6=yes,ipv4=no,tftp=/path/to/tftp,bootfile=ppc64.img--- slirp/Makefile.objs | 2 +- slirp/dhcpv6.c | 197 ++++++++++++++++++++++++++++++++++++++++++++++++++++ slirp/dhcpv6.h | 8 +++ slirp/ip6.h | 6 ++ slirp/udp6.c | 13 +++- 5 files changed, 224 insertions(+), 2 deletions(-) create mode 100644 slirp/dhcpv6.c create mode 100644 slirp/dhcpv6.h
slirp/Makefile.objs +1 -1
@@ -1,5 +1,5 @@ common-obj-y = cksum.o if.o ip_icmp.o ip6_icmp.o ip6_input.o ip6_output.o \ - ip_input.o ip_output.o dnssearch.o + ip_input.o ip_output.o dnssearch.o dhcpv6.o common-obj-y += slirp.o mbuf.o misc.o sbuf.o socket.o tcp_input.o tcp_output.o common-obj-y += tcp_subr.o tcp_timer.o udp.o udp6.o bootp.o tftp.o arp_table.o \ ndp_table.o
slirp/dhcpv6.c new file +197 -0
197 lines not on this page · raw message
slirp/dhcpv6.h new file +8 -0
@@ -0,0 +1,8 @@ +#ifndef SLIRP_DHCPV6_H +#define SLIRP_DHCPV6_H + +#define DHCPV6_SERVER_PORT 547 + +void dhcpv6_input (struct sockaddr_in6 *srcsas,struct mbuf *m); + +#endif
slirp/ip6.h +6 -0
@@ -14,6 +14,12 @@0x00 ,0x00 ,0x00 ,0x00 ,\ 0x00, 0x00, 0x00, 0x01 } } +#define ALLDHCP_MULTICAST { .s6_addr = \ + { 0xff, 0x02, 0x00, 0x00,\ + 0x00, 0x00, 0x00, 0x00,\ + 0x00, 0x00, 0x00, 0x00,\ + 0x00, 0x01, 0x00, 0x02 } } +#define SOLICITED_NODE_PREFIX { .s6_addr = \ { 0xff, 0x02, 0x00, 0x00,\ 0x00, 0x00, 0x00, 0x00,\
slirp/udp6.c +12 -1
@@ -7,6 +7,7 @@#include "qemu-common.h" #include "slirp.h" #include "udp.h" +#include "dhcpv6.h" void udp6_input (struct mbuf *m) {
@@ -61,7 +62,17 @@ void udp6_input(struct mbuf *m) lhost.sin6_addr = ip->ip_src; lhost.sin6_port = uh->uh_sport; -/* TODO handle DHCP/BOOTP */ +/* handle DHCPv6 */ +if (ntohs (uh->uh_dport) ==DHCPV6_SERVER_PORT && + (in6_equal (&ip->ip_dst, &slirp->vhost_addr6) || +in6_equal (&ip->ip_dst, &(struct in6_addr )ALLDHCP_MULTICAST ))) { + m->m_data += iphlen; + m->m_len -= iphlen; +dhcpv6_input (&lhost, m); + m->m_data -= iphlen; + m->m_len += iphlen; + goto bad; + }/* handle TFTP */ if (ntohs(uh->uh_dport) == TFTP_SERVER &&
Signature · 1 line
1.8.3.1
Hello,
Quoted 3 lines · Thomas Huth, on Sun 26 Jun 2016 10:04:02 +0200, wrote:
Provide basic support for stateless DHCPv6 (see RFC 3736) so that guests can also automatically boot via IPv6 with SLIRP (for IPv6 network booting, see RFC 5970 for details).
Cool :)
I'm here commenting in my reading order, not the file order.
slirp/dhcpv6.c line 185
Quoted 5 lines
slirp/dhcpv6.c new file +5
-0+void dhcpv6_input (struct sockaddr_in6 *srcsas,struct mbuf *m) +{ +uint8_t *data = (uint8_t *)m->m_data +sizeof (struct udphdr ); +int data_len = m->m_len -sizeof (struct udphdr ); +uint32_t xid;
We need to make sure that data_len >= 4 here.
Quoted 1 line
+ xid = data[1] << 16 | data[2] << 8 | data[3];
Mmm, strictly speaking, this breaks on systems where int is 16bit only. I guess in the context of qemu we are fine, but it's probably better to avoid leaving code like this, in case somebody copies it for e.g. a 64bit value. It's a bit tedious, but
xid = (uint32_t) data[1] << 16 |
(uint32_t) data[2] << 8 |
data[3];would thus be preferrable. Or we could cast to uint32_t*, use ntohs, and mask out the high 8 bits.
slirp/dhcpv6.c line 56
Quoted 11 lines
slirp/dhcpv6.c new file +11
-0+/** +* Analyze the info request message sent by the client to +* see what data it provided and what it wants to have. +*/ +static int dhcpv6_parse_info_request (uint8_t *odata,int olen, +struct requested_infos *ri) +{ +int i; + +while (olen >0 ) { +/* Parse one option */
Here we need to check that olen >= 4.
Quoted 7 lines
slirp/dhcpv6.c new file +7
-0+int option = odata[0 ] <<8 | odata[1 ]; +int len = odata[2 ] <<8 | odata[3 ]; + +if (len +4 > olen) { +qemu_log_mask (LOG_GUEST_ERROR ,"Guest sent bad DHCPv6 packet!\n" ); +return -E2BIG ; + }
slirp/dhcpv6.c line 73
Quoted 10 lines
slirp/dhcpv6.c new file +10
-0+ +switch (option) { +case OPTION_IAADDR : +/* According to RFC3315, we must discard requests with IA option */ +return -EINVAL ; +case OPTION_CLIENTID : +if (len >256 ) { +/* Avoid very long IDs which could cause problems later */ +return -E2BIG ; + }
Maybe document in the comment of the dhcpv6_parse_info_request function that it fills struct requested_infos with pointers withing odata (and thus the validity is not beyond the odata liveness).
slirp/dhcpv6.c line 83
Quoted 10 lines
slirp/dhcpv6.c new file +10
-0+ ri->client_id = odata +4 ; + ri->client_id_len = len; +break ; +case OPTION_ORO :/* Option request option */ +if (len &1 ) { +return -EINVAL ; + } +/* Check which options the client wants to have */ +for (i =0 ; i < len; i +=2 ) { +switch (odata[4 + i *2 ] <<8 | odata[4 + i *2 +1 ]) {
Ok, this time this is always valid in C, that looks fine enough to me :)
slirp/dhcpv6.c line 89
Quoted 6 lines
slirp/dhcpv6.c new file +6
-0+case OPTION_DNS_SERVERS : + ri->want_dns = true; +break ; +case OPTION_BOOTFILE_URL : + ri->want_boot_url = true; +break ;
Perhaps add a DEBUG_MISC for unsupported option requests?
Quoted 6 lines
slirp/dhcpv6.c new file +6
-0+/** +* Handle information request messages +*/ +static void dhcpv6_info_request (Slirp *slirp,struct sockaddr_in6 *srcsas, +uint32_t xid,uint8_t *odata,int olen) +{
[...]
slirp/dhcpv6.c line 139
Quoted 5 lines
slirp/dhcpv6.c new file +5
-0+if (ri.client_id) { + *resp++ =0 ; + *resp++ =OPTION_CLIENTID ; + *resp++ = ri.client_id_len >>8 ; + *resp++ = ri.client_id_len;
That does not look good. I'd say introduce an explicit struct with uint16_t, and using htons(), I think it will look much nicer (at first I was even wondering what that 0 standed for...)
slirp/dhcpv6.c line 141
Quoted 2 lines
slirp/dhcpv6.c new file +2
-0+memcpy (resp, ri.client_id, ri.client_id_len); + resp += ri.client_id_len;
G + }
slirp/dhcpv6.c line 147
Quoted 5 lines
slirp/dhcpv6.c new file +5
-0+if (ri.want_dns) { + *resp++ =0 ; + *resp++ =OPTION_DNS_SERVERS ; + *resp++ =0 ; + *resp++ =16 ;/* Length */
(ditto for the structure)
slirp/dhcpv6.c line 157
Quoted 10 lines
slirp/dhcpv6.c new file +10
-0+memcpy (resp, &slirp->vnameserver_addr6,16 ); + resp +=16 ; + } +if (ri.want_boot_url) { +uint8_t *sa = slirp->vhost_addr6.s6_addr; +int slen; + + *resp++ =0 ; + *resp++ =OPTION_BOOTFILE_URL ; + snprintf((char *)resp + 2, (uint8_t *)m->m_data + IF_MTU - resp - 2,
We'd need an extra -1 for the trailing \0 used by the following strlen, don't we?
But we could as well use the value returned by snprintf which is exactly what we want to have in slen, don't we?
Quoted 9 lines
slirp/dhcpv6.c new file +9
-0+"tftp://[%02x%02x:%02x%02x:%02x%02x:%02x%02x:" +"%02x%02x:%02x%02x:%02x%02x:%02x%02x]/%s" , + sa[0 ], sa[1 ], sa[2 ], sa[3 ], sa[4 ], sa[5 ], sa[6 ], sa[7 ], + sa[8 ], sa[9 ], sa[10 ], sa[11 ], sa[12 ], sa[13 ], sa[14 ], sa[15 ], + slirp->bootp_filename); + slen =strlen ((char *)resp +2 ); + *resp++ = slen >>8 ; + *resp++ = slen; + resp += slen;
slirp/dhcpv6.c line 176
Quoted 10 lines
slirp/dhcpv6.c new file +10
-0+ } + + sa6.sin6_addr = slirp->vhost_addr6; + sa6.sin6_port =DHCPV6_SERVER_PORT ; + da6.sin6_addr = srcsas->sin6_addr; + da6.sin6_port = srcsas->sin6_port; + m->m_data +=sizeof (struct ip6 ) +sizeof (struct udphdr ); + m->m_len = resp - (uint8_t *)m->m_data; +udp6_output (NULL , m, &sa6, &da6); +}
Thanks!
Samuel
Quoted 11 lines · Samuel Thibault
Hello,
Quoted 3 lines · Thomas Huth, on Sun 26 Jun 2016 10:04:02 +0200, wrote:
Provide basic support for stateless DHCPv6 (see RFC 3736) so that guests can also automatically boot via IPv6 with SLIRP (for IPv6 network booting, see RFC 5970 for details).
Cool :)
I'm here commenting in my reading order, not the file order.
Quoted 7 lines
slirp/dhcpv6.c line 185
Quoted 5 lines
slirp/dhcpv6.c new file +5
-0+void dhcpv6_input (struct sockaddr_in6 *srcsas,struct mbuf *m) +{ +uint8_t *data = (uint8_t *)m->m_data +sizeof (struct udphdr ); +int data_len = m->m_len -sizeof (struct udphdr ); +uint32_t xid;We need to make sure that
data_len>= 4 here.
Ok, I'll add a check.
Quoted 10 lines
Quoted 1 line
+ xid = data[1] << 16 | data[2] << 8 | data[3];
Mmm, strictly speaking, this breaks on systems where int is 16bit only. I guess in the context of qemu we are fine, but it's probably better to avoid leaving code like this, in case somebody copies it for e.g. a 64bit value. It's a bit tedious, but
xid = (uint32_t) data[1] << 16 | (uint32_t) data[2] << 8 | data[3];
We don't support sizeof(int) == 2 in QEMU, so IMHO it does not make sense to add such ugly casts here. I think one can safely assume that sizeof(int) is at least 4 on every modern C compiler (unless you're compiling code for an embedded 16- or 8-bit system, but QEMU won't work there anyway).
BTW, there are also other places in the slirp code where shifting with size bigger than 16 is done without cast ( grep -r "<< 16" slirp/*.c ), so I really think there is no need for these casts here.
Quoted 2 lines
would thus be preferrable. Or we could cast to uint32_t*, use ntohs, and mask out the high 8 bits.
Not sure whether I like that here (since we're only touching 3 bytes), but I can give it a try to see how it looks like...
Quoted 5 lines
Quoted 5 lines
slirp/dhcpv6.c new file +5
-0+/** +* Analyze the info request message sent by the client to +* see what data it provided and what it wants to have. +*/ +static int dhcpv6_parse_info_request(uint8_t *odata, int olen,
Quoted 8 lines
slirp/dhcpv6.c line 56
Quoted 6 lines
slirp/dhcpv6.c new file +6
-0+ struct requested_infos *ri) +{ +int i; + +while (olen >0 ) { +/* Parse one option */ Here we need to check that olen >= 4.
Ok.
Quoted 13 lines
Quoted 13 lines
slirp/dhcpv6.c new file +13
-0+int option = odata[0 ] <<8 | odata[1 ]; +int len = odata[2 ] <<8 | odata[3 ]; + +if (len +4 > olen) { +qemu_log_mask (LOG_GUEST_ERROR ,"Guest sent bad DHCPv6 packet!\n" ); +return -E2BIG ; + } + +switch (option) { +case OPTION_IAADDR : +/* According to RFC3315, we must discard requests with IA option */ +return -EINVAL ; +case OPTION_CLIENTID :
Quoted 8 lines
slirp/dhcpv6.c line 73
Quoted 4 lines
slirp/dhcpv6.c new file +4
-0+if (len >256 ) { +/* Avoid very long IDs which could cause problems later */ +return -E2BIG ; + }Maybe document in the comment of the
dhcpv6_parse_info_requestfunction that it fills structrequested_infoswith pointers withing odata (and thus the validity is not beyond the odata liveness).
Ok.
Quoted 13 lines
slirp/dhcpv6.c line 83
Quoted 10 lines
slirp/dhcpv6.c new file +10
-0+ ri->client_id = odata +4 ; + ri->client_id_len = len; +break ; +case OPTION_ORO :/* Option request option */ +if (len &1 ) { +return -EINVAL ; + } +/* Check which options the client wants to have */ +for (i =0 ; i < len; i +=2 ) { +switch (odata[4 + i *2 ] <<8 | odata[4 + i *2 +1 ]) {Ok, this time this is always valid in C, that looks fine enough to me :)
Quoted 8 lines
slirp/dhcpv6.c line 89
Quoted 6 lines
slirp/dhcpv6.c new file +6
-0+case OPTION_DNS_SERVERS : + ri->want_dns = true; +break ; +case OPTION_BOOTFILE_URL : + ri->want_boot_url = true; +break ;Perhaps add a
DEBUG_MISCfor unsupported option requests?
Ok, I'll add a default case here, too.
Quoted 13 lines
Quoted 6 lines
slirp/dhcpv6.c new file +6
-0+/** +* Handle information request messages +*/ +static void dhcpv6_info_request (Slirp *slirp,struct sockaddr_in6 *srcsas, +uint32_t xid,uint8_t *odata,int olen) +{[...]
slirp/dhcpv6.c line 139
Quoted 5 lines
slirp/dhcpv6.c new file +5
-0+if (ri.client_id) { + *resp++ =0 ; + *resp++ =OPTION_CLIENTID ; + *resp++ = ri.client_id_len >>8 ; + *resp++ = ri.client_id_len;
Quoted 3 lines
That does not look good. I'd say introduce an explicit struct with
uint16_t, and usinghtons(), I think it will look much nicer (at first I was even wondering what that 0 standed for...)
I'm afraid that won't work very well, too: The options in the DHCPv6 packet do not have any alignment requirement, so the can also start at uneven addresses. For example if the CLIENTID option has an even length, the following option (DNS_SERVERS if requested) will start at an uneven offset.
So if I'd use a struct pointer here to fill in the values, this might still work OK on x86, but it would fail on all other architectures that can not do unaligned memory accesses (IIRC Sparc is one of those architectures, and older versions of ARM chips).
So I'd like to keep the current code, but I can add some more comments if you think that helps to understand the "0" for example.
Quoted 22 lines
slirp/dhcpv6.c line 141
Quoted 2 lines
slirp/dhcpv6.c new file +2
-0+memcpy (resp, ri.client_id, ri.client_id_len); + resp += ri.client_id_len;G + }
slirp/dhcpv6.c line 147
Quoted 5 lines
slirp/dhcpv6.c new file +5
-0+if (ri.want_dns) { + *resp++ =0 ; + *resp++ =OPTION_DNS_SERVERS ; + *resp++ =0 ; + *resp++ =16 ;/* Length */ (ditto for the structure)
slirp/dhcpv6.c line 157
Quoted 10 lines
slirp/dhcpv6.c new file +10
-0+memcpy (resp, &slirp->vnameserver_addr6,16 ); + resp +=16 ; + } +if (ri.want_boot_url) { +uint8_t *sa = slirp->vhost_addr6.s6_addr; +int slen; + + *resp++ =0 ; + *resp++ =OPTION_BOOTFILE_URL ; + snprintf((char *)resp + 2, (uint8_t *)m->m_data + IF_MTU - resp - 2,
Quoted 5 lines
We'd need an extra -1 for the trailing \0 used by the following strlen, don't we?
But we could as well use the value returned by snprintf which is exactly what we want to have in slen, don't we?
True, I'll change that.
Quoted 11 lines
Quoted 11 lines
slirp/dhcpv6.c new file +11
-0+"tftp://[%02x%02x:%02x%02x:%02x%02x:%02x%02x:" +"%02x%02x:%02x%02x:%02x%02x:%02x%02x]/%s" , + sa[0 ], sa[1 ], sa[2 ], sa[3 ], sa[4 ], sa[5 ], sa[6 ], sa[7 ], + sa[8 ], sa[9 ], sa[10 ], sa[11 ], sa[12 ], sa[13 ], sa[14 ], sa[15 ], + slirp->bootp_filename); + slen =strlen ((char *)resp +2 ); + *resp++ = slen >>8 ; + *resp++ = slen; + resp += slen; + } +
Quoted 8 lines
slirp/dhcpv6.c line 176
Quoted 8 lines
slirp/dhcpv6.c new file +8
-0+ sa6.sin6_addr = slirp->vhost_addr6; + sa6.sin6_port =DHCPV6_SERVER_PORT ; + da6.sin6_addr = srcsas->sin6_addr; + da6.sin6_port = srcsas->sin6_port; + m->m_data +=sizeof (struct ip6 ) +sizeof (struct udphdr ); + m->m_len = resp - (uint8_t *)m->m_data; +udp6_output (NULL , m, &sa6, &da6); +}
Thomas
Quoted 2 lines · Thomas Huth, on Tue 28 Jun 2016 09:01:50 +0200, wrote:
The options in the DHCPv6 packet do not have any alignment requirement, so the can also start at uneven addresses.
Aow, OK :/
Quoted 2 lines
So I'd like to keep the current code, but I can add some more comments if you think that helps to understand the "0" for example.
Just adding /* 16bit values */ should be enough, yes.
Samuel