]>
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) | |
996c9314 LB |
61 | zlog_debug( |
62 | "interface %s [%u] join AllSPFRouters Multicast group.", | |
63 | inet_ntoa(p->u.prefix4), ifindex); | |
05ba78e4 | 64 | } |
d62a17ae | 65 | |
66 | return ret; | |
718e3744 | 67 | } |
68 | ||
d62a17ae | 69 | int ospf_if_drop_allspfrouters(struct ospf *top, struct prefix *p, |
70 | ifindex_t ifindex) | |
718e3744 | 71 | { |
d62a17ae | 72 | int ret; |
73 | ||
74 | ret = setsockopt_ipv4_multicast(top->fd, IP_DROP_MEMBERSHIP, | |
75 | p->u.prefix4, htonl(OSPF_ALLSPFROUTERS), | |
76 | ifindex); | |
77 | if (ret < 0) | |
78 | zlog_warn( | |
79 | "can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, " | |
80 | "ifindex %u, AllSPFRouters): %s", | |
81 | top->fd, inet_ntoa(p->u.prefix4), ifindex, | |
82 | safe_strerror(errno)); | |
05ba78e4 CS |
83 | else { |
84 | if (IS_DEBUG_OSPF_EVENT) | |
996c9314 LB |
85 | zlog_debug( |
86 | "interface %s [%u] leave AllSPFRouters Multicast group.", | |
87 | inet_ntoa(p->u.prefix4), ifindex); | |
05ba78e4 | 88 | } |
d62a17ae | 89 | |
90 | return ret; | |
718e3744 | 91 | } |
92 | ||
93 | /* Join to the OSPF ALL Designated ROUTERS multicast group. */ | |
d62a17ae | 94 | int ospf_if_add_alldrouters(struct ospf *top, struct prefix *p, |
95 | ifindex_t ifindex) | |
718e3744 | 96 | { |
d62a17ae | 97 | int ret; |
98 | ||
99 | ret = setsockopt_ipv4_multicast(top->fd, IP_ADD_MEMBERSHIP, | |
100 | p->u.prefix4, htonl(OSPF_ALLDROUTERS), | |
101 | ifindex); | |
102 | if (ret < 0) | |
103 | zlog_warn( | |
104 | "can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, " | |
105 | "ifindex %u, AllDRouters): %s; perhaps a kernel limit " | |
106 | "on # of multicast group memberships has been exceeded?", | |
107 | top->fd, inet_ntoa(p->u.prefix4), ifindex, | |
108 | safe_strerror(errno)); | |
109 | else | |
110 | zlog_debug( | |
111 | "interface %s [%u] join AllDRouters Multicast group.", | |
112 | inet_ntoa(p->u.prefix4), ifindex); | |
113 | ||
114 | return ret; | |
718e3744 | 115 | } |
116 | ||
d62a17ae | 117 | int ospf_if_drop_alldrouters(struct ospf *top, struct prefix *p, |
118 | ifindex_t ifindex) | |
718e3744 | 119 | { |
d62a17ae | 120 | int ret; |
121 | ||
122 | ret = setsockopt_ipv4_multicast(top->fd, IP_DROP_MEMBERSHIP, | |
123 | p->u.prefix4, htonl(OSPF_ALLDROUTERS), | |
124 | ifindex); | |
125 | if (ret < 0) | |
126 | zlog_warn( | |
127 | "can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, " | |
128 | "ifindex %u, AllDRouters): %s", | |
129 | top->fd, inet_ntoa(p->u.prefix4), ifindex, | |
130 | safe_strerror(errno)); | |
131 | else | |
132 | zlog_debug( | |
133 | "interface %s [%u] leave AllDRouters Multicast group.", | |
134 | inet_ntoa(p->u.prefix4), ifindex); | |
135 | ||
136 | return ret; | |
718e3744 | 137 | } |
138 | ||
d62a17ae | 139 | int ospf_if_ipmulticast(struct ospf *top, struct prefix *p, ifindex_t ifindex) |
718e3744 | 140 | { |
d62a17ae | 141 | u_char val; |
142 | int ret, len; | |
143 | ||
144 | /* Prevent receiving self-origined multicast packets. */ | |
145 | ret = setsockopt_ipv4_multicast_loop(top->fd, 0); | |
146 | if (ret < 0) | |
147 | zlog_warn("can't setsockopt IP_MULTICAST_LOOP(0) for fd %d: %s", | |
148 | top->fd, safe_strerror(errno)); | |
149 | ||
150 | /* Explicitly set multicast ttl to 1 -- endo. */ | |
151 | val = 1; | |
152 | len = sizeof(val); | |
153 | ret = setsockopt(top->fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&val, | |
154 | len); | |
155 | if (ret < 0) | |
156 | zlog_warn("can't setsockopt IP_MULTICAST_TTL(1) for fd %d: %s", | |
157 | top->fd, safe_strerror(errno)); | |
e1b18df1 CS |
158 | #ifndef GNU_LINUX |
159 | /* For GNU LINUX ospf_write uses IP_PKTINFO, in_pktinfo to send | |
160 | * packet out of ifindex. Below would be used Non Linux system. | |
161 | */ | |
162 | ret = setsockopt_ipv4_multicast_if(top->fd, p->u.prefix4, ifindex); | |
163 | if (ret < 0) | |
164 | zlog_warn( | |
165 | "can't setsockopt IP_MULTICAST_IF(fd %d, addr %s, " | |
166 | "ifindex %u): %s", | |
167 | top->fd, inet_ntoa(p->u.prefix4), ifindex, | |
168 | safe_strerror(errno)); | |
169 | #endif | |
d62a17ae | 170 | |
e7503eab CS |
171 | return ret; |
172 | } | |
173 | ||
e7503eab | 174 | int ospf_sock_init(struct ospf *ospf) |
718e3744 | 175 | { |
d62a17ae | 176 | int ospf_sock; |
177 | int ret, hincl = 1; | |
178 | int bufsize = (8 * 1024 * 1024); | |
179 | ||
3c0eb8fa PG |
180 | /* silently ignore. already done */ |
181 | if (ospf->fd > 0) | |
182 | return -1; | |
183 | ||
184 | if (ospf->vrf_id == VRF_UNKNOWN) { | |
185 | /* silently return since VRF is not ready */ | |
186 | return -1; | |
187 | } | |
e7503eab | 188 | if (ospfd_privs.change(ZPRIVS_RAISE)) { |
d62a17ae | 189 | zlog_err("ospf_sock_init: could not raise privs, %s", |
190 | safe_strerror(errno)); | |
e7503eab | 191 | } |
d62a17ae | 192 | |
996c9314 LB |
193 | ospf_sock = vrf_socket(AF_INET, SOCK_RAW, IPPROTO_OSPFIGP, ospf->vrf_id, |
194 | ospf->name); | |
d62a17ae | 195 | if (ospf_sock < 0) { |
196 | int save_errno = errno; | |
e7503eab | 197 | |
d62a17ae | 198 | if (ospfd_privs.change(ZPRIVS_LOWER)) |
199 | zlog_err("ospf_sock_init: could not lower privs, %s", | |
200 | safe_strerror(errno)); | |
201 | zlog_err("ospf_read_sock_init: socket: %s", | |
202 | safe_strerror(save_errno)); | |
203 | exit(1); | |
204 | } | |
205 | ||
5bd4189c | 206 | #ifdef IP_HDRINCL |
d62a17ae | 207 | /* we will include IP header with packet */ |
208 | ret = setsockopt(ospf_sock, IPPROTO_IP, IP_HDRINCL, &hincl, | |
209 | sizeof(hincl)); | |
210 | if (ret < 0) { | |
211 | int save_errno = errno; | |
e7503eab | 212 | |
d62a17ae | 213 | zlog_warn("Can't set IP_HDRINCL option for fd %d: %s", |
214 | ospf_sock, safe_strerror(save_errno)); | |
757fd711 | 215 | close(ospf_sock); |
e7503eab | 216 | goto out; |
d62a17ae | 217 | } |
218 | #elif defined(IPTOS_PREC_INTERNETCONTROL) | |
5bd4189c | 219 | #warning "IP_HDRINCL not available on this system" |
220 | #warning "using IPTOS_PREC_INTERNETCONTROL" | |
d62a17ae | 221 | ret = setsockopt_ipv4_tos(ospf_sock, IPTOS_PREC_INTERNETCONTROL); |
222 | if (ret < 0) { | |
223 | int save_errno = errno; | |
e7503eab | 224 | |
d62a17ae | 225 | zlog_warn("can't set sockopt IP_TOS %d to socket %d: %s", tos, |
226 | ospf_sock, safe_strerror(save_errno)); | |
227 | close(ospf_sock); /* Prevent sd leak. */ | |
e7503eab | 228 | goto out; |
d62a17ae | 229 | } |
5bd4189c | 230 | #else /* !IPTOS_PREC_INTERNETCONTROL */ |
231 | #warning "IP_HDRINCL not available, nor is IPTOS_PREC_INTERNETCONTROL" | |
d62a17ae | 232 | zlog_warn("IP_HDRINCL option not available"); |
5bd4189c | 233 | #endif /* IP_HDRINCL */ |
718e3744 | 234 | |
d62a17ae | 235 | ret = setsockopt_ifindex(AF_INET, ospf_sock, 1); |
ac191232 | 236 | |
d62a17ae | 237 | if (ret < 0) |
238 | zlog_warn("Can't set pktinfo option for fd %d", ospf_sock); | |
edd7c245 | 239 | |
e7503eab CS |
240 | setsockopt_so_sendbuf(ospf_sock, bufsize); |
241 | setsockopt_so_recvbuf(ospf_sock, bufsize); | |
242 | ||
243 | ospf->fd = ospf_sock; | |
244 | out: | |
d62a17ae | 245 | if (ospfd_privs.change(ZPRIVS_LOWER)) { |
246 | zlog_err("ospf_sock_init: could not lower privs, %s", | |
247 | safe_strerror(errno)); | |
248 | } | |
e7503eab | 249 | return ret; |
718e3744 | 250 | } |