You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

342 lines
7.5 KiB

8 years ago
​
​
​
​
​
​
​
8 years ago
​
​
​
​
8 years ago
​
​
​
8 years ago
​
​
​
​
​
​
8 years ago
​
​
​
​
8 years ago
​
​
​
​
​
​
​
​
​
​
​
8 years ago
​
8 years ago
​
​
8 years ago
​
8 years ago
​
​
​
​
​
​
​
​
​
8 years ago
​
​
​
8 years ago
8 years ago
​
8 years ago
8 years ago
​
​
​
​
​
​
8 years ago
​
​
​
​
8 years ago
​
​
​
​
​
​
​
​
8 years ago
​
​
​
​
8 years ago
​
​
​
​
​
8 years ago
​
​
​
​
​
​
​
8 years ago
​
​
​
8 years ago
​
​
​
​
​
​
8 years ago
​
​
8 years ago
8 years ago
8 years ago
​
​
​
8 years ago
​
​
​
8 years ago
8 years ago
​
​
​
8 years ago
​
​
​
​
​
​
8 years ago
​
​
8 years ago
​
​
​
​
8 years ago
​
​
​
8 years ago
​
​
​
8 years ago
​
​
​
​
​
​
8 years ago
​
​
​
​
​
​
​
​
8 years ago
8 years ago
​
​
​
​
​
8 years ago
​
​
​
​
8 years ago
8 years ago
​
​
​
​
8 years ago
​
​
​
​
8 years ago
​
​
​
​
8 years ago
​
​
​
8 years ago
​
​
8 years ago
​
8 years ago
​
​
​
​
8 years ago
​
8 years ago
​
​
8 years ago
​
​
​
​
​
8 years ago
​
8 years ago
​
​
​
​
​
8 years ago
​
​
​
​
​
​
​
​
8 years ago
​
​
​
8 years ago
​
​
​
​
​
​
​
​
8 years ago
​
​
​
​
8 years ago
​
​
​
8 years ago
​
​
​
​
​
​
8 years ago
8 years ago
8 years ago
​
​
​
​
​
​
  1. /* $Id: getroute.c,v 1.4 2014/12/01 09:07:17 nanard Exp $ */
  2. /* MiniUPnP project
  3. * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
  4. * (c) 2006-2014 Thomas Bernard
  5. * This software is subject to the conditions detailed
  6. * in the LICENCE file provided within the distribution */
  7. #include <errno.h>
  8. #include <netinet/in.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <sys/socket.h>
  12. #include <sys/types.h>
  13. #include <syslog.h>
  14. #include <unistd.h>
  15. #ifdef __linux__
  16. /*#include <linux/in_route.h>*/
  17. #include <libnfnetlink/libnfnetlink.h>
  18. #include <linux/netlink.h>
  19. #include <linux/rtnetlink.h>
  20. #else /* __linux__ */
  21. #include <net/if.h>
  22. #include <net/route.h>
  23. #include <netinet/in.h>
  24. #ifdef AF_LINK
  25. #include <net/if_dl.h>
  26. #endif /* AF_LINK */
  27. #endif /* __linux__ */
  28. #include "getroute.h"
  29. #include "upnputils.h"
  30. #if defined(__sun)
  31. static size_t _sa_len(const struct sockaddr * addr)
  32. {
  33. if (addr->sa_family == AF_INET)
  34. {
  35. return (sizeof(struct sockaddr_in));
  36. }
  37. else if (addr->sa_family == AF_INET6)
  38. {
  39. return (sizeof(struct sockaddr_in6));
  40. }
  41. else
  42. {
  43. return (sizeof(struct sockaddr));
  44. }
  45. }
  46. #define SA_LEN(sa) (_sa_len(sa))
  47. #else
  48. #if ! defined(SA_LEN)
  49. #define SA_LEN(sa) ((sa)->sa_len)
  50. #endif
  51. #endif
  52. int get_src_for_route_to(const struct sockaddr * dst, void * src, size_t * src_len, int * index)
  53. {
  54. #if __linux__
  55. int fd = -1;
  56. struct nlmsghdr * h;
  57. int status;
  58. struct
  59. {
  60. struct nlmsghdr n;
  61. struct rtmsg r;
  62. char buf[1024];
  63. } req;
  64. struct sockaddr_nl nladdr;
  65. struct iovec iov = {
  66. .iov_base = (void *) &req.n,
  67. };
  68. struct msghdr msg = {
  69. .msg_name = &nladdr,
  70. .msg_namelen = sizeof(nladdr),
  71. .msg_iov = &iov,
  72. .msg_iovlen = 1,
  73. };
  74. const struct sockaddr_in * dst4;
  75. const struct sockaddr_in6 * dst6;
  76. memset(&req, 0, sizeof(req));
  77. req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
  78. req.n.nlmsg_flags = NLM_F_REQUEST;
  79. req.n.nlmsg_type = RTM_GETROUTE;
  80. req.r.rtm_family = dst->sa_family;
  81. req.r.rtm_table = 0;
  82. req.r.rtm_protocol = 0;
  83. req.r.rtm_scope = 0;
  84. req.r.rtm_type = 0;
  85. req.r.rtm_src_len = 0;
  86. req.r.rtm_dst_len = 0;
  87. req.r.rtm_tos = 0;
  88. {
  89. char dst_str[128];
  90. sockaddr_to_string(dst, dst_str, sizeof(dst_str));
  91. syslog(LOG_DEBUG, "get_src_for_route_to (%s)", dst_str);
  92. }
  93. /* add address */
  94. if (dst->sa_family == AF_INET)
  95. {
  96. dst4 = (const struct sockaddr_in *) dst;
  97. nfnl_addattr_l(&req.n, sizeof(req), RTA_DST, &dst4->sin_addr, 4);
  98. req.r.rtm_dst_len = 32;
  99. }
  100. else
  101. {
  102. dst6 = (const struct sockaddr_in6 *) dst;
  103. nfnl_addattr_l(&req.n, sizeof(req), RTA_DST, &dst6->sin6_addr, 16);
  104. req.r.rtm_dst_len = 128;
  105. }
  106. fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  107. if (fd < 0)
  108. {
  109. syslog(LOG_ERR, "socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE) : %m");
  110. return -1;
  111. }
  112. memset(&nladdr, 0, sizeof(nladdr));
  113. nladdr.nl_family = AF_NETLINK;
  114. req.n.nlmsg_seq = 1;
  115. iov.iov_len = req.n.nlmsg_len;
  116. status = sendmsg(fd, &msg, 0);
  117. if (status < 0)
  118. {
  119. syslog(LOG_ERR, "sendmsg(rtnetlink) : %m");
  120. goto error;
  121. }
  122. memset(&req, 0, sizeof(req));
  123. for (;;)
  124. {
  125. iov.iov_len = sizeof(req);
  126. status = recvmsg(fd, &msg, 0);
  127. if (status < 0)
  128. {
  129. if (errno == EINTR || errno == EAGAIN)
  130. {
  131. continue;
  132. }
  133. syslog(LOG_ERR, "recvmsg(rtnetlink) %m");
  134. goto error;
  135. }
  136. if (status == 0)
  137. {
  138. syslog(LOG_ERR, "recvmsg(rtnetlink) EOF");
  139. goto error;
  140. }
  141. for (h = (struct nlmsghdr *) &req.n; status >= (int) sizeof(*h);)
  142. {
  143. int len = h->nlmsg_len;
  144. int l = len - sizeof(*h);
  145. if (l < 0 || len > status)
  146. {
  147. if (msg.msg_flags & MSG_TRUNC)
  148. {
  149. syslog(LOG_ERR, "Truncated message");
  150. }
  151. syslog(LOG_ERR, "malformed message: len=%d", len);
  152. goto error;
  153. }
  154. if (nladdr.nl_pid != 0 || h->nlmsg_seq != 1 /*seq*/)
  155. {
  156. syslog(LOG_ERR, "wrong seq = %d\n", h->nlmsg_seq);
  157. /* Don't forget to skip that message. */
  158. status -= NLMSG_ALIGN(len);
  159. h = (struct nlmsghdr *) ((char *) h + NLMSG_ALIGN(len));
  160. continue;
  161. }
  162. if (h->nlmsg_type == NLMSG_ERROR)
  163. {
  164. struct nlmsgerr * err = (struct nlmsgerr *) NLMSG_DATA(h);
  165. syslog(LOG_ERR, "NLMSG_ERROR %d : %s", err->error, strerror(-err->error));
  166. goto error;
  167. }
  168. if (h->nlmsg_type == RTM_NEWROUTE)
  169. {
  170. struct rtattr * rta;
  171. int len = h->nlmsg_len;
  172. len -= NLMSG_LENGTH(sizeof(struct rtmsg));
  173. for (rta = RTM_RTA(NLMSG_DATA(h)); RTA_OK(rta, len); rta = RTA_NEXT(rta, len))
  174. {
  175. unsigned char * data = RTA_DATA(rta);
  176. if (rta->rta_type == RTA_PREFSRC)
  177. {
  178. if (src_len && src)
  179. {
  180. if (*src_len < RTA_PAYLOAD(rta))
  181. {
  182. syslog(LOG_WARNING, "cannot copy src: %u<%lu", (unsigned) *src_len, RTA_PAYLOAD(rta));
  183. goto error;
  184. }
  185. *src_len = RTA_PAYLOAD(rta);
  186. memcpy(src, data, RTA_PAYLOAD(rta));
  187. }
  188. }
  189. else if (rta->rta_type == RTA_OIF)
  190. {
  191. if (index)
  192. {
  193. memcpy(index, data, sizeof(int));
  194. }
  195. }
  196. }
  197. close(fd);
  198. return 0;
  199. }
  200. status -= NLMSG_ALIGN(len);
  201. h = (struct nlmsghdr *) ((char *) h + NLMSG_ALIGN(len));
  202. }
  203. }
  204. syslog(LOG_WARNING, "get_src_for_route_to() : src not found");
  205. error:
  206. if (fd >= 0)
  207. {
  208. close(fd);
  209. }
  210. return -1;
  211. #else /* __linux__ */
  212. int found = 0;
  213. int s;
  214. int l, i;
  215. char * p;
  216. struct sockaddr * sa;
  217. struct
  218. {
  219. struct rt_msghdr m_rtm;
  220. char m_space[512];
  221. } m_rtmsg;
  222. #define rtm m_rtmsg.m_rtm
  223. if (dst == NULL)
  224. {
  225. return -1;
  226. }
  227. #ifdef __APPLE__
  228. if (dst->sa_family == AF_INET6)
  229. {
  230. syslog(LOG_ERR, "Sorry, get_src_for_route_to() is known to fail with IPV6 on OS X...");
  231. return -1;
  232. }
  233. #endif
  234. s = socket(PF_ROUTE, SOCK_RAW, dst->sa_family);
  235. if (s < 0)
  236. {
  237. syslog(LOG_ERR, "socket(PF_ROUTE) failed : %m");
  238. return -1;
  239. }
  240. memset(&rtm, 0, sizeof(rtm));
  241. rtm.rtm_type = RTM_GET;
  242. rtm.rtm_flags = RTF_UP;
  243. rtm.rtm_version = RTM_VERSION;
  244. rtm.rtm_seq = 1;
  245. rtm.rtm_addrs = RTA_DST; /* destination address */
  246. memcpy(m_rtmsg.m_space, dst, sizeof(struct sockaddr));
  247. rtm.rtm_msglen = sizeof(struct rt_msghdr) + sizeof(struct sockaddr);
  248. if (write(s, &m_rtmsg, rtm.rtm_msglen) < 0)
  249. {
  250. syslog(LOG_ERR, "write: %m");
  251. close(s);
  252. return -1;
  253. }
  254. do
  255. {
  256. l = read(s, &m_rtmsg, sizeof(m_rtmsg));
  257. if (l < 0)
  258. {
  259. syslog(LOG_ERR, "read: %m");
  260. close(s);
  261. return -1;
  262. }
  263. syslog(LOG_DEBUG, "read l=%d seq=%d pid=%d", l, rtm.rtm_seq, rtm.rtm_pid);
  264. }
  265. while (l > 0 && (rtm.rtm_pid != getpid() || rtm.rtm_seq != 1));
  266. close(s);
  267. p = m_rtmsg.m_space;
  268. if (rtm.rtm_addrs)
  269. {
  270. for (i = 1; i < 0x8000; i <<= 1)
  271. {
  272. if (i & rtm.rtm_addrs)
  273. {
  274. char tmp[256] = {0};
  275. sa = (struct sockaddr *) p;
  276. sockaddr_to_string(sa, tmp, sizeof(tmp));
  277. syslog(LOG_DEBUG, "type=%d sa_len=%d sa_family=%d %s", i, SA_LEN(sa), sa->sa_family, tmp);
  278. if ((i == RTA_DST || i == RTA_GATEWAY) && (src_len && src))
  279. {
  280. size_t len = 0;
  281. void * paddr = NULL;
  282. if (sa->sa_family == AF_INET)
  283. {
  284. paddr = &((struct sockaddr_in *) sa)->sin_addr;
  285. len = sizeof(struct in_addr);
  286. }
  287. else if (sa->sa_family == AF_INET6)
  288. {
  289. paddr = &((struct sockaddr_in6 *) sa)->sin6_addr;
  290. len = sizeof(struct in6_addr);
  291. }
  292. if (paddr)
  293. {
  294. if (*src_len < len)
  295. {
  296. syslog(LOG_WARNING, "cannot copy src. %u<%u", (unsigned) *src_len, (unsigned) len);
  297. return -1;
  298. }
  299. memcpy(src, paddr, len);
  300. *src_len = len;
  301. found = 1;
  302. }
  303. }
  304. #ifdef AF_LINK
  305. if (sa->sa_family == AF_LINK)
  306. {
  307. struct sockaddr_dl * sdl = (struct sockaddr_dl *) sa;
  308. if (index)
  309. {
  310. *index = sdl->sdl_index;
  311. }
  312. }
  313. #endif
  314. p += SA_LEN(sa);
  315. }
  316. }
  317. }
  318. return found ? 0 : -1;
  319. #endif /* __linux__ */
  320. }