Coverage Report

Created: 2019-06-19 13:33

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