device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-util / nm-setting-bond.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
3 /*
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.
8  *
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.
13  *
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.
18  *
19  * Copyright 2011 - 2013 Red Hat, Inc.
20  */
21
22 #include "nm-default.h"
23
24 #include <string.h>
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
29 #include <dbus/dbus-glib.h>
30
31 #include "nm-setting-bond.h"
32 #include "nm-param-spec-specialized.h"
33 #include "nm-utils.h"
34 #include "nm-utils-private.h"
35 #include "nm-dbus-glib-types.h"
36 #include "nm-setting-private.h"
37
38 /**
39  * SECTION:nm-setting-bond
40  * @short_description: Describes connection properties for bonds
41  * @include: nm-setting-bond.h
42  *
43  * The #NMSettingBond object is a #NMSetting subclass that describes properties
44  * necessary for bond connections.
45  **/
46
47 /**
48  * nm_setting_bond_error_quark:
49  *
50  * Registers an error quark for #NMSettingBond if necessary.
51  *
52  * Returns: the error quark used for #NMSettingBond errors.
53  **/
54 GQuark
55 nm_setting_bond_error_quark (void)
56 {
57         static GQuark quark;
58
59         if (G_UNLIKELY (!quark))
60                 quark = g_quark_from_static_string ("nm-setting-bond-error-quark");
61         return quark;
62 }
63
64
65 G_DEFINE_TYPE_WITH_CODE (NMSettingBond, nm_setting_bond, NM_TYPE_SETTING,
66                          _nm_register_setting (NM_SETTING_BOND_SETTING_NAME,
67                                                g_define_type_id,
68                                                1,
69                                                NM_SETTING_BOND_ERROR))
70 NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_BOND)
71
72 #define NM_SETTING_BOND_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_BOND, NMSettingBondPrivate))
73
74 typedef struct {
75         char *interface_name;
76         GHashTable *options;
77 } NMSettingBondPrivate;
78
79 enum {
80         PROP_0,
81         PROP_INTERFACE_NAME,
82         PROP_OPTIONS,
83         LAST_PROP
84 };
85
86 enum {
87         TYPE_INT,
88         TYPE_STR,
89         TYPE_BOTH,
90         TYPE_IP,
91         TYPE_IFNAME,
92 };
93
94 typedef struct {
95         const char *opt;
96         const char *val;
97         guint opt_type;
98         guint min;
99         guint max;
100         char *list[10];
101 } BondDefault;
102
103 static const BondDefault defaults[] = {
104         { NM_SETTING_BOND_OPTION_MODE,             "balance-rr", TYPE_BOTH, 0, 6,
105           { "balance-rr", "active-backup", "balance-xor", "broadcast", "802.3ad", "balance-tlb", "balance-alb", NULL } },
106         { NM_SETTING_BOND_OPTION_MIIMON,           "100",        TYPE_INT, 0, G_MAXINT },
107         { NM_SETTING_BOND_OPTION_DOWNDELAY,        "0",          TYPE_INT, 0, G_MAXINT },
108         { NM_SETTING_BOND_OPTION_UPDELAY,          "0",          TYPE_INT, 0, G_MAXINT },
109         { NM_SETTING_BOND_OPTION_ARP_INTERVAL,     "0",          TYPE_INT, 0, G_MAXINT },
110         { NM_SETTING_BOND_OPTION_ARP_IP_TARGET,    "",           TYPE_IP },
111         { NM_SETTING_BOND_OPTION_ARP_VALIDATE,     "0",          TYPE_BOTH, 0, 3,
112           { "none", "active", "backup", "all", NULL } },
113         { NM_SETTING_BOND_OPTION_PRIMARY,          "",           TYPE_IFNAME },
114         { NM_SETTING_BOND_OPTION_PRIMARY_RESELECT, "0",          TYPE_BOTH, 0, 2,
115           { "always", "better", "failure", NULL } },
116         { NM_SETTING_BOND_OPTION_FAIL_OVER_MAC,    "0",          TYPE_BOTH, 0, 2,
117           { "none", "active", "follow", NULL } },
118         { NM_SETTING_BOND_OPTION_USE_CARRIER,      "1",          TYPE_INT, 0, 1 },
119         { NM_SETTING_BOND_OPTION_AD_SELECT,        "0",          TYPE_BOTH, 0, 2,
120           { "stable", "bandwidth", "count", NULL } },
121         { NM_SETTING_BOND_OPTION_XMIT_HASH_POLICY, "0",          TYPE_BOTH, 0, 2,
122           { "layer2", "layer3+4", "layer2+3", NULL } },
123         { NM_SETTING_BOND_OPTION_RESEND_IGMP,      "1",          TYPE_INT, 0, 255 },
124         { NM_SETTING_BOND_OPTION_LACP_RATE,        "0",          TYPE_BOTH, 0, 1,
125           { "slow", "fast", NULL } },
126 };
127
128 /**
129  * nm_setting_bond_new:
130  *
131  * Creates a new #NMSettingBond object with default values.
132  *
133  * Returns: (transfer full): the new empty #NMSettingBond object
134  **/
135 NMSetting *
136 nm_setting_bond_new (void)
137 {
138         return (NMSetting *) g_object_new (NM_TYPE_SETTING_BOND, NULL);
139 }
140
141 /**
142  * nm_setting_bond_get_interface_name:
143  * @setting: the #NMSettingBond
144  *
145  * Returns: the #NMSettingBond:interface-name property of the setting
146  **/
147 const char *
148 nm_setting_bond_get_interface_name (NMSettingBond *setting)
149 {
150         g_return_val_if_fail (NM_IS_SETTING_BOND (setting), NULL);
151
152         return NM_SETTING_BOND_GET_PRIVATE (setting)->interface_name;
153 }
154
155 /**
156  * nm_setting_bond_get_num_options:
157  * @setting: the #NMSettingBond
158  *
159  * Returns the number of options that should be set for this bond when it
160  * is activated. This can be used to retrieve each option individually
161  * using nm_setting_bond_get_option().
162  *
163  * Returns: the number of bonding options
164  **/
165 guint32
166 nm_setting_bond_get_num_options (NMSettingBond *setting)
167 {
168         g_return_val_if_fail (NM_IS_SETTING_BOND (setting), 0);
169
170         return g_hash_table_size (NM_SETTING_BOND_GET_PRIVATE (setting)->options);
171 }
172
173 /**
174  * nm_setting_bond_get_option:
175  * @setting: the #NMSettingBond
176  * @idx: index of the desired option, from 0 to
177  * nm_setting_bond_get_num_options() - 1
178  * @out_name: (out): on return, the name of the bonding option; this
179  * value is owned by the setting and should not be modified
180  * @out_value: (out): on return, the value of the name of the bonding
181  * option; this value is owned by the setting and should not be modified
182  *
183  * Given an index, return the value of the bonding option at that index.  Indexes
184  * are *not* guaranteed to be static across modifications to options done by
185  * nm_setting_bond_add_option() and nm_setting_bond_remove_option(),
186  * and should not be used to refer to options except for short periods of time
187  * such as during option iteration.
188  *
189  * Returns: %TRUE on success if the index was valid and an option was found,
190  * %FALSE if the index was invalid (ie, greater than the number of options
191  * currently held by the setting)
192  **/
193 gboolean
194 nm_setting_bond_get_option (NMSettingBond *setting,
195                             guint32 idx,
196                             const char **out_name,
197                             const char **out_value)
198 {
199         NMSettingBondPrivate *priv;
200         GList *keys;
201         const char *_key = NULL, *_value = NULL;
202
203         g_return_val_if_fail (NM_IS_SETTING_BOND (setting), FALSE);
204
205         priv = NM_SETTING_BOND_GET_PRIVATE (setting);
206
207         if (idx >= nm_setting_bond_get_num_options (setting))
208                 return FALSE;
209
210         keys = g_hash_table_get_keys (priv->options);
211         _key = g_list_nth_data (keys, idx);
212         _value = g_hash_table_lookup (priv->options, _key);
213
214         if (out_name)
215                 *out_name = _key;
216         if (out_value)
217                 *out_value = _value;
218
219         g_list_free (keys);
220         return TRUE;
221 }
222
223 static gboolean
224 validate_int (const char *name, const char *value, const BondDefault *def)
225 {
226         glong num;
227         guint i;
228
229         for (i = 0; i < strlen (value); i++) {
230                 if (!g_ascii_isdigit (value[i]) && value[i] != '-')
231                         return FALSE;
232         }
233
234         errno = 0;
235         num = strtol (value, NULL, 10);
236         if (errno)
237                 return FALSE;
238         if (num < def->min || num > def->max)
239                 return FALSE;
240
241         return TRUE;
242 }
243
244 static gboolean
245 validate_list (const char *name, const char *value, const BondDefault *def)
246 {
247         guint i;
248
249         for (i = 0; i < G_N_ELEMENTS (def->list) && def->list[i]; i++) {
250                 if (g_strcmp0 (def->list[i], value) == 0)
251                         return TRUE;
252         }
253
254         /* empty validation list means all values pass */
255         return def->list[0] == NULL ? TRUE : FALSE;
256 }
257
258 static gboolean
259 validate_ip (const char *name, const char *value)
260 {
261         char **ips, **iter;
262         gboolean success = TRUE;
263         struct in_addr addr;
264
265         if (!value || !value[0])
266                 return FALSE;
267
268         ips = g_strsplit_set (value, ",", 0);
269         for (iter = ips; iter && *iter && success; iter++)
270                 success = !!inet_aton (*iter, &addr);
271         g_strfreev (ips);
272
273         return success;
274 }
275
276 static gboolean
277 validate_ifname (const char *name, const char *value)
278 {
279         if (!value || !value[0])
280                 return FALSE;
281
282         return nm_utils_iface_valid_name (value);
283 }
284
285 /**
286  * nm_setting_bond_validate_option:
287  * @name: the name of the option to validate
288  * @value: the value of the option to validate
289  *
290  * Checks whether @name is a valid bond option and @value is a valid value for
291  * the @name. If @value is %NULL, the function only validates the option name.
292  *
293  * Returns: %TRUE, if the @value is valid for the given name.
294  * If the @name is not a valid option, %FALSE will be returned.
295  *
296  * Since: 0.9.10
297  **/
298 gboolean
299 nm_setting_bond_validate_option (const char *name,
300                                  const char *value)
301 {
302         guint i;
303
304         if (!name || !name[0])
305                 return FALSE;
306
307         for (i = 0; i < G_N_ELEMENTS (defaults); i++) {
308                 if (g_strcmp0 (defaults[i].opt, name) == 0) {
309                         if (value == NULL)
310                                 return TRUE;
311                         switch (defaults[i].opt_type) {
312                         case TYPE_INT:
313                                 return validate_int (name, value, &defaults[i]);
314                         case TYPE_STR:
315                                 return validate_list (name, value, &defaults[i]);
316                         case TYPE_BOTH:
317                                 return (   validate_int (name, value, &defaults[i])
318                                         || validate_list (name, value, &defaults[i]));
319                         case TYPE_IP:
320                                 return validate_ip (name, value);
321                         case TYPE_IFNAME:
322                                 return validate_ifname (name, value);
323                         }
324                         return FALSE;
325                 }
326         }
327         return FALSE;
328 }
329
330 /**
331  * nm_setting_bond_get_option_by_name:
332  * @setting: the #NMSettingBond
333  * @name: the option name for which to retrieve the value
334  *
335  * Returns the value associated with the bonding option specified by
336  * @name, if it exists.
337  *
338  * Returns: the value, or %NULL if the key/value pair was never added to the
339  * setting; the value is owned by the setting and must not be modified
340  **/
341 const char *
342 nm_setting_bond_get_option_by_name (NMSettingBond *setting,
343                                     const char *name)
344 {
345         g_return_val_if_fail (NM_IS_SETTING_BOND (setting), NULL);
346
347         if (!nm_setting_bond_validate_option (name, NULL))
348                 return NULL;
349
350         return g_hash_table_lookup (NM_SETTING_BOND_GET_PRIVATE (setting)->options, name);
351 }
352
353 /**
354  * nm_setting_bond_add_option:
355  * @setting: the #NMSettingBond
356  * @name: name for the option
357  * @value: value for the option
358  *
359  * Add an option to the table.  The option is compared to an internal list
360  * of allowed options.  Option names may contain only alphanumeric characters
361  * (ie [a-zA-Z0-9]).  Adding a new name replaces any existing name/value pair
362  * that may already exist.
363  *
364  * The order of how to set several options is relevant because there are options
365  * that conflict with each other.
366  *
367  * Returns: %TRUE if the option was valid and was added to the internal option
368  * list, %FALSE if it was not.
369  **/
370 gboolean
371 nm_setting_bond_add_option (NMSettingBond *setting,
372                             const char *name,
373                             const char *value)
374 {
375         NMSettingBondPrivate *priv;
376
377         g_return_val_if_fail (NM_IS_SETTING_BOND (setting), FALSE);
378
379         if (!value || !nm_setting_bond_validate_option (name, value))
380                 return FALSE;
381
382         priv = NM_SETTING_BOND_GET_PRIVATE (setting);
383
384         g_hash_table_insert (priv->options, g_strdup (name), g_strdup (value));
385
386         if (   !strcmp (name, NM_SETTING_BOND_OPTION_MIIMON)
387             && strcmp (value, "0") != 0) {
388                 g_hash_table_remove (priv->options, NM_SETTING_BOND_OPTION_ARP_INTERVAL);
389                 g_hash_table_remove (priv->options, NM_SETTING_BOND_OPTION_ARP_IP_TARGET);
390         } else if (   !strcmp (name, NM_SETTING_BOND_OPTION_ARP_INTERVAL)
391                    && strcmp (value, "0") != 0) {
392                 g_hash_table_remove (priv->options, NM_SETTING_BOND_OPTION_MIIMON);
393                 g_hash_table_remove (priv->options, NM_SETTING_BOND_OPTION_DOWNDELAY);
394                 g_hash_table_remove (priv->options, NM_SETTING_BOND_OPTION_UPDELAY);
395         }
396
397         g_object_notify (G_OBJECT (setting), NM_SETTING_BOND_OPTIONS);
398
399         return TRUE;
400 }
401
402 /**
403  * nm_setting_bond_remove_option:
404  * @setting: the #NMSettingBond
405  * @name: name of the option to remove
406  *
407  * Remove the bonding option referenced by @name from the internal option
408  * list.
409  *
410  * Returns: %TRUE if the option was found and removed from the internal option
411  * list, %FALSE if it was not.
412  **/
413 gboolean
414 nm_setting_bond_remove_option (NMSettingBond *setting,
415                                const char *name)
416 {
417         gboolean found;
418
419         g_return_val_if_fail (NM_IS_SETTING_BOND (setting), FALSE);
420
421         if (!nm_setting_bond_validate_option (name, NULL))
422                 return FALSE;
423
424         found = g_hash_table_remove (NM_SETTING_BOND_GET_PRIVATE (setting)->options, name);
425         if (found)
426                 g_object_notify (G_OBJECT (setting), NM_SETTING_BOND_OPTIONS);
427         return found;
428 }
429
430 /**
431  * nm_setting_bond_get_valid_options:
432  * @setting: the #NMSettingBond
433  *
434  * Returns a list of valid bond options.
435  *
436  * Returns: (transfer none): a %NULL-terminated array of strings of valid bond options.
437  **/
438 const char **
439 nm_setting_bond_get_valid_options  (NMSettingBond *setting)
440 {
441         static const char *array[G_N_ELEMENTS (defaults) + 1] = { NULL };
442         int i;
443
444         /* initialize the array once */
445         if (G_UNLIKELY (array[0] == NULL)) {
446                 for (i = 0; i < G_N_ELEMENTS (defaults); i++)
447                         array[i] = defaults[i].opt;
448                 array[i] = NULL;
449         }
450         return array;
451 }
452
453 /**
454  * nm_setting_bond_get_option_default:
455  * @setting: the #NMSettingBond
456  * @name: the name of the option
457  *
458  * Returns: the value of the bond option if not overridden by an entry in
459  *   the #NMSettingBond:options property.
460  **/
461 const char *
462 nm_setting_bond_get_option_default (NMSettingBond *setting, const char *name)
463 {
464         guint i;
465
466         g_return_val_if_fail (NM_IS_SETTING_BOND (setting), NULL);
467         g_return_val_if_fail (nm_setting_bond_validate_option (name, NULL), NULL);
468
469         for (i = 0; i < G_N_ELEMENTS (defaults); i++) {
470                 if (g_strcmp0 (defaults[i].opt, name) == 0)
471                         return defaults[i].val;
472         }
473         /* Any option that passes nm_setting_bond_validate_option() should also be found in defaults */
474         g_assert_not_reached ();
475 }
476
477 static gboolean
478 verify (NMSetting *setting, GSList *all_settings, GError **error)
479 {
480         NMSettingBondPrivate *priv = NM_SETTING_BOND_GET_PRIVATE (setting);
481         GHashTableIter iter;
482         const char *key, *value;
483         const char *valid_modes[] = { "balance-rr",
484                                       "active-backup",
485                                       "balance-xor",
486                                       "broadcast",
487                                       "802.3ad",
488                                       "balance-tlb",
489                                       "balance-alb",
490                                       NULL };
491         int miimon = 0, arp_interval = 0;
492         const char *arp_ip_target = NULL;
493         const char *lacp_rate;
494         const char *primary;
495
496         g_hash_table_iter_init (&iter, priv->options);
497         while (g_hash_table_iter_next (&iter, (gpointer) &key, (gpointer) &value)) {
498                 if (!value[0] || !nm_setting_bond_validate_option (key, value)) {
499                         g_set_error (error,
500                                      NM_SETTING_BOND_ERROR,
501                                      NM_SETTING_BOND_ERROR_INVALID_OPTION,
502                                      _("invalid option '%s' or its value '%s'"),
503                                      key, value);
504                         g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
505                         return FALSE;
506                 }
507         }
508
509         value = g_hash_table_lookup (priv->options, NM_SETTING_BOND_OPTION_MIIMON);
510         if (value)
511                 miimon = atoi (value);
512         value = g_hash_table_lookup (priv->options, NM_SETTING_BOND_OPTION_ARP_INTERVAL);
513         if (value)
514                 arp_interval = atoi (value);
515
516         /* Can only set one of miimon and arp_interval */
517         if (miimon > 0 && arp_interval > 0) {
518                 g_set_error (error,
519                              NM_SETTING_BOND_ERROR,
520                              NM_SETTING_BOND_ERROR_INVALID_OPTION,
521                              _("only one of '%s' and '%s' can be set"),
522                              NM_SETTING_BOND_OPTION_MIIMON,
523                              NM_SETTING_BOND_OPTION_ARP_INTERVAL);
524                 g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
525         }
526
527         value = g_hash_table_lookup (priv->options, NM_SETTING_BOND_OPTION_MODE);
528         if (!value) {
529                 g_set_error (error,
530                              NM_SETTING_BOND_ERROR,
531                              NM_SETTING_BOND_ERROR_MISSING_OPTION,
532                              _("mandatory option '%s' is missing"),
533                              NM_SETTING_BOND_OPTION_MODE);
534                 g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
535                 return FALSE;
536         }
537         if (!_nm_utils_string_in_list (value, valid_modes)) {
538                 g_set_error (error,
539                              NM_SETTING_BOND_ERROR,
540                              NM_SETTING_BOND_ERROR_INVALID_OPTION,
541                              _("'%s' is not a valid value for '%s'"),
542                              value, NM_SETTING_BOND_OPTION_MODE);
543                 g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
544                 return FALSE;
545         }
546
547         /* Make sure mode is compatible with other settings */
548         if (   strcmp (value, "balance-alb") == 0
549             || strcmp (value, "balance-tlb") == 0) {
550                 if (arp_interval > 0) {
551                         g_set_error (error,
552                                      NM_SETTING_BOND_ERROR,
553                                      NM_SETTING_BOND_ERROR_INVALID_OPTION,
554                                      _("'%s=%s' is incompatible with '%s > 0'"),
555                                      NM_SETTING_BOND_OPTION_MODE, value, NM_SETTING_BOND_OPTION_ARP_INTERVAL);
556                         g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
557                         return FALSE;
558                 }
559         }
560
561         primary = g_hash_table_lookup (priv->options, NM_SETTING_BOND_OPTION_PRIMARY);
562         if (strcmp (value, "active-backup") == 0) {
563                 if (primary && !nm_utils_iface_valid_name (primary)) {
564                         g_set_error (error,
565                                      NM_SETTING_BOND_ERROR,
566                                      NM_SETTING_BOND_ERROR_INVALID_OPTION,
567                                      _("'%s' is not a valid interface name for '%s' option"),
568                                      primary, NM_SETTING_BOND_OPTION_PRIMARY);
569                         g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
570                         return FALSE;
571                 }
572         } else {
573                 if (primary) {
574                         g_set_error (error,
575                                      NM_SETTING_BOND_ERROR,
576                                      NM_SETTING_BOND_ERROR_INVALID_OPTION,
577                                      _("'%s' option is only valid for '%s=%s'"),
578                                      NM_SETTING_BOND_OPTION_PRIMARY,
579                                      NM_SETTING_BOND_OPTION_MODE, "active-backup");
580                         g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
581                         return FALSE;
582                 }
583         }
584
585         if (nm_setting_find_in_list (all_settings, NM_SETTING_INFINIBAND_SETTING_NAME)) {
586                 if (strcmp (value, "active-backup") != 0) {
587                         g_set_error (error,
588                                      NM_SETTING_BOND_ERROR,
589                                      NM_SETTING_BOND_ERROR_INVALID_OPTION,
590                                      _("'%s=%s' is not a valid configuration for '%s'"),
591                                      NM_SETTING_BOND_OPTION_MODE, value, NM_SETTING_INFINIBAND_SETTING_NAME);
592                         g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
593                         return FALSE;
594                 }
595         }
596
597         if (miimon == 0) {
598                 /* updelay and downdelay can only be used with miimon */
599                 if (g_hash_table_lookup (priv->options, NM_SETTING_BOND_OPTION_UPDELAY)) {
600                         g_set_error (error,
601                                      NM_SETTING_BOND_ERROR,
602                                      NM_SETTING_BOND_ERROR_INVALID_OPTION,
603                                      _("'%s' option requires '%s' option to be set"),
604                                      NM_SETTING_BOND_OPTION_UPDELAY, NM_SETTING_BOND_OPTION_MIIMON);
605                         g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
606                         return FALSE;
607                 }
608                 if (g_hash_table_lookup (priv->options, NM_SETTING_BOND_OPTION_DOWNDELAY)) {
609                         g_set_error (error,
610                                      NM_SETTING_BOND_ERROR,
611                                      NM_SETTING_BOND_ERROR_INVALID_OPTION,
612                                      _("'%s' option requires '%s' option to be set"),
613                                      NM_SETTING_BOND_OPTION_DOWNDELAY, NM_SETTING_BOND_OPTION_MIIMON);
614                         g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
615                         return FALSE;
616                 }
617         }
618
619         /* arp_ip_target can only be used with arp_interval, and must
620          * contain a comma-separated list of IPv4 addresses.
621          */
622         arp_ip_target = g_hash_table_lookup (priv->options, NM_SETTING_BOND_OPTION_ARP_IP_TARGET);
623         if (arp_interval > 0) {
624                 char **addrs;
625                 guint32 addr;
626                 int i;
627
628                 if (!arp_ip_target) {
629                         g_set_error (error,
630                                      NM_SETTING_BOND_ERROR,
631                                      NM_SETTING_BOND_ERROR_MISSING_OPTION,
632                                      _("'%s' option requires '%s' option to be set"),
633                                      NM_SETTING_BOND_OPTION_ARP_INTERVAL, NM_SETTING_BOND_OPTION_ARP_IP_TARGET);
634                         g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
635                         return FALSE;
636                 }
637
638                 addrs = g_strsplit (arp_ip_target, ",", -1);
639                 if (!addrs[0]) {
640                         g_set_error (error,
641                                      NM_SETTING_BOND_ERROR,
642                                      NM_SETTING_BOND_ERROR_INVALID_OPTION,
643                                      _("'%s' option is empty"),
644                                      NM_SETTING_BOND_OPTION_ARP_IP_TARGET);
645                         g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
646                         g_strfreev (addrs);
647                         return FALSE;
648                 }
649
650                 for (i = 0; addrs[i]; i++) {
651                         if (!inet_pton (AF_INET, addrs[i], &addr)) {
652                                 g_set_error (error,
653                                              NM_SETTING_BOND_ERROR,
654                                              NM_SETTING_BOND_ERROR_INVALID_OPTION,
655                                              _("'%s' is not a valid IPv4 address for '%s' option"),
656                                              NM_SETTING_BOND_OPTION_ARP_IP_TARGET, addrs[i]);
657                                 g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
658                                 g_strfreev (addrs);
659                                 return FALSE;
660                         }
661                 }
662                 g_strfreev (addrs);
663         } else {
664                 if (arp_ip_target) {
665                         g_set_error (error,
666                                      NM_SETTING_BOND_ERROR,
667                                      NM_SETTING_BOND_ERROR_INVALID_OPTION,
668                                      _("'%s' option requires '%s' option to be set"),
669                                      NM_SETTING_BOND_OPTION_ARP_IP_TARGET, NM_SETTING_BOND_OPTION_ARP_INTERVAL);
670                         g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
671                         return FALSE;
672                 }
673         }
674
675         lacp_rate = g_hash_table_lookup (priv->options, NM_SETTING_BOND_OPTION_LACP_RATE);
676         if (   lacp_rate
677             && (g_strcmp0 (value, "802.3ad") != 0 && g_strcmp0 (value, "4") != 0)
678             && (strcmp (lacp_rate, "slow") != 0 && strcmp (lacp_rate, "0") != 0)) {
679                 g_set_error (error,
680                              NM_SETTING_BOND_ERROR,
681                              NM_SETTING_BOND_ERROR_INVALID_OPTION,
682                              _("'%s' option is only valid with mode '%s'"),
683                              NM_SETTING_BOND_OPTION_LACP_RATE, "802.3ad");
684                 g_prefix_error (error, "%s.%s: ", NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_OPTIONS);
685                 return FALSE;
686         }
687
688         return _nm_setting_verify_deprecated_virtual_iface_name (
689                  priv->interface_name, FALSE,
690                  NM_SETTING_BOND_SETTING_NAME, NM_SETTING_BOND_INTERFACE_NAME,
691                  NM_SETTING_BOND_ERROR,
692                  NM_SETTING_BOND_ERROR_INVALID_PROPERTY,
693                  NM_SETTING_BOND_ERROR_MISSING_PROPERTY,
694                  all_settings, error);
695 }
696
697 static const char *
698 get_virtual_iface_name (NMSetting *setting)
699 {
700         NMSettingBond *self = NM_SETTING_BOND (setting);
701
702         return nm_setting_bond_get_interface_name (self);
703 }
704
705 static void
706 nm_setting_bond_init (NMSettingBond *setting)
707 {
708         NMSettingBondPrivate *priv = NM_SETTING_BOND_GET_PRIVATE (setting);
709
710         priv->options = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
711
712         /* Default values: */
713         nm_setting_bond_add_option (setting, NM_SETTING_BOND_OPTION_MODE, "balance-rr");
714 }
715
716 static void
717 finalize (GObject *object)
718 {
719         NMSettingBondPrivate *priv = NM_SETTING_BOND_GET_PRIVATE (object);
720
721         g_free (priv->interface_name);
722         g_hash_table_destroy (priv->options);
723
724         G_OBJECT_CLASS (nm_setting_bond_parent_class)->finalize (object);
725 }
726
727 static void
728 copy_hash (gpointer key, gpointer value, gpointer user_data)
729 {
730         g_hash_table_insert ((GHashTable *) user_data, g_strdup (key), g_strdup (value));
731 }
732
733 static void
734 set_property (GObject *object, guint prop_id,
735               const GValue *value, GParamSpec *pspec)
736 {
737         NMSettingBondPrivate *priv = NM_SETTING_BOND_GET_PRIVATE (object);
738         GHashTable *new_hash;
739
740         switch (prop_id) {
741         case PROP_INTERFACE_NAME:
742                 g_free (priv->interface_name);
743                 priv->interface_name = g_value_dup_string (value);
744                 break;
745         case PROP_OPTIONS:
746                 /* Must make a deep copy of the hash table here... */
747                 g_hash_table_remove_all (priv->options);
748                 new_hash = g_value_get_boxed (value);
749                 if (new_hash)
750                         g_hash_table_foreach (new_hash, copy_hash, priv->options);
751                 break;
752         default:
753                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
754                 break;
755         }
756 }
757
758 static void
759 get_property (GObject *object, guint prop_id,
760               GValue *value, GParamSpec *pspec)
761 {
762         NMSettingBondPrivate *priv = NM_SETTING_BOND_GET_PRIVATE (object);
763         NMSettingBond *setting = NM_SETTING_BOND (object);
764
765         switch (prop_id) {
766         case PROP_INTERFACE_NAME:
767                 g_value_set_string (value, nm_setting_bond_get_interface_name (setting));
768                 break;
769         case PROP_OPTIONS:
770                 g_value_set_boxed (value, priv->options);
771                 break;
772         default:
773                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
774                 break;
775         }
776 }
777
778 static void
779 nm_setting_bond_class_init (NMSettingBondClass *setting_class)
780 {
781         GObjectClass *object_class = G_OBJECT_CLASS (setting_class);
782         NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class);
783
784         g_type_class_add_private (setting_class, sizeof (NMSettingBondPrivate));
785
786         /* virtual methods */
787         object_class->set_property = set_property;
788         object_class->get_property = get_property;
789         object_class->finalize     = finalize;
790         parent_class->verify       = verify;
791         parent_class->get_virtual_iface_name = get_virtual_iface_name;
792
793         /* Properties */
794         /**
795          * NMSettingBond:interface-name:
796          *
797          * The name of the virtual in-kernel bonding network interface
798          **/
799         g_object_class_install_property
800                 (object_class, PROP_INTERFACE_NAME,
801                  g_param_spec_string (NM_SETTING_BOND_INTERFACE_NAME, "", "",
802                                       NULL,
803                                       G_PARAM_READWRITE |
804                                       NM_SETTING_PARAM_INFERRABLE |
805                                       G_PARAM_STATIC_STRINGS));
806
807         /**
808          * NMSettingBond:options:
809          *
810          * Dictionary of key/value pairs of bonding options.  Both keys and values
811          * must be strings. Option names must contain only alphanumeric characters
812          * (ie, [a-zA-Z0-9]).
813          **/
814          g_object_class_install_property
815                  (object_class, PROP_OPTIONS,
816                  _nm_param_spec_specialized (NM_SETTING_BOND_OPTIONS, "", "",
817                                              DBUS_TYPE_G_MAP_OF_STRING,
818                                              G_PARAM_READWRITE |
819                                              NM_SETTING_PARAM_INFERRABLE |
820                                              G_PARAM_STATIC_STRINGS));
821 }