Commit | Line | Data |
---|---|---|
61aceca0 | 1 | /* udp_usrreq.c 6.11 85/04/29 */ |
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 | |
4828da7c | 35 | int udpcksum = 0; |
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; | |
e199a8a5 BJ |
79 | ui->ui_len = htons((u_short)len); |
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 SL |
94 | /* don't send ICMP response for broadcast packet */ |
95 | if (in_lnaof(ui->ui_dst) == INADDR_ANY) | |
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 | { | |
131 | struct in_addr *sin; | |
132 | extern u_char inetctlerrmap[]; | |
39674d5f SL |
133 | |
134 | if (cmd < 0 || cmd > PRC_NCMDS) | |
135 | return; | |
136 | switch (cmd) { | |
137 | ||
138 | case PRC_ROUTEDEAD: | |
139 | break; | |
140 | ||
141 | case PRC_QUENCH: | |
142 | break; | |
143 | ||
144 | /* these are handled by ip */ | |
145 | case PRC_IFDOWN: | |
146 | case PRC_HOSTDEAD: | |
147 | case PRC_HOSTUNREACH: | |
148 | break; | |
149 | ||
150 | default: | |
151 | sin = &((struct icmp *)arg)->icmp_ip.ip_dst; | |
a1edc12b | 152 | in_pcbnotify(&udb, sin, (int)inetctlerrmap[cmd], udp_abort); |
39674d5f SL |
153 | } |
154 | } | |
155 | ||
4ad99bae | 156 | udp_output(inp, m0) |
2b4b57cd | 157 | struct inpcb *inp; |
2b4b57cd | 158 | struct mbuf *m0; |
1bd53abe | 159 | { |
2b4b57cd BJ |
160 | register struct mbuf *m; |
161 | register struct udpiphdr *ui; | |
f553b1f7 MK |
162 | register struct socket *so; |
163 | register int len = 0; | |
27e5eff5 | 164 | register struct route *ro; |
2b4b57cd BJ |
165 | |
166 | /* | |
167 | * Calculate data length and get a mbuf | |
4aed14e3 | 168 | * for UDP and IP headers. |
2b4b57cd BJ |
169 | */ |
170 | for (m = m0; m; m = m->m_next) | |
171 | len += m->m_len; | |
cce93e4b | 172 | m = m_get(M_DONTWAIT, MT_HEADER); |
8a2f82db SL |
173 | if (m == 0) { |
174 | m_freem(m0); | |
175 | return (ENOBUFS); | |
176 | } | |
2b4b57cd BJ |
177 | |
178 | /* | |
4aed14e3 | 179 | * Fill in mbuf with extended UDP header |
2b4b57cd BJ |
180 | * and addresses and length put into network format. |
181 | */ | |
182 | m->m_off = MMAXOFF - sizeof (struct udpiphdr); | |
183 | m->m_len = sizeof (struct udpiphdr); | |
184 | m->m_next = m0; | |
185 | ui = mtod(m, struct udpiphdr *); | |
186 | ui->ui_next = ui->ui_prev = 0; | |
187 | ui->ui_x1 = 0; | |
188 | ui->ui_pr = IPPROTO_UDP; | |
ee847abd | 189 | ui->ui_len = htons((u_short)len + sizeof (struct udphdr)); |
4e60622a BJ |
190 | ui->ui_src = inp->inp_laddr; |
191 | ui->ui_dst = inp->inp_faddr; | |
192 | ui->ui_sport = inp->inp_lport; | |
193 | ui->ui_dport = inp->inp_fport; | |
ee847abd | 194 | ui->ui_ulen = ui->ui_len; |
2b4b57cd BJ |
195 | |
196 | /* | |
197 | * Stuff checksum and output datagram. | |
198 | */ | |
199 | ui->ui_sum = 0; | |
4828da7c MK |
200 | if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0) |
201 | ui->ui_sum = -1; | |
4e60622a BJ |
202 | ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; |
203 | ((struct ip *)ui)->ip_ttl = MAXTTL; | |
f553b1f7 MK |
204 | so = inp->inp_socket; |
205 | if (so->so_options & SO_DONTROUTE) | |
206 | return (ip_output(m, (struct mbuf *)0, (struct route *)0, | |
207 | (so->so_options & SO_BROADCAST) | IP_ROUTETOIF)); | |
208 | /* | |
209 | * Use cached route for previous datagram if | |
210 | * this is also to the same destination. | |
211 | * | |
212 | * NB: We don't handle broadcasts because that | |
213 | * would require 3 subroutine calls. | |
214 | */ | |
215 | ro = &inp->inp_route; | |
27e5eff5 | 216 | #define satosin(sa) ((struct sockaddr_in *)(sa)) |
f553b1f7 MK |
217 | if (ro->ro_rt && |
218 | satosin(&ro->ro_dst)->sin_addr.s_addr != ui->ui_dst.s_addr) { | |
219 | RTFREE(ro->ro_rt); | |
220 | ro->ro_rt = (struct rtentry *)0; | |
221 | } | |
222 | return (ip_output(m, (struct mbuf *)0, ro, | |
223 | so->so_options & SO_BROADCAST)); | |
1bd53abe BJ |
224 | } |
225 | ||
1d14d351 MK |
226 | int udp_sendspace = 2048; /* really max datagram size */ |
227 | int udp_recvspace = 4 * (1024+sizeof(struct sockaddr_in)); /* 4 1K dgrams */ | |
228 | ||
a8d3bf7f | 229 | /*ARGSUSED*/ |
ab85b059 | 230 | udp_usrreq(so, req, m, nam, rights) |
53a5409e | 231 | struct socket *so; |
1bd53abe | 232 | int req; |
ab85b059 | 233 | struct mbuf *m, *nam, *rights; |
1bd53abe | 234 | { |
53a5409e | 235 | struct inpcb *inp = sotoinpcb(so); |
8a2f82db | 236 | int error = 0; |
1bd53abe | 237 | |
1d14d351 MK |
238 | if (req == PRU_CONTROL) |
239 | return (in_control(so, (int)m, (caddr_t)nam, | |
240 | (struct ifnet *)rights)); | |
ab85b059 SL |
241 | if (rights && rights->m_len) { |
242 | error = EINVAL; | |
243 | goto release; | |
244 | } | |
a3fa431a SL |
245 | if (inp == NULL && req != PRU_ATTACH) { |
246 | error = EINVAL; | |
247 | goto release; | |
248 | } | |
1bd53abe BJ |
249 | switch (req) { |
250 | ||
251 | case PRU_ATTACH: | |
a3fa431a SL |
252 | if (inp != NULL) { |
253 | error = EINVAL; | |
254 | break; | |
255 | } | |
8075bb0e BJ |
256 | error = in_pcballoc(so, &udb); |
257 | if (error) | |
258 | break; | |
1d14d351 | 259 | error = soreserve(so, udp_sendspace, udp_recvspace); |
8075bb0e BJ |
260 | if (error) |
261 | break; | |
53a5409e | 262 | break; |
1bd53abe BJ |
263 | |
264 | case PRU_DETACH: | |
a3fa431a SL |
265 | if (inp == NULL) { |
266 | error = ENOTCONN; | |
267 | break; | |
268 | } | |
405c9168 | 269 | in_pcbdetach(inp); |
53a5409e | 270 | break; |
1bd53abe | 271 | |
8075bb0e BJ |
272 | case PRU_BIND: |
273 | error = in_pcbbind(inp, nam); | |
274 | break; | |
275 | ||
276 | case PRU_LISTEN: | |
277 | error = EOPNOTSUPP; | |
278 | break; | |
279 | ||
1bd53abe | 280 | case PRU_CONNECT: |
a3fa431a SL |
281 | if (inp->inp_faddr.s_addr != INADDR_ANY) { |
282 | error = EISCONN; | |
283 | break; | |
284 | } | |
8075bb0e | 285 | error = in_pcbconnect(inp, nam); |
8a2f82db SL |
286 | if (error == 0) |
287 | soisconnected(so); | |
1bd53abe BJ |
288 | break; |
289 | ||
4945768c SL |
290 | case PRU_CONNECT2: |
291 | error = EOPNOTSUPP; | |
292 | break; | |
293 | ||
2b4b57cd | 294 | case PRU_ACCEPT: |
a3fa431a SL |
295 | error = EOPNOTSUPP; |
296 | break; | |
2b4b57cd | 297 | |
53a5409e | 298 | case PRU_DISCONNECT: |
a3fa431a SL |
299 | if (inp->inp_faddr.s_addr == INADDR_ANY) { |
300 | error = ENOTCONN; | |
301 | break; | |
302 | } | |
405c9168 | 303 | in_pcbdisconnect(inp); |
53a5409e | 304 | soisdisconnected(so); |
1bd53abe BJ |
305 | break; |
306 | ||
cdad2eb1 BJ |
307 | case PRU_SHUTDOWN: |
308 | socantsendmore(so); | |
309 | break; | |
310 | ||
1aa87517 BJ |
311 | case PRU_SEND: { |
312 | struct in_addr laddr; | |
5f55b8b5 | 313 | int s; |
1aa87517 | 314 | |
8075bb0e | 315 | if (nam) { |
1aa87517 | 316 | laddr = inp->inp_laddr; |
a3fa431a SL |
317 | if (inp->inp_faddr.s_addr != INADDR_ANY) { |
318 | error = EISCONN; | |
319 | break; | |
320 | } | |
5f55b8b5 MK |
321 | /* |
322 | * Must block input while temporarily connected. | |
323 | */ | |
324 | s = splnet(); | |
8075bb0e | 325 | error = in_pcbconnect(inp, nam); |
5f55b8b5 MK |
326 | if (error) { |
327 | splx(s); | |
8a2f82db | 328 | break; |
5f55b8b5 | 329 | } |
4ad99bae | 330 | } else { |
a3fa431a SL |
331 | if (inp->inp_faddr.s_addr == INADDR_ANY) { |
332 | error = ENOTCONN; | |
333 | break; | |
334 | } | |
4ad99bae | 335 | } |
8a2f82db | 336 | error = udp_output(inp, m); |
a3fa431a | 337 | m = NULL; |
8075bb0e | 338 | if (nam) { |
405c9168 | 339 | in_pcbdisconnect(inp); |
1aa87517 | 340 | inp->inp_laddr = laddr; |
1d14d351 | 341 | splx(s); |
1aa87517 BJ |
342 | } |
343 | } | |
1bd53abe BJ |
344 | break; |
345 | ||
346 | case PRU_ABORT: | |
405c9168 | 347 | in_pcbdetach(inp); |
53a5409e BJ |
348 | sofree(so); |
349 | soisdisconnected(so); | |
1bd53abe BJ |
350 | break; |
351 | ||
126472ab | 352 | case PRU_SOCKADDR: |
8075bb0e | 353 | in_setsockaddr(inp, nam); |
126472ab SL |
354 | break; |
355 | ||
a7343092 SL |
356 | case PRU_PEERADDR: |
357 | in_setpeeraddr(inp, nam); | |
358 | break; | |
359 | ||
74040e68 MK |
360 | case PRU_SENSE: |
361 | /* | |
362 | * stat: don't bother with a blocksize. | |
363 | */ | |
364 | return (0); | |
365 | ||
11d2a668 SL |
366 | case PRU_SENDOOB: |
367 | case PRU_FASTTIMO: | |
368 | case PRU_SLOWTIMO: | |
369 | case PRU_PROTORCV: | |
370 | case PRU_PROTOSEND: | |
371 | error = EOPNOTSUPP; | |
372 | break; | |
4945768c | 373 | |
5f55b8b5 MK |
374 | case PRU_RCVD: |
375 | case PRU_RCVOOB: | |
376 | return (EOPNOTSUPP); /* do not free mbuf's */ | |
377 | ||
4945768c SL |
378 | default: |
379 | panic("udp_usrreq"); | |
d52566dd | 380 | } |
a3fa431a SL |
381 | release: |
382 | if (m != NULL) | |
383 | m_freem(m); | |
8a2f82db | 384 | return (error); |
d52566dd | 385 | } |