irdp_main.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 "prefix.h"
  39. #include "command.h"
  40. #include "memory.h"
  41. #include "stream.h"
  42. #include "ioctl.h"
  43. #include "connected.h"
  44. #include "log.h"
  45. #include "zclient.h"
  46. #include "thread.h"
  47. #include "privs.h"
  48. #include "zebra/interface.h"
  49. #include "zebra/rtadv.h"
  50. #include "zebra/rib.h"
  51. #include "zebra/zserv.h"
  52. #include "zebra/redistribute.h"
  53. #include "zebra/irdp.h"
  54. #include <netinet/ip_icmp.h>
  55. #include "if.h"
  56. #include "sockunion.h"
  57. #include "log.h"
  58. /* GLOBAL VARS */
  59. extern struct zebra_privs_t zserv_privs;
  60. /* Master of threads. */
  61. extern struct zebra_t zebrad;
  62. struct thread *t_irdp_raw;
  63. /* Timer interval of irdp. */
  64. int irdp_timer_interval = IRDP_DEFAULT_INTERVAL;
  65. int irdp_read_raw(struct thread *r);
  66. int in_cksum (void *ptr, int nbytes);
  67. extern int irdp_sock;
  68. void send_packet(struct interface *ifp,
  69. struct stream *s,
  70. u_int32_t dst,
  71. struct prefix *p,
  72. u_int32_t ttl);
  73. void irdp_if_init ();
  74. char *
  75. inet_2a(u_int32_t a, char *b)
  76. {
  77. sprintf(b, "%u.%u.%u.%u",
  78. (a ) & 0xFF,
  79. (a>> 8) & 0xFF,
  80. (a>>16) & 0xFF,
  81. (a>>24) & 0xFF);
  82. return b;
  83. }
  84. int
  85. irdp_sock_init (void)
  86. {
  87. int ret, i;
  88. if ( zserv_privs.change (ZPRIVS_RAISE) )
  89. zlog_err ("irdp_sock_init: could not raise privs, %s",
  90. strerror (errno) );
  91. irdp_sock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
  92. if ( zserv_privs.change (ZPRIVS_LOWER) )
  93. zlog_err ("irdp_sock_init: could not lower privs, %s",
  94. strerror (errno) );
  95. if (irdp_sock < 0) {
  96. zlog_warn ("IRDP: can't create irdp socket %s", strerror(errno));
  97. return irdp_sock;
  98. };
  99. i = 1;
  100. ret = setsockopt (irdp_sock, IPPROTO_IP, IP_TTL,
  101. (void *) &i, sizeof (i));
  102. if (ret < 0) {
  103. zlog_warn ("IRDP: can't do irdp sockopt %s", strerror(errno));
  104. return ret;
  105. };
  106. ret = setsockopt_pktinfo_ipv4 (irdp_sock, 1);
  107. if (ret < 0) {
  108. zlog_warn ("IRDP: can't do irdp sockopt %s", strerror(errno));
  109. return ret;
  110. };
  111. t_irdp_raw = thread_add_read (zebrad.master, irdp_read_raw, NULL, irdp_sock);
  112. return irdp_sock;
  113. }
  114. int get_pref(struct irdp_interface *irdp, struct prefix *p)
  115. {
  116. listnode node;
  117. struct Adv *adv;
  118. /* Use default preference or use the override pref */
  119. if( irdp->AdvPrefList == NULL ) return irdp->Preference;
  120. for (node = listhead (irdp->AdvPrefList); node; nextnode (node)) {
  121. adv = getdata (node);
  122. if( p->u.prefix4.s_addr == adv->ip.s_addr )
  123. return adv->pref;
  124. }
  125. return irdp->Preference;
  126. }
  127. /* Make ICMP Router Advertisement Message. */
  128. int make_advertisement_packet (struct interface *ifp,
  129. struct prefix *p,
  130. struct stream *s)
  131. {
  132. struct zebra_if *zi=ifp->info;
  133. struct irdp_interface *irdp=&zi->irdp;
  134. int size;
  135. int pref;
  136. u_int16_t checksum;
  137. pref = get_pref(irdp, p);
  138. stream_putc (s, ICMP_ROUTERADVERT); /* Type. */
  139. stream_putc (s, 0); /* Code. */
  140. stream_putw (s, 0); /* Checksum. */
  141. stream_putc (s, 1); /* Num address. */
  142. stream_putc (s, 2); /* Address Entry Size. */
  143. if(irdp->flags & IF_SHUTDOWN)
  144. stream_putw (s, 0);
  145. else
  146. stream_putw (s, irdp->Lifetime);
  147. stream_putl (s, htonl(p->u.prefix4.s_addr)); /* Router address. */
  148. stream_putl (s, pref);
  149. /* in_cksum return network byte order value */
  150. size = 16;
  151. checksum = in_cksum (s->data, size);
  152. stream_putw_at (s, 2, htons(checksum));
  153. return size;
  154. }
  155. void irdp_send(struct interface *ifp,
  156. struct prefix *p,
  157. struct stream *s)
  158. {
  159. struct zebra_if *zi=ifp->info;
  160. struct irdp_interface *irdp=&zi->irdp;
  161. u_int32_t dst;
  162. u_int32_t ttl=1;
  163. if (! (ifp->flags & IFF_UP)) return;
  164. if (irdp->flags & IF_BROADCAST)
  165. dst =INADDR_BROADCAST ;
  166. else
  167. dst = htonl(INADDR_ALLHOSTS_GROUP);
  168. if(irdp->flags & IF_DEBUG_MESSAGES)
  169. zlog_warn("IRDP: TX Advert on %s %s/%d Holdtime=%d Preference=%d",
  170. ifp->name,
  171. inet_ntoa(p->u.prefix4),
  172. p->prefixlen,
  173. irdp->flags & IF_SHUTDOWN? 0 : irdp->Lifetime,
  174. get_pref(irdp, p));
  175. send_packet (ifp, s, dst, p, ttl);
  176. }
  177. void irdp_advertisement (struct interface *ifp,
  178. struct prefix *p)
  179. {
  180. struct stream *s;
  181. s = stream_new (128);
  182. make_advertisement_packet (ifp, p, s);
  183. irdp_send(ifp, p, s);
  184. }
  185. int irdp_send_thread(struct thread *t_advert)
  186. {
  187. u_int32_t timer, tmp;
  188. struct interface *ifp = THREAD_ARG (t_advert);
  189. struct zebra_if *zi=ifp->info;
  190. struct irdp_interface *irdp=&zi->irdp;
  191. struct prefix *p;
  192. listnode node;
  193. struct connected *ifc;
  194. irdp->flags &= ~IF_SOLICIT;
  195. if(ifp->connected)
  196. for (node = listhead (ifp->connected); node; nextnode (node)) {
  197. ifc = getdata (node);
  198. p = ifc->address;
  199. irdp_advertisement(ifp, p);
  200. irdp->irdp_sent++;
  201. }
  202. tmp = irdp->MaxAdvertInterval-irdp->MinAdvertInterval;
  203. timer = (random () % tmp ) + 1;
  204. timer = irdp->MinAdvertInterval + timer;
  205. if(irdp->irdp_sent < MAX_INITIAL_ADVERTISEMENTS &&
  206. timer > MAX_INITIAL_ADVERT_INTERVAL )
  207. timer= MAX_INITIAL_ADVERT_INTERVAL;
  208. if(irdp->flags & IF_DEBUG_MISC)
  209. zlog_warn("IRDP: New timer for %s set to %u\n", ifp->name, timer);
  210. irdp->t_advertise = thread_add_timer(zebrad.master, irdp_send_thread, ifp, timer);
  211. return 0;
  212. }
  213. void irdp_advert_off(struct interface *ifp)
  214. {
  215. struct zebra_if *zi=ifp->info;
  216. struct irdp_interface *irdp=&zi->irdp;
  217. listnode node;
  218. int i;
  219. struct connected *ifc;
  220. struct prefix *p;
  221. if(irdp->t_advertise) thread_cancel(irdp->t_advertise);
  222. irdp->t_advertise = NULL;
  223. if(ifp->connected)
  224. for (node = listhead (ifp->connected); node; nextnode (node)) {
  225. ifc = getdata (node);
  226. p = ifc->address;
  227. /* Output some packets with Lifetime 0
  228. we should add a wait...
  229. */
  230. for(i=0; i< IRDP_LAST_ADVERT_MESSAGES; i++) {
  231. irdp->irdp_sent++;
  232. irdp_advertisement(ifp, p);
  233. }
  234. }
  235. }
  236. void process_solicit (struct interface *ifp)
  237. {
  238. struct zebra_if *zi=ifp->info;
  239. struct irdp_interface *irdp=&zi->irdp;
  240. u_int32_t timer;
  241. /* When SOLICIT is active we reject further incoming solicits
  242. this keeps down the answering rate so we don't have think
  243. about DoS attacks here. */
  244. if( irdp->flags & IF_SOLICIT) return;
  245. irdp->flags |= IF_SOLICIT;
  246. if(irdp->t_advertise) thread_cancel(irdp->t_advertise);
  247. irdp->t_advertise = NULL;
  248. timer = (random () % MAX_RESPONSE_DELAY) + 1;
  249. irdp->t_advertise = thread_add_timer(zebrad.master,
  250. irdp_send_thread,
  251. ifp,
  252. timer);
  253. }
  254. void irdp_finish()
  255. {
  256. listnode node;
  257. struct interface *ifp;
  258. struct zebra_if *zi;
  259. struct irdp_interface *irdp;
  260. zlog_warn("IRDP: Received shutdown notification.");
  261. for (node = listhead (iflist); node; node = nextnode (node))
  262. {
  263. ifp = getdata(node);
  264. zi= ifp->info;
  265. if(! zi) continue;
  266. irdp = &zi->irdp;
  267. if(!irdp) continue;
  268. if(irdp->flags & IF_ACTIVE ) {
  269. irdp->flags |= IF_SHUTDOWN;
  270. irdp_advert_off(ifp);
  271. }
  272. }
  273. }
  274. void irdp_init()
  275. {
  276. irdp_sock_init();
  277. irdp_if_init ();
  278. }
  279. #endif /* HAVE_IRDP */