1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
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.
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.
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/>.
21 #include "sd-netlink.h"
23 #include "netlink-internal.h"
24 #include "netlink-util.h"
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
;
35 r
= sd_netlink_open(rtnl
);
40 r
= sd_rtnl_message_new_link(*rtnl
, &message
, RTM_SETLINK
, ifindex
);
44 r
= sd_netlink_message_append_string(message
, IFLA_IFNAME
, name
);
48 r
= sd_netlink_call(*rtnl
, message
, 0, NULL
);
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
;
63 if (!alias
&& !mac
&& mtu
== 0)
67 r
= sd_netlink_open(rtnl
);
72 r
= sd_rtnl_message_new_link(*rtnl
, &message
, RTM_SETLINK
, ifindex
);
77 r
= sd_netlink_message_append_string(message
, IFLA_IFALIAS
, alias
);
83 r
= sd_netlink_message_append_ether_addr(message
, IFLA_ADDRESS
, mac
);
89 r
= sd_netlink_message_append_u32(message
, IFLA_MTU
, mtu
);
94 r
= sd_netlink_call(*rtnl
, message
, 0, NULL
);
101 int rtnl_message_new_synthetic_error(int error
, uint32_t serial
, sd_netlink_message
**ret
) {
102 struct nlmsgerr
*err
;
107 r
= message_new(NULL
, ret
, NLMSG_ERROR
);
111 (*ret
)->hdr
->nlmsg_seq
= serial
;
113 err
= NLMSG_DATA((*ret
)->hdr
);
120 int rtnl_log_parse_error(int r
) {
121 return log_error_errno(r
, "Failed to parse netlink message: %m");
124 int rtnl_log_create_error(int r
) {
125 return log_error_errno(r
, "Failed to create netlink message: %m");