| 1 | /* Determine whether interfaces use native transport. Linux version. |
| 2 | Copyright (C) 2007-2020 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <assert.h> |
| 20 | #include <errno.h> |
| 21 | #include <ifaddrs.h> |
| 22 | #include <stddef.h> |
| 23 | #include <stdint.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <time.h> |
| 27 | #include <unistd.h> |
| 28 | #include <net/if.h> |
| 29 | #include <net/if_arp.h> |
| 30 | #include <sys/ioctl.h> |
| 31 | |
| 32 | #include <asm/types.h> |
| 33 | #include <linux/netlink.h> |
| 34 | #include <linux/rtnetlink.h> |
| 35 | |
| 36 | #include <not-cancel.h> |
| 37 | |
| 38 | #include "netlinkaccess.h" |
| 39 | |
| 40 | void |
| 41 | __check_native (uint32_t a1_index, int *a1_native, |
| 42 | uint32_t a2_index, int *a2_native) |
| 43 | { |
| 44 | int fd = __socket (PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE); |
| 45 | |
| 46 | struct sockaddr_nl nladdr; |
| 47 | memset (&nladdr, '\0', sizeof (nladdr)); |
| 48 | nladdr.nl_family = AF_NETLINK; |
| 49 | |
| 50 | socklen_t addr_len = sizeof (nladdr); |
| 51 | |
| 52 | if (fd < 0 |
| 53 | || __bind (fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) != 0 |
| 54 | || __getsockname (fd, (struct sockaddr *) &nladdr, &addr_len) != 0) |
| 55 | return; |
| 56 | |
| 57 | pid_t pid = nladdr.nl_pid; |
| 58 | struct req |
| 59 | { |
| 60 | struct nlmsghdr nlh; |
| 61 | struct rtgenmsg g; |
| 62 | /* struct rtgenmsg consists of a single byte. This means there |
| 63 | are three bytes of padding included in the REQ definition. |
| 64 | We make them explicit here. */ |
| 65 | char pad[3]; |
| 66 | } req; |
| 67 | |
| 68 | req.nlh.nlmsg_len = sizeof (req); |
| 69 | req.nlh.nlmsg_type = RTM_GETLINK; |
| 70 | req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST; |
| 71 | req.nlh.nlmsg_pid = 0; |
| 72 | req.nlh.nlmsg_seq = time_now (); |
| 73 | req.g.rtgen_family = AF_UNSPEC; |
| 74 | |
| 75 | assert (sizeof (req) - offsetof (struct req, pad) == 3); |
| 76 | memset (req.pad, '\0', sizeof (req.pad)); |
| 77 | |
| 78 | memset (&nladdr, '\0', sizeof (nladdr)); |
| 79 | nladdr.nl_family = AF_NETLINK; |
| 80 | |
| 81 | #ifdef PAGE_SIZE |
| 82 | /* Help the compiler optimize out the malloc call if PAGE_SIZE |
| 83 | is constant and smaller or equal to PTHREAD_STACK_MIN/4. */ |
| 84 | const size_t buf_size = PAGE_SIZE; |
| 85 | #else |
| 86 | const size_t buf_size = __getpagesize (); |
| 87 | #endif |
| 88 | bool use_malloc = false; |
| 89 | char *buf; |
| 90 | |
| 91 | if (__libc_use_alloca (buf_size)) |
| 92 | buf = alloca (buf_size); |
| 93 | else |
| 94 | { |
| 95 | buf = malloc (buf_size); |
| 96 | if (buf != NULL) |
| 97 | use_malloc = true; |
| 98 | else |
| 99 | goto out_fail; |
| 100 | } |
| 101 | |
| 102 | struct iovec iov = { buf, buf_size }; |
| 103 | |
| 104 | if (TEMP_FAILURE_RETRY (__sendto (fd, (void *) &req, sizeof (req), 0, |
| 105 | (struct sockaddr *) &nladdr, |
| 106 | sizeof (nladdr))) < 0) |
| 107 | goto out_fail; |
| 108 | |
| 109 | bool done = false; |
| 110 | do |
| 111 | { |
| 112 | struct msghdr msg = |
| 113 | { |
| 114 | .msg_name = (void *) &nladdr, |
| 115 | .msg_namelen = sizeof (nladdr), |
| 116 | .msg_iov = &iov, |
| 117 | .msg_iovlen = 1, |
| 118 | .msg_control = NULL, |
| 119 | .msg_controllen = 0, |
| 120 | .msg_flags = 0 |
| 121 | }; |
| 122 | |
| 123 | ssize_t read_len = TEMP_FAILURE_RETRY (__recvmsg (fd, &msg, 0)); |
| 124 | __netlink_assert_response (fd, read_len); |
| 125 | if (read_len < 0) |
| 126 | goto out_fail; |
| 127 | |
| 128 | if (msg.msg_flags & MSG_TRUNC) |
| 129 | goto out_fail; |
| 130 | |
| 131 | struct nlmsghdr *nlmh; |
| 132 | for (nlmh = (struct nlmsghdr *) buf; |
| 133 | NLMSG_OK (nlmh, (size_t) read_len); |
| 134 | nlmh = (struct nlmsghdr *) NLMSG_NEXT (nlmh, read_len)) |
| 135 | { |
| 136 | if (nladdr.nl_pid != 0 || (pid_t) nlmh->nlmsg_pid != pid |
| 137 | || nlmh->nlmsg_seq != req.nlh.nlmsg_seq) |
| 138 | continue; |
| 139 | |
| 140 | if (nlmh->nlmsg_type == RTM_NEWLINK) |
| 141 | { |
| 142 | struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlmh); |
| 143 | int native = (ifim->ifi_type != ARPHRD_TUNNEL6 |
| 144 | && ifim->ifi_type != ARPHRD_TUNNEL |
| 145 | && ifim->ifi_type != ARPHRD_SIT); |
| 146 | |
| 147 | if (a1_index == ifim->ifi_index) |
| 148 | { |
| 149 | *a1_native = native; |
| 150 | a1_index = 0xffffffffu; |
| 151 | } |
| 152 | if (a2_index == ifim->ifi_index) |
| 153 | { |
| 154 | *a2_native = native; |
| 155 | a2_index = 0xffffffffu; |
| 156 | } |
| 157 | |
| 158 | if (a1_index == 0xffffffffu |
| 159 | && a2_index == 0xffffffffu) |
| 160 | goto out; |
| 161 | } |
| 162 | else if (nlmh->nlmsg_type == NLMSG_DONE) |
| 163 | /* We found the end, leave the loop. */ |
| 164 | done = true; |
| 165 | } |
| 166 | } |
| 167 | while (! done); |
| 168 | |
| 169 | out: |
| 170 | __close_nocancel_nostatus (fd); |
| 171 | |
| 172 | return; |
| 173 | |
| 174 | out_fail: |
| 175 | if (use_malloc) |
| 176 | free (buf); |
| 177 | } |
| 178 | |