setup-ip 4.0 KB

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