paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 2 | * Copyright (C) 2003 Yasuhiro Ohara |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3 | * |
| 4 | * This file is part of GNU Zebra. |
| 5 | * |
| 6 | * GNU Zebra is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the |
| 8 | * Free Software Foundation; either version 2, or (at your option) any |
| 9 | * later version. |
| 10 | * |
| 11 | * GNU Zebra is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with GNU Zebra; see the file COPYING. If not, write to the |
| 18 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 19 | * Boston, MA 02111-1307, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <zebra.h> |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 23 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 24 | #include "log.h" |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 25 | #include "memory.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 26 | #include "sockunion.h" |
hasso | 03d52f8 | 2004-09-29 00:26:19 +0000 | [diff] [blame] | 27 | #include "sockopt.h" |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 28 | #include "privs.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 29 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 30 | #include "ospf6_proto.h" |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 31 | #include "ospf6_network.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 32 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 33 | extern struct zebra_privs_t ospf6d_privs; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 34 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 35 | int ospf6_sock; |
| 36 | struct in6_addr allspfrouters6; |
| 37 | struct in6_addr alldrouters6; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 38 | |
| 39 | /* setsockopt ReUseAddr to on */ |
| 40 | void |
Paul Jakma | 6ac29a5 | 2008-08-15 13:45:30 +0100 | [diff] [blame] | 41 | ospf6_set_reuseaddr (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 42 | { |
| 43 | u_int on = 0; |
| 44 | if (setsockopt (ospf6_sock, SOL_SOCKET, SO_REUSEADDR, &on, |
| 45 | sizeof (u_int)) < 0) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 46 | zlog_warn ("Network: set SO_REUSEADDR failed: %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /* setsockopt MulticastLoop to off */ |
| 50 | void |
Paul Jakma | 6ac29a5 | 2008-08-15 13:45:30 +0100 | [diff] [blame] | 51 | ospf6_reset_mcastloop (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 52 | { |
| 53 | u_int off = 0; |
| 54 | if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, |
| 55 | &off, sizeof (u_int)) < 0) |
| 56 | zlog_warn ("Network: reset IPV6_MULTICAST_LOOP failed: %s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 57 | safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void |
Paul Jakma | 6ac29a5 | 2008-08-15 13:45:30 +0100 | [diff] [blame] | 61 | ospf6_set_pktinfo (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 62 | { |
paul | 79dc373 | 2004-07-23 15:17:45 +0000 | [diff] [blame] | 63 | setsockopt_ipv6_pktinfo (ospf6_sock, 1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void |
Stephen Hemminger | d1e2faa | 2011-09-28 14:23:35 +0400 | [diff] [blame] | 67 | ospf6_set_transport_class (void) |
| 68 | { |
| 69 | #ifdef IPTOS_PREC_INTERNETCONTROL |
| 70 | setsockopt_ipv6_tclass (ospf6_sock, IPTOS_PREC_INTERNETCONTROL); |
| 71 | #endif |
| 72 | } |
| 73 | |
| 74 | void |
Paul Jakma | 6ac29a5 | 2008-08-15 13:45:30 +0100 | [diff] [blame] | 75 | ospf6_set_checksum (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 76 | { |
| 77 | int offset = 12; |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 78 | #ifndef DISABLE_IPV6_CHECKSUM |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 79 | if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_CHECKSUM, |
| 80 | &offset, sizeof (offset)) < 0) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 81 | zlog_warn ("Network: set IPV6_CHECKSUM failed: %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 82 | #else |
| 83 | zlog_warn ("Network: Don't set IPV6_CHECKSUM"); |
| 84 | #endif /* DISABLE_IPV6_CHECKSUM */ |
| 85 | } |
| 86 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 87 | /* Make ospf6d's server socket. */ |
| 88 | int |
Paul Jakma | 6ac29a5 | 2008-08-15 13:45:30 +0100 | [diff] [blame] | 89 | ospf6_serv_sock (void) |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 90 | { |
| 91 | if (ospf6d_privs.change (ZPRIVS_RAISE)) |
| 92 | zlog_err ("ospf6_serv_sock: could not raise privs"); |
| 93 | |
| 94 | ospf6_sock = socket (AF_INET6, SOCK_RAW, IPPROTO_OSPFIGP); |
| 95 | if (ospf6_sock < 0) |
| 96 | { |
| 97 | zlog_warn ("Network: can't create OSPF6 socket."); |
| 98 | if (ospf6d_privs.change (ZPRIVS_LOWER)) |
| 99 | zlog_err ("ospf_sock_init: could not lower privs"); |
| 100 | return -1; |
| 101 | } |
| 102 | if (ospf6d_privs.change (ZPRIVS_LOWER)) |
| 103 | zlog_err ("ospf_sock_init: could not lower privs"); |
| 104 | |
| 105 | /* set socket options */ |
| 106 | #if 1 |
| 107 | sockopt_reuseaddr (ospf6_sock); |
| 108 | #else |
| 109 | ospf6_set_reuseaddr (); |
| 110 | #endif /*1*/ |
| 111 | ospf6_reset_mcastloop (); |
| 112 | ospf6_set_pktinfo (); |
Stephen Hemminger | d1e2faa | 2011-09-28 14:23:35 +0400 | [diff] [blame] | 113 | ospf6_set_transport_class (); |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 114 | ospf6_set_checksum (); |
| 115 | |
| 116 | /* setup global in6_addr, allspf6 and alldr6 for later use */ |
| 117 | inet_pton (AF_INET6, ALLSPFROUTERS6, &allspfrouters6); |
| 118 | inet_pton (AF_INET6, ALLDROUTERS6, &alldrouters6); |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 123 | void |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 124 | ospf6_join_allspfrouters (u_int ifindex) |
| 125 | { |
| 126 | struct ipv6_mreq mreq6; |
| 127 | int retval; |
| 128 | |
| 129 | assert (ifindex); |
| 130 | mreq6.ipv6mr_interface = ifindex; |
| 131 | memcpy (&mreq6.ipv6mr_multiaddr, &allspfrouters6, |
| 132 | sizeof (struct in6_addr)); |
| 133 | |
| 134 | retval = setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, |
| 135 | &mreq6, sizeof (mreq6)); |
| 136 | |
| 137 | if (retval < 0) |
| 138 | zlog_err ("Network: Join AllSPFRouters on ifindex %d failed: %s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 139 | ifindex, safe_strerror (errno)); |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 140 | #if 0 |
| 141 | else |
hasso | c6487d6 | 2004-12-24 06:00:11 +0000 | [diff] [blame] | 142 | zlog_debug ("Network: Join AllSPFRouters on ifindex %d", ifindex); |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 143 | #endif |
| 144 | } |
| 145 | |
| 146 | void |
| 147 | ospf6_leave_allspfrouters (u_int ifindex) |
| 148 | { |
| 149 | struct ipv6_mreq mreq6; |
| 150 | |
| 151 | assert (ifindex); |
| 152 | mreq6.ipv6mr_interface = ifindex; |
| 153 | memcpy (&mreq6.ipv6mr_multiaddr, &allspfrouters6, |
| 154 | sizeof (struct in6_addr)); |
| 155 | |
| 156 | if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP, |
| 157 | &mreq6, sizeof (mreq6)) < 0) |
| 158 | zlog_warn ("Network: Leave AllSPFRouters on ifindex %d Failed: %s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 159 | ifindex, safe_strerror (errno)); |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 160 | #if 0 |
| 161 | else |
hasso | c6487d6 | 2004-12-24 06:00:11 +0000 | [diff] [blame] | 162 | zlog_debug ("Network: Leave AllSPFRouters on ifindex %d", ifindex); |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 163 | #endif |
| 164 | } |
| 165 | |
| 166 | void |
| 167 | ospf6_join_alldrouters (u_int ifindex) |
| 168 | { |
| 169 | struct ipv6_mreq mreq6; |
| 170 | |
| 171 | assert (ifindex); |
| 172 | mreq6.ipv6mr_interface = ifindex; |
| 173 | memcpy (&mreq6.ipv6mr_multiaddr, &alldrouters6, |
| 174 | sizeof (struct in6_addr)); |
| 175 | |
| 176 | if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, |
| 177 | &mreq6, sizeof (mreq6)) < 0) |
| 178 | zlog_warn ("Network: Join AllDRouters on ifindex %d Failed: %s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 179 | ifindex, safe_strerror (errno)); |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 180 | #if 0 |
| 181 | else |
hasso | c6487d6 | 2004-12-24 06:00:11 +0000 | [diff] [blame] | 182 | zlog_debug ("Network: Join AllDRouters on ifindex %d", ifindex); |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 183 | #endif |
| 184 | } |
| 185 | |
| 186 | void |
| 187 | ospf6_leave_alldrouters (u_int ifindex) |
| 188 | { |
| 189 | struct ipv6_mreq mreq6; |
| 190 | |
| 191 | assert (ifindex); |
| 192 | mreq6.ipv6mr_interface = ifindex; |
| 193 | memcpy (&mreq6.ipv6mr_multiaddr, &alldrouters6, |
| 194 | sizeof (struct in6_addr)); |
| 195 | |
| 196 | if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP, |
| 197 | &mreq6, sizeof (mreq6)) < 0) |
| 198 | zlog_warn ("Network: Leave AllDRouters on ifindex %d Failed", ifindex); |
| 199 | #if 0 |
| 200 | else |
hasso | c6487d6 | 2004-12-24 06:00:11 +0000 | [diff] [blame] | 201 | zlog_debug ("Network: Leave AllDRouters on ifindex %d", ifindex); |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 202 | #endif |
| 203 | } |
| 204 | |
Paul Jakma | 6ac29a5 | 2008-08-15 13:45:30 +0100 | [diff] [blame] | 205 | static int |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 206 | iov_count (struct iovec *iov) |
| 207 | { |
| 208 | int i; |
| 209 | for (i = 0; iov[i].iov_base; i++) |
| 210 | ; |
| 211 | return i; |
| 212 | } |
| 213 | |
Paul Jakma | 6ac29a5 | 2008-08-15 13:45:30 +0100 | [diff] [blame] | 214 | static int |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 215 | iov_totallen (struct iovec *iov) |
| 216 | { |
| 217 | int i; |
| 218 | int totallen = 0; |
| 219 | for (i = 0; iov[i].iov_base; i++) |
| 220 | totallen += iov[i].iov_len; |
| 221 | return totallen; |
| 222 | } |
| 223 | |
| 224 | int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 225 | ospf6_sendmsg (struct in6_addr *src, struct in6_addr *dst, |
| 226 | unsigned int *ifindex, struct iovec *message) |
| 227 | { |
| 228 | int retval; |
| 229 | struct msghdr smsghdr; |
| 230 | struct cmsghdr *scmsgp; |
| 231 | u_char cmsgbuf[CMSG_SPACE(sizeof (struct in6_pktinfo))]; |
| 232 | struct in6_pktinfo *pktinfo; |
| 233 | struct sockaddr_in6 dst_sin6; |
| 234 | |
| 235 | assert (dst); |
| 236 | assert (*ifindex); |
| 237 | |
| 238 | scmsgp = (struct cmsghdr *)cmsgbuf; |
| 239 | pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp)); |
| 240 | memset (&dst_sin6, 0, sizeof (struct sockaddr_in6)); |
| 241 | |
| 242 | /* source address */ |
| 243 | pktinfo->ipi6_ifindex = *ifindex; |
| 244 | if (src) |
| 245 | memcpy (&pktinfo->ipi6_addr, src, sizeof (struct in6_addr)); |
| 246 | else |
| 247 | memset (&pktinfo->ipi6_addr, 0, sizeof (struct in6_addr)); |
| 248 | |
| 249 | /* destination address */ |
| 250 | dst_sin6.sin6_family = AF_INET6; |
| 251 | #ifdef SIN6_LEN |
| 252 | dst_sin6.sin6_len = sizeof (struct sockaddr_in6); |
| 253 | #endif /*SIN6_LEN*/ |
| 254 | memcpy (&dst_sin6.sin6_addr, dst, sizeof (struct in6_addr)); |
| 255 | #ifdef HAVE_SIN6_SCOPE_ID |
| 256 | dst_sin6.sin6_scope_id = *ifindex; |
| 257 | #endif |
| 258 | |
| 259 | /* send control msg */ |
| 260 | scmsgp->cmsg_level = IPPROTO_IPV6; |
| 261 | scmsgp->cmsg_type = IPV6_PKTINFO; |
| 262 | scmsgp->cmsg_len = CMSG_LEN (sizeof (struct in6_pktinfo)); |
| 263 | /* scmsgp = CMSG_NXTHDR (&smsghdr, scmsgp); */ |
| 264 | |
| 265 | /* send msg hdr */ |
Paul Jakma | cf1ce25 | 2006-05-15 10:46:07 +0000 | [diff] [blame] | 266 | memset (&smsghdr, 0, sizeof (smsghdr)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 267 | smsghdr.msg_iov = message; |
| 268 | smsghdr.msg_iovlen = iov_count (message); |
| 269 | smsghdr.msg_name = (caddr_t) &dst_sin6; |
| 270 | smsghdr.msg_namelen = sizeof (struct sockaddr_in6); |
| 271 | smsghdr.msg_control = (caddr_t) cmsgbuf; |
| 272 | smsghdr.msg_controllen = sizeof (cmsgbuf); |
| 273 | |
| 274 | retval = sendmsg (ospf6_sock, &smsghdr, 0); |
| 275 | if (retval != iov_totallen (message)) |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 276 | zlog_warn ("sendmsg failed: ifindex: %d: %s (%d)", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 277 | *ifindex, safe_strerror (errno), errno); |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 278 | |
| 279 | return retval; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 280 | } |
| 281 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 282 | int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 283 | ospf6_recvmsg (struct in6_addr *src, struct in6_addr *dst, |
| 284 | unsigned int *ifindex, struct iovec *message) |
| 285 | { |
| 286 | int retval; |
| 287 | struct msghdr rmsghdr; |
| 288 | struct cmsghdr *rcmsgp; |
| 289 | u_char cmsgbuf[CMSG_SPACE(sizeof (struct in6_pktinfo))]; |
| 290 | struct in6_pktinfo *pktinfo; |
| 291 | struct sockaddr_in6 src_sin6; |
| 292 | |
| 293 | rcmsgp = (struct cmsghdr *)cmsgbuf; |
| 294 | pktinfo = (struct in6_pktinfo *)(CMSG_DATA(rcmsgp)); |
| 295 | memset (&src_sin6, 0, sizeof (struct sockaddr_in6)); |
| 296 | |
| 297 | /* receive control msg */ |
| 298 | rcmsgp->cmsg_level = IPPROTO_IPV6; |
| 299 | rcmsgp->cmsg_type = IPV6_PKTINFO; |
| 300 | rcmsgp->cmsg_len = CMSG_LEN (sizeof (struct in6_pktinfo)); |
| 301 | /* rcmsgp = CMSG_NXTHDR (&rmsghdr, rcmsgp); */ |
| 302 | |
| 303 | /* receive msg hdr */ |
Paul Jakma | cf1ce25 | 2006-05-15 10:46:07 +0000 | [diff] [blame] | 304 | memset (&rmsghdr, 0, sizeof (rmsghdr)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 305 | rmsghdr.msg_iov = message; |
| 306 | rmsghdr.msg_iovlen = iov_count (message); |
| 307 | rmsghdr.msg_name = (caddr_t) &src_sin6; |
| 308 | rmsghdr.msg_namelen = sizeof (struct sockaddr_in6); |
| 309 | rmsghdr.msg_control = (caddr_t) cmsgbuf; |
| 310 | rmsghdr.msg_controllen = sizeof (cmsgbuf); |
| 311 | |
| 312 | retval = recvmsg (ospf6_sock, &rmsghdr, 0); |
| 313 | if (retval < 0) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 314 | zlog_warn ("recvmsg failed: %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 315 | else if (retval == iov_totallen (message)) |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 316 | zlog_warn ("recvmsg read full buffer size: %d", retval); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 317 | |
| 318 | /* source address */ |
| 319 | assert (src); |
| 320 | memcpy (src, &src_sin6.sin6_addr, sizeof (struct in6_addr)); |
| 321 | |
| 322 | /* destination address */ |
| 323 | if (ifindex) |
| 324 | *ifindex = pktinfo->ipi6_ifindex; |
| 325 | if (dst) |
| 326 | memcpy (dst, &pktinfo->ipi6_addr, sizeof (struct in6_addr)); |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 327 | |
| 328 | return retval; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 329 | } |
| 330 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 331 | |