2005-11-18 20:12:40 +00:00
|
|
|
/*
|
2017-11-20 19:43:44 +00:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
2005-11-18 20:12:40 +00:00
|
|
|
* Copyright (c) 1982, 1986, 1988, 1993
|
2007-05-11 10:48:30 +00:00
|
|
|
* The Regents of the University of California.
|
|
|
|
* Copyright (c) 2005 Andre Oppermann, Internet Business Solutions AG.
|
|
|
|
* All rights reserved.
|
2005-11-18 20:12:40 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2017-02-28 23:42:47 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
2005-11-18 20:12:40 +00:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 20:44:24 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2005-11-18 20:12:40 +00:00
|
|
|
#include "opt_ipstealth.h"
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/domain.h>
|
|
|
|
#include <sys/protosw.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/syslog.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_types.h>
|
|
|
|
#include <net/if_var.h>
|
|
|
|
#include <net/if_dl.h>
|
|
|
|
#include <net/route.h>
|
2020-07-02 21:04:08 +00:00
|
|
|
#include <net/route/nhop.h>
|
2005-11-18 20:12:40 +00:00
|
|
|
#include <net/netisr.h>
|
2009-08-01 19:26:27 +00:00
|
|
|
#include <net/vnet.h>
|
2005-11-18 20:12:40 +00:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
Merge helper fib* functions used for basic lookups.
Vast majority of rtalloc(9) users require only basic info from
route table (e.g. "does the rtentry interface match with the interface
I have?". "what is the MTU?", "Give me the IPv4 source address to use",
etc..).
Instead of hand-rolling lookups, checking if rtentry is up, valid,
dealing with IPv6 mtu, finding "address" ifp (almost never done right),
provide easy-to-use API hiding all the complexity and returning the
needed info into small on-stack structure.
This change also helps hiding route subsystem internals (locking, direct
rtentry accesses).
Additionaly, using this API improves lookup performance since rtentry is not
locked.
(This is safe, since all the rtentry changes happens under both radix WLOCK
and rtentry WLOCK).
Sponsored by: Yandex LLC
2015-12-08 10:50:03 +00:00
|
|
|
#include <netinet/in_fib.h>
|
2005-11-18 20:12:40 +00:00
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/in_var.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/in_pcb.h>
|
|
|
|
#include <netinet/ip_var.h>
|
|
|
|
#include <netinet/ip_options.h>
|
|
|
|
#include <netinet/ip_icmp.h>
|
|
|
|
#include <machine/in_cksum.h>
|
|
|
|
|
|
|
|
#include <sys/socketvar.h>
|
2006-10-22 11:52:19 +00:00
|
|
|
|
2018-07-24 16:35:52 +00:00
|
|
|
VNET_DEFINE_STATIC(int, ip_dosourceroute);
|
2014-09-15 14:43:58 +00:00
|
|
|
SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute,
|
|
|
|
CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_dosourceroute), 0,
|
2014-09-15 07:20:40 +00:00
|
|
|
"Enable forwarding source routed IP packets");
|
|
|
|
#define V_ip_dosourceroute VNET(ip_dosourceroute)
|
2005-11-18 20:12:40 +00:00
|
|
|
|
2018-07-24 16:35:52 +00:00
|
|
|
VNET_DEFINE_STATIC(int, ip_acceptsourceroute);
|
2020-02-12 13:31:36 +00:00
|
|
|
SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute,
|
|
|
|
CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_acceptsourceroute), 0,
|
2005-11-18 20:12:40 +00:00
|
|
|
"Enable accepting source routed IP packets");
|
2014-09-15 07:20:40 +00:00
|
|
|
#define V_ip_acceptsourceroute VNET(ip_acceptsourceroute)
|
2005-11-18 20:12:40 +00:00
|
|
|
|
2014-09-15 07:20:40 +00:00
|
|
|
VNET_DEFINE(int, ip_doopts) = 1; /* 0 = ignore, 1 = process, 2 = reject */
|
2014-09-15 14:43:58 +00:00
|
|
|
SYSCTL_INT(_net_inet_ip, OID_AUTO, process_options, CTLFLAG_VNET | CTLFLAG_RW,
|
2014-09-15 07:20:40 +00:00
|
|
|
&VNET_NAME(ip_doopts), 0, "Enable IP options processing ([LS]SRR, RR, TS)");
|
2005-11-18 20:12:40 +00:00
|
|
|
|
|
|
|
static void save_rte(struct mbuf *m, u_char *, struct in_addr);
|
|
|
|
|
|
|
|
/*
|
2007-05-11 10:48:30 +00:00
|
|
|
* Do option processing on a datagram, possibly discarding it if bad options
|
|
|
|
* are encountered, or forwarding it if source-routed.
|
|
|
|
*
|
|
|
|
* The pass argument is used when operating in the IPSTEALTH mode to tell
|
|
|
|
* what options to process: [LS]SRR (pass 0) or the others (pass 1). The
|
|
|
|
* reason for as many as two passes is that when doing IPSTEALTH, non-routing
|
|
|
|
* options should be processed only if the packet is for us.
|
|
|
|
*
|
|
|
|
* Returns 1 if packet has been forwarded/freed, 0 if the packet should be
|
|
|
|
* processed further.
|
2005-11-18 20:12:40 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
ip_dooptions(struct mbuf *m, int pass)
|
|
|
|
{
|
|
|
|
struct ip *ip = mtod(m, struct ip *);
|
|
|
|
u_char *cp;
|
|
|
|
struct in_ifaddr *ia;
|
|
|
|
int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
|
|
|
|
struct in_addr *sin, dst;
|
2009-02-13 15:14:43 +00:00
|
|
|
uint32_t ntime;
|
2020-07-02 21:04:08 +00:00
|
|
|
struct nhop_object *nh;
|
2005-11-18 20:12:40 +00:00
|
|
|
struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
|
Widen NET_EPOCH coverage.
When epoch(9) was introduced to network stack, it was basically
dropped in place of existing locking, which was mutexes and
rwlocks. For the sake of performance mutex covered areas were
as small as possible, so became epoch covered areas.
However, epoch doesn't introduce any contention, it just delays
memory reclaim. So, there is no point to minimise epoch covered
areas in sense of performance. Meanwhile entering/exiting epoch
also has non-zero CPU usage, so doing this less often is a win.
Not the least is also code maintainability. In the new paradigm
we can assume that at any stage of processing a packet, we are
inside network epoch. This makes coding both input and output
path way easier.
On output path we already enter epoch quite early - in the
ip_output(), in the ip6_output().
This patch does the same for the input path. All ISR processing,
network related callouts, other ways of packet injection to the
network stack shall be performed in net_epoch. Any leaf function
that walks network configuration now asserts epoch.
Tricky part is configuration code paths - ioctls, sysctls. They
also call into leaf functions, so some need to be changed.
This patch would introduce more epoch recursions (see EPOCH_TRACE)
than we had before. They will be cleaned up separately, as several
of them aren't trivial. Note, that unlike a lock recursion the
epoch recursion is safe and just wastes a bit of resources.
Reviewed by: gallatin, hselasky, cy, adrian, kristof
Differential Revision: https://reviews.freebsd.org/D19111
2019-10-07 22:40:05 +00:00
|
|
|
|
|
|
|
NET_EPOCH_ASSERT();
|
2005-11-18 20:12:40 +00:00
|
|
|
|
2007-05-11 10:48:30 +00:00
|
|
|
/* Ignore or reject packets with IP options. */
|
2014-09-15 07:20:40 +00:00
|
|
|
if (V_ip_doopts == 0)
|
2005-11-18 20:12:40 +00:00
|
|
|
return 0;
|
2014-09-15 07:20:40 +00:00
|
|
|
else if (V_ip_doopts == 2) {
|
2005-11-18 20:12:40 +00:00
|
|
|
type = ICMP_UNREACH;
|
|
|
|
code = ICMP_UNREACH_FILTER_PROHIB;
|
Widen NET_EPOCH coverage.
When epoch(9) was introduced to network stack, it was basically
dropped in place of existing locking, which was mutexes and
rwlocks. For the sake of performance mutex covered areas were
as small as possible, so became epoch covered areas.
However, epoch doesn't introduce any contention, it just delays
memory reclaim. So, there is no point to minimise epoch covered
areas in sense of performance. Meanwhile entering/exiting epoch
also has non-zero CPU usage, so doing this less often is a win.
Not the least is also code maintainability. In the new paradigm
we can assume that at any stage of processing a packet, we are
inside network epoch. This makes coding both input and output
path way easier.
On output path we already enter epoch quite early - in the
ip_output(), in the ip6_output().
This patch does the same for the input path. All ISR processing,
network related callouts, other ways of packet injection to the
network stack shall be performed in net_epoch. Any leaf function
that walks network configuration now asserts epoch.
Tricky part is configuration code paths - ioctls, sysctls. They
also call into leaf functions, so some need to be changed.
This patch would introduce more epoch recursions (see EPOCH_TRACE)
than we had before. They will be cleaned up separately, as several
of them aren't trivial. Note, that unlike a lock recursion the
epoch recursion is safe and just wastes a bit of resources.
Reviewed by: gallatin, hselasky, cy, adrian, kristof
Differential Revision: https://reviews.freebsd.org/D19111
2019-10-07 22:40:05 +00:00
|
|
|
goto bad;
|
2005-11-18 20:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dst = ip->ip_dst;
|
|
|
|
cp = (u_char *)(ip + 1);
|
|
|
|
cnt = (ip->ip_hl << 2) - sizeof (struct ip);
|
|
|
|
for (; cnt > 0; cnt -= optlen, cp += optlen) {
|
|
|
|
opt = cp[IPOPT_OPTVAL];
|
|
|
|
if (opt == IPOPT_EOL)
|
|
|
|
break;
|
|
|
|
if (opt == IPOPT_NOP)
|
|
|
|
optlen = 1;
|
|
|
|
else {
|
|
|
|
if (cnt < IPOPT_OLEN + sizeof(*cp)) {
|
|
|
|
code = &cp[IPOPT_OLEN] - (u_char *)ip;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
optlen = cp[IPOPT_OLEN];
|
|
|
|
if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
|
|
|
|
code = &cp[IPOPT_OLEN] - (u_char *)ip;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch (opt) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
2007-05-11 10:48:30 +00:00
|
|
|
* Source routing with record. Find interface with current
|
|
|
|
* destination address. If none on this machine then drop if
|
|
|
|
* strictly routed, or do nothing if loosely routed. Record
|
|
|
|
* interface address and bring up next address component. If
|
|
|
|
* strictly routed make sure next address is on directly
|
|
|
|
* accessible net.
|
2005-11-18 20:12:40 +00:00
|
|
|
*/
|
|
|
|
case IPOPT_LSRR:
|
|
|
|
case IPOPT_SSRR:
|
|
|
|
#ifdef IPSTEALTH
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
if (V_ipstealth && pass > 0)
|
2005-11-18 20:12:40 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
|
|
|
|
code = &cp[IPOPT_OLEN] - (u_char *)ip;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
|
|
|
|
code = &cp[IPOPT_OFFSET] - (u_char *)ip;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
ipaddr.sin_addr = ip->ip_dst;
|
2009-06-23 20:19:09 +00:00
|
|
|
if (ifa_ifwithaddr_check((struct sockaddr *)&ipaddr)
|
|
|
|
== 0) {
|
2005-11-18 20:12:40 +00:00
|
|
|
if (opt == IPOPT_SSRR) {
|
|
|
|
type = ICMP_UNREACH;
|
|
|
|
code = ICMP_UNREACH_SRCFAIL;
|
|
|
|
goto bad;
|
|
|
|
}
|
2014-09-15 07:20:40 +00:00
|
|
|
if (!V_ip_dosourceroute)
|
2005-11-18 20:12:40 +00:00
|
|
|
goto nosourcerouting;
|
|
|
|
/*
|
|
|
|
* Loose routing, and not at next destination
|
|
|
|
* yet; nothing to do except forward.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
off--; /* 0 origin */
|
|
|
|
if (off > optlen - (int)sizeof(struct in_addr)) {
|
|
|
|
/*
|
|
|
|
* End of source route. Should be for us.
|
|
|
|
*/
|
2014-09-15 07:20:40 +00:00
|
|
|
if (!V_ip_acceptsourceroute)
|
2005-11-18 20:12:40 +00:00
|
|
|
goto nosourcerouting;
|
|
|
|
save_rte(m, cp, ip->ip_src);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#ifdef IPSTEALTH
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
if (V_ipstealth)
|
2005-11-18 20:12:40 +00:00
|
|
|
goto dropit;
|
|
|
|
#endif
|
2014-09-15 07:20:40 +00:00
|
|
|
if (!V_ip_dosourceroute) {
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
if (V_ipforwarding) {
|
2017-02-16 20:47:41 +00:00
|
|
|
char srcbuf[INET_ADDRSTRLEN];
|
|
|
|
char dstbuf[INET_ADDRSTRLEN];
|
|
|
|
|
2005-11-18 20:12:40 +00:00
|
|
|
/*
|
2007-05-11 10:48:30 +00:00
|
|
|
* Acting as a router, so generate
|
|
|
|
* ICMP
|
2005-11-18 20:12:40 +00:00
|
|
|
*/
|
|
|
|
nosourcerouting:
|
2020-02-12 13:31:36 +00:00
|
|
|
log(LOG_WARNING,
|
2017-02-16 20:47:41 +00:00
|
|
|
"attempted source route from %s "
|
|
|
|
"to %s\n",
|
|
|
|
inet_ntoa_r(ip->ip_src, srcbuf),
|
|
|
|
inet_ntoa_r(ip->ip_dst, dstbuf));
|
2005-11-18 20:12:40 +00:00
|
|
|
type = ICMP_UNREACH;
|
|
|
|
code = ICMP_UNREACH_SRCFAIL;
|
|
|
|
goto bad;
|
|
|
|
} else {
|
|
|
|
/*
|
2007-05-11 10:48:30 +00:00
|
|
|
* Not acting as a router, so
|
|
|
|
* silently drop.
|
2005-11-18 20:12:40 +00:00
|
|
|
*/
|
|
|
|
#ifdef IPSTEALTH
|
|
|
|
dropit:
|
|
|
|
#endif
|
2009-04-11 23:35:20 +00:00
|
|
|
IPSTAT_INC(ips_cantforward);
|
2005-11-18 20:12:40 +00:00
|
|
|
m_freem(m);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* locate outgoing interface
|
|
|
|
*/
|
|
|
|
(void)memcpy(&ipaddr.sin_addr, cp + off,
|
|
|
|
sizeof(ipaddr.sin_addr));
|
|
|
|
|
Merge helper fib* functions used for basic lookups.
Vast majority of rtalloc(9) users require only basic info from
route table (e.g. "does the rtentry interface match with the interface
I have?". "what is the MTU?", "Give me the IPv4 source address to use",
etc..).
Instead of hand-rolling lookups, checking if rtentry is up, valid,
dealing with IPv6 mtu, finding "address" ifp (almost never done right),
provide easy-to-use API hiding all the complexity and returning the
needed info into small on-stack structure.
This change also helps hiding route subsystem internals (locking, direct
rtentry accesses).
Additionaly, using this API improves lookup performance since rtentry is not
locked.
(This is safe, since all the rtentry changes happens under both radix WLOCK
and rtentry WLOCK).
Sponsored by: Yandex LLC
2015-12-08 10:50:03 +00:00
|
|
|
type = ICMP_UNREACH;
|
|
|
|
code = ICMP_UNREACH_SRCFAIL;
|
|
|
|
|
2005-11-18 20:12:40 +00:00
|
|
|
if (opt == IPOPT_SSRR) {
|
|
|
|
#define INA struct in_ifaddr *
|
|
|
|
#define SA struct sockaddr *
|
2014-09-11 20:21:03 +00:00
|
|
|
ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr,
|
|
|
|
RT_ALL_FIBS);
|
|
|
|
if (ia == NULL)
|
|
|
|
ia = (INA)ifa_ifwithnet((SA)&ipaddr, 0,
|
|
|
|
RT_ALL_FIBS);
|
Merge helper fib* functions used for basic lookups.
Vast majority of rtalloc(9) users require only basic info from
route table (e.g. "does the rtentry interface match with the interface
I have?". "what is the MTU?", "Give me the IPv4 source address to use",
etc..).
Instead of hand-rolling lookups, checking if rtentry is up, valid,
dealing with IPv6 mtu, finding "address" ifp (almost never done right),
provide easy-to-use API hiding all the complexity and returning the
needed info into small on-stack structure.
This change also helps hiding route subsystem internals (locking, direct
rtentry accesses).
Additionaly, using this API improves lookup performance since rtentry is not
locked.
(This is safe, since all the rtentry changes happens under both radix WLOCK
and rtentry WLOCK).
Sponsored by: Yandex LLC
2015-12-08 10:50:03 +00:00
|
|
|
if (ia == NULL)
|
|
|
|
goto bad;
|
|
|
|
|
|
|
|
memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
|
|
|
|
sizeof(struct in_addr));
|
|
|
|
} else {
|
|
|
|
/* XXX MRT 0 for routing */
|
2020-07-02 21:04:08 +00:00
|
|
|
nh = fib4_lookup(M_GETFIB(m), ipaddr.sin_addr,
|
|
|
|
0, NHR_NONE, 0);
|
|
|
|
if (nh == NULL)
|
Merge helper fib* functions used for basic lookups.
Vast majority of rtalloc(9) users require only basic info from
route table (e.g. "does the rtentry interface match with the interface
I have?". "what is the MTU?", "Give me the IPv4 source address to use",
etc..).
Instead of hand-rolling lookups, checking if rtentry is up, valid,
dealing with IPv6 mtu, finding "address" ifp (almost never done right),
provide easy-to-use API hiding all the complexity and returning the
needed info into small on-stack structure.
This change also helps hiding route subsystem internals (locking, direct
rtentry accesses).
Additionaly, using this API improves lookup performance since rtentry is not
locked.
(This is safe, since all the rtentry changes happens under both radix WLOCK
and rtentry WLOCK).
Sponsored by: Yandex LLC
2015-12-08 10:50:03 +00:00
|
|
|
goto bad;
|
|
|
|
|
2020-07-02 21:04:08 +00:00
|
|
|
memcpy(cp + off, &(IA_SIN(nh->nh_ifa)->sin_addr),
|
Merge helper fib* functions used for basic lookups.
Vast majority of rtalloc(9) users require only basic info from
route table (e.g. "does the rtentry interface match with the interface
I have?". "what is the MTU?", "Give me the IPv4 source address to use",
etc..).
Instead of hand-rolling lookups, checking if rtentry is up, valid,
dealing with IPv6 mtu, finding "address" ifp (almost never done right),
provide easy-to-use API hiding all the complexity and returning the
needed info into small on-stack structure.
This change also helps hiding route subsystem internals (locking, direct
rtentry accesses).
Additionaly, using this API improves lookup performance since rtentry is not
locked.
(This is safe, since all the rtentry changes happens under both radix WLOCK
and rtentry WLOCK).
Sponsored by: Yandex LLC
2015-12-08 10:50:03 +00:00
|
|
|
sizeof(struct in_addr));
|
2005-11-18 20:12:40 +00:00
|
|
|
}
|
Merge helper fib* functions used for basic lookups.
Vast majority of rtalloc(9) users require only basic info from
route table (e.g. "does the rtentry interface match with the interface
I have?". "what is the MTU?", "Give me the IPv4 source address to use",
etc..).
Instead of hand-rolling lookups, checking if rtentry is up, valid,
dealing with IPv6 mtu, finding "address" ifp (almost never done right),
provide easy-to-use API hiding all the complexity and returning the
needed info into small on-stack structure.
This change also helps hiding route subsystem internals (locking, direct
rtentry accesses).
Additionaly, using this API improves lookup performance since rtentry is not
locked.
(This is safe, since all the rtentry changes happens under both radix WLOCK
and rtentry WLOCK).
Sponsored by: Yandex LLC
2015-12-08 10:50:03 +00:00
|
|
|
|
2005-11-18 20:12:40 +00:00
|
|
|
ip->ip_dst = ipaddr.sin_addr;
|
|
|
|
cp[IPOPT_OFFSET] += sizeof(struct in_addr);
|
|
|
|
/*
|
|
|
|
* Let ip_intr's mcast routing check handle mcast pkts
|
|
|
|
*/
|
|
|
|
forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IPOPT_RR:
|
|
|
|
#ifdef IPSTEALTH
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
if (V_ipstealth && pass == 0)
|
2005-11-18 20:12:40 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
|
|
|
|
code = &cp[IPOPT_OFFSET] - (u_char *)ip;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
|
|
|
|
code = &cp[IPOPT_OFFSET] - (u_char *)ip;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If no space remains, ignore.
|
|
|
|
*/
|
|
|
|
off--; /* 0 origin */
|
|
|
|
if (off > optlen - (int)sizeof(struct in_addr))
|
|
|
|
break;
|
|
|
|
(void)memcpy(&ipaddr.sin_addr, &ip->ip_dst,
|
|
|
|
sizeof(ipaddr.sin_addr));
|
|
|
|
/*
|
2007-05-11 10:48:30 +00:00
|
|
|
* Locate outgoing interface; if we're the
|
|
|
|
* destination, use the incoming interface (should be
|
|
|
|
* same).
|
2005-11-18 20:12:40 +00:00
|
|
|
*/
|
2015-12-09 11:14:27 +00:00
|
|
|
if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) != NULL) {
|
|
|
|
memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
|
|
|
|
sizeof(struct in_addr));
|
2020-07-02 21:04:08 +00:00
|
|
|
} else if ((nh = fib4_lookup(M_GETFIB(m),
|
|
|
|
ipaddr.sin_addr, 0, NHR_NONE, 0)) != NULL) {
|
|
|
|
memcpy(cp + off, &(IA_SIN(nh->nh_ifa)->sin_addr),
|
2015-12-09 11:14:27 +00:00
|
|
|
sizeof(struct in_addr));
|
|
|
|
} else {
|
2005-11-18 20:12:40 +00:00
|
|
|
type = ICMP_UNREACH;
|
|
|
|
code = ICMP_UNREACH_HOST;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
cp[IPOPT_OFFSET] += sizeof(struct in_addr);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IPOPT_TS:
|
|
|
|
#ifdef IPSTEALTH
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
if (V_ipstealth && pass == 0)
|
2005-11-18 20:12:40 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
code = cp - (u_char *)ip;
|
|
|
|
if (optlen < 4 || optlen > 40) {
|
|
|
|
code = &cp[IPOPT_OLEN] - (u_char *)ip;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
if ((off = cp[IPOPT_OFFSET]) < 5) {
|
|
|
|
code = &cp[IPOPT_OLEN] - (u_char *)ip;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
if (off > optlen - (int)sizeof(int32_t)) {
|
|
|
|
cp[IPOPT_OFFSET + 1] += (1 << 4);
|
|
|
|
if ((cp[IPOPT_OFFSET + 1] & 0xf0) == 0) {
|
|
|
|
code = &cp[IPOPT_OFFSET] - (u_char *)ip;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
off--; /* 0 origin */
|
|
|
|
sin = (struct in_addr *)(cp + off);
|
|
|
|
switch (cp[IPOPT_OFFSET + 1] & 0x0f) {
|
|
|
|
case IPOPT_TS_TSONLY:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IPOPT_TS_TSANDADDR:
|
2009-02-13 15:14:43 +00:00
|
|
|
if (off + sizeof(uint32_t) +
|
2005-11-18 20:12:40 +00:00
|
|
|
sizeof(struct in_addr) > optlen) {
|
|
|
|
code = &cp[IPOPT_OFFSET] - (u_char *)ip;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
ipaddr.sin_addr = dst;
|
|
|
|
ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
|
|
|
|
m->m_pkthdr.rcvif);
|
|
|
|
if (ia == NULL)
|
|
|
|
continue;
|
|
|
|
(void)memcpy(sin, &IA_SIN(ia)->sin_addr,
|
|
|
|
sizeof(struct in_addr));
|
|
|
|
cp[IPOPT_OFFSET] += sizeof(struct in_addr);
|
|
|
|
off += sizeof(struct in_addr);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IPOPT_TS_PRESPEC:
|
2009-02-13 15:14:43 +00:00
|
|
|
if (off + sizeof(uint32_t) +
|
2005-11-18 20:12:40 +00:00
|
|
|
sizeof(struct in_addr) > optlen) {
|
|
|
|
code = &cp[IPOPT_OFFSET] - (u_char *)ip;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
(void)memcpy(&ipaddr.sin_addr, sin,
|
|
|
|
sizeof(struct in_addr));
|
2010-10-14 12:32:49 +00:00
|
|
|
if (ifa_ifwithaddr_check((SA)&ipaddr) == 0)
|
2005-11-18 20:12:40 +00:00
|
|
|
continue;
|
|
|
|
cp[IPOPT_OFFSET] += sizeof(struct in_addr);
|
|
|
|
off += sizeof(struct in_addr);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
code = &cp[IPOPT_OFFSET + 1] - (u_char *)ip;
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
ntime = iptime();
|
2009-02-13 15:14:43 +00:00
|
|
|
(void)memcpy(cp + off, &ntime, sizeof(uint32_t));
|
|
|
|
cp[IPOPT_OFFSET] += sizeof(uint32_t);
|
2005-11-18 20:12:40 +00:00
|
|
|
}
|
|
|
|
}
|
Commit step 1 of the vimage project, (network stack)
virtualization work done by Marko Zec (zec@).
This is the first in a series of commits over the course
of the next few weeks.
Mark all uses of global variables to be virtualized
with a V_ prefix.
Use macros to map them back to their global names for
now, so this is a NOP change only.
We hope to have caught at least 85-90% of what is needed
so we do not invalidate a lot of outstanding patches again.
Obtained from: //depot/projects/vimage-commit2/...
Reviewed by: brooks, des, ed, mav, julian,
jamie, kris, rwatson, zec, ...
(various people I forgot, different versions)
md5 (with a bit of help)
Sponsored by: NLnet Foundation, The FreeBSD Foundation
X-MFC after: never
V_Commit_Message_Reviewed_By: more people than the patch
2008-08-17 23:27:27 +00:00
|
|
|
if (forward && V_ipforwarding) {
|
2005-11-18 20:12:40 +00:00
|
|
|
ip_forward(m, 1);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
bad:
|
|
|
|
icmp_error(m, type, code, 0, 0);
|
2009-04-11 23:35:20 +00:00
|
|
|
IPSTAT_INC(ips_badoptions);
|
2005-11-18 20:12:40 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-05-11 10:48:30 +00:00
|
|
|
* Save incoming source route for use in replies, to be picked up later by
|
|
|
|
* ip_srcroute if the receiver is interested.
|
2005-11-18 20:12:40 +00:00
|
|
|
*/
|
|
|
|
static void
|
2007-05-10 15:58:48 +00:00
|
|
|
save_rte(struct mbuf *m, u_char *option, struct in_addr dst)
|
2005-11-18 20:12:40 +00:00
|
|
|
{
|
|
|
|
unsigned olen;
|
|
|
|
struct ipopt_tag *opts;
|
|
|
|
|
|
|
|
opts = (struct ipopt_tag *)m_tag_get(PACKET_TAG_IPOPTIONS,
|
2007-05-11 10:48:30 +00:00
|
|
|
sizeof(struct ipopt_tag), M_NOWAIT);
|
2005-11-18 20:12:40 +00:00
|
|
|
if (opts == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
olen = option[IPOPT_OLEN];
|
|
|
|
if (olen > sizeof(opts->ip_srcrt) - (1 + sizeof(dst))) {
|
|
|
|
m_tag_free((struct m_tag *)opts);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bcopy(option, opts->ip_srcrt.srcopt, olen);
|
|
|
|
opts->ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
|
|
|
|
opts->ip_srcrt.dst = dst;
|
|
|
|
m_tag_prepend(m, (struct m_tag *)opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-05-11 10:48:30 +00:00
|
|
|
* Retrieve incoming source route for use in replies, in the same form used
|
|
|
|
* by setsockopt. The first hop is placed before the options, will be
|
|
|
|
* removed later.
|
2005-11-18 20:12:40 +00:00
|
|
|
*/
|
|
|
|
struct mbuf *
|
2007-05-10 15:58:48 +00:00
|
|
|
ip_srcroute(struct mbuf *m0)
|
2005-11-18 20:12:40 +00:00
|
|
|
{
|
2007-05-11 10:48:30 +00:00
|
|
|
struct in_addr *p, *q;
|
|
|
|
struct mbuf *m;
|
2005-11-18 20:12:40 +00:00
|
|
|
struct ipopt_tag *opts;
|
|
|
|
|
|
|
|
opts = (struct ipopt_tag *)m_tag_find(m0, PACKET_TAG_IPOPTIONS, NULL);
|
|
|
|
if (opts == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
if (opts->ip_nhops == 0)
|
|
|
|
return (NULL);
|
2012-12-05 08:04:20 +00:00
|
|
|
m = m_get(M_NOWAIT, MT_DATA);
|
2005-11-18 20:12:40 +00:00
|
|
|
if (m == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
#define OPTSIZ (sizeof(opts->ip_srcrt.nop) + sizeof(opts->ip_srcrt.srcopt))
|
|
|
|
|
|
|
|
/* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
|
|
|
|
m->m_len = opts->ip_nhops * sizeof(struct in_addr) +
|
|
|
|
sizeof(struct in_addr) + OPTSIZ;
|
|
|
|
|
|
|
|
/*
|
2007-05-11 10:48:30 +00:00
|
|
|
* First, save first hop for return route.
|
2005-11-18 20:12:40 +00:00
|
|
|
*/
|
|
|
|
p = &(opts->ip_srcrt.route[opts->ip_nhops - 1]);
|
|
|
|
*(mtod(m, struct in_addr *)) = *p--;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy option fields and padding (nop) to mbuf.
|
|
|
|
*/
|
|
|
|
opts->ip_srcrt.nop = IPOPT_NOP;
|
|
|
|
opts->ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
|
|
|
|
(void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr),
|
|
|
|
&(opts->ip_srcrt.nop), OPTSIZ);
|
|
|
|
q = (struct in_addr *)(mtod(m, caddr_t) +
|
|
|
|
sizeof(struct in_addr) + OPTSIZ);
|
|
|
|
#undef OPTSIZ
|
|
|
|
/*
|
2007-05-11 10:48:30 +00:00
|
|
|
* Record return path as an IP source route, reversing the path
|
|
|
|
* (pointers are now aligned).
|
2005-11-18 20:12:40 +00:00
|
|
|
*/
|
|
|
|
while (p >= opts->ip_srcrt.route) {
|
|
|
|
*q++ = *p--;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Last hop goes to final destination.
|
|
|
|
*/
|
|
|
|
*q = opts->ip_srcrt.dst;
|
|
|
|
m_tag_delete(m0, (struct m_tag *)opts);
|
|
|
|
return (m);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-10-12 09:24:24 +00:00
|
|
|
* Strip out IP options, at higher level protocol in the kernel.
|
2005-11-18 20:12:40 +00:00
|
|
|
*/
|
|
|
|
void
|
2012-10-12 09:24:24 +00:00
|
|
|
ip_stripoptions(struct mbuf *m)
|
2005-11-18 20:12:40 +00:00
|
|
|
{
|
|
|
|
struct ip *ip = mtod(m, struct ip *);
|
|
|
|
int olen;
|
|
|
|
|
2012-10-23 10:29:31 +00:00
|
|
|
olen = (ip->ip_hl << 2) - sizeof(struct ip);
|
2005-11-18 20:12:40 +00:00
|
|
|
m->m_len -= olen;
|
|
|
|
if (m->m_flags & M_PKTHDR)
|
|
|
|
m->m_pkthdr.len -= olen;
|
2012-10-23 08:33:13 +00:00
|
|
|
ip->ip_len = htons(ntohs(ip->ip_len) - olen);
|
2005-11-18 20:12:40 +00:00
|
|
|
ip->ip_hl = sizeof(struct ip) >> 2;
|
2012-10-23 10:29:31 +00:00
|
|
|
|
|
|
|
bcopy((char *)ip + sizeof(struct ip) + olen, (ip + 1),
|
|
|
|
(size_t )(m->m_len - sizeof(struct ip)));
|
2005-11-18 20:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-05-11 10:48:30 +00:00
|
|
|
* Insert IP options into preformed packet. Adjust IP destination as
|
|
|
|
* required for IP source routing, as indicated by a non-zero in_addr at the
|
|
|
|
* start of the options.
|
2005-11-18 20:12:40 +00:00
|
|
|
*
|
|
|
|
* XXX This routine assumes that the packet has no options in place.
|
|
|
|
*/
|
|
|
|
struct mbuf *
|
2007-05-10 15:58:48 +00:00
|
|
|
ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen)
|
2005-11-18 20:12:40 +00:00
|
|
|
{
|
2007-05-11 10:48:30 +00:00
|
|
|
struct ipoption *p = mtod(opt, struct ipoption *);
|
2005-11-18 20:12:40 +00:00
|
|
|
struct mbuf *n;
|
2007-05-11 10:48:30 +00:00
|
|
|
struct ip *ip = mtod(m, struct ip *);
|
2005-11-18 20:12:40 +00:00
|
|
|
unsigned optlen;
|
|
|
|
|
|
|
|
optlen = opt->m_len - sizeof(p->ipopt_dst);
|
2012-10-22 21:09:03 +00:00
|
|
|
if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET) {
|
2005-11-18 20:12:40 +00:00
|
|
|
*phlen = 0;
|
|
|
|
return (m); /* XXX should fail */
|
|
|
|
}
|
|
|
|
if (p->ipopt_dst.s_addr)
|
|
|
|
ip->ip_dst = p->ipopt_dst;
|
2015-01-06 14:32:28 +00:00
|
|
|
if (!M_WRITABLE(m) || M_LEADINGSPACE(m) < optlen) {
|
2013-03-15 12:55:30 +00:00
|
|
|
n = m_gethdr(M_NOWAIT, MT_DATA);
|
2005-11-18 20:12:40 +00:00
|
|
|
if (n == NULL) {
|
|
|
|
*phlen = 0;
|
|
|
|
return (m);
|
|
|
|
}
|
2013-03-16 08:58:28 +00:00
|
|
|
m_move_pkthdr(n, m);
|
2005-11-18 20:12:40 +00:00
|
|
|
n->m_pkthdr.rcvif = NULL;
|
|
|
|
n->m_pkthdr.len += optlen;
|
|
|
|
m->m_len -= sizeof(struct ip);
|
|
|
|
m->m_data += sizeof(struct ip);
|
|
|
|
n->m_next = m;
|
|
|
|
m = n;
|
|
|
|
m->m_len = optlen + sizeof(struct ip);
|
|
|
|
m->m_data += max_linkhdr;
|
|
|
|
bcopy(ip, mtod(m, void *), sizeof(struct ip));
|
|
|
|
} else {
|
|
|
|
m->m_data -= optlen;
|
|
|
|
m->m_len += optlen;
|
|
|
|
m->m_pkthdr.len += optlen;
|
|
|
|
bcopy(ip, mtod(m, void *), sizeof(struct ip));
|
|
|
|
}
|
|
|
|
ip = mtod(m, struct ip *);
|
|
|
|
bcopy(p->ipopt_list, ip + 1, optlen);
|
|
|
|
*phlen = sizeof(struct ip) + optlen;
|
|
|
|
ip->ip_v = IPVERSION;
|
|
|
|
ip->ip_hl = *phlen >> 2;
|
2012-10-22 21:09:03 +00:00
|
|
|
ip->ip_len = htons(ntohs(ip->ip_len) + optlen);
|
2005-11-18 20:12:40 +00:00
|
|
|
return (m);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-05-11 10:48:30 +00:00
|
|
|
* Copy options from ip to jp, omitting those not copied during
|
|
|
|
* fragmentation.
|
2005-11-18 20:12:40 +00:00
|
|
|
*/
|
|
|
|
int
|
2007-05-10 15:58:48 +00:00
|
|
|
ip_optcopy(struct ip *ip, struct ip *jp)
|
2005-11-18 20:12:40 +00:00
|
|
|
{
|
2007-05-11 10:48:30 +00:00
|
|
|
u_char *cp, *dp;
|
2005-11-18 20:12:40 +00:00
|
|
|
int opt, optlen, cnt;
|
|
|
|
|
|
|
|
cp = (u_char *)(ip + 1);
|
|
|
|
dp = (u_char *)(jp + 1);
|
|
|
|
cnt = (ip->ip_hl << 2) - sizeof (struct ip);
|
|
|
|
for (; cnt > 0; cnt -= optlen, cp += optlen) {
|
|
|
|
opt = cp[0];
|
|
|
|
if (opt == IPOPT_EOL)
|
|
|
|
break;
|
|
|
|
if (opt == IPOPT_NOP) {
|
|
|
|
/* Preserve for IP mcast tunnel's LSRR alignment. */
|
|
|
|
*dp++ = IPOPT_NOP;
|
|
|
|
optlen = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp),
|
|
|
|
("ip_optcopy: malformed ipv4 option"));
|
|
|
|
optlen = cp[IPOPT_OLEN];
|
|
|
|
KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt,
|
|
|
|
("ip_optcopy: malformed ipv4 option"));
|
|
|
|
|
2007-05-11 10:48:30 +00:00
|
|
|
/* Bogus lengths should have been caught by ip_dooptions. */
|
2005-11-18 20:12:40 +00:00
|
|
|
if (optlen > cnt)
|
|
|
|
optlen = cnt;
|
|
|
|
if (IPOPT_COPIED(opt)) {
|
|
|
|
bcopy(cp, dp, optlen);
|
|
|
|
dp += optlen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
|
|
|
|
*dp++ = IPOPT_EOL;
|
|
|
|
return (optlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-05-11 10:48:30 +00:00
|
|
|
* Set up IP options in pcb for insertion in output packets. Store in mbuf
|
|
|
|
* with pointer in pcbopt, adding pseudo-option with destination address if
|
|
|
|
* source routed.
|
2005-11-18 20:12:40 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
ip_pcbopts(struct inpcb *inp, int optname, struct mbuf *m)
|
|
|
|
{
|
2007-05-11 10:48:30 +00:00
|
|
|
int cnt, optlen;
|
|
|
|
u_char *cp;
|
2005-11-18 20:12:40 +00:00
|
|
|
struct mbuf **pcbopt;
|
|
|
|
u_char opt;
|
|
|
|
|
2008-04-17 21:38:18 +00:00
|
|
|
INP_WLOCK_ASSERT(inp);
|
2005-11-18 20:12:40 +00:00
|
|
|
|
|
|
|
pcbopt = &inp->inp_options;
|
|
|
|
|
|
|
|
/* turn off any old options */
|
|
|
|
if (*pcbopt)
|
|
|
|
(void)m_free(*pcbopt);
|
2016-04-15 15:46:41 +00:00
|
|
|
*pcbopt = NULL;
|
2005-11-18 20:12:40 +00:00
|
|
|
if (m == NULL || m->m_len == 0) {
|
|
|
|
/*
|
|
|
|
* Only turning off any previous options.
|
|
|
|
*/
|
|
|
|
if (m != NULL)
|
|
|
|
(void)m_free(m);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m->m_len % sizeof(int32_t))
|
|
|
|
goto bad;
|
|
|
|
/*
|
2007-05-11 10:48:30 +00:00
|
|
|
* IP first-hop destination address will be stored before actual
|
|
|
|
* options; move other options back and clear it when none present.
|
2005-11-18 20:12:40 +00:00
|
|
|
*/
|
|
|
|
if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
|
|
|
|
goto bad;
|
|
|
|
cnt = m->m_len;
|
|
|
|
m->m_len += sizeof(struct in_addr);
|
|
|
|
cp = mtod(m, u_char *) + sizeof(struct in_addr);
|
|
|
|
bcopy(mtod(m, void *), cp, (unsigned)cnt);
|
|
|
|
bzero(mtod(m, void *), sizeof(struct in_addr));
|
|
|
|
|
|
|
|
for (; cnt > 0; cnt -= optlen, cp += optlen) {
|
|
|
|
opt = cp[IPOPT_OPTVAL];
|
|
|
|
if (opt == IPOPT_EOL)
|
|
|
|
break;
|
|
|
|
if (opt == IPOPT_NOP)
|
|
|
|
optlen = 1;
|
|
|
|
else {
|
|
|
|
if (cnt < IPOPT_OLEN + sizeof(*cp))
|
|
|
|
goto bad;
|
|
|
|
optlen = cp[IPOPT_OLEN];
|
|
|
|
if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
|
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
switch (opt) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IPOPT_LSRR:
|
|
|
|
case IPOPT_SSRR:
|
|
|
|
/*
|
2007-05-11 10:48:30 +00:00
|
|
|
* User process specifies route as:
|
|
|
|
*
|
2005-11-18 20:12:40 +00:00
|
|
|
* ->A->B->C->D
|
2007-05-11 10:48:30 +00:00
|
|
|
*
|
2005-11-18 20:12:40 +00:00
|
|
|
* D must be our final destination (but we can't
|
|
|
|
* check that since we may not have connected yet).
|
2007-05-11 10:48:30 +00:00
|
|
|
* A is first hop destination, which doesn't appear
|
|
|
|
* in actual IP option, but is stored before the
|
|
|
|
* options.
|
2005-11-18 20:12:40 +00:00
|
|
|
*/
|
2008-01-24 08:25:59 +00:00
|
|
|
/* XXX-BZ PRIV_NETINET_SETHDROPTS? */
|
2005-11-18 20:12:40 +00:00
|
|
|
if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
|
|
|
|
goto bad;
|
|
|
|
m->m_len -= sizeof(struct in_addr);
|
|
|
|
cnt -= sizeof(struct in_addr);
|
|
|
|
optlen -= sizeof(struct in_addr);
|
|
|
|
cp[IPOPT_OLEN] = optlen;
|
|
|
|
/*
|
|
|
|
* Move first hop before start of options.
|
|
|
|
*/
|
|
|
|
bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
|
|
|
|
sizeof(struct in_addr));
|
|
|
|
/*
|
|
|
|
* Then copy rest of options back
|
|
|
|
* to close up the deleted entry.
|
|
|
|
*/
|
|
|
|
bcopy((&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)),
|
|
|
|
&cp[IPOPT_OFFSET+1],
|
|
|
|
(unsigned)cnt - (IPOPT_MINOFF - 1));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
|
|
|
|
goto bad;
|
|
|
|
*pcbopt = m;
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
bad:
|
|
|
|
(void)m_free(m);
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
2009-03-04 02:51:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for the presence of the IP Router Alert option [RFC2113]
|
|
|
|
* in the header of an IPv4 datagram.
|
|
|
|
*
|
|
|
|
* This call is not intended for use from the forwarding path; it is here
|
|
|
|
* so that protocol domains may check for the presence of the option.
|
|
|
|
* Given how FreeBSD's IPv4 stack is currently structured, the Router Alert
|
|
|
|
* option does not have much relevance to the implementation, though this
|
|
|
|
* may change in future.
|
|
|
|
* Router alert options SHOULD be passed if running in IPSTEALTH mode and
|
|
|
|
* we are not the endpoint.
|
2016-05-03 18:05:43 +00:00
|
|
|
* Length checks on individual options should already have been performed
|
2009-03-04 02:51:22 +00:00
|
|
|
* by ip_dooptions() therefore they are folded under INVARIANTS here.
|
|
|
|
*
|
|
|
|
* Return zero if not present or options are invalid, non-zero if present.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ip_checkrouteralert(struct mbuf *m)
|
|
|
|
{
|
|
|
|
struct ip *ip = mtod(m, struct ip *);
|
|
|
|
u_char *cp;
|
|
|
|
int opt, optlen, cnt, found_ra;
|
|
|
|
|
|
|
|
found_ra = 0;
|
|
|
|
cp = (u_char *)(ip + 1);
|
|
|
|
cnt = (ip->ip_hl << 2) - sizeof (struct ip);
|
|
|
|
for (; cnt > 0; cnt -= optlen, cp += optlen) {
|
|
|
|
opt = cp[IPOPT_OPTVAL];
|
|
|
|
if (opt == IPOPT_EOL)
|
|
|
|
break;
|
|
|
|
if (opt == IPOPT_NOP)
|
|
|
|
optlen = 1;
|
|
|
|
else {
|
|
|
|
#ifdef INVARIANTS
|
|
|
|
if (cnt < IPOPT_OLEN + sizeof(*cp))
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
optlen = cp[IPOPT_OLEN];
|
|
|
|
#ifdef INVARIANTS
|
|
|
|
if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
switch (opt) {
|
|
|
|
case IPOPT_RA:
|
|
|
|
#ifdef INVARIANTS
|
|
|
|
if (optlen != IPOPT_OFFSET + sizeof(uint16_t) ||
|
|
|
|
(*((uint16_t *)&cp[IPOPT_OFFSET]) != 0))
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
found_ra = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (found_ra);
|
|
|
|
}
|