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