]> git.ipfire.org Git - thirdparty/systemd.git/blob - 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
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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
21 #include "sd-netlink.h"
22
23 #include "netlink-internal.h"
24 #include "netlink-util.h"
25
26 int rtnl_set_link_name(sd_netlink **rtnl, int ifindex, const char *name) {
27 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL;
28 int r;
29
30 assert(rtnl);
31 assert(ifindex > 0);
32 assert(name);
33
34 if (!*rtnl) {
35 r = sd_netlink_open(rtnl);
36 if (r < 0)
37 return r;
38 }
39
40 r = sd_rtnl_message_new_link(*rtnl, &message, RTM_SETLINK, ifindex);
41 if (r < 0)
42 return r;
43
44 r = sd_netlink_message_append_string(message, IFLA_IFNAME, name);
45 if (r < 0)
46 return r;
47
48 r = sd_netlink_call(*rtnl, message, 0, NULL);
49 if (r < 0)
50 return r;
51
52 return 0;
53 }
54
55 int rtnl_set_link_properties(sd_netlink **rtnl, int ifindex, const char *alias,
56 const struct ether_addr *mac, unsigned mtu) {
57 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL;
58 int r;
59
60 assert(rtnl);
61 assert(ifindex > 0);
62
63 if (!alias && !mac && mtu == 0)
64 return 0;
65
66 if (!*rtnl) {
67 r = sd_netlink_open(rtnl);
68 if (r < 0)
69 return r;
70 }
71
72 r = sd_rtnl_message_new_link(*rtnl, &message, RTM_SETLINK, ifindex);
73 if (r < 0)
74 return r;
75
76 if (alias) {
77 r = sd_netlink_message_append_string(message, IFLA_IFALIAS, alias);
78 if (r < 0)
79 return r;
80 }
81
82 if (mac) {
83 r = sd_netlink_message_append_ether_addr(message, IFLA_ADDRESS, mac);
84 if (r < 0)
85 return r;
86 }
87
88 if (mtu > 0) {
89 r = sd_netlink_message_append_u32(message, IFLA_MTU, mtu);
90 if (r < 0)
91 return r;
92 }
93
94 r = sd_netlink_call(*rtnl, message, 0, NULL);
95 if (r < 0)
96 return r;
97
98 return 0;
99 }
100
101 int rtnl_message_new_synthetic_error(int error, uint32_t serial, sd_netlink_message **ret) {
102 struct nlmsgerr *err;
103 int r;
104
105 assert(error <= 0);
106
107 r = message_new(NULL, ret, NLMSG_ERROR);
108 if (r < 0)
109 return r;
110
111 (*ret)->hdr->nlmsg_seq = serial;
112
113 err = NLMSG_DATA((*ret)->hdr);
114
115 err->error = error;
116
117 return 0;
118 }
119
120 int rtnl_log_parse_error(int r) {
121 return log_error_errno(r, "Failed to parse netlink message: %m");
122 }
123
124 int rtnl_log_create_error(int r) {
125 return log_error_errno(r, "Failed to create netlink message: %m");
126 }