1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library 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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA.
19 * Copyright 2014 Red Hat, Inc.
22 #include "nm-default.h"
26 #include "nm-setting-ip4-config.h"
27 #include "nm-setting-private.h"
30 * SECTION:nm-setting-ip4-config
31 * @short_description: Describes IPv4 addressing, routing, and name service properties
33 * The #NMSettingIP4Config object is a #NMSetting subclass that describes
34 * properties related to IPv4 addressing, routing, and Domain Name Service.
36 * #NMSettingIP4Config has few properties or methods of its own; it inherits
37 * almost everything from #NMSettingIPConfig.
39 * NetworkManager supports 5 values for the #NMSettingIPConfig:method property
40 * for IPv4. If "auto" is specified then the appropriate automatic method
41 * (DHCP, PPP, etc) is used for the interface and most other properties can be
42 * left unset. If "link-local" is specified, then a link-local address in the
43 * 169.254/16 range will be assigned to the interface. If "manual" is
44 * specified, static IP addressing is used and at least one IP address must be
45 * given in the "addresses" property. If "shared" is specified (indicating that
46 * this connection will provide network access to other computers) then the
47 * interface is assigned an address in the 10.42.x.1/24 range and a DHCP and
48 * forwarding DNS server are started, and the interface is NAT-ed to the current
49 * default network connection. "disabled" means IPv4 will not be used on this
53 G_DEFINE_TYPE_WITH_CODE (NMSettingIP4Config, nm_setting_ip4_config, NM_TYPE_SETTING_IP_CONFIG,
54 _nm_register_setting (IP4_CONFIG, 4))
55 NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_IP4_CONFIG)
57 #define NM_SETTING_IP4_CONFIG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_IP4_CONFIG, NMSettingIP4ConfigPrivate))
62 } NMSettingIP4ConfigPrivate;
73 * nm_setting_ip4_config_new:
75 * Creates a new #NMSettingIP4Config object with default values.
77 * Returns: (transfer full): the new empty #NMSettingIP4Config object
80 nm_setting_ip4_config_new (void)
82 return (NMSetting *) g_object_new (NM_TYPE_SETTING_IP4_CONFIG, NULL);
86 * nm_setting_ip4_config_get_dhcp_client_id:
87 * @setting: the #NMSettingIP4Config
89 * Returns the value contained in the #NMSettingIP4Config:dhcp-client-id
92 * Returns: the configured Client ID to send to the DHCP server when requesting
96 nm_setting_ip4_config_get_dhcp_client_id (NMSettingIP4Config *setting)
98 g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), NULL);
100 return NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->dhcp_client_id;
104 * nm_setting_ip4_config_get_dhcp_fqdn:
105 * @setting: the #NMSettingIP4Config
107 * Returns the value contained in the #NMSettingIP4Config:dhcp-fqdn
110 * Returns: the configured FQDN to send to the DHCP server
115 nm_setting_ip4_config_get_dhcp_fqdn (NMSettingIP4Config *setting)
117 g_return_val_if_fail (NM_IS_SETTING_IP4_CONFIG (setting), NULL);
119 return NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting)->dhcp_fqdn;
123 verify (NMSetting *setting, NMConnection *connection, GError **error)
125 NMSettingIP4ConfigPrivate *priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (setting);
126 NMSettingIPConfig *s_ip = NM_SETTING_IP_CONFIG (setting);
127 NMSettingVerifyResult ret;
130 ret = NM_SETTING_CLASS (nm_setting_ip4_config_parent_class)->verify (setting, connection, error);
131 if (ret != NM_SETTING_VERIFY_SUCCESS)
134 method = nm_setting_ip_config_get_method (s_ip);
135 /* Base class already checked that it exists */
138 if (!strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_MANUAL)) {
139 if (nm_setting_ip_config_get_num_addresses (s_ip) == 0) {
142 NM_CONNECTION_ERROR_MISSING_PROPERTY,
143 _("this property cannot be empty for '%s=%s'"),
144 NM_SETTING_IP_CONFIG_METHOD, method);
145 g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP_CONFIG_ADDRESSES);
148 } else if ( !strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL)
149 || !strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_SHARED)
150 || !strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_DISABLED)) {
151 if (nm_setting_ip_config_get_num_dns (s_ip) > 0) {
154 NM_CONNECTION_ERROR_INVALID_PROPERTY,
155 _("this property is not allowed for '%s=%s'"),
156 NM_SETTING_IP_CONFIG_METHOD, method);
157 g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP_CONFIG_DNS);
161 if (nm_setting_ip_config_get_num_dns_searches (s_ip) > 0) {
164 NM_CONNECTION_ERROR_INVALID_PROPERTY,
165 _("this property is not allowed for '%s=%s'"),
166 NM_SETTING_IP_CONFIG_METHOD, method);
167 g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP_CONFIG_DNS_SEARCH);
171 /* Shared allows IP addresses; link-local and disabled do not */
172 if (strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_SHARED) != 0) {
173 if (nm_setting_ip_config_get_num_addresses (s_ip) > 0) {
176 NM_CONNECTION_ERROR_INVALID_PROPERTY,
177 _("this property is not allowed for '%s=%s'"),
178 NM_SETTING_IP_CONFIG_METHOD, method);
179 g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP_CONFIG_ADDRESSES);
183 } else if (!strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_AUTO)) {
186 g_set_error_literal (error,
188 NM_CONNECTION_ERROR_INVALID_PROPERTY,
189 _("property is invalid"));
190 g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP_CONFIG_METHOD);
194 if (priv->dhcp_client_id && !strlen (priv->dhcp_client_id)) {
195 g_set_error_literal (error,
197 NM_CONNECTION_ERROR_INVALID_PROPERTY,
198 _("property is empty"));
199 g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID);
203 if (priv->dhcp_fqdn && !*priv->dhcp_fqdn) {
204 g_set_error_literal (error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY,
205 _("property is empty"));
206 g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_DHCP_FQDN);
210 if (priv->dhcp_fqdn && !strchr (priv->dhcp_fqdn, '.')) {
213 NM_CONNECTION_ERROR_INVALID_PROPERTY,
214 _("'%s' is not a valid FQDN"), priv->dhcp_fqdn);
215 g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_DHCP_FQDN);
219 if (priv->dhcp_fqdn && nm_setting_ip_config_get_dhcp_hostname (s_ip)) {
220 g_set_error_literal (error,
222 NM_CONNECTION_ERROR_INVALID_PROPERTY,
223 _("property cannot be set when dhcp-hostname is also set"));
224 g_prefix_error (error, "%s.%s: ", NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_SETTING_IP4_CONFIG_DHCP_FQDN);
232 nm_setting_ip4_config_init (NMSettingIP4Config *setting)
237 finalize (GObject *object)
239 NMSettingIP4ConfigPrivate *priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (object);
241 g_free (priv->dhcp_client_id);
242 g_free (priv->dhcp_fqdn);
244 G_OBJECT_CLASS (nm_setting_ip4_config_parent_class)->finalize (object);
248 set_property (GObject *object, guint prop_id,
249 const GValue *value, GParamSpec *pspec)
251 NMSettingIP4ConfigPrivate *priv = NM_SETTING_IP4_CONFIG_GET_PRIVATE (object);
254 case PROP_DHCP_CLIENT_ID:
255 g_free (priv->dhcp_client_id);
256 priv->dhcp_client_id = g_value_dup_string (value);
259 g_free (priv->dhcp_fqdn);
260 priv->dhcp_fqdn = g_value_dup_string (value);
263 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
269 get_property (GObject *object, guint prop_id,
270 GValue *value, GParamSpec *pspec)
272 NMSettingIP4Config *s_ip4 = NM_SETTING_IP4_CONFIG (object);
275 case PROP_DHCP_CLIENT_ID:
276 g_value_set_string (value, nm_setting_ip4_config_get_dhcp_client_id (s_ip4));
279 g_value_set_string (value, nm_setting_ip4_config_get_dhcp_fqdn (s_ip4));
282 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
288 ip4_dns_to_dbus (const GValue *prop_value)
290 return nm_utils_ip4_dns_to_variant (g_value_get_boxed (prop_value));
294 ip4_dns_from_dbus (GVariant *dbus_value,
297 g_value_take_boxed (prop_value, nm_utils_ip4_dns_from_variant (dbus_value));
301 ip4_addresses_get (NMSetting *setting,
302 const char *property)
308 g_object_get (setting, property, &addrs, NULL);
309 gateway = nm_setting_ip_config_get_gateway (NM_SETTING_IP_CONFIG (setting));
310 ret = nm_utils_ip4_addresses_to_variant (addrs, gateway);
311 g_ptr_array_unref (addrs);
317 ip4_addresses_set (NMSetting *setting,
318 GVariant *connection_dict,
319 const char *property,
324 char **labels, *gateway = NULL;
327 if (!_nm_setting_use_legacy_property (setting, connection_dict, "addresses", "address-data"))
330 addrs = nm_utils_ip4_addresses_from_variant (value, &gateway);
332 s_ip4 = g_variant_lookup_value (connection_dict, NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_VARIANT_TYPE_SETTING);
333 if (g_variant_lookup (s_ip4, "address-labels", "^as", &labels)) {
334 for (i = 0; i < addrs->len && labels[i]; i++)
336 nm_ip_address_set_attribute (addrs->pdata[i], "label", g_variant_new_string (labels[i]));
339 g_variant_unref (s_ip4);
341 g_object_set (setting,
342 NM_SETTING_IP_CONFIG_ADDRESSES, addrs,
343 NM_SETTING_IP_CONFIG_GATEWAY, gateway,
345 g_ptr_array_unref (addrs);
350 ip4_address_labels_get (NMSetting *setting,
351 NMConnection *connection,
352 const char *property)
354 NMSettingIPConfig *s_ip = NM_SETTING_IP_CONFIG (setting);
355 gboolean have_labels = FALSE;
360 num_addrs = nm_setting_ip_config_get_num_addresses (s_ip);
361 for (i = 0; i < num_addrs; i++) {
362 NMIPAddress *addr = nm_setting_ip_config_get_address (s_ip, i);
363 GVariant *label = nm_ip_address_get_attribute (addr, "label");
373 labels = g_ptr_array_sized_new (num_addrs);
374 for (i = 0; i < num_addrs; i++) {
375 NMIPAddress *addr = nm_setting_ip_config_get_address (s_ip, i);
376 GVariant *label = nm_ip_address_get_attribute (addr, "label");
378 g_ptr_array_add (labels, (char *) (label ? g_variant_get_string (label, NULL) : ""));
381 ret = g_variant_new_strv ((const char * const *) labels->pdata, labels->len);
382 g_ptr_array_unref (labels);
388 ip4_address_data_get (NMSetting *setting,
389 NMConnection *connection,
390 const char *property)
395 g_object_get (setting, NM_SETTING_IP_CONFIG_ADDRESSES, &addrs, NULL);
396 ret = nm_utils_ip_addresses_to_variant (addrs);
397 g_ptr_array_unref (addrs);
403 ip4_address_data_set (NMSetting *setting,
404 GVariant *connection_dict,
405 const char *property,
410 /* Ignore 'address-data' if we're going to process 'addresses' */
411 if (_nm_setting_use_legacy_property (setting, connection_dict, "addresses", "address-data"))
414 addrs = nm_utils_ip_addresses_from_variant (value, AF_INET);
415 g_object_set (setting, NM_SETTING_IP_CONFIG_ADDRESSES, addrs, NULL);
416 g_ptr_array_unref (addrs);
420 ip4_routes_get (NMSetting *setting,
421 const char *property)
426 g_object_get (setting, property, &routes, NULL);
427 ret = nm_utils_ip4_routes_to_variant (routes);
428 g_ptr_array_unref (routes);
434 ip4_routes_set (NMSetting *setting,
435 GVariant *connection_dict,
436 const char *property,
441 if (!_nm_setting_use_legacy_property (setting, connection_dict, "routes", "route-data"))
444 routes = nm_utils_ip4_routes_from_variant (value);
445 g_object_set (setting, property, routes, NULL);
446 g_ptr_array_unref (routes);
450 ip4_route_data_get (NMSetting *setting,
451 NMConnection *connection,
452 const char *property)
457 g_object_get (setting, NM_SETTING_IP_CONFIG_ROUTES, &routes, NULL);
458 ret = nm_utils_ip_routes_to_variant (routes);
459 g_ptr_array_unref (routes);
465 ip4_route_data_set (NMSetting *setting,
466 GVariant *connection_dict,
467 const char *property,
472 /* Ignore 'route-data' if we're going to process 'routes' */
473 if (_nm_setting_use_legacy_property (setting, connection_dict, "routes", "route-data"))
476 routes = nm_utils_ip_routes_from_variant (value, AF_INET);
477 g_object_set (setting, NM_SETTING_IP_CONFIG_ROUTES, routes, NULL);
478 g_ptr_array_unref (routes);
483 nm_setting_ip4_config_class_init (NMSettingIP4ConfigClass *ip4_class)
485 NMSettingClass *setting_class = NM_SETTING_CLASS (ip4_class);
486 GObjectClass *object_class = G_OBJECT_CLASS (ip4_class);
488 g_type_class_add_private (setting_class, sizeof (NMSettingIP4ConfigPrivate));
490 /* virtual methods */
491 object_class->set_property = set_property;
492 object_class->get_property = get_property;
493 object_class->finalize = finalize;
494 setting_class->verify = verify;
500 * variable: BOOTPROTO
502 * values: none, dhcp (bootp), static, ibft, autoip, shared
504 * description: Method used for IPv4 protocol configuration.
510 * format: list of DNS IP addresses
511 * description: List of DNS servers.
512 * example: dns=1.2.3.4;8.8.8.8;8.8.4.4;
516 * variable: DNS1, DNS2, ...
518 * description: List of DNS servers. Even if NetworkManager supports many DNS
519 * servers, initscripts and resolver only care about the first three, usually.
520 * example: DNS1=1.2.3.4 DNS2=10.0.0.254 DNS3=8.8.8.8
525 * property: dns-search
527 * format: string (space-separated domains)
528 * description: List of DNS search domains.
533 * property: addresses
534 * variable: address1, address2, ...
535 * format: address/plen
536 * description: List of static IP addresses.
537 * example: address1=192.168.100.100/24 address2=10.1.1.5/24
540 * property: addresses
541 * variable: IPADDR, PREFIX, IPADDR1, PREFIX1, ...
542 * description: List of static IP addresses.
543 * example: IPADDR=10.5.5.23 PREFIX=24 IPADDR1=1.1.1.2 PREFIX1=16
551 * description: Gateway IP addresses as a string.
552 * example: gateway=192.168.100.1
557 * description: Gateway IP address.
558 * example: GATEWAY=10.5.5.1
564 * variable: route1, route2, ...
565 * format: route/plen[,gateway,metric]
566 * description: List of IP routes.
567 * example: route1=8.8.8.0/24,10.1.1.1,77
572 * variable: ADDRESS1, NETMASK1, GATEWAY1, METRIC1, ...
573 * description: List of static routes. They are not stored in ifcfg-* file,
574 * but in route-* file instead.
579 * property: ignore-auto-routes
580 * variable: PEERROUTES(+)
582 * description: PEERROUTES has the opposite meaning as 'ignore-auto-routes' property.
587 * property: ignore-auto-dns
590 * description: PEERDNS has the opposite meaning as 'ignore-auto-dns' property.
595 * property: dhcp-send-hostname
596 * variable: DHCP_SEND_HOSTNAME(+)
598 * description: Whether DHCP_HOSTNAME should be sent to the DHCP server.
603 * property: dhcp-hostname
604 * variable: DHCP_HOSTNAME
605 * description: Hostname to send to the DHCP server. When both DHCP_HOSTNAME and
606 * DHCP_FQDN are specified only the latter is used.
611 * property: never-default
612 * variable: DEFROUTE (GATEWAYDEV in /etc/sysconfig/network)
614 * description: DEFROUTE=no tells NetworkManager that this connection
615 * should not be assigned the default route. DEFROUTE has the opposite
616 * meaning as 'never-default' property.
622 * variable: IPV4_FAILURE_FATAL(+)
624 * description: IPV4_FAILURE_FATAL has the opposite meaning as 'may-fail' property.
629 * property: route-metric
630 * variable: IPV4_ROUTE_METRIC(+)
632 * description: IPV4_ROUTE_METRIC is the default IPv4 metric for routes on this connection.
633 * If set to -1, a default metric based on the device type is used.
638 * NMSettingIP4Config:dhcp-client-id:
640 * A string sent to the DHCP server to identify the local machine which the
641 * DHCP server may use to customize the DHCP lease and options.
644 * property: dhcp-client-id
645 * variable: DHCP_CLIENT_ID(+)
646 * description: A string sent to the DHCP server to identify the local machine.
647 * example: DHCP_CLIENT_ID=ax-srv-1
650 g_object_class_install_property
651 (object_class, PROP_DHCP_CLIENT_ID,
652 g_param_spec_string (NM_SETTING_IP4_CONFIG_DHCP_CLIENT_ID, "", "",
655 G_PARAM_STATIC_STRINGS));
658 * property: dad-timeout
659 * variable: ARPING_WAIT
660 * default: missing variable means global default (config override or 3)
661 * description: Timeout (in seconds) for performing DAD before configuring
662 * IPv4 addresses. 0 turns off the DAD completely, -1 means default value.
663 * example: ARPING_WAIT=2
668 * property: dhcp-timeout
669 * variable: IPV4_DHCP_TIMEOUT(+)
670 * description: A timeout after which the DHCP transaction fails in case of no response.
671 * example: IPV4_DHCP_TIMEOUT=10
676 * NMSettingIP4Config:dhcp-fqdn:
678 * If the #NMSettingIPConfig:dhcp-send-hostname property is %TRUE, then the
679 * specified FQDN will be sent to the DHCP server when acquiring a lease. This
680 * property and #NMSettingIPConfig:dhcp-hostname are mutually exclusive and
681 * cannot be set at the same time.
686 * property: dhcp-fqdn
687 * variable: DHCP_FQDN
688 * description: FQDN to send to the DHCP server. When both DHCP_HOSTNAME and
689 * DHCP_FQDN are specified only the latter is used.
690 * example: DHCP_FQDN=foo.bar.com
693 g_object_class_install_property
694 (object_class, PROP_DHCP_FQDN,
695 g_param_spec_string (NM_SETTING_IP4_CONFIG_DHCP_FQDN, "", "",
698 G_PARAM_STATIC_STRINGS));
700 /* IP4-specific property overrides */
704 * format: array of uint32
705 * description: Array of IP addresses of DNS servers (as network-byte-order
709 _nm_setting_class_transform_property (setting_class,
710 NM_SETTING_IP_CONFIG_DNS,
711 G_VARIANT_TYPE ("au"),
716 * property: addresses
717 * format: array of array of uint32
718 * description: Deprecated in favor of the 'address-data' and 'gateway'
719 * properties, but this can be used for backward-compatibility with older
720 * daemons. Note that if you send this property the daemon will ignore
721 * 'address-data' and 'gateway'.
723 * Array of IPv4 address structures. Each IPv4 address structure is
724 * composed of 3 32-bit values; the first being the IPv4 address (network
725 * byte order), the second the prefix (1 - 32), and last the IPv4 gateway
726 * (network byte order). The gateway may be left as 0 if no gateway exists
730 _nm_setting_class_override_property (setting_class,
731 NM_SETTING_IP_CONFIG_ADDRESSES,
732 G_VARIANT_TYPE ("aau"),
737 _nm_setting_class_add_dbus_only_property (setting_class,
739 G_VARIANT_TYPE_STRING_ARRAY,
740 ip4_address_labels_get,
744 * property: address-data
745 * format: array of vardict
746 * description: Array of IPv4 addresses. Each address dictionary contains at
747 * least 'address' and 'prefix' entries, containing the IP address as a
748 * string, and the prefix length as a uint32. Additional attributes may
749 * also exist on some addresses.
752 _nm_setting_class_add_dbus_only_property (setting_class,
754 G_VARIANT_TYPE ("aa{sv}"),
755 ip4_address_data_get,
756 ip4_address_data_set);
760 * format: array of array of uint32
761 * description: Deprecated in favor of the 'route-data' property, but this
762 * can be used for backward-compatibility with older daemons. Note that if
763 * you send this property the daemon will ignore 'route-data'.
765 * Array of IPv4 route structures. Each IPv4 route structure is composed
766 * of 4 32-bit values; the first being the destination IPv4 network or
767 * address (network byte order), the second the destination network or
768 * address prefix (1 - 32), the third being the next-hop (network byte
769 * order) if any, and the fourth being the route metric. If the metric is
770 * 0, NM will choose an appropriate default metric for the device. (There
771 * is no way to explicitly specify an actual metric of 0 with this
775 _nm_setting_class_override_property (setting_class,
776 NM_SETTING_IP_CONFIG_ROUTES,
777 G_VARIANT_TYPE ("aau"),
783 * property: route-data
784 * format: array of vardict
785 * description: Array of IPv4 routes. Each route dictionary contains at
786 * least 'dest' and 'prefix' entries, containing the destination IP
787 * address as a string, and the prefix length as a uint32. Most routes
788 * will also have a 'gateway' entry, containing the gateway IP address as
789 * a string. If the route has a 'metric' entry (containing a uint32), that
790 * will be used as the metric for the route (otherwise NM will pick a
791 * default value appropriate to the device). Additional attributes may
792 * also exist on some routes.
795 _nm_setting_class_add_dbus_only_property (setting_class,
797 G_VARIANT_TYPE ("aa{sv}"),