]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd/sd-netlink/netlink-util.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / libsystemd / sd-netlink / netlink-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
d8921c6d
TG
2/***
3 This file is part of systemd.
4
5 Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19***/
20
1c4baffc 21#include "sd-netlink.h"
d8921c6d 22
1c4baffc 23#include "netlink-internal.h"
cf0fbc49 24#include "netlink-util.h"
d8921c6d 25
1c4baffc 26int rtnl_set_link_name(sd_netlink **rtnl, int ifindex, const char *name) {
4afd3348 27 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL;
d8921c6d
TG
28 int r;
29
30 assert(rtnl);
31 assert(ifindex > 0);
3e137a1b 32 assert(name);
d8921c6d 33
4c83d994 34 if (!*rtnl) {
1c4baffc 35 r = sd_netlink_open(rtnl);
4c83d994
TG
36 if (r < 0)
37 return r;
38 }
39
40 r = sd_rtnl_message_new_link(*rtnl, &message, RTM_SETLINK, ifindex);
d8921c6d
TG
41 if (r < 0)
42 return r;
43
1c4baffc 44 r = sd_netlink_message_append_string(message, IFLA_IFNAME, name);
3e137a1b
TG
45 if (r < 0)
46 return r;
d8921c6d 47
1c4baffc 48 r = sd_netlink_call(*rtnl, message, 0, NULL);
3e137a1b
TG
49 if (r < 0)
50 return r;
51
52 return 0;
53}
54
1c4baffc 55int rtnl_set_link_properties(sd_netlink **rtnl, int ifindex, const char *alias,
d2df0d0e 56 const struct ether_addr *mac, unsigned mtu) {
4afd3348 57 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL;
3e137a1b
TG
58 int r;
59
60 assert(rtnl);
61 assert(ifindex > 0);
62
d2df0d0e 63 if (!alias && !mac && mtu == 0)
3e137a1b
TG
64 return 0;
65
aedca892 66 if (!*rtnl) {
1c4baffc 67 r = sd_netlink_open(rtnl);
aedca892
TG
68 if (r < 0)
69 return r;
70 }
71
72 r = sd_rtnl_message_new_link(*rtnl, &message, RTM_SETLINK, ifindex);
3e137a1b
TG
73 if (r < 0)
74 return r;
d8921c6d 75
d2df0d0e 76 if (alias) {
1c4baffc 77 r = sd_netlink_message_append_string(message, IFLA_IFALIAS, alias);
d2df0d0e
TG
78 if (r < 0)
79 return r;
d2df0d0e
TG
80 }
81
d8921c6d 82 if (mac) {
1c4baffc 83 r = sd_netlink_message_append_ether_addr(message, IFLA_ADDRESS, mac);
d8921c6d
TG
84 if (r < 0)
85 return r;
d8921c6d
TG
86 }
87
88 if (mtu > 0) {
1c4baffc 89 r = sd_netlink_message_append_u32(message, IFLA_MTU, mtu);
d8921c6d
TG
90 if (r < 0)
91 return r;
d8921c6d
TG
92 }
93
1c4baffc 94 r = sd_netlink_call(*rtnl, message, 0, NULL);
aedca892
TG
95 if (r < 0)
96 return r;
d8921c6d
TG
97
98 return 0;
99}
3815f36f 100
1c4baffc 101int rtnl_message_new_synthetic_error(int error, uint32_t serial, sd_netlink_message **ret) {
3815f36f
TG
102 struct nlmsgerr *err;
103 int r;
104
105 assert(error <= 0);
106
b621bfd2 107 r = message_new(NULL, ret, NLMSG_ERROR);
3815f36f
TG
108 if (r < 0)
109 return r;
110
3815f36f
TG
111 (*ret)->hdr->nlmsg_seq = serial;
112
113 err = NLMSG_DATA((*ret)->hdr);
114
115 err->error = error;
116
117 return 0;
118}
119
ee8c4568 120int rtnl_log_parse_error(int r) {
8d3d7072 121 return log_error_errno(r, "Failed to parse netlink message: %m");
ee8c4568
LP
122}
123
124int rtnl_log_create_error(int r) {
8d3d7072 125 return log_error_errno(r, "Failed to create netlink message: %m");
ee8c4568 126}