ospf_network.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. unsigned int 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. unsigned int 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, unsigned int
  80. ifindex)
  81. {
  82. int ret;
  83. ret = setsockopt_ipv4_multicast (top->fd, IP_ADD_MEMBERSHIP,
  84. htonl (OSPF_ALLDROUTERS),
  85. ifindex);
  86. if (ret < 0)
  87. zlog_warn ("can't setsockopt IP_ADD_MEMBERSHIP (fd %d, addr %s, "
  88. "ifindex %u, AllDRouters): %s; perhaps a kernel limit "
  89. "on # of multicast group memberships has been exceeded?",
  90. top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno));
  91. else
  92. zlog_debug ("interface %s [%u] join AllDRouters Multicast group.",
  93. inet_ntoa (p->u.prefix4), ifindex);
  94. return ret;
  95. }
  96. int
  97. ospf_if_drop_alldrouters (struct ospf *top, struct prefix *p, unsigned int
  98. ifindex)
  99. {
  100. int ret;
  101. ret = setsockopt_ipv4_multicast (top->fd, IP_DROP_MEMBERSHIP,
  102. htonl (OSPF_ALLDROUTERS),
  103. ifindex);
  104. if (ret < 0)
  105. zlog_warn ("can't setsockopt IP_DROP_MEMBERSHIP (fd %d, addr %s, "
  106. "ifindex %u, AllDRouters): %s",
  107. top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno));
  108. else
  109. zlog_debug ("interface %s [%u] leave AllDRouters Multicast group.",
  110. inet_ntoa (p->u.prefix4), ifindex);
  111. return ret;
  112. }
  113. int
  114. ospf_if_ipmulticast (struct ospf *top, struct prefix *p, unsigned int ifindex)
  115. {
  116. u_char val;
  117. int ret, len;
  118. val = 0;
  119. len = sizeof (val);
  120. /* Prevent receiving self-origined multicast packets. */
  121. ret = setsockopt (top->fd, IPPROTO_IP, IP_MULTICAST_LOOP, (void *)&val, len);
  122. if (ret < 0)
  123. zlog_warn ("can't setsockopt IP_MULTICAST_LOOP(0) for fd %d: %s",
  124. top->fd, safe_strerror(errno));
  125. /* Explicitly set multicast ttl to 1 -- endo. */
  126. val = 1;
  127. ret = setsockopt (top->fd, IPPROTO_IP, IP_MULTICAST_TTL, (void *)&val, len);
  128. if (ret < 0)
  129. zlog_warn ("can't setsockopt IP_MULTICAST_TTL(1) for fd %d: %s",
  130. top->fd, safe_strerror (errno));
  131. ret = setsockopt_ipv4_multicast_if (top->fd, ifindex);
  132. if (ret < 0)
  133. zlog_warn("can't setsockopt IP_MULTICAST_IF(fd %d, addr %s, "
  134. "ifindex %u): %s",
  135. top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno));
  136. return ret;
  137. }
  138. int
  139. ospf_sock_init (void)
  140. {
  141. int ospf_sock;
  142. int ret, hincl = 1;
  143. if ( ospfd_privs.change (ZPRIVS_RAISE) )
  144. zlog_err ("ospf_sock_init: could not raise privs, %s",
  145. safe_strerror (errno) );
  146. ospf_sock = socket (AF_INET, SOCK_RAW, IPPROTO_OSPFIGP);
  147. if (ospf_sock < 0)
  148. {
  149. int save_errno = errno;
  150. if ( ospfd_privs.change (ZPRIVS_LOWER) )
  151. zlog_err ("ospf_sock_init: could not lower privs, %s",
  152. safe_strerror (errno) );
  153. zlog_err ("ospf_read_sock_init: socket: %s", safe_strerror (save_errno));
  154. exit(1);
  155. }
  156. #ifdef IP_HDRINCL
  157. /* we will include IP header with packet */
  158. ret = setsockopt (ospf_sock, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof (hincl));
  159. if (ret < 0)
  160. {
  161. int save_errno = errno;
  162. if ( ospfd_privs.change (ZPRIVS_LOWER) )
  163. zlog_err ("ospf_sock_init: could not lower privs, %s",
  164. safe_strerror (errno) );
  165. zlog_warn ("Can't set IP_HDRINCL option for fd %d: %s",
  166. ospf_sock, safe_strerror(save_errno));
  167. }
  168. #elif defined (IPTOS_PREC_INTERNETCONTROL)
  169. #warning "IP_HDRINCL not available on this system"
  170. #warning "using IPTOS_PREC_INTERNETCONTROL"
  171. ret = setsockopt_ipv4_tos(ospf_sock, IPTOS_PREC_INTERNETCONTROL);
  172. if (ret < 0)
  173. {
  174. int save_errno = errno;
  175. if ( ospfd_privs.change (ZPRIVS_LOWER) )
  176. zlog_err ("ospf_sock_init: could not lower privs, %s",
  177. safe_strerror (errno) );
  178. zlog_warn ("can't set sockopt IP_TOS %d to socket %d: %s",
  179. tos, ospf_sock, safe_strerror(save_errno));
  180. close (ospf_sock); /* Prevent sd leak. */
  181. return ret;
  182. }
  183. #else /* !IPTOS_PREC_INTERNETCONTROL */
  184. #warning "IP_HDRINCL not available, nor is IPTOS_PREC_INTERNETCONTROL"
  185. zlog_warn ("IP_HDRINCL option not available");
  186. #endif /* IP_HDRINCL */
  187. ret = setsockopt_ifindex (AF_INET, ospf_sock, 1);
  188. if (ret < 0)
  189. zlog_warn ("Can't set pktinfo option for fd %d", ospf_sock);
  190. if (ospfd_privs.change (ZPRIVS_LOWER))
  191. {
  192. zlog_err ("ospf_sock_init: could not lower privs, %s",
  193. safe_strerror (errno) );
  194. }
  195. return ospf_sock;
  196. }
  197. void
  198. ospf_adjust_sndbuflen (struct ospf * ospf, unsigned int buflen)
  199. {
  200. int ret, newbuflen;
  201. /* Check if any work has to be done at all. */
  202. if (ospf->maxsndbuflen >= buflen)
  203. return;
  204. if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
  205. zlog_debug ("%s: adjusting OSPF send buffer size to %d",
  206. __func__, buflen);
  207. if (ospfd_privs.change (ZPRIVS_RAISE))
  208. zlog_err ("%s: could not raise privs, %s", __func__,
  209. safe_strerror (errno));
  210. /* Now we try to set SO_SNDBUF to what our caller has requested
  211. * (the MTU of a newly added interface). However, if the OS has
  212. * truncated the actual buffer size to somewhat less size, try
  213. * to detect it and update our records appropriately. The OS
  214. * may allocate more buffer space, than requested, this isn't
  215. * a error.
  216. */
  217. ret = setsockopt_so_sendbuf (ospf->fd, buflen);
  218. newbuflen = getsockopt_so_sendbuf (ospf->fd);
  219. if (ret < 0 || newbuflen < 0 || newbuflen < (int) buflen)
  220. zlog_warn ("%s: tried to set SO_SNDBUF to %u, but got %d",
  221. __func__, buflen, newbuflen);
  222. if (newbuflen >= 0)
  223. ospf->maxsndbuflen = (unsigned int)newbuflen;
  224. else
  225. zlog_warn ("%s: failed to get SO_SNDBUF", __func__);
  226. if (ospfd_privs.change (ZPRIVS_LOWER))
  227. zlog_err ("%s: could not lower privs, %s", __func__,
  228. safe_strerror (errno));
  229. }