3 * Copyright (C) 2000 Robert Olsson.
4 * Swedish University of Agricultural Sciences
6 * This file is part of GNU Zebra.
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
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.
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
25 * This work includes work with the following copywrite:
27 * Copyright (C) 1997, 2000 Kunihiro Ishiguro
32 * Thanks to Jens Låås at Swedish University of Agricultural Sciences
33 * for reviewing and tests.
44 #include "sockunion.h"
50 #include "connected.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>
63 #include "sockunion.h"
72 extern struct zebra_t zebrad;
73 extern struct thread *t_irdp_raw;
76 parse_irdp_packet(char *p,
78 struct interface *ifp)
80 struct ip *ip = (struct ip *)p ;
83 int ip_hlen, iplen, datalen;
85 struct irdp_interface *irdp;
95 ip_hlen = ip->ip_hl << 2;
97 sockopt_iphdrincl_swab_systoh (ip);
100 datalen = len - ip_hlen;
105 zlog_err ("IRDP: RX length doesn't match IP length");
109 if (iplen < ICMP_MINLEN)
111 zlog_err ("IRDP: RX ICMP packet too short from %s\n",
116 /* XXX: RAW doesn't receive link-layer, surely? ??? */
117 /* Check so we don't checksum packets longer than oure RX_BUF - (ethlen +
118 len of IP-header) 14+20 */
119 if (iplen > IRDP_RX_BUF-34)
121 zlog_err ("IRDP: RX ICMP packet too long from %s\n",
126 icmp = (struct icmphdr *) (p+ip_hlen);
128 /* check icmp checksum */
129 if (in_cksum (icmp, datalen) != icmp->checksum)
131 zlog_warn ("IRDP: RX ICMP packet from %s. Bad checksum, silently ignored",
136 /* Handle just only IRDP */
137 if (!(icmp->type == ICMP_ROUTERADVERT
138 || icmp->type == ICMP_ROUTERSOLICIT))
143 zlog_warn ("IRDP: RX packet type %d from %s. Bad ICMP type code,"
145 icmp->type, inet_ntoa (src));
149 if (! ((ntohl (ip->ip_dst.s_addr) == INADDR_BROADCAST)
150 && (irdp->flags & IF_BROADCAST))
152 (ntohl (ip->ip_dst.s_addr) == INADDR_ALLRTRS_GROUP
153 && !(irdp->flags & IF_BROADCAST)))
155 zlog_warn ("IRDP: RX illegal from %s to %s while %s operates in %s\n",
157 ntohl (ip->ip_dst.s_addr) == INADDR_ALLRTRS_GROUP ?
158 "multicast" : inet_ntoa (ip->ip_dst),
160 irdp->flags & IF_BROADCAST ? "broadcast" : "multicast");
162 zlog_warn ("IRDP: Please correct settings\n");
168 case ICMP_ROUTERADVERT:
171 case ICMP_ROUTERSOLICIT:
173 if(irdp->flags & IF_DEBUG_MESSAGES)
174 zlog_debug ("IRDP: RX Solicit on %s from %s\n",
178 process_solicit(ifp);
182 zlog_warn ("IRDP: RX type %d from %s. Bad ICMP type, silently ignored",
189 irdp_recvmsg (int sock, u_char *buf, int size, int *ifindex)
193 char adata[CMSG_SPACE( SOPT_SIZE_CMSG_PKTINFO_IPV4() )];
196 msg.msg_name = (void *)0;
200 msg.msg_control = (void *) adata;
201 msg.msg_controllen = sizeof adata;
206 ret = recvmsg (sock, &msg, 0);
208 zlog_warn("IRDP: recvmsg: read error %s", safe_strerror(errno));
212 if (msg.msg_flags & MSG_TRUNC) {
213 zlog_warn("IRDP: recvmsg: truncated message");
216 if (msg.msg_flags & MSG_CTRUNC) {
217 zlog_warn("IRDP: recvmsg: truncated control message");
221 *ifindex = getsockopt_ifindex (AF_INET, &msg);
226 int irdp_read_raw(struct thread *r)
228 struct interface *ifp;
230 struct irdp_interface *irdp;
231 char buf[IRDP_RX_BUF];
232 int ret, ifindex = 0;
234 int irdp_sock = THREAD_FD (r);
235 t_irdp_raw = thread_add_read (zebrad.master, irdp_read_raw, NULL, irdp_sock);
237 ret = irdp_recvmsg (irdp_sock, (u_char *) buf, IRDP_RX_BUF, &ifindex);
239 if (ret < 0) zlog_warn ("IRDP: RX Error length = %d", ret);
241 ifp = if_lookup_by_index(ifindex);
242 if(! ifp ) return ret;
245 if(! zi ) return ret;
248 if(! irdp ) return ret;
250 if(! (irdp->flags & IF_ACTIVE)) {
252 if(irdp->flags & IF_DEBUG_MISC)
253 zlog_debug("IRDP: RX ICMP for disabled interface %s\n", ifp->name);
257 if(irdp->flags & IF_DEBUG_PACKET) {
259 zlog_debug("IRDP: RX (idx %d) ", ifindex);
260 for(i=0; i < ret; i++) zlog_debug( "IRDP: RX %x ", buf[i]&0xFF);
263 parse_irdp_packet(buf, ret, ifp);
269 send_packet(struct interface *ifp,
275 static struct sockaddr_in sockdst = {AF_INET};
277 struct icmphdr *icmp;
279 struct cmsghdr *cmsg;
280 struct iovec iovector;
283 struct in_pktinfo *pktinfo;
287 if (!(ifp->flags & IFF_UP))
291 src = ntohl(p->u.prefix4.s_addr);
293 src = 0; /* Is filled in */
295 ip = (struct ip *) buf;
296 ip->ip_hl = sizeof(struct ip) >> 2;
297 ip->ip_v = IPVERSION;
300 ip->ip_p = 1; /* IP_ICMP */
302 ip->ip_src.s_addr = src;
303 ip->ip_dst.s_addr = dst;
304 icmp = (struct icmphdr *) (buf + sizeof (struct ip));
306 /* Merge IP header with icmp packet */
307 assert (stream_get_endp(s) < (sizeof (buf) - sizeof (struct ip)));
308 stream_get(icmp, s, stream_get_endp(s));
310 /* icmp->checksum is already calculated */
311 ip->ip_len = sizeof(struct ip) + stream_get_endp(s);
314 if (setsockopt(irdp_sock, IPPROTO_IP, IP_HDRINCL,
315 (char *) &on, sizeof(on)) < 0)
316 zlog_warn("sendto %s", safe_strerror (errno));
319 if(dst == INADDR_BROADCAST ) {
321 if (setsockopt(irdp_sock, SOL_SOCKET, SO_BROADCAST,
322 (char *) &on, sizeof(on)) < 0)
323 zlog_warn("sendto %s", safe_strerror (errno));
326 if(dst != INADDR_BROADCAST) {
328 if( setsockopt(irdp_sock,IPPROTO_IP, IP_MULTICAST_LOOP,
329 (char *)&on,sizeof(on)) < 0)
330 zlog_warn("sendto %s", safe_strerror (errno));
333 memset(&sockdst,0,sizeof(sockdst));
334 sockdst.sin_family=AF_INET;
335 sockdst.sin_addr.s_addr = dst;
337 cmsg = (struct cmsghdr *) (msgbuf + sizeof(struct msghdr));
338 cmsg->cmsg_len = sizeof(struct cmsghdr) + sizeof(struct in_pktinfo);
339 cmsg->cmsg_level = SOL_IP;
340 cmsg->cmsg_type = IP_PKTINFO;
341 pktinfo = (struct in_pktinfo *) CMSG_DATA(cmsg);
342 pktinfo->ipi_ifindex = ifp->ifindex;
343 pktinfo->ipi_spec_dst.s_addr = src;
344 pktinfo->ipi_addr.s_addr = src;
346 iovector.iov_base = (void *) buf;
347 iovector.iov_len = ip->ip_len;
348 msg = (struct msghdr *) msgbuf;
349 msg->msg_name = &sockdst;
350 msg->msg_namelen = sizeof(sockdst);
351 msg->msg_iov = &iovector;
353 msg->msg_control = cmsg;
354 msg->msg_controllen = cmsg->cmsg_len;
356 sockopt_iphdrincl_swab_htosys (ip);
358 if (sendmsg(irdp_sock, msg, 0) < 0) {
359 zlog_warn("sendto %s", safe_strerror (errno));
361 /* printf("TX on %s idx %d\n", ifp->name, ifp->ifindex); */
365 #endif /* HAVE_IRDP */