2 * Copyright (C) 2003 Yasuhiro Ohara
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
26 #include "sockunion.h"
30 #include "ospf6_proto.h"
31 #include "ospf6_network.h"
33 extern struct zebra_privs_t ospf6d_privs
;
36 struct in6_addr allspfrouters6
;
37 struct in6_addr alldrouters6
;
39 /* setsockopt ReUseAddr to on */
41 ospf6_set_reuseaddr (void)
44 if (setsockopt (ospf6_sock
, SOL_SOCKET
, SO_REUSEADDR
, &on
,
46 zlog_warn ("Network: set SO_REUSEADDR failed: %s", safe_strerror (errno
));
49 /* setsockopt MulticastLoop to off */
51 ospf6_reset_mcastloop (void)
54 if (setsockopt (ospf6_sock
, IPPROTO_IPV6
, IPV6_MULTICAST_LOOP
,
55 &off
, sizeof (u_int
)) < 0)
56 zlog_warn ("Network: reset IPV6_MULTICAST_LOOP failed: %s",
57 safe_strerror (errno
));
61 ospf6_set_pktinfo (void)
63 setsockopt_ipv6_pktinfo (ospf6_sock
, 1);
67 ospf6_set_transport_class (void)
69 #ifdef IPTOS_PREC_INTERNETCONTROL
70 setsockopt_ipv6_tclass (ospf6_sock
, IPTOS_PREC_INTERNETCONTROL
);
75 ospf6_set_checksum (void)
78 #ifndef DISABLE_IPV6_CHECKSUM
79 if (setsockopt (ospf6_sock
, IPPROTO_IPV6
, IPV6_CHECKSUM
,
80 &offset
, sizeof (offset
)) < 0)
81 zlog_warn ("Network: set IPV6_CHECKSUM failed: %s", safe_strerror (errno
));
83 zlog_warn ("Network: Don't set IPV6_CHECKSUM");
84 #endif /* DISABLE_IPV6_CHECKSUM */
87 /* Make ospf6d's server socket. */
89 ospf6_serv_sock (void)
91 if (ospf6d_privs
.change (ZPRIVS_RAISE
))
92 zlog_err ("ospf6_serv_sock: could not raise privs");
94 ospf6_sock
= socket (AF_INET6
, SOCK_RAW
, IPPROTO_OSPFIGP
);
97 zlog_warn ("Network: can't create OSPF6 socket.");
98 if (ospf6d_privs
.change (ZPRIVS_LOWER
))
99 zlog_err ("ospf_sock_init: could not lower privs");
102 if (ospf6d_privs
.change (ZPRIVS_LOWER
))
103 zlog_err ("ospf_sock_init: could not lower privs");
105 /* set socket options */
107 sockopt_reuseaddr (ospf6_sock
);
109 ospf6_set_reuseaddr ();
111 ospf6_reset_mcastloop ();
112 ospf6_set_pktinfo ();
113 ospf6_set_transport_class ();
114 ospf6_set_checksum ();
116 /* setup global in6_addr, allspf6 and alldr6 for later use */
117 inet_pton (AF_INET6
, ALLSPFROUTERS6
, &allspfrouters6
);
118 inet_pton (AF_INET6
, ALLDROUTERS6
, &alldrouters6
);
123 /* ospf6 set socket option */
125 ospf6_sso (u_int ifindex
, struct in6_addr
*group
, int option
)
127 struct ipv6_mreq mreq6
;
131 mreq6
.ipv6mr_interface
= ifindex
;
132 memcpy (&mreq6
.ipv6mr_multiaddr
, group
, sizeof (struct in6_addr
));
134 ret
= setsockopt (ospf6_sock
, IPPROTO_IPV6
, option
,
135 &mreq6
, sizeof (mreq6
));
137 zlog_err ("Network: setsockopt (%d) on ifindex %d failed: %s",
138 option
, ifindex
, safe_strerror (errno
));
142 iov_count (struct iovec
*iov
)
145 for (i
= 0; iov
[i
].iov_base
; i
++)
151 iov_totallen (struct iovec
*iov
)
155 for (i
= 0; iov
[i
].iov_base
; i
++)
156 totallen
+= iov
[i
].iov_len
;
161 ospf6_sendmsg (struct in6_addr
*src
, struct in6_addr
*dst
,
162 unsigned int *ifindex
, struct iovec
*message
)
165 struct msghdr smsghdr
;
166 struct cmsghdr
*scmsgp
;
167 u_char cmsgbuf
[CMSG_SPACE(sizeof (struct in6_pktinfo
))];
168 struct in6_pktinfo
*pktinfo
;
169 struct sockaddr_in6 dst_sin6
;
174 scmsgp
= (struct cmsghdr
*)cmsgbuf
;
175 pktinfo
= (struct in6_pktinfo
*)(CMSG_DATA(scmsgp
));
176 memset (&dst_sin6
, 0, sizeof (struct sockaddr_in6
));
179 pktinfo
->ipi6_ifindex
= *ifindex
;
181 memcpy (&pktinfo
->ipi6_addr
, src
, sizeof (struct in6_addr
));
183 memset (&pktinfo
->ipi6_addr
, 0, sizeof (struct in6_addr
));
185 /* destination address */
186 dst_sin6
.sin6_family
= AF_INET6
;
188 dst_sin6
.sin6_len
= sizeof (struct sockaddr_in6
);
190 memcpy (&dst_sin6
.sin6_addr
, dst
, sizeof (struct in6_addr
));
191 #ifdef HAVE_SIN6_SCOPE_ID
192 dst_sin6
.sin6_scope_id
= *ifindex
;
195 /* send control msg */
196 scmsgp
->cmsg_level
= IPPROTO_IPV6
;
197 scmsgp
->cmsg_type
= IPV6_PKTINFO
;
198 scmsgp
->cmsg_len
= CMSG_LEN (sizeof (struct in6_pktinfo
));
199 /* scmsgp = CMSG_NXTHDR (&smsghdr, scmsgp); */
202 memset (&smsghdr
, 0, sizeof (smsghdr
));
203 smsghdr
.msg_iov
= message
;
204 smsghdr
.msg_iovlen
= iov_count (message
);
205 smsghdr
.msg_name
= (caddr_t
) &dst_sin6
;
206 smsghdr
.msg_namelen
= sizeof (struct sockaddr_in6
);
207 smsghdr
.msg_control
= (caddr_t
) cmsgbuf
;
208 smsghdr
.msg_controllen
= sizeof (cmsgbuf
);
210 retval
= sendmsg (ospf6_sock
, &smsghdr
, 0);
211 if (retval
!= iov_totallen (message
))
212 zlog_warn ("sendmsg failed: ifindex: %d: %s (%d)",
213 *ifindex
, safe_strerror (errno
), errno
);
219 ospf6_recvmsg (struct in6_addr
*src
, struct in6_addr
*dst
,
220 unsigned int *ifindex
, struct iovec
*message
)
223 struct msghdr rmsghdr
;
224 struct cmsghdr
*rcmsgp
;
225 u_char cmsgbuf
[CMSG_SPACE(sizeof (struct in6_pktinfo
))];
226 struct in6_pktinfo
*pktinfo
;
227 struct sockaddr_in6 src_sin6
;
229 rcmsgp
= (struct cmsghdr
*)cmsgbuf
;
230 pktinfo
= (struct in6_pktinfo
*)(CMSG_DATA(rcmsgp
));
231 memset (&src_sin6
, 0, sizeof (struct sockaddr_in6
));
233 /* receive control msg */
234 rcmsgp
->cmsg_level
= IPPROTO_IPV6
;
235 rcmsgp
->cmsg_type
= IPV6_PKTINFO
;
236 rcmsgp
->cmsg_len
= CMSG_LEN (sizeof (struct in6_pktinfo
));
237 /* rcmsgp = CMSG_NXTHDR (&rmsghdr, rcmsgp); */
239 /* receive msg hdr */
240 memset (&rmsghdr
, 0, sizeof (rmsghdr
));
241 rmsghdr
.msg_iov
= message
;
242 rmsghdr
.msg_iovlen
= iov_count (message
);
243 rmsghdr
.msg_name
= (caddr_t
) &src_sin6
;
244 rmsghdr
.msg_namelen
= sizeof (struct sockaddr_in6
);
245 rmsghdr
.msg_control
= (caddr_t
) cmsgbuf
;
246 rmsghdr
.msg_controllen
= sizeof (cmsgbuf
);
248 retval
= recvmsg (ospf6_sock
, &rmsghdr
, 0);
250 zlog_warn ("recvmsg failed: %s", safe_strerror (errno
));
251 else if (retval
== iov_totallen (message
))
252 zlog_warn ("recvmsg read full buffer size: %d", retval
);
256 memcpy (src
, &src_sin6
.sin6_addr
, sizeof (struct in6_addr
));
258 /* destination address */
260 *ifindex
= pktinfo
->ipi6_ifindex
;
262 memcpy (dst
, &pktinfo
->ipi6_addr
, sizeof (struct in6_addr
));