Commit | Line | Data |
---|---|---|
8ae0e4b4 | 1 | /* |
9d91b170 | 2 | * Copyright (c) 1982, 1986, 1988 Regents of the University of California. |
2b6b6284 | 3 | * All rights reserved. |
8ae0e4b4 | 4 | * |
2b6b6284 | 5 | * Redistribution and use in source and binary forms are permitted |
616d42db KB |
6 | * provided that the above copyright notice and this paragraph are |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
2b6b6284 | 16 | * |
9d91b170 | 17 | * @(#)udp_usrreq.c 7.9 (Berkeley) %G% |
8ae0e4b4 | 18 | */ |
20666ad3 JB |
19 | |
20 | #include "param.h" | |
21 | #include "dir.h" | |
22 | #include "user.h" | |
9d91b170 | 23 | #include "malloc.h" |
20666ad3 JB |
24 | #include "mbuf.h" |
25 | #include "protosw.h" | |
26 | #include "socket.h" | |
27 | #include "socketvar.h" | |
28 | #include "errno.h" | |
29 | #include "stat.h" | |
f4d55810 | 30 | |
72e4f44e | 31 | #include "../net/if.h" |
c124e997 | 32 | #include "../net/route.h" |
f4d55810 | 33 | |
20666ad3 JB |
34 | #include "in.h" |
35 | #include "in_pcb.h" | |
36 | #include "in_systm.h" | |
37 | #include "ip.h" | |
38 | #include "ip_var.h" | |
39 | #include "ip_icmp.h" | |
40 | #include "udp.h" | |
41 | #include "udp_var.h" | |
d52566dd | 42 | |
2b4b57cd BJ |
43 | /* |
44 | * UDP protocol implementation. | |
45 | * Per RFC 768, August, 1980. | |
46 | */ | |
d52566dd BJ |
47 | udp_init() |
48 | { | |
49 | ||
eb44bfb2 | 50 | udb.inp_next = udb.inp_prev = &udb; |
d52566dd | 51 | } |
1bd53abe | 52 | |
c50542f3 | 53 | #ifndef COMPAT_42 |
dd8707ae | 54 | int udpcksum = 1; |
c50542f3 MK |
55 | #else |
56 | int udpcksum = 0; /* XXX */ | |
57 | #endif | |
0eda4f11 | 58 | int udp_ttl = UDP_TTL; |
c50542f3 | 59 | |
2b4b57cd | 60 | struct sockaddr_in udp_in = { AF_INET }; |
eb44bfb2 | 61 | |
9d91b170 MK |
62 | udp_input(m, iphlen) |
63 | register struct mbuf *m; | |
64 | int iphlen; | |
1bd53abe | 65 | { |
eb44bfb2 | 66 | register struct udpiphdr *ui; |
53a5409e | 67 | register struct inpcb *inp; |
e199a8a5 | 68 | int len; |
dcea2497 | 69 | struct ip ip; |
eb44bfb2 | 70 | |
2b4b57cd | 71 | /* |
4aed14e3 | 72 | * Get IP and UDP header together in first mbuf. |
7212ba67 | 73 | * Note: IP leaves IP header in first mbuf. |
2b4b57cd | 74 | */ |
4e60622a | 75 | ui = mtod(m, struct udpiphdr *); |
9d91b170 MK |
76 | if (iphlen > sizeof (struct ip)) |
77 | ip_stripoptions(m, (struct mbuf *)0); | |
78 | if (m->m_len < sizeof (struct udpiphdr)) { | |
7212ba67 MK |
79 | if ((m = m_pullup(m, sizeof (struct udpiphdr))) == 0) { |
80 | udpstat.udps_hdrops++; | |
81 | return; | |
82 | } | |
83 | ui = mtod(m, struct udpiphdr *); | |
84 | } | |
2b4b57cd BJ |
85 | |
86 | /* | |
4aed14e3 BJ |
87 | * Make mbuf data length reflect UDP length. |
88 | * If not enough data to reflect UDP length, drop. | |
2b4b57cd | 89 | */ |
e199a8a5 | 90 | len = ntohs((u_short)ui->ui_ulen); |
2b4b57cd BJ |
91 | if (((struct ip *)ui)->ip_len != len) { |
92 | if (len > ((struct ip *)ui)->ip_len) { | |
93 | udpstat.udps_badlen++; | |
94 | goto bad; | |
95 | } | |
4828da7c | 96 | m_adj(m, len - ((struct ip *)ui)->ip_len); |
dcea2497 | 97 | /* ((struct ip *)ui)->ip_len = len; */ |
2b4b57cd | 98 | } |
dcea2497 MK |
99 | /* |
100 | * Save a copy of the IP header in case we want restore it for ICMP. | |
101 | */ | |
9d91b170 | 102 | ip = *(struct ip *)ui; |
2b4b57cd BJ |
103 | |
104 | /* | |
4aed14e3 | 105 | * Checksum extended UDP header and data. |
2b4b57cd | 106 | */ |
4828da7c | 107 | if (udpcksum && ui->ui_sum) { |
2b4b57cd BJ |
108 | ui->ui_next = ui->ui_prev = 0; |
109 | ui->ui_x1 = 0; | |
ee09448f | 110 | ui->ui_len = ui->ui_ulen; |
e199a8a5 | 111 | if (ui->ui_sum = in_cksum(m, len + sizeof (struct ip))) { |
2b4b57cd | 112 | udpstat.udps_badsum++; |
eb44bfb2 BJ |
113 | m_freem(m); |
114 | return; | |
115 | } | |
116 | } | |
2b4b57cd BJ |
117 | |
118 | /* | |
e199a8a5 | 119 | * Locate pcb for datagram. |
2b4b57cd | 120 | */ |
2b4b57cd | 121 | inp = in_pcblookup(&udb, |
ebcadd38 BJ |
122 | ui->ui_src, ui->ui_sport, ui->ui_dst, ui->ui_dport, |
123 | INPLOOKUP_WILDCARD); | |
72e4f44e | 124 | if (inp == 0) { |
5cdc4d65 | 125 | /* don't send ICMP response for broadcast packet */ |
9d91b170 | 126 | if (m->m_flags & M_BCAST) |
72e4f44e | 127 | goto bad; |
dcea2497 | 128 | *(struct ip *)ui = ip; |
9d91b170 | 129 | icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT); |
72e4f44e SL |
130 | return; |
131 | } | |
2b4b57cd BJ |
132 | |
133 | /* | |
134 | * Construct sockaddr format source address. | |
135 | * Stuff source address and datagram in user buffer. | |
136 | */ | |
137 | udp_in.sin_port = ui->ui_sport; | |
138 | udp_in.sin_addr = ui->ui_src; | |
4e60622a | 139 | m->m_len -= sizeof (struct udpiphdr); |
9d91b170 MK |
140 | m->m_pkthdr.len -= sizeof (struct udpiphdr); |
141 | m->m_data += sizeof (struct udpiphdr); | |
ab85b059 SL |
142 | if (sbappendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in, |
143 | m, (struct mbuf *)0) == 0) | |
2b4b57cd | 144 | goto bad; |
4e60622a | 145 | sorwakeup(inp->inp_socket); |
53a5409e | 146 | return; |
2b4b57cd | 147 | bad: |
53a5409e | 148 | m_freem(m); |
1bd53abe BJ |
149 | } |
150 | ||
41715710 MK |
151 | /* |
152 | * Notify a udp user of an asynchronous error; | |
153 | * just wake up so that he can collect error status. | |
154 | */ | |
155 | udp_notify(inp) | |
156 | register struct inpcb *inp; | |
157 | { | |
158 | ||
159 | sorwakeup(inp->inp_socket); | |
160 | sowwakeup(inp->inp_socket); | |
161 | } | |
162 | ||
dcea2497 | 163 | udp_ctlinput(cmd, sa) |
39674d5f | 164 | int cmd; |
dcea2497 | 165 | struct sockaddr *sa; |
39674d5f | 166 | { |
39674d5f | 167 | extern u_char inetctlerrmap[]; |
dcea2497 | 168 | struct sockaddr_in *sin; |
336240bf | 169 | int in_rtchange(); |
39674d5f | 170 | |
dcea2497 | 171 | if ((unsigned)cmd > PRC_NCMDS) |
39674d5f | 172 | return; |
dcea2497 MK |
173 | if (sa->sa_family != AF_INET && sa->sa_family != AF_IMPLINK) |
174 | return; | |
175 | sin = (struct sockaddr_in *)sa; | |
176 | if (sin->sin_addr.s_addr == INADDR_ANY) | |
177 | return; | |
178 | ||
39674d5f SL |
179 | switch (cmd) { |
180 | ||
dcea2497 | 181 | case PRC_QUENCH: |
39674d5f SL |
182 | break; |
183 | ||
dcea2497 | 184 | case PRC_ROUTEDEAD: |
336240bf MK |
185 | case PRC_REDIRECT_NET: |
186 | case PRC_REDIRECT_HOST: | |
dcea2497 MK |
187 | case PRC_REDIRECT_TOSNET: |
188 | case PRC_REDIRECT_TOSHOST: | |
189 | in_pcbnotify(&udb, &sin->sin_addr, 0, in_rtchange); | |
39674d5f SL |
190 | break; |
191 | ||
39674d5f | 192 | default: |
336240bf MK |
193 | if (inetctlerrmap[cmd] == 0) |
194 | return; /* XXX */ | |
dcea2497 | 195 | in_pcbnotify(&udb, &sin->sin_addr, (int)inetctlerrmap[cmd], |
41715710 | 196 | udp_notify); |
39674d5f SL |
197 | } |
198 | } | |
199 | ||
9d91b170 | 200 | udp_output(inp, m) |
d55475b1 | 201 | register struct inpcb *inp; |
2b4b57cd | 202 | register struct mbuf *m; |
9d91b170 | 203 | { |
2b4b57cd | 204 | register struct udpiphdr *ui; |
9d91b170 | 205 | register int len = m->m_pkthdr.len; |
2b4b57cd BJ |
206 | |
207 | /* | |
208 | * Calculate data length and get a mbuf | |
4aed14e3 | 209 | * for UDP and IP headers. |
2b4b57cd | 210 | */ |
9d91b170 | 211 | M_PREPEND(m, sizeof(struct udpiphdr), M_WAIT); |
2b4b57cd BJ |
212 | |
213 | /* | |
4aed14e3 | 214 | * Fill in mbuf with extended UDP header |
2b4b57cd BJ |
215 | * and addresses and length put into network format. |
216 | */ | |
2b4b57cd BJ |
217 | ui = mtod(m, struct udpiphdr *); |
218 | ui->ui_next = ui->ui_prev = 0; | |
219 | ui->ui_x1 = 0; | |
220 | ui->ui_pr = IPPROTO_UDP; | |
ee847abd | 221 | ui->ui_len = htons((u_short)len + sizeof (struct udphdr)); |
4e60622a BJ |
222 | ui->ui_src = inp->inp_laddr; |
223 | ui->ui_dst = inp->inp_faddr; | |
224 | ui->ui_sport = inp->inp_lport; | |
225 | ui->ui_dport = inp->inp_fport; | |
ee847abd | 226 | ui->ui_ulen = ui->ui_len; |
2b4b57cd BJ |
227 | |
228 | /* | |
229 | * Stuff checksum and output datagram. | |
230 | */ | |
231 | ui->ui_sum = 0; | |
dd8707ae MK |
232 | if (udpcksum) { |
233 | if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0) | |
890024da | 234 | ui->ui_sum = 0xffff; |
ee09448f | 235 | } |
4e60622a | 236 | ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; |
0eda4f11 | 237 | ((struct ip *)ui)->ip_ttl = udp_ttl; |
d55475b1 MK |
238 | return (ip_output(m, inp->inp_options, &inp->inp_route, |
239 | inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST))); | |
1bd53abe BJ |
240 | } |
241 | ||
9340d736 MK |
242 | u_long udp_sendspace = 2048; /* really max datagram size */ |
243 | u_long udp_recvspace = 4 * (1024+sizeof(struct sockaddr_in)); /* 4 1K dgrams */ | |
1d14d351 | 244 | |
a8d3bf7f | 245 | /*ARGSUSED*/ |
9d91b170 | 246 | udp_usrreq(so, req, m, nam, rights, control) |
53a5409e | 247 | struct socket *so; |
1bd53abe | 248 | int req; |
9d91b170 | 249 | struct mbuf *m, *nam, *rights, *control; |
1bd53abe | 250 | { |
53a5409e | 251 | struct inpcb *inp = sotoinpcb(so); |
8a2f82db | 252 | int error = 0; |
1bd53abe | 253 | |
1d14d351 MK |
254 | if (req == PRU_CONTROL) |
255 | return (in_control(so, (int)m, (caddr_t)nam, | |
256 | (struct ifnet *)rights)); | |
ab85b059 SL |
257 | if (rights && rights->m_len) { |
258 | error = EINVAL; | |
259 | goto release; | |
260 | } | |
a3fa431a SL |
261 | if (inp == NULL && req != PRU_ATTACH) { |
262 | error = EINVAL; | |
263 | goto release; | |
264 | } | |
1bd53abe BJ |
265 | switch (req) { |
266 | ||
267 | case PRU_ATTACH: | |
a3fa431a SL |
268 | if (inp != NULL) { |
269 | error = EINVAL; | |
270 | break; | |
271 | } | |
8075bb0e BJ |
272 | error = in_pcballoc(so, &udb); |
273 | if (error) | |
274 | break; | |
1d14d351 | 275 | error = soreserve(so, udp_sendspace, udp_recvspace); |
8075bb0e BJ |
276 | if (error) |
277 | break; | |
53a5409e | 278 | break; |
1bd53abe BJ |
279 | |
280 | case PRU_DETACH: | |
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); |
f96a0781 | 316 | so->so_state &= ~SS_ISCONNECTED; /* XXX */ |
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: | |
53a5409e | 359 | soisdisconnected(so); |
97f3441b | 360 | in_pcbdetach(inp); |
1bd53abe BJ |
361 | break; |
362 | ||
126472ab | 363 | case PRU_SOCKADDR: |
8075bb0e | 364 | in_setsockaddr(inp, nam); |
126472ab SL |
365 | break; |
366 | ||
a7343092 SL |
367 | case PRU_PEERADDR: |
368 | in_setpeeraddr(inp, nam); | |
369 | break; | |
370 | ||
74040e68 MK |
371 | case PRU_SENSE: |
372 | /* | |
373 | * stat: don't bother with a blocksize. | |
374 | */ | |
375 | return (0); | |
376 | ||
11d2a668 SL |
377 | case PRU_SENDOOB: |
378 | case PRU_FASTTIMO: | |
379 | case PRU_SLOWTIMO: | |
380 | case PRU_PROTORCV: | |
381 | case PRU_PROTOSEND: | |
382 | error = EOPNOTSUPP; | |
383 | break; | |
4945768c | 384 | |
5f55b8b5 MK |
385 | case PRU_RCVD: |
386 | case PRU_RCVOOB: | |
387 | return (EOPNOTSUPP); /* do not free mbuf's */ | |
388 | ||
4945768c SL |
389 | default: |
390 | panic("udp_usrreq"); | |
d52566dd | 391 | } |
a3fa431a SL |
392 | release: |
393 | if (m != NULL) | |
394 | m_freem(m); | |
8a2f82db | 395 | return (error); |
d52566dd | 396 | } |