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.

388 lines
12 KiB

  1. --- /dev/null
  2. +++ b/.gitignore
  3. @@ -0,0 +1,4 @@
  4. +Clients/build
  5. +mDNSPosix/build
  6. +mDNSPosix/objects
  7. +
  8. --- a/Clients/Makefile
  9. +++ b/Clients/Makefile
  10. @@ -36,7 +36,7 @@ TARGETS = build/dns-sd build/dns-sd64
  11. LIBS =
  12. else
  13. TARGETS = build/dns-sd
  14. -LIBS = -L../mDNSPosix/build/prod/ -ldns_sd
  15. +LIBS ?= -L../mDNSPosix/build/prod/ -ldns_sd
  16. endif
  17. all: $(TARGETS)
  18. --- a/mDNSPosix/PosixDaemon.c
  19. +++ b/mDNSPosix/PosixDaemon.c
  20. @@ -37,6 +37,11 @@
  21. #include <fcntl.h>
  22. #include <pwd.h>
  23. #include <sys/types.h>
  24. +#ifdef __linux__
  25. +#include <sys/capability.h> /* !!! We require libcap-dev for this. Oh well. */
  26. +/* prctl is required to enable inheriting of capabilities across setuid */
  27. +#include <sys/prctl.h>
  28. +#endif /* __linux__ */
  29. #if __APPLE__
  30. #undef daemon
  31. @@ -184,16 +189,50 @@ int main(int argc, char **argv)
  32. Reconfigure(&mDNSStorage);
  33. +#ifdef __linux__
  34. + /*
  35. + * SO_BINDTODEVICE is privileged operation; however, we can get
  36. + * around it using capabilities instead of remaining root.
  37. + */
  38. + if (mStatus_NoError == err)
  39. + {
  40. + if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) < 0)
  41. + perror("prctl PR_SET_KEEPCAPS");
  42. + }
  43. +#endif /* __linux__ */
  44. +
  45. // Now that we're finished with anything privileged, switch over to running as "nobody"
  46. if (mStatus_NoError == err)
  47. {
  48. const struct passwd *pw = getpwnam("nobody");
  49. if (pw != NULL)
  50. + {
  51. setuid(pw->pw_uid);
  52. +#ifdef __linux__
  53. + struct __user_cap_header_struct ch;
  54. + struct __user_cap_data_struct cd[_LINUX_CAPABILITY_U32S_3];
  55. +
  56. + memset(&ch, 0, sizeof(ch));
  57. + ch.version = _LINUX_CAPABILITY_VERSION_3;
  58. + ch.pid = getpid();
  59. + memset(&cd[0], 0, sizeof(cd));
  60. + /* CAP_NET_RAW is required to use SO_BINDTODEVICE */
  61. + int caps = CAP_TO_MASK(CAP_NET_RAW);
  62. + cd[0].permitted = caps;
  63. + cd[0].effective = caps;
  64. + if (capset(&ch, &cd[0]) < 0)
  65. + perror("capset");
  66. +#endif /* __linux__ */
  67. + }
  68. else
  69. LogMsg("WARNING: mdnsd continuing as root because user \"nobody\" does not exist");
  70. }
  71. +#ifdef __linux__
  72. + if (mStatus_NoError == err)
  73. + err = mDNSPlatformPosixRefreshInterfaceList(&mDNSStorage);
  74. +#endif /* __linux__ */
  75. +
  76. if (mStatus_NoError == err)
  77. err = MainLoop(&mDNSStorage);
  78. --- a/mDNSPosix/Responder.c
  79. +++ b/mDNSPosix/Responder.c
  80. @@ -603,7 +603,8 @@ static mStatus RegisterServicesInFile(co
  81. status = mStatus_UnknownErr;
  82. }
  83. - assert(0 == fclose(fp));
  84. + int rv = fclose(fp);
  85. + assert(0 == rv);
  86. return status;
  87. }
  88. --- a/mDNSPosix/mDNSPosix.c
  89. +++ b/mDNSPosix/mDNSPosix.c
  90. @@ -138,7 +138,7 @@ mDNSlocal void SockAddrTomDNSAddr(const
  91. // mDNS core calls this routine when it needs to send a packet.
  92. mDNSexport mStatus mDNSPlatformSendUDP(const mDNS *const m, const void *const msg, const mDNSu8 *const end,
  93. - mDNSInterfaceID InterfaceID, UDPSocket *src, const mDNSAddr *dst,
  94. + mDNSInterfaceID InterfaceID, UDPSocket *src, const mDNSAddr *dst,
  95. mDNSIPPort dstPort, mDNSBool useBackgroundTrafficClass)
  96. {
  97. int err = 0;
  98. @@ -583,9 +583,17 @@ mDNSlocal void FreePosixNetworkInterface
  99. {
  100. assert(intf != NULL);
  101. if (intf->intfName != NULL) free((void *)intf->intfName);
  102. - if (intf->multicastSocket4 != -1) assert(close(intf->multicastSocket4) == 0);
  103. + if (intf->multicastSocket4 != -1)
  104. + {
  105. + int rv = close(intf->multicastSocket4);
  106. + assert(rv == 0);
  107. + }
  108. #if HAVE_IPV6
  109. - if (intf->multicastSocket6 != -1) assert(close(intf->multicastSocket6) == 0);
  110. + if (intf->multicastSocket6 != -1)
  111. + {
  112. + int rv = close(intf->multicastSocket6);
  113. + assert(rv == 0);
  114. + }
  115. #endif
  116. // Move interface to the RecentInterfaces list for a minute
  117. @@ -724,6 +732,29 @@ mDNSlocal int SetupSocket(struct sockadd
  118. if (err < 0) { err = errno; perror("setsockopt - IP_MULTICAST_TTL"); }
  119. }
  120. +#ifdef __linux__
  121. +#ifdef SO_BINDTODEVICE
  122. + if (err == 0 && interfaceIndex)
  123. + {
  124. + char ifname[IFNAMSIZ];
  125. + if (if_indextoname(interfaceIndex, ifname))
  126. + {
  127. + err = setsockopt(*sktPtr, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname));
  128. + if (err < 0)
  129. + {
  130. + err = errno;
  131. + perror("setsockopt - SO_BINDTODEVICE");
  132. + }
  133. + }
  134. + else
  135. + {
  136. + err = errno;
  137. + perror("if_indextoname");
  138. + }
  139. + }
  140. +#endif /* SO_BINDTODEVICE */
  141. +#endif /* __linux__ */
  142. +
  143. // And start listening for packets
  144. if (err == 0)
  145. {
  146. @@ -805,6 +836,29 @@ mDNSlocal int SetupSocket(struct sockadd
  147. if (err < 0) { err = errno; perror("setsockopt - IPV6_MULTICAST_HOPS"); }
  148. }
  149. +#ifdef __linux__
  150. +#ifdef SO_BINDTODEVICE
  151. + if (err == 0 && interfaceIndex)
  152. + {
  153. + char ifname[IFNAMSIZ];
  154. + if (if_indextoname(interfaceIndex, ifname))
  155. + {
  156. + err = setsockopt(*sktPtr, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen(ifname));
  157. + if (err < 0)
  158. + {
  159. + err = errno;
  160. + perror("setsockopt - SO_BINDTODEVICE");
  161. + }
  162. + }
  163. + else
  164. + {
  165. + err = errno;
  166. + perror("if_indextoname");
  167. + }
  168. + }
  169. +#endif /* SO_BINDTODEVICE */
  170. +#endif /* __linux__ */
  171. +
  172. // And start listening for packets
  173. if (err == 0)
  174. {
  175. @@ -836,7 +890,12 @@ mDNSlocal int SetupSocket(struct sockadd
  176. }
  177. // Clean up
  178. - if (err != 0 && *sktPtr != -1) { assert(close(*sktPtr) == 0); *sktPtr = -1; }
  179. + if (err != 0 && *sktPtr != -1)
  180. + {
  181. + int rv = close(*sktPtr);
  182. + assert(rv == 0);
  183. + *sktPtr = -1;
  184. + }
  185. assert((err == 0) == (*sktPtr != -1));
  186. return err;
  187. }
  188. @@ -942,19 +1001,14 @@ mDNSlocal int SetupInterfaceList(mDNS *c
  189. int err = 0;
  190. struct ifi_info *intfList = get_ifi_info(AF_INET, mDNStrue);
  191. struct ifi_info *firstLoopback = NULL;
  192. + struct ifi_info **p = &intfList;
  193. assert(m != NULL);
  194. debugf("SetupInterfaceList");
  195. - if (intfList == NULL) err = ENOENT;
  196. -
  197. #if HAVE_IPV6
  198. - if (err == 0) /* Link the IPv6 list to the end of the IPv4 list */
  199. - {
  200. - struct ifi_info **p = &intfList;
  201. - while (*p) p = &(*p)->ifi_next;
  202. - *p = get_ifi_info(AF_INET6, mDNStrue);
  203. - }
  204. + while (*p) p = &(*p)->ifi_next;
  205. + *p = get_ifi_info(AF_INET6, mDNStrue);
  206. #endif
  207. if (err == 0)
  208. @@ -1030,7 +1084,7 @@ mDNSlocal mStatus OpenIfNotifySocket(int
  209. /* Subscribe the socket to Link & IP addr notifications. */
  210. mDNSPlatformMemZero(&snl, sizeof snl);
  211. snl.nl_family = AF_NETLINK;
  212. - snl.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR;
  213. + snl.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR;
  214. ret = bind(sock, (struct sockaddr *) &snl, sizeof snl);
  215. if (0 == ret)
  216. *pFD = sock;
  217. @@ -1108,11 +1162,18 @@ mDNSlocal mDNSu32 ProcessRoutingNo
  218. PrintNetLinkMsg(pNLMsg);
  219. #endif
  220. + // this result isn't used anywhere as a number, just as
  221. + // non-zero - however, I have seen devices with more than 32
  222. + // interfaces at some point..
  223. + // (on Linux, every tunnel increases index for example)
  224. +
  225. // Process the NetLink message
  226. if (pNLMsg->nlmsg_type == RTM_GETLINK || pNLMsg->nlmsg_type == RTM_NEWLINK)
  227. - result |= 1 << ((struct ifinfomsg*) NLMSG_DATA(pNLMsg))->ifi_index;
  228. + result |= 1;
  229. + // << ((struct ifinfomsg*) NLMSG_DATA(pNLMsg))->ifi_index;
  230. else if (pNLMsg->nlmsg_type == RTM_DELADDR || pNLMsg->nlmsg_type == RTM_NEWADDR)
  231. - result |= 1 << ((struct ifaddrmsg*) NLMSG_DATA(pNLMsg))->ifa_index;
  232. + result |= 1;
  233. + // << ((struct ifaddrmsg*) NLMSG_DATA(pNLMsg))->ifa_index;
  234. // Advance pNLMsg to the next message in the buffer
  235. if ((pNLMsg->nlmsg_flags & NLM_F_MULTI) != 0 && pNLMsg->nlmsg_type != NLMSG_DONE)
  236. @@ -1283,8 +1344,12 @@ mDNSexport mStatus mDNSPlatformInit(mDNS
  237. if (err == mStatus_NoError) err = SetupSocket(&sa, zeroIPPort, 0, &m->p->unicastSocket6);
  238. #endif
  239. + // In Linux case, we can't set up sockets with different owner -
  240. + // it blows up SO_REUSEPORT. So we do this step bit later.
  241. +#ifndef __linux__
  242. // Tell mDNS core about the network interfaces on this machine.
  243. if (err == mStatus_NoError) err = SetupInterfaceList(m);
  244. +#endif /* !__linux__ */
  245. // Tell mDNS core about DNS Servers
  246. mDNS_Lock(m);
  247. @@ -1317,9 +1382,17 @@ mDNSexport void mDNSPlatformClose(mDNS *
  248. {
  249. assert(m != NULL);
  250. ClearInterfaceList(m);
  251. - if (m->p->unicastSocket4 != -1) assert(close(m->p->unicastSocket4) == 0);
  252. + if (m->p->unicastSocket4 != -1)
  253. + {
  254. + int rv = close(m->p->unicastSocket4);
  255. + assert(rv == 0);
  256. + }
  257. #if HAVE_IPV6
  258. - if (m->p->unicastSocket6 != -1) assert(close(m->p->unicastSocket6) == 0);
  259. + if (m->p->unicastSocket6 != -1)
  260. + {
  261. + int rv = close(m->p->unicastSocket6);
  262. + assert(rv == 0);
  263. + }
  264. #endif
  265. }
  266. @@ -1575,14 +1648,14 @@ mDNSexport mStatus mDNSPlatformClearS
  267. mDNSexport mDNSu16 mDNSPlatformGetUDPPort(UDPSocket *sock)
  268. {
  269. (void) sock; // unused
  270. -
  271. +
  272. return (mDNSu16)-1;
  273. }
  274. mDNSexport mDNSBool mDNSPlatformInterfaceIsD2D(mDNSInterfaceID InterfaceID)
  275. {
  276. (void) InterfaceID; // unused
  277. -
  278. +
  279. return mDNSfalse;
  280. }
  281. --- a/mDNSPosix/mDNSUNP.c
  282. +++ b/mDNSPosix/mDNSUNP.c
  283. @@ -63,6 +63,7 @@
  284. #if defined(AF_INET6) && HAVE_IPV6 && HAVE_LINUX
  285. #include <netdb.h>
  286. #include <arpa/inet.h>
  287. +#include <linux/if_addr.h>
  288. /* Converts a prefix length to IPv6 network mask */
  289. void plen_to_mask(int plen, char *addr) {
  290. @@ -86,7 +87,7 @@ struct ifi_info *get_ifi_info_linuxv6(in
  291. FILE *fp;
  292. char addr[8][5];
  293. int flags, myflags, index, plen, scope;
  294. - char ifname[9], lastname[IFNAMSIZ];
  295. + char ifname[IFNAMSIZ], lastname[IFNAMSIZ];
  296. char addr6[32+7+1]; /* don't forget the seven ':' */
  297. struct addrinfo hints, *res0;
  298. struct sockaddr_in6 *sin6;
  299. @@ -94,7 +95,8 @@ struct ifi_info *get_ifi_info_linuxv6(in
  300. int err;
  301. int sockfd = -1;
  302. struct ifreq ifr;
  303. -
  304. + char mask[64] = "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %";
  305. + sprintf(mask+strlen(mask), "%ds\n", IFNAMSIZ);
  306. res0=NULL;
  307. ifihead = NULL;
  308. ifipnext = &ifihead;
  309. @@ -106,11 +108,12 @@ struct ifi_info *get_ifi_info_linuxv6(in
  310. goto gotError;
  311. }
  312. while (fscanf(fp,
  313. - "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %8s\n",
  314. + mask,
  315. addr[0],addr[1],addr[2],addr[3],
  316. addr[4],addr[5],addr[6],addr[7],
  317. &index, &plen, &scope, &flags, ifname) != EOF) {
  318. + if (flags & IFA_F_DEPRECATED) continue;
  319. myflags = 0;
  320. if (strncmp(lastname, ifname, IFNAMSIZ) == 0) {
  321. if (doaliases == 0)
  322. @@ -204,8 +207,11 @@ gotError:
  323. res0=NULL;
  324. }
  325. done:
  326. + if (fp)
  327. + fclose(fp);
  328. if (sockfd != -1) {
  329. - assert(close(sockfd) == 0);
  330. + int rv = close(sockfd);
  331. + assert(rv == 0);
  332. }
  333. return(ifihead); /* pointer to first structure in linked list */
  334. }
  335. --- a/mDNSShared/dnsextd_parser.y
  336. +++ b/mDNSShared/dnsextd_parser.y
  337. @@ -15,6 +15,8 @@
  338. * limitations under the License.
  339. */
  340. +%parse-param { void *context }
  341. +
  342. %{
  343. #include <stdio.h>
  344. #include <stdlib.h>
  345. @@ -23,7 +25,7 @@
  346. #include "DebugServices.h"
  347. #include "dnsextd.h"
  348. -void yyerror( const char* error );
  349. +void yyerror( void *context, const char* error );
  350. int yylex(void);
  351. @@ -378,7 +380,7 @@ int yywrap(void);
  352. extern int yylineno;
  353. -void yyerror( const char *str )
  354. +void yyerror( void *context, const char *str )
  355. {
  356. fprintf( stderr,"%s:%d: error: %s\n", g_filename, yylineno, str );
  357. }