]>
git.proxmox.com Git - mirror_frr.git/blob - zebra/irdp_packet.c
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 Låås at Swedish University of Agricultural Sciences
32 * for reviewing and tests.
41 #include "sockunion.h"
45 #include "zebra_memory.h"
48 #include "connected.h"
52 #include "zebra/interface.h"
53 #include "zebra/rtadv.h"
54 #include "zebra/rib.h"
55 #include "zebra/zserv.h"
56 #include "zebra/redistribute.h"
57 #include "zebra/irdp.h"
58 #include <netinet/ip_icmp.h>
61 #include "sockunion.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 zlog_err("IRDP: RX length doesnt match IP length");
102 if (iplen
< ICMP_MINLEN
) {
103 zlog_err("IRDP: RX ICMP packet too short from %s\n",
108 /* XXX: RAW doesnt receive link-layer, surely? ??? */
109 /* Check so we don't checksum packets longer than oure RX_BUF - (ethlen
111 len of IP-header) 14+20 */
112 if (iplen
> IRDP_RX_BUF
- 34) {
113 zlog_err("IRDP: RX ICMP packet too long from %s\n",
118 icmp
= (struct icmphdr
*)(p
+ ip_hlen
);
120 /* check icmp checksum */
121 if (in_cksum(icmp
, datalen
) != icmp
->checksum
) {
123 "IRDP: RX ICMP packet from %s. Bad checksum, silently ignored",
128 /* Handle just only IRDP */
129 if (!(icmp
->type
== ICMP_ROUTERADVERT
130 || icmp
->type
== ICMP_ROUTERSOLICIT
))
133 if (icmp
->code
!= 0) {
135 "IRDP: RX packet type %d from %s. Bad ICMP type code,"
137 icmp
->type
, inet_ntoa(src
));
141 if (!((ntohl(ip
->ip_dst
.s_addr
) == INADDR_BROADCAST
)
142 && (irdp
->flags
& IF_BROADCAST
))
143 || (ntohl(ip
->ip_dst
.s_addr
) == INADDR_ALLRTRS_GROUP
144 && !(irdp
->flags
& IF_BROADCAST
))) {
146 "IRDP: RX illegal from %s to %s while %s operates in %s\n",
148 ntohl(ip
->ip_dst
.s_addr
) == INADDR_ALLRTRS_GROUP
150 : inet_ntoa(ip
->ip_dst
),
152 irdp
->flags
& IF_BROADCAST
? "broadcast" : "multicast");
154 zlog_warn("IRDP: Please correct settings\n");
158 switch (icmp
->type
) {
159 case ICMP_ROUTERADVERT
:
162 case ICMP_ROUTERSOLICIT
:
164 if (irdp
->flags
& IF_DEBUG_MESSAGES
)
165 zlog_debug("IRDP: RX Solicit on %s from %s\n",
166 ifp
->name
, inet_ntoa(src
));
168 process_solicit(ifp
);
173 "IRDP: RX type %d from %s. Bad ICMP type, silently ignored",
174 icmp
->type
, inet_ntoa(src
));
178 static int irdp_recvmsg(int sock
, u_char
*buf
, int size
, int *ifindex
)
182 char adata
[CMSG_SPACE(SOPT_SIZE_CMSG_PKTINFO_IPV4())];
185 msg
.msg_name
= (void *)0;
189 msg
.msg_control
= (void *)adata
;
190 msg
.msg_controllen
= sizeof adata
;
195 ret
= recvmsg(sock
, &msg
, 0);
197 zlog_warn("IRDP: recvmsg: read error %s", safe_strerror(errno
));
201 if (msg
.msg_flags
& MSG_TRUNC
) {
202 zlog_warn("IRDP: recvmsg: truncated message");
205 if (msg
.msg_flags
& MSG_CTRUNC
) {
206 zlog_warn("IRDP: recvmsg: truncated control message");
210 *ifindex
= getsockopt_ifindex(AF_INET
, &msg
);
215 int irdp_read_raw(struct thread
*r
)
217 struct interface
*ifp
;
219 struct irdp_interface
*irdp
;
220 char buf
[IRDP_RX_BUF
];
221 int ret
, ifindex
= 0;
223 int irdp_sock
= THREAD_FD(r
);
225 thread_add_read(zebrad
.master
, irdp_read_raw
, NULL
, irdp_sock
,
228 ret
= irdp_recvmsg(irdp_sock
, (u_char
*)buf
, IRDP_RX_BUF
, &ifindex
);
231 zlog_warn("IRDP: RX Error length = %d", ret
);
233 ifp
= if_lookup_by_index(ifindex
, VRF_DEFAULT
);
245 if (!(irdp
->flags
& IF_ACTIVE
)) {
247 if (irdp
->flags
& IF_DEBUG_MISC
)
248 zlog_debug("IRDP: RX ICMP for disabled interface %s\n",
253 if (irdp
->flags
& IF_DEBUG_PACKET
) {
255 zlog_debug("IRDP: RX (idx %d) ", ifindex
);
256 for (i
= 0; i
< ret
; i
++)
257 zlog_debug("IRDP: RX %x ", buf
[i
] & 0xFF);
260 parse_irdp_packet(buf
, ret
, ifp
);
265 void send_packet(struct interface
*ifp
, struct stream
*s
, u_int32_t dst
,
266 struct prefix
*p
, u_int32_t ttl
)
268 static struct sockaddr_in sockdst
= {AF_INET
};
270 struct icmphdr
*icmp
;
272 struct cmsghdr
*cmsg
;
273 struct iovec iovector
;
276 struct in_pktinfo
*pktinfo
;
280 if (!(ifp
->flags
& IFF_UP
))
284 src
= ntohl(p
->u
.prefix4
.s_addr
);
286 src
= 0; /* Is filled in */
288 ip
= (struct ip
*)buf
;
289 ip
->ip_hl
= sizeof(struct ip
) >> 2;
290 ip
->ip_v
= IPVERSION
;
293 ip
->ip_p
= 1; /* IP_ICMP */
295 ip
->ip_src
.s_addr
= src
;
296 ip
->ip_dst
.s_addr
= dst
;
297 icmp
= (struct icmphdr
*)(buf
+ sizeof(struct ip
));
299 /* Merge IP header with icmp packet */
300 assert(stream_get_endp(s
) < (sizeof(buf
) - sizeof(struct ip
)));
301 stream_get(icmp
, s
, stream_get_endp(s
));
303 /* icmp->checksum is already calculated */
304 ip
->ip_len
= sizeof(struct ip
) + stream_get_endp(s
);
307 if (setsockopt(irdp_sock
, IPPROTO_IP
, IP_HDRINCL
, (char *)&on
,
310 zlog_warn("sendto %s", safe_strerror(errno
));
313 if (dst
== INADDR_BROADCAST
) {
315 if (setsockopt(irdp_sock
, SOL_SOCKET
, SO_BROADCAST
, (char *)&on
,
318 zlog_warn("sendto %s", safe_strerror(errno
));
321 if (dst
!= INADDR_BROADCAST
)
322 setsockopt_ipv4_multicast_loop(irdp_sock
, 0);
324 memset(&sockdst
, 0, sizeof(sockdst
));
325 sockdst
.sin_family
= AF_INET
;
326 sockdst
.sin_addr
.s_addr
= dst
;
328 cmsg
= (struct cmsghdr
*)(msgbuf
+ sizeof(struct msghdr
));
329 cmsg
->cmsg_len
= sizeof(struct cmsghdr
) + sizeof(struct in_pktinfo
);
330 cmsg
->cmsg_level
= SOL_IP
;
331 cmsg
->cmsg_type
= IP_PKTINFO
;
332 pktinfo
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
333 pktinfo
->ipi_ifindex
= ifp
->ifindex
;
334 pktinfo
->ipi_spec_dst
.s_addr
= src
;
335 pktinfo
->ipi_addr
.s_addr
= src
;
337 iovector
.iov_base
= (void *)buf
;
338 iovector
.iov_len
= ip
->ip_len
;
339 msg
= (struct msghdr
*)msgbuf
;
340 msg
->msg_name
= &sockdst
;
341 msg
->msg_namelen
= sizeof(sockdst
);
342 msg
->msg_iov
= &iovector
;
344 msg
->msg_control
= cmsg
;
345 msg
->msg_controllen
= cmsg
->cmsg_len
;
347 sockopt_iphdrincl_swab_htosys(ip
);
349 if (sendmsg(irdp_sock
, msg
, 0) < 0) {
350 zlog_warn("sendto %s", safe_strerror(errno
));
352 /* printf("TX on %s idx %d\n", ifp->name, ifp->ifindex); */