Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | /* |
2 | * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California. | |
3 | * 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 | * | |
38e82238 | 33 | * from: @(#)udp_usrreq.c 7.20 (Berkeley) 4/20/91 |
fde1aeb2 | 34 | * $Id: udp_usrreq.c,v 1.5 1993/11/25 01:35:22 wollman Exp $ |
15637ed4 RG |
35 | */ |
36 | ||
37 | #include "param.h" | |
fde1aeb2 | 38 | #include "systm.h" |
15637ed4 RG |
39 | #include "malloc.h" |
40 | #include "mbuf.h" | |
41 | #include "protosw.h" | |
42 | #include "socket.h" | |
43 | #include "socketvar.h" | |
44 | #include "stat.h" | |
45 | ||
46 | #include "../net/if.h" | |
47 | #include "../net/route.h" | |
48 | ||
49 | #include "in.h" | |
50 | #include "in_systm.h" | |
fde1aeb2 | 51 | #include "in_var.h" |
15637ed4 RG |
52 | #include "ip.h" |
53 | #include "in_pcb.h" | |
54 | #include "ip_var.h" | |
55 | #include "ip_icmp.h" | |
56 | #include "udp.h" | |
57 | #include "udp_var.h" | |
58 | ||
fde1aeb2 GW |
59 | static struct inpcb udb; |
60 | static struct inpcb *udp_last_inpcb = &udb; | |
15637ed4 | 61 | |
4c45483e GW |
62 | static void udp_detach(struct inpcb *); |
63 | ||
15637ed4 RG |
64 | /* |
65 | * UDP protocol implementation. | |
66 | * Per RFC 768, August, 1980. | |
67 | */ | |
4c45483e | 68 | void |
15637ed4 RG |
69 | udp_init() |
70 | { | |
71 | ||
72 | udb.inp_next = udb.inp_prev = &udb; | |
73 | } | |
74 | ||
fde1aeb2 | 75 | static struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET }; |
15637ed4 | 76 | |
4c45483e | 77 | void |
15637ed4 RG |
78 | udp_input(m, iphlen) |
79 | register struct mbuf *m; | |
80 | int iphlen; | |
81 | { | |
82 | register struct ip *ip; | |
83 | register struct udphdr *uh; | |
84 | register struct inpcb *inp; | |
85 | struct mbuf *opts = 0; | |
86 | int len; | |
87 | struct ip save_ip; | |
88 | ||
89 | udpstat.udps_ipackets++; | |
90 | ||
91 | /* | |
92 | * Strip IP options, if any; should skip this, | |
93 | * make available to user, and use on returned packets, | |
94 | * but we don't yet have a way to check the checksum | |
95 | * with options still present. | |
96 | */ | |
97 | if (iphlen > sizeof (struct ip)) { | |
98 | ip_stripoptions(m, (struct mbuf *)0); | |
99 | iphlen = sizeof(struct ip); | |
100 | } | |
101 | ||
102 | /* | |
103 | * Get IP and UDP header together in first mbuf. | |
104 | */ | |
105 | ip = mtod(m, struct ip *); | |
106 | if (m->m_len < iphlen + sizeof(struct udphdr)) { | |
107 | if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) { | |
108 | udpstat.udps_hdrops++; | |
109 | return; | |
110 | } | |
111 | ip = mtod(m, struct ip *); | |
112 | } | |
113 | uh = (struct udphdr *)((caddr_t)ip + iphlen); | |
114 | ||
115 | /* | |
116 | * Make mbuf data length reflect UDP length. | |
117 | * If not enough data to reflect UDP length, drop. | |
118 | */ | |
119 | len = ntohs((u_short)uh->uh_ulen); | |
120 | if (ip->ip_len != len) { | |
121 | if (len > ip->ip_len) { | |
122 | udpstat.udps_badlen++; | |
123 | goto bad; | |
124 | } | |
125 | m_adj(m, len - ip->ip_len); | |
126 | /* ip->ip_len = len; */ | |
127 | } | |
128 | /* | |
129 | * Save a copy of the IP header in case we want restore it | |
130 | * for sending an ICMP error message in response. | |
131 | */ | |
132 | save_ip = *ip; | |
133 | ||
134 | /* | |
135 | * Checksum extended UDP header and data. | |
136 | */ | |
137 | if (udpcksum && uh->uh_sum) { | |
138 | ((struct ipovly *)ip)->ih_next = 0; | |
139 | ((struct ipovly *)ip)->ih_prev = 0; | |
140 | ((struct ipovly *)ip)->ih_x1 = 0; | |
141 | ((struct ipovly *)ip)->ih_len = uh->uh_ulen; | |
142 | if (uh->uh_sum = in_cksum(m, len + sizeof (struct ip))) { | |
143 | udpstat.udps_badsum++; | |
144 | m_freem(m); | |
145 | return; | |
146 | } | |
147 | } | |
148 | ||
149 | /* | |
150 | * Locate pcb for datagram. | |
151 | */ | |
152 | inp = udp_last_inpcb; | |
153 | if (inp->inp_lport != uh->uh_dport || | |
154 | inp->inp_fport != uh->uh_sport || | |
155 | inp->inp_faddr.s_addr != ip->ip_src.s_addr || | |
156 | inp->inp_laddr.s_addr != ip->ip_dst.s_addr) { | |
157 | inp = in_pcblookup(&udb, ip->ip_src, uh->uh_sport, | |
158 | ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD); | |
159 | if (inp) | |
160 | udp_last_inpcb = inp; | |
161 | udpstat.udpps_pcbcachemiss++; | |
162 | } | |
163 | if (inp == 0) { | |
164 | /* don't send ICMP response for broadcast packet */ | |
165 | udpstat.udps_noport++; | |
166 | if (m->m_flags & M_BCAST) { | |
167 | udpstat.udps_noportbcast++; | |
168 | goto bad; | |
169 | } | |
170 | *ip = save_ip; | |
171 | ip->ip_len += iphlen; | |
2cb63509 GW |
172 | { |
173 | static struct in_addr fake; | |
174 | icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, fake, 0); | |
175 | } | |
15637ed4 RG |
176 | return; |
177 | } | |
178 | ||
179 | /* | |
180 | * Construct sockaddr format source address. | |
181 | * Stuff source address and datagram in user buffer. | |
182 | */ | |
183 | udp_in.sin_port = uh->uh_sport; | |
184 | udp_in.sin_addr = ip->ip_src; | |
185 | if (inp->inp_flags & INP_CONTROLOPTS) { | |
186 | struct mbuf **mp = &opts; | |
187 | struct mbuf *udp_saveopt(); | |
188 | ||
189 | if (inp->inp_flags & INP_RECVDSTADDR) { | |
190 | *mp = udp_saveopt((caddr_t) &ip->ip_dst, | |
191 | sizeof(struct in_addr), IP_RECVDSTADDR); | |
192 | if (*mp) | |
193 | mp = &(*mp)->m_next; | |
194 | } | |
195 | #ifdef notyet | |
196 | /* options were tossed above */ | |
197 | if (inp->inp_flags & INP_RECVOPTS) { | |
198 | *mp = udp_saveopt((caddr_t) opts_deleted_above, | |
199 | sizeof(struct in_addr), IP_RECVOPTS); | |
200 | if (*mp) | |
201 | mp = &(*mp)->m_next; | |
202 | } | |
203 | /* ip_srcroute doesn't do what we want here, need to fix */ | |
204 | if (inp->inp_flags & INP_RECVRETOPTS) { | |
205 | *mp = udp_saveopt((caddr_t) ip_srcroute(), | |
206 | sizeof(struct in_addr), IP_RECVRETOPTS); | |
207 | if (*mp) | |
208 | mp = &(*mp)->m_next; | |
209 | } | |
210 | #endif | |
211 | } | |
212 | iphlen += sizeof(struct udphdr); | |
213 | m->m_len -= iphlen; | |
214 | m->m_pkthdr.len -= iphlen; | |
215 | m->m_data += iphlen; | |
216 | if (sbappendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in, | |
217 | m, opts) == 0) { | |
218 | udpstat.udps_fullsock++; | |
219 | goto bad; | |
220 | } | |
221 | sorwakeup(inp->inp_socket); | |
222 | return; | |
223 | bad: | |
224 | m_freem(m); | |
225 | if (opts) | |
226 | m_freem(opts); | |
227 | } | |
228 | ||
229 | /* | |
230 | * Create a "control" mbuf containing the specified data | |
231 | * with the specified type for presentation with a datagram. | |
232 | */ | |
233 | struct mbuf * | |
234 | udp_saveopt(p, size, type) | |
235 | caddr_t p; | |
236 | register int size; | |
237 | int type; | |
238 | { | |
239 | register struct cmsghdr *cp; | |
240 | struct mbuf *m; | |
241 | ||
242 | if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL) | |
243 | return ((struct mbuf *) NULL); | |
244 | cp = (struct cmsghdr *) mtod(m, struct cmsghdr *); | |
245 | bcopy(p, (caddr_t)(cp + 1), size); | |
246 | size += sizeof(*cp); | |
247 | m->m_len = size; | |
248 | cp->cmsg_len = size; | |
249 | cp->cmsg_level = IPPROTO_IP; | |
250 | cp->cmsg_type = type; | |
251 | return (m); | |
252 | } | |
253 | ||
254 | /* | |
255 | * Notify a udp user of an asynchronous error; | |
256 | * just wake up so that he can collect error status. | |
257 | */ | |
4c45483e | 258 | void |
15637ed4 RG |
259 | udp_notify(inp, errno) |
260 | register struct inpcb *inp; | |
4c45483e | 261 | int errno; |
15637ed4 RG |
262 | { |
263 | ||
264 | inp->inp_socket->so_error = errno; | |
265 | sorwakeup(inp->inp_socket); | |
266 | sowwakeup(inp->inp_socket); | |
267 | } | |
268 | ||
4c45483e | 269 | void |
15637ed4 RG |
270 | udp_ctlinput(cmd, sa, ip) |
271 | int cmd; | |
272 | struct sockaddr *sa; | |
273 | register struct ip *ip; | |
274 | { | |
275 | register struct udphdr *uh; | |
276 | extern struct in_addr zeroin_addr; | |
277 | extern u_char inetctlerrmap[]; | |
278 | ||
279 | if ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0) | |
280 | return; | |
281 | if (ip) { | |
282 | uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); | |
283 | in_pcbnotify(&udb, sa, uh->uh_dport, ip->ip_src, uh->uh_sport, | |
284 | cmd, udp_notify); | |
285 | } else | |
286 | in_pcbnotify(&udb, sa, 0, zeroin_addr, 0, cmd, udp_notify); | |
287 | } | |
288 | ||
4c45483e | 289 | int |
15637ed4 RG |
290 | udp_output(inp, m, addr, control) |
291 | register struct inpcb *inp; | |
292 | register struct mbuf *m; | |
293 | struct mbuf *addr, *control; | |
294 | { | |
295 | register struct udpiphdr *ui; | |
296 | register int len = m->m_pkthdr.len; | |
297 | struct in_addr laddr; | |
4c45483e | 298 | int s = 0, error = 0; |
15637ed4 RG |
299 | |
300 | if (control) | |
301 | m_freem(control); /* XXX */ | |
302 | ||
303 | if (addr) { | |
304 | laddr = inp->inp_laddr; | |
305 | if (inp->inp_faddr.s_addr != INADDR_ANY) { | |
306 | error = EISCONN; | |
307 | goto release; | |
308 | } | |
309 | /* | |
310 | * Must block input while temporarily connected. | |
311 | */ | |
312 | s = splnet(); | |
313 | error = in_pcbconnect(inp, addr); | |
314 | if (error) { | |
315 | splx(s); | |
316 | goto release; | |
317 | } | |
318 | } else { | |
319 | if (inp->inp_faddr.s_addr == INADDR_ANY) { | |
320 | error = ENOTCONN; | |
321 | goto release; | |
322 | } | |
323 | } | |
324 | /* | |
325 | * Calculate data length and get a mbuf | |
326 | * for UDP and IP headers. | |
327 | */ | |
328 | M_PREPEND(m, sizeof(struct udpiphdr), M_WAIT); | |
329 | ||
330 | /* | |
331 | * Fill in mbuf with extended UDP header | |
332 | * and addresses and length put into network format. | |
333 | */ | |
334 | ui = mtod(m, struct udpiphdr *); | |
335 | ui->ui_next = ui->ui_prev = 0; | |
336 | ui->ui_x1 = 0; | |
337 | ui->ui_pr = IPPROTO_UDP; | |
338 | ui->ui_len = htons((u_short)len + sizeof (struct udphdr)); | |
339 | ui->ui_src = inp->inp_laddr; | |
340 | ui->ui_dst = inp->inp_faddr; | |
341 | ui->ui_sport = inp->inp_lport; | |
342 | ui->ui_dport = inp->inp_fport; | |
343 | ui->ui_ulen = ui->ui_len; | |
344 | ||
345 | /* | |
346 | * Stuff checksum and output datagram. | |
347 | */ | |
348 | ui->ui_sum = 0; | |
349 | if (udpcksum) { | |
350 | if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0) | |
351 | ui->ui_sum = 0xffff; | |
352 | } | |
353 | ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; | |
354 | ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */ | |
355 | ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */ | |
356 | udpstat.udps_opackets++; | |
357 | error = ip_output(m, inp->inp_options, &inp->inp_route, | |
358 | inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)); | |
359 | ||
360 | if (addr) { | |
361 | in_pcbdisconnect(inp); | |
362 | inp->inp_laddr = laddr; | |
363 | splx(s); | |
364 | } | |
365 | return (error); | |
366 | ||
367 | release: | |
368 | m_freem(m); | |
369 | return (error); | |
370 | } | |
371 | ||
15637ed4 | 372 | /*ARGSUSED*/ |
4c45483e | 373 | int |
15637ed4 RG |
374 | udp_usrreq(so, req, m, addr, control) |
375 | struct socket *so; | |
376 | int req; | |
377 | struct mbuf *m, *addr, *control; | |
378 | { | |
379 | struct inpcb *inp = sotoinpcb(so); | |
380 | int error = 0; | |
381 | int s; | |
382 | ||
383 | if (req == PRU_CONTROL) | |
384 | return (in_control(so, (int)m, (caddr_t)addr, | |
385 | (struct ifnet *)control)); | |
386 | if (inp == NULL && req != PRU_ATTACH) { | |
387 | error = EINVAL; | |
388 | goto release; | |
389 | } | |
390 | /* | |
391 | * Note: need to block udp_input while changing | |
392 | * the udp pcb queue and/or pcb addresses. | |
393 | */ | |
394 | switch (req) { | |
395 | ||
396 | case PRU_ATTACH: | |
397 | if (inp != NULL) { | |
398 | error = EINVAL; | |
399 | break; | |
400 | } | |
401 | s = splnet(); | |
402 | error = in_pcballoc(so, &udb); | |
403 | splx(s); | |
404 | if (error) | |
405 | break; | |
406 | error = soreserve(so, udp_sendspace, udp_recvspace); | |
407 | if (error) | |
408 | break; | |
409 | ((struct inpcb *) so->so_pcb)->inp_ip.ip_ttl = udp_ttl; | |
410 | break; | |
411 | ||
412 | case PRU_DETACH: | |
413 | udp_detach(inp); | |
414 | break; | |
415 | ||
416 | case PRU_BIND: | |
417 | s = splnet(); | |
418 | error = in_pcbbind(inp, addr); | |
419 | splx(s); | |
420 | break; | |
421 | ||
422 | case PRU_LISTEN: | |
423 | error = EOPNOTSUPP; | |
424 | break; | |
425 | ||
426 | case PRU_CONNECT: | |
427 | if (inp->inp_faddr.s_addr != INADDR_ANY) { | |
428 | error = EISCONN; | |
429 | break; | |
430 | } | |
431 | s = splnet(); | |
432 | error = in_pcbconnect(inp, addr); | |
433 | splx(s); | |
434 | if (error == 0) | |
435 | soisconnected(so); | |
436 | break; | |
437 | ||
438 | case PRU_CONNECT2: | |
439 | error = EOPNOTSUPP; | |
440 | break; | |
441 | ||
442 | case PRU_ACCEPT: | |
443 | error = EOPNOTSUPP; | |
444 | break; | |
445 | ||
446 | case PRU_DISCONNECT: | |
447 | if (inp->inp_faddr.s_addr == INADDR_ANY) { | |
448 | error = ENOTCONN; | |
449 | break; | |
450 | } | |
451 | s = splnet(); | |
452 | in_pcbdisconnect(inp); | |
453 | inp->inp_laddr.s_addr = INADDR_ANY; | |
454 | splx(s); | |
455 | so->so_state &= ~SS_ISCONNECTED; /* XXX */ | |
456 | break; | |
457 | ||
458 | case PRU_SHUTDOWN: | |
459 | socantsendmore(so); | |
460 | break; | |
461 | ||
462 | case PRU_SEND: | |
463 | return (udp_output(inp, m, addr, control)); | |
464 | ||
465 | case PRU_ABORT: | |
466 | soisdisconnected(so); | |
467 | udp_detach(inp); | |
468 | break; | |
469 | ||
470 | case PRU_SOCKADDR: | |
471 | in_setsockaddr(inp, addr); | |
472 | break; | |
473 | ||
474 | case PRU_PEERADDR: | |
475 | in_setpeeraddr(inp, addr); | |
476 | break; | |
477 | ||
478 | case PRU_SENSE: | |
479 | /* | |
480 | * stat: don't bother with a blocksize. | |
481 | */ | |
482 | return (0); | |
483 | ||
484 | case PRU_SENDOOB: | |
485 | case PRU_FASTTIMO: | |
486 | case PRU_SLOWTIMO: | |
487 | case PRU_PROTORCV: | |
488 | case PRU_PROTOSEND: | |
489 | error = EOPNOTSUPP; | |
490 | break; | |
491 | ||
492 | case PRU_RCVD: | |
493 | case PRU_RCVOOB: | |
494 | return (EOPNOTSUPP); /* do not free mbuf's */ | |
495 | ||
496 | default: | |
497 | panic("udp_usrreq"); | |
498 | } | |
499 | ||
500 | release: | |
501 | if (control) { | |
502 | printf("udp control data unexpectedly retained\n"); | |
503 | m_freem(control); | |
504 | } | |
505 | if (m) | |
506 | m_freem(m); | |
507 | return (error); | |
508 | } | |
509 | ||
4c45483e | 510 | static void |
15637ed4 RG |
511 | udp_detach(inp) |
512 | struct inpcb *inp; | |
513 | { | |
514 | int s = splnet(); | |
515 | ||
516 | if (inp == udp_last_inpcb) | |
517 | udp_last_inpcb = &udb; | |
518 | in_pcbdetach(inp); | |
519 | splx(s); | |
520 | } |