udp_usrreq.c revision 1.8 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.8 1994/01/08 23:19:48 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 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT);
267 return;
268 }
269
270 /*
271 * Construct sockaddr format source address.
272 * Stuff source address and datagram in user buffer.
273 */
274 udp_in.sin_port = uh->uh_sport;
275 udp_in.sin_addr = ip->ip_src;
276 if (inp->inp_flags & INP_CONTROLOPTS) {
277 struct mbuf **mp = &opts;
278
279 if (inp->inp_flags & INP_RECVDSTADDR) {
280 *mp = udp_saveopt((caddr_t) &ip->ip_dst,
281 sizeof(struct in_addr), IP_RECVDSTADDR);
282 if (*mp)
283 mp = &(*mp)->m_next;
284 }
285 #ifdef notyet
286 /* options were tossed above */
287 if (inp->inp_flags & INP_RECVOPTS) {
288 *mp = udp_saveopt((caddr_t) opts_deleted_above,
289 sizeof(struct in_addr), IP_RECVOPTS);
290 if (*mp)
291 mp = &(*mp)->m_next;
292 }
293 /* ip_srcroute doesn't do what we want here, need to fix */
294 if (inp->inp_flags & INP_RECVRETOPTS) {
295 *mp = udp_saveopt((caddr_t) ip_srcroute(),
296 sizeof(struct in_addr), IP_RECVRETOPTS);
297 if (*mp)
298 mp = &(*mp)->m_next;
299 }
300 #endif
301 }
302 iphlen += sizeof(struct udphdr);
303 m->m_len -= iphlen;
304 m->m_pkthdr.len -= iphlen;
305 m->m_data += iphlen;
306 if (sbappendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in,
307 m, opts) == 0) {
308 udpstat.udps_fullsock++;
309 goto bad;
310 }
311 sorwakeup(inp->inp_socket);
312 return;
313 bad:
314 m_freem(m);
315 if (opts)
316 m_freem(opts);
317 }
318
319 /*
320 * Create a "control" mbuf containing the specified data
321 * with the specified type for presentation with a datagram.
322 */
323 static struct mbuf *
324 udp_saveopt(p, size, type)
325 caddr_t p;
326 register int size;
327 int type;
328 {
329 register struct cmsghdr *cp;
330 struct mbuf *m;
331
332 if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
333 return ((struct mbuf *) NULL);
334 cp = (struct cmsghdr *) mtod(m, struct cmsghdr *);
335 bcopy(p, (caddr_t)(cp + 1), size);
336 size += sizeof(*cp);
337 m->m_len = size;
338 cp->cmsg_len = size;
339 cp->cmsg_level = IPPROTO_IP;
340 cp->cmsg_type = type;
341 return (m);
342 }
343
344 /*
345 * Notify a udp user of an asynchronous error;
346 * just wake up so that he can collect error status.
347 */
348 static void
349 udp_notify(inp, errno)
350 register struct inpcb *inp;
351 int errno;
352 {
353
354 inp->inp_socket->so_error = errno;
355 sorwakeup(inp->inp_socket);
356 sowwakeup(inp->inp_socket);
357 }
358
359 void
360 udp_ctlinput(cmd, sa, ip)
361 int cmd;
362 struct sockaddr *sa;
363 register struct ip *ip;
364 {
365 register struct udphdr *uh;
366 extern struct in_addr zeroin_addr;
367 extern u_char inetctlerrmap[];
368
369 if ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0)
370 return;
371 if (ip) {
372 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
373 in_pcbnotify(&udb, sa, uh->uh_dport, ip->ip_src, uh->uh_sport,
374 cmd, udp_notify);
375 } else
376 in_pcbnotify(&udb, sa, 0, zeroin_addr, 0, cmd, udp_notify);
377 }
378
379 int
380 udp_output(inp, m, addr, control)
381 register struct inpcb *inp;
382 register struct mbuf *m;
383 struct mbuf *addr, *control;
384 {
385 register struct udpiphdr *ui;
386 register int len = m->m_pkthdr.len;
387 struct in_addr laddr;
388 int s, error = 0;
389
390 if (control)
391 m_freem(control); /* XXX */
392
393 if (addr) {
394 laddr = inp->inp_laddr;
395 if (inp->inp_faddr.s_addr != INADDR_ANY) {
396 error = EISCONN;
397 goto release;
398 }
399 /*
400 * Must block input while temporarily connected.
401 */
402 s = splnet();
403 error = in_pcbconnect(inp, addr);
404 if (error) {
405 splx(s);
406 goto release;
407 }
408 } else {
409 if (inp->inp_faddr.s_addr == INADDR_ANY) {
410 error = ENOTCONN;
411 goto release;
412 }
413 }
414 /*
415 * Calculate data length and get a mbuf
416 * for UDP and IP headers.
417 */
418 M_PREPEND(m, sizeof(struct udpiphdr), M_WAIT);
419
420 /*
421 * Fill in mbuf with extended UDP header
422 * and addresses and length put into network format.
423 */
424 ui = mtod(m, struct udpiphdr *);
425 ui->ui_next = ui->ui_prev = 0;
426 ui->ui_x1 = 0;
427 ui->ui_pr = IPPROTO_UDP;
428 ui->ui_len = htons((u_short)len + sizeof (struct udphdr));
429 ui->ui_src = inp->inp_laddr;
430 ui->ui_dst = inp->inp_faddr;
431 ui->ui_sport = inp->inp_lport;
432 ui->ui_dport = inp->inp_fport;
433 ui->ui_ulen = ui->ui_len;
434
435 /*
436 * Stuff checksum and output datagram.
437 */
438 ui->ui_sum = 0;
439 if (udpcksum) {
440 if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0)
441 ui->ui_sum = 0xffff;
442 }
443 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
444 ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
445 ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
446 udpstat.udps_opackets++;
447 error = ip_output(m, inp->inp_options, &inp->inp_route,
448 inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)
449 #ifdef MULTICAST
450 | IP_MULTICASTOPTS, inp->inp_moptions
451 #endif
452 );
453
454 if (addr) {
455 in_pcbdisconnect(inp);
456 inp->inp_laddr = laddr;
457 splx(s);
458 }
459 return (error);
460
461 release:
462 m_freem(m);
463 return (error);
464 }
465
466 u_long udp_sendspace = 9216; /* really max datagram size */
467 u_long udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
468 /* 40 1K datagrams */
469
470 /*ARGSUSED*/
471 int
472 udp_usrreq(so, req, m, addr, control)
473 struct socket *so;
474 int req;
475 struct mbuf *m, *addr, *control;
476 {
477 struct inpcb *inp = sotoinpcb(so);
478 int error = 0;
479 int s;
480
481 if (req == PRU_CONTROL)
482 return (in_control(so, (int)m, (caddr_t)addr,
483 (struct ifnet *)control));
484 if (inp == NULL && req != PRU_ATTACH) {
485 error = EINVAL;
486 goto release;
487 }
488 /*
489 * Note: need to block udp_input while changing
490 * the udp pcb queue and/or pcb addresses.
491 */
492 switch (req) {
493
494 case PRU_ATTACH:
495 if (inp != NULL) {
496 error = EINVAL;
497 break;
498 }
499 s = splnet();
500 error = in_pcballoc(so, &udb);
501 splx(s);
502 if (error)
503 break;
504 error = soreserve(so, udp_sendspace, udp_recvspace);
505 if (error)
506 break;
507 ((struct inpcb *) so->so_pcb)->inp_ip.ip_ttl = udp_ttl;
508 break;
509
510 case PRU_DETACH:
511 udp_detach(inp);
512 break;
513
514 case PRU_BIND:
515 s = splnet();
516 error = in_pcbbind(inp, addr);
517 splx(s);
518 break;
519
520 case PRU_LISTEN:
521 error = EOPNOTSUPP;
522 break;
523
524 case PRU_CONNECT:
525 if (inp->inp_faddr.s_addr != INADDR_ANY) {
526 error = EISCONN;
527 break;
528 }
529 s = splnet();
530 error = in_pcbconnect(inp, addr);
531 splx(s);
532 if (error == 0)
533 soisconnected(so);
534 break;
535
536 case PRU_CONNECT2:
537 error = EOPNOTSUPP;
538 break;
539
540 case PRU_ACCEPT:
541 error = EOPNOTSUPP;
542 break;
543
544 case PRU_DISCONNECT:
545 if (inp->inp_faddr.s_addr == INADDR_ANY) {
546 error = ENOTCONN;
547 break;
548 }
549 s = splnet();
550 in_pcbdisconnect(inp);
551 inp->inp_laddr.s_addr = INADDR_ANY;
552 splx(s);
553 so->so_state &= ~SS_ISCONNECTED; /* XXX */
554 break;
555
556 case PRU_SHUTDOWN:
557 socantsendmore(so);
558 break;
559
560 case PRU_SEND:
561 return (udp_output(inp, m, addr, control));
562
563 case PRU_ABORT:
564 soisdisconnected(so);
565 udp_detach(inp);
566 break;
567
568 case PRU_SOCKADDR:
569 in_setsockaddr(inp, addr);
570 break;
571
572 case PRU_PEERADDR:
573 in_setpeeraddr(inp, addr);
574 break;
575
576 case PRU_SENSE:
577 /*
578 * stat: don't bother with a blocksize.
579 */
580 return (0);
581
582 case PRU_SENDOOB:
583 case PRU_FASTTIMO:
584 case PRU_SLOWTIMO:
585 case PRU_PROTORCV:
586 case PRU_PROTOSEND:
587 error = EOPNOTSUPP;
588 break;
589
590 case PRU_RCVD:
591 case PRU_RCVOOB:
592 return (EOPNOTSUPP); /* do not free mbuf's */
593
594 default:
595 panic("udp_usrreq");
596 }
597
598 release:
599 if (control) {
600 printf("udp control data unexpectedly retained\n");
601 m_freem(control);
602 }
603 if (m)
604 m_freem(m);
605 return (error);
606 }
607
608 static void
609 udp_detach(inp)
610 struct inpcb *inp;
611 {
612 int s = splnet();
613
614 if (inp == udp_last_inpcb)
615 udp_last_inpcb = &udb;
616 in_pcbdetach(inp);
617 splx(s);
618 }
619