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