blob: 1e2d44e6b9d9e3e79dc7a964e28d408ced7eea2b [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,
51 unsigned int ifindex)
52{
53 int ret;
54
55 ret = setsockopt_multicast_ipv4 (top->fd, IP_ADD_MEMBERSHIP,
56 p->u.prefix4, htonl (OSPF_ALLSPFROUTERS),
57 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,
72 unsigned int ifindex)
73{
74 int ret;
75
76 ret = setsockopt_multicast_ipv4 (top->fd, IP_DROP_MEMBERSHIP,
77 p->u.prefix4, htonl (OSPF_ALLSPFROUTERS),
78 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
92ospf_if_add_alldrouters (struct ospf *top, struct prefix *p, unsigned int
93 ifindex)
94{
95 int ret;
96
97 ret = setsockopt_multicast_ipv4 (top->fd, IP_ADD_MEMBERSHIP,
98 p->u.prefix4, htonl (OSPF_ALLDROUTERS),
99 ifindex);
100 if (ret < 0)
ajs3dc56b52004-12-30 15:11:19 +0000101 zlog_warn ("can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, "
102 "ifindex %u, AllDRouters): %s; perhaps a kernel limit "
103 "on # of multicast group memberships has been exceeded?",
104 top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +0000105 else
Paul Jakmaec704972010-12-06 12:21:52 +0000106 zlog_debug ("interface %s [%u] join AllDRouters Multicast group.",
107 inet_ntoa (p->u.prefix4), ifindex);
paul718e3742002-12-13 20:15:29 +0000108
109 return ret;
110}
111
112int
113ospf_if_drop_alldrouters (struct ospf *top, struct prefix *p, unsigned int
114 ifindex)
115{
116 int ret;
117
118 ret = setsockopt_multicast_ipv4 (top->fd, IP_DROP_MEMBERSHIP,
119 p->u.prefix4, htonl (OSPF_ALLDROUTERS),
120 ifindex);
121 if (ret < 0)
ajs3dc56b52004-12-30 15:11:19 +0000122 zlog_warn ("can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, "
123 "ifindex %u, AllDRouters): %s",
124 top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +0000125 else
Paul Jakmaec704972010-12-06 12:21:52 +0000126 zlog_debug ("interface %s [%u] leave AllDRouters Multicast group.",
127 inet_ntoa (p->u.prefix4), ifindex);
paul718e3742002-12-13 20:15:29 +0000128
129 return ret;
130}
131
132int
133ospf_if_ipmulticast (struct ospf *top, struct prefix *p, unsigned int ifindex)
134{
135 u_char val;
136 int ret, len;
137
138 val = 0;
139 len = sizeof (val);
140
141 /* Prevent receiving self-origined multicast packets. */
142 ret = setsockopt (top->fd, IPPROTO_IP, IP_MULTICAST_LOOP, (void *)&val, len);
143 if (ret < 0)
ajs3dc56b52004-12-30 15:11:19 +0000144 zlog_warn ("can't setsockopt IP_MULTICAST_LOOP(0) for fd %d: %s",
145 top->fd, safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +0000146
147 /* Explicitly set multicast ttl to 1 -- endo. */
148 val = 1;
149 ret = setsockopt (top->fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&val, len);
150 if (ret < 0)
ajs3dc56b52004-12-30 15:11:19 +0000151 zlog_warn ("can't setsockopt IP_MULTICAST_TTL(1) for fd %d: %s",
152 top->fd, safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000153
154 ret = setsockopt_multicast_ipv4 (top->fd, IP_MULTICAST_IF,
155 p->u.prefix4, 0, ifindex);
156 if (ret < 0)
ajs3dc56b52004-12-30 15:11:19 +0000157 zlog_warn("can't setsockopt IP_MULTICAST_IF(fd %d, addr %s, "
158 "ifindex %u): %s",
159 top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno));
paul718e3742002-12-13 20:15:29 +0000160
161 return ret;
162}
163
164int
165ospf_sock_init (void)
166{
167 int ospf_sock;
Stephen Hemminger1423c802008-08-14 17:59:25 +0100168 int ret, hincl = 1;
paul718e3742002-12-13 20:15:29 +0000169
pauledd7c242003-06-04 13:59:38 +0000170 if ( ospfd_privs.change (ZPRIVS_RAISE) )
171 zlog_err ("ospf_sock_init: could not raise privs, %s",
ajs6099b3b2004-11-20 02:06:59 +0000172 safe_strerror (errno) );
pauledd7c242003-06-04 13:59:38 +0000173
paul718e3742002-12-13 20:15:29 +0000174 ospf_sock = socket (AF_INET, SOCK_RAW, IPPROTO_OSPFIGP);
175 if (ospf_sock < 0)
176 {
ajs0b7d97d2005-01-30 17:24:02 +0000177 int save_errno = errno;
pauledd7c242003-06-04 13:59:38 +0000178 if ( ospfd_privs.change (ZPRIVS_LOWER) )
179 zlog_err ("ospf_sock_init: could not lower privs, %s",
ajs6099b3b2004-11-20 02:06:59 +0000180 safe_strerror (errno) );
ajs0b7d97d2005-01-30 17:24:02 +0000181 zlog_err ("ospf_read_sock_init: socket: %s", safe_strerror (save_errno));
182 exit(1);
paul718e3742002-12-13 20:15:29 +0000183 }
pauledd7c242003-06-04 13:59:38 +0000184
paul5bd41892004-05-05 17:29:24 +0000185#ifdef IP_HDRINCL
186 /* we will include IP header with packet */
187 ret = setsockopt (ospf_sock, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof (hincl));
188 if (ret < 0)
189 {
ajs0b7d97d2005-01-30 17:24:02 +0000190 int save_errno = errno;
paul5bd41892004-05-05 17:29:24 +0000191 if ( ospfd_privs.change (ZPRIVS_LOWER) )
192 zlog_err ("ospf_sock_init: could not lower privs, %s",
ajs6099b3b2004-11-20 02:06:59 +0000193 safe_strerror (errno) );
ajs0b7d97d2005-01-30 17:24:02 +0000194 zlog_warn ("Can't set IP_HDRINCL option for fd %d: %s",
195 ospf_sock, safe_strerror(save_errno));
paul5bd41892004-05-05 17:29:24 +0000196 }
197#elif defined (IPTOS_PREC_INTERNETCONTROL)
198#warning "IP_HDRINCL not available on this system"
199#warning "using IPTOS_PREC_INTERNETCONTROL"
Stephen Hemminger1423c802008-08-14 17:59:25 +0100200 ret = setsockopt_ipv4_tos(ospf_sock, IPTOS_PREC_INTERNETCONTROL);
paul718e3742002-12-13 20:15:29 +0000201 if (ret < 0)
202 {
ajs0b7d97d2005-01-30 17:24:02 +0000203 int save_errno = errno;
pauledd7c242003-06-04 13:59:38 +0000204 if ( ospfd_privs.change (ZPRIVS_LOWER) )
205 zlog_err ("ospf_sock_init: could not lower privs, %s",
ajs6099b3b2004-11-20 02:06:59 +0000206 safe_strerror (errno) );
ajs0b7d97d2005-01-30 17:24:02 +0000207 zlog_warn ("can't set sockopt IP_TOS %d to socket %d: %s",
208 tos, ospf_sock, safe_strerror(save_errno));
paul718e3742002-12-13 20:15:29 +0000209 close (ospf_sock); /* Prevent sd leak. */
210 return ret;
211 }
paul5bd41892004-05-05 17:29:24 +0000212#else /* !IPTOS_PREC_INTERNETCONTROL */
213#warning "IP_HDRINCL not available, nor is IPTOS_PREC_INTERNETCONTROL"
214 zlog_warn ("IP_HDRINCL option not available");
215#endif /* IP_HDRINCL */
paul718e3742002-12-13 20:15:29 +0000216
paulac191232004-10-22 12:05:17 +0000217 ret = setsockopt_ifindex (AF_INET, ospf_sock, 1);
218
paul2dd8bb42004-07-23 15:13:48 +0000219 if (ret < 0)
ajs3dc56b52004-12-30 15:11:19 +0000220 zlog_warn ("Can't set pktinfo option for fd %d", ospf_sock);
pauledd7c242003-06-04 13:59:38 +0000221
222 if (ospfd_privs.change (ZPRIVS_LOWER))
223 {
224 zlog_err ("ospf_sock_init: could not lower privs, %s",
ajs6099b3b2004-11-20 02:06:59 +0000225 safe_strerror (errno) );
pauledd7c242003-06-04 13:59:38 +0000226 }
paul718e3742002-12-13 20:15:29 +0000227
228 return ospf_sock;
229}
Denis Ovsienkob7fe4142007-08-21 16:32:56 +0000230
231void
232ospf_adjust_sndbuflen (struct ospf * ospf, int buflen)
233{
234 int ret, newbuflen;
235 /* Check if any work has to be done at all. */
236 if (ospf->maxsndbuflen >= buflen)
237 return;
238 if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
239 zlog_debug ("%s: adjusting OSPF send buffer size to %d",
240 __func__, buflen);
241 if (ospfd_privs.change (ZPRIVS_RAISE))
242 zlog_err ("%s: could not raise privs, %s", __func__,
243 safe_strerror (errno));
244 /* Now we try to set SO_SNDBUF to what our caller has requested
Denis Ovsienkof102e752007-09-18 09:01:13 +0000245 * (the MTU of a newly added interface). However, if the OS has
246 * truncated the actual buffer size to somewhat less size, try
247 * to detect it and update our records appropriately. The OS
248 * may allocate more buffer space, than requested, this isn't
249 * a error.
Denis Ovsienkob7fe4142007-08-21 16:32:56 +0000250 */
251 ret = setsockopt_so_sendbuf (ospf->fd, buflen);
252 newbuflen = getsockopt_so_sendbuf (ospf->fd);
Denis Ovsienkof102e752007-09-18 09:01:13 +0000253 if (ret < 0 || newbuflen < buflen)
Denis Ovsienkob7fe4142007-08-21 16:32:56 +0000254 zlog_warn ("%s: tried to set SO_SNDBUF to %d, but got %d",
255 __func__, buflen, newbuflen);
256 if (newbuflen >= 0)
257 ospf->maxsndbuflen = newbuflen;
258 else
259 zlog_warn ("%s: failed to get SO_SNDBUF", __func__);
260 if (ospfd_privs.change (ZPRIVS_LOWER))
261 zlog_err ("%s: could not lower privs, %s", __func__,
262 safe_strerror (errno));
263}