1 | /* |
---|
2 | * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 |
---|
3 | * The Regents of the University of California. All rights reserved. |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms, with or without |
---|
6 | * modification, are permitted provided that the following conditions |
---|
7 | * are met: |
---|
8 | * 1. Redistributions of source code must retain the above copyright |
---|
9 | * notice, this list of conditions and the following disclaimer. |
---|
10 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer in the |
---|
12 | * documentation and/or other materials provided with the distribution. |
---|
13 | * 4. Neither the name of the University nor the names of its contributors |
---|
14 | * may be used to endorse or promote products derived from this software |
---|
15 | * without specific prior written permission. |
---|
16 | * |
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
27 | * SUCH DAMAGE. |
---|
28 | * |
---|
29 | * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95 |
---|
30 | * $FreeBSD: src/sys/netinet/udp_usrreq.c,v 1.170 2004/11/08 14:44:53 phk Exp $ |
---|
31 | */ |
---|
32 | |
---|
33 | #ifdef HAVE_CONFIG_H |
---|
34 | #include "config.h" |
---|
35 | #endif |
---|
36 | |
---|
37 | #include <sys/param.h> |
---|
38 | #include <rtems/bsd/sys/queue.h> |
---|
39 | #include <sys/systm.h> |
---|
40 | #include <sys/malloc.h> |
---|
41 | #include <sys/mbuf.h> |
---|
42 | #include <sys/protosw.h> |
---|
43 | #include <sys/socket.h> |
---|
44 | #include <sys/socketvar.h> |
---|
45 | #include <errno.h> |
---|
46 | #include <sys/stat.h> |
---|
47 | #include <sys/kernel.h> |
---|
48 | #include <sys/sysctl.h> |
---|
49 | #include <sys/syslog.h> |
---|
50 | |
---|
51 | #include <net/if.h> |
---|
52 | #include <net/route.h> |
---|
53 | |
---|
54 | #include <netinet/in.h> |
---|
55 | #include <netinet/in_systm.h> |
---|
56 | #include <netinet/ip.h> |
---|
57 | #include <netinet/in_pcb.h> |
---|
58 | #include <netinet/in_var.h> |
---|
59 | #include <netinet/ip_var.h> |
---|
60 | #include <netinet/ip_icmp.h> |
---|
61 | #include <netinet/udp.h> |
---|
62 | #include <netinet/udp_var.h> |
---|
63 | |
---|
64 | /* |
---|
65 | * UDP protocol implementation. |
---|
66 | * Per RFC 768, August, 1980. |
---|
67 | */ |
---|
68 | #ifndef COMPAT_42 |
---|
69 | static int udpcksum = 1; |
---|
70 | #else |
---|
71 | static int udpcksum = 0; /* XXX */ |
---|
72 | #endif |
---|
73 | SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, |
---|
74 | &udpcksum, 0, ""); |
---|
75 | |
---|
76 | static int log_in_vain = 0; |
---|
77 | SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, |
---|
78 | &log_in_vain, 0, ""); |
---|
79 | |
---|
80 | struct inpcbhead udb; /* from udp_var.h */ |
---|
81 | struct inpcbinfo udbinfo; |
---|
82 | |
---|
83 | #ifndef UDBHASHSIZE |
---|
84 | #define UDBHASHSIZE 64 |
---|
85 | #endif |
---|
86 | |
---|
87 | struct udpstat udpstat; /* from udp_var.h */ |
---|
88 | SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RD, |
---|
89 | &udpstat, udpstat, ""); |
---|
90 | |
---|
91 | static struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET, 0, {0}, {0} }; |
---|
92 | |
---|
93 | static void udp_detach(struct inpcb *); |
---|
94 | static int udp_output(struct inpcb *, struct mbuf *, struct mbuf *, |
---|
95 | struct mbuf *); |
---|
96 | static void udp_notify(struct inpcb *, int); |
---|
97 | |
---|
98 | void |
---|
99 | udp_init(void) |
---|
100 | { |
---|
101 | LIST_INIT(&udb); |
---|
102 | udbinfo.listhead = &udb; |
---|
103 | udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask); |
---|
104 | } |
---|
105 | |
---|
106 | void |
---|
107 | udp_input(struct mbuf *m, int iphlen) |
---|
108 | { |
---|
109 | register struct ip *ip; |
---|
110 | register struct udphdr *uh; |
---|
111 | register struct inpcb *inp; |
---|
112 | struct mbuf *opts = 0; |
---|
113 | int len; |
---|
114 | struct ip save_ip; |
---|
115 | |
---|
116 | udpstat.udps_ipackets++; |
---|
117 | |
---|
118 | /* |
---|
119 | * Strip IP options, if any; should skip this, |
---|
120 | * make available to user, and use on returned packets, |
---|
121 | * but we don't yet have a way to check the checksum |
---|
122 | * with options still present. |
---|
123 | */ |
---|
124 | if (iphlen > sizeof (struct ip)) { |
---|
125 | ip_stripoptions(m, (struct mbuf *)0); |
---|
126 | iphlen = sizeof(struct ip); |
---|
127 | } |
---|
128 | |
---|
129 | /* |
---|
130 | * Get IP and UDP header together in first mbuf. |
---|
131 | */ |
---|
132 | ip = mtod(m, struct ip *); |
---|
133 | if (m->m_len < iphlen + sizeof(struct udphdr)) { |
---|
134 | if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) { |
---|
135 | udpstat.udps_hdrops++; |
---|
136 | return; |
---|
137 | } |
---|
138 | ip = mtod(m, struct ip *); |
---|
139 | } |
---|
140 | uh = (struct udphdr *)((caddr_t)ip + iphlen); |
---|
141 | |
---|
142 | /* |
---|
143 | * Make mbuf data length reflect UDP length. |
---|
144 | * If not enough data to reflect UDP length, drop. |
---|
145 | */ |
---|
146 | len = ntohs((u_short)uh->uh_ulen); |
---|
147 | if (ip->ip_len != len) { |
---|
148 | if (len > ip->ip_len || len < sizeof(struct udphdr)) { |
---|
149 | udpstat.udps_badlen++; |
---|
150 | goto bad; |
---|
151 | } |
---|
152 | m_adj(m, len - ip->ip_len); |
---|
153 | /* ip->ip_len = len; */ |
---|
154 | } |
---|
155 | /* |
---|
156 | * Save a copy of the IP header in case we want restore it |
---|
157 | * for sending an ICMP error message in response. |
---|
158 | */ |
---|
159 | save_ip = *ip; |
---|
160 | |
---|
161 | /* |
---|
162 | * Checksum extended UDP header and data. |
---|
163 | */ |
---|
164 | if (uh->uh_sum) { |
---|
165 | ((struct ipovly *)ip)->ih_next = 0; |
---|
166 | ((struct ipovly *)ip)->ih_prev = 0; |
---|
167 | ((struct ipovly *)ip)->ih_x1 = 0; |
---|
168 | ((struct ipovly *)ip)->ih_len = uh->uh_ulen; |
---|
169 | uh->uh_sum = in_cksum(m, len + sizeof (struct ip)); |
---|
170 | if (uh->uh_sum) { |
---|
171 | udpstat.udps_badsum++; |
---|
172 | m_freem(m); |
---|
173 | return; |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || |
---|
178 | in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { |
---|
179 | struct inpcb *last; |
---|
180 | /* |
---|
181 | * Deliver a multicast or broadcast datagram to *all* sockets |
---|
182 | * for which the local and remote addresses and ports match |
---|
183 | * those of the incoming datagram. This allows more than |
---|
184 | * one process to receive multi/broadcasts on the same port. |
---|
185 | * (This really ought to be done for unicast datagrams as |
---|
186 | * well, but that would cause problems with existing |
---|
187 | * applications that open both address-specific sockets and |
---|
188 | * a wildcard socket listening to the same port -- they would |
---|
189 | * end up receiving duplicates of every unicast datagram. |
---|
190 | * Those applications open the multiple sockets to overcome an |
---|
191 | * inadequacy of the UDP socket interface, but for backwards |
---|
192 | * compatibility we avoid the problem here rather than |
---|
193 | * fixing the interface. Maybe 4.5BSD will remedy this?) |
---|
194 | */ |
---|
195 | |
---|
196 | /* |
---|
197 | * Construct sockaddr format source address. |
---|
198 | */ |
---|
199 | udp_in.sin_port = uh->uh_sport; |
---|
200 | udp_in.sin_addr = ip->ip_src; |
---|
201 | m->m_len -= sizeof (struct udpiphdr); |
---|
202 | m->m_data += sizeof (struct udpiphdr); |
---|
203 | /* |
---|
204 | * Locate pcb(s) for datagram. |
---|
205 | * (Algorithm copied from raw_intr().) |
---|
206 | */ |
---|
207 | last = NULL; |
---|
208 | for (inp = udb.lh_first; inp != NULL; inp = inp->inp_list.le_next) { |
---|
209 | if (inp->inp_lport != uh->uh_dport) |
---|
210 | continue; |
---|
211 | if (inp->inp_laddr.s_addr != INADDR_ANY) { |
---|
212 | if (inp->inp_laddr.s_addr != |
---|
213 | ip->ip_dst.s_addr) |
---|
214 | continue; |
---|
215 | } |
---|
216 | if (inp->inp_faddr.s_addr != INADDR_ANY) { |
---|
217 | if (inp->inp_faddr.s_addr != |
---|
218 | ip->ip_src.s_addr || |
---|
219 | inp->inp_fport != uh->uh_sport) |
---|
220 | continue; |
---|
221 | } |
---|
222 | |
---|
223 | if (last != NULL) { |
---|
224 | struct mbuf *n; |
---|
225 | |
---|
226 | if ((n = m_copy(m, 0, M_COPYALL)) != NULL) { |
---|
227 | if (last->inp_flags & INP_CONTROLOPTS |
---|
228 | || last->inp_socket->so_options & SO_TIMESTAMP) |
---|
229 | ip_savecontrol(last, &opts, ip, n); |
---|
230 | if (sbappendaddr(&last->inp_socket->so_rcv, |
---|
231 | (struct sockaddr *)&udp_in, |
---|
232 | n, opts) == 0) { |
---|
233 | m_freem(n); |
---|
234 | if (opts) |
---|
235 | m_freem(opts); |
---|
236 | udpstat.udps_fullsock++; |
---|
237 | } else |
---|
238 | sorwakeup(last->inp_socket); |
---|
239 | opts = 0; |
---|
240 | } |
---|
241 | } |
---|
242 | last = inp; |
---|
243 | /* |
---|
244 | * Don't look for additional matches if this one does |
---|
245 | * not have either the SO_REUSEPORT or SO_REUSEADDR |
---|
246 | * socket options set. This heuristic avoids searching |
---|
247 | * through all pcbs in the common case of a non-shared |
---|
248 | * port. It * assumes that an application will never |
---|
249 | * clear these options after setting them. |
---|
250 | */ |
---|
251 | if (((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)) |
---|
252 | break; |
---|
253 | } |
---|
254 | |
---|
255 | if (last == NULL) { |
---|
256 | /* |
---|
257 | * No matching pcb found; discard datagram. |
---|
258 | * (No need to send an ICMP Port Unreachable |
---|
259 | * for a broadcast or multicast datgram.) |
---|
260 | */ |
---|
261 | udpstat.udps_noportbcast++; |
---|
262 | goto bad; |
---|
263 | } |
---|
264 | if (last->inp_flags & INP_CONTROLOPTS |
---|
265 | || last->inp_socket->so_options & SO_TIMESTAMP) |
---|
266 | ip_savecontrol(last, &opts, ip, m); |
---|
267 | if (sbappendaddr(&last->inp_socket->so_rcv, |
---|
268 | (struct sockaddr *)&udp_in, |
---|
269 | m, opts) == 0) { |
---|
270 | udpstat.udps_fullsock++; |
---|
271 | goto bad; |
---|
272 | } |
---|
273 | sorwakeup(last->inp_socket); |
---|
274 | return; |
---|
275 | } |
---|
276 | /* |
---|
277 | * Locate pcb for datagram. |
---|
278 | */ |
---|
279 | inp = in_pcblookuphash(&udbinfo, ip->ip_src, uh->uh_sport, |
---|
280 | ip->ip_dst, uh->uh_dport, 1); |
---|
281 | if (inp == NULL) { |
---|
282 | if (log_in_vain) { |
---|
283 | char buf[4*sizeof "123"]; |
---|
284 | |
---|
285 | strcpy(buf, inet_ntoa(ip->ip_dst)); |
---|
286 | log(LOG_INFO, "Connection attempt to UDP %s:%d" |
---|
287 | " from %s:%d\n", |
---|
288 | buf, ntohs(uh->uh_dport), |
---|
289 | inet_ntoa(ip->ip_src), ntohs(uh->uh_sport)); |
---|
290 | } |
---|
291 | udpstat.udps_noport++; |
---|
292 | if (m->m_flags & (M_BCAST | M_MCAST)) { |
---|
293 | udpstat.udps_noportbcast++; |
---|
294 | goto bad; |
---|
295 | } |
---|
296 | *ip = save_ip; |
---|
297 | icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); |
---|
298 | return; |
---|
299 | } |
---|
300 | |
---|
301 | /* |
---|
302 | * Construct sockaddr format source address. |
---|
303 | * Stuff source address and datagram in user buffer. |
---|
304 | */ |
---|
305 | udp_in.sin_port = uh->uh_sport; |
---|
306 | udp_in.sin_addr = ip->ip_src; |
---|
307 | if (inp->inp_flags & INP_CONTROLOPTS |
---|
308 | || inp->inp_socket->so_options & SO_TIMESTAMP) |
---|
309 | ip_savecontrol(inp, &opts, ip, m); |
---|
310 | iphlen += sizeof(struct udphdr); |
---|
311 | m->m_len -= iphlen; |
---|
312 | m->m_pkthdr.len -= iphlen; |
---|
313 | m->m_data += iphlen; |
---|
314 | if (sbappendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in, |
---|
315 | m, opts) == 0) { |
---|
316 | udpstat.udps_fullsock++; |
---|
317 | goto bad; |
---|
318 | } |
---|
319 | sorwakeup(inp->inp_socket); |
---|
320 | return; |
---|
321 | bad: |
---|
322 | m_freem(m); |
---|
323 | if (opts) |
---|
324 | m_freem(opts); |
---|
325 | } |
---|
326 | |
---|
327 | /* |
---|
328 | * Notify a udp user of an asynchronous error; |
---|
329 | * just wake up so that he can collect error status. |
---|
330 | */ |
---|
331 | static void |
---|
332 | udp_notify(struct inpcb *inp, int errnum) |
---|
333 | { |
---|
334 | inp->inp_socket->so_error = errnum; |
---|
335 | sorwakeup(inp->inp_socket); |
---|
336 | sowwakeup(inp->inp_socket); |
---|
337 | } |
---|
338 | |
---|
339 | void |
---|
340 | udp_ctlinput(int cmd, struct sockaddr *sa, void *vip) |
---|
341 | { |
---|
342 | register struct ip *ip = vip; |
---|
343 | register struct udphdr *uh; |
---|
344 | |
---|
345 | if (!PRC_IS_REDIRECT(cmd) && |
---|
346 | ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)) |
---|
347 | return; |
---|
348 | if (ip) { |
---|
349 | uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); |
---|
350 | in_pcbnotify(&udb, sa, uh->uh_dport, ip->ip_src, uh->uh_sport, |
---|
351 | cmd, udp_notify); |
---|
352 | } else |
---|
353 | in_pcbnotify(&udb, sa, 0, zeroin_addr, 0, cmd, udp_notify); |
---|
354 | } |
---|
355 | |
---|
356 | static int |
---|
357 | udp_output(struct inpcb *inp, struct mbuf *m, struct mbuf *addr, |
---|
358 | struct mbuf *control) |
---|
359 | { |
---|
360 | register struct udpiphdr *ui; |
---|
361 | register int len = m->m_pkthdr.len; |
---|
362 | struct in_addr laddr; |
---|
363 | int s = 0, error = 0; |
---|
364 | |
---|
365 | laddr.s_addr = 0; |
---|
366 | if (control) |
---|
367 | m_freem(control); /* XXX */ |
---|
368 | |
---|
369 | if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { |
---|
370 | error = EMSGSIZE; |
---|
371 | goto release; |
---|
372 | } |
---|
373 | |
---|
374 | if (addr) { |
---|
375 | laddr = inp->inp_laddr; |
---|
376 | if (inp->inp_faddr.s_addr != INADDR_ANY) { |
---|
377 | error = EISCONN; |
---|
378 | goto release; |
---|
379 | } |
---|
380 | /* |
---|
381 | * Must block input while temporarily connected. |
---|
382 | */ |
---|
383 | s = splnet(); |
---|
384 | error = in_pcbconnect(inp, addr); |
---|
385 | if (error) { |
---|
386 | splx(s); |
---|
387 | goto release; |
---|
388 | } |
---|
389 | } else { |
---|
390 | if (inp->inp_faddr.s_addr == INADDR_ANY) { |
---|
391 | error = ENOTCONN; |
---|
392 | goto release; |
---|
393 | } |
---|
394 | } |
---|
395 | /* |
---|
396 | * Calculate data length and get a mbuf |
---|
397 | * for UDP and IP headers. |
---|
398 | */ |
---|
399 | M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT); |
---|
400 | if (m == 0) { |
---|
401 | error = ENOBUFS; |
---|
402 | if (addr) |
---|
403 | splx(s); |
---|
404 | goto release; |
---|
405 | } |
---|
406 | |
---|
407 | /* |
---|
408 | * Fill in mbuf with extended UDP header |
---|
409 | * and addresses and length put into network format. |
---|
410 | */ |
---|
411 | ui = mtod(m, struct udpiphdr *); |
---|
412 | ui->ui_next = ui->ui_prev = 0; |
---|
413 | ui->ui_x1 = 0; |
---|
414 | ui->ui_pr = IPPROTO_UDP; |
---|
415 | ui->ui_len = htons((u_short)len + sizeof (struct udphdr)); |
---|
416 | ui->ui_src = inp->inp_laddr; |
---|
417 | ui->ui_dst = inp->inp_faddr; |
---|
418 | ui->ui_sport = inp->inp_lport; |
---|
419 | ui->ui_dport = inp->inp_fport; |
---|
420 | ui->ui_ulen = ui->ui_len; |
---|
421 | |
---|
422 | /* |
---|
423 | * Stuff checksum and output datagram. |
---|
424 | */ |
---|
425 | ui->ui_sum = 0; |
---|
426 | if (udpcksum) { |
---|
427 | if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0) |
---|
428 | ui->ui_sum = 0xffff; |
---|
429 | } |
---|
430 | ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; |
---|
431 | ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ |
---|
432 | ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */ |
---|
433 | udpstat.udps_opackets++; |
---|
434 | error = ip_output(m, inp->inp_options, &inp->inp_route, |
---|
435 | inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST), |
---|
436 | inp->inp_moptions); |
---|
437 | |
---|
438 | if (addr) { |
---|
439 | in_pcbdisconnect(inp); |
---|
440 | inp->inp_laddr = laddr; |
---|
441 | splx(s); |
---|
442 | } |
---|
443 | return (error); |
---|
444 | |
---|
445 | release: |
---|
446 | m_freem(m); |
---|
447 | return (error); |
---|
448 | } |
---|
449 | |
---|
450 | #ifdef __rtems__ |
---|
451 | #define INP_INFO_RLOCK(a) |
---|
452 | #define INP_INFO_RUNLOCK(a) |
---|
453 | #define INP_LOCK(a) |
---|
454 | #define INP_UNLOCK(a) |
---|
455 | #endif |
---|
456 | |
---|
457 | static int |
---|
458 | udp_pcblist(SYSCTL_HANDLER_ARGS) |
---|
459 | { |
---|
460 | int error, i, n, s; |
---|
461 | struct inpcb *inp, **inp_list; |
---|
462 | inp_gen_t gencnt; |
---|
463 | struct xinpgen xig; |
---|
464 | |
---|
465 | /* |
---|
466 | * The process of preparing the TCB list is too time-consuming and |
---|
467 | * resource-intensive to repeat twice on every request. |
---|
468 | */ |
---|
469 | if (req->oldptr == 0) { |
---|
470 | n = udbinfo.ipi_count; |
---|
471 | req->oldidx = 2 * (sizeof xig) |
---|
472 | + (n + n/8) * sizeof(struct xinpcb); |
---|
473 | return 0; |
---|
474 | } |
---|
475 | |
---|
476 | if (req->newptr != 0) |
---|
477 | return EPERM; |
---|
478 | |
---|
479 | /* |
---|
480 | * OK, now we're committed to doing something. |
---|
481 | */ |
---|
482 | s = splnet(); |
---|
483 | gencnt = udbinfo.ipi_gencnt; |
---|
484 | n = udbinfo.ipi_count; |
---|
485 | splx(s); |
---|
486 | |
---|
487 | sysctl_wire_old_buffer(req, 2 * (sizeof xig) |
---|
488 | + n * sizeof(struct xinpcb)); |
---|
489 | |
---|
490 | xig.xig_len = sizeof xig; |
---|
491 | xig.xig_count = n; |
---|
492 | xig.xig_gen = gencnt; |
---|
493 | #if 0 |
---|
494 | xig.xig_sogen = so_gencnt; |
---|
495 | #endif |
---|
496 | error = SYSCTL_OUT(req, &xig, sizeof xig); |
---|
497 | if (error) |
---|
498 | return error; |
---|
499 | |
---|
500 | /* ccj add the exit if count is 0 */ |
---|
501 | if (!n) |
---|
502 | return error; |
---|
503 | |
---|
504 | inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); |
---|
505 | if (inp_list == 0) |
---|
506 | return ENOMEM; |
---|
507 | |
---|
508 | s = splnet(); |
---|
509 | INP_INFO_RLOCK(&udbinfo); |
---|
510 | for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n; |
---|
511 | inp = LIST_NEXT(inp, inp_list)) { |
---|
512 | INP_LOCK(inp); |
---|
513 | if (inp->inp_gencnt <= gencnt) |
---|
514 | #if 0 |
---|
515 | && |
---|
516 | cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0) |
---|
517 | #endif |
---|
518 | inp_list[i++] = inp; |
---|
519 | INP_UNLOCK(inp); |
---|
520 | } |
---|
521 | INP_INFO_RUNLOCK(&udbinfo); |
---|
522 | splx(s); |
---|
523 | n = i; |
---|
524 | |
---|
525 | error = 0; |
---|
526 | for (i = 0; i < n; i++) { |
---|
527 | inp = inp_list[i]; |
---|
528 | INP_LOCK(inp); |
---|
529 | if (inp->inp_gencnt <= gencnt) { |
---|
530 | struct xinpcb xi; |
---|
531 | xi.xi_len = sizeof xi; |
---|
532 | /* XXX should avoid extra copy */ |
---|
533 | bcopy(inp, &xi.xi_inp, sizeof *inp); |
---|
534 | #if 0 |
---|
535 | if (inp->inp_socket) |
---|
536 | sotoxsocket(inp->inp_socket, &xi.xi_socket); |
---|
537 | #endif |
---|
538 | error = SYSCTL_OUT(req, &xi, sizeof xi); |
---|
539 | } |
---|
540 | INP_UNLOCK(inp); |
---|
541 | } |
---|
542 | if (!error) { |
---|
543 | /* |
---|
544 | * Give the user an updated idea of our state. |
---|
545 | * If the generation differs from what we told |
---|
546 | * her before, she knows that something happened |
---|
547 | * while we were processing this request, and it |
---|
548 | * might be necessary to retry. |
---|
549 | */ |
---|
550 | s = splnet(); |
---|
551 | INP_INFO_RLOCK(&udbinfo); |
---|
552 | xig.xig_gen = udbinfo.ipi_gencnt; |
---|
553 | #if 0 |
---|
554 | xig.xig_sogen = so_gencnt; |
---|
555 | #endif |
---|
556 | xig.xig_count = udbinfo.ipi_count; |
---|
557 | INP_INFO_RUNLOCK(&udbinfo); |
---|
558 | splx(s); |
---|
559 | error = SYSCTL_OUT(req, &xig, sizeof xig); |
---|
560 | } |
---|
561 | free(inp_list, M_TEMP); |
---|
562 | return error; |
---|
563 | } |
---|
564 | |
---|
565 | SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0, |
---|
566 | udp_pcblist, "S,xinpcb", "List of active UDP sockets"); |
---|
567 | |
---|
568 | static u_long udp_sendspace = 9216; /* really max datagram size */ |
---|
569 | /* 40 1K datagrams */ |
---|
570 | SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, |
---|
571 | &udp_sendspace, 0, ""); |
---|
572 | |
---|
573 | static u_long udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in)); |
---|
574 | SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, |
---|
575 | &udp_recvspace, 0, ""); |
---|
576 | |
---|
577 | #if defined(__rtems__) |
---|
578 | void rtems_set_udp_buffer_sizes(u_long sendspace, u_long recvspace) |
---|
579 | { |
---|
580 | if ( sendspace != 0 ) |
---|
581 | udp_sendspace = sendspace; |
---|
582 | if ( recvspace != 0 ) |
---|
583 | udp_recvspace = recvspace; |
---|
584 | } |
---|
585 | #endif |
---|
586 | |
---|
587 | /*ARGSUSED*/ |
---|
588 | int |
---|
589 | udp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *addr, |
---|
590 | struct mbuf *control) |
---|
591 | { |
---|
592 | struct inpcb *inp = sotoinpcb(so); |
---|
593 | int error = 0; |
---|
594 | int s; |
---|
595 | |
---|
596 | if (req == PRU_CONTROL) |
---|
597 | return (in_control(so, (u_long)m, (caddr_t)addr, |
---|
598 | (struct ifnet *)control)); |
---|
599 | if (inp == NULL && req != PRU_ATTACH) { |
---|
600 | error = EINVAL; |
---|
601 | goto release; |
---|
602 | } |
---|
603 | /* |
---|
604 | * Note: need to block udp_input while changing |
---|
605 | * the udp pcb queue and/or pcb addresses. |
---|
606 | */ |
---|
607 | switch (req) { |
---|
608 | |
---|
609 | case PRU_ATTACH: |
---|
610 | if (inp != NULL) { |
---|
611 | error = EINVAL; |
---|
612 | break; |
---|
613 | } |
---|
614 | s = splnet(); |
---|
615 | error = in_pcballoc(so, &udbinfo); |
---|
616 | splx(s); |
---|
617 | if (error) |
---|
618 | break; |
---|
619 | error = soreserve(so, udp_sendspace, udp_recvspace); |
---|
620 | if (error) |
---|
621 | break; |
---|
622 | ((struct inpcb *) so->so_pcb)->inp_ip_ttl = ip_defttl; |
---|
623 | break; |
---|
624 | |
---|
625 | case PRU_DETACH: |
---|
626 | udp_detach(inp); |
---|
627 | break; |
---|
628 | |
---|
629 | case PRU_BIND: |
---|
630 | s = splnet(); |
---|
631 | error = in_pcbbind(inp, addr); |
---|
632 | splx(s); |
---|
633 | break; |
---|
634 | |
---|
635 | case PRU_LISTEN: |
---|
636 | error = EOPNOTSUPP; |
---|
637 | break; |
---|
638 | |
---|
639 | case PRU_CONNECT: |
---|
640 | if (inp->inp_faddr.s_addr != INADDR_ANY) { |
---|
641 | error = EISCONN; |
---|
642 | break; |
---|
643 | } |
---|
644 | s = splnet(); |
---|
645 | error = in_pcbconnect(inp, addr); |
---|
646 | splx(s); |
---|
647 | if (error == 0) |
---|
648 | soisconnected(so); |
---|
649 | break; |
---|
650 | |
---|
651 | case PRU_CONNECT2: |
---|
652 | error = EOPNOTSUPP; |
---|
653 | break; |
---|
654 | |
---|
655 | case PRU_ACCEPT: |
---|
656 | error = EOPNOTSUPP; |
---|
657 | break; |
---|
658 | |
---|
659 | case PRU_DISCONNECT: |
---|
660 | if (inp->inp_faddr.s_addr == INADDR_ANY) { |
---|
661 | error = ENOTCONN; |
---|
662 | break; |
---|
663 | } |
---|
664 | s = splnet(); |
---|
665 | in_pcbdisconnect(inp); |
---|
666 | inp->inp_laddr.s_addr = INADDR_ANY; |
---|
667 | splx(s); |
---|
668 | so->so_state &= ~SS_ISCONNECTED; /* XXX */ |
---|
669 | break; |
---|
670 | |
---|
671 | case PRU_SHUTDOWN: |
---|
672 | socantsendmore(so); |
---|
673 | break; |
---|
674 | |
---|
675 | case PRU_SEND: |
---|
676 | return (udp_output(inp, m, addr, control)); |
---|
677 | |
---|
678 | case PRU_ABORT: |
---|
679 | soisdisconnected(so); |
---|
680 | udp_detach(inp); |
---|
681 | break; |
---|
682 | |
---|
683 | case PRU_SOCKADDR: |
---|
684 | in_setsockaddr(inp, addr); |
---|
685 | break; |
---|
686 | |
---|
687 | case PRU_PEERADDR: |
---|
688 | in_setpeeraddr(inp, addr); |
---|
689 | break; |
---|
690 | |
---|
691 | case PRU_SENSE: |
---|
692 | /* |
---|
693 | * stat: don't bother with a blocksize. |
---|
694 | */ |
---|
695 | return (0); |
---|
696 | |
---|
697 | case PRU_SENDOOB: |
---|
698 | case PRU_FASTTIMO: |
---|
699 | case PRU_SLOWTIMO: |
---|
700 | case PRU_PROTORCV: |
---|
701 | case PRU_PROTOSEND: |
---|
702 | error = EOPNOTSUPP; |
---|
703 | break; |
---|
704 | |
---|
705 | case PRU_RCVD: |
---|
706 | case PRU_RCVOOB: |
---|
707 | return (EOPNOTSUPP); /* do not free mbuf's */ |
---|
708 | |
---|
709 | default: |
---|
710 | panic("udp_usrreq"); |
---|
711 | } |
---|
712 | |
---|
713 | release: |
---|
714 | if (control) { |
---|
715 | printf("udp control data unexpectedly retained\n"); |
---|
716 | m_freem(control); |
---|
717 | } |
---|
718 | if (m) |
---|
719 | m_freem(m); |
---|
720 | return (error); |
---|
721 | } |
---|
722 | |
---|
723 | static void |
---|
724 | udp_detach(struct inpcb *inp) |
---|
725 | { |
---|
726 | int s = splnet(); |
---|
727 | |
---|
728 | in_pcbdetach(inp); |
---|
729 | splx(s); |
---|
730 | } |
---|