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
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
29 #include "sockunion.h"
34 extern struct zebra_privs_t ospfd_privs
;
36 #include "ospfd/ospfd.h"
37 #include "ospfd/ospf_network.h"
38 #include "ospfd/ospf_interface.h"
39 #include "ospfd/ospf_asbr.h"
40 #include "ospfd/ospf_lsa.h"
41 #include "ospfd/ospf_lsdb.h"
42 #include "ospfd/ospf_neighbor.h"
43 #include "ospfd/ospf_packet.h"
47 /* Join to the OSPF ALL SPF ROUTERS multicast group. */
49 ospf_if_add_allspfrouters (struct ospf
*top
, struct prefix
*p
,
54 ret
= setsockopt_multicast_ipv4 (top
->fd
, IP_ADD_MEMBERSHIP
,
55 p
->u
.prefix4
, htonl (OSPF_ALLSPFROUTERS
),
58 zlog_warn ("can't setsockopt IP_ADD_MEMBERSHIP (AllSPFRouters): %s",
61 zlog_info ("interface %s join AllSPFRouters Multicast group.",
62 inet_ntoa (p
->u
.prefix4
));
68 ospf_if_drop_allspfrouters (struct ospf
*top
, struct prefix
*p
,
73 ret
= setsockopt_multicast_ipv4 (top
->fd
, IP_DROP_MEMBERSHIP
,
74 p
->u
.prefix4
, htonl (OSPF_ALLSPFROUTERS
),
77 zlog_warn("can't setsockopt IP_DROP_MEMBERSHIP (AllSPFRouters): %s",
80 zlog_info ("interface %s leave AllSPFRouters Multicast group.",
81 inet_ntoa (p
->u
.prefix4
));
86 /* Join to the OSPF ALL Designated ROUTERS multicast group. */
88 ospf_if_add_alldrouters (struct ospf
*top
, struct prefix
*p
, unsigned int
93 ret
= setsockopt_multicast_ipv4 (top
->fd
, IP_ADD_MEMBERSHIP
,
94 p
->u
.prefix4
, htonl (OSPF_ALLDROUTERS
),
97 zlog_warn ("can't setsockopt IP_ADD_MEMBERSHIP (AllDRouters): %s",
100 zlog_info ("interface %s join AllDRouters Multicast group.",
101 inet_ntoa (p
->u
.prefix4
));
107 ospf_if_drop_alldrouters (struct ospf
*top
, struct prefix
*p
, unsigned int
112 ret
= setsockopt_multicast_ipv4 (top
->fd
, IP_DROP_MEMBERSHIP
,
113 p
->u
.prefix4
, htonl (OSPF_ALLDROUTERS
),
116 zlog_warn ("can't setsockopt IP_DROP_MEMBERSHIP (AllDRouters): %s",
119 zlog_info ("interface %s leave AllDRouters Multicast group.",
120 inet_ntoa (p
->u
.prefix4
));
126 ospf_if_ipmulticast (struct ospf
*top
, struct prefix
*p
, unsigned int ifindex
)
134 /* Prevent receiving self-origined multicast packets. */
135 ret
= setsockopt (top
->fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
, (void *)&val
, len
);
137 zlog_warn ("can't setsockopt IP_MULTICAST_LOOP(0): %s", strerror (errno
));
139 /* Explicitly set multicast ttl to 1 -- endo. */
141 ret
= setsockopt (top
->fd
, IPPROTO_IP
, IP_MULTICAST_TTL
, (void *)&val
, len
);
143 zlog_warn ("can't setsockopt IP_MULTICAST_TTL(1): %s", strerror (errno
));
145 ret
= setsockopt_multicast_ipv4 (top
->fd
, IP_MULTICAST_IF
,
146 p
->u
.prefix4
, 0, ifindex
);
148 zlog_warn ("can't setsockopt IP_MULTICAST_IF: %s", strerror (errno
));
154 ospf_sock_init (void)
157 int ret
, tos
, hincl
= 1;
159 if ( ospfd_privs
.change (ZPRIVS_RAISE
) )
160 zlog_err ("ospf_sock_init: could not raise privs, %s",
163 ospf_sock
= socket (AF_INET
, SOCK_RAW
, IPPROTO_OSPFIGP
);
166 if ( ospfd_privs
.change (ZPRIVS_LOWER
) )
167 zlog_err ("ospf_sock_init: could not lower privs, %s",
169 zlog_warn ("ospf_read_sock_init: socket: %s", strerror (errno
));
174 /* Set precedence field. */
175 #ifdef IPTOS_PREC_INTERNETCONTROL
176 tos
= IPTOS_PREC_INTERNETCONTROL
;
177 ret
= setsockopt (ospf_sock
, IPPROTO_IP
, IP_TOS
,
178 (char *) &tos
, sizeof (int));
181 if ( ospfd_privs
.change (ZPRIVS_LOWER
) )
182 zlog_err ("ospf_sock_init: could not lower privs, %s",
184 zlog_warn ("can't set sockopt IP_TOS %d to socket %d", tos
, ospf_sock
);
185 close (ospf_sock
); /* Prevent sd leak. */
188 #endif /* IPTOS_PREC_INTERNETCONTROL */
190 /* we will include IP header with packet */
191 ret
= setsockopt (ospf_sock
, IPPROTO_IP
, IP_HDRINCL
, &hincl
, sizeof (hincl
));
194 if ( ospfd_privs
.change (ZPRIVS_LOWER
) )
195 zlog_err ("ospf_sock_init: could not lower privs, %s",
197 zlog_warn ("Can't set IP_HDRINCL option");
200 #if defined (IP_PKTINFO)
201 ret
= setsockopt (ospf_sock
, IPPROTO_IP
, IP_PKTINFO
, &hincl
, sizeof (hincl
));
204 if ( ospfd_privs
.change (ZPRIVS_LOWER
) )
205 zlog_err ("ospf_sock_init: could not lower privs, %s",
207 zlog_warn ("Can't set IP_PKTINFO option");
209 #elif defined (IP_RECVIF)
210 ret
= setsockopt (ospf_sock
, IPPROTO_IP
, IP_RECVIF
, &hincl
, sizeof (hincl
));
213 if ( ospfd_privs
.change (ZPRIVS_LOWER
) )
214 zlog_err ("ospf_sock_init: could not lower privs, %s",
216 zlog_warn ("Can't set IP_RECVIF option");
219 #warning "cannot be able to receive link information on this OS"
222 if (ospfd_privs
.change (ZPRIVS_LOWER
))
224 zlog_err ("ospf_sock_init: could not lower privs, %s",