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"
55 #include "lib_errors.h"
57 #include "zebra_memory.h"
58 #include "zebra/interface.h"
59 #include "zebra/rtadv.h"
60 #include "zebra/rib.h"
61 #include "zebra/zebra_router.h"
62 #include "zebra/redistribute.h"
63 #include "zebra/irdp.h"
64 #include "zebra/zebra_errors.h"
71 extern struct thread
*t_irdp_raw
;
73 static void parse_irdp_packet(char *p
, int len
, struct interface
*ifp
)
75 struct ip
*ip
= (struct ip
*)p
;
78 int ip_hlen
, iplen
, datalen
;
80 struct irdp_interface
*irdp
;
81 uint16_t saved_chksum
;
82 char buf
[PREFIX_STRLEN
];
92 ip_hlen
= ip
->ip_hl
<< 2;
94 sockopt_iphdrincl_swab_systoh(ip
);
97 datalen
= len
- ip_hlen
;
101 flog_err(EC_ZEBRA_IRDP_LEN_MISMATCH
,
102 "IRDP: RX length doesn't match IP length");
106 if (iplen
< ICMP_MINLEN
) {
107 flog_err(EC_ZEBRA_IRDP_LEN_MISMATCH
,
108 "IRDP: RX ICMP packet too short from %pI4\n",
113 /* XXX: RAW doesn't receive link-layer, surely? ??? */
114 /* Check so we don't checksum packets longer than oure RX_BUF - (ethlen
116 len of IP-header) 14+20 */
117 if (iplen
> IRDP_RX_BUF
- 34) {
118 flog_err(EC_ZEBRA_IRDP_LEN_MISMATCH
,
119 "IRDP: RX ICMP packet too long from %pI4\n",
124 icmp
= (struct icmphdr
*)(p
+ ip_hlen
);
126 saved_chksum
= icmp
->checksum
;
128 /* check icmp checksum */
129 if (in_cksum(icmp
, datalen
) != saved_chksum
) {
131 EC_ZEBRA_IRDP_BAD_CHECKSUM
,
132 "IRDP: RX ICMP packet from %pI4 Bad checksum, silently ignored",
137 /* Handle just only IRDP */
138 if (!(icmp
->type
== ICMP_ROUTERADVERT
139 || icmp
->type
== ICMP_ROUTERSOLICIT
))
142 if (icmp
->code
!= 0) {
144 EC_ZEBRA_IRDP_BAD_TYPE_CODE
,
145 "IRDP: RX packet type %d from %pI4 Bad ICMP type code, silently ignored",
150 if (!((ntohl(ip
->ip_dst
.s_addr
) == INADDR_BROADCAST
)
151 && (irdp
->flags
& IF_BROADCAST
))
152 || (ntohl(ip
->ip_dst
.s_addr
) == INADDR_ALLRTRS_GROUP
153 && !(irdp
->flags
& IF_BROADCAST
))) {
155 EC_ZEBRA_IRDP_BAD_RX_FLAGS
,
156 "IRDP: RX illegal from %pI4 to %s while %s operates in %s; Please correct settings\n",
158 ntohl(ip
->ip_dst
.s_addr
) == INADDR_ALLRTRS_GROUP
160 : inet_ntop(AF_INET
, &ip
->ip_dst
,
163 irdp
->flags
& IF_BROADCAST
? "broadcast" : "multicast");
167 switch (icmp
->type
) {
168 case ICMP_ROUTERADVERT
:
171 case ICMP_ROUTERSOLICIT
:
173 if (irdp
->flags
& IF_DEBUG_MESSAGES
)
174 zlog_debug("IRDP: RX Solicit on %s from %pI4",
177 process_solicit(ifp
);
182 EC_ZEBRA_IRDP_BAD_TYPE_CODE
,
183 "IRDP: RX packet type %d from %pI4 Bad ICMP type code, silently ignored",
188 static int irdp_recvmsg(int sock
, uint8_t *buf
, int size
, int *ifindex
)
192 char adata
[CMSG_SPACE(SOPT_SIZE_CMSG_PKTINFO_IPV4())];
195 memset(&msg
, 0, sizeof(msg
));
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 flog_warn(EC_LIB_SOCKET
, "IRDP: recvmsg: read error %s",
209 safe_strerror(errno
));
213 if (msg
.msg_flags
& MSG_TRUNC
) {
214 flog_warn(EC_LIB_SOCKET
, "IRDP: recvmsg: truncated message");
217 if (msg
.msg_flags
& MSG_CTRUNC
) {
218 flog_warn(EC_LIB_SOCKET
,
219 "IRDP: recvmsg: truncated control message");
223 *ifindex
= getsockopt_ifindex(AF_INET
, &msg
);
228 int irdp_read_raw(struct thread
*r
)
230 struct interface
*ifp
;
232 struct irdp_interface
*irdp
;
233 char buf
[IRDP_RX_BUF
];
234 int ret
, ifindex
= 0;
236 int irdp_sock
= THREAD_FD(r
);
238 thread_add_read(zrouter
.master
, irdp_read_raw
, NULL
, irdp_sock
,
241 ret
= irdp_recvmsg(irdp_sock
, (uint8_t *)buf
, IRDP_RX_BUF
, &ifindex
);
244 flog_warn(EC_LIB_SOCKET
, "IRDP: RX Error length = %d", ret
);
246 ifp
= if_lookup_by_index(ifindex
, VRF_DEFAULT
);
258 if (!(irdp
->flags
& IF_ACTIVE
)) {
260 if (irdp
->flags
& IF_DEBUG_MISC
)
261 zlog_debug("IRDP: RX ICMP for disabled interface %s",
266 if (irdp
->flags
& IF_DEBUG_PACKET
) {
268 zlog_debug("IRDP: RX (idx %d) ", ifindex
);
269 for (i
= 0; i
< ret
; i
++)
270 zlog_debug("IRDP: RX %x ", buf
[i
] & 0xFF);
273 parse_irdp_packet(buf
, ret
, ifp
);
278 void send_packet(struct interface
*ifp
, struct stream
*s
, uint32_t dst
,
279 struct prefix
*p
, uint32_t ttl
)
281 static struct sockaddr_in sockdst
= {AF_INET
};
283 struct icmphdr
*icmp
;
285 struct cmsghdr
*cmsg
;
286 struct iovec iovector
;
289 struct in_pktinfo
*pktinfo
;
293 if (!(ifp
->flags
& IFF_UP
))
297 src
= ntohl(p
->u
.prefix4
.s_addr
);
299 src
= 0; /* Is filled in */
301 ip
= (struct ip
*)buf
;
302 ip
->ip_hl
= sizeof(struct ip
) >> 2;
303 ip
->ip_v
= IPVERSION
;
306 ip
->ip_p
= 1; /* IP_ICMP */
308 ip
->ip_src
.s_addr
= src
;
309 ip
->ip_dst
.s_addr
= dst
;
310 icmp
= (struct icmphdr
*)(buf
+ sizeof(struct ip
));
312 /* Merge IP header with icmp packet */
313 assert(stream_get_endp(s
) < (sizeof(buf
) - sizeof(struct ip
)));
314 stream_get(icmp
, s
, stream_get_endp(s
));
316 /* icmp->checksum is already calculated */
317 ip
->ip_len
= sizeof(struct ip
) + stream_get_endp(s
);
320 if (setsockopt(irdp_sock
, IPPROTO_IP
, IP_HDRINCL
, (char *)&on
,
323 flog_err(EC_LIB_SOCKET
,
324 "IRDP: Cannot set IP_HDRINCLU %s(%d) on %s",
325 safe_strerror(errno
), errno
, ifp
->name
);
328 if (dst
== INADDR_BROADCAST
) {
331 if (setsockopt(irdp_sock
, SOL_SOCKET
, SO_BROADCAST
, &bon
,
334 flog_err(EC_LIB_SOCKET
,
335 "IRDP: Cannot set SO_BROADCAST %s(%d) on %s",
336 safe_strerror(errno
), errno
, ifp
->name
);
339 if (dst
!= INADDR_BROADCAST
)
340 setsockopt_ipv4_multicast_loop(irdp_sock
, 0);
342 memset(&sockdst
, 0, sizeof(sockdst
));
343 sockdst
.sin_family
= AF_INET
;
344 sockdst
.sin_addr
.s_addr
= dst
;
346 cmsg
= (struct cmsghdr
*)(msgbuf
+ sizeof(struct msghdr
));
347 cmsg
->cmsg_len
= sizeof(struct cmsghdr
) + sizeof(struct in_pktinfo
);
348 cmsg
->cmsg_level
= SOL_IP
;
349 cmsg
->cmsg_type
= IP_PKTINFO
;
350 pktinfo
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
351 pktinfo
->ipi_ifindex
= ifp
->ifindex
;
352 pktinfo
->ipi_spec_dst
.s_addr
= src
;
353 pktinfo
->ipi_addr
.s_addr
= src
;
355 iovector
.iov_base
= (void *)buf
;
356 iovector
.iov_len
= ip
->ip_len
;
357 msg
= (struct msghdr
*)msgbuf
;
358 msg
->msg_name
= &sockdst
;
359 msg
->msg_namelen
= sizeof(sockdst
);
360 msg
->msg_iov
= &iovector
;
362 msg
->msg_control
= cmsg
;
363 msg
->msg_controllen
= cmsg
->cmsg_len
;
365 sockopt_iphdrincl_swab_htosys(ip
);
367 if (sendmsg(irdp_sock
, msg
, 0) < 0)
368 flog_err(EC_LIB_SOCKET
,
369 "IRDP: sendmsg send failure %s(%d) on %s",
370 safe_strerror(errno
), errno
, ifp
->name
);