44#include <sys/socket.h>
45#include <sys/sockio.h>
48#include <sys/kernel.h>
49#include <sys/sysctl.h>
50#include <sys/malloc.h>
53#include <net/ethernet.h>
55#include <net/if_var.h>
72#include <net/if_gif.h>
76#define V_ip_gif_ttl VNET(ip_gif_ttl)
77SYSCTL_INT(_net_inet_ip, IPCTL_GIF_TTL, gifttl, CTLFLAG_VNET | CTLFLAG_RW,
78 &VNET_NAME(ip_gif_ttl), 0,
"Default TTL value for encapsulated packets");
87#define V_ipv4_hashtbl VNET(ipv4_hashtbl)
88#define V_ipv4_srchashtbl VNET(ipv4_srchashtbl)
89#define V_ipv4_list VNET(ipv4_list)
91#define GIF_HASH(src, dst) (V_ipv4_hashtbl[\
92 in_gif_hashval((src), (dst)) & (GIF_HASH_SIZE - 1)])
93#define GIF_SRCHASH(src) (V_ipv4_srchashtbl[\
94 fnv_32_buf(&(src), sizeof(src), FNV1_32_INIT) & (GIF_HASH_SIZE - 1)])
95#define GIF_HASH_SC(sc) GIF_HASH((sc)->gif_iphdr->ip_src.s_addr,\
96 (sc)->gif_iphdr->ip_dst.s_addr)
102 ret = fnv_32_buf(&src,
sizeof(src), FNV1_32_INIT);
103 return (fnv_32_buf(&dst,
sizeof(dst), ret));
109 struct gif_softc *tmp;
111 if (sc->gif_family == AF_INET &&
112 sc->gif_iphdr->ip_src.s_addr == src &&
113 sc->gif_iphdr->ip_dst.s_addr == dst)
116 CK_LIST_FOREACH(tmp, &
GIF_HASH(src, dst), chain) {
119 if (tmp->gif_iphdr->ip_src.s_addr == src &&
120 tmp->gif_iphdr->ip_dst.s_addr == dst)
121 return (EADDRNOTAVAIL);
134 GIF2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
136 GIF2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
149 struct gif_softc *sc;
168 if (sc->gif_options & GIF_IGNORE_SOURCE)
173 CK_LIST_INSERT_HEAD(&
GIF_SRCHASH(sc->gif_iphdr->ip_src.s_addr),
182 MPASS(sc->gif_family == AF_INET);
183 MPASS(sc->gif_options != options);
185 if ((options & GIF_IGNORE_SOURCE) !=
186 (sc->gif_options & GIF_IGNORE_SOURCE)) {
187 CK_LIST_REMOVE(sc, srchash);
188 CK_LIST_REMOVE(sc, chain);
189 sc->gif_options = options;
198 struct ifreq *ifr = (
struct ifreq *)data;
199 struct epoch_tracker et;
219 error = EADDRNOTAVAIL;
228 if (error == EADDRNOTAVAIL)
230 if (error == EEXIST) {
235 ip = malloc(
sizeof(*
ip), M_GIF, M_WAITOK | M_ZERO);
238 if (sc->gif_family != 0) {
240 CK_LIST_REMOVE(sc, srchash);
241 CK_LIST_REMOVE(sc, chain);
243 free(sc->gif_hdr, M_GIF);
246 sc->gif_family = AF_INET;
253 case SIOCGIFPSRCADDR:
254 case SIOCGIFPDSTADDR:
255 if (sc->gif_family != AF_INET) {
256 error = EADDRNOTAVAIL;
260 memset(src, 0,
sizeof(*src));
263 src->
sin_addr = (cmd == SIOCGIFPSRCADDR) ?
264 sc->gif_iphdr->ip_src: sc->gif_iphdr->ip_dst;
265 error = prison_if(curthread->td_ucred, (
struct sockaddr *)src);
267 memset(src, 0,
sizeof(*src));
276 struct gif_softc *sc = ifp->if_softc;
282 len =
sizeof(
struct ip);
283#ifndef __NO_STRICT_ALIGNMENT
284 if (proto == IPPROTO_ETHERIP)
285 len += ETHERIP_ALIGN;
287 M_PREPEND(m, len, M_NOWAIT);
290#ifndef __NO_STRICT_ALIGNMENT
291 if (proto == IPPROTO_ETHERIP) {
292 len = mtod(m, vm_offset_t) & 3;
293 KASSERT(len == 0 || len == ETHERIP_ALIGN,
294 (
"in_gif_output: unexpected misalignment"));
296 m->m_len -= ETHERIP_ALIGN;
299 ip = mtod(m,
struct ip *);
301 MPASS(sc->gif_family == AF_INET);
302 bcopy(sc->gif_iphdr,
ip,
sizeof(
struct ip));
306 ip->
ip_len = htons(m->m_pkthdr.len);
309 return (
ip_output(m, NULL, NULL, 0, NULL, NULL));
315 struct gif_softc *sc = arg;
324 return (IPPROTO_DONE);
327 if ((gifp->if_flags & IFF_UP) != 0) {
328 ip = mtod(m,
struct ip *);
331 gif_input(m, gifp, proto, ecn);
336 return (IPPROTO_DONE);
343 struct gif_softc *sc;
350 ip = mtod(m,
const struct ip *);
358 ip->ip_src.s_addr), chain) {
364 sc->gif_iphdr->ip_dst.s_addr ==
ip->ip_src.s_addr) {
381 if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
384 if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0) {
386 m->m_pkthdr.rcvif) == 0)
400 .proto = IPPROTO_IPV4,
401 .min_length = 2 *
sizeof(
struct ip),
410 .proto = IPPROTO_IPV6,
411 .min_length =
sizeof(
struct ip) +
421 .proto = IPPROTO_ETHERIP,
422 .min_length =
sizeof(
struct ip) +
423 sizeof(struct etherip_header) +
424 sizeof(struct ether_header),
437 if (!IS_DEFAULT_VNET(curvnet))
452 if (IS_DEFAULT_VNET(curvnet)) {
bool in_localip(struct in_addr in)
int fib4_check_urpf(uint32_t fibnum, struct in_addr dst, uint32_t scopeid, uint32_t flags, const struct ifnet *src_if)
int in_gif_ioctl(struct gif_softc *sc, u_long cmd, caddr_t data)
static void in_gif_set_running(struct gif_softc *sc)
static void in_gif_attach(struct gif_softc *sc)
static uint32_t in_gif_hashval(in_addr_t src, in_addr_t dst)
static void in_gif_srcaddr(void *arg __unused, const struct sockaddr *sa, int event __unused)
const struct encap_config encap
static int in_gif_input(struct mbuf *m, int off, int proto, void *arg)
int in_gif_setopts(struct gif_softc *sc, u_int options)
static struct @2 ipv4_encap_cfg[]
static int in_gif_lookup(const struct mbuf *m, int off, int proto, void **arg)
VNET_DEFINE_STATIC(int, ip_gif_ttl)
const struct encaptab * cookie
#define GIF_HASH(src, dst)
#define V_ipv4_srchashtbl
SYSCTL_INT(_net_inet_ip, IPCTL_GIF_TTL, gifttl, CTLFLAG_VNET|CTLFLAG_RW, &VNET_NAME(ip_gif_ttl), 0, "Default TTL value for encapsulated packets")
int in_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn)
static int in_gif_checkdup(const struct gif_softc *sc, in_addr_t src, in_addr_t dst)
static const struct srcaddrtab * ipv4_srcaddrtab
int ip_encap_detach(const struct encaptab *)
int ip_encap_unregister_srcaddr(const struct srcaddrtab *)
const struct encaptab * ip_encap_attach(const struct encap_config *, void *arg, int mflags)
const struct srcaddrtab * ip_encap_register_srcaddr(encap_srcaddr_t, void *arg, int mflags)
int ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags, struct ip_moptions *imo, struct inpcb *inp)
#define KMOD_IPSTAT_INC(name)
struct in_addr ip_src ip_dst