.
[glibc/history.git] / sysdeps / unix / sysv / linux / check_native.c
blob6e6624abe105d60637280a80915e8e4af8558259
1 /* Determine whether interfaces use native transport. Linux version.
2 Copyright (C) 2007 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <assert.h>
21 #include <errno.h>
22 #include <ifaddrs.h>
23 #include <stddef.h>
24 #include <stdint.h>
25 #include <stdlib.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>
32 #include <asm/types.h>
33 #include <linux/netlink.h>
34 #include <linux/rtnetlink.h>
36 #include <not-cancel.h>
39 void
40 __check_native (uint32_t a1_index, int *a1_native,
41 uint32_t a2_index, int *a2_native)
43 int fd = __socket (PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
45 struct sockaddr_nl nladdr;
46 memset (&nladdr, '\0', sizeof (nladdr));
47 nladdr.nl_family = AF_NETLINK;
49 socklen_t addr_len = sizeof (nladdr);
51 if (fd < 0
52 || __bind (fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) != 0
53 || __getsockname (fd, (struct sockaddr *) &nladdr, &addr_len) != 0)
54 return;
56 pid_t pid = nladdr.nl_pid;
57 struct req
59 struct nlmsghdr nlh;
60 struct rtgenmsg g;
61 /* struct rtgenmsg consists of a single byte. This means there
62 are three bytes of padding included in the REQ definition.
63 We make them explicit here. */
64 char pad[3];
65 } req;
67 req.nlh.nlmsg_len = sizeof (req);
68 req.nlh.nlmsg_type = RTM_GETLINK;
69 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
70 req.nlh.nlmsg_pid = 0;
71 req.nlh.nlmsg_seq = time (NULL);
72 req.g.rtgen_family = AF_UNSPEC;
74 assert (sizeof (req) - offsetof (struct req, pad) == 3);
75 memset (req.pad, '\0', sizeof (req.pad));
77 memset (&nladdr, '\0', sizeof (nladdr));
78 nladdr.nl_family = AF_NETLINK;
80 #ifdef PAGE_SIZE
81 /* Help the compiler optimize out the malloc call if PAGE_SIZE
82 is constant and smaller or equal to PTHREAD_STACK_MIN/4. */
83 const size_t buf_size = PAGE_SIZE;
84 #else
85 const size_t buf_size = __getpagesize ();
86 #endif
87 bool use_malloc = false;
88 char *buf;
90 if (__libc_use_alloca (buf_size))
91 buf = alloca (buf_size);
92 else
94 buf = malloc (buf_size);
95 if (buf != NULL)
96 use_malloc = true;
97 else
98 goto out_fail;
101 struct iovec iov = { buf, buf_size };
103 if (TEMP_FAILURE_RETRY (__sendto (fd, (void *) &req, sizeof (req), 0,
104 (struct sockaddr *) &nladdr,
105 sizeof (nladdr))) < 0)
106 goto out_fail;
108 bool done = false;
111 struct msghdr msg =
113 (void *) &nladdr, sizeof (nladdr),
114 &iov, 1,
115 NULL, 0,
119 ssize_t read_len = TEMP_FAILURE_RETRY (__recvmsg (fd, &msg, 0));
120 if (read_len < 0)
121 goto out_fail;
123 if (msg.msg_flags & MSG_TRUNC)
124 goto out_fail;
126 struct nlmsghdr *nlmh;
127 for (nlmh = (struct nlmsghdr *) buf;
128 NLMSG_OK (nlmh, (size_t) read_len);
129 nlmh = (struct nlmsghdr *) NLMSG_NEXT (nlmh, read_len))
131 if (nladdr.nl_pid != 0 || (pid_t) nlmh->nlmsg_pid != pid
132 || nlmh->nlmsg_seq != req.nlh.nlmsg_seq)
133 continue;
135 if (nlmh->nlmsg_type == RTM_NEWLINK)
137 struct ifinfomsg *ifim = (struct ifinfomsg *) NLMSG_DATA (nlmh);
138 int native = (ifim->ifi_type != ARPHRD_TUNNEL6
139 && ifim->ifi_type != ARPHRD_TUNNEL
140 && ifim->ifi_type != ARPHRD_SIT);
142 if (a1_index == ifim->ifi_index)
144 *a1_native = native;
145 a1_index = 0xffffffffu;
147 if (a2_index == ifim->ifi_index)
149 *a2_native = native;
150 a2_index = 0xffffffffu;
153 if (a1_index == 0xffffffffu
154 && a2_index == 0xffffffffu)
155 goto out;
157 else if (nlmh->nlmsg_type == NLMSG_DONE)
158 /* We found the end, leave the loop. */
159 done = true;
162 while (! done);
164 out:
165 close_not_cancel_no_status (fd);
167 return;
169 out_fail:
170 if (use_malloc)
171 free (buf);