2 * OSPF network related functions
3 * Copyright (C) 1999 Toshiaki Takada
5 * This file is part of GNU Zebra.
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
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.
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
28 #include "sockunion.h"
32 #include "lib_errors.h"
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"
44 /* Join to the OSPF ALL SPF ROUTERS multicast group. */
45 int ospf_if_add_allspfrouters(struct ospf
*top
, struct prefix
*p
,
50 ret
= setsockopt_ipv4_multicast(top
->fd
, IP_ADD_MEMBERSHIP
,
51 p
->u
.prefix4
, htonl(OSPF_ALLSPFROUTERS
),
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
));
62 if (IS_DEBUG_OSPF_EVENT
)
64 "interface %s [%u] join AllSPFRouters Multicast group.",
65 inet_ntoa(p
->u
.prefix4
), ifindex
);
71 int ospf_if_drop_allspfrouters(struct ospf
*top
, struct prefix
*p
,
76 ret
= setsockopt_ipv4_multicast(top
->fd
, IP_DROP_MEMBERSHIP
,
77 p
->u
.prefix4
, htonl(OSPF_ALLSPFROUTERS
),
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
));
86 if (IS_DEBUG_OSPF_EVENT
)
88 "interface %s [%u] leave AllSPFRouters Multicast group.",
89 inet_ntoa(p
->u
.prefix4
), ifindex
);
95 /* Join to the OSPF ALL Designated ROUTERS multicast group. */
96 int ospf_if_add_alldrouters(struct ospf
*top
, struct prefix
*p
,
101 ret
= setsockopt_ipv4_multicast(top
->fd
, IP_ADD_MEMBERSHIP
,
102 p
->u
.prefix4
, htonl(OSPF_ALLDROUTERS
),
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
));
114 "interface %s [%u] join AllDRouters Multicast group.",
115 inet_ntoa(p
->u
.prefix4
), ifindex
);
120 int ospf_if_drop_alldrouters(struct ospf
*top
, struct prefix
*p
,
125 ret
= setsockopt_ipv4_multicast(top
->fd
, IP_DROP_MEMBERSHIP
,
126 p
->u
.prefix4
, htonl(OSPF_ALLDROUTERS
),
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
));
136 "interface %s [%u] leave AllDRouters Multicast group.",
137 inet_ntoa(p
->u
.prefix4
), ifindex
);
142 int ospf_if_ipmulticast(struct ospf
*top
, struct prefix
*p
, ifindex_t ifindex
)
147 /* Prevent receiving self-origined multicast packets. */
148 ret
= setsockopt_ipv4_multicast_loop(top
->fd
, 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
));
154 /* Explicitly set multicast ttl to 1 -- endo. */
157 ret
= setsockopt(top
->fd
, IPPROTO_IP
, IP_MULTICAST_TTL
, (void *)&val
,
160 flog_err(EC_LIB_SOCKET
,
161 "can't setsockopt IP_MULTICAST_TTL(1) for fd %d: %s",
162 top
->fd
, safe_strerror(errno
));
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.
167 ret
= setsockopt_ipv4_multicast_if(top
->fd
, p
->u
.prefix4
, ifindex
);
169 flog_err(EC_LIB_SOCKET
,
170 "can't setsockopt IP_MULTICAST_IF(fd %d, addr %s, "
172 top
->fd
, inet_ntoa(p
->u
.prefix4
), ifindex
,
173 safe_strerror(errno
));
179 int ospf_sock_init(struct ospf
*ospf
)
183 int bufsize
= (8 * 1024 * 1024);
185 /* silently ignore. already done */
189 if (ospf
->vrf_id
== VRF_UNKNOWN
) {
190 /* silently return since VRF is not ready */
193 frr_with_privs(&ospfd_privs
) {
194 ospf_sock
= vrf_socket(AF_INET
, SOCK_RAW
, IPPROTO_OSPFIGP
,
195 ospf
->vrf_id
, ospf
->name
);
197 flog_err(EC_LIB_SOCKET
,
198 "ospf_read_sock_init: socket: %s",
199 safe_strerror(errno
));
204 /* we will include IP header with packet */
205 ret
= setsockopt(ospf_sock
, IPPROTO_IP
, IP_HDRINCL
, &hincl
,
208 flog_err(EC_LIB_SOCKET
,
209 "Can't set IP_HDRINCL option for fd %d: %s",
210 ospf_sock
, safe_strerror(errno
));
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
);
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. */
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 */
231 ret
= setsockopt_ifindex(AF_INET
, ospf_sock
, 1);
234 flog_err(EC_LIB_SOCKET
,
235 "Can't set pktinfo option for fd %d",
239 setsockopt_so_sendbuf(ospf_sock
, bufsize
);
240 setsockopt_so_recvbuf(ospf_sock
, bufsize
);
242 ospf
->fd
= ospf_sock
;