paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * OSPF network related functions |
| 3 | * Copyright (C) 1999 Toshiaki Takada |
| 4 | * |
| 5 | * This file is part of GNU Zebra. |
| 6 | * |
| 7 | * GNU Zebra is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the |
| 9 | * Free Software Foundation; either version 2, or (at your option) any |
| 10 | * later version. |
| 11 | * |
| 12 | * GNU Zebra is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 19 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 20 | * 02111-1307, USA. |
| 21 | */ |
| 22 | |
| 23 | #include <zebra.h> |
| 24 | |
| 25 | #include "thread.h" |
| 26 | #include "linklist.h" |
| 27 | #include "prefix.h" |
| 28 | #include "if.h" |
| 29 | #include "sockunion.h" |
| 30 | #include "log.h" |
| 31 | #include "sockopt.h" |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 32 | #include "privs.h" |
| 33 | |
| 34 | extern struct zebra_privs_t ospfd_privs; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 35 | |
| 36 | #include "ospfd/ospfd.h" |
| 37 | #include "ospfd/ospf_network.h" |
| 38 | #include "ospfd/ospf_interface.h" |
| 39 | #include "ospfd/ospf_asbr.h" |
| 40 | #include "ospfd/ospf_lsa.h" |
| 41 | #include "ospfd/ospf_lsdb.h" |
| 42 | #include "ospfd/ospf_neighbor.h" |
| 43 | #include "ospfd/ospf_packet.h" |
Denis Ovsienko | b7fe414 | 2007-08-21 16:32:56 +0000 | [diff] [blame] | 44 | #include "ospfd/ospf_dump.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 45 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 46 | |
| 47 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 48 | /* Join to the OSPF ALL SPF ROUTERS multicast group. */ |
| 49 | int |
| 50 | ospf_if_add_allspfrouters (struct ospf *top, struct prefix *p, |
| 51 | unsigned int ifindex) |
| 52 | { |
| 53 | int ret; |
| 54 | |
| 55 | ret = setsockopt_multicast_ipv4 (top->fd, IP_ADD_MEMBERSHIP, |
| 56 | p->u.prefix4, htonl (OSPF_ALLSPFROUTERS), |
| 57 | ifindex); |
| 58 | if (ret < 0) |
ajs | 3dc56b5 | 2004-12-30 15:11:19 +0000 | [diff] [blame] | 59 | zlog_warn ("can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, " |
| 60 | "ifindex %u, AllSPFRouters): %s; perhaps a kernel limit " |
| 61 | "on # of multicast group memberships has been exceeded?", |
| 62 | top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 63 | else |
paul | 42c9819 | 2005-05-07 02:22:51 +0000 | [diff] [blame] | 64 | zlog_info ("interface %s [%u] join AllSPFRouters Multicast group.", |
| 65 | inet_ntoa (p->u.prefix4), ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 66 | |
| 67 | return ret; |
| 68 | } |
| 69 | |
| 70 | int |
| 71 | ospf_if_drop_allspfrouters (struct ospf *top, struct prefix *p, |
| 72 | unsigned int ifindex) |
| 73 | { |
| 74 | int ret; |
| 75 | |
| 76 | ret = setsockopt_multicast_ipv4 (top->fd, IP_DROP_MEMBERSHIP, |
| 77 | p->u.prefix4, htonl (OSPF_ALLSPFROUTERS), |
| 78 | ifindex); |
| 79 | if (ret < 0) |
ajs | 3dc56b5 | 2004-12-30 15:11:19 +0000 | [diff] [blame] | 80 | zlog_warn ("can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, " |
| 81 | "ifindex %u, AllSPFRouters): %s", |
| 82 | top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 83 | else |
paul | 42c9819 | 2005-05-07 02:22:51 +0000 | [diff] [blame] | 84 | zlog_info ("interface %s [%u] leave AllSPFRouters Multicast group.", |
| 85 | inet_ntoa (p->u.prefix4), ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 86 | |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | /* Join to the OSPF ALL Designated ROUTERS multicast group. */ |
| 91 | int |
| 92 | ospf_if_add_alldrouters (struct ospf *top, struct prefix *p, unsigned int |
| 93 | ifindex) |
| 94 | { |
| 95 | int ret; |
| 96 | |
| 97 | ret = setsockopt_multicast_ipv4 (top->fd, IP_ADD_MEMBERSHIP, |
| 98 | p->u.prefix4, htonl (OSPF_ALLDROUTERS), |
| 99 | ifindex); |
| 100 | if (ret < 0) |
ajs | 3dc56b5 | 2004-12-30 15:11:19 +0000 | [diff] [blame] | 101 | zlog_warn ("can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, " |
| 102 | "ifindex %u, AllDRouters): %s; perhaps a kernel limit " |
| 103 | "on # of multicast group memberships has been exceeded?", |
| 104 | top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 105 | else |
paul | 42c9819 | 2005-05-07 02:22:51 +0000 | [diff] [blame] | 106 | zlog_info ("interface %s [%u] join AllDRouters Multicast group.", |
| 107 | inet_ntoa (p->u.prefix4), ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 108 | |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | int |
| 113 | ospf_if_drop_alldrouters (struct ospf *top, struct prefix *p, unsigned int |
| 114 | ifindex) |
| 115 | { |
| 116 | int ret; |
| 117 | |
| 118 | ret = setsockopt_multicast_ipv4 (top->fd, IP_DROP_MEMBERSHIP, |
| 119 | p->u.prefix4, htonl (OSPF_ALLDROUTERS), |
| 120 | ifindex); |
| 121 | if (ret < 0) |
ajs | 3dc56b5 | 2004-12-30 15:11:19 +0000 | [diff] [blame] | 122 | zlog_warn ("can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, " |
| 123 | "ifindex %u, AllDRouters): %s", |
| 124 | top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 125 | else |
paul | 42c9819 | 2005-05-07 02:22:51 +0000 | [diff] [blame] | 126 | zlog_info ("interface %s [%u] leave AllDRouters Multicast group.", |
| 127 | inet_ntoa (p->u.prefix4), ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 128 | |
| 129 | return ret; |
| 130 | } |
| 131 | |
| 132 | int |
| 133 | ospf_if_ipmulticast (struct ospf *top, struct prefix *p, unsigned int ifindex) |
| 134 | { |
| 135 | u_char val; |
| 136 | int ret, len; |
| 137 | |
| 138 | val = 0; |
| 139 | len = sizeof (val); |
| 140 | |
| 141 | /* Prevent receiving self-origined multicast packets. */ |
| 142 | ret = setsockopt (top->fd, IPPROTO_IP, IP_MULTICAST_LOOP, (void *)&val, len); |
| 143 | if (ret < 0) |
ajs | 3dc56b5 | 2004-12-30 15:11:19 +0000 | [diff] [blame] | 144 | zlog_warn ("can't setsockopt IP_MULTICAST_LOOP(0) for fd %d: %s", |
| 145 | top->fd, safe_strerror(errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 146 | |
| 147 | /* Explicitly set multicast ttl to 1 -- endo. */ |
| 148 | val = 1; |
| 149 | ret = setsockopt (top->fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&val, len); |
| 150 | if (ret < 0) |
ajs | 3dc56b5 | 2004-12-30 15:11:19 +0000 | [diff] [blame] | 151 | zlog_warn ("can't setsockopt IP_MULTICAST_TTL(1) for fd %d: %s", |
| 152 | top->fd, safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 153 | |
| 154 | ret = setsockopt_multicast_ipv4 (top->fd, IP_MULTICAST_IF, |
| 155 | p->u.prefix4, 0, ifindex); |
| 156 | if (ret < 0) |
ajs | 3dc56b5 | 2004-12-30 15:11:19 +0000 | [diff] [blame] | 157 | zlog_warn("can't setsockopt IP_MULTICAST_IF(fd %d, addr %s, " |
| 158 | "ifindex %u): %s", |
| 159 | top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 160 | |
| 161 | return ret; |
| 162 | } |
| 163 | |
| 164 | int |
| 165 | ospf_sock_init (void) |
| 166 | { |
| 167 | int ospf_sock; |
gdt | f4436f7 | 2004-12-09 14:49:51 +0000 | [diff] [blame] | 168 | /* |
| 169 | * XXX warning: unused variable `tos' |
| 170 | * tos should be ifdefed similarly to usage |
| 171 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 172 | int ret, tos, hincl = 1; |
| 173 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 174 | if ( ospfd_privs.change (ZPRIVS_RAISE) ) |
| 175 | zlog_err ("ospf_sock_init: could not raise privs, %s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 176 | safe_strerror (errno) ); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 177 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 178 | ospf_sock = socket (AF_INET, SOCK_RAW, IPPROTO_OSPFIGP); |
| 179 | if (ospf_sock < 0) |
| 180 | { |
ajs | 0b7d97d | 2005-01-30 17:24:02 +0000 | [diff] [blame] | 181 | int save_errno = errno; |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 182 | if ( ospfd_privs.change (ZPRIVS_LOWER) ) |
| 183 | zlog_err ("ospf_sock_init: could not lower privs, %s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 184 | safe_strerror (errno) ); |
ajs | 0b7d97d | 2005-01-30 17:24:02 +0000 | [diff] [blame] | 185 | zlog_err ("ospf_read_sock_init: socket: %s", safe_strerror (save_errno)); |
| 186 | exit(1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 187 | } |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 188 | |
paul | 5bd4189 | 2004-05-05 17:29:24 +0000 | [diff] [blame] | 189 | #ifdef IP_HDRINCL |
| 190 | /* we will include IP header with packet */ |
| 191 | ret = setsockopt (ospf_sock, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof (hincl)); |
| 192 | if (ret < 0) |
| 193 | { |
ajs | 0b7d97d | 2005-01-30 17:24:02 +0000 | [diff] [blame] | 194 | int save_errno = errno; |
paul | 5bd4189 | 2004-05-05 17:29:24 +0000 | [diff] [blame] | 195 | if ( ospfd_privs.change (ZPRIVS_LOWER) ) |
| 196 | zlog_err ("ospf_sock_init: could not lower privs, %s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 197 | safe_strerror (errno) ); |
ajs | 0b7d97d | 2005-01-30 17:24:02 +0000 | [diff] [blame] | 198 | zlog_warn ("Can't set IP_HDRINCL option for fd %d: %s", |
| 199 | ospf_sock, safe_strerror(save_errno)); |
paul | 5bd4189 | 2004-05-05 17:29:24 +0000 | [diff] [blame] | 200 | } |
| 201 | #elif defined (IPTOS_PREC_INTERNETCONTROL) |
| 202 | #warning "IP_HDRINCL not available on this system" |
| 203 | #warning "using IPTOS_PREC_INTERNETCONTROL" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 204 | /* Set precedence field. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 205 | tos = IPTOS_PREC_INTERNETCONTROL; |
| 206 | ret = setsockopt (ospf_sock, IPPROTO_IP, IP_TOS, |
| 207 | (char *) &tos, sizeof (int)); |
| 208 | if (ret < 0) |
| 209 | { |
ajs | 0b7d97d | 2005-01-30 17:24:02 +0000 | [diff] [blame] | 210 | int save_errno = errno; |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 211 | if ( ospfd_privs.change (ZPRIVS_LOWER) ) |
| 212 | zlog_err ("ospf_sock_init: could not lower privs, %s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 213 | safe_strerror (errno) ); |
ajs | 0b7d97d | 2005-01-30 17:24:02 +0000 | [diff] [blame] | 214 | zlog_warn ("can't set sockopt IP_TOS %d to socket %d: %s", |
| 215 | tos, ospf_sock, safe_strerror(save_errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 216 | close (ospf_sock); /* Prevent sd leak. */ |
| 217 | return ret; |
| 218 | } |
paul | 5bd4189 | 2004-05-05 17:29:24 +0000 | [diff] [blame] | 219 | #else /* !IPTOS_PREC_INTERNETCONTROL */ |
| 220 | #warning "IP_HDRINCL not available, nor is IPTOS_PREC_INTERNETCONTROL" |
| 221 | zlog_warn ("IP_HDRINCL option not available"); |
| 222 | #endif /* IP_HDRINCL */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 223 | |
paul | ac19123 | 2004-10-22 12:05:17 +0000 | [diff] [blame] | 224 | ret = setsockopt_ifindex (AF_INET, ospf_sock, 1); |
| 225 | |
paul | 2dd8bb4 | 2004-07-23 15:13:48 +0000 | [diff] [blame] | 226 | if (ret < 0) |
ajs | 3dc56b5 | 2004-12-30 15:11:19 +0000 | [diff] [blame] | 227 | zlog_warn ("Can't set pktinfo option for fd %d", ospf_sock); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 228 | |
| 229 | if (ospfd_privs.change (ZPRIVS_LOWER)) |
| 230 | { |
| 231 | zlog_err ("ospf_sock_init: could not lower privs, %s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 232 | safe_strerror (errno) ); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 233 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 234 | |
| 235 | return ospf_sock; |
| 236 | } |
Denis Ovsienko | b7fe414 | 2007-08-21 16:32:56 +0000 | [diff] [blame] | 237 | |
| 238 | void |
| 239 | ospf_adjust_sndbuflen (struct ospf * ospf, int buflen) |
| 240 | { |
| 241 | int ret, newbuflen; |
| 242 | /* Check if any work has to be done at all. */ |
| 243 | if (ospf->maxsndbuflen >= buflen) |
| 244 | return; |
| 245 | if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE)) |
| 246 | zlog_debug ("%s: adjusting OSPF send buffer size to %d", |
| 247 | __func__, buflen); |
| 248 | if (ospfd_privs.change (ZPRIVS_RAISE)) |
| 249 | zlog_err ("%s: could not raise privs, %s", __func__, |
| 250 | safe_strerror (errno)); |
| 251 | /* Now we try to set SO_SNDBUF to what our caller has requested |
| 252 | * (OSPF_SNDBUFLEN_DEFAULT initially, which seems to be a sane |
| 253 | * default; or the MTU of a newly added interface). However, |
| 254 | * if the OS has truncated the actual buffer size to somewhat |
| 255 | * less or bigger size, try to detect it and update our records |
| 256 | * appropriately. |
| 257 | */ |
| 258 | ret = setsockopt_so_sendbuf (ospf->fd, buflen); |
| 259 | newbuflen = getsockopt_so_sendbuf (ospf->fd); |
| 260 | if (ret < 0 || newbuflen != buflen) |
| 261 | zlog_warn ("%s: tried to set SO_SNDBUF to %d, but got %d", |
| 262 | __func__, buflen, newbuflen); |
| 263 | if (newbuflen >= 0) |
| 264 | ospf->maxsndbuflen = newbuflen; |
| 265 | else |
| 266 | zlog_warn ("%s: failed to get SO_SNDBUF", __func__); |
| 267 | if (ospfd_privs.change (ZPRIVS_LOWER)) |
| 268 | zlog_err ("%s: could not lower privs, %s", __func__, |
| 269 | safe_strerror (errno)); |
| 270 | } |