3 ### TRIPE_SLIPIF dynamic allocation script for use with `userv-ipif'
5 ### (c) 2012 Mark Wooding
8 ###----- Licensing notice ---------------------------------------------------
10 ### This file is part of Trivial IP Encryption (TrIPE).
12 ### TrIPE is free software: you can redistribute it and/or modify it under
13 ### the terms of the GNU General Public License as published by the Free
14 ### Software Foundation; either version 3 of the License, or (at your
15 ### option) any later version.
17 ### TrIPE is distributed in the hope that it will be useful, but WITHOUT
18 ### ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 ### FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 ### You should have received a copy of the GNU General Public License
23 ### along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
25 ###--------------------------------------------------------------------------
28 ### This script is an adaptor for attaching tripe's `slip' tunnel driver to
29 ### `userv-ipif'. The latter is a service for GNU Userv which allows
30 ### otherwise unprivileged users to implement network devices, subject to
31 ### administrative limitations on which addresses can be configured and which
32 ### prefixes routed through them. The software is available as part of the
33 ### `userv-utils' package.
35 ### To use this script, you'll need to set up a configuration file
36 ### `$TRIPEDIR/ipif.tab'. This file may contain comments (begining `#') and
37 ### blank lines, both of which are ignored, and entries of the form
39 ### PEER REMOTE-EXT LOCAL-INT REMOTE-INT ROUTE,...
41 ### The PEER names a peer, as given to tripe's ADD command. REMOTE-EXT is
42 ### the external IP address of the peer, i.e., the one which tripe will send
43 ### its packets to. LOCAL-INT and REMOTE-INT are the local and remote
44 ### addresses to be associated with the point-to-point interface. Finally,
45 ### the ROUTEs are a comma-separated list of PREFIX/LEN pairs declaring
46 ### which prefixes should be routed over this interface. The *-INT and
47 ### ROUTEs fields are passed on to the `userv-ipif' service. The REMOTE-EXT
48 ### field is used (a) by the accompanying `ipif-peers' script to set up the
49 ### peer association, and (b) to determine the correct MTU to set; it
50 ### should have the form ADDRESS[:PORT], where the PORT defaults to 4070 if
51 ### it's not given explicitly.
53 ### Having done all of that, and having configured userv-ipif correctly,
54 ### you should set TRIPE_SLIPIF=.../tripe-ipif and everything should just
55 ### work. If you drop the script `ipif-peers' into the $TRIPEDIR/peers
56 ### directory, then the init script will run it and all of the configured
57 ### peers with known remote addresses will be added on startup.
61 : ${TRIPEDIR=@configdir@}
62 : ${logfile=@logfile@}
63 : ${TRIPE_IPIF_LOG=${logfile%/*}/tripe-ipif.log
}
65 ## Parse the command line.
66 case $# in 1) ;; *) echo >&2 "Usage: $quis PEER"; exit 1 ;; esac
69 *) echo >&2 "$quis: \`TRIPEDIR' unset"; exit 1 ;;
73 ## Arrange for errors to go somewhere.
74 exec 2>>"$TRIPE_IPIF_LOG"
75 now
=$
(date +"%Y-%m-%d %H:%M:%S")
76 echo >&2 "$now $quis[$$] running for peer \`$peer'"
78 ## Find the record in the peer table.
80 while read name remote_ext local_int remote_int routes
; do
81 case $name in "$peer") foundp
=t
; break ;; esac
82 done <$TRIPEDIR/ipif.tab
84 nil
) echo >&2 "$quis[$$]: unknown peer \`$peer'"; exit 1 ;;
87 ## Announce the interface name. We actually have no way to determine this,
88 ## so lie and hope that nobody cares.
91 ## Now we can interrogate the server without deadlocking it.
92 algs
=$
(tripectl algs
) overhead
=nil
95 case $i in bulk-overhead
=*) overhead
=${i#*=} ;; esac
101 nil
) echo >&2 "$quis[$$]: failed to discover overhead"; exit 1 ;;
104 ## Determine the remote address if none is specified; strip off a port number
106 case "$remote_ext" in
108 addr
=$
(tripectl addr
$peer)
111 INET
) remote_ext
=$2 ;;
112 *) echo >&2 "$quis: unexpected address family \`$1'"; exit 1 ;;
116 remote_ext
=${remote_ext%:*}
120 ## Determine the MTU based on the path.
121 pmtu
=$
(pathmtu
$remote_ext)
122 mtu
=$
(( $pmtu - 29 - $overhead ))
124 ## Obtain the tunnel and run it.
125 now
=$
(date +"%Y-%m-%d %H:%M:%S")
126 info
="invoking \`userv ipif' for \`$peer'; mtu = $mtu"
127 info
="$info; $local_int -> $remote_int${routes+ $routes}"
128 echo >&2 "$now $quis[$$] $info"
129 exec userv root ipif
$local_int,$remote_int,$mtu,slip
$routes
131 ###----- That's all, folks --------------------------------------------------