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/rmlock.h>
54 #include <sys/rwlock.h>
55 #include <sys/signalvar.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
59 #include <sys/sysctl.h>
60 #include <sys/systm.h>
65 #include <net/if_var.h>
66 #include <net/route.h>
67 #include <net/route/route_ctl.h>
70 #include <netinet/in.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/in_fib.h>
73 #include <netinet/in_pcb.h>
74 #include <netinet/in_var.h>
75 #include <netinet/if_ether.h>
76 #include <netinet/ip.h>
77 #include <netinet/ip_var.h>
78 #include <netinet/ip_mroute.h>
79 #include <netinet/ip_icmp.h>
81 #include <netipsec/ipsec_support.h>
83 #include <machine/stdarg.h>
84 #include <security/mac/mac_framework.h>
86 VNET_DEFINE(int, ip_defttl) = IPDEFTTL;
87 SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_VNET | CTLFLAG_RW,
88 &VNET_NAME(ip_defttl), 0,
89 "Maximum TTL on IP packets");
91 VNET_DEFINE(struct inpcbhead, ripcb);
92 VNET_DEFINE(struct inpcbinfo, ripcbinfo);
94 #define V_ripcb VNET(ripcb)
95 #define V_ripcbinfo VNET(ripcbinfo)
98 * Control and data hooks for ipfw, dummynet, divert and so on.
99 * The data hooks are not used here but it is convenient
100 * to keep them all in one place.
102 VNET_DEFINE(ip_fw_chk_ptr_t, ip_fw_chk_ptr) = NULL;
103 VNET_DEFINE(ip_fw_ctl_ptr_t, ip_fw_ctl_ptr) = NULL;
105 int (*ip_dn_ctl_ptr)(struct sockopt *);
106 int (*ip_dn_io_ptr)(struct mbuf **, struct ip_fw_args *);
107 void (*ip_divert_ptr)(struct mbuf *, bool);
108 int (*ng_ipfw_input_p)(struct mbuf **, struct ip_fw_args *, bool);
112 * Hooks for multicast routing. They all default to NULL, so leave them not
113 * initialized and rely on BSS being set to 0.
117 * The socket used to communicate with the multicast routing daemon.
119 VNET_DEFINE(struct socket *, ip_mrouter);
122 * The various mrouter and rsvp functions.
124 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
125 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
126 int (*ip_mrouter_done)(void);
127 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
128 struct ip_moptions *);
129 int (*mrt_ioctl)(u_long, caddr_t, int);
130 int (*legal_vif_num)(int);
131 u_long (*ip_mcast_src)(int);
133 int (*rsvp_input_p)(struct mbuf **, int *, int);
134 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
135 void (*ip_rsvp_force_done)(struct socket *);
138 extern struct protosw inetsw[];
140 u_long rip_sendspace = 9216;
141 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
142 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
144 u_long rip_recvspace = 9216;
145 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
146 &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
152 #define INP_PCBHASH_RAW_SIZE 256
153 #define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \
154 (((proto) + (laddr) + (faddr)) % (mask) + 1)
158 rip_inshash(struct inpcb *inp)
160 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
161 struct inpcbhead *pcbhash;
164 INP_INFO_WLOCK_ASSERT(pcbinfo);
165 INP_WLOCK_ASSERT(inp);
167 if (inp->inp_ip_p != 0 &&
168 inp->inp_laddr.s_addr != INADDR_ANY &&
169 inp->inp_faddr.s_addr != INADDR_ANY) {
170 hash = INP_PCBHASH_RAW(inp->inp_ip_p, inp->inp_laddr.s_addr,
171 inp->inp_faddr.s_addr, pcbinfo->ipi_hashmask);
174 pcbhash = &pcbinfo->ipi_hashbase[hash];
175 CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
179 rip_delhash(struct inpcb *inp)
182 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
183 INP_WLOCK_ASSERT(inp);
185 CK_LIST_REMOVE(inp, inp_hash);
190 * Raw interface to IP protocol.
194 * Initialize raw connection block q.
197 rip_zone_change(void *tag)
200 uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
204 rip_inpcb_init(void *mem, int size, int flags)
206 struct inpcb *inp = mem;
208 INP_LOCK_INIT(inp, "inp", "rawinp");
216 in_pcbinfo_init(&V_ripcbinfo, "rip", &V_ripcb, INP_PCBHASH_RAW_SIZE,
217 1, "ripcb", rip_inpcb_init, IPI_HASHFIELDS_NONE);
218 EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change, NULL,
219 EVENTHANDLER_PRI_ANY);
224 rip_destroy(void *unused __unused)
227 in_pcbinfo_destroy(&V_ripcbinfo);
229 VNET_SYSUNINIT(raw_ip, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, rip_destroy, NULL);
234 rip_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
235 struct sockaddr_in *ripsrc)
239 INP_LOCK_ASSERT(last);
241 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
242 /* check AH/ESP integrity. */
243 if (IPSEC_ENABLED(ipv4)) {
244 if (IPSEC_CHECK_POLICY(ipv4, n, last) != 0)
249 if (!policyfail && mac_inpcb_check_deliver(last, n) != 0)
252 /* Check the minimum TTL for socket. */
253 if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
256 struct mbuf *opts = NULL;
259 so = last->inp_socket;
260 if ((last->inp_flags & INP_CONTROLOPTS) ||
261 (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
262 ip_savecontrol(last, &opts, ip, n);
263 SOCKBUF_LOCK(&so->so_rcv);
264 if (sbappendaddr_locked(&so->so_rcv,
265 (struct sockaddr *)ripsrc, n, opts) == 0) {
266 soroverflow_locked(so);
271 sorwakeup_locked(so);
278 * Setup generic address and protocol structures for raw_input routine, then
279 * pass them along with mbuf chain.
282 rip_input(struct mbuf **mp, int *offp, int proto)
285 struct mbuf *m = *mp;
286 struct ip *ip = mtod(m, struct ip *);
287 struct inpcb *inp, *last;
288 struct sockaddr_in ripsrc;
295 bzero(&ripsrc, sizeof(ripsrc));
296 ripsrc.sin_len = sizeof(ripsrc);
297 ripsrc.sin_family = AF_INET;
298 ripsrc.sin_addr = ip->ip_src;
301 ifp = m->m_pkthdr.rcvif;
303 hash = INP_PCBHASH_RAW(proto, ip->ip_src.s_addr,
304 ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask);
305 CK_LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[hash], inp_hash) {
306 if (inp->inp_ip_p != proto)
309 /* XXX inp locking */
310 if ((inp->inp_vflag & INP_IPV4) == 0)
313 if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
315 if (inp->inp_faddr.s_addr != ip->ip_src.s_addr)
320 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
322 (void) rip_append(last, ip, n, &ripsrc);
323 /* XXX count dropped packet */
328 if (__predict_false(inp->inp_flags2 & INP_FREED))
330 if (jailed_without_vnet(inp->inp_cred)) {
332 * XXX: If faddr was bound to multicast group,
333 * jailed raw socket will drop datagram.
335 if (prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
343 CK_LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[0], inp_hash) {
344 if (inp->inp_ip_p && inp->inp_ip_p != proto)
347 /* XXX inp locking */
348 if ((inp->inp_vflag & INP_IPV4) == 0)
351 if (!in_nullhost(inp->inp_laddr) &&
352 !in_hosteq(inp->inp_laddr, ip->ip_dst))
354 if (!in_nullhost(inp->inp_faddr) &&
355 !in_hosteq(inp->inp_faddr, ip->ip_src))
360 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
362 (void) rip_append(last, ip, n, &ripsrc);
363 /* XXX count dropped packet */
368 if (__predict_false(inp->inp_flags2 & INP_FREED))
370 if (jailed_without_vnet(inp->inp_cred)) {
372 * Allow raw socket in jail to receive multicast;
373 * assume process had PRIV_NETINET_RAW at attach,
374 * and fall through into normal filter path if so.
376 if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
377 prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
381 * If this raw socket has multicast state, and we
382 * have received a multicast, check if this socket
383 * should receive it, as multicast filtering is now
384 * the responsibility of the transport layer.
386 if (inp->inp_moptions != NULL &&
387 IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
389 * If the incoming datagram is for IGMP, allow it
390 * through unconditionally to the raw socket.
392 * In the case of IGMPv2, we may not have explicitly
393 * joined the group, and may have set IFF_ALLMULTI
394 * on the interface. imo_multi_filter() may discard
395 * control traffic we actually need to see.
397 * Userland multicast routing daemons should continue
398 * filter the control traffic appropriately.
402 blocked = MCAST_PASS;
403 if (proto != IPPROTO_IGMP) {
404 struct sockaddr_in group;
406 bzero(&group, sizeof(struct sockaddr_in));
407 group.sin_len = sizeof(struct sockaddr_in);
408 group.sin_family = AF_INET;
409 group.sin_addr = ip->ip_dst;
411 blocked = imo_multi_filter(inp->inp_moptions,
413 (struct sockaddr *)&group,
414 (struct sockaddr *)&ripsrc);
417 if (blocked != MCAST_PASS) {
418 IPSTAT_INC(ips_notmember);
428 if (rip_append(last, ip, m, &ripsrc) != 0)
429 IPSTAT_INC(ips_delivered);
432 if (inetsw[ip_protox[ip->ip_p]].pr_input == rip_input) {
433 IPSTAT_INC(ips_noproto);
434 IPSTAT_DEC(ips_delivered);
435 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL, 0, 0);
440 return (IPPROTO_DONE);
444 * Generate IP header and pass packet to ip_output. Tack on options user may
445 * have setup with control call.
448 rip_output(struct mbuf *m, struct socket *so, ...)
450 struct epoch_tracker et;
453 struct inpcb *inp = sotoinpcb(so);
456 int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
459 u_char opttype, optlen, *cp;
462 dst = va_arg(ap, u_long);
466 * If the user handed us a complete IP packet, use it. Otherwise,
467 * allocate an mbuf for a header and fill it in.
469 if ((inp->inp_flags & INP_HDRINCL) == 0) {
470 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
474 M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
479 ip = mtod(m, struct ip *);
480 ip->ip_tos = inp->inp_ip_tos;
481 if (inp->inp_flags & INP_DONTFRAG)
482 ip->ip_off = htons(IP_DF);
484 ip->ip_off = htons(0);
485 ip->ip_p = inp->inp_ip_p;
486 ip->ip_len = htons(m->m_pkthdr.len);
487 ip->ip_src = inp->inp_laddr;
488 ip->ip_dst.s_addr = dst;
490 if (CALC_FLOWID_OUTBOUND) {
491 uint32_t hash_type, hash_val;
493 hash_val = fib4_calc_software_hash(ip->ip_src,
494 ip->ip_dst, 0, 0, ip->ip_p, &hash_type);
495 m->m_pkthdr.flowid = hash_val;
496 M_HASHTYPE_SET(m, hash_type);
497 flags |= IP_NODEFAULTFLOWID;
500 if (jailed(inp->inp_cred)) {
502 * prison_local_ip4() would be good enough but would
503 * let a source of INADDR_ANY pass, which we do not
504 * want to see from jails.
506 if (ip->ip_src.s_addr == INADDR_ANY) {
508 error = in_pcbladdr(inp, &ip->ip_dst,
509 &ip->ip_src, inp->inp_cred);
512 error = prison_local_ip4(inp->inp_cred,
521 ip->ip_ttl = inp->inp_ip_ttl;
523 if (m->m_pkthdr.len > IP_MAXPACKET) {
527 if (m->m_pkthdr.len < sizeof(*ip)) {
531 m = m_pullup(m, sizeof(*ip));
534 ip = mtod(m, struct ip *);
535 hlen = ip->ip_hl << 2;
536 if (m->m_len < hlen) {
537 m = m_pullup(m, hlen);
540 ip = mtod(m, struct ip *);
543 if (CALC_FLOWID_OUTBOUND) {
544 uint32_t hash_type, hash_val;
546 hash_val = fib4_calc_software_hash(ip->ip_dst,
547 ip->ip_src, 0, 0, ip->ip_p, &hash_type);
548 m->m_pkthdr.flowid = hash_val;
549 M_HASHTYPE_SET(m, hash_type);
550 flags |= IP_NODEFAULTFLOWID;
555 * Don't allow both user specified and setsockopt options,
556 * and don't allow packet length sizes that will crash.
558 if ((hlen < sizeof (*ip))
559 || ((hlen > sizeof (*ip)) && inp->inp_options)
560 || (ntohs(ip->ip_len) != m->m_pkthdr.len)) {
565 error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
572 * Don't allow IP options which do not have the required
573 * structure as specified in section 3.1 of RFC 791 on
576 cp = (u_char *)(ip + 1);
577 cnt = hlen - sizeof (struct ip);
578 for (; cnt > 0; cnt -= optlen, cp += optlen) {
579 opttype = cp[IPOPT_OPTVAL];
580 if (opttype == IPOPT_EOL)
582 if (opttype == IPOPT_NOP) {
586 if (cnt < IPOPT_OLEN + sizeof(u_char)) {
591 optlen = cp[IPOPT_OLEN];
592 if (optlen < IPOPT_OLEN + sizeof(u_char) ||
600 * This doesn't allow application to specify ID of zero,
601 * but we got this limitation from the beginning of history.
607 * XXX prevent ip_output from overwriting header fields.
609 flags |= IP_RAWOUTPUT;
610 IPSTAT_INC(ips_rawout);
613 if (inp->inp_flags & INP_ONESBCAST)
614 flags |= IP_SENDONES;
617 mac_inpcb_create_mbuf(inp, m);
621 error = ip_output(m, inp->inp_options, NULL, flags,
622 inp->inp_moptions, inp);
629 * Raw IP socket option processing.
631 * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
632 * only be created by a privileged process, and as such, socket option
633 * operations to manage system properties on any raw socket were allowed to
634 * take place without explicit additional access control checks. However,
635 * raw sockets can now also be created in jail(), and therefore explicit
636 * checks are now required. Likewise, raw sockets can be used by a process
637 * after it gives up privilege, so some caution is required. For options
638 * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
639 * performed in ip_ctloutput() and therefore no check occurs here.
640 * Unilaterally checking priv_check() here breaks normal IP socket option
641 * operations on raw sockets.
643 * When adding new socket options here, make sure to add access control
644 * checks here as necessary.
646 * XXX-BZ inp locking?
649 rip_ctloutput(struct socket *so, struct sockopt *sopt)
651 struct inpcb *inp = sotoinpcb(so);
654 if (sopt->sopt_level != IPPROTO_IP) {
655 if ((sopt->sopt_level == SOL_SOCKET) &&
656 (sopt->sopt_name == SO_SETFIB)) {
657 inp->inp_inc.inc_fibnum = so->so_fibnum;
664 switch (sopt->sopt_dir) {
666 switch (sopt->sopt_name) {
668 optval = inp->inp_flags & INP_HDRINCL;
669 error = sooptcopyout(sopt, &optval, sizeof optval);
672 case IP_FW3: /* generic ipfw v.3 functions */
673 case IP_FW_ADD: /* ADD actually returns the body... */
675 case IP_FW_TABLE_GETSIZE:
676 case IP_FW_TABLE_LIST:
677 case IP_FW_NAT_GET_CONFIG:
678 case IP_FW_NAT_GET_LOG:
679 if (V_ip_fw_ctl_ptr != NULL)
680 error = V_ip_fw_ctl_ptr(sopt);
685 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
686 case IP_DUMMYNET_GET:
687 if (ip_dn_ctl_ptr != NULL)
688 error = ip_dn_ctl_ptr(sopt);
701 case MRT_API_SUPPORT:
703 case MRT_ADD_BW_UPCALL:
704 case MRT_DEL_BW_UPCALL:
705 error = priv_check(curthread, PRIV_NETINET_MROUTE);
708 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
713 error = ip_ctloutput(so, sopt);
719 switch (sopt->sopt_name) {
721 error = sooptcopyin(sopt, &optval, sizeof optval,
726 inp->inp_flags |= INP_HDRINCL;
728 inp->inp_flags &= ~INP_HDRINCL;
731 case IP_FW3: /* generic ipfw v.3 functions */
737 case IP_FW_TABLE_ADD:
738 case IP_FW_TABLE_DEL:
739 case IP_FW_TABLE_FLUSH:
742 if (V_ip_fw_ctl_ptr != NULL)
743 error = V_ip_fw_ctl_ptr(sopt);
748 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
749 case IP_DUMMYNET_CONFIGURE:
750 case IP_DUMMYNET_DEL:
751 case IP_DUMMYNET_FLUSH:
752 if (ip_dn_ctl_ptr != NULL)
753 error = ip_dn_ctl_ptr(sopt);
755 error = ENOPROTOOPT ;
759 error = priv_check(curthread, PRIV_NETINET_MROUTE);
762 error = ip_rsvp_init(so);
766 error = priv_check(curthread, PRIV_NETINET_MROUTE);
769 error = ip_rsvp_done();
773 case IP_RSVP_VIF_OFF:
774 error = priv_check(curthread, PRIV_NETINET_MROUTE);
777 error = ip_rsvp_vif ?
778 ip_rsvp_vif(so, sopt) : EINVAL;
789 case MRT_API_SUPPORT:
791 case MRT_ADD_BW_UPCALL:
792 case MRT_DEL_BW_UPCALL:
793 error = priv_check(curthread, PRIV_NETINET_MROUTE);
796 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
801 error = ip_ctloutput(so, sopt);
811 * This function exists solely to receive the PRC_IFDOWN messages which are
812 * sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, and calls
813 * in_ifadown() to remove all routes corresponding to that address. It also
814 * receives the PRC_IFUP messages from if_up() and reinstalls the interface
818 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
820 struct rm_priotracker in_ifa_tracker;
821 struct in_ifaddr *ia;
828 IN_IFADDR_RLOCK(&in_ifa_tracker);
829 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
830 if (ia->ia_ifa.ifa_addr == sa
831 && (ia->ia_flags & IFA_ROUTE)) {
832 ifa_ref(&ia->ia_ifa);
833 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
835 * in_scrubprefix() kills the interface route.
837 in_scrubprefix(ia, 0);
839 * in_ifadown gets rid of all the rest of the
840 * routes. This is not quite the right thing
841 * to do, but at least if we are running a
842 * routing process they will come back.
844 in_ifadown(&ia->ia_ifa, 0);
845 ifa_free(&ia->ia_ifa);
849 if (ia == NULL) /* If ia matched, already unlocked. */
850 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
854 IN_IFADDR_RLOCK(&in_ifa_tracker);
855 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
856 if (ia->ia_ifa.ifa_addr == sa)
859 if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
860 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
863 ifa_ref(&ia->ia_ifa);
864 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
866 ifp = ia->ia_ifa.ifa_ifp;
868 if ((ifp->if_flags & IFF_LOOPBACK)
869 || (ifp->if_flags & IFF_POINTOPOINT))
872 err = ifa_del_loopback_route((struct ifaddr *)ia, sa);
874 rt_addrmsg(RTM_ADD, &ia->ia_ifa, ia->ia_ifp->if_fib);
875 err = in_handle_ifaddr_route(RTM_ADD, ia);
877 ia->ia_flags |= IFA_ROUTE;
879 err = ifa_add_loopback_route((struct ifaddr *)ia, sa);
881 ifa_free(&ia->ia_ifa);
887 rip_attach(struct socket *so, int proto, struct thread *td)
893 KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
895 error = priv_check(td, PRIV_NETINET_RAW);
898 if (proto >= IPPROTO_MAX || proto < 0)
899 return EPROTONOSUPPORT;
900 error = soreserve(so, rip_sendspace, rip_recvspace);
903 INP_INFO_WLOCK(&V_ripcbinfo);
904 error = in_pcballoc(so, &V_ripcbinfo);
906 INP_INFO_WUNLOCK(&V_ripcbinfo);
909 inp = (struct inpcb *)so->so_pcb;
910 inp->inp_vflag |= INP_IPV4;
911 inp->inp_ip_p = proto;
912 inp->inp_ip_ttl = V_ip_defttl;
914 INP_INFO_WUNLOCK(&V_ripcbinfo);
920 rip_detach(struct socket *so)
925 KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
926 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
927 ("rip_detach: not closed"));
929 INP_INFO_WLOCK(&V_ripcbinfo);
932 if (so == V_ip_mrouter && ip_mrouter_done)
934 if (ip_rsvp_force_done)
935 ip_rsvp_force_done(so);
936 if (so == V_ip_rsvpd)
940 INP_INFO_WUNLOCK(&V_ripcbinfo);
944 rip_dodisconnect(struct socket *so, struct inpcb *inp)
946 struct inpcbinfo *pcbinfo;
948 pcbinfo = inp->inp_pcbinfo;
949 INP_INFO_WLOCK(pcbinfo);
952 inp->inp_faddr.s_addr = INADDR_ANY;
955 so->so_state &= ~SS_ISCONNECTED;
958 INP_INFO_WUNLOCK(pcbinfo);
962 rip_abort(struct socket *so)
967 KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
969 rip_dodisconnect(so, inp);
973 rip_close(struct socket *so)
978 KASSERT(inp != NULL, ("rip_close: inp == NULL"));
980 rip_dodisconnect(so, inp);
984 rip_disconnect(struct socket *so)
988 if ((so->so_state & SS_ISCONNECTED) == 0)
992 KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
994 rip_dodisconnect(so, inp);
999 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1001 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
1005 if (nam->sa_family != AF_INET)
1006 return (EAFNOSUPPORT);
1007 if (nam->sa_len != sizeof(*addr))
1010 error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
1014 inp = sotoinpcb(so);
1015 KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
1017 if (CK_STAILQ_EMPTY(&V_ifnet) ||
1018 (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
1019 (addr->sin_addr.s_addr &&
1020 (inp->inp_flags & INP_BINDANY) == 0 &&
1021 ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
1022 return (EADDRNOTAVAIL);
1024 INP_INFO_WLOCK(&V_ripcbinfo);
1027 inp->inp_laddr = addr->sin_addr;
1030 INP_INFO_WUNLOCK(&V_ripcbinfo);
1035 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1037 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
1040 if (nam->sa_len != sizeof(*addr))
1042 if (CK_STAILQ_EMPTY(&V_ifnet))
1043 return (EADDRNOTAVAIL);
1044 if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
1045 return (EAFNOSUPPORT);
1047 inp = sotoinpcb(so);
1048 KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
1050 INP_INFO_WLOCK(&V_ripcbinfo);
1053 inp->inp_faddr = addr->sin_addr;
1057 INP_INFO_WUNLOCK(&V_ripcbinfo);
1062 rip_shutdown(struct socket *so)
1066 inp = sotoinpcb(so);
1067 KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
1076 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
1077 struct mbuf *control, struct thread *td)
1083 inp = sotoinpcb(so);
1084 KASSERT(inp != NULL, ("rip_send: inp == NULL"));
1086 if (control != NULL) {
1092 * Note: 'dst' reads below are unlocked.
1094 if (so->so_state & SS_ISCONNECTED) {
1099 dst = inp->inp_faddr.s_addr; /* Unlocked read. */
1104 else if (nam->sa_family != AF_INET)
1105 error = EAFNOSUPPORT;
1106 else if (nam->sa_len != sizeof(struct sockaddr_in))
1110 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
1112 return (rip_output(m, so, dst));
1121 rip_pcblist(SYSCTL_HANDLER_ARGS)
1124 struct epoch_tracker et;
1128 if (req->newptr != 0)
1131 if (req->oldptr == 0) {
1134 n = V_ripcbinfo.ipi_count;
1135 n += imax(n / 8, 10);
1136 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
1140 if ((error = sysctl_wire_old_buffer(req, 0)) != 0)
1143 bzero(&xig, sizeof(xig));
1144 xig.xig_len = sizeof xig;
1145 xig.xig_count = V_ripcbinfo.ipi_count;
1146 xig.xig_gen = V_ripcbinfo.ipi_gencnt;
1147 xig.xig_sogen = so_gencnt;
1148 error = SYSCTL_OUT(req, &xig, sizeof xig);
1152 NET_EPOCH_ENTER(et);
1153 for (inp = CK_LIST_FIRST(V_ripcbinfo.ipi_listhead);
1155 inp = CK_LIST_NEXT(inp, inp_list)) {
1157 if (inp->inp_gencnt <= xig.xig_gen &&
1158 cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
1161 in_pcbtoxinpcb(inp, &xi);
1163 error = SYSCTL_OUT(req, &xi, sizeof xi);
1173 * Give the user an updated idea of our state. If the
1174 * generation differs from what we told her before, she knows
1175 * that something happened while we were processing this
1176 * request, and it might be necessary to retry.
1178 xig.xig_gen = V_ripcbinfo.ipi_gencnt;
1179 xig.xig_sogen = so_gencnt;
1180 xig.xig_count = V_ripcbinfo.ipi_count;
1181 error = SYSCTL_OUT(req, &xig, sizeof xig);
1187 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist,
1188 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
1189 rip_pcblist, "S,xinpcb",
1190 "List of active raw IP sockets");
1193 struct pr_usrreqs rip_usrreqs = {
1194 .pru_abort = rip_abort,
1195 .pru_attach = rip_attach,
1196 .pru_bind = rip_bind,
1197 .pru_connect = rip_connect,
1198 .pru_control = in_control,
1199 .pru_detach = rip_detach,
1200 .pru_disconnect = rip_disconnect,
1201 .pru_peeraddr = in_getpeeraddr,
1202 .pru_send = rip_send,
1203 .pru_shutdown = rip_shutdown,
1204 .pru_sockaddr = in_getsockaddr,
1205 .pru_sosetlabel = in_pcbsosetlabel,
1206 .pru_close = rip_close,