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