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