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 "parse-util.h"
11 #include "string-util.h"
13 #define CAN_TERMINATION_OHM_VALUE 120
15 int config_parse_can_bitrate(
20 unsigned section_line
,
36 r
= parse_size(rvalue
, 1000, &sz
);
38 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
,
39 "Failed to parse can bitrate '%s', ignoring: %m", rvalue
);
43 /* Linux uses __u32 for bitrates, so the value should not exceed that. */
44 if (sz
<= 0 || sz
> UINT32_MAX
) {
45 log_syntax(unit
, LOG_WARNING
, filename
, line
, 0,
46 "Bit rate out of permitted range 1...4294967295");
55 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
60 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
63 r
= sd_netlink_message_get_errno(m
);
64 if (r
< 0 && r
!= -EEXIST
) {
65 log_link_message_warning_errno(link
, m
, r
, "Failed to configure CAN link");
66 link_enter_failed(link
);
70 log_link_debug(link
, "Link set");
72 r
= link_activate(link
);
74 link_enter_failed(link
);
78 link
->can_configured
= true;
79 link_check_ready(link
);
84 static int link_set_can(Link
*link
) {
85 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
86 struct can_ctrlmode cm
= {};
90 assert(link
->network
);
91 assert(link
->manager
);
92 assert(link
->manager
->rtnl
);
94 log_link_debug(link
, "Configuring CAN link.");
96 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->ifindex
);
98 return log_link_error_errno(link
, r
, "Failed to allocate netlink message: %m");
100 r
= sd_netlink_message_set_flags(m
, NLM_F_REQUEST
| NLM_F_ACK
);
102 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
104 r
= sd_netlink_message_open_container(m
, IFLA_LINKINFO
);
106 return log_link_error_errno(link
, r
, "Failed to open netlink container: %m");
108 r
= sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, link
->kind
);
110 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
112 if (link
->network
->can_bitrate
> 0 || link
->network
->can_sample_point
> 0) {
113 struct can_bittiming bt
= {
114 .bitrate
= link
->network
->can_bitrate
,
115 .sample_point
= link
->network
->can_sample_point
,
118 log_link_debug(link
, "Setting bitrate = %d bit/s", bt
.bitrate
);
119 if (link
->network
->can_sample_point
> 0)
120 log_link_debug(link
, "Setting sample point = %d.%d%%", bt
.sample_point
/ 10, bt
.sample_point
% 10);
122 log_link_debug(link
, "Using default sample point");
124 r
= sd_netlink_message_append_data(m
, IFLA_CAN_BITTIMING
, &bt
, sizeof(bt
));
126 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_BITTIMING attribute: %m");
129 if (link
->network
->can_data_bitrate
> 0 || link
->network
->can_data_sample_point
> 0) {
130 struct can_bittiming bt
= {
131 .bitrate
= link
->network
->can_data_bitrate
,
132 .sample_point
= link
->network
->can_data_sample_point
,
135 log_link_debug(link
, "Setting data bitrate = %d bit/s", bt
.bitrate
);
136 if (link
->network
->can_data_sample_point
> 0)
137 log_link_debug(link
, "Setting data sample point = %d.%d%%", bt
.sample_point
/ 10, bt
.sample_point
% 10);
139 log_link_debug(link
, "Using default data sample point");
141 r
= sd_netlink_message_append_data(m
, IFLA_CAN_DATA_BITTIMING
, &bt
, sizeof(bt
));
143 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_DATA_BITTIMING attribute: %m");
146 if (link
->network
->can_fd_mode
>= 0) {
147 cm
.mask
|= CAN_CTRLMODE_FD
;
148 SET_FLAG(cm
.flags
, CAN_CTRLMODE_FD
, link
->network
->can_fd_mode
);
149 log_link_debug(link
, "Setting FD mode to '%s'.", yes_no(link
->network
->can_fd_mode
));
152 if (link
->network
->can_non_iso
>= 0) {
153 cm
.mask
|= CAN_CTRLMODE_FD_NON_ISO
;
154 SET_FLAG(cm
.flags
, CAN_CTRLMODE_FD_NON_ISO
, link
->network
->can_non_iso
);
155 log_link_debug(link
, "Setting FD non-ISO mode to '%s'.", yes_no(link
->network
->can_non_iso
));
158 if (link
->network
->can_restart_us
> 0) {
159 char time_string
[FORMAT_TIMESPAN_MAX
];
162 if (link
->network
->can_restart_us
== USEC_INFINITY
)
165 restart_ms
= DIV_ROUND_UP(link
->network
->can_restart_us
, USEC_PER_MSEC
);
167 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, restart_ms
* 1000, MSEC_PER_SEC
);
169 if (restart_ms
> UINT32_MAX
)
170 return log_link_error_errno(link
, SYNTHETIC_ERRNO(ERANGE
), "restart timeout (%s) too big.", time_string
);
172 log_link_debug(link
, "Setting restart = %s", time_string
);
174 r
= sd_netlink_message_append_u32(m
, IFLA_CAN_RESTART_MS
, restart_ms
);
176 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_RESTART_MS attribute: %m");
179 if (link
->network
->can_triple_sampling
>= 0) {
180 cm
.mask
|= CAN_CTRLMODE_3_SAMPLES
;
181 SET_FLAG(cm
.flags
, CAN_CTRLMODE_3_SAMPLES
, link
->network
->can_triple_sampling
);
182 log_link_debug(link
, "Setting triple-sampling to '%s'.", yes_no(link
->network
->can_triple_sampling
));
185 if (link
->network
->can_berr_reporting
>= 0) {
186 cm
.mask
|= CAN_CTRLMODE_BERR_REPORTING
;
187 SET_FLAG(cm
.flags
, CAN_CTRLMODE_BERR_REPORTING
, link
->network
->can_berr_reporting
);
188 log_link_debug(link
, "Setting bus error reporting to '%s'.", yes_no(link
->network
->can_berr_reporting
));
191 if (link
->network
->can_listen_only
>= 0) {
192 cm
.mask
|= CAN_CTRLMODE_LISTENONLY
;
193 SET_FLAG(cm
.flags
, CAN_CTRLMODE_LISTENONLY
, link
->network
->can_listen_only
);
194 log_link_debug(link
, "Setting listen-only mode to '%s'.", yes_no(link
->network
->can_listen_only
));
198 r
= sd_netlink_message_append_data(m
, IFLA_CAN_CTRLMODE
, &cm
, sizeof(cm
));
200 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_CTRLMODE attribute: %m");
203 if (link
->network
->can_termination
>= 0) {
205 log_link_debug(link
, "Setting can-termination to '%s'.", yes_no(link
->network
->can_termination
));
207 r
= sd_netlink_message_append_u16(m
, IFLA_CAN_TERMINATION
,
208 link
->network
->can_termination
? CAN_TERMINATION_OHM_VALUE
: 0);
210 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_TERMINATION attribute: %m");
214 r
= sd_netlink_message_close_container(m
);
216 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
218 r
= sd_netlink_message_close_container(m
);
220 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
222 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, m
, link_set_handler
,
223 link_netlink_destroy_callback
, link
);
225 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
232 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
237 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
240 r
= sd_netlink_message_get_errno(m
);
242 log_link_message_warning_errno(link
, m
, r
, "Could not bring down interface");
243 link_enter_failed(link
);
247 r
= link_set_can(link
);
249 link_enter_failed(link
);
254 int link_configure_can(Link
*link
) {
257 link_set_state(link
, LINK_STATE_CONFIGURING
);
259 if (streq_ptr(link
->kind
, "can")) {
260 /* The CAN interface must be down to configure bitrate, etc... */
261 if ((link
->flags
& IFF_UP
))
262 r
= link_down(link
, link_down_handler
);
264 r
= link_set_can(link
);
266 link_enter_failed(link
);
270 r
= link_activate(link
);
274 link
->can_configured
= true;
275 link_check_ready(link
);