udp_usrreq.c revision 1.23 1 1.23 cgd /* $NetBSD: udp_usrreq.c,v 1.23 1995/06/26 08:46:16 cgd Exp $ */
2 1.14 cgd
3 1.1 cgd /*
4 1.13 mycroft * Copyright (c) 1982, 1986, 1988, 1990, 1993
5 1.13 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd *
35 1.14 cgd * @(#)udp_usrreq.c 8.4 (Berkeley) 1/21/94
36 1.1 cgd */
37 1.1 cgd
38 1.5 mycroft #include <sys/param.h>
39 1.5 mycroft #include <sys/malloc.h>
40 1.5 mycroft #include <sys/mbuf.h>
41 1.5 mycroft #include <sys/protosw.h>
42 1.5 mycroft #include <sys/socket.h>
43 1.5 mycroft #include <sys/socketvar.h>
44 1.13 mycroft #include <sys/errno.h>
45 1.5 mycroft #include <sys/stat.h>
46 1.1 cgd
47 1.5 mycroft #include <net/if.h>
48 1.5 mycroft #include <net/route.h>
49 1.1 cgd
50 1.5 mycroft #include <netinet/in.h>
51 1.5 mycroft #include <netinet/in_systm.h>
52 1.15 cgd #include <netinet/in_var.h>
53 1.5 mycroft #include <netinet/ip.h>
54 1.5 mycroft #include <netinet/in_pcb.h>
55 1.5 mycroft #include <netinet/ip_var.h>
56 1.5 mycroft #include <netinet/ip_icmp.h>
57 1.5 mycroft #include <netinet/udp.h>
58 1.5 mycroft #include <netinet/udp_var.h>
59 1.1 cgd
60 1.8 mycroft /*
61 1.8 mycroft * UDP protocol implementation.
62 1.8 mycroft * Per RFC 768, August, 1980.
63 1.8 mycroft */
64 1.8 mycroft #ifndef COMPAT_42
65 1.8 mycroft int udpcksum = 1;
66 1.8 mycroft #else
67 1.8 mycroft int udpcksum = 0; /* XXX */
68 1.8 mycroft #endif
69 1.8 mycroft
70 1.8 mycroft struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET };
71 1.18 mycroft struct inpcb *udp_last_inpcb = 0;
72 1.1 cgd
73 1.13 mycroft static void udp_detach __P((struct inpcb *));
74 1.13 mycroft static void udp_notify __P((struct inpcb *, int));
75 1.13 mycroft static struct mbuf *udp_saveopt __P((caddr_t, int, int));
76 1.7 mycroft
77 1.7 mycroft void
78 1.1 cgd udp_init()
79 1.1 cgd {
80 1.18 mycroft
81 1.18 mycroft in_pcbinit(&udbtable);
82 1.1 cgd }
83 1.1 cgd
84 1.7 mycroft void
85 1.1 cgd udp_input(m, iphlen)
86 1.1 cgd register struct mbuf *m;
87 1.1 cgd int iphlen;
88 1.1 cgd {
89 1.1 cgd register struct ip *ip;
90 1.1 cgd register struct udphdr *uh;
91 1.1 cgd register struct inpcb *inp;
92 1.1 cgd struct mbuf *opts = 0;
93 1.1 cgd int len;
94 1.1 cgd struct ip save_ip;
95 1.1 cgd
96 1.1 cgd udpstat.udps_ipackets++;
97 1.1 cgd
98 1.1 cgd /*
99 1.1 cgd * Strip IP options, if any; should skip this,
100 1.1 cgd * make available to user, and use on returned packets,
101 1.1 cgd * but we don't yet have a way to check the checksum
102 1.1 cgd * with options still present.
103 1.1 cgd */
104 1.1 cgd if (iphlen > sizeof (struct ip)) {
105 1.1 cgd ip_stripoptions(m, (struct mbuf *)0);
106 1.1 cgd iphlen = sizeof(struct ip);
107 1.1 cgd }
108 1.1 cgd
109 1.1 cgd /*
110 1.1 cgd * Get IP and UDP header together in first mbuf.
111 1.1 cgd */
112 1.1 cgd ip = mtod(m, struct ip *);
113 1.1 cgd if (m->m_len < iphlen + sizeof(struct udphdr)) {
114 1.1 cgd if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
115 1.1 cgd udpstat.udps_hdrops++;
116 1.1 cgd return;
117 1.1 cgd }
118 1.1 cgd ip = mtod(m, struct ip *);
119 1.1 cgd }
120 1.1 cgd uh = (struct udphdr *)((caddr_t)ip + iphlen);
121 1.1 cgd
122 1.1 cgd /*
123 1.1 cgd * Make mbuf data length reflect UDP length.
124 1.1 cgd * If not enough data to reflect UDP length, drop.
125 1.1 cgd */
126 1.15 cgd len = ntohs((u_int16_t)uh->uh_ulen);
127 1.1 cgd if (ip->ip_len != len) {
128 1.1 cgd if (len > ip->ip_len) {
129 1.1 cgd udpstat.udps_badlen++;
130 1.1 cgd goto bad;
131 1.1 cgd }
132 1.1 cgd m_adj(m, len - ip->ip_len);
133 1.1 cgd /* ip->ip_len = len; */
134 1.1 cgd }
135 1.1 cgd /*
136 1.1 cgd * Save a copy of the IP header in case we want restore it
137 1.1 cgd * for sending an ICMP error message in response.
138 1.1 cgd */
139 1.1 cgd save_ip = *ip;
140 1.1 cgd
141 1.1 cgd /*
142 1.1 cgd * Checksum extended UDP header and data.
143 1.1 cgd */
144 1.1 cgd if (udpcksum && uh->uh_sum) {
145 1.1 cgd ((struct ipovly *)ip)->ih_next = 0;
146 1.1 cgd ((struct ipovly *)ip)->ih_prev = 0;
147 1.1 cgd ((struct ipovly *)ip)->ih_x1 = 0;
148 1.1 cgd ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
149 1.1 cgd if (uh->uh_sum = in_cksum(m, len + sizeof (struct ip))) {
150 1.1 cgd udpstat.udps_badsum++;
151 1.1 cgd m_freem(m);
152 1.1 cgd return;
153 1.1 cgd }
154 1.1 cgd }
155 1.13 mycroft
156 1.16 mycroft if (IN_MULTICAST(ip->ip_dst.s_addr) ||
157 1.13 mycroft in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
158 1.4 hpeyerl struct socket *last;
159 1.4 hpeyerl /*
160 1.4 hpeyerl * Deliver a multicast or broadcast datagram to *all* sockets
161 1.4 hpeyerl * for which the local and remote addresses and ports match
162 1.4 hpeyerl * those of the incoming datagram. This allows more than
163 1.4 hpeyerl * one process to receive multi/broadcasts on the same port.
164 1.4 hpeyerl * (This really ought to be done for unicast datagrams as
165 1.4 hpeyerl * well, but that would cause problems with existing
166 1.4 hpeyerl * applications that open both address-specific sockets and
167 1.4 hpeyerl * a wildcard socket listening to the same port -- they would
168 1.4 hpeyerl * end up receiving duplicates of every unicast datagram.
169 1.4 hpeyerl * Those applications open the multiple sockets to overcome an
170 1.4 hpeyerl * inadequacy of the UDP socket interface, but for backwards
171 1.4 hpeyerl * compatibility we avoid the problem here rather than
172 1.13 mycroft * fixing the interface. Maybe 4.5BSD will remedy this?)
173 1.4 hpeyerl */
174 1.13 mycroft
175 1.4 hpeyerl /*
176 1.4 hpeyerl * Construct sockaddr format source address.
177 1.4 hpeyerl */
178 1.4 hpeyerl udp_in.sin_port = uh->uh_sport;
179 1.4 hpeyerl udp_in.sin_addr = ip->ip_src;
180 1.4 hpeyerl m->m_len -= sizeof (struct udpiphdr);
181 1.4 hpeyerl m->m_data += sizeof (struct udpiphdr);
182 1.4 hpeyerl /*
183 1.4 hpeyerl * Locate pcb(s) for datagram.
184 1.4 hpeyerl * (Algorithm copied from raw_intr().)
185 1.4 hpeyerl */
186 1.4 hpeyerl last = NULL;
187 1.22 cgd for (inp = udbtable.inpt_queue.cqh_first;
188 1.22 cgd inp != (struct inpcb *)&udbtable.inpt_queue;
189 1.22 cgd inp = inp->inp_queue.cqe_next) {
190 1.4 hpeyerl if (inp->inp_lport != uh->uh_dport)
191 1.4 hpeyerl continue;
192 1.4 hpeyerl if (inp->inp_laddr.s_addr != INADDR_ANY) {
193 1.4 hpeyerl if (inp->inp_laddr.s_addr !=
194 1.4 hpeyerl ip->ip_dst.s_addr)
195 1.4 hpeyerl continue;
196 1.4 hpeyerl }
197 1.4 hpeyerl if (inp->inp_faddr.s_addr != INADDR_ANY) {
198 1.6 mycroft if (inp->inp_faddr.s_addr !=
199 1.4 hpeyerl ip->ip_src.s_addr ||
200 1.4 hpeyerl inp->inp_fport != uh->uh_sport)
201 1.4 hpeyerl continue;
202 1.4 hpeyerl }
203 1.4 hpeyerl
204 1.4 hpeyerl if (last != NULL) {
205 1.4 hpeyerl struct mbuf *n;
206 1.4 hpeyerl
207 1.4 hpeyerl if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
208 1.4 hpeyerl if (sbappendaddr(&last->so_rcv,
209 1.17 mycroft sintosa(&udp_in), n,
210 1.17 mycroft (struct mbuf *)0) == 0) {
211 1.4 hpeyerl m_freem(n);
212 1.13 mycroft udpstat.udps_fullsock++;
213 1.13 mycroft } else
214 1.4 hpeyerl sorwakeup(last);
215 1.4 hpeyerl }
216 1.4 hpeyerl }
217 1.4 hpeyerl last = inp->inp_socket;
218 1.4 hpeyerl /*
219 1.13 mycroft * Don't look for additional matches if this one does
220 1.13 mycroft * not have either the SO_REUSEPORT or SO_REUSEADDR
221 1.13 mycroft * socket options set. This heuristic avoids searching
222 1.13 mycroft * through all pcbs in the common case of a non-shared
223 1.13 mycroft * port. It * assumes that an application will never
224 1.13 mycroft * clear these options after setting them.
225 1.4 hpeyerl */
226 1.13 mycroft if ((last->so_options&(SO_REUSEPORT|SO_REUSEADDR) == 0))
227 1.4 hpeyerl break;
228 1.4 hpeyerl }
229 1.6 mycroft
230 1.4 hpeyerl if (last == NULL) {
231 1.4 hpeyerl /*
232 1.4 hpeyerl * No matching pcb found; discard datagram.
233 1.4 hpeyerl * (No need to send an ICMP Port Unreachable
234 1.4 hpeyerl * for a broadcast or multicast datgram.)
235 1.4 hpeyerl */
236 1.13 mycroft udpstat.udps_noportbcast++;
237 1.4 hpeyerl goto bad;
238 1.4 hpeyerl }
239 1.17 mycroft if (sbappendaddr(&last->so_rcv, sintosa(&udp_in), m,
240 1.17 mycroft (struct mbuf *)0) == 0) {
241 1.13 mycroft udpstat.udps_fullsock++;
242 1.4 hpeyerl goto bad;
243 1.13 mycroft }
244 1.4 hpeyerl sorwakeup(last);
245 1.4 hpeyerl return;
246 1.4 hpeyerl }
247 1.1 cgd /*
248 1.1 cgd * Locate pcb for datagram.
249 1.1 cgd */
250 1.1 cgd inp = udp_last_inpcb;
251 1.18 mycroft if (inp == 0 ||
252 1.18 mycroft inp->inp_lport != uh->uh_dport ||
253 1.1 cgd inp->inp_fport != uh->uh_sport ||
254 1.1 cgd inp->inp_faddr.s_addr != ip->ip_src.s_addr ||
255 1.1 cgd inp->inp_laddr.s_addr != ip->ip_dst.s_addr) {
256 1.18 mycroft udpstat.udpps_pcbcachemiss++;
257 1.18 mycroft inp = in_pcblookup(&udbtable, ip->ip_src, uh->uh_sport,
258 1.1 cgd ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD);
259 1.18 mycroft if (inp == 0) {
260 1.18 mycroft udpstat.udps_noport++;
261 1.18 mycroft if (m->m_flags & (M_BCAST | M_MCAST)) {
262 1.18 mycroft udpstat.udps_noportbcast++;
263 1.18 mycroft goto bad;
264 1.18 mycroft }
265 1.18 mycroft *ip = save_ip;
266 1.18 mycroft ip->ip_len += iphlen;
267 1.18 mycroft icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
268 1.18 mycroft return;
269 1.13 mycroft }
270 1.18 mycroft udp_last_inpcb = inp;
271 1.1 cgd }
272 1.1 cgd
273 1.1 cgd /*
274 1.1 cgd * Construct sockaddr format source address.
275 1.1 cgd * Stuff source address and datagram in user buffer.
276 1.1 cgd */
277 1.1 cgd udp_in.sin_port = uh->uh_sport;
278 1.1 cgd udp_in.sin_addr = ip->ip_src;
279 1.1 cgd if (inp->inp_flags & INP_CONTROLOPTS) {
280 1.1 cgd struct mbuf **mp = &opts;
281 1.1 cgd
282 1.1 cgd if (inp->inp_flags & INP_RECVDSTADDR) {
283 1.1 cgd *mp = udp_saveopt((caddr_t) &ip->ip_dst,
284 1.1 cgd sizeof(struct in_addr), IP_RECVDSTADDR);
285 1.1 cgd if (*mp)
286 1.1 cgd mp = &(*mp)->m_next;
287 1.1 cgd }
288 1.1 cgd #ifdef notyet
289 1.1 cgd /* options were tossed above */
290 1.1 cgd if (inp->inp_flags & INP_RECVOPTS) {
291 1.1 cgd *mp = udp_saveopt((caddr_t) opts_deleted_above,
292 1.1 cgd sizeof(struct in_addr), IP_RECVOPTS);
293 1.1 cgd if (*mp)
294 1.1 cgd mp = &(*mp)->m_next;
295 1.1 cgd }
296 1.1 cgd /* ip_srcroute doesn't do what we want here, need to fix */
297 1.1 cgd if (inp->inp_flags & INP_RECVRETOPTS) {
298 1.1 cgd *mp = udp_saveopt((caddr_t) ip_srcroute(),
299 1.1 cgd sizeof(struct in_addr), IP_RECVRETOPTS);
300 1.1 cgd if (*mp)
301 1.1 cgd mp = &(*mp)->m_next;
302 1.1 cgd }
303 1.1 cgd #endif
304 1.1 cgd }
305 1.1 cgd iphlen += sizeof(struct udphdr);
306 1.1 cgd m->m_len -= iphlen;
307 1.1 cgd m->m_pkthdr.len -= iphlen;
308 1.1 cgd m->m_data += iphlen;
309 1.17 mycroft if (sbappendaddr(&inp->inp_socket->so_rcv, sintosa(&udp_in), m,
310 1.17 mycroft opts) == 0) {
311 1.1 cgd udpstat.udps_fullsock++;
312 1.1 cgd goto bad;
313 1.1 cgd }
314 1.1 cgd sorwakeup(inp->inp_socket);
315 1.1 cgd return;
316 1.1 cgd bad:
317 1.1 cgd m_freem(m);
318 1.1 cgd if (opts)
319 1.1 cgd m_freem(opts);
320 1.1 cgd }
321 1.1 cgd
322 1.1 cgd /*
323 1.1 cgd * Create a "control" mbuf containing the specified data
324 1.1 cgd * with the specified type for presentation with a datagram.
325 1.1 cgd */
326 1.13 mycroft struct mbuf *
327 1.1 cgd udp_saveopt(p, size, type)
328 1.1 cgd caddr_t p;
329 1.1 cgd register int size;
330 1.1 cgd int type;
331 1.1 cgd {
332 1.1 cgd register struct cmsghdr *cp;
333 1.1 cgd struct mbuf *m;
334 1.1 cgd
335 1.1 cgd if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
336 1.1 cgd return ((struct mbuf *) NULL);
337 1.1 cgd cp = (struct cmsghdr *) mtod(m, struct cmsghdr *);
338 1.13 mycroft bcopy(p, CMSG_DATA(cp), size);
339 1.1 cgd size += sizeof(*cp);
340 1.1 cgd m->m_len = size;
341 1.1 cgd cp->cmsg_len = size;
342 1.1 cgd cp->cmsg_level = IPPROTO_IP;
343 1.1 cgd cp->cmsg_type = type;
344 1.1 cgd return (m);
345 1.1 cgd }
346 1.1 cgd
347 1.1 cgd /*
348 1.1 cgd * Notify a udp user of an asynchronous error;
349 1.1 cgd * just wake up so that he can collect error status.
350 1.1 cgd */
351 1.7 mycroft static void
352 1.1 cgd udp_notify(inp, errno)
353 1.1 cgd register struct inpcb *inp;
354 1.7 mycroft int errno;
355 1.1 cgd {
356 1.1 cgd inp->inp_socket->so_error = errno;
357 1.1 cgd sorwakeup(inp->inp_socket);
358 1.1 cgd sowwakeup(inp->inp_socket);
359 1.1 cgd }
360 1.1 cgd
361 1.7 mycroft void
362 1.1 cgd udp_ctlinput(cmd, sa, ip)
363 1.1 cgd int cmd;
364 1.1 cgd struct sockaddr *sa;
365 1.1 cgd register struct ip *ip;
366 1.1 cgd {
367 1.1 cgd register struct udphdr *uh;
368 1.1 cgd extern struct in_addr zeroin_addr;
369 1.21 mycroft extern int inetctlerrmap[];
370 1.18 mycroft void (*notify) __P((struct inpcb *, int)) = udp_notify;
371 1.21 mycroft int errno;
372 1.1 cgd
373 1.20 mycroft if ((unsigned)cmd >= PRC_NCMDS)
374 1.20 mycroft return;
375 1.20 mycroft errno = inetctlerrmap[cmd];
376 1.18 mycroft if (PRC_IS_REDIRECT(cmd))
377 1.19 mycroft notify = in_rtchange, ip = 0;
378 1.18 mycroft else if (cmd == PRC_HOSTDEAD)
379 1.19 mycroft ip = 0;
380 1.23 cgd else if (errno == 0)
381 1.1 cgd return;
382 1.19 mycroft if (ip) {
383 1.1 cgd uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
384 1.18 mycroft in_pcbnotify(&udbtable, sa, uh->uh_dport, ip->ip_src,
385 1.20 mycroft uh->uh_sport, errno, notify);
386 1.19 mycroft } else
387 1.20 mycroft in_pcbnotifyall(&udbtable, sa, errno, notify);
388 1.1 cgd }
389 1.1 cgd
390 1.7 mycroft int
391 1.1 cgd udp_output(inp, m, addr, control)
392 1.1 cgd register struct inpcb *inp;
393 1.1 cgd register struct mbuf *m;
394 1.1 cgd struct mbuf *addr, *control;
395 1.1 cgd {
396 1.1 cgd register struct udpiphdr *ui;
397 1.1 cgd register int len = m->m_pkthdr.len;
398 1.1 cgd struct in_addr laddr;
399 1.1 cgd int s, error = 0;
400 1.1 cgd
401 1.1 cgd if (control)
402 1.1 cgd m_freem(control); /* XXX */
403 1.1 cgd
404 1.1 cgd if (addr) {
405 1.1 cgd laddr = inp->inp_laddr;
406 1.1 cgd if (inp->inp_faddr.s_addr != INADDR_ANY) {
407 1.1 cgd error = EISCONN;
408 1.1 cgd goto release;
409 1.1 cgd }
410 1.1 cgd /*
411 1.1 cgd * Must block input while temporarily connected.
412 1.1 cgd */
413 1.1 cgd s = splnet();
414 1.1 cgd error = in_pcbconnect(inp, addr);
415 1.1 cgd if (error) {
416 1.1 cgd splx(s);
417 1.1 cgd goto release;
418 1.1 cgd }
419 1.1 cgd } else {
420 1.1 cgd if (inp->inp_faddr.s_addr == INADDR_ANY) {
421 1.1 cgd error = ENOTCONN;
422 1.1 cgd goto release;
423 1.1 cgd }
424 1.1 cgd }
425 1.1 cgd /*
426 1.1 cgd * Calculate data length and get a mbuf
427 1.1 cgd * for UDP and IP headers.
428 1.1 cgd */
429 1.13 mycroft M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
430 1.13 mycroft if (m == 0) {
431 1.13 mycroft error = ENOBUFS;
432 1.13 mycroft goto release;
433 1.13 mycroft }
434 1.1 cgd
435 1.1 cgd /*
436 1.1 cgd * Fill in mbuf with extended UDP header
437 1.1 cgd * and addresses and length put into network format.
438 1.1 cgd */
439 1.1 cgd ui = mtod(m, struct udpiphdr *);
440 1.1 cgd ui->ui_next = ui->ui_prev = 0;
441 1.1 cgd ui->ui_x1 = 0;
442 1.1 cgd ui->ui_pr = IPPROTO_UDP;
443 1.15 cgd ui->ui_len = htons((u_int16_t)len + sizeof (struct udphdr));
444 1.1 cgd ui->ui_src = inp->inp_laddr;
445 1.1 cgd ui->ui_dst = inp->inp_faddr;
446 1.1 cgd ui->ui_sport = inp->inp_lport;
447 1.1 cgd ui->ui_dport = inp->inp_fport;
448 1.1 cgd ui->ui_ulen = ui->ui_len;
449 1.1 cgd
450 1.1 cgd /*
451 1.1 cgd * Stuff checksum and output datagram.
452 1.1 cgd */
453 1.1 cgd ui->ui_sum = 0;
454 1.1 cgd if (udpcksum) {
455 1.1 cgd if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0)
456 1.1 cgd ui->ui_sum = 0xffff;
457 1.1 cgd }
458 1.1 cgd ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
459 1.1 cgd ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
460 1.1 cgd ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
461 1.1 cgd udpstat.udps_opackets++;
462 1.1 cgd error = ip_output(m, inp->inp_options, &inp->inp_route,
463 1.12 mycroft inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
464 1.12 mycroft inp->inp_moptions);
465 1.1 cgd
466 1.1 cgd if (addr) {
467 1.1 cgd in_pcbdisconnect(inp);
468 1.1 cgd inp->inp_laddr = laddr;
469 1.1 cgd splx(s);
470 1.1 cgd }
471 1.1 cgd return (error);
472 1.1 cgd
473 1.1 cgd release:
474 1.1 cgd m_freem(m);
475 1.1 cgd return (error);
476 1.1 cgd }
477 1.1 cgd
478 1.1 cgd u_long udp_sendspace = 9216; /* really max datagram size */
479 1.1 cgd u_long udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
480 1.1 cgd /* 40 1K datagrams */
481 1.1 cgd
482 1.1 cgd /*ARGSUSED*/
483 1.7 mycroft int
484 1.1 cgd udp_usrreq(so, req, m, addr, control)
485 1.1 cgd struct socket *so;
486 1.1 cgd int req;
487 1.1 cgd struct mbuf *m, *addr, *control;
488 1.1 cgd {
489 1.1 cgd struct inpcb *inp = sotoinpcb(so);
490 1.1 cgd int error = 0;
491 1.1 cgd int s;
492 1.1 cgd
493 1.1 cgd if (req == PRU_CONTROL)
494 1.15 cgd return (in_control(so, (long)m, (caddr_t)addr,
495 1.1 cgd (struct ifnet *)control));
496 1.1 cgd if (inp == NULL && req != PRU_ATTACH) {
497 1.1 cgd error = EINVAL;
498 1.1 cgd goto release;
499 1.1 cgd }
500 1.1 cgd /*
501 1.1 cgd * Note: need to block udp_input while changing
502 1.1 cgd * the udp pcb queue and/or pcb addresses.
503 1.1 cgd */
504 1.1 cgd switch (req) {
505 1.1 cgd
506 1.1 cgd case PRU_ATTACH:
507 1.1 cgd if (inp != NULL) {
508 1.1 cgd error = EINVAL;
509 1.1 cgd break;
510 1.1 cgd }
511 1.1 cgd s = splnet();
512 1.18 mycroft error = in_pcballoc(so, &udbtable);
513 1.1 cgd splx(s);
514 1.1 cgd if (error)
515 1.1 cgd break;
516 1.1 cgd error = soreserve(so, udp_sendspace, udp_recvspace);
517 1.1 cgd if (error)
518 1.1 cgd break;
519 1.13 mycroft ((struct inpcb *) so->so_pcb)->inp_ip.ip_ttl = ip_defttl;
520 1.1 cgd break;
521 1.1 cgd
522 1.1 cgd case PRU_DETACH:
523 1.1 cgd udp_detach(inp);
524 1.1 cgd break;
525 1.1 cgd
526 1.1 cgd case PRU_BIND:
527 1.1 cgd s = splnet();
528 1.1 cgd error = in_pcbbind(inp, addr);
529 1.1 cgd splx(s);
530 1.1 cgd break;
531 1.1 cgd
532 1.1 cgd case PRU_LISTEN:
533 1.1 cgd error = EOPNOTSUPP;
534 1.1 cgd break;
535 1.1 cgd
536 1.1 cgd case PRU_CONNECT:
537 1.1 cgd if (inp->inp_faddr.s_addr != INADDR_ANY) {
538 1.1 cgd error = EISCONN;
539 1.1 cgd break;
540 1.1 cgd }
541 1.1 cgd s = splnet();
542 1.1 cgd error = in_pcbconnect(inp, addr);
543 1.1 cgd splx(s);
544 1.1 cgd if (error == 0)
545 1.1 cgd soisconnected(so);
546 1.1 cgd break;
547 1.1 cgd
548 1.1 cgd case PRU_CONNECT2:
549 1.1 cgd error = EOPNOTSUPP;
550 1.1 cgd break;
551 1.1 cgd
552 1.1 cgd case PRU_ACCEPT:
553 1.1 cgd error = EOPNOTSUPP;
554 1.1 cgd break;
555 1.1 cgd
556 1.1 cgd case PRU_DISCONNECT:
557 1.1 cgd if (inp->inp_faddr.s_addr == INADDR_ANY) {
558 1.1 cgd error = ENOTCONN;
559 1.1 cgd break;
560 1.1 cgd }
561 1.1 cgd s = splnet();
562 1.1 cgd in_pcbdisconnect(inp);
563 1.1 cgd inp->inp_laddr.s_addr = INADDR_ANY;
564 1.1 cgd splx(s);
565 1.1 cgd so->so_state &= ~SS_ISCONNECTED; /* XXX */
566 1.1 cgd break;
567 1.1 cgd
568 1.1 cgd case PRU_SHUTDOWN:
569 1.1 cgd socantsendmore(so);
570 1.1 cgd break;
571 1.1 cgd
572 1.1 cgd case PRU_SEND:
573 1.1 cgd return (udp_output(inp, m, addr, control));
574 1.1 cgd
575 1.1 cgd case PRU_ABORT:
576 1.1 cgd soisdisconnected(so);
577 1.1 cgd udp_detach(inp);
578 1.1 cgd break;
579 1.1 cgd
580 1.1 cgd case PRU_SOCKADDR:
581 1.1 cgd in_setsockaddr(inp, addr);
582 1.1 cgd break;
583 1.1 cgd
584 1.1 cgd case PRU_PEERADDR:
585 1.1 cgd in_setpeeraddr(inp, addr);
586 1.1 cgd break;
587 1.1 cgd
588 1.1 cgd case PRU_SENSE:
589 1.1 cgd /*
590 1.1 cgd * stat: don't bother with a blocksize.
591 1.1 cgd */
592 1.1 cgd return (0);
593 1.1 cgd
594 1.1 cgd case PRU_SENDOOB:
595 1.1 cgd case PRU_FASTTIMO:
596 1.1 cgd case PRU_SLOWTIMO:
597 1.1 cgd case PRU_PROTORCV:
598 1.1 cgd case PRU_PROTOSEND:
599 1.1 cgd error = EOPNOTSUPP;
600 1.1 cgd break;
601 1.1 cgd
602 1.1 cgd case PRU_RCVD:
603 1.1 cgd case PRU_RCVOOB:
604 1.1 cgd return (EOPNOTSUPP); /* do not free mbuf's */
605 1.1 cgd
606 1.1 cgd default:
607 1.1 cgd panic("udp_usrreq");
608 1.1 cgd }
609 1.1 cgd
610 1.1 cgd release:
611 1.1 cgd if (control) {
612 1.1 cgd printf("udp control data unexpectedly retained\n");
613 1.1 cgd m_freem(control);
614 1.1 cgd }
615 1.1 cgd if (m)
616 1.1 cgd m_freem(m);
617 1.1 cgd return (error);
618 1.1 cgd }
619 1.1 cgd
620 1.7 mycroft static void
621 1.1 cgd udp_detach(inp)
622 1.1 cgd struct inpcb *inp;
623 1.1 cgd {
624 1.1 cgd int s = splnet();
625 1.1 cgd
626 1.1 cgd if (inp == udp_last_inpcb)
627 1.18 mycroft udp_last_inpcb = 0;
628 1.1 cgd in_pcbdetach(inp);
629 1.1 cgd splx(s);
630 1.13 mycroft }
631 1.13 mycroft
632 1.13 mycroft /*
633 1.13 mycroft * Sysctl for udp variables.
634 1.13 mycroft */
635 1.13 mycroft udp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
636 1.13 mycroft int *name;
637 1.13 mycroft u_int namelen;
638 1.13 mycroft void *oldp;
639 1.13 mycroft size_t *oldlenp;
640 1.13 mycroft void *newp;
641 1.13 mycroft size_t newlen;
642 1.13 mycroft {
643 1.13 mycroft /* All sysctl names at this level are terminal. */
644 1.13 mycroft if (namelen != 1)
645 1.13 mycroft return (ENOTDIR);
646 1.13 mycroft
647 1.13 mycroft switch (name[0]) {
648 1.13 mycroft case UDPCTL_CHECKSUM:
649 1.13 mycroft return (sysctl_int(oldp, oldlenp, newp, newlen, &udpcksum));
650 1.13 mycroft default:
651 1.13 mycroft return (ENOPROTOOPT);
652 1.13 mycroft }
653 1.13 mycroft /* NOTREACHED */
654 1.1 cgd }
655