/src/systemd/src/libsystemd/sd-netlink/netlink-util.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | | |
3 | | #include "sd-netlink.h" |
4 | | |
5 | | #include "netlink-internal.h" |
6 | | #include "netlink-util.h" |
7 | | |
8 | 0 | int rtnl_set_link_name(sd_netlink **rtnl, int ifindex, const char *name) { |
9 | 0 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL; |
10 | 0 | int r; |
11 | 0 |
|
12 | 0 | assert(rtnl); |
13 | 0 | assert(ifindex > 0); |
14 | 0 | assert(name); |
15 | 0 |
|
16 | 0 | if (!ifname_valid(name)) |
17 | 0 | return -EINVAL; |
18 | 0 | |
19 | 0 | if (!*rtnl) { |
20 | 0 | r = sd_netlink_open(rtnl); |
21 | 0 | if (r < 0) |
22 | 0 | return r; |
23 | 0 | } |
24 | 0 | |
25 | 0 | r = sd_rtnl_message_new_link(*rtnl, &message, RTM_SETLINK, ifindex); |
26 | 0 | if (r < 0) |
27 | 0 | return r; |
28 | 0 | |
29 | 0 | r = sd_netlink_message_append_string(message, IFLA_IFNAME, name); |
30 | 0 | if (r < 0) |
31 | 0 | return r; |
32 | 0 | |
33 | 0 | r = sd_netlink_call(*rtnl, message, 0, NULL); |
34 | 0 | if (r < 0) |
35 | 0 | return r; |
36 | 0 | |
37 | 0 | return 0; |
38 | 0 | } |
39 | | |
40 | | int rtnl_set_link_properties(sd_netlink **rtnl, int ifindex, const char *alias, |
41 | 0 | const struct ether_addr *mac, uint32_t mtu) { |
42 | 0 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL; |
43 | 0 | int r; |
44 | 0 |
|
45 | 0 | assert(rtnl); |
46 | 0 | assert(ifindex > 0); |
47 | 0 |
|
48 | 0 | if (!alias && !mac && mtu == 0) |
49 | 0 | return 0; |
50 | 0 | |
51 | 0 | if (!*rtnl) { |
52 | 0 | r = sd_netlink_open(rtnl); |
53 | 0 | if (r < 0) |
54 | 0 | return r; |
55 | 0 | } |
56 | 0 | |
57 | 0 | r = sd_rtnl_message_new_link(*rtnl, &message, RTM_SETLINK, ifindex); |
58 | 0 | if (r < 0) |
59 | 0 | return r; |
60 | 0 | |
61 | 0 | if (alias) { |
62 | 0 | r = sd_netlink_message_append_string(message, IFLA_IFALIAS, alias); |
63 | 0 | if (r < 0) |
64 | 0 | return r; |
65 | 0 | } |
66 | 0 | |
67 | 0 | if (mac) { |
68 | 0 | r = sd_netlink_message_append_ether_addr(message, IFLA_ADDRESS, mac); |
69 | 0 | if (r < 0) |
70 | 0 | return r; |
71 | 0 | } |
72 | 0 | |
73 | 0 | if (mtu != 0) { |
74 | 0 | r = sd_netlink_message_append_u32(message, IFLA_MTU, mtu); |
75 | 0 | if (r < 0) |
76 | 0 | return r; |
77 | 0 | } |
78 | 0 | |
79 | 0 | r = sd_netlink_call(*rtnl, message, 0, NULL); |
80 | 0 | if (r < 0) |
81 | 0 | return r; |
82 | 0 | |
83 | 0 | return 0; |
84 | 0 | } |
85 | | |
86 | 0 | int rtnl_message_new_synthetic_error(sd_netlink *rtnl, int error, uint32_t serial, sd_netlink_message **ret) { |
87 | 0 | struct nlmsgerr *err; |
88 | 0 | int r; |
89 | 0 |
|
90 | 0 | assert(error <= 0); |
91 | 0 |
|
92 | 0 | r = message_new(rtnl, ret, NLMSG_ERROR); |
93 | 0 | if (r < 0) |
94 | 0 | return r; |
95 | 0 | |
96 | 0 | (*ret)->hdr->nlmsg_seq = serial; |
97 | 0 |
|
98 | 0 | err = NLMSG_DATA((*ret)->hdr); |
99 | 0 |
|
100 | 0 | err->error = error; |
101 | 0 |
|
102 | 0 | return 0; |
103 | 0 | } |
104 | | |
105 | 0 | int rtnl_log_parse_error(int r) { |
106 | 0 | return log_error_errno(r, "Failed to parse netlink message: %m"); |
107 | 0 | } |
108 | | |
109 | 0 | int rtnl_log_create_error(int r) { |
110 | 0 | return log_error_errno(r, "Failed to create netlink message: %m"); |
111 | 0 | } |