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