xref: /freebsd/sys/netinet/raw_ip.c (revision ebbd4fa8c8427d3dd847ba33c45c996e0500e6ff)
1 /*
2  * Copyright (c) 1982, 1986, 1988, 1993
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)raw_ip.c	8.7 (Berkeley) 5/15/95
34  * $FreeBSD$
35  */
36 
37 #include "opt_inet6.h"
38 #include "opt_ipsec.h"
39 #include "opt_mac.h"
40 #include "opt_random_ip_id.h"
41 
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/proc.h>
48 #include <sys/protosw.h>
49 #include <sys/signalvar.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sx.h>
53 #include <sys/sysctl.h>
54 #include <sys/systm.h>
55 
56 #include <vm/uma.h>
57 
58 #include <net/if.h>
59 #include <net/route.h>
60 
61 #define _IP_VHL
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/in_pcb.h>
65 #include <netinet/in_var.h>
66 #include <netinet/ip.h>
67 #include <netinet/ip_var.h>
68 #include <netinet/ip_mroute.h>
69 
70 #include <netinet/ip_fw.h>
71 #include <netinet/ip_dummynet.h>
72 
73 #ifdef IPSEC
74 #include <netinet6/ipsec.h>
75 #endif /*IPSEC*/
76 
77 struct	inpcbhead ripcb;
78 struct	inpcbinfo ripcbinfo;
79 
80 /* control hooks for ipfw and dummynet */
81 ip_fw_ctl_t *ip_fw_ctl_ptr;
82 ip_dn_ctl_t *ip_dn_ctl_ptr;
83 
84 /*
85  * Nominal space allocated to a raw ip socket.
86  */
87 #define	RIPSNDQ		8192
88 #define	RIPRCVQ		8192
89 
90 /*
91  * Raw interface to IP protocol.
92  */
93 
94 /*
95  * Initialize raw connection block q.
96  */
97 void
98 rip_init()
99 {
100 	INP_INFO_LOCK_INIT(&ripcbinfo, "rip");
101 	LIST_INIT(&ripcb);
102 	ripcbinfo.listhead = &ripcb;
103 	/*
104 	 * XXX We don't use the hash list for raw IP, but it's easier
105 	 * to allocate a one entry hash list than it is to check all
106 	 * over the place for hashbase == NULL.
107 	 */
108 	ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask);
109 	ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask);
110 	ripcbinfo.ipi_zone = uma_zcreate("ripcb", sizeof(struct inpcb),
111 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
112 	uma_zone_set_max(ripcbinfo.ipi_zone, maxsockets);
113 }
114 
115 static struct	sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
116 /*
117  * Setup generic address and protocol structures
118  * for raw_input routine, then pass them along with
119  * mbuf chain.
120  */
121 void
122 rip_input(m, off)
123 	struct mbuf *m;
124 	int off;
125 {
126 	register struct ip *ip = mtod(m, struct ip *);
127 	register struct inpcb *inp;
128 	struct inpcb *last = 0;
129 	struct mbuf *opts = 0;
130 	int proto = ip->ip_p;
131 
132 	ripsrc.sin_addr = ip->ip_src;
133 	LIST_FOREACH(inp, &ripcb, inp_list) {
134 #ifdef INET6
135 		if ((inp->inp_vflag & INP_IPV4) == 0)
136 			continue;
137 #endif
138 		if (inp->inp_ip_p && inp->inp_ip_p != proto)
139 			continue;
140 		if (inp->inp_laddr.s_addr &&
141                   inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
142 			continue;
143 		if (inp->inp_faddr.s_addr &&
144                   inp->inp_faddr.s_addr != ip->ip_src.s_addr)
145 			continue;
146 		if (last) {
147 			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
148 			int policyfail = 0;
149 
150 			if (n != NULL) {
151 #ifdef IPSSEC
152 				/* check AH/ESP integrity. */
153 				if (ipsec4_in_reject_so(n, last->inp_socket)) {
154 					policyfail = 1;
155 					ipsecstat.in_polvio++;
156 					/* do not inject data to pcb */
157 				}
158 #endif /*IPSEC*/
159 #ifdef MAC
160 				if (policyfail == 0 &&
161 				    mac_check_socket_receive(last->inp_socket,
162 				    n) != 0)
163 					policyfail = 1;
164 #endif
165 			}
166 			if (policyfail)
167 				m_freem(n);
168 			else if (n) {
169 				if (last->inp_flags & INP_CONTROLOPTS ||
170 				    last->inp_socket->so_options & SO_TIMESTAMP)
171 				    ip_savecontrol(last, &opts, ip, n);
172 				if (sbappendaddr(&last->inp_socket->so_rcv,
173 				    (struct sockaddr *)&ripsrc, n,
174 				    opts) == 0) {
175 					/* should notify about lost packet */
176 					m_freem(n);
177 					if (opts)
178 					    m_freem(opts);
179 				} else
180 					sorwakeup(last->inp_socket);
181 				opts = 0;
182 			}
183 		}
184 		last = inp;
185 	}
186 	if (last) {
187 #ifdef IPSEC
188 		/* check AH/ESP integrity. */
189 		if (ipsec4_in_reject_so(m, last->inp_socket)) {
190 			m_freem(m);
191 			ipsecstat.in_polvio++;
192 			ipstat.ips_delivered--;
193 			/* do not inject data to pcb */
194 			return;
195 		}
196 #endif /*IPSEC*/
197 #ifdef MAC
198 		if (mac_check_socket_receive(last->inp_socket, m) != 0) {
199 			m_freem(m);
200 			ipstat.ips_delivered--;
201 			return;
202 		}
203 #endif
204 		if (last->inp_flags & INP_CONTROLOPTS ||
205 		    last->inp_socket->so_options & SO_TIMESTAMP)
206 			ip_savecontrol(last, &opts, ip, m);
207 		if (sbappendaddr(&last->inp_socket->so_rcv,
208 		    (struct sockaddr *)&ripsrc, m, opts) == 0) {
209 			m_freem(m);
210 			if (opts)
211 			    m_freem(opts);
212 		} else
213 			sorwakeup(last->inp_socket);
214 	} else {
215 		m_freem(m);
216 		ipstat.ips_noproto++;
217 		ipstat.ips_delivered--;
218 	}
219 }
220 
221 /*
222  * Generate IP header and pass packet to ip_output.
223  * Tack on options user may have setup with control call.
224  */
225 int
226 rip_output(m, so, dst)
227 	struct mbuf *m;
228 	struct socket *so;
229 	u_long dst;
230 {
231 	register struct ip *ip;
232 	register struct inpcb *inp = sotoinpcb(so);
233 	int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
234 
235 #ifdef MAC
236 	mac_create_mbuf_from_socket(so, m);
237 #endif
238 
239 	/*
240 	 * If the user handed us a complete IP packet, use it.
241 	 * Otherwise, allocate an mbuf for a header and fill it in.
242 	 */
243 	if ((inp->inp_flags & INP_HDRINCL) == 0) {
244 		if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
245 			m_freem(m);
246 			return(EMSGSIZE);
247 		}
248 		M_PREPEND(m, sizeof(struct ip), M_TRYWAIT);
249 		ip = mtod(m, struct ip *);
250 		ip->ip_tos = inp->inp_ip_tos;
251 		ip->ip_off = 0;
252 		ip->ip_p = inp->inp_ip_p;
253 		ip->ip_len = m->m_pkthdr.len;
254 		ip->ip_src = inp->inp_laddr;
255 		ip->ip_dst.s_addr = dst;
256 		ip->ip_ttl = inp->inp_ip_ttl;
257 	} else {
258 		if (m->m_pkthdr.len > IP_MAXPACKET) {
259 			m_freem(m);
260 			return(EMSGSIZE);
261 		}
262 		ip = mtod(m, struct ip *);
263 		/* don't allow both user specified and setsockopt options,
264 		   and don't allow packet length sizes that will crash */
265 		if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2))
266 		     && inp->inp_options)
267 		    || (ip->ip_len > m->m_pkthdr.len)
268 		    || (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) {
269 			m_freem(m);
270 			return EINVAL;
271 		}
272 		if (ip->ip_id == 0)
273 #ifdef RANDOM_IP_ID
274 			ip->ip_id = ip_randomid();
275 #else
276 			ip->ip_id = htons(ip_id++);
277 #endif
278 		/* XXX prevent ip_output from overwriting header fields */
279 		flags |= IP_RAWOUTPUT;
280 		ipstat.ips_rawout++;
281 	}
282 
283 #ifdef IPSEC
284 	if (ipsec_setsocket(m, so) != 0) {
285 		m_freem(m);
286 		return ENOBUFS;
287 	}
288 #endif /*IPSEC*/
289 
290 	return (ip_output(m, inp->inp_options, &inp->inp_route, flags,
291 			  inp->inp_moptions));
292 }
293 
294 /*
295  * Raw IP socket option processing.
296  */
297 int
298 rip_ctloutput(so, sopt)
299 	struct socket *so;
300 	struct sockopt *sopt;
301 {
302 	struct	inpcb *inp = sotoinpcb(so);
303 	int	error, optval;
304 
305 	if (sopt->sopt_level != IPPROTO_IP)
306 		return (EINVAL);
307 
308 	error = 0;
309 
310 	switch (sopt->sopt_dir) {
311 	case SOPT_GET:
312 		switch (sopt->sopt_name) {
313 		case IP_HDRINCL:
314 			optval = inp->inp_flags & INP_HDRINCL;
315 			error = sooptcopyout(sopt, &optval, sizeof optval);
316 			break;
317 
318 		case IP_FW_ADD:	/* ADD actually returns the body... */
319 		case IP_FW_GET:
320 			if (IPFW_LOADED)
321 				error = ip_fw_ctl_ptr(sopt);
322 			else
323 				error = ENOPROTOOPT;
324 			break;
325 
326 		case IP_DUMMYNET_GET:
327 			if (DUMMYNET_LOADED)
328 				error = ip_dn_ctl_ptr(sopt);
329 			else
330 				error = ENOPROTOOPT;
331 			break ;
332 
333 		case MRT_INIT:
334 		case MRT_DONE:
335 		case MRT_ADD_VIF:
336 		case MRT_DEL_VIF:
337 		case MRT_ADD_MFC:
338 		case MRT_DEL_MFC:
339 		case MRT_VERSION:
340 		case MRT_ASSERT:
341 			error = ip_mrouter_get(so, sopt);
342 			break;
343 
344 		default:
345 			error = ip_ctloutput(so, sopt);
346 			break;
347 		}
348 		break;
349 
350 	case SOPT_SET:
351 		switch (sopt->sopt_name) {
352 		case IP_HDRINCL:
353 			error = sooptcopyin(sopt, &optval, sizeof optval,
354 					    sizeof optval);
355 			if (error)
356 				break;
357 			if (optval)
358 				inp->inp_flags |= INP_HDRINCL;
359 			else
360 				inp->inp_flags &= ~INP_HDRINCL;
361 			break;
362 
363 		case IP_FW_ADD:
364 		case IP_FW_DEL:
365 		case IP_FW_FLUSH:
366 		case IP_FW_ZERO:
367 		case IP_FW_RESETLOG:
368 			if (IPFW_LOADED)
369 				error = ip_fw_ctl_ptr(sopt);
370 			else
371 				error = ENOPROTOOPT;
372 			break;
373 
374 		case IP_DUMMYNET_CONFIGURE:
375 		case IP_DUMMYNET_DEL:
376 		case IP_DUMMYNET_FLUSH:
377 			if (DUMMYNET_LOADED)
378 				error = ip_dn_ctl_ptr(sopt);
379 			else
380 				error = ENOPROTOOPT ;
381 			break ;
382 
383 		case IP_RSVP_ON:
384 			error = ip_rsvp_init(so);
385 			break;
386 
387 		case IP_RSVP_OFF:
388 			error = ip_rsvp_done();
389 			break;
390 
391 			/* XXX - should be combined */
392 		case IP_RSVP_VIF_ON:
393 			error = ip_rsvp_vif_init(so, sopt);
394 			break;
395 
396 		case IP_RSVP_VIF_OFF:
397 			error = ip_rsvp_vif_done(so, sopt);
398 			break;
399 
400 		case MRT_INIT:
401 		case MRT_DONE:
402 		case MRT_ADD_VIF:
403 		case MRT_DEL_VIF:
404 		case MRT_ADD_MFC:
405 		case MRT_DEL_MFC:
406 		case MRT_VERSION:
407 		case MRT_ASSERT:
408 			error = ip_mrouter_set(so, sopt);
409 			break;
410 
411 		default:
412 			error = ip_ctloutput(so, sopt);
413 			break;
414 		}
415 		break;
416 	}
417 
418 	return (error);
419 }
420 
421 /*
422  * This function exists solely to receive the PRC_IFDOWN messages which
423  * are sent by if_down().  It looks for an ifaddr whose ifa_addr is sa,
424  * and calls in_ifadown() to remove all routes corresponding to that address.
425  * It also receives the PRC_IFUP messages from if_up() and reinstalls the
426  * interface routes.
427  */
428 void
429 rip_ctlinput(cmd, sa, vip)
430 	int cmd;
431 	struct sockaddr *sa;
432 	void *vip;
433 {
434 	struct in_ifaddr *ia;
435 	struct ifnet *ifp;
436 	int err;
437 	int flags;
438 
439 	switch (cmd) {
440 	case PRC_IFDOWN:
441 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
442 			if (ia->ia_ifa.ifa_addr == sa
443 			    && (ia->ia_flags & IFA_ROUTE)) {
444 				/*
445 				 * in_ifscrub kills the interface route.
446 				 */
447 				in_ifscrub(ia->ia_ifp, ia);
448 				/*
449 				 * in_ifadown gets rid of all the rest of
450 				 * the routes.  This is not quite the right
451 				 * thing to do, but at least if we are running
452 				 * a routing process they will come back.
453 				 */
454 				in_ifadown(&ia->ia_ifa, 0);
455 				break;
456 			}
457 		}
458 		break;
459 
460 	case PRC_IFUP:
461 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
462 			if (ia->ia_ifa.ifa_addr == sa)
463 				break;
464 		}
465 		if (ia == 0 || (ia->ia_flags & IFA_ROUTE))
466 			return;
467 		flags = RTF_UP;
468 		ifp = ia->ia_ifa.ifa_ifp;
469 
470 		if ((ifp->if_flags & IFF_LOOPBACK)
471 		    || (ifp->if_flags & IFF_POINTOPOINT))
472 			flags |= RTF_HOST;
473 
474 		err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
475 		if (err == 0)
476 			ia->ia_flags |= IFA_ROUTE;
477 		break;
478 	}
479 }
480 
481 u_long	rip_sendspace = RIPSNDQ;
482 u_long	rip_recvspace = RIPRCVQ;
483 
484 SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
485     &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
486 SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
487     &rip_recvspace, 0, "Maximum incoming raw IP datagram size");
488 
489 static int
490 rip_attach(struct socket *so, int proto, struct thread *td)
491 {
492 	struct inpcb *inp;
493 	int error, s;
494 
495 	inp = sotoinpcb(so);
496 	if (inp)
497 		panic("rip_attach");
498 	if (td && (error = suser(td)) != 0)
499 		return error;
500 
501 	error = soreserve(so, rip_sendspace, rip_recvspace);
502 	if (error)
503 		return error;
504 	s = splnet();
505 	error = in_pcballoc(so, &ripcbinfo, td);
506 	splx(s);
507 	if (error)
508 		return error;
509 	inp = (struct inpcb *)so->so_pcb;
510 	inp->inp_vflag |= INP_IPV4;
511 	inp->inp_ip_p = proto;
512 	inp->inp_ip_ttl = ip_defttl;
513 	return 0;
514 }
515 
516 static int
517 rip_detach(struct socket *so)
518 {
519 	struct inpcb *inp;
520 
521 	inp = sotoinpcb(so);
522 	if (inp == 0)
523 		panic("rip_detach");
524 	if (so == ip_mrouter)
525 		ip_mrouter_done();
526 	ip_rsvp_force_done(so);
527 	if (so == ip_rsvpd)
528 		ip_rsvp_done();
529 	in_pcbdetach(inp);
530 	return 0;
531 }
532 
533 static int
534 rip_abort(struct socket *so)
535 {
536 	soisdisconnected(so);
537 	return rip_detach(so);
538 }
539 
540 static int
541 rip_disconnect(struct socket *so)
542 {
543 	if ((so->so_state & SS_ISCONNECTED) == 0)
544 		return ENOTCONN;
545 	return rip_abort(so);
546 }
547 
548 static int
549 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
550 {
551 	struct inpcb *inp = sotoinpcb(so);
552 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
553 
554 	if (nam->sa_len != sizeof(*addr))
555 		return EINVAL;
556 
557 	if (TAILQ_EMPTY(&ifnet) || ((addr->sin_family != AF_INET) &&
558 				    (addr->sin_family != AF_IMPLINK)) ||
559 	    (addr->sin_addr.s_addr &&
560 	     ifa_ifwithaddr((struct sockaddr *)addr) == 0))
561 		return EADDRNOTAVAIL;
562 	inp->inp_laddr = addr->sin_addr;
563 	return 0;
564 }
565 
566 static int
567 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
568 {
569 	struct inpcb *inp = sotoinpcb(so);
570 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
571 
572 	if (nam->sa_len != sizeof(*addr))
573 		return EINVAL;
574 	if (TAILQ_EMPTY(&ifnet))
575 		return EADDRNOTAVAIL;
576 	if ((addr->sin_family != AF_INET) &&
577 	    (addr->sin_family != AF_IMPLINK))
578 		return EAFNOSUPPORT;
579 	inp->inp_faddr = addr->sin_addr;
580 	soisconnected(so);
581 	return 0;
582 }
583 
584 static int
585 rip_shutdown(struct socket *so)
586 {
587 	socantsendmore(so);
588 	return 0;
589 }
590 
591 static int
592 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
593 	 struct mbuf *control, struct thread *td)
594 {
595 	struct inpcb *inp = sotoinpcb(so);
596 	register u_long dst;
597 
598 	if (so->so_state & SS_ISCONNECTED) {
599 		if (nam) {
600 			m_freem(m);
601 			return EISCONN;
602 		}
603 		dst = inp->inp_faddr.s_addr;
604 	} else {
605 		if (nam == NULL) {
606 			m_freem(m);
607 			return ENOTCONN;
608 		}
609 		dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
610 	}
611 	return rip_output(m, so, dst);
612 }
613 
614 static int
615 rip_pcblist(SYSCTL_HANDLER_ARGS)
616 {
617 	int error, i, n, s;
618 	struct inpcb *inp, **inp_list;
619 	inp_gen_t gencnt;
620 	struct xinpgen xig;
621 
622 	/*
623 	 * The process of preparing the TCB list is too time-consuming and
624 	 * resource-intensive to repeat twice on every request.
625 	 */
626 	if (req->oldptr == 0) {
627 		n = ripcbinfo.ipi_count;
628 		req->oldidx = 2 * (sizeof xig)
629 			+ (n + n/8) * sizeof(struct xinpcb);
630 		return 0;
631 	}
632 
633 	if (req->newptr != 0)
634 		return EPERM;
635 
636 	/*
637 	 * OK, now we're committed to doing something.
638 	 */
639 	s = splnet();
640 	gencnt = ripcbinfo.ipi_gencnt;
641 	n = ripcbinfo.ipi_count;
642 	splx(s);
643 
644 	xig.xig_len = sizeof xig;
645 	xig.xig_count = n;
646 	xig.xig_gen = gencnt;
647 	xig.xig_sogen = so_gencnt;
648 	error = SYSCTL_OUT(req, &xig, sizeof xig);
649 	if (error)
650 		return error;
651 
652 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
653 	if (inp_list == 0)
654 		return ENOMEM;
655 
656 	s = splnet();
657 	for (inp = LIST_FIRST(ripcbinfo.listhead), i = 0; inp && i < n;
658 	     inp = LIST_NEXT(inp, inp_list)) {
659 		if (inp->inp_gencnt <= gencnt) {
660 			if (cr_canseesocket(req->td->td_ucred,
661 			    inp->inp_socket))
662 				continue;
663 			inp_list[i++] = inp;
664 		}
665 	}
666 	splx(s);
667 	n = i;
668 
669 	error = 0;
670 	for (i = 0; i < n; i++) {
671 		inp = inp_list[i];
672 		if (inp->inp_gencnt <= gencnt) {
673 			struct xinpcb xi;
674 			xi.xi_len = sizeof xi;
675 			/* XXX should avoid extra copy */
676 			bcopy(inp, &xi.xi_inp, sizeof *inp);
677 			if (inp->inp_socket)
678 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
679 			error = SYSCTL_OUT(req, &xi, sizeof xi);
680 		}
681 	}
682 	if (!error) {
683 		/*
684 		 * Give the user an updated idea of our state.
685 		 * If the generation differs from what we told
686 		 * her before, she knows that something happened
687 		 * while we were processing this request, and it
688 		 * might be necessary to retry.
689 		 */
690 		s = splnet();
691 		xig.xig_gen = ripcbinfo.ipi_gencnt;
692 		xig.xig_sogen = so_gencnt;
693 		xig.xig_count = ripcbinfo.ipi_count;
694 		splx(s);
695 		error = SYSCTL_OUT(req, &xig, sizeof xig);
696 	}
697 	free(inp_list, M_TEMP);
698 	return error;
699 }
700 
701 /*
702  * This is the wrapper function for in_setsockaddr.  We just pass down
703  * the pcbinfo for in_setpeeraddr to lock.
704  */
705 static int
706 rip_sockaddr(struct socket *so, struct sockaddr **nam)
707 {
708 	return (in_setsockaddr(so, nam, &ripcbinfo));
709 }
710 
711 /*
712  * This is the wrapper function for in_setpeeraddr.  We just pass down
713  * the pcbinfo for in_setpeeraddr to lock.
714  */
715 static int
716 rip_peeraddr(struct socket *so, struct sockaddr **nam)
717 {
718 	return (in_setpeeraddr(so, nam, &ripcbinfo));
719 }
720 
721 
722 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
723 	    rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
724 
725 struct pr_usrreqs rip_usrreqs = {
726 	rip_abort, pru_accept_notsupp, rip_attach, rip_bind, rip_connect,
727 	pru_connect2_notsupp, in_control, rip_detach, rip_disconnect,
728 	pru_listen_notsupp, rip_peeraddr, pru_rcvd_notsupp,
729 	pru_rcvoob_notsupp, rip_send, pru_sense_null, rip_shutdown,
730 	rip_sockaddr, sosend, soreceive, sopoll
731 };
732