hasso | ca77698 | 2004-06-12 14:33:05 +0000 | [diff] [blame] | 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 | /* |
| 25 | * This work includes work with the following copywrite: |
| 26 | * |
| 27 | * Copyright (C) 1997, 2000 Kunihiro Ishiguro |
| 28 | * |
| 29 | */ |
| 30 | |
| 31 | /* |
| 32 | * Thanks to Jens Låås at Swedish University of Agricultural Sciences |
| 33 | * for reviewing and tests. |
| 34 | */ |
| 35 | |
| 36 | |
| 37 | #include <zebra.h> |
| 38 | |
| 39 | #ifdef HAVE_IRDP |
| 40 | |
| 41 | #include "if.h" |
| 42 | #include "vty.h" |
| 43 | #include "sockunion.h" |
| 44 | #include "prefix.h" |
| 45 | #include "command.h" |
| 46 | #include "memory.h" |
| 47 | #include "stream.h" |
| 48 | #include "ioctl.h" |
| 49 | #include "connected.h" |
| 50 | #include "log.h" |
| 51 | #include "zclient.h" |
| 52 | #include "thread.h" |
hasso | 25dac85 | 2004-07-13 03:06:51 +0000 | [diff] [blame] | 53 | #include "privs.h" |
hasso | ca77698 | 2004-06-12 14:33:05 +0000 | [diff] [blame] | 54 | #include "zebra/interface.h" |
| 55 | #include "zebra/rtadv.h" |
| 56 | #include "zebra/rib.h" |
| 57 | #include "zebra/zserv.h" |
| 58 | #include "zebra/redistribute.h" |
| 59 | #include "zebra/irdp.h" |
| 60 | #include <netinet/ip_icmp.h> |
| 61 | |
| 62 | #include "if.h" |
| 63 | #include "sockunion.h" |
| 64 | #include "log.h" |
| 65 | |
| 66 | /* GLOBAL VARS */ |
| 67 | |
hasso | 25dac85 | 2004-07-13 03:06:51 +0000 | [diff] [blame] | 68 | extern struct zebra_privs_t zserv_privs; |
| 69 | |
hasso | ca77698 | 2004-06-12 14:33:05 +0000 | [diff] [blame] | 70 | /* Master of threads. */ |
| 71 | extern struct zebra_t zebrad; |
| 72 | struct thread *t_irdp_raw; |
| 73 | |
| 74 | /* Timer interval of irdp. */ |
| 75 | int irdp_timer_interval = IRDP_DEFAULT_INTERVAL; |
| 76 | |
| 77 | int irdp_read_raw(struct thread *r); |
| 78 | int in_cksum (void *ptr, int nbytes); |
| 79 | extern int irdp_sock; |
| 80 | void send_packet(struct interface *ifp, |
| 81 | struct stream *s, |
| 82 | u_int32_t dst, |
| 83 | struct prefix *p, |
| 84 | u_int32_t ttl); |
| 85 | |
| 86 | void irdp_if_init (); |
| 87 | |
| 88 | char * |
| 89 | inet_2a(u_int32_t a, char *b) |
| 90 | { |
| 91 | sprintf(b, "%u.%u.%u.%u", |
| 92 | (a ) & 0xFF, |
| 93 | (a>> 8) & 0xFF, |
| 94 | (a>>16) & 0xFF, |
| 95 | (a>>24) & 0xFF); |
| 96 | return b; |
| 97 | } |
| 98 | |
| 99 | int |
| 100 | irdp_sock_init (void) |
| 101 | { |
| 102 | int ret, i; |
| 103 | |
hasso | 25dac85 | 2004-07-13 03:06:51 +0000 | [diff] [blame] | 104 | if ( zserv_privs.change (ZPRIVS_RAISE) ) |
| 105 | zlog_err ("irdp_sock_init: could not raise privs, %s", |
| 106 | strerror (errno) ); |
| 107 | |
hasso | ca77698 | 2004-06-12 14:33:05 +0000 | [diff] [blame] | 108 | irdp_sock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP); |
hasso | 25dac85 | 2004-07-13 03:06:51 +0000 | [diff] [blame] | 109 | |
| 110 | if ( zserv_privs.change (ZPRIVS_LOWER) ) |
| 111 | zlog_err ("irdp_sock_init: could not lower privs, %s", |
| 112 | strerror (errno) ); |
| 113 | |
hasso | ca77698 | 2004-06-12 14:33:05 +0000 | [diff] [blame] | 114 | if (irdp_sock < 0) { |
| 115 | zlog_warn ("IRDP: can't create irdp socket %s", strerror(errno)); |
| 116 | return irdp_sock; |
| 117 | }; |
| 118 | |
| 119 | i = 1; |
| 120 | ret = setsockopt (irdp_sock, IPPROTO_IP, IP_TTL, |
| 121 | (void *) &i, sizeof (i)); |
| 122 | if (ret < 0) { |
| 123 | zlog_warn ("IRDP: can't do irdp sockopt %s", strerror(errno)); |
| 124 | return ret; |
| 125 | }; |
| 126 | |
paul | 0de1cde | 2004-08-19 04:45:33 +0000 | [diff] [blame^] | 127 | ret = setsockopt_pktinfo (AF_INET, irdp_sock, 1); |
hasso | ca77698 | 2004-06-12 14:33:05 +0000 | [diff] [blame] | 128 | if (ret < 0) { |
| 129 | zlog_warn ("IRDP: can't do irdp sockopt %s", strerror(errno)); |
| 130 | return ret; |
| 131 | }; |
| 132 | |
| 133 | t_irdp_raw = thread_add_read (zebrad.master, irdp_read_raw, NULL, irdp_sock); |
| 134 | |
| 135 | return irdp_sock; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | int get_pref(struct irdp_interface *irdp, struct prefix *p) |
| 140 | { |
| 141 | listnode node; |
| 142 | struct Adv *adv; |
| 143 | |
| 144 | /* Use default preference or use the override pref */ |
| 145 | |
| 146 | if( irdp->AdvPrefList == NULL ) return irdp->Preference; |
| 147 | |
| 148 | for (node = listhead (irdp->AdvPrefList); node; nextnode (node)) { |
| 149 | adv = getdata (node); |
| 150 | if( p->u.prefix4.s_addr == adv->ip.s_addr ) |
| 151 | return adv->pref; |
| 152 | } |
| 153 | return irdp->Preference; |
| 154 | } |
| 155 | |
| 156 | /* Make ICMP Router Advertisement Message. */ |
| 157 | int make_advertisement_packet (struct interface *ifp, |
| 158 | struct prefix *p, |
| 159 | struct stream *s) |
| 160 | { |
| 161 | struct zebra_if *zi=ifp->info; |
| 162 | struct irdp_interface *irdp=&zi->irdp; |
| 163 | int size; |
| 164 | int pref; |
| 165 | u_int16_t checksum; |
| 166 | |
| 167 | pref = get_pref(irdp, p); |
| 168 | |
| 169 | stream_putc (s, ICMP_ROUTERADVERT); /* Type. */ |
| 170 | stream_putc (s, 0); /* Code. */ |
| 171 | stream_putw (s, 0); /* Checksum. */ |
| 172 | stream_putc (s, 1); /* Num address. */ |
| 173 | stream_putc (s, 2); /* Address Entry Size. */ |
| 174 | |
| 175 | if(irdp->flags & IF_SHUTDOWN) |
| 176 | stream_putw (s, 0); |
| 177 | else |
| 178 | stream_putw (s, irdp->Lifetime); |
| 179 | |
| 180 | stream_putl (s, htonl(p->u.prefix4.s_addr)); /* Router address. */ |
| 181 | stream_putl (s, pref); |
| 182 | |
| 183 | /* in_cksum return network byte order value */ |
| 184 | size = 16; |
| 185 | checksum = in_cksum (s->data, size); |
| 186 | stream_putw_at (s, 2, htons(checksum)); |
| 187 | |
| 188 | return size; |
| 189 | } |
| 190 | |
| 191 | void irdp_send(struct interface *ifp, |
| 192 | struct prefix *p, |
| 193 | struct stream *s) |
| 194 | { |
| 195 | struct zebra_if *zi=ifp->info; |
| 196 | struct irdp_interface *irdp=&zi->irdp; |
| 197 | u_int32_t dst; |
| 198 | u_int32_t ttl=1; |
| 199 | |
| 200 | if (! (ifp->flags & IFF_UP)) return; |
| 201 | |
| 202 | if (irdp->flags & IF_BROADCAST) |
| 203 | dst =INADDR_BROADCAST ; |
| 204 | else |
| 205 | dst = htonl(INADDR_ALLHOSTS_GROUP); |
| 206 | |
| 207 | if(irdp->flags & IF_DEBUG_MESSAGES) |
| 208 | zlog_warn("IRDP: TX Advert on %s %s/%d Holdtime=%d Preference=%d", |
| 209 | ifp->name, |
| 210 | inet_ntoa(p->u.prefix4), |
| 211 | p->prefixlen, |
| 212 | irdp->flags & IF_SHUTDOWN? 0 : irdp->Lifetime, |
| 213 | get_pref(irdp, p)); |
| 214 | |
| 215 | send_packet (ifp, s, dst, p, ttl); |
| 216 | } |
| 217 | |
| 218 | void irdp_advertisement (struct interface *ifp, |
| 219 | struct prefix *p) |
| 220 | { |
| 221 | struct stream *s; |
| 222 | s = stream_new (128); |
| 223 | make_advertisement_packet (ifp, p, s); |
| 224 | irdp_send(ifp, p, s); |
| 225 | } |
| 226 | |
| 227 | int irdp_send_thread(struct thread *t_advert) |
| 228 | { |
| 229 | u_int32_t timer, tmp; |
| 230 | struct interface *ifp = THREAD_ARG (t_advert); |
| 231 | struct zebra_if *zi=ifp->info; |
| 232 | struct irdp_interface *irdp=&zi->irdp; |
| 233 | struct prefix *p; |
| 234 | listnode node; |
| 235 | struct connected *ifc; |
| 236 | |
| 237 | irdp->flags &= ~IF_SOLICIT; |
| 238 | |
| 239 | if(ifp->connected) |
| 240 | for (node = listhead (ifp->connected); node; nextnode (node)) { |
| 241 | ifc = getdata (node); |
| 242 | |
| 243 | p = ifc->address; |
| 244 | |
| 245 | irdp_advertisement(ifp, p); |
| 246 | irdp->irdp_sent++; |
| 247 | |
| 248 | } |
| 249 | |
| 250 | tmp = irdp->MaxAdvertInterval-irdp->MinAdvertInterval; |
| 251 | timer = (random () % tmp ) + 1; |
| 252 | timer = irdp->MinAdvertInterval + timer; |
| 253 | |
| 254 | if(irdp->irdp_sent < MAX_INITIAL_ADVERTISEMENTS && |
| 255 | timer > MAX_INITIAL_ADVERT_INTERVAL ) |
| 256 | timer= MAX_INITIAL_ADVERT_INTERVAL; |
| 257 | |
| 258 | if(irdp->flags & IF_DEBUG_MISC) |
| 259 | zlog_warn("IRDP: New timer for %s set to %u\n", ifp->name, timer); |
| 260 | |
| 261 | irdp->t_advertise = thread_add_timer(zebrad.master, irdp_send_thread, ifp, timer); |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | void irdp_advert_off(struct interface *ifp) |
| 266 | { |
| 267 | struct zebra_if *zi=ifp->info; |
| 268 | struct irdp_interface *irdp=&zi->irdp; |
| 269 | listnode node; |
| 270 | int i; |
| 271 | struct connected *ifc; |
| 272 | struct prefix *p; |
| 273 | |
| 274 | if(irdp->t_advertise) thread_cancel(irdp->t_advertise); |
| 275 | irdp->t_advertise = NULL; |
| 276 | |
| 277 | if(ifp->connected) |
| 278 | for (node = listhead (ifp->connected); node; nextnode (node)) { |
| 279 | ifc = getdata (node); |
| 280 | |
| 281 | p = ifc->address; |
| 282 | |
| 283 | /* Output some packets with Lifetime 0 |
| 284 | we should add a wait... |
| 285 | */ |
| 286 | |
| 287 | for(i=0; i< IRDP_LAST_ADVERT_MESSAGES; i++) { |
| 288 | |
| 289 | irdp->irdp_sent++; |
| 290 | irdp_advertisement(ifp, p); |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | |
| 296 | void process_solicit (struct interface *ifp) |
| 297 | { |
| 298 | struct zebra_if *zi=ifp->info; |
| 299 | struct irdp_interface *irdp=&zi->irdp; |
| 300 | u_int32_t timer; |
| 301 | |
| 302 | /* When SOLICIT is active we reject further incoming solicits |
| 303 | this keeps down the answering rate so we don't have think |
| 304 | about DoS attacks here. */ |
| 305 | |
| 306 | if( irdp->flags & IF_SOLICIT) return; |
| 307 | |
| 308 | irdp->flags |= IF_SOLICIT; |
| 309 | if(irdp->t_advertise) thread_cancel(irdp->t_advertise); |
| 310 | irdp->t_advertise = NULL; |
| 311 | |
| 312 | timer = (random () % MAX_RESPONSE_DELAY) + 1; |
| 313 | |
| 314 | irdp->t_advertise = thread_add_timer(zebrad.master, |
| 315 | irdp_send_thread, |
| 316 | ifp, |
| 317 | timer); |
| 318 | } |
| 319 | |
| 320 | void irdp_finish() |
| 321 | { |
| 322 | |
| 323 | listnode node; |
| 324 | struct interface *ifp; |
| 325 | struct zebra_if *zi; |
| 326 | struct irdp_interface *irdp; |
| 327 | |
| 328 | zlog_warn("IRDP: Received shutdown notification."); |
| 329 | |
| 330 | for (node = listhead (iflist); node; node = nextnode (node)) |
| 331 | { |
| 332 | ifp = getdata(node); |
| 333 | zi= ifp->info; |
| 334 | if(! zi) continue; |
| 335 | irdp = &zi->irdp; |
| 336 | if(!irdp) continue; |
| 337 | |
| 338 | if(irdp->flags & IF_ACTIVE ) { |
| 339 | irdp->flags |= IF_SHUTDOWN; |
| 340 | irdp_advert_off(ifp); |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | void irdp_init() |
| 346 | { |
| 347 | irdp_sock_init(); |
| 348 | irdp_if_init (); |
| 349 | } |
| 350 | |
| 351 | |
| 352 | |
| 353 | #endif /* HAVE_IRDP */ |
| 354 | |
| 355 | |
| 356 | |
| 357 | |