|
| 1 | +(* |
| 2 | + * Copyright (c) Cloud Software Group, Inc. |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Lesser General Public License as published |
| 6 | + * by the Free Software Foundation; version 2.1 only. with the special |
| 7 | + * exception on linking described in file LICENSE. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Lesser General Public License for more details. |
| 13 | + *) |
| 14 | + |
| 15 | +module D = Debug.Make (struct let name = __MODULE__ end) |
| 16 | + |
| 17 | +open D |
| 18 | + |
| 19 | +type service_type = Dlm | Nbd | Ssh | Vxlan | Http | Xenha [@@deriving enum] |
| 20 | + |
| 21 | +type status = Enabled | Disabled |
| 22 | + |
| 23 | +type protocol = TCP | UDP |
| 24 | + |
| 25 | +(* 1. For firewalld, xapi is always responsible for dynamically controlling |
| 26 | + firewalld services. |
| 27 | + 2. For legacy iptables compatibility: |
| 28 | + - Certain iptables ports (such as 4789 for VXLAN) are managed dynamically. |
| 29 | + - Other ports (such as 22 for SSH) do not require dynamic control, as they |
| 30 | + are already enabled when the host boots up. |
| 31 | + - The `dynamic_control_iptables_port` parameter is used to decide if the |
| 32 | + ports should be controlled dynamically when iptables is selected. |
| 33 | +*) |
| 34 | +type service_info = { |
| 35 | + name: string |
| 36 | + ; port: int |
| 37 | + ; protocol: protocol |
| 38 | + ; dynamic_control_iptables_port: bool |
| 39 | +} |
| 40 | + |
| 41 | +let all_service_types = |
| 42 | + let length = max_service_type - min_service_type + 1 in |
| 43 | + List.init length (fun i -> |
| 44 | + service_type_of_enum (min_service_type + i) |> Option.get |
| 45 | + ) |
| 46 | + |
| 47 | +let status_to_string = function Enabled -> "enabled" | Disabled -> "disabled" |
| 48 | + |
| 49 | +let protocol_to_string = function TCP -> "tcp" | UDP -> "udp" |
| 50 | + |
| 51 | +let service_type_to_service_info = function |
| 52 | + | Dlm -> |
| 53 | + { |
| 54 | + name= "dlm" |
| 55 | + ; port= !Xapi_globs.xapi_clusterd_port |
| 56 | + ; protocol= TCP |
| 57 | + ; dynamic_control_iptables_port= true |
| 58 | + } |
| 59 | + | Nbd -> |
| 60 | + { |
| 61 | + name= "nbd" |
| 62 | + ; port= 10809 |
| 63 | + ; protocol= TCP |
| 64 | + ; dynamic_control_iptables_port= true |
| 65 | + } |
| 66 | + | Ssh -> |
| 67 | + { |
| 68 | + name= "ssh" |
| 69 | + ; port= 22 |
| 70 | + ; protocol= TCP |
| 71 | + ; dynamic_control_iptables_port= false |
| 72 | + } |
| 73 | + | Vxlan -> |
| 74 | + { |
| 75 | + name= "vxlan" |
| 76 | + ; port= 4789 |
| 77 | + ; protocol= UDP |
| 78 | + ; dynamic_control_iptables_port= true |
| 79 | + } |
| 80 | + | Http -> |
| 81 | + { |
| 82 | + name= "xapi-insecure" |
| 83 | + ; port= Constants.http_port |
| 84 | + ; protocol= TCP |
| 85 | + ; dynamic_control_iptables_port= true |
| 86 | + } |
| 87 | + | Xenha -> |
| 88 | + { |
| 89 | + name= "xenha" |
| 90 | + ; port= Xapi_globs.xha_udp_port |
| 91 | + ; protocol= UDP |
| 92 | + ; dynamic_control_iptables_port= false |
| 93 | + } |
| 94 | + |
| 95 | +module type FIREWALL = sig |
| 96 | + val update_firewall_status : |
| 97 | + ?interfaces:string list -> service_type -> status -> unit |
| 98 | +end |
| 99 | + |
| 100 | +module Firewalld : FIREWALL = struct |
| 101 | + let update_firewall_status ?(interfaces = []) service status = |
| 102 | + if !Xapi_globs.dynamic_control_firewalld_service then ( |
| 103 | + let service_option = |
| 104 | + match status with |
| 105 | + | Enabled -> |
| 106 | + "--add-service" |
| 107 | + | Disabled -> |
| 108 | + "--remove-service" |
| 109 | + in |
| 110 | + let service_info = service_type_to_service_info service in |
| 111 | + ( match interfaces with |
| 112 | + | _ :: _ when service = Nbd -> |
| 113 | + let interface_list = String.concat ", " interfaces in |
| 114 | + debug |
| 115 | + "%s: Enable NBD service as the following interfaces are used for \ |
| 116 | + NBD: [%s]" |
| 117 | + __FUNCTION__ interface_list |
| 118 | + | _ -> |
| 119 | + () |
| 120 | + ) ; |
| 121 | + try |
| 122 | + Helpers.call_script !Xapi_globs.firewall_cmd |
| 123 | + [service_option; service_info.name] |
| 124 | + |> ignore |
| 125 | + with e -> |
| 126 | + error |
| 127 | + "%s: Failed to update firewall service (%s) to (%s) with error: %s" |
| 128 | + __FUNCTION__ service_info.name (status_to_string status) |
| 129 | + (Printexc.to_string e) ; |
| 130 | + Helpers.internal_error "Failed to update firewall service (%s)" |
| 131 | + service_info.name |
| 132 | + ) |
| 133 | +end |
| 134 | + |
| 135 | +module Iptables : FIREWALL = struct |
| 136 | + let update_firewall_status ?(interfaces = []) service status = |
| 137 | + let service_info = service_type_to_service_info service in |
| 138 | + if service_info.dynamic_control_iptables_port then ( |
| 139 | + try |
| 140 | + match service with |
| 141 | + | Dlm | Ssh | Vxlan | Http | Xenha -> |
| 142 | + let op = |
| 143 | + match status with Enabled -> "open" | Disabled -> "close" |
| 144 | + in |
| 145 | + Helpers.call_script |
| 146 | + !Xapi_globs.firewall_port_config_script |
| 147 | + [ |
| 148 | + op |
| 149 | + ; string_of_int service_info.port |
| 150 | + ; protocol_to_string service_info.protocol |
| 151 | + ] |
| 152 | + |> ignore |
| 153 | + | Nbd -> |
| 154 | + (* For legacy iptables, NBD port needs to be precisely controlled on |
| 155 | + each interface *) |
| 156 | + let args = "set" :: interfaces in |
| 157 | + Helpers.call_script !Xapi_globs.nbd_firewall_config_script args |
| 158 | + |> ignore |
| 159 | + with e -> |
| 160 | + error |
| 161 | + "%s: Failed to update firewall service (%s) to (%s) with error: %s" |
| 162 | + __FUNCTION__ service_info.name (status_to_string status) |
| 163 | + (Printexc.to_string e) ; |
| 164 | + Helpers.internal_error "Failed to update firewall service (%s)" |
| 165 | + service_info.name |
| 166 | + ) |
| 167 | +end |
| 168 | + |
| 169 | +let firewall_provider (backend : Xapi_globs.firewall_backend_type) : |
| 170 | + (module FIREWALL) = |
| 171 | + match backend with |
| 172 | + | Firewalld -> |
| 173 | + (module Firewalld) |
| 174 | + | Iptables -> |
| 175 | + (module Iptables) |
0 commit comments