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