ospf6_network.c 7.1 KB

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