setup-ip 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/bin/sh
  2. # Source Qubes library.
  3. # shellcheck disable=SC1091
  4. . /usr/lib/qubes/init/functions
  5. have_qubesdb || exit 0
  6. mac="$(/usr/bin/qubesdb-read /qubes-mac 2> /dev/null)"
  7. current_mac="$(get_mac_from_iface "$INTERFACE")"
  8. if [ "$mac" = "$current_mac" ] || [ "x$mac" = "x" ] ; then
  9. ip="$(/usr/bin/qubesdb-read /qubes-ip 2> /dev/null)"
  10. ip6="$(/usr/bin/qubesdb-read /qubes-ip6 2> /dev/null)"
  11. if [ "x$ip" != x ]; then
  12. #netmask=$(/usr/bin/qubesdb-read /qubes-netmask)
  13. gateway=$(/usr/bin/qubesdb-read /qubes-gateway)
  14. gateway6=$(/usr/bin/qubesdb-read /qubes-gateway6)
  15. primary_dns=$(/usr/bin/qubesdb-read /qubes-primary-dns 2>/dev/null || echo "$gateway")
  16. secondary_dns=$(/usr/bin/qubesdb-read /qubes-secondary-dns)
  17. /sbin/ethtool -K "$INTERFACE" sg off
  18. /sbin/ethtool -K "$INTERFACE" tx off
  19. # If NetworkManager is enabled, let it configure the network
  20. if qsvc network-manager ; then
  21. nm_config="/etc/NetworkManager/system-connections/qubes-uplink-$INTERFACE"
  22. cat > "$nm_config" <<__EOF__
  23. [802-3-ethernet]
  24. duplex=full
  25. [ethernet]
  26. mac-address=$(ip l show dev "$INTERFACE" |grep link|awk '{print $2}')
  27. [connection]
  28. id=VM uplink $INTERFACE
  29. uuid=de85f79b-8c3d-405f-a652-cb4c10b4f9ef
  30. type=802-3-ethernet
  31. __EOF__
  32. ip4_nm_config=""
  33. ip6_nm_config=""
  34. if ! qsvc disable-dns-server ; then
  35. ip4_nm_config="${ip4_nm_config}
  36. dns=${primary_dns};${secondary_dns}"
  37. fi
  38. if ! qsvc disable-default-route ; then
  39. ip4_nm_config="${ip4_nm_config}
  40. addresses1=$ip;32;$gateway"
  41. if [ -n "$ip6" ]; then
  42. ip6_nm_config="${ip6_nm_config}
  43. addresses1=$ip6;128;$gateway6"
  44. fi
  45. else
  46. ip4_nm_config="${ip4_nm_config}
  47. addresses1=$ip;32"
  48. if [ -n "$ip6" ]; then
  49. ip6_nm_config="${ip6_nm_config}
  50. addresses1=$ip6;128"
  51. fi
  52. fi
  53. if [ -n "$ip4_nm_config" ]; then
  54. cat >> "$nm_config" <<__EOF__
  55. [ipv4]
  56. method=manual
  57. may-fail=false
  58. $ip4_nm_config
  59. __EOF__
  60. else
  61. cat >> "$nm_config" <<__EOF__
  62. [ipv4]
  63. method=ignore
  64. __EOF__
  65. fi
  66. if [ -n "$ip6_nm_config" ]; then
  67. cat >> "$nm_config" <<__EOF__
  68. [ipv6]
  69. method=manual
  70. may-fail=false
  71. $ip6_nm_config
  72. __EOF__
  73. else
  74. cat >> "$nm_config" <<__EOF__
  75. [ipv6]
  76. method=ignore
  77. __EOF__
  78. fi
  79. chmod 600 "$nm_config"
  80. # reload connection
  81. nmcli connection load "$nm_config" || :
  82. else
  83. # No NetworkManager enabled, configure the network manually
  84. /sbin/ifconfig "$INTERFACE" "$ip" netmask 255.255.255.255
  85. if [ -n "$ip6" ]; then
  86. /sbin/ifconfig "$INTERFACE" add "$ip6"/128
  87. fi
  88. /sbin/ifconfig "$INTERFACE" up
  89. /sbin/route add -host "$gateway" dev "$INTERFACE"
  90. if [ -n "$gateway6" ] && ! echo "$gateway6" | grep -q "^fe80:"; then
  91. /sbin/route -6 add "$gateway6/128" dev "$INTERFACE"
  92. fi
  93. if ! qsvc disable-default-route ; then
  94. /sbin/route add default gw "$gateway"
  95. if [ -n "$gateway6" ]; then
  96. /sbin/route -6 add default gw "$gateway6" dev "$INTERFACE"
  97. fi
  98. fi
  99. if ! is_protected_file /etc/resolv.conf ; then
  100. echo > /etc/resolv.conf
  101. if ! qsvc disable-dns-server ; then
  102. echo "nameserver $primary_dns" > /etc/resolv.conf
  103. echo "nameserver $secondary_dns" >> /etc/resolv.conf
  104. fi
  105. fi
  106. fi
  107. network=$(qubesdb-read /qubes-netvm-network 2>/dev/null)
  108. if [ "x$network" != "x" ] && ! qsvc disable-dns-server ; then
  109. gateway=$(qubesdb-read /qubes-netvm-gateway)
  110. #netmask=$(qubesdb-read /qubes-netvm-netmask)
  111. primary_dns=$(qubesdb-read /qubes-netvm-primary-dns 2>/dev/null || echo "$gateway")
  112. secondary_dns=$(qubesdb-read /qubes-netvm-secondary-dns)
  113. echo "NS1=$primary_dns" > /var/run/qubes/qubes-ns
  114. echo "NS2=$secondary_dns" >> /var/run/qubes/qubes-ns
  115. /usr/lib/qubes/qubes-setup-dnat-to-ns
  116. fi
  117. if [ "x$network" != "x" ]; then
  118. if [ -x /rw/config/qubes-ip-change-hook ]; then
  119. /rw/config/qubes-ip-change-hook
  120. fi
  121. # XXX: Backward compatibility
  122. if [ -x /rw/config/qubes_ip_change_hook ]; then
  123. /rw/config/qubes_ip_change_hook
  124. fi
  125. fi
  126. fi
  127. fi