]>
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
, uint8_t *buf
, int size
, int *ifindex
)
182 char adata
[CMSG_SPACE(SOPT_SIZE_CMSG_PKTINFO_IPV4())];
185 memset(&msg
, 0, sizeof(msg
));
186 msg
.msg_name
= (void *)0;
190 msg
.msg_control
= (void *)adata
;
191 msg
.msg_controllen
= sizeof adata
;
196 ret
= recvmsg(sock
, &msg
, 0);
198 zlog_warn("IRDP: recvmsg: read error %s", safe_strerror(errno
));
202 if (msg
.msg_flags
& MSG_TRUNC
) {
203 zlog_warn("IRDP: recvmsg: truncated message");
206 if (msg
.msg_flags
& MSG_CTRUNC
) {
207 zlog_warn("IRDP: recvmsg: truncated control message");
211 *ifindex
= getsockopt_ifindex(AF_INET
, &msg
);
216 int irdp_read_raw(struct thread
*r
)
218 struct interface
*ifp
;
220 struct irdp_interface
*irdp
;
221 char buf
[IRDP_RX_BUF
];
222 int ret
, ifindex
= 0;
224 int irdp_sock
= THREAD_FD(r
);
226 thread_add_read(zebrad
.master
, irdp_read_raw
, NULL
, irdp_sock
,
229 ret
= irdp_recvmsg(irdp_sock
, (uint8_t *)buf
, IRDP_RX_BUF
, &ifindex
);
232 zlog_warn("IRDP: RX Error length = %d", ret
);
234 ifp
= if_lookup_by_index(ifindex
, VRF_DEFAULT
);
246 if (!(irdp
->flags
& IF_ACTIVE
)) {
248 if (irdp
->flags
& IF_DEBUG_MISC
)
249 zlog_debug("IRDP: RX ICMP for disabled interface %s\n",
254 if (irdp
->flags
& IF_DEBUG_PACKET
) {
256 zlog_debug("IRDP: RX (idx %d) ", ifindex
);
257 for (i
= 0; i
< ret
; i
++)
258 zlog_debug("IRDP: RX %x ", buf
[i
] & 0xFF);
261 parse_irdp_packet(buf
, ret
, ifp
);
266 void send_packet(struct interface
*ifp
, struct stream
*s
, uint32_t dst
,
267 struct prefix
*p
, uint32_t ttl
)
269 static struct sockaddr_in sockdst
= {AF_INET
};
271 struct icmphdr
*icmp
;
273 struct cmsghdr
*cmsg
;
274 struct iovec iovector
;
277 struct in_pktinfo
*pktinfo
;
281 if (!(ifp
->flags
& IFF_UP
))
285 src
= ntohl(p
->u
.prefix4
.s_addr
);
287 src
= 0; /* Is filled in */
289 ip
= (struct ip
*)buf
;
290 ip
->ip_hl
= sizeof(struct ip
) >> 2;
291 ip
->ip_v
= IPVERSION
;
294 ip
->ip_p
= 1; /* IP_ICMP */
296 ip
->ip_src
.s_addr
= src
;
297 ip
->ip_dst
.s_addr
= dst
;
298 icmp
= (struct icmphdr
*)(buf
+ sizeof(struct ip
));
300 /* Merge IP header with icmp packet */
301 assert(stream_get_endp(s
) < (sizeof(buf
) - sizeof(struct ip
)));
302 stream_get(icmp
, s
, stream_get_endp(s
));
304 /* icmp->checksum is already calculated */
305 ip
->ip_len
= sizeof(struct ip
) + stream_get_endp(s
);
308 if (setsockopt(irdp_sock
, IPPROTO_IP
, IP_HDRINCL
, (char *)&on
,
311 zlog_warn("sendto %s", safe_strerror(errno
));
314 if (dst
== INADDR_BROADCAST
) {
316 if (setsockopt(irdp_sock
, SOL_SOCKET
, SO_BROADCAST
, (char *)&on
,
319 zlog_warn("sendto %s", safe_strerror(errno
));
322 if (dst
!= INADDR_BROADCAST
)
323 setsockopt_ipv4_multicast_loop(irdp_sock
, 0);
325 memset(&sockdst
, 0, sizeof(sockdst
));
326 sockdst
.sin_family
= AF_INET
;
327 sockdst
.sin_addr
.s_addr
= dst
;
329 cmsg
= (struct cmsghdr
*)(msgbuf
+ sizeof(struct msghdr
));
330 cmsg
->cmsg_len
= sizeof(struct cmsghdr
) + sizeof(struct in_pktinfo
);
331 cmsg
->cmsg_level
= SOL_IP
;
332 cmsg
->cmsg_type
= IP_PKTINFO
;
333 pktinfo
= (struct in_pktinfo
*)CMSG_DATA(cmsg
);
334 pktinfo
->ipi_ifindex
= ifp
->ifindex
;
335 pktinfo
->ipi_spec_dst
.s_addr
= src
;
336 pktinfo
->ipi_addr
.s_addr
= src
;
338 iovector
.iov_base
= (void *)buf
;
339 iovector
.iov_len
= ip
->ip_len
;
340 msg
= (struct msghdr
*)msgbuf
;
341 msg
->msg_name
= &sockdst
;
342 msg
->msg_namelen
= sizeof(sockdst
);
343 msg
->msg_iov
= &iovector
;
345 msg
->msg_control
= cmsg
;
346 msg
->msg_controllen
= cmsg
->cmsg_len
;
348 sockopt_iphdrincl_swab_htosys(ip
);
350 if (sendmsg(irdp_sock
, msg
, 0) < 0) {
351 zlog_warn("sendto %s", safe_strerror(errno
));
353 /* printf("TX on %s idx %d\n", ifp->name, ifp->ifindex); */