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>
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 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 inpcbhead, ripcb);
91 VNET_DEFINE(struct inpcbinfo, ripcbinfo);
93 #define V_ripcb VNET(ripcb)
94 #define V_ripcbinfo VNET(ripcbinfo)
97 * Control and data hooks for ipfw, dummynet, divert and so on.
98 * The data hooks are not used here but it is convenient
99 * to keep them all in one place.
101 VNET_DEFINE(ip_fw_chk_ptr_t, ip_fw_chk_ptr) = NULL;
102 VNET_DEFINE(ip_fw_ctl_ptr_t, ip_fw_ctl_ptr) = NULL;
104 int (*ip_dn_ctl_ptr)(struct sockopt *);
105 int (*ip_dn_io_ptr)(struct mbuf **, struct ip_fw_args *);
106 void (*ip_divert_ptr)(struct mbuf *, bool);
107 int (*ng_ipfw_input_p)(struct mbuf **, struct ip_fw_args *, bool);
111 * Hooks for multicast routing. They all default to NULL, so leave them not
112 * initialized and rely on BSS being set to 0.
116 * The socket used to communicate with the multicast routing daemon.
118 VNET_DEFINE(struct socket *, ip_mrouter);
121 * The various mrouter and rsvp functions.
123 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
124 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
125 int (*ip_mrouter_done)(void);
126 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
127 struct ip_moptions *);
128 int (*mrt_ioctl)(u_long, caddr_t, int);
129 int (*legal_vif_num)(int);
130 u_long (*ip_mcast_src)(int);
132 int (*rsvp_input_p)(struct mbuf **, int *, int);
133 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
134 void (*ip_rsvp_force_done)(struct socket *);
137 extern struct protosw inetsw[];
139 u_long rip_sendspace = 9216;
140 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
141 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
143 u_long rip_recvspace = 9216;
144 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
145 &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
151 #define INP_PCBHASH_RAW_SIZE 256
152 #define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \
153 (((proto) + (laddr) + (faddr)) % (mask) + 1)
157 rip_inshash(struct inpcb *inp)
159 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
160 struct inpcbhead *pcbhash;
163 INP_INFO_WLOCK_ASSERT(pcbinfo);
164 INP_WLOCK_ASSERT(inp);
166 if (inp->inp_ip_p != 0 &&
167 inp->inp_laddr.s_addr != INADDR_ANY &&
168 inp->inp_faddr.s_addr != INADDR_ANY) {
169 hash = INP_PCBHASH_RAW(inp->inp_ip_p, inp->inp_laddr.s_addr,
170 inp->inp_faddr.s_addr, pcbinfo->ipi_hashmask);
173 pcbhash = &pcbinfo->ipi_hashbase[hash];
174 CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
178 rip_delhash(struct inpcb *inp)
181 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
182 INP_WLOCK_ASSERT(inp);
184 CK_LIST_REMOVE(inp, inp_hash);
189 * Raw interface to IP protocol.
193 * Initialize raw connection block q.
196 rip_zone_change(void *tag)
199 uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
203 rip_inpcb_init(void *mem, int size, int flags)
205 struct inpcb *inp = mem;
207 INP_LOCK_INIT(inp, "inp", "rawinp");
215 in_pcbinfo_init(&V_ripcbinfo, "rip", &V_ripcb, INP_PCBHASH_RAW_SIZE,
216 1, "ripcb", rip_inpcb_init, IPI_HASHFIELDS_NONE);
217 EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change, NULL,
218 EVENTHANDLER_PRI_ANY);
223 rip_destroy(void *unused __unused)
226 in_pcbinfo_destroy(&V_ripcbinfo);
228 VNET_SYSUNINIT(raw_ip, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, rip_destroy, NULL);
233 rip_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
234 struct sockaddr_in *ripsrc)
238 INP_LOCK_ASSERT(last);
240 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
241 /* check AH/ESP integrity. */
242 if (IPSEC_ENABLED(ipv4)) {
243 if (IPSEC_CHECK_POLICY(ipv4, n, last) != 0)
248 if (!policyfail && mac_inpcb_check_deliver(last, n) != 0)
251 /* Check the minimum TTL for socket. */
252 if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
255 struct mbuf *opts = NULL;
258 so = last->inp_socket;
259 if ((last->inp_flags & INP_CONTROLOPTS) ||
260 (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
261 ip_savecontrol(last, &opts, ip, n);
262 SOCKBUF_LOCK(&so->so_rcv);
263 if (sbappendaddr_locked(&so->so_rcv,
264 (struct sockaddr *)ripsrc, n, opts) == 0) {
265 /* should notify about lost packet */
269 SOCKBUF_UNLOCK(&so->so_rcv);
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 ip = mtod(m, struct ip *);
528 hlen = ip->ip_hl << 2;
529 if (m->m_len < hlen) {
530 m = m_pullup(m, hlen);
533 ip = mtod(m, struct ip *);
536 if (CALC_FLOWID_OUTBOUND) {
537 uint32_t hash_type, hash_val;
539 hash_val = fib4_calc_software_hash(ip->ip_dst,
540 ip->ip_src, 0, 0, ip->ip_p, &hash_type);
541 m->m_pkthdr.flowid = hash_val;
542 M_HASHTYPE_SET(m, hash_type);
543 flags |= IP_NODEFAULTFLOWID;
548 * Don't allow both user specified and setsockopt options,
549 * and don't allow packet length sizes that will crash.
551 if ((hlen < sizeof (*ip))
552 || ((hlen > sizeof (*ip)) && inp->inp_options)
553 || (ntohs(ip->ip_len) != m->m_pkthdr.len)) {
558 error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
565 * Don't allow IP options which do not have the required
566 * structure as specified in section 3.1 of RFC 791 on
569 cp = (u_char *)(ip + 1);
570 cnt = hlen - sizeof (struct ip);
571 for (; cnt > 0; cnt -= optlen, cp += optlen) {
572 opttype = cp[IPOPT_OPTVAL];
573 if (opttype == IPOPT_EOL)
575 if (opttype == IPOPT_NOP) {
579 if (cnt < IPOPT_OLEN + sizeof(u_char)) {
584 optlen = cp[IPOPT_OLEN];
585 if (optlen < IPOPT_OLEN + sizeof(u_char) ||
593 * This doesn't allow application to specify ID of zero,
594 * but we got this limitation from the beginning of history.
600 * XXX prevent ip_output from overwriting header fields.
602 flags |= IP_RAWOUTPUT;
603 IPSTAT_INC(ips_rawout);
606 if (inp->inp_flags & INP_ONESBCAST)
607 flags |= IP_SENDONES;
610 mac_inpcb_create_mbuf(inp, m);
614 error = ip_output(m, inp->inp_options, NULL, flags,
615 inp->inp_moptions, inp);
622 * Raw IP socket option processing.
624 * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
625 * only be created by a privileged process, and as such, socket option
626 * operations to manage system properties on any raw socket were allowed to
627 * take place without explicit additional access control checks. However,
628 * raw sockets can now also be created in jail(), and therefore explicit
629 * checks are now required. Likewise, raw sockets can be used by a process
630 * after it gives up privilege, so some caution is required. For options
631 * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
632 * performed in ip_ctloutput() and therefore no check occurs here.
633 * Unilaterally checking priv_check() here breaks normal IP socket option
634 * operations on raw sockets.
636 * When adding new socket options here, make sure to add access control
637 * checks here as necessary.
639 * XXX-BZ inp locking?
642 rip_ctloutput(struct socket *so, struct sockopt *sopt)
644 struct inpcb *inp = sotoinpcb(so);
647 if (sopt->sopt_level != IPPROTO_IP) {
648 if ((sopt->sopt_level == SOL_SOCKET) &&
649 (sopt->sopt_name == SO_SETFIB)) {
650 inp->inp_inc.inc_fibnum = so->so_fibnum;
657 switch (sopt->sopt_dir) {
659 switch (sopt->sopt_name) {
661 optval = inp->inp_flags & INP_HDRINCL;
662 error = sooptcopyout(sopt, &optval, sizeof optval);
665 case IP_FW3: /* generic ipfw v.3 functions */
666 case IP_FW_ADD: /* ADD actually returns the body... */
668 case IP_FW_TABLE_GETSIZE:
669 case IP_FW_TABLE_LIST:
670 case IP_FW_NAT_GET_CONFIG:
671 case IP_FW_NAT_GET_LOG:
672 if (V_ip_fw_ctl_ptr != NULL)
673 error = V_ip_fw_ctl_ptr(sopt);
678 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
679 case IP_DUMMYNET_GET:
680 if (ip_dn_ctl_ptr != NULL)
681 error = ip_dn_ctl_ptr(sopt);
694 case MRT_API_SUPPORT:
696 case MRT_ADD_BW_UPCALL:
697 case MRT_DEL_BW_UPCALL:
698 error = priv_check(curthread, PRIV_NETINET_MROUTE);
701 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
706 error = ip_ctloutput(so, sopt);
712 switch (sopt->sopt_name) {
714 error = sooptcopyin(sopt, &optval, sizeof optval,
719 inp->inp_flags |= INP_HDRINCL;
721 inp->inp_flags &= ~INP_HDRINCL;
724 case IP_FW3: /* generic ipfw v.3 functions */
730 case IP_FW_TABLE_ADD:
731 case IP_FW_TABLE_DEL:
732 case IP_FW_TABLE_FLUSH:
735 if (V_ip_fw_ctl_ptr != NULL)
736 error = V_ip_fw_ctl_ptr(sopt);
741 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
742 case IP_DUMMYNET_CONFIGURE:
743 case IP_DUMMYNET_DEL:
744 case IP_DUMMYNET_FLUSH:
745 if (ip_dn_ctl_ptr != NULL)
746 error = ip_dn_ctl_ptr(sopt);
748 error = ENOPROTOOPT ;
752 error = priv_check(curthread, PRIV_NETINET_MROUTE);
755 error = ip_rsvp_init(so);
759 error = priv_check(curthread, PRIV_NETINET_MROUTE);
762 error = ip_rsvp_done();
766 case IP_RSVP_VIF_OFF:
767 error = priv_check(curthread, PRIV_NETINET_MROUTE);
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 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
794 error = ip_ctloutput(so, sopt);
804 * This function exists solely to receive the PRC_IFDOWN messages which are
805 * sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, and calls
806 * in_ifadown() to remove all routes corresponding to that address. It also
807 * receives the PRC_IFUP messages from if_up() and reinstalls the interface
811 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
813 struct rm_priotracker in_ifa_tracker;
814 struct in_ifaddr *ia;
821 IN_IFADDR_RLOCK(&in_ifa_tracker);
822 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
823 if (ia->ia_ifa.ifa_addr == sa
824 && (ia->ia_flags & IFA_ROUTE)) {
825 ifa_ref(&ia->ia_ifa);
826 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
828 * in_scrubprefix() kills the interface route.
830 in_scrubprefix(ia, 0);
832 * in_ifadown gets rid of all the rest of the
833 * routes. This is not quite the right thing
834 * to do, but at least if we are running a
835 * routing process they will come back.
837 in_ifadown(&ia->ia_ifa, 0);
838 ifa_free(&ia->ia_ifa);
842 if (ia == NULL) /* If ia matched, already unlocked. */
843 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
847 IN_IFADDR_RLOCK(&in_ifa_tracker);
848 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
849 if (ia->ia_ifa.ifa_addr == sa)
852 if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
853 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
856 ifa_ref(&ia->ia_ifa);
857 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
859 ifp = ia->ia_ifa.ifa_ifp;
861 if ((ifp->if_flags & IFF_LOOPBACK)
862 || (ifp->if_flags & IFF_POINTOPOINT))
865 err = ifa_del_loopback_route((struct ifaddr *)ia, sa);
867 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
869 ia->ia_flags |= IFA_ROUTE;
871 err = ifa_add_loopback_route((struct ifaddr *)ia, sa);
873 ifa_free(&ia->ia_ifa);
879 rip_attach(struct socket *so, int proto, struct thread *td)
885 KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
887 error = priv_check(td, PRIV_NETINET_RAW);
890 if (proto >= IPPROTO_MAX || proto < 0)
891 return EPROTONOSUPPORT;
892 error = soreserve(so, rip_sendspace, rip_recvspace);
895 INP_INFO_WLOCK(&V_ripcbinfo);
896 error = in_pcballoc(so, &V_ripcbinfo);
898 INP_INFO_WUNLOCK(&V_ripcbinfo);
901 inp = (struct inpcb *)so->so_pcb;
902 inp->inp_vflag |= INP_IPV4;
903 inp->inp_ip_p = proto;
904 inp->inp_ip_ttl = V_ip_defttl;
906 INP_INFO_WUNLOCK(&V_ripcbinfo);
912 rip_detach(struct socket *so)
917 KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
918 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
919 ("rip_detach: not closed"));
921 INP_INFO_WLOCK(&V_ripcbinfo);
924 if (so == V_ip_mrouter && ip_mrouter_done)
926 if (ip_rsvp_force_done)
927 ip_rsvp_force_done(so);
928 if (so == V_ip_rsvpd)
932 INP_INFO_WUNLOCK(&V_ripcbinfo);
936 rip_dodisconnect(struct socket *so, struct inpcb *inp)
938 struct inpcbinfo *pcbinfo;
940 pcbinfo = inp->inp_pcbinfo;
941 INP_INFO_WLOCK(pcbinfo);
944 inp->inp_faddr.s_addr = INADDR_ANY;
947 so->so_state &= ~SS_ISCONNECTED;
950 INP_INFO_WUNLOCK(pcbinfo);
954 rip_abort(struct socket *so)
959 KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
961 rip_dodisconnect(so, inp);
965 rip_close(struct socket *so)
970 KASSERT(inp != NULL, ("rip_close: inp == NULL"));
972 rip_dodisconnect(so, inp);
976 rip_disconnect(struct socket *so)
980 if ((so->so_state & SS_ISCONNECTED) == 0)
984 KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
986 rip_dodisconnect(so, inp);
991 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
993 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
997 if (nam->sa_len != sizeof(*addr))
1000 error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
1004 inp = sotoinpcb(so);
1005 KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
1007 if (CK_STAILQ_EMPTY(&V_ifnet) ||
1008 (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
1009 (addr->sin_addr.s_addr &&
1010 (inp->inp_flags & INP_BINDANY) == 0 &&
1011 ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
1012 return (EADDRNOTAVAIL);
1014 INP_INFO_WLOCK(&V_ripcbinfo);
1017 inp->inp_laddr = addr->sin_addr;
1020 INP_INFO_WUNLOCK(&V_ripcbinfo);
1025 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1027 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
1030 if (nam->sa_len != sizeof(*addr))
1032 if (CK_STAILQ_EMPTY(&V_ifnet))
1033 return (EADDRNOTAVAIL);
1034 if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
1035 return (EAFNOSUPPORT);
1037 inp = sotoinpcb(so);
1038 KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
1040 INP_INFO_WLOCK(&V_ripcbinfo);
1043 inp->inp_faddr = addr->sin_addr;
1047 INP_INFO_WUNLOCK(&V_ripcbinfo);
1052 rip_shutdown(struct socket *so)
1056 inp = sotoinpcb(so);
1057 KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
1066 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
1067 struct mbuf *control, struct thread *td)
1072 inp = sotoinpcb(so);
1073 KASSERT(inp != NULL, ("rip_send: inp == NULL"));
1076 * Note: 'dst' reads below are unlocked.
1078 if (so->so_state & SS_ISCONNECTED) {
1083 dst = inp->inp_faddr.s_addr; /* Unlocked read. */
1089 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
1091 return (rip_output(m, so, dst));
1096 rip_pcblist(SYSCTL_HANDLER_ARGS)
1099 struct epoch_tracker et;
1103 if (req->newptr != 0)
1106 if (req->oldptr == 0) {
1109 n = V_ripcbinfo.ipi_count;
1110 n += imax(n / 8, 10);
1111 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
1115 if ((error = sysctl_wire_old_buffer(req, 0)) != 0)
1118 bzero(&xig, sizeof(xig));
1119 xig.xig_len = sizeof xig;
1120 xig.xig_count = V_ripcbinfo.ipi_count;
1121 xig.xig_gen = V_ripcbinfo.ipi_gencnt;
1122 xig.xig_sogen = so_gencnt;
1123 error = SYSCTL_OUT(req, &xig, sizeof xig);
1127 NET_EPOCH_ENTER(et);
1128 for (inp = CK_LIST_FIRST(V_ripcbinfo.ipi_listhead);
1130 inp = CK_LIST_NEXT(inp, inp_list)) {
1132 if (inp->inp_gencnt <= xig.xig_gen &&
1133 cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
1136 in_pcbtoxinpcb(inp, &xi);
1138 error = SYSCTL_OUT(req, &xi, sizeof xi);
1148 * Give the user an updated idea of our state. If the
1149 * generation differs from what we told her before, she knows
1150 * that something happened while we were processing this
1151 * request, and it might be necessary to retry.
1153 xig.xig_gen = V_ripcbinfo.ipi_gencnt;
1154 xig.xig_sogen = so_gencnt;
1155 xig.xig_count = V_ripcbinfo.ipi_count;
1156 error = SYSCTL_OUT(req, &xig, sizeof xig);
1162 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist,
1163 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
1164 rip_pcblist, "S,xinpcb",
1165 "List of active raw IP sockets");
1168 struct pr_usrreqs rip_usrreqs = {
1169 .pru_abort = rip_abort,
1170 .pru_attach = rip_attach,
1171 .pru_bind = rip_bind,
1172 .pru_connect = rip_connect,
1173 .pru_control = in_control,
1174 .pru_detach = rip_detach,
1175 .pru_disconnect = rip_disconnect,
1176 .pru_peeraddr = in_getpeeraddr,
1177 .pru_send = rip_send,
1178 .pru_shutdown = rip_shutdown,
1179 .pru_sockaddr = in_getsockaddr,
1180 .pru_sosetlabel = in_pcbsosetlabel,
1181 .pru_close = rip_close,