1 /* SPDX-License-Identifier: LGPL-2.1+ */
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 "string-util.h"
12 #define CAN_TERMINATION_OHM_VALUE 120
14 static int link_up_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
19 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
22 r
= sd_netlink_message_get_errno(m
);
24 /* we warn but don't fail the link, as it may be brought up later */
25 log_link_message_warning_errno(link
, m
, r
, "Could not bring up interface");
30 static int link_up_can(Link
*link
) {
31 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
;
36 log_link_debug(link
, "Bringing CAN link up");
38 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &req
, RTM_SETLINK
, link
->ifindex
);
40 return log_link_error_errno(link
, r
, "Could not allocate RTM_SETLINK message: %m");
42 r
= sd_rtnl_message_link_set_flags(req
, IFF_UP
, IFF_UP
);
44 return log_link_error_errno(link
, r
, "Could not set link flags: %m");
46 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, req
, link_up_handler
,
47 link_netlink_destroy_callback
, link
);
49 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
56 static int link_set_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
61 log_link_debug(link
, "Set link");
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
);
72 static int link_set_can(Link
*link
) {
73 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*m
= NULL
;
74 struct can_ctrlmode cm
= {};
78 assert(link
->network
);
79 assert(link
->manager
);
80 assert(link
->manager
->rtnl
);
82 log_link_debug(link
, "Configuring CAN link.");
84 r
= sd_rtnl_message_new_link(link
->manager
->rtnl
, &m
, RTM_NEWLINK
, link
->ifindex
);
86 return log_link_error_errno(link
, r
, "Failed to allocate netlink message: %m");
88 r
= sd_netlink_message_set_flags(m
, NLM_F_REQUEST
| NLM_F_ACK
);
90 return log_link_error_errno(link
, r
, "Could not set netlink flags: %m");
92 r
= sd_netlink_message_open_container(m
, IFLA_LINKINFO
);
94 return log_link_error_errno(link
, r
, "Failed to open netlink container: %m");
96 r
= sd_netlink_message_open_container_union(m
, IFLA_INFO_DATA
, link
->kind
);
98 return log_link_error_errno(link
, r
, "Could not append IFLA_INFO_DATA attribute: %m");
100 if (link
->network
->can_bitrate
> 0 || link
->network
->can_sample_point
> 0) {
101 struct can_bittiming bt
= {
102 .bitrate
= link
->network
->can_bitrate
,
103 .sample_point
= link
->network
->can_sample_point
,
106 if (link
->network
->can_bitrate
> UINT32_MAX
) {
107 log_link_error(link
, "bitrate (%" PRIu64
") too big.", link
->network
->can_bitrate
);
111 log_link_debug(link
, "Setting bitrate = %d bit/s", bt
.bitrate
);
112 if (link
->network
->can_sample_point
> 0)
113 log_link_debug(link
, "Setting sample point = %d.%d%%", bt
.sample_point
/ 10, bt
.sample_point
% 10);
115 log_link_debug(link
, "Using default sample point");
117 r
= sd_netlink_message_append_data(m
, IFLA_CAN_BITTIMING
, &bt
, sizeof(bt
));
119 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_BITTIMING attribute: %m");
122 if (link
->network
->can_restart_us
> 0) {
123 char time_string
[FORMAT_TIMESPAN_MAX
];
126 if (link
->network
->can_restart_us
== USEC_INFINITY
)
129 restart_ms
= DIV_ROUND_UP(link
->network
->can_restart_us
, USEC_PER_MSEC
);
131 format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, restart_ms
* 1000, MSEC_PER_SEC
);
133 if (restart_ms
> UINT32_MAX
) {
134 log_link_error(link
, "restart timeout (%s) too big.", time_string
);
138 log_link_debug(link
, "Setting restart = %s", time_string
);
140 r
= sd_netlink_message_append_u32(m
, IFLA_CAN_RESTART_MS
, restart_ms
);
142 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_RESTART_MS attribute: %m");
145 if (link
->network
->can_triple_sampling
>= 0) {
146 cm
.mask
|= CAN_CTRLMODE_3_SAMPLES
;
147 SET_FLAG(cm
.flags
, CAN_CTRLMODE_3_SAMPLES
, link
->network
->can_triple_sampling
);
148 log_link_debug(link
, "%sabling triple-sampling", link
->network
->can_triple_sampling
? "En" : "Dis");
151 if (link
->network
->can_listen_only
>= 0) {
152 cm
.mask
|= CAN_CTRLMODE_LISTENONLY
;
153 SET_FLAG(cm
.flags
, CAN_CTRLMODE_LISTENONLY
, link
->network
->can_listen_only
);
154 log_link_debug(link
, "%sabling listen-only mode", link
->network
->can_listen_only
? "En" : "Dis");
158 r
= sd_netlink_message_append_data(m
, IFLA_CAN_CTRLMODE
, &cm
, sizeof(cm
));
160 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_CTRLMODE attribute: %m");
163 if (link
->network
->can_termination
>= 0) {
165 log_link_debug(link
, "%sabling can-termination", link
->network
->can_termination
? "En" : "Dis");
167 r
= sd_netlink_message_append_u16(m
, IFLA_CAN_TERMINATION
,
168 link
->network
->can_termination
? CAN_TERMINATION_OHM_VALUE
: 0);
170 return log_link_error_errno(link
, r
, "Could not append IFLA_CAN_TERMINATION attribute: %m");
174 r
= sd_netlink_message_close_container(m
);
176 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
178 r
= sd_netlink_message_close_container(m
);
180 return log_link_error_errno(link
, r
, "Failed to close netlink container: %m");
182 r
= netlink_call_async(link
->manager
->rtnl
, NULL
, m
, link_set_handler
,
183 link_netlink_destroy_callback
, link
);
185 return log_link_error_errno(link
, r
, "Could not send rtnetlink message: %m");
189 if (!(link
->flags
& IFF_UP
))
190 return link_up_can(link
);
195 static int link_down_handler(sd_netlink
*rtnl
, sd_netlink_message
*m
, Link
*link
) {
200 if (IN_SET(link
->state
, LINK_STATE_FAILED
, LINK_STATE_LINGER
))
203 r
= sd_netlink_message_get_errno(m
);
205 log_link_message_warning_errno(link
, m
, r
, "Could not bring down interface");
206 link_enter_failed(link
);
210 r
= link_set_can(link
);
212 link_enter_failed(link
);
217 int link_configure_can(Link
*link
) {
220 link_set_state(link
, LINK_STATE_CONFIGURING
);
222 if (streq_ptr(link
->kind
, "can")) {
223 /* The CAN interface must be down to configure bitrate, etc... */
224 if ((link
->flags
& IFF_UP
)) {
225 r
= link_down(link
, link_down_handler
);
227 link_enter_failed(link
);
231 r
= link_set_can(link
);
233 link_enter_failed(link
);
241 if (!(link
->flags
& IFF_UP
)) {
242 r
= link_up_can(link
);
244 link_enter_failed(link
);