]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd/sd-rtnl/rtnl-util.c
sd-dhcp-client/net-util: make netmask_to_prefixlen generic
[thirdparty/systemd.git] / src / libsystemd / sd-rtnl / rtnl-util.c
CommitLineData
d8921c6d
TG
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
a33dece5 22#include <linux/rtnetlink.h>
d8921c6d
TG
23#include <netinet/ether.h>
24
25#include "sd-rtnl.h"
26
27#include "rtnl-util.h"
28
3e137a1b
TG
29int rtnl_set_link_name(sd_rtnl *rtnl, int ifindex, const char *name) {
30 _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *message = NULL;
d8921c6d
TG
31 int r;
32
33 assert(rtnl);
34 assert(ifindex > 0);
3e137a1b 35 assert(name);
d8921c6d 36
fc25d7f8 37 r = sd_rtnl_message_link_new(RTM_SETLINK, ifindex, &message);
d8921c6d
TG
38 if (r < 0)
39 return r;
40
0a0dc69b 41 r = sd_rtnl_message_append_string(message, IFLA_IFNAME, name);
3e137a1b
TG
42 if (r < 0)
43 return r;
d8921c6d 44
fe4824e0 45 r = sd_rtnl_call(rtnl, message, 0, NULL);
3e137a1b
TG
46 if (r < 0)
47 return r;
48
49 return 0;
50}
51
d2df0d0e
TG
52int rtnl_set_link_properties(sd_rtnl *rtnl, int ifindex, const char *alias,
53 const struct ether_addr *mac, unsigned mtu) {
3e137a1b
TG
54 _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *message = NULL;
55 bool need_update = false;
56 int r;
57
58 assert(rtnl);
59 assert(ifindex > 0);
60
d2df0d0e 61 if (!alias && !mac && mtu == 0)
3e137a1b
TG
62 return 0;
63
fc25d7f8 64 r = sd_rtnl_message_link_new(RTM_SETLINK, ifindex, &message);
3e137a1b
TG
65 if (r < 0)
66 return r;
d8921c6d 67
d2df0d0e 68 if (alias) {
0a0dc69b 69 r = sd_rtnl_message_append_string(message, IFLA_IFALIAS, alias);
d2df0d0e
TG
70 if (r < 0)
71 return r;
72
73 need_update = true;
74
75 }
76
d8921c6d 77 if (mac) {
0a0dc69b 78 r = sd_rtnl_message_append_ether_addr(message, IFLA_ADDRESS, mac);
d8921c6d
TG
79 if (r < 0)
80 return r;
81
82 need_update = true;
83 }
84
85 if (mtu > 0) {
0a0dc69b 86 r = sd_rtnl_message_append_u32(message, IFLA_MTU, mtu);
d8921c6d
TG
87 if (r < 0)
88 return r;
89
90 need_update = true;
91 }
92
93 if (need_update) {
fe4824e0 94 r = sd_rtnl_call(rtnl, message, 0, NULL);
d8921c6d
TG
95 if (r < 0)
96 return r;
97 }
98
99 return 0;
100}