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