pim_sock.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 "pim_mroute.h"
  19. #include <sys/types.h>
  20. #include <sys/socket.h>
  21. #include <netinet/in.h>
  22. #include <netinet/igmp.h>
  23. #include <arpa/inet.h>
  24. #include <unistd.h>
  25. #include <netdb.h>
  26. #include <errno.h>
  27. #include <zebra.h>
  28. #include "log.h"
  29. #include "privs.h"
  30. #include "pimd.h"
  31. #include "pim_sock.h"
  32. #include "pim_str.h"
  33. /* GLOBAL VARS */
  34. extern struct zebra_privs_t pimd_privs;
  35. #ifndef MCAST_JOIN_SOURCE_GROUP
  36. #define MCAST_JOIN_SOURCE_GROUP 46
  37. struct group_source_req
  38. {
  39. uint32_t gsr_interface;
  40. struct sockaddr_storage gsr_group;
  41. struct sockaddr_storage gsr_source;
  42. };
  43. #endif
  44. int pim_socket_raw(int protocol)
  45. {
  46. int fd;
  47. if ( pimd_privs.change (ZPRIVS_RAISE) )
  48. zlog_err ("pim_sockek_raw: could not raise privs, %s",
  49. safe_strerror (errno) );
  50. fd = socket(AF_INET, SOCK_RAW, protocol);
  51. if ( pimd_privs.change (ZPRIVS_LOWER) )
  52. zlog_err ("pim_socket_raw: could not lower privs, %s",
  53. safe_strerror (errno) );
  54. if (fd < 0) {
  55. zlog_warn("Could not create raw socket: errno=%d: %s",
  56. errno, strerror(errno));
  57. return PIM_SOCK_ERR_SOCKET;
  58. }
  59. return fd;
  60. }
  61. int pim_socket_mcast(int protocol, struct in_addr ifaddr, int loop)
  62. {
  63. int fd;
  64. fd = pim_socket_raw(protocol);
  65. if (fd < 0) {
  66. zlog_warn("Could not create multicast socket: errno=%d: %s",
  67. errno, strerror(errno));
  68. return PIM_SOCK_ERR_SOCKET;
  69. }
  70. /* Needed to obtain destination address from recvmsg() */
  71. {
  72. #if defined(HAVE_IP_PKTINFO)
  73. /* Linux IP_PKTINFO */
  74. int opt = 1;
  75. if (setsockopt(fd, SOL_IP, IP_PKTINFO, &opt, sizeof(opt))) {
  76. zlog_warn("Could not set IP_PKTINFO on socket fd=%d: errno=%d: %s",
  77. fd, errno, strerror(errno));
  78. }
  79. #elif defined(HAVE_IP_RECVDSTADDR)
  80. /* BSD IP_RECVDSTADDR */
  81. int opt = 1;
  82. if (setsockopt(fd, IPPROTO_IP, IP_RECVDSTADDR, &opt, sizeof(opt))) {
  83. zlog_warn("Could not set IP_RECVDSTADDR on socket fd=%d: errno=%d: %s",
  84. fd, errno, strerror(errno));
  85. }
  86. #else
  87. zlog_err("%s %s: Missing IP_PKTINFO and IP_RECVDSTADDR: unable to get dst addr from recvmsg()",
  88. __FILE__, __PRETTY_FUNCTION__);
  89. close(fd);
  90. return PIM_SOCK_ERR_DSTADDR;
  91. #endif
  92. }
  93. /* Set router alert (RFC 2113) */
  94. {
  95. char ra[4];
  96. ra[0] = 148;
  97. ra[1] = 4;
  98. ra[2] = 0;
  99. ra[3] = 0;
  100. if (setsockopt(fd, IPPROTO_IP, IP_OPTIONS, ra, 4)) {
  101. zlog_warn("Could not set Router Alert Option on socket fd=%d: errno=%d: %s",
  102. fd, errno, strerror(errno));
  103. close(fd);
  104. return PIM_SOCK_ERR_RA;
  105. }
  106. }
  107. {
  108. int reuse = 1;
  109. if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
  110. (void *) &reuse, sizeof(reuse))) {
  111. zlog_warn("Could not set Reuse Address Option on socket fd=%d: errno=%d: %s",
  112. fd, errno, strerror(errno));
  113. close(fd);
  114. return PIM_SOCK_ERR_REUSE;
  115. }
  116. }
  117. {
  118. const int MTTL = 1;
  119. int ttl = MTTL;
  120. if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL,
  121. (void *) &ttl, sizeof(ttl))) {
  122. zlog_warn("Could not set multicast TTL=%d on socket fd=%d: errno=%d: %s",
  123. MTTL, fd, errno, strerror(errno));
  124. close(fd);
  125. return PIM_SOCK_ERR_TTL;
  126. }
  127. }
  128. if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
  129. (void *) &loop, sizeof(loop))) {
  130. zlog_warn("Could not %s Multicast Loopback Option on socket fd=%d: errno=%d: %s",
  131. loop ? "enable" : "disable",
  132. fd, errno, strerror(errno));
  133. close(fd);
  134. return PIM_SOCK_ERR_LOOP;
  135. }
  136. if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF,
  137. (void *) &ifaddr, sizeof(ifaddr))) {
  138. zlog_warn("Could not set Outgoing Interface Option on socket fd=%d: errno=%d: %s",
  139. fd, errno, strerror(errno));
  140. close(fd);
  141. return PIM_SOCK_ERR_IFACE;
  142. }
  143. {
  144. long flags;
  145. flags = fcntl(fd, F_GETFL, 0);
  146. if (flags < 0) {
  147. zlog_warn("Could not get fcntl(F_GETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
  148. fd, errno, strerror(errno));
  149. close(fd);
  150. return PIM_SOCK_ERR_NONBLOCK_GETFL;
  151. }
  152. if (fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
  153. zlog_warn("Could not set fcntl(F_SETFL,O_NONBLOCK) on socket fd=%d: errno=%d: %s",
  154. fd, errno, strerror(errno));
  155. close(fd);
  156. return PIM_SOCK_ERR_NONBLOCK_SETFL;
  157. }
  158. }
  159. return fd;
  160. }
  161. int pim_socket_join(int fd, struct in_addr group,
  162. struct in_addr ifaddr, int ifindex)
  163. {
  164. int ret;
  165. #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
  166. struct ip_mreqn opt;
  167. #else
  168. struct ip_mreq opt;
  169. #endif
  170. opt.imr_multiaddr = group;
  171. #ifdef HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
  172. opt.imr_address = ifaddr;
  173. opt.imr_ifindex = ifindex;
  174. #else
  175. opt.imr_interface = ifaddr;
  176. #endif
  177. ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &opt, sizeof(opt));
  178. if (ret) {
  179. char group_str[100];
  180. char ifaddr_str[100];
  181. if (!inet_ntop(AF_INET, &group, group_str , sizeof(group_str)))
  182. sprintf(group_str, "<group?>");
  183. if (!inet_ntop(AF_INET, &ifaddr, ifaddr_str , sizeof(ifaddr_str)))
  184. sprintf(ifaddr_str, "<ifaddr?>");
  185. zlog_err("Failure socket joining fd=%d group %s on interface address %s: errno=%d: %s",
  186. fd, group_str, ifaddr_str, errno, strerror(errno));
  187. return ret;
  188. }
  189. if (PIM_DEBUG_TRACE) {
  190. char group_str[100];
  191. char ifaddr_str[100];
  192. if (!inet_ntop(AF_INET, &group, group_str , sizeof(group_str)))
  193. sprintf(group_str, "<group?>");
  194. if (!inet_ntop(AF_INET, &ifaddr, ifaddr_str , sizeof(ifaddr_str)))
  195. sprintf(ifaddr_str, "<ifaddr?>");
  196. zlog_debug("Socket fd=%d joined group %s on interface address %s",
  197. fd, group_str, ifaddr_str);
  198. }
  199. return ret;
  200. }
  201. int pim_socket_join_source(int fd, int ifindex,
  202. struct in_addr group_addr,
  203. struct in_addr source_addr,
  204. const char *ifname)
  205. {
  206. struct group_source_req req;
  207. struct sockaddr_in *group_sa = (struct sockaddr_in *) &req.gsr_group;
  208. struct sockaddr_in *source_sa = (struct sockaddr_in *) &req.gsr_source;
  209. memset(group_sa, 0, sizeof(*group_sa));
  210. group_sa->sin_family = AF_INET;
  211. group_sa->sin_addr = group_addr;
  212. group_sa->sin_port = htons(0);
  213. memset(source_sa, 0, sizeof(*source_sa));
  214. source_sa->sin_family = AF_INET;
  215. source_sa->sin_addr = source_addr;
  216. source_sa->sin_port = htons(0);
  217. req.gsr_interface = ifindex;
  218. if (setsockopt(fd, SOL_IP, MCAST_JOIN_SOURCE_GROUP,
  219. &req, sizeof(req))) {
  220. int e = errno;
  221. char group_str[100];
  222. char source_str[100];
  223. pim_inet4_dump("<grp?>", group_addr, group_str, sizeof(group_str));
  224. pim_inet4_dump("<src?>", source_addr, source_str, sizeof(source_str));
  225. zlog_warn("%s: setsockopt(fd=%d) failure for IGMP group %s source %s ifindex %d on interface %s: errno=%d: %s",
  226. __PRETTY_FUNCTION__,
  227. fd, group_str, source_str, ifindex, ifname,
  228. e, strerror(e));
  229. return -1;
  230. }
  231. return 0;
  232. }
  233. int pim_socket_recvfromto(int fd, char *buf, size_t len,
  234. struct sockaddr_in *from, socklen_t *fromlen,
  235. struct sockaddr_in *to, socklen_t *tolen,
  236. int *ifindex)
  237. {
  238. struct msghdr msgh;
  239. struct cmsghdr *cmsg;
  240. struct iovec iov;
  241. char cbuf[1000];
  242. int err;
  243. memset(&msgh, 0, sizeof(struct msghdr));
  244. iov.iov_base = buf;
  245. iov.iov_len = len;
  246. msgh.msg_control = cbuf;
  247. msgh.msg_controllen = sizeof(cbuf);
  248. msgh.msg_name = from;
  249. msgh.msg_namelen = fromlen ? *fromlen : 0;
  250. msgh.msg_iov = &iov;
  251. msgh.msg_iovlen = 1;
  252. msgh.msg_flags = 0;
  253. err = recvmsg(fd, &msgh, 0);
  254. if (err < 0)
  255. return err;
  256. if (fromlen)
  257. *fromlen = msgh.msg_namelen;
  258. for (cmsg = CMSG_FIRSTHDR(&msgh);
  259. cmsg != NULL;
  260. cmsg = CMSG_NXTHDR(&msgh,cmsg)) {
  261. #ifdef HAVE_IP_PKTINFO
  262. if ((cmsg->cmsg_level == SOL_IP) && (cmsg->cmsg_type == IP_PKTINFO)) {
  263. struct in_pktinfo *i = (struct in_pktinfo *) CMSG_DATA(cmsg);
  264. if (to)
  265. ((struct sockaddr_in *) to)->sin_addr = i->ipi_addr;
  266. if (tolen)
  267. *tolen = sizeof(struct sockaddr_in);
  268. if (ifindex)
  269. *ifindex = i->ipi_ifindex;
  270. break;
  271. }
  272. #endif
  273. #ifdef HAVE_IP_RECVDSTADDR
  274. if ((cmsg->cmsg_level == IPPROTO_IP) && (cmsg->cmsg_type == IP_RECVDSTADDR)) {
  275. struct in_addr *i = (struct in_addr *) CMSG_DATA(cmsg);
  276. if (to)
  277. ((struct sockaddr_in *) to)->sin_addr = *i;
  278. if (tolen)
  279. *tolen = sizeof(struct sockaddr_in);
  280. break;
  281. }
  282. #endif
  283. #if defined(HAVE_IP_RECVIF) && defined(CMSG_IFINDEX)
  284. if (cmsg->cmsg_type == IP_RECVIF)
  285. if (ifindex)
  286. *ifindex = CMSG_IFINDEX(cmsg);
  287. #endif
  288. } /* for (cmsg) */
  289. return err; /* len */
  290. }
  291. int pim_socket_mcastloop_get(int fd)
  292. {
  293. int loop;
  294. socklen_t loop_len = sizeof(loop);
  295. if (getsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
  296. &loop, &loop_len)) {
  297. int e = errno;
  298. zlog_warn("Could not get Multicast Loopback Option on socket fd=%d: errno=%d: %s",
  299. fd, errno, strerror(errno));
  300. errno = e;
  301. return PIM_SOCK_ERR_LOOP;
  302. }
  303. return loop;
  304. }