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 | |
Dinesh Dutt | 8551e6d | 2013-10-22 17:42:18 -0700 | [diff] [blame] | 30 | #include "libospf.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 31 | #include "ospf6_proto.h" |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 32 | #include "ospf6_network.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 33 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 34 | extern struct zebra_privs_t ospf6d_privs; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 35 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 36 | int ospf6_sock; |
| 37 | struct in6_addr allspfrouters6; |
| 38 | struct in6_addr alldrouters6; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 39 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 40 | /* setsockopt MulticastLoop to off */ |
Paul Jakma | 7aa9dce | 2014-09-19 14:42:23 +0100 | [diff] [blame] | 41 | static void |
Paul Jakma | 6ac29a5 | 2008-08-15 13:45:30 +0100 | [diff] [blame] | 42 | ospf6_reset_mcastloop (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 43 | { |
| 44 | u_int off = 0; |
| 45 | if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, |
| 46 | &off, sizeof (u_int)) < 0) |
| 47 | zlog_warn ("Network: reset IPV6_MULTICAST_LOOP failed: %s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 48 | safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Paul Jakma | 7aa9dce | 2014-09-19 14:42:23 +0100 | [diff] [blame] | 51 | static void |
Paul Jakma | 6ac29a5 | 2008-08-15 13:45:30 +0100 | [diff] [blame] | 52 | ospf6_set_pktinfo (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 53 | { |
paul | 79dc373 | 2004-07-23 15:17:45 +0000 | [diff] [blame] | 54 | setsockopt_ipv6_pktinfo (ospf6_sock, 1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Paul Jakma | 7aa9dce | 2014-09-19 14:42:23 +0100 | [diff] [blame] | 57 | static void |
Stephen Hemminger | 6d0732c | 2011-09-28 14:23:35 +0400 | [diff] [blame] | 58 | ospf6_set_transport_class (void) |
| 59 | { |
| 60 | #ifdef IPTOS_PREC_INTERNETCONTROL |
| 61 | setsockopt_ipv6_tclass (ospf6_sock, IPTOS_PREC_INTERNETCONTROL); |
| 62 | #endif |
| 63 | } |
| 64 | |
Paul Jakma | 7aa9dce | 2014-09-19 14:42:23 +0100 | [diff] [blame] | 65 | static void |
Paul Jakma | 6ac29a5 | 2008-08-15 13:45:30 +0100 | [diff] [blame] | 66 | ospf6_set_checksum (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 67 | { |
| 68 | int offset = 12; |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 69 | #ifndef DISABLE_IPV6_CHECKSUM |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 70 | if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_CHECKSUM, |
| 71 | &offset, sizeof (offset)) < 0) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 72 | zlog_warn ("Network: set IPV6_CHECKSUM failed: %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 73 | #else |
| 74 | zlog_warn ("Network: Don't set IPV6_CHECKSUM"); |
| 75 | #endif /* DISABLE_IPV6_CHECKSUM */ |
| 76 | } |
| 77 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 78 | /* Make ospf6d's server socket. */ |
| 79 | int |
Paul Jakma | 6ac29a5 | 2008-08-15 13:45:30 +0100 | [diff] [blame] | 80 | ospf6_serv_sock (void) |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 81 | { |
| 82 | if (ospf6d_privs.change (ZPRIVS_RAISE)) |
| 83 | zlog_err ("ospf6_serv_sock: could not raise privs"); |
| 84 | |
| 85 | ospf6_sock = socket (AF_INET6, SOCK_RAW, IPPROTO_OSPFIGP); |
| 86 | if (ospf6_sock < 0) |
| 87 | { |
| 88 | zlog_warn ("Network: can't create OSPF6 socket."); |
| 89 | if (ospf6d_privs.change (ZPRIVS_LOWER)) |
| 90 | zlog_err ("ospf_sock_init: could not lower privs"); |
| 91 | return -1; |
| 92 | } |
| 93 | if (ospf6d_privs.change (ZPRIVS_LOWER)) |
| 94 | zlog_err ("ospf_sock_init: could not lower privs"); |
| 95 | |
| 96 | /* set socket options */ |
| 97 | #if 1 |
| 98 | sockopt_reuseaddr (ospf6_sock); |
| 99 | #else |
| 100 | ospf6_set_reuseaddr (); |
| 101 | #endif /*1*/ |
| 102 | ospf6_reset_mcastloop (); |
| 103 | ospf6_set_pktinfo (); |
Stephen Hemminger | 6d0732c | 2011-09-28 14:23:35 +0400 | [diff] [blame] | 104 | ospf6_set_transport_class (); |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 105 | ospf6_set_checksum (); |
| 106 | |
| 107 | /* setup global in6_addr, allspf6 and alldr6 for later use */ |
| 108 | inet_pton (AF_INET6, ALLSPFROUTERS6, &allspfrouters6); |
| 109 | inet_pton (AF_INET6, ALLDROUTERS6, &alldrouters6); |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
Vyacheslav Trushkin | 9a9446e | 2011-11-21 20:26:39 +0400 | [diff] [blame] | 114 | /* ospf6 set socket option */ |
Vipin Kumar | cb37de4 | 2015-05-19 18:03:40 -0700 | [diff] [blame^] | 115 | int |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 116 | ospf6_sso (ifindex_t ifindex, struct in6_addr *group, int option) |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 117 | { |
| 118 | struct ipv6_mreq mreq6; |
Vyacheslav Trushkin | 9a9446e | 2011-11-21 20:26:39 +0400 | [diff] [blame] | 119 | int ret; |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 120 | |
| 121 | assert (ifindex); |
| 122 | mreq6.ipv6mr_interface = ifindex; |
Vyacheslav Trushkin | 9a9446e | 2011-11-21 20:26:39 +0400 | [diff] [blame] | 123 | memcpy (&mreq6.ipv6mr_multiaddr, group, sizeof (struct in6_addr)); |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 124 | |
Vyacheslav Trushkin | 9a9446e | 2011-11-21 20:26:39 +0400 | [diff] [blame] | 125 | ret = setsockopt (ospf6_sock, IPPROTO_IPV6, option, |
| 126 | &mreq6, sizeof (mreq6)); |
| 127 | if (ret < 0) |
Vipin Kumar | cb37de4 | 2015-05-19 18:03:40 -0700 | [diff] [blame^] | 128 | { |
| 129 | zlog_err ("Network: setsockopt (%d) on ifindex %d failed: %s", |
| 130 | option, ifindex, safe_strerror (errno)); |
| 131 | } |
| 132 | |
| 133 | return ret; |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Paul Jakma | 6ac29a5 | 2008-08-15 13:45:30 +0100 | [diff] [blame] | 136 | static int |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 137 | iov_count (struct iovec *iov) |
| 138 | { |
| 139 | int i; |
| 140 | for (i = 0; iov[i].iov_base; i++) |
| 141 | ; |
| 142 | return i; |
| 143 | } |
| 144 | |
Paul Jakma | 6ac29a5 | 2008-08-15 13:45:30 +0100 | [diff] [blame] | 145 | static int |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 146 | iov_totallen (struct iovec *iov) |
| 147 | { |
| 148 | int i; |
| 149 | int totallen = 0; |
| 150 | for (i = 0; iov[i].iov_base; i++) |
| 151 | totallen += iov[i].iov_len; |
| 152 | return totallen; |
| 153 | } |
| 154 | |
| 155 | int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 156 | ospf6_sendmsg (struct in6_addr *src, struct in6_addr *dst, |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 157 | ifindex_t *ifindex, struct iovec *message) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 158 | { |
| 159 | int retval; |
| 160 | struct msghdr smsghdr; |
| 161 | struct cmsghdr *scmsgp; |
| 162 | u_char cmsgbuf[CMSG_SPACE(sizeof (struct in6_pktinfo))]; |
| 163 | struct in6_pktinfo *pktinfo; |
| 164 | struct sockaddr_in6 dst_sin6; |
| 165 | |
| 166 | assert (dst); |
| 167 | assert (*ifindex); |
| 168 | |
| 169 | scmsgp = (struct cmsghdr *)cmsgbuf; |
| 170 | pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp)); |
| 171 | memset (&dst_sin6, 0, sizeof (struct sockaddr_in6)); |
| 172 | |
| 173 | /* source address */ |
| 174 | pktinfo->ipi6_ifindex = *ifindex; |
| 175 | if (src) |
| 176 | memcpy (&pktinfo->ipi6_addr, src, sizeof (struct in6_addr)); |
| 177 | else |
| 178 | memset (&pktinfo->ipi6_addr, 0, sizeof (struct in6_addr)); |
| 179 | |
| 180 | /* destination address */ |
| 181 | dst_sin6.sin6_family = AF_INET6; |
| 182 | #ifdef SIN6_LEN |
| 183 | dst_sin6.sin6_len = sizeof (struct sockaddr_in6); |
| 184 | #endif /*SIN6_LEN*/ |
| 185 | memcpy (&dst_sin6.sin6_addr, dst, sizeof (struct in6_addr)); |
| 186 | #ifdef HAVE_SIN6_SCOPE_ID |
| 187 | dst_sin6.sin6_scope_id = *ifindex; |
| 188 | #endif |
| 189 | |
| 190 | /* send control msg */ |
| 191 | scmsgp->cmsg_level = IPPROTO_IPV6; |
| 192 | scmsgp->cmsg_type = IPV6_PKTINFO; |
| 193 | scmsgp->cmsg_len = CMSG_LEN (sizeof (struct in6_pktinfo)); |
| 194 | /* scmsgp = CMSG_NXTHDR (&smsghdr, scmsgp); */ |
| 195 | |
| 196 | /* send msg hdr */ |
Paul Jakma | cf1ce25 | 2006-05-15 10:46:07 +0000 | [diff] [blame] | 197 | memset (&smsghdr, 0, sizeof (smsghdr)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 198 | smsghdr.msg_iov = message; |
| 199 | smsghdr.msg_iovlen = iov_count (message); |
| 200 | smsghdr.msg_name = (caddr_t) &dst_sin6; |
| 201 | smsghdr.msg_namelen = sizeof (struct sockaddr_in6); |
| 202 | smsghdr.msg_control = (caddr_t) cmsgbuf; |
Christian Franke | 1579a67 | 2013-03-08 02:35:38 +0100 | [diff] [blame] | 203 | smsghdr.msg_controllen = scmsgp->cmsg_len; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 204 | |
| 205 | retval = sendmsg (ospf6_sock, &smsghdr, 0); |
| 206 | if (retval != iov_totallen (message)) |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 207 | zlog_warn ("sendmsg failed: ifindex: %d: %s (%d)", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 208 | *ifindex, safe_strerror (errno), errno); |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 209 | |
| 210 | return retval; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 211 | } |
| 212 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 213 | int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 214 | ospf6_recvmsg (struct in6_addr *src, struct in6_addr *dst, |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 215 | ifindex_t *ifindex, struct iovec *message) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 216 | { |
| 217 | int retval; |
| 218 | struct msghdr rmsghdr; |
| 219 | struct cmsghdr *rcmsgp; |
| 220 | u_char cmsgbuf[CMSG_SPACE(sizeof (struct in6_pktinfo))]; |
| 221 | struct in6_pktinfo *pktinfo; |
| 222 | struct sockaddr_in6 src_sin6; |
| 223 | |
| 224 | rcmsgp = (struct cmsghdr *)cmsgbuf; |
| 225 | pktinfo = (struct in6_pktinfo *)(CMSG_DATA(rcmsgp)); |
| 226 | memset (&src_sin6, 0, sizeof (struct sockaddr_in6)); |
| 227 | |
| 228 | /* receive control msg */ |
| 229 | rcmsgp->cmsg_level = IPPROTO_IPV6; |
| 230 | rcmsgp->cmsg_type = IPV6_PKTINFO; |
| 231 | rcmsgp->cmsg_len = CMSG_LEN (sizeof (struct in6_pktinfo)); |
| 232 | /* rcmsgp = CMSG_NXTHDR (&rmsghdr, rcmsgp); */ |
| 233 | |
| 234 | /* receive msg hdr */ |
Paul Jakma | cf1ce25 | 2006-05-15 10:46:07 +0000 | [diff] [blame] | 235 | memset (&rmsghdr, 0, sizeof (rmsghdr)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 236 | rmsghdr.msg_iov = message; |
| 237 | rmsghdr.msg_iovlen = iov_count (message); |
| 238 | rmsghdr.msg_name = (caddr_t) &src_sin6; |
| 239 | rmsghdr.msg_namelen = sizeof (struct sockaddr_in6); |
| 240 | rmsghdr.msg_control = (caddr_t) cmsgbuf; |
| 241 | rmsghdr.msg_controllen = sizeof (cmsgbuf); |
| 242 | |
| 243 | retval = recvmsg (ospf6_sock, &rmsghdr, 0); |
| 244 | if (retval < 0) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 245 | zlog_warn ("recvmsg failed: %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 246 | else if (retval == iov_totallen (message)) |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 247 | zlog_warn ("recvmsg read full buffer size: %d", retval); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 248 | |
| 249 | /* source address */ |
| 250 | assert (src); |
| 251 | memcpy (src, &src_sin6.sin6_addr, sizeof (struct in6_addr)); |
| 252 | |
| 253 | /* destination address */ |
| 254 | if (ifindex) |
| 255 | *ifindex = pktinfo->ipi6_ifindex; |
| 256 | if (dst) |
| 257 | memcpy (dst, &pktinfo->ipi6_addr, sizeof (struct in6_addr)); |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 258 | |
| 259 | return retval; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 260 | } |
| 261 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 262 | |