2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 4. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 #include "opt_inet6.h"
38 #include "opt_ipsec.h"
40 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/eventhandler.h>
45 #include <sys/malloc.h>
49 #include <sys/protosw.h>
50 #include <sys/rmlock.h>
51 #include <sys/rwlock.h>
52 #include <sys/signalvar.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
56 #include <sys/sysctl.h>
57 #include <sys/systm.h>
62 #include <net/if_var.h>
63 #include <net/route.h>
66 #include <netinet/in.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/in_pcb.h>
69 #include <netinet/in_var.h>
70 #include <netinet/if_ether.h>
71 #include <netinet/ip.h>
72 #include <netinet/ip_var.h>
73 #include <netinet/ip_mroute.h>
74 #include <netinet/ip_icmp.h>
76 #include <netipsec/ipsec_support.h>
78 #include <machine/stdarg.h>
79 #include <security/mac/mac_framework.h>
81 VNET_DEFINE(int, ip_defttl) = IPDEFTTL;
82 SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_VNET | CTLFLAG_RW,
83 &VNET_NAME(ip_defttl), 0,
84 "Maximum TTL on IP packets");
86 VNET_DEFINE(struct inpcbhead, ripcb);
87 VNET_DEFINE(struct inpcbinfo, ripcbinfo);
89 #define V_ripcb VNET(ripcb)
90 #define V_ripcbinfo VNET(ripcbinfo)
93 * Control and data hooks for ipfw, dummynet, divert and so on.
94 * The data hooks are not used here but it is convenient
95 * to keep them all in one place.
97 VNET_DEFINE(ip_fw_chk_ptr_t, ip_fw_chk_ptr) = NULL;
98 VNET_DEFINE(ip_fw_ctl_ptr_t, ip_fw_ctl_ptr) = NULL;
100 int (*ip_dn_ctl_ptr)(struct sockopt *);
101 int (*ip_dn_io_ptr)(struct mbuf **, int, struct ip_fw_args *);
102 void (*ip_divert_ptr)(struct mbuf *, int);
103 int (*ng_ipfw_input_p)(struct mbuf **, int,
104 struct ip_fw_args *, int);
108 * Hooks for multicast routing. They all default to NULL, so leave them not
109 * initialized and rely on BSS being set to 0.
113 * The socket used to communicate with the multicast routing daemon.
115 VNET_DEFINE(struct socket *, ip_mrouter);
118 * The various mrouter and rsvp functions.
120 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
121 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
122 int (*ip_mrouter_done)(void);
123 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
124 struct ip_moptions *);
125 int (*mrt_ioctl)(u_long, caddr_t, int);
126 int (*legal_vif_num)(int);
127 u_long (*ip_mcast_src)(int);
129 int (*rsvp_input_p)(struct mbuf **, int *, int);
130 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
131 void (*ip_rsvp_force_done)(struct socket *);
134 extern struct protosw inetsw[];
136 u_long rip_sendspace = 9216;
137 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
138 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
140 u_long rip_recvspace = 9216;
141 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
142 &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
148 #define INP_PCBHASH_RAW_SIZE 256
149 #define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \
150 (((proto) + (laddr) + (faddr)) % (mask) + 1)
154 rip_inshash(struct inpcb *inp)
156 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
157 struct inpcbhead *pcbhash;
160 INP_INFO_WLOCK_ASSERT(pcbinfo);
161 INP_WLOCK_ASSERT(inp);
163 if (inp->inp_ip_p != 0 &&
164 inp->inp_laddr.s_addr != INADDR_ANY &&
165 inp->inp_faddr.s_addr != INADDR_ANY) {
166 hash = INP_PCBHASH_RAW(inp->inp_ip_p, inp->inp_laddr.s_addr,
167 inp->inp_faddr.s_addr, pcbinfo->ipi_hashmask);
170 pcbhash = &pcbinfo->ipi_hashbase[hash];
171 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
175 rip_delhash(struct inpcb *inp)
178 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
179 INP_WLOCK_ASSERT(inp);
181 LIST_REMOVE(inp, inp_hash);
186 * Raw interface to IP protocol.
190 * Initialize raw connection block q.
193 rip_zone_change(void *tag)
196 uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
200 rip_inpcb_init(void *mem, int size, int flags)
202 struct inpcb *inp = mem;
204 INP_LOCK_INIT(inp, "inp", "rawinp");
212 in_pcbinfo_init(&V_ripcbinfo, "rip", &V_ripcb, INP_PCBHASH_RAW_SIZE,
213 1, "ripcb", rip_inpcb_init, NULL, 0, IPI_HASHFIELDS_NONE);
214 EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change, NULL,
215 EVENTHANDLER_PRI_ANY);
220 rip_destroy(void *unused __unused)
223 in_pcbinfo_destroy(&V_ripcbinfo);
225 VNET_SYSUNINIT(raw_ip, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, rip_destroy, NULL);
230 rip_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
231 struct sockaddr_in *ripsrc)
235 INP_LOCK_ASSERT(last);
237 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
238 /* check AH/ESP integrity. */
239 if (IPSEC_ENABLED(ipv4)) {
240 if (IPSEC_CHECK_POLICY(ipv4, n, last) != 0)
245 if (!policyfail && mac_inpcb_check_deliver(last, n) != 0)
248 /* Check the minimum TTL for socket. */
249 if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
252 struct mbuf *opts = NULL;
255 so = last->inp_socket;
256 if ((last->inp_flags & INP_CONTROLOPTS) ||
257 (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
258 ip_savecontrol(last, &opts, ip, n);
259 SOCKBUF_LOCK(&so->so_rcv);
260 if (sbappendaddr_locked(&so->so_rcv,
261 (struct sockaddr *)ripsrc, n, opts) == 0) {
262 /* should notify about lost packet */
266 SOCKBUF_UNLOCK(&so->so_rcv);
268 sorwakeup_locked(so);
275 * Setup generic address and protocol structures for raw_input routine, then
276 * pass them along with mbuf chain.
279 rip_input(struct mbuf **mp, int *offp, int proto)
282 struct mbuf *m = *mp;
283 struct ip *ip = mtod(m, struct ip *);
284 struct inpcb *inp, *last;
285 struct sockaddr_in ripsrc;
290 bzero(&ripsrc, sizeof(ripsrc));
291 ripsrc.sin_len = sizeof(ripsrc);
292 ripsrc.sin_family = AF_INET;
293 ripsrc.sin_addr = ip->ip_src;
296 ifp = m->m_pkthdr.rcvif;
298 hash = INP_PCBHASH_RAW(proto, ip->ip_src.s_addr,
299 ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask);
300 INP_INFO_RLOCK(&V_ripcbinfo);
301 LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[hash], inp_hash) {
302 if (inp->inp_ip_p != proto)
305 /* XXX inp locking */
306 if ((inp->inp_vflag & INP_IPV4) == 0)
309 if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
311 if (inp->inp_faddr.s_addr != ip->ip_src.s_addr)
313 if (jailed_without_vnet(inp->inp_cred)) {
315 * XXX: If faddr was bound to multicast group,
316 * jailed raw socket will drop datagram.
318 if (prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
324 n = m_copy(m, 0, (int)M_COPYALL);
326 (void) rip_append(last, ip, n, &ripsrc);
327 /* XXX count dropped packet */
333 LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[0], inp_hash) {
334 if (inp->inp_ip_p && inp->inp_ip_p != proto)
337 /* XXX inp locking */
338 if ((inp->inp_vflag & INP_IPV4) == 0)
341 if (!in_nullhost(inp->inp_laddr) &&
342 !in_hosteq(inp->inp_laddr, ip->ip_dst))
344 if (!in_nullhost(inp->inp_faddr) &&
345 !in_hosteq(inp->inp_faddr, ip->ip_src))
347 if (jailed_without_vnet(inp->inp_cred)) {
349 * Allow raw socket in jail to receive multicast;
350 * assume process had PRIV_NETINET_RAW at attach,
351 * and fall through into normal filter path if so.
353 if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
354 prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
358 * If this raw socket has multicast state, and we
359 * have received a multicast, check if this socket
360 * should receive it, as multicast filtering is now
361 * the responsibility of the transport layer.
363 if (inp->inp_moptions != NULL &&
364 IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
366 * If the incoming datagram is for IGMP, allow it
367 * through unconditionally to the raw socket.
369 * In the case of IGMPv2, we may not have explicitly
370 * joined the group, and may have set IFF_ALLMULTI
371 * on the interface. imo_multi_filter() may discard
372 * control traffic we actually need to see.
374 * Userland multicast routing daemons should continue
375 * filter the control traffic appropriately.
379 blocked = MCAST_PASS;
380 if (proto != IPPROTO_IGMP) {
381 struct sockaddr_in group;
383 bzero(&group, sizeof(struct sockaddr_in));
384 group.sin_len = sizeof(struct sockaddr_in);
385 group.sin_family = AF_INET;
386 group.sin_addr = ip->ip_dst;
388 blocked = imo_multi_filter(inp->inp_moptions,
390 (struct sockaddr *)&group,
391 (struct sockaddr *)&ripsrc);
394 if (blocked != MCAST_PASS) {
395 IPSTAT_INC(ips_notmember);
402 n = m_copy(m, 0, (int)M_COPYALL);
404 (void) rip_append(last, ip, n, &ripsrc);
405 /* XXX count dropped packet */
411 INP_INFO_RUNLOCK(&V_ripcbinfo);
413 if (rip_append(last, ip, m, &ripsrc) != 0)
414 IPSTAT_INC(ips_delivered);
417 if (inetsw[ip_protox[ip->ip_p]].pr_input == rip_input) {
418 IPSTAT_INC(ips_noproto);
419 IPSTAT_DEC(ips_delivered);
420 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL, 0, 0);
425 return (IPPROTO_DONE);
429 * Generate IP header and pass packet to ip_output. Tack on options user may
430 * have setup with control call.
433 rip_output(struct mbuf *m, struct socket *so, ...)
437 struct inpcb *inp = sotoinpcb(so);
440 int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
443 u_char opttype, optlen, *cp;
446 dst = va_arg(ap, u_long);
450 * If the user handed us a complete IP packet, use it. Otherwise,
451 * allocate an mbuf for a header and fill it in.
453 if ((inp->inp_flags & INP_HDRINCL) == 0) {
454 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
458 M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
463 ip = mtod(m, struct ip *);
464 ip->ip_tos = inp->inp_ip_tos;
465 if (inp->inp_flags & INP_DONTFRAG)
466 ip->ip_off = htons(IP_DF);
468 ip->ip_off = htons(0);
469 ip->ip_p = inp->inp_ip_p;
470 ip->ip_len = htons(m->m_pkthdr.len);
471 ip->ip_src = inp->inp_laddr;
472 ip->ip_dst.s_addr = dst;
473 if (jailed(inp->inp_cred)) {
475 * prison_local_ip4() would be good enough but would
476 * let a source of INADDR_ANY pass, which we do not
477 * want to see from jails.
479 if (ip->ip_src.s_addr == INADDR_ANY) {
480 error = in_pcbladdr(inp, &ip->ip_dst, &ip->ip_src,
483 error = prison_local_ip4(inp->inp_cred,
492 ip->ip_ttl = inp->inp_ip_ttl;
494 if (m->m_pkthdr.len > IP_MAXPACKET) {
498 ip = mtod(m, struct ip *);
499 hlen = ip->ip_hl << 2;
500 if (m->m_len < hlen) {
501 m = m_pullup(m, hlen);
504 ip = mtod(m, struct ip *);
509 * Don't allow both user specified and setsockopt options,
510 * and don't allow packet length sizes that will crash.
512 if ((hlen < sizeof (*ip))
513 || ((hlen > sizeof (*ip)) && inp->inp_options)
514 || (ntohs(ip->ip_len) != m->m_pkthdr.len)) {
519 error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
526 * Don't allow IP options which do not have the required
527 * structure as specified in section 3.1 of RFC 791 on
530 cp = (u_char *)(ip + 1);
531 cnt = hlen - sizeof (struct ip);
532 for (; cnt > 0; cnt -= optlen, cp += optlen) {
533 opttype = cp[IPOPT_OPTVAL];
534 if (opttype == IPOPT_EOL)
536 if (opttype == IPOPT_NOP) {
540 if (cnt < IPOPT_OLEN + sizeof(u_char)) {
545 optlen = cp[IPOPT_OLEN];
546 if (optlen < IPOPT_OLEN + sizeof(u_char) ||
554 * This doesn't allow application to specify ID of zero,
555 * but we got this limitation from the beginning of history.
561 * XXX prevent ip_output from overwriting header fields.
563 flags |= IP_RAWOUTPUT;
564 IPSTAT_INC(ips_rawout);
567 if (inp->inp_flags & INP_ONESBCAST)
568 flags |= IP_SENDONES;
571 mac_inpcb_create_mbuf(inp, m);
574 error = ip_output(m, inp->inp_options, NULL, flags,
575 inp->inp_moptions, inp);
581 * Raw IP socket option processing.
583 * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
584 * only be created by a privileged process, and as such, socket option
585 * operations to manage system properties on any raw socket were allowed to
586 * take place without explicit additional access control checks. However,
587 * raw sockets can now also be created in jail(), and therefore explicit
588 * checks are now required. Likewise, raw sockets can be used by a process
589 * after it gives up privilege, so some caution is required. For options
590 * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
591 * performed in ip_ctloutput() and therefore no check occurs here.
592 * Unilaterally checking priv_check() here breaks normal IP socket option
593 * operations on raw sockets.
595 * When adding new socket options here, make sure to add access control
596 * checks here as necessary.
598 * XXX-BZ inp locking?
601 rip_ctloutput(struct socket *so, struct sockopt *sopt)
603 struct inpcb *inp = sotoinpcb(so);
606 if (sopt->sopt_level != IPPROTO_IP) {
607 if ((sopt->sopt_level == SOL_SOCKET) &&
608 (sopt->sopt_name == SO_SETFIB)) {
609 inp->inp_inc.inc_fibnum = so->so_fibnum;
616 switch (sopt->sopt_dir) {
618 switch (sopt->sopt_name) {
620 optval = inp->inp_flags & INP_HDRINCL;
621 error = sooptcopyout(sopt, &optval, sizeof optval);
624 case IP_FW3: /* generic ipfw v.3 functions */
625 case IP_FW_ADD: /* ADD actually returns the body... */
627 case IP_FW_TABLE_GETSIZE:
628 case IP_FW_TABLE_LIST:
629 case IP_FW_NAT_GET_CONFIG:
630 case IP_FW_NAT_GET_LOG:
631 if (V_ip_fw_ctl_ptr != NULL)
632 error = V_ip_fw_ctl_ptr(sopt);
637 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
638 case IP_DUMMYNET_GET:
639 if (ip_dn_ctl_ptr != NULL)
640 error = ip_dn_ctl_ptr(sopt);
653 case MRT_API_SUPPORT:
655 case MRT_ADD_BW_UPCALL:
656 case MRT_DEL_BW_UPCALL:
657 error = priv_check(curthread, PRIV_NETINET_MROUTE);
660 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
665 error = ip_ctloutput(so, sopt);
671 switch (sopt->sopt_name) {
673 error = sooptcopyin(sopt, &optval, sizeof optval,
678 inp->inp_flags |= INP_HDRINCL;
680 inp->inp_flags &= ~INP_HDRINCL;
683 case IP_FW3: /* generic ipfw v.3 functions */
689 case IP_FW_TABLE_ADD:
690 case IP_FW_TABLE_DEL:
691 case IP_FW_TABLE_FLUSH:
694 if (V_ip_fw_ctl_ptr != NULL)
695 error = V_ip_fw_ctl_ptr(sopt);
700 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
701 case IP_DUMMYNET_CONFIGURE:
702 case IP_DUMMYNET_DEL:
703 case IP_DUMMYNET_FLUSH:
704 if (ip_dn_ctl_ptr != NULL)
705 error = ip_dn_ctl_ptr(sopt);
707 error = ENOPROTOOPT ;
711 error = priv_check(curthread, PRIV_NETINET_MROUTE);
714 error = ip_rsvp_init(so);
718 error = priv_check(curthread, PRIV_NETINET_MROUTE);
721 error = ip_rsvp_done();
725 case IP_RSVP_VIF_OFF:
726 error = priv_check(curthread, PRIV_NETINET_MROUTE);
729 error = ip_rsvp_vif ?
730 ip_rsvp_vif(so, sopt) : EINVAL;
741 case MRT_API_SUPPORT:
743 case MRT_ADD_BW_UPCALL:
744 case MRT_DEL_BW_UPCALL:
745 error = priv_check(curthread, PRIV_NETINET_MROUTE);
748 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
753 error = ip_ctloutput(so, sopt);
763 * This function exists solely to receive the PRC_IFDOWN messages which are
764 * sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, and calls
765 * in_ifadown() to remove all routes corresponding to that address. It also
766 * receives the PRC_IFUP messages from if_up() and reinstalls the interface
770 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
772 struct rm_priotracker in_ifa_tracker;
773 struct in_ifaddr *ia;
780 IN_IFADDR_RLOCK(&in_ifa_tracker);
781 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
782 if (ia->ia_ifa.ifa_addr == sa
783 && (ia->ia_flags & IFA_ROUTE)) {
784 ifa_ref(&ia->ia_ifa);
785 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
787 * in_scrubprefix() kills the interface route.
789 in_scrubprefix(ia, 0);
791 * in_ifadown gets rid of all the rest of the
792 * routes. This is not quite the right thing
793 * to do, but at least if we are running a
794 * routing process they will come back.
796 in_ifadown(&ia->ia_ifa, 0);
797 ifa_free(&ia->ia_ifa);
801 if (ia == NULL) /* If ia matched, already unlocked. */
802 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
806 IN_IFADDR_RLOCK(&in_ifa_tracker);
807 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
808 if (ia->ia_ifa.ifa_addr == sa)
811 if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
812 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
815 ifa_ref(&ia->ia_ifa);
816 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
818 ifp = ia->ia_ifa.ifa_ifp;
820 if ((ifp->if_flags & IFF_LOOPBACK)
821 || (ifp->if_flags & IFF_POINTOPOINT))
824 err = ifa_del_loopback_route((struct ifaddr *)ia, sa);
826 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
828 ia->ia_flags |= IFA_ROUTE;
830 err = ifa_add_loopback_route((struct ifaddr *)ia, sa);
832 ifa_free(&ia->ia_ifa);
838 rip_attach(struct socket *so, int proto, struct thread *td)
844 KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
846 error = priv_check(td, PRIV_NETINET_RAW);
849 if (proto >= IPPROTO_MAX || proto < 0)
850 return EPROTONOSUPPORT;
851 error = soreserve(so, rip_sendspace, rip_recvspace);
854 INP_INFO_WLOCK(&V_ripcbinfo);
855 error = in_pcballoc(so, &V_ripcbinfo);
857 INP_INFO_WUNLOCK(&V_ripcbinfo);
860 inp = (struct inpcb *)so->so_pcb;
861 inp->inp_vflag |= INP_IPV4;
862 inp->inp_ip_p = proto;
863 inp->inp_ip_ttl = V_ip_defttl;
865 INP_INFO_WUNLOCK(&V_ripcbinfo);
871 rip_detach(struct socket *so)
876 KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
877 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
878 ("rip_detach: not closed"));
880 INP_INFO_WLOCK(&V_ripcbinfo);
883 if (so == V_ip_mrouter && ip_mrouter_done)
885 if (ip_rsvp_force_done)
886 ip_rsvp_force_done(so);
887 if (so == V_ip_rsvpd)
891 INP_INFO_WUNLOCK(&V_ripcbinfo);
895 rip_dodisconnect(struct socket *so, struct inpcb *inp)
897 struct inpcbinfo *pcbinfo;
899 pcbinfo = inp->inp_pcbinfo;
900 INP_INFO_WLOCK(pcbinfo);
903 inp->inp_faddr.s_addr = INADDR_ANY;
906 so->so_state &= ~SS_ISCONNECTED;
909 INP_INFO_WUNLOCK(pcbinfo);
913 rip_abort(struct socket *so)
918 KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
920 rip_dodisconnect(so, inp);
924 rip_close(struct socket *so)
929 KASSERT(inp != NULL, ("rip_close: inp == NULL"));
931 rip_dodisconnect(so, inp);
935 rip_disconnect(struct socket *so)
939 if ((so->so_state & SS_ISCONNECTED) == 0)
943 KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
945 rip_dodisconnect(so, inp);
950 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
952 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
956 if (nam->sa_len != sizeof(*addr))
959 error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
964 KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
966 if (TAILQ_EMPTY(&V_ifnet) ||
967 (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
968 (addr->sin_addr.s_addr &&
969 (inp->inp_flags & INP_BINDANY) == 0 &&
970 ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
971 return (EADDRNOTAVAIL);
973 INP_INFO_WLOCK(&V_ripcbinfo);
976 inp->inp_laddr = addr->sin_addr;
979 INP_INFO_WUNLOCK(&V_ripcbinfo);
984 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
986 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
989 if (nam->sa_len != sizeof(*addr))
991 if (TAILQ_EMPTY(&V_ifnet))
992 return (EADDRNOTAVAIL);
993 if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
994 return (EAFNOSUPPORT);
997 KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
999 INP_INFO_WLOCK(&V_ripcbinfo);
1002 inp->inp_faddr = addr->sin_addr;
1006 INP_INFO_WUNLOCK(&V_ripcbinfo);
1011 rip_shutdown(struct socket *so)
1015 inp = sotoinpcb(so);
1016 KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
1025 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
1026 struct mbuf *control, struct thread *td)
1031 inp = sotoinpcb(so);
1032 KASSERT(inp != NULL, ("rip_send: inp == NULL"));
1035 * Note: 'dst' reads below are unlocked.
1037 if (so->so_state & SS_ISCONNECTED) {
1042 dst = inp->inp_faddr.s_addr; /* Unlocked read. */
1048 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
1050 return (rip_output(m, so, dst));
1055 rip_pcblist(SYSCTL_HANDLER_ARGS)
1058 struct inpcb *inp, **inp_list;
1063 * The process of preparing the TCB list is too time-consuming and
1064 * resource-intensive to repeat twice on every request.
1066 if (req->oldptr == 0) {
1067 n = V_ripcbinfo.ipi_count;
1068 n += imax(n / 8, 10);
1069 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
1073 if (req->newptr != 0)
1077 * OK, now we're committed to doing something.
1079 INP_INFO_RLOCK(&V_ripcbinfo);
1080 gencnt = V_ripcbinfo.ipi_gencnt;
1081 n = V_ripcbinfo.ipi_count;
1082 INP_INFO_RUNLOCK(&V_ripcbinfo);
1084 bzero(&xig, sizeof(xig));
1085 xig.xig_len = sizeof xig;
1087 xig.xig_gen = gencnt;
1088 xig.xig_sogen = so_gencnt;
1089 error = SYSCTL_OUT(req, &xig, sizeof xig);
1093 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
1095 INP_INFO_RLOCK(&V_ripcbinfo);
1096 for (inp = LIST_FIRST(V_ripcbinfo.ipi_listhead), i = 0; inp && i < n;
1097 inp = LIST_NEXT(inp, inp_list)) {
1099 if (inp->inp_gencnt <= gencnt &&
1100 cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
1102 inp_list[i++] = inp;
1106 INP_INFO_RUNLOCK(&V_ripcbinfo);
1110 for (i = 0; i < n; i++) {
1113 if (inp->inp_gencnt <= gencnt) {
1116 bzero(&xi, sizeof(xi));
1117 xi.xi_len = sizeof xi;
1118 /* XXX should avoid extra copy */
1119 bcopy(inp, &xi.xi_inp, sizeof *inp);
1120 if (inp->inp_socket)
1121 sotoxsocket(inp->inp_socket, &xi.xi_socket);
1123 error = SYSCTL_OUT(req, &xi, sizeof xi);
1127 INP_INFO_WLOCK(&V_ripcbinfo);
1128 for (i = 0; i < n; i++) {
1131 if (!in_pcbrele_rlocked(inp))
1134 INP_INFO_WUNLOCK(&V_ripcbinfo);
1138 * Give the user an updated idea of our state. If the
1139 * generation differs from what we told her before, she knows
1140 * that something happened while we were processing this
1141 * request, and it might be necessary to retry.
1143 INP_INFO_RLOCK(&V_ripcbinfo);
1144 xig.xig_gen = V_ripcbinfo.ipi_gencnt;
1145 xig.xig_sogen = so_gencnt;
1146 xig.xig_count = V_ripcbinfo.ipi_count;
1147 INP_INFO_RUNLOCK(&V_ripcbinfo);
1148 error = SYSCTL_OUT(req, &xig, sizeof xig);
1150 free(inp_list, M_TEMP);
1154 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist,
1155 CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0,
1156 rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
1159 struct pr_usrreqs rip_usrreqs = {
1160 .pru_abort = rip_abort,
1161 .pru_attach = rip_attach,
1162 .pru_bind = rip_bind,
1163 .pru_connect = rip_connect,
1164 .pru_control = in_control,
1165 .pru_detach = rip_detach,
1166 .pru_disconnect = rip_disconnect,
1167 .pru_peeraddr = in_getpeeraddr,
1168 .pru_send = rip_send,
1169 .pru_shutdown = rip_shutdown,
1170 .pru_sockaddr = in_getsockaddr,
1171 .pru_sosetlabel = in_pcbsosetlabel,
1172 .pru_close = rip_close,