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