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