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