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