kernel: Make SMP support default (and non-optional).
[dragonfly.git] / sys / netinet / udp_usrreq.c
CommitLineData
984263bc 1/*
66d6c637
JH
2 * Copyright (c) 2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
f23061d4 4 *
66d6c637
JH
5 * This code is derived from software contributed to The DragonFly Project
6 * by Jeffrey M. Hsu.
f23061d4 7 *
66d6c637
JH
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of The DragonFly Project nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific, prior written permission.
f23061d4 19 *
66d6c637
JH
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
66d6c637 34/*
984263bc
MD
35 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
36 * The Regents of the University of California. All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
67 * $FreeBSD: src/sys/netinet/udp_usrreq.c,v 1.64.2.18 2003/01/24 05:11:34 sam Exp $
68 */
69
70#include "opt_ipsec.h"
71#include "opt_inet6.h"
72
73#include <sys/param.h>
74#include <sys/systm.h>
75#include <sys/kernel.h>
76#include <sys/malloc.h>
77#include <sys/mbuf.h>
78#include <sys/domain.h>
79#include <sys/proc.h>
895c1f85 80#include <sys/priv.h>
984263bc
MD
81#include <sys/protosw.h>
82#include <sys/socket.h>
83#include <sys/socketvar.h>
84#include <sys/sysctl.h>
85#include <sys/syslog.h>
3f9db7f8 86#include <sys/in_cksum.h>
984263bc 87
6cef7136
MD
88#include <sys/thread2.h>
89#include <sys/socketvar2.h>
5a38e392 90#include <sys/serialize.h>
6cef7136 91
a00138cb
JS
92#include <machine/stdarg.h>
93
984263bc
MD
94#include <net/if.h>
95#include <net/route.h>
14572273 96#include <net/netmsg2.h>
984263bc
MD
97
98#include <netinet/in.h>
99#include <netinet/in_systm.h>
100#include <netinet/ip.h>
101#ifdef INET6
102#include <netinet/ip6.h>
103#endif
104#include <netinet/in_pcb.h>
105#include <netinet/in_var.h>
106#include <netinet/ip_var.h>
107#ifdef INET6
108#include <netinet6/ip6_var.h>
109#endif
110#include <netinet/ip_icmp.h>
111#include <netinet/icmp_var.h>
112#include <netinet/udp.h>
113#include <netinet/udp_var.h>
114
115#ifdef FAST_IPSEC
bf844ffa 116#include <netproto/ipsec/ipsec.h>
407e896e 117#endif
984263bc
MD
118
119#ifdef IPSEC
120#include <netinet6/ipsec.h>
407e896e 121#endif
984263bc 122
984263bc
MD
123/*
124 * UDP protocol implementation.
125 * Per RFC 768, August, 1980.
126 */
127#ifndef COMPAT_42
128static int udpcksum = 1;
129#else
130static int udpcksum = 0; /* XXX */
131#endif
132SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
c9458756 133 &udpcksum, 0, "Enable checksumming of UDP packets");
984263bc
MD
134
135int log_in_vain = 0;
f23061d4 136SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
984263bc
MD
137 &log_in_vain, 0, "Log all incoming UDP packets");
138
139static int blackhole = 0;
140SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
141 &blackhole, 0, "Do not send port unreachables for refused connects");
142
56ae102f
MD
143static int strict_mcast_mship = 1;
144SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW,
145 &strict_mcast_mship, 0, "Only send multicast to member sockets");
146
0df7608b
SZ
147int udp_sosend_async = 1;
148SYSCTL_INT(_net_inet_udp, OID_AUTO, sosend_async, CTLFLAG_RW,
149 &udp_sosend_async, 0, "UDP asynchronized pru_send");
6ce03112 150
984263bc
MD
151struct inpcbinfo udbinfo;
152
6003dd53 153static struct netisr_barrier *udbinfo_br;
5a38e392 154static struct lwkt_serialize udbinfo_slize = LWKT_SERIALIZE_INITIALIZER;
6003dd53 155
984263bc
MD
156#ifndef UDBHASHSIZE
157#define UDBHASHSIZE 16
158#endif
159
160struct udpstat udpstat; /* from udp_var.h */
161SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW,
162 &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
163
407e896e 164static struct sockaddr_in udp_in = { sizeof udp_in, AF_INET };
984263bc
MD
165#ifdef INET6
166struct udp_in6 {
167 struct sockaddr_in6 uin6_sin;
168 u_char uin6_init_done : 1;
169} udp_in6 = {
407e896e 170 { sizeof udp_in6.uin6_sin, AF_INET6 },
984263bc
MD
171 0
172};
173struct udp_ip6 {
174 struct ip6_hdr uip6_ip6;
175 u_char uip6_init_done : 1;
176} udp_ip6;
177#endif /* INET6 */
178
42a7fc75
RG
179static void udp_append (struct inpcb *last, struct ip *ip,
180 struct mbuf *n, int off);
984263bc 181#ifdef INET6
42a7fc75 182static void ip_2_ip6_hdr (struct ip6_hdr *ip6, struct ip *ip);
984263bc
MD
183#endif
184
7018f314
SW
185static int udp_connect_oncpu(struct socket *so, struct thread *td,
186 struct sockaddr_in *sin, struct sockaddr_in *if_sin);
002c1265 187static int udp_output (struct inpcb *, struct mbuf *, struct sockaddr *,
47d61296 188 struct thread *, int);
984263bc
MD
189
190void
f3f70f0d 191udp_init(void)
984263bc 192{
d2e9e54c 193 in_pcbinfo_init(&udbinfo);
984263bc
MD
194 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
195 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
196 &udbinfo.porthashmask);
2322f6ca
JH
197 udbinfo.wildcardhashbase = hashinit(UDBHASHSIZE, M_PCB,
198 &udbinfo.wildcardhashmask);
9f42c129 199 udbinfo.ipi_size = sizeof(struct inpcb);
6003dd53
SZ
200
201 udbinfo_br = netisr_barrier_create();
984263bc
MD
202}
203
56ae102f
MD
204/*
205 * Check multicast packets to make sure they are only sent to sockets with
206 * multicast memberships for the packet's destination address and arrival
207 * interface. Multicast packets to multicast-unaware sockets are also
208 * disallowed.
209 *
210 * Returns 0 if the packet is acceptable, -1 if it is not.
211 */
0c3c561c 212static __inline int
56ae102f
MD
213check_multicast_membership(struct ip *ip, struct inpcb *inp, struct mbuf *m)
214{
215 int mshipno;
216 struct ip_moptions *mopt;
217
218 if (strict_mcast_mship == 0 ||
219 !IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
0c3c561c 220 return (0);
56ae102f
MD
221 }
222 mopt = inp->inp_moptions;
223 if (mopt == NULL)
0c3c561c 224 return (-1);
58ad643d 225 for (mshipno = 0; mshipno < mopt->imo_num_memberships; ++mshipno) {
0c3c561c
JH
226 struct in_multi *maddr = mopt->imo_membership[mshipno];
227
228 if (ip->ip_dst.s_addr == maddr->inm_addr.s_addr &&
229 m->m_pkthdr.rcvif == maddr->inm_ifp) {
230 return (0);
56ae102f
MD
231 }
232 }
0c3c561c 233 return (-1);
56ae102f
MD
234}
235
002c1265
MD
236int
237udp_input(struct mbuf **mp, int *offp, int proto)
984263bc 238{
a00138cb 239 int iphlen;
2256ba69
RG
240 struct ip *ip;
241 struct udphdr *uh;
242 struct inpcb *inp;
002c1265 243 struct mbuf *m;
407e896e 244 struct mbuf *opts = NULL;
002c1265 245 int len, off;
984263bc
MD
246 struct ip save_ip;
247 struct sockaddr *append_sa;
a00138cb 248
002c1265
MD
249 off = *offp;
250 m = *mp;
251 *mp = NULL;
984263bc 252
a00138cb 253 iphlen = off;
984263bc
MD
254 udpstat.udps_ipackets++;
255
256 /*
257 * Strip IP options, if any; should skip this,
258 * make available to user, and use on returned packets,
259 * but we don't yet have a way to check the checksum
260 * with options still present.
261 */
407e896e 262 if (iphlen > sizeof(struct ip)) {
bddf0751 263 ip_stripoptions(m);
984263bc
MD
264 iphlen = sizeof(struct ip);
265 }
266
267 /*
55d829f8
JH
268 * IP and UDP headers are together in first mbuf.
269 * Already checked and pulled up in ip_demux().
984263bc 270 */
55d829f8
JH
271 KASSERT(m->m_len >= iphlen + sizeof(struct udphdr),
272 ("UDP header not in one mbuf"));
273
984263bc 274 ip = mtod(m, struct ip *);
984263bc
MD
275 uh = (struct udphdr *)((caddr_t)ip + iphlen);
276
277 /* destination port of 0 is illegal, based on RFC768. */
278 if (uh->uh_dport == 0)
279 goto bad;
280
281 /*
282 * Make mbuf data length reflect UDP length.
283 * If not enough data to reflect UDP length, drop.
284 */
285 len = ntohs((u_short)uh->uh_ulen);
286 if (ip->ip_len != len) {
287 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
288 udpstat.udps_badlen++;
289 goto bad;
290 }
291 m_adj(m, len - ip->ip_len);
292 /* ip->ip_len = len; */
293 }
294 /*
295 * Save a copy of the IP header in case we want restore it
296 * for sending an ICMP error message in response.
297 */
298 save_ip = *ip;
299
300 /*
301 * Checksum extended UDP header and data.
302 */
303 if (uh->uh_sum) {
304 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
305 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
306 uh->uh_sum = m->m_pkthdr.csum_data;
307 else
f23061d4 308 uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
984263bc
MD
309 ip->ip_dst.s_addr, htonl((u_short)len +
310 m->m_pkthdr.csum_data + IPPROTO_UDP));
311 uh->uh_sum ^= 0xffff;
312 } else {
313 char b[9];
407e896e 314
984263bc
MD
315 bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
316 bzero(((struct ipovly *)ip)->ih_x1, 9);
317 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
407e896e 318 uh->uh_sum = in_cksum(m, len + sizeof(struct ip));
984263bc
MD
319 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
320 }
321 if (uh->uh_sum) {
322 udpstat.udps_badsum++;
323 m_freem(m);
002c1265 324 return(IPPROTO_DONE);
984263bc
MD
325 }
326 } else
327 udpstat.udps_nosum++;
328
329 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
330 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
331 struct inpcb *last;
407e896e 332
984263bc
MD
333 /*
334 * Deliver a multicast or broadcast datagram to *all* sockets
335 * for which the local and remote addresses and ports match
336 * those of the incoming datagram. This allows more than
337 * one process to receive multi/broadcasts on the same port.
338 * (This really ought to be done for unicast datagrams as
339 * well, but that would cause problems with existing
340 * applications that open both address-specific sockets and
341 * a wildcard socket listening to the same port -- they would
342 * end up receiving duplicates of every unicast datagram.
343 * Those applications open the multiple sockets to overcome an
344 * inadequacy of the UDP socket interface, but for backwards
345 * compatibility we avoid the problem here rather than
346 * fixing the interface. Maybe 4.5BSD will remedy this?)
347 */
348
349 /*
350 * Construct sockaddr format source address.
351 */
352 udp_in.sin_port = uh->uh_sport;
353 udp_in.sin_addr = ip->ip_src;
354 /*
355 * Locate pcb(s) for datagram.
356 * (Algorithm copied from raw_intr().)
357 */
358 last = NULL;
359#ifdef INET6
360 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
361#endif
d2e9e54c 362 LIST_FOREACH(inp, &udbinfo.pcblisthead, inp_list) {
5a38e392 363 KKASSERT((inp->inp_flags & INP_PLACEMARKER) == 0);
984263bc 364#ifdef INET6
407e896e 365 if (!(inp->inp_vflag & INP_IPV4))
984263bc
MD
366 continue;
367#endif
368 if (inp->inp_lport != uh->uh_dport)
369 continue;
370 if (inp->inp_laddr.s_addr != INADDR_ANY) {
371 if (inp->inp_laddr.s_addr !=
372 ip->ip_dst.s_addr)
373 continue;
374 }
375 if (inp->inp_faddr.s_addr != INADDR_ANY) {
376 if (inp->inp_faddr.s_addr !=
377 ip->ip_src.s_addr ||
378 inp->inp_fport != uh->uh_sport)
379 continue;
380 }
381
56ae102f
MD
382 if (check_multicast_membership(ip, inp, m) < 0)
383 continue;
384
984263bc
MD
385 if (last != NULL) {
386 struct mbuf *n;
387
388#ifdef IPSEC
389 /* check AH/ESP integrity. */
390 if (ipsec4_in_reject_so(m, last->inp_socket))
391 ipsecstat.in_polvio++;
392 /* do not inject data to pcb */
393 else
394#endif /*IPSEC*/
395#ifdef FAST_IPSEC
396 /* check AH/ESP integrity. */
397 if (ipsec4_in_reject(m, last))
398 ;
399 else
400#endif /*FAST_IPSEC*/
f23061d4 401 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL)
984263bc
MD
402 udp_append(last, ip, n,
403 iphlen +
404 sizeof(struct udphdr));
405 }
406 last = inp;
407 /*
408 * Don't look for additional matches if this one does
409 * not have either the SO_REUSEPORT or SO_REUSEADDR
410 * socket options set. This heuristic avoids searching
411 * through all pcbs in the common case of a non-shared
412 * port. It * assumes that an application will never
413 * clear these options after setting them.
414 */
407e896e
JH
415 if (!(last->inp_socket->so_options &
416 (SO_REUSEPORT | SO_REUSEADDR)))
984263bc
MD
417 break;
418 }
419
420 if (last == NULL) {
421 /*
422 * No matching pcb found; discard datagram.
423 * (No need to send an ICMP Port Unreachable
424 * for a broadcast or multicast datgram.)
425 */
426 udpstat.udps_noportbcast++;
427 goto bad;
428 }
429#ifdef IPSEC
430 /* check AH/ESP integrity. */
431 if (ipsec4_in_reject_so(m, last->inp_socket)) {
432 ipsecstat.in_polvio++;
433 goto bad;
434 }
435#endif /*IPSEC*/
436#ifdef FAST_IPSEC
437 /* check AH/ESP integrity. */
438 if (ipsec4_in_reject(m, last))
439 goto bad;
440#endif /*FAST_IPSEC*/
441 udp_append(last, ip, m, iphlen + sizeof(struct udphdr));
002c1265 442 return(IPPROTO_DONE);
984263bc
MD
443 }
444 /*
445 * Locate pcb for datagram.
446 */
447 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
448 ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
449 if (inp == NULL) {
450 if (log_in_vain) {
1141eb20 451 char buf[sizeof "aaa.bbb.ccc.ddd"];
984263bc
MD
452
453 strcpy(buf, inet_ntoa(ip->ip_dst));
454 log(LOG_INFO,
455 "Connection attempt to UDP %s:%d from %s:%d\n",
456 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
457 ntohs(uh->uh_sport));
458 }
459 udpstat.udps_noport++;
460 if (m->m_flags & (M_BCAST | M_MCAST)) {
461 udpstat.udps_noportbcast++;
462 goto bad;
463 }
464 if (blackhole)
465 goto bad;
466#ifdef ICMP_BANDLIM
467 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
468 goto bad;
469#endif
470 *ip = save_ip;
471 ip->ip_len += iphlen;
472 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
002c1265 473 return(IPPROTO_DONE);
984263bc
MD
474 }
475#ifdef IPSEC
476 if (ipsec4_in_reject_so(m, inp->inp_socket)) {
477 ipsecstat.in_polvio++;
478 goto bad;
479 }
480#endif /*IPSEC*/
481#ifdef FAST_IPSEC
482 if (ipsec4_in_reject(m, inp))
483 goto bad;
484#endif /*FAST_IPSEC*/
95926362
MD
485 /*
486 * Check the minimum TTL for socket.
487 */
488 if (ip->ip_ttl < inp->inp_ip_minttl)
489 goto bad;
984263bc
MD
490
491 /*
492 * Construct sockaddr format source address.
493 * Stuff source address and datagram in user buffer.
494 */
495 udp_in.sin_port = uh->uh_sport;
496 udp_in.sin_addr = ip->ip_src;
407e896e
JH
497 if ((inp->inp_flags & INP_CONTROLOPTS) ||
498 (inp->inp_socket->so_options & SO_TIMESTAMP)) {
984263bc
MD
499#ifdef INET6
500 if (inp->inp_vflag & INP_IPV6) {
501 int savedflags;
502
503 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
504 savedflags = inp->inp_flags;
505 inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
506 ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m);
507 inp->inp_flags = savedflags;
508 } else
509#endif
510 ip_savecontrol(inp, &opts, ip, m);
511 }
f23061d4 512 m_adj(m, iphlen + sizeof(struct udphdr));
984263bc
MD
513#ifdef INET6
514 if (inp->inp_vflag & INP_IPV6) {
515 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
516 append_sa = (struct sockaddr *)&udp_in6;
517 } else
518#endif
407e896e 519 append_sa = (struct sockaddr *)&udp_in;
74d8470d
MD
520
521 lwkt_gettoken(&inp->inp_socket->so_rcv.ssb_token);
6d49aa6f 522 if (ssb_appendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) {
984263bc 523 udpstat.udps_fullsock++;
74d8470d 524 lwkt_reltoken(&inp->inp_socket->so_rcv.ssb_token);
984263bc
MD
525 goto bad;
526 }
74d8470d 527 lwkt_reltoken(&inp->inp_socket->so_rcv.ssb_token);
984263bc 528 sorwakeup(inp->inp_socket);
002c1265 529 return(IPPROTO_DONE);
984263bc
MD
530bad:
531 m_freem(m);
532 if (opts)
533 m_freem(opts);
002c1265 534 return(IPPROTO_DONE);
984263bc
MD
535}
536
537#ifdef INET6
538static void
f3f70f0d 539ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip)
984263bc 540{
407e896e 541 bzero(ip6, sizeof *ip6);
984263bc
MD
542
543 ip6->ip6_vfc = IPV6_VERSION;
544 ip6->ip6_plen = ip->ip_len;
545 ip6->ip6_nxt = ip->ip_p;
546 ip6->ip6_hlim = ip->ip_ttl;
547 ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
548 IPV6_ADDR_INT32_SMP;
549 ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
550 ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
551}
552#endif
553
554/*
555 * subroutine of udp_input(), mainly for source code readability.
556 * caller must properly init udp_ip6 and udp_in6 beforehand.
557 */
558static void
f3f70f0d 559udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off)
984263bc
MD
560{
561 struct sockaddr *append_sa;
407e896e 562 struct mbuf *opts = NULL;
984263bc
MD
563
564 if (last->inp_flags & INP_CONTROLOPTS ||
565 last->inp_socket->so_options & SO_TIMESTAMP) {
566#ifdef INET6
567 if (last->inp_vflag & INP_IPV6) {
568 int savedflags;
569
570 if (udp_ip6.uip6_init_done == 0) {
571 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
572 udp_ip6.uip6_init_done = 1;
573 }
574 savedflags = last->inp_flags;
575 last->inp_flags &= ~INP_UNMAPPABLEOPTS;
576 ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n);
577 last->inp_flags = savedflags;
578 } else
579#endif
580 ip_savecontrol(last, &opts, ip, n);
581 }
582#ifdef INET6
583 if (last->inp_vflag & INP_IPV6) {
584 if (udp_in6.uin6_init_done == 0) {
585 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
586 udp_in6.uin6_init_done = 1;
587 }
588 append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
589 } else
590#endif
407e896e 591 append_sa = (struct sockaddr *)&udp_in;
984263bc 592 m_adj(n, off);
74d8470d 593 lwkt_gettoken(&last->inp_socket->so_rcv.ssb_token);
6d49aa6f 594 if (ssb_appendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
984263bc
MD
595 m_freem(n);
596 if (opts)
597 m_freem(opts);
598 udpstat.udps_fullsock++;
74d8470d 599 } else {
984263bc 600 sorwakeup(last->inp_socket);
74d8470d
MD
601 }
602 lwkt_reltoken(&last->inp_socket->so_rcv.ssb_token);
984263bc
MD
603}
604
605/*
606 * Notify a udp user of an asynchronous error;
607 * just wake up so that he can collect error status.
608 */
609void
ce4ac214 610udp_notify(struct inpcb *inp, int error)
984263bc 611{
ce4ac214 612 inp->inp_socket->so_error = error;
984263bc
MD
613 sorwakeup(inp->inp_socket);
614 sowwakeup(inp->inp_socket);
615}
616
14572273 617struct netmsg_udp_notify {
002c1265 618 struct netmsg_base base;
14572273
SZ
619 void (*nm_notify)(struct inpcb *, int);
620 struct in_addr nm_faddr;
621 int nm_arg;
622};
623
624static void
002c1265 625udp_notifyall_oncpu(netmsg_t msg)
14572273 626{
002c1265 627 struct netmsg_udp_notify *nm = (struct netmsg_udp_notify *)msg;
0ce85c47 628#if 0
14572273 629 int nextcpu;
0ce85c47 630#endif
14572273 631
002c1265
MD
632 in_pcbnotifyall(&udbinfo.pcblisthead, nm->nm_faddr,
633 nm->nm_arg, nm->nm_notify);
634 lwkt_replymsg(&nm->base.lmsg, 0);
14572273 635
0ce85c47
MD
636#if 0
637 /* XXX currently udp only runs on cpu 0 */
14572273
SZ
638 nextcpu = mycpuid + 1;
639 if (nextcpu < ncpus2)
3abced87 640 lwkt_forwardmsg(netisr_portfn(nextcpu), &nm->base.lmsg);
14572273 641 else
002c1265 642 lwkt_replymsg(&nmsg->base.lmsg, 0);
0ce85c47 643#endif
14572273
SZ
644}
645
646static void
647udp_rtchange(struct inpcb *inp, int err)
648{
14572273
SZ
649 /* XXX Nuke this, once UDP inpcbs are CPU localized */
650 if (inp->inp_route.ro_rt && inp->inp_route.ro_rt->rt_cpuid == mycpuid) {
651 rtfree(inp->inp_route.ro_rt);
652 inp->inp_route.ro_rt = NULL;
653 /*
654 * A new route can be allocated the next time
655 * output is attempted.
656 */
657 }
14572273
SZ
658}
659
984263bc 660void
002c1265 661udp_ctlinput(netmsg_t msg)
984263bc 662{
002c1265
MD
663 struct sockaddr *sa = msg->ctlinput.nm_arg;
664 struct ip *ip = msg->ctlinput.nm_extra;
665 int cmd = msg->ctlinput.nm_cmd;
984263bc 666 struct udphdr *uh;
42a7fc75 667 void (*notify) (struct inpcb *, int) = udp_notify;
f23061d4 668 struct in_addr faddr;
984263bc 669 struct inpcb *inp;
984263bc 670
3abced87 671 KKASSERT(&curthread->td_msgport == netisr_portfn(0));
6003dd53 672
984263bc
MD
673 faddr = ((struct sockaddr_in *)sa)->sin_addr;
674 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
002c1265 675 goto done;
984263bc
MD
676
677 if (PRC_IS_REDIRECT(cmd)) {
407e896e 678 ip = NULL;
14572273 679 notify = udp_rtchange;
002c1265 680 } else if (cmd == PRC_HOSTDEAD) {
407e896e 681 ip = NULL;
002c1265
MD
682 } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
683 goto done;
684 }
685
984263bc 686 if (ip) {
984263bc
MD
687 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
688 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
f23061d4 689 ip->ip_src, uh->uh_sport, 0, NULL);
984263bc
MD
690 if (inp != NULL && inp->inp_socket != NULL)
691 (*notify)(inp, inetctlerrmap[cmd]);
14572273 692 } else if (PRC_IS_REDIRECT(cmd)) {
002c1265 693 struct netmsg_udp_notify *nm;
14572273 694
3abced87 695 KKASSERT(&curthread->td_msgport == netisr_portfn(0));
002c1265
MD
696 nm = kmalloc(sizeof(*nm), M_LWKTMSG, M_INTWAIT);
697 netmsg_init(&nm->base, NULL, &netisr_afree_rport,
48e7b118 698 0, udp_notifyall_oncpu);
002c1265
MD
699 nm->nm_faddr = faddr;
700 nm->nm_arg = inetctlerrmap[cmd];
701 nm->nm_notify = notify;
3abced87 702 lwkt_sendmsg(netisr_portfn(0), &nm->base.lmsg);
14572273
SZ
703 } else {
704 /*
705 * XXX We should forward msg upon PRC_HOSTHEAD and ip == NULL,
706 * once UDP inpcbs are CPU localized
707 */
3abced87 708 KKASSERT(&curthread->td_msgport == netisr_portfn(0));
d2e9e54c 709 in_pcbnotifyall(&udbinfo.pcblisthead, faddr, inetctlerrmap[cmd],
f23061d4 710 notify);
14572273 711 }
002c1265
MD
712done:
713 lwkt_replymsg(&msg->lmsg, 0);
984263bc
MD
714}
715
5a38e392
SZ
716static int
717udp_pcblist(SYSCTL_HANDLER_ARGS)
718{
db190d5c
SZ
719 struct xinpcb *xi;
720 int error, nxi, i;
5a38e392
SZ
721
722 udbinfo_lock();
db190d5c 723 error = in_pcblist_global_nomarker(oidp, arg1, arg2, req, &xi, &nxi);
5a38e392
SZ
724 udbinfo_unlock();
725
db190d5c
SZ
726 if (error) {
727 KKASSERT(xi == NULL);
728 return error;
729 }
730 if (nxi == 0) {
731 KKASSERT(xi == NULL);
732 return 0;
733 }
734
735 for (i = 0; i < nxi; ++i) {
736 error = SYSCTL_OUT(req, &xi[i], sizeof(xi[i]));
737 if (error)
738 break;
739 }
740 kfree(xi, M_TEMP);
741
5a38e392
SZ
742 return error;
743}
48a2763f 744SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, &udbinfo, 0,
5a38e392 745 udp_pcblist, "S,xinpcb", "List of active UDP sockets");
984263bc
MD
746
747static int
748udp_getcred(SYSCTL_HANDLER_ARGS)
749{
750 struct sockaddr_in addrs[2];
17f6e908 751 struct ucred cred0, *cred = NULL;
984263bc 752 struct inpcb *inp;
1cae611f 753 int error;
984263bc 754
895c1f85 755 error = priv_check(req->td, PRIV_ROOT);
984263bc
MD
756 if (error)
757 return (error);
f23061d4 758 error = SYSCTL_IN(req, addrs, sizeof addrs);
984263bc
MD
759 if (error)
760 return (error);
5a38e392
SZ
761
762 udbinfo_lock();
984263bc
MD
763 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
764 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
765 if (inp == NULL || inp->inp_socket == NULL) {
766 error = ENOENT;
17f6e908
SZ
767 } else {
768 if (inp->inp_socket->so_cred != NULL) {
769 cred0 = *(inp->inp_socket->so_cred);
770 cred = &cred0;
771 }
984263bc 772 }
5a38e392 773 udbinfo_unlock();
17f6e908
SZ
774
775 if (error)
776 return error;
777
778 return SYSCTL_OUT(req, cred, sizeof(struct ucred));
984263bc
MD
779}
780
781SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
782 0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection");
783
784static int
f3f70f0d 785udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *dstaddr,
47d61296 786 struct thread *td, int flags)
984263bc 787{
dadab5e9
MD
788 struct udpiphdr *ui;
789 int len = m->m_pkthdr.len;
7e41e800 790 struct sockaddr_in *sin; /* really is initialized before use */
9b37b73b 791 int error = 0, lport_any = 0;
984263bc 792
984263bc
MD
793 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
794 error = EMSGSIZE;
795 goto release;
796 }
797
7e41e800 798 if (inp->inp_lport == 0) { /* unbound socket */
2038fb68 799 error = in_pcbbind(inp, NULL, td);
7e41e800
JH
800 if (error)
801 goto release;
6003dd53
SZ
802
803 udbinfo_barrier_set();
cdebf5c9 804 in_pcbinswildcardhash(inp);
6003dd53 805 udbinfo_barrier_rem();
9b37b73b 806 lport_any = 1;
7e41e800
JH
807 }
808
809 if (dstaddr != NULL) { /* destination address specified */
984263bc 810 if (inp->inp_faddr.s_addr != INADDR_ANY) {
7e41e800 811 /* already connected */
984263bc
MD
812 error = EISCONN;
813 goto release;
814 }
7e41e800 815 sin = (struct sockaddr_in *)dstaddr;
3e4150ef
VBD
816 if (!prison_remote_ip(td, (struct sockaddr *)&sin)) {
817 error = EAFNOSUPPORT; /* IPv6 only jail */
818 goto release;
819 }
984263bc
MD
820 } else {
821 if (inp->inp_faddr.s_addr == INADDR_ANY) {
7e41e800 822 /* no destination specified and not already connected */
984263bc
MD
823 error = ENOTCONN;
824 goto release;
825 }
b5f1acc2 826 sin = NULL;
984263bc 827 }
7e41e800 828
984263bc
MD
829 /*
830 * Calculate data length and get a mbuf
831 * for UDP and IP headers.
832 */
74f1caca 833 M_PREPEND(m, sizeof(struct udpiphdr), MB_DONTWAIT);
407e896e 834 if (m == NULL) {
984263bc 835 error = ENOBUFS;
984263bc
MD
836 goto release;
837 }
838
839 /*
840 * Fill in mbuf with extended UDP header
841 * and addresses and length put into network format.
842 */
843 ui = mtod(m, struct udpiphdr *);
407e896e 844 bzero(ui->ui_x1, sizeof ui->ui_x1); /* XXX still needed? */
984263bc 845 ui->ui_pr = IPPROTO_UDP;
7e41e800
JH
846
847 /*
848 * Set destination address.
849 */
850 if (dstaddr != NULL) { /* use specified destination */
851 ui->ui_dst = sin->sin_addr;
852 ui->ui_dport = sin->sin_port;
853 } else { /* use connected destination */
854 ui->ui_dst = inp->inp_faddr;
855 ui->ui_dport = inp->inp_fport;
856 }
857
858 /*
4c1e6a35 859 * Set source address.
7e41e800 860 */
4c1e6a35 861 if (inp->inp_laddr.s_addr == INADDR_ANY) {
7e41e800 862 struct sockaddr_in *if_sin;
4c1e6a35 863
09f756d1
JH
864 if (dstaddr == NULL) {
865 /*
866 * connect() had (or should have) failed because
867 * the interface had no IP address, but the
868 * application proceeded to call send() anyways.
869 */
870 error = ENOTCONN;
871 goto release;
872 }
4c1e6a35
JH
873
874 /* Look up outgoing interface. */
3e4150ef 875 if ((error = in_pcbladdr(inp, dstaddr, &if_sin, td)))
7e41e800 876 goto release;
4c1e6a35
JH
877 ui->ui_src = if_sin->sin_addr; /* use address of interface */
878 } else {
879 ui->ui_src = inp->inp_laddr; /* use non-null bound address */
7e41e800 880 }
984263bc 881 ui->ui_sport = inp->inp_lport;
7e41e800
JH
882 KASSERT(inp->inp_lport != 0, ("inp lport should have been bound"));
883
984263bc
MD
884 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
885
886 /*
887 * Set up checksum and output datagram.
888 */
889 if (udpcksum) {
f23061d4 890 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
984263bc
MD
891 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
892 m->m_pkthdr.csum_flags = CSUM_UDP;
893 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
7df36335 894 m->m_pkthdr.csum_thlen = sizeof(struct udphdr);
984263bc
MD
895 } else {
896 ui->ui_sum = 0;
897 }
407e896e 898 ((struct ip *)ui)->ip_len = sizeof(struct udpiphdr) + len;
984263bc
MD
899 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */
900 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */
901 udpstat.udps_opackets++;
902
903 error = ip_output(m, inp->inp_options, &inp->inp_route,
facaabe1 904 (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)) |
47d61296 905 flags | IP_DEBUGROUTE,
984263bc
MD
906 inp->inp_moptions, inp);
907
9b37b73b
SZ
908 /*
909 * If this is the first data gram sent on an unbound and unconnected
910 * UDP socket, lport will be changed in this function. If target
911 * CPU after this lport changing is no longer the current CPU, then
912 * free the route entry allocated on the current CPU.
913 */
914 if (lport_any) {
facaabe1
SZ
915 if (udp_addrcpu(inp->inp_faddr.s_addr, inp->inp_fport,
916 inp->inp_laddr.s_addr, inp->inp_lport) != mycpuid) {
6003dd53 917#ifdef notyet
9b37b73b
SZ
918 struct route *ro = &inp->inp_route;
919
920 if (ro->ro_rt != NULL)
921 RTFREE(ro->ro_rt);
922 bzero(ro, sizeof(*ro));
6003dd53
SZ
923#else
924 panic("UDP activity should only be in netisr0");
925#endif
9b37b73b
SZ
926 }
927 }
984263bc
MD
928 return (error);
929
930release:
931 m_freem(m);
932 return (error);
933}
934
935u_long udp_sendspace = 9216; /* really max datagram size */
936 /* 40 1K datagrams */
937SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
938 &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
939
940u_long udp_recvspace = 40 * (1024 +
941#ifdef INET6
942 sizeof(struct sockaddr_in6)
943#else
944 sizeof(struct sockaddr_in)
945#endif
946 );
947SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
948 &udp_recvspace, 0, "Maximum incoming UDP datagram size");
949
6cef7136
MD
950/*
951 * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
952 * will sofree() it when we return.
953 */
002c1265
MD
954static void
955udp_abort(netmsg_t msg)
984263bc 956{
002c1265 957 struct socket *so = msg->abort.base.nm_so;
984263bc 958 struct inpcb *inp;
6cef7136 959 int error;
984263bc 960
3abced87 961 KKASSERT(&curthread->td_msgport == netisr_portfn(0));
6003dd53 962
ed894f8c 963 inp = so->so_pcb;
6cef7136
MD
964 if (inp) {
965 soisdisconnected(so);
6003dd53
SZ
966
967 udbinfo_barrier_set();
6cef7136 968 in_pcbdetach(inp);
6003dd53 969 udbinfo_barrier_rem();
6cef7136
MD
970 error = 0;
971 } else {
972 error = EINVAL;
973 }
002c1265 974 lwkt_replymsg(&msg->abort.base.lmsg, error);
984263bc
MD
975}
976
002c1265
MD
977static void
978udp_attach(netmsg_t msg)
984263bc 979{
002c1265
MD
980 struct socket *so = msg->attach.base.nm_so;
981 struct pru_attach_info *ai = msg->attach.nm_ai;
984263bc 982 struct inpcb *inp;
1cae611f 983 int error;
984263bc 984
3abced87 985 KKASSERT(&curthread->td_msgport == netisr_portfn(0));
6003dd53 986
ed894f8c 987 inp = so->so_pcb;
002c1265
MD
988 if (inp != NULL) {
989 error = EINVAL;
990 goto out;
991 }
e4700d00 992 error = soreserve(so, udp_sendspace, udp_recvspace, ai->sb_rlimit);
984263bc 993 if (error)
002c1265 994 goto out;
6003dd53
SZ
995
996 udbinfo_barrier_set();
e4700d00 997 error = in_pcballoc(so, &udbinfo);
6003dd53
SZ
998 udbinfo_barrier_rem();
999
984263bc 1000 if (error)
002c1265 1001 goto out;
0ce0603e
MD
1002
1003 /*
1004 * Set default port for protocol processing prior to bind/connect.
1005 */
3abced87 1006 sosetport(so, netisr_portfn(0));
984263bc
MD
1007
1008 inp = (struct inpcb *)so->so_pcb;
1009 inp->inp_vflag |= INP_IPV4;
1010 inp->inp_ip_ttl = ip_defttl;
002c1265
MD
1011 error = 0;
1012out:
1013 lwkt_replymsg(&msg->attach.base.lmsg, error);
984263bc
MD
1014}
1015
002c1265
MD
1016static void
1017udp_bind(netmsg_t msg)
984263bc 1018{
002c1265
MD
1019 struct socket *so = msg->bind.base.nm_so;
1020 struct sockaddr *nam = msg->bind.nm_nam;
1021 struct thread *td = msg->bind.nm_td;
2322f6ca 1022 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
984263bc 1023 struct inpcb *inp;
1cae611f 1024 int error;
984263bc 1025
ed894f8c 1026 inp = so->so_pcb;
002c1265
MD
1027 if (inp) {
1028 error = in_pcbbind(inp, nam, td);
1029 if (error == 0) {
1030 if (sin->sin_addr.s_addr != INADDR_ANY)
1031 inp->inp_flags |= INP_WASBOUND_NOTANY;
6003dd53
SZ
1032
1033 udbinfo_barrier_set();
002c1265 1034 in_pcbinswildcardhash(inp);
6003dd53 1035 udbinfo_barrier_rem();
002c1265
MD
1036 }
1037 } else {
1038 error = EINVAL;
f772087c 1039 }
002c1265 1040 lwkt_replymsg(&msg->bind.base.lmsg, error);
984263bc
MD
1041}
1042
48e7b118 1043static void
002c1265 1044udp_connect(netmsg_t msg)
984263bc 1045{
002c1265
MD
1046 struct socket *so = msg->connect.base.nm_so;
1047 struct sockaddr *nam = msg->connect.nm_nam;
1048 struct thread *td = msg->connect.nm_td;
984263bc 1049 struct inpcb *inp;
48e7b118
MD
1050 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1051 struct sockaddr_in *if_sin;
1052 lwkt_port_t port;
1cae611f 1053 int error;
984263bc 1054
3abced87 1055 KKASSERT(&curthread->td_msgport == netisr_portfn(0));
6003dd53 1056
ed894f8c 1057 inp = so->so_pcb;
002c1265
MD
1058 if (inp == NULL) {
1059 error = EINVAL;
1060 goto out;
1061 }
1062
1063 if (msg->connect.nm_reconnect & NMSG_RECONNECT_RECONNECT) {
ed20d0e3 1064 panic("UDP does not support RECONNECT");
6003dd53 1065#ifdef notyet
002c1265
MD
1066 msg->connect.nm_reconnect &= ~NMSG_RECONNECT_RECONNECT;
1067 in_pcblink(inp, &udbinfo);
6003dd53 1068#endif
002c1265
MD
1069 }
1070
1071 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1072 error = EISCONN;
1073 goto out;
1074 }
984263bc 1075 error = 0;
48e7b118
MD
1076
1077 /*
1078 * Bind if we have to
1079 */
2322f6ca
JH
1080 if (td->td_proc && td->td_proc->p_ucred->cr_prison != NULL &&
1081 inp->inp_laddr.s_addr == INADDR_ANY) {
dadab5e9 1082 error = in_pcbbind(inp, NULL, td);
48e7b118 1083 if (error)
002c1265 1084 goto out;
dadab5e9 1085 }
48e7b118
MD
1086
1087 /*
1088 * Calculate the correct protocol processing thread. The connect
1089 * operation must run there.
1090 */
1091 error = in_pcbladdr(inp, nam, &if_sin, td);
1092 if (error)
002c1265
MD
1093 goto out;
1094 if (!prison_remote_ip(td, nam)) {
1095 error = EAFNOSUPPORT; /* IPv6 only jail */
1096 goto out;
1097 }
48e7b118
MD
1098
1099 port = udp_addrport(sin->sin_addr.s_addr, sin->sin_port,
1100 inp->inp_laddr.s_addr, inp->inp_lport);
48e7b118 1101 if (port != &curthread->td_msgport) {
6003dd53 1102#ifdef notyet
48e7b118 1103 struct route *ro = &inp->inp_route;
9b37b73b
SZ
1104
1105 /*
48e7b118
MD
1106 * in_pcbladdr() may have allocated a route entry for us
1107 * on the current CPU, but we need a route entry on the
1108 * inpcb's owner CPU, so free it here.
9b37b73b 1109 */
48e7b118
MD
1110 if (ro->ro_rt != NULL)
1111 RTFREE(ro->ro_rt);
1112 bzero(ro, sizeof(*ro));
9b37b73b 1113
48e7b118 1114 /*
0ce85c47
MD
1115 * We are moving the protocol processing port the socket
1116 * is on, we have to unlink here and re-link on the
1117 * target cpu.
1118 */
1119 in_pcbunlink(so->so_pcb, &udbinfo);
1120 /* in_pcbunlink(so->so_pcb, &udbinfo[mycpu->gd_cpuid]); */
1121 sosetport(so, port);
002c1265
MD
1122 msg->connect.nm_reconnect |= NMSG_RECONNECT_RECONNECT;
1123 msg->connect.base.nm_dispatch = udp_connect;
0ce85c47 1124
002c1265
MD
1125 lwkt_forwardmsg(port, &msg->connect.base.lmsg);
1126 /* msg invalid now */
1127 return;
6003dd53
SZ
1128#else
1129 panic("UDP activity should only be in netisr0");
1130#endif
48e7b118 1131 }
0ce0603e 1132 KKASSERT(port == &curthread->td_msgport);
441fe96a 1133 error = udp_connect_oncpu(so, td, sin, if_sin);
002c1265
MD
1134out:
1135 KKASSERT(msg->connect.nm_m == NULL);
1136 lwkt_replymsg(&msg->connect.base.lmsg, error);
48e7b118
MD
1137}
1138
1139static int
1140udp_connect_oncpu(struct socket *so, struct thread *td,
1141 struct sockaddr_in *sin, struct sockaddr_in *if_sin)
1142{
1143 struct inpcb *inp;
1144 int error;
1145
6003dd53
SZ
1146 udbinfo_barrier_set();
1147
48e7b118
MD
1148 inp = so->so_pcb;
1149 if (inp->inp_flags & INP_WILDCARD)
1150 in_pcbremwildcardhash(inp);
1151 error = in_pcbconnect(inp, (struct sockaddr *)sin, td);
1152
1153 if (error == 0) {
1154 /*
1155 * No more errors can occur, finish adjusting the socket
1156 * and change the processing port to reflect the connected
1157 * socket. Once set we can no longer safely mess with the
1158 * socket.
1159 */
1160 soisconnected(so);
9b37b73b 1161 } else if (error == EAFNOSUPPORT) { /* connection dissolved */
2322f6ca
JH
1162 /*
1163 * Follow traditional BSD behavior and retain
1164 * the local port binding. But, fix the old misbehavior
1165 * of overwriting any previously bound local address.
1166 */
1167 if (!(inp->inp_flags & INP_WASBOUND_NOTANY))
1168 inp->inp_laddr.s_addr = INADDR_ANY;
1169 in_pcbinswildcardhash(inp);
1170 }
6003dd53
SZ
1171
1172 udbinfo_barrier_rem();
984263bc
MD
1173 return error;
1174}
1175
002c1265
MD
1176static void
1177udp_detach(netmsg_t msg)
984263bc 1178{
002c1265 1179 struct socket *so = msg->detach.base.nm_so;
984263bc 1180 struct inpcb *inp;
002c1265 1181 int error;
984263bc 1182
3abced87 1183 KKASSERT(&curthread->td_msgport == netisr_portfn(0));
6003dd53 1184
ed894f8c 1185 inp = so->so_pcb;
002c1265 1186 if (inp) {
6003dd53 1187 udbinfo_barrier_set();
002c1265 1188 in_pcbdetach(inp);
6003dd53 1189 udbinfo_barrier_rem();
002c1265
MD
1190 error = 0;
1191 } else {
1192 error = EINVAL;
1193 }
1194 lwkt_replymsg(&msg->detach.base.lmsg, error);
984263bc
MD
1195}
1196
002c1265
MD
1197static void
1198udp_disconnect(netmsg_t msg)
984263bc 1199{
002c1265 1200 struct socket *so = msg->disconnect.base.nm_so;
6ec45209 1201 struct route *ro;
984263bc 1202 struct inpcb *inp;
002c1265 1203 int error;
984263bc 1204
3abced87 1205 KKASSERT(&curthread->td_msgport == netisr_portfn(0));
6003dd53 1206
ed894f8c 1207 inp = so->so_pcb;
002c1265
MD
1208 if (inp == NULL) {
1209 error = EINVAL;
1210 goto out;
1211 }
1212 if (inp->inp_faddr.s_addr == INADDR_ANY) {
1213 error = ENOTCONN;
1214 goto out;
1215 }
984263bc 1216
6cef7136 1217 soreference(so);
6003dd53
SZ
1218
1219 udbinfo_barrier_set();
984263bc 1220 in_pcbdisconnect(inp);
6003dd53
SZ
1221 udbinfo_barrier_rem();
1222
6cef7136
MD
1223 soclrstate(so, SS_ISCONNECTED); /* XXX */
1224 sofree(so);
9b37b73b 1225
6ec45209
SZ
1226 ro = &inp->inp_route;
1227 if (ro->ro_rt != NULL)
1228 RTFREE(ro->ro_rt);
1229 bzero(ro, sizeof(*ro));
002c1265
MD
1230 error = 0;
1231out:
1232 lwkt_replymsg(&msg->disconnect.base.lmsg, error);
984263bc
MD
1233}
1234
002c1265
MD
1235static void
1236udp_send(netmsg_t msg)
984263bc 1237{
002c1265 1238 struct socket *so = msg->send.base.nm_so;
32e297c1 1239 struct mbuf *m = msg->send.nm_m;
7b7dc575 1240 struct sockaddr *addr = msg->send.nm_addr;
47d61296 1241 int pru_flags = msg->send.nm_flags;
984263bc 1242 struct inpcb *inp;
002c1265 1243 int error;
984263bc 1244
3abced87 1245 KKASSERT(&curthread->td_msgport == netisr_portfn(0));
0f267b72 1246 KKASSERT(msg->send.nm_control == NULL);
6003dd53 1247
ed894f8c 1248 inp = so->so_pcb;
002c1265 1249 if (inp) {
32e297c1 1250 struct thread *td = msg->send.nm_td;
47d61296 1251 int flags = 0;
32e297c1 1252
47d61296
SZ
1253 if (pru_flags & PRUS_DONTROUTE)
1254 flags |= SO_DONTROUTE;
1255 error = udp_output(inp, m, addr, td, flags);
002c1265 1256 } else {
32e297c1 1257 m_freem(m);
002c1265 1258 error = EINVAL;
984263bc 1259 }
6ce03112 1260
7b7dc575
SZ
1261 if (pru_flags & PRUS_FREEADDR)
1262 kfree(addr, M_SONAME);
1263
6ce03112
SZ
1264 if ((pru_flags & PRUS_NOREPLY) == 0)
1265 lwkt_replymsg(&msg->send.base.lmsg, error);
984263bc
MD
1266}
1267
002c1265
MD
1268void
1269udp_shutdown(netmsg_t msg)
984263bc 1270{
002c1265 1271 struct socket *so = msg->shutdown.base.nm_so;
984263bc 1272 struct inpcb *inp;
002c1265 1273 int error;
984263bc 1274
3abced87 1275 KKASSERT(&curthread->td_msgport == netisr_portfn(0));
6003dd53 1276
ed894f8c 1277 inp = so->so_pcb;
002c1265
MD
1278 if (inp) {
1279 socantsendmore(so);
1280 error = 0;
1281 } else {
1282 error = EINVAL;
1283 }
1284 lwkt_replymsg(&msg->shutdown.base.lmsg, error);
984263bc
MD
1285}
1286
5a38e392
SZ
1287void
1288udbinfo_lock(void)
1289{
1290 lwkt_serialize_enter(&udbinfo_slize);
1291}
1292
1293void
1294udbinfo_unlock(void)
1295{
1296 lwkt_serialize_exit(&udbinfo_slize);
1297}
1298
aad1f734 1299void
6003dd53
SZ
1300udbinfo_barrier_set(void)
1301{
1302 netisr_barrier_set(udbinfo_br);
5a38e392 1303 udbinfo_lock();
6003dd53
SZ
1304}
1305
aad1f734 1306void
6003dd53
SZ
1307udbinfo_barrier_rem(void)
1308{
5a38e392 1309 udbinfo_unlock();
6003dd53
SZ
1310 netisr_barrier_rem(udbinfo_br);
1311}
1312
984263bc 1313struct pr_usrreqs udp_usrreqs = {
fa5e758c 1314 .pru_abort = udp_abort,
002c1265 1315 .pru_accept = pr_generic_notsupp,
fa5e758c
MD
1316 .pru_attach = udp_attach,
1317 .pru_bind = udp_bind,
1318 .pru_connect = udp_connect,
002c1265
MD
1319 .pru_connect2 = pr_generic_notsupp,
1320 .pru_control = in_control_dispatch,
fa5e758c
MD
1321 .pru_detach = udp_detach,
1322 .pru_disconnect = udp_disconnect,
002c1265
MD
1323 .pru_listen = pr_generic_notsupp,
1324 .pru_peeraddr = in_setpeeraddr_dispatch,
1325 .pru_rcvd = pr_generic_notsupp,
1326 .pru_rcvoob = pr_generic_notsupp,
fa5e758c
MD
1327 .pru_send = udp_send,
1328 .pru_sense = pru_sense_null,
1329 .pru_shutdown = udp_shutdown,
002c1265 1330 .pru_sockaddr = in_setsockaddr_dispatch,
fa5e758c 1331 .pru_sosend = sosendudp,
8b5c39bb 1332 .pru_soreceive = soreceive
984263bc
MD
1333};
1334