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>
37 #include "opt_inet6.h"
38 #include "opt_ipsec.h"
39 #include "opt_route.h"
41 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/eventhandler.h>
46 #include <sys/malloc.h>
50 #include <sys/protosw.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>
64 #include <net/route/route_ctl.h>
67 #include <netinet/in.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/in_fib.h>
70 #include <netinet/in_pcb.h>
71 #include <netinet/in_var.h>
72 #include <netinet/if_ether.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_var.h>
75 #include <netinet/ip_mroute.h>
76 #include <netinet/ip_icmp.h>
78 #include <netipsec/ipsec_support.h>
80 #include <machine/stdarg.h>
81 #include <security/mac/mac_framework.h>
83 extern ipproto_input_t *ip_protox[];
85 VNET_DEFINE(int, ip_defttl) = IPDEFTTL;
86 SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_VNET | CTLFLAG_RW,
87 &VNET_NAME(ip_defttl), 0,
88 "Maximum TTL on IP packets");
90 VNET_DEFINE(struct inpcbinfo, ripcbinfo);
91 #define V_ripcbinfo VNET(ripcbinfo)
94 * Control and data hooks for ipfw, dummynet, divert and so on.
95 * The data hooks are not used here but it is convenient
96 * to keep them all in one place.
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 **, struct ip_fw_args *);
102 void (*ip_divert_ptr)(struct mbuf *, bool);
103 int (*ng_ipfw_input_p)(struct mbuf **, struct ip_fw_args *, bool);
107 * Hooks for multicast routing. They all default to NULL, so leave them not
108 * initialized and rely on BSS being set to 0.
112 * The socket used to communicate with the multicast routing daemon.
114 VNET_DEFINE(struct socket *, ip_mrouter);
117 * The various mrouter and rsvp functions.
119 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
120 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
121 int (*ip_mrouter_done)(void);
122 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
123 struct ip_moptions *);
124 int (*mrt_ioctl)(u_long, caddr_t, int);
125 int (*legal_vif_num)(int);
126 u_long (*ip_mcast_src)(int);
128 int (*rsvp_input_p)(struct mbuf **, int *, int);
129 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
130 void (*ip_rsvp_force_done)(struct socket *);
133 u_long rip_sendspace = 9216;
134 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
135 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
137 u_long rip_recvspace = 9216;
138 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
139 &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
145 #define INP_PCBHASH_RAW_SIZE 256
146 #define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \
147 (((proto) + (laddr) + (faddr)) % (mask) + 1)
151 rip_inshash(struct inpcb *inp)
153 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
154 struct inpcbhead *pcbhash;
157 INP_HASH_WLOCK_ASSERT(pcbinfo);
158 INP_WLOCK_ASSERT(inp);
160 if (inp->inp_ip_p != 0 &&
161 inp->inp_laddr.s_addr != INADDR_ANY &&
162 inp->inp_faddr.s_addr != INADDR_ANY) {
163 hash = INP_PCBHASH_RAW(inp->inp_ip_p, inp->inp_laddr.s_addr,
164 inp->inp_faddr.s_addr, pcbinfo->ipi_hashmask);
167 pcbhash = &pcbinfo->ipi_hash_exact[hash];
168 CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash_exact);
172 rip_delhash(struct inpcb *inp)
175 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
176 INP_WLOCK_ASSERT(inp);
178 CK_LIST_REMOVE(inp, inp_hash_exact);
182 INPCBSTORAGE_DEFINE(ripcbstor, inpcb, "rawinp", "ripcb", "rip", "riphash");
185 rip_init(void *arg __unused)
188 in_pcbinfo_init(&V_ripcbinfo, &ripcbstor, INP_PCBHASH_RAW_SIZE, 1);
190 VNET_SYSINIT(rip_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rip_init, NULL);
194 rip_destroy(void *unused __unused)
197 in_pcbinfo_destroy(&V_ripcbinfo);
199 VNET_SYSUNINIT(raw_ip, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, rip_destroy, NULL);
204 rip_append(struct inpcb *inp, struct ip *ip, struct mbuf *m,
205 struct sockaddr_in *ripsrc)
207 struct socket *so = inp->inp_socket;
208 struct mbuf *n, *opts = NULL;
210 INP_LOCK_ASSERT(inp);
212 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
213 /* check AH/ESP integrity. */
214 if (IPSEC_ENABLED(ipv4) && IPSEC_CHECK_POLICY(ipv4, m, inp) != 0)
218 if (mac_inpcb_check_deliver(inp, m) != 0)
221 /* Check the minimum TTL for socket. */
222 if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl)
225 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL)
228 if ((inp->inp_flags & INP_CONTROLOPTS) ||
229 (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
230 ip_savecontrol(inp, &opts, ip, n);
231 SOCKBUF_LOCK(&so->so_rcv);
232 if (sbappendaddr_locked(&so->so_rcv,
233 (struct sockaddr *)ripsrc, n, opts) == 0) {
234 soroverflow_locked(so);
240 sorwakeup_locked(so);
245 struct rip_inp_match_ctx {
251 rip_inp_match1(const struct inpcb *inp, void *v)
253 struct rip_inp_match_ctx *ctx = v;
255 if (inp->inp_ip_p != ctx->proto)
258 /* XXX inp locking */
259 if ((inp->inp_vflag & INP_IPV4) == 0)
262 if (inp->inp_laddr.s_addr != ctx->ip->ip_dst.s_addr)
264 if (inp->inp_faddr.s_addr != ctx->ip->ip_src.s_addr)
270 rip_inp_match2(const struct inpcb *inp, void *v)
272 struct rip_inp_match_ctx *ctx = v;
274 if (inp->inp_ip_p && inp->inp_ip_p != ctx->proto)
277 /* XXX inp locking */
278 if ((inp->inp_vflag & INP_IPV4) == 0)
281 if (!in_nullhost(inp->inp_laddr) &&
282 !in_hosteq(inp->inp_laddr, ctx->ip->ip_dst))
284 if (!in_nullhost(inp->inp_faddr) &&
285 !in_hosteq(inp->inp_faddr, ctx->ip->ip_src))
291 * Setup generic address and protocol structures for raw_input routine, then
292 * pass them along with mbuf chain.
295 rip_input(struct mbuf **mp, int *offp, int proto)
297 struct rip_inp_match_ctx ctx = {
298 .ip = mtod(*mp, struct ip *),
301 struct inpcb_iterator inpi = INP_ITERATOR(&V_ripcbinfo,
302 INPLOOKUP_RLOCKPCB, rip_inp_match1, &ctx);
304 struct mbuf *m = *mp;
306 struct sockaddr_in ripsrc;
312 bzero(&ripsrc, sizeof(ripsrc));
313 ripsrc.sin_len = sizeof(ripsrc);
314 ripsrc.sin_family = AF_INET;
315 ripsrc.sin_addr = ctx.ip->ip_src;
317 ifp = m->m_pkthdr.rcvif;
319 inpi.hash = INP_PCBHASH_RAW(proto, ctx.ip->ip_src.s_addr,
320 ctx.ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask);
321 while ((inp = inp_next(&inpi)) != NULL) {
322 INP_RLOCK_ASSERT(inp);
323 if (jailed_without_vnet(inp->inp_cred) &&
324 prison_check_ip4(inp->inp_cred, &ctx.ip->ip_dst) != 0) {
326 * XXX: If faddr was bound to multicast group,
327 * jailed raw socket will drop datagram.
331 appended += rip_append(inp, ctx.ip, m, &ripsrc);
335 inpi.match = rip_inp_match2;
336 MPASS(inpi.inp == NULL);
337 while ((inp = inp_next(&inpi)) != NULL) {
338 INP_RLOCK_ASSERT(inp);
339 if (jailed_without_vnet(inp->inp_cred) &&
340 !IN_MULTICAST(ntohl(ctx.ip->ip_dst.s_addr)) &&
341 prison_check_ip4(inp->inp_cred, &ctx.ip->ip_dst) != 0)
343 * Allow raw socket in jail to receive multicast;
344 * assume process had PRIV_NETINET_RAW at attach,
345 * and fall through into normal filter path if so.
349 * If this raw socket has multicast state, and we
350 * have received a multicast, check if this socket
351 * should receive it, as multicast filtering is now
352 * the responsibility of the transport layer.
354 if (inp->inp_moptions != NULL &&
355 IN_MULTICAST(ntohl(ctx.ip->ip_dst.s_addr))) {
357 * If the incoming datagram is for IGMP, allow it
358 * through unconditionally to the raw socket.
360 * In the case of IGMPv2, we may not have explicitly
361 * joined the group, and may have set IFF_ALLMULTI
362 * on the interface. imo_multi_filter() may discard
363 * control traffic we actually need to see.
365 * Userland multicast routing daemons should continue
366 * filter the control traffic appropriately.
370 blocked = MCAST_PASS;
371 if (proto != IPPROTO_IGMP) {
372 struct sockaddr_in group;
374 bzero(&group, sizeof(struct sockaddr_in));
375 group.sin_len = sizeof(struct sockaddr_in);
376 group.sin_family = AF_INET;
377 group.sin_addr = ctx.ip->ip_dst;
379 blocked = imo_multi_filter(inp->inp_moptions,
381 (struct sockaddr *)&group,
382 (struct sockaddr *)&ripsrc);
385 if (blocked != MCAST_PASS) {
386 IPSTAT_INC(ips_notmember);
390 appended += rip_append(inp, ctx.ip, m, &ripsrc);
392 if (appended == 0 && ip_protox[ctx.ip->ip_p] == rip_input) {
393 IPSTAT_INC(ips_noproto);
394 IPSTAT_DEC(ips_delivered);
395 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL, 0, 0);
398 return (IPPROTO_DONE);
402 * Generate IP header and pass packet to ip_output. Tack on options user may
403 * have setup with control call.
406 rip_send(struct socket *so, int pruflags, struct mbuf *m, struct sockaddr *nam,
407 struct mbuf *control, struct thread *td)
409 struct epoch_tracker et;
413 int error, flags, cnt, hlen;
414 u_char opttype, optlen, *cp;
417 KASSERT(inp != NULL, ("rip_send: inp == NULL"));
419 if (control != NULL) {
424 if (so->so_state & SS_ISCONNECTED) {
430 dst = &inp->inp_faddr.s_addr;
434 else if (nam->sa_family != AF_INET)
435 error = EAFNOSUPPORT;
436 else if (nam->sa_len != sizeof(struct sockaddr_in))
444 dst = &((struct sockaddr_in *)nam)->sin_addr.s_addr;
447 flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
451 * If the user handed us a complete IP packet, use it. Otherwise,
452 * allocate an mbuf for a header and fill it in.
454 if ((inp->inp_flags & INP_HDRINCL) == 0) {
455 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
459 M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
464 ip = mtod(m, struct ip *);
465 ip->ip_tos = inp->inp_ip_tos;
466 if (inp->inp_flags & INP_DONTFRAG)
467 ip->ip_off = htons(IP_DF);
469 ip->ip_off = htons(0);
470 ip->ip_p = inp->inp_ip_p;
471 ip->ip_len = htons(m->m_pkthdr.len);
472 ip->ip_src = inp->inp_laddr;
473 ip->ip_dst.s_addr = *dst;
475 if (CALC_FLOWID_OUTBOUND) {
476 uint32_t hash_type, hash_val;
478 hash_val = fib4_calc_software_hash(ip->ip_src,
479 ip->ip_dst, 0, 0, ip->ip_p, &hash_type);
480 m->m_pkthdr.flowid = hash_val;
481 M_HASHTYPE_SET(m, hash_type);
482 flags |= IP_NODEFAULTFLOWID;
485 if (jailed(inp->inp_cred)) {
487 * prison_local_ip4() would be good enough but would
488 * let a source of INADDR_ANY pass, which we do not
489 * want to see from jails.
491 if (ip->ip_src.s_addr == INADDR_ANY) {
493 error = in_pcbladdr(inp, &ip->ip_dst,
494 &ip->ip_src, inp->inp_cred);
497 error = prison_local_ip4(inp->inp_cred,
506 ip->ip_ttl = inp->inp_ip_ttl;
508 if (m->m_pkthdr.len > IP_MAXPACKET) {
512 if (m->m_pkthdr.len < sizeof(*ip)) {
516 m = m_pullup(m, sizeof(*ip));
519 ip = mtod(m, struct ip *);
520 hlen = ip->ip_hl << 2;
521 if (m->m_len < hlen) {
522 m = m_pullup(m, hlen);
525 ip = mtod(m, struct ip *);
528 if (CALC_FLOWID_OUTBOUND) {
529 uint32_t hash_type, hash_val;
531 hash_val = fib4_calc_software_hash(ip->ip_dst,
532 ip->ip_src, 0, 0, ip->ip_p, &hash_type);
533 m->m_pkthdr.flowid = hash_val;
534 M_HASHTYPE_SET(m, hash_type);
535 flags |= IP_NODEFAULTFLOWID;
540 * Don't allow both user specified and setsockopt options,
541 * and don't allow packet length sizes that will crash.
543 if ((hlen < sizeof (*ip))
544 || ((hlen > sizeof (*ip)) && inp->inp_options)
545 || (ntohs(ip->ip_len) != m->m_pkthdr.len)) {
550 error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
557 * Don't allow IP options which do not have the required
558 * structure as specified in section 3.1 of RFC 791 on
561 cp = (u_char *)(ip + 1);
562 cnt = hlen - sizeof (struct ip);
563 for (; cnt > 0; cnt -= optlen, cp += optlen) {
564 opttype = cp[IPOPT_OPTVAL];
565 if (opttype == IPOPT_EOL)
567 if (opttype == IPOPT_NOP) {
571 if (cnt < IPOPT_OLEN + sizeof(u_char)) {
576 optlen = cp[IPOPT_OLEN];
577 if (optlen < IPOPT_OLEN + sizeof(u_char) ||
585 * This doesn't allow application to specify ID of zero,
586 * but we got this limitation from the beginning of history.
592 * XXX prevent ip_output from overwriting header fields.
594 flags |= IP_RAWOUTPUT;
595 IPSTAT_INC(ips_rawout);
598 if (inp->inp_flags & INP_ONESBCAST)
599 flags |= IP_SENDONES;
602 mac_inpcb_create_mbuf(inp, m);
606 error = ip_output(m, inp->inp_options, NULL, flags,
607 inp->inp_moptions, inp);
614 * Raw IP socket option processing.
616 * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
617 * only be created by a privileged process, and as such, socket option
618 * operations to manage system properties on any raw socket were allowed to
619 * take place without explicit additional access control checks. However,
620 * raw sockets can now also be created in jail(), and therefore explicit
621 * checks are now required. Likewise, raw sockets can be used by a process
622 * after it gives up privilege, so some caution is required. For options
623 * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
624 * performed in ip_ctloutput() and therefore no check occurs here.
625 * Unilaterally checking priv_check() here breaks normal IP socket option
626 * operations on raw sockets.
628 * When adding new socket options here, make sure to add access control
629 * checks here as necessary.
631 * XXX-BZ inp locking?
634 rip_ctloutput(struct socket *so, struct sockopt *sopt)
636 struct inpcb *inp = sotoinpcb(so);
639 if (sopt->sopt_level != IPPROTO_IP) {
640 if ((sopt->sopt_level == SOL_SOCKET) &&
641 (sopt->sopt_name == SO_SETFIB)) {
642 inp->inp_inc.inc_fibnum = so->so_fibnum;
649 switch (sopt->sopt_dir) {
651 switch (sopt->sopt_name) {
653 optval = inp->inp_flags & INP_HDRINCL;
654 error = sooptcopyout(sopt, &optval, sizeof optval);
657 case IP_FW3: /* generic ipfw v.3 functions */
658 case IP_FW_ADD: /* ADD actually returns the body... */
660 case IP_FW_TABLE_GETSIZE:
661 case IP_FW_TABLE_LIST:
662 case IP_FW_NAT_GET_CONFIG:
663 case IP_FW_NAT_GET_LOG:
664 if (V_ip_fw_ctl_ptr != NULL)
665 error = V_ip_fw_ctl_ptr(sopt);
670 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
671 case IP_DUMMYNET_GET:
672 if (ip_dn_ctl_ptr != NULL)
673 error = ip_dn_ctl_ptr(sopt);
686 case MRT_API_SUPPORT:
688 case MRT_ADD_BW_UPCALL:
689 case MRT_DEL_BW_UPCALL:
690 error = priv_check(curthread, PRIV_NETINET_MROUTE);
693 if (inp->inp_ip_p != IPPROTO_IGMP)
695 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
700 error = ip_ctloutput(so, sopt);
706 switch (sopt->sopt_name) {
708 error = sooptcopyin(sopt, &optval, sizeof optval,
713 inp->inp_flags |= INP_HDRINCL;
715 inp->inp_flags &= ~INP_HDRINCL;
718 case IP_FW3: /* generic ipfw v.3 functions */
724 case IP_FW_TABLE_ADD:
725 case IP_FW_TABLE_DEL:
726 case IP_FW_TABLE_FLUSH:
729 if (V_ip_fw_ctl_ptr != NULL)
730 error = V_ip_fw_ctl_ptr(sopt);
735 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
736 case IP_DUMMYNET_CONFIGURE:
737 case IP_DUMMYNET_DEL:
738 case IP_DUMMYNET_FLUSH:
739 if (ip_dn_ctl_ptr != NULL)
740 error = ip_dn_ctl_ptr(sopt);
742 error = ENOPROTOOPT ;
746 error = priv_check(curthread, PRIV_NETINET_MROUTE);
749 if (inp->inp_ip_p != IPPROTO_RSVP)
751 error = ip_rsvp_init(so);
755 error = priv_check(curthread, PRIV_NETINET_MROUTE);
758 error = ip_rsvp_done();
762 case IP_RSVP_VIF_OFF:
763 error = priv_check(curthread, PRIV_NETINET_MROUTE);
766 if (inp->inp_ip_p != IPPROTO_RSVP)
768 error = ip_rsvp_vif ?
769 ip_rsvp_vif(so, sopt) : EINVAL;
780 case MRT_API_SUPPORT:
782 case MRT_ADD_BW_UPCALL:
783 case MRT_DEL_BW_UPCALL:
784 error = priv_check(curthread, PRIV_NETINET_MROUTE);
787 if (inp->inp_ip_p != IPPROTO_IGMP)
789 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
794 error = ip_ctloutput(so, sopt);
804 rip_ctlinput(struct icmp *icmp)
806 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
807 if (IPSEC_ENABLED(ipv4))
808 IPSEC_CTLINPUT(ipv4, icmp);
813 rip_attach(struct socket *so, int proto, struct thread *td)
819 KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
821 error = priv_check(td, PRIV_NETINET_RAW);
824 if (proto >= IPPROTO_MAX || proto < 0)
825 return EPROTONOSUPPORT;
826 error = soreserve(so, rip_sendspace, rip_recvspace);
829 error = in_pcballoc(so, &V_ripcbinfo);
832 inp = (struct inpcb *)so->so_pcb;
833 inp->inp_ip_p = proto;
834 inp->inp_ip_ttl = V_ip_defttl;
835 INP_HASH_WLOCK(&V_ripcbinfo);
837 INP_HASH_WUNLOCK(&V_ripcbinfo);
843 rip_detach(struct socket *so)
848 KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
849 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
850 ("rip_detach: not closed"));
852 /* Disable mrouter first */
853 if (so == V_ip_mrouter && ip_mrouter_done)
857 INP_HASH_WLOCK(&V_ripcbinfo);
859 INP_HASH_WUNLOCK(&V_ripcbinfo);
861 if (ip_rsvp_force_done)
862 ip_rsvp_force_done(so);
863 if (so == V_ip_rsvpd)
870 rip_dodisconnect(struct socket *so, struct inpcb *inp)
872 struct inpcbinfo *pcbinfo;
874 pcbinfo = inp->inp_pcbinfo;
876 INP_HASH_WLOCK(pcbinfo);
878 inp->inp_faddr.s_addr = INADDR_ANY;
880 INP_HASH_WUNLOCK(pcbinfo);
882 so->so_state &= ~SS_ISCONNECTED;
888 rip_abort(struct socket *so)
893 KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
895 rip_dodisconnect(so, inp);
899 rip_close(struct socket *so)
904 KASSERT(inp != NULL, ("rip_close: inp == NULL"));
906 rip_dodisconnect(so, inp);
910 rip_disconnect(struct socket *so)
914 if ((so->so_state & SS_ISCONNECTED) == 0)
918 KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
920 rip_dodisconnect(so, inp);
925 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
927 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
931 if (nam->sa_family != AF_INET)
932 return (EAFNOSUPPORT);
933 if (nam->sa_len != sizeof(*addr))
936 error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
941 KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
943 if (CK_STAILQ_EMPTY(&V_ifnet) ||
944 (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
945 (addr->sin_addr.s_addr &&
946 (inp->inp_flags & INP_BINDANY) == 0 &&
947 ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
948 return (EADDRNOTAVAIL);
951 INP_HASH_WLOCK(&V_ripcbinfo);
953 inp->inp_laddr = addr->sin_addr;
955 INP_HASH_WUNLOCK(&V_ripcbinfo);
961 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
963 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
966 if (nam->sa_len != sizeof(*addr))
968 if (CK_STAILQ_EMPTY(&V_ifnet))
969 return (EADDRNOTAVAIL);
970 if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
971 return (EAFNOSUPPORT);
974 KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
977 INP_HASH_WLOCK(&V_ripcbinfo);
979 inp->inp_faddr = addr->sin_addr;
981 INP_HASH_WUNLOCK(&V_ripcbinfo);
988 rip_shutdown(struct socket *so)
993 KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
1003 rip_pcblist(SYSCTL_HANDLER_ARGS)
1005 struct inpcb_iterator inpi = INP_ALL_ITERATOR(&V_ripcbinfo,
1006 INPLOOKUP_RLOCKPCB);
1011 if (req->newptr != 0)
1014 if (req->oldptr == 0) {
1017 n = V_ripcbinfo.ipi_count;
1018 n += imax(n / 8, 10);
1019 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
1023 if ((error = sysctl_wire_old_buffer(req, 0)) != 0)
1026 bzero(&xig, sizeof(xig));
1027 xig.xig_len = sizeof xig;
1028 xig.xig_count = V_ripcbinfo.ipi_count;
1029 xig.xig_gen = V_ripcbinfo.ipi_gencnt;
1030 xig.xig_sogen = so_gencnt;
1031 error = SYSCTL_OUT(req, &xig, sizeof xig);
1035 while ((inp = inp_next(&inpi)) != NULL) {
1036 if (inp->inp_gencnt <= xig.xig_gen &&
1037 cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
1040 in_pcbtoxinpcb(inp, &xi);
1041 error = SYSCTL_OUT(req, &xi, sizeof xi);
1051 * Give the user an updated idea of our state. If the
1052 * generation differs from what we told her before, she knows
1053 * that something happened while we were processing this
1054 * request, and it might be necessary to retry.
1056 xig.xig_gen = V_ripcbinfo.ipi_gencnt;
1057 xig.xig_sogen = so_gencnt;
1058 xig.xig_count = V_ripcbinfo.ipi_count;
1059 error = SYSCTL_OUT(req, &xig, sizeof xig);
1065 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist,
1066 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
1067 rip_pcblist, "S,xinpcb",
1068 "List of active raw IP sockets");
1071 struct protosw rip_protosw = {
1072 .pr_type = SOCK_RAW,
1073 .pr_flags = PR_ATOMIC|PR_ADDR,
1074 .pr_ctloutput = rip_ctloutput,
1075 .pr_abort = rip_abort,
1076 .pr_attach = rip_attach,
1077 .pr_bind = rip_bind,
1078 .pr_connect = rip_connect,
1079 .pr_control = in_control,
1080 .pr_detach = rip_detach,
1081 .pr_disconnect = rip_disconnect,
1082 .pr_peeraddr = in_getpeeraddr,
1083 .pr_send = rip_send,
1084 .pr_shutdown = rip_shutdown,
1085 .pr_sockaddr = in_getsockaddr,
1086 .pr_sosetlabel = in_pcbsosetlabel,
1087 .pr_close = rip_close