Advertisement
alynna

Untitled

Sep 17th, 2017
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.56 KB | None | 0 0
  1. #!/bin/bash
  2. # Make sure the following package is installed:
  3. # apt install wide-dhcp6c-client
  4.  
  5. # IFACE is our public facing ethernet interface.
  6. EXT=enp7s1
  7. INT=bond0
  8. FORWARD=1
  9. IPV6SUFFIX=::1
  10.  
  11. doit() {
  12. # Fulfill my prerequisites
  13. if [ ! $(which dhcp6c) ]; then apt install wide-dhcpv6-client; fi
  14.  
  15. # Completely reset IPV6
  16. if [ "$1" = "reset" ]; then
  17.  dhcp6c_conf
  18.  echo "[IPV6]: Fully resetting IPv6."
  19.  ip -6 addr flush dev $EXT
  20.  if [ "$FORWARD" = "0" ]; then
  21.   ip -6 addr flush dev $INT
  22.  fi
  23.  sysctl -qw net.ipv6.conf.all.disable_ipv6=1
  24.  sysctl -qw net.ipv6.conf.all.disable_ipv6=0
  25.  sleep 2
  26. fi
  27.  
  28. # Kill off DHCPv6 so we can refresh our lease.
  29. echo -n "[IPV6]: Killing DHCPv6"
  30. killall -9 dhcp6c
  31. while true; do
  32.  echo -n .
  33.  sleep 1
  34.  if [ "$(pgrep -f dhcp6c)" == "" ]; then echo ""; break; fi
  35. done
  36. dhcp6c $EXT
  37. sleep 1
  38.  
  39. # Accept router advertisements on the proper interface
  40. # and configure ip forwarding
  41. sysctl -qw net.ipv6.conf.all.forwarding=$FORWARD
  42. sysctl -qw net.ipv6.conf.default.forwarding=$FORWARD
  43. sysctl -qw net.ipv6.conf.$EXT.forwarding=0
  44.  
  45. sysctl -qw net.ipv6.conf.all.accept_ra=0
  46. sysctl -qw net.ipv6.conf.default.accept_ra=0
  47. sysctl -qw net.ipv6.conf.$EXT.accept_ra=2
  48.  
  49. # Get a lease for our external interface
  50. echo -n "[IPV6]: Waiting for lease"
  51. while true; do
  52.  echo -n .
  53.  IPV6PREFIX=`ip -6 addr show $INT | grep "scope global" | cut -d' ' -f6 | cut -d: -f1-4`
  54.  if [ "$IPV6PREFIX" != "" ]; then echo ""; break; fi
  55.  sleep 1
  56. done
  57.  
  58. # Depreciate Comcast unicast global address immediately
  59. echo -n "[IPV6]: Depreciating Unicast address: "
  60. sleep 1
  61. OLDADDR=`ip -6 addr show $EXT | grep "2001.*scope global" | cut -d' ' -f6`
  62. if [ "$OLDADDR" != "" ]; then
  63.  ip -6 addr change $OLDADDR dev $EXT preferred_lft 0
  64. fi
  65. echo $OLDADDR
  66.  
  67. echo -n $IPV6PREFIX > /etc/ipv6-network
  68. echo "[IPV6]: IPv6 prefix detected: $IPV6PREFIX"
  69.  
  70. ip -6 addr replace ${IPV6PREFIX}${IPV6SUFFIX} dev $EXT home
  71. echo "[IPV6]: IPV6 address added to external interface $EXT: ${IPV6PREFIX}${IPV6SUFFIX}"
  72.  
  73. # Turn off receiving more router advertisements and make our current route semi-permanent.
  74. echo -n "[IPV6]: Waiting for route"
  75. while true; do
  76.  echo -n :
  77.  IPV6ROUTER=`ip -6 route | grep default | cut -d' ' -f3`
  78.  if [ "$IPV6ROUTER" != "" ]; then break; fi
  79.  sleep 1
  80. done
  81. ip -6 route replace default via $IPV6ROUTER dev $EXT
  82. echo " $IPV6ROUTER"
  83.  
  84. # Stop accepting router announcements
  85. sysctl -qw net.ipv6.conf.$EXT.accept_ra=0
  86.  
  87. # If we're routing, restart radvd for any new prefix updates.
  88. if [ "$FORWARD" = "1" ]; then
  89.  radvd_conf
  90.  echo -n "[IPV6]: "
  91.  /etc/init.d/radvd restart
  92. fi
  93. }
  94.  
  95. # HELPERS
  96. # This sets up wide-dhcp6c whenever the script requests a reset.
  97. dhcp6c_conf() {
  98. cat <<EOF >/etc/wide-dhcpv6/dhcp6c.conf
  99. profile default
  100. {
  101.   information-only;
  102.   request domain-name-servers;
  103.   request domain-name;
  104.   script "/etc/wide-dhcpv6/dhcp6c-script";
  105. };
  106. EOF
  107. echo "interface $EXT {" >> /etc/wide-dhcpv6/dhcp6c.conf
  108. cat <<EOF >>/etc/wide-dhcpv6/dhcp6c.conf
  109.     send rapid-commit;
  110.     send ia-na 0;
  111.     send ia-pd 0;
  112. };
  113. id-assoc na 0 { };
  114. id-assoc pd 0 {
  115.     prefix ::/60 infinity;
  116.     # Internal interface (LAN)
  117. EOF
  118. echo "    prefix-interface $INT {" >> /etc/wide-dhcpv6/dhcp6c.conf
  119. cat <<EOF >>/etc/wide-dhcpv6/dhcp6c.conf
  120.         sla-len 4;
  121.         sla-id 0;
  122.         ifid 1;
  123.     };
  124. };
  125. # Yerf
  126. EOF
  127. }
  128.  
  129. # This sets up radvd.conf for routers.
  130. radvd_conf() {
  131. echo "interface $INT {" > /etc/radvd.conf
  132. cat <<EOF >>/etc/radvd.conf
  133.  AdvSourceLLAddress off;
  134.  AdvSendAdvert on;
  135.  prefix ::/64 {
  136.   AdvOnLink on;
  137.   AdvAutonomous on;
  138.  };
  139. };
  140. # Yerf
  141. EOF
  142. }
  143.  
  144. doit $*
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement