pim_sock.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. PIM for Quagga
  3. Copyright (C) 2008 Everton da Silva Marques
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; see the file COPYING; if not, write to the
  14. Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  15. MA 02110-1301 USA
  16. $QuaggaId: $Format:%an, %ai, %h$ $
  17. */
  18. #include <zebra.h>
  19. #include "pim_mroute.h"
  20. #include <sys/types.h>
  21. #include <sys/socket.h>
  22. #include <netinet/in.h>
  23. #include <netinet/igmp.h>
  24. #include <arpa/inet.h>
  25. #include <unistd.h>
  26. #include <netdb.h>
  27. #include <errno.h>
  28. #include "log.h"
  29. #include "privs.h"
  30. #include "pimd.h"
  31. #include "pim_sock.h"
  32. #include "pim_str.h"
  33. #include "pim_igmp_join.h"
  34. /* GLOBAL VARS */
  35. extern struct zebra_privs_t pimd_privs;
  36. int pim_socket_raw(int protocol)
  37. {
  38. int fd;
  39. if ( pimd_privs.change (ZPRIVS_RAISE) )
  40. zlog_err ("pim_sockek_raw: could not raise privs, %s",
  41. safe_strerror (errno) );
  42. fd = socket(AF_INET, SOCK_RAW, protocol);
  43. if ( pimd_privs.change (ZPRIVS_LOWER) )
  44. zlog_err ("pim_socket_raw: could not lower privs, %s",
  45. safe_strerror (errno) );
  46. if (fd < 0) {
  47. zlog_warn("Could not create raw socket: errno=%d: %s",
  48. errno, safe_strerror(errno));
  49. return PIM_SOCK_ERR_SOCKET;
  50. }
  51. return fd;
  52. }
  53. int pim_socket_mcast(int protocol, struct in_addr ifaddr, int loop)
  54. {
  55. int fd;
  56. fd = pim_socket_raw(protocol);
  57. if (fd < 0) {
  58. zlog_warn("Could not create multicast socket: errno=%d: %s",
  59. errno, safe_strerror(errno));
  60. return PIM_SOCK_ERR_SOCKET;
  61. }
  62. /* Needed to obtain destination address from recvmsg() */
  63. {
  64. #if defined(HAVE_IP_PKTINFO)
  65. /* Linux IP_PKTINFO */
  66. int opt = 1;
  67. if (setsockopt(fd, SOL_IP, IP_PKTINFO, &opt, sizeof(opt))) {
  68. zlog_warn("Could not set IP_PKTINFO on socket fd=%d: errno=%d: %s",
  69. fd, errno, safe_strerror(errno));
  70. }
  71. #elif defined(HAVE_IP_RECVDSTADDR)
  72. /* BSD IP_RECVDSTADDR */
  73. int opt = 1;
  74. if (setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &opt, sizeof(opt))) {
  75. zlog_warn("Could not set IP_RECVDSTADDR on socket fd=%d: errno=%d: %s",
  76. fd, errno, safe_strerror(errno));
  77. }
  78. #else
  79. zlog_err("%s %s: Missing IP_PKTINFO and IP_RECVDSTADDR: unable to get dst addr from recvmsg()",
  80. __FILE__, __PRETTY_FUNCTION__);
  81. close(fd);
  82. return PIM_SOCK_ERR_DSTADDR;
  83. #endif
  84. }
  85. /* Set router alert (RFC 2113) for all IGMP messages (RFC 3376 4. Message Formats)*/
  86. if (protocol == IPPROTO_IGMP) {
  87. char ra[4];
  88. ra[0] = 148;
  89. ra[1] = 4;
  90. ra[2] = 0;
  91. ra[3] = 0;
  92. if (setsockopt(fd, IPPROTO_IP, IP_OPTIONS, ra, 4)) {
  93. zlog_warn("Could not set Router Alert Option on socket fd=%d: errno=%d: %s",
  94. fd, errno, safe_strerror(errno));
  95. close(fd);
  96. return PIM_SOCK_ERR_RA;
  97. }
  98. }
  99. {
  100. int reuse = 1;
  101. if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
  102. (void *) &reuse, sizeof(reuse))) {
  103. zlog_warn("Could not set Reuse Address Option on socket fd=%d: errno=%d: %s",
  104. fd, errno, safe_strerror(errno));
  105. close(fd);
  106. return PIM_SOCK_ERR_REUSE;
  107. }
  108. }
  109. {
  110. const int MTTL = 1;
  111. int ttl = MTTL;
  112. if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL,
  113. (void *) &ttl, sizeof(ttl))) {
  114. zlog_warn("Could not set multicast TTL=%d on socket fd=%d: errno=%d: %s",
  115. MTTL, fd, errno, safe_strerror(errno));
  116. close(fd);
  117. return PIM_SOCK_ERR_TTL;
  118. }
  119. }
  120. if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
  121. (void *) &loop, sizeof(loop))) {
  122. zlog_warn("Could not %s Multicast Loopback Option on socket fd=%d: errno=%d: %s",
  123. loop ? "enable" : "disable",
  124. fd, errno, safe_strerror(errno));
  125. close(fd);
  126. return PIM_SOCK_ERR_LOOP;
  127. }
  128. if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
  129. (void *) &ifaddr, sizeof(ifaddr))) {
  130. zlog_warn("Could not set Outgoing Interface Option on socket fd=%d: errno=%d: %s",
  131. fd, errno, safe_strerror(errno));
  132. close(fd);
  133. return PIM_SOCK_ERR_IFACE;
  134. }
  135. {
  136. long flags;
  137. flags = fcntl(fd, F_GETFL, 0);
  138. if (flags < 0) {
  139. zlog_warn("Could not get fcntl(F_GETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
  140. fd, errno, safe_strerror(errno));
  141. close(fd);
  142. return PIM_SOCK_ERR_NONBLOCK_GETFL;
  143. }
  144. if (fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
  145. zlog_warn("Could not set fcntl(F_SETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
  146. fd, errno, safe_strerror(errno));
  147. close(fd);
  148. return PIM_SOCK_ERR_NONBLOCK_SETFL;
  149. }
  150. }
  151. return fd;
  152. }
  153. int pim_socket_join(int fd, struct in_addr group,
  154. struct in_addr ifaddr, int ifindex)
  155. {
  156. int ret;
  157. #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
  158. struct ip_mreqn opt;
  159. #else
  160. struct ip_mreq opt;
  161. #endif
  162. opt.imr_multiaddr = group;
  163. #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
  164. opt.imr_address = ifaddr;
  165. opt.imr_ifindex = ifindex;
  166. #else
  167. opt.imr_interface = ifaddr;
  168. #endif
  169. ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &opt, sizeof(opt));
  170. if (ret) {
  171. char group_str[100];
  172. char ifaddr_str[100];
  173. if (!inet_ntop(AF_INET, &group, group_str , sizeof(group_str)))
  174. sprintf(group_str, "<group?>");
  175. if (!inet_ntop(AF_INET, &ifaddr, ifaddr_str , sizeof(ifaddr_str)))
  176. sprintf(ifaddr_str, "<ifaddr?>");
  177. zlog_err("Failure socket joining fd=%d group %s on interface address %s: errno=%d: %s",
  178. fd, group_str, ifaddr_str, errno, safe_strerror(errno));
  179. return ret;
  180. }
  181. if (PIM_DEBUG_TRACE) {
  182. char group_str[100];
  183. char ifaddr_str[100];
  184. if (!inet_ntop(AF_INET, &group, group_str , sizeof(group_str)))
  185. sprintf(group_str, "<group?>");
  186. if (!inet_ntop(AF_INET, &ifaddr, ifaddr_str , sizeof(ifaddr_str)))
  187. sprintf(ifaddr_str, "<ifaddr?>");
  188. zlog_debug("Socket fd=%d joined group %s on interface address %s",
  189. fd, group_str, ifaddr_str);
  190. }
  191. return ret;
  192. }
  193. int pim_socket_join_source(int fd, int ifindex,
  194. struct in_addr group_addr,
  195. struct in_addr source_addr,
  196. const char *ifname)
  197. {
  198. if (pim_igmp_join_source(fd, ifindex, group_addr, source_addr)) {
  199. int e = errno;
  200. char group_str[100];
  201. char source_str[100];
  202. pim_inet4_dump("<grp?>", group_addr, group_str, sizeof(group_str));
  203. pim_inet4_dump("<src?>", source_addr, source_str, sizeof(source_str));
  204. zlog_warn("%s: setsockopt(fd=%d) failure for IGMP group %s source %s ifindex %d on interface %s: errno=%d: %s",
  205. __PRETTY_FUNCTION__,
  206. fd, group_str, source_str, ifindex, ifname,
  207. e, safe_strerror(e));
  208. return -1;
  209. }
  210. return 0;
  211. }
  212. int pim_socket_recvfromto(int fd, uint8_t *buf, size_t len,
  213. struct sockaddr_in *from, socklen_t *fromlen,
  214. struct sockaddr_in *to, socklen_t *tolen,
  215. int *ifindex)
  216. {
  217. struct msghdr msgh;
  218. struct cmsghdr *cmsg;
  219. struct iovec iov;
  220. char cbuf[1000];
  221. int err;
  222. /*
  223. * IP_PKTINFO / IP_RECVDSTADDR don't yield sin_port.
  224. * Use getsockname() to get sin_port.
  225. */
  226. if (to) {
  227. struct sockaddr_in si;
  228. socklen_t si_len = sizeof(si);
  229. ((struct sockaddr_in *) to)->sin_family = AF_INET;
  230. if (pim_socket_getsockname(fd, (struct sockaddr *) &si, &si_len)) {
  231. ((struct sockaddr_in *) to)->sin_port = ntohs(0);
  232. ((struct sockaddr_in *) to)->sin_addr.s_addr = ntohl(0);
  233. }
  234. else {
  235. ((struct sockaddr_in *) to)->sin_port = si.sin_port;
  236. ((struct sockaddr_in *) to)->sin_addr = si.sin_addr;
  237. }
  238. if (tolen)
  239. *tolen = sizeof(si);
  240. }
  241. memset(&msgh, 0, sizeof(struct msghdr));
  242. iov.iov_base = buf;
  243. iov.iov_len = len;
  244. msgh.msg_control = cbuf;
  245. msgh.msg_controllen = sizeof(cbuf);
  246. msgh.msg_name = from;
  247. msgh.msg_namelen = fromlen ? *fromlen : 0;
  248. msgh.msg_iov = &iov;
  249. msgh.msg_iovlen = 1;
  250. msgh.msg_flags = 0;
  251. err = recvmsg(fd, &msgh, 0);
  252. if (err < 0)
  253. return err;
  254. if (fromlen)
  255. *fromlen = msgh.msg_namelen;
  256. for (cmsg = CMSG_FIRSTHDR(&msgh);
  257. cmsg != NULL;
  258. cmsg = CMSG_NXTHDR(&msgh,cmsg)) {
  259. #ifdef HAVE_IP_PKTINFO
  260. if ((cmsg->cmsg_level == SOL_IP) && (cmsg->cmsg_type == IP_PKTINFO)) {
  261. struct in_pktinfo *i = (struct in_pktinfo *) CMSG_DATA(cmsg);
  262. if (to)
  263. ((struct sockaddr_in *) to)->sin_addr = i->ipi_addr;
  264. if (tolen)
  265. *tolen = sizeof(struct sockaddr_in);
  266. if (ifindex)
  267. *ifindex = i->ipi_ifindex;
  268. if (to && PIM_DEBUG_PACKETS) {
  269. char to_str[100];
  270. pim_inet4_dump("<to?>", to->sin_addr, to_str, sizeof(to_str));
  271. zlog_debug("%s: HAVE_IP_PKTINFO to=%s,%d",
  272. __PRETTY_FUNCTION__,
  273. to_str, ntohs(to->sin_port));
  274. }
  275. break;
  276. }
  277. #endif
  278. #ifdef HAVE_IP_RECVDSTADDR
  279. if ((cmsg->cmsg_level == IPPROTO_IP) && (cmsg->cmsg_type == IP_RECVDSTADDR)) {
  280. struct in_addr *i = (struct in_addr *) CMSG_DATA(cmsg);
  281. if (to)
  282. ((struct sockaddr_in *) to)->sin_addr = *i;
  283. if (tolen)
  284. *tolen = sizeof(struct sockaddr_in);
  285. if (to && PIM_DEBUG_PACKETS) {
  286. char to_str[100];
  287. pim_inet4_dump("<to?>", to->sin_addr, to_str, sizeof(to_str));
  288. zlog_debug("%s: HAVE_IP_RECVDSTADDR to=%s,%d",
  289. __PRETTY_FUNCTION__,
  290. to_str, ntohs(to->sin_port));
  291. }
  292. break;
  293. }
  294. #endif
  295. #if defined(HAVE_IP_RECVIF) && defined(CMSG_IFINDEX)
  296. if (cmsg->cmsg_type == IP_RECVIF)
  297. if (ifindex)
  298. *ifindex = CMSG_IFINDEX(cmsg);
  299. #endif
  300. } /* for (cmsg) */
  301. return err; /* len */
  302. }
  303. int pim_socket_mcastloop_get(int fd)
  304. {
  305. int loop;
  306. socklen_t loop_len = sizeof(loop);
  307. if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
  308. &loop, &loop_len)) {
  309. int e = errno;
  310. zlog_warn("Could not get Multicast Loopback Option on socket fd=%d: errno=%d: %s",
  311. fd, errno, safe_strerror(errno));
  312. errno = e;
  313. return PIM_SOCK_ERR_LOOP;
  314. }
  315. return loop;
  316. }
  317. int pim_socket_getsockname(int fd, struct sockaddr *name, socklen_t *namelen)
  318. {
  319. if (getsockname(fd, name, namelen)) {
  320. int e = errno;
  321. zlog_warn("Could not get Socket Name for socket fd=%d: errno=%d: %s",
  322. fd, errno, safe_strerror(errno));
  323. errno = e;
  324. return PIM_SOCK_ERR_NAME;
  325. }
  326. return PIM_SOCK_ERR_NONE;
  327. }