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