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