device: renew dhcp leases on awake for software devices
[NetworkManager.git] / libnm-util / nm-setting-bridge-port.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 2012 - 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 #include <dbus/dbus-glib.h>
28
29 #include "nm-setting-bridge-port.h"
30 #include "nm-utils.h"
31 #include "nm-utils-private.h"
32 #include "nm-setting-private.h"
33
34 /**
35  * SECTION:nm-setting-bridge-port
36  * @short_description: Describes connection properties for bridge ports
37  * @include: nm-setting-bridge-port.h
38  *
39  * The #NMSettingBridgePort object is a #NMSetting subclass that describes
40  * optional properties that apply to bridge ports.
41  *
42  * Since: 0.9.8
43  **/
44
45 /**
46  * nm_setting_bridge_port_error_quark:
47  *
48  * Registers an error quark for #NMSettingBridgePort if necessary.
49  *
50  * Returns: the error quark used for #NMSettingBridgePort errors.
51  *
52  * Since: 0.9.8
53  **/
54 GQuark
55 nm_setting_bridge_port_error_quark (void)
56 {
57         static GQuark quark;
58
59         if (G_UNLIKELY (!quark))
60                 quark = g_quark_from_static_string ("nm-setting-bridge-port-error-quark");
61         return quark;
62 }
63
64 G_DEFINE_TYPE_WITH_CODE (NMSettingBridgePort, nm_setting_bridge_port, NM_TYPE_SETTING,
65                          _nm_register_setting (NM_SETTING_BRIDGE_PORT_SETTING_NAME,
66                                                g_define_type_id,
67                                                3,
68                                                NM_SETTING_BRIDGE_PORT_ERROR))
69 NM_SETTING_REGISTER_TYPE (NM_TYPE_SETTING_BRIDGE_PORT)
70
71 #define NM_SETTING_BRIDGE_PORT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_SETTING_BRIDGE_PORT, NMSettingBridgePortPrivate))
72
73 typedef struct {
74         guint16 priority;
75         guint16 path_cost;
76         gboolean hairpin_mode;
77 } NMSettingBridgePortPrivate;
78
79 enum {
80         PROP_0,
81         PROP_PRIORITY,
82         PROP_PATH_COST,
83         PROP_HAIRPIN_MODE,
84         LAST_PROP
85 };
86
87 /**************************************************************************/
88
89 /**
90  * nm_setting_bridge_port_get_priority:
91  * @setting: the #NMSettingBridgePort
92  *
93  * Returns: the #NMSettingBridgePort:priority property of the setting
94  *
95  * Since: 0.9.8
96  **/
97 guint16
98 nm_setting_bridge_port_get_priority (NMSettingBridgePort *setting)
99 {
100         g_return_val_if_fail (NM_IS_SETTING_BRIDGE_PORT (setting), 0);
101
102         return NM_SETTING_BRIDGE_PORT_GET_PRIVATE (setting)->priority;
103 }
104
105 /**
106  * nm_setting_bridge_port_get_path_cost:
107  * @setting: the #NMSettingBridgePort
108  *
109  * Returns: the #NMSettingBridgePort:path-cost property of the setting
110  *
111  * Since: 0.9.8
112  **/
113 guint16
114 nm_setting_bridge_port_get_path_cost (NMSettingBridgePort *setting)
115 {
116         g_return_val_if_fail (NM_IS_SETTING_BRIDGE_PORT (setting), 0);
117
118         return NM_SETTING_BRIDGE_PORT_GET_PRIVATE (setting)->path_cost;
119 }
120
121 /**
122  * nm_setting_bridge_port_get_hairpin_mode:
123  * @setting: the #NMSettingBridgePort
124  *
125  * Returns: the #NMSettingBridgePort:hairpin-mode property of the setting
126  *
127  * Since: 0.9.8
128  **/
129 gboolean
130 nm_setting_bridge_port_get_hairpin_mode (NMSettingBridgePort *setting)
131 {
132         g_return_val_if_fail (NM_IS_SETTING_BRIDGE_PORT (setting), FALSE);
133
134         return NM_SETTING_BRIDGE_PORT_GET_PRIVATE (setting)->hairpin_mode;
135 }
136
137 /**************************************************************************/
138
139 #define BR_MAX_PORT_PRIORITY 63
140 #define BR_DEF_PRIORITY      32
141
142 #define BR_MIN_PATH_COST     1
143 #define BR_MAX_PATH_COST     65535
144
145 static gboolean
146 verify (NMSetting *setting, GSList *all_settings, GError **error)
147 {
148         NMSettingBridgePortPrivate *priv = NM_SETTING_BRIDGE_PORT_GET_PRIVATE (setting);
149
150         if (priv->priority > BR_MAX_PORT_PRIORITY) {
151                 g_set_error (error,
152                              NM_SETTING_BRIDGE_PORT_ERROR,
153                              NM_SETTING_BRIDGE_PORT_ERROR_INVALID_PROPERTY,
154                              _("'%d' is not a valid value for the property (should be <= %d)"),
155                              priv->priority, BR_MAX_PORT_PRIORITY);
156                 g_prefix_error (error, "%s.%s: ",
157                                 NM_SETTING_BRIDGE_PORT_SETTING_NAME,
158                                 NM_SETTING_BRIDGE_PORT_PRIORITY);
159                 return FALSE;
160         }
161
162         if (priv->path_cost > BR_MAX_PATH_COST) {
163                 g_set_error (error,
164                              NM_SETTING_BRIDGE_PORT_ERROR,
165                              NM_SETTING_BRIDGE_PORT_ERROR_INVALID_PROPERTY,
166                              _("'%d' is not a valid value for the property (should be <= %d)"),
167                              priv->path_cost, BR_MAX_PATH_COST);
168                 g_prefix_error (error, "%s.%s: ",
169                                 NM_SETTING_BRIDGE_PORT_SETTING_NAME,
170                                 NM_SETTING_BRIDGE_PORT_PATH_COST);
171                 return FALSE;
172         }
173
174         return TRUE;
175 }
176
177 /**************************************************************************/
178
179 /**
180  * nm_setting_bridge_port_new:
181  *
182  * Creates a new #NMSettingBridgePort object with default values.
183  *
184  * Returns: (transfer full): the new empty #NMSettingBridgePort object
185  *
186  * Since: 0.9.8
187  **/
188 NMSetting *
189 nm_setting_bridge_port_new (void)
190 {
191         return (NMSetting *) g_object_new (NM_TYPE_SETTING_BRIDGE_PORT, NULL);
192 }
193
194 static void
195 nm_setting_bridge_port_init (NMSettingBridgePort *setting)
196 {
197 }
198
199 static void
200 set_property (GObject *object, guint prop_id,
201               const GValue *value, GParamSpec *pspec)
202 {
203         NMSettingBridgePortPrivate *priv = NM_SETTING_BRIDGE_PORT_GET_PRIVATE (object);
204
205         switch (prop_id) {
206         case PROP_PRIORITY:
207                 priv->priority = (guint16) (g_value_get_uint (value) & 0xFFFF);
208                 break;
209         case PROP_PATH_COST:
210                 priv->path_cost = (guint16) (g_value_get_uint (value) & 0xFFFF);
211                 break;
212         case PROP_HAIRPIN_MODE:
213                 priv->hairpin_mode = g_value_get_boolean (value);
214                 break;
215         default:
216                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
217                 break;
218         }
219 }
220
221 static void
222 get_property (GObject *object, guint prop_id,
223               GValue *value, GParamSpec *pspec)
224 {
225         NMSettingBridgePortPrivate *priv = NM_SETTING_BRIDGE_PORT_GET_PRIVATE (object);
226
227         switch (prop_id) {
228         case PROP_PRIORITY:
229                 g_value_set_uint (value, priv->priority);
230                 break;
231         case PROP_PATH_COST:
232                 g_value_set_uint (value, priv->path_cost);
233                 break;
234         case PROP_HAIRPIN_MODE:
235                 g_value_set_boolean (value, priv->hairpin_mode);
236                 break;
237         default:
238                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
239                 break;
240         }
241 }
242
243 static void
244 nm_setting_bridge_port_class_init (NMSettingBridgePortClass *setting_class)
245 {
246         GObjectClass *object_class = G_OBJECT_CLASS (setting_class);
247         NMSettingClass *parent_class = NM_SETTING_CLASS (setting_class);
248
249         g_type_class_add_private (setting_class, sizeof (NMSettingBridgePortPrivate));
250
251         /* virtual methods */
252         object_class->set_property = set_property;
253         object_class->get_property = get_property;
254         parent_class->verify       = verify;
255
256         /* Properties */
257         /**
258          * NMSettingBridgePort:priority:
259          *
260          * The Spanning Tree Protocol (STP) priority of this bridge port.
261          *
262          * Since: 0.9.8
263          **/
264         g_object_class_install_property
265                 (object_class, PROP_PRIORITY,
266                  g_param_spec_uint (NM_SETTING_BRIDGE_PORT_PRIORITY, "", "",
267                                     0, BR_MAX_PORT_PRIORITY, BR_DEF_PRIORITY,
268                                     G_PARAM_READWRITE |
269                                     G_PARAM_CONSTRUCT |
270                                     NM_SETTING_PARAM_INFERRABLE |
271                                     G_PARAM_STATIC_STRINGS));
272
273         /**
274          * NMSettingBridgePort:path-cost:
275          *
276          * The Spanning Tree Protocol (STP) port cost for destinations via this
277          * port.
278          *
279          * Since: 0.9.8
280          **/
281         g_object_class_install_property
282                 (object_class, PROP_PATH_COST,
283                  g_param_spec_uint (NM_SETTING_BRIDGE_PORT_PATH_COST, "", "",
284                                     0, BR_MAX_PATH_COST, 100,
285                                     G_PARAM_READWRITE |
286                                     G_PARAM_CONSTRUCT |
287                                     NM_SETTING_PARAM_INFERRABLE |
288                                     G_PARAM_STATIC_STRINGS));
289
290         /**
291          * NMSettingBridgePort:hairpin-mode:
292          *
293          * Enables or disabled "hairpin mode" for the port, which allows frames to
294          * be sent back out through the port the frame was received on.
295          *
296          * Since: 0.9.8
297          **/
298         g_object_class_install_property
299                 (object_class, PROP_HAIRPIN_MODE,
300                  g_param_spec_boolean (NM_SETTING_BRIDGE_PORT_HAIRPIN_MODE, "", "",
301                                        FALSE,
302                                        G_PARAM_READWRITE |
303                                        NM_SETTING_PARAM_INFERRABLE |
304                                        G_PARAM_STATIC_STRINGS));
305 }