2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1988, 1993
5 * The Regents of the University of California.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 #include "opt_inet6.h"
40 #include "opt_ipsec.h"
41 #include "opt_route.h"
43 #include <sys/param.h>
45 #include <sys/kernel.h>
46 #include <sys/eventhandler.h>
48 #include <sys/malloc.h>
52 #include <sys/protosw.h>
53 #include <sys/rwlock.h>
54 #include <sys/signalvar.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
58 #include <sys/sysctl.h>
59 #include <sys/systm.h>
64 #include <net/if_var.h>
65 #include <net/route.h>
66 #include <net/route/route_ctl.h>
69 #include <netinet/in.h>
70 #include <netinet/in_systm.h>
71 #include <netinet/in_fib.h>
72 #include <netinet/in_pcb.h>
73 #include <netinet/in_var.h>
74 #include <netinet/if_ether.h>
75 #include <netinet/ip.h>
76 #include <netinet/ip_var.h>
77 #include <netinet/ip_mroute.h>
78 #include <netinet/ip_icmp.h>
80 #include <netipsec/ipsec_support.h>
82 #include <machine/stdarg.h>
83 #include <security/mac/mac_framework.h>
85 extern ipproto_input_t *ip_protox[];
87 VNET_DEFINE(int, ip_defttl) = IPDEFTTL;
88 SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_VNET | CTLFLAG_RW,
89 &VNET_NAME(ip_defttl), 0,
90 "Maximum TTL on IP packets");
92 VNET_DEFINE(struct inpcbinfo, ripcbinfo);
93 #define V_ripcbinfo VNET(ripcbinfo)
96 * Control and data hooks for ipfw, dummynet, divert and so on.
97 * The data hooks are not used here but it is convenient
98 * to keep them all in one place.
100 VNET_DEFINE(ip_fw_ctl_ptr_t, ip_fw_ctl_ptr) = NULL;
102 int (*ip_dn_ctl_ptr)(struct sockopt *);
103 int (*ip_dn_io_ptr)(struct mbuf **, struct ip_fw_args *);
104 void (*ip_divert_ptr)(struct mbuf *, bool);
105 int (*ng_ipfw_input_p)(struct mbuf **, struct ip_fw_args *, bool);
109 * Hooks for multicast routing. They all default to NULL, so leave them not
110 * initialized and rely on BSS being set to 0.
114 * The socket used to communicate with the multicast routing daemon.
116 VNET_DEFINE(struct socket *, ip_mrouter);
119 * The various mrouter and rsvp functions.
121 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
122 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
123 int (*ip_mrouter_done)(void);
124 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
125 struct ip_moptions *);
126 int (*mrt_ioctl)(u_long, caddr_t, int);
127 int (*legal_vif_num)(int);
128 u_long (*ip_mcast_src)(int);
130 int (*rsvp_input_p)(struct mbuf **, int *, int);
131 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
132 void (*ip_rsvp_force_done)(struct socket *);
135 u_long rip_sendspace = 9216;
136 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
137 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
139 u_long rip_recvspace = 9216;
140 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
141 &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
147 #define INP_PCBHASH_RAW_SIZE 256
148 #define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \
149 (((proto) + (laddr) + (faddr)) % (mask) + 1)
153 rip_inshash(struct inpcb *inp)
155 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
156 struct inpcbhead *pcbhash;
159 INP_HASH_WLOCK_ASSERT(pcbinfo);
160 INP_WLOCK_ASSERT(inp);
162 if (inp->inp_ip_p != 0 &&
163 inp->inp_laddr.s_addr != INADDR_ANY &&
164 inp->inp_faddr.s_addr != INADDR_ANY) {
165 hash = INP_PCBHASH_RAW(inp->inp_ip_p, inp->inp_laddr.s_addr,
166 inp->inp_faddr.s_addr, pcbinfo->ipi_hashmask);
169 pcbhash = &pcbinfo->ipi_hashbase[hash];
170 CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
174 rip_delhash(struct inpcb *inp)
177 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
178 INP_WLOCK_ASSERT(inp);
180 CK_LIST_REMOVE(inp, inp_hash);
184 INPCBSTORAGE_DEFINE(ripcbstor, inpcb, "rawinp", "ripcb", "rip", "riphash");
187 rip_init(void *arg __unused)
190 in_pcbinfo_init(&V_ripcbinfo, &ripcbstor, INP_PCBHASH_RAW_SIZE, 1);
192 VNET_SYSINIT(rip_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rip_init, NULL);
196 rip_destroy(void *unused __unused)
199 in_pcbinfo_destroy(&V_ripcbinfo);
201 VNET_SYSUNINIT(raw_ip, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, rip_destroy, NULL);
206 rip_append(struct inpcb *inp, struct ip *ip, struct mbuf *m,
207 struct sockaddr_in *ripsrc)
209 struct socket *so = inp->inp_socket;
210 struct mbuf *n, *opts = NULL;
212 INP_LOCK_ASSERT(inp);
214 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
215 /* check AH/ESP integrity. */
216 if (IPSEC_ENABLED(ipv4) && IPSEC_CHECK_POLICY(ipv4, m, inp) != 0)
220 if (mac_inpcb_check_deliver(inp, m) != 0)
223 /* Check the minimum TTL for socket. */
224 if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl)
227 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL)
230 if ((inp->inp_flags & INP_CONTROLOPTS) ||
231 (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
232 ip_savecontrol(inp, &opts, ip, n);
233 SOCKBUF_LOCK(&so->so_rcv);
234 if (sbappendaddr_locked(&so->so_rcv,
235 (struct sockaddr *)ripsrc, n, opts) == 0) {
236 soroverflow_locked(so);
242 sorwakeup_locked(so);
247 struct rip_inp_match_ctx {
253 rip_inp_match1(const struct inpcb *inp, void *v)
255 struct rip_inp_match_ctx *ctx = v;
257 if (inp->inp_ip_p != ctx->proto)
260 /* XXX inp locking */
261 if ((inp->inp_vflag & INP_IPV4) == 0)
264 if (inp->inp_laddr.s_addr != ctx->ip->ip_dst.s_addr)
266 if (inp->inp_faddr.s_addr != ctx->ip->ip_src.s_addr)
272 rip_inp_match2(const struct inpcb *inp, void *v)
274 struct rip_inp_match_ctx *ctx = v;
276 if (inp->inp_ip_p && inp->inp_ip_p != ctx->proto)
279 /* XXX inp locking */
280 if ((inp->inp_vflag & INP_IPV4) == 0)
283 if (!in_nullhost(inp->inp_laddr) &&
284 !in_hosteq(inp->inp_laddr, ctx->ip->ip_dst))
286 if (!in_nullhost(inp->inp_faddr) &&
287 !in_hosteq(inp->inp_faddr, ctx->ip->ip_src))
293 * Setup generic address and protocol structures for raw_input routine, then
294 * pass them along with mbuf chain.
297 rip_input(struct mbuf **mp, int *offp, int proto)
299 struct rip_inp_match_ctx ctx = {
300 .ip = mtod(*mp, struct ip *),
303 struct inpcb_iterator inpi = INP_ITERATOR(&V_ripcbinfo,
304 INPLOOKUP_RLOCKPCB, rip_inp_match1, &ctx);
306 struct mbuf *m = *mp;
308 struct sockaddr_in ripsrc;
314 bzero(&ripsrc, sizeof(ripsrc));
315 ripsrc.sin_len = sizeof(ripsrc);
316 ripsrc.sin_family = AF_INET;
317 ripsrc.sin_addr = ctx.ip->ip_src;
319 ifp = m->m_pkthdr.rcvif;
321 inpi.hash = INP_PCBHASH_RAW(proto, ctx.ip->ip_src.s_addr,
322 ctx.ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask);
323 while ((inp = inp_next(&inpi)) != NULL) {
324 INP_RLOCK_ASSERT(inp);
325 if (jailed_without_vnet(inp->inp_cred) &&
326 prison_check_ip4(inp->inp_cred, &ctx.ip->ip_dst) != 0) {
328 * XXX: If faddr was bound to multicast group,
329 * jailed raw socket will drop datagram.
333 appended += rip_append(inp, ctx.ip, m, &ripsrc);
337 inpi.match = rip_inp_match2;
338 MPASS(inpi.inp == NULL);
339 while ((inp = inp_next(&inpi)) != NULL) {
340 INP_RLOCK_ASSERT(inp);
341 if (jailed_without_vnet(inp->inp_cred) &&
342 !IN_MULTICAST(ntohl(ctx.ip->ip_dst.s_addr)) &&
343 prison_check_ip4(inp->inp_cred, &ctx.ip->ip_dst) != 0)
345 * Allow raw socket in jail to receive multicast;
346 * assume process had PRIV_NETINET_RAW at attach,
347 * and fall through into normal filter path if so.
351 * If this raw socket has multicast state, and we
352 * have received a multicast, check if this socket
353 * should receive it, as multicast filtering is now
354 * the responsibility of the transport layer.
356 if (inp->inp_moptions != NULL &&
357 IN_MULTICAST(ntohl(ctx.ip->ip_dst.s_addr))) {
359 * If the incoming datagram is for IGMP, allow it
360 * through unconditionally to the raw socket.
362 * In the case of IGMPv2, we may not have explicitly
363 * joined the group, and may have set IFF_ALLMULTI
364 * on the interface. imo_multi_filter() may discard
365 * control traffic we actually need to see.
367 * Userland multicast routing daemons should continue
368 * filter the control traffic appropriately.
372 blocked = MCAST_PASS;
373 if (proto != IPPROTO_IGMP) {
374 struct sockaddr_in group;
376 bzero(&group, sizeof(struct sockaddr_in));
377 group.sin_len = sizeof(struct sockaddr_in);
378 group.sin_family = AF_INET;
379 group.sin_addr = ctx.ip->ip_dst;
381 blocked = imo_multi_filter(inp->inp_moptions,
383 (struct sockaddr *)&group,
384 (struct sockaddr *)&ripsrc);
387 if (blocked != MCAST_PASS) {
388 IPSTAT_INC(ips_notmember);
392 appended += rip_append(inp, ctx.ip, m, &ripsrc);
394 if (appended == 0 && ip_protox[ctx.ip->ip_p] == rip_input) {
395 IPSTAT_INC(ips_noproto);
396 IPSTAT_DEC(ips_delivered);
397 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL, 0, 0);
400 return (IPPROTO_DONE);
404 * Generate IP header and pass packet to ip_output. Tack on options user may
405 * have setup with control call.
408 rip_send(struct socket *so, int pruflags, struct mbuf *m, struct sockaddr *nam,
409 struct mbuf *control, struct thread *td)
411 struct epoch_tracker et;
415 int error, flags, cnt, hlen;
416 u_char opttype, optlen, *cp;
419 KASSERT(inp != NULL, ("rip_send: inp == NULL"));
421 if (control != NULL) {
426 if (so->so_state & SS_ISCONNECTED) {
432 dst = &inp->inp_faddr.s_addr;
436 else if (nam->sa_family != AF_INET)
437 error = EAFNOSUPPORT;
438 else if (nam->sa_len != sizeof(struct sockaddr_in))
446 dst = &((struct sockaddr_in *)nam)->sin_addr.s_addr;
449 flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
453 * If the user handed us a complete IP packet, use it. Otherwise,
454 * allocate an mbuf for a header and fill it in.
456 if ((inp->inp_flags & INP_HDRINCL) == 0) {
457 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
461 M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
466 ip = mtod(m, struct ip *);
467 ip->ip_tos = inp->inp_ip_tos;
468 if (inp->inp_flags & INP_DONTFRAG)
469 ip->ip_off = htons(IP_DF);
471 ip->ip_off = htons(0);
472 ip->ip_p = inp->inp_ip_p;
473 ip->ip_len = htons(m->m_pkthdr.len);
474 ip->ip_src = inp->inp_laddr;
475 ip->ip_dst.s_addr = *dst;
477 if (CALC_FLOWID_OUTBOUND) {
478 uint32_t hash_type, hash_val;
480 hash_val = fib4_calc_software_hash(ip->ip_src,
481 ip->ip_dst, 0, 0, ip->ip_p, &hash_type);
482 m->m_pkthdr.flowid = hash_val;
483 M_HASHTYPE_SET(m, hash_type);
484 flags |= IP_NODEFAULTFLOWID;
487 if (jailed(inp->inp_cred)) {
489 * prison_local_ip4() would be good enough but would
490 * let a source of INADDR_ANY pass, which we do not
491 * want to see from jails.
493 if (ip->ip_src.s_addr == INADDR_ANY) {
495 error = in_pcbladdr(inp, &ip->ip_dst,
496 &ip->ip_src, inp->inp_cred);
499 error = prison_local_ip4(inp->inp_cred,
508 ip->ip_ttl = inp->inp_ip_ttl;
510 if (m->m_pkthdr.len > IP_MAXPACKET) {
514 if (m->m_pkthdr.len < sizeof(*ip)) {
518 m = m_pullup(m, sizeof(*ip));
521 ip = mtod(m, struct ip *);
522 hlen = ip->ip_hl << 2;
523 if (m->m_len < hlen) {
524 m = m_pullup(m, hlen);
527 ip = mtod(m, struct ip *);
530 if (CALC_FLOWID_OUTBOUND) {
531 uint32_t hash_type, hash_val;
533 hash_val = fib4_calc_software_hash(ip->ip_dst,
534 ip->ip_src, 0, 0, ip->ip_p, &hash_type);
535 m->m_pkthdr.flowid = hash_val;
536 M_HASHTYPE_SET(m, hash_type);
537 flags |= IP_NODEFAULTFLOWID;
542 * Don't allow both user specified and setsockopt options,
543 * and don't allow packet length sizes that will crash.
545 if ((hlen < sizeof (*ip))
546 || ((hlen > sizeof (*ip)) && inp->inp_options)
547 || (ntohs(ip->ip_len) != m->m_pkthdr.len)) {
552 error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
559 * Don't allow IP options which do not have the required
560 * structure as specified in section 3.1 of RFC 791 on
563 cp = (u_char *)(ip + 1);
564 cnt = hlen - sizeof (struct ip);
565 for (; cnt > 0; cnt -= optlen, cp += optlen) {
566 opttype = cp[IPOPT_OPTVAL];
567 if (opttype == IPOPT_EOL)
569 if (opttype == IPOPT_NOP) {
573 if (cnt < IPOPT_OLEN + sizeof(u_char)) {
578 optlen = cp[IPOPT_OLEN];
579 if (optlen < IPOPT_OLEN + sizeof(u_char) ||
587 * This doesn't allow application to specify ID of zero,
588 * but we got this limitation from the beginning of history.
594 * XXX prevent ip_output from overwriting header fields.
596 flags |= IP_RAWOUTPUT;
597 IPSTAT_INC(ips_rawout);
600 if (inp->inp_flags & INP_ONESBCAST)
601 flags |= IP_SENDONES;
604 mac_inpcb_create_mbuf(inp, m);
608 error = ip_output(m, inp->inp_options, NULL, flags,
609 inp->inp_moptions, inp);
616 * Raw IP socket option processing.
618 * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
619 * only be created by a privileged process, and as such, socket option
620 * operations to manage system properties on any raw socket were allowed to
621 * take place without explicit additional access control checks. However,
622 * raw sockets can now also be created in jail(), and therefore explicit
623 * checks are now required. Likewise, raw sockets can be used by a process
624 * after it gives up privilege, so some caution is required. For options
625 * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
626 * performed in ip_ctloutput() and therefore no check occurs here.
627 * Unilaterally checking priv_check() here breaks normal IP socket option
628 * operations on raw sockets.
630 * When adding new socket options here, make sure to add access control
631 * checks here as necessary.
633 * XXX-BZ inp locking?
636 rip_ctloutput(struct socket *so, struct sockopt *sopt)
638 struct inpcb *inp = sotoinpcb(so);
641 if (sopt->sopt_level != IPPROTO_IP) {
642 if ((sopt->sopt_level == SOL_SOCKET) &&
643 (sopt->sopt_name == SO_SETFIB)) {
644 inp->inp_inc.inc_fibnum = so->so_fibnum;
651 switch (sopt->sopt_dir) {
653 switch (sopt->sopt_name) {
655 optval = inp->inp_flags & INP_HDRINCL;
656 error = sooptcopyout(sopt, &optval, sizeof optval);
659 case IP_FW3: /* generic ipfw v.3 functions */
660 case IP_FW_ADD: /* ADD actually returns the body... */
662 case IP_FW_TABLE_GETSIZE:
663 case IP_FW_TABLE_LIST:
664 case IP_FW_NAT_GET_CONFIG:
665 case IP_FW_NAT_GET_LOG:
666 if (V_ip_fw_ctl_ptr != NULL)
667 error = V_ip_fw_ctl_ptr(sopt);
672 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
673 case IP_DUMMYNET_GET:
674 if (ip_dn_ctl_ptr != NULL)
675 error = ip_dn_ctl_ptr(sopt);
688 case MRT_API_SUPPORT:
690 case MRT_ADD_BW_UPCALL:
691 case MRT_DEL_BW_UPCALL:
692 error = priv_check(curthread, PRIV_NETINET_MROUTE);
695 if (inp->inp_ip_p != IPPROTO_IGMP)
697 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
702 error = ip_ctloutput(so, sopt);
708 switch (sopt->sopt_name) {
710 error = sooptcopyin(sopt, &optval, sizeof optval,
715 inp->inp_flags |= INP_HDRINCL;
717 inp->inp_flags &= ~INP_HDRINCL;
720 case IP_FW3: /* generic ipfw v.3 functions */
726 case IP_FW_TABLE_ADD:
727 case IP_FW_TABLE_DEL:
728 case IP_FW_TABLE_FLUSH:
731 if (V_ip_fw_ctl_ptr != NULL)
732 error = V_ip_fw_ctl_ptr(sopt);
737 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
738 case IP_DUMMYNET_CONFIGURE:
739 case IP_DUMMYNET_DEL:
740 case IP_DUMMYNET_FLUSH:
741 if (ip_dn_ctl_ptr != NULL)
742 error = ip_dn_ctl_ptr(sopt);
744 error = ENOPROTOOPT ;
748 error = priv_check(curthread, PRIV_NETINET_MROUTE);
751 if (inp->inp_ip_p != IPPROTO_RSVP)
753 error = ip_rsvp_init(so);
757 error = priv_check(curthread, PRIV_NETINET_MROUTE);
760 error = ip_rsvp_done();
764 case IP_RSVP_VIF_OFF:
765 error = priv_check(curthread, PRIV_NETINET_MROUTE);
768 if (inp->inp_ip_p != IPPROTO_RSVP)
770 error = ip_rsvp_vif ?
771 ip_rsvp_vif(so, sopt) : EINVAL;
782 case MRT_API_SUPPORT:
784 case MRT_ADD_BW_UPCALL:
785 case MRT_DEL_BW_UPCALL:
786 error = priv_check(curthread, PRIV_NETINET_MROUTE);
789 if (inp->inp_ip_p != IPPROTO_IGMP)
791 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
796 error = ip_ctloutput(so, sopt);
806 rip_ctlinput(struct icmp *icmp)
808 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
809 if (IPSEC_ENABLED(ipv4))
810 IPSEC_CTLINPUT(ipv4, icmp);
815 rip_attach(struct socket *so, int proto, struct thread *td)
821 KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
823 error = priv_check(td, PRIV_NETINET_RAW);
826 if (proto >= IPPROTO_MAX || proto < 0)
827 return EPROTONOSUPPORT;
828 error = soreserve(so, rip_sendspace, rip_recvspace);
831 error = in_pcballoc(so, &V_ripcbinfo);
834 inp = (struct inpcb *)so->so_pcb;
835 inp->inp_ip_p = proto;
836 inp->inp_ip_ttl = V_ip_defttl;
837 INP_HASH_WLOCK(&V_ripcbinfo);
839 INP_HASH_WUNLOCK(&V_ripcbinfo);
845 rip_detach(struct socket *so)
850 KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
851 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
852 ("rip_detach: not closed"));
854 /* Disable mrouter first */
855 if (so == V_ip_mrouter && ip_mrouter_done)
859 INP_HASH_WLOCK(&V_ripcbinfo);
861 INP_HASH_WUNLOCK(&V_ripcbinfo);
863 if (ip_rsvp_force_done)
864 ip_rsvp_force_done(so);
865 if (so == V_ip_rsvpd)
872 rip_dodisconnect(struct socket *so, struct inpcb *inp)
874 struct inpcbinfo *pcbinfo;
876 pcbinfo = inp->inp_pcbinfo;
878 INP_HASH_WLOCK(pcbinfo);
880 inp->inp_faddr.s_addr = INADDR_ANY;
882 INP_HASH_WUNLOCK(pcbinfo);
884 so->so_state &= ~SS_ISCONNECTED;
890 rip_abort(struct socket *so)
895 KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
897 rip_dodisconnect(so, inp);
901 rip_close(struct socket *so)
906 KASSERT(inp != NULL, ("rip_close: inp == NULL"));
908 rip_dodisconnect(so, inp);
912 rip_disconnect(struct socket *so)
916 if ((so->so_state & SS_ISCONNECTED) == 0)
920 KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
922 rip_dodisconnect(so, inp);
927 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
929 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
933 if (nam->sa_family != AF_INET)
934 return (EAFNOSUPPORT);
935 if (nam->sa_len != sizeof(*addr))
938 error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
943 KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
945 if (CK_STAILQ_EMPTY(&V_ifnet) ||
946 (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
947 (addr->sin_addr.s_addr &&
948 (inp->inp_flags & INP_BINDANY) == 0 &&
949 ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
950 return (EADDRNOTAVAIL);
953 INP_HASH_WLOCK(&V_ripcbinfo);
955 inp->inp_laddr = addr->sin_addr;
957 INP_HASH_WUNLOCK(&V_ripcbinfo);
963 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
965 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
968 if (nam->sa_len != sizeof(*addr))
970 if (CK_STAILQ_EMPTY(&V_ifnet))
971 return (EADDRNOTAVAIL);
972 if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
973 return (EAFNOSUPPORT);
976 KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
979 INP_HASH_WLOCK(&V_ripcbinfo);
981 inp->inp_faddr = addr->sin_addr;
983 INP_HASH_WUNLOCK(&V_ripcbinfo);
990 rip_shutdown(struct socket *so)
995 KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
1005 rip_pcblist(SYSCTL_HANDLER_ARGS)
1007 struct inpcb_iterator inpi = INP_ALL_ITERATOR(&V_ripcbinfo,
1008 INPLOOKUP_RLOCKPCB);
1013 if (req->newptr != 0)
1016 if (req->oldptr == 0) {
1019 n = V_ripcbinfo.ipi_count;
1020 n += imax(n / 8, 10);
1021 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
1025 if ((error = sysctl_wire_old_buffer(req, 0)) != 0)
1028 bzero(&xig, sizeof(xig));
1029 xig.xig_len = sizeof xig;
1030 xig.xig_count = V_ripcbinfo.ipi_count;
1031 xig.xig_gen = V_ripcbinfo.ipi_gencnt;
1032 xig.xig_sogen = so_gencnt;
1033 error = SYSCTL_OUT(req, &xig, sizeof xig);
1037 while ((inp = inp_next(&inpi)) != NULL) {
1038 if (inp->inp_gencnt <= xig.xig_gen &&
1039 cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
1042 in_pcbtoxinpcb(inp, &xi);
1043 error = SYSCTL_OUT(req, &xi, sizeof xi);
1053 * Give the user an updated idea of our state. If the
1054 * generation differs from what we told her before, she knows
1055 * that something happened while we were processing this
1056 * request, and it might be necessary to retry.
1058 xig.xig_gen = V_ripcbinfo.ipi_gencnt;
1059 xig.xig_sogen = so_gencnt;
1060 xig.xig_count = V_ripcbinfo.ipi_count;
1061 error = SYSCTL_OUT(req, &xig, sizeof xig);
1067 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist,
1068 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
1069 rip_pcblist, "S,xinpcb",
1070 "List of active raw IP sockets");
1073 struct protosw rip_protosw = {
1074 .pr_type = SOCK_RAW,
1075 .pr_flags = PR_ATOMIC|PR_ADDR,
1076 .pr_ctloutput = rip_ctloutput,
1077 .pr_abort = rip_abort,
1078 .pr_attach = rip_attach,
1079 .pr_bind = rip_bind,
1080 .pr_connect = rip_connect,
1081 .pr_control = in_control,
1082 .pr_detach = rip_detach,
1083 .pr_disconnect = rip_disconnect,
1084 .pr_peeraddr = in_getpeeraddr,
1085 .pr_send = rip_send,
1086 .pr_shutdown = rip_shutdown,
1087 .pr_sockaddr = in_getsockaddr,
1088 .pr_sosetlabel = in_pcbsosetlabel,
1089 .pr_close = rip_close