ospf6_network.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright (C) 2003 Yasuhiro Ohara
  3. *
  4. * This file is part of GNU Zebra.
  5. *
  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
  9. * later version.
  10. *
  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.
  15. *
  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.
  20. */
  21. #include <zebra.h>
  22. #include "log.h"
  23. #include "memory.h"
  24. #include "sockunion.h"
  25. #include "sockopt.h"
  26. #include "privs.h"
  27. #include "libospf.h"
  28. #include "ospf6_proto.h"
  29. #include "ospf6_network.h"
  30. extern struct zebra_privs_t ospf6d_privs;
  31. int ospf6_sock;
  32. struct in6_addr allspfrouters6;
  33. struct in6_addr alldrouters6;
  34. /* setsockopt MulticastLoop to off */
  35. static void
  36. ospf6_reset_mcastloop (void)
  37. {
  38. u_int off = 0;
  39. if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
  40. &off, sizeof (u_int)) < 0)
  41. zlog_warn ("Network: reset IPV6_MULTICAST_LOOP failed: %s",
  42. safe_strerror (errno));
  43. }
  44. static void
  45. ospf6_set_pktinfo (void)
  46. {
  47. setsockopt_ipv6_pktinfo (ospf6_sock, 1);
  48. }
  49. static void
  50. ospf6_set_transport_class (void)
  51. {
  52. #ifdef IPTOS_PREC_INTERNETCONTROL
  53. setsockopt_ipv6_tclass (ospf6_sock, IPTOS_PREC_INTERNETCONTROL);
  54. #endif
  55. }
  56. static void
  57. ospf6_set_checksum (void)
  58. {
  59. int offset = 12;
  60. #ifndef DISABLE_IPV6_CHECKSUM
  61. if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_CHECKSUM,
  62. &offset, sizeof (offset)) < 0)
  63. zlog_warn ("Network: set IPV6_CHECKSUM failed: %s", safe_strerror (errno));
  64. #else
  65. zlog_warn ("Network: Don't set IPV6_CHECKSUM");
  66. #endif /* DISABLE_IPV6_CHECKSUM */
  67. }
  68. /* Make ospf6d's server socket. */
  69. int
  70. ospf6_serv_sock (void)
  71. {
  72. if (ospf6d_privs.change (ZPRIVS_RAISE))
  73. zlog_err ("ospf6_serv_sock: could not raise privs");
  74. ospf6_sock = socket (AF_INET6, SOCK_RAW, IPPROTO_OSPFIGP);
  75. if (ospf6_sock < 0)
  76. {
  77. zlog_warn ("Network: can't create OSPF6 socket.");
  78. if (ospf6d_privs.change (ZPRIVS_LOWER))
  79. zlog_err ("ospf_sock_init: could not lower privs");
  80. return -1;
  81. }
  82. if (ospf6d_privs.change (ZPRIVS_LOWER))
  83. zlog_err ("ospf_sock_init: could not lower privs");
  84. /* set socket options */
  85. #if 1
  86. sockopt_reuseaddr (ospf6_sock);
  87. #else
  88. ospf6_set_reuseaddr ();
  89. #endif /*1*/
  90. ospf6_reset_mcastloop ();
  91. ospf6_set_pktinfo ();
  92. ospf6_set_transport_class ();
  93. ospf6_set_checksum ();
  94. /* setup global in6_addr, allspf6 and alldr6 for later use */
  95. inet_pton (AF_INET6, ALLSPFROUTERS6, &allspfrouters6);
  96. inet_pton (AF_INET6, ALLDROUTERS6, &alldrouters6);
  97. return 0;
  98. }
  99. /* ospf6 set socket option */
  100. int
  101. ospf6_sso (ifindex_t ifindex, struct in6_addr *group, int option)
  102. {
  103. struct ipv6_mreq mreq6;
  104. int ret;
  105. assert (ifindex);
  106. mreq6.ipv6mr_interface = ifindex;
  107. memcpy (&mreq6.ipv6mr_multiaddr, group, sizeof (struct in6_addr));
  108. ret = setsockopt (ospf6_sock, IPPROTO_IPV6, option,
  109. &mreq6, sizeof (mreq6));
  110. if (ret < 0)
  111. {
  112. zlog_err ("Network: setsockopt (%d) on ifindex %d failed: %s",
  113. option, ifindex, safe_strerror (errno));
  114. }
  115. return ret;
  116. }
  117. static int
  118. iov_count (struct iovec *iov)
  119. {
  120. int i;
  121. for (i = 0; iov[i].iov_base; i++)
  122. ;
  123. return i;
  124. }
  125. static int
  126. iov_totallen (struct iovec *iov)
  127. {
  128. int i;
  129. int totallen = 0;
  130. for (i = 0; iov[i].iov_base; i++)
  131. totallen += iov[i].iov_len;
  132. return totallen;
  133. }
  134. int
  135. ospf6_sendmsg (struct in6_addr *src, struct in6_addr *dst,
  136. ifindex_t *ifindex, struct iovec *message)
  137. {
  138. int retval;
  139. struct msghdr smsghdr;
  140. struct cmsghdr *scmsgp;
  141. u_char cmsgbuf[CMSG_SPACE(sizeof (struct in6_pktinfo))];
  142. struct in6_pktinfo *pktinfo;
  143. struct sockaddr_in6 dst_sin6;
  144. assert (dst);
  145. assert (*ifindex);
  146. scmsgp = (struct cmsghdr *)cmsgbuf;
  147. pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
  148. memset (&dst_sin6, 0, sizeof (struct sockaddr_in6));
  149. /* source address */
  150. pktinfo->ipi6_ifindex = *ifindex;
  151. if (src)
  152. memcpy (&pktinfo->ipi6_addr, src, sizeof (struct in6_addr));
  153. else
  154. memset (&pktinfo->ipi6_addr, 0, sizeof (struct in6_addr));
  155. /* destination address */
  156. dst_sin6.sin6_family = AF_INET6;
  157. #ifdef SIN6_LEN
  158. dst_sin6.sin6_len = sizeof (struct sockaddr_in6);
  159. #endif /*SIN6_LEN*/
  160. memcpy (&dst_sin6.sin6_addr, dst, sizeof (struct in6_addr));
  161. #ifdef HAVE_SIN6_SCOPE_ID
  162. dst_sin6.sin6_scope_id = *ifindex;
  163. #endif
  164. /* send control msg */
  165. scmsgp->cmsg_level = IPPROTO_IPV6;
  166. scmsgp->cmsg_type = IPV6_PKTINFO;
  167. scmsgp->cmsg_len = CMSG_LEN (sizeof (struct in6_pktinfo));
  168. /* scmsgp = CMSG_NXTHDR (&smsghdr, scmsgp); */
  169. /* send msg hdr */
  170. memset (&smsghdr, 0, sizeof (smsghdr));
  171. smsghdr.msg_iov = message;
  172. smsghdr.msg_iovlen = iov_count (message);
  173. smsghdr.msg_name = (caddr_t) &dst_sin6;
  174. smsghdr.msg_namelen = sizeof (struct sockaddr_in6);
  175. smsghdr.msg_control = (caddr_t) cmsgbuf;
  176. smsghdr.msg_controllen = scmsgp->cmsg_len;
  177. retval = sendmsg (ospf6_sock, &smsghdr, 0);
  178. if (retval != iov_totallen (message))
  179. zlog_warn ("sendmsg failed: ifindex: %d: %s (%d)",
  180. *ifindex, safe_strerror (errno), errno);
  181. return retval;
  182. }
  183. int
  184. ospf6_recvmsg (struct in6_addr *src, struct in6_addr *dst,
  185. ifindex_t *ifindex, struct iovec *message)
  186. {
  187. int retval;
  188. struct msghdr rmsghdr;
  189. struct cmsghdr *rcmsgp;
  190. u_char cmsgbuf[CMSG_SPACE(sizeof (struct in6_pktinfo))];
  191. struct in6_pktinfo *pktinfo;
  192. struct sockaddr_in6 src_sin6;
  193. rcmsgp = (struct cmsghdr *)cmsgbuf;
  194. pktinfo = (struct in6_pktinfo *)(CMSG_DATA(rcmsgp));
  195. memset (&src_sin6, 0, sizeof (struct sockaddr_in6));
  196. /* receive control msg */
  197. rcmsgp->cmsg_level = IPPROTO_IPV6;
  198. rcmsgp->cmsg_type = IPV6_PKTINFO;
  199. rcmsgp->cmsg_len = CMSG_LEN (sizeof (struct in6_pktinfo));
  200. /* rcmsgp = CMSG_NXTHDR (&rmsghdr, rcmsgp); */
  201. /* receive msg hdr */
  202. memset (&rmsghdr, 0, sizeof (rmsghdr));
  203. rmsghdr.msg_iov = message;
  204. rmsghdr.msg_iovlen = iov_count (message);
  205. rmsghdr.msg_name = (caddr_t) &src_sin6;
  206. rmsghdr.msg_namelen = sizeof (struct sockaddr_in6);
  207. rmsghdr.msg_control = (caddr_t) cmsgbuf;
  208. rmsghdr.msg_controllen = sizeof (cmsgbuf);
  209. retval = recvmsg (ospf6_sock, &rmsghdr, 0);
  210. if (retval < 0)
  211. zlog_warn ("recvmsg failed: %s", safe_strerror (errno));
  212. else if (retval == iov_totallen (message))
  213. zlog_warn ("recvmsg read full buffer size: %d", retval);
  214. /* source address */
  215. assert (src);
  216. memcpy (src, &src_sin6.sin6_addr, sizeof (struct in6_addr));
  217. /* destination address */
  218. if (ifindex)
  219. *ifindex = pktinfo->ipi6_ifindex;
  220. if (dst)
  221. memcpy (dst, &pktinfo->ipi6_addr, sizeof (struct in6_addr));
  222. return retval;
  223. }