summaryrefslogtreecommitdiff
path: root/misc/setup_routing.sh
blob: 730e036a1e9100db626871fc9f6f4384af166a7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/bin/bash
CONFFILE=/root/config_adt.bkp.sh
if [ $# -gt 0 ]
then	export DRY=echo
fi

function nettoyage_nat() {
	ip4_public=$1

	ip addr show dev lo | grep -q "$ip4_public/32" && $DRY ip addr del $ip4_public/32 dev lo

	deleted=0
	ruleno_pre=$(iptables -t nat -L PREROUTING -n --line-numbers | grep " $ip4_public " | cut -d' ' -f1 | head -n1)
	if [ -n "$ruleno_pre" ]
	then	$DRY iptables -t nat -D PREROUTING $ruleno_pre
		deleted=1
	fi
	ruleno_post=$(iptables -t nat -L POSTROUTING -n --line-numbers | grep -E "to:$ip4_public$" | cut -d' ' -f1 | head -n1)
	if [ -n "$ruleno_post" ]
	then	$DRY iptables -t nat -D POSTROUTING $ruleno_post
		deleted=1
	fi
	if [ $deleted -eq 1 ]
	then	$DRY conntrack -L -q $ip4_public
	fi
}

function activation_nat() {
	ip4_nexthop=$1
	ip4_public=$2

	ip addr show dev lo | grep -q "$ip4_public/32" || $DRY ip addr add $ip4_public/32 dev lo

	iptables -t nat -L PREROUTING -n --line-numbers | grep -E "to:$ip4_nexthop$" -q \
	|| $DRY iptables -t nat -A PREROUTING -d $ip4_public -j DNAT --to-destination $ip4_nexthop

	iptables -t nat -L POSTROUTING -n --line-numbers | grep " $ip4_nexthop " -q \
	|| $DRY iptables -t nat -A POSTROUTING -s $ip4_nexthop -j SNAT --to-source $ip4_public
}

# Appelé pour chaque service à router
function do_conf() {
	dev=eth1
	svc_etat=$1
	svc_id=$2
	ip4_nexthop=$3
	ip4_public=$4
	ip6_nexthop=$5
	ip6_prefix=$6
	mac_nexthop=$7
	
	# Référencer les IP du service en cours de configuration dans le fichiers hosts (résolutions DNS locales)
	(
		[ "$ip4_nexthop" != "-" ] && printf '%-22s %s\n' ${ip4_nexthop} ${svc_id}.nh4
		[ "$ip6_nexthop" != "-" ] && printf '%-22s %s\n' ${ip6_nexthop} ${svc_id}.nh6
		[ "$ip4_public" != "-" ] && printf '%-22s %s\n' ${ip4_public} ${svc_id}.ip4
		[ "$ip6_prefix" != "-" ] && printf '%-22s %s\n' ${ip6_prefix}1 ${svc_id}.ip6
	) >> /etc/hosts.adt

	# Ajouter des entrées statiques dans /etc/ethers pour la table ARP (économie broadcast + changements IP WAN)
	if [ "$mac_nexthop" != "-" -a "$mac_nexthop" != "" ]
	then	if [ "$ip4_nexthop" != "-" ]
		then	echo $mac_nexthop $ip4_nexthop >> /etc/ethers.adt
		else	if [ "$ip4_public" != "-" ]
			then	echo $mac_nexthop $ip4_public >> /etc/ethers.adt
			fi
		fi
	fi

	case $svc_etat in
		actif)
			# Cas CHD OpenWRT sans NAT
			if [ ${ip4_nexthop} = "-" ]
			then	
				echo $svc_id CHD no NAT
				# Nettoyage NAT éventuel
				nettoyage_nat $ip4_public

				# CHD IPv4 Internet -> ADT
				$DRY ip -4 route replace $ip4_public/32 dev $dev

				# CHD IPv6 Internet -> ADT
				if [ $ip6_prefix != "-" -a $ip6_nexthop != "-" ]
				then	$DRY ip -6 route replace $ip6_prefix/56 via $ip6_nexthop dev $dev
				fi
			else
				# Cas CHD avec NAT (OpenWRT natté ou firmware TP-Link)
				echo $svc_id CHD with NAT

				# CHD IPv4 NAT Internet <-> ADT
				activation_nat $ip4_nexthop $ip4_public

				# CHD IPv6 Internet -> ADT
				if [ $ip6_prefix != "-" -a $ip6_nexthop != "-" ]
				then	$DRY ip -6 route replace $ip6_prefix/56 via $ip6_nexthop dev $dev
				fi
			fi

		;;
		resilie)
			echo "Attention, durant la migration les resilies sont a decabler manuellement"
			echo "-> $*"
		;;
	esac
}

#function main() {

	# Initialise
	> /etc/hosts.adt
	> /etc/ethers.adt

	# Met en place la conf de chaque adherent
	source $CONFFILE

	# Finalise
	cat /etc/hosts.system /etc/hosts.adt > /etc/hosts
	cat /etc/ethers.system /etc/ethers.adt > /etc/ethers
	$DRY ip route flush cache
	$DRY arp -f /etc/ethers
	exit 0
#}