]>
Commit | Line | Data |
---|---|---|
718e3744 | 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 | * | |
896014f4 DL |
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 | |
718e3744 | 20 | */ |
21 | ||
22 | #include <zebra.h> | |
23 | ||
24 | #include "thread.h" | |
25 | #include "linklist.h" | |
26 | #include "prefix.h" | |
27 | #include "if.h" | |
28 | #include "sockunion.h" | |
29 | #include "log.h" | |
30 | #include "sockopt.h" | |
edd7c245 | 31 | #include "privs.h" |
313d7993 | 32 | #include "lib_errors.h" |
edd7c245 | 33 | |
718e3744 | 34 | #include "ospfd/ospfd.h" |
35 | #include "ospfd/ospf_network.h" | |
36 | #include "ospfd/ospf_interface.h" | |
37 | #include "ospfd/ospf_asbr.h" | |
38 | #include "ospfd/ospf_lsa.h" | |
39 | #include "ospfd/ospf_lsdb.h" | |
40 | #include "ospfd/ospf_neighbor.h" | |
41 | #include "ospfd/ospf_packet.h" | |
05ba78e4 | 42 | #include "ospfd/ospf_dump.h" |
edd7c245 | 43 | |
718e3744 | 44 | /* Join to the OSPF ALL SPF ROUTERS multicast group. */ |
d62a17ae | 45 | int ospf_if_add_allspfrouters(struct ospf *top, struct prefix *p, |
46 | ifindex_t ifindex) | |
718e3744 | 47 | { |
d62a17ae | 48 | int ret; |
49 | ||
50 | ret = setsockopt_ipv4_multicast(top->fd, IP_ADD_MEMBERSHIP, | |
51 | p->u.prefix4, htonl(OSPF_ALLSPFROUTERS), | |
52 | ifindex); | |
53 | if (ret < 0) | |
54 | zlog_warn( | |
55 | "can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, " | |
56 | "ifindex %u, AllSPFRouters): %s; perhaps a kernel limit " | |
57 | "on # of multicast group memberships has been exceeded?", | |
58 | top->fd, inet_ntoa(p->u.prefix4), ifindex, | |
59 | safe_strerror(errno)); | |
05ba78e4 CS |
60 | else { |
61 | if (IS_DEBUG_OSPF_EVENT) | |
996c9314 LB |
62 | zlog_debug( |
63 | "interface %s [%u] join AllSPFRouters Multicast group.", | |
64 | inet_ntoa(p->u.prefix4), ifindex); | |
05ba78e4 | 65 | } |
d62a17ae | 66 | |
67 | return ret; | |
718e3744 | 68 | } |
69 | ||
d62a17ae | 70 | int ospf_if_drop_allspfrouters(struct ospf *top, struct prefix *p, |
71 | ifindex_t ifindex) | |
718e3744 | 72 | { |
d62a17ae | 73 | int ret; |
74 | ||
75 | ret = setsockopt_ipv4_multicast(top->fd, IP_DROP_MEMBERSHIP, | |
76 | p->u.prefix4, htonl(OSPF_ALLSPFROUTERS), | |
77 | ifindex); | |
78 | if (ret < 0) | |
79 | zlog_warn( | |
80 | "can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, " | |
81 | "ifindex %u, AllSPFRouters): %s", | |
82 | top->fd, inet_ntoa(p->u.prefix4), ifindex, | |
83 | safe_strerror(errno)); | |
05ba78e4 CS |
84 | else { |
85 | if (IS_DEBUG_OSPF_EVENT) | |
996c9314 LB |
86 | zlog_debug( |
87 | "interface %s [%u] leave AllSPFRouters Multicast group.", | |
88 | inet_ntoa(p->u.prefix4), ifindex); | |
05ba78e4 | 89 | } |
d62a17ae | 90 | |
91 | return ret; | |
718e3744 | 92 | } |
93 | ||
94 | /* Join to the OSPF ALL Designated ROUTERS multicast group. */ | |
d62a17ae | 95 | int ospf_if_add_alldrouters(struct ospf *top, struct prefix *p, |
96 | ifindex_t ifindex) | |
718e3744 | 97 | { |
d62a17ae | 98 | int ret; |
99 | ||
100 | ret = setsockopt_ipv4_multicast(top->fd, IP_ADD_MEMBERSHIP, | |
101 | p->u.prefix4, htonl(OSPF_ALLDROUTERS), | |
102 | ifindex); | |
103 | if (ret < 0) | |
104 | zlog_warn( | |
105 | "can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, " | |
106 | "ifindex %u, AllDRouters): %s; perhaps a kernel limit " | |
107 | "on # of multicast group memberships has been exceeded?", | |
108 | top->fd, inet_ntoa(p->u.prefix4), ifindex, | |
109 | safe_strerror(errno)); | |
110 | else | |
111 | zlog_debug( | |
112 | "interface %s [%u] join AllDRouters Multicast group.", | |
113 | inet_ntoa(p->u.prefix4), ifindex); | |
114 | ||
115 | return ret; | |
718e3744 | 116 | } |
117 | ||
d62a17ae | 118 | int ospf_if_drop_alldrouters(struct ospf *top, struct prefix *p, |
119 | ifindex_t ifindex) | |
718e3744 | 120 | { |
d62a17ae | 121 | int ret; |
122 | ||
123 | ret = setsockopt_ipv4_multicast(top->fd, IP_DROP_MEMBERSHIP, | |
124 | p->u.prefix4, htonl(OSPF_ALLDROUTERS), | |
125 | ifindex); | |
126 | if (ret < 0) | |
127 | zlog_warn( | |
128 | "can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, " | |
129 | "ifindex %u, AllDRouters): %s", | |
130 | top->fd, inet_ntoa(p->u.prefix4), ifindex, | |
131 | safe_strerror(errno)); | |
132 | else | |
133 | zlog_debug( | |
134 | "interface %s [%u] leave AllDRouters Multicast group.", | |
135 | inet_ntoa(p->u.prefix4), ifindex); | |
136 | ||
137 | return ret; | |
718e3744 | 138 | } |
139 | ||
d62a17ae | 140 | int ospf_if_ipmulticast(struct ospf *top, struct prefix *p, ifindex_t ifindex) |
718e3744 | 141 | { |
d7c0a89a | 142 | uint8_t val; |
d62a17ae | 143 | int ret, len; |
144 | ||
145 | /* Prevent receiving self-origined multicast packets. */ | |
146 | ret = setsockopt_ipv4_multicast_loop(top->fd, 0); | |
147 | if (ret < 0) | |
148 | zlog_warn("can't setsockopt IP_MULTICAST_LOOP(0) for fd %d: %s", | |
149 | top->fd, safe_strerror(errno)); | |
150 | ||
151 | /* Explicitly set multicast ttl to 1 -- endo. */ | |
152 | val = 1; | |
153 | len = sizeof(val); | |
154 | ret = setsockopt(top->fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&val, | |
155 | len); | |
156 | if (ret < 0) | |
157 | zlog_warn("can't setsockopt IP_MULTICAST_TTL(1) for fd %d: %s", | |
158 | top->fd, safe_strerror(errno)); | |
e1b18df1 CS |
159 | #ifndef GNU_LINUX |
160 | /* For GNU LINUX ospf_write uses IP_PKTINFO, in_pktinfo to send | |
161 | * packet out of ifindex. Below would be used Non Linux system. | |
162 | */ | |
163 | ret = setsockopt_ipv4_multicast_if(top->fd, p->u.prefix4, ifindex); | |
164 | if (ret < 0) | |
165 | zlog_warn( | |
166 | "can't setsockopt IP_MULTICAST_IF(fd %d, addr %s, " | |
167 | "ifindex %u): %s", | |
168 | top->fd, inet_ntoa(p->u.prefix4), ifindex, | |
169 | safe_strerror(errno)); | |
170 | #endif | |
d62a17ae | 171 | |
e7503eab CS |
172 | return ret; |
173 | } | |
174 | ||
e7503eab | 175 | int ospf_sock_init(struct ospf *ospf) |
718e3744 | 176 | { |
d62a17ae | 177 | int ospf_sock; |
178 | int ret, hincl = 1; | |
179 | int bufsize = (8 * 1024 * 1024); | |
180 | ||
3c0eb8fa PG |
181 | /* silently ignore. already done */ |
182 | if (ospf->fd > 0) | |
183 | return -1; | |
184 | ||
185 | if (ospf->vrf_id == VRF_UNKNOWN) { | |
186 | /* silently return since VRF is not ready */ | |
187 | return -1; | |
188 | } | |
6bb30c2c DL |
189 | frr_elevate_privs(&ospfd_privs) { |
190 | ospf_sock = vrf_socket(AF_INET, SOCK_RAW, IPPROTO_OSPFIGP, | |
191 | ospf->vrf_id, ospf->name); | |
192 | if (ospf_sock < 0) { | |
193 | zlog_err("ospf_read_sock_init: socket: %s", | |
194 | safe_strerror(errno)); | |
195 | exit(1); | |
196 | } | |
d62a17ae | 197 | |
5bd4189c | 198 | #ifdef IP_HDRINCL |
6bb30c2c DL |
199 | /* we will include IP header with packet */ |
200 | ret = setsockopt(ospf_sock, IPPROTO_IP, IP_HDRINCL, &hincl, | |
201 | sizeof(hincl)); | |
202 | if (ret < 0) { | |
203 | zlog_warn("Can't set IP_HDRINCL option for fd %d: %s", | |
204 | ospf_sock, safe_strerror(errno)); | |
205 | close(ospf_sock); | |
206 | break; | |
207 | } | |
d62a17ae | 208 | #elif defined(IPTOS_PREC_INTERNETCONTROL) |
5bd4189c | 209 | #warning "IP_HDRINCL not available on this system" |
210 | #warning "using IPTOS_PREC_INTERNETCONTROL" | |
6bb30c2c DL |
211 | ret = setsockopt_ipv4_tos(ospf_sock, |
212 | IPTOS_PREC_INTERNETCONTROL); | |
213 | if (ret < 0) { | |
214 | zlog_warn("can't set sockopt IP_TOS %d to socket %d: %s", | |
215 | tos, ospf_sock, safe_strerror(errno)); | |
216 | close(ospf_sock); /* Prevent sd leak. */ | |
217 | break; | |
218 | } | |
5bd4189c | 219 | #else /* !IPTOS_PREC_INTERNETCONTROL */ |
220 | #warning "IP_HDRINCL not available, nor is IPTOS_PREC_INTERNETCONTROL" | |
6bb30c2c | 221 | zlog_warn("IP_HDRINCL option not available"); |
5bd4189c | 222 | #endif /* IP_HDRINCL */ |
718e3744 | 223 | |
6bb30c2c | 224 | ret = setsockopt_ifindex(AF_INET, ospf_sock, 1); |
ac191232 | 225 | |
6bb30c2c DL |
226 | if (ret < 0) |
227 | zlog_warn("Can't set pktinfo option for fd %d", | |
228 | ospf_sock); | |
edd7c245 | 229 | |
6bb30c2c DL |
230 | setsockopt_so_sendbuf(ospf_sock, bufsize); |
231 | setsockopt_so_recvbuf(ospf_sock, bufsize); | |
232 | } | |
e7503eab CS |
233 | |
234 | ospf->fd = ospf_sock; | |
e7503eab | 235 | return ret; |
718e3744 | 236 | } |