Commit | Line | Data |
---|---|---|
8ae0e4b4 | 1 | /* |
ad787160 | 2 | * Copyright (c) 1982, 1986, 1988, 1990, 1993 |
1611db1e | 3 | * The Regents of the University of California. All rights reserved. |
8ae0e4b4 | 4 | * |
ad787160 C |
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. | |
2b6b6284 | 20 | * |
ad787160 C |
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 | * | |
ed554bc5 | 33 | * @(#)udp_usrreq.c 8.4 (Berkeley) 1/21/94 |
8ae0e4b4 | 34 | */ |
20666ad3 | 35 | |
5548a02f KB |
36 | #include <sys/param.h> |
37 | #include <sys/malloc.h> | |
38 | #include <sys/mbuf.h> | |
39 | #include <sys/protosw.h> | |
40 | #include <sys/socket.h> | |
41 | #include <sys/socketvar.h> | |
42 | #include <sys/errno.h> | |
43 | #include <sys/stat.h> | |
44 | ||
45 | #include <net/if.h> | |
46 | #include <net/route.h> | |
47 | ||
48 | #include <netinet/in.h> | |
49 | #include <netinet/in_systm.h> | |
50 | #include <netinet/ip.h> | |
51 | #include <netinet/in_pcb.h> | |
52 | #include <netinet/ip_var.h> | |
53 | #include <netinet/ip_icmp.h> | |
54 | #include <netinet/udp.h> | |
55 | #include <netinet/udp_var.h> | |
d52566dd | 56 | |
2b4b57cd BJ |
57 | /* |
58 | * UDP protocol implementation. | |
59 | * Per RFC 768, August, 1980. | |
60 | */ | |
c50542f3 | 61 | #ifndef COMPAT_42 |
dd8707ae | 62 | int udpcksum = 1; |
c50542f3 MK |
63 | #else |
64 | int udpcksum = 0; /* XXX */ | |
65 | #endif | |
66 | ||
5328bc49 | 67 | struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET }; |
c46785cb KB |
68 | struct inpcb *udp_last_inpcb = &udb; |
69 | ||
70 | static void udp_detach __P((struct inpcb *)); | |
71 | static void udp_notify __P((struct inpcb *, int)); | |
72 | static struct mbuf *udp_saveopt __P((caddr_t, int, int)); | |
73 | ||
74 | void | |
75 | udp_init() | |
76 | { | |
77 | udb.inp_next = udb.inp_prev = &udb; | |
78 | } | |
eb44bfb2 | 79 | |
c46785cb | 80 | void |
9d91b170 MK |
81 | udp_input(m, iphlen) |
82 | register struct mbuf *m; | |
83 | int iphlen; | |
1bd53abe | 84 | { |
0dfbe263 MK |
85 | register struct ip *ip; |
86 | register struct udphdr *uh; | |
53a5409e | 87 | register struct inpcb *inp; |
5921352b | 88 | struct mbuf *opts = 0; |
e199a8a5 | 89 | int len; |
0dfbe263 | 90 | struct ip save_ip; |
eb44bfb2 | 91 | |
5921352b | 92 | udpstat.udps_ipackets++; |
9f5c260d MK |
93 | |
94 | /* | |
95 | * Strip IP options, if any; should skip this, | |
96 | * make available to user, and use on returned packets, | |
97 | * but we don't yet have a way to check the checksum | |
98 | * with options still present. | |
99 | */ | |
100 | if (iphlen > sizeof (struct ip)) { | |
0dfbe263 | 101 | ip_stripoptions(m, (struct mbuf *)0); |
9f5c260d MK |
102 | iphlen = sizeof(struct ip); |
103 | } | |
104 | ||
2b4b57cd | 105 | /* |
4aed14e3 | 106 | * Get IP and UDP header together in first mbuf. |
2b4b57cd | 107 | */ |
0dfbe263 MK |
108 | ip = mtod(m, struct ip *); |
109 | if (m->m_len < iphlen + sizeof(struct udphdr)) { | |
110 | if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) { | |
7212ba67 MK |
111 | udpstat.udps_hdrops++; |
112 | return; | |
113 | } | |
0dfbe263 | 114 | ip = mtod(m, struct ip *); |
7212ba67 | 115 | } |
0dfbe263 | 116 | uh = (struct udphdr *)((caddr_t)ip + iphlen); |
2b4b57cd BJ |
117 | |
118 | /* | |
4aed14e3 BJ |
119 | * Make mbuf data length reflect UDP length. |
120 | * If not enough data to reflect UDP length, drop. | |
2b4b57cd | 121 | */ |
0dfbe263 MK |
122 | len = ntohs((u_short)uh->uh_ulen); |
123 | if (ip->ip_len != len) { | |
124 | if (len > ip->ip_len) { | |
2b4b57cd BJ |
125 | udpstat.udps_badlen++; |
126 | goto bad; | |
127 | } | |
0dfbe263 MK |
128 | m_adj(m, len - ip->ip_len); |
129 | /* ip->ip_len = len; */ | |
2b4b57cd | 130 | } |
dcea2497 | 131 | /* |
0dfbe263 MK |
132 | * Save a copy of the IP header in case we want restore it |
133 | * for sending an ICMP error message in response. | |
dcea2497 | 134 | */ |
0dfbe263 | 135 | save_ip = *ip; |
2b4b57cd BJ |
136 | |
137 | /* | |
4aed14e3 | 138 | * Checksum extended UDP header and data. |
2b4b57cd | 139 | */ |
0dfbe263 MK |
140 | if (udpcksum && uh->uh_sum) { |
141 | ((struct ipovly *)ip)->ih_next = 0; | |
142 | ((struct ipovly *)ip)->ih_prev = 0; | |
143 | ((struct ipovly *)ip)->ih_x1 = 0; | |
144 | ((struct ipovly *)ip)->ih_len = uh->uh_ulen; | |
145 | if (uh->uh_sum = in_cksum(m, len + sizeof (struct ip))) { | |
2b4b57cd | 146 | udpstat.udps_badsum++; |
eb44bfb2 BJ |
147 | m_freem(m); |
148 | return; | |
149 | } | |
150 | } | |
2b4b57cd | 151 | |
c63cb169 | 152 | if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || |
09ed489e | 153 | in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { |
d6fa15c2 KS |
154 | struct socket *last; |
155 | /* | |
156 | * Deliver a multicast or broadcast datagram to *all* sockets | |
157 | * for which the local and remote addresses and ports match | |
158 | * those of the incoming datagram. This allows more than | |
159 | * one process to receive multi/broadcasts on the same port. | |
160 | * (This really ought to be done for unicast datagrams as | |
161 | * well, but that would cause problems with existing | |
162 | * applications that open both address-specific sockets and | |
163 | * a wildcard socket listening to the same port -- they would | |
164 | * end up receiving duplicates of every unicast datagram. | |
165 | * Those applications open the multiple sockets to overcome an | |
166 | * inadequacy of the UDP socket interface, but for backwards | |
167 | * compatibility we avoid the problem here rather than | |
69d96ae2 | 168 | * fixing the interface. Maybe 4.5BSD will remedy this?) |
d6fa15c2 KS |
169 | */ |
170 | ||
171 | /* | |
172 | * Construct sockaddr format source address. | |
173 | */ | |
174 | udp_in.sin_port = uh->uh_sport; | |
175 | udp_in.sin_addr = ip->ip_src; | |
176 | m->m_len -= sizeof (struct udpiphdr); | |
177 | m->m_data += sizeof (struct udpiphdr); | |
178 | /* | |
179 | * Locate pcb(s) for datagram. | |
180 | * (Algorithm copied from raw_intr().) | |
181 | */ | |
182 | last = NULL; | |
183 | for (inp = udb.inp_next; inp != &udb; inp = inp->inp_next) { | |
184 | if (inp->inp_lport != uh->uh_dport) | |
185 | continue; | |
186 | if (inp->inp_laddr.s_addr != INADDR_ANY) { | |
187 | if (inp->inp_laddr.s_addr != | |
188 | ip->ip_dst.s_addr) | |
189 | continue; | |
190 | } | |
191 | if (inp->inp_faddr.s_addr != INADDR_ANY) { | |
192 | if (inp->inp_faddr.s_addr != | |
193 | ip->ip_src.s_addr || | |
194 | inp->inp_fport != uh->uh_sport) | |
195 | continue; | |
196 | } | |
197 | ||
198 | if (last != NULL) { | |
199 | struct mbuf *n; | |
200 | ||
201 | if ((n = m_copy(m, 0, M_COPYALL)) != NULL) { | |
202 | if (sbappendaddr(&last->so_rcv, | |
203 | (struct sockaddr *)&udp_in, | |
69d96ae2 | 204 | n, (struct mbuf *)0) == 0) { |
d6fa15c2 | 205 | m_freem(n); |
69d96ae2 AC |
206 | udpstat.udps_fullsock++; |
207 | } else | |
d6fa15c2 KS |
208 | sorwakeup(last); |
209 | } | |
210 | } | |
211 | last = inp->inp_socket; | |
212 | /* | |
d2cc32a6 MK |
213 | * Don't look for additional matches if this one does |
214 | * not have either the SO_REUSEPORT or SO_REUSEADDR | |
215 | * socket options set. This heuristic avoids searching | |
216 | * through all pcbs in the common case of a non-shared | |
217 | * port. It * assumes that an application will never | |
218 | * clear these options after setting them. | |
d6fa15c2 | 219 | */ |
d2cc32a6 | 220 | if ((last->so_options&(SO_REUSEPORT|SO_REUSEADDR) == 0)) |
d6fa15c2 KS |
221 | break; |
222 | } | |
223 | ||
224 | if (last == NULL) { | |
225 | /* | |
226 | * No matching pcb found; discard datagram. | |
227 | * (No need to send an ICMP Port Unreachable | |
228 | * for a broadcast or multicast datgram.) | |
229 | */ | |
69d96ae2 | 230 | udpstat.udps_noportbcast++; |
d6fa15c2 KS |
231 | goto bad; |
232 | } | |
233 | if (sbappendaddr(&last->so_rcv, (struct sockaddr *)&udp_in, | |
69d96ae2 AC |
234 | m, (struct mbuf *)0) == 0) { |
235 | udpstat.udps_fullsock++; | |
d6fa15c2 | 236 | goto bad; |
69d96ae2 | 237 | } |
d6fa15c2 KS |
238 | sorwakeup(last); |
239 | return; | |
240 | } | |
2b4b57cd | 241 | /* |
e199a8a5 | 242 | * Locate pcb for datagram. |
2b4b57cd | 243 | */ |
5921352b MK |
244 | inp = udp_last_inpcb; |
245 | if (inp->inp_lport != uh->uh_dport || | |
246 | inp->inp_fport != uh->uh_sport || | |
247 | inp->inp_faddr.s_addr != ip->ip_src.s_addr || | |
248 | inp->inp_laddr.s_addr != ip->ip_dst.s_addr) { | |
249 | inp = in_pcblookup(&udb, ip->ip_src, uh->uh_sport, | |
250 | ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD); | |
251 | if (inp) | |
252 | udp_last_inpcb = inp; | |
253 | udpstat.udpps_pcbcachemiss++; | |
254 | } | |
72e4f44e | 255 | if (inp == 0) { |
0dfbe263 | 256 | udpstat.udps_noport++; |
5927fb26 KM |
257 | if (m->m_flags & (M_BCAST | M_MCAST)) { |
258 | udpstat.udps_noportbcast++; | |
259 | goto bad; | |
260 | } | |
0dfbe263 | 261 | *ip = save_ip; |
9f5c260d | 262 | ip->ip_len += iphlen; |
d99f1c28 | 263 | icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); |
72e4f44e SL |
264 | return; |
265 | } | |
2b4b57cd BJ |
266 | |
267 | /* | |
268 | * Construct sockaddr format source address. | |
269 | * Stuff source address and datagram in user buffer. | |
270 | */ | |
0dfbe263 MK |
271 | udp_in.sin_port = uh->uh_sport; |
272 | udp_in.sin_addr = ip->ip_src; | |
5921352b MK |
273 | if (inp->inp_flags & INP_CONTROLOPTS) { |
274 | struct mbuf **mp = &opts; | |
5921352b MK |
275 | |
276 | if (inp->inp_flags & INP_RECVDSTADDR) { | |
277 | *mp = udp_saveopt((caddr_t) &ip->ip_dst, | |
278 | sizeof(struct in_addr), IP_RECVDSTADDR); | |
279 | if (*mp) | |
280 | mp = &(*mp)->m_next; | |
281 | } | |
282 | #ifdef notyet | |
283 | /* options were tossed above */ | |
284 | if (inp->inp_flags & INP_RECVOPTS) { | |
285 | *mp = udp_saveopt((caddr_t) opts_deleted_above, | |
286 | sizeof(struct in_addr), IP_RECVOPTS); | |
287 | if (*mp) | |
288 | mp = &(*mp)->m_next; | |
289 | } | |
290 | /* ip_srcroute doesn't do what we want here, need to fix */ | |
291 | if (inp->inp_flags & INP_RECVRETOPTS) { | |
292 | *mp = udp_saveopt((caddr_t) ip_srcroute(), | |
293 | sizeof(struct in_addr), IP_RECVRETOPTS); | |
294 | if (*mp) | |
295 | mp = &(*mp)->m_next; | |
296 | } | |
297 | #endif | |
298 | } | |
0dfbe263 MK |
299 | iphlen += sizeof(struct udphdr); |
300 | m->m_len -= iphlen; | |
301 | m->m_pkthdr.len -= iphlen; | |
302 | m->m_data += iphlen; | |
ab85b059 | 303 | if (sbappendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in, |
5921352b MK |
304 | m, opts) == 0) { |
305 | udpstat.udps_fullsock++; | |
2b4b57cd | 306 | goto bad; |
5921352b | 307 | } |
4e60622a | 308 | sorwakeup(inp->inp_socket); |
53a5409e | 309 | return; |
2b4b57cd | 310 | bad: |
53a5409e | 311 | m_freem(m); |
5921352b MK |
312 | if (opts) |
313 | m_freem(opts); | |
314 | } | |
315 | ||
316 | /* | |
317 | * Create a "control" mbuf containing the specified data | |
318 | * with the specified type for presentation with a datagram. | |
319 | */ | |
320 | struct mbuf * | |
321 | udp_saveopt(p, size, type) | |
322 | caddr_t p; | |
323 | register int size; | |
324 | int type; | |
325 | { | |
326 | register struct cmsghdr *cp; | |
327 | struct mbuf *m; | |
328 | ||
329 | if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL) | |
330 | return ((struct mbuf *) NULL); | |
331 | cp = (struct cmsghdr *) mtod(m, struct cmsghdr *); | |
0924b337 | 332 | bcopy(p, CMSG_DATA(cp), size); |
5921352b MK |
333 | size += sizeof(*cp); |
334 | m->m_len = size; | |
335 | cp->cmsg_len = size; | |
336 | cp->cmsg_level = IPPROTO_IP; | |
337 | cp->cmsg_type = type; | |
338 | return (m); | |
1bd53abe BJ |
339 | } |
340 | ||
41715710 MK |
341 | /* |
342 | * Notify a udp user of an asynchronous error; | |
343 | * just wake up so that he can collect error status. | |
344 | */ | |
c46785cb | 345 | static void |
5921352b | 346 | udp_notify(inp, errno) |
41715710 | 347 | register struct inpcb *inp; |
64f72726 | 348 | int errno; |
41715710 | 349 | { |
5921352b | 350 | inp->inp_socket->so_error = errno; |
41715710 MK |
351 | sorwakeup(inp->inp_socket); |
352 | sowwakeup(inp->inp_socket); | |
353 | } | |
354 | ||
c46785cb | 355 | void |
0dfbe263 | 356 | udp_ctlinput(cmd, sa, ip) |
39674d5f | 357 | int cmd; |
dcea2497 | 358 | struct sockaddr *sa; |
0dfbe263 | 359 | register struct ip *ip; |
39674d5f | 360 | { |
0dfbe263 MK |
361 | register struct udphdr *uh; |
362 | extern struct in_addr zeroin_addr; | |
39674d5f | 363 | extern u_char inetctlerrmap[]; |
39674d5f | 364 | |
69d96ae2 AC |
365 | if (!PRC_IS_REDIRECT(cmd) && |
366 | ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)) | |
dcea2497 | 367 | return; |
0dfbe263 MK |
368 | if (ip) { |
369 | uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); | |
370 | in_pcbnotify(&udb, sa, uh->uh_dport, ip->ip_src, uh->uh_sport, | |
371 | cmd, udp_notify); | |
372 | } else | |
373 | in_pcbnotify(&udb, sa, 0, zeroin_addr, 0, cmd, udp_notify); | |
39674d5f SL |
374 | } |
375 | ||
c46785cb | 376 | int |
a942fe02 | 377 | udp_output(inp, m, addr, control) |
d55475b1 | 378 | register struct inpcb *inp; |
2b4b57cd | 379 | register struct mbuf *m; |
a942fe02 | 380 | struct mbuf *addr, *control; |
9d91b170 | 381 | { |
2b4b57cd | 382 | register struct udpiphdr *ui; |
9d91b170 | 383 | register int len = m->m_pkthdr.len; |
a942fe02 MK |
384 | struct in_addr laddr; |
385 | int s, error = 0; | |
2b4b57cd | 386 | |
a942fe02 MK |
387 | if (control) |
388 | m_freem(control); /* XXX */ | |
389 | ||
390 | if (addr) { | |
391 | laddr = inp->inp_laddr; | |
392 | if (inp->inp_faddr.s_addr != INADDR_ANY) { | |
393 | error = EISCONN; | |
394 | goto release; | |
395 | } | |
396 | /* | |
397 | * Must block input while temporarily connected. | |
398 | */ | |
399 | s = splnet(); | |
400 | error = in_pcbconnect(inp, addr); | |
401 | if (error) { | |
402 | splx(s); | |
403 | goto release; | |
404 | } | |
405 | } else { | |
406 | if (inp->inp_faddr.s_addr == INADDR_ANY) { | |
407 | error = ENOTCONN; | |
408 | goto release; | |
409 | } | |
410 | } | |
2b4b57cd BJ |
411 | /* |
412 | * Calculate data length and get a mbuf | |
4aed14e3 | 413 | * for UDP and IP headers. |
2b4b57cd | 414 | */ |
b2a072f3 KM |
415 | M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT); |
416 | if (m == 0) { | |
417 | error = ENOBUFS; | |
418 | goto release; | |
419 | } | |
2b4b57cd BJ |
420 | |
421 | /* | |
4aed14e3 | 422 | * Fill in mbuf with extended UDP header |
2b4b57cd BJ |
423 | * and addresses and length put into network format. |
424 | */ | |
2b4b57cd BJ |
425 | ui = mtod(m, struct udpiphdr *); |
426 | ui->ui_next = ui->ui_prev = 0; | |
427 | ui->ui_x1 = 0; | |
428 | ui->ui_pr = IPPROTO_UDP; | |
ee847abd | 429 | ui->ui_len = htons((u_short)len + sizeof (struct udphdr)); |
4e60622a BJ |
430 | ui->ui_src = inp->inp_laddr; |
431 | ui->ui_dst = inp->inp_faddr; | |
432 | ui->ui_sport = inp->inp_lport; | |
433 | ui->ui_dport = inp->inp_fport; | |
ee847abd | 434 | ui->ui_ulen = ui->ui_len; |
2b4b57cd BJ |
435 | |
436 | /* | |
437 | * Stuff checksum and output datagram. | |
438 | */ | |
439 | ui->ui_sum = 0; | |
dd8707ae MK |
440 | if (udpcksum) { |
441 | if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0) | |
890024da | 442 | ui->ui_sum = 0xffff; |
ee09448f | 443 | } |
4e60622a | 444 | ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; |
5921352b MK |
445 | ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */ |
446 | ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */ | |
0dfbe263 | 447 | udpstat.udps_opackets++; |
a942fe02 | 448 | error = ip_output(m, inp->inp_options, &inp->inp_route, |
69d96ae2 AC |
449 | inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST), |
450 | inp->inp_moptions); | |
a942fe02 MK |
451 | |
452 | if (addr) { | |
453 | in_pcbdisconnect(inp); | |
454 | inp->inp_laddr = laddr; | |
455 | splx(s); | |
456 | } | |
457 | return (error); | |
458 | ||
459 | release: | |
460 | m_freem(m); | |
461 | return (error); | |
1bd53abe BJ |
462 | } |
463 | ||
c9209cff KM |
464 | u_long udp_sendspace = 9216; /* really max datagram size */ |
465 | u_long udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in)); | |
466 | /* 40 1K datagrams */ | |
1d14d351 | 467 | |
a8d3bf7f | 468 | /*ARGSUSED*/ |
c46785cb | 469 | int |
a942fe02 | 470 | udp_usrreq(so, req, m, addr, control) |
53a5409e | 471 | struct socket *so; |
1bd53abe | 472 | int req; |
a942fe02 | 473 | struct mbuf *m, *addr, *control; |
1bd53abe | 474 | { |
53a5409e | 475 | struct inpcb *inp = sotoinpcb(so); |
8a2f82db | 476 | int error = 0; |
5921352b | 477 | int s; |
1bd53abe | 478 | |
1d14d351 | 479 | if (req == PRU_CONTROL) |
a942fe02 | 480 | return (in_control(so, (int)m, (caddr_t)addr, |
0dfbe263 | 481 | (struct ifnet *)control)); |
a3fa431a SL |
482 | if (inp == NULL && req != PRU_ATTACH) { |
483 | error = EINVAL; | |
484 | goto release; | |
485 | } | |
5921352b | 486 | /* |
a44aeee3 MK |
487 | * Note: need to block udp_input while changing |
488 | * the udp pcb queue and/or pcb addresses. | |
5921352b | 489 | */ |
1bd53abe BJ |
490 | switch (req) { |
491 | ||
492 | case PRU_ATTACH: | |
a3fa431a SL |
493 | if (inp != NULL) { |
494 | error = EINVAL; | |
495 | break; | |
496 | } | |
5921352b | 497 | s = splnet(); |
8075bb0e | 498 | error = in_pcballoc(so, &udb); |
5921352b | 499 | splx(s); |
8075bb0e BJ |
500 | if (error) |
501 | break; | |
1d14d351 | 502 | error = soreserve(so, udp_sendspace, udp_recvspace); |
8075bb0e BJ |
503 | if (error) |
504 | break; | |
0321539c | 505 | ((struct inpcb *) so->so_pcb)->inp_ip.ip_ttl = ip_defttl; |
53a5409e | 506 | break; |
1bd53abe BJ |
507 | |
508 | case PRU_DETACH: | |
a44aeee3 | 509 | udp_detach(inp); |
53a5409e | 510 | break; |
1bd53abe | 511 | |
8075bb0e | 512 | case PRU_BIND: |
a44aeee3 | 513 | s = splnet(); |
a942fe02 | 514 | error = in_pcbbind(inp, addr); |
a44aeee3 | 515 | splx(s); |
8075bb0e BJ |
516 | break; |
517 | ||
518 | case PRU_LISTEN: | |
519 | error = EOPNOTSUPP; | |
520 | break; | |
521 | ||
1bd53abe | 522 | case PRU_CONNECT: |
a3fa431a SL |
523 | if (inp->inp_faddr.s_addr != INADDR_ANY) { |
524 | error = EISCONN; | |
525 | break; | |
526 | } | |
a44aeee3 | 527 | s = splnet(); |
a942fe02 | 528 | error = in_pcbconnect(inp, addr); |
a44aeee3 | 529 | splx(s); |
8a2f82db SL |
530 | if (error == 0) |
531 | soisconnected(so); | |
1bd53abe BJ |
532 | break; |
533 | ||
4945768c SL |
534 | case PRU_CONNECT2: |
535 | error = EOPNOTSUPP; | |
536 | break; | |
537 | ||
2b4b57cd | 538 | case PRU_ACCEPT: |
a3fa431a SL |
539 | error = EOPNOTSUPP; |
540 | break; | |
2b4b57cd | 541 | |
53a5409e | 542 | case PRU_DISCONNECT: |
a3fa431a SL |
543 | if (inp->inp_faddr.s_addr == INADDR_ANY) { |
544 | error = ENOTCONN; | |
545 | break; | |
546 | } | |
a44aeee3 | 547 | s = splnet(); |
405c9168 | 548 | in_pcbdisconnect(inp); |
0dfbe263 | 549 | inp->inp_laddr.s_addr = INADDR_ANY; |
a44aeee3 | 550 | splx(s); |
f96a0781 | 551 | so->so_state &= ~SS_ISCONNECTED; /* XXX */ |
1bd53abe BJ |
552 | break; |
553 | ||
cdad2eb1 BJ |
554 | case PRU_SHUTDOWN: |
555 | socantsendmore(so); | |
556 | break; | |
557 | ||
a942fe02 | 558 | case PRU_SEND: |
5921352b | 559 | return (udp_output(inp, m, addr, control)); |
1bd53abe BJ |
560 | |
561 | case PRU_ABORT: | |
53a5409e | 562 | soisdisconnected(so); |
a44aeee3 | 563 | udp_detach(inp); |
1bd53abe BJ |
564 | break; |
565 | ||
126472ab | 566 | case PRU_SOCKADDR: |
a942fe02 | 567 | in_setsockaddr(inp, addr); |
126472ab SL |
568 | break; |
569 | ||
a7343092 | 570 | case PRU_PEERADDR: |
a942fe02 | 571 | in_setpeeraddr(inp, addr); |
a7343092 SL |
572 | break; |
573 | ||
74040e68 MK |
574 | case PRU_SENSE: |
575 | /* | |
576 | * stat: don't bother with a blocksize. | |
577 | */ | |
578 | return (0); | |
579 | ||
11d2a668 SL |
580 | case PRU_SENDOOB: |
581 | case PRU_FASTTIMO: | |
582 | case PRU_SLOWTIMO: | |
583 | case PRU_PROTORCV: | |
584 | case PRU_PROTOSEND: | |
585 | error = EOPNOTSUPP; | |
586 | break; | |
4945768c | 587 | |
5f55b8b5 MK |
588 | case PRU_RCVD: |
589 | case PRU_RCVOOB: | |
590 | return (EOPNOTSUPP); /* do not free mbuf's */ | |
591 | ||
4945768c SL |
592 | default: |
593 | panic("udp_usrreq"); | |
d52566dd | 594 | } |
5921352b | 595 | |
a3fa431a | 596 | release: |
a942fe02 MK |
597 | if (control) { |
598 | printf("udp control data unexpectedly retained\n"); | |
599 | m_freem(control); | |
600 | } | |
601 | if (m) | |
a3fa431a | 602 | m_freem(m); |
8a2f82db | 603 | return (error); |
d52566dd | 604 | } |
a44aeee3 | 605 | |
c46785cb | 606 | static void |
a44aeee3 MK |
607 | udp_detach(inp) |
608 | struct inpcb *inp; | |
609 | { | |
610 | int s = splnet(); | |
611 | ||
612 | if (inp == udp_last_inpcb) | |
613 | udp_last_inpcb = &udb; | |
614 | in_pcbdetach(inp); | |
615 | splx(s); | |
616 | } | |
5082da2b KM |
617 | |
618 | /* | |
619 | * Sysctl for udp variables. | |
620 | */ | |
621 | udp_sysctl(name, namelen, oldp, oldlenp, newp, newlen) | |
622 | int *name; | |
623 | u_int namelen; | |
624 | void *oldp; | |
625 | size_t *oldlenp; | |
626 | void *newp; | |
627 | size_t newlen; | |
628 | { | |
67e8af50 | 629 | /* All sysctl names at this level are terminal. */ |
5082da2b KM |
630 | if (namelen != 1) |
631 | return (ENOTDIR); | |
632 | ||
633 | switch (name[0]) { | |
634 | case UDPCTL_CHECKSUM: | |
635 | return (sysctl_int(oldp, oldlenp, newp, newlen, &udpcksum)); | |
636 | default: | |
637 | return (ENOPROTOOPT); | |
638 | } | |
639 | /* NOTREACHED */ | |
640 | } |