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