]>
Commit | Line | Data |
---|---|---|
6235b3de TG |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2014 Tom Gundersen <teg@jklm.no> | |
5 | Copyright 2014 Susant Sahani | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
20 | ||
540eb5f0 | 21 | #include <net/if.h> |
6235b3de | 22 | |
6235b3de | 23 | #include "missing.h" |
540eb5f0 | 24 | #include "netlink-util.h" |
441e9ae4 | 25 | #include "netdev/bridge.h" |
23f53b99 | 26 | #include "networkd-manager.h" |
0d6c68eb | 27 | #include "vlan-util.h" |
540eb5f0 SS |
28 | |
29 | /* callback for brige netdev's parameter set */ | |
30 | static int netdev_bridge_set_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { | |
31 | _cleanup_netdev_unref_ NetDev *netdev = userdata; | |
32 | int r; | |
33 | ||
34 | assert(netdev); | |
35 | assert(m); | |
36 | ||
37 | r = sd_netlink_message_get_errno(m); | |
38 | if (r < 0) { | |
39 | log_netdev_warning_errno(netdev, r, "Bridge parameters could not be set: %m"); | |
40 | return 1; | |
41 | } | |
42 | ||
82936769 | 43 | log_netdev_debug(netdev, "Bridge parameters set success"); |
540eb5f0 SS |
44 | |
45 | return 1; | |
46 | } | |
47 | ||
48 | static int netdev_bridge_post_create(NetDev *netdev, Link *link, sd_netlink_message *m) { | |
4afd3348 | 49 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
540eb5f0 SS |
50 | Bridge *b; |
51 | int r; | |
52 | ||
53 | assert(netdev); | |
54 | ||
55 | b = BRIDGE(netdev); | |
56 | ||
57 | assert(b); | |
58 | ||
59 | r = sd_rtnl_message_new_link(netdev->manager->rtnl, &req, RTM_NEWLINK, netdev->ifindex); | |
60 | if (r < 0) | |
61 | return log_netdev_error_errno(netdev, r, "Could not allocate RTM_SETLINK message: %m"); | |
62 | ||
63 | r = sd_netlink_message_set_flags(req, NLM_F_REQUEST | NLM_F_ACK); | |
64 | if (r < 0) | |
65 | return log_link_error_errno(link, r, "Could not set netlink flags: %m"); | |
66 | ||
67 | r = sd_netlink_message_open_container(req, IFLA_LINKINFO); | |
68 | if (r < 0) | |
69 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_PROTINFO attribute: %m"); | |
70 | ||
71 | r = sd_netlink_message_open_container_union(req, IFLA_INFO_DATA, netdev_kind_to_string(netdev->kind)); | |
72 | if (r < 0) | |
73 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_INFO_DATA attribute: %m"); | |
74 | ||
1a14863e | 75 | /* convert to jiffes */ |
730389b6 | 76 | if (b->forward_delay != USEC_INFINITY) { |
1a14863e | 77 | r = sd_netlink_message_append_u32(req, IFLA_BR_FORWARD_DELAY, usec_to_jiffies(b->forward_delay)); |
540eb5f0 SS |
78 | if (r < 0) |
79 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_FORWARD_DELAY attribute: %m"); | |
80 | } | |
81 | ||
82 | if (b->hello_time > 0) { | |
1a14863e | 83 | r = sd_netlink_message_append_u32(req, IFLA_BR_HELLO_TIME, usec_to_jiffies(b->hello_time)); |
540eb5f0 SS |
84 | if (r < 0) |
85 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_HELLO_TIME attribute: %m"); | |
86 | } | |
87 | ||
88 | if (b->max_age > 0) { | |
1a14863e | 89 | r = sd_netlink_message_append_u32(req, IFLA_BR_MAX_AGE, usec_to_jiffies(b->max_age)); |
540eb5f0 SS |
90 | if (r < 0) |
91 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_MAX_AGE attribute: %m"); | |
92 | } | |
93 | ||
c7440e74 TJ |
94 | if (b->ageing_time > 0) { |
95 | r = sd_netlink_message_append_u32(req, IFLA_BR_AGEING_TIME, usec_to_jiffies(b->ageing_time)); | |
96 | if (r < 0) | |
97 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_AGEING_TIME attribute: %m"); | |
98 | } | |
99 | ||
100 | if (b->priority > 0) { | |
101 | r = sd_netlink_message_append_u16(req, IFLA_BR_PRIORITY, b->priority); | |
102 | if (r < 0) | |
103 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_PRIORITY attribute: %m"); | |
104 | } | |
105 | ||
c4819961 JC |
106 | if (b->group_fwd_mask > 0) { |
107 | r = sd_netlink_message_append_u16(req, IFLA_BR_GROUP_FWD_MASK, b->group_fwd_mask); | |
108 | if (r < 0) | |
109 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_GROUP_FWD_MASK attribute: %m"); | |
110 | } | |
111 | ||
0d6c68eb | 112 | if (b->default_pvid != VLANID_INVALID) { |
c7440e74 TJ |
113 | r = sd_netlink_message_append_u16(req, IFLA_BR_VLAN_DEFAULT_PVID, b->default_pvid); |
114 | if (r < 0) | |
115 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_VLAN_DEFAULT_PVID attribute: %m"); | |
116 | } | |
117 | ||
3fef7a3f SS |
118 | if (b->mcast_querier >= 0) { |
119 | r = sd_netlink_message_append_u8(req, IFLA_BR_MCAST_QUERIER, b->mcast_querier); | |
120 | if (r < 0) | |
121 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_MCAST_QUERIER attribute: %m"); | |
122 | } | |
123 | ||
6df6d898 SS |
124 | if (b->mcast_snooping >= 0) { |
125 | r = sd_netlink_message_append_u8(req, IFLA_BR_MCAST_SNOOPING, b->mcast_snooping); | |
126 | if (r < 0) | |
127 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_MCAST_SNOOPING attribute: %m"); | |
128 | } | |
129 | ||
c6f8d17d TJ |
130 | if (b->vlan_filtering >= 0) { |
131 | r = sd_netlink_message_append_u8(req, IFLA_BR_VLAN_FILTERING, b->vlan_filtering); | |
132 | if (r < 0) | |
133 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_VLAN_FILTERING attribute: %m"); | |
134 | } | |
135 | ||
b760a9af SS |
136 | if (b->stp >= 0) { |
137 | r = sd_netlink_message_append_u32(req, IFLA_BR_STP_STATE, b->stp); | |
138 | if (r < 0) | |
139 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_STP_STATE attribute: %m"); | |
140 | } | |
141 | ||
540eb5f0 SS |
142 | r = sd_netlink_message_close_container(req); |
143 | if (r < 0) | |
144 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_LINKINFO attribute: %m"); | |
145 | ||
146 | r = sd_netlink_message_close_container(req); | |
147 | if (r < 0) | |
148 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_INFO_DATA attribute: %m"); | |
149 | ||
150 | r = sd_netlink_call_async(netdev->manager->rtnl, req, netdev_bridge_set_handler, netdev, 0, NULL); | |
151 | if (r < 0) | |
152 | return log_netdev_error_errno(netdev, r, "Could not send rtnetlink message: %m"); | |
153 | ||
154 | netdev_ref(netdev); | |
155 | ||
156 | return r; | |
157 | } | |
6235b3de | 158 | |
3fef7a3f SS |
159 | static void bridge_init(NetDev *n) { |
160 | Bridge *b; | |
161 | ||
162 | b = BRIDGE(n); | |
163 | ||
164 | assert(b); | |
165 | ||
166 | b->mcast_querier = -1; | |
6df6d898 | 167 | b->mcast_snooping = -1; |
c6f8d17d | 168 | b->vlan_filtering = -1; |
b760a9af | 169 | b->stp = -1; |
0d6c68eb | 170 | b->default_pvid = VLANID_INVALID; |
730389b6 | 171 | b->forward_delay = USEC_INFINITY; |
3fef7a3f SS |
172 | } |
173 | ||
3be1d7e0 | 174 | const NetDevVTable bridge_vtable = { |
aa9f1140 | 175 | .object_size = sizeof(Bridge), |
3fef7a3f | 176 | .init = bridge_init, |
540eb5f0 SS |
177 | .sections = "Match\0NetDev\0Bridge\0", |
178 | .post_create = netdev_bridge_post_create, | |
aa9f1140 | 179 | .create_type = NETDEV_CREATE_MASTER, |
3be1d7e0 | 180 | }; |