fa934c461889890557f582f4ecd959fd447d3c5e
[NetworkManager.git] / libnm-core / nm-setting-bridge.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 <ctype.h>
26 #include <stdlib.h>
27
28 #include "nm-setting-bridge.h"
29 #include "nm-connection-private.h"
30 #include "nm-utils.h"
31 #include "nm-utils-private.h"
32
33 /**
34  * SECTION:nm-setting-bridge
35  * @short_description: Describes connection properties for bridges
36  *
37  * The #NMSettingBridge object is a #NMSetting subclass that describes properties
38  * necessary for bridging connections.
39  **/
40
41 G_DEFINE_TYPE_WITH_CODE (NMSettingBridge, nm_setting_bridge, NM_TYPE_SETTING,
42                          _nm_register_setting (BRIDGE, 1))
43 NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_BRIDGE)
44
45 #define NM_SETTING_BRIDGE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_BRIDGE, NMSettingBridgePrivate))
46
47 typedef struct {
48         char *   mac_address;
49         gboolean stp;
50         guint16  priority;
51         guint16  forward_delay;
52         guint16  hello_time;
53         guint16  max_age;
54         guint32  ageing_time;
55         gboolean multicast_snooping;
56 } NMSettingBridgePrivate;
57
58 enum {
59         PROP_0,
60         PROP_MAC_ADDRESS,
61         PROP_STP,
62         PROP_PRIORITY,
63         PROP_FORWARD_DELAY,
64         PROP_HELLO_TIME,
65         PROP_MAX_AGE,
66         PROP_AGEING_TIME,
67         PROP_MULTICAST_SNOOPING,
68         LAST_PROP
69 };
70
71 /**
72  * nm_setting_bridge_new:
73  *
74  * Creates a new #NMSettingBridge object with default values.
75  *
76  * Returns: (transfer full): the new empty #NMSettingBridge object
77  **/
78 NMSetting *
79 nm_setting_bridge_new (void)
80 {
81         return (NMSetting *) g_object_new (NM_TYPE_SETTING_BRIDGE, NULL);
82 }
83
84 /**
85  * nm_setting_bridge_get_mac_address:
86  * @setting: the #NMSettingBridge
87  *
88  * Returns: the #NMSettingBridge:mac-address property of the setting
89  **/
90 const char *
91 nm_setting_bridge_get_mac_address (NMSettingBridge *setting)
92 {
93         g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), NULL);
94
95         return NM_SETTING_BRIDGE_GET_PRIVATE (setting)->mac_address;
96 }
97
98 /**
99  * nm_setting_bridge_get_stp:
100  * @setting: the #NMSettingBridge
101  *
102  * Returns: the #NMSettingBridge:stp property of the setting
103  **/
104 gboolean
105 nm_setting_bridge_get_stp (NMSettingBridge *setting)
106 {
107         g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), FALSE);
108
109         return NM_SETTING_BRIDGE_GET_PRIVATE (setting)->stp;
110 }
111
112 /**
113  * nm_setting_bridge_get_priority:
114  * @setting: the #NMSettingBridge
115  *
116  * Returns: the #NMSettingBridge:priority property of the setting
117  **/
118 guint16
119 nm_setting_bridge_get_priority (NMSettingBridge *setting)
120 {
121         g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), 0);
122
123         return NM_SETTING_BRIDGE_GET_PRIVATE (setting)->priority;
124 }
125
126 /**
127  * nm_setting_bridge_get_forward_delay:
128  * @setting: the #NMSettingBridge
129  *
130  * Returns: the #NMSettingBridge:forward-delay property of the setting
131  **/
132 guint16
133 nm_setting_bridge_get_forward_delay (NMSettingBridge *setting)
134 {
135         g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), 0);
136
137         return NM_SETTING_BRIDGE_GET_PRIVATE (setting)->forward_delay;
138 }
139
140 /**
141  * nm_setting_bridge_get_hello_time:
142  * @setting: the #NMSettingBridge
143  *
144  * Returns: the #NMSettingBridge:hello-time property of the setting
145  **/
146 guint16
147 nm_setting_bridge_get_hello_time (NMSettingBridge *setting)
148 {
149         g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), 0);
150
151         return NM_SETTING_BRIDGE_GET_PRIVATE (setting)->hello_time;
152 }
153
154 /**
155  * nm_setting_bridge_get_max_age:
156  * @setting: the #NMSettingBridge
157  *
158  * Returns: the #NMSettingBridge:max-age property of the setting
159  **/
160 guint16
161 nm_setting_bridge_get_max_age (NMSettingBridge *setting)
162 {
163         g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), 0);
164
165         return NM_SETTING_BRIDGE_GET_PRIVATE (setting)->max_age;
166 }
167
168 /**
169  * nm_setting_bridge_get_ageing_time:
170  * @setting: the #NMSettingBridge
171  *
172  * Returns: the #NMSettingBridge:ageing-time property of the setting
173  **/
174 guint
175 nm_setting_bridge_get_ageing_time (NMSettingBridge *setting)
176 {
177         g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), 0);
178
179         return NM_SETTING_BRIDGE_GET_PRIVATE (setting)->ageing_time;
180 }
181
182 /**
183  * nm_setting_bridge_get_multicast_snooping:
184  * @setting: the #NMSettingBridge
185  *
186  * Returns: the #NMSettingBridge:multicast-snooping property of the setting
187  *
188  * Since: 1.2
189  **/
190 gboolean
191 nm_setting_bridge_get_multicast_snooping (NMSettingBridge *setting)
192 {
193         g_return_val_if_fail (NM_IS_SETTING_BRIDGE (setting), FALSE);
194
195         return NM_SETTING_BRIDGE_GET_PRIVATE (setting)->multicast_snooping;
196 }
197
198 /* IEEE 802.1D-1998 timer values */
199 #define BR_MIN_HELLO_TIME    1
200 #define BR_MAX_HELLO_TIME    10
201
202 #define BR_MIN_FORWARD_DELAY 2
203 #define BR_MAX_FORWARD_DELAY 30
204
205 #define BR_MIN_MAX_AGE       6
206 #define BR_MAX_MAX_AGE       40
207
208 /* IEEE 802.1D-1998 Table 7.4 */
209 #define BR_MIN_AGEING_TIME   0
210 #define BR_MAX_AGEING_TIME   1000000
211
212 static inline gboolean
213 check_range (guint32 val,
214              guint32 min,
215              guint32 max,
216              gboolean zero,
217              const char *prop,
218              GError **error)
219 {
220         if (zero && val == 0)
221                 return TRUE;
222
223         if (val < min || val > max) {
224                 g_set_error (error,
225                              NM_CONNECTION_ERROR,
226                              NM_CONNECTION_ERROR_INVALID_PROPERTY,
227                              _("value '%d' is out of range <%d-%d>"),
228                              val, min, max);
229                 g_prefix_error (error, "%s.%s: ", NM_SETTING_BRIDGE_SETTING_NAME, prop);
230                 return FALSE;
231         }
232         return TRUE;
233 }
234
235 static gboolean
236 verify (NMSetting *setting, NMConnection *connection, GError **error)
237 {
238         NMSettingBridgePrivate *priv = NM_SETTING_BRIDGE_GET_PRIVATE (setting);
239
240         if (priv->mac_address && !nm_utils_hwaddr_valid (priv->mac_address, ETH_ALEN)) {
241                 g_set_error_literal (error,
242                                      NM_CONNECTION_ERROR,
243                                      NM_CONNECTION_ERROR_INVALID_PROPERTY,
244                                      _("is not a valid MAC address"));
245                 g_prefix_error (error, "%s.%s: ", NM_SETTING_BRIDGE_SETTING_NAME, NM_SETTING_BRIDGE_MAC_ADDRESS);
246                 return FALSE;
247         }
248
249         if (!check_range (priv->forward_delay,
250                           BR_MIN_FORWARD_DELAY,
251                           BR_MAX_FORWARD_DELAY,
252                           !priv->stp,
253                           NM_SETTING_BRIDGE_FORWARD_DELAY,
254                           error))
255                 return FALSE;
256
257         if (!check_range (priv->hello_time,
258                           BR_MIN_HELLO_TIME,
259                           BR_MAX_HELLO_TIME,
260                           !priv->stp,
261                           NM_SETTING_BRIDGE_HELLO_TIME,
262                           error))
263                 return FALSE;
264
265         if (!check_range (priv->max_age,
266                           BR_MIN_MAX_AGE,
267                           BR_MAX_MAX_AGE,
268                           !priv->stp,
269                           NM_SETTING_BRIDGE_MAX_AGE,
270                           error))
271                 return FALSE;
272
273         if (!check_range (priv->ageing_time,
274                           BR_MIN_AGEING_TIME,
275                           BR_MAX_AGEING_TIME,
276                           !priv->stp,
277                           NM_SETTING_BRIDGE_AGEING_TIME,
278                           error))
279                 return FALSE;
280
281         return _nm_connection_verify_required_interface_name (connection, error);
282 }
283
284 static void
285 nm_setting_bridge_init (NMSettingBridge *setting)
286 {
287 }
288
289 static void
290 finalize (GObject *object)
291 {
292         NMSettingBridgePrivate *priv = NM_SETTING_BRIDGE_GET_PRIVATE (object);
293
294         g_free (priv->mac_address);
295
296         G_OBJECT_CLASS (nm_setting_bridge_parent_class)->finalize (object);
297 }
298
299 static void
300 set_property (GObject *object, guint prop_id,
301               const GValue *value, GParamSpec *pspec)
302 {
303         NMSettingBridgePrivate *priv = NM_SETTING_BRIDGE_GET_PRIVATE (object);
304
305         switch (prop_id) {
306         case PROP_MAC_ADDRESS:
307                 g_free (priv->mac_address);
308                 priv->mac_address = _nm_utils_hwaddr_canonical_or_invalid (g_value_get_string (value),
309                                                                            ETH_ALEN);
310                 break;
311         case PROP_STP:
312                 priv->stp = g_value_get_boolean (value);
313                 break;
314         case PROP_PRIORITY:
315                 priv->priority = (guint16) g_value_get_uint (value);
316                 break;
317         case PROP_FORWARD_DELAY:
318                 priv->forward_delay = (guint16) g_value_get_uint (value);
319                 break;
320         case PROP_HELLO_TIME:
321                 priv->hello_time = (guint16) g_value_get_uint (value);
322                 break;
323         case PROP_MAX_AGE:
324                 priv->max_age = (guint16) g_value_get_uint (value);
325                 break;
326         case PROP_AGEING_TIME:
327                 priv->ageing_time = g_value_get_uint (value);
328                 break;
329         case PROP_MULTICAST_SNOOPING:
330                 priv->multicast_snooping = g_value_get_boolean (value);
331                 break;
332         default:
333                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
334                 break;
335         }
336 }
337
338 static void
339 get_property (GObject *object, guint prop_id,
340               GValue *value, GParamSpec *pspec)
341 {
342         NMSettingBridgePrivate *priv = NM_SETTING_BRIDGE_GET_PRIVATE (object);
343         NMSettingBridge *setting = NM_SETTING_BRIDGE (object);
344
345         switch (prop_id) {
346         case PROP_MAC_ADDRESS:
347                 g_value_set_string (value, nm_setting_bridge_get_mac_address (setting));
348                 break;
349         case PROP_STP:
350                 g_value_set_boolean (value, priv->stp);
351                 break;
352         case PROP_PRIORITY:
353                 g_value_set_uint (value, priv->priority);
354                 break;
355         case PROP_FORWARD_DELAY:
356                 g_value_set_uint (value, priv->forward_delay);
357                 break;
358         case PROP_HELLO_TIME:
359                 g_value_set_uint (value, priv->hello_time);
360                 break;
361         case PROP_MAX_AGE:
362                 g_value_set_uint (value, priv->max_age);
363                 break;
364         case PROP_AGEING_TIME:
365                 g_value_set_uint (value, priv->ageing_time);
366                 break;
367         case PROP_MULTICAST_SNOOPING:
368                 g_value_set_boolean (value, priv->multicast_snooping);
369                 break;
370         default:
371                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
372                 break;
373         }
374 }
375
376 static void
377 nm_setting_bridge_class_init (NMSettingBridgeClass *setting_class)
378 {
379         GObjectClass *object_class = G_OBJECT_CLASS (setting_class);
380         NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class);
381
382         g_type_class_add_private (setting_class, sizeof (NMSettingBridgePrivate));
383
384         /* virtual methods */
385         object_class->set_property = set_property;
386         object_class->get_property = get_property;
387         object_class->finalize     = finalize;
388         parent_class->verify       = verify;
389
390         /* Properties */
391         /**
392          * NMSettingBridge:mac-address:
393          *
394          * If specified, the MAC address of bridge. When creating a new bridge, this
395          * MAC address will be set. When matching an existing (outside
396          * NetworkManager created) bridge, this MAC address must match.
397          **/
398         /* ---keyfile---
399          * property: mac-address
400          * format: ususal hex-digits-and-colons notation
401          * description: MAC address in traditional hex-digits-and-colons notation,
402          *   or semicolon separated list of 6 decimal bytes (obsolete)
403          * example: mac-address=00:22:68:12:79:A2
404          *  mac-address=0;34;104;18;121;162;
405          * ---end---
406          * ---ifcfg-rh---
407          * property: mac-address
408          * variable: MACADDR(+)
409          * description: MAC address of the bridge. Note that this requires a recent
410          *   kernel support, originally introduced in 3.15 upstream kernel)
411          *   MACADDR for bridges is an NM extension.
412          * ---end---
413          */
414         g_object_class_install_property
415                 (object_class, PROP_MAC_ADDRESS,
416                  g_param_spec_string (NM_SETTING_BRIDGE_MAC_ADDRESS, "", "",
417                                       NULL,
418                                       G_PARAM_READWRITE |
419                                       NM_SETTING_PARAM_INFERRABLE |
420                                       G_PARAM_STATIC_STRINGS));
421         _nm_setting_class_transform_property (parent_class, NM_SETTING_BRIDGE_MAC_ADDRESS,
422                                               G_VARIANT_TYPE_BYTESTRING,
423                                               _nm_utils_hwaddr_to_dbus,
424                                               _nm_utils_hwaddr_from_dbus);
425
426         /**
427          * NMSettingBridge:stp:
428          *
429          * Controls whether Spanning Tree Protocol (STP) is enabled for this bridge.
430          **/
431         /* ---ifcfg-rh---
432          * property: stp
433          * variable: STP
434          * default: no
435          * description: Span tree protocol participation.
436          * ---end---
437          */
438         g_object_class_install_property
439                 (object_class, PROP_STP,
440                  g_param_spec_boolean (NM_SETTING_BRIDGE_STP, "", "",
441                                        TRUE,
442                                        G_PARAM_READWRITE |
443                                        G_PARAM_CONSTRUCT |
444                                        NM_SETTING_PARAM_INFERRABLE |
445                                        G_PARAM_STATIC_STRINGS));
446
447         /**
448          * NMSettingBridge:priority:
449          *
450          * Sets the Spanning Tree Protocol (STP) priority for this bridge.  Lower
451          * values are "better"; the lowest priority bridge will be elected the root
452          * bridge.
453          **/
454         /* ---ifcfg-rh---
455          * property: priority
456          * variable: BRIDGING_OPTS: priority=
457          * values: 0 - 32768
458          * default: 32768
459          * description: STP priority.
460          * ---end---
461          */
462         g_object_class_install_property
463                 (object_class, PROP_PRIORITY,
464                  g_param_spec_uint (NM_SETTING_BRIDGE_PRIORITY, "", "",
465                                     0, G_MAXUINT16, 0x8000,
466                                     G_PARAM_READWRITE |
467                                     G_PARAM_CONSTRUCT |
468                                     NM_SETTING_PARAM_INFERRABLE |
469                                     G_PARAM_STATIC_STRINGS));
470
471         /**
472          * NMSettingBridge:forward-delay:
473          *
474          * The Spanning Tree Protocol (STP) forwarding delay, in seconds.
475          **/
476         /* ---ifcfg-rh---
477          * property: forward-delay
478          * variable: DELAY
479          * values: 2 - 30
480          * default: 15
481          * description: STP forwarding delay.
482          * ---end---
483          */
484         g_object_class_install_property
485                 (object_class, PROP_FORWARD_DELAY,
486                  g_param_spec_uint (NM_SETTING_BRIDGE_FORWARD_DELAY, "", "",
487                                     0, BR_MAX_FORWARD_DELAY, 15,
488                                     G_PARAM_READWRITE |
489                                     G_PARAM_CONSTRUCT |
490                                     NM_SETTING_PARAM_INFERRABLE |
491                                     G_PARAM_STATIC_STRINGS));
492
493         /**
494          * NMSettingBridge:hello-time:
495          *
496          * The Spanning Tree Protocol (STP) hello time, in seconds.
497          **/
498         /* ---ifcfg-rh---
499          * property: hello-time
500          * variable: BRIDGING_OPTS: hello_time=
501          * values: 1 - 10
502          * default: 2
503          * description: STP hello time.
504          * ---end---
505          */
506         g_object_class_install_property
507                 (object_class, PROP_HELLO_TIME,
508                  g_param_spec_uint (NM_SETTING_BRIDGE_HELLO_TIME, "", "",
509                                     0, BR_MAX_HELLO_TIME, 2,
510                                     G_PARAM_READWRITE |
511                                     G_PARAM_CONSTRUCT |
512                                     NM_SETTING_PARAM_INFERRABLE |
513                                     G_PARAM_STATIC_STRINGS));
514
515         /**
516          * NMSettingBridge:max-age:
517          *
518          * The Spanning Tree Protocol (STP) maximum message age, in seconds.
519          **/
520         /* ---ifcfg-rh---
521          * property: max-age
522          * variable: BRIDGING_OPTS: max_age=
523          * values: 6 - 40
524          * default: 20
525          * description: STP maximum message age.
526          * ---end---
527          */
528         g_object_class_install_property
529                 (object_class, PROP_MAX_AGE,
530                  g_param_spec_uint (NM_SETTING_BRIDGE_MAX_AGE, "", "",
531                                     0, BR_MAX_MAX_AGE, 20,
532                                     G_PARAM_READWRITE |
533                                     G_PARAM_CONSTRUCT |
534                                     NM_SETTING_PARAM_INFERRABLE |
535                                     G_PARAM_STATIC_STRINGS));
536
537         /**
538          * NMSettingBridge:ageing-time:
539          *
540          * The Ethernet MAC address aging time, in seconds.
541          **/
542         /* ---ifcfg-rh---
543          * property: ageing-time
544          * variable: BRIDGING_OPTS: ageing_time=
545          * values: 0 - 1000000
546          * default: 300
547          * description: Ethernet MAC ageing time.
548          * ---end---
549          */
550         g_object_class_install_property
551                 (object_class, PROP_AGEING_TIME,
552                  g_param_spec_uint (NM_SETTING_BRIDGE_AGEING_TIME, "", "",
553                                     0, BR_MAX_AGEING_TIME, 300,
554                                     G_PARAM_READWRITE |
555                                     G_PARAM_CONSTRUCT |
556                                     NM_SETTING_PARAM_INFERRABLE |
557                                     G_PARAM_STATIC_STRINGS));
558
559         /**
560          * NMSettingBridge:multicast-snooping:
561          *
562          * Controls whether IGMP snooping is enabled for this bridge.
563          * Note that if snooping was automatically disabled due to hash collisions,
564          * the system may refuse to enable the feature until the collisions are
565          * resolved.
566          *
567          * Since: 1.2
568          **/
569         /* ---ifcfg-rh---
570          * property: multicast-snooping
571          * variable: BRIDGING_OPTS: multicast_snooping=
572          * values: 0 or 1
573          * default: 1
574          * description: IGMP snooping support.
575          * ---end---
576          */
577         g_object_class_install_property
578                 (object_class, PROP_MULTICAST_SNOOPING,
579                  g_param_spec_boolean (NM_SETTING_BRIDGE_MULTICAST_SNOOPING, "", "",
580                                        TRUE,
581                                        G_PARAM_READWRITE |
582                                        G_PARAM_CONSTRUCT |
583                                        NM_SETTING_PARAM_INFERRABLE |
584                                        G_PARAM_STATIC_STRINGS));
585
586         /* ---dbus---
587          * property: interface-name
588          * format: string
589          * description: Deprecated in favor of connection.interface-name, but can
590          *   be used for backward-compatibility with older daemons, to set the
591          *   bridge's interface name.
592          * ---end---
593          */
594         _nm_setting_class_add_dbus_only_property (parent_class, "interface-name",
595                                                   G_VARIANT_TYPE_STRING,
596                                                   _nm_setting_get_deprecated_virtual_interface_name,
597                                                   NULL);
598 }