irdp_main.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. *
  3. * Copyright (C) 2000 Robert Olsson.
  4. * Swedish University of Agricultural Sciences
  5. *
  6. * This file is part of GNU Zebra.
  7. *
  8. * GNU Zebra is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2, or (at your option) any
  11. * later version.
  12. *
  13. * GNU Zebra is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  20. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  21. * 02111-1307, USA.
  22. */
  23. /*
  24. * This work includes work with the following copywrite:
  25. *
  26. * Copyright (C) 1997, 2000 Kunihiro Ishiguro
  27. *
  28. */
  29. /*
  30. * Thanks to Jens Låås at Swedish University of Agricultural Sciences
  31. * for reviewing and tests.
  32. */
  33. #include <zebra.h>
  34. #ifdef HAVE_IRDP
  35. #include "if.h"
  36. #include "vty.h"
  37. #include "sockunion.h"
  38. #include "sockopt.h"
  39. #include "prefix.h"
  40. #include "command.h"
  41. #include "memory.h"
  42. #include "stream.h"
  43. #include "ioctl.h"
  44. #include "connected.h"
  45. #include "log.h"
  46. #include "zclient.h"
  47. #include "thread.h"
  48. #include "privs.h"
  49. #include "zebra/interface.h"
  50. #include "zebra/rtadv.h"
  51. #include "zebra/rib.h"
  52. #include "zebra/zserv.h"
  53. #include "zebra/redistribute.h"
  54. #include "zebra/irdp.h"
  55. #include <netinet/ip_icmp.h>
  56. #include "checksum.h"
  57. #include "if.h"
  58. #include "sockunion.h"
  59. #include "log.h"
  60. /* GLOBAL VARS */
  61. extern struct zebra_privs_t zserv_privs;
  62. /* Master of threads. */
  63. extern struct zebra_t zebrad;
  64. struct thread *t_irdp_raw;
  65. /* Timer interval of irdp. */
  66. int irdp_timer_interval = IRDP_DEFAULT_INTERVAL;
  67. int
  68. irdp_sock_init (void)
  69. {
  70. int ret, i;
  71. int save_errno;
  72. int sock;
  73. if ( zserv_privs.change (ZPRIVS_RAISE) )
  74. zlog_err ("irdp_sock_init: could not raise privs, %s",
  75. safe_strerror (errno) );
  76. sock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
  77. save_errno = errno;
  78. if ( zserv_privs.change (ZPRIVS_LOWER) )
  79. zlog_err ("irdp_sock_init: could not lower privs, %s",
  80. safe_strerror (errno) );
  81. if (sock < 0) {
  82. zlog_warn ("IRDP: can't create irdp socket %s", safe_strerror(save_errno));
  83. return sock;
  84. };
  85. i = 1;
  86. ret = setsockopt (sock, IPPROTO_IP, IP_TTL,
  87. (void *) &i, sizeof (i));
  88. if (ret < 0) {
  89. zlog_warn ("IRDP: can't do irdp sockopt %s", safe_strerror(errno));
  90. close(sock);
  91. return ret;
  92. };
  93. ret = setsockopt_ifindex (AF_INET, sock, 1);
  94. if (ret < 0) {
  95. zlog_warn ("IRDP: can't do irdp sockopt %s", safe_strerror(errno));
  96. close(sock);
  97. return ret;
  98. };
  99. t_irdp_raw = thread_add_read (zebrad.master, irdp_read_raw, NULL, sock);
  100. return sock;
  101. }
  102. static int
  103. get_pref(struct irdp_interface *irdp, struct prefix *p)
  104. {
  105. struct listnode *node;
  106. struct Adv *adv;
  107. /* Use default preference or use the override pref */
  108. if( irdp->AdvPrefList == NULL )
  109. return irdp->Preference;
  110. for (ALL_LIST_ELEMENTS_RO (irdp->AdvPrefList, node, adv))
  111. if( p->u.prefix4.s_addr == adv->ip.s_addr )
  112. return adv->pref;
  113. return irdp->Preference;
  114. }
  115. /* Make ICMP Router Advertisement Message. */
  116. static int
  117. make_advertisement_packet (struct interface *ifp,
  118. struct prefix *p,
  119. struct stream *s)
  120. {
  121. struct zebra_if *zi=ifp->info;
  122. struct irdp_interface *irdp=&zi->irdp;
  123. int size;
  124. int pref;
  125. u_int16_t checksum;
  126. pref = get_pref(irdp, p);
  127. stream_putc (s, ICMP_ROUTERADVERT); /* Type. */
  128. stream_putc (s, 0); /* Code. */
  129. stream_putw (s, 0); /* Checksum. */
  130. stream_putc (s, 1); /* Num address. */
  131. stream_putc (s, 2); /* Address Entry Size. */
  132. if(irdp->flags & IF_SHUTDOWN)
  133. stream_putw (s, 0);
  134. else
  135. stream_putw (s, irdp->Lifetime);
  136. stream_putl (s, htonl(p->u.prefix4.s_addr)); /* Router address. */
  137. stream_putl (s, pref);
  138. /* in_cksum return network byte order value */
  139. size = 16;
  140. checksum = in_cksum (s->data, size);
  141. stream_putw_at (s, 2, htons(checksum));
  142. return size;
  143. }
  144. static void
  145. irdp_send(struct interface *ifp, struct prefix *p, struct stream *s)
  146. {
  147. struct zebra_if *zi=ifp->info;
  148. struct irdp_interface *irdp=&zi->irdp;
  149. char buf[PREFIX_STRLEN];
  150. u_int32_t dst;
  151. u_int32_t ttl=1;
  152. if (! (ifp->flags & IFF_UP)) return;
  153. if (irdp->flags & IF_BROADCAST)
  154. dst =INADDR_BROADCAST ;
  155. else
  156. dst = htonl(INADDR_ALLHOSTS_GROUP);
  157. if(irdp->flags & IF_DEBUG_MESSAGES)
  158. zlog_debug("IRDP: TX Advert on %s %s Holdtime=%d Preference=%d",
  159. ifp->name,
  160. prefix2str(p, buf, sizeof buf),
  161. irdp->flags & IF_SHUTDOWN? 0 : irdp->Lifetime,
  162. get_pref(irdp, p));
  163. send_packet (ifp, s, dst, p, ttl);
  164. }
  165. static void irdp_advertisement (struct interface *ifp, struct prefix *p)
  166. {
  167. struct stream *s;
  168. s = stream_new (128);
  169. make_advertisement_packet (ifp, p, s);
  170. irdp_send(ifp, p, s);
  171. stream_free (s);
  172. }
  173. int irdp_send_thread(struct thread *t_advert)
  174. {
  175. u_int32_t timer, tmp;
  176. struct interface *ifp = THREAD_ARG (t_advert);
  177. struct zebra_if *zi=ifp->info;
  178. struct irdp_interface *irdp=&zi->irdp;
  179. struct prefix *p;
  180. struct listnode *node, *nnode;
  181. struct connected *ifc;
  182. irdp->flags &= ~IF_SOLICIT;
  183. if(ifp->connected)
  184. for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, ifc))
  185. {
  186. p = ifc->address;
  187. if (p->family != AF_INET)
  188. continue;
  189. irdp_advertisement(ifp, p);
  190. irdp->irdp_sent++;
  191. }
  192. tmp = irdp->MaxAdvertInterval-irdp->MinAdvertInterval;
  193. timer = (random () % tmp ) + 1;
  194. timer = irdp->MinAdvertInterval + timer;
  195. if(irdp->irdp_sent < MAX_INITIAL_ADVERTISEMENTS &&
  196. timer > MAX_INITIAL_ADVERT_INTERVAL )
  197. timer= MAX_INITIAL_ADVERT_INTERVAL;
  198. if(irdp->flags & IF_DEBUG_MISC)
  199. zlog_debug("IRDP: New timer for %s set to %u\n", ifp->name, timer);
  200. irdp->t_advertise = thread_add_timer(zebrad.master, irdp_send_thread, ifp, timer);
  201. return 0;
  202. }
  203. void irdp_advert_off(struct interface *ifp)
  204. {
  205. struct zebra_if *zi=ifp->info;
  206. struct irdp_interface *irdp=&zi->irdp;
  207. struct listnode *node, *nnode;
  208. int i;
  209. struct connected *ifc;
  210. struct prefix *p;
  211. if(irdp->t_advertise) thread_cancel(irdp->t_advertise);
  212. irdp->t_advertise = NULL;
  213. if(ifp->connected)
  214. for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, ifc))
  215. {
  216. p = ifc->address;
  217. /* Output some packets with Lifetime 0
  218. we should add a wait...
  219. */
  220. for(i=0; i< IRDP_LAST_ADVERT_MESSAGES; i++)
  221. {
  222. irdp->irdp_sent++;
  223. irdp_advertisement(ifp, p);
  224. }
  225. }
  226. }
  227. void process_solicit (struct interface *ifp)
  228. {
  229. struct zebra_if *zi=ifp->info;
  230. struct irdp_interface *irdp=&zi->irdp;
  231. u_int32_t timer;
  232. /* When SOLICIT is active we reject further incoming solicits
  233. this keeps down the answering rate so we don't have think
  234. about DoS attacks here. */
  235. if( irdp->flags & IF_SOLICIT) return;
  236. irdp->flags |= IF_SOLICIT;
  237. if(irdp->t_advertise) thread_cancel(irdp->t_advertise);
  238. irdp->t_advertise = NULL;
  239. timer = (random () % MAX_RESPONSE_DELAY) + 1;
  240. irdp->t_advertise = thread_add_timer(zebrad.master,
  241. irdp_send_thread,
  242. ifp,
  243. timer);
  244. }
  245. void irdp_finish()
  246. {
  247. struct listnode *node, *nnode;
  248. struct interface *ifp;
  249. struct zebra_if *zi;
  250. struct irdp_interface *irdp;
  251. zlog_info("IRDP: Received shutdown notification.");
  252. for (ALL_LIST_ELEMENTS (iflist, node, nnode, ifp))
  253. {
  254. zi = ifp->info;
  255. if (!zi)
  256. continue;
  257. irdp = &zi->irdp;
  258. if (!irdp)
  259. continue;
  260. if (irdp->flags & IF_ACTIVE )
  261. {
  262. irdp->flags |= IF_SHUTDOWN;
  263. irdp_advert_off(ifp);
  264. }
  265. }
  266. }
  267. #endif /* HAVE_IRDP */