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