xref: /original-bsd/sys/netinet/udp_usrreq.c (revision 8583c8cb)
1 /*	udp_usrreq.c	4.20	82/02/27	*/
2 
3 #include "../h/param.h"
4 #include "../h/dir.h"
5 #include "../h/user.h"
6 #include "../h/mbuf.h"
7 #include "../h/protosw.h"
8 #include "../h/socket.h"
9 #include "../h/socketvar.h"
10 #include "../net/in.h"
11 #include "../net/in_pcb.h"
12 #include "../net/in_systm.h"
13 #include "../net/ip.h"
14 #include "../net/ip_var.h"
15 #include "../net/udp.h"
16 #include "../net/udp_var.h"
17 
18 /*
19  * UDP protocol implementation.
20  * Per RFC 768, August, 1980.
21  */
22 udp_init()
23 {
24 
25 COUNT(UDP_INIT);
26 	udb.inp_next = udb.inp_prev = &udb;
27 }
28 
29 int	udpcksum;
30 struct	sockaddr_in udp_in = { AF_INET };
31 
32 udp_input(m0)
33 	struct mbuf *m0;
34 {
35 	register struct udpiphdr *ui;
36 	register struct inpcb *inp;
37 	register struct mbuf *m;
38 	int len, ulen;
39 
40 COUNT(UDP_INPUT);
41 	/*
42 	 * Get IP and UDP header together in first mbuf.
43 	 */
44 	m = m0;
45 	if ((m->m_off > MMAXOFF || m->m_len < sizeof (struct udpiphdr)) &&
46 	    (m = m_pullup(m, sizeof (struct udpiphdr))) == 0) {
47 		udpstat.udps_hdrops++;
48 		return;
49 	}
50 	ui = mtod(m, struct udpiphdr *);
51 	if (((struct ip *)ui)->ip_hl > (sizeof (struct ip) >> 2))
52 		ip_stripoptions((struct ip *)ui, (struct mbuf *)0);
53 
54 	/*
55 	 * Make mbuf data length reflect UDP length.
56 	 * If not enough data to reflect UDP length, drop.
57 	 */
58 	ulen = ntohs((u_short)ui->ui_ulen);
59 	len = sizeof (struct udphdr) + ulen;
60 	if (((struct ip *)ui)->ip_len != len) {
61 		if (len > ((struct ip *)ui)->ip_len) {
62 			udpstat.udps_badlen++;
63 			goto bad;
64 		}
65 		m_adj(m, ((struct ip *)ui)->ip_len - len);
66 		/* (struct ip *)ui->ip_len = len; */
67 	}
68 
69 	/*
70 	 * Checksum extended UDP header and data.
71 	 */
72 	if (udpcksum) {
73 		ui->ui_next = ui->ui_prev = 0;
74 		ui->ui_x1 = 0;
75 		ui->ui_len = htons((u_short)(sizeof (struct udphdr) + ulen));
76 		if (ui->ui_sum = in_cksum(m, len)) {
77 			udpstat.udps_badsum++;
78 			printf("udp cksum %x\n", ui->ui_sum);
79 			m_freem(m);
80 			return;
81 		}
82 	}
83 
84 	/*
85 	 * Locate pcb for datagram.  On wildcard match, update
86 	 * control block to anchor network and host address.
87 	 */
88 	inp = in_pcblookup(&udb,
89 	    ui->ui_src, ui->ui_sport, ui->ui_dst, ui->ui_dport, 1);
90 	if (inp == 0)
91 		goto bad;
92 
93 	/*
94 	 * Construct sockaddr format source address.
95 	 * Stuff source address and datagram in user buffer.
96 	 */
97 	udp_in.sin_port = ui->ui_sport;
98 	udp_in.sin_addr = ui->ui_src;
99 	m->m_len -= sizeof (struct udpiphdr);
100 	m->m_off += sizeof (struct udpiphdr);
101 	if (sbappendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in, m) == 0)
102 		goto bad;
103 	sorwakeup(inp->inp_socket);
104 	return;
105 bad:
106 	m_freem(m);
107 }
108 
109 udp_ctlinput(m)
110 	struct mbuf *m;
111 {
112 
113 COUNT(UDP_CTLINPUT);
114 	m_freem(m);
115 }
116 
117 udp_output(inp, m0)
118 	struct inpcb *inp;
119 	struct mbuf *m0;
120 {
121 	register struct mbuf *m;
122 	register struct udpiphdr *ui;
123 	register int len = 0;
124 
125 COUNT(UDP_OUTPUT);
126 	/*
127 	 * Calculate data length and get a mbuf
128 	 * for UDP and IP headers.
129 	 */
130 	for (m = m0; m; m = m->m_next)
131 		len += m->m_len;
132 	m = m_get(M_DONTWAIT);
133 	if (m == 0)
134 		goto bad;
135 
136 	/*
137 	 * Fill in mbuf with extended UDP header
138 	 * and addresses and length put into network format.
139 	 */
140 	m->m_off = MMAXOFF - sizeof (struct udpiphdr);
141 	m->m_len = sizeof (struct udpiphdr);
142 	m->m_next = m0;
143 	ui = mtod(m, struct udpiphdr *);
144 	ui->ui_next = ui->ui_prev = 0;
145 	ui->ui_x1 = 0;
146 	ui->ui_pr = IPPROTO_UDP;
147 	ui->ui_len = sizeof (struct udpiphdr) + len;
148 	ui->ui_src = inp->inp_laddr;
149 	ui->ui_dst = inp->inp_faddr;
150 	ui->ui_sport = inp->inp_lport;
151 	ui->ui_dport = inp->inp_fport;
152 	ui->ui_ulen = htons((u_short)len);
153 
154 	/*
155 	 * Stuff checksum and output datagram.
156 	 */
157 	ui->ui_sum = 0;
158 	ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len);
159 	((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
160 	((struct ip *)ui)->ip_ttl = MAXTTL;
161 	(void) ip_output(m, (struct mbuf *)0);
162 	return;
163 bad:
164 	m_freem(m);
165 }
166 
167 udp_usrreq(so, req, m, addr)
168 	struct socket *so;
169 	int req;
170 	struct mbuf *m;
171 	caddr_t addr;
172 {
173 	struct inpcb *inp = sotoinpcb(so);
174 	int error;
175 
176 COUNT(UDP_USRREQ);
177 	if (inp == 0 && req != PRU_ATTACH)
178 		return (EINVAL);
179 	switch (req) {
180 
181 	case PRU_ATTACH:
182 		if (inp != 0)
183 			return (EINVAL);
184 		error = in_pcbattach(so, &udb, 2048, 2048, (struct sockaddr_in *)addr);
185 		if (error)
186 			return (error);
187 		break;
188 
189 	case PRU_DETACH:
190 		if (inp == 0)
191 			return (ENOTCONN);
192 		in_pcbdetach(inp);
193 		break;
194 
195 	case PRU_CONNECT:
196 		if (inp->inp_faddr.s_addr)
197 			return (EISCONN);
198 		error = in_pcbconnect(inp, (struct sockaddr_in *)addr);
199 		if (error)
200 			return (error);
201 		soisconnected(so);
202 		break;
203 
204 	case PRU_ACCEPT:
205 		return (EOPNOTSUPP);
206 
207 	case PRU_DISCONNECT:
208 		if (inp->inp_faddr.s_addr == 0)
209 			return (ENOTCONN);
210 		in_pcbdisconnect(inp);
211 		soisdisconnected(so);
212 		break;
213 
214 	case PRU_SHUTDOWN:
215 		socantsendmore(so);
216 		break;
217 
218 	case PRU_SEND: {
219 		struct in_addr laddr;
220 
221 		if (addr) {
222 			laddr = inp->inp_laddr;
223 			if (inp->inp_faddr.s_addr)
224 				return (EISCONN);
225 			error = in_pcbconnect(inp, (struct sockaddr_in *)addr);
226 			if (error)
227 				return (error);
228 		} else {
229 			if (inp->inp_faddr.s_addr == 0)
230 				return (ENOTCONN);
231 		}
232 		udp_output(inp, m);
233 		if (addr) {
234 			in_pcbdisconnect(inp);
235 			inp->inp_laddr = laddr;
236 		}
237 		}
238 		break;
239 
240 	case PRU_ABORT:
241 		in_pcbdetach(inp);
242 		sofree(so);
243 		soisdisconnected(so);
244 		break;
245 
246 	case PRU_CONTROL:
247 		return (EOPNOTSUPP);
248 
249 	default:
250 		panic("udp_usrreq");
251 	}
252 	return (0);
253 }
254