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
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
20 * 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
25#include "thread.h"
26#include "linklist.h"
27#include "prefix.h"
28#include "if.h"
29#include "sockunion.h"
30#include "log.h"
31#include "sockopt.h"
32
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
42/* Join to the OSPF ALL SPF ROUTERS multicast group. */
43int
44ospf_if_add_allspfrouters (struct ospf *top, struct prefix *p,
45			   unsigned int ifindex)
46{
47  int ret;
48
49  ret = setsockopt_multicast_ipv4 (top->fd, IP_ADD_MEMBERSHIP,
50                                   p->u.prefix4, htonl (OSPF_ALLSPFROUTERS),
51                                   ifindex);
52  if (ret < 0)
53    zlog_warn ("can't setsockopt IP_ADD_MEMBERSHIP (AllSPFRouters): %s",
54               strerror (errno));
55  else
56    zlog_info ("interface %s join AllSPFRouters Multicast group.",
57	       inet_ntoa (p->u.prefix4));
58
59  return ret;
60}
61
62int
63ospf_if_drop_allspfrouters (struct ospf *top, struct prefix *p,
64			    unsigned int ifindex)
65{
66  int ret;
67
68  ret = setsockopt_multicast_ipv4 (top->fd, IP_DROP_MEMBERSHIP,
69                                   p->u.prefix4, htonl (OSPF_ALLSPFROUTERS),
70                                   ifindex);
71  if (ret < 0)
72    zlog_warn("can't setsockopt IP_DROP_MEMBERSHIP (AllSPFRouters): %s",
73	      strerror (errno));
74  else
75    zlog_info ("interface %s leave AllSPFRouters Multicast group.",
76	       inet_ntoa (p->u.prefix4));
77
78  return ret;
79}
80
81/* Join to the OSPF ALL Designated ROUTERS multicast group. */
82int
83ospf_if_add_alldrouters (struct ospf *top, struct prefix *p, unsigned int
84			 ifindex)
85{
86  int ret;
87
88  ret = setsockopt_multicast_ipv4 (top->fd, IP_ADD_MEMBERSHIP,
89                                   p->u.prefix4, htonl (OSPF_ALLDROUTERS),
90                                   ifindex);
91  if (ret < 0)
92    zlog_warn ("can't setsockopt IP_ADD_MEMBERSHIP (AllDRouters): %s",
93               strerror (errno));
94  else
95    zlog_info ("interface %s join AllDRouters Multicast group.",
96	       inet_ntoa (p->u.prefix4));
97
98  return ret;
99}
100
101int
102ospf_if_drop_alldrouters (struct ospf *top, struct prefix *p, unsigned int
103			  ifindex)
104{
105  int ret;
106
107  ret = setsockopt_multicast_ipv4 (top->fd, IP_DROP_MEMBERSHIP,
108                                   p->u.prefix4, htonl (OSPF_ALLDROUTERS),
109                                   ifindex);
110  if (ret < 0)
111    zlog_warn ("can't setsockopt IP_DROP_MEMBERSHIP (AllDRouters): %s",
112	       strerror (errno));
113  else
114    zlog_info ("interface %s leave AllDRouters Multicast group.",
115	       inet_ntoa (p->u.prefix4));
116
117  return ret;
118}
119
120int
121ospf_if_ipmulticast (struct ospf *top, struct prefix *p, unsigned int ifindex)
122{
123  u_char val;
124  int ret, len;
125
126  val = 0;
127  len = sizeof (val);
128
129  /* Prevent receiving self-origined multicast packets. */
130  ret = setsockopt (top->fd, IPPROTO_IP, IP_MULTICAST_LOOP, (void *)&val, len);
131  if (ret < 0)
132    zlog_warn ("can't setsockopt IP_MULTICAST_LOOP(0): %s", strerror (errno));
133
134  /* Explicitly set multicast ttl to 1 -- endo. */
135  val = 1;
136  ret = setsockopt (top->fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&val, len);
137  if (ret < 0)
138    zlog_warn ("can't setsockopt IP_MULTICAST_TTL(1): %s", strerror (errno));
139
140  ret = setsockopt_multicast_ipv4 (top->fd, IP_MULTICAST_IF,
141                                   p->u.prefix4, 0, ifindex);
142  if (ret < 0)
143    zlog_warn ("can't setsockopt IP_MULTICAST_IF: %s", strerror (errno));
144
145  return ret;
146}
147
148int
149ospf_sock_init (void)
150{
151  int ospf_sock;
152  int ret, tos, hincl = 1;
153
154  ospf_sock = socket (AF_INET, SOCK_RAW, IPPROTO_OSPFIGP);
155  if (ospf_sock < 0)
156    {
157      zlog_warn ("ospf_read_sock_init: socket: %s", strerror (errno));
158      return -1;
159    }
160
161  /* Set precedence field. */
162#ifdef IPTOS_PREC_INTERNETCONTROL
163  tos = IPTOS_PREC_INTERNETCONTROL;
164  ret = setsockopt (ospf_sock, IPPROTO_IP, IP_TOS,
165		    (char *) &tos, sizeof (int));
166  if (ret < 0)
167    {
168      zlog_warn ("can't set sockopt IP_TOS %d to socket %d", tos, ospf_sock);
169      close (ospf_sock);	/* Prevent sd leak. */
170      return ret;
171    }
172#endif /* IPTOS_PREC_INTERNETCONTROL */
173
174  /* we will include IP header with packet */
175  ret = setsockopt (ospf_sock, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof (hincl));
176  if (ret < 0)
177    zlog_warn ("Can't set IP_HDRINCL option");
178
179#if defined (IP_PKTINFO)
180  ret = setsockopt (ospf_sock, IPPROTO_IP, IP_PKTINFO, &hincl, sizeof (hincl));
181   if (ret < 0)
182    zlog_warn ("Can't set IP_PKTINFO option");
183#elif defined (IP_RECVIF)
184  ret = setsockopt (ospf_sock, IPPROTO_IP, IP_RECVIF, &hincl, sizeof (hincl));
185   if (ret < 0)
186    zlog_warn ("Can't set IP_RECVIF option");
187#else
188#warning "cannot be able to receive link information on this OS"
189#endif
190
191  return ospf_sock;
192}
193