?? ripngd.c
字號:
/* RIPng daemon * Copyright (C) 1998, 1999 Kunihiro Ishiguro * * This file is part of GNU Zebra. * * GNU Zebra is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2, or (at your option) any * later version. * * GNU Zebra is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with GNU Zebra; see the file COPYING. If not, write to the Free * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA. */#include <zebra.h>/* For struct udphdr. */#include <netinet/udp.h>#include "prefix.h"#include "filter.h"#include "log.h"#include "thread.h"#include "memory.h"#include "if.h"#include "stream.h"#include "table.h"#include "command.h"#include "sockopt.h"#include "distribute.h"#include "plist.h"#include "routemap.h"#include "ripngd/ripngd.h"#include "ripngd/ripng_route.h"#include "ripngd/ripng_debug.h"#include "ripngd/ripng_ifrmap.h"#define min(a, b) ((a) < (b) ? (a) : (b))/* RIPng structure which includes many parameters related to RIPng protocol. If ripng couldn't active or ripng doesn't configured, ripng->fd must be negative value. */struct ripng *ripng = NULL;enum{ ripng_all_route, ripng_changed_route, ripng_split_horizon, ripng_no_split_horizon};/* Prototypes. */voidripng_output_process (struct interface *, struct sockaddr_in6 *, int, int);intripng_triggered_update (struct thread *);/* RIPng next hop specification. */struct ripng_nexthop{ enum ripng_nexthop_type { RIPNG_NEXTHOP_UNSPEC, RIPNG_NEXTHOP_ADDRESS } flag; struct in6_addr address;};/* Utility function for making IPv6 address string. */const char *inet6_ntop (struct in6_addr *p){ static char buf[INET6_ADDRSTRLEN]; inet_ntop (AF_INET6, p, buf, INET6_ADDRSTRLEN); return buf;}/* Allocate new ripng information. */struct ripng_info *ripng_info_new (){ struct ripng_info *new; new = XCALLOC (MTYPE_RIPNG_ROUTE, sizeof (struct ripng_info)); return new;}/* Free ripng information. */voidripng_info_free (struct ripng_info *rinfo){ XFREE (MTYPE_RIPNG_ROUTE, rinfo);}static intsetsockopt_so_recvbuf (int sock, int size){ int ret; ret = setsockopt (sock, SOL_SOCKET, SO_RCVBUF, (char *) &size, sizeof (int)); if (ret < 0) zlog (NULL, LOG_ERR, "can't setsockopt SO_RCVBUF"); return ret;}/* Create ripng socket. */int ripng_make_socket (void){ int ret; int sock; struct sockaddr_in6 ripaddr; sock = socket (AF_INET6, SOCK_DGRAM, 0); if (sock < 0) { zlog (NULL, LOG_ERR, "Can't make ripng socket"); return sock; } ret = setsockopt_so_recvbuf (sock, 8096); if (ret < 0) return ret; ret = setsockopt_ipv6_pktinfo (sock, 1); if (ret < 0) return ret; ret = setsockopt_ipv6_multicast_hops (sock, 255); if (ret < 0) return ret; ret = setsockopt_ipv6_multicast_loop (sock, 0); if (ret < 0) return ret; ret = setsockopt_ipv6_hoplimit (sock, 1); if (ret < 0) return ret; memset (&ripaddr, 0, sizeof (ripaddr)); ripaddr.sin6_family = AF_INET6;#ifdef SIN6_LEN ripaddr.sin6_len = sizeof (struct sockaddr_in6);#endif /* SIN6_LEN */ ripaddr.sin6_port = htons (RIPNG_PORT_DEFAULT); ret = bind (sock, (struct sockaddr *) &ripaddr, sizeof (ripaddr)); if (ret < 0) { zlog (NULL, LOG_ERR, "Can't bind ripng socket: %s.", strerror (errno)); return ret; } return sock;}/* Send RIPng packet. */intripng_send_packet (caddr_t buf, int bufsize, struct sockaddr_in6 *to, struct interface *ifp){ int ret; struct msghdr msg; struct iovec iov; struct cmsghdr *cmsgptr; char adata [256]; struct in6_pktinfo *pkt; struct sockaddr_in6 addr;#ifdef DEBUG if (to) zlog_info ("DEBUG RIPng: send to %s", inet6_ntop (&to->sin6_addr)); zlog_info ("DEBUG RIPng: send if %s", ifp->name); zlog_info ("DEBUG RIPng: send packet size %d", bufsize);#endif /* DEBUG */ memset (&addr, 0, sizeof (struct sockaddr_in6)); addr.sin6_family = AF_INET6;#ifdef SIN6_LEN addr.sin6_len = sizeof (struct sockaddr_in6);#endif /* SIN6_LEN */ addr.sin6_flowinfo = htonl (RIPNG_PRIORITY_DEFAULT); /* When destination is specified. */ if (to != NULL) { addr.sin6_addr = to->sin6_addr; addr.sin6_port = to->sin6_port; } else { inet_pton(AF_INET6, RIPNG_GROUP, &addr.sin6_addr); addr.sin6_port = htons (RIPNG_PORT_DEFAULT); } msg.msg_name = (void *) &addr; msg.msg_namelen = sizeof (struct sockaddr_in6); msg.msg_iov = &iov; msg.msg_iovlen = 1; msg.msg_control = (void *) adata; msg.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo)); iov.iov_base = buf; iov.iov_len = bufsize; cmsgptr = (struct cmsghdr *)adata; cmsgptr->cmsg_len = CMSG_LEN(sizeof (struct in6_pktinfo)); cmsgptr->cmsg_level = IPPROTO_IPV6; cmsgptr->cmsg_type = IPV6_PKTINFO; pkt = (struct in6_pktinfo *) CMSG_DATA (cmsgptr); memset (&pkt->ipi6_addr, 0, sizeof (struct in6_addr)); pkt->ipi6_ifindex = ifp->ifindex; ret = sendmsg (ripng->sock, &msg, 0); if (ret < 0) zlog_warn ("RIPng send fail on %s: %s", ifp->name, strerror (errno)); return ret;}/* Receive UDP RIPng packet from socket. */intripng_recv_packet (int sock, u_char *buf, int bufsize, struct sockaddr_in6 *from, unsigned int *ifindex, int *hoplimit){ int ret; struct msghdr msg; struct iovec iov; struct cmsghdr *cmsgptr; struct in6_addr dst; /* Ancillary data. This store cmsghdr and in6_pktinfo. But at this point I can't determine size of cmsghdr */ char adata[1024]; /* Fill in message and iovec. */ msg.msg_name = (void *) from; msg.msg_namelen = sizeof (struct sockaddr_in6); msg.msg_iov = &iov; msg.msg_iovlen = 1; msg.msg_control = (void *) adata; msg.msg_controllen = sizeof adata; iov.iov_base = buf; iov.iov_len = bufsize; /* If recvmsg fail return minus value. */ ret = recvmsg (sock, &msg, 0); if (ret < 0) return ret; for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL; cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) { /* I want interface index which this packet comes from. */ if (cmsgptr->cmsg_level == IPPROTO_IPV6 && cmsgptr->cmsg_type == IPV6_PKTINFO) { struct in6_pktinfo *ptr; ptr = (struct in6_pktinfo *) CMSG_DATA (cmsgptr); *ifindex = ptr->ipi6_ifindex; dst = ptr->ipi6_addr; if (*ifindex == 0) zlog_warn ("Interface index returned by IPV6_PKTINFO is zero"); } /* Incoming packet's multicast hop limit. */ if (cmsgptr->cmsg_level == IPPROTO_IPV6 && cmsgptr->cmsg_type == IPV6_HOPLIMIT) *hoplimit = *((int *) CMSG_DATA (cmsgptr)); } /* Hoplimit check shold be done when destination address is multicast address. */ if (! IN6_IS_ADDR_MULTICAST (&dst)) *hoplimit = -1; return ret;}/* Dump rip packet */voidripng_packet_dump (struct ripng_packet *packet, int size, char *sndrcv){ caddr_t lim; struct rte *rte; char *command_str; /* Set command string. */ if (packet->command == RIPNG_REQUEST) command_str = "request"; else if (packet->command == RIPNG_RESPONSE) command_str = "response"; else command_str = "unknown"; /* Dump packet header. */ zlog_info ("%s %s version %d packet size %d", sndrcv, command_str, packet->version, size); /* Dump each routing table entry. */ rte = packet->rte; for (lim = (caddr_t) packet + size; (caddr_t) rte < lim; rte++) { if (rte->metric == RIPNG_METRIC_NEXTHOP) zlog_info (" nexthop %s/%d", inet6_ntop (&rte->addr), rte->prefixlen); else zlog_info (" %s/%d metric %d tag %d", inet6_ntop (&rte->addr), rte->prefixlen, rte->metric, ntohs (rte->tag)); }}/* RIPng next hop address RTE (Route Table Entry). */voidripng_nexthop_rte (struct rte *rte, struct sockaddr_in6 *from, struct ripng_nexthop *nexthop){ char buf[INET6_BUFSIZ]; /* Logging before checking RTE. */ if (IS_RIPNG_DEBUG_RECV) zlog_info ("RIPng nexthop RTE address %s tag %d prefixlen %d", inet6_ntop (&rte->addr), ntohs (rte->tag), rte->prefixlen); /* RFC2080 2.1.1 Next Hop: The route tag and prefix length in the next hop RTE must be set to zero on sending and ignored on receiption. */ if (ntohs (rte->tag) != 0) zlog_warn ("RIPng nexthop RTE with non zero tag value %d from %s", ntohs (rte->tag), inet6_ntop (&from->sin6_addr)); if (rte->prefixlen != 0) zlog_warn ("RIPng nexthop RTE with non zero prefixlen value %d from %s", rte->prefixlen, inet6_ntop (&from->sin6_addr)); /* Specifying a value of 0:0:0:0:0:0:0:0 in the prefix field of a next hop RTE indicates that the next hop address should be the originator of the RIPng advertisement. An address specified as a next hop must be a link-local address. */ if (IN6_IS_ADDR_UNSPECIFIED (&rte->addr)) { nexthop->flag = RIPNG_NEXTHOP_UNSPEC; memset (&nexthop->address, 0, sizeof (struct in6_addr)); return; } if (IN6_IS_ADDR_LINKLOCAL (&rte->addr)) { nexthop->flag = RIPNG_NEXTHOP_ADDRESS; IPV6_ADDR_COPY (&nexthop->address, &rte->addr); return; } /* The purpose of the next hop RTE is to eliminate packets being routed through extra hops in the system. It is particularly useful when RIPng is not being run on all of the routers on a network. Note that next hop RTE is "advisory". That is, if the provided information is ignored, a possibly sub-optimal, but absolutely valid, route may be taken. If the received next hop address is not a link-local address, it should be treated as 0:0:0:0:0:0:0:0. */ zlog_warn ("RIPng nexthop RTE with non link-local address %s from %s", inet6_ntop (&rte->addr), inet_ntop (AF_INET6, &from->sin6_addr, buf, INET6_BUFSIZ)); nexthop->flag = RIPNG_NEXTHOP_UNSPEC; memset (&nexthop->address, 0, sizeof (struct in6_addr)); return;}/* If ifp has same link-local address then return 1. */intripng_lladdr_check (struct interface *ifp, struct in6_addr *addr){ listnode listnode; struct connected *connected; struct prefix *p; for (listnode = listhead (ifp->connected); listnode; nextnode (listnode)) if ((connected = getdata (listnode)) != NULL) { p = connected->address; if (p->family == AF_INET6 && IN6_IS_ADDR_LINKLOCAL (&p->u.prefix6) && IN6_ARE_ADDR_EQUAL (&p->u.prefix6, addr)) return 1; } return 0;}/* RIPng route garbage collect timer. */intripng_garbage_collect (struct thread *t){ struct ripng_info *rinfo; struct route_node *rp; rinfo = THREAD_ARG (t); rinfo->t_garbage_collect = NULL; /* Off timeout timer. */ RIPNG_TIMER_OFF (rinfo->t_timeout); /* Get route_node pointer. */ rp = rinfo->rp; /* Delete this route from the kernel. */ ripng_zebra_ipv6_delete ((struct prefix_ipv6 *)&rp->p, &rinfo->nexthop, rinfo->ifindex); rinfo->flags &= ~RIPNG_RTF_FIB; /* Aggregate count decrement. */ ripng_aggregate_decrement (rp, rinfo); /* Unlock route_node. */ rp->info = NULL; route_unlock_node (rp); /* Free RIPng routing information. */ ripng_info_free (rinfo); return 0;}/* Timeout RIPng routes. */intripng_timeout (struct thread *t){ struct ripng_info *rinfo; struct route_node *rp; rinfo = THREAD_ARG (t); rinfo->t_timeout = NULL; /* Get route_node pointer. */ rp = rinfo->rp; /* - The garbage-collection timer is set for 120 seconds. */ RIPNG_TIMER_ON (rinfo->t_garbage_collect, ripng_garbage_collect, ripng->garbage_time); /* - The metric for the route is set to 16 (infinity). This causes the route to be removed from service. */ rinfo->metric = RIPNG_METRIC_INFINITY; /* - The route change flag is to indicate that this entry has been changed. */ rinfo->flags |= RIPNG_RTF_CHANGED; /* - The output process is signalled to trigger a response. */ ripng_event (RIPNG_TRIGGERED_UPDATE, 0); return 0;}voidripng_timeout_update (struct ripng_info *rinfo){ if (rinfo->metric != RIPNG_METRIC_INFINITY) { RIPNG_TIMER_OFF (rinfo->t_timeout); RIPNG_TIMER_ON (rinfo->t_timeout, ripng_timeout, ripng->timeout_time); }}/* Process RIPng route according to RFC2080. */voidripng_route_process (struct rte *rte, struct sockaddr_in6 *from, struct ripng_nexthop *ripng_nexthop, struct interface *ifp){ struct prefix_ipv6 p; struct route_node *rp; struct ripng_info *rinfo; struct ripng_interface *ri; struct in6_addr *nexthop; u_char oldmetric; int same = 0; /* Make prefix structure. */ memset (&p, 0, sizeof (struct prefix_ipv6)); p.family = AF_INET6; /* p.prefix = rte->addr; */ IPV6_ADDR_COPY (&p.prefix, &rte->addr); p.prefixlen = rte->prefixlen; /* Make sure mask is applied. */ /* XXX We have to check the prefix is valid or not before call
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -