Commit | Line | Data |
---|---|---|
336240bf | 1 | /* udp_usrreq.c 6.13 85/05/28 */ |
20666ad3 JB |
2 | |
3 | #include "param.h" | |
4 | #include "dir.h" | |
5 | #include "user.h" | |
6 | #include "mbuf.h" | |
7 | #include "protosw.h" | |
8 | #include "socket.h" | |
9 | #include "socketvar.h" | |
10 | #include "errno.h" | |
11 | #include "stat.h" | |
f4d55810 | 12 | |
72e4f44e | 13 | #include "../net/if.h" |
c124e997 | 14 | #include "../net/route.h" |
f4d55810 | 15 | |
20666ad3 JB |
16 | #include "in.h" |
17 | #include "in_pcb.h" | |
18 | #include "in_systm.h" | |
19 | #include "ip.h" | |
20 | #include "ip_var.h" | |
21 | #include "ip_icmp.h" | |
22 | #include "udp.h" | |
23 | #include "udp_var.h" | |
d52566dd | 24 | |
2b4b57cd BJ |
25 | /* |
26 | * UDP protocol implementation. | |
27 | * Per RFC 768, August, 1980. | |
28 | */ | |
d52566dd BJ |
29 | udp_init() |
30 | { | |
31 | ||
eb44bfb2 | 32 | udb.inp_next = udb.inp_prev = &udb; |
d52566dd | 33 | } |
1bd53abe | 34 | |
dd8707ae | 35 | int udpcksum = 1; |
2b4b57cd | 36 | struct sockaddr_in udp_in = { AF_INET }; |
eb44bfb2 | 37 | |
2b4b57cd BJ |
38 | udp_input(m0) |
39 | struct mbuf *m0; | |
1bd53abe | 40 | { |
eb44bfb2 | 41 | register struct udpiphdr *ui; |
53a5409e | 42 | register struct inpcb *inp; |
2b4b57cd | 43 | register struct mbuf *m; |
e199a8a5 | 44 | int len; |
eb44bfb2 | 45 | |
2b4b57cd | 46 | /* |
4aed14e3 | 47 | * Get IP and UDP header together in first mbuf. |
2b4b57cd BJ |
48 | */ |
49 | m = m0; | |
89925e89 BJ |
50 | if ((m->m_off > MMAXOFF || m->m_len < sizeof (struct udpiphdr)) && |
51 | (m = m_pullup(m, sizeof (struct udpiphdr))) == 0) { | |
2b4b57cd | 52 | udpstat.udps_hdrops++; |
89925e89 | 53 | return; |
2b4b57cd | 54 | } |
4e60622a | 55 | ui = mtod(m, struct udpiphdr *); |
4aed14e3 | 56 | if (((struct ip *)ui)->ip_hl > (sizeof (struct ip) >> 2)) |
cca1853f | 57 | ip_stripoptions((struct ip *)ui, (struct mbuf *)0); |
2b4b57cd BJ |
58 | |
59 | /* | |
4aed14e3 BJ |
60 | * Make mbuf data length reflect UDP length. |
61 | * If not enough data to reflect UDP length, drop. | |
2b4b57cd | 62 | */ |
e199a8a5 | 63 | len = ntohs((u_short)ui->ui_ulen); |
2b4b57cd BJ |
64 | if (((struct ip *)ui)->ip_len != len) { |
65 | if (len > ((struct ip *)ui)->ip_len) { | |
66 | udpstat.udps_badlen++; | |
67 | goto bad; | |
68 | } | |
4828da7c | 69 | m_adj(m, len - ((struct ip *)ui)->ip_len); |
2b4b57cd BJ |
70 | /* (struct ip *)ui->ip_len = len; */ |
71 | } | |
72 | ||
73 | /* | |
4aed14e3 | 74 | * Checksum extended UDP header and data. |
2b4b57cd | 75 | */ |
4828da7c | 76 | if (udpcksum && ui->ui_sum) { |
2b4b57cd BJ |
77 | ui->ui_next = ui->ui_prev = 0; |
78 | ui->ui_x1 = 0; | |
ee09448f | 79 | ui->ui_len = ui->ui_ulen; |
e199a8a5 | 80 | if (ui->ui_sum = in_cksum(m, len + sizeof (struct ip))) { |
2b4b57cd | 81 | udpstat.udps_badsum++; |
eb44bfb2 BJ |
82 | m_freem(m); |
83 | return; | |
84 | } | |
85 | } | |
2b4b57cd BJ |
86 | |
87 | /* | |
e199a8a5 | 88 | * Locate pcb for datagram. |
2b4b57cd | 89 | */ |
2b4b57cd | 90 | inp = in_pcblookup(&udb, |
ebcadd38 BJ |
91 | ui->ui_src, ui->ui_sport, ui->ui_dst, ui->ui_dport, |
92 | INPLOOKUP_WILDCARD); | |
72e4f44e | 93 | if (inp == 0) { |
5cdc4d65 | 94 | /* don't send ICMP response for broadcast packet */ |
ee09448f | 95 | if (in_broadcast(ui->ui_dst)) |
72e4f44e SL |
96 | goto bad; |
97 | icmp_error((struct ip *)ui, ICMP_UNREACH, ICMP_UNREACH_PORT); | |
98 | return; | |
99 | } | |
2b4b57cd BJ |
100 | |
101 | /* | |
102 | * Construct sockaddr format source address. | |
103 | * Stuff source address and datagram in user buffer. | |
104 | */ | |
105 | udp_in.sin_port = ui->ui_sport; | |
106 | udp_in.sin_addr = ui->ui_src; | |
4e60622a BJ |
107 | m->m_len -= sizeof (struct udpiphdr); |
108 | m->m_off += sizeof (struct udpiphdr); | |
ab85b059 SL |
109 | if (sbappendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in, |
110 | m, (struct mbuf *)0) == 0) | |
2b4b57cd | 111 | goto bad; |
4e60622a | 112 | sorwakeup(inp->inp_socket); |
53a5409e | 113 | return; |
2b4b57cd | 114 | bad: |
53a5409e | 115 | m_freem(m); |
1bd53abe BJ |
116 | } |
117 | ||
72e4f44e SL |
118 | udp_abort(inp) |
119 | struct inpcb *inp; | |
120 | { | |
121 | struct socket *so = inp->inp_socket; | |
122 | ||
123 | in_pcbdisconnect(inp); | |
124 | soisdisconnected(so); | |
1bd53abe BJ |
125 | } |
126 | ||
39674d5f SL |
127 | udp_ctlinput(cmd, arg) |
128 | int cmd; | |
129 | caddr_t arg; | |
130 | { | |
336240bf | 131 | struct in_addr *in; |
39674d5f | 132 | extern u_char inetctlerrmap[]; |
336240bf | 133 | int in_rtchange(); |
39674d5f SL |
134 | |
135 | if (cmd < 0 || cmd > PRC_NCMDS) | |
136 | return; | |
137 | switch (cmd) { | |
138 | ||
139 | case PRC_ROUTEDEAD: | |
140 | break; | |
141 | ||
336240bf MK |
142 | case PRC_REDIRECT_NET: |
143 | case PRC_REDIRECT_HOST: | |
144 | in = &((struct icmp *)arg)->icmp_ip.ip_dst; | |
145 | in_pcbnotify(&udb, in, 0, in_rtchange); | |
39674d5f SL |
146 | break; |
147 | ||
39674d5f | 148 | case PRC_IFDOWN: |
336240bf MK |
149 | in = &((struct sockaddr_in *)arg)->sin_addr; |
150 | goto notify; | |
151 | ||
39674d5f SL |
152 | case PRC_HOSTDEAD: |
153 | case PRC_HOSTUNREACH: | |
336240bf MK |
154 | in = (struct in_addr *)arg; |
155 | goto notify; | |
39674d5f SL |
156 | |
157 | default: | |
336240bf MK |
158 | if (inetctlerrmap[cmd] == 0) |
159 | return; /* XXX */ | |
160 | in = &((struct icmp *)arg)->icmp_ip.ip_dst; | |
161 | notify: | |
162 | in_pcbnotify(&udb, in, (int)inetctlerrmap[cmd], udp_abort); | |
39674d5f SL |
163 | } |
164 | } | |
165 | ||
4ad99bae | 166 | udp_output(inp, m0) |
2b4b57cd | 167 | struct inpcb *inp; |
2b4b57cd | 168 | struct mbuf *m0; |
1bd53abe | 169 | { |
2b4b57cd BJ |
170 | register struct mbuf *m; |
171 | register struct udpiphdr *ui; | |
f553b1f7 MK |
172 | register struct socket *so; |
173 | register int len = 0; | |
27e5eff5 | 174 | register struct route *ro; |
2b4b57cd BJ |
175 | |
176 | /* | |
177 | * Calculate data length and get a mbuf | |
4aed14e3 | 178 | * for UDP and IP headers. |
2b4b57cd BJ |
179 | */ |
180 | for (m = m0; m; m = m->m_next) | |
181 | len += m->m_len; | |
ee09448f | 182 | MGET(m, M_DONTWAIT, MT_HEADER); |
8a2f82db SL |
183 | if (m == 0) { |
184 | m_freem(m0); | |
185 | return (ENOBUFS); | |
186 | } | |
2b4b57cd BJ |
187 | |
188 | /* | |
4aed14e3 | 189 | * Fill in mbuf with extended UDP header |
2b4b57cd BJ |
190 | * and addresses and length put into network format. |
191 | */ | |
192 | m->m_off = MMAXOFF - sizeof (struct udpiphdr); | |
193 | m->m_len = sizeof (struct udpiphdr); | |
194 | m->m_next = m0; | |
195 | ui = mtod(m, struct udpiphdr *); | |
196 | ui->ui_next = ui->ui_prev = 0; | |
197 | ui->ui_x1 = 0; | |
198 | ui->ui_pr = IPPROTO_UDP; | |
ee847abd | 199 | ui->ui_len = htons((u_short)len + sizeof (struct udphdr)); |
4e60622a BJ |
200 | ui->ui_src = inp->inp_laddr; |
201 | ui->ui_dst = inp->inp_faddr; | |
202 | ui->ui_sport = inp->inp_lport; | |
203 | ui->ui_dport = inp->inp_fport; | |
ee847abd | 204 | ui->ui_ulen = ui->ui_len; |
2b4b57cd BJ |
205 | |
206 | /* | |
207 | * Stuff checksum and output datagram. | |
208 | */ | |
209 | ui->ui_sum = 0; | |
dd8707ae MK |
210 | if (udpcksum) { |
211 | if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0) | |
4828da7c | 212 | ui->ui_sum = -1; |
ee09448f | 213 | } |
4e60622a BJ |
214 | ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; |
215 | ((struct ip *)ui)->ip_ttl = MAXTTL; | |
f553b1f7 MK |
216 | so = inp->inp_socket; |
217 | if (so->so_options & SO_DONTROUTE) | |
218 | return (ip_output(m, (struct mbuf *)0, (struct route *)0, | |
219 | (so->so_options & SO_BROADCAST) | IP_ROUTETOIF)); | |
220 | /* | |
221 | * Use cached route for previous datagram if | |
222 | * this is also to the same destination. | |
223 | * | |
224 | * NB: We don't handle broadcasts because that | |
225 | * would require 3 subroutine calls. | |
226 | */ | |
227 | ro = &inp->inp_route; | |
27e5eff5 | 228 | #define satosin(sa) ((struct sockaddr_in *)(sa)) |
f553b1f7 MK |
229 | if (ro->ro_rt && |
230 | satosin(&ro->ro_dst)->sin_addr.s_addr != ui->ui_dst.s_addr) { | |
231 | RTFREE(ro->ro_rt); | |
232 | ro->ro_rt = (struct rtentry *)0; | |
233 | } | |
234 | return (ip_output(m, (struct mbuf *)0, ro, | |
235 | so->so_options & SO_BROADCAST)); | |
1bd53abe BJ |
236 | } |
237 | ||
1d14d351 MK |
238 | int udp_sendspace = 2048; /* really max datagram size */ |
239 | int udp_recvspace = 4 * (1024+sizeof(struct sockaddr_in)); /* 4 1K dgrams */ | |
240 | ||
a8d3bf7f | 241 | /*ARGSUSED*/ |
ab85b059 | 242 | udp_usrreq(so, req, m, nam, rights) |
53a5409e | 243 | struct socket *so; |
1bd53abe | 244 | int req; |
ab85b059 | 245 | struct mbuf *m, *nam, *rights; |
1bd53abe | 246 | { |
53a5409e | 247 | struct inpcb *inp = sotoinpcb(so); |
8a2f82db | 248 | int error = 0; |
1bd53abe | 249 | |
1d14d351 MK |
250 | if (req == PRU_CONTROL) |
251 | return (in_control(so, (int)m, (caddr_t)nam, | |
252 | (struct ifnet *)rights)); | |
ab85b059 SL |
253 | if (rights && rights->m_len) { |
254 | error = EINVAL; | |
255 | goto release; | |
256 | } | |
a3fa431a SL |
257 | if (inp == NULL && req != PRU_ATTACH) { |
258 | error = EINVAL; | |
259 | goto release; | |
260 | } | |
1bd53abe BJ |
261 | switch (req) { |
262 | ||
263 | case PRU_ATTACH: | |
a3fa431a SL |
264 | if (inp != NULL) { |
265 | error = EINVAL; | |
266 | break; | |
267 | } | |
8075bb0e BJ |
268 | error = in_pcballoc(so, &udb); |
269 | if (error) | |
270 | break; | |
1d14d351 | 271 | error = soreserve(so, udp_sendspace, udp_recvspace); |
8075bb0e BJ |
272 | if (error) |
273 | break; | |
53a5409e | 274 | break; |
1bd53abe BJ |
275 | |
276 | case PRU_DETACH: | |
a3fa431a SL |
277 | if (inp == NULL) { |
278 | error = ENOTCONN; | |
279 | break; | |
280 | } | |
405c9168 | 281 | in_pcbdetach(inp); |
53a5409e | 282 | break; |
1bd53abe | 283 | |
8075bb0e BJ |
284 | case PRU_BIND: |
285 | error = in_pcbbind(inp, nam); | |
286 | break; | |
287 | ||
288 | case PRU_LISTEN: | |
289 | error = EOPNOTSUPP; | |
290 | break; | |
291 | ||
1bd53abe | 292 | case PRU_CONNECT: |
a3fa431a SL |
293 | if (inp->inp_faddr.s_addr != INADDR_ANY) { |
294 | error = EISCONN; | |
295 | break; | |
296 | } | |
8075bb0e | 297 | error = in_pcbconnect(inp, nam); |
8a2f82db SL |
298 | if (error == 0) |
299 | soisconnected(so); | |
1bd53abe BJ |
300 | break; |
301 | ||
4945768c SL |
302 | case PRU_CONNECT2: |
303 | error = EOPNOTSUPP; | |
304 | break; | |
305 | ||
2b4b57cd | 306 | case PRU_ACCEPT: |
a3fa431a SL |
307 | error = EOPNOTSUPP; |
308 | break; | |
2b4b57cd | 309 | |
53a5409e | 310 | case PRU_DISCONNECT: |
a3fa431a SL |
311 | if (inp->inp_faddr.s_addr == INADDR_ANY) { |
312 | error = ENOTCONN; | |
313 | break; | |
314 | } | |
405c9168 | 315 | in_pcbdisconnect(inp); |
53a5409e | 316 | soisdisconnected(so); |
1bd53abe BJ |
317 | break; |
318 | ||
cdad2eb1 BJ |
319 | case PRU_SHUTDOWN: |
320 | socantsendmore(so); | |
321 | break; | |
322 | ||
1aa87517 BJ |
323 | case PRU_SEND: { |
324 | struct in_addr laddr; | |
5f55b8b5 | 325 | int s; |
1aa87517 | 326 | |
8075bb0e | 327 | if (nam) { |
1aa87517 | 328 | laddr = inp->inp_laddr; |
a3fa431a SL |
329 | if (inp->inp_faddr.s_addr != INADDR_ANY) { |
330 | error = EISCONN; | |
331 | break; | |
332 | } | |
5f55b8b5 MK |
333 | /* |
334 | * Must block input while temporarily connected. | |
335 | */ | |
336 | s = splnet(); | |
8075bb0e | 337 | error = in_pcbconnect(inp, nam); |
5f55b8b5 MK |
338 | if (error) { |
339 | splx(s); | |
8a2f82db | 340 | break; |
5f55b8b5 | 341 | } |
4ad99bae | 342 | } else { |
a3fa431a SL |
343 | if (inp->inp_faddr.s_addr == INADDR_ANY) { |
344 | error = ENOTCONN; | |
345 | break; | |
346 | } | |
4ad99bae | 347 | } |
8a2f82db | 348 | error = udp_output(inp, m); |
a3fa431a | 349 | m = NULL; |
8075bb0e | 350 | if (nam) { |
405c9168 | 351 | in_pcbdisconnect(inp); |
1aa87517 | 352 | inp->inp_laddr = laddr; |
1d14d351 | 353 | splx(s); |
1aa87517 BJ |
354 | } |
355 | } | |
1bd53abe BJ |
356 | break; |
357 | ||
358 | case PRU_ABORT: | |
405c9168 | 359 | in_pcbdetach(inp); |
53a5409e BJ |
360 | sofree(so); |
361 | soisdisconnected(so); | |
1bd53abe BJ |
362 | break; |
363 | ||
126472ab | 364 | case PRU_SOCKADDR: |
8075bb0e | 365 | in_setsockaddr(inp, nam); |
126472ab SL |
366 | break; |
367 | ||
a7343092 SL |
368 | case PRU_PEERADDR: |
369 | in_setpeeraddr(inp, nam); | |
370 | break; | |
371 | ||
74040e68 MK |
372 | case PRU_SENSE: |
373 | /* | |
374 | * stat: don't bother with a blocksize. | |
375 | */ | |
376 | return (0); | |
377 | ||
11d2a668 SL |
378 | case PRU_SENDOOB: |
379 | case PRU_FASTTIMO: | |
380 | case PRU_SLOWTIMO: | |
381 | case PRU_PROTORCV: | |
382 | case PRU_PROTOSEND: | |
383 | error = EOPNOTSUPP; | |
384 | break; | |
4945768c | 385 | |
5f55b8b5 MK |
386 | case PRU_RCVD: |
387 | case PRU_RCVOOB: | |
388 | return (EOPNOTSUPP); /* do not free mbuf's */ | |
389 | ||
4945768c SL |
390 | default: |
391 | panic("udp_usrreq"); | |
d52566dd | 392 | } |
a3fa431a SL |
393 | release: |
394 | if (m != NULL) | |
395 | m_freem(m); | |
8a2f82db | 396 | return (error); |
d52566dd | 397 | } |