ospfd: Tighten up the connected check for redistribution
[jleu-quagga.git] / zebra / irdp_packet.c
blob600383728f19f3352d347a1c399980509d98d6cb
1 /*
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
11 * later version.
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
21 * 02111-1307, USA.
24 /*
25 * This work includes work with the following copywrite:
27 * Copyright (C) 1997, 2000 Kunihiro Ishiguro
31 /*
32 * Thanks to Jens Låås at Swedish University of Agricultural Sciences
33 * for reviewing and tests.
37 #include <zebra.h>
40 #ifdef HAVE_IRDP
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 #include "sockopt.h"
67 /* GLOBAL VARS */
69 int irdp_sock = -1;
70 char b1[16], b2[16], b3[16], b4[16]; /* For inet_2a */
72 extern struct zebra_t zebrad;
73 extern struct thread *t_irdp_raw;
74 int in_cksum (void *ptr, int nbytes);
75 void process_solicit (struct interface *ifp);
77 static void
78 parse_irdp_packet(char *p,
79 int len,
80 struct interface *ifp)
82 struct ip *ip = (struct ip *)p ;
83 struct icmphdr *icmp;
84 struct in_addr src;
85 int ip_hlen, iplen, datalen;
86 struct zebra_if *zi;
87 struct irdp_interface *irdp;
89 zi = ifp->info;
90 if (!zi)
91 return;
93 irdp = &zi->irdp;
94 if (!irdp)
95 return;
97 ip_hlen = ip->ip_hl << 2;
99 sockopt_iphdrincl_swab_systoh (ip);
101 iplen = ip->ip_len;
102 datalen = len - ip_hlen;
103 src = ip->ip_src;
105 if (len != iplen)
107 zlog_err ("IRDP: RX length doesnt match IP length");
108 return;
111 if (iplen < ICMP_MINLEN)
113 zlog_err ("IRDP: RX ICMP packet too short from %s\n",
114 inet_ntoa (src));
115 return;
118 /* XXX: RAW doesnt receive link-layer, surely? ??? */
119 /* Check so we don't checksum packets longer than oure RX_BUF - (ethlen +
120 len of IP-header) 14+20 */
121 if (iplen > IRDP_RX_BUF-34)
123 zlog_err ("IRDP: RX ICMP packet too long from %s\n",
124 inet_ntoa (src));
125 return;
128 icmp = (struct icmphdr *) (p+ip_hlen);
130 /* check icmp checksum */
131 if (in_cksum (icmp, datalen) != icmp->checksum)
133 zlog_warn ("IRDP: RX ICMP packet from %s. Bad checksum, silently ignored",
134 inet_ntoa (src));
135 return;
138 /* Handle just only IRDP */
139 if (!(icmp->type == ICMP_ROUTERADVERT
140 || icmp->type == ICMP_ROUTERSOLICIT))
141 return;
143 if (icmp->code != 0)
145 zlog_warn ("IRDP: RX packet type %d from %s. Bad ICMP type code,"
146 " silently ignored",
147 icmp->type, inet_ntoa (src));
148 return;
151 if (! ((ntohl (ip->ip_dst.s_addr) == INADDR_BROADCAST)
152 && (irdp->flags & IF_BROADCAST))
154 (ntohl (ip->ip_dst.s_addr) == INADDR_ALLRTRS_GROUP
155 && !(irdp->flags & IF_BROADCAST)))
157 zlog_warn ("IRDP: RX illegal from %s to %s while %s operates in %s\n",
158 inet_ntoa (src),
159 ntohl (ip->ip_dst.s_addr) == INADDR_ALLRTRS_GROUP ?
160 "multicast" : inet_ntoa (ip->ip_dst),
161 ifp->name,
162 irdp->flags & IF_BROADCAST ? "broadcast" : "multicast");
164 zlog_warn ("IRDP: Please correct settings\n");
165 return;
168 switch (icmp->type)
170 case ICMP_ROUTERADVERT:
171 break;
173 case ICMP_ROUTERSOLICIT:
175 if(irdp->flags & IF_DEBUG_MESSAGES)
176 zlog_debug ("IRDP: RX Solicit on %s from %s\n",
177 ifp->name,
178 inet_ntoa (src));
180 process_solicit(ifp);
181 break;
183 default:
184 zlog_warn ("IRDP: RX type %d from %s. Bad ICMP type, silently ignored",
185 icmp->type,
186 inet_ntoa (src));
190 static int
191 irdp_recvmsg (int sock, u_char *buf, int size, int *ifindex)
193 struct msghdr msg;
194 struct iovec iov;
195 char adata[CMSG_SPACE( SOPT_SIZE_CMSG_PKTINFO_IPV4() )];
196 int ret;
198 msg.msg_name = (void *)0;
199 msg.msg_namelen = 0;
200 msg.msg_iov = &iov;
201 msg.msg_iovlen = 1;
202 msg.msg_control = (void *) adata;
203 msg.msg_controllen = sizeof adata;
205 iov.iov_base = buf;
206 iov.iov_len = size;
208 ret = recvmsg (sock, &msg, 0);
209 if (ret < 0) {
210 zlog_warn("IRDP: recvmsg: read error %s", safe_strerror(errno));
211 return ret;
214 if (msg.msg_flags & MSG_TRUNC) {
215 zlog_warn("IRDP: recvmsg: truncated message");
216 return ret;
218 if (msg.msg_flags & MSG_CTRUNC) {
219 zlog_warn("IRDP: recvmsg: truncated control message");
220 return ret;
223 *ifindex = getsockopt_ifindex (AF_INET, &msg);
225 return ret;
228 int irdp_read_raw(struct thread *r)
230 struct interface *ifp;
231 struct zebra_if *zi;
232 struct irdp_interface *irdp;
233 char buf[IRDP_RX_BUF];
234 int ret, ifindex;
236 int irdp_sock = THREAD_FD (r);
237 t_irdp_raw = thread_add_read (zebrad.master, irdp_read_raw, NULL, irdp_sock);
239 ret = irdp_recvmsg (irdp_sock, (u_char *) buf, IRDP_RX_BUF, &ifindex);
241 if (ret < 0) zlog_warn ("IRDP: RX Error length = %d", ret);
243 ifp = if_lookup_by_index(ifindex);
244 if(! ifp ) return ret;
246 zi= ifp->info;
247 if(! zi ) return ret;
249 irdp = &zi->irdp;
250 if(! irdp ) return ret;
252 if(! (irdp->flags & IF_ACTIVE)) {
254 if(irdp->flags & IF_DEBUG_MISC)
255 zlog_debug("IRDP: RX ICMP for disabled interface %s\n", ifp->name);
256 return 0;
259 if(irdp->flags & IF_DEBUG_PACKET) {
260 int i;
261 zlog_debug("IRDP: RX (idx %d) ", ifindex);
262 for(i=0; i < ret; i++) zlog_debug( "IRDP: RX %x ", buf[i]&0xFF);
265 parse_irdp_packet(buf, ret, ifp);
267 return ret;
270 void
271 send_packet(struct interface *ifp,
272 struct stream *s,
273 u_int32_t dst,
274 struct prefix *p,
275 u_int32_t ttl)
277 static struct sockaddr_in sockdst = {AF_INET};
278 struct ip *ip;
279 struct icmphdr *icmp;
280 struct msghdr *msg;
281 struct cmsghdr *cmsg;
282 struct iovec iovector;
283 char msgbuf[256];
284 char buf[256];
285 struct in_pktinfo *pktinfo;
286 u_long src;
287 int on;
289 if (!(ifp->flags & IFF_UP))
290 return;
292 if (!p)
293 src = ntohl(p->u.prefix4.s_addr);
294 else
295 src = 0; /* Is filled in */
297 ip = (struct ip *) buf;
298 ip->ip_hl = sizeof(struct ip) >> 2;
299 ip->ip_v = IPVERSION;
300 ip->ip_tos = 0xC0;
301 ip->ip_off = 0L;
302 ip->ip_p = 1; /* IP_ICMP */
303 ip->ip_ttl = ttl;
304 ip->ip_src.s_addr = src;
305 ip->ip_dst.s_addr = dst;
306 icmp = (struct icmphdr *) (buf + sizeof (struct ip));
308 /* Merge IP header with icmp packet */
309 assert (stream_get_endp(s) < (sizeof (buf) - sizeof (struct ip)));
310 stream_get(icmp, s, stream_get_endp(s));
312 /* icmp->checksum is already calculated */
313 ip->ip_len = sizeof(struct ip) + stream_get_endp(s);
315 on = 1;
316 if (setsockopt(irdp_sock, IPPROTO_IP, IP_HDRINCL,
317 (char *) &on, sizeof(on)) < 0)
318 zlog_warn("sendto %s", safe_strerror (errno));
321 if(dst == INADDR_BROADCAST ) {
322 on = 1;
323 if (setsockopt(irdp_sock, SOL_SOCKET, SO_BROADCAST,
324 (char *) &on, sizeof(on)) < 0)
325 zlog_warn("sendto %s", safe_strerror (errno));
328 if(dst != INADDR_BROADCAST) {
329 on = 0;
330 if( setsockopt(irdp_sock,IPPROTO_IP, IP_MULTICAST_LOOP,
331 (char *)&on,sizeof(on)) < 0)
332 zlog_warn("sendto %s", safe_strerror (errno));
335 memset(&sockdst,0,sizeof(sockdst));
336 sockdst.sin_family=AF_INET;
337 sockdst.sin_addr.s_addr = dst;
339 cmsg = (struct cmsghdr *) (msgbuf + sizeof(struct msghdr));
340 cmsg->cmsg_len = sizeof(struct cmsghdr) + sizeof(struct in_pktinfo);
341 cmsg->cmsg_level = SOL_IP;
342 cmsg->cmsg_type = IP_PKTINFO;
343 pktinfo = (struct in_pktinfo *) CMSG_DATA(cmsg);
344 pktinfo->ipi_ifindex = ifp->ifindex;
345 pktinfo->ipi_spec_dst.s_addr = src;
346 pktinfo->ipi_addr.s_addr = src;
348 iovector.iov_base = (void *) buf;
349 iovector.iov_len = ip->ip_len;
350 msg = (struct msghdr *) msgbuf;
351 msg->msg_name = &sockdst;
352 msg->msg_namelen = sizeof(sockdst);
353 msg->msg_iov = &iovector;
354 msg->msg_iovlen = 1;
355 msg->msg_control = cmsg;
356 msg->msg_controllen = cmsg->cmsg_len;
358 sockopt_iphdrincl_swab_htosys (ip);
360 if (sendmsg(irdp_sock, msg, 0) < 0) {
361 zlog_warn("sendto %s", safe_strerror (errno));
363 /* printf("TX on %s idx %d\n", ifp->name, ifp->ifindex); */
367 #endif /* HAVE_IRDP */