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