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