1 /* SPDX-License-Identifier: BSD-3-Clause */
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 = %p", 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;
65 if (ntohs(uh->uh_dport) == DHCPV6_SERVER_PORT &&
66 (in6_equal(&ip->ip_dst, &slirp->vhost_addr6) ||
67 in6_dhcp_multicast(&ip->ip_dst))) {
70 dhcpv6_input(&lhost, m);
77 if (ntohs(uh->uh_dport) == TFTP_SERVER &&
78 !memcmp(ip->ip_dst.s6_addr, slirp->vhost_addr6.s6_addr, 16)) {
81 tftp_input((struct sockaddr_storage *)&lhost, m);
87 so = solookup(&slirp->udp_last_so, &slirp->udb,
88 (struct sockaddr_storage *) &lhost, NULL);
91 /* If there's no socket for this packet, create one. */
93 if (udp_attach(so, AF_INET6) == -1) {
94 DEBUG_MISC(" udp6_attach errno = %d-%s", errno, strerror(errno));
100 so->so_lfamily = AF_INET6;
101 so->so_laddr6 = ip->ip_src;
102 so->so_lport6 = uh->uh_sport;
105 so->so_ffamily = AF_INET6;
106 so->so_faddr6 = ip->ip_dst; /* XXX */
107 so->so_fport6 = uh->uh_dport; /* XXX */
109 iphlen += sizeof(struct udphdr);
114 * Now we sendto() the packet.
116 if (sosendto(so, m) == -1) {
120 DEBUG_MISC("udp tx errno = %d-%s", errno, strerror(errno));
121 icmp6_send_error(m, ICMP6_UNREACH, ICMP6_UNREACH_NO_ROUTE);
125 m_free(so->so_m); /* used for ICMP if error on sorecvfrom */
127 /* restore the orig mbuf packet */
138 int udp6_output(struct socket *so, struct mbuf *m,
139 struct sockaddr_in6 *saddr, struct sockaddr_in6 *daddr)
144 DEBUG_CALL("udp6_output");
145 DEBUG_ARG("so = %p", so);
146 DEBUG_ARG("m = %p", m);
148 /* adjust for header */
149 m->m_data -= sizeof(struct udphdr);
150 m->m_len += sizeof(struct udphdr);
151 uh = mtod(m, struct udphdr *);
152 m->m_data -= sizeof(struct ip6);
153 m->m_len += sizeof(struct ip6);
154 ip = mtod(m, struct ip6 *);
156 /* Build IP header */
157 ip->ip_pl = htons(m->m_len - sizeof(struct ip6));
158 ip->ip_nh = IPPROTO_UDP;
159 ip->ip_src = saddr->sin6_addr;
160 ip->ip_dst = daddr->sin6_addr;
162 /* Build UDP header */
163 uh->uh_sport = saddr->sin6_port;
164 uh->uh_dport = daddr->sin6_port;
165 uh->uh_ulen = ip->ip_pl;
167 uh->uh_sum = ip6_cksum(m);
168 if (uh->uh_sum == 0) {
172 return ip6_output(so, m, 0);