Deleted Added
full compact
udp6_usrreq.c (54952) udp6_usrreq.c (55009)
1/*
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30/*
31 * Copyright (c) 1982, 1986, 1989, 1993
32 * The Regents of the University of California. All rights reserved.
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 *
62 * @(#)udp_var.h 8.1 (Berkeley) 6/10/93
1/*
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30/*
31 * Copyright (c) 1982, 1986, 1989, 1993
32 * The Regents of the University of California. All rights reserved.
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 *
62 * @(#)udp_var.h 8.1 (Berkeley) 6/10/93
63 * $FreeBSD: head/sys/netinet6/udp6_usrreq.c 54952 1999-12-21 11:14:12Z eivind $
63 * $FreeBSD: head/sys/netinet6/udp6_usrreq.c 55009 1999-12-22 19:13:38Z shin $
64 */
65
64 */
65
66#include "opt_ipsec.h"
67
66#include <sys/param.h>
67#include <sys/kernel.h>
68#include <sys/malloc.h>
69#include <sys/mbuf.h>
70#include <sys/protosw.h>
71#include <sys/socket.h>
72#include <sys/socketvar.h>
73#include <sys/sysctl.h>
74#include <sys/errno.h>
75#include <sys/stat.h>
76#include <sys/systm.h>
77#include <sys/syslog.h>
78#include <sys/proc.h>
79
80#include <net/if.h>
81#include <net/route.h>
82#include <net/if_types.h>
83
84#include <netinet/in.h>
85#include <netinet/in_systm.h>
86#include <netinet/ip.h>
87#include <netinet/in_pcb.h>
88#include <netinet/in_var.h>
89#include <netinet/ip_var.h>
90#include <netinet/udp.h>
91#include <netinet/udp_var.h>
92#include <netinet6/ip6.h>
93#include <netinet6/ip6_var.h>
94#include <netinet6/in6_pcb.h>
95#include <netinet6/icmp6.h>
96#include <netinet6/udp6_var.h>
97#include <netinet6/ip6protosw.h>
98
99#ifdef IPSEC
100#include <netinet6/ipsec.h>
68#include <sys/param.h>
69#include <sys/kernel.h>
70#include <sys/malloc.h>
71#include <sys/mbuf.h>
72#include <sys/protosw.h>
73#include <sys/socket.h>
74#include <sys/socketvar.h>
75#include <sys/sysctl.h>
76#include <sys/errno.h>
77#include <sys/stat.h>
78#include <sys/systm.h>
79#include <sys/syslog.h>
80#include <sys/proc.h>
81
82#include <net/if.h>
83#include <net/route.h>
84#include <net/if_types.h>
85
86#include <netinet/in.h>
87#include <netinet/in_systm.h>
88#include <netinet/ip.h>
89#include <netinet/in_pcb.h>
90#include <netinet/in_var.h>
91#include <netinet/ip_var.h>
92#include <netinet/udp.h>
93#include <netinet/udp_var.h>
94#include <netinet6/ip6.h>
95#include <netinet6/ip6_var.h>
96#include <netinet6/in6_pcb.h>
97#include <netinet6/icmp6.h>
98#include <netinet6/udp6_var.h>
99#include <netinet6/ip6protosw.h>
100
101#ifdef IPSEC
102#include <netinet6/ipsec.h>
103#include <netinet6/ipsec6.h>
101#endif /*IPSEC*/
102
103#include "faith.h"
104
105/*
106 * UDP protocol inplementation.
107 * Per RFC 768, August, 1980.
108 */
109
110extern struct protosw inetsw[];
111static int in6_mcmatch __P((struct inpcb *, struct in6_addr *, struct ifnet *));
112static int udp6_detach __P((struct socket *so));
113
114static int
115in6_mcmatch(in6p, ia6, ifp)
116 struct inpcb *in6p;
117 register struct in6_addr *ia6;
118 struct ifnet *ifp;
119{
120 struct ip6_moptions *im6o = in6p->in6p_moptions;
121 struct in6_multi_mship *imm;
122
123 if (im6o == NULL)
124 return 0;
125
126 for (imm = im6o->im6o_memberships.lh_first; imm != NULL;
127 imm = imm->i6mm_chain.le_next) {
128 if ((ifp == NULL ||
129 imm->i6mm_maddr->in6m_ifp == ifp) &&
130 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
131 ia6))
132 return 1;
133 }
134 return 0;
135}
136
137int
138udp6_input(mp, offp, proto)
139 struct mbuf **mp;
140 int *offp, proto;
141{
142 struct mbuf *m = *mp;
143 register struct ip6_hdr *ip6;
144 register struct udphdr *uh;
145 register struct inpcb *in6p;
146 struct mbuf *opts = 0;
147 int off = *offp;
148 int plen, ulen;
149 struct sockaddr_in6 udp_in6;
150
151#if defined(NFAITH) && 0 < NFAITH
152 if (m->m_pkthdr.rcvif) {
153 if (m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
154 /* XXX send icmp6 host/port unreach? */
155 m_freem(m);
156 return IPPROTO_DONE;
157 }
158 }
159#endif
160 udpstat.udps_ipackets++;
161
162 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
163
164 ip6 = mtod(m, struct ip6_hdr *);
165 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
166 uh = (struct udphdr *)((caddr_t)ip6 + off);
167 ulen = ntohs((u_short)uh->uh_ulen);
168
169 if (plen != ulen) {
170 udpstat.udps_badlen++;
171 goto bad;
172 }
173
174 /*
175 * Checksum extended UDP header and data.
176 */
177 if (uh->uh_sum == 0)
178 udpstat.udps_nosum++;
179 else if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
180 udpstat.udps_badsum++;
181 goto bad;
182 }
183
184 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
185 struct inpcb *last;
186
187 /*
188 * Deliver a multicast datagram to all sockets
189 * for which the local and remote addresses and ports match
190 * those of the incoming datagram. This allows more than
191 * one process to receive multicasts on the same port.
192 * (This really ought to be done for unicast datagrams as
193 * well, but that would cause problems with existing
194 * applications that open both address-specific sockets and
195 * a wildcard socket listening to the same port -- they would
196 * end up receiving duplicates of every unicast datagram.
197 * Those applications open the multiple sockets to overcome an
198 * inadequacy of the UDP socket interface, but for backwards
199 * compatibility we avoid the problem here rather than
200 * fixing the interface. Maybe 4.5BSD will remedy this?)
201 */
202
203 /*
204 * In a case that laddr should be set to the link-local
205 * address (this happens in RIPng), the multicast address
206 * specified in the received packet does not match with
207 * laddr. To cure this situation, the matching is relaxed
208 * if the receiving interface is the same as one specified
209 * in the socket and if the destination multicast address
210 * matches one of the multicast groups specified in the socket.
211 */
212
213 /*
214 * Construct sockaddr format source address.
215 */
216 init_sin6(&udp_in6, m); /* general init */
217 udp_in6.sin6_port = uh->uh_sport;
218 /*
219 * KAME note: usually we drop udphdr from mbuf here.
220 * We need udphdr for IPsec processing so we do that later.
221 */
222
223 /*
224 * Locate pcb(s) for datagram.
225 * (Algorithm copied from raw_intr().)
226 */
227 last = NULL;
228 LIST_FOREACH(in6p, &udb, inp_list) {
229 if ((in6p->inp_vflag & INP_IPV6) == 0)
230 continue;
231 if (in6p->in6p_lport != uh->uh_dport)
232 continue;
233 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
234 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
235 &ip6->ip6_dst) &&
236 !in6_mcmatch(in6p, &ip6->ip6_dst,
237 m->m_pkthdr.rcvif))
238 continue;
239 }
240 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
241 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
242 &ip6->ip6_src) ||
243 in6p->in6p_fport != uh->uh_sport)
244 continue;
245 }
246
247 if (last != NULL) {
248 struct mbuf *n;
249
250#ifdef IPSEC
251 /*
252 * Check AH/ESP integrity.
253 */
104#endif /*IPSEC*/
105
106#include "faith.h"
107
108/*
109 * UDP protocol inplementation.
110 * Per RFC 768, August, 1980.
111 */
112
113extern struct protosw inetsw[];
114static int in6_mcmatch __P((struct inpcb *, struct in6_addr *, struct ifnet *));
115static int udp6_detach __P((struct socket *so));
116
117static int
118in6_mcmatch(in6p, ia6, ifp)
119 struct inpcb *in6p;
120 register struct in6_addr *ia6;
121 struct ifnet *ifp;
122{
123 struct ip6_moptions *im6o = in6p->in6p_moptions;
124 struct in6_multi_mship *imm;
125
126 if (im6o == NULL)
127 return 0;
128
129 for (imm = im6o->im6o_memberships.lh_first; imm != NULL;
130 imm = imm->i6mm_chain.le_next) {
131 if ((ifp == NULL ||
132 imm->i6mm_maddr->in6m_ifp == ifp) &&
133 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
134 ia6))
135 return 1;
136 }
137 return 0;
138}
139
140int
141udp6_input(mp, offp, proto)
142 struct mbuf **mp;
143 int *offp, proto;
144{
145 struct mbuf *m = *mp;
146 register struct ip6_hdr *ip6;
147 register struct udphdr *uh;
148 register struct inpcb *in6p;
149 struct mbuf *opts = 0;
150 int off = *offp;
151 int plen, ulen;
152 struct sockaddr_in6 udp_in6;
153
154#if defined(NFAITH) && 0 < NFAITH
155 if (m->m_pkthdr.rcvif) {
156 if (m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
157 /* XXX send icmp6 host/port unreach? */
158 m_freem(m);
159 return IPPROTO_DONE;
160 }
161 }
162#endif
163 udpstat.udps_ipackets++;
164
165 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
166
167 ip6 = mtod(m, struct ip6_hdr *);
168 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
169 uh = (struct udphdr *)((caddr_t)ip6 + off);
170 ulen = ntohs((u_short)uh->uh_ulen);
171
172 if (plen != ulen) {
173 udpstat.udps_badlen++;
174 goto bad;
175 }
176
177 /*
178 * Checksum extended UDP header and data.
179 */
180 if (uh->uh_sum == 0)
181 udpstat.udps_nosum++;
182 else if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
183 udpstat.udps_badsum++;
184 goto bad;
185 }
186
187 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
188 struct inpcb *last;
189
190 /*
191 * Deliver a multicast datagram to all sockets
192 * for which the local and remote addresses and ports match
193 * those of the incoming datagram. This allows more than
194 * one process to receive multicasts on the same port.
195 * (This really ought to be done for unicast datagrams as
196 * well, but that would cause problems with existing
197 * applications that open both address-specific sockets and
198 * a wildcard socket listening to the same port -- they would
199 * end up receiving duplicates of every unicast datagram.
200 * Those applications open the multiple sockets to overcome an
201 * inadequacy of the UDP socket interface, but for backwards
202 * compatibility we avoid the problem here rather than
203 * fixing the interface. Maybe 4.5BSD will remedy this?)
204 */
205
206 /*
207 * In a case that laddr should be set to the link-local
208 * address (this happens in RIPng), the multicast address
209 * specified in the received packet does not match with
210 * laddr. To cure this situation, the matching is relaxed
211 * if the receiving interface is the same as one specified
212 * in the socket and if the destination multicast address
213 * matches one of the multicast groups specified in the socket.
214 */
215
216 /*
217 * Construct sockaddr format source address.
218 */
219 init_sin6(&udp_in6, m); /* general init */
220 udp_in6.sin6_port = uh->uh_sport;
221 /*
222 * KAME note: usually we drop udphdr from mbuf here.
223 * We need udphdr for IPsec processing so we do that later.
224 */
225
226 /*
227 * Locate pcb(s) for datagram.
228 * (Algorithm copied from raw_intr().)
229 */
230 last = NULL;
231 LIST_FOREACH(in6p, &udb, inp_list) {
232 if ((in6p->inp_vflag & INP_IPV6) == 0)
233 continue;
234 if (in6p->in6p_lport != uh->uh_dport)
235 continue;
236 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
237 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
238 &ip6->ip6_dst) &&
239 !in6_mcmatch(in6p, &ip6->ip6_dst,
240 m->m_pkthdr.rcvif))
241 continue;
242 }
243 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
244 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
245 &ip6->ip6_src) ||
246 in6p->in6p_fport != uh->uh_sport)
247 continue;
248 }
249
250 if (last != NULL) {
251 struct mbuf *n;
252
253#ifdef IPSEC
254 /*
255 * Check AH/ESP integrity.
256 */
254 if (last != NULL &&
255 ipsec6_in_reject_so(m, last->inp_socket)) {
257 if (ipsec6_in_reject_so(m, last->inp_socket))
256 ipsec6stat.in_polvio++;
257 /* do not inject data into pcb */
258 ipsec6stat.in_polvio++;
259 /* do not inject data into pcb */
258 } else
260 else
259#endif /*IPSEC*/
260 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
261 /*
262 * KAME NOTE: do not
263 * m_copy(m, offset, ...) above.
264 * sbappendaddr() expects M_PKTHDR,
265 * and m_copy() will copy M_PKTHDR
266 * only if offset is 0.
267 */
268 if (last->in6p_flags & IN6P_CONTROLOPTS
269 || last->in6p_socket->so_options & SO_TIMESTAMP)
270 ip6_savecontrol(last, &opts,
271 ip6, n);
272 m_adj(n, off + sizeof(struct udphdr));
273 if (sbappendaddr(&last->in6p_socket->so_rcv,
274 (struct sockaddr *)&udp_in6,
275 n, opts) == 0) {
276 m_freem(n);
277 if (opts)
278 m_freem(opts);
279 udpstat.udps_fullsock++;
280 } else
281 sorwakeup(last->in6p_socket);
282 opts = 0;
283 }
284 }
285 last = in6p;
286 /*
287 * Don't look for additional matches if this one does
288 * not have either the SO_REUSEPORT or SO_REUSEADDR
289 * socket options set. This heuristic avoids searching
290 * through all pcbs in the common case of a non-shared
291 * port. It assumes that an application will never
292 * clear these options after setting them.
293 */
294 if ((last->in6p_socket->so_options &
295 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
296 break;
297 }
298
299 if (last == NULL) {
300 /*
301 * No matching pcb found; discard datagram.
302 * (No need to send an ICMP Port Unreachable
303 * for a broadcast or multicast datgram.)
304 */
305 udpstat.udps_noport++;
306 udpstat.udps_noportmcast++;
307 goto bad;
308 }
309#ifdef IPSEC
310 /*
311 * Check AH/ESP integrity.
312 */
261#endif /*IPSEC*/
262 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
263 /*
264 * KAME NOTE: do not
265 * m_copy(m, offset, ...) above.
266 * sbappendaddr() expects M_PKTHDR,
267 * and m_copy() will copy M_PKTHDR
268 * only if offset is 0.
269 */
270 if (last->in6p_flags & IN6P_CONTROLOPTS
271 || last->in6p_socket->so_options & SO_TIMESTAMP)
272 ip6_savecontrol(last, &opts,
273 ip6, n);
274 m_adj(n, off + sizeof(struct udphdr));
275 if (sbappendaddr(&last->in6p_socket->so_rcv,
276 (struct sockaddr *)&udp_in6,
277 n, opts) == 0) {
278 m_freem(n);
279 if (opts)
280 m_freem(opts);
281 udpstat.udps_fullsock++;
282 } else
283 sorwakeup(last->in6p_socket);
284 opts = 0;
285 }
286 }
287 last = in6p;
288 /*
289 * Don't look for additional matches if this one does
290 * not have either the SO_REUSEPORT or SO_REUSEADDR
291 * socket options set. This heuristic avoids searching
292 * through all pcbs in the common case of a non-shared
293 * port. It assumes that an application will never
294 * clear these options after setting them.
295 */
296 if ((last->in6p_socket->so_options &
297 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
298 break;
299 }
300
301 if (last == NULL) {
302 /*
303 * No matching pcb found; discard datagram.
304 * (No need to send an ICMP Port Unreachable
305 * for a broadcast or multicast datgram.)
306 */
307 udpstat.udps_noport++;
308 udpstat.udps_noportmcast++;
309 goto bad;
310 }
311#ifdef IPSEC
312 /*
313 * Check AH/ESP integrity.
314 */
313 if (last != NULL && ipsec6_in_reject_so(m, last->inp_socket)) {
315 if (ipsec6_in_reject_so(m, last->inp_socket)) {
314 ipsec6stat.in_polvio++;
315 goto bad;
316 }
317#endif /*IPSEC*/
318 if (last->in6p_flags & IN6P_CONTROLOPTS
319 || last->in6p_socket->so_options & SO_TIMESTAMP)
320 ip6_savecontrol(last, &opts, ip6, m);
321
322 m_adj(m, off + sizeof(struct udphdr));
323 if (sbappendaddr(&last->in6p_socket->so_rcv,
324 (struct sockaddr *)&udp_in6,
325 m, opts) == 0) {
326 udpstat.udps_fullsock++;
327 goto bad;
328 }
329 sorwakeup(last->in6p_socket);
330 return IPPROTO_DONE;
331 }
332 /*
333 * Locate pcb for datagram.
334 */
335 in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport,
336 &ip6->ip6_dst, uh->uh_dport, 1,
337 m->m_pkthdr.rcvif);
338 if (in6p == 0) {
339 if (log_in_vain) {
340 char buf[INET6_ADDRSTRLEN];
341
342 strcpy(buf, ip6_sprintf(&ip6->ip6_dst));
343 log(LOG_INFO,
344 "Connection attempt to UDP %s:%d from %s:%d\n",
345 buf, ntohs(uh->uh_dport),
346 ip6_sprintf(&ip6->ip6_src), ntohs(uh->uh_sport));
347 }
348 udpstat.udps_noport++;
349 if (m->m_flags & M_MCAST) {
350 printf("UDP6: M_MCAST is set in a unicast packet.\n");
351 udpstat.udps_noportmcast++;
352 goto bad;
353 }
354 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
355 return IPPROTO_DONE;
356 }
357#ifdef IPSEC
358 /*
359 * Check AH/ESP integrity.
360 */
316 ipsec6stat.in_polvio++;
317 goto bad;
318 }
319#endif /*IPSEC*/
320 if (last->in6p_flags & IN6P_CONTROLOPTS
321 || last->in6p_socket->so_options & SO_TIMESTAMP)
322 ip6_savecontrol(last, &opts, ip6, m);
323
324 m_adj(m, off + sizeof(struct udphdr));
325 if (sbappendaddr(&last->in6p_socket->so_rcv,
326 (struct sockaddr *)&udp_in6,
327 m, opts) == 0) {
328 udpstat.udps_fullsock++;
329 goto bad;
330 }
331 sorwakeup(last->in6p_socket);
332 return IPPROTO_DONE;
333 }
334 /*
335 * Locate pcb for datagram.
336 */
337 in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport,
338 &ip6->ip6_dst, uh->uh_dport, 1,
339 m->m_pkthdr.rcvif);
340 if (in6p == 0) {
341 if (log_in_vain) {
342 char buf[INET6_ADDRSTRLEN];
343
344 strcpy(buf, ip6_sprintf(&ip6->ip6_dst));
345 log(LOG_INFO,
346 "Connection attempt to UDP %s:%d from %s:%d\n",
347 buf, ntohs(uh->uh_dport),
348 ip6_sprintf(&ip6->ip6_src), ntohs(uh->uh_sport));
349 }
350 udpstat.udps_noport++;
351 if (m->m_flags & M_MCAST) {
352 printf("UDP6: M_MCAST is set in a unicast packet.\n");
353 udpstat.udps_noportmcast++;
354 goto bad;
355 }
356 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
357 return IPPROTO_DONE;
358 }
359#ifdef IPSEC
360 /*
361 * Check AH/ESP integrity.
362 */
361 if (in6p != NULL && ipsec6_in_reject_so(m, in6p->in6p_socket)) {
363 if (ipsec6_in_reject_so(m, in6p->in6p_socket)) {
362 ipsec6stat.in_polvio++;
363 goto bad;
364 }
365#endif /*IPSEC*/
366
367 /*
368 * Construct sockaddr format source address.
369 * Stuff source address and datagram in user buffer.
370 */
371 init_sin6(&udp_in6, m); /* general init */
372 udp_in6.sin6_port = uh->uh_sport;
373 if (in6p->in6p_flags & IN6P_CONTROLOPTS
374 || in6p->in6p_socket->so_options & SO_TIMESTAMP)
375 ip6_savecontrol(in6p, &opts, ip6, m);
376 m_adj(m, off + sizeof(struct udphdr));
377 if (sbappendaddr(&in6p->in6p_socket->so_rcv,
378 (struct sockaddr *)&udp_in6,
379 m, opts) == 0) {
380 udpstat.udps_fullsock++;
381 goto bad;
382 }
383 sorwakeup(in6p->in6p_socket);
384 return IPPROTO_DONE;
385bad:
386 if (m)
387 m_freem(m);
388 if (opts)
389 m_freem(opts);
390 return IPPROTO_DONE;
391}
392
393void
394udp6_ctlinput(cmd, sa, d)
395 int cmd;
396 struct sockaddr *sa;
397 void *d;
398{
399 register struct udphdr *uhp;
400 struct udphdr uh;
401 struct sockaddr_in6 sa6;
402 struct ip6_hdr *ip6;
403 struct mbuf *m;
404 int off;
405
406 if (sa->sa_family != AF_INET6 ||
407 sa->sa_len != sizeof(struct sockaddr_in6))
408 return;
409
410 off = 0;
411 if (!PRC_IS_REDIRECT(cmd) &&
412 ((unsigned)cmd >= PRC_NCMDS || inet6ctlerrmap[cmd] == 0))
413 return;
414
415 /* if the parameter is from icmp6, decode it. */
416 if (d != NULL) {
417 struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d;
418 m = ip6cp->ip6c_m;
419 ip6 = ip6cp->ip6c_ip6;
420 off = ip6cp->ip6c_off;
421 } else {
422 m = NULL;
423 ip6 = NULL;
424 }
425
426 /* translate addresses into internal form */
427 sa6 = *(struct sockaddr_in6 *)sa;
428 if (IN6_IS_ADDR_LINKLOCAL(&sa6.sin6_addr))
429 sa6.sin6_addr.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
430
431 if (ip6) {
432 /*
433 * XXX: We assume that when IPV6 is non NULL,
434 * M and OFF are valid.
435 */
436 struct in6_addr s;
437
438 /* translate addresses into internal form */
439 memcpy(&s, &ip6->ip6_src, sizeof(s));
440 if (IN6_IS_ADDR_LINKLOCAL(&s))
441 s.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
442
443 if (m->m_len < off + sizeof(uh)) {
444 /*
445 * this should be rare case,
446 * so we compromise on this copy...
447 */
448 m_copydata(m, off, sizeof(uh), (caddr_t)&uh);
449 uhp = &uh;
450 } else
451 uhp = (struct udphdr *)(mtod(m, caddr_t) + off);
452 (void) in6_pcbnotify(&udb, (struct sockaddr *)&sa6,
453 uhp->uh_dport, &s,
454 uhp->uh_sport, cmd, udp_notify);
455 } else
456 (void) in6_pcbnotify(&udb, (struct sockaddr *)&sa6, 0,
457 &zeroin6_addr, 0, cmd, udp_notify);
458}
459
460static int
461udp6_getcred SYSCTL_HANDLER_ARGS
462{
463 struct sockaddr_in6 addrs[2];
464 struct inpcb *inp;
465 int error, s;
466
467 error = suser(req->p);
468 if (error)
469 return (error);
470 error = SYSCTL_IN(req, addrs, sizeof(addrs));
471 if (error)
472 return (error);
473 s = splnet();
474 inp = in6_pcblookup_hash(&udbinfo, &addrs[1].sin6_addr,
475 addrs[1].sin6_port,
476 &addrs[0].sin6_addr, addrs[0].sin6_port,
477 1, NULL);
364 ipsec6stat.in_polvio++;
365 goto bad;
366 }
367#endif /*IPSEC*/
368
369 /*
370 * Construct sockaddr format source address.
371 * Stuff source address and datagram in user buffer.
372 */
373 init_sin6(&udp_in6, m); /* general init */
374 udp_in6.sin6_port = uh->uh_sport;
375 if (in6p->in6p_flags & IN6P_CONTROLOPTS
376 || in6p->in6p_socket->so_options & SO_TIMESTAMP)
377 ip6_savecontrol(in6p, &opts, ip6, m);
378 m_adj(m, off + sizeof(struct udphdr));
379 if (sbappendaddr(&in6p->in6p_socket->so_rcv,
380 (struct sockaddr *)&udp_in6,
381 m, opts) == 0) {
382 udpstat.udps_fullsock++;
383 goto bad;
384 }
385 sorwakeup(in6p->in6p_socket);
386 return IPPROTO_DONE;
387bad:
388 if (m)
389 m_freem(m);
390 if (opts)
391 m_freem(opts);
392 return IPPROTO_DONE;
393}
394
395void
396udp6_ctlinput(cmd, sa, d)
397 int cmd;
398 struct sockaddr *sa;
399 void *d;
400{
401 register struct udphdr *uhp;
402 struct udphdr uh;
403 struct sockaddr_in6 sa6;
404 struct ip6_hdr *ip6;
405 struct mbuf *m;
406 int off;
407
408 if (sa->sa_family != AF_INET6 ||
409 sa->sa_len != sizeof(struct sockaddr_in6))
410 return;
411
412 off = 0;
413 if (!PRC_IS_REDIRECT(cmd) &&
414 ((unsigned)cmd >= PRC_NCMDS || inet6ctlerrmap[cmd] == 0))
415 return;
416
417 /* if the parameter is from icmp6, decode it. */
418 if (d != NULL) {
419 struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d;
420 m = ip6cp->ip6c_m;
421 ip6 = ip6cp->ip6c_ip6;
422 off = ip6cp->ip6c_off;
423 } else {
424 m = NULL;
425 ip6 = NULL;
426 }
427
428 /* translate addresses into internal form */
429 sa6 = *(struct sockaddr_in6 *)sa;
430 if (IN6_IS_ADDR_LINKLOCAL(&sa6.sin6_addr))
431 sa6.sin6_addr.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
432
433 if (ip6) {
434 /*
435 * XXX: We assume that when IPV6 is non NULL,
436 * M and OFF are valid.
437 */
438 struct in6_addr s;
439
440 /* translate addresses into internal form */
441 memcpy(&s, &ip6->ip6_src, sizeof(s));
442 if (IN6_IS_ADDR_LINKLOCAL(&s))
443 s.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
444
445 if (m->m_len < off + sizeof(uh)) {
446 /*
447 * this should be rare case,
448 * so we compromise on this copy...
449 */
450 m_copydata(m, off, sizeof(uh), (caddr_t)&uh);
451 uhp = &uh;
452 } else
453 uhp = (struct udphdr *)(mtod(m, caddr_t) + off);
454 (void) in6_pcbnotify(&udb, (struct sockaddr *)&sa6,
455 uhp->uh_dport, &s,
456 uhp->uh_sport, cmd, udp_notify);
457 } else
458 (void) in6_pcbnotify(&udb, (struct sockaddr *)&sa6, 0,
459 &zeroin6_addr, 0, cmd, udp_notify);
460}
461
462static int
463udp6_getcred SYSCTL_HANDLER_ARGS
464{
465 struct sockaddr_in6 addrs[2];
466 struct inpcb *inp;
467 int error, s;
468
469 error = suser(req->p);
470 if (error)
471 return (error);
472 error = SYSCTL_IN(req, addrs, sizeof(addrs));
473 if (error)
474 return (error);
475 s = splnet();
476 inp = in6_pcblookup_hash(&udbinfo, &addrs[1].sin6_addr,
477 addrs[1].sin6_port,
478 &addrs[0].sin6_addr, addrs[0].sin6_port,
479 1, NULL);
478 if (!inp || !inp->inp_socket || !inp->inp_socket->so_cred) {
480 if (!inp || !inp->inp_socket) {
479 error = ENOENT;
480 goto out;
481 }
482 error = SYSCTL_OUT(req, inp->inp_socket->so_cred,
483 sizeof(struct ucred));
484
485out:
486 splx(s);
487 return (error);
488}
489
490SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
491 0, 0,
492 udp6_getcred, "S,ucred", "Get the ucred of a UDP6 connection");
493
494int
495udp6_output(in6p, m, addr6, control, p)
496 register struct inpcb *in6p;
497 struct mbuf *m;
498 struct sockaddr *addr6;
499 struct mbuf *control;
500 struct proc *p;
501{
502 register int ulen = m->m_pkthdr.len;
503 int plen = sizeof(struct udphdr) + ulen;
504 struct ip6_hdr *ip6;
505 struct udphdr *udp6;
506 struct in6_addr laddr6;
507 int s = 0, error = 0;
508 struct ip6_pktopts opt, *stickyopt = in6p->in6p_outputopts;
509
510 if (control) {
511 if ((error = ip6_setpktoptions(control, &opt, suser(p))) != 0)
512 goto release;
513 in6p->in6p_outputopts = &opt;
514 }
515
516 if (addr6) {
517 laddr6 = in6p->in6p_laddr;
518 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
519 error = EISCONN;
520 goto release;
521 }
522 /*
523 * Must block input while temporarily connected.
524 */
525 s = splnet();
526 /*
527 * XXX: the user might want to overwrite the local address
528 * via an ancillary data.
529 */
530 bzero(&in6p->in6p_laddr, sizeof(struct in6_addr));
531 error = in6_pcbconnect(in6p, addr6, p);
532 if (error) {
533 splx(s);
534 goto release;
535 }
536 } else {
537 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
538 error = ENOTCONN;
539 goto release;
540 }
541 }
542 /*
543 * Calculate data length and get a mbuf
544 * for UDP and IP6 headers.
545 */
546 M_PREPEND(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr),
547 M_DONTWAIT);
548 if (m == 0) {
549 error = ENOBUFS;
550 if (addr6)
551 splx(s);
552 goto release;
553 }
554
555 /*
556 * Stuff checksum and output datagram.
557 */
558 ip6 = mtod(m, struct ip6_hdr *);
481 error = ENOENT;
482 goto out;
483 }
484 error = SYSCTL_OUT(req, inp->inp_socket->so_cred,
485 sizeof(struct ucred));
486
487out:
488 splx(s);
489 return (error);
490}
491
492SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
493 0, 0,
494 udp6_getcred, "S,ucred", "Get the ucred of a UDP6 connection");
495
496int
497udp6_output(in6p, m, addr6, control, p)
498 register struct inpcb *in6p;
499 struct mbuf *m;
500 struct sockaddr *addr6;
501 struct mbuf *control;
502 struct proc *p;
503{
504 register int ulen = m->m_pkthdr.len;
505 int plen = sizeof(struct udphdr) + ulen;
506 struct ip6_hdr *ip6;
507 struct udphdr *udp6;
508 struct in6_addr laddr6;
509 int s = 0, error = 0;
510 struct ip6_pktopts opt, *stickyopt = in6p->in6p_outputopts;
511
512 if (control) {
513 if ((error = ip6_setpktoptions(control, &opt, suser(p))) != 0)
514 goto release;
515 in6p->in6p_outputopts = &opt;
516 }
517
518 if (addr6) {
519 laddr6 = in6p->in6p_laddr;
520 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
521 error = EISCONN;
522 goto release;
523 }
524 /*
525 * Must block input while temporarily connected.
526 */
527 s = splnet();
528 /*
529 * XXX: the user might want to overwrite the local address
530 * via an ancillary data.
531 */
532 bzero(&in6p->in6p_laddr, sizeof(struct in6_addr));
533 error = in6_pcbconnect(in6p, addr6, p);
534 if (error) {
535 splx(s);
536 goto release;
537 }
538 } else {
539 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
540 error = ENOTCONN;
541 goto release;
542 }
543 }
544 /*
545 * Calculate data length and get a mbuf
546 * for UDP and IP6 headers.
547 */
548 M_PREPEND(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr),
549 M_DONTWAIT);
550 if (m == 0) {
551 error = ENOBUFS;
552 if (addr6)
553 splx(s);
554 goto release;
555 }
556
557 /*
558 * Stuff checksum and output datagram.
559 */
560 ip6 = mtod(m, struct ip6_hdr *);
559 ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
560 ip6->ip6_vfc = IPV6_VERSION;
561 ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
562 (in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK);
563 ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
564 (IPV6_VERSION & IPV6_VERSION_MASK);
561 /* ip6_plen will be filled in ip6_output. */
562 ip6->ip6_nxt = IPPROTO_UDP;
563 ip6->ip6_hlim = in6_selecthlim(in6p,
564 in6p->in6p_route.ro_rt ?
565 in6p->in6p_route.ro_rt->rt_ifp :
566 NULL);
567 ip6->ip6_src = in6p->in6p_laddr;
568 ip6->ip6_dst = in6p->in6p_faddr;
569
570 udp6 = (struct udphdr *)(ip6 + 1);
571 udp6->uh_sport = in6p->in6p_lport;
572 udp6->uh_dport = in6p->in6p_fport;
573 udp6->uh_ulen = htons((u_short)plen);
574 udp6->uh_sum = 0;
575
576 if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
577 sizeof(struct ip6_hdr), plen)) == 0) {
578 udp6->uh_sum = 0xffff;
579 }
580
581 udpstat.udps_opackets++;
582
583#ifdef IPSEC
584 m->m_pkthdr.rcvif = (struct ifnet *)in6p->in6p_socket;
585#endif /*IPSEC*/
586 error = ip6_output(m, in6p->in6p_outputopts, &in6p->in6p_route,
565 /* ip6_plen will be filled in ip6_output. */
566 ip6->ip6_nxt = IPPROTO_UDP;
567 ip6->ip6_hlim = in6_selecthlim(in6p,
568 in6p->in6p_route.ro_rt ?
569 in6p->in6p_route.ro_rt->rt_ifp :
570 NULL);
571 ip6->ip6_src = in6p->in6p_laddr;
572 ip6->ip6_dst = in6p->in6p_faddr;
573
574 udp6 = (struct udphdr *)(ip6 + 1);
575 udp6->uh_sport = in6p->in6p_lport;
576 udp6->uh_dport = in6p->in6p_fport;
577 udp6->uh_ulen = htons((u_short)plen);
578 udp6->uh_sum = 0;
579
580 if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
581 sizeof(struct ip6_hdr), plen)) == 0) {
582 udp6->uh_sum = 0xffff;
583 }
584
585 udpstat.udps_opackets++;
586
587#ifdef IPSEC
588 m->m_pkthdr.rcvif = (struct ifnet *)in6p->in6p_socket;
589#endif /*IPSEC*/
590 error = ip6_output(m, in6p->in6p_outputopts, &in6p->in6p_route,
587 0, in6p->in6p_moptions, NULL);
591 IPV6_SOCKINMRCVIF, in6p->in6p_moptions, NULL);
588
589 if (addr6) {
590 in6_pcbdisconnect(in6p);
591 in6p->in6p_laddr = laddr6;
592 splx(s);
593 }
594 goto releaseopt;
595
596release:
597 m_freem(m);
598
599releaseopt:
600 if (control) {
601 in6p->in6p_outputopts = stickyopt;
602 m_freem(control);
603 }
604 return(error);
605}
606
607static int
608udp6_abort(struct socket *so)
609{
610 struct inpcb *inp;
611 int s;
612
613 inp = sotoinpcb(so);
614 if (inp == 0)
615 return EINVAL; /* ??? possible? panic instead? */
616 soisdisconnected(so);
617 s = splnet();
618 in6_pcbdetach(inp);
619 splx(s);
620 return 0;
621}
622
623static int
624udp6_attach(struct socket *so, int proto, struct proc *p)
625{
626 struct inpcb *inp;
627 int s, error;
628
629 inp = sotoinpcb(so);
630 if (inp != 0)
631 return EINVAL;
632
633 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
634 error = soreserve(so, udp_sendspace, udp_recvspace);
635 if (error)
636 return error;
637 }
638 s = splnet();
639 error = in_pcballoc(so, &udbinfo, p);
640 splx(s);
641 if (error)
642 return error;
643 inp = (struct inpcb *)so->so_pcb;
644 inp->inp_vflag |= INP_IPV6;
645 inp->in6p_hops = -1; /* use kernel default */
646 inp->in6p_cksum = -1; /* just to be sure */
647#ifdef IPSEC
648 error = ipsec_init_policy(so, &inp->in6p_sp);
649 if (error != 0) {
650 in6_pcbdetach(inp);
651 return (error);
652 }
653#endif /*IPSEC*/
654 return 0;
655}
656
657static int
658udp6_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
659{
660 struct inpcb *inp;
661 int s, error;
662
663 inp = sotoinpcb(so);
664 if (inp == 0)
665 return EINVAL;
666
667 inp->inp_vflag &= ~INP_IPV4;
668 inp->inp_vflag |= INP_IPV6;
669 if (ip6_mapped_addr_on && (inp->inp_flags & IN6P_BINDV6ONLY) == 0) {
670 struct sockaddr_in6 *sin6_p;
671
672 sin6_p = (struct sockaddr_in6 *)nam;
673
674 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
675 inp->inp_vflag |= INP_IPV4;
676 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
677 struct sockaddr_in sin;
678
679 in6_sin6_2_sin(&sin, sin6_p);
680 inp->inp_vflag |= INP_IPV4;
681 inp->inp_vflag &= ~INP_IPV6;
682 s = splnet();
683 error = in_pcbbind(inp, (struct sockaddr *)&sin, p);
684 splx(s);
685 return error;
686 }
687 }
688
689 s = splnet();
690 error = in6_pcbbind(inp, nam, p);
691 splx(s);
692 return error;
693}
694
695static int
696udp6_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
697{
698 struct inpcb *inp;
699 int s, error;
700
701 inp = sotoinpcb(so);
702 if (inp == 0)
703 return EINVAL;
704
705 if (ip6_mapped_addr_on) {
706 struct sockaddr_in6 *sin6_p;
707
708 sin6_p = (struct sockaddr_in6 *)nam;
709 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
710 struct sockaddr_in sin;
711
712 if (inp->inp_faddr.s_addr != INADDR_ANY)
713 return EISCONN;
714 in6_sin6_2_sin(&sin, sin6_p);
715 s = splnet();
716 error = in_pcbconnect(inp, (struct sockaddr *)&sin, p);
717 splx(s);
718 if (error == 0) {
719 inp->inp_vflag |= INP_IPV4;
720 inp->inp_vflag &= ~INP_IPV6;
721 soisconnected(so);
722 }
723 return error;
724 }
725 }
726
727 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
728 return EISCONN;
729 s = splnet();
730 error = in6_pcbconnect(inp, nam, p);
731 if (ip6_auto_flowlabel) {
732 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
733 inp->in6p_flowinfo |=
734 (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
735 }
736 splx(s);
737 if (error == 0) {
738 if (ip6_mapped_addr_on) { /* should be non mapped addr */
739 inp->inp_vflag &= ~INP_IPV4;
740 inp->inp_vflag |= INP_IPV6;
741 }
742 soisconnected(so);
743 }
744 return error;
745}
746
747static int
748udp6_detach(struct socket *so)
749{
750 struct inpcb *inp;
751 int s;
752
753 inp = sotoinpcb(so);
754 if (inp == 0)
755 return EINVAL;
756 s = splnet();
757 in6_pcbdetach(inp);
758 splx(s);
759 return 0;
760}
761
762static int
763udp6_disconnect(struct socket *so)
764{
765 struct inpcb *inp;
766 int s;
767
768 inp = sotoinpcb(so);
769 if (inp == 0)
770 return EINVAL;
771
772 if (inp->inp_vflag & INP_IPV4) {
773 struct pr_usrreqs *pru;
774
775 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
776 return ((*pru->pru_disconnect)(so));
777 }
778
779 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
780 return ENOTCONN;
781
782 s = splnet();
783 in6_pcbdisconnect(inp);
784 inp->in6p_laddr = in6addr_any;
785 splx(s);
786 so->so_state &= ~SS_ISCONNECTED; /* XXX */
787 return 0;
788}
789
790static int
791udp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
792 struct mbuf *control, struct proc *p)
793{
794 struct inpcb *inp;
795
796 inp = sotoinpcb(so);
797 if (inp == 0) {
798 m_freem(m);
799 return EINVAL;
800 }
801
802 if (ip6_mapped_addr_on) {
803 int hasv4addr;
804 struct sockaddr_in6 *sin6 = 0;
805
806 if (addr == 0)
807 hasv4addr = (inp->inp_vflag & INP_IPV4);
808 else {
809 sin6 = (struct sockaddr_in6 *)addr;
810 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
811 ? 1 : 0;
812 }
813 if (hasv4addr) {
814 struct pr_usrreqs *pru;
815 int error;
816
817 if (sin6)
818 in6_sin6_2_sin_in_sock(addr);
819 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
820 error = ((*pru->pru_send)(so, flags, m, addr, control,
821 p));
822 /* addr will just be freed in sendit(). */
823 return error;
824 }
825 }
826
827 return udp6_output(inp, m, addr, control, p);
828}
829
830struct pr_usrreqs udp6_usrreqs = {
831 udp6_abort, pru_accept_notsupp, udp6_attach, udp6_bind, udp6_connect,
832 pru_connect2_notsupp, in6_control, udp6_detach, udp6_disconnect,
833 pru_listen_notsupp, in6_mapped_peeraddr, pru_rcvd_notsupp,
834 pru_rcvoob_notsupp, udp6_send, pru_sense_null, udp_shutdown,
835 in6_mapped_sockaddr, sosend, soreceive, sopoll
836};
592
593 if (addr6) {
594 in6_pcbdisconnect(in6p);
595 in6p->in6p_laddr = laddr6;
596 splx(s);
597 }
598 goto releaseopt;
599
600release:
601 m_freem(m);
602
603releaseopt:
604 if (control) {
605 in6p->in6p_outputopts = stickyopt;
606 m_freem(control);
607 }
608 return(error);
609}
610
611static int
612udp6_abort(struct socket *so)
613{
614 struct inpcb *inp;
615 int s;
616
617 inp = sotoinpcb(so);
618 if (inp == 0)
619 return EINVAL; /* ??? possible? panic instead? */
620 soisdisconnected(so);
621 s = splnet();
622 in6_pcbdetach(inp);
623 splx(s);
624 return 0;
625}
626
627static int
628udp6_attach(struct socket *so, int proto, struct proc *p)
629{
630 struct inpcb *inp;
631 int s, error;
632
633 inp = sotoinpcb(so);
634 if (inp != 0)
635 return EINVAL;
636
637 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
638 error = soreserve(so, udp_sendspace, udp_recvspace);
639 if (error)
640 return error;
641 }
642 s = splnet();
643 error = in_pcballoc(so, &udbinfo, p);
644 splx(s);
645 if (error)
646 return error;
647 inp = (struct inpcb *)so->so_pcb;
648 inp->inp_vflag |= INP_IPV6;
649 inp->in6p_hops = -1; /* use kernel default */
650 inp->in6p_cksum = -1; /* just to be sure */
651#ifdef IPSEC
652 error = ipsec_init_policy(so, &inp->in6p_sp);
653 if (error != 0) {
654 in6_pcbdetach(inp);
655 return (error);
656 }
657#endif /*IPSEC*/
658 return 0;
659}
660
661static int
662udp6_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
663{
664 struct inpcb *inp;
665 int s, error;
666
667 inp = sotoinpcb(so);
668 if (inp == 0)
669 return EINVAL;
670
671 inp->inp_vflag &= ~INP_IPV4;
672 inp->inp_vflag |= INP_IPV6;
673 if (ip6_mapped_addr_on && (inp->inp_flags & IN6P_BINDV6ONLY) == 0) {
674 struct sockaddr_in6 *sin6_p;
675
676 sin6_p = (struct sockaddr_in6 *)nam;
677
678 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
679 inp->inp_vflag |= INP_IPV4;
680 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
681 struct sockaddr_in sin;
682
683 in6_sin6_2_sin(&sin, sin6_p);
684 inp->inp_vflag |= INP_IPV4;
685 inp->inp_vflag &= ~INP_IPV6;
686 s = splnet();
687 error = in_pcbbind(inp, (struct sockaddr *)&sin, p);
688 splx(s);
689 return error;
690 }
691 }
692
693 s = splnet();
694 error = in6_pcbbind(inp, nam, p);
695 splx(s);
696 return error;
697}
698
699static int
700udp6_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
701{
702 struct inpcb *inp;
703 int s, error;
704
705 inp = sotoinpcb(so);
706 if (inp == 0)
707 return EINVAL;
708
709 if (ip6_mapped_addr_on) {
710 struct sockaddr_in6 *sin6_p;
711
712 sin6_p = (struct sockaddr_in6 *)nam;
713 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
714 struct sockaddr_in sin;
715
716 if (inp->inp_faddr.s_addr != INADDR_ANY)
717 return EISCONN;
718 in6_sin6_2_sin(&sin, sin6_p);
719 s = splnet();
720 error = in_pcbconnect(inp, (struct sockaddr *)&sin, p);
721 splx(s);
722 if (error == 0) {
723 inp->inp_vflag |= INP_IPV4;
724 inp->inp_vflag &= ~INP_IPV6;
725 soisconnected(so);
726 }
727 return error;
728 }
729 }
730
731 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
732 return EISCONN;
733 s = splnet();
734 error = in6_pcbconnect(inp, nam, p);
735 if (ip6_auto_flowlabel) {
736 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
737 inp->in6p_flowinfo |=
738 (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
739 }
740 splx(s);
741 if (error == 0) {
742 if (ip6_mapped_addr_on) { /* should be non mapped addr */
743 inp->inp_vflag &= ~INP_IPV4;
744 inp->inp_vflag |= INP_IPV6;
745 }
746 soisconnected(so);
747 }
748 return error;
749}
750
751static int
752udp6_detach(struct socket *so)
753{
754 struct inpcb *inp;
755 int s;
756
757 inp = sotoinpcb(so);
758 if (inp == 0)
759 return EINVAL;
760 s = splnet();
761 in6_pcbdetach(inp);
762 splx(s);
763 return 0;
764}
765
766static int
767udp6_disconnect(struct socket *so)
768{
769 struct inpcb *inp;
770 int s;
771
772 inp = sotoinpcb(so);
773 if (inp == 0)
774 return EINVAL;
775
776 if (inp->inp_vflag & INP_IPV4) {
777 struct pr_usrreqs *pru;
778
779 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
780 return ((*pru->pru_disconnect)(so));
781 }
782
783 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
784 return ENOTCONN;
785
786 s = splnet();
787 in6_pcbdisconnect(inp);
788 inp->in6p_laddr = in6addr_any;
789 splx(s);
790 so->so_state &= ~SS_ISCONNECTED; /* XXX */
791 return 0;
792}
793
794static int
795udp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
796 struct mbuf *control, struct proc *p)
797{
798 struct inpcb *inp;
799
800 inp = sotoinpcb(so);
801 if (inp == 0) {
802 m_freem(m);
803 return EINVAL;
804 }
805
806 if (ip6_mapped_addr_on) {
807 int hasv4addr;
808 struct sockaddr_in6 *sin6 = 0;
809
810 if (addr == 0)
811 hasv4addr = (inp->inp_vflag & INP_IPV4);
812 else {
813 sin6 = (struct sockaddr_in6 *)addr;
814 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
815 ? 1 : 0;
816 }
817 if (hasv4addr) {
818 struct pr_usrreqs *pru;
819 int error;
820
821 if (sin6)
822 in6_sin6_2_sin_in_sock(addr);
823 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
824 error = ((*pru->pru_send)(so, flags, m, addr, control,
825 p));
826 /* addr will just be freed in sendit(). */
827 return error;
828 }
829 }
830
831 return udp6_output(inp, m, addr, control, p);
832}
833
834struct pr_usrreqs udp6_usrreqs = {
835 udp6_abort, pru_accept_notsupp, udp6_attach, udp6_bind, udp6_connect,
836 pru_connect2_notsupp, in6_control, udp6_detach, udp6_disconnect,
837 pru_listen_notsupp, in6_mapped_peeraddr, pru_rcvd_notsupp,
838 pru_rcvoob_notsupp, udp6_send, pru_sense_null, udp_shutdown,
839 in6_mapped_sockaddr, sosend, soreceive, sopoll
840};