]>
Commit | Line | Data |
---|---|---|
3ddcbeea YW |
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | ||
3 | #include <net/if.h> | |
4 | #include <linux/can/netlink.h> | |
5 | ||
6 | #include "netlink-util.h" | |
7 | #include "networkd-can.h" | |
8 | #include "networkd-link.h" | |
9 | #include "networkd-manager.h" | |
10 | #include "string-util.h" | |
11 | ||
52aa38f1 MR |
12 | #define CAN_TERMINATION_OHM_VALUE 120 |
13 | ||
3ddcbeea YW |
14 | static int link_up_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { |
15 | int r; | |
16 | ||
17 | assert(link); | |
18 | ||
19 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
20 | return 1; | |
21 | ||
22 | r = sd_netlink_message_get_errno(m); | |
23 | if (r < 0) | |
24 | /* we warn but don't fail the link, as it may be brought up later */ | |
5ecb131d | 25 | log_link_message_warning_errno(link, m, r, "Could not bring up interface"); |
3ddcbeea YW |
26 | |
27 | return 1; | |
28 | } | |
29 | ||
30 | static int link_up_can(Link *link) { | |
31 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; | |
32 | int r; | |
33 | ||
34 | assert(link); | |
35 | ||
36 | log_link_debug(link, "Bringing CAN link up"); | |
37 | ||
38 | r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex); | |
39 | if (r < 0) | |
40 | return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m"); | |
41 | ||
42 | r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP); | |
43 | if (r < 0) | |
44 | return log_link_error_errno(link, r, "Could not set link flags: %m"); | |
45 | ||
46 | r = netlink_call_async(link->manager->rtnl, NULL, req, link_up_handler, | |
47 | link_netlink_destroy_callback, link); | |
48 | if (r < 0) | |
49 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
50 | ||
51 | link_ref(link); | |
52 | ||
53 | return 0; | |
54 | } | |
55 | ||
56 | static int link_set_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { | |
57 | int r; | |
58 | ||
59 | assert(link); | |
60 | ||
61 | log_link_debug(link, "Set link"); | |
62 | ||
63 | r = sd_netlink_message_get_errno(m); | |
64 | if (r < 0 && r != -EEXIST) { | |
5ecb131d | 65 | log_link_message_warning_errno(link, m, r, "Failed to configure CAN link"); |
3ddcbeea YW |
66 | link_enter_failed(link); |
67 | } | |
68 | ||
69 | return 1; | |
70 | } | |
71 | ||
72 | static int link_set_can(Link *link) { | |
73 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL; | |
74 | int r; | |
75 | ||
76 | assert(link); | |
77 | assert(link->network); | |
78 | assert(link->manager); | |
79 | assert(link->manager->rtnl); | |
80 | ||
81 | log_link_debug(link, "Configuring CAN link."); | |
82 | ||
83 | r = sd_rtnl_message_new_link(link->manager->rtnl, &m, RTM_NEWLINK, link->ifindex); | |
84 | if (r < 0) | |
85 | return log_link_error_errno(link, r, "Failed to allocate netlink message: %m"); | |
86 | ||
87 | r = sd_netlink_message_set_flags(m, NLM_F_REQUEST | NLM_F_ACK); | |
88 | if (r < 0) | |
89 | return log_link_error_errno(link, r, "Could not set netlink flags: %m"); | |
90 | ||
91 | r = sd_netlink_message_open_container(m, IFLA_LINKINFO); | |
92 | if (r < 0) | |
93 | return log_link_error_errno(link, r, "Failed to open netlink container: %m"); | |
94 | ||
95 | r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, link->kind); | |
96 | if (r < 0) | |
97 | return log_link_error_errno(link, r, "Could not append IFLA_INFO_DATA attribute: %m"); | |
98 | ||
99 | if (link->network->can_bitrate > 0 || link->network->can_sample_point > 0) { | |
100 | struct can_bittiming bt = { | |
101 | .bitrate = link->network->can_bitrate, | |
102 | .sample_point = link->network->can_sample_point, | |
103 | }; | |
104 | ||
105 | if (link->network->can_bitrate > UINT32_MAX) { | |
50299121 | 106 | log_link_error(link, "bitrate (%" PRIu64 ") too big.", link->network->can_bitrate); |
3ddcbeea YW |
107 | return -ERANGE; |
108 | } | |
109 | ||
110 | log_link_debug(link, "Setting bitrate = %d bit/s", bt.bitrate); | |
111 | if (link->network->can_sample_point > 0) | |
112 | log_link_debug(link, "Setting sample point = %d.%d%%", bt.sample_point / 10, bt.sample_point % 10); | |
113 | else | |
114 | log_link_debug(link, "Using default sample point"); | |
115 | ||
116 | r = sd_netlink_message_append_data(m, IFLA_CAN_BITTIMING, &bt, sizeof(bt)); | |
117 | if (r < 0) | |
118 | return log_link_error_errno(link, r, "Could not append IFLA_CAN_BITTIMING attribute: %m"); | |
119 | } | |
120 | ||
121 | if (link->network->can_restart_us > 0) { | |
122 | char time_string[FORMAT_TIMESPAN_MAX]; | |
123 | uint64_t restart_ms; | |
124 | ||
125 | if (link->network->can_restart_us == USEC_INFINITY) | |
126 | restart_ms = 0; | |
127 | else | |
128 | restart_ms = DIV_ROUND_UP(link->network->can_restart_us, USEC_PER_MSEC); | |
129 | ||
130 | format_timespan(time_string, FORMAT_TIMESPAN_MAX, restart_ms * 1000, MSEC_PER_SEC); | |
131 | ||
132 | if (restart_ms > UINT32_MAX) { | |
133 | log_link_error(link, "restart timeout (%s) too big.", time_string); | |
134 | return -ERANGE; | |
135 | } | |
136 | ||
137 | log_link_debug(link, "Setting restart = %s", time_string); | |
138 | ||
139 | r = sd_netlink_message_append_u32(m, IFLA_CAN_RESTART_MS, restart_ms); | |
140 | if (r < 0) | |
141 | return log_link_error_errno(link, r, "Could not append IFLA_CAN_RESTART_MS attribute: %m"); | |
142 | } | |
143 | ||
144 | if (link->network->can_triple_sampling >= 0) { | |
145 | struct can_ctrlmode cm = { | |
146 | .mask = CAN_CTRLMODE_3_SAMPLES, | |
147 | .flags = link->network->can_triple_sampling ? CAN_CTRLMODE_3_SAMPLES : 0, | |
148 | }; | |
149 | ||
150 | log_link_debug(link, "%sabling triple-sampling", link->network->can_triple_sampling ? "En" : "Dis"); | |
151 | ||
152 | r = sd_netlink_message_append_data(m, IFLA_CAN_CTRLMODE, &cm, sizeof(cm)); | |
153 | if (r < 0) | |
154 | return log_link_error_errno(link, r, "Could not append IFLA_CAN_CTRLMODE attribute: %m"); | |
155 | } | |
156 | ||
52aa38f1 MR |
157 | if (link->network->can_termination >= 0) { |
158 | ||
159 | log_link_debug(link, "%sabling can-termination", link->network->can_termination ? "En" : "Dis"); | |
160 | ||
161 | r = sd_netlink_message_append_u16(m, IFLA_CAN_TERMINATION, | |
162 | link->network->can_termination ? CAN_TERMINATION_OHM_VALUE : 0); | |
163 | if (r < 0) | |
164 | return log_link_error_errno(link, r, "Could not append IFLA_CAN_TERMINATION attribute: %m"); | |
165 | ||
166 | } | |
167 | ||
3ddcbeea YW |
168 | r = sd_netlink_message_close_container(m); |
169 | if (r < 0) | |
170 | return log_link_error_errno(link, r, "Failed to close netlink container: %m"); | |
171 | ||
172 | r = sd_netlink_message_close_container(m); | |
173 | if (r < 0) | |
174 | return log_link_error_errno(link, r, "Failed to close netlink container: %m"); | |
175 | ||
176 | r = netlink_call_async(link->manager->rtnl, NULL, m, link_set_handler, | |
177 | link_netlink_destroy_callback, link); | |
178 | if (r < 0) | |
179 | return log_link_error_errno(link, r, "Could not send rtnetlink message: %m"); | |
180 | ||
181 | link_ref(link); | |
182 | ||
183 | if (!(link->flags & IFF_UP)) | |
184 | return link_up_can(link); | |
185 | ||
186 | return 0; | |
187 | } | |
188 | ||
189 | static int link_down_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) { | |
190 | int r; | |
191 | ||
192 | assert(link); | |
193 | ||
194 | if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER)) | |
195 | return 1; | |
196 | ||
197 | r = sd_netlink_message_get_errno(m); | |
198 | if (r < 0) { | |
5ecb131d | 199 | log_link_message_warning_errno(link, m, r, "Could not bring down interface"); |
3ddcbeea YW |
200 | link_enter_failed(link); |
201 | return 1; | |
202 | } | |
203 | ||
204 | r = link_set_can(link); | |
205 | if (r < 0) | |
206 | link_enter_failed(link); | |
207 | ||
208 | return 1; | |
209 | } | |
210 | ||
211 | int link_configure_can(Link *link) { | |
212 | int r; | |
213 | ||
af9ba57a YW |
214 | link_set_state(link, LINK_STATE_CONFIGURING); |
215 | ||
3ddcbeea YW |
216 | if (streq_ptr(link->kind, "can")) { |
217 | /* The CAN interface must be down to configure bitrate, etc... */ | |
218 | if ((link->flags & IFF_UP)) { | |
219 | r = link_down(link, link_down_handler); | |
220 | if (r < 0) { | |
221 | link_enter_failed(link); | |
222 | return r; | |
223 | } | |
224 | } else { | |
225 | r = link_set_can(link); | |
226 | if (r < 0) { | |
227 | link_enter_failed(link); | |
228 | return r; | |
229 | } | |
230 | } | |
231 | ||
232 | return 0; | |
233 | } | |
234 | ||
235 | if (!(link->flags & IFF_UP)) { | |
236 | r = link_up_can(link); | |
237 | if (r < 0) { | |
238 | link_enter_failed(link); | |
239 | return r; | |
240 | } | |
241 | } | |
242 | ||
243 | return 0; | |
244 | } |