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"
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"
41 #include "ospfd/ospf_dump.h"
43 /* Join to the OSPF ALL SPF ROUTERS multicast group. */
44 int ospf_if_add_allspfrouters(struct ospf
*top
, struct prefix
*p
,
49 ret
= setsockopt_ipv4_multicast(top
->fd
, IP_ADD_MEMBERSHIP
,
50 p
->u
.prefix4
, htonl(OSPF_ALLSPFROUTERS
),
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
));
60 if (IS_DEBUG_OSPF_EVENT
)
61 zlog_debug("interface %s [%u] join AllSPFRouters Multicast group.",
62 inet_ntoa(p
->u
.prefix4
), ifindex
);
68 int ospf_if_drop_allspfrouters(struct ospf
*top
, struct prefix
*p
,
73 ret
= setsockopt_ipv4_multicast(top
->fd
, IP_DROP_MEMBERSHIP
,
74 p
->u
.prefix4
, htonl(OSPF_ALLSPFROUTERS
),
78 "can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, "
79 "ifindex %u, AllSPFRouters): %s",
80 top
->fd
, inet_ntoa(p
->u
.prefix4
), ifindex
,
81 safe_strerror(errno
));
83 if (IS_DEBUG_OSPF_EVENT
)
84 zlog_debug("interface %s [%u] leave AllSPFRouters Multicast group.",
85 inet_ntoa(p
->u
.prefix4
), ifindex
);
91 /* Join to the OSPF ALL Designated ROUTERS multicast group. */
92 int ospf_if_add_alldrouters(struct ospf
*top
, struct prefix
*p
,
97 ret
= setsockopt_ipv4_multicast(top
->fd
, IP_ADD_MEMBERSHIP
,
98 p
->u
.prefix4
, htonl(OSPF_ALLDROUTERS
),
102 "can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, "
103 "ifindex %u, AllDRouters): %s; perhaps a kernel limit "
104 "on # of multicast group memberships has been exceeded?",
105 top
->fd
, inet_ntoa(p
->u
.prefix4
), ifindex
,
106 safe_strerror(errno
));
109 "interface %s [%u] join AllDRouters Multicast group.",
110 inet_ntoa(p
->u
.prefix4
), ifindex
);
115 int ospf_if_drop_alldrouters(struct ospf
*top
, struct prefix
*p
,
120 ret
= setsockopt_ipv4_multicast(top
->fd
, IP_DROP_MEMBERSHIP
,
121 p
->u
.prefix4
, htonl(OSPF_ALLDROUTERS
),
125 "can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, "
126 "ifindex %u, AllDRouters): %s",
127 top
->fd
, inet_ntoa(p
->u
.prefix4
), ifindex
,
128 safe_strerror(errno
));
131 "interface %s [%u] leave AllDRouters Multicast group.",
132 inet_ntoa(p
->u
.prefix4
), ifindex
);
137 int ospf_if_ipmulticast(struct ospf
*top
, struct prefix
*p
, ifindex_t ifindex
)
142 /* Prevent receiving self-origined multicast packets. */
143 ret
= setsockopt_ipv4_multicast_loop(top
->fd
, 0);
145 zlog_warn("can't setsockopt IP_MULTICAST_LOOP(0) for fd %d: %s",
146 top
->fd
, safe_strerror(errno
));
148 /* Explicitly set multicast ttl to 1 -- endo. */
151 ret
= setsockopt(top
->fd
, IPPROTO_IP
, IP_MULTICAST_TTL
, (void *)&val
,
154 zlog_warn("can't setsockopt IP_MULTICAST_TTL(1) for fd %d: %s",
155 top
->fd
, safe_strerror(errno
));
157 /* For GNU LINUX ospf_write uses IP_PKTINFO, in_pktinfo to send
158 * packet out of ifindex. Below would be used Non Linux system.
160 ret
= setsockopt_ipv4_multicast_if(top
->fd
, p
->u
.prefix4
, ifindex
);
163 "can't setsockopt IP_MULTICAST_IF(fd %d, addr %s, "
165 top
->fd
, inet_ntoa(p
->u
.prefix4
), ifindex
,
166 safe_strerror(errno
));
172 int ospf_sock_init(struct ospf
*ospf
)
176 int bufsize
= (8 * 1024 * 1024);
178 /* silently ignore. already done */
182 if (ospf
->vrf_id
== VRF_UNKNOWN
) {
183 /* silently return since VRF is not ready */
186 if (ospfd_privs
.change(ZPRIVS_RAISE
)) {
187 zlog_err("ospf_sock_init: could not raise privs, %s",
188 safe_strerror(errno
));
191 ospf_sock
= vrf_socket(AF_INET
, SOCK_RAW
, IPPROTO_OSPFIGP
,
192 ospf
->vrf_id
, ospf
->name
);
194 int save_errno
= errno
;
196 if (ospfd_privs
.change(ZPRIVS_LOWER
))
197 zlog_err("ospf_sock_init: could not lower privs, %s",
198 safe_strerror(errno
));
199 zlog_err("ospf_read_sock_init: socket: %s",
200 safe_strerror(save_errno
));
205 /* we will include IP header with packet */
206 ret
= setsockopt(ospf_sock
, IPPROTO_IP
, IP_HDRINCL
, &hincl
,
209 int save_errno
= errno
;
211 zlog_warn("Can't set IP_HDRINCL option for fd %d: %s",
212 ospf_sock
, safe_strerror(save_errno
));
216 #elif defined(IPTOS_PREC_INTERNETCONTROL)
217 #warning "IP_HDRINCL not available on this system"
218 #warning "using IPTOS_PREC_INTERNETCONTROL"
219 ret
= setsockopt_ipv4_tos(ospf_sock
, IPTOS_PREC_INTERNETCONTROL
);
221 int save_errno
= errno
;
223 zlog_warn("can't set sockopt IP_TOS %d to socket %d: %s", tos
,
224 ospf_sock
, safe_strerror(save_errno
));
225 close(ospf_sock
); /* Prevent sd leak. */
228 #else /* !IPTOS_PREC_INTERNETCONTROL */
229 #warning "IP_HDRINCL not available, nor is IPTOS_PREC_INTERNETCONTROL"
230 zlog_warn("IP_HDRINCL option not available");
231 #endif /* IP_HDRINCL */
233 ret
= setsockopt_ifindex(AF_INET
, ospf_sock
, 1);
236 zlog_warn("Can't set pktinfo option for fd %d", ospf_sock
);
238 setsockopt_so_sendbuf(ospf_sock
, bufsize
);
239 setsockopt_so_recvbuf(ospf_sock
, bufsize
);
241 ospf
->fd
= ospf_sock
;
243 if (ospfd_privs
.change(ZPRIVS_LOWER
)) {
244 zlog_err("ospf_sock_init: could not lower privs, %s",
245 safe_strerror(errno
));