6 #include "qemu/osdep.h"
7 #include "qemu-common.h"
11 void udp6_input(struct mbuf
*m
)
13 Slirp
*slirp
= m
->slirp
;
14 struct ip6
*ip
, save_ip
;
16 int iphlen
= sizeof(struct ip6
);
19 struct sockaddr_in6 lhost
;
21 DEBUG_CALL("udp6_input");
22 DEBUG_ARG("m = %lx", (long)m
);
24 if (slirp
->restricted
) {
28 ip
= mtod(m
, struct ip6
*);
31 uh
= mtod(m
, struct udphdr
*);
39 len
= ntohs((uint16_t)uh
->uh_ulen
);
42 * Make mbuf data length reflect UDP length.
43 * If not enough data to reflect UDP length, drop.
45 if (ntohs(ip
->ip_pl
) != len
) {
46 if (len
> ntohs(ip
->ip_pl
)) {
49 m_adj(m
, len
- ntohs(ip
->ip_pl
));
50 ip
->ip_pl
= htons(len
);
54 * Save a copy of the IP header in case we want restore it
55 * for sending an ICMP error message in response.
59 /* Locate pcb for datagram. */
60 lhost
.sin6_family
= AF_INET6
;
61 lhost
.sin6_addr
= ip
->ip_src
;
62 lhost
.sin6_port
= uh
->uh_sport
;
64 /* TODO handle DHCP/BOOTP */
67 if (ntohs(uh
->uh_dport
) == TFTP_SERVER
&&
68 !memcmp(ip
->ip_dst
.s6_addr
, slirp
->vhost_addr6
.s6_addr
, 16)) {
71 tftp_input((struct sockaddr_storage
*)&lhost
, m
);
77 so
= solookup(&slirp
->udp_last_so
, &slirp
->udb
,
78 (struct sockaddr_storage
*) &lhost
, NULL
);
81 /* If there's no socket for this packet, create one. */
86 if (udp_attach(so
, AF_INET6
) == -1) {
87 DEBUG_MISC((dfd
, " udp6_attach errno = %d-%s\n",
88 errno
, strerror(errno
)));
94 so
->so_lfamily
= AF_INET6
;
95 so
->so_laddr6
= ip
->ip_src
;
96 so
->so_lport6
= uh
->uh_sport
;
99 so
->so_ffamily
= AF_INET6
;
100 so
->so_faddr6
= ip
->ip_dst
; /* XXX */
101 so
->so_fport6
= uh
->uh_dport
; /* XXX */
103 iphlen
+= sizeof(struct udphdr
);
108 * Now we sendto() the packet.
110 if (sosendto(so
, m
) == -1) {
114 DEBUG_MISC((dfd
, "udp tx errno = %d-%s\n", errno
, strerror(errno
)));
115 icmp6_send_error(m
, ICMP6_UNREACH
, ICMP6_UNREACH_NO_ROUTE
);
119 m_free(so
->so_m
); /* used for ICMP if error on sorecvfrom */
121 /* restore the orig mbuf packet */
132 int udp6_output(struct socket
*so
, struct mbuf
*m
,
133 struct sockaddr_in6
*saddr
, struct sockaddr_in6
*daddr
)
138 DEBUG_CALL("udp6_output");
139 DEBUG_ARG("so = %lx", (long)so
);
140 DEBUG_ARG("m = %lx", (long)m
);
142 /* adjust for header */
143 m
->m_data
-= sizeof(struct udphdr
);
144 m
->m_len
+= sizeof(struct udphdr
);
145 uh
= mtod(m
, struct udphdr
*);
146 m
->m_data
-= sizeof(struct ip6
);
147 m
->m_len
+= sizeof(struct ip6
);
148 ip
= mtod(m
, struct ip6
*);
150 /* Build IP header */
151 ip
->ip_pl
= htons(m
->m_len
- sizeof(struct ip6
));
152 ip
->ip_nh
= IPPROTO_UDP
;
153 ip
->ip_src
= saddr
->sin6_addr
;
154 ip
->ip_dst
= daddr
->sin6_addr
;
156 /* Build UDP header */
157 uh
->uh_sport
= saddr
->sin6_port
;
158 uh
->uh_dport
= daddr
->sin6_port
;
159 uh
->uh_ulen
= ip
->ip_pl
;
161 uh
->uh_sum
= ip6_cksum(m
);
162 if (uh
->uh_sum
== 0) {
166 return ip6_output(so
, m
, 0);