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 /* should notify about lost packet */
270 SOCKBUF_UNLOCK(&so->so_rcv);
272 sorwakeup_locked(so);
279 * Setup generic address and protocol structures for raw_input routine, then
280 * pass them along with mbuf chain.
283 rip_input(struct mbuf **mp, int *offp, int proto)
286 struct mbuf *m = *mp;
287 struct ip *ip = mtod(m, struct ip *);
288 struct inpcb *inp, *last;
289 struct sockaddr_in ripsrc;
296 bzero(&ripsrc, sizeof(ripsrc));
297 ripsrc.sin_len = sizeof(ripsrc);
298 ripsrc.sin_family = AF_INET;
299 ripsrc.sin_addr = ip->ip_src;
302 ifp = m->m_pkthdr.rcvif;
304 hash = INP_PCBHASH_RAW(proto, ip->ip_src.s_addr,
305 ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask);
306 CK_LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[hash], inp_hash) {
307 if (inp->inp_ip_p != proto)
310 /* XXX inp locking */
311 if ((inp->inp_vflag & INP_IPV4) == 0)
314 if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
316 if (inp->inp_faddr.s_addr != ip->ip_src.s_addr)
321 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
323 (void) rip_append(last, ip, n, &ripsrc);
324 /* XXX count dropped packet */
329 if (__predict_false(inp->inp_flags2 & INP_FREED))
331 if (jailed_without_vnet(inp->inp_cred)) {
333 * XXX: If faddr was bound to multicast group,
334 * jailed raw socket will drop datagram.
336 if (prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
344 CK_LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[0], inp_hash) {
345 if (inp->inp_ip_p && inp->inp_ip_p != proto)
348 /* XXX inp locking */
349 if ((inp->inp_vflag & INP_IPV4) == 0)
352 if (!in_nullhost(inp->inp_laddr) &&
353 !in_hosteq(inp->inp_laddr, ip->ip_dst))
355 if (!in_nullhost(inp->inp_faddr) &&
356 !in_hosteq(inp->inp_faddr, ip->ip_src))
361 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
363 (void) rip_append(last, ip, n, &ripsrc);
364 /* XXX count dropped packet */
369 if (__predict_false(inp->inp_flags2 & INP_FREED))
371 if (jailed_without_vnet(inp->inp_cred)) {
373 * Allow raw socket in jail to receive multicast;
374 * assume process had PRIV_NETINET_RAW at attach,
375 * and fall through into normal filter path if so.
377 if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
378 prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
382 * If this raw socket has multicast state, and we
383 * have received a multicast, check if this socket
384 * should receive it, as multicast filtering is now
385 * the responsibility of the transport layer.
387 if (inp->inp_moptions != NULL &&
388 IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
390 * If the incoming datagram is for IGMP, allow it
391 * through unconditionally to the raw socket.
393 * In the case of IGMPv2, we may not have explicitly
394 * joined the group, and may have set IFF_ALLMULTI
395 * on the interface. imo_multi_filter() may discard
396 * control traffic we actually need to see.
398 * Userland multicast routing daemons should continue
399 * filter the control traffic appropriately.
403 blocked = MCAST_PASS;
404 if (proto != IPPROTO_IGMP) {
405 struct sockaddr_in group;
407 bzero(&group, sizeof(struct sockaddr_in));
408 group.sin_len = sizeof(struct sockaddr_in);
409 group.sin_family = AF_INET;
410 group.sin_addr = ip->ip_dst;
412 blocked = imo_multi_filter(inp->inp_moptions,
414 (struct sockaddr *)&group,
415 (struct sockaddr *)&ripsrc);
418 if (blocked != MCAST_PASS) {
419 IPSTAT_INC(ips_notmember);
429 if (rip_append(last, ip, m, &ripsrc) != 0)
430 IPSTAT_INC(ips_delivered);
433 if (inetsw[ip_protox[ip->ip_p]].pr_input == rip_input) {
434 IPSTAT_INC(ips_noproto);
435 IPSTAT_DEC(ips_delivered);
436 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL, 0, 0);
441 return (IPPROTO_DONE);
445 * Generate IP header and pass packet to ip_output. Tack on options user may
446 * have setup with control call.
449 rip_output(struct mbuf *m, struct socket *so, ...)
451 struct epoch_tracker et;
454 struct inpcb *inp = sotoinpcb(so);
457 int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
460 u_char opttype, optlen, *cp;
463 dst = va_arg(ap, u_long);
467 * If the user handed us a complete IP packet, use it. Otherwise,
468 * allocate an mbuf for a header and fill it in.
470 if ((inp->inp_flags & INP_HDRINCL) == 0) {
471 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
475 M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
480 ip = mtod(m, struct ip *);
481 ip->ip_tos = inp->inp_ip_tos;
482 if (inp->inp_flags & INP_DONTFRAG)
483 ip->ip_off = htons(IP_DF);
485 ip->ip_off = htons(0);
486 ip->ip_p = inp->inp_ip_p;
487 ip->ip_len = htons(m->m_pkthdr.len);
488 ip->ip_src = inp->inp_laddr;
489 ip->ip_dst.s_addr = dst;
491 if (CALC_FLOWID_OUTBOUND) {
492 uint32_t hash_type, hash_val;
494 hash_val = fib4_calc_software_hash(ip->ip_src,
495 ip->ip_dst, 0, 0, ip->ip_p, &hash_type);
496 m->m_pkthdr.flowid = hash_val;
497 M_HASHTYPE_SET(m, hash_type);
498 flags |= IP_NODEFAULTFLOWID;
501 if (jailed(inp->inp_cred)) {
503 * prison_local_ip4() would be good enough but would
504 * let a source of INADDR_ANY pass, which we do not
505 * want to see from jails.
507 if (ip->ip_src.s_addr == INADDR_ANY) {
509 error = in_pcbladdr(inp, &ip->ip_dst,
510 &ip->ip_src, inp->inp_cred);
513 error = prison_local_ip4(inp->inp_cred,
522 ip->ip_ttl = inp->inp_ip_ttl;
524 if (m->m_pkthdr.len > IP_MAXPACKET) {
528 ip = mtod(m, struct ip *);
529 hlen = ip->ip_hl << 2;
530 if (m->m_len < hlen) {
531 m = m_pullup(m, hlen);
534 ip = mtod(m, struct ip *);
537 if (CALC_FLOWID_OUTBOUND) {
538 uint32_t hash_type, hash_val;
540 hash_val = fib4_calc_software_hash(ip->ip_dst,
541 ip->ip_src, 0, 0, ip->ip_p, &hash_type);
542 m->m_pkthdr.flowid = hash_val;
543 M_HASHTYPE_SET(m, hash_type);
544 flags |= IP_NODEFAULTFLOWID;
549 * Don't allow both user specified and setsockopt options,
550 * and don't allow packet length sizes that will crash.
552 if ((hlen < sizeof (*ip))
553 || ((hlen > sizeof (*ip)) && inp->inp_options)
554 || (ntohs(ip->ip_len) != m->m_pkthdr.len)) {
559 error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
566 * Don't allow IP options which do not have the required
567 * structure as specified in section 3.1 of RFC 791 on
570 cp = (u_char *)(ip + 1);
571 cnt = hlen - sizeof (struct ip);
572 for (; cnt > 0; cnt -= optlen, cp += optlen) {
573 opttype = cp[IPOPT_OPTVAL];
574 if (opttype == IPOPT_EOL)
576 if (opttype == IPOPT_NOP) {
580 if (cnt < IPOPT_OLEN + sizeof(u_char)) {
585 optlen = cp[IPOPT_OLEN];
586 if (optlen < IPOPT_OLEN + sizeof(u_char) ||
594 * This doesn't allow application to specify ID of zero,
595 * but we got this limitation from the beginning of history.
601 * XXX prevent ip_output from overwriting header fields.
603 flags |= IP_RAWOUTPUT;
604 IPSTAT_INC(ips_rawout);
607 if (inp->inp_flags & INP_ONESBCAST)
608 flags |= IP_SENDONES;
611 mac_inpcb_create_mbuf(inp, m);
615 error = ip_output(m, inp->inp_options, NULL, flags,
616 inp->inp_moptions, inp);
623 * Raw IP socket option processing.
625 * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
626 * only be created by a privileged process, and as such, socket option
627 * operations to manage system properties on any raw socket were allowed to
628 * take place without explicit additional access control checks. However,
629 * raw sockets can now also be created in jail(), and therefore explicit
630 * checks are now required. Likewise, raw sockets can be used by a process
631 * after it gives up privilege, so some caution is required. For options
632 * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
633 * performed in ip_ctloutput() and therefore no check occurs here.
634 * Unilaterally checking priv_check() here breaks normal IP socket option
635 * operations on raw sockets.
637 * When adding new socket options here, make sure to add access control
638 * checks here as necessary.
640 * XXX-BZ inp locking?
643 rip_ctloutput(struct socket *so, struct sockopt *sopt)
645 struct inpcb *inp = sotoinpcb(so);
648 if (sopt->sopt_level != IPPROTO_IP) {
649 if ((sopt->sopt_level == SOL_SOCKET) &&
650 (sopt->sopt_name == SO_SETFIB)) {
651 inp->inp_inc.inc_fibnum = so->so_fibnum;
658 switch (sopt->sopt_dir) {
660 switch (sopt->sopt_name) {
662 optval = inp->inp_flags & INP_HDRINCL;
663 error = sooptcopyout(sopt, &optval, sizeof optval);
666 case IP_FW3: /* generic ipfw v.3 functions */
667 case IP_FW_ADD: /* ADD actually returns the body... */
669 case IP_FW_TABLE_GETSIZE:
670 case IP_FW_TABLE_LIST:
671 case IP_FW_NAT_GET_CONFIG:
672 case IP_FW_NAT_GET_LOG:
673 if (V_ip_fw_ctl_ptr != NULL)
674 error = V_ip_fw_ctl_ptr(sopt);
679 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
680 case IP_DUMMYNET_GET:
681 if (ip_dn_ctl_ptr != NULL)
682 error = ip_dn_ctl_ptr(sopt);
695 case MRT_API_SUPPORT:
697 case MRT_ADD_BW_UPCALL:
698 case MRT_DEL_BW_UPCALL:
699 error = priv_check(curthread, PRIV_NETINET_MROUTE);
702 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
707 error = ip_ctloutput(so, sopt);
713 switch (sopt->sopt_name) {
715 error = sooptcopyin(sopt, &optval, sizeof optval,
720 inp->inp_flags |= INP_HDRINCL;
722 inp->inp_flags &= ~INP_HDRINCL;
725 case IP_FW3: /* generic ipfw v.3 functions */
731 case IP_FW_TABLE_ADD:
732 case IP_FW_TABLE_DEL:
733 case IP_FW_TABLE_FLUSH:
736 if (V_ip_fw_ctl_ptr != NULL)
737 error = V_ip_fw_ctl_ptr(sopt);
742 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
743 case IP_DUMMYNET_CONFIGURE:
744 case IP_DUMMYNET_DEL:
745 case IP_DUMMYNET_FLUSH:
746 if (ip_dn_ctl_ptr != NULL)
747 error = ip_dn_ctl_ptr(sopt);
749 error = ENOPROTOOPT ;
753 error = priv_check(curthread, PRIV_NETINET_MROUTE);
756 error = ip_rsvp_init(so);
760 error = priv_check(curthread, PRIV_NETINET_MROUTE);
763 error = ip_rsvp_done();
767 case IP_RSVP_VIF_OFF:
768 error = priv_check(curthread, PRIV_NETINET_MROUTE);
771 error = ip_rsvp_vif ?
772 ip_rsvp_vif(so, sopt) : EINVAL;
783 case MRT_API_SUPPORT:
785 case MRT_ADD_BW_UPCALL:
786 case MRT_DEL_BW_UPCALL:
787 error = priv_check(curthread, PRIV_NETINET_MROUTE);
790 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
795 error = ip_ctloutput(so, sopt);
805 * This function exists solely to receive the PRC_IFDOWN messages which are
806 * sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, and calls
807 * in_ifadown() to remove all routes corresponding to that address. It also
808 * receives the PRC_IFUP messages from if_up() and reinstalls the interface
812 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
814 struct rm_priotracker in_ifa_tracker;
815 struct in_ifaddr *ia;
822 IN_IFADDR_RLOCK(&in_ifa_tracker);
823 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
824 if (ia->ia_ifa.ifa_addr == sa
825 && (ia->ia_flags & IFA_ROUTE)) {
826 ifa_ref(&ia->ia_ifa);
827 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
829 * in_scrubprefix() kills the interface route.
831 in_scrubprefix(ia, 0);
833 * in_ifadown gets rid of all the rest of the
834 * routes. This is not quite the right thing
835 * to do, but at least if we are running a
836 * routing process they will come back.
838 in_ifadown(&ia->ia_ifa, 0);
839 ifa_free(&ia->ia_ifa);
843 if (ia == NULL) /* If ia matched, already unlocked. */
844 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
848 IN_IFADDR_RLOCK(&in_ifa_tracker);
849 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
850 if (ia->ia_ifa.ifa_addr == sa)
853 if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
854 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
857 ifa_ref(&ia->ia_ifa);
858 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
860 ifp = ia->ia_ifa.ifa_ifp;
862 if ((ifp->if_flags & IFF_LOOPBACK)
863 || (ifp->if_flags & IFF_POINTOPOINT))
866 err = ifa_del_loopback_route((struct ifaddr *)ia, sa);
868 rt_addrmsg(RTM_ADD, &ia->ia_ifa, ia->ia_ifp->if_fib);
869 err = in_handle_ifaddr_route(RTM_ADD, ia);
871 ia->ia_flags |= IFA_ROUTE;
873 err = ifa_add_loopback_route((struct ifaddr *)ia, sa);
875 ifa_free(&ia->ia_ifa);
881 rip_attach(struct socket *so, int proto, struct thread *td)
887 KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
889 error = priv_check(td, PRIV_NETINET_RAW);
892 if (proto >= IPPROTO_MAX || proto < 0)
893 return EPROTONOSUPPORT;
894 error = soreserve(so, rip_sendspace, rip_recvspace);
897 INP_INFO_WLOCK(&V_ripcbinfo);
898 error = in_pcballoc(so, &V_ripcbinfo);
900 INP_INFO_WUNLOCK(&V_ripcbinfo);
903 inp = (struct inpcb *)so->so_pcb;
904 inp->inp_vflag |= INP_IPV4;
905 inp->inp_ip_p = proto;
906 inp->inp_ip_ttl = V_ip_defttl;
908 INP_INFO_WUNLOCK(&V_ripcbinfo);
914 rip_detach(struct socket *so)
919 KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
920 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
921 ("rip_detach: not closed"));
923 INP_INFO_WLOCK(&V_ripcbinfo);
926 if (so == V_ip_mrouter && ip_mrouter_done)
928 if (ip_rsvp_force_done)
929 ip_rsvp_force_done(so);
930 if (so == V_ip_rsvpd)
934 INP_INFO_WUNLOCK(&V_ripcbinfo);
938 rip_dodisconnect(struct socket *so, struct inpcb *inp)
940 struct inpcbinfo *pcbinfo;
942 pcbinfo = inp->inp_pcbinfo;
943 INP_INFO_WLOCK(pcbinfo);
946 inp->inp_faddr.s_addr = INADDR_ANY;
949 so->so_state &= ~SS_ISCONNECTED;
952 INP_INFO_WUNLOCK(pcbinfo);
956 rip_abort(struct socket *so)
961 KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
963 rip_dodisconnect(so, inp);
967 rip_close(struct socket *so)
972 KASSERT(inp != NULL, ("rip_close: inp == NULL"));
974 rip_dodisconnect(so, inp);
978 rip_disconnect(struct socket *so)
982 if ((so->so_state & SS_ISCONNECTED) == 0)
986 KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
988 rip_dodisconnect(so, inp);
993 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
995 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
999 if (nam->sa_len != sizeof(*addr))
1002 error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
1006 inp = sotoinpcb(so);
1007 KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
1009 if (CK_STAILQ_EMPTY(&V_ifnet) ||
1010 (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
1011 (addr->sin_addr.s_addr &&
1012 (inp->inp_flags & INP_BINDANY) == 0 &&
1013 ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
1014 return (EADDRNOTAVAIL);
1016 INP_INFO_WLOCK(&V_ripcbinfo);
1019 inp->inp_laddr = addr->sin_addr;
1022 INP_INFO_WUNLOCK(&V_ripcbinfo);
1027 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1029 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
1032 if (nam->sa_len != sizeof(*addr))
1034 if (CK_STAILQ_EMPTY(&V_ifnet))
1035 return (EADDRNOTAVAIL);
1036 if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
1037 return (EAFNOSUPPORT);
1039 inp = sotoinpcb(so);
1040 KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
1042 INP_INFO_WLOCK(&V_ripcbinfo);
1045 inp->inp_faddr = addr->sin_addr;
1049 INP_INFO_WUNLOCK(&V_ripcbinfo);
1054 rip_shutdown(struct socket *so)
1058 inp = sotoinpcb(so);
1059 KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
1068 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
1069 struct mbuf *control, struct thread *td)
1074 inp = sotoinpcb(so);
1075 KASSERT(inp != NULL, ("rip_send: inp == NULL"));
1078 * Note: 'dst' reads below are unlocked.
1080 if (so->so_state & SS_ISCONNECTED) {
1085 dst = inp->inp_faddr.s_addr; /* Unlocked read. */
1091 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
1093 return (rip_output(m, so, dst));
1098 rip_pcblist(SYSCTL_HANDLER_ARGS)
1101 struct epoch_tracker et;
1105 if (req->newptr != 0)
1108 if (req->oldptr == 0) {
1111 n = V_ripcbinfo.ipi_count;
1112 n += imax(n / 8, 10);
1113 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
1117 if ((error = sysctl_wire_old_buffer(req, 0)) != 0)
1120 bzero(&xig, sizeof(xig));
1121 xig.xig_len = sizeof xig;
1122 xig.xig_count = V_ripcbinfo.ipi_count;
1123 xig.xig_gen = V_ripcbinfo.ipi_gencnt;
1124 xig.xig_sogen = so_gencnt;
1125 error = SYSCTL_OUT(req, &xig, sizeof xig);
1129 NET_EPOCH_ENTER(et);
1130 for (inp = CK_LIST_FIRST(V_ripcbinfo.ipi_listhead);
1132 inp = CK_LIST_NEXT(inp, inp_list)) {
1134 if (inp->inp_gencnt <= xig.xig_gen &&
1135 cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
1138 in_pcbtoxinpcb(inp, &xi);
1140 error = SYSCTL_OUT(req, &xi, sizeof xi);
1150 * Give the user an updated idea of our state. If the
1151 * generation differs from what we told her before, she knows
1152 * that something happened while we were processing this
1153 * request, and it might be necessary to retry.
1155 xig.xig_gen = V_ripcbinfo.ipi_gencnt;
1156 xig.xig_sogen = so_gencnt;
1157 xig.xig_count = V_ripcbinfo.ipi_count;
1158 error = SYSCTL_OUT(req, &xig, sizeof xig);
1164 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist,
1165 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
1166 rip_pcblist, "S,xinpcb",
1167 "List of active raw IP sockets");
1170 struct pr_usrreqs rip_usrreqs = {
1171 .pru_abort = rip_abort,
1172 .pru_attach = rip_attach,
1173 .pru_bind = rip_bind,
1174 .pru_connect = rip_connect,
1175 .pru_control = in_control,
1176 .pru_detach = rip_detach,
1177 .pru_disconnect = rip_disconnect,
1178 .pru_peeraddr = in_getpeeraddr,
1179 .pru_send = rip_send,
1180 .pru_shutdown = rip_shutdown,
1181 .pru_sockaddr = in_getsockaddr,
1182 .pru_sosetlabel = in_pcbsosetlabel,
1183 .pru_close = rip_close,