Deleted Added
full compact
udp_usrreq.c (55009) udp_usrreq.c (58698)
1/*
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3 * The Regents of the University of California. 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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
1/*
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3 * The Regents of the University of California. 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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
34 * $FreeBSD: head/sys/netinet/udp_usrreq.c 55009 1999-12-22 19:13:38Z shin $
34 * $FreeBSD: head/sys/netinet/udp_usrreq.c 58698 2000-03-27 19:14:27Z jlemon $
35 */
36
37#include "opt_ipsec.h"
38#include "opt_inet6.h"
39
35 */
36
37#include "opt_ipsec.h"
38#include "opt_inet6.h"
39
40#include <stddef.h>
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/kernel.h>
43#include <sys/malloc.h>
44#include <sys/mbuf.h>
45#include <sys/domain.h>
46#include <sys/proc.h>
47#include <sys/protosw.h>
48#include <sys/socket.h>
49#include <sys/socketvar.h>
50#include <sys/sysctl.h>
51#include <sys/syslog.h>
52
53#include <vm/vm_zone.h>
54
55#include <net/if.h>
56#include <net/route.h>
57
58#include <netinet/in.h>
59#include <netinet/in_systm.h>
60#include <netinet/ip.h>
61#ifdef INET6
62#include <netinet/ip6.h>
63#endif
64#include <netinet/in_pcb.h>
65#include <netinet/in_var.h>
66#include <netinet/ip_var.h>
67#ifdef INET6
68#include <netinet6/ip6_var.h>
69#endif
70#include <netinet/ip_icmp.h>
71#include <netinet/icmp_var.h>
72#include <netinet/udp.h>
73#include <netinet/udp_var.h>
74
75#ifdef IPSEC
76#include <netinet6/ipsec.h>
77#endif /*IPSEC*/
78
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/kernel.h>
44#include <sys/malloc.h>
45#include <sys/mbuf.h>
46#include <sys/domain.h>
47#include <sys/proc.h>
48#include <sys/protosw.h>
49#include <sys/socket.h>
50#include <sys/socketvar.h>
51#include <sys/sysctl.h>
52#include <sys/syslog.h>
53
54#include <vm/vm_zone.h>
55
56#include <net/if.h>
57#include <net/route.h>
58
59#include <netinet/in.h>
60#include <netinet/in_systm.h>
61#include <netinet/ip.h>
62#ifdef INET6
63#include <netinet/ip6.h>
64#endif
65#include <netinet/in_pcb.h>
66#include <netinet/in_var.h>
67#include <netinet/ip_var.h>
68#ifdef INET6
69#include <netinet6/ip6_var.h>
70#endif
71#include <netinet/ip_icmp.h>
72#include <netinet/icmp_var.h>
73#include <netinet/udp.h>
74#include <netinet/udp_var.h>
75
76#ifdef IPSEC
77#include <netinet6/ipsec.h>
78#endif /*IPSEC*/
79
80#include <machine/in_cksum.h>
81
79/*
80 * UDP protocol implementation.
81 * Per RFC 768, August, 1980.
82 */
83#ifndef COMPAT_42
84static int udpcksum = 1;
85#else
86static int udpcksum = 0; /* XXX */
87#endif
88SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
89 &udpcksum, 0, "");
90
91int log_in_vain = 0;
92SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
93 &log_in_vain, 0, "Log all incoming UDP packets");
94
95static int blackhole = 0;
96SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
97 &blackhole, 0, "Do not send port unreachables for refused connects");
98
99struct inpcbhead udb; /* from udp_var.h */
100#define udb6 udb /* for KAME src sync over BSD*'s */
101struct inpcbinfo udbinfo;
102
103#ifndef UDBHASHSIZE
104#define UDBHASHSIZE 16
105#endif
106
107struct udpstat udpstat; /* from udp_var.h */
108SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RD,
109 &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
110
111static struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET };
112#ifdef INET6
113struct udp_in6 {
114 struct sockaddr_in6 uin6_sin;
115 u_char uin6_init_done : 1;
116} udp_in6 = {
117 { sizeof(udp_in6.uin6_sin), AF_INET6 },
118 0
119};
120struct udp_ip6 {
121 struct ip6_hdr uip6_ip6;
122 u_char uip6_init_done : 1;
123} udp_ip6;
124#endif /* INET6 */
125
126static void udp_append __P((struct inpcb *last, struct ip *ip,
127 struct mbuf *n, int off));
128#ifdef INET6
129static void ip_2_ip6_hdr __P((struct ip6_hdr *ip6, struct ip *ip));
130#endif
131
132static int udp_detach __P((struct socket *so));
133static int udp_output __P((struct inpcb *, struct mbuf *, struct sockaddr *,
134 struct mbuf *, struct proc *));
135
136void
137udp_init()
138{
139 LIST_INIT(&udb);
140 udbinfo.listhead = &udb;
141 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
142 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
143 &udbinfo.porthashmask);
144 udbinfo.ipi_zone = zinit("udpcb", sizeof(struct inpcb), maxsockets,
145 ZONE_INTERRUPT, 0);
146}
147
148void
149udp_input(m, off, proto)
150 register struct mbuf *m;
151 int off, proto;
152{
153 int iphlen = off;
154 register struct ip *ip;
155 register struct udphdr *uh;
156 register struct inpcb *inp;
157 struct mbuf *opts = 0;
158 int len;
159 struct ip save_ip;
160 struct sockaddr *append_sa;
161
162 udpstat.udps_ipackets++;
163
164 /*
165 * Strip IP options, if any; should skip this,
166 * make available to user, and use on returned packets,
167 * but we don't yet have a way to check the checksum
168 * with options still present.
169 */
170 if (iphlen > sizeof (struct ip)) {
171 ip_stripoptions(m, (struct mbuf *)0);
172 iphlen = sizeof(struct ip);
173 }
174
175 /*
176 * Get IP and UDP header together in first mbuf.
177 */
178 ip = mtod(m, struct ip *);
179 if (m->m_len < iphlen + sizeof(struct udphdr)) {
180 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
181 udpstat.udps_hdrops++;
182 return;
183 }
184 ip = mtod(m, struct ip *);
185 }
186 uh = (struct udphdr *)((caddr_t)ip + iphlen);
187
188 /*
189 * Make mbuf data length reflect UDP length.
190 * If not enough data to reflect UDP length, drop.
191 */
192 len = ntohs((u_short)uh->uh_ulen);
193 if (ip->ip_len != len) {
194 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
195 udpstat.udps_badlen++;
196 goto bad;
197 }
198 m_adj(m, len - ip->ip_len);
199 /* ip->ip_len = len; */
200 }
201 /*
202 * Save a copy of the IP header in case we want restore it
203 * for sending an ICMP error message in response.
204 */
205 save_ip = *ip;
206
207 /*
208 * Checksum extended UDP header and data.
209 */
210 if (uh->uh_sum) {
82/*
83 * UDP protocol implementation.
84 * Per RFC 768, August, 1980.
85 */
86#ifndef COMPAT_42
87static int udpcksum = 1;
88#else
89static int udpcksum = 0; /* XXX */
90#endif
91SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
92 &udpcksum, 0, "");
93
94int log_in_vain = 0;
95SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
96 &log_in_vain, 0, "Log all incoming UDP packets");
97
98static int blackhole = 0;
99SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
100 &blackhole, 0, "Do not send port unreachables for refused connects");
101
102struct inpcbhead udb; /* from udp_var.h */
103#define udb6 udb /* for KAME src sync over BSD*'s */
104struct inpcbinfo udbinfo;
105
106#ifndef UDBHASHSIZE
107#define UDBHASHSIZE 16
108#endif
109
110struct udpstat udpstat; /* from udp_var.h */
111SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RD,
112 &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
113
114static struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET };
115#ifdef INET6
116struct udp_in6 {
117 struct sockaddr_in6 uin6_sin;
118 u_char uin6_init_done : 1;
119} udp_in6 = {
120 { sizeof(udp_in6.uin6_sin), AF_INET6 },
121 0
122};
123struct udp_ip6 {
124 struct ip6_hdr uip6_ip6;
125 u_char uip6_init_done : 1;
126} udp_ip6;
127#endif /* INET6 */
128
129static void udp_append __P((struct inpcb *last, struct ip *ip,
130 struct mbuf *n, int off));
131#ifdef INET6
132static void ip_2_ip6_hdr __P((struct ip6_hdr *ip6, struct ip *ip));
133#endif
134
135static int udp_detach __P((struct socket *so));
136static int udp_output __P((struct inpcb *, struct mbuf *, struct sockaddr *,
137 struct mbuf *, struct proc *));
138
139void
140udp_init()
141{
142 LIST_INIT(&udb);
143 udbinfo.listhead = &udb;
144 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
145 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
146 &udbinfo.porthashmask);
147 udbinfo.ipi_zone = zinit("udpcb", sizeof(struct inpcb), maxsockets,
148 ZONE_INTERRUPT, 0);
149}
150
151void
152udp_input(m, off, proto)
153 register struct mbuf *m;
154 int off, proto;
155{
156 int iphlen = off;
157 register struct ip *ip;
158 register struct udphdr *uh;
159 register struct inpcb *inp;
160 struct mbuf *opts = 0;
161 int len;
162 struct ip save_ip;
163 struct sockaddr *append_sa;
164
165 udpstat.udps_ipackets++;
166
167 /*
168 * Strip IP options, if any; should skip this,
169 * make available to user, and use on returned packets,
170 * but we don't yet have a way to check the checksum
171 * with options still present.
172 */
173 if (iphlen > sizeof (struct ip)) {
174 ip_stripoptions(m, (struct mbuf *)0);
175 iphlen = sizeof(struct ip);
176 }
177
178 /*
179 * Get IP and UDP header together in first mbuf.
180 */
181 ip = mtod(m, struct ip *);
182 if (m->m_len < iphlen + sizeof(struct udphdr)) {
183 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
184 udpstat.udps_hdrops++;
185 return;
186 }
187 ip = mtod(m, struct ip *);
188 }
189 uh = (struct udphdr *)((caddr_t)ip + iphlen);
190
191 /*
192 * Make mbuf data length reflect UDP length.
193 * If not enough data to reflect UDP length, drop.
194 */
195 len = ntohs((u_short)uh->uh_ulen);
196 if (ip->ip_len != len) {
197 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
198 udpstat.udps_badlen++;
199 goto bad;
200 }
201 m_adj(m, len - ip->ip_len);
202 /* ip->ip_len = len; */
203 }
204 /*
205 * Save a copy of the IP header in case we want restore it
206 * for sending an ICMP error message in response.
207 */
208 save_ip = *ip;
209
210 /*
211 * Checksum extended UDP header and data.
212 */
213 if (uh->uh_sum) {
211 bzero(((struct ipovly *)ip)->ih_x1, 9);
212 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
213 uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
214 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
215 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
216 uh->uh_sum = m->m_pkthdr.csum_data;
217 else
218 uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
219 ip->ip_dst.s_addr, htonl(ip->ip_len +
220 m->m_pkthdr.csum_data + IPPROTO_UDP));
221 uh->uh_sum ^= 0xffff;
222 } else {
223 bzero(((struct ipovly *)ip)->ih_x1, 9);
224 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
225 uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
226 }
214 if (uh->uh_sum) {
215 udpstat.udps_badsum++;
216 m_freem(m);
217 return;
218 }
219 }
220
221 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
222 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
223 struct inpcb *last;
224 /*
225 * Deliver a multicast or broadcast datagram to *all* sockets
226 * for which the local and remote addresses and ports match
227 * those of the incoming datagram. This allows more than
228 * one process to receive multi/broadcasts on the same port.
229 * (This really ought to be done for unicast datagrams as
230 * well, but that would cause problems with existing
231 * applications that open both address-specific sockets and
232 * a wildcard socket listening to the same port -- they would
233 * end up receiving duplicates of every unicast datagram.
234 * Those applications open the multiple sockets to overcome an
235 * inadequacy of the UDP socket interface, but for backwards
236 * compatibility we avoid the problem here rather than
237 * fixing the interface. Maybe 4.5BSD will remedy this?)
238 */
239
240 /*
241 * Construct sockaddr format source address.
242 */
243 udp_in.sin_port = uh->uh_sport;
244 udp_in.sin_addr = ip->ip_src;
245 /*
246 * Locate pcb(s) for datagram.
247 * (Algorithm copied from raw_intr().)
248 */
249 last = NULL;
250#ifdef INET6
251 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
252#endif
253 LIST_FOREACH(inp, &udb, inp_list) {
254#ifdef INET6
255 if ((inp->inp_vflag & INP_IPV4) == 0)
256 continue;
257#endif
258 if (inp->inp_lport != uh->uh_dport)
259 continue;
260 if (inp->inp_laddr.s_addr != INADDR_ANY) {
261 if (inp->inp_laddr.s_addr !=
262 ip->ip_dst.s_addr)
263 continue;
264 }
265 if (inp->inp_faddr.s_addr != INADDR_ANY) {
266 if (inp->inp_faddr.s_addr !=
267 ip->ip_src.s_addr ||
268 inp->inp_fport != uh->uh_sport)
269 continue;
270 }
271
272 if (last != NULL) {
273 struct mbuf *n;
274
275#ifdef IPSEC
276 /* check AH/ESP integrity. */
277 if (ipsec4_in_reject_so(m, last->inp_socket))
278 ipsecstat.in_polvio++;
279 /* do not inject data to pcb */
280 else
281#endif /*IPSEC*/
282 if ((n = m_copy(m, 0, M_COPYALL)) != NULL)
283 udp_append(last, ip, n,
284 iphlen +
285 sizeof(struct udphdr));
286 }
287 last = inp;
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->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
297 break;
298 }
299
300 if (last == NULL) {
301 /*
302 * No matching pcb found; discard datagram.
303 * (No need to send an ICMP Port Unreachable
304 * for a broadcast or multicast datgram.)
305 */
306 udpstat.udps_noportbcast++;
307 goto bad;
308 }
309#ifdef IPSEC
310 /* check AH/ESP integrity. */
311 if (ipsec4_in_reject_so(m, last->inp_socket)) {
312 ipsecstat.in_polvio++;
313 goto bad;
314 }
315#endif /*IPSEC*/
316 udp_append(last, ip, m, iphlen + sizeof(struct udphdr));
317 return;
318 }
319 /*
320 * Locate pcb for datagram.
321 */
322 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
323 ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
324 if (inp == NULL) {
325 if (log_in_vain) {
326 char buf[4*sizeof "123"];
327
328 strcpy(buf, inet_ntoa(ip->ip_dst));
329 log(LOG_INFO,
330 "Connection attempt to UDP %s:%d from %s:%d\n",
331 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
332 ntohs(uh->uh_sport));
333 }
334 udpstat.udps_noport++;
335 if (m->m_flags & (M_BCAST | M_MCAST)) {
336 udpstat.udps_noportbcast++;
337 goto bad;
338 }
339 *ip = save_ip;
340#ifdef ICMP_BANDLIM
341 if (badport_bandlim(0) < 0)
342 goto bad;
343#endif
344 if (!blackhole)
345 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
346 else
347 goto bad;
348 return;
349 }
350#ifdef IPSEC
351 if (ipsec4_in_reject_so(m, inp->inp_socket)) {
352 ipsecstat.in_polvio++;
353 goto bad;
354 }
355#endif /*IPSEC*/
356
357 /*
358 * Construct sockaddr format source address.
359 * Stuff source address and datagram in user buffer.
360 */
361 udp_in.sin_port = uh->uh_sport;
362 udp_in.sin_addr = ip->ip_src;
363 if (inp->inp_flags & INP_CONTROLOPTS
364 || inp->inp_socket->so_options & SO_TIMESTAMP) {
365#ifdef INET6
366 if (inp->inp_vflag & INP_IPV6) {
367 int savedflags;
368
369 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
370 savedflags = inp->inp_flags;
371 inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
372 ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m);
373 inp->inp_flags = savedflags;
374 } else
375#endif
376 ip_savecontrol(inp, &opts, ip, m);
377 }
378 iphlen += sizeof(struct udphdr);
379 m_adj(m, iphlen);
380#ifdef INET6
381 if (inp->inp_vflag & INP_IPV6) {
382 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
383 append_sa = (struct sockaddr *)&udp_in6;
384 } else
385#endif
386 append_sa = (struct sockaddr *)&udp_in;
387 if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) {
388 udpstat.udps_fullsock++;
389 goto bad;
390 }
391 sorwakeup(inp->inp_socket);
392 return;
393bad:
394 m_freem(m);
395 if (opts)
396 m_freem(opts);
397 return;
398}
399
400#if defined(INET6)
401static void
402ip_2_ip6_hdr(ip6, ip)
403 struct ip6_hdr *ip6;
404 struct ip *ip;
405{
406 bzero(ip6, sizeof(*ip6));
407
408 ip6->ip6_vfc = IPV6_VERSION;
409 ip6->ip6_plen = ip->ip_len;
410 ip6->ip6_nxt = ip->ip_p;
411 ip6->ip6_hlim = ip->ip_ttl;
412 ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
413 IPV6_ADDR_INT32_SMP;
414 ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
415 ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
416}
417#endif
418
419/*
420 * subroutine of udp_input(), mainly for source code readability.
421 * caller must properly init udp_ip6 and udp_in6 beforehand.
422 */
423static void
424udp_append(last, ip, n, off)
425 struct inpcb *last;
426 struct ip *ip;
427 struct mbuf *n;
428 int off;
429{
430 struct sockaddr *append_sa;
431 struct mbuf *opts = 0;
432
433 if (last->inp_flags & INP_CONTROLOPTS ||
434 last->inp_socket->so_options & SO_TIMESTAMP) {
435#ifdef INET6
436 if (last->inp_vflag & INP_IPV6) {
437 int savedflags;
438
439 if (udp_ip6.uip6_init_done == 0) {
440 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
441 udp_ip6.uip6_init_done = 1;
442 }
443 savedflags = last->inp_flags;
444 last->inp_flags &= ~INP_UNMAPPABLEOPTS;
445 ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n);
446 last->inp_flags = savedflags;
447 } else
448#endif
449 ip_savecontrol(last, &opts, ip, n);
450 }
451#ifdef INET6
452 if (last->inp_vflag & INP_IPV6) {
453 if (udp_in6.uin6_init_done == 0) {
454 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
455 udp_in6.uin6_init_done = 1;
456 }
457 append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
458 } else
459#endif
460 append_sa = (struct sockaddr *)&udp_in;
461 m_adj(n, off);
462 if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
463 m_freem(n);
464 if (opts)
465 m_freem(opts);
466 udpstat.udps_fullsock++;
467 } else
468 sorwakeup(last->inp_socket);
469}
470
471/*
472 * Notify a udp user of an asynchronous error;
473 * just wake up so that he can collect error status.
474 */
475void
476udp_notify(inp, errno)
477 register struct inpcb *inp;
478 int errno;
479{
480 inp->inp_socket->so_error = errno;
481 sorwakeup(inp->inp_socket);
482 sowwakeup(inp->inp_socket);
483}
484
485void
486udp_ctlinput(cmd, sa, vip)
487 int cmd;
488 struct sockaddr *sa;
489 void *vip;
490{
491 register struct ip *ip = vip;
492 register struct udphdr *uh;
493
494 if (!PRC_IS_REDIRECT(cmd) &&
495 ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0))
496 return;
497 if (ip) {
498 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
499 in_pcbnotify(&udb, sa, uh->uh_dport, ip->ip_src, uh->uh_sport,
500 cmd, udp_notify);
501 } else
502 in_pcbnotify(&udb, sa, 0, zeroin_addr, 0, cmd, udp_notify);
503}
504
505static int
506udp_pcblist SYSCTL_HANDLER_ARGS
507{
508 int error, i, n, s;
509 struct inpcb *inp, **inp_list;
510 inp_gen_t gencnt;
511 struct xinpgen xig;
512
513 /*
514 * The process of preparing the TCB list is too time-consuming and
515 * resource-intensive to repeat twice on every request.
516 */
517 if (req->oldptr == 0) {
518 n = udbinfo.ipi_count;
519 req->oldidx = 2 * (sizeof xig)
520 + (n + n/8) * sizeof(struct xinpcb);
521 return 0;
522 }
523
524 if (req->newptr != 0)
525 return EPERM;
526
527 /*
528 * OK, now we're committed to doing something.
529 */
530 s = splnet();
531 gencnt = udbinfo.ipi_gencnt;
532 n = udbinfo.ipi_count;
533 splx(s);
534
535 xig.xig_len = sizeof xig;
536 xig.xig_count = n;
537 xig.xig_gen = gencnt;
538 xig.xig_sogen = so_gencnt;
539 error = SYSCTL_OUT(req, &xig, sizeof xig);
540 if (error)
541 return error;
542
543 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
544 if (inp_list == 0)
545 return ENOMEM;
546
547 s = splnet();
548 for (inp = udbinfo.listhead->lh_first, i = 0; inp && i < n;
549 inp = inp->inp_list.le_next) {
550 if (inp->inp_gencnt <= gencnt && !prison_xinpcb(req->p, inp))
551 inp_list[i++] = inp;
552 }
553 splx(s);
554 n = i;
555
556 error = 0;
557 for (i = 0; i < n; i++) {
558 inp = inp_list[i];
559 if (inp->inp_gencnt <= gencnt) {
560 struct xinpcb xi;
561 xi.xi_len = sizeof xi;
562 /* XXX should avoid extra copy */
563 bcopy(inp, &xi.xi_inp, sizeof *inp);
564 if (inp->inp_socket)
565 sotoxsocket(inp->inp_socket, &xi.xi_socket);
566 error = SYSCTL_OUT(req, &xi, sizeof xi);
567 }
568 }
569 if (!error) {
570 /*
571 * Give the user an updated idea of our state.
572 * If the generation differs from what we told
573 * her before, she knows that something happened
574 * while we were processing this request, and it
575 * might be necessary to retry.
576 */
577 s = splnet();
578 xig.xig_gen = udbinfo.ipi_gencnt;
579 xig.xig_sogen = so_gencnt;
580 xig.xig_count = udbinfo.ipi_count;
581 splx(s);
582 error = SYSCTL_OUT(req, &xig, sizeof xig);
583 }
584 free(inp_list, M_TEMP);
585 return error;
586}
587
588SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
589 udp_pcblist, "S,xinpcb", "List of active UDP sockets");
590
591static int
592udp_getcred SYSCTL_HANDLER_ARGS
593{
594 struct sockaddr_in addrs[2];
595 struct inpcb *inp;
596 int error, s;
597
598 error = suser(req->p);
599 if (error)
600 return (error);
601 error = SYSCTL_IN(req, addrs, sizeof(addrs));
602 if (error)
603 return (error);
604 s = splnet();
605 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
606 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
607 if (inp == NULL || inp->inp_socket == NULL) {
608 error = ENOENT;
609 goto out;
610 }
611 error = SYSCTL_OUT(req, inp->inp_socket->so_cred, sizeof(struct ucred));
612out:
613 splx(s);
614 return (error);
615}
616
617SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
618 0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection");
619
620static int
621udp_output(inp, m, addr, control, p)
622 register struct inpcb *inp;
623 struct mbuf *m;
624 struct sockaddr *addr;
625 struct mbuf *control;
626 struct proc *p;
627{
628 register struct udpiphdr *ui;
629 register int len = m->m_pkthdr.len;
630 struct in_addr laddr;
631 struct sockaddr_in *sin;
632 int s = 0, error = 0;
633
634 if (control)
635 m_freem(control); /* XXX */
636
637 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
638 error = EMSGSIZE;
639 goto release;
640 }
641
642 if (addr) {
643 sin = (struct sockaddr_in *)addr;
644 prison_remote_ip(p, 0, &sin->sin_addr.s_addr);
645 laddr = inp->inp_laddr;
646 if (inp->inp_faddr.s_addr != INADDR_ANY) {
647 error = EISCONN;
648 goto release;
649 }
650 /*
651 * Must block input while temporarily connected.
652 */
653 s = splnet();
654 error = in_pcbconnect(inp, addr, p);
655 if (error) {
656 splx(s);
657 goto release;
658 }
659 } else {
660 if (inp->inp_faddr.s_addr == INADDR_ANY) {
661 error = ENOTCONN;
662 goto release;
663 }
664 }
665 /*
666 * Calculate data length and get a mbuf
667 * for UDP and IP headers.
668 */
669 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
670 if (m == 0) {
671 error = ENOBUFS;
672 if (addr)
673 splx(s);
674 goto release;
675 }
676
677 /*
678 * Fill in mbuf with extended UDP header
679 * and addresses and length put into network format.
680 */
681 ui = mtod(m, struct udpiphdr *);
227 if (uh->uh_sum) {
228 udpstat.udps_badsum++;
229 m_freem(m);
230 return;
231 }
232 }
233
234 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
235 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
236 struct inpcb *last;
237 /*
238 * Deliver a multicast or broadcast datagram to *all* sockets
239 * for which the local and remote addresses and ports match
240 * those of the incoming datagram. This allows more than
241 * one process to receive multi/broadcasts on the same port.
242 * (This really ought to be done for unicast datagrams as
243 * well, but that would cause problems with existing
244 * applications that open both address-specific sockets and
245 * a wildcard socket listening to the same port -- they would
246 * end up receiving duplicates of every unicast datagram.
247 * Those applications open the multiple sockets to overcome an
248 * inadequacy of the UDP socket interface, but for backwards
249 * compatibility we avoid the problem here rather than
250 * fixing the interface. Maybe 4.5BSD will remedy this?)
251 */
252
253 /*
254 * Construct sockaddr format source address.
255 */
256 udp_in.sin_port = uh->uh_sport;
257 udp_in.sin_addr = ip->ip_src;
258 /*
259 * Locate pcb(s) for datagram.
260 * (Algorithm copied from raw_intr().)
261 */
262 last = NULL;
263#ifdef INET6
264 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
265#endif
266 LIST_FOREACH(inp, &udb, inp_list) {
267#ifdef INET6
268 if ((inp->inp_vflag & INP_IPV4) == 0)
269 continue;
270#endif
271 if (inp->inp_lport != uh->uh_dport)
272 continue;
273 if (inp->inp_laddr.s_addr != INADDR_ANY) {
274 if (inp->inp_laddr.s_addr !=
275 ip->ip_dst.s_addr)
276 continue;
277 }
278 if (inp->inp_faddr.s_addr != INADDR_ANY) {
279 if (inp->inp_faddr.s_addr !=
280 ip->ip_src.s_addr ||
281 inp->inp_fport != uh->uh_sport)
282 continue;
283 }
284
285 if (last != NULL) {
286 struct mbuf *n;
287
288#ifdef IPSEC
289 /* check AH/ESP integrity. */
290 if (ipsec4_in_reject_so(m, last->inp_socket))
291 ipsecstat.in_polvio++;
292 /* do not inject data to pcb */
293 else
294#endif /*IPSEC*/
295 if ((n = m_copy(m, 0, M_COPYALL)) != NULL)
296 udp_append(last, ip, n,
297 iphlen +
298 sizeof(struct udphdr));
299 }
300 last = inp;
301 /*
302 * Don't look for additional matches if this one does
303 * not have either the SO_REUSEPORT or SO_REUSEADDR
304 * socket options set. This heuristic avoids searching
305 * through all pcbs in the common case of a non-shared
306 * port. It * assumes that an application will never
307 * clear these options after setting them.
308 */
309 if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
310 break;
311 }
312
313 if (last == NULL) {
314 /*
315 * No matching pcb found; discard datagram.
316 * (No need to send an ICMP Port Unreachable
317 * for a broadcast or multicast datgram.)
318 */
319 udpstat.udps_noportbcast++;
320 goto bad;
321 }
322#ifdef IPSEC
323 /* check AH/ESP integrity. */
324 if (ipsec4_in_reject_so(m, last->inp_socket)) {
325 ipsecstat.in_polvio++;
326 goto bad;
327 }
328#endif /*IPSEC*/
329 udp_append(last, ip, m, iphlen + sizeof(struct udphdr));
330 return;
331 }
332 /*
333 * Locate pcb for datagram.
334 */
335 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
336 ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
337 if (inp == NULL) {
338 if (log_in_vain) {
339 char buf[4*sizeof "123"];
340
341 strcpy(buf, inet_ntoa(ip->ip_dst));
342 log(LOG_INFO,
343 "Connection attempt to UDP %s:%d from %s:%d\n",
344 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
345 ntohs(uh->uh_sport));
346 }
347 udpstat.udps_noport++;
348 if (m->m_flags & (M_BCAST | M_MCAST)) {
349 udpstat.udps_noportbcast++;
350 goto bad;
351 }
352 *ip = save_ip;
353#ifdef ICMP_BANDLIM
354 if (badport_bandlim(0) < 0)
355 goto bad;
356#endif
357 if (!blackhole)
358 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
359 else
360 goto bad;
361 return;
362 }
363#ifdef IPSEC
364 if (ipsec4_in_reject_so(m, inp->inp_socket)) {
365 ipsecstat.in_polvio++;
366 goto bad;
367 }
368#endif /*IPSEC*/
369
370 /*
371 * Construct sockaddr format source address.
372 * Stuff source address and datagram in user buffer.
373 */
374 udp_in.sin_port = uh->uh_sport;
375 udp_in.sin_addr = ip->ip_src;
376 if (inp->inp_flags & INP_CONTROLOPTS
377 || inp->inp_socket->so_options & SO_TIMESTAMP) {
378#ifdef INET6
379 if (inp->inp_vflag & INP_IPV6) {
380 int savedflags;
381
382 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
383 savedflags = inp->inp_flags;
384 inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
385 ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m);
386 inp->inp_flags = savedflags;
387 } else
388#endif
389 ip_savecontrol(inp, &opts, ip, m);
390 }
391 iphlen += sizeof(struct udphdr);
392 m_adj(m, iphlen);
393#ifdef INET6
394 if (inp->inp_vflag & INP_IPV6) {
395 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
396 append_sa = (struct sockaddr *)&udp_in6;
397 } else
398#endif
399 append_sa = (struct sockaddr *)&udp_in;
400 if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) {
401 udpstat.udps_fullsock++;
402 goto bad;
403 }
404 sorwakeup(inp->inp_socket);
405 return;
406bad:
407 m_freem(m);
408 if (opts)
409 m_freem(opts);
410 return;
411}
412
413#if defined(INET6)
414static void
415ip_2_ip6_hdr(ip6, ip)
416 struct ip6_hdr *ip6;
417 struct ip *ip;
418{
419 bzero(ip6, sizeof(*ip6));
420
421 ip6->ip6_vfc = IPV6_VERSION;
422 ip6->ip6_plen = ip->ip_len;
423 ip6->ip6_nxt = ip->ip_p;
424 ip6->ip6_hlim = ip->ip_ttl;
425 ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
426 IPV6_ADDR_INT32_SMP;
427 ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
428 ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
429}
430#endif
431
432/*
433 * subroutine of udp_input(), mainly for source code readability.
434 * caller must properly init udp_ip6 and udp_in6 beforehand.
435 */
436static void
437udp_append(last, ip, n, off)
438 struct inpcb *last;
439 struct ip *ip;
440 struct mbuf *n;
441 int off;
442{
443 struct sockaddr *append_sa;
444 struct mbuf *opts = 0;
445
446 if (last->inp_flags & INP_CONTROLOPTS ||
447 last->inp_socket->so_options & SO_TIMESTAMP) {
448#ifdef INET6
449 if (last->inp_vflag & INP_IPV6) {
450 int savedflags;
451
452 if (udp_ip6.uip6_init_done == 0) {
453 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
454 udp_ip6.uip6_init_done = 1;
455 }
456 savedflags = last->inp_flags;
457 last->inp_flags &= ~INP_UNMAPPABLEOPTS;
458 ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n);
459 last->inp_flags = savedflags;
460 } else
461#endif
462 ip_savecontrol(last, &opts, ip, n);
463 }
464#ifdef INET6
465 if (last->inp_vflag & INP_IPV6) {
466 if (udp_in6.uin6_init_done == 0) {
467 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
468 udp_in6.uin6_init_done = 1;
469 }
470 append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
471 } else
472#endif
473 append_sa = (struct sockaddr *)&udp_in;
474 m_adj(n, off);
475 if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
476 m_freem(n);
477 if (opts)
478 m_freem(opts);
479 udpstat.udps_fullsock++;
480 } else
481 sorwakeup(last->inp_socket);
482}
483
484/*
485 * Notify a udp user of an asynchronous error;
486 * just wake up so that he can collect error status.
487 */
488void
489udp_notify(inp, errno)
490 register struct inpcb *inp;
491 int errno;
492{
493 inp->inp_socket->so_error = errno;
494 sorwakeup(inp->inp_socket);
495 sowwakeup(inp->inp_socket);
496}
497
498void
499udp_ctlinput(cmd, sa, vip)
500 int cmd;
501 struct sockaddr *sa;
502 void *vip;
503{
504 register struct ip *ip = vip;
505 register struct udphdr *uh;
506
507 if (!PRC_IS_REDIRECT(cmd) &&
508 ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0))
509 return;
510 if (ip) {
511 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
512 in_pcbnotify(&udb, sa, uh->uh_dport, ip->ip_src, uh->uh_sport,
513 cmd, udp_notify);
514 } else
515 in_pcbnotify(&udb, sa, 0, zeroin_addr, 0, cmd, udp_notify);
516}
517
518static int
519udp_pcblist SYSCTL_HANDLER_ARGS
520{
521 int error, i, n, s;
522 struct inpcb *inp, **inp_list;
523 inp_gen_t gencnt;
524 struct xinpgen xig;
525
526 /*
527 * The process of preparing the TCB list is too time-consuming and
528 * resource-intensive to repeat twice on every request.
529 */
530 if (req->oldptr == 0) {
531 n = udbinfo.ipi_count;
532 req->oldidx = 2 * (sizeof xig)
533 + (n + n/8) * sizeof(struct xinpcb);
534 return 0;
535 }
536
537 if (req->newptr != 0)
538 return EPERM;
539
540 /*
541 * OK, now we're committed to doing something.
542 */
543 s = splnet();
544 gencnt = udbinfo.ipi_gencnt;
545 n = udbinfo.ipi_count;
546 splx(s);
547
548 xig.xig_len = sizeof xig;
549 xig.xig_count = n;
550 xig.xig_gen = gencnt;
551 xig.xig_sogen = so_gencnt;
552 error = SYSCTL_OUT(req, &xig, sizeof xig);
553 if (error)
554 return error;
555
556 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
557 if (inp_list == 0)
558 return ENOMEM;
559
560 s = splnet();
561 for (inp = udbinfo.listhead->lh_first, i = 0; inp && i < n;
562 inp = inp->inp_list.le_next) {
563 if (inp->inp_gencnt <= gencnt && !prison_xinpcb(req->p, inp))
564 inp_list[i++] = inp;
565 }
566 splx(s);
567 n = i;
568
569 error = 0;
570 for (i = 0; i < n; i++) {
571 inp = inp_list[i];
572 if (inp->inp_gencnt <= gencnt) {
573 struct xinpcb xi;
574 xi.xi_len = sizeof xi;
575 /* XXX should avoid extra copy */
576 bcopy(inp, &xi.xi_inp, sizeof *inp);
577 if (inp->inp_socket)
578 sotoxsocket(inp->inp_socket, &xi.xi_socket);
579 error = SYSCTL_OUT(req, &xi, sizeof xi);
580 }
581 }
582 if (!error) {
583 /*
584 * Give the user an updated idea of our state.
585 * If the generation differs from what we told
586 * her before, she knows that something happened
587 * while we were processing this request, and it
588 * might be necessary to retry.
589 */
590 s = splnet();
591 xig.xig_gen = udbinfo.ipi_gencnt;
592 xig.xig_sogen = so_gencnt;
593 xig.xig_count = udbinfo.ipi_count;
594 splx(s);
595 error = SYSCTL_OUT(req, &xig, sizeof xig);
596 }
597 free(inp_list, M_TEMP);
598 return error;
599}
600
601SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
602 udp_pcblist, "S,xinpcb", "List of active UDP sockets");
603
604static int
605udp_getcred SYSCTL_HANDLER_ARGS
606{
607 struct sockaddr_in addrs[2];
608 struct inpcb *inp;
609 int error, s;
610
611 error = suser(req->p);
612 if (error)
613 return (error);
614 error = SYSCTL_IN(req, addrs, sizeof(addrs));
615 if (error)
616 return (error);
617 s = splnet();
618 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
619 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
620 if (inp == NULL || inp->inp_socket == NULL) {
621 error = ENOENT;
622 goto out;
623 }
624 error = SYSCTL_OUT(req, inp->inp_socket->so_cred, sizeof(struct ucred));
625out:
626 splx(s);
627 return (error);
628}
629
630SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
631 0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection");
632
633static int
634udp_output(inp, m, addr, control, p)
635 register struct inpcb *inp;
636 struct mbuf *m;
637 struct sockaddr *addr;
638 struct mbuf *control;
639 struct proc *p;
640{
641 register struct udpiphdr *ui;
642 register int len = m->m_pkthdr.len;
643 struct in_addr laddr;
644 struct sockaddr_in *sin;
645 int s = 0, error = 0;
646
647 if (control)
648 m_freem(control); /* XXX */
649
650 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
651 error = EMSGSIZE;
652 goto release;
653 }
654
655 if (addr) {
656 sin = (struct sockaddr_in *)addr;
657 prison_remote_ip(p, 0, &sin->sin_addr.s_addr);
658 laddr = inp->inp_laddr;
659 if (inp->inp_faddr.s_addr != INADDR_ANY) {
660 error = EISCONN;
661 goto release;
662 }
663 /*
664 * Must block input while temporarily connected.
665 */
666 s = splnet();
667 error = in_pcbconnect(inp, addr, p);
668 if (error) {
669 splx(s);
670 goto release;
671 }
672 } else {
673 if (inp->inp_faddr.s_addr == INADDR_ANY) {
674 error = ENOTCONN;
675 goto release;
676 }
677 }
678 /*
679 * Calculate data length and get a mbuf
680 * for UDP and IP headers.
681 */
682 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
683 if (m == 0) {
684 error = ENOBUFS;
685 if (addr)
686 splx(s);
687 goto release;
688 }
689
690 /*
691 * Fill in mbuf with extended UDP header
692 * and addresses and length put into network format.
693 */
694 ui = mtod(m, struct udpiphdr *);
682 bzero(ui->ui_x1, sizeof(ui->ui_x1));
695 bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */
683 ui->ui_pr = IPPROTO_UDP;
696 ui->ui_pr = IPPROTO_UDP;
684 ui->ui_len = htons((u_short)len + sizeof (struct udphdr));
685 ui->ui_src = inp->inp_laddr;
686 ui->ui_dst = inp->inp_faddr;
687 ui->ui_sport = inp->inp_lport;
688 ui->ui_dport = inp->inp_fport;
697 ui->ui_src = inp->inp_laddr;
698 ui->ui_dst = inp->inp_faddr;
699 ui->ui_sport = inp->inp_lport;
700 ui->ui_dport = inp->inp_fport;
689 ui->ui_ulen = ui->ui_len;
701 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
690
691 /*
702
703 /*
692 * Stuff checksum and output datagram.
704 * Set up checksum and output datagram.
693 */
705 */
694 ui->ui_sum = 0;
695 if (udpcksum) {
706 if (udpcksum) {
696 if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0)
697 ui->ui_sum = 0xffff;
707 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
708 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
709 m->m_pkthdr.csum_flags = CSUM_UDP;
710 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
711 } else {
712 ui->ui_sum = 0;
698 }
699 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
700 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */
701 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */
702 udpstat.udps_opackets++;
703
704#ifdef IPSEC
705 m->m_pkthdr.rcvif = (struct ifnet *)inp->inp_socket;
706#endif /*IPSEC*/
707
708 error = ip_output(m, inp->inp_options, &inp->inp_route,
709 (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST))
710 | IP_SOCKINMRCVIF,
711 inp->inp_moptions);
712
713 if (addr) {
714 in_pcbdisconnect(inp);
715 inp->inp_laddr = laddr; /* XXX rehash? */
716 splx(s);
717 }
718 return (error);
719
720release:
721 m_freem(m);
722 return (error);
723}
724
725u_long udp_sendspace = 9216; /* really max datagram size */
726 /* 40 1K datagrams */
727SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
728 &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
729
730u_long udp_recvspace = 40 * (1024 +
731#ifdef INET6
732 sizeof(struct sockaddr_in6)
733#else
734 sizeof(struct sockaddr_in)
735#endif
736 );
737SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
738 &udp_recvspace, 0, "Maximum incoming UDP datagram size");
739
740static int
741udp_abort(struct socket *so)
742{
743 struct inpcb *inp;
744 int s;
745
746 inp = sotoinpcb(so);
747 if (inp == 0)
748 return EINVAL; /* ??? possible? panic instead? */
749 soisdisconnected(so);
750 s = splnet();
751 in_pcbdetach(inp);
752 splx(s);
753 return 0;
754}
755
756static int
757udp_attach(struct socket *so, int proto, struct proc *p)
758{
759 struct inpcb *inp;
760 int s, error;
761
762 inp = sotoinpcb(so);
763 if (inp != 0)
764 return EINVAL;
765
766 error = soreserve(so, udp_sendspace, udp_recvspace);
767 if (error)
768 return error;
769 s = splnet();
770 error = in_pcballoc(so, &udbinfo, p);
771 splx(s);
772 if (error)
773 return error;
774
775 inp = (struct inpcb *)so->so_pcb;
776 inp->inp_vflag |= INP_IPV4;
777 inp->inp_ip_ttl = ip_defttl;
778#ifdef IPSEC
779 error = ipsec_init_policy(so, &inp->inp_sp);
780 if (error != 0) {
781 in_pcbdetach(inp);
782 return error;
783 }
784#endif /*IPSEC*/
785 return 0;
786}
787
788static int
789udp_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
790{
791 struct inpcb *inp;
792 int s, error;
793
794 inp = sotoinpcb(so);
795 if (inp == 0)
796 return EINVAL;
797 s = splnet();
798 error = in_pcbbind(inp, nam, p);
799 splx(s);
800 return error;
801}
802
803static int
804udp_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
805{
806 struct inpcb *inp;
807 int s, error;
808 struct sockaddr_in *sin;
809
810 inp = sotoinpcb(so);
811 if (inp == 0)
812 return EINVAL;
813 if (inp->inp_faddr.s_addr != INADDR_ANY)
814 return EISCONN;
815 s = splnet();
816 sin = (struct sockaddr_in *)nam;
817 prison_remote_ip(p, 0, &sin->sin_addr.s_addr);
818 error = in_pcbconnect(inp, nam, p);
819 splx(s);
820 if (error == 0)
821 soisconnected(so);
822 return error;
823}
824
825static int
826udp_detach(struct socket *so)
827{
828 struct inpcb *inp;
829 int s;
830
831 inp = sotoinpcb(so);
832 if (inp == 0)
833 return EINVAL;
834 s = splnet();
835 in_pcbdetach(inp);
836 splx(s);
837 return 0;
838}
839
840static int
841udp_disconnect(struct socket *so)
842{
843 struct inpcb *inp;
844 int s;
845
846 inp = sotoinpcb(so);
847 if (inp == 0)
848 return EINVAL;
849 if (inp->inp_faddr.s_addr == INADDR_ANY)
850 return ENOTCONN;
851
852 s = splnet();
853 in_pcbdisconnect(inp);
854 inp->inp_laddr.s_addr = INADDR_ANY;
855 splx(s);
856 so->so_state &= ~SS_ISCONNECTED; /* XXX */
857 return 0;
858}
859
860static int
861udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
862 struct mbuf *control, struct proc *p)
863{
864 struct inpcb *inp;
865
866 inp = sotoinpcb(so);
867 if (inp == 0) {
868 m_freem(m);
869 return EINVAL;
870 }
871 return udp_output(inp, m, addr, control, p);
872}
873
874int
875udp_shutdown(struct socket *so)
876{
877 struct inpcb *inp;
878
879 inp = sotoinpcb(so);
880 if (inp == 0)
881 return EINVAL;
882 socantsendmore(so);
883 return 0;
884}
885
886struct pr_usrreqs udp_usrreqs = {
887 udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect,
888 pru_connect2_notsupp, in_control, udp_detach, udp_disconnect,
889 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
890 pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown,
891 in_setsockaddr, sosend, soreceive, sopoll
892};
893
713 }
714 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
715 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */
716 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */
717 udpstat.udps_opackets++;
718
719#ifdef IPSEC
720 m->m_pkthdr.rcvif = (struct ifnet *)inp->inp_socket;
721#endif /*IPSEC*/
722
723 error = ip_output(m, inp->inp_options, &inp->inp_route,
724 (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST))
725 | IP_SOCKINMRCVIF,
726 inp->inp_moptions);
727
728 if (addr) {
729 in_pcbdisconnect(inp);
730 inp->inp_laddr = laddr; /* XXX rehash? */
731 splx(s);
732 }
733 return (error);
734
735release:
736 m_freem(m);
737 return (error);
738}
739
740u_long udp_sendspace = 9216; /* really max datagram size */
741 /* 40 1K datagrams */
742SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
743 &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
744
745u_long udp_recvspace = 40 * (1024 +
746#ifdef INET6
747 sizeof(struct sockaddr_in6)
748#else
749 sizeof(struct sockaddr_in)
750#endif
751 );
752SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
753 &udp_recvspace, 0, "Maximum incoming UDP datagram size");
754
755static int
756udp_abort(struct socket *so)
757{
758 struct inpcb *inp;
759 int s;
760
761 inp = sotoinpcb(so);
762 if (inp == 0)
763 return EINVAL; /* ??? possible? panic instead? */
764 soisdisconnected(so);
765 s = splnet();
766 in_pcbdetach(inp);
767 splx(s);
768 return 0;
769}
770
771static int
772udp_attach(struct socket *so, int proto, struct proc *p)
773{
774 struct inpcb *inp;
775 int s, error;
776
777 inp = sotoinpcb(so);
778 if (inp != 0)
779 return EINVAL;
780
781 error = soreserve(so, udp_sendspace, udp_recvspace);
782 if (error)
783 return error;
784 s = splnet();
785 error = in_pcballoc(so, &udbinfo, p);
786 splx(s);
787 if (error)
788 return error;
789
790 inp = (struct inpcb *)so->so_pcb;
791 inp->inp_vflag |= INP_IPV4;
792 inp->inp_ip_ttl = ip_defttl;
793#ifdef IPSEC
794 error = ipsec_init_policy(so, &inp->inp_sp);
795 if (error != 0) {
796 in_pcbdetach(inp);
797 return error;
798 }
799#endif /*IPSEC*/
800 return 0;
801}
802
803static int
804udp_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
805{
806 struct inpcb *inp;
807 int s, error;
808
809 inp = sotoinpcb(so);
810 if (inp == 0)
811 return EINVAL;
812 s = splnet();
813 error = in_pcbbind(inp, nam, p);
814 splx(s);
815 return error;
816}
817
818static int
819udp_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
820{
821 struct inpcb *inp;
822 int s, error;
823 struct sockaddr_in *sin;
824
825 inp = sotoinpcb(so);
826 if (inp == 0)
827 return EINVAL;
828 if (inp->inp_faddr.s_addr != INADDR_ANY)
829 return EISCONN;
830 s = splnet();
831 sin = (struct sockaddr_in *)nam;
832 prison_remote_ip(p, 0, &sin->sin_addr.s_addr);
833 error = in_pcbconnect(inp, nam, p);
834 splx(s);
835 if (error == 0)
836 soisconnected(so);
837 return error;
838}
839
840static int
841udp_detach(struct socket *so)
842{
843 struct inpcb *inp;
844 int s;
845
846 inp = sotoinpcb(so);
847 if (inp == 0)
848 return EINVAL;
849 s = splnet();
850 in_pcbdetach(inp);
851 splx(s);
852 return 0;
853}
854
855static int
856udp_disconnect(struct socket *so)
857{
858 struct inpcb *inp;
859 int s;
860
861 inp = sotoinpcb(so);
862 if (inp == 0)
863 return EINVAL;
864 if (inp->inp_faddr.s_addr == INADDR_ANY)
865 return ENOTCONN;
866
867 s = splnet();
868 in_pcbdisconnect(inp);
869 inp->inp_laddr.s_addr = INADDR_ANY;
870 splx(s);
871 so->so_state &= ~SS_ISCONNECTED; /* XXX */
872 return 0;
873}
874
875static int
876udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
877 struct mbuf *control, struct proc *p)
878{
879 struct inpcb *inp;
880
881 inp = sotoinpcb(so);
882 if (inp == 0) {
883 m_freem(m);
884 return EINVAL;
885 }
886 return udp_output(inp, m, addr, control, p);
887}
888
889int
890udp_shutdown(struct socket *so)
891{
892 struct inpcb *inp;
893
894 inp = sotoinpcb(so);
895 if (inp == 0)
896 return EINVAL;
897 socantsendmore(so);
898 return 0;
899}
900
901struct pr_usrreqs udp_usrreqs = {
902 udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect,
903 pru_connect2_notsupp, in_control, udp_detach, udp_disconnect,
904 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
905 pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown,
906 in_setsockaddr, sosend, soreceive, sopoll
907};
908