Advertisement
alynna

ipv6.sh for DHCPv6

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