]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
6235b3de | 2 | |
540eb5f0 | 3 | #include <net/if.h> |
6235b3de | 4 | |
6235b3de | 5 | #include "missing.h" |
540eb5f0 | 6 | #include "netlink-util.h" |
441e9ae4 | 7 | #include "netdev/bridge.h" |
23f53b99 | 8 | #include "networkd-manager.h" |
0d6c68eb | 9 | #include "vlan-util.h" |
540eb5f0 SS |
10 | |
11 | /* callback for brige netdev's parameter set */ | |
302a796f | 12 | static int netdev_bridge_set_handler(sd_netlink *rtnl, sd_netlink_message *m, NetDev *netdev) { |
540eb5f0 SS |
13 | int r; |
14 | ||
15 | assert(netdev); | |
16 | assert(m); | |
17 | ||
18 | r = sd_netlink_message_get_errno(m); | |
19 | if (r < 0) { | |
20 | log_netdev_warning_errno(netdev, r, "Bridge parameters could not be set: %m"); | |
21 | return 1; | |
22 | } | |
23 | ||
82936769 | 24 | log_netdev_debug(netdev, "Bridge parameters set success"); |
540eb5f0 SS |
25 | |
26 | return 1; | |
27 | } | |
28 | ||
29 | static int netdev_bridge_post_create(NetDev *netdev, Link *link, sd_netlink_message *m) { | |
4afd3348 | 30 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
540eb5f0 SS |
31 | Bridge *b; |
32 | int r; | |
33 | ||
34 | assert(netdev); | |
35 | ||
36 | b = BRIDGE(netdev); | |
37 | ||
38 | assert(b); | |
39 | ||
40 | r = sd_rtnl_message_new_link(netdev->manager->rtnl, &req, RTM_NEWLINK, netdev->ifindex); | |
41 | if (r < 0) | |
42 | return log_netdev_error_errno(netdev, r, "Could not allocate RTM_SETLINK message: %m"); | |
43 | ||
44 | r = sd_netlink_message_set_flags(req, NLM_F_REQUEST | NLM_F_ACK); | |
45 | if (r < 0) | |
46 | return log_link_error_errno(link, r, "Could not set netlink flags: %m"); | |
47 | ||
48 | r = sd_netlink_message_open_container(req, IFLA_LINKINFO); | |
49 | if (r < 0) | |
15cdaeee | 50 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_LINKINFO attribute: %m"); |
540eb5f0 SS |
51 | |
52 | r = sd_netlink_message_open_container_union(req, IFLA_INFO_DATA, netdev_kind_to_string(netdev->kind)); | |
53 | if (r < 0) | |
54 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_INFO_DATA attribute: %m"); | |
55 | ||
1a14863e | 56 | /* convert to jiffes */ |
730389b6 | 57 | if (b->forward_delay != USEC_INFINITY) { |
1a14863e | 58 | r = sd_netlink_message_append_u32(req, IFLA_BR_FORWARD_DELAY, usec_to_jiffies(b->forward_delay)); |
540eb5f0 SS |
59 | if (r < 0) |
60 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_FORWARD_DELAY attribute: %m"); | |
61 | } | |
62 | ||
63 | if (b->hello_time > 0) { | |
1a14863e | 64 | r = sd_netlink_message_append_u32(req, IFLA_BR_HELLO_TIME, usec_to_jiffies(b->hello_time)); |
540eb5f0 SS |
65 | if (r < 0) |
66 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_HELLO_TIME attribute: %m"); | |
67 | } | |
68 | ||
69 | if (b->max_age > 0) { | |
1a14863e | 70 | r = sd_netlink_message_append_u32(req, IFLA_BR_MAX_AGE, usec_to_jiffies(b->max_age)); |
540eb5f0 SS |
71 | if (r < 0) |
72 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_MAX_AGE attribute: %m"); | |
73 | } | |
74 | ||
0da81203 | 75 | if (b->ageing_time != USEC_INFINITY) { |
c7440e74 TJ |
76 | r = sd_netlink_message_append_u32(req, IFLA_BR_AGEING_TIME, usec_to_jiffies(b->ageing_time)); |
77 | if (r < 0) | |
78 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_AGEING_TIME attribute: %m"); | |
79 | } | |
80 | ||
81 | if (b->priority > 0) { | |
82 | r = sd_netlink_message_append_u16(req, IFLA_BR_PRIORITY, b->priority); | |
83 | if (r < 0) | |
84 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_PRIORITY attribute: %m"); | |
85 | } | |
86 | ||
c4819961 JC |
87 | if (b->group_fwd_mask > 0) { |
88 | r = sd_netlink_message_append_u16(req, IFLA_BR_GROUP_FWD_MASK, b->group_fwd_mask); | |
89 | if (r < 0) | |
90 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_GROUP_FWD_MASK attribute: %m"); | |
91 | } | |
92 | ||
0d6c68eb | 93 | if (b->default_pvid != VLANID_INVALID) { |
c7440e74 TJ |
94 | r = sd_netlink_message_append_u16(req, IFLA_BR_VLAN_DEFAULT_PVID, b->default_pvid); |
95 | if (r < 0) | |
96 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_VLAN_DEFAULT_PVID attribute: %m"); | |
97 | } | |
98 | ||
3fef7a3f SS |
99 | if (b->mcast_querier >= 0) { |
100 | r = sd_netlink_message_append_u8(req, IFLA_BR_MCAST_QUERIER, b->mcast_querier); | |
101 | if (r < 0) | |
102 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_MCAST_QUERIER attribute: %m"); | |
103 | } | |
104 | ||
6df6d898 SS |
105 | if (b->mcast_snooping >= 0) { |
106 | r = sd_netlink_message_append_u8(req, IFLA_BR_MCAST_SNOOPING, b->mcast_snooping); | |
107 | if (r < 0) | |
108 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_MCAST_SNOOPING attribute: %m"); | |
109 | } | |
110 | ||
c6f8d17d TJ |
111 | if (b->vlan_filtering >= 0) { |
112 | r = sd_netlink_message_append_u8(req, IFLA_BR_VLAN_FILTERING, b->vlan_filtering); | |
113 | if (r < 0) | |
114 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_VLAN_FILTERING attribute: %m"); | |
115 | } | |
116 | ||
b760a9af SS |
117 | if (b->stp >= 0) { |
118 | r = sd_netlink_message_append_u32(req, IFLA_BR_STP_STATE, b->stp); | |
119 | if (r < 0) | |
120 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_STP_STATE attribute: %m"); | |
121 | } | |
122 | ||
540eb5f0 SS |
123 | r = sd_netlink_message_close_container(req); |
124 | if (r < 0) | |
125 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_LINKINFO attribute: %m"); | |
126 | ||
127 | r = sd_netlink_message_close_container(req); | |
128 | if (r < 0) | |
129 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_INFO_DATA attribute: %m"); | |
130 | ||
302a796f YW |
131 | r = netlink_call_async(netdev->manager->rtnl, NULL, req, netdev_bridge_set_handler, |
132 | netdev_destroy_callback, netdev); | |
540eb5f0 SS |
133 | if (r < 0) |
134 | return log_netdev_error_errno(netdev, r, "Could not send rtnetlink message: %m"); | |
135 | ||
136 | netdev_ref(netdev); | |
137 | ||
138 | return r; | |
139 | } | |
6235b3de | 140 | |
3fef7a3f SS |
141 | static void bridge_init(NetDev *n) { |
142 | Bridge *b; | |
143 | ||
144 | b = BRIDGE(n); | |
145 | ||
146 | assert(b); | |
147 | ||
148 | b->mcast_querier = -1; | |
6df6d898 | 149 | b->mcast_snooping = -1; |
c6f8d17d | 150 | b->vlan_filtering = -1; |
b760a9af | 151 | b->stp = -1; |
0d6c68eb | 152 | b->default_pvid = VLANID_INVALID; |
730389b6 | 153 | b->forward_delay = USEC_INFINITY; |
0da81203 | 154 | b->ageing_time = USEC_INFINITY; |
3fef7a3f SS |
155 | } |
156 | ||
3be1d7e0 | 157 | const NetDevVTable bridge_vtable = { |
aa9f1140 | 158 | .object_size = sizeof(Bridge), |
3fef7a3f | 159 | .init = bridge_init, |
540eb5f0 SS |
160 | .sections = "Match\0NetDev\0Bridge\0", |
161 | .post_create = netdev_bridge_post_create, | |
aa9f1140 | 162 | .create_type = NETDEV_CREATE_MASTER, |
3be1d7e0 | 163 | }; |