hw/arm/iotkit: Rename files to hw/arm/armsse.[ch]
[qemu/ar7.git] / slirp / udp6.c
blobfa531e03c4bab0f7f72f5cc0a5ce9138d1177bd3
1 /*
2 * Copyright (c) 2013
3 * Guillaume Subiron
4 */
6 #include "qemu/osdep.h"
7 #include "qemu-common.h"
8 #include "slirp.h"
9 #include "udp.h"
10 #include "dhcpv6.h"
12 void udp6_input(struct mbuf *m)
14 Slirp *slirp = m->slirp;
15 struct ip6 *ip, save_ip;
16 struct udphdr *uh;
17 int iphlen = sizeof(struct ip6);
18 int len;
19 struct socket *so;
20 struct sockaddr_in6 lhost;
22 DEBUG_CALL("udp6_input");
23 DEBUG_ARG("m = %p", m);
25 if (slirp->restricted) {
26 goto bad;
29 ip = mtod(m, struct ip6 *);
30 m->m_len -= iphlen;
31 m->m_data += iphlen;
32 uh = mtod(m, struct udphdr *);
33 m->m_len += iphlen;
34 m->m_data -= iphlen;
36 if (ip6_cksum(m)) {
37 goto bad;
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)) {
48 goto bad;
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.
58 save_ip = *ip;
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;
65 /* handle DHCPv6 */
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))) {
69 m->m_data += iphlen;
70 m->m_len -= iphlen;
71 dhcpv6_input(&lhost, m);
72 m->m_data -= iphlen;
73 m->m_len += iphlen;
74 goto bad;
77 /* handle TFTP */
78 if (ntohs(uh->uh_dport) == TFTP_SERVER &&
79 !memcmp(ip->ip_dst.s6_addr, slirp->vhost_addr6.s6_addr, 16)) {
80 m->m_data += iphlen;
81 m->m_len -= iphlen;
82 tftp_input((struct sockaddr_storage *)&lhost, m);
83 m->m_data -= iphlen;
84 m->m_len += iphlen;
85 goto bad;
88 so = solookup(&slirp->udp_last_so, &slirp->udb,
89 (struct sockaddr_storage *) &lhost, NULL);
91 if (so == NULL) {
92 /* If there's no socket for this packet, create one. */
93 so = socreate(slirp);
94 if (udp_attach(so, AF_INET6) == -1) {
95 DEBUG_MISC(" udp6_attach errno = %d-%s", errno, strerror(errno));
96 sofree(so);
97 goto bad;
100 /* Setup fields */
101 so->so_lfamily = AF_INET6;
102 so->so_laddr6 = ip->ip_src;
103 so->so_lport6 = uh->uh_sport;
106 so->so_ffamily = AF_INET6;
107 so->so_faddr6 = ip->ip_dst; /* XXX */
108 so->so_fport6 = uh->uh_dport; /* XXX */
110 iphlen += sizeof(struct udphdr);
111 m->m_len -= iphlen;
112 m->m_data += iphlen;
115 * Now we sendto() the packet.
117 if (sosendto(so, m) == -1) {
118 m->m_len += iphlen;
119 m->m_data -= iphlen;
120 *ip = save_ip;
121 DEBUG_MISC("udp tx errno = %d-%s", errno, strerror(errno));
122 icmp6_send_error(m, ICMP6_UNREACH, ICMP6_UNREACH_NO_ROUTE);
123 goto bad;
126 m_free(so->so_m); /* used for ICMP if error on sorecvfrom */
128 /* restore the orig mbuf packet */
129 m->m_len += iphlen;
130 m->m_data -= iphlen;
131 *ip = save_ip;
132 so->so_m = m;
134 return;
135 bad:
136 m_free(m);
139 int udp6_output(struct socket *so, struct mbuf *m,
140 struct sockaddr_in6 *saddr, struct sockaddr_in6 *daddr)
142 struct ip6 *ip;
143 struct udphdr *uh;
145 DEBUG_CALL("udp6_output");
146 DEBUG_ARG("so = %p", so);
147 DEBUG_ARG("m = %p", m);
149 /* adjust for header */
150 m->m_data -= sizeof(struct udphdr);
151 m->m_len += sizeof(struct udphdr);
152 uh = mtod(m, struct udphdr *);
153 m->m_data -= sizeof(struct ip6);
154 m->m_len += sizeof(struct ip6);
155 ip = mtod(m, struct ip6 *);
157 /* Build IP header */
158 ip->ip_pl = htons(m->m_len - sizeof(struct ip6));
159 ip->ip_nh = IPPROTO_UDP;
160 ip->ip_src = saddr->sin6_addr;
161 ip->ip_dst = daddr->sin6_addr;
163 /* Build UDP header */
164 uh->uh_sport = saddr->sin6_port;
165 uh->uh_dport = daddr->sin6_port;
166 uh->uh_ulen = ip->ip_pl;
167 uh->uh_sum = 0;
168 uh->uh_sum = ip6_cksum(m);
169 if (uh->uh_sum == 0) {
170 uh->uh_sum = 0xffff;
173 return ip6_output(so, m, 0);