]> git.proxmox.com Git - systemd.git/blame - src/libsystemd/sd-rtnl/rtnl-util.c
Imported Upstream version 217
[systemd.git] / src / libsystemd / sd-rtnl / rtnl-util.c
CommitLineData
60f067b4
JS
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
22#include <netinet/ether.h>
23
24#include "sd-rtnl.h"
25
26#include "rtnl-util.h"
27#include "rtnl-internal.h"
28
5eef597e 29int rtnl_set_link_name(sd_rtnl **rtnl, int ifindex, const char *name) {
60f067b4
JS
30 _cleanup_rtnl_message_unref_ sd_rtnl_message *message = NULL;
31 int r;
32
33 assert(rtnl);
34 assert(ifindex > 0);
35 assert(name);
36
5eef597e
MP
37 if (!*rtnl) {
38 r = sd_rtnl_open(rtnl, 0);
39 if (r < 0)
40 return r;
41 }
42
43 r = sd_rtnl_message_new_link(*rtnl, &message, RTM_SETLINK, ifindex);
60f067b4
JS
44 if (r < 0)
45 return r;
46
47 r = sd_rtnl_message_append_string(message, IFLA_IFNAME, name);
48 if (r < 0)
49 return r;
50
5eef597e 51 r = sd_rtnl_call(*rtnl, message, 0, NULL);
60f067b4
JS
52 if (r < 0)
53 return r;
54
55 return 0;
56}
57
5eef597e 58int rtnl_set_link_properties(sd_rtnl **rtnl, int ifindex, const char *alias,
60f067b4
JS
59 const struct ether_addr *mac, unsigned mtu) {
60 _cleanup_rtnl_message_unref_ sd_rtnl_message *message = NULL;
60f067b4
JS
61 int r;
62
63 assert(rtnl);
64 assert(ifindex > 0);
65
66 if (!alias && !mac && mtu == 0)
67 return 0;
68
5eef597e
MP
69 if (!*rtnl) {
70 r = sd_rtnl_open(rtnl, 0);
71 if (r < 0)
72 return r;
73 }
74
75 r = sd_rtnl_message_new_link(*rtnl, &message, RTM_SETLINK, ifindex);
60f067b4
JS
76 if (r < 0)
77 return r;
78
79 if (alias) {
80 r = sd_rtnl_message_append_string(message, IFLA_IFALIAS, alias);
81 if (r < 0)
82 return r;
60f067b4
JS
83 }
84
85 if (mac) {
86 r = sd_rtnl_message_append_ether_addr(message, IFLA_ADDRESS, mac);
87 if (r < 0)
88 return r;
60f067b4
JS
89 }
90
91 if (mtu > 0) {
92 r = sd_rtnl_message_append_u32(message, IFLA_MTU, mtu);
93 if (r < 0)
94 return r;
60f067b4
JS
95 }
96
5eef597e
MP
97 r = sd_rtnl_call(*rtnl, message, 0, NULL);
98 if (r < 0)
99 return r;
60f067b4
JS
100
101 return 0;
102}
103
104int rtnl_message_new_synthetic_error(int error, uint32_t serial, sd_rtnl_message **ret) {
105 struct nlmsgerr *err;
106 int r;
107
108 assert(error <= 0);
109
110 r = message_new(NULL, ret, NLMSG_SPACE(sizeof(struct nlmsgerr)));
111 if (r < 0)
112 return r;
113
114 (*ret)->hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
115 (*ret)->hdr->nlmsg_type = NLMSG_ERROR;
116 (*ret)->hdr->nlmsg_seq = serial;
117
118 err = NLMSG_DATA((*ret)->hdr);
119
120 err->error = error;
121
122 return 0;
123}
124
125bool rtnl_message_type_is_route(uint16_t type) {
126 switch (type) {
127 case RTM_NEWROUTE:
128 case RTM_GETROUTE:
129 case RTM_DELROUTE:
130 return true;
131 default:
132 return false;
133 }
134}
135
136bool rtnl_message_type_is_link(uint16_t type) {
137 switch (type) {
138 case RTM_NEWLINK:
139 case RTM_SETLINK:
140 case RTM_GETLINK:
141 case RTM_DELLINK:
142 return true;
143 default:
144 return false;
145 }
146}
147
148bool rtnl_message_type_is_addr(uint16_t type) {
149 switch (type) {
150 case RTM_NEWADDR:
151 case RTM_GETADDR:
152 case RTM_DELADDR:
153 return true;
154 default:
155 return false;
156 }
157}
5eef597e
MP
158
159int rtnl_log_parse_error(int r) {
160 log_error("Failed to parse netlink message: %s", strerror(-r));
161 return r;
162}
163
164int rtnl_log_create_error(int r) {
165 log_error("Failed to create netlink message: %s", strerror(-r));
166 return r;
167}