45#include <sys/kernel.h>
46#include <sys/eventhandler.h>
48#include <sys/malloc.h>
52#include <sys/protosw.h>
53#include <sys/rwlock.h>
54#include <sys/signalvar.h>
55#include <sys/socket.h>
56#include <sys/socketvar.h>
58#include <sys/sysctl.h>
64#include <net/if_var.h>
66#include <net/route/route_ctl.h>
80#include <netipsec/ipsec_support.h>
82#include <machine/stdarg.h>
83#include <security/mac/mac_framework.h>
86SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_VNET | CTLFLAG_RW,
87 &VNET_NAME(ip_defttl), 0,
88 "Maximum TTL on IP packets");
91#define V_ripcbinfo VNET(ripcbinfo)
123int (*
ip_mforward)(
struct ip *,
struct ifnet *,
struct mbuf *,
124 struct ip_moptions *);
130int (*
ip_rsvp_vif)(
struct socket *,
struct sockopt *);
134extern struct protosw
inetsw[];
142 &
rip_recvspace, 0,
"Maximum space for incoming raw IP datagrams");
148#define INP_PCBHASH_RAW_SIZE 256
149#define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \
150 (((proto) + (laddr) + (faddr)) % (mask) + 1)
154rip_inshash(
struct inpcb *inp)
157 struct inpcbhead *pcbhash;
171 CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
175rip_delhash(
struct inpcb *inp)
181 CK_LIST_REMOVE(inp, inp_hash);
197rip_destroy(
void *unused __unused)
202VNET_SYSUNINIT(raw_ip, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, rip_destroy, NULL);
207rip_append(
struct inpcb *inp,
struct ip *
ip,
struct mbuf *m,
211 struct mbuf *n, *opts = NULL;
215#if defined(IPSEC) || defined(IPSEC_SUPPORT)
217 if (IPSEC_ENABLED(ipv4) && IPSEC_CHECK_POLICY(ipv4, m, inp) != 0)
221 if (mac_inpcb_check_deliver(inp, m) != 0)
228 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL)
232 (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
234 SOCKBUF_LOCK(&so->so_rcv);
235 if (sbappendaddr_locked(&so->so_rcv,
236 (
struct sockaddr *)ripsrc, n, opts) == 0) {
237 soroverflow_locked(so);
243 sorwakeup_locked(so);
248struct rip_inp_match_ctx {
254rip_inp_match1(
const struct inpcb *inp,
void *v)
256 struct rip_inp_match_ctx *ctx = v;
265 if (inp->inp_laddr.s_addr != ctx->ip->ip_dst.s_addr)
267 if (inp->inp_faddr.s_addr != ctx->ip->ip_src.s_addr)
273rip_inp_match2(
const struct inpcb *inp,
void *v)
275 struct rip_inp_match_ctx *ctx = v;
285 !
in_hosteq(inp->inp_laddr, ctx->ip->ip_dst))
288 !
in_hosteq(inp->inp_faddr, ctx->ip->ip_src))
298rip_input(
struct mbuf **mp,
int *offp,
int proto)
300 struct rip_inp_match_ctx ctx = {
301 .ip = mtod(*mp,
struct ip *),
307 struct mbuf *m = *mp;
315 bzero(&ripsrc,
sizeof(ripsrc));
316 ripsrc.sin_len =
sizeof(ripsrc);
317 ripsrc.sin_family = AF_INET;
318 ripsrc.sin_addr = ctx.ip->ip_src;
320 ifp = m->m_pkthdr.rcvif;
324 while ((inp =
inp_next(&inpi)) != NULL) {
326 if (jailed_without_vnet(inp->
inp_cred) &&
334 appended += rip_append(inp, ctx.ip, m, &ripsrc);
338 inpi.
match = rip_inp_match2;
339 MPASS(inpi.
inp == NULL);
340 while ((inp =
inp_next(&inpi)) != NULL) {
342 if (jailed_without_vnet(inp->
inp_cred) &&
343 !IN_MULTICAST(ntohl(ctx.ip->ip_dst.s_addr)) &&
358 IN_MULTICAST(ntohl(ctx.ip->ip_dst.s_addr))) {
374 if (proto != IPPROTO_IGMP) {
379 group.sin_family = AF_INET;
380 group.sin_addr = ctx.ip->ip_dst;
384 (
struct sockaddr *)&group,
385 (
struct sockaddr *)&ripsrc);
393 appended += rip_append(inp, ctx.ip, m, &ripsrc);
402 return (IPPROTO_DONE);
410rip_output(
struct mbuf *m,
struct socket *so, ...)
412 struct epoch_tracker et;
418 int flags = ((so->so_options & SO_DONTROUTE) ?
IP_ROUTETOIF : 0) |
421 u_char opttype, optlen, *cp;
424 dst = va_arg(ap, u_long);
436 M_PREPEND(m,
sizeof(
struct ip), M_NOWAIT);
441 ip = mtod(m,
struct ip *);
448 ip->
ip_len = htons(m->m_pkthdr.len);
449 ip->ip_src = inp->inp_laddr;
452 if (CALC_FLOWID_OUTBOUND) {
457 m->m_pkthdr.flowid = hash_val;
458 M_HASHTYPE_SET(m, hash_type);
489 if (m->m_pkthdr.len <
sizeof(*
ip)) {
493 m = m_pullup(m,
sizeof(*
ip));
496 ip = mtod(m,
struct ip *);
498 if (m->m_len < hlen) {
499 m = m_pullup(m, hlen);
502 ip = mtod(m,
struct ip *);
505 if (CALC_FLOWID_OUTBOUND) {
509 ip->ip_src, 0, 0,
ip->
ip_p, &hash_type);
510 m->m_pkthdr.flowid = hash_val;
511 M_HASHTYPE_SET(m, hash_type);
520 if ((hlen <
sizeof (*
ip))
522 || (ntohs(
ip->
ip_len) != m->m_pkthdr.len)) {
538 cp = (u_char *)(
ip + 1);
539 cnt = hlen -
sizeof (
struct ip);
540 for (; cnt > 0; cnt -= optlen, cp += optlen) {
579 mac_inpcb_create_mbuf(inp, m);
617 if ((sopt->sopt_level == SOL_SOCKET) &&
618 (sopt->sopt_name == SO_SETFIB)) {
626 switch (sopt->sopt_dir) {
628 switch (sopt->sopt_name) {
631 error = sooptcopyout(sopt, &optval,
sizeof optval);
637 case IP_FW_TABLE_GETSIZE:
638 case IP_FW_TABLE_LIST:
639 case IP_FW_NAT_GET_CONFIG:
640 case IP_FW_NAT_GET_LOG:
648 case IP_DUMMYNET_GET:
667 error = priv_check(curthread, PRIV_NETINET_MROUTE);
681 switch (sopt->sopt_name) {
683 error = sooptcopyin(sopt, &optval,
sizeof optval,
699 case IP_FW_TABLE_ADD:
700 case IP_FW_TABLE_DEL:
701 case IP_FW_TABLE_FLUSH:
711 case IP_DUMMYNET_CONFIGURE:
712 case IP_DUMMYNET_DEL:
713 case IP_DUMMYNET_FLUSH:
717 error = ENOPROTOOPT ;
721 error = priv_check(curthread, PRIV_NETINET_MROUTE);
728 error = priv_check(curthread, PRIV_NETINET_MROUTE);
735 case IP_RSVP_VIF_OFF:
736 error = priv_check(curthread, PRIV_NETINET_MROUTE);
755 error = priv_check(curthread, PRIV_NETINET_MROUTE);
790 if (ia->
ia_ifa.ifa_addr == sa
791 && (ia->ia_flags & IFA_ROUTE)) {
812 if (ia->
ia_ifa.ifa_addr == sa)
815 if (ia == NULL || (ia->ia_flags & IFA_ROUTE))
819 err = ifa_del_loopback_route((
struct ifaddr *)ia, sa);
821 rt_addrmsg(RTM_ADD, &ia->
ia_ifa, ia->ia_ifp->if_fib);
824 ia->ia_flags |= IFA_ROUTE;
826 err = ifa_add_loopback_route((
struct ifaddr *)ia, sa);
830#if defined(IPSEC) || defined(IPSEC_SUPPORT)
832 if (IPSEC_ENABLED(ipv4))
833 IPSEC_CTLINPUT(ipv4, cmd, sa, vip);
840rip_attach(
struct socket *so,
int proto,
struct thread *td)
846 KASSERT(inp == NULL, (
"rip_attach: inp != NULL"));
848 error = priv_check(td, PRIV_NETINET_RAW);
851 if (proto >= IPPROTO_MAX || proto < 0)
852 return EPROTONOSUPPORT;
859 inp = (
struct inpcb *)so->so_pcb;
871rip_detach(
struct socket *so)
876 KASSERT(inp != NULL, (
"rip_detach: inp == NULL"));
878 (
"rip_detach: not closed"));
898rip_dodisconnect(
struct socket *so,
struct inpcb *inp)
910 so->so_state &= ~SS_ISCONNECTED;
916rip_abort(
struct socket *so)
921 KASSERT(inp != NULL, (
"rip_abort: inp == NULL"));
923 rip_dodisconnect(so, inp);
927rip_close(
struct socket *so)
932 KASSERT(inp != NULL, (
"rip_close: inp == NULL"));
934 rip_dodisconnect(so, inp);
938rip_disconnect(
struct socket *so)
942 if ((so->so_state & SS_ISCONNECTED) == 0)
946 KASSERT(inp != NULL, (
"rip_disconnect: inp == NULL"));
948 rip_dodisconnect(so, inp);
953rip_bind(
struct socket *so,
struct sockaddr *nam,
struct thread *td)
959 if (nam->sa_family != AF_INET)
960 return (EAFNOSUPPORT);
961 if (nam->sa_len !=
sizeof(*addr))
969 KASSERT(inp != NULL, (
"rip_bind: inp == NULL"));
971 if (CK_STAILQ_EMPTY(&V_ifnet) ||
975 ifa_ifwithaddr_check((
struct sockaddr *)addr) == 0))
976 return (EADDRNOTAVAIL);
981 inp->inp_laddr = addr->sin_addr;
989rip_connect(
struct socket *so,
struct sockaddr *nam,
struct thread *td)
994 if (nam->sa_len !=
sizeof(*addr))
996 if (CK_STAILQ_EMPTY(&V_ifnet))
997 return (EADDRNOTAVAIL);
999 return (EAFNOSUPPORT);
1002 KASSERT(inp != NULL, (
"rip_connect: inp == NULL"));
1016rip_shutdown(
struct socket *so)
1021 KASSERT(inp != NULL, (
"rip_shutdown: inp == NULL"));
1030rip_send(
struct socket *so,
int flags,
struct mbuf *m,
struct sockaddr *nam,
1031 struct mbuf *control,
struct thread *td)
1038 KASSERT(inp != NULL, (
"rip_send: inp == NULL"));
1040 if (control != NULL) {
1048 if (so->so_state & SS_ISCONNECTED) {
1053 dst = inp->inp_faddr.s_addr;
1058 else if (nam->sa_family != AF_INET)
1059 error = EAFNOSUPPORT;
1060 else if (nam->sa_len !=
sizeof(
struct sockaddr_in))
1064 dst = ((
struct sockaddr_in *)nam)->sin_addr.s_addr;
1083 if (req->newptr != 0)
1086 if (req->oldptr == 0) {
1090 n += imax(n / 8, 10);
1091 req->oldidx = 2 * (
sizeof xig) + n *
sizeof(
struct xinpcb);
1095 if ((error = sysctl_wire_old_buffer(req, 0)) != 0)
1098 bzero(&xig,
sizeof(xig));
1099 xig.xig_len =
sizeof xig;
1102 xig.xig_sogen = so_gencnt;
1103 error = SYSCTL_OUT(req, &xig,
sizeof xig);
1107 while ((inp =
inp_next(&inpi)) != NULL) {
1113 error = SYSCTL_OUT(req, &xi,
sizeof xi);
1129 xig.xig_sogen = so_gencnt;
1131 error = SYSCTL_OUT(req, &xig,
sizeof xig);
1138 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
1140 "List of active raw IP sockets");
1144 .pru_abort = rip_abort,
1145 .pru_attach = rip_attach,
1146 .pru_bind = rip_bind,
1147 .pru_connect = rip_connect,
1149 .pru_detach = rip_detach,
1150 .pru_disconnect = rip_disconnect,
1152 .pru_send = rip_send,
1153 .pru_shutdown = rip_shutdown,
1156 .pru_close = rip_close,
int in_scrubprefix(struct in_ifaddr *target, u_int flags)
int in_handle_ifaddr_route(int cmd, struct in_ifaddr *ia)
int in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
uint32_t fib4_calc_software_hash(struct in_addr src, struct in_addr dst, unsigned short src_port, unsigned short dst_port, char proto, uint32_t *phashtype)
int prison_local_ip4(struct ucred *cred, struct in_addr *ia)
int prison_check_ip4(const struct ucred *cred, const struct in_addr *ia)
int imo_multi_filter(const struct ip_moptions *imo, const struct ifnet *ifp, const struct sockaddr *group, const struct sockaddr *src)
struct inpcb * inp_next(struct inpcb_iterator *ii)
int in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
void in_pcbinfo_destroy(struct inpcbinfo *pcbinfo)
void in_pcbtoxinpcb(const struct inpcb *inp, struct xinpcb *xi)
void in_pcbsosetlabel(struct socket *so)
void in_pcbdetach(struct inpcb *inp)
void in_pcbinfo_init(struct inpcbinfo *pcbinfo, struct inpcbstorage *pcbstor, u_int hash_nelements, u_int porthash_nelements)
void in_pcbfree(struct inpcb *inp)
#define INP_LOCK_ASSERT(inp)
#define INP_HASH_WLOCK_ASSERT(ipi)
#define INP_HASH_WUNLOCK(ipi)
#define INP_ALL_ITERATOR(_ipi, _lock)
int in_getsockaddr(struct socket *so, struct sockaddr **nam)
int in_pcbladdr(struct inpcb *, struct in_addr *, struct in_addr *, struct ucred *)
#define INP_WLOCK_ASSERT(inp)
int in_getpeeraddr(struct socket *so, struct sockaddr **nam)
#define INP_HASH_WLOCK(ipi)
#define INP_RLOCK_ASSERT(inp)
#define INP_ITERATOR(_ipi, _lock, _match, _ctx)
int cr_canseeinpcb(struct ucred *cred, struct inpcb *inp)
void in_ifadown(struct ifaddr *ifa, int delete)
VNET_SYSUNINIT(divert, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, div_destroy, NULL)
#define ICMP_UNREACH_PROTOCOL
void icmp_error(struct mbuf *, int, int, uint32_t, int)
void ip_fillid(struct ip *ip)
int(* mrt_ioctl)(u_long, caddr_t, int)
#define MRT_DEL_BW_UPCALL
int(* ip_mrouter_done)(void)
#define MRT_ADD_BW_UPCALL
int(* ip_mrouter_set)(struct socket *, struct sockopt *)
int(* ip_mrouter_get)(struct socket *, struct sockopt *)
int ip_ctloutput(struct socket *so, struct sockopt *sopt)
int ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags, struct ip_moptions *imo, struct inpcb *inp)
int rip_output(struct mbuf *, struct socket *,...)
int(* ip_fw_ctl_ptr_t)(struct sockopt *)
int(* ip_fw_chk_ptr_t)(struct ip_fw_args *args)
struct pr_usrreqs rip_usrreqs
int(* ip_rsvp_vif)(struct socket *, struct sockopt *)
int(* rsvp_input_p)(struct mbuf **, int *, int)
#define IP_NODEFAULTFLOWID
#define IP_ALLOWBROADCAST
int(* legal_vif_num)(int)
int(* ip_mforward)(struct ip *, struct ifnet *, struct mbuf *, struct ip_moptions *)
void(* ip_rsvp_force_done)(struct socket *)
u_long(* ip_mcast_src)(int)
void rip_ctlinput(int, struct sockaddr *, void *)
int rip_ctloutput(struct socket *, struct sockopt *)
int rip_input(struct mbuf **, int *, int)
VNET_SYSINIT(rip_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rip_init, NULL)
int(* ip_dn_io_ptr)(struct mbuf **, struct ip_fw_args *)
int(* ip_dn_ctl_ptr)(struct sockopt *)
void(* ip_divert_ptr)(struct mbuf *, bool)
#define INP_PCBHASH_RAW(proto, laddr, faddr, mask)
SYSCTL_PROC(_net_inet_raw, OID_AUTO, pcblist, CTLTYPE_OPAQUE|CTLFLAG_RD|CTLFLAG_MPSAFE, NULL, 0, rip_pcblist, "S,xinpcb", "List of active raw IP sockets")
static void rip_init(void *arg __unused)
#define INP_PCBHASH_RAW_SIZE
SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW, &rip_sendspace, 0, "Maximum outgoing raw IP datagram size")
INPCBSTORAGE_DEFINE(ripcbstor, "rawinp", "ripcb", "rip", "riphash")
SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_VNET|CTLFLAG_RW, &VNET_NAME(ip_defttl), 0, "Maximum TTL on IP packets")
static int rip_pcblist(SYSCTL_HANDLER_ARGS)
VNET_DEFINE(int, ip_defttl)
int(* ng_ipfw_input_p)(struct mbuf **, struct ip_fw_args *, bool)
struct socket * inp_socket
struct mbuf * inp_options
struct ip_moptions * inp_moptions
struct inpcbinfo * inp_pcbinfo
struct in_conninfo inp_inc
struct inpcbhead * ipi_hashbase
struct in_addr ip_src ip_dst