]>
Commit | Line | Data |
---|---|---|
718e3744 | 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 | * | |
896014f4 DL |
17 | * You should have received a copy of the GNU General Public License along |
18 | * with this program; see the file COPYING; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
718e3744 | 20 | */ |
21 | ||
22 | #include <zebra.h> | |
23 | ||
24 | #include "thread.h" | |
25 | #include "linklist.h" | |
26 | #include "prefix.h" | |
27 | #include "if.h" | |
28 | #include "sockunion.h" | |
29 | #include "log.h" | |
30 | #include "sockopt.h" | |
edd7c245 | 31 | #include "privs.h" |
32 | ||
718e3744 | 33 | #include "ospfd/ospfd.h" |
34 | #include "ospfd/ospf_network.h" | |
35 | #include "ospfd/ospf_interface.h" | |
36 | #include "ospfd/ospf_asbr.h" | |
37 | #include "ospfd/ospf_lsa.h" | |
38 | #include "ospfd/ospf_lsdb.h" | |
39 | #include "ospfd/ospf_neighbor.h" | |
40 | #include "ospfd/ospf_packet.h" | |
05ba78e4 | 41 | #include "ospfd/ospf_dump.h" |
edd7c245 | 42 | |
718e3744 | 43 | /* Join to the OSPF ALL SPF ROUTERS multicast group. */ |
d62a17ae | 44 | int ospf_if_add_allspfrouters(struct ospf *top, struct prefix *p, |
45 | ifindex_t ifindex) | |
718e3744 | 46 | { |
d62a17ae | 47 | int ret; |
48 | ||
49 | ret = setsockopt_ipv4_multicast(top->fd, IP_ADD_MEMBERSHIP, | |
50 | p->u.prefix4, htonl(OSPF_ALLSPFROUTERS), | |
51 | ifindex); | |
52 | if (ret < 0) | |
53 | zlog_warn( | |
54 | "can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, " | |
55 | "ifindex %u, AllSPFRouters): %s; perhaps a kernel limit " | |
56 | "on # of multicast group memberships has been exceeded?", | |
57 | top->fd, inet_ntoa(p->u.prefix4), ifindex, | |
58 | safe_strerror(errno)); | |
05ba78e4 CS |
59 | else { |
60 | if (IS_DEBUG_OSPF_EVENT) | |
61 | zlog_debug("interface %s [%u] join AllSPFRouters Multicast group.", | |
62 | inet_ntoa(p->u.prefix4), ifindex); | |
63 | } | |
d62a17ae | 64 | |
65 | return ret; | |
718e3744 | 66 | } |
67 | ||
d62a17ae | 68 | int ospf_if_drop_allspfrouters(struct ospf *top, struct prefix *p, |
69 | ifindex_t ifindex) | |
718e3744 | 70 | { |
d62a17ae | 71 | int ret; |
72 | ||
73 | ret = setsockopt_ipv4_multicast(top->fd, IP_DROP_MEMBERSHIP, | |
74 | p->u.prefix4, htonl(OSPF_ALLSPFROUTERS), | |
75 | ifindex); | |
76 | if (ret < 0) | |
77 | zlog_warn( | |
78 | "can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, " | |
79 | "ifindex %u, AllSPFRouters): %s", | |
80 | top->fd, inet_ntoa(p->u.prefix4), ifindex, | |
81 | safe_strerror(errno)); | |
05ba78e4 CS |
82 | else { |
83 | if (IS_DEBUG_OSPF_EVENT) | |
84 | zlog_debug("interface %s [%u] leave AllSPFRouters Multicast group.", | |
85 | inet_ntoa(p->u.prefix4), ifindex); | |
86 | } | |
d62a17ae | 87 | |
88 | return ret; | |
718e3744 | 89 | } |
90 | ||
91 | /* Join to the OSPF ALL Designated ROUTERS multicast group. */ | |
d62a17ae | 92 | int ospf_if_add_alldrouters(struct ospf *top, struct prefix *p, |
93 | ifindex_t ifindex) | |
718e3744 | 94 | { |
d62a17ae | 95 | int ret; |
96 | ||
97 | ret = setsockopt_ipv4_multicast(top->fd, IP_ADD_MEMBERSHIP, | |
98 | p->u.prefix4, htonl(OSPF_ALLDROUTERS), | |
99 | ifindex); | |
100 | if (ret < 0) | |
101 | zlog_warn( | |
102 | "can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, " | |
103 | "ifindex %u, AllDRouters): %s; perhaps a kernel limit " | |
104 | "on # of multicast group memberships has been exceeded?", | |
105 | top->fd, inet_ntoa(p->u.prefix4), ifindex, | |
106 | safe_strerror(errno)); | |
107 | else | |
108 | zlog_debug( | |
109 | "interface %s [%u] join AllDRouters Multicast group.", | |
110 | inet_ntoa(p->u.prefix4), ifindex); | |
111 | ||
112 | return ret; | |
718e3744 | 113 | } |
114 | ||
d62a17ae | 115 | int ospf_if_drop_alldrouters(struct ospf *top, struct prefix *p, |
116 | ifindex_t ifindex) | |
718e3744 | 117 | { |
d62a17ae | 118 | int ret; |
119 | ||
120 | ret = setsockopt_ipv4_multicast(top->fd, IP_DROP_MEMBERSHIP, | |
121 | p->u.prefix4, htonl(OSPF_ALLDROUTERS), | |
122 | ifindex); | |
123 | if (ret < 0) | |
124 | zlog_warn( | |
125 | "can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, " | |
126 | "ifindex %u, AllDRouters): %s", | |
127 | top->fd, inet_ntoa(p->u.prefix4), ifindex, | |
128 | safe_strerror(errno)); | |
129 | else | |
130 | zlog_debug( | |
131 | "interface %s [%u] leave AllDRouters Multicast group.", | |
132 | inet_ntoa(p->u.prefix4), ifindex); | |
133 | ||
134 | return ret; | |
718e3744 | 135 | } |
136 | ||
d62a17ae | 137 | int ospf_if_ipmulticast(struct ospf *top, struct prefix *p, ifindex_t ifindex) |
718e3744 | 138 | { |
d62a17ae | 139 | u_char val; |
140 | int ret, len; | |
141 | ||
142 | /* Prevent receiving self-origined multicast packets. */ | |
143 | ret = setsockopt_ipv4_multicast_loop(top->fd, 0); | |
144 | if (ret < 0) | |
145 | zlog_warn("can't setsockopt IP_MULTICAST_LOOP(0) for fd %d: %s", | |
146 | top->fd, safe_strerror(errno)); | |
147 | ||
148 | /* Explicitly set multicast ttl to 1 -- endo. */ | |
149 | val = 1; | |
150 | len = sizeof(val); | |
151 | ret = setsockopt(top->fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&val, | |
152 | len); | |
153 | if (ret < 0) | |
154 | zlog_warn("can't setsockopt IP_MULTICAST_TTL(1) for fd %d: %s", | |
155 | top->fd, safe_strerror(errno)); | |
e1b18df1 CS |
156 | #ifndef GNU_LINUX |
157 | /* For GNU LINUX ospf_write uses IP_PKTINFO, in_pktinfo to send | |
158 | * packet out of ifindex. Below would be used Non Linux system. | |
159 | */ | |
160 | ret = setsockopt_ipv4_multicast_if(top->fd, p->u.prefix4, ifindex); | |
161 | if (ret < 0) | |
162 | zlog_warn( | |
163 | "can't setsockopt IP_MULTICAST_IF(fd %d, addr %s, " | |
164 | "ifindex %u): %s", | |
165 | top->fd, inet_ntoa(p->u.prefix4), ifindex, | |
166 | safe_strerror(errno)); | |
167 | #endif | |
d62a17ae | 168 | |
e7503eab CS |
169 | return ret; |
170 | } | |
171 | ||
172 | int ospf_bind_vrfdevice(struct ospf *ospf, int ospf_sock) | |
173 | { | |
174 | int ret = 0; | |
d62a17ae | 175 | |
e7503eab CS |
176 | #ifdef SO_BINDTODEVICE |
177 | ||
178 | if (ospf && ospf->vrf_id != VRF_DEFAULT && | |
179 | ospf->vrf_id != VRF_UNKNOWN) { | |
180 | ret = setsockopt(ospf_sock, SOL_SOCKET, SO_BINDTODEVICE, | |
181 | ospf->name, | |
182 | strlen(ospf->name)); | |
183 | if (ret < 0) { | |
184 | int save_errno = errno; | |
185 | ||
186 | zlog_warn("%s: Could not setsockopt SO_BINDTODEVICE %s", | |
187 | __PRETTY_FUNCTION__, | |
188 | safe_strerror(save_errno)); | |
e7503eab | 189 | } |
b1c3ae8c | 190 | |
e7503eab CS |
191 | } |
192 | #endif | |
d62a17ae | 193 | return ret; |
718e3744 | 194 | } |
195 | ||
e7503eab | 196 | int ospf_sock_init(struct ospf *ospf) |
718e3744 | 197 | { |
d62a17ae | 198 | int ospf_sock; |
199 | int ret, hincl = 1; | |
200 | int bufsize = (8 * 1024 * 1024); | |
201 | ||
e7503eab | 202 | if (ospfd_privs.change(ZPRIVS_RAISE)) { |
d62a17ae | 203 | zlog_err("ospf_sock_init: could not raise privs, %s", |
204 | safe_strerror(errno)); | |
e7503eab | 205 | } |
d62a17ae | 206 | |
207 | ospf_sock = socket(AF_INET, SOCK_RAW, IPPROTO_OSPFIGP); | |
208 | if (ospf_sock < 0) { | |
209 | int save_errno = errno; | |
e7503eab | 210 | |
d62a17ae | 211 | if (ospfd_privs.change(ZPRIVS_LOWER)) |
212 | zlog_err("ospf_sock_init: could not lower privs, %s", | |
213 | safe_strerror(errno)); | |
214 | zlog_err("ospf_read_sock_init: socket: %s", | |
215 | safe_strerror(save_errno)); | |
216 | exit(1); | |
217 | } | |
218 | ||
e7503eab | 219 | ret = ospf_bind_vrfdevice(ospf, ospf_sock); |
757fd711 DS |
220 | if (ret < 0) { |
221 | close(ospf_sock); | |
e7503eab | 222 | goto out; |
757fd711 | 223 | } |
e7503eab | 224 | |
5bd4189c | 225 | #ifdef IP_HDRINCL |
d62a17ae | 226 | /* we will include IP header with packet */ |
227 | ret = setsockopt(ospf_sock, IPPROTO_IP, IP_HDRINCL, &hincl, | |
228 | sizeof(hincl)); | |
229 | if (ret < 0) { | |
230 | int save_errno = errno; | |
e7503eab | 231 | |
d62a17ae | 232 | zlog_warn("Can't set IP_HDRINCL option for fd %d: %s", |
233 | ospf_sock, safe_strerror(save_errno)); | |
757fd711 | 234 | close(ospf_sock); |
e7503eab | 235 | goto out; |
d62a17ae | 236 | } |
237 | #elif defined(IPTOS_PREC_INTERNETCONTROL) | |
5bd4189c | 238 | #warning "IP_HDRINCL not available on this system" |
239 | #warning "using IPTOS_PREC_INTERNETCONTROL" | |
d62a17ae | 240 | ret = setsockopt_ipv4_tos(ospf_sock, IPTOS_PREC_INTERNETCONTROL); |
241 | if (ret < 0) { | |
242 | int save_errno = errno; | |
e7503eab | 243 | |
d62a17ae | 244 | zlog_warn("can't set sockopt IP_TOS %d to socket %d: %s", tos, |
245 | ospf_sock, safe_strerror(save_errno)); | |
246 | close(ospf_sock); /* Prevent sd leak. */ | |
e7503eab | 247 | goto out; |
d62a17ae | 248 | } |
5bd4189c | 249 | #else /* !IPTOS_PREC_INTERNETCONTROL */ |
250 | #warning "IP_HDRINCL not available, nor is IPTOS_PREC_INTERNETCONTROL" | |
d62a17ae | 251 | zlog_warn("IP_HDRINCL option not available"); |
5bd4189c | 252 | #endif /* IP_HDRINCL */ |
718e3744 | 253 | |
d62a17ae | 254 | ret = setsockopt_ifindex(AF_INET, ospf_sock, 1); |
ac191232 | 255 | |
d62a17ae | 256 | if (ret < 0) |
257 | zlog_warn("Can't set pktinfo option for fd %d", ospf_sock); | |
edd7c245 | 258 | |
e7503eab CS |
259 | setsockopt_so_sendbuf(ospf_sock, bufsize); |
260 | setsockopt_so_recvbuf(ospf_sock, bufsize); | |
261 | ||
262 | ospf->fd = ospf_sock; | |
263 | out: | |
d62a17ae | 264 | if (ospfd_privs.change(ZPRIVS_LOWER)) { |
265 | zlog_err("ospf_sock_init: could not lower privs, %s", | |
266 | safe_strerror(errno)); | |
267 | } | |
e7503eab | 268 | return ret; |
718e3744 | 269 | } |