blob: 02ddf92f07836a6340e2eae8180ea05ab078cce8 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
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"
pauledd7c242003-06-04 13:59:38 +000032#include "privs.h"
33
34extern struct zebra_privs_t ospfd_privs;
paul718e3742002-12-13 20:15:29 +000035
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"
Denis Ovsienkob7fe4142007-08-21 16:32:56 +000044#include "ospfd/ospf_dump.h"
paul718e3742002-12-13 20:15:29 +000045
pauledd7c242003-06-04 13:59:38 +000046
47
paul718e3742002-12-13 20:15:29 +000048/* Join to the OSPF ALL SPF ROUTERS multicast group. */
49int
50ospf_if_add_allspfrouters (struct ospf *top, struct prefix *p,
Paul Jakma9099f9b2016-01-18 10:12:10 +000051 ifindex_t ifindex)
paul718e3742002-12-13 20:15:29 +000052{
53 int ret;
54
Dmitrij Tejblum69bf3a32011-08-18 20:22:17 +040055 ret = setsockopt_ipv4_multicast (top->fd, IP_ADD_MEMBERSHIP,
56 htonl (OSPF_ALLSPFROUTERS),
paul718e3742002-12-13 20:15:29 +000057 ifindex);
58 if (ret < 0)
ajs3dc56b52004-12-30 15:11:19 +000059 zlog_warn ("can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, "
60 "ifindex %u, AllSPFRouters): %s; perhaps a kernel limit "
61 "on # of multicast group memberships has been exceeded?",
62 top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +000063 else
Paul Jakmaec704972010-12-06 12:21:52 +000064 zlog_debug ("interface %s [%u] join AllSPFRouters Multicast group.",
paul42c98192005-05-07 02:22:51 +000065 inet_ntoa (p->u.prefix4), ifindex);
paul718e3742002-12-13 20:15:29 +000066
67 return ret;
68}
69
70int
71ospf_if_drop_allspfrouters (struct ospf *top, struct prefix *p,
Paul Jakma9099f9b2016-01-18 10:12:10 +000072 ifindex_t ifindex)
paul718e3742002-12-13 20:15:29 +000073{
74 int ret;
75
Dmitrij Tejblum69bf3a32011-08-18 20:22:17 +040076 ret = setsockopt_ipv4_multicast (top->fd, IP_DROP_MEMBERSHIP,
77 htonl (OSPF_ALLSPFROUTERS),
paul718e3742002-12-13 20:15:29 +000078 ifindex);
79 if (ret < 0)
ajs3dc56b52004-12-30 15:11:19 +000080 zlog_warn ("can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, "
81 "ifindex %u, AllSPFRouters): %s",
82 top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +000083 else
Paul Jakmaec704972010-12-06 12:21:52 +000084 zlog_debug ("interface %s [%u] leave AllSPFRouters Multicast group.",
85 inet_ntoa (p->u.prefix4), ifindex);
paul718e3742002-12-13 20:15:29 +000086
87 return ret;
88}
89
90/* Join to the OSPF ALL Designated ROUTERS multicast group. */
91int
Paul Jakma9099f9b2016-01-18 10:12:10 +000092ospf_if_add_alldrouters (struct ospf *top, struct prefix *p, ifindex_t ifindex)
paul718e3742002-12-13 20:15:29 +000093{
94 int ret;
95
Dmitrij Tejblum69bf3a32011-08-18 20:22:17 +040096 ret = setsockopt_ipv4_multicast (top->fd, IP_ADD_MEMBERSHIP,
97 htonl (OSPF_ALLDROUTERS),
paul718e3742002-12-13 20:15:29 +000098 ifindex);
99 if (ret < 0)
ajs3dc56b52004-12-30 15:11:19 +0000100 zlog_warn ("can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, "
101 "ifindex %u, AllDRouters): %s; perhaps a kernel limit "
102 "on # of multicast group memberships has been exceeded?",
103 top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +0000104 else
Paul Jakmaec704972010-12-06 12:21:52 +0000105 zlog_debug ("interface %s [%u] join AllDRouters Multicast group.",
106 inet_ntoa (p->u.prefix4), ifindex);
paul718e3742002-12-13 20:15:29 +0000107
108 return ret;
109}
110
111int
Paul Jakma9099f9b2016-01-18 10:12:10 +0000112ospf_if_drop_alldrouters (struct ospf *top, struct prefix *p, ifindex_t ifindex)
paul718e3742002-12-13 20:15:29 +0000113{
114 int ret;
115
Dmitrij Tejblum69bf3a32011-08-18 20:22:17 +0400116 ret = setsockopt_ipv4_multicast (top->fd, IP_DROP_MEMBERSHIP,
117 htonl (OSPF_ALLDROUTERS),
paul718e3742002-12-13 20:15:29 +0000118 ifindex);
119 if (ret < 0)
ajs3dc56b52004-12-30 15:11:19 +0000120 zlog_warn ("can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, "
121 "ifindex %u, AllDRouters): %s",
122 top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +0000123 else
Paul Jakmaec704972010-12-06 12:21:52 +0000124 zlog_debug ("interface %s [%u] leave AllDRouters Multicast group.",
125 inet_ntoa (p->u.prefix4), ifindex);
paul718e3742002-12-13 20:15:29 +0000126
127 return ret;
128}
129
130int
Paul Jakma9099f9b2016-01-18 10:12:10 +0000131ospf_if_ipmulticast (struct ospf *top, struct prefix *p, ifindex_t ifindex)
paul718e3742002-12-13 20:15:29 +0000132{
133 u_char val;
134 int ret, len;
135
136 val = 0;
137 len = sizeof (val);
138
139 /* Prevent receiving self-origined multicast packets. */
140 ret = setsockopt (top->fd, IPPROTO_IP, IP_MULTICAST_LOOP, (void *)&val, len);
141 if (ret < 0)
ajs3dc56b52004-12-30 15:11:19 +0000142 zlog_warn ("can't setsockopt IP_MULTICAST_LOOP(0) for fd %d: %s",
143 top->fd, safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +0000144
145 /* Explicitly set multicast ttl to 1 -- endo. */
146 val = 1;
147 ret = setsockopt (top->fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&val, len);
148 if (ret < 0)
ajs3dc56b52004-12-30 15:11:19 +0000149 zlog_warn ("can't setsockopt IP_MULTICAST_TTL(1) for fd %d: %s",
150 top->fd, safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000151
Dmitrij Tejblum69bf3a32011-08-18 20:22:17 +0400152 ret = setsockopt_ipv4_multicast_if (top->fd, ifindex);
paul718e3742002-12-13 20:15:29 +0000153 if (ret < 0)
ajs3dc56b52004-12-30 15:11:19 +0000154 zlog_warn("can't setsockopt IP_MULTICAST_IF(fd %d, addr %s, "
155 "ifindex %u): %s",
156 top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +0000157
158 return ret;
159}
160
161int
162ospf_sock_init (void)
163{
164 int ospf_sock;
Stephen Hemminger1423c802008-08-14 17:59:25 +0100165 int ret, hincl = 1;
paul718e3742002-12-13 20:15:29 +0000166
pauledd7c242003-06-04 13:59:38 +0000167 if ( ospfd_privs.change (ZPRIVS_RAISE) )
168 zlog_err ("ospf_sock_init: could not raise privs, %s",
ajs6099b3b2004-11-20 02:06:59 +0000169 safe_strerror (errno) );
pauledd7c242003-06-04 13:59:38 +0000170
paul718e3742002-12-13 20:15:29 +0000171 ospf_sock = socket (AF_INET, SOCK_RAW, IPPROTO_OSPFIGP);
172 if (ospf_sock < 0)
173 {
ajs0b7d97d2005-01-30 17:24:02 +0000174 int save_errno = errno;
pauledd7c242003-06-04 13:59:38 +0000175 if ( ospfd_privs.change (ZPRIVS_LOWER) )
176 zlog_err ("ospf_sock_init: could not lower privs, %s",
ajs6099b3b2004-11-20 02:06:59 +0000177 safe_strerror (errno) );
ajs0b7d97d2005-01-30 17:24:02 +0000178 zlog_err ("ospf_read_sock_init: socket: %s", safe_strerror (save_errno));
179 exit(1);
paul718e3742002-12-13 20:15:29 +0000180 }
pauledd7c242003-06-04 13:59:38 +0000181
paul5bd41892004-05-05 17:29:24 +0000182#ifdef IP_HDRINCL
183 /* we will include IP header with packet */
184 ret = setsockopt (ospf_sock, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof (hincl));
185 if (ret < 0)
186 {
ajs0b7d97d2005-01-30 17:24:02 +0000187 int save_errno = errno;
paul5bd41892004-05-05 17:29:24 +0000188 if ( ospfd_privs.change (ZPRIVS_LOWER) )
189 zlog_err ("ospf_sock_init: could not lower privs, %s",
ajs6099b3b2004-11-20 02:06:59 +0000190 safe_strerror (errno) );
ajs0b7d97d2005-01-30 17:24:02 +0000191 zlog_warn ("Can't set IP_HDRINCL option for fd %d: %s",
192 ospf_sock, safe_strerror(save_errno));
paul5bd41892004-05-05 17:29:24 +0000193 }
194#elif defined (IPTOS_PREC_INTERNETCONTROL)
195#warning "IP_HDRINCL not available on this system"
196#warning "using IPTOS_PREC_INTERNETCONTROL"
Stephen Hemminger1423c802008-08-14 17:59:25 +0100197 ret = setsockopt_ipv4_tos(ospf_sock, IPTOS_PREC_INTERNETCONTROL);
paul718e3742002-12-13 20:15:29 +0000198 if (ret < 0)
199 {
ajs0b7d97d2005-01-30 17:24:02 +0000200 int save_errno = errno;
pauledd7c242003-06-04 13:59:38 +0000201 if ( ospfd_privs.change (ZPRIVS_LOWER) )
202 zlog_err ("ospf_sock_init: could not lower privs, %s",
ajs6099b3b2004-11-20 02:06:59 +0000203 safe_strerror (errno) );
ajs0b7d97d2005-01-30 17:24:02 +0000204 zlog_warn ("can't set sockopt IP_TOS %d to socket %d: %s",
205 tos, ospf_sock, safe_strerror(save_errno));
paul718e3742002-12-13 20:15:29 +0000206 close (ospf_sock); /* Prevent sd leak. */
207 return ret;
208 }
paul5bd41892004-05-05 17:29:24 +0000209#else /* !IPTOS_PREC_INTERNETCONTROL */
210#warning "IP_HDRINCL not available, nor is IPTOS_PREC_INTERNETCONTROL"
211 zlog_warn ("IP_HDRINCL option not available");
212#endif /* IP_HDRINCL */
paul718e3742002-12-13 20:15:29 +0000213
paulac191232004-10-22 12:05:17 +0000214 ret = setsockopt_ifindex (AF_INET, ospf_sock, 1);
215
paul2dd8bb42004-07-23 15:13:48 +0000216 if (ret < 0)
ajs3dc56b52004-12-30 15:11:19 +0000217 zlog_warn ("Can't set pktinfo option for fd %d", ospf_sock);
pauledd7c242003-06-04 13:59:38 +0000218
219 if (ospfd_privs.change (ZPRIVS_LOWER))
220 {
221 zlog_err ("ospf_sock_init: could not lower privs, %s",
ajs6099b3b2004-11-20 02:06:59 +0000222 safe_strerror (errno) );
pauledd7c242003-06-04 13:59:38 +0000223 }
paul718e3742002-12-13 20:15:29 +0000224
225 return ospf_sock;
226}
Denis Ovsienkob7fe4142007-08-21 16:32:56 +0000227
228void
Andrew Certain0798cee2012-12-04 13:43:42 -0800229ospf_adjust_sndbuflen (struct ospf * ospf, unsigned int buflen)
Denis Ovsienkob7fe4142007-08-21 16:32:56 +0000230{
231 int ret, newbuflen;
232 /* Check if any work has to be done at all. */
233 if (ospf->maxsndbuflen >= buflen)
234 return;
235 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
236 zlog_debug ("%s: adjusting OSPF send buffer size to %d",
237 __func__, buflen);
238 if (ospfd_privs.change (ZPRIVS_RAISE))
239 zlog_err ("%s: could not raise privs, %s", __func__,
240 safe_strerror (errno));
241 /* Now we try to set SO_SNDBUF to what our caller has requested
Denis Ovsienkof102e752007-09-18 09:01:13 +0000242 * (the MTU of a newly added interface). However, if the OS has
243 * truncated the actual buffer size to somewhat less size, try
244 * to detect it and update our records appropriately. The OS
245 * may allocate more buffer space, than requested, this isn't
246 * a error.
Denis Ovsienkob7fe4142007-08-21 16:32:56 +0000247 */
248 ret = setsockopt_so_sendbuf (ospf->fd, buflen);
249 newbuflen = getsockopt_so_sendbuf (ospf->fd);
Andrew Certain0798cee2012-12-04 13:43:42 -0800250 if (ret < 0 || newbuflen < 0 || newbuflen < (int) buflen)
251 zlog_warn ("%s: tried to set SO_SNDBUF to %u, but got %d",
Denis Ovsienkob7fe4142007-08-21 16:32:56 +0000252 __func__, buflen, newbuflen);
253 if (newbuflen >= 0)
Andrew Certain0798cee2012-12-04 13:43:42 -0800254 ospf->maxsndbuflen = (unsigned int)newbuflen;
Denis Ovsienkob7fe4142007-08-21 16:32:56 +0000255 else
256 zlog_warn ("%s: failed to get SO_SNDBUF", __func__);
257 if (ospfd_privs.change (ZPRIVS_LOWER))
258 zlog_err ("%s: could not lower privs, %s", __func__,
259 safe_strerror (errno));
260}