ospf_network.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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_multicast_ipv4 (top->fd, IP_ADD_MEMBERSHIP,
  48. p->u.prefix4, 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_info ("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_multicast_ipv4 (top->fd, IP_DROP_MEMBERSHIP,
  66. p->u.prefix4, 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_info ("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_multicast_ipv4 (top->fd, IP_ADD_MEMBERSHIP,
  84. p->u.prefix4, 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_info ("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_multicast_ipv4 (top->fd, IP_DROP_MEMBERSHIP,
  102. p->u.prefix4, 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_info ("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_multicast_ipv4 (top->fd, IP_MULTICAST_IF,
  132. p->u.prefix4, 0, ifindex);
  133. if (ret < 0)
  134. zlog_warn("can't setsockopt IP_MULTICAST_IF(fd %d, addr %s, "
  135. "ifindex %u): %s",
  136. top->fd, inet_ntoa(p->u.prefix4), ifindex, safe_strerror(errno));
  137. return ret;
  138. }
  139. int
  140. ospf_sock_init (void)
  141. {
  142. int ospf_sock;
  143. int ret, hincl = 1;
  144. if ( ospfd_privs.change (ZPRIVS_RAISE) )
  145. zlog_err ("ospf_sock_init: could not raise privs, %s",
  146. safe_strerror (errno) );
  147. ospf_sock = socket (AF_INET, SOCK_RAW, IPPROTO_OSPFIGP);
  148. if (ospf_sock < 0)
  149. {
  150. int save_errno = errno;
  151. if ( ospfd_privs.change (ZPRIVS_LOWER) )
  152. zlog_err ("ospf_sock_init: could not lower privs, %s",
  153. safe_strerror (errno) );
  154. zlog_err ("ospf_read_sock_init: socket: %s", safe_strerror (save_errno));
  155. exit(1);
  156. }
  157. #ifdef IP_HDRINCL
  158. /* we will include IP header with packet */
  159. ret = setsockopt (ospf_sock, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof (hincl));
  160. if (ret < 0)
  161. {
  162. int save_errno = errno;
  163. if ( ospfd_privs.change (ZPRIVS_LOWER) )
  164. zlog_err ("ospf_sock_init: could not lower privs, %s",
  165. safe_strerror (errno) );
  166. zlog_warn ("Can't set IP_HDRINCL option for fd %d: %s",
  167. ospf_sock, safe_strerror(save_errno));
  168. }
  169. #elif defined (IPTOS_PREC_INTERNETCONTROL)
  170. #warning "IP_HDRINCL not available on this system"
  171. #warning "using IPTOS_PREC_INTERNETCONTROL"
  172. ret = setsockopt_ipv4_tos(ospf_sock, IPTOS_PREC_INTERNETCONTROL);
  173. if (ret < 0)
  174. {
  175. int save_errno = errno;
  176. if ( ospfd_privs.change (ZPRIVS_LOWER) )
  177. zlog_err ("ospf_sock_init: could not lower privs, %s",
  178. safe_strerror (errno) );
  179. zlog_warn ("can't set sockopt IP_TOS %d to socket %d: %s",
  180. tos, ospf_sock, safe_strerror(save_errno));
  181. close (ospf_sock); /* Prevent sd leak. */
  182. return ret;
  183. }
  184. #else /* !IPTOS_PREC_INTERNETCONTROL */
  185. #warning "IP_HDRINCL not available, nor is IPTOS_PREC_INTERNETCONTROL"
  186. zlog_warn ("IP_HDRINCL option not available");
  187. #endif /* IP_HDRINCL */
  188. ret = setsockopt_ifindex (AF_INET, ospf_sock, 1);
  189. if (ret < 0)
  190. zlog_warn ("Can't set pktinfo option for fd %d", ospf_sock);
  191. if (ospfd_privs.change (ZPRIVS_LOWER))
  192. {
  193. zlog_err ("ospf_sock_init: could not lower privs, %s",
  194. safe_strerror (errno) );
  195. }
  196. return ospf_sock;
  197. }
  198. void
  199. ospf_adjust_sndbuflen (struct ospf * ospf, int buflen)
  200. {
  201. int ret, newbuflen;
  202. /* Check if any work has to be done at all. */
  203. if (ospf->maxsndbuflen >= buflen)
  204. return;
  205. if (IS_DEBUG_OSPF (zebra, ZEBRA_INTERFACE))
  206. zlog_debug ("%s: adjusting OSPF send buffer size to %d",
  207. __func__, buflen);
  208. if (ospfd_privs.change (ZPRIVS_RAISE))
  209. zlog_err ("%s: could not raise privs, %s", __func__,
  210. safe_strerror (errno));
  211. /* Now we try to set SO_SNDBUF to what our caller has requested
  212. * (the MTU of a newly added interface). However, if the OS has
  213. * truncated the actual buffer size to somewhat less size, try
  214. * to detect it and update our records appropriately. The OS
  215. * may allocate more buffer space, than requested, this isn't
  216. * a error.
  217. */
  218. ret = setsockopt_so_sendbuf (ospf->fd, buflen);
  219. newbuflen = getsockopt_so_sendbuf (ospf->fd);
  220. if (ret < 0 || newbuflen < buflen)
  221. zlog_warn ("%s: tried to set SO_SNDBUF to %d, but got %d",
  222. __func__, buflen, newbuflen);
  223. if (newbuflen >= 0)
  224. ospf->maxsndbuflen = newbuflen;
  225. else
  226. zlog_warn ("%s: failed to get SO_SNDBUF", __func__);
  227. if (ospfd_privs.change (ZPRIVS_LOWER))
  228. zlog_err ("%s: could not lower privs, %s", __func__,
  229. safe_strerror (errno));
  230. }