Matus Marhefka | 1a48719 | 2014-10-02 16:17:02 +0200 | [diff] [blame] | 1 | /* Copyright (c) 2014 Red Hat, Inc. |
| 2 | * |
| 3 | * This program is free software: you can redistribute it and/or modify |
| 4 | * it under the terms of version 2 the GNU General Public License as |
| 5 | * published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | * You should have received a copy of the GNU General Public License |
| 13 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 14 | *********************************************************************** |
| 15 | * File: netns_netlink.c |
| 16 | * |
| 17 | * Tests a netlink interface inside a new network namespace. |
| 18 | * Description: |
| 19 | * 1. Unshares a network namespace (so network related actions |
| 20 | * have no effect on a real system) |
| 21 | * 2. Forks a child which creates a NETLINK_ROUTE netlink socket |
| 22 | * and listens to RTMGRP_LINK (network interface create/delete/up/down) |
| 23 | * multicast group. |
| 24 | * 4. Child then waits for parent approval to receive data from socket |
| 25 | * 3. Parent creates a new TAP interface (dummy0) and immediately |
| 26 | * removes it (which should generate some data in child's netlink socket). |
| 27 | * Then it allows child to continue. |
| 28 | * 4. As the child was listening to RTMGRP_LINK multicast group, it should |
| 29 | * detect the new interface creation/deletion (by reading data from netlink |
| 30 | * socket), if so, the test passes, otherwise it fails. |
| 31 | */ |
| 32 | |
| 33 | #define _GNU_SOURCE |
| 34 | #include <sys/wait.h> |
| 35 | #include <asm/types.h> |
| 36 | #include <sys/socket.h> |
| 37 | #include <linux/netlink.h> |
| 38 | #include <linux/rtnetlink.h> |
| 39 | #include <unistd.h> |
| 40 | #include <stdio.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <errno.h> |
Matus Marhefka | 1a48719 | 2014-10-02 16:17:02 +0200 | [diff] [blame] | 43 | #include "test.h" |
| 44 | #include "safe_macros.h" |
| 45 | #include "netns_helper.h" |
| 46 | |
| 47 | |
| 48 | #define MAX_TRIES 1000 |
Xing Gu | bb0ae55 | 2014-12-05 15:00:58 +0800 | [diff] [blame] | 49 | #define IP_TUNTAP_MIN_VER 100519 |
| 50 | |
Matus Marhefka | 1a48719 | 2014-10-02 16:17:02 +0200 | [diff] [blame] | 51 | char *TCID = "netns_netlink"; |
| 52 | int TST_TOTAL = 1; |
| 53 | struct tst_checkpoint checkpoint; |
| 54 | |
| 55 | |
| 56 | static void cleanup(void) |
| 57 | { |
| 58 | tst_rmdir(); |
| 59 | } |
| 60 | |
| 61 | static void setup(void) |
| 62 | { |
| 63 | tst_require_root(NULL); |
Xing Gu | bb0ae55 | 2014-12-05 15:00:58 +0800 | [diff] [blame] | 64 | check_iproute(IP_TUNTAP_MIN_VER); |
Matus Marhefka | 1a48719 | 2014-10-02 16:17:02 +0200 | [diff] [blame] | 65 | check_netns(); |
| 66 | tst_tmpdir(); |
| 67 | TST_CHECKPOINT_CREATE(&checkpoint); |
| 68 | } |
| 69 | |
| 70 | int child_func(void) |
| 71 | { |
| 72 | int fd, len, event_found, tries; |
| 73 | struct sockaddr_nl sa; |
| 74 | char buffer[4096]; |
| 75 | struct nlmsghdr *nlh; |
| 76 | |
| 77 | /* child will listen to a network interface create/delete/up/down |
| 78 | * events */ |
| 79 | memset(&sa, 0, sizeof(sa)); |
| 80 | sa.nl_family = AF_NETLINK; |
| 81 | sa.nl_groups = RTMGRP_LINK; |
| 82 | |
| 83 | fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); |
| 84 | if (fd == -1) { |
| 85 | perror("socket"); |
| 86 | return 1; |
| 87 | } |
| 88 | if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) == -1) { |
| 89 | perror("bind"); |
| 90 | close(fd); |
| 91 | return 1; |
| 92 | } |
| 93 | |
| 94 | /* waits for parent to create an interface */ |
| 95 | TST_CHECKPOINT_CHILD_WAIT(&checkpoint); |
| 96 | |
| 97 | /* To get rid of "resource temporarily unavailable" errors |
| 98 | * when testing with -i option */ |
| 99 | tries = 0; |
| 100 | event_found = 0; |
| 101 | nlh = (struct nlmsghdr *) buffer; |
| 102 | while (tries < MAX_TRIES) { |
| 103 | len = recv(fd, nlh, sizeof(buffer), MSG_DONTWAIT); |
| 104 | if (len > 0) { |
| 105 | /* stop receiving only on interface create/delete |
| 106 | * event */ |
| 107 | if (nlh->nlmsg_type == RTM_NEWLINK || |
| 108 | nlh->nlmsg_type == RTM_DELLINK) { |
| 109 | event_found++; |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | usleep(10000); |
| 114 | tries++; |
| 115 | } |
| 116 | |
| 117 | close(fd); |
| 118 | |
| 119 | if (!event_found) { |
| 120 | perror("recv"); |
| 121 | return 1; |
| 122 | } |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static void test(void) |
| 128 | { |
| 129 | pid_t pid; |
| 130 | int status; |
| 131 | |
| 132 | /* unshares the network namespace */ |
| 133 | if (unshare(CLONE_NEWNET) == -1) |
| 134 | tst_brkm(TBROK | TERRNO, cleanup, "unshare failed"); |
| 135 | |
| 136 | pid = tst_fork(); |
| 137 | if (pid < 0) { |
| 138 | tst_brkm(TBROK | TERRNO, cleanup, "fork failed"); |
| 139 | } |
| 140 | if (pid == 0) { |
| 141 | _exit(child_func()); |
| 142 | } |
| 143 | |
| 144 | /* creates TAP network interface dummy0 */ |
| 145 | if (WEXITSTATUS(system("ip tuntap add dev dummy0 mode tap")) == -1) |
| 146 | tst_brkm(TBROK | TERRNO, cleanup, "system failed"); |
| 147 | |
| 148 | /* removes previously created dummy0 device */ |
| 149 | if (WEXITSTATUS(system("ip tuntap del mode tap dummy0")) == -1) |
| 150 | tst_brkm(TBROK | TERRNO, cleanup, "system failed"); |
| 151 | |
| 152 | /* allow child to continue */ |
| 153 | TST_CHECKPOINT_SIGNAL_CHILD(cleanup, &checkpoint); |
| 154 | |
| 155 | |
| 156 | SAFE_WAITPID(cleanup, pid, &status, 0); |
| 157 | if (WIFEXITED(status) && WEXITSTATUS(status) != 0) { |
| 158 | tst_resm(TFAIL, "netlink interface fail"); |
| 159 | return; |
| 160 | } |
| 161 | if (WIFSIGNALED(status)) { |
| 162 | tst_resm(TFAIL, "child was killed with signal %s", |
| 163 | tst_strsig(WTERMSIG(status))); |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | tst_resm(TPASS, "netlink interface pass"); |
| 168 | } |
| 169 | |
| 170 | int main(int argc, char *argv[]) |
| 171 | { |
| 172 | const char *msg; |
| 173 | int lc; |
| 174 | |
| 175 | msg = parse_opts(argc, argv, NULL, NULL); |
| 176 | if (msg != NULL) |
| 177 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 178 | |
| 179 | setup(); |
| 180 | |
| 181 | for (lc = 0; TEST_LOOPING(lc); lc++) |
| 182 | test(); |
| 183 | |
| 184 | cleanup(); |
| 185 | tst_exit(); |
| 186 | } |