ospf6_network.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. void
  101. ospf6_sso (u_int 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. zlog_err ("Network: setsockopt (%d) on ifindex %d failed: %s",
  112. option, ifindex, safe_strerror (errno));
  113. }
  114. static int
  115. iov_count (struct iovec *iov)
  116. {
  117. int i;
  118. for (i = 0; iov[i].iov_base; i++)
  119. ;
  120. return i;
  121. }
  122. static int
  123. iov_totallen (struct iovec *iov)
  124. {
  125. int i;
  126. int totallen = 0;
  127. for (i = 0; iov[i].iov_base; i++)
  128. totallen += iov[i].iov_len;
  129. return totallen;
  130. }
  131. int
  132. ospf6_sendmsg (struct in6_addr *src, struct in6_addr *dst,
  133. unsigned int *ifindex, struct iovec *message)
  134. {
  135. int retval;
  136. struct msghdr smsghdr;
  137. struct cmsghdr *scmsgp;
  138. u_char cmsgbuf[CMSG_SPACE(sizeof (struct in6_pktinfo))];
  139. struct in6_pktinfo *pktinfo;
  140. struct sockaddr_in6 dst_sin6;
  141. assert (dst);
  142. assert (*ifindex);
  143. scmsgp = (struct cmsghdr *)cmsgbuf;
  144. pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
  145. memset (&dst_sin6, 0, sizeof (struct sockaddr_in6));
  146. /* source address */
  147. pktinfo->ipi6_ifindex = *ifindex;
  148. if (src)
  149. memcpy (&pktinfo->ipi6_addr, src, sizeof (struct in6_addr));
  150. else
  151. memset (&pktinfo->ipi6_addr, 0, sizeof (struct in6_addr));
  152. /* destination address */
  153. dst_sin6.sin6_family = AF_INET6;
  154. #ifdef SIN6_LEN
  155. dst_sin6.sin6_len = sizeof (struct sockaddr_in6);
  156. #endif /*SIN6_LEN*/
  157. memcpy (&dst_sin6.sin6_addr, dst, sizeof (struct in6_addr));
  158. #ifdef HAVE_SIN6_SCOPE_ID
  159. dst_sin6.sin6_scope_id = *ifindex;
  160. #endif
  161. /* send control msg */
  162. scmsgp->cmsg_level = IPPROTO_IPV6;
  163. scmsgp->cmsg_type = IPV6_PKTINFO;
  164. scmsgp->cmsg_len = CMSG_LEN (sizeof (struct in6_pktinfo));
  165. /* scmsgp = CMSG_NXTHDR (&smsghdr, scmsgp); */
  166. /* send msg hdr */
  167. memset (&smsghdr, 0, sizeof (smsghdr));
  168. smsghdr.msg_iov = message;
  169. smsghdr.msg_iovlen = iov_count (message);
  170. smsghdr.msg_name = (caddr_t) &dst_sin6;
  171. smsghdr.msg_namelen = sizeof (struct sockaddr_in6);
  172. smsghdr.msg_control = (caddr_t) cmsgbuf;
  173. smsghdr.msg_controllen = scmsgp->cmsg_len;
  174. retval = sendmsg (ospf6_sock, &smsghdr, 0);
  175. if (retval != iov_totallen (message))
  176. zlog_warn ("sendmsg failed: ifindex: %d: %s (%d)",
  177. *ifindex, safe_strerror (errno), errno);
  178. return retval;
  179. }
  180. int
  181. ospf6_recvmsg (struct in6_addr *src, struct in6_addr *dst,
  182. unsigned int *ifindex, struct iovec *message)
  183. {
  184. int retval;
  185. struct msghdr rmsghdr;
  186. struct cmsghdr *rcmsgp;
  187. u_char cmsgbuf[CMSG_SPACE(sizeof (struct in6_pktinfo))];
  188. struct in6_pktinfo *pktinfo;
  189. struct sockaddr_in6 src_sin6;
  190. rcmsgp = (struct cmsghdr *)cmsgbuf;
  191. pktinfo = (struct in6_pktinfo *)(CMSG_DATA(rcmsgp));
  192. memset (&src_sin6, 0, sizeof (struct sockaddr_in6));
  193. /* receive control msg */
  194. rcmsgp->cmsg_level = IPPROTO_IPV6;
  195. rcmsgp->cmsg_type = IPV6_PKTINFO;
  196. rcmsgp->cmsg_len = CMSG_LEN (sizeof (struct in6_pktinfo));
  197. /* rcmsgp = CMSG_NXTHDR (&rmsghdr, rcmsgp); */
  198. /* receive msg hdr */
  199. memset (&rmsghdr, 0, sizeof (rmsghdr));
  200. rmsghdr.msg_iov = message;
  201. rmsghdr.msg_iovlen = iov_count (message);
  202. rmsghdr.msg_name = (caddr_t) &src_sin6;
  203. rmsghdr.msg_namelen = sizeof (struct sockaddr_in6);
  204. rmsghdr.msg_control = (caddr_t) cmsgbuf;
  205. rmsghdr.msg_controllen = sizeof (cmsgbuf);
  206. retval = recvmsg (ospf6_sock, &rmsghdr, 0);
  207. if (retval < 0)
  208. zlog_warn ("recvmsg failed: %s", safe_strerror (errno));
  209. else if (retval == iov_totallen (message))
  210. zlog_warn ("recvmsg read full buffer size: %d", retval);
  211. /* source address */
  212. assert (src);
  213. memcpy (src, &src_sin6.sin6_addr, sizeof (struct in6_addr));
  214. /* destination address */
  215. if (ifindex)
  216. *ifindex = pktinfo->ipi6_ifindex;
  217. if (dst)
  218. memcpy (dst, &pktinfo->ipi6_addr, sizeof (struct in6_addr));
  219. return retval;
  220. }