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