1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 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 <netinet/ether.h>
27 #include "socket-util.h"
29 static void test_link_configure(sd_rtnl *rtnl, int ifindex) {
30 _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *message;
32 const char *mac = "98:fe:94:3f:c6:18", *name = "test";
33 unsigned int mtu = 1450;
36 /* we'd really like to test NEWLINK, but let's not mess with the running kernel */
37 assert(sd_rtnl_message_link_new(RTM_GETLINK, ifindex, 0, 0, &message) >= 0);
38 assert(sd_rtnl_message_append(message, IFLA_IFNAME, name) >= 0);
39 assert(sd_rtnl_message_append(message, IFLA_ADDRESS, ether_aton(mac)) >= 0);
40 assert(sd_rtnl_message_append(message, IFLA_MTU, &mtu) >= 0);
42 assert(sd_rtnl_message_read(message, &type, &data) >= 0);
43 assert(type == IFLA_IFNAME);
44 assert(streq(name, (char *) data));
46 assert(sd_rtnl_message_read(message, &type, &data) >= 0);
47 assert(type == IFLA_ADDRESS);
48 assert(streq(mac, ether_ntoa(data)));
50 assert(sd_rtnl_message_read(message, &type, &data) >= 0);
51 assert(type == IFLA_MTU);
52 assert(mtu == *(unsigned int *) data);
54 /* let's assume that this test is always ran when the loopback device is up, so that it will fail */
55 assert(sd_rtnl_send_with_reply_and_block(rtnl, message, 2 * USEC_PER_SEC, NULL) == 0);
66 unsigned int *mtu_reply;
68 assert(sd_rtnl_open(0, &rtnl) >= 0);
71 if_loopback = (int) if_nametoindex("lo");
72 assert(if_loopback > 0);
74 test_link_configure(rtnl, if_loopback);
76 assert(sd_rtnl_message_link_new(RTM_GETLINK, if_loopback, 0, 0, &m) >= 0);
79 assert(sd_rtnl_message_get_type(m, &type) >= 0);
80 assert(type == RTM_GETLINK);
82 assert(sd_rtnl_message_read(m, &type, &data) == 0);
84 assert(sd_rtnl_send_with_reply_and_block(rtnl, m, 100000000, &r) >= 0);
85 assert(sd_rtnl_message_get_type(r, &type) >= 0);
86 assert(type == RTM_NEWLINK);
88 assert(sd_rtnl_message_read(m, &type, data) == 0);
89 assert((r = sd_rtnl_message_unref(r)) == NULL);
91 assert(sd_rtnl_send_with_reply_and_block(rtnl, m, -1, &r) == -EPERM);
92 assert((m = sd_rtnl_message_unref(m)) == NULL);
93 assert((r = sd_rtnl_message_unref(r)) == NULL);
95 assert(sd_rtnl_message_link_new(RTM_GETLINK, if_loopback, 0, 0, &m) >= 0);
98 assert(sd_rtnl_message_append(m, IFLA_MTU, &mtu) >= 0);
99 assert(sd_rtnl_message_read(m, &type, (void **) &mtu_reply) == 1);
101 assert(type == IFLA_MTU);
102 assert(*mtu_reply == 0);
104 assert(sd_rtnl_message_read(m, &type, data) == 0);
106 assert(sd_rtnl_send_with_reply_and_block(rtnl, m, -1, &r) >= 0);
107 while (sd_rtnl_message_read(r, &type, &data)) {
110 assert(*(unsigned int *) data == 65536);
113 assert(streq((char *) data, "lo"));
116 assert(streq((char *) data, "noqueue"));
121 assert((m = sd_rtnl_message_unref(m)) == NULL);
122 assert((r = sd_rtnl_message_unref(r)) == NULL);
123 assert((rtnl = sd_rtnl_unref(rtnl)) == NULL);