udp6_usrreq.c revision 1.1.2.3 1 1.1.2.1 itojun /*
2 1.1.2.1 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 1.1.2.1 itojun * All rights reserved.
4 1.1.2.1 itojun *
5 1.1.2.1 itojun * Redistribution and use in source and binary forms, with or without
6 1.1.2.1 itojun * modification, are permitted provided that the following conditions
7 1.1.2.1 itojun * are met:
8 1.1.2.1 itojun * 1. Redistributions of source code must retain the above copyright
9 1.1.2.1 itojun * notice, this list of conditions and the following disclaimer.
10 1.1.2.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
11 1.1.2.1 itojun * notice, this list of conditions and the following disclaimer in the
12 1.1.2.1 itojun * documentation and/or other materials provided with the distribution.
13 1.1.2.1 itojun * 3. Neither the name of the project nor the names of its contributors
14 1.1.2.1 itojun * may be used to endorse or promote products derived from this software
15 1.1.2.1 itojun * without specific prior written permission.
16 1.1.2.1 itojun *
17 1.1.2.1 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 1.1.2.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1.2.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1.2.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 1.1.2.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1.2.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1.2.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1.2.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1.2.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1.2.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1.2.1 itojun * SUCH DAMAGE.
28 1.1.2.1 itojun */
29 1.1.2.1 itojun
30 1.1.2.1 itojun /*
31 1.1.2.1 itojun * Copyright (c) 1982, 1986, 1989, 1993
32 1.1.2.1 itojun * The Regents of the University of California. All rights reserved.
33 1.1.2.1 itojun *
34 1.1.2.1 itojun * Redistribution and use in source and binary forms, with or without
35 1.1.2.1 itojun * modification, are permitted provided that the following conditions
36 1.1.2.1 itojun * are met:
37 1.1.2.1 itojun * 1. Redistributions of source code must retain the above copyright
38 1.1.2.1 itojun * notice, this list of conditions and the following disclaimer.
39 1.1.2.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
40 1.1.2.1 itojun * notice, this list of conditions and the following disclaimer in the
41 1.1.2.1 itojun * documentation and/or other materials provided with the distribution.
42 1.1.2.1 itojun * 3. All advertising materials mentioning features or use of this software
43 1.1.2.1 itojun * must display the following acknowledgement:
44 1.1.2.1 itojun * This product includes software developed by the University of
45 1.1.2.1 itojun * California, Berkeley and its contributors.
46 1.1.2.1 itojun * 4. Neither the name of the University nor the names of its contributors
47 1.1.2.1 itojun * may be used to endorse or promote products derived from this software
48 1.1.2.1 itojun * without specific prior written permission.
49 1.1.2.1 itojun *
50 1.1.2.1 itojun * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 1.1.2.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 1.1.2.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 1.1.2.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 1.1.2.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 1.1.2.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 1.1.2.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 1.1.2.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 1.1.2.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 1.1.2.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 1.1.2.1 itojun * SUCH DAMAGE.
61 1.1.2.1 itojun *
62 1.1.2.1 itojun * @(#)udp_var.h 8.1 (Berkeley) 6/10/93
63 1.1.2.1 itojun */
64 1.1.2.1 itojun
65 1.1.2.3 itojun #ifdef __NetBSD__ /*XXX*/
66 1.1.2.3 itojun #include "opt_ipsec.h"
67 1.1.2.3 itojun #endif
68 1.1.2.3 itojun
69 1.1.2.1 itojun #include <sys/param.h>
70 1.1.2.1 itojun #include <sys/malloc.h>
71 1.1.2.1 itojun #include <sys/mbuf.h>
72 1.1.2.1 itojun #include <sys/protosw.h>
73 1.1.2.1 itojun #include <sys/socket.h>
74 1.1.2.1 itojun #include <sys/socketvar.h>
75 1.1.2.1 itojun #include <sys/errno.h>
76 1.1.2.1 itojun #include <sys/stat.h>
77 1.1.2.1 itojun #include <sys/systm.h>
78 1.1.2.1 itojun #ifdef __NetBSD__
79 1.1.2.1 itojun #include <sys/proc.h>
80 1.1.2.1 itojun #endif
81 1.1.2.3 itojun #include <sys/syslog.h>
82 1.1.2.1 itojun
83 1.1.2.1 itojun #include <net/if.h>
84 1.1.2.1 itojun #include <net/route.h>
85 1.1.2.1 itojun #include <net/if_types.h>
86 1.1.2.1 itojun
87 1.1.2.1 itojun #include <netinet/in.h>
88 1.1.2.1 itojun #include <netinet/in_var.h>
89 1.1.2.3 itojun #include <netinet/in_systm.h>
90 1.1.2.3 itojun #include <netinet/ip.h>
91 1.1.2.3 itojun #include <netinet/ip_var.h>
92 1.1.2.3 itojun #include <netinet/in_pcb.h>
93 1.1.2.3 itojun #include <netinet/udp.h>
94 1.1.2.3 itojun #include <netinet/udp_var.h>
95 1.1.2.1 itojun #include <netinet6/ip6.h>
96 1.1.2.1 itojun #include <netinet6/in6_pcb.h>
97 1.1.2.1 itojun #include <netinet6/ip6_var.h>
98 1.1.2.1 itojun #include <netinet6/icmp6.h>
99 1.1.2.1 itojun #include <netinet6/udp6_var.h>
100 1.1.2.3 itojun #include <netinet6/ip6protosw.h>
101 1.1.2.1 itojun
102 1.1.2.1 itojun #ifdef IPSEC
103 1.1.2.1 itojun #include <netinet6/ipsec.h>
104 1.1.2.1 itojun #endif /*IPSEC*/
105 1.1.2.1 itojun
106 1.1.2.1 itojun #include "faith.h"
107 1.1.2.1 itojun
108 1.1.2.1 itojun /*
109 1.1.2.1 itojun * UDP protocol inplementation.
110 1.1.2.1 itojun * Per RFC 768, August, 1980.
111 1.1.2.1 itojun */
112 1.1.2.1 itojun
113 1.1.2.1 itojun struct in6pcb *udp6_last_in6pcb = &udb6;
114 1.1.2.1 itojun
115 1.1.2.3 itojun #ifndef __NetBSD__
116 1.1.2.1 itojun static int in6_mcmatch __P((struct in6pcb *, struct in6_addr *, struct ifnet *));
117 1.1.2.3 itojun #endif
118 1.1.2.1 itojun static void udp6_detach __P((struct in6pcb *));
119 1.1.2.1 itojun static void udp6_notify __P((struct in6pcb *, int));
120 1.1.2.1 itojun
121 1.1.2.3 itojun #ifdef __FreeBSD__
122 1.1.2.3 itojun extern int udp_log_in_vain;
123 1.1.2.3 itojun #endif
124 1.1.2.3 itojun
125 1.1.2.1 itojun void
126 1.1.2.1 itojun udp6_init()
127 1.1.2.1 itojun {
128 1.1.2.1 itojun udb6.in6p_next = udb6.in6p_prev = &udb6;
129 1.1.2.1 itojun }
130 1.1.2.1 itojun
131 1.1.2.3 itojun #ifndef __NetBSD__
132 1.1.2.1 itojun static int
133 1.1.2.1 itojun in6_mcmatch(in6p, ia6, ifp)
134 1.1.2.1 itojun struct in6pcb *in6p;
135 1.1.2.1 itojun register struct in6_addr *ia6;
136 1.1.2.1 itojun struct ifnet *ifp;
137 1.1.2.1 itojun {
138 1.1.2.1 itojun struct ip6_moptions *im6o = in6p->in6p_moptions;
139 1.1.2.1 itojun struct in6_multi_mship *imm;
140 1.1.2.1 itojun
141 1.1.2.1 itojun if (im6o == NULL)
142 1.1.2.1 itojun return 0;
143 1.1.2.1 itojun
144 1.1.2.1 itojun for (imm = im6o->im6o_memberships.lh_first; imm != NULL;
145 1.1.2.1 itojun imm = imm->i6mm_chain.le_next) {
146 1.1.2.1 itojun if ((ifp == NULL ||
147 1.1.2.1 itojun imm->i6mm_maddr->in6m_ifp == ifp) &&
148 1.1.2.1 itojun IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
149 1.1.2.1 itojun ia6))
150 1.1.2.1 itojun return 1;
151 1.1.2.1 itojun }
152 1.1.2.1 itojun return 0;
153 1.1.2.1 itojun }
154 1.1.2.1 itojun
155 1.1.2.1 itojun int
156 1.1.2.1 itojun udp6_input(mp, offp, proto)
157 1.1.2.1 itojun struct mbuf **mp;
158 1.1.2.1 itojun int *offp, proto;
159 1.1.2.1 itojun {
160 1.1.2.1 itojun struct mbuf *m = *mp;
161 1.1.2.1 itojun register struct ip6_hdr *ip6;
162 1.1.2.1 itojun register struct udphdr *uh;
163 1.1.2.1 itojun register struct in6pcb *in6p;
164 1.1.2.1 itojun struct mbuf *opts = 0;
165 1.1.2.1 itojun int off = *offp;
166 1.1.2.3 itojun u_int32_t plen, ulen;
167 1.1.2.1 itojun struct sockaddr_in6 udp_in6;
168 1.1.2.1 itojun
169 1.1.2.1 itojun #if defined(NFAITH) && 0 < NFAITH
170 1.1.2.1 itojun if (m->m_pkthdr.rcvif) {
171 1.1.2.1 itojun if (m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
172 1.1.2.1 itojun /* send icmp6 host unreach? */
173 1.1.2.1 itojun m_freem(m);
174 1.1.2.1 itojun return IPPROTO_DONE;
175 1.1.2.1 itojun }
176 1.1.2.1 itojun }
177 1.1.2.1 itojun #endif
178 1.1.2.1 itojun udp6stat.udp6s_ipackets++;
179 1.1.2.1 itojun
180 1.1.2.1 itojun ip6 = mtod(m, struct ip6_hdr *);
181 1.1.2.3 itojun /* check for jumbogram is done in ip6_input. we can trust pkthdr.len */
182 1.1.2.3 itojun plen = m->m_pkthdr.len - off;
183 1.1.2.3 itojun #ifndef PULLDOWN_TEST
184 1.1.2.3 itojun IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
185 1.1.2.1 itojun uh = (struct udphdr *)((caddr_t)ip6 + off);
186 1.1.2.3 itojun #else
187 1.1.2.3 itojun IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udhpdr));
188 1.1.2.3 itojun if (uh == NULL) {
189 1.1.2.3 itojun udp6stat.udp6s_hdrops++;
190 1.1.2.3 itojun return IPPROTO_DONE;
191 1.1.2.3 itojun }
192 1.1.2.3 itojun #endif
193 1.1.2.1 itojun ulen = ntohs((u_short)uh->uh_ulen);
194 1.1.2.3 itojun if (ulen == 0 && plen > 0xffff) /* jumbogram */
195 1.1.2.3 itojun ulen = plen;
196 1.1.2.1 itojun
197 1.1.2.1 itojun if (plen != ulen) {
198 1.1.2.1 itojun udp6stat.udp6s_badlen++;
199 1.1.2.1 itojun goto bad;
200 1.1.2.1 itojun }
201 1.1.2.1 itojun
202 1.1.2.1 itojun /*
203 1.1.2.1 itojun * Checksum extended UDP header and data.
204 1.1.2.1 itojun */
205 1.1.2.1 itojun if (uh->uh_sum == 0)
206 1.1.2.1 itojun udp6stat.udp6s_nosum++;
207 1.1.2.1 itojun else if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
208 1.1.2.1 itojun udp6stat.udp6s_badsum++;
209 1.1.2.1 itojun goto bad;
210 1.1.2.1 itojun }
211 1.1.2.1 itojun
212 1.1.2.1 itojun if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
213 1.1.2.1 itojun struct in6pcb *last;
214 1.1.2.1 itojun
215 1.1.2.1 itojun /*
216 1.1.2.1 itojun * Deliver a multicast datagram to all sockets
217 1.1.2.1 itojun * for which the local and remote addresses and ports match
218 1.1.2.1 itojun * those of the incoming datagram. This allows more than
219 1.1.2.1 itojun * one process to receive multicasts on the same port.
220 1.1.2.1 itojun * (This really ought to be done for unicast datagrams as
221 1.1.2.1 itojun * well, but that would cause problems with existing
222 1.1.2.1 itojun * applications that open both address-specific sockets and
223 1.1.2.1 itojun * a wildcard socket listening to the same port -- they would
224 1.1.2.1 itojun * end up receiving duplicates of every unicast datagram.
225 1.1.2.1 itojun * Those applications open the multiple sockets to overcome an
226 1.1.2.1 itojun * inadequacy of the UDP socket interface, but for backwards
227 1.1.2.1 itojun * compatibility we avoid the problem here rather than
228 1.1.2.1 itojun * fixing the interface. Maybe 4.5BSD will remedy this?)
229 1.1.2.1 itojun */
230 1.1.2.1 itojun
231 1.1.2.1 itojun /*
232 1.1.2.1 itojun * In a case that laddr should be set to the link-local
233 1.1.2.1 itojun * address (this happens in RIPng), the multicast address
234 1.1.2.1 itojun * specified in the received packet does not match with
235 1.1.2.1 itojun * laddr. To cure this situation, the matching is relaxed
236 1.1.2.1 itojun * if the receiving interface is the same as one specified
237 1.1.2.1 itojun * in the socket and if the destination multicast address
238 1.1.2.1 itojun * matches one of the multicast groups specified in the socket.
239 1.1.2.1 itojun */
240 1.1.2.1 itojun
241 1.1.2.1 itojun /*
242 1.1.2.1 itojun * Construct sockaddr format source address.
243 1.1.2.1 itojun */
244 1.1.2.1 itojun bzero(&udp_in6, sizeof(udp_in6));
245 1.1.2.1 itojun udp_in6.sin6_len = sizeof(struct sockaddr_in6);
246 1.1.2.1 itojun udp_in6.sin6_family = AF_INET6;
247 1.1.2.1 itojun udp_in6.sin6_port = uh->uh_sport;
248 1.1.2.1 itojun udp_in6.sin6_addr = ip6->ip6_src;
249 1.1.2.1 itojun if (IN6_IS_SCOPE_LINKLOCAL(&udp_in6.sin6_addr))
250 1.1.2.1 itojun udp_in6.sin6_addr.s6_addr16[1] = 0;
251 1.1.2.1 itojun if (m->m_pkthdr.rcvif) {
252 1.1.2.1 itojun if (IN6_IS_SCOPE_LINKLOCAL(&udp_in6.sin6_addr)) {
253 1.1.2.1 itojun udp_in6.sin6_scope_id =
254 1.1.2.1 itojun m->m_pkthdr.rcvif->if_index;
255 1.1.2.1 itojun } else
256 1.1.2.1 itojun udp_in6.sin6_scope_id = 0;
257 1.1.2.1 itojun } else
258 1.1.2.1 itojun udp_in6.sin6_scope_id = 0;
259 1.1.2.1 itojun /*
260 1.1.2.1 itojun * KAME note: usually we drop udphdr from mbuf here.
261 1.1.2.1 itojun * We need udphdr for IPsec processing so we do that later.
262 1.1.2.1 itojun */
263 1.1.2.1 itojun
264 1.1.2.1 itojun /*
265 1.1.2.1 itojun * Locate pcb(s) for datagram.
266 1.1.2.1 itojun * (Algorithm copied from raw_intr().)
267 1.1.2.1 itojun */
268 1.1.2.1 itojun last = NULL;
269 1.1.2.1 itojun for (in6p = udb6.in6p_next;
270 1.1.2.1 itojun in6p != &udb6;
271 1.1.2.1 itojun in6p = in6p->in6p_next) {
272 1.1.2.1 itojun if (in6p->in6p_lport != uh->uh_dport)
273 1.1.2.1 itojun continue;
274 1.1.2.3 itojun if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
275 1.1.2.1 itojun if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
276 1.1.2.1 itojun &ip6->ip6_dst) &&
277 1.1.2.1 itojun !in6_mcmatch(in6p, &ip6->ip6_dst,
278 1.1.2.1 itojun m->m_pkthdr.rcvif))
279 1.1.2.1 itojun continue;
280 1.1.2.1 itojun }
281 1.1.2.3 itojun if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
282 1.1.2.1 itojun if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
283 1.1.2.1 itojun &ip6->ip6_src) ||
284 1.1.2.1 itojun in6p->in6p_fport != uh->uh_sport)
285 1.1.2.1 itojun continue;
286 1.1.2.1 itojun }
287 1.1.2.1 itojun
288 1.1.2.1 itojun if (last != NULL) {
289 1.1.2.1 itojun struct mbuf *n;
290 1.1.2.1 itojun
291 1.1.2.1 itojun #ifdef IPSEC
292 1.1.2.1 itojun /*
293 1.1.2.1 itojun * Check AH/ESP integrity.
294 1.1.2.1 itojun */
295 1.1.2.1 itojun if (last != NULL && ipsec6_in_reject(m, last)) {
296 1.1.2.1 itojun ipsec6stat.in_polvio++;
297 1.1.2.1 itojun /* do not inject data into pcb */
298 1.1.2.1 itojun } else
299 1.1.2.1 itojun #endif /*IPSEC*/
300 1.1.2.1 itojun if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
301 1.1.2.1 itojun /*
302 1.1.2.1 itojun * KAME NOTE: do not
303 1.1.2.1 itojun * m_copy(m, offset, ...) above.
304 1.1.2.1 itojun * sbappendaddr() expects M_PKTHDR,
305 1.1.2.1 itojun * and m_copy() will copy M_PKTHDR
306 1.1.2.1 itojun * only if offset is 0.
307 1.1.2.1 itojun */
308 1.1.2.3 itojun if (last->in6p_flags & IN6P_CONTROLOPTS
309 1.1.2.3 itojun #ifdef SO_TIMESTAMP
310 1.1.2.3 itojun || last->in6p_socket->so_options & SO_TIMESTAMP
311 1.1.2.3 itojun #endif
312 1.1.2.3 itojun ) {
313 1.1.2.1 itojun ip6_savecontrol(last, &opts,
314 1.1.2.1 itojun ip6, n);
315 1.1.2.1 itojun }
316 1.1.2.1 itojun
317 1.1.2.3 itojun m_adj(n, off + sizeof(struct udphdr));
318 1.1.2.1 itojun if (sbappendaddr(&last->in6p_socket->so_rcv,
319 1.1.2.1 itojun (struct sockaddr *)&udp_in6,
320 1.1.2.1 itojun n, opts) == 0) {
321 1.1.2.1 itojun m_freem(n);
322 1.1.2.1 itojun if (opts)
323 1.1.2.1 itojun m_freem(opts);
324 1.1.2.1 itojun udp6stat.udp6s_fullsock++;
325 1.1.2.1 itojun } else
326 1.1.2.1 itojun sorwakeup(last->in6p_socket);
327 1.1.2.1 itojun opts = 0;
328 1.1.2.1 itojun }
329 1.1.2.1 itojun }
330 1.1.2.1 itojun last = in6p;
331 1.1.2.1 itojun /*
332 1.1.2.1 itojun * Don't look for additional matches if this one does
333 1.1.2.1 itojun * not have either the SO_REUSEPORT or SO_REUSEADDR
334 1.1.2.1 itojun * socket options set. This heuristic avoids searching
335 1.1.2.1 itojun * through all pcbs in the common case of a non-shared
336 1.1.2.1 itojun * port. It assumes that an application will never
337 1.1.2.1 itojun * clear these options after setting them.
338 1.1.2.1 itojun */
339 1.1.2.1 itojun if ((last->in6p_socket->so_options &
340 1.1.2.1 itojun (SO_REUSEPORT|SO_REUSEADDR)) == 0)
341 1.1.2.1 itojun break;
342 1.1.2.1 itojun }
343 1.1.2.1 itojun
344 1.1.2.1 itojun if (last == NULL) {
345 1.1.2.1 itojun /*
346 1.1.2.1 itojun * No matching pcb found; discard datagram.
347 1.1.2.1 itojun * (No need to send an ICMP Port Unreachable
348 1.1.2.1 itojun * for a broadcast or multicast datgram.)
349 1.1.2.1 itojun */
350 1.1.2.1 itojun udp6stat.udp6s_noport++;
351 1.1.2.1 itojun udp6stat.udp6s_noportmcast++;
352 1.1.2.1 itojun goto bad;
353 1.1.2.1 itojun }
354 1.1.2.1 itojun #ifdef IPSEC
355 1.1.2.1 itojun /*
356 1.1.2.1 itojun * Check AH/ESP integrity.
357 1.1.2.1 itojun */
358 1.1.2.1 itojun if (last != NULL && ipsec6_in_reject(m, last)) {
359 1.1.2.1 itojun ipsec6stat.in_polvio++;
360 1.1.2.1 itojun goto bad;
361 1.1.2.1 itojun }
362 1.1.2.1 itojun #endif /*IPSEC*/
363 1.1.2.3 itojun if (last->in6p_flags & IN6P_CONTROLOPTS
364 1.1.2.3 itojun #ifdef SO_TIMESTAMP
365 1.1.2.3 itojun || last->in6p_socket->so_options & SO_TIMESTAMP
366 1.1.2.3 itojun #endif
367 1.1.2.3 itojun ) {
368 1.1.2.1 itojun ip6_savecontrol(last, &opts, ip6, m);
369 1.1.2.3 itojun }
370 1.1.2.1 itojun
371 1.1.2.1 itojun m_adj(m, off + sizeof(struct udphdr));
372 1.1.2.1 itojun if (sbappendaddr(&last->in6p_socket->so_rcv,
373 1.1.2.1 itojun (struct sockaddr *)&udp_in6,
374 1.1.2.1 itojun m, opts) == 0) {
375 1.1.2.1 itojun udp6stat.udp6s_fullsock++;
376 1.1.2.1 itojun goto bad;
377 1.1.2.1 itojun }
378 1.1.2.1 itojun sorwakeup(last->in6p_socket);
379 1.1.2.1 itojun return IPPROTO_DONE;
380 1.1.2.1 itojun }
381 1.1.2.1 itojun /*
382 1.1.2.1 itojun * Locate pcb for datagram.
383 1.1.2.1 itojun */
384 1.1.2.1 itojun in6p = udp6_last_in6pcb;
385 1.1.2.1 itojun if (in6p->in6p_lport != uh->uh_dport ||
386 1.1.2.1 itojun in6p->in6p_fport != uh->uh_sport ||
387 1.1.2.1 itojun !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src) ||
388 1.1.2.1 itojun !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) {
389 1.1.2.1 itojun in6p = in6_pcblookup(&udb6,
390 1.1.2.1 itojun &ip6->ip6_src, uh->uh_sport,
391 1.1.2.1 itojun &ip6->ip6_dst, uh->uh_dport,
392 1.1.2.1 itojun IN6PLOOKUP_WILDCARD);
393 1.1.2.1 itojun if (in6p)
394 1.1.2.1 itojun udp6_last_in6pcb = in6p;
395 1.1.2.1 itojun udp6stat.udp6ps_pcbcachemiss++;
396 1.1.2.1 itojun }
397 1.1.2.1 itojun if (in6p == 0) {
398 1.1.2.3 itojun #ifdef __FreeBSD__
399 1.1.2.3 itojun if (udp_log_in_vain) {
400 1.1.2.3 itojun char buf[INET6_ADDRSTRLEN];
401 1.1.2.3 itojun
402 1.1.2.3 itojun strcpy(buf, ip6_sprintf(&ip6->ip6_dst));
403 1.1.2.3 itojun log(LOG_INFO,
404 1.1.2.3 itojun "Connection attempt to UDP %s:%d from %s:%d\n",
405 1.1.2.3 itojun buf, ntohs(uh->uh_dport),
406 1.1.2.3 itojun ip6_sprintf(&ip6->ip6_src), ntohs(uh->uh_sport));
407 1.1.2.3 itojun }
408 1.1.2.3 itojun #endif
409 1.1.2.1 itojun udp6stat.udp6s_noport++;
410 1.1.2.1 itojun if (m->m_flags & M_MCAST) {
411 1.1.2.1 itojun printf("UDP6: M_MCAST is set in a unicast packet.\n");
412 1.1.2.1 itojun udp6stat.udp6s_noportmcast++;
413 1.1.2.1 itojun goto bad;
414 1.1.2.1 itojun }
415 1.1.2.1 itojun icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
416 1.1.2.1 itojun return IPPROTO_DONE;
417 1.1.2.1 itojun }
418 1.1.2.1 itojun #ifdef IPSEC
419 1.1.2.1 itojun /*
420 1.1.2.1 itojun * Check AH/ESP integrity.
421 1.1.2.1 itojun */
422 1.1.2.1 itojun if (in6p != NULL && ipsec6_in_reject(m, in6p)) {
423 1.1.2.1 itojun ipsec6stat.in_polvio++;
424 1.1.2.1 itojun goto bad;
425 1.1.2.1 itojun }
426 1.1.2.1 itojun #endif /*IPSEC*/
427 1.1.2.1 itojun
428 1.1.2.1 itojun /*
429 1.1.2.1 itojun * Construct sockaddr format source address.
430 1.1.2.1 itojun * Stuff source address and datagram in user buffer.
431 1.1.2.1 itojun */
432 1.1.2.1 itojun bzero(&udp_in6, sizeof(udp_in6));
433 1.1.2.1 itojun udp_in6.sin6_len = sizeof(struct sockaddr_in6);
434 1.1.2.1 itojun udp_in6.sin6_family = AF_INET6;
435 1.1.2.1 itojun udp_in6.sin6_port = uh->uh_sport;
436 1.1.2.1 itojun udp_in6.sin6_addr = ip6->ip6_src;
437 1.1.2.1 itojun if (IN6_IS_SCOPE_LINKLOCAL(&udp_in6.sin6_addr))
438 1.1.2.1 itojun udp_in6.sin6_addr.s6_addr16[1] = 0;
439 1.1.2.1 itojun if (m->m_pkthdr.rcvif) {
440 1.1.2.1 itojun if (IN6_IS_SCOPE_LINKLOCAL(&udp_in6.sin6_addr))
441 1.1.2.1 itojun udp_in6.sin6_scope_id = m->m_pkthdr.rcvif->if_index;
442 1.1.2.1 itojun else
443 1.1.2.1 itojun udp_in6.sin6_scope_id = 0;
444 1.1.2.1 itojun } else
445 1.1.2.1 itojun udp_in6.sin6_scope_id = 0;
446 1.1.2.3 itojun if (in6p->in6p_flags & IN6P_CONTROLOPTS
447 1.1.2.3 itojun #ifdef SO_TIMESTAMP
448 1.1.2.3 itojun || in6p->in6p_socket->so_options & SO_TIMESTAMP
449 1.1.2.3 itojun #endif
450 1.1.2.3 itojun ) {
451 1.1.2.1 itojun ip6_savecontrol(in6p, &opts, ip6, m);
452 1.1.2.3 itojun }
453 1.1.2.1 itojun
454 1.1.2.1 itojun m_adj(m, off + sizeof(struct udphdr));
455 1.1.2.1 itojun if (sbappendaddr(&in6p->in6p_socket->so_rcv,
456 1.1.2.1 itojun (struct sockaddr *)&udp_in6,
457 1.1.2.1 itojun m, opts) == 0) {
458 1.1.2.1 itojun udp6stat.udp6s_fullsock++;
459 1.1.2.1 itojun goto bad;
460 1.1.2.1 itojun }
461 1.1.2.1 itojun sorwakeup(in6p->in6p_socket);
462 1.1.2.1 itojun return IPPROTO_DONE;
463 1.1.2.1 itojun bad:
464 1.1.2.1 itojun if (m)
465 1.1.2.1 itojun m_freem(m);
466 1.1.2.1 itojun if (opts)
467 1.1.2.1 itojun m_freem(opts);
468 1.1.2.1 itojun return IPPROTO_DONE;
469 1.1.2.1 itojun }
470 1.1.2.3 itojun #endif
471 1.1.2.1 itojun
472 1.1.2.1 itojun /*
473 1.1.2.1 itojun * Notify a udp user of an asynchronous error;
474 1.1.2.1 itojun * just wake up so tat he can collect error status.
475 1.1.2.1 itojun */
476 1.1.2.1 itojun static void
477 1.1.2.1 itojun udp6_notify(in6p, errno)
478 1.1.2.1 itojun register struct in6pcb *in6p;
479 1.1.2.1 itojun int errno;
480 1.1.2.1 itojun {
481 1.1.2.1 itojun in6p->in6p_socket->so_error = errno;
482 1.1.2.1 itojun sorwakeup(in6p->in6p_socket);
483 1.1.2.1 itojun sowwakeup(in6p->in6p_socket);
484 1.1.2.1 itojun }
485 1.1.2.1 itojun
486 1.1.2.1 itojun void
487 1.1.2.3 itojun udp6_ctlinput(cmd, sa, d)
488 1.1.2.1 itojun int cmd;
489 1.1.2.1 itojun struct sockaddr *sa;
490 1.1.2.3 itojun void *d;
491 1.1.2.1 itojun {
492 1.1.2.1 itojun register struct udphdr *uhp;
493 1.1.2.1 itojun struct udphdr uh;
494 1.1.2.3 itojun struct sockaddr_in6 sa6;
495 1.1.2.3 itojun register struct ip6_hdr *ip6;
496 1.1.2.3 itojun struct mbuf *m;
497 1.1.2.3 itojun int off;
498 1.1.2.3 itojun
499 1.1.2.3 itojun if (sa->sa_family != AF_INET6 ||
500 1.1.2.3 itojun sa->sa_len != sizeof(struct sockaddr_in6))
501 1.1.2.3 itojun return;
502 1.1.2.1 itojun
503 1.1.2.1 itojun #if 0
504 1.1.2.1 itojun if (cmd == PRC_IFNEWADDR)
505 1.1.2.1 itojun in6_mrejoin(&udb6);
506 1.1.2.3 itojun } else
507 1.1.2.1 itojun #endif
508 1.1.2.1 itojun if (!PRC_IS_REDIRECT(cmd) &&
509 1.1.2.1 itojun ((unsigned)cmd >= PRC_NCMDS || inet6ctlerrmap[cmd] == 0))
510 1.1.2.1 itojun return;
511 1.1.2.3 itojun
512 1.1.2.3 itojun /* if the parameter is from icmp6, decode it. */
513 1.1.2.3 itojun if (d != NULL) {
514 1.1.2.3 itojun struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d;
515 1.1.2.3 itojun m = ip6cp->ip6c_m;
516 1.1.2.3 itojun ip6 = ip6cp->ip6c_ip6;
517 1.1.2.3 itojun off = ip6cp->ip6c_off;
518 1.1.2.3 itojun } else {
519 1.1.2.3 itojun m = NULL;
520 1.1.2.3 itojun ip6 = NULL;
521 1.1.2.3 itojun }
522 1.1.2.3 itojun
523 1.1.2.3 itojun /* translate addresses into internal form */
524 1.1.2.3 itojun sa6 = *(struct sockaddr_in6 *)sa;
525 1.1.2.3 itojun if (IN6_IS_ADDR_LINKLOCAL(&sa6.sin6_addr))
526 1.1.2.3 itojun sa6.sin6_addr.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
527 1.1.2.3 itojun
528 1.1.2.1 itojun if (ip6) {
529 1.1.2.1 itojun /*
530 1.1.2.1 itojun * XXX: We assume that when IPV6 is non NULL,
531 1.1.2.1 itojun * M and OFF are valid.
532 1.1.2.1 itojun */
533 1.1.2.3 itojun struct in6_addr s;
534 1.1.2.3 itojun
535 1.1.2.3 itojun /* translate addresses into internal form */
536 1.1.2.3 itojun memcpy(&s, &ip6->ip6_src, sizeof(s));
537 1.1.2.3 itojun if (IN6_IS_ADDR_LINKLOCAL(&s))
538 1.1.2.3 itojun s.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
539 1.1.2.3 itojun
540 1.1.2.1 itojun if (m->m_len < off + sizeof(uh)) {
541 1.1.2.1 itojun /*
542 1.1.2.1 itojun * this should be rare case,
543 1.1.2.1 itojun * so we compromise on this copy...
544 1.1.2.1 itojun */
545 1.1.2.1 itojun m_copydata(m, off, sizeof(uh), (caddr_t)&uh);
546 1.1.2.1 itojun uhp = &uh;
547 1.1.2.1 itojun } else
548 1.1.2.1 itojun uhp = (struct udphdr *)(mtod(m, caddr_t) + off);
549 1.1.2.3 itojun (void) in6_pcbnotify(&udb6, (struct sockaddr *)&sa6,
550 1.1.2.3 itojun uhp->uh_dport, &s,
551 1.1.2.1 itojun uhp->uh_sport, cmd, udp6_notify);
552 1.1.2.1 itojun } else {
553 1.1.2.3 itojun (void) in6_pcbnotify(&udb6, (struct sockaddr *)&sa6, 0,
554 1.1.2.3 itojun &zeroin6_addr, 0, cmd, udp6_notify);
555 1.1.2.1 itojun }
556 1.1.2.1 itojun }
557 1.1.2.1 itojun
558 1.1.2.1 itojun int
559 1.1.2.1 itojun udp6_output(in6p, m, addr6, control)
560 1.1.2.1 itojun register struct in6pcb *in6p;
561 1.1.2.1 itojun register struct mbuf *m;
562 1.1.2.1 itojun struct mbuf *addr6, *control;
563 1.1.2.1 itojun {
564 1.1.2.3 itojun register u_int32_t ulen = m->m_pkthdr.len;
565 1.1.2.3 itojun u_int32_t plen = sizeof(struct udphdr) + ulen;
566 1.1.2.1 itojun struct ip6_hdr *ip6;
567 1.1.2.1 itojun struct udphdr *udp6;
568 1.1.2.3 itojun struct in6_addr *laddr, *faddr;
569 1.1.2.3 itojun u_short fport;
570 1.1.2.3 itojun int error = 0;
571 1.1.2.1 itojun struct ip6_pktopts opt, *stickyopt = in6p->in6p_outputopts;
572 1.1.2.3 itojun int priv;
573 1.1.2.3 itojun #if defined(__NetBSD__) || (defined(__FreeBSD__) && __FreeBSD__ >= 3)
574 1.1.2.1 itojun struct proc *p = curproc; /* XXX */
575 1.1.2.3 itojun #endif
576 1.1.2.3 itojun int af, hlen;
577 1.1.2.3 itojun #ifdef __NetBSD__
578 1.1.2.3 itojun struct ip *ip;
579 1.1.2.3 itojun #endif
580 1.1.2.1 itojun
581 1.1.2.3 itojun priv = 0;
582 1.1.2.3 itojun #if defined(__NetBSD__) || (defined(__FreeBSD__) && __FreeBSD__ >= 3)
583 1.1.2.1 itojun if (p && !suser(p->p_ucred, &p->p_acflag))
584 1.1.2.1 itojun priv = 1;
585 1.1.2.3 itojun #else
586 1.1.2.3 itojun if ((in6p->in6p_socket->so_state & SS_PRIV) != 0)
587 1.1.2.3 itojun priv = 1;
588 1.1.2.3 itojun #endif
589 1.1.2.1 itojun if (control) {
590 1.1.2.1 itojun if ((error = ip6_setpktoptions(control, &opt, priv)) != 0)
591 1.1.2.1 itojun goto release;
592 1.1.2.1 itojun in6p->in6p_outputopts = &opt;
593 1.1.2.1 itojun }
594 1.1.2.1 itojun
595 1.1.2.1 itojun if (addr6) {
596 1.1.2.3 itojun /*
597 1.1.2.3 itojun * IPv4 version of udp_output calls in_pcbconnect in this case,
598 1.1.2.3 itojun * which needs splnet and affects performance.
599 1.1.2.3 itojun * Since we saw no essential reason for calling in_pcbconnect,
600 1.1.2.3 itojun * we get rid of such kind of logic, and call in6_selectsrc
601 1.1.2.3 itojun * and In6_pcbsetport in order to fill in the local address
602 1.1.2.3 itojun * and the local port.
603 1.1.2.3 itojun */
604 1.1.2.3 itojun struct sockaddr_in6 *sin6 = mtod(addr6, struct sockaddr_in6 *);
605 1.1.2.3 itojun
606 1.1.2.3 itojun if (addr6->m_len != sizeof(*sin6)) {
607 1.1.2.3 itojun error = EINVAL;
608 1.1.2.3 itojun goto release;
609 1.1.2.3 itojun }
610 1.1.2.3 itojun if (sin6->sin6_family != AF_INET6) {
611 1.1.2.3 itojun error = EAFNOSUPPORT;
612 1.1.2.3 itojun goto release;
613 1.1.2.3 itojun }
614 1.1.2.3 itojun if (sin6->sin6_port == 0) {
615 1.1.2.3 itojun error = EADDRNOTAVAIL;
616 1.1.2.3 itojun goto release;
617 1.1.2.3 itojun }
618 1.1.2.3 itojun
619 1.1.2.3 itojun if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
620 1.1.2.1 itojun error = EISCONN;
621 1.1.2.1 itojun goto release;
622 1.1.2.1 itojun }
623 1.1.2.3 itojun
624 1.1.2.3 itojun faddr = &sin6->sin6_addr;
625 1.1.2.3 itojun fport = sin6->sin6_port; /* allow 0 port */
626 1.1.2.1 itojun /*
627 1.1.2.3 itojun * If the scope of the destination is link-local,
628 1.1.2.3 itojun * embed the interface
629 1.1.2.3 itojun * index in the address.
630 1.1.2.3 itojun *
631 1.1.2.3 itojun * XXX advanced-api value overrides sin6_scope_id
632 1.1.2.1 itojun */
633 1.1.2.3 itojun if (IN6_IS_ADDR_LINKLOCAL(faddr) ||
634 1.1.2.3 itojun IN6_IS_ADDR_MC_LINKLOCAL(faddr)) {
635 1.1.2.3 itojun struct ip6_pktopts *optp = in6p->in6p_outputopts;
636 1.1.2.3 itojun struct in6_pktinfo *pi = NULL;
637 1.1.2.3 itojun struct ifnet *oifp = NULL;
638 1.1.2.3 itojun struct ip6_moptions *mopt = NULL;
639 1.1.2.3 itojun
640 1.1.2.3 itojun /*
641 1.1.2.3 itojun * XXX Boundary check is assumed to be already done in
642 1.1.2.3 itojun * ip6_setpktoptions().
643 1.1.2.3 itojun */
644 1.1.2.3 itojun if (optp && (pi = optp->ip6po_pktinfo) &&
645 1.1.2.3 itojun pi->ipi6_ifindex) {
646 1.1.2.3 itojun faddr->s6_addr16[1] = htons(pi->ipi6_ifindex);
647 1.1.2.3 itojun oifp = ifindex2ifnet[pi->ipi6_ifindex];
648 1.1.2.3 itojun }
649 1.1.2.3 itojun else if (IN6_IS_ADDR_MULTICAST(faddr) &&
650 1.1.2.3 itojun (mopt = in6p->in6p_moptions) &&
651 1.1.2.3 itojun mopt->im6o_multicast_ifp) {
652 1.1.2.3 itojun oifp = mopt->im6o_multicast_ifp;
653 1.1.2.3 itojun faddr->s6_addr16[1] = oifp->if_index;
654 1.1.2.3 itojun } else if (sin6->sin6_scope_id) {
655 1.1.2.3 itojun /* boundary check */
656 1.1.2.3 itojun if (sin6->sin6_scope_id < 0
657 1.1.2.3 itojun || if_index < sin6->sin6_scope_id) {
658 1.1.2.3 itojun error = ENXIO; /* XXX EINVAL? */
659 1.1.2.3 itojun goto release;
660 1.1.2.3 itojun }
661 1.1.2.3 itojun /* XXX */
662 1.1.2.3 itojun faddr->s6_addr16[1] =
663 1.1.2.3 itojun htons(sin6->sin6_scope_id & 0xffff);
664 1.1.2.3 itojun }
665 1.1.2.3 itojun }
666 1.1.2.3 itojun
667 1.1.2.3 itojun if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
668 1.1.2.3 itojun laddr = in6_selectsrc(sin6, in6p->in6p_outputopts,
669 1.1.2.3 itojun in6p->in6p_moptions,
670 1.1.2.3 itojun &in6p->in6p_route,
671 1.1.2.3 itojun &in6p->in6p_laddr, &error);
672 1.1.2.3 itojun } else
673 1.1.2.3 itojun laddr = &in6p->in6p_laddr; /*XXX*/
674 1.1.2.3 itojun if (laddr == NULL) {
675 1.1.2.3 itojun if (error == 0)
676 1.1.2.3 itojun error = EADDRNOTAVAIL;
677 1.1.2.1 itojun goto release;
678 1.1.2.1 itojun }
679 1.1.2.3 itojun if (in6p->in6p_lport == 0 &&
680 1.1.2.3 itojun (error = in6_pcbsetport(laddr, in6p)) != 0)
681 1.1.2.3 itojun goto release;
682 1.1.2.1 itojun } else {
683 1.1.2.3 itojun if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
684 1.1.2.1 itojun error = ENOTCONN;
685 1.1.2.1 itojun goto release;
686 1.1.2.1 itojun }
687 1.1.2.3 itojun laddr = &in6p->in6p_laddr;
688 1.1.2.3 itojun faddr = &in6p->in6p_faddr;
689 1.1.2.3 itojun fport = in6p->in6p_fport;
690 1.1.2.1 itojun }
691 1.1.2.3 itojun
692 1.1.2.3 itojun if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
693 1.1.2.3 itojun af = AF_INET6;
694 1.1.2.3 itojun hlen = sizeof(struct ip6_hdr);
695 1.1.2.3 itojun } else {
696 1.1.2.3 itojun af = AF_INET;
697 1.1.2.3 itojun hlen = sizeof(struct ip);
698 1.1.2.3 itojun }
699 1.1.2.3 itojun
700 1.1.2.1 itojun /*
701 1.1.2.1 itojun * Calculate data length and get a mbuf
702 1.1.2.1 itojun * for UDP and IP6 headers.
703 1.1.2.1 itojun */
704 1.1.2.3 itojun M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
705 1.1.2.1 itojun if (m == 0) {
706 1.1.2.1 itojun error = ENOBUFS;
707 1.1.2.1 itojun goto release;
708 1.1.2.1 itojun }
709 1.1.2.1 itojun
710 1.1.2.1 itojun /*
711 1.1.2.1 itojun * Stuff checksum and output datagram.
712 1.1.2.1 itojun */
713 1.1.2.3 itojun udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
714 1.1.2.3 itojun udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */
715 1.1.2.3 itojun udp6->uh_dport = fport;
716 1.1.2.3 itojun if (plen <= 0xffff)
717 1.1.2.3 itojun udp6->uh_ulen = htons((u_short)plen);
718 1.1.2.3 itojun else
719 1.1.2.3 itojun udp6->uh_ulen = 0;
720 1.1.2.3 itojun udp6->uh_sum = 0;
721 1.1.2.3 itojun
722 1.1.2.3 itojun switch (af) {
723 1.1.2.3 itojun case AF_INET6:
724 1.1.2.3 itojun ip6 = mtod(m, struct ip6_hdr *);
725 1.1.2.3 itojun ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
726 1.1.2.3 itojun ip6->ip6_vfc = IPV6_VERSION;
727 1.1.2.1 itojun #if 0 /* ip6_plen will be filled in ip6_output. */
728 1.1.2.3 itojun ip6->ip6_plen = htons((u_short)plen);
729 1.1.2.1 itojun #endif
730 1.1.2.3 itojun ip6->ip6_nxt = IPPROTO_UDP;
731 1.1.2.3 itojun ip6->ip6_hlim = in6_selecthlim(in6p,
732 1.1.2.3 itojun in6p->in6p_route.ro_rt ?
733 1.1.2.3 itojun in6p->in6p_route.ro_rt->rt_ifp : NULL);
734 1.1.2.3 itojun ip6->ip6_src = *laddr;
735 1.1.2.3 itojun ip6->ip6_dst = *faddr;
736 1.1.2.3 itojun
737 1.1.2.3 itojun if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
738 1.1.2.3 itojun sizeof(struct ip6_hdr), plen)) == 0) {
739 1.1.2.3 itojun udp6->uh_sum = 0xffff;
740 1.1.2.3 itojun }
741 1.1.2.1 itojun
742 1.1.2.3 itojun udp6stat.udp6s_opackets++;
743 1.1.2.3 itojun #ifdef IPSEC
744 1.1.2.3 itojun m->m_pkthdr.rcvif = (struct ifnet *)in6p->in6p_socket;
745 1.1.2.3 itojun #endif /*IPSEC*/
746 1.1.2.3 itojun error = ip6_output(m, in6p->in6p_outputopts, &in6p->in6p_route,
747 1.1.2.3 itojun 0, in6p->in6p_moptions, NULL);
748 1.1.2.3 itojun break;
749 1.1.2.3 itojun #ifdef __NetBSD__
750 1.1.2.3 itojun case AF_INET:
751 1.1.2.3 itojun /* can't transmit jumbogram over IPv4 */
752 1.1.2.3 itojun if (plen > 0xffff) {
753 1.1.2.3 itojun error = EMSGSIZE;
754 1.1.2.3 itojun goto release;
755 1.1.2.3 itojun }
756 1.1.2.1 itojun
757 1.1.2.3 itojun ip = mtod(m, struct ip *);
758 1.1.2.1 itojun
759 1.1.2.3 itojun ip->ip_len = plen;
760 1.1.2.3 itojun ip->ip_p = IPPROTO_UDP;
761 1.1.2.3 itojun ip->ip_ttl = in6p->in6p_hops; /*XXX*/
762 1.1.2.3 itojun ip->ip_tos = 0; /*XXX*/
763 1.1.2.3 itojun bcopy(&laddr->s6_addr[12], &ip->ip_src, sizeof(ip->ip_src));
764 1.1.2.3 itojun bcopy(&faddr->s6_addr[12], &ip->ip_dst, sizeof(ip->ip_dst));
765 1.1.2.3 itojun
766 1.1.2.3 itojun udp6->uh_sum = 0;
767 1.1.2.3 itojun if ((udp6->uh_sum = in_cksum(m, ulen)) == 0)
768 1.1.2.3 itojun udp6->uh_sum = 0xffff;
769 1.1.2.1 itojun
770 1.1.2.3 itojun udpstat.udps_opackets++;
771 1.1.2.1 itojun #ifdef IPSEC
772 1.1.2.3 itojun m->m_pkthdr.rcvif = NULL; /*XXX*/
773 1.1.2.1 itojun #endif /*IPSEC*/
774 1.1.2.3 itojun error = ip_output(m, NULL, &in6p->in6p_route, 0 /*XXX*/);
775 1.1.2.3 itojun #else
776 1.1.2.3 itojun error = EAFNOSUPPORT;
777 1.1.2.3 itojun goto release;
778 1.1.2.3 itojun #endif
779 1.1.2.3 itojun break;
780 1.1.2.1 itojun }
781 1.1.2.1 itojun goto releaseopt;
782 1.1.2.1 itojun
783 1.1.2.1 itojun release:
784 1.1.2.1 itojun m_freem(m);
785 1.1.2.1 itojun
786 1.1.2.1 itojun releaseopt:
787 1.1.2.1 itojun if (control) {
788 1.1.2.1 itojun in6p->in6p_outputopts = stickyopt;
789 1.1.2.1 itojun m_freem(control);
790 1.1.2.1 itojun }
791 1.1.2.1 itojun return(error);
792 1.1.2.1 itojun }
793 1.1.2.1 itojun
794 1.1.2.1 itojun extern int udp6_sendspace;
795 1.1.2.1 itojun extern int udp6_recvspace;
796 1.1.2.1 itojun
797 1.1.2.1 itojun int
798 1.1.2.3 itojun #if defined(__NetBSD__) || (defined(__FreeBSD__) && __FreeBSD__ >= 3)
799 1.1.2.1 itojun udp6_usrreq(so, req, m, addr6, control, p)
800 1.1.2.1 itojun struct socket *so;
801 1.1.2.1 itojun int req;
802 1.1.2.1 itojun struct mbuf *m, *addr6, *control;
803 1.1.2.1 itojun struct proc *p;
804 1.1.2.3 itojun #else
805 1.1.2.3 itojun udp6_usrreq(so, req, m, addr6, control)
806 1.1.2.3 itojun struct socket *so;
807 1.1.2.3 itojun int req;
808 1.1.2.3 itojun struct mbuf *m, *addr6, *control;
809 1.1.2.3 itojun #endif
810 1.1.2.1 itojun {
811 1.1.2.1 itojun struct in6pcb *in6p = sotoin6pcb(so);
812 1.1.2.1 itojun int error = 0;
813 1.1.2.1 itojun int s;
814 1.1.2.1 itojun
815 1.1.2.1 itojun /*
816 1.1.2.1 itojun * MAPPED_ADDR implementation info:
817 1.1.2.1 itojun * Mapped addr support for PRU_CONTROL is not necessary.
818 1.1.2.1 itojun * Because typical user of PRU_CONTROL is such as ifconfig,
819 1.1.2.1 itojun * and they don't associate any addr to their socket. Then
820 1.1.2.1 itojun * socket family is only hint about the PRU_CONTROL'ed address
821 1.1.2.1 itojun * family, especially when getting addrs from kernel.
822 1.1.2.1 itojun * So AF_INET socket need to be used to control AF_INET addrs,
823 1.1.2.1 itojun * and AF_INET6 socket for AF_INET6 addrs.
824 1.1.2.1 itojun */
825 1.1.2.1 itojun if (req == PRU_CONTROL)
826 1.1.2.1 itojun return(in6_control(so, (u_long)m, (caddr_t)addr6,
827 1.1.2.3 itojun (struct ifnet *)control
828 1.1.2.3 itojun #if defined(__NetBSD__) || (defined(__FreeBSD__) && __FreeBSD__ >= 3)
829 1.1.2.3 itojun , p
830 1.1.2.3 itojun #endif
831 1.1.2.3 itojun ));
832 1.1.2.1 itojun
833 1.1.2.1 itojun if (in6p == NULL && req != PRU_ATTACH) {
834 1.1.2.1 itojun error = EINVAL;
835 1.1.2.1 itojun goto release;
836 1.1.2.1 itojun }
837 1.1.2.1 itojun
838 1.1.2.1 itojun switch (req) {
839 1.1.2.1 itojun case PRU_ATTACH:
840 1.1.2.1 itojun /*
841 1.1.2.1 itojun * MAPPED_ADDR implementation spec:
842 1.1.2.1 itojun * Always attach for IPv6,
843 1.1.2.1 itojun * and only when necessary for IPv4.
844 1.1.2.1 itojun */
845 1.1.2.2 itojun if (in6p != NULL) {
846 1.1.2.1 itojun error = EINVAL;
847 1.1.2.1 itojun break;
848 1.1.2.1 itojun }
849 1.1.2.3 itojun #ifdef __NetBSD__
850 1.1.2.2 itojun s = splsoftnet();
851 1.1.2.3 itojun #else
852 1.1.2.3 itojun s = splnet();
853 1.1.2.3 itojun #endif
854 1.1.2.1 itojun error = in6_pcballoc(so, &udb6);
855 1.1.2.1 itojun splx(s);
856 1.1.2.1 itojun if (error)
857 1.1.2.1 itojun break;
858 1.1.2.1 itojun error = soreserve(so, udp6_sendspace, udp6_recvspace);
859 1.1.2.1 itojun if (error)
860 1.1.2.1 itojun break;
861 1.1.2.1 itojun in6p = sotoin6pcb(so);
862 1.1.2.1 itojun in6p->in6p_cksum = -1; /* just to be sure */
863 1.1.2.1 itojun #ifdef IPSEC
864 1.1.2.3 itojun error = ipsec_init_policy(so, &in6p->in6p_sp);
865 1.1.2.3 itojun if (error != 0) {
866 1.1.2.3 itojun in6_pcbdetach(in6p);
867 1.1.2.3 itojun break;
868 1.1.2.3 itojun }
869 1.1.2.1 itojun #endif /*IPSEC*/
870 1.1.2.1 itojun break;
871 1.1.2.1 itojun
872 1.1.2.1 itojun case PRU_DETACH:
873 1.1.2.1 itojun udp6_detach(in6p);
874 1.1.2.1 itojun break;
875 1.1.2.1 itojun
876 1.1.2.1 itojun case PRU_BIND:
877 1.1.2.3 itojun #ifdef __NetBSD__
878 1.1.2.2 itojun s = splsoftnet();
879 1.1.2.3 itojun #else
880 1.1.2.3 itojun s = splnet();
881 1.1.2.3 itojun #endif
882 1.1.2.1 itojun error = in6_pcbbind(in6p, addr6);
883 1.1.2.1 itojun splx(s);
884 1.1.2.1 itojun break;
885 1.1.2.1 itojun
886 1.1.2.1 itojun case PRU_LISTEN:
887 1.1.2.1 itojun error = EOPNOTSUPP;
888 1.1.2.1 itojun break;
889 1.1.2.1 itojun
890 1.1.2.1 itojun case PRU_CONNECT:
891 1.1.2.3 itojun if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
892 1.1.2.1 itojun error = EISCONN;
893 1.1.2.1 itojun break;
894 1.1.2.1 itojun }
895 1.1.2.3 itojun #ifdef __NetBSD__
896 1.1.2.2 itojun s = splsoftnet();
897 1.1.2.3 itojun #else
898 1.1.2.3 itojun s = splnet();
899 1.1.2.3 itojun #endif
900 1.1.2.1 itojun error = in6_pcbconnect(in6p, addr6);
901 1.1.2.1 itojun if (ip6_auto_flowlabel) {
902 1.1.2.1 itojun in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
903 1.1.2.1 itojun in6p->in6p_flowinfo |=
904 1.1.2.1 itojun (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
905 1.1.2.1 itojun }
906 1.1.2.1 itojun splx(s);
907 1.1.2.1 itojun if (error == 0)
908 1.1.2.1 itojun soisconnected(so);
909 1.1.2.1 itojun break;
910 1.1.2.1 itojun
911 1.1.2.1 itojun case PRU_CONNECT2:
912 1.1.2.1 itojun error = EOPNOTSUPP;
913 1.1.2.1 itojun break;
914 1.1.2.1 itojun
915 1.1.2.1 itojun case PRU_ACCEPT:
916 1.1.2.1 itojun error = EOPNOTSUPP;
917 1.1.2.1 itojun break;
918 1.1.2.1 itojun
919 1.1.2.1 itojun case PRU_DISCONNECT:
920 1.1.2.1 itojun if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
921 1.1.2.1 itojun error = ENOTCONN;
922 1.1.2.1 itojun break;
923 1.1.2.1 itojun }
924 1.1.2.3 itojun #ifdef __NetBSD__
925 1.1.2.2 itojun s = splsoftnet();
926 1.1.2.3 itojun #else
927 1.1.2.3 itojun s = splnet();
928 1.1.2.3 itojun #endif
929 1.1.2.1 itojun in6_pcbdisconnect(in6p);
930 1.1.2.1 itojun bzero((caddr_t)&in6p->in6p_laddr, sizeof(in6p->in6p_laddr));
931 1.1.2.1 itojun splx(s);
932 1.1.2.1 itojun so->so_state &= ~SS_ISCONNECTED; /* XXX */
933 1.1.2.1 itojun break;
934 1.1.2.1 itojun
935 1.1.2.1 itojun case PRU_SHUTDOWN:
936 1.1.2.1 itojun socantsendmore(so);
937 1.1.2.1 itojun break;
938 1.1.2.1 itojun
939 1.1.2.1 itojun case PRU_SEND:
940 1.1.2.1 itojun return(udp6_output(in6p, m, addr6, control));
941 1.1.2.1 itojun
942 1.1.2.1 itojun case PRU_ABORT:
943 1.1.2.1 itojun soisdisconnected(so);
944 1.1.2.1 itojun udp6_detach(in6p);
945 1.1.2.1 itojun break;
946 1.1.2.1 itojun
947 1.1.2.1 itojun case PRU_SOCKADDR:
948 1.1.2.1 itojun in6_setsockaddr(in6p, addr6);
949 1.1.2.1 itojun break;
950 1.1.2.1 itojun
951 1.1.2.1 itojun case PRU_PEERADDR:
952 1.1.2.1 itojun in6_setpeeraddr(in6p, addr6);
953 1.1.2.1 itojun break;
954 1.1.2.1 itojun
955 1.1.2.1 itojun case PRU_SENSE:
956 1.1.2.1 itojun /*
957 1.1.2.1 itojun * stat: don't bother with a blocksize
958 1.1.2.1 itojun */
959 1.1.2.1 itojun return(0);
960 1.1.2.1 itojun
961 1.1.2.1 itojun case PRU_SENDOOB:
962 1.1.2.1 itojun case PRU_FASTTIMO:
963 1.1.2.1 itojun case PRU_SLOWTIMO:
964 1.1.2.1 itojun case PRU_PROTORCV:
965 1.1.2.1 itojun case PRU_PROTOSEND:
966 1.1.2.1 itojun error = EOPNOTSUPP;
967 1.1.2.1 itojun break;
968 1.1.2.1 itojun
969 1.1.2.1 itojun case PRU_RCVD:
970 1.1.2.1 itojun case PRU_RCVOOB:
971 1.1.2.1 itojun return(EOPNOTSUPP); /* do not free mbuf's */
972 1.1.2.1 itojun
973 1.1.2.1 itojun default:
974 1.1.2.1 itojun panic("udp6_usrreq");
975 1.1.2.1 itojun }
976 1.1.2.1 itojun
977 1.1.2.1 itojun release:
978 1.1.2.1 itojun if (control) {
979 1.1.2.1 itojun printf("udp control data unexpectedly retained\n");
980 1.1.2.1 itojun m_freem(control);
981 1.1.2.1 itojun }
982 1.1.2.1 itojun if (m)
983 1.1.2.1 itojun m_freem(m);
984 1.1.2.1 itojun return(error);
985 1.1.2.1 itojun }
986 1.1.2.1 itojun
987 1.1.2.1 itojun static void
988 1.1.2.1 itojun udp6_detach(in6p)
989 1.1.2.1 itojun struct in6pcb *in6p;
990 1.1.2.1 itojun {
991 1.1.2.3 itojun #ifdef __NetBSD__
992 1.1.2.3 itojun int s = splsoftnet();
993 1.1.2.3 itojun #else
994 1.1.2.3 itojun int s = splnet();
995 1.1.2.3 itojun #endif
996 1.1.2.1 itojun
997 1.1.2.1 itojun if (in6p == udp6_last_in6pcb)
998 1.1.2.1 itojun udp6_last_in6pcb = &udb6;
999 1.1.2.1 itojun in6_pcbdetach(in6p);
1000 1.1.2.1 itojun splx(s);
1001 1.1.2.1 itojun }
1002 1.1.2.1 itojun
1003 1.1.2.1 itojun #ifdef __bsdi__
1004 1.1.2.1 itojun int *udp6_sysvars[] = UDP6CTL_VARS;
1005 1.1.2.1 itojun
1006 1.1.2.1 itojun int
1007 1.1.2.1 itojun udp6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
1008 1.1.2.1 itojun int *name;
1009 1.1.2.1 itojun u_int namelen;
1010 1.1.2.1 itojun void *oldp;
1011 1.1.2.1 itojun size_t *oldlenp;
1012 1.1.2.1 itojun void *newp;
1013 1.1.2.1 itojun size_t newlen;
1014 1.1.2.1 itojun {
1015 1.1.2.1 itojun if (name[0] >= UDP6CTL_MAXID)
1016 1.1.2.1 itojun return (EOPNOTSUPP);
1017 1.1.2.1 itojun switch (name[0]) {
1018 1.1.2.1 itojun case UDP6CTL_STATS:
1019 1.1.2.1 itojun return sysctl_rdtrunc(oldp, oldlenp, newp, &udp6stat,
1020 1.1.2.1 itojun sizeof(udp6stat));
1021 1.1.2.1 itojun
1022 1.1.2.1 itojun default:
1023 1.1.2.1 itojun return (sysctl_int_arr(udp6_sysvars, name, namelen,
1024 1.1.2.1 itojun oldp, oldlenp, newp, newlen));
1025 1.1.2.1 itojun }
1026 1.1.2.1 itojun }
1027 1.1.2.1 itojun #endif /*__bsdi__*/
1028 1.1.2.1 itojun
1029 1.1.2.1 itojun #ifdef __NetBSD__
1030 1.1.2.1 itojun #include <vm/vm.h>
1031 1.1.2.1 itojun #include <sys/sysctl.h>
1032 1.1.2.1 itojun
1033 1.1.2.1 itojun int
1034 1.1.2.1 itojun udp6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
1035 1.1.2.1 itojun int *name;
1036 1.1.2.1 itojun u_int namelen;
1037 1.1.2.1 itojun void *oldp;
1038 1.1.2.1 itojun size_t *oldlenp;
1039 1.1.2.1 itojun void *newp;
1040 1.1.2.1 itojun size_t newlen;
1041 1.1.2.1 itojun {
1042 1.1.2.1 itojun /* All sysctl names at this level are terminal. */
1043 1.1.2.1 itojun if (namelen != 1)
1044 1.1.2.1 itojun return ENOTDIR;
1045 1.1.2.1 itojun
1046 1.1.2.1 itojun switch (name[0]) {
1047 1.1.2.1 itojun
1048 1.1.2.1 itojun case UDP6CTL_SENDMAX:
1049 1.1.2.1 itojun return sysctl_int(oldp, oldlenp, newp, newlen,
1050 1.1.2.1 itojun &udp6_sendspace);
1051 1.1.2.1 itojun case UDP6CTL_RECVSPACE:
1052 1.1.2.1 itojun return sysctl_int(oldp, oldlenp, newp, newlen,
1053 1.1.2.1 itojun &udp6_recvspace);
1054 1.1.2.1 itojun default:
1055 1.1.2.1 itojun return ENOPROTOOPT;
1056 1.1.2.1 itojun }
1057 1.1.2.1 itojun /* NOTREACHED */
1058 1.1.2.1 itojun }
1059 1.1.2.1 itojun #endif
1060