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 along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 * This work includes work with the following copywrite:
26 * Copyright (C) 1997, 2000 Kunihiro Ishiguro
31 * Thanks to Jens Laas at Swedish University of Agricultural Sciences
32 * for reviewing and tests.
37 #include <netinet/ip_icmp.h>
41 #include "connected.h"
49 #include "sockunion.h"
50 #include "sockunion.h"
56 #include "zebra_memory.h"
57 #include "zebra/interface.h"
58 #include "zebra/rtadv.h"
59 #include "zebra/rib.h"
60 #include "zebra/zserv.h"
61 #include "zebra/redistribute.h"
62 #include "zebra/irdp.h"
63 #include "zebra/zebra_errors.h"
70 extern struct thread
*t_irdp_raw
;
72 static void parse_irdp_packet(char *p
, int len
, struct interface
*ifp
)
74 struct ip
*ip
= (struct ip
*)p
;
77 int ip_hlen
, iplen
, datalen
;
79 struct irdp_interface
*irdp
;
89 ip_hlen
= ip
->ip_hl
<< 2;
91 sockopt_iphdrincl_swab_systoh(ip
);
94 datalen
= len
- ip_hlen
;
98 flog_err(ZEBRA_ERR_IRDP_LEN_MISMATCH
,
99 "IRDP: RX length doesn't match IP length");
103 if (iplen
< ICMP_MINLEN
) {
104 flog_err(ZEBRA_ERR_IRDP_LEN_MISMATCH
,
105 "IRDP: RX ICMP packet too short from %s\n",
110 /* XXX: RAW doesn't receive link-layer, surely? ??? */
111 /* Check so we don't checksum packets longer than oure RX_BUF - (ethlen
113 len of IP-header) 14+20 */
114 if (iplen
> IRDP_RX_BUF
- 34) {
115 flog_err(ZEBRA_ERR_IRDP_LEN_MISMATCH
,
116 "IRDP: RX ICMP packet too long from %s\n",
121 icmp
= (struct icmphdr
*)(p
+ ip_hlen
);
123 /* check icmp checksum */
124 if (in_cksum(icmp
, datalen
) != icmp
->checksum
) {
126 "IRDP: RX ICMP packet from %s. Bad checksum, silently ignored",
131 /* Handle just only IRDP */
132 if (!(icmp
->type
== ICMP_ROUTERADVERT
133 || icmp
->type
== ICMP_ROUTERSOLICIT
))
136 if (icmp
->code
!= 0) {
138 "IRDP: RX packet type %d from %s. Bad ICMP type code,"
140 icmp
->type
, inet_ntoa(src
));
144 if (!((ntohl(ip
->ip_dst
.s_addr
) == INADDR_BROADCAST
)
145 && (irdp
->flags
& IF_BROADCAST
))
146 || (ntohl(ip
->ip_dst
.s_addr
) == INADDR_ALLRTRS_GROUP
147 && !(irdp
->flags
& IF_BROADCAST
))) {
149 "IRDP: RX illegal from %s to %s while %s operates in %s\n",
151 ntohl(ip
->ip_dst
.s_addr
) == INADDR_ALLRTRS_GROUP
153 : inet_ntoa(ip
->ip_dst
),
155 irdp
->flags
& IF_BROADCAST
? "broadcast" : "multicast");
157 zlog_warn("IRDP: Please correct settings\n");
161 switch (icmp
->type
) {
162 case ICMP_ROUTERADVERT
:
165 case ICMP_ROUTERSOLICIT
:
167 if (irdp
->flags
& IF_DEBUG_MESSAGES
)
168 zlog_debug("IRDP: RX Solicit on %s from %s\n",
169 ifp
->name
, inet_ntoa(src
));
171 process_solicit(ifp
);
176 "IRDP: RX type %d from %s. Bad ICMP type, silently ignored",
177 icmp
->type
, inet_ntoa(src
));
181 static int irdp_recvmsg(int sock
, uint8_t *buf
, int size
, int *ifindex
)
185 char adata
[CMSG_SPACE(SOPT_SIZE_CMSG_PKTINFO_IPV4())];
188 memset(&msg
, 0, sizeof(msg
));
189 msg
.msg_name
= (void *)0;
193 msg
.msg_control
= (void *)adata
;
194 msg
.msg_controllen
= sizeof adata
;
199 ret
= recvmsg(sock
, &msg
, 0);
201 zlog_warn("IRDP: recvmsg: read error %s", safe_strerror(errno
));
205 if (msg
.msg_flags
& MSG_TRUNC
) {
206 zlog_warn("IRDP: recvmsg: truncated message");
209 if (msg
.msg_flags
& MSG_CTRUNC
) {
210 zlog_warn("IRDP: recvmsg: truncated control message");
214 *ifindex
= getsockopt_ifindex(AF_INET
, &msg
);
219 int irdp_read_raw(struct thread
*r
)
221 struct interface
*ifp
;
223 struct irdp_interface
*irdp
;
224 char buf
[IRDP_RX_BUF
];
225 int ret
, ifindex
= 0;
227 int irdp_sock
= THREAD_FD(r
);
229 thread_add_read(zebrad
.master
, irdp_read_raw
, NULL
, irdp_sock
,
232 ret
= irdp_recvmsg(irdp_sock
, (uint8_t *)buf
, IRDP_RX_BUF
, &ifindex
);
235 zlog_warn("IRDP: RX Error length = %d", ret
);
237 ifp
= if_lookup_by_index(ifindex
, VRF_DEFAULT
);
249 if (!(irdp
->flags
& IF_ACTIVE
)) {
251 if (irdp
->flags
& IF_DEBUG_MISC
)
252 zlog_debug("IRDP: RX ICMP for disabled interface %s\n",
257 if (irdp
->flags
& IF_DEBUG_PACKET
) {
259 zlog_debug("IRDP: RX (idx %d) ", ifindex
);
260 for (i
= 0; i
< ret
; i
++)
261 zlog_debug("IRDP: RX %x ", buf
[i
] & 0xFF);
264 parse_irdp_packet(buf
, ret
, ifp
);
269 void send_packet(struct interface
*ifp
, struct stream
*s
, uint32_t dst
,
270 struct prefix
*p
, uint32_t ttl
)
272 static struct sockaddr_in sockdst
= {AF_INET
};
274 struct icmphdr
*icmp
;
276 struct cmsghdr
*cmsg
;
277 struct iovec iovector
;
280 struct in_pktinfo
*pktinfo
;
284 if (!(ifp
->flags
& IFF_UP
))
288 src
= ntohl(p
->u
.prefix4
.s_addr
);
290 src
= 0; /* Is filled in */
292 ip
= (struct ip
*)buf
;
293 ip
->ip_hl
= sizeof(struct ip
) >> 2;
294 ip
->ip_v
= IPVERSION
;
297 ip
->ip_p
= 1; /* IP_ICMP */
299 ip
->ip_src
.s_addr
= src
;
300 ip
->ip_dst
.s_addr
= dst
;
301 icmp
= (struct icmphdr
*)(buf
+ sizeof(struct ip
));
303 /* Merge IP header with icmp packet */
304 assert(stream_get_endp(s
) < (sizeof(buf
) - sizeof(struct ip
)));
305 stream_get(icmp
, s
, stream_get_endp(s
));
307 /* icmp->checksum is already calculated */
308 ip
->ip_len
= sizeof(struct ip
) + stream_get_endp(s
);
311 if (setsockopt(irdp_sock
, IPPROTO_IP
, IP_HDRINCL
, (char *)&on
,
314 zlog_warn("sendto %s", safe_strerror(errno
));
317 if (dst
== INADDR_BROADCAST
) {
319 if (setsockopt(irdp_sock
, SOL_SOCKET
, SO_BROADCAST
, (char *)&on
,
322 zlog_warn("sendto %s", safe_strerror(errno
));
325 if (dst
!= INADDR_BROADCAST
)
326 setsockopt_ipv4_multicast_loop(irdp_sock
, 0);
328 memset(&sockdst
, 0, sizeof(sockdst
));
329 sockdst
.sin_family
= AF_INET
;
330 sockdst
.sin_addr
.s_addr
= dst
;
332 cmsg
= (struct cmsghdr
*)(msgbuf
+ sizeof(struct msghdr
));
333 cmsg
->cmsg_len
= sizeof(struct cmsghdr
) + sizeof(struct in_pktinfo
);
334 cmsg
->cmsg_level
= SOL_IP
;
335 cmsg
->cmsg_type
= IP_PKTINFO
;
336 pktinfo
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
337 pktinfo
->ipi_ifindex
= ifp
->ifindex
;
338 pktinfo
->ipi_spec_dst
.s_addr
= src
;
339 pktinfo
->ipi_addr
.s_addr
= src
;
341 iovector
.iov_base
= (void *)buf
;
342 iovector
.iov_len
= ip
->ip_len
;
343 msg
= (struct msghdr
*)msgbuf
;
344 msg
->msg_name
= &sockdst
;
345 msg
->msg_namelen
= sizeof(sockdst
);
346 msg
->msg_iov
= &iovector
;
348 msg
->msg_control
= cmsg
;
349 msg
->msg_controllen
= cmsg
->cmsg_len
;
351 sockopt_iphdrincl_swab_htosys(ip
);
353 if (sendmsg(irdp_sock
, msg
, 0) < 0) {
354 zlog_warn("sendto %s", safe_strerror(errno
));
356 /* printf("TX on %s idx %d\n", ifp->name, ifp->ifindex); */