6 #include "qemu/osdep.h"
7 #include "qemu-common.h"
12 void udp6_input(struct mbuf *m)
14 Slirp *slirp = m->slirp;
15 struct ip6 *ip, save_ip;
17 int iphlen = sizeof(struct ip6);
20 struct sockaddr_in6 lhost;
22 DEBUG_CALL("udp6_input");
23 DEBUG_ARG("m = %lx", (long)m);
25 if (slirp->restricted) {
29 ip = mtod(m, struct ip6 *);
32 uh = mtod(m, struct udphdr *);
40 len = ntohs((uint16_t)uh->uh_ulen);
43 * Make mbuf data length reflect UDP length.
44 * If not enough data to reflect UDP length, drop.
46 if (ntohs(ip->ip_pl) != len) {
47 if (len > ntohs(ip->ip_pl)) {
50 m_adj(m, len - ntohs(ip->ip_pl));
51 ip->ip_pl = htons(len);
55 * Save a copy of the IP header in case we want restore it
56 * for sending an ICMP error message in response.
60 /* Locate pcb for datagram. */
61 lhost.sin6_family = AF_INET6;
62 lhost.sin6_addr = ip->ip_src;
63 lhost.sin6_port = uh->uh_sport;
66 if (ntohs(uh->uh_dport) == DHCPV6_SERVER_PORT &&
67 (in6_equal(&ip->ip_dst, &slirp->vhost_addr6) ||
68 in6_dhcp_multicast(&ip->ip_dst))) {
71 dhcpv6_input(&lhost, m);
78 if (ntohs(uh->uh_dport) == TFTP_SERVER &&
79 !memcmp(ip->ip_dst.s6_addr, slirp->vhost_addr6.s6_addr, 16)) {
82 tftp_input((struct sockaddr_storage *)&lhost, m);
88 so = solookup(&slirp->udp_last_so, &slirp->udb,
89 (struct sockaddr_storage *) &lhost, NULL);
92 /* If there's no socket for this packet, create one. */
94 if (udp_attach(so, AF_INET6) == -1) {
95 DEBUG_MISC((dfd, " udp6_attach errno = %d-%s\n",
96 errno, strerror(errno)));
102 so->so_lfamily = AF_INET6;
103 so->so_laddr6 = ip->ip_src;
104 so->so_lport6 = uh->uh_sport;
107 so->so_ffamily = AF_INET6;
108 so->so_faddr6 = ip->ip_dst; /* XXX */
109 so->so_fport6 = uh->uh_dport; /* XXX */
111 iphlen += sizeof(struct udphdr);
116 * Now we sendto() the packet.
118 if (sosendto(so, m) == -1) {
122 DEBUG_MISC((dfd, "udp tx errno = %d-%s\n", errno, strerror(errno)));
123 icmp6_send_error(m, ICMP6_UNREACH, ICMP6_UNREACH_NO_ROUTE);
127 m_free(so->so_m); /* used for ICMP if error on sorecvfrom */
129 /* restore the orig mbuf packet */
140 int udp6_output(struct socket *so, struct mbuf *m,
141 struct sockaddr_in6 *saddr, struct sockaddr_in6 *daddr)
146 DEBUG_CALL("udp6_output");
147 DEBUG_ARG("so = %lx", (long)so);
148 DEBUG_ARG("m = %lx", (long)m);
150 /* adjust for header */
151 m->m_data -= sizeof(struct udphdr);
152 m->m_len += sizeof(struct udphdr);
153 uh = mtod(m, struct udphdr *);
154 m->m_data -= sizeof(struct ip6);
155 m->m_len += sizeof(struct ip6);
156 ip = mtod(m, struct ip6 *);
158 /* Build IP header */
159 ip->ip_pl = htons(m->m_len - sizeof(struct ip6));
160 ip->ip_nh = IPPROTO_UDP;
161 ip->ip_src = saddr->sin6_addr;
162 ip->ip_dst = daddr->sin6_addr;
164 /* Build UDP header */
165 uh->uh_sport = saddr->sin6_port;
166 uh->uh_dport = daddr->sin6_port;
167 uh->uh_ulen = ip->ip_pl;
169 uh->uh_sum = ip6_cksum(m);
170 if (uh->uh_sum == 0) {
174 return ip6_output(so, m, 0);