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