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 | |
| 40 | #ifdef HAVE_IRDP |
| 41 | |
| 42 | #include "if.h" |
| 43 | #include "vty.h" |
| 44 | #include "sockunion.h" |
| 45 | #include "prefix.h" |
| 46 | #include "command.h" |
| 47 | #include "memory.h" |
| 48 | #include "stream.h" |
| 49 | #include "ioctl.h" |
| 50 | #include "connected.h" |
| 51 | #include "log.h" |
| 52 | #include "zclient.h" |
| 53 | #include "thread.h" |
| 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 | #include "if.h" |
| 62 | #include "sockunion.h" |
| 63 | #include "log.h" |
| 64 | |
| 65 | |
| 66 | |
| 67 | /* GLOBAL VARS */ |
| 68 | |
| 69 | int irdp_sock; |
| 70 | char b1[16], b2[16], b3[16], b4[16]; /* For inet_2a */ |
| 71 | |
| 72 | extern struct zebra_t zebrad; |
| 73 | extern struct thread *t_irdp_raw; |
| 74 | extern struct interface *get_iflist_ifp(int idx); |
| 75 | int in_cksum (void *ptr, int nbytes); |
| 76 | void process_solicit (struct interface *ifp); |
| 77 | |
| 78 | void parse_irdp_packet(char *p, |
| 79 | int len, |
| 80 | struct interface *ifp) |
| 81 | { |
| 82 | struct ip *ip = (struct ip *)p ; |
| 83 | struct icmphdr *icmp; |
| 84 | struct in_addr src; |
| 85 | int ip_hlen, ip_len; |
| 86 | struct zebra_if *zi; |
| 87 | struct irdp_interface *irdp; |
| 88 | |
| 89 | zi = ifp->info; |
| 90 | if(!zi) return; |
| 91 | |
| 92 | irdp = &zi->irdp; |
| 93 | if(!irdp) return; |
| 94 | |
| 95 | ip_hlen = ip->ip_hl*4; |
| 96 | ip_len = ntohs(ip->ip_len); |
| 97 | len = len - ip_hlen; |
| 98 | src = ip->ip_src; |
| 99 | |
| 100 | if(ip_len < ICMP_MINLEN) { |
| 101 | zlog_err ("IRDP: RX ICMP packet too short from %s\n", |
| 102 | inet_ntoa (src)); |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | /* Check so we don't checksum packets longer than oure RX_BUF - (ethlen + |
| 107 | len of IP-header) 14+20 */ |
| 108 | |
| 109 | if(ip_len > IRDP_RX_BUF-34) { |
| 110 | zlog_err ("IRDP: RX ICMP packet too long from %s\n", |
| 111 | inet_ntoa (src)); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | if (in_cksum (ip, ip_len)) { |
| 117 | zlog_warn ("IRDP: RX ICMP packet from %s. Bad checksum, silently ignored", |
| 118 | inet_ntoa (src)); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | icmp = (struct icmphdr *) (p+ip_hlen); |
| 123 | |
| 124 | |
| 125 | /* Handle just only IRDP */ |
| 126 | |
| 127 | if( icmp->type == ICMP_ROUTERADVERT); |
| 128 | else if( icmp->type == ICMP_ROUTERSOLICIT); |
| 129 | else return; |
| 130 | |
| 131 | |
| 132 | if (icmp->code != 0) { |
| 133 | zlog_warn ("IRDP: RX packet type %d from %s. Bad ICMP type code, silently ignored", |
| 134 | icmp->type, |
| 135 | inet_ntoa (src)); |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | if(ip->ip_dst.s_addr == INADDR_BROADCAST && |
| 140 | irdp->flags & IF_BROADCAST); |
| 141 | |
| 142 | else if ( ntohl(ip->ip_dst.s_addr) == INADDR_ALLRTRS_GROUP && |
| 143 | ! (irdp->flags & IF_BROADCAST)); |
| 144 | |
| 145 | else { /* ERROR */ |
| 146 | |
| 147 | zlog_warn ("IRDP: RX illegal from %s to %s while %s operates in %s\n", |
| 148 | inet_ntoa (src), |
| 149 | ntohl(ip->ip_dst.s_addr) == INADDR_ALLRTRS_GROUP? |
| 150 | "multicast" : inet_ntoa(ip->ip_dst), |
| 151 | ifp->name, |
| 152 | irdp->flags & IF_BROADCAST? |
| 153 | "broadcast" : "multicast"); |
| 154 | |
| 155 | zlog_warn ("IRDP: Please correct settings\n"); |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | switch (icmp->type) |
| 160 | { |
| 161 | case ICMP_ROUTERADVERT: |
| 162 | break; |
| 163 | |
| 164 | case ICMP_ROUTERSOLICIT: |
| 165 | |
| 166 | if(irdp->flags & IF_DEBUG_MESSAGES) |
| 167 | zlog_warn ("IRDP: RX Solicit on %s from %s\n", |
| 168 | ifp->name, |
| 169 | inet_ntoa (src)); |
| 170 | |
| 171 | process_solicit(ifp); |
| 172 | break; |
| 173 | |
| 174 | default: |
| 175 | zlog_warn ("IRDP: RX type %d from %s. Bad ICMP type, silently ignored", |
| 176 | icmp->type, |
| 177 | inet_ntoa (src)); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | int irdp_recvmsg (int sock, |
| 182 | u_char *buf, |
| 183 | int size, |
| 184 | int *ifindex) |
| 185 | { |
| 186 | struct msghdr msg; |
| 187 | struct iovec iov; |
| 188 | struct cmsghdr *ptr; |
| 189 | char adata[1024]; |
| 190 | int ret; |
| 191 | |
| 192 | msg.msg_name = (void *)0; |
| 193 | msg.msg_namelen = 0; |
| 194 | msg.msg_iov = &iov; |
| 195 | msg.msg_iovlen = 1; |
| 196 | msg.msg_control = (void *) adata; |
| 197 | msg.msg_controllen = sizeof adata; |
| 198 | |
| 199 | iov.iov_base = buf; |
| 200 | iov.iov_len = size; |
| 201 | |
| 202 | ret = recvmsg (sock, &msg, 0); |
| 203 | if (ret < 0) { |
| 204 | zlog_warn("IRDP: recvmsg: read error %s", strerror(errno)); |
| 205 | return ret; |
| 206 | } |
| 207 | |
| 208 | if (msg.msg_flags & MSG_TRUNC) { |
| 209 | zlog_warn("IRDP: recvmsg: truncated message"); |
| 210 | return ret; |
| 211 | } |
| 212 | if (msg.msg_flags & MSG_CTRUNC) { |
| 213 | zlog_warn("IRDP: recvmsg: truncated control message"); |
| 214 | return ret; |
| 215 | } |
| 216 | |
| 217 | for (ptr = CMSG_FIRSTHDR(&msg); ptr ; ptr = CMSG_NXTHDR(&msg, ptr)) |
| 218 | if (ptr->cmsg_level == SOL_IP && ptr->cmsg_type == IP_PKTINFO) |
| 219 | { |
| 220 | struct in_pktinfo *pktinfo; |
| 221 | pktinfo = (struct in_pktinfo *) CMSG_DATA (ptr); |
| 222 | *ifindex = pktinfo->ipi_ifindex; |
| 223 | } |
| 224 | return ret; |
| 225 | } |
| 226 | |
| 227 | int irdp_read_raw(struct thread *r) |
| 228 | { |
| 229 | struct interface *ifp; |
| 230 | struct zebra_if *zi; |
| 231 | struct irdp_interface *irdp; |
| 232 | char buf[IRDP_RX_BUF]; |
| 233 | int ret, ifindex; |
| 234 | |
| 235 | int irdp_sock = THREAD_FD (r); |
| 236 | t_irdp_raw = thread_add_read (zebrad.master, irdp_read_raw, NULL, irdp_sock); |
| 237 | |
| 238 | ret = irdp_recvmsg (irdp_sock, buf, IRDP_RX_BUF, &ifindex); |
| 239 | |
| 240 | if (ret < 0) zlog_warn ("IRDP: RX Error length = %d", ret); |
| 241 | |
| 242 | ifp = get_iflist_ifp(ifindex); |
| 243 | if(! ifp ) return ret; |
| 244 | |
| 245 | zi= ifp->info; |
| 246 | if(! zi ) return ret; |
| 247 | |
| 248 | irdp = &zi->irdp; |
| 249 | if(! irdp ) return ret; |
| 250 | |
| 251 | if(! (irdp->flags & IF_ACTIVE)) { |
| 252 | |
| 253 | if(irdp->flags & IF_DEBUG_MISC) |
| 254 | zlog_warn("IRDP: RX ICMP for disabled interface %s\n", |
| 255 | ifp->name); |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | if(irdp->flags & IF_DEBUG_PACKET) { |
| 260 | int i; |
| 261 | zlog_warn("IRDP: RX (idx %d) ", ifindex); |
| 262 | for(i=0; i < ret; i++) zlog_warn( "IRDP: RX %x ", buf[i]&0xFF); |
| 263 | } |
| 264 | |
| 265 | parse_irdp_packet(buf, ret, ifp); |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | void send_packet(struct interface *ifp, |
| 270 | struct stream *s, |
| 271 | u_int32_t dst, |
| 272 | struct prefix *p, |
| 273 | u_int32_t ttl) |
| 274 | { |
| 275 | static struct sockaddr_in sockdst = {AF_INET}; |
| 276 | struct ip *ip; |
| 277 | struct icmphdr *icmp; |
| 278 | struct msghdr *msg; |
| 279 | struct cmsghdr *cmsg; |
| 280 | struct iovec iovector; |
| 281 | char msgbuf[256]; |
| 282 | char buf[256]; |
| 283 | struct in_pktinfo *pktinfo; |
| 284 | u_long src; |
| 285 | int on; |
| 286 | |
| 287 | if (! (ifp->flags & IFF_UP)) return; |
| 288 | |
| 289 | if(!p) src = ntohl(p->u.prefix4.s_addr); |
| 290 | else src = 0; /* Is filled in */ |
| 291 | |
| 292 | ip = (struct ip *) buf; |
| 293 | ip->ip_hl = sizeof(struct ip) >> 2; |
| 294 | ip->ip_v = IPVERSION; |
| 295 | ip->ip_tos = 0xC0; |
| 296 | ip->ip_off = 0L; |
| 297 | ip->ip_p = 1; /* IP_ICMP */ |
| 298 | ip->ip_ttl = ttl; |
| 299 | ip->ip_src.s_addr = src; |
| 300 | ip->ip_dst.s_addr = dst; |
| 301 | icmp = (struct icmphdr *) (buf + 20); |
| 302 | |
| 303 | /* Merge IP header with icmp packet */ |
| 304 | |
| 305 | stream_get(icmp, s, s->putp); |
| 306 | |
| 307 | /* icmp->checksum is already calculated */ |
| 308 | ip->ip_len = sizeof(struct ip) + s->putp; |
| 309 | stream_free(s); |
| 310 | |
| 311 | on = 1; |
| 312 | if (setsockopt(irdp_sock, IPPROTO_IP, IP_HDRINCL, |
| 313 | (char *) &on, sizeof(on)) < 0) |
| 314 | zlog_warn("sendto %s", strerror (errno)); |
| 315 | |
| 316 | |
| 317 | if(dst == INADDR_BROADCAST ) { |
| 318 | on = 1; |
| 319 | if (setsockopt(irdp_sock, SOL_SOCKET, SO_BROADCAST, |
| 320 | (char *) &on, sizeof(on)) < 0) |
| 321 | zlog_warn("sendto %s", strerror (errno)); |
| 322 | } |
| 323 | |
| 324 | if(dst != INADDR_BROADCAST) { |
| 325 | on = 0; |
| 326 | if( setsockopt(irdp_sock,IPPROTO_IP, IP_MULTICAST_LOOP, |
| 327 | (char *)&on,sizeof(on)) < 0) |
| 328 | zlog_warn("sendto %s", strerror (errno)); |
| 329 | } |
| 330 | |
| 331 | bzero(&sockdst,sizeof(sockdst)); |
| 332 | sockdst.sin_family=AF_INET; |
| 333 | sockdst.sin_addr.s_addr = dst; |
| 334 | |
| 335 | cmsg = (struct cmsghdr *) (msgbuf + sizeof(struct msghdr)); |
| 336 | cmsg->cmsg_len = sizeof(struct cmsghdr) + sizeof(struct in_pktinfo); |
| 337 | cmsg->cmsg_level = SOL_IP; |
| 338 | cmsg->cmsg_type = IP_PKTINFO; |
| 339 | pktinfo = (struct in_pktinfo *) CMSG_DATA(cmsg); |
| 340 | pktinfo->ipi_ifindex = ifp->ifindex; |
| 341 | pktinfo->ipi_spec_dst.s_addr = src; |
| 342 | pktinfo->ipi_addr.s_addr = src; |
| 343 | |
| 344 | iovector.iov_base = (void *) buf; |
| 345 | iovector.iov_len = ip->ip_len; |
| 346 | msg = (struct msghdr *) msgbuf; |
| 347 | msg->msg_name = &sockdst; |
| 348 | msg->msg_namelen = sizeof(sockdst); |
| 349 | msg->msg_iov = &iovector; |
| 350 | msg->msg_iovlen = 1; |
| 351 | msg->msg_control = cmsg; |
| 352 | msg->msg_controllen = cmsg->cmsg_len; |
| 353 | |
| 354 | if (sendmsg(irdp_sock, msg, 0) < 0) { |
| 355 | zlog_warn("sendto %s", strerror (errno)); |
| 356 | } |
| 357 | /* printf("TX on %s idx %d\n", ifp->name, ifp->ifindex); */ |
| 358 | } |
| 359 | |
| 360 | |
| 361 | #endif /* HAVE_IRDP */ |
| 362 | |
| 363 | |
| 364 | |