1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
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.
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.
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/>.
22 #include <linux/rtnetlink.h>
23 #include <netinet/ether.h>
27 #include "rtnl-util.h"
28 #include "rtnl-internal.h"
30 int rtnl_set_link_name(sd_rtnl *rtnl, int ifindex, const char *name) {
31 _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *message = NULL;
38 r = sd_rtnl_message_link_new(RTM_SETLINK, ifindex, &message);
42 r = sd_rtnl_message_append_string(message, IFLA_IFNAME, name);
46 r = sd_rtnl_call(rtnl, message, 0, NULL);
53 int rtnl_set_link_properties(sd_rtnl *rtnl, int ifindex, const char *alias,
54 const struct ether_addr *mac, unsigned mtu) {
55 _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *message = NULL;
56 bool need_update = false;
62 if (!alias && !mac && mtu == 0)
65 r = sd_rtnl_message_link_new(RTM_SETLINK, ifindex, &message);
70 r = sd_rtnl_message_append_string(message, IFLA_IFALIAS, alias);
79 r = sd_rtnl_message_append_ether_addr(message, IFLA_ADDRESS, mac);
87 r = sd_rtnl_message_append_u32(message, IFLA_MTU, mtu);
95 r = sd_rtnl_call(rtnl, message, 0, NULL);
103 int rtnl_message_new_synthetic_error(int error, uint32_t serial, sd_rtnl_message **ret) {
104 struct nlmsgerr *err;
109 r = message_new(ret, NLMSG_SPACE(sizeof(struct nlmsgerr)));
113 (*ret)->hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
114 (*ret)->hdr->nlmsg_type = NLMSG_ERROR;
115 (*ret)->hdr->nlmsg_seq = serial;
117 err = NLMSG_DATA((*ret)->hdr);
124 bool rtnl_message_type_is_route(uint16_t type) {
135 bool rtnl_message_type_is_link(uint16_t type) {
147 bool rtnl_message_type_is_addr(uint16_t type) {
158 int rtnl_message_link_get_ifname(sd_rtnl_message *message, const char **ret) {
162 assert(rtnl_message_type_is_link(message->hdr->nlmsg_type));
164 while (sd_rtnl_message_read(message, &type, &name)) {
165 if (type == IFLA_IFNAME) {