ospf6_network.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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_checksum (void)
  59. {
  60. int offset = 12;
  61. #ifndef DISABLE_IPV6_CHECKSUM
  62. if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_CHECKSUM,
  63. &offset, sizeof (offset)) < 0)
  64. zlog_warn ("Network: set IPV6_CHECKSUM failed: %s", safe_strerror (errno));
  65. #else
  66. zlog_warn ("Network: Don't set IPV6_CHECKSUM");
  67. #endif /* DISABLE_IPV6_CHECKSUM */
  68. }
  69. /* Make ospf6d's server socket. */
  70. int
  71. ospf6_serv_sock (void)
  72. {
  73. if (ospf6d_privs.change (ZPRIVS_RAISE))
  74. zlog_err ("ospf6_serv_sock: could not raise privs");
  75. ospf6_sock = socket (AF_INET6, SOCK_RAW, IPPROTO_OSPFIGP);
  76. if (ospf6_sock < 0)
  77. {
  78. zlog_warn ("Network: can't create OSPF6 socket.");
  79. if (ospf6d_privs.change (ZPRIVS_LOWER))
  80. zlog_err ("ospf_sock_init: could not lower privs");
  81. return -1;
  82. }
  83. if (ospf6d_privs.change (ZPRIVS_LOWER))
  84. zlog_err ("ospf_sock_init: could not lower privs");
  85. /* set socket options */
  86. #if 1
  87. sockopt_reuseaddr (ospf6_sock);
  88. #else
  89. ospf6_set_reuseaddr ();
  90. #endif /*1*/
  91. ospf6_reset_mcastloop ();
  92. ospf6_set_pktinfo ();
  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. void
  100. ospf6_join_allspfrouters (u_int ifindex)
  101. {
  102. struct ipv6_mreq mreq6;
  103. int retval;
  104. assert (ifindex);
  105. mreq6.ipv6mr_interface = ifindex;
  106. memcpy (&mreq6.ipv6mr_multiaddr, &allspfrouters6,
  107. sizeof (struct in6_addr));
  108. retval = setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
  109. &mreq6, sizeof (mreq6));
  110. if (retval < 0)
  111. zlog_err ("Network: Join AllSPFRouters on ifindex %d failed: %s",
  112. ifindex, safe_strerror (errno));
  113. #if 0
  114. else
  115. zlog_debug ("Network: Join AllSPFRouters on ifindex %d", ifindex);
  116. #endif
  117. }
  118. void
  119. ospf6_leave_allspfrouters (u_int ifindex)
  120. {
  121. struct ipv6_mreq mreq6;
  122. assert (ifindex);
  123. mreq6.ipv6mr_interface = ifindex;
  124. memcpy (&mreq6.ipv6mr_multiaddr, &allspfrouters6,
  125. sizeof (struct in6_addr));
  126. if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
  127. &mreq6, sizeof (mreq6)) < 0)
  128. zlog_warn ("Network: Leave AllSPFRouters on ifindex %d Failed: %s",
  129. ifindex, safe_strerror (errno));
  130. #if 0
  131. else
  132. zlog_debug ("Network: Leave AllSPFRouters on ifindex %d", ifindex);
  133. #endif
  134. }
  135. void
  136. ospf6_join_alldrouters (u_int ifindex)
  137. {
  138. struct ipv6_mreq mreq6;
  139. assert (ifindex);
  140. mreq6.ipv6mr_interface = ifindex;
  141. memcpy (&mreq6.ipv6mr_multiaddr, &alldrouters6,
  142. sizeof (struct in6_addr));
  143. if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
  144. &mreq6, sizeof (mreq6)) < 0)
  145. zlog_warn ("Network: Join AllDRouters on ifindex %d Failed: %s",
  146. ifindex, safe_strerror (errno));
  147. #if 0
  148. else
  149. zlog_debug ("Network: Join AllDRouters on ifindex %d", ifindex);
  150. #endif
  151. }
  152. void
  153. ospf6_leave_alldrouters (u_int ifindex)
  154. {
  155. struct ipv6_mreq mreq6;
  156. assert (ifindex);
  157. mreq6.ipv6mr_interface = ifindex;
  158. memcpy (&mreq6.ipv6mr_multiaddr, &alldrouters6,
  159. sizeof (struct in6_addr));
  160. if (setsockopt (ospf6_sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
  161. &mreq6, sizeof (mreq6)) < 0)
  162. zlog_warn ("Network: Leave AllDRouters on ifindex %d Failed", ifindex);
  163. #if 0
  164. else
  165. zlog_debug ("Network: Leave AllDRouters on ifindex %d", ifindex);
  166. #endif
  167. }
  168. static int
  169. iov_count (struct iovec *iov)
  170. {
  171. int i;
  172. for (i = 0; iov[i].iov_base; i++)
  173. ;
  174. return i;
  175. }
  176. static int
  177. iov_totallen (struct iovec *iov)
  178. {
  179. int i;
  180. int totallen = 0;
  181. for (i = 0; iov[i].iov_base; i++)
  182. totallen += iov[i].iov_len;
  183. return totallen;
  184. }
  185. int
  186. ospf6_sendmsg (struct in6_addr *src, struct in6_addr *dst,
  187. unsigned int *ifindex, struct iovec *message)
  188. {
  189. int retval;
  190. struct msghdr smsghdr;
  191. struct cmsghdr *scmsgp;
  192. u_char cmsgbuf[CMSG_SPACE(sizeof (struct in6_pktinfo))];
  193. struct in6_pktinfo *pktinfo;
  194. struct sockaddr_in6 dst_sin6;
  195. assert (dst);
  196. assert (*ifindex);
  197. scmsgp = (struct cmsghdr *)cmsgbuf;
  198. pktinfo = (struct in6_pktinfo *)(CMSG_DATA(scmsgp));
  199. memset (&dst_sin6, 0, sizeof (struct sockaddr_in6));
  200. /* source address */
  201. pktinfo->ipi6_ifindex = *ifindex;
  202. if (src)
  203. memcpy (&pktinfo->ipi6_addr, src, sizeof (struct in6_addr));
  204. else
  205. memset (&pktinfo->ipi6_addr, 0, sizeof (struct in6_addr));
  206. /* destination address */
  207. dst_sin6.sin6_family = AF_INET6;
  208. #ifdef SIN6_LEN
  209. dst_sin6.sin6_len = sizeof (struct sockaddr_in6);
  210. #endif /*SIN6_LEN*/
  211. memcpy (&dst_sin6.sin6_addr, dst, sizeof (struct in6_addr));
  212. #ifdef HAVE_SIN6_SCOPE_ID
  213. dst_sin6.sin6_scope_id = *ifindex;
  214. #endif
  215. /* send control msg */
  216. scmsgp->cmsg_level = IPPROTO_IPV6;
  217. scmsgp->cmsg_type = IPV6_PKTINFO;
  218. scmsgp->cmsg_len = CMSG_LEN (sizeof (struct in6_pktinfo));
  219. /* scmsgp = CMSG_NXTHDR (&smsghdr, scmsgp); */
  220. /* send msg hdr */
  221. memset (&smsghdr, 0, sizeof (smsghdr));
  222. smsghdr.msg_iov = message;
  223. smsghdr.msg_iovlen = iov_count (message);
  224. smsghdr.msg_name = (caddr_t) &dst_sin6;
  225. smsghdr.msg_namelen = sizeof (struct sockaddr_in6);
  226. smsghdr.msg_control = (caddr_t) cmsgbuf;
  227. smsghdr.msg_controllen = sizeof (cmsgbuf);
  228. retval = sendmsg (ospf6_sock, &smsghdr, 0);
  229. if (retval != iov_totallen (message))
  230. zlog_warn ("sendmsg failed: ifindex: %d: %s (%d)",
  231. *ifindex, safe_strerror (errno), errno);
  232. return retval;
  233. }
  234. int
  235. ospf6_recvmsg (struct in6_addr *src, struct in6_addr *dst,
  236. unsigned int *ifindex, struct iovec *message)
  237. {
  238. int retval;
  239. struct msghdr rmsghdr;
  240. struct cmsghdr *rcmsgp;
  241. u_char cmsgbuf[CMSG_SPACE(sizeof (struct in6_pktinfo))];
  242. struct in6_pktinfo *pktinfo;
  243. struct sockaddr_in6 src_sin6;
  244. rcmsgp = (struct cmsghdr *)cmsgbuf;
  245. pktinfo = (struct in6_pktinfo *)(CMSG_DATA(rcmsgp));
  246. memset (&src_sin6, 0, sizeof (struct sockaddr_in6));
  247. /* receive control msg */
  248. rcmsgp->cmsg_level = IPPROTO_IPV6;
  249. rcmsgp->cmsg_type = IPV6_PKTINFO;
  250. rcmsgp->cmsg_len = CMSG_LEN (sizeof (struct in6_pktinfo));
  251. /* rcmsgp = CMSG_NXTHDR (&rmsghdr, rcmsgp); */
  252. /* receive msg hdr */
  253. memset (&rmsghdr, 0, sizeof (rmsghdr));
  254. rmsghdr.msg_iov = message;
  255. rmsghdr.msg_iovlen = iov_count (message);
  256. rmsghdr.msg_name = (caddr_t) &src_sin6;
  257. rmsghdr.msg_namelen = sizeof (struct sockaddr_in6);
  258. rmsghdr.msg_control = (caddr_t) cmsgbuf;
  259. rmsghdr.msg_controllen = sizeof (cmsgbuf);
  260. retval = recvmsg (ospf6_sock, &rmsghdr, 0);
  261. if (retval < 0)
  262. zlog_warn ("recvmsg failed: %s", safe_strerror (errno));
  263. else if (retval == iov_totallen (message))
  264. zlog_warn ("recvmsg read full buffer size: %d", retval);
  265. /* source address */
  266. assert (src);
  267. memcpy (src, &src_sin6.sin6_addr, sizeof (struct in6_addr));
  268. /* destination address */
  269. if (ifindex)
  270. *ifindex = pktinfo->ipi6_ifindex;
  271. if (dst)
  272. memcpy (dst, &pktinfo->ipi6_addr, sizeof (struct in6_addr));
  273. return retval;
  274. }