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