xref: /freebsd/share/examples/netgraph/udp.tunnel (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
11177ed6fSArchie Cobbs#!/bin/sh
21177ed6fSArchie Cobbs
31177ed6fSArchie Cobbs# This script sets up a virtual point-to-point WAN link between
41177ed6fSArchie Cobbs# two subnets, using UDP packets as the ``WAN connection.''
51177ed6fSArchie Cobbs# The two subnets might be non-routable addresses behind a
61177ed6fSArchie Cobbs# firewall.
71177ed6fSArchie Cobbs#
81177ed6fSArchie Cobbs
91177ed6fSArchie Cobbs# Here define the local and remote inside networks as well
101177ed6fSArchie Cobbs# as the local and remote outside IP addresses and UDP port
111177ed6fSArchie Cobbs# number that will be used for the tunnel.
121177ed6fSArchie Cobbs#
131177ed6fSArchie CobbsLOC_INTERIOR_IP=192.168.1.1
141177ed6fSArchie CobbsLOC_EXTERIOR_IP=1.1.1.1
151177ed6fSArchie CobbsREM_INTERIOR_IP=192.168.2.1
161177ed6fSArchie CobbsREM_EXTERIOR_IP=2.2.2.2
171177ed6fSArchie CobbsREM_INSIDE_NET=192.168.2.0
181177ed6fSArchie CobbsUDP_TUNNEL_PORT=4028
191177ed6fSArchie Cobbs
201177ed6fSArchie Cobbs# Create the interface node ``ng0'' if it doesn't exist already,
211177ed6fSArchie Cobbs# otherwise just make sure it's not connected to anything.
221177ed6fSArchie Cobbs# In FreeBSD, interfaces cannot be removed so it might already
231177ed6fSArchie Cobbs# be there from before.
241177ed6fSArchie Cobbs#
251177ed6fSArchie Cobbsif ifconfig ng0 >/dev/null 2>&1; then
261177ed6fSArchie Cobbs	ifconfig ng0 inet down delete >/dev/null 2>&1
271177ed6fSArchie Cobbs	ngctl shutdown ng0:
281177ed6fSArchie Cobbselse
291177ed6fSArchie Cobbs	ngctl mkpeer iface dummy inet
301177ed6fSArchie Cobbsfi
311177ed6fSArchie Cobbs
321177ed6fSArchie Cobbs# Attach a UDP socket to the ``inet'' hook of the interface node
336a8b93feSJulian Elischer# using the ng_ksocket(4) node type.
341177ed6fSArchie Cobbs#
351177ed6fSArchie Cobbsngctl mkpeer ng0: ksocket inet inet/dgram/udp
361177ed6fSArchie Cobbs
371177ed6fSArchie Cobbs# Bind the UDP socket to the local external IP address and port
381177ed6fSArchie Cobbs#
391177ed6fSArchie Cobbsngctl msg ng0:inet bind inet/${LOC_EXTERIOR_IP}:${UDP_TUNNEL_PORT}
401177ed6fSArchie Cobbs
411177ed6fSArchie Cobbs# Connect the UDP socket to the peer's external IP address and port
421177ed6fSArchie Cobbs#
431177ed6fSArchie Cobbsngctl msg ng0:inet connect inet/${REM_EXTERIOR_IP}:${UDP_TUNNEL_PORT}
441177ed6fSArchie Cobbs
451177ed6fSArchie Cobbs# Configure the point-to-point interface
461177ed6fSArchie Cobbs#
471177ed6fSArchie Cobbsifconfig ng0 ${LOC_INTERIOR_IP} ${REM_INTERIOR_IP}
481177ed6fSArchie Cobbs
491177ed6fSArchie Cobbs# Add a route to the peer's interior network via the tunnel
501177ed6fSArchie Cobbs#
511177ed6fSArchie Cobbsroute add ${REM_INSIDE_NET} ${REM_INTERIOR_IP}
521177ed6fSArchie Cobbs
53