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