]> git.proxmox.com Git - mirror_frr.git/blame - zebra/irdp_packet.c
*: manual SPDX License ID conversions
[mirror_frr.git] / zebra / irdp_packet.c
CommitLineData
ca776988 1/*
2 *
3 * Copyright (C) 2000 Robert Olsson.
4 * Swedish University of Agricultural Sciences
5 *
6 * This file is part of GNU Zebra.
7 *
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.
12 *
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.
17 *
896014f4
DL
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
ca776988 21 */
22
d62a17ae 23/*
ca776988 24 * This work includes work with the following copywrite:
25 *
26 * Copyright (C) 1997, 2000 Kunihiro Ishiguro
27 *
28 */
29
d62a17ae 30/*
43e52561 31 * Thanks to Jens Laas at Swedish University of Agricultural Sciences
ca776988 32 * for reviewing and tests.
33 */
34
35
36#include <zebra.h>
43e52561 37#include <netinet/ip_icmp.h>
ca776988 38
43e52561 39#include "checksum.h"
ca776988 40#include "command.h"
ca776988 41#include "connected.h"
43e52561
QY
42#include "if.h"
43#include "ioctl.h"
ca776988 44#include "log.h"
43e52561
QY
45#include "log.h"
46#include "memory.h"
47#include "prefix.h"
48#include "sockopt.h"
49#include "sockunion.h"
50#include "sockunion.h"
51#include "stream.h"
ca776988 52#include "thread.h"
43e52561
QY
53#include "vty.h"
54#include "zclient.h"
9df414fe 55#include "lib_errors.h"
43e52561 56
ca776988 57#include "zebra/interface.h"
58#include "zebra/rtadv.h"
59#include "zebra/rib.h"
3801e764 60#include "zebra/zebra_router.h"
ca776988 61#include "zebra/redistribute.h"
62#include "zebra/irdp.h"
43e52561 63#include "zebra/zebra_errors.h"
ca776988 64
65
66/* GLOBAL VARS */
67
2da40f49 68int irdp_sock = -1;
ca776988 69
ca776988 70extern struct thread *t_irdp_raw;
ca776988 71
d62a17ae 72static void parse_irdp_packet(char *p, int len, struct interface *ifp)
ca776988 73{
d62a17ae 74 struct ip *ip = (struct ip *)p;
75 struct icmphdr *icmp;
76 struct in_addr src;
77 int ip_hlen, iplen, datalen;
78 struct zebra_if *zi;
79 struct irdp_interface *irdp;
30b544ed 80 uint16_t saved_chksum;
9bcef951 81 char buf[PREFIX_STRLEN];
d62a17ae 82
83 zi = ifp->info;
84 if (!zi)
85 return;
86
ead4ee99 87 irdp = zi->irdp;
d62a17ae 88 if (!irdp)
89 return;
90
91 ip_hlen = ip->ip_hl << 2;
92
93 sockopt_iphdrincl_swab_systoh(ip);
94
95 iplen = ip->ip_len;
96 datalen = len - ip_hlen;
97 src = ip->ip_src;
98
99 if (len != iplen) {
e914ccbe 100 flog_err(EC_ZEBRA_IRDP_LEN_MISMATCH,
0437e105 101 "IRDP: RX length doesn't match IP length");
d62a17ae 102 return;
103 }
104
105 if (iplen < ICMP_MINLEN) {
e914ccbe 106 flog_err(EC_ZEBRA_IRDP_LEN_MISMATCH,
1d5453d6 107 "IRDP: RX ICMP packet too short from %pI4",
9bcef951 108 &src);
d62a17ae 109 return;
110 }
111
0437e105 112 /* XXX: RAW doesn't receive link-layer, surely? ??? */
d62a17ae 113 /* Check so we don't checksum packets longer than oure RX_BUF - (ethlen
114 +
115 len of IP-header) 14+20 */
116 if (iplen > IRDP_RX_BUF - 34) {
e914ccbe 117 flog_err(EC_ZEBRA_IRDP_LEN_MISMATCH,
1d5453d6 118 "IRDP: RX ICMP packet too long from %pI4",
9bcef951 119 &src);
d62a17ae 120 return;
121 }
122
123 icmp = (struct icmphdr *)(p + ip_hlen);
124
30b544ed
DS
125 saved_chksum = icmp->checksum;
126 icmp->checksum = 0;
d62a17ae 127 /* check icmp checksum */
30b544ed 128 if (in_cksum(icmp, datalen) != saved_chksum) {
9df414fe 129 flog_warn(
e914ccbe 130 EC_ZEBRA_IRDP_BAD_CHECKSUM,
9bcef951
MS
131 "IRDP: RX ICMP packet from %pI4 Bad checksum, silently ignored",
132 &src);
d62a17ae 133 return;
134 }
135
136 /* Handle just only IRDP */
137 if (!(icmp->type == ICMP_ROUTERADVERT
138 || icmp->type == ICMP_ROUTERSOLICIT))
139 return;
140
141 if (icmp->code != 0) {
f203510a
QY
142 flog_warn(
143 EC_ZEBRA_IRDP_BAD_TYPE_CODE,
9bcef951
MS
144 "IRDP: RX packet type %d from %pI4 Bad ICMP type code, silently ignored",
145 icmp->type, &src);
d62a17ae 146 return;
147 }
148
149 if (!((ntohl(ip->ip_dst.s_addr) == INADDR_BROADCAST)
150 && (irdp->flags & IF_BROADCAST))
151 || (ntohl(ip->ip_dst.s_addr) == INADDR_ALLRTRS_GROUP
152 && !(irdp->flags & IF_BROADCAST))) {
9df414fe 153 flog_warn(
e914ccbe 154 EC_ZEBRA_IRDP_BAD_RX_FLAGS,
1d5453d6 155 "IRDP: RX illegal from %pI4 to %s while %s operates in %s; Please correct settings",
9bcef951 156 &src,
d62a17ae 157 ntohl(ip->ip_dst.s_addr) == INADDR_ALLRTRS_GROUP
158 ? "multicast"
9bcef951
MS
159 : inet_ntop(AF_INET, &ip->ip_dst,
160 buf, sizeof(buf)),
d62a17ae 161 ifp->name,
162 irdp->flags & IF_BROADCAST ? "broadcast" : "multicast");
d62a17ae 163 return;
164 }
165
166 switch (icmp->type) {
167 case ICMP_ROUTERADVERT:
168 break;
169
170 case ICMP_ROUTERSOLICIT:
171
172 if (irdp->flags & IF_DEBUG_MESSAGES)
9bcef951
MS
173 zlog_debug("IRDP: RX Solicit on %s from %pI4",
174 ifp->name, &src);
d62a17ae 175
176 process_solicit(ifp);
177 break;
178
179 default:
9df414fe 180 flog_warn(
f203510a 181 EC_ZEBRA_IRDP_BAD_TYPE_CODE,
9bcef951
MS
182 "IRDP: RX packet type %d from %pI4 Bad ICMP type code, silently ignored",
183 icmp->type, &src);
d62a17ae 184 }
ca776988 185}
6b0655a2 186
d7c0a89a 187static int irdp_recvmsg(int sock, uint8_t *buf, int size, int *ifindex)
ca776988 188{
d62a17ae 189 struct msghdr msg;
190 struct iovec iov;
191 char adata[CMSG_SPACE(SOPT_SIZE_CMSG_PKTINFO_IPV4())];
192 int ret;
193
0af35d90 194 memset(&msg, 0, sizeof(msg));
d62a17ae 195 msg.msg_name = (void *)0;
196 msg.msg_namelen = 0;
197 msg.msg_iov = &iov;
198 msg.msg_iovlen = 1;
199 msg.msg_control = (void *)adata;
0d6f7fd6 200 msg.msg_controllen = sizeof(adata);
d62a17ae 201
202 iov.iov_base = buf;
203 iov.iov_len = size;
204
205 ret = recvmsg(sock, &msg, 0);
206 if (ret < 0) {
450971aa 207 flog_warn(EC_LIB_SOCKET, "IRDP: recvmsg: read error %s",
9df414fe 208 safe_strerror(errno));
d62a17ae 209 return ret;
210 }
211
212 if (msg.msg_flags & MSG_TRUNC) {
450971aa 213 flog_warn(EC_LIB_SOCKET, "IRDP: recvmsg: truncated message");
d62a17ae 214 return ret;
215 }
216 if (msg.msg_flags & MSG_CTRUNC) {
450971aa 217 flog_warn(EC_LIB_SOCKET,
9df414fe 218 "IRDP: recvmsg: truncated control message");
d62a17ae 219 return ret;
220 }
221
222 *ifindex = getsockopt_ifindex(AF_INET, &msg);
223
224 return ret;
ca776988 225}
6b0655a2 226
cc9f21da 227void irdp_read_raw(struct thread *r)
ca776988 228{
d62a17ae 229 struct interface *ifp;
230 struct zebra_if *zi;
231 struct irdp_interface *irdp;
232 char buf[IRDP_RX_BUF];
233 int ret, ifindex = 0;
234
235 int irdp_sock = THREAD_FD(r);
3801e764 236 thread_add_read(zrouter.master, irdp_read_raw, NULL, irdp_sock,
d62a17ae 237 &t_irdp_raw);
238
d7c0a89a 239 ret = irdp_recvmsg(irdp_sock, (uint8_t *)buf, IRDP_RX_BUF, &ifindex);
d62a17ae 240
241 if (ret < 0)
450971aa 242 flog_warn(EC_LIB_SOCKET, "IRDP: RX Error length = %d", ret);
d62a17ae 243
244 ifp = if_lookup_by_index(ifindex, VRF_DEFAULT);
245 if (!ifp)
cc9f21da 246 return;
d62a17ae 247
248 zi = ifp->info;
249 if (!zi)
cc9f21da 250 return;
d62a17ae 251
ead4ee99 252 irdp = zi->irdp;
d62a17ae 253 if (!irdp)
cc9f21da 254 return;
d62a17ae 255
256 if (!(irdp->flags & IF_ACTIVE)) {
257
258 if (irdp->flags & IF_DEBUG_MISC)
9165c5f5 259 zlog_debug("IRDP: RX ICMP for disabled interface %s",
d62a17ae 260 ifp->name);
cc9f21da 261 return;
d62a17ae 262 }
263
264 if (irdp->flags & IF_DEBUG_PACKET) {
265 int i;
266 zlog_debug("IRDP: RX (idx %d) ", ifindex);
267 for (i = 0; i < ret; i++)
268 zlog_debug("IRDP: RX %x ", buf[i] & 0xFF);
269 }
270
271 parse_irdp_packet(buf, ret, ifp);
ca776988 272}
6b0655a2 273
d7c0a89a
QY
274void send_packet(struct interface *ifp, struct stream *s, uint32_t dst,
275 struct prefix *p, uint32_t ttl)
ca776988 276{
d62a17ae 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;
d7c0a89a
QY
286 unsigned long src;
287 uint8_t on;
d62a17ae 288
289 if (!(ifp->flags & IFF_UP))
290 return;
291
292 if (p)
293 src = ntohl(p->u.prefix4.s_addr);
294 else
295 src = 0; /* Is filled in */
296
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));
307
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));
311
312 /* icmp->checksum is already calculated */
313 ip->ip_len = sizeof(struct ip) + stream_get_endp(s);
314
315 on = 1;
316 if (setsockopt(irdp_sock, IPPROTO_IP, IP_HDRINCL, (char *)&on,
317 sizeof(on))
318 < 0)
3d0deb9c
DS
319 flog_err(EC_LIB_SOCKET,
320 "IRDP: Cannot set IP_HDRINCLU %s(%d) on %s",
321 safe_strerror(errno), errno, ifp->name);
d62a17ae 322
323
324 if (dst == INADDR_BROADCAST) {
08fa52a8
DS
325 uint32_t bon = 1;
326
327 if (setsockopt(irdp_sock, SOL_SOCKET, SO_BROADCAST, &bon,
328 sizeof(bon))
d62a17ae 329 < 0)
3d0deb9c
DS
330 flog_err(EC_LIB_SOCKET,
331 "IRDP: Cannot set SO_BROADCAST %s(%d) on %s",
332 safe_strerror(errno), errno, ifp->name);
d62a17ae 333 }
334
335 if (dst != INADDR_BROADCAST)
336 setsockopt_ipv4_multicast_loop(irdp_sock, 0);
337
338 memset(&sockdst, 0, sizeof(sockdst));
339 sockdst.sin_family = AF_INET;
340 sockdst.sin_addr.s_addr = dst;
341
342 cmsg = (struct cmsghdr *)(msgbuf + sizeof(struct msghdr));
343 cmsg->cmsg_len = sizeof(struct cmsghdr) + sizeof(struct in_pktinfo);
344 cmsg->cmsg_level = SOL_IP;
345 cmsg->cmsg_type = IP_PKTINFO;
346 pktinfo = (struct in_pktinfo *)CMSG_DATA(cmsg);
347 pktinfo->ipi_ifindex = ifp->ifindex;
348 pktinfo->ipi_spec_dst.s_addr = src;
349 pktinfo->ipi_addr.s_addr = src;
350
351 iovector.iov_base = (void *)buf;
352 iovector.iov_len = ip->ip_len;
353 msg = (struct msghdr *)msgbuf;
354 msg->msg_name = &sockdst;
355 msg->msg_namelen = sizeof(sockdst);
356 msg->msg_iov = &iovector;
357 msg->msg_iovlen = 1;
358 msg->msg_control = cmsg;
359 msg->msg_controllen = cmsg->cmsg_len;
360
361 sockopt_iphdrincl_swab_htosys(ip);
362
3d0deb9c
DS
363 if (sendmsg(irdp_sock, msg, 0) < 0)
364 flog_err(EC_LIB_SOCKET,
365 "IRDP: sendmsg send failure %s(%d) on %s",
366 safe_strerror(errno), errno, ifp->name);
ca776988 367}