ospf_network.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * OSPF network related functions
  3. * Copyright (C) 1999 Toshiaki Takada
  4. *
  5. * This file is part of GNU Zebra.
  6. *
  7. * GNU Zebra is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2, or (at your option) any
  10. * later version.
  11. *
  12. * GNU Zebra is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  19. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. * 02111-1307, USA.
  21. */
  22. #include <zebra.h>
  23. #include "thread.h"
  24. #include "linklist.h"
  25. #include "prefix.h"
  26. #include "if.h"
  27. #include "sockunion.h"
  28. #include "log.h"
  29. #include "sockopt.h"
  30. #include "privs.h"
  31. extern struct zebra_privs_t ospfd_privs;
  32. #include "ospfd/ospfd.h"
  33. #include "ospfd/ospf_network.h"
  34. #include "ospfd/ospf_interface.h"
  35. #include "ospfd/ospf_asbr.h"
  36. #include "ospfd/ospf_lsa.h"
  37. #include "ospfd/ospf_lsdb.h"
  38. #include "ospfd/ospf_neighbor.h"
  39. #include "ospfd/ospf_packet.h"
  40. #include "ospfd/ospf_dump.h"
  41. /* Join to the OSPF ALL SPF ROUTERS multicast group. */
  42. int
  43. ospf_if_add_allspfrouters (struct ospf *top, struct prefix *p,
  44. ifindex_t ifindex)
  45. {
  46. int ret;
  47. ret = setsockopt_ipv4_multicast (top->fd, IP_ADD_MEMBERSHIP,
  48. htonl (OSPF_ALLSPFROUTERS),
  49. ifindex);
  50. if (ret < 0)
  51. zlog_warn ("can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, "
  52. "ifindex %u, AllSPFRouters): %s; perhaps a kernel limit "
  53. "on # of multicast group memberships has been exceeded?",
  54. top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno));
  55. else
  56. zlog_debug ("interface %s [%u] join AllSPFRouters Multicast group.",
  57. inet_ntoa (p->u.prefix4), ifindex);
  58. return ret;
  59. }
  60. int
  61. ospf_if_drop_allspfrouters (struct ospf *top, struct prefix *p,
  62. ifindex_t ifindex)
  63. {
  64. int ret;
  65. ret = setsockopt_ipv4_multicast (top->fd, IP_DROP_MEMBERSHIP,
  66. htonl (OSPF_ALLSPFROUTERS),
  67. ifindex);
  68. if (ret < 0)
  69. zlog_warn ("can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, "
  70. "ifindex %u, AllSPFRouters): %s",
  71. top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno));
  72. else
  73. zlog_debug ("interface %s [%u] leave AllSPFRouters Multicast group.",
  74. inet_ntoa (p->u.prefix4), ifindex);
  75. return ret;
  76. }
  77. /* Join to the OSPF ALL Designated ROUTERS multicast group. */
  78. int
  79. ospf_if_add_alldrouters (struct ospf *top, struct prefix *p, ifindex_t ifindex)
  80. {
  81. int ret;
  82. ret = setsockopt_ipv4_multicast (top->fd, IP_ADD_MEMBERSHIP,
  83. htonl (OSPF_ALLDROUTERS),
  84. ifindex);
  85. if (ret < 0)
  86. zlog_warn ("can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, "
  87. "ifindex %u, AllDRouters): %s; perhaps a kernel limit "
  88. "on # of multicast group memberships has been exceeded?",
  89. top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno));
  90. else
  91. zlog_debug ("interface %s [%u] join AllDRouters Multicast group.",
  92. inet_ntoa (p->u.prefix4), ifindex);
  93. return ret;
  94. }
  95. int
  96. ospf_if_drop_alldrouters (struct ospf *top, struct prefix *p, ifindex_t ifindex)
  97. {
  98. int ret;
  99. ret = setsockopt_ipv4_multicast (top->fd, IP_DROP_MEMBERSHIP,
  100. htonl (OSPF_ALLDROUTERS),
  101. ifindex);
  102. if (ret < 0)
  103. zlog_warn ("can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, "
  104. "ifindex %u, AllDRouters): %s",
  105. top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno));
  106. else
  107. zlog_debug ("interface %s [%u] leave AllDRouters Multicast group.",
  108. inet_ntoa (p->u.prefix4), ifindex);
  109. return ret;
  110. }
  111. int
  112. ospf_if_ipmulticast (struct ospf *top, struct prefix *p, ifindex_t ifindex)
  113. {
  114. u_char val;
  115. int ret, len;
  116. val = 0;
  117. len = sizeof (val);
  118. /* Prevent receiving self-origined multicast packets. */
  119. ret = setsockopt (top->fd, IPPROTO_IP, IP_MULTICAST_LOOP, (void *)&val, len);
  120. if (ret < 0)
  121. zlog_warn ("can't setsockopt IP_MULTICAST_LOOP(0) for fd %d: %s",
  122. top->fd, safe_strerror(errno));
  123. /* Explicitly set multicast ttl to 1 -- endo. */
  124. val = 1;
  125. ret = setsockopt (top->fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&val, len);
  126. if (ret < 0)
  127. zlog_warn ("can't setsockopt IP_MULTICAST_TTL(1) for fd %d: %s",
  128. top->fd, safe_strerror (errno));
  129. ret = setsockopt_ipv4_multicast_if (top->fd, ifindex);
  130. if (ret < 0)
  131. zlog_warn("can't setsockopt IP_MULTICAST_IF(fd %d, addr %s, "
  132. "ifindex %u): %s",
  133. top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno));
  134. return ret;
  135. }
  136. int
  137. ospf_sock_init (void)
  138. {
  139. int ospf_sock;
  140. int ret, hincl = 1;
  141. if ( ospfd_privs.change (ZPRIVS_RAISE) )
  142. zlog_err ("ospf_sock_init: could not raise privs, %s",
  143. safe_strerror (errno) );
  144. ospf_sock = socket (AF_INET, SOCK_RAW, IPPROTO_OSPFIGP);
  145. if (ospf_sock < 0)
  146. {
  147. int save_errno = errno;
  148. if ( ospfd_privs.change (ZPRIVS_LOWER) )
  149. zlog_err ("ospf_sock_init: could not lower privs, %s",
  150. safe_strerror (errno) );
  151. zlog_err ("ospf_read_sock_init: socket: %s", safe_strerror (save_errno));
  152. exit(1);
  153. }
  154. #ifdef IP_HDRINCL
  155. /* we will include IP header with packet */
  156. ret = setsockopt (ospf_sock, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof (hincl));
  157. if (ret < 0)
  158. {
  159. int save_errno = errno;
  160. if ( ospfd_privs.change (ZPRIVS_LOWER) )
  161. zlog_err ("ospf_sock_init: could not lower privs, %s",
  162. safe_strerror (errno) );
  163. zlog_warn ("Can't set IP_HDRINCL option for fd %d: %s",
  164. ospf_sock, safe_strerror(save_errno));
  165. }
  166. #elif defined (IPTOS_PREC_INTERNETCONTROL)
  167. #warning "IP_HDRINCL not available on this system"
  168. #warning "using IPTOS_PREC_INTERNETCONTROL"
  169. ret = setsockopt_ipv4_tos(ospf_sock, IPTOS_PREC_INTERNETCONTROL);
  170. if (ret < 0)
  171. {
  172. int save_errno = errno;
  173. if ( ospfd_privs.change (ZPRIVS_LOWER) )
  174. zlog_err ("ospf_sock_init: could not lower privs, %s",
  175. safe_strerror (errno) );
  176. zlog_warn ("can't set sockopt IP_TOS %d to socket %d: %s",
  177. tos, ospf_sock, safe_strerror(save_errno));
  178. close (ospf_sock); /* Prevent sd leak. */
  179. return ret;
  180. }
  181. #else /* !IPTOS_PREC_INTERNETCONTROL */
  182. #warning "IP_HDRINCL not available, nor is IPTOS_PREC_INTERNETCONTROL"
  183. zlog_warn ("IP_HDRINCL option not available");
  184. #endif /* IP_HDRINCL */
  185. ret = setsockopt_ifindex (AF_INET, ospf_sock, 1);
  186. if (ret < 0)
  187. zlog_warn ("Can't set pktinfo option for fd %d", ospf_sock);
  188. if (ospfd_privs.change (ZPRIVS_LOWER))
  189. {
  190. zlog_err ("ospf_sock_init: could not lower privs, %s",
  191. safe_strerror (errno) );
  192. }
  193. return ospf_sock;
  194. }
  195. void
  196. ospf_adjust_sndbuflen (struct ospf * ospf, unsigned int buflen)
  197. {
  198. int ret, newbuflen;
  199. /* Check if any work has to be done at all. */
  200. if (ospf->maxsndbuflen >= buflen)
  201. return;
  202. if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
  203. zlog_debug ("%s: adjusting OSPF send buffer size to %d",
  204. __func__, buflen);
  205. if (ospfd_privs.change (ZPRIVS_RAISE))
  206. zlog_err ("%s: could not raise privs, %s", __func__,
  207. safe_strerror (errno));
  208. /* Now we try to set SO_SNDBUF to what our caller has requested
  209. * (the MTU of a newly added interface). However, if the OS has
  210. * truncated the actual buffer size to somewhat less size, try
  211. * to detect it and update our records appropriately. The OS
  212. * may allocate more buffer space, than requested, this isn't
  213. * a error.
  214. */
  215. ret = setsockopt_so_sendbuf (ospf->fd, buflen);
  216. newbuflen = getsockopt_so_sendbuf (ospf->fd);
  217. if (ret < 0 || newbuflen < 0 || newbuflen < (int) buflen)
  218. zlog_warn ("%s: tried to set SO_SNDBUF to %u, but got %d",
  219. __func__, buflen, newbuflen);
  220. if (newbuflen >= 0)
  221. ospf->maxsndbuflen = (unsigned int)newbuflen;
  222. else
  223. zlog_warn ("%s: failed to get SO_SNDBUF", __func__);
  224. if (ospfd_privs.change (ZPRIVS_LOWER))
  225. zlog_err ("%s: could not lower privs, %s", __func__,
  226. safe_strerror (errno));
  227. }