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" |
| 44 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 45 | |
| 46 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 47 | /* Join to the OSPF ALL SPF ROUTERS multicast group. */ |
| 48 | int |
| 49 | ospf_if_add_allspfrouters (struct ospf *top, struct prefix *p, |
| 50 | unsigned int ifindex) |
| 51 | { |
| 52 | int ret; |
| 53 | |
| 54 | ret = setsockopt_multicast_ipv4 (top->fd, IP_ADD_MEMBERSHIP, |
| 55 | p->u.prefix4, htonl (OSPF_ALLSPFROUTERS), |
| 56 | ifindex); |
| 57 | if (ret < 0) |
ajs | 3dc56b5 | 2004-12-30 15:11:19 +0000 | [diff] [blame] | 58 | zlog_warn ("can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, " |
| 59 | "ifindex %u, AllSPFRouters): %s; perhaps a kernel limit " |
| 60 | "on # of multicast group memberships has been exceeded?", |
| 61 | top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 62 | else |
paul | 42c9819 | 2005-05-07 02:22:51 +0000 | [diff] [blame] | 63 | zlog_info ("interface %s [%u] join AllSPFRouters Multicast group.", |
| 64 | inet_ntoa (p->u.prefix4), ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 65 | |
| 66 | return ret; |
| 67 | } |
| 68 | |
| 69 | int |
| 70 | ospf_if_drop_allspfrouters (struct ospf *top, struct prefix *p, |
| 71 | unsigned int ifindex) |
| 72 | { |
| 73 | int ret; |
| 74 | |
| 75 | ret = setsockopt_multicast_ipv4 (top->fd, IP_DROP_MEMBERSHIP, |
| 76 | p->u.prefix4, htonl (OSPF_ALLSPFROUTERS), |
| 77 | ifindex); |
| 78 | if (ret < 0) |
ajs | 3dc56b5 | 2004-12-30 15:11:19 +0000 | [diff] [blame] | 79 | zlog_warn ("can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, " |
| 80 | "ifindex %u, AllSPFRouters): %s", |
| 81 | top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 82 | else |
paul | 42c9819 | 2005-05-07 02:22:51 +0000 | [diff] [blame] | 83 | zlog_info ("interface %s [%u] leave AllSPFRouters Multicast group.", |
| 84 | inet_ntoa (p->u.prefix4), ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 85 | |
| 86 | return ret; |
| 87 | } |
| 88 | |
| 89 | /* Join to the OSPF ALL Designated ROUTERS multicast group. */ |
| 90 | int |
| 91 | ospf_if_add_alldrouters (struct ospf *top, struct prefix *p, unsigned int |
| 92 | ifindex) |
| 93 | { |
| 94 | int ret; |
| 95 | |
| 96 | ret = setsockopt_multicast_ipv4 (top->fd, IP_ADD_MEMBERSHIP, |
| 97 | p->u.prefix4, htonl (OSPF_ALLDROUTERS), |
| 98 | ifindex); |
| 99 | if (ret < 0) |
ajs | 3dc56b5 | 2004-12-30 15:11:19 +0000 | [diff] [blame] | 100 | zlog_warn ("can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, " |
| 101 | "ifindex %u, AllDRouters): %s; perhaps a kernel limit " |
| 102 | "on # of multicast group memberships has been exceeded?", |
| 103 | top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 104 | else |
paul | 42c9819 | 2005-05-07 02:22:51 +0000 | [diff] [blame] | 105 | zlog_info ("interface %s [%u] join AllDRouters Multicast group.", |
| 106 | inet_ntoa (p->u.prefix4), ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 107 | |
| 108 | return ret; |
| 109 | } |
| 110 | |
| 111 | int |
| 112 | ospf_if_drop_alldrouters (struct ospf *top, struct prefix *p, unsigned int |
| 113 | ifindex) |
| 114 | { |
| 115 | int ret; |
| 116 | |
| 117 | ret = setsockopt_multicast_ipv4 (top->fd, IP_DROP_MEMBERSHIP, |
| 118 | p->u.prefix4, htonl (OSPF_ALLDROUTERS), |
| 119 | ifindex); |
| 120 | if (ret < 0) |
ajs | 3dc56b5 | 2004-12-30 15:11:19 +0000 | [diff] [blame] | 121 | zlog_warn ("can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, " |
| 122 | "ifindex %u, AllDRouters): %s", |
| 123 | top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 124 | else |
paul | 42c9819 | 2005-05-07 02:22:51 +0000 | [diff] [blame] | 125 | zlog_info ("interface %s [%u] leave AllDRouters Multicast group.", |
| 126 | inet_ntoa (p->u.prefix4), ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 127 | |
| 128 | return ret; |
| 129 | } |
| 130 | |
| 131 | int |
| 132 | ospf_if_ipmulticast (struct ospf *top, struct prefix *p, unsigned int ifindex) |
| 133 | { |
| 134 | u_char val; |
| 135 | int ret, len; |
| 136 | |
| 137 | val = 0; |
| 138 | len = sizeof (val); |
| 139 | |
| 140 | /* Prevent receiving self-origined multicast packets. */ |
| 141 | ret = setsockopt (top->fd, IPPROTO_IP, IP_MULTICAST_LOOP, (void *)&val, len); |
| 142 | if (ret < 0) |
ajs | 3dc56b5 | 2004-12-30 15:11:19 +0000 | [diff] [blame] | 143 | zlog_warn ("can't setsockopt IP_MULTICAST_LOOP(0) for fd %d: %s", |
| 144 | top->fd, safe_strerror(errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 145 | |
| 146 | /* Explicitly set multicast ttl to 1 -- endo. */ |
| 147 | val = 1; |
| 148 | ret = setsockopt (top->fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&val, len); |
| 149 | if (ret < 0) |
ajs | 3dc56b5 | 2004-12-30 15:11:19 +0000 | [diff] [blame] | 150 | zlog_warn ("can't setsockopt IP_MULTICAST_TTL(1) for fd %d: %s", |
| 151 | top->fd, safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 152 | |
| 153 | ret = setsockopt_multicast_ipv4 (top->fd, IP_MULTICAST_IF, |
| 154 | p->u.prefix4, 0, ifindex); |
| 155 | if (ret < 0) |
ajs | 3dc56b5 | 2004-12-30 15:11:19 +0000 | [diff] [blame] | 156 | zlog_warn("can't setsockopt IP_MULTICAST_IF(fd %d, addr %s, " |
| 157 | "ifindex %u): %s", |
| 158 | top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 159 | |
| 160 | return ret; |
| 161 | } |
| 162 | |
| 163 | int |
| 164 | ospf_sock_init (void) |
| 165 | { |
| 166 | int ospf_sock; |
gdt | f4436f7 | 2004-12-09 14:49:51 +0000 | [diff] [blame] | 167 | /* |
| 168 | * XXX warning: unused variable `tos' |
| 169 | * tos should be ifdefed similarly to usage |
| 170 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 171 | int ret, tos, hincl = 1; |
| 172 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 173 | if ( ospfd_privs.change (ZPRIVS_RAISE) ) |
| 174 | zlog_err ("ospf_sock_init: could not raise privs, %s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 175 | safe_strerror (errno) ); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 176 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 177 | ospf_sock = socket (AF_INET, SOCK_RAW, IPPROTO_OSPFIGP); |
| 178 | if (ospf_sock < 0) |
| 179 | { |
ajs | 0b7d97d | 2005-01-30 17:24:02 +0000 | [diff] [blame] | 180 | int save_errno = errno; |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 181 | if ( ospfd_privs.change (ZPRIVS_LOWER) ) |
| 182 | zlog_err ("ospf_sock_init: could not lower privs, %s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 183 | safe_strerror (errno) ); |
ajs | 0b7d97d | 2005-01-30 17:24:02 +0000 | [diff] [blame] | 184 | zlog_err ("ospf_read_sock_init: socket: %s", safe_strerror (save_errno)); |
| 185 | exit(1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 186 | } |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 187 | |
paul | 5bd4189 | 2004-05-05 17:29:24 +0000 | [diff] [blame] | 188 | #ifdef IP_HDRINCL |
| 189 | /* we will include IP header with packet */ |
| 190 | ret = setsockopt (ospf_sock, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof (hincl)); |
| 191 | if (ret < 0) |
| 192 | { |
ajs | 0b7d97d | 2005-01-30 17:24:02 +0000 | [diff] [blame] | 193 | int save_errno = errno; |
paul | 5bd4189 | 2004-05-05 17:29:24 +0000 | [diff] [blame] | 194 | if ( ospfd_privs.change (ZPRIVS_LOWER) ) |
| 195 | zlog_err ("ospf_sock_init: could not lower privs, %s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 196 | safe_strerror (errno) ); |
ajs | 0b7d97d | 2005-01-30 17:24:02 +0000 | [diff] [blame] | 197 | zlog_warn ("Can't set IP_HDRINCL option for fd %d: %s", |
| 198 | ospf_sock, safe_strerror(save_errno)); |
paul | 5bd4189 | 2004-05-05 17:29:24 +0000 | [diff] [blame] | 199 | } |
| 200 | #elif defined (IPTOS_PREC_INTERNETCONTROL) |
| 201 | #warning "IP_HDRINCL not available on this system" |
| 202 | #warning "using IPTOS_PREC_INTERNETCONTROL" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 203 | /* Set precedence field. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 204 | tos = IPTOS_PREC_INTERNETCONTROL; |
| 205 | ret = setsockopt (ospf_sock, IPPROTO_IP, IP_TOS, |
| 206 | (char *) &tos, sizeof (int)); |
| 207 | if (ret < 0) |
| 208 | { |
ajs | 0b7d97d | 2005-01-30 17:24:02 +0000 | [diff] [blame] | 209 | int save_errno = errno; |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 210 | if ( ospfd_privs.change (ZPRIVS_LOWER) ) |
| 211 | zlog_err ("ospf_sock_init: could not lower privs, %s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 212 | safe_strerror (errno) ); |
ajs | 0b7d97d | 2005-01-30 17:24:02 +0000 | [diff] [blame] | 213 | zlog_warn ("can't set sockopt IP_TOS %d to socket %d: %s", |
| 214 | tos, ospf_sock, safe_strerror(save_errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 215 | close (ospf_sock); /* Prevent sd leak. */ |
| 216 | return ret; |
| 217 | } |
paul | 5bd4189 | 2004-05-05 17:29:24 +0000 | [diff] [blame] | 218 | #else /* !IPTOS_PREC_INTERNETCONTROL */ |
| 219 | #warning "IP_HDRINCL not available, nor is IPTOS_PREC_INTERNETCONTROL" |
| 220 | zlog_warn ("IP_HDRINCL option not available"); |
| 221 | #endif /* IP_HDRINCL */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 222 | |
paul | ac19123 | 2004-10-22 12:05:17 +0000 | [diff] [blame] | 223 | ret = setsockopt_ifindex (AF_INET, ospf_sock, 1); |
| 224 | |
paul | 2dd8bb4 | 2004-07-23 15:13:48 +0000 | [diff] [blame] | 225 | if (ret < 0) |
ajs | 3dc56b5 | 2004-12-30 15:11:19 +0000 | [diff] [blame] | 226 | zlog_warn ("Can't set pktinfo option for fd %d", ospf_sock); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 227 | |
| 228 | if (ospfd_privs.change (ZPRIVS_LOWER)) |
| 229 | { |
| 230 | zlog_err ("ospf_sock_init: could not lower privs, %s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 231 | safe_strerror (errno) ); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 232 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 233 | |
| 234 | return ospf_sock; |
| 235 | } |