check_pf.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /* Determine protocol families for which interfaces exist. Linux version.
  2. Copyright (C) 2003-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <assert.h>
  16. #include <errno.h>
  17. #include <ifaddrs.h>
  18. #include <netdb.h>
  19. #include <stddef.h>
  20. #include <string.h>
  21. #include <time.h>
  22. #include <unistd.h>
  23. #include <stdint.h>
  24. #include <sys/socket.h>
  25. #include <asm/types.h>
  26. #include <linux/netlink.h>
  27. #include <linux/rtnetlink.h>
  28. #include <not-cancel.h>
  29. #include <libc-lock.h>
  30. #include <atomic.h>
  31. #include <nscd/nscd-client.h>
  32. #include "netlinkaccess.h"
  33. #ifndef IFA_F_HOMEADDRESS
  34. # define IFA_F_HOMEADDRESS 0
  35. #endif
  36. #ifndef IFA_F_OPTIMISTIC
  37. # define IFA_F_OPTIMISTIC 0
  38. #endif
  39. struct cached_data
  40. {
  41. uint32_t timestamp;
  42. uint32_t usecnt;
  43. bool seen_ipv4;
  44. bool seen_ipv6;
  45. size_t in6ailen;
  46. struct in6addrinfo in6ai[0];
  47. };
  48. static struct cached_data noai6ai_cached =
  49. {
  50. .usecnt = 1, /* Make sure we never try to delete this entry. */
  51. .in6ailen = 0
  52. };
  53. static struct cached_data *cache;
  54. __libc_lock_define_initialized (static, lock);
  55. #if IS_IN (nscd)
  56. static uint32_t nl_timestamp;
  57. uint32_t
  58. __bump_nl_timestamp (void)
  59. {
  60. if (atomic_increment_val (&nl_timestamp) == 0)
  61. atomic_increment (&nl_timestamp);
  62. return nl_timestamp;
  63. }
  64. #endif
  65. static inline uint32_t
  66. get_nl_timestamp (void)
  67. {
  68. #if IS_IN (nscd)
  69. return nl_timestamp;
  70. #elif defined USE_NSCD
  71. return __nscd_get_nl_timestamp ();
  72. #else
  73. return 0;
  74. #endif
  75. }
  76. static inline bool
  77. cache_valid_p (void)
  78. {
  79. if (cache != NULL)
  80. {
  81. uint32_t timestamp = get_nl_timestamp ();
  82. return timestamp != 0 && cache->timestamp == timestamp;
  83. }
  84. return false;
  85. }
  86. static struct cached_data *
  87. make_request (int fd, pid_t pid)
  88. {
  89. struct cached_data *result = NULL;
  90. size_t result_len = 0;
  91. size_t result_cap = 32;
  92. struct req
  93. {
  94. struct nlmsghdr nlh;
  95. struct rtgenmsg g;
  96. /* struct rtgenmsg consists of a single byte. This means there
  97. are three bytes of padding included in the REQ definition.
  98. We make them explicit here. */
  99. char pad[3];
  100. } req;
  101. struct sockaddr_nl nladdr;
  102. req.nlh.nlmsg_len = sizeof (req);
  103. req.nlh.nlmsg_type = RTM_GETADDR;
  104. req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
  105. req.nlh.nlmsg_pid = 0;
  106. req.nlh.nlmsg_seq = time (NULL);
  107. req.g.rtgen_family = AF_UNSPEC;
  108. assert (sizeof (req) - offsetof (struct req, pad) == 3);
  109. memset (req.pad, '\0', sizeof (req.pad));
  110. memset (&nladdr, '\0', sizeof (nladdr));
  111. nladdr.nl_family = AF_NETLINK;
  112. #ifdef PAGE_SIZE
  113. const size_t buf_size = PAGE_SIZE;
  114. #else
  115. const size_t buf_size = 4096;
  116. #endif
  117. char buf[buf_size];
  118. struct iovec iov = { buf, buf_size };
  119. if (TEMP_FAILURE_RETRY (__sendto (fd, (void *) &req, sizeof (req), 0,
  120. (struct sockaddr *) &nladdr,
  121. sizeof (nladdr))) < 0)
  122. goto out_fail;
  123. bool done = false;
  124. bool seen_ipv4 = false;
  125. bool seen_ipv6 = false;
  126. do
  127. {
  128. struct msghdr msg =
  129. {
  130. .msg_name = (void *) &nladdr,
  131. .msg_namelen = sizeof (nladdr),
  132. .msg_iov = &iov,
  133. .msg_iovlen = 1,
  134. .msg_control = NULL,
  135. .msg_controllen = 0,
  136. .msg_flags = 0
  137. };
  138. ssize_t read_len = TEMP_FAILURE_RETRY (__recvmsg (fd, &msg, 0));
  139. __netlink_assert_response (fd, read_len);
  140. if (read_len < 0)
  141. goto out_fail;
  142. if (msg.msg_flags & MSG_TRUNC)
  143. goto out_fail;
  144. struct nlmsghdr *nlmh;
  145. for (nlmh = (struct nlmsghdr *) buf;
  146. NLMSG_OK (nlmh, (size_t) read_len);
  147. nlmh = (struct nlmsghdr *) NLMSG_NEXT (nlmh, read_len))
  148. {
  149. if (nladdr.nl_pid != 0 || (pid_t) nlmh->nlmsg_pid != pid
  150. || nlmh->nlmsg_seq != req.nlh.nlmsg_seq)
  151. continue;
  152. if (nlmh->nlmsg_type == RTM_NEWADDR)
  153. {
  154. struct ifaddrmsg *ifam = (struct ifaddrmsg *) NLMSG_DATA (nlmh);
  155. struct rtattr *rta = IFA_RTA (ifam);
  156. size_t len = nlmh->nlmsg_len - NLMSG_LENGTH (sizeof (*ifam));
  157. if (ifam->ifa_family != AF_INET
  158. && ifam->ifa_family != AF_INET6)
  159. continue;
  160. const void *local = NULL;
  161. const void *address = NULL;
  162. while (RTA_OK (rta, len))
  163. {
  164. switch (rta->rta_type)
  165. {
  166. case IFA_LOCAL:
  167. local = RTA_DATA (rta);
  168. break;
  169. case IFA_ADDRESS:
  170. address = RTA_DATA (rta);
  171. goto out;
  172. }
  173. rta = RTA_NEXT (rta, len);
  174. }
  175. if (local != NULL)
  176. {
  177. address = local;
  178. out:
  179. if (ifam->ifa_family == AF_INET)
  180. {
  181. if (*(const in_addr_t *) address
  182. != htonl (INADDR_LOOPBACK))
  183. seen_ipv4 = true;
  184. }
  185. else
  186. {
  187. if (!IN6_IS_ADDR_LOOPBACK (address))
  188. seen_ipv6 = true;
  189. }
  190. }
  191. if (result_len == 0 || result_len == result_cap)
  192. {
  193. result_cap = 2 * result_cap;
  194. result = realloc (result, sizeof (*result)
  195. + result_cap
  196. * sizeof (struct in6addrinfo));
  197. }
  198. if (!result)
  199. goto out_fail;
  200. struct in6addrinfo *info = &result->in6ai[result_len++];
  201. info->flags = (((ifam->ifa_flags
  202. & (IFA_F_DEPRECATED | IFA_F_OPTIMISTIC))
  203. ? in6ai_deprecated : 0)
  204. | ((ifam->ifa_flags & IFA_F_HOMEADDRESS)
  205. ? in6ai_homeaddress : 0));
  206. info->prefixlen = ifam->ifa_prefixlen;
  207. info->index = ifam->ifa_index;
  208. if (ifam->ifa_family == AF_INET)
  209. {
  210. info->addr[0] = 0;
  211. info->addr[1] = 0;
  212. info->addr[2] = htonl (0xffff);
  213. info->addr[3] = *(const in_addr_t *) address;
  214. }
  215. else
  216. memcpy (info->addr, address, sizeof (info->addr));
  217. }
  218. else if (nlmh->nlmsg_type == NLMSG_DONE)
  219. /* We found the end, leave the loop. */
  220. done = true;
  221. }
  222. }
  223. while (! done);
  224. if (seen_ipv6 && result != NULL)
  225. {
  226. result->timestamp = get_nl_timestamp ();
  227. result->usecnt = 2;
  228. result->seen_ipv4 = seen_ipv4;
  229. result->seen_ipv6 = true;
  230. result->in6ailen = result_len;
  231. }
  232. else
  233. {
  234. free (result);
  235. atomic_add (&noai6ai_cached.usecnt, 2);
  236. noai6ai_cached.seen_ipv4 = seen_ipv4;
  237. noai6ai_cached.seen_ipv6 = seen_ipv6;
  238. result = &noai6ai_cached;
  239. }
  240. return result;
  241. out_fail:
  242. free (result);
  243. return NULL;
  244. }
  245. void
  246. attribute_hidden
  247. __check_pf (bool *seen_ipv4, bool *seen_ipv6,
  248. struct in6addrinfo **in6ai, size_t *in6ailen)
  249. {
  250. *in6ai = NULL;
  251. *in6ailen = 0;
  252. struct cached_data *olddata = NULL;
  253. struct cached_data *data = NULL;
  254. __libc_lock_lock (lock);
  255. if (cache_valid_p ())
  256. {
  257. data = cache;
  258. atomic_increment (&cache->usecnt);
  259. }
  260. else
  261. {
  262. int fd = __socket (PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
  263. if (__glibc_likely (fd >= 0))
  264. {
  265. struct sockaddr_nl nladdr;
  266. memset (&nladdr, '\0', sizeof (nladdr));
  267. nladdr.nl_family = AF_NETLINK;
  268. socklen_t addr_len = sizeof (nladdr);
  269. if (__bind (fd, (struct sockaddr *) &nladdr, sizeof (nladdr)) == 0
  270. && __getsockname (fd, (struct sockaddr *) &nladdr,
  271. &addr_len) == 0)
  272. data = make_request (fd, nladdr.nl_pid);
  273. __close_nocancel_nostatus (fd);
  274. }
  275. if (data != NULL)
  276. {
  277. olddata = cache;
  278. cache = data;
  279. }
  280. }
  281. __libc_lock_unlock (lock);
  282. if (data != NULL)
  283. {
  284. /* It worked. */
  285. *seen_ipv4 = data->seen_ipv4;
  286. *seen_ipv6 = data->seen_ipv6;
  287. *in6ailen = data->in6ailen;
  288. *in6ai = data->in6ai;
  289. if (olddata != NULL && olddata->usecnt > 0
  290. && atomic_add_zero (&olddata->usecnt, -1))
  291. free (olddata);
  292. return;
  293. }
  294. /* We cannot determine what interfaces are available. Be
  295. pessimistic. */
  296. *seen_ipv4 = true;
  297. *seen_ipv6 = true;
  298. }
  299. /* Free the cache if it has been allocated. */
  300. libc_freeres_fn (freecache)
  301. {
  302. if (cache)
  303. __free_in6ai (cache->in6ai);
  304. }
  305. void
  306. __free_in6ai (struct in6addrinfo *ai)
  307. {
  308. if (ai != NULL)
  309. {
  310. struct cached_data *data =
  311. (struct cached_data *) ((char *) ai
  312. - offsetof (struct cached_data, in6ai));
  313. if (atomic_add_zero (&data->usecnt, -1))
  314. {
  315. __libc_lock_lock (lock);
  316. if (data->usecnt == 0)
  317. /* Still unused. */
  318. free (data);
  319. __libc_lock_unlock (lock);
  320. }
  321. }
  322. }