1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include <linux/can/netlink.h>
6 #include "netlink-util.h"
7 #include "networkd-can.h"
8 #include "networkd-link.h"
9 #include "networkd-manager.h"
10 #include "networkd-setlink.h"
11 #include "parse-util.h"
12 #include "string-util.h"
14 #define CAN_TERMINATION_OHM_VALUE 120
16 int config_parse_can_bitrate(
21 unsigned section_line
,
37 r
= parse_size(rvalue
, 1000, &sz
);
39 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
40 "Failed to parse can bitrate '%s', ignoring: %m", rvalue
);
44 /* Linux uses __u32 for bitrates, so the value should not exceed that. */
45 if (sz
<= 0 || sz
> UINT32_MAX
) {
46 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
47 "Bit rate out of permitted range 1...4294967295");
56 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
61 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
64 r
= sd_netlink_message_get_errno(m
);
65 if (r
< 0 && r
!= -EEXIST
) {
66 log_link_message_warning_errno(link
, m
, r
, "Failed to configure CAN link");
67 link_enter_failed(link
);
71 log_link_debug(link
, "Link set");
73 r
= link_request_to_activate(link
);
75 link_enter_failed(link
);
79 link
->can_configured
= true;
80 link_check_ready(link
);
85 static int link_set_can(Link
*link
) {
86 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
87 struct can_ctrlmode cm
= {};
91 assert(link
->network
);
92 assert(link
->manager
);
93 assert(link
->manager
->rtnl
);
95 log_link_debug(link
, "Configuring CAN link.");
97 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->ifindex
);
99 return log_link_error_errno(link
, r
, "Failed to allocate netlink message: %m");
101 r
= sd_netlink_message_set_flags(m
, NLM_F_REQUEST
| NLM_F_ACK
);
103 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
105 r
= sd_netlink_message_open_container(m
, IFLA_LINKINFO
);
107 return log_link_error_errno(link
, r
, "Failed to open netlink container: %m");
109 r
= sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, link
->kind
);
111 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
113 if (link
->network
->can_bitrate
> 0 || link
->network
->can_sample_point
> 0) {
114 struct can_bittiming bt
= {
115 .bitrate
= link
->network
->can_bitrate
,
116 .sample_point
= link
->network
->can_sample_point
,
119 log_link_debug(link
, "Setting bitrate = %d bit/s", bt
.bitrate
);
120 if (link
->network
->can_sample_point
> 0)
121 log_link_debug(link
, "Setting sample point = %d.%d%%", bt
.sample_point
/ 10, bt
.sample_point
% 10);
123 log_link_debug(link
, "Using default sample point");
125 r
= sd_netlink_message_append_data(m
, IFLA_CAN_BITTIMING
, &bt
, sizeof(bt
));
127 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_BITTIMING attribute: %m");
130 if (link
->network
->can_data_bitrate
> 0 || link
->network
->can_data_sample_point
> 0) {
131 struct can_bittiming bt
= {
132 .bitrate
= link
->network
->can_data_bitrate
,
133 .sample_point
= link
->network
->can_data_sample_point
,
136 log_link_debug(link
, "Setting data bitrate = %d bit/s", bt
.bitrate
);
137 if (link
->network
->can_data_sample_point
> 0)
138 log_link_debug(link
, "Setting data sample point = %d.%d%%", bt
.sample_point
/ 10, bt
.sample_point
% 10);
140 log_link_debug(link
, "Using default data sample point");
142 r
= sd_netlink_message_append_data(m
, IFLA_CAN_DATA_BITTIMING
, &bt
, sizeof(bt
));
144 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_DATA_BITTIMING attribute: %m");
147 if (link
->network
->can_fd_mode
>= 0) {
148 cm
.mask
|= CAN_CTRLMODE_FD
;
149 SET_FLAG(cm
.flags
, CAN_CTRLMODE_FD
, link
->network
->can_fd_mode
);
150 log_link_debug(link
, "Setting FD mode to '%s'.", yes_no(link
->network
->can_fd_mode
));
153 if (link
->network
->can_non_iso
>= 0) {
154 cm
.mask
|= CAN_CTRLMODE_FD_NON_ISO
;
155 SET_FLAG(cm
.flags
, CAN_CTRLMODE_FD_NON_ISO
, link
->network
->can_non_iso
);
156 log_link_debug(link
, "Setting FD non-ISO mode to '%s'.", yes_no(link
->network
->can_non_iso
));
159 if (link
->network
->can_restart_us
> 0) {
160 char time_string
[FORMAT_TIMESPAN_MAX
];
163 if (link
->network
->can_restart_us
== USEC_INFINITY
)
166 restart_ms
= DIV_ROUND_UP(link
->network
->can_restart_us
, USEC_PER_MSEC
);
168 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, restart_ms
* 1000, MSEC_PER_SEC
);
170 if (restart_ms
> UINT32_MAX
)
171 return log_link_error_errno(link
, SYNTHETIC_ERRNO(ERANGE
), "restart timeout (%s) too big.", time_string
);
173 log_link_debug(link
, "Setting restart = %s", time_string
);
175 r
= sd_netlink_message_append_u32(m
, IFLA_CAN_RESTART_MS
, restart_ms
);
177 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_RESTART_MS attribute: %m");
180 if (link
->network
->can_triple_sampling
>= 0) {
181 cm
.mask
|= CAN_CTRLMODE_3_SAMPLES
;
182 SET_FLAG(cm
.flags
, CAN_CTRLMODE_3_SAMPLES
, link
->network
->can_triple_sampling
);
183 log_link_debug(link
, "Setting triple-sampling to '%s'.", yes_no(link
->network
->can_triple_sampling
));
186 if (link
->network
->can_berr_reporting
>= 0) {
187 cm
.mask
|= CAN_CTRLMODE_BERR_REPORTING
;
188 SET_FLAG(cm
.flags
, CAN_CTRLMODE_BERR_REPORTING
, link
->network
->can_berr_reporting
);
189 log_link_debug(link
, "Setting bus error reporting to '%s'.", yes_no(link
->network
->can_berr_reporting
));
192 if (link
->network
->can_listen_only
>= 0) {
193 cm
.mask
|= CAN_CTRLMODE_LISTENONLY
;
194 SET_FLAG(cm
.flags
, CAN_CTRLMODE_LISTENONLY
, link
->network
->can_listen_only
);
195 log_link_debug(link
, "Setting listen-only mode to '%s'.", yes_no(link
->network
->can_listen_only
));
199 r
= sd_netlink_message_append_data(m
, IFLA_CAN_CTRLMODE
, &cm
, sizeof(cm
));
201 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_CTRLMODE attribute: %m");
204 if (link
->network
->can_termination
>= 0) {
206 log_link_debug(link
, "Setting can-termination to '%s'.", yes_no(link
->network
->can_termination
));
208 r
= sd_netlink_message_append_u16(m
, IFLA_CAN_TERMINATION
,
209 link
->network
->can_termination
? CAN_TERMINATION_OHM_VALUE
: 0);
211 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_TERMINATION attribute: %m");
215 r
= sd_netlink_message_close_container(m
);
217 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
219 r
= sd_netlink_message_close_container(m
);
221 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
223 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, m
, link_set_handler
,
224 link_netlink_destroy_callback
, link
);
226 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
233 int link_configure_can(Link
*link
) {
236 link_set_state(link
, LINK_STATE_CONFIGURING
);
238 if (streq_ptr(link
->kind
, "can")) {
239 /* The CAN interface must be down to configure bitrate, etc... */
240 if (link
->flags
& IFF_UP
) {
246 return link_set_can(link
);
249 r
= link_request_to_activate(link
);
253 link
->can_configured
= true;
254 link_check_ready(link
);