📄 ipripng.c
字号:
/* * Copyright 2006 Wind River Systems, Inc. * * The right to copy, distribute, modify or otherwise make use * of this software may be licensed only pursuant to the terms * of an applicable Wind River license agreement. *//*modification history--------------------01i,05nov08,rme Added 'ipripng_sflag' (WIND00141137)01h,25sep08,rme Cleanup after coverity report01g,23feb07,tlu Initialize the message flag and socket's scope id when sending a message01f,24jan07,tlu Fix gnu compiling warnings01e,24jan07,kch Convert VxWorks types and functions to IPCOM equivalent01d,23jan07,tlu Redefine ripng_cmdString as how it is defined in ipripng_daemon.c01c,20nov06,tlu Remove loopback IF from RIPng IF list and set IP_IPV6_PKTINFO socket option for RIPng socket01b,21sep06,tlu Fix a typo01a,12jun06,tlu Initial creation*//* **************************************************************************** * 1 DESCRIPTION **************************************************************************** * Interpeak Routing Information Protocol - IPRIPng * ------------------------------------------------- * RTOS and TCP/IP stack portable implementation of Routing Information * Protocol RIPng. * * Compliant with the following RFC: * RFC 2080 RIPng * *//* **************************************************************************** * 2 CONFIGURATION **************************************************************************** */#define IPCOM_USE_ANDORNOT/* **************************************************************************** * 3 INCLUDE FILES **************************************************************************** */#define IPCOM_USE_CLIB_PROTO#include <ipcom_type.h>#include <ipcom_os.h>#include <ipcom_list.h>#include <ipcom_clib.h>#include <ipcom_time.h>#include <ipcom_sock.h>#include <ipcom_sock2.h>#include <ipcom_sock6.h>#include <ipcom_tmo.h>#include <ipcom_tags.h>#include <ipcom_err.h>#include <ipcom_syslog.h>#include <ipcom_sysvar.h>#include <ipcom_sysctl.h>#include <ipnet_h.h>#include <ipnet_ip6.h>#include "ipripng.h"#include "ipripng_constant.h"#include "ipripng_config.h"#ifdef IPRIPNG_INCLUDE_MD5#include <openssl/md5.h>#endif/* VxWorks specific?? */#include <unistd.h>/* **************************************************************************** * 4 EXTERN PROTOTYPES **************************************************************************** *//* **************************************************************************** * 5 DEFINES **************************************************************************** *//* Syslog priority and facility. *//*#define IPCOM_SYSLOG_PRIORITY IPRIPNG_SYSLOG_PRIORITY#define IPCOM_SYSLOG_FACILITY IPCOM_LOG_IPRIPNG*/#if 0/* This macro returns the first RIPng entry index to use. */#define FIRST_RIPENTRY_INDEX(xxifp) ((xxifp)->authtype != IPRIP_AUTHTYPE_NONE ? 1 : 0)#endif#define IPRIPNG_ENABLE_REENTRANT#define IPRIPNG_LEAVE(xerrval) do { retval = xerrval; goto leave; } while(0)#ifdef IPRIPNG_ENABLE_REENTRANT#define IPRIPNG_ISLOCKED_ASSERT() ip_assert(ipripng.code_mutex_count == 1)#define IPRIPNG_LOCK() do { ipcom_mutex_lock(ipripng.code_mutex); \ ip_assert(ipripng.code_mutex_count == 0); \ ipripng.code_mutex_count++; \ } while(0)#define IPRIPNG_UNLOCK() do { ip_assert(ipripng.code_mutex_count == 1); \ ipripng.code_mutex_count--; \ ipcom_mutex_unlock(ipripng.code_mutex); \ } while(0)#else#define IPRIPNG_LOCK() IP_NOOP#define IPRIPNG_UNLOCK() IP_NOOP#endif#define SIMPLE_MALLOC(type) ((type *)ipcom_malloc(sizeof(type)))#define IN6_LINKLOCAL_IFINDEX(addr) \ ((addr).s6_addr[2] << 8 | (addr).s6_addr[3])#define RIPSIZE(n) \ (sizeof(struct rip6) + ((n)-1) * sizeof(struct netinfo6))/* **************************************************************************** * 6 TYPES **************************************************************************** *//* *=========================================================================== * Global ipripng data *=========================================================================== */struct rip6 { Ip_u8 rip6_cmd; Ip_u8 rip6_vers; Ip_u8 rip6_res1[2]; struct netinfo6 rip6_nets[1];};typedef struct Ipripng_data_struct{ Ip_bool opened; Ipcom_mutex code_mutex; /* Mutex to protect RIPng global data. */ int code_mutex_count; Ipcom_tmo update; /* Timeout til next update (ca 30 sec). */ Ip_u32 rndtmo; /* Seconds til next RIPng update. */ Ipcom_tmo flash; /* Timeout til next flash (0-3 sec). */ Ip_u16 update_inst; /* Trick to avoid parsing routes after update. */ Ip_u16 pad; Ipcom_route *rtab; Ipcom_list rt_head; char str[16]; /* Tong: to be replaced by rtab and rt_head */ struct Ipripng_rt *riprt; int rt_num; /* number of valid routes */ Ip_fd udp_fd; /* RIPng socket fd */ Ip_fd rt_fd; /* routing socket fd */ Ip_err (*ripng_write) (Ip_fd udp_fd, void *cookie, Ip_u32 srcaddr_n, Ipcom_sock_addr *dest, void *buf, Ip_size_t len); Ip_err (*add_route) (Ipripng_rt *rrt, const struct in6_addr *gw, struct ifc *ifcp); Ip_err (*delete_route) (struct netinfo6 *np, const struct in6_addr *gw, Ip_bool proto_set); /* Interface parameters */ struct ifc *loopifcp; /* pointing to loopback */ struct ifc *ifc; /* pointer to RIPng interfaces */ int nifc; /* number of valid ifc's */ struct ifc **index2ifc; /* map of if index to RIPng interface */ int nindex2ifc; /* Configuration. */ int update_seconds; int update_deltaseconds; int expire_seconds; int garbage_seconds; int flash_seconds; int auth_requests; char *filter[MAXARGS]; /* configured filter */ int filtertype[MAXARGS]; int nfilter; char *ifhoriz[MAXARGS]; /* configured Horizon */ int ifhoriztype[MAXARGS]; int nifhoriz; int rflag; /* restricted neighor list */ struct nb_list * nb_list_root; unsigned long routetag; /* route tag attached on originating case */ int aflag; /* age out even the statically defined routes */ int dflag; /* debug flag */ int pflag; /* disable poison reverse for new interfaces */ int hflag; /* disable split horizon for new interfaces */ int lflag; /* exchange site local routes */ int nflag; /* don't update kernel routing table */ int qflag; /* quiet flag */ int sflag; /* announce static routes w/ split horizon */ int Sflag; /* announce static routes to every interface */ /* I/O */ struct rip6 *ripngbuf; /* RIPng packet buffer for sending */}Ipripng_data;/* **************************************************************************** * 7 LOCAL PROTOTYPES **************************************************************************** *//* main */IP_STATIC int ipripng_sendpacket(struct Ip_sockaddr_in6 *sin6, int len);IP_STATIC void ipripng_flush(struct ifc *ifcp, struct Ip_sockaddr_in6 *sin6);IP_STATIC void ipripng_send_response(struct ifc *ifcp, struct Ip_sockaddr_in6 *sin6, int flag);IP_STATIC void ipripng_process_request(struct ifc *ifcp, struct netinfo6 *np, int nn, struct Ip_sockaddr_in6 *sin6);/* interfaces. */IP_STATIC struct ifc *ipripng_ifget(Ip_u32 ifindex, Ip_u32 ipaddr_n);IP_STATIC Ip_err ipripng_ifconfig1(const char *name, const struct Ip_sockaddr *sa, struct ifc *ifcp, int s);IP_STATIC void ipripng_ifdel (struct ifc * ifcp_todel);IP_STATIC Ip_err setindex2ifc(int idx, struct ifc *ifcp);IP_STATIC Ip_err ipripng_ifrt_p2p(struct ifc *ifcp, int again);/* routes. */IP_STATIC Ipripng_rt *ipripng_rtalloc(void);IP_STATIC void ipripng_rtfree(Ipripng_rt *rt, Ip_bool proto_set);IP_STATIC Ip_err ipripng_rtinsert(Ipripng_rt *rt, struct ifc *ifcp);IP_STATIC void ipripng_rtremove(Ipripng_rt *rt);IP_STATIC Ipripng_rt *ipripng_rtlookup(struct netinfo6 *np, Ipripng_rt **prev_rrt);IP_STATIC void ipripng_rtnotify(Ipcom_route *rtab, Ipcom_route_entry *entry, int code);IP_STATIC void ipripng_rtflash(Ipripng_rt *rt);IP_STATIC int rt_del(const struct Ip_sockaddr_in6 *sdst, const struct Ip_sockaddr_in6 *sgw, const struct Ip_sockaddr_in6 *smask);IP_STATIC int rt_change(const struct Ip_sockaddr_in6 *sdst, const struct Ip_sockaddr_in6 *sgw, const struct Ip_sockaddr_in6 *smask, int index, int flags);IP_STATIC int rt_deladdr(struct ifc *ifcp, const struct Ip_sockaddr_in6 *sifa, const struct Ip_sockaddr_in6 *smask);IP_STATIC Ip_err rt_entry(struct Ipnet_rt_msghdr *rtm, int again);/* RIPng update/flash timeout. */IP_STATIC Ip_s32 ipripng_update_timeout(Ipcom_tmo *tmo, void *cookie);IP_STATIC Ip_s32 ipripng_flash_timeout(Ipcom_tmo *tmo, void *cookie);/* RIPng configuration parsing */IP_STATIC void ipripng_horiz_configset (struct ifc * ifcp);IP_STATIC void ipripng_neighboradd(char * optarg);/* **************************************************************************** * 8 DATA **************************************************************************** */IP_STATIC struct Ip_sockaddr_storage ripngsin;/* * ipripng_flush flushes the rip datagram stored in the rip buffer */IP_STATIC int priv_ripng_nrt;IP_STATIC struct netinfo6 *priv_ripng_np;IP_EXTERN char ripng_cmdString[IPRIPNG_MAXARGS + 2];IP_EXTERN Ip_bool ripngStopFlag;/* *=========================================================================== * Global ipripng data *=========================================================================== * All global IPRIPng data is located in this structure. */IP_STATIC Ipripng_data ipripng;/* **************************************************************************** * 9 LOCAL FUNCTIONS **************************************************************************** *//* *=========================================================================== * ipripng_ifget *=========================================================================== * Description: Find a RIPng interface with interface index 'ifindex' or * IP address 'ipaddr_n'. * Parameters: ifindex Interface index or IP_INVAL_IFINDEX. * ipaddr_n IPv6 address. * Returns: Pointer to Interface structure. * */IP_STATIC struct ifc *ipripng_ifget(Ip_u32 ifindex, Ip_u32 ipaddr_n){ /* Check the interface index if within range, i.e. valid. */ if (ifindex < ipripng.nindex2ifc) { return ipripng.index2ifc[ifindex]; } /* else look for an IP address match. */ else { struct ifc *ifp; struct ifac *ifacp; Ip_s32 i; for (i = 0; i < ipripng.nindex2ifc; i++) { ifp = ipripng.index2ifc[i]; if (ifp) { for (ifacp = ifp->ifc_addr; ifacp; ifacp = ifacp->ifa_next) if (IP_IN6_ARE_ADDR_EQUAL(&(ifacp->ifa_addr), (struct in6_addr *)ipaddr_n)) { return ifp; } } } } return IP_NULL;}/* *=========================================================================== * ipripng_rtalloc *=========================================================================== * Description: Allocate, clear and initialize RIPng route entry structure. * Parameters: . * Returns: Pointer to RIPng route entry structure - Ipripng_rt. * */IP_STATIC Ipripng_rt *ipripng_rtalloc(void){ Ipripng_rt *rt; rt = ipcom_malloc(sizeof(Ipripng_rt)); if (rt == IP_NULL) return IP_NULL; ipcom_memset(rt, 0, sizeof(Ipripng_rt)); /* internal info. */ /* internal info. */ rt->keymask[0] = 0xffffffff; rt->keymask[1] = 0xffffffff; rt->update_inst = ipripng.update_inst; /* new route, flash it. */ return rt;}/* *=========================================================================== * ipripng_rtfree *=========================================================================== * Description: * Parameters: * Returns: * */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -