⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bgp_routemap.c

📁 大名鼎鼎的路由器源码。程序分ZEBRA、OSPFRIP等3个包。程序框架采用一个路由协议一个进程的方式
💻 C
📖 第 1 页 / 共 5 页
字号:
/* Route map function of bgpd.   Copyright (C) 1998, 1999 Kunihiro IshiguroThis file is part of GNU Zebra.GNU Zebra is free software; you can redistribute it and/or modify itunder the terms of the GNU General Public License as published by theFree Software Foundation; either version 2, or (at your option) anylater version.GNU Zebra is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Zebra; see the file COPYING.  If not, write to the FreeSoftware Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA02111-1307, USA.  */#include <zebra.h>#include "prefix.h"#include "filter.h"#include "routemap.h"#include "command.h"#include "linklist.h"#include "plist.h"#include "memory.h"#include "log.h"#ifdef HAVE_GNU_REGEX#include <regex.h>#else#include "regex-gnu.h"#endif /* HAVE_GNU_REGEX */#include "buffer.h"#include "sockunion.h"#include "bgpd/bgpd.h"#include "bgpd/bgp_table.h"#include "bgpd/bgp_attr.h"#include "bgpd/bgp_aspath.h"#include "bgpd/bgp_route.h"#include "bgpd/bgp_regex.h"#include "bgpd/bgp_community.h"#include "bgpd/bgp_clist.h"#include "bgpd/bgp_filter.h"#include "bgpd/bgp_mplsvpn.h"#include "bgpd/bgp_ecommunity.h"/* Memo of route-map commands.o Cisco route-map match as-path          :  Done       community        :  Done       interface        :  Not yet       ip address       :  Done       ip next-hop      :  Done       ip route-source  :  (This will not be implemented by bgpd)       ip prefix-list   :  Done       ipv6 address     :  Done       ipv6 next-hop    :  Done       ipv6 route-source:  (This will not be implemented by bgpd)       ipv6 prefix-list :  Done       length           :  (This will not be implemented by bgpd)       metric           :  Done       route-type       :  (This will not be implemented by bgpd)       tag              :  (This will not be implemented by bgpd) set  as-path prepend   :  Done      as-path tag       :  Not yet      automatic-tag     :  (This will not be implemented by bgpd)      community         :  Done      comm-list         :  Not yet      dampning          :  Not yet      default           :  (This will not be implemented by bgpd)      interface         :  (This will not be implemented by bgpd)      ip default        :  (This will not be implemented by bgpd)      ip next-hop       :  Done      ip precedence     :  (This will not be implemented by bgpd)      ip tos            :  (This will not be implemented by bgpd)      level             :  (This will not be implemented by bgpd)      local-preference  :  Done      metric            :  Done      metric-type       :  Not yet      origin            :  Done      tag               :  (This will not be implemented by bgpd)      weight            :  Doneo Local extention  set ipv6 next-hop global: Done  set ipv6 next-hop local : Done*/ /* `match ip address IP_ACCESS_LIST' *//* Match function should return 1 if match is success else return   zero. */route_map_result_troute_match_ip_address (void *rule, struct prefix *prefix, 			route_map_object_t type, void *object){  struct access_list *alist;  /* struct prefix_ipv4 match; */  if (type == RMAP_BGP)    {      alist = access_list_lookup (AFI_IP, (char *) rule);      if (alist == NULL)	return RMAP_NOMATCH;          return (access_list_apply (alist, prefix) == FILTER_DENY ?	      RMAP_NOMATCH : RMAP_MATCH);    }  return RMAP_NOMATCH;}/* Route map `ip address' match statement.  `arg' should be   access-list name. */void *route_match_ip_address_compile (char *arg){  return XSTRDUP (MTYPE_ROUTE_MAP_COMPILED, arg);}/* Free route map's compiled `ip address' value. */voidroute_match_ip_address_free (void *rule){  XFREE (MTYPE_ROUTE_MAP_COMPILED, rule);}/* Route map commands for ip address matching. */struct route_map_rule_cmd route_match_ip_address_cmd ={  "ip address",  route_match_ip_address,  route_match_ip_address_compile,  route_match_ip_address_free};/* `match ip next-hop IP_ADDRESS' *//* Match function return 1 if match is success else return zero. */route_map_result_troute_match_ip_next_hop (void *rule, struct prefix *prefix, 			 route_map_object_t type, void *object){  struct access_list *alist;  struct bgp_info *bgp_info;  struct prefix_ipv4 p;  if (type == RMAP_BGP)    {      bgp_info = object;      p.family = AF_INET;      p.prefix = bgp_info->attr->nexthop;      p.prefixlen = IPV4_MAX_BITLEN;      alist = access_list_lookup (AFI_IP, (char *) rule);      if (alist == NULL)	return RMAP_NOMATCH;      return (access_list_apply (alist, &p) == FILTER_DENY ?              RMAP_NOMATCH : RMAP_MATCH);    }  return RMAP_NOMATCH;}/* Route map `ip next-hop' match statement. `arg' is   access-list name. */void *route_match_ip_next_hop_compile (char *arg){  return XSTRDUP (MTYPE_ROUTE_MAP_COMPILED, arg);}/* Free route map's compiled `ip address' value. */voidroute_match_ip_next_hop_free (void *rule){  XFREE (MTYPE_ROUTE_MAP_COMPILED, rule);}/* Route map commands for ip next-hop matching. */struct route_map_rule_cmd route_match_ip_next_hop_cmd ={  "ip next-hop",  route_match_ip_next_hop,  route_match_ip_next_hop_compile,  route_match_ip_next_hop_free};/* `match ip address prefix-list PREFIX_LIST' */route_map_result_troute_match_ip_address_prefix_list (void *rule, struct prefix *prefix, 				    route_map_object_t type, void *object){  struct prefix_list *plist;  if (type == RMAP_BGP)    {      plist = prefix_list_lookup (AFI_IP, (char *) rule);      if (plist == NULL)	return RMAP_NOMATCH;          return (prefix_list_apply (plist, prefix) == PREFIX_DENY ?	      RMAP_NOMATCH : RMAP_MATCH);    }  return RMAP_NOMATCH;}void *route_match_ip_address_prefix_list_compile (char *arg){  return XSTRDUP (MTYPE_ROUTE_MAP_COMPILED, arg);}voidroute_match_ip_address_prefix_list_free (void *rule){  XFREE (MTYPE_ROUTE_MAP_COMPILED, rule);}struct route_map_rule_cmd route_match_ip_address_prefix_list_cmd ={  "ip address prefix-list",  route_match_ip_address_prefix_list,  route_match_ip_address_prefix_list_compile,  route_match_ip_address_prefix_list_free};/* `match ip next-hop prefix-list PREFIX_LIST' */route_map_result_troute_match_ip_next_hop_prefix_list (void *rule, struct prefix *prefix,                                    route_map_object_t type, void *object){  struct prefix_list *plist;  struct bgp_info *bgp_info;  struct prefix_ipv4 p;  if (type == RMAP_BGP)    {      bgp_info = object;      p.family = AF_INET;      p.prefix = bgp_info->attr->nexthop;      p.prefixlen = IPV4_MAX_BITLEN;      plist = prefix_list_lookup (AFI_IP, (char *) rule);      if (plist == NULL)        return RMAP_NOMATCH;      return (prefix_list_apply (plist, &p) == PREFIX_DENY ?              RMAP_NOMATCH : RMAP_MATCH);    }  return RMAP_NOMATCH;}void *route_match_ip_next_hop_prefix_list_compile (char *arg){  return XSTRDUP (MTYPE_ROUTE_MAP_COMPILED, arg);}voidroute_match_ip_next_hop_prefix_list_free (void *rule){  XFREE (MTYPE_ROUTE_MAP_COMPILED, rule);}struct route_map_rule_cmd route_match_ip_next_hop_prefix_list_cmd ={  "ip next-hop prefix-list",  route_match_ip_next_hop_prefix_list,  route_match_ip_next_hop_prefix_list_compile,  route_match_ip_next_hop_prefix_list_free};/* `match metric METRIC' *//* Match function return 1 if match is success else return zero. */route_map_result_troute_match_metric (void *rule, struct prefix *prefix, 		    route_map_object_t type, void *object){  u_int32_t *med;  struct bgp_info *bgp_info;  if (type == RMAP_BGP)    {      med = rule;      bgp_info = object;          if (bgp_info->attr->med == *med)	return RMAP_MATCH;      else	return RMAP_NOMATCH;    }  return RMAP_NOMATCH;}/* Route map `match metric' match statement. `arg' is MED value */void *route_match_metric_compile (char *arg){  u_int32_t *med;  char *endptr = NULL;  med = XMALLOC (MTYPE_ROUTE_MAP_COMPILED, sizeof (u_int32_t));  *med = strtoul (arg, &endptr, 10);  if (*endptr != '\0' || *med == ULONG_MAX)    {      XFREE (MTYPE_ROUTE_MAP_COMPILED, med);      return NULL;    }  return med;}/* Free route map's compiled `match metric' value. */voidroute_match_metric_free (void *rule){  XFREE (MTYPE_ROUTE_MAP_COMPILED, rule);}/* Route map commands for metric matching. */struct route_map_rule_cmd route_match_metric_cmd ={  "metric",  route_match_metric,  route_match_metric_compile,  route_match_metric_free};/* `match as-path ASPATH' *//* Match function for as-path match.  I assume given object is */route_map_result_troute_match_aspath (void *rule, struct prefix *prefix, 		    route_map_object_t type, void *object){    struct as_list *as_list;  struct bgp_info *bgp_info;  if (type == RMAP_BGP)    {      as_list = as_list_lookup ((char *) rule);      if (as_list == NULL)	return RMAP_NOMATCH;          bgp_info = object;          /* Perform match. */      return ((as_list_apply (as_list, bgp_info->attr->aspath) == AS_FILTER_DENY) ? RMAP_NOMATCH : RMAP_MATCH);    }  return RMAP_NOMATCH;}/* Compile function for as-path match. */void *route_match_aspath_compile (char *arg){  return XSTRDUP (MTYPE_ROUTE_MAP_COMPILED, arg);}/* Compile function for as-path match. */voidroute_match_aspath_free (void *rule){  XFREE (MTYPE_ROUTE_MAP_COMPILED, rule);}/* Route map commands for aspath matching. */struct route_map_rule_cmd route_match_aspath_cmd = {  "as-path",  route_match_aspath,  route_match_aspath_compile,  route_match_aspath_free};#if ROUTE_MATCH_ASPATH_OLD/* `match as-path ASPATH' *//* Match function for as-path match.  I assume given object is */introute_match_aspath (void *rule, struct prefix *prefix, void *object){  regex_t *regex;  struct bgp_info *bgp_info;  regex = rule;  bgp_info = object;    /* Perform match. */  return bgp_regexec (regex, bgp_info->attr->aspath);}/* Compile function for as-path match. */void *route_match_aspath_compile (char *arg){  regex_t *regex;  regex = bgp_regcomp (arg);  if (! regex)    return NULL;  return regex;}/* Compile function for as-path match. */voidroute_match_aspath_free (void *rule){  regex_t *regex = rule;  bgp_regex_free (regex);}/* Route map commands for aspath matching. */struct route_map_rule_cmd route_match_aspath_cmd = {  "as-path",  route_match_aspath,  route_match_aspath_compile,  route_match_aspath_free};#endif /* ROUTE_MATCH_ASPATH_OLD *//* `match community COMMUNIY' */struct rmap_community{  char *name;  int exact;};/* Match function for community match. */route_map_result_troute_match_community (void *rule, struct prefix *prefix, 		       route_map_object_t type, void *object){  struct community_list *list;  struct bgp_info *bgp_info;  struct rmap_community *rcom;  if (type == RMAP_BGP)     {      bgp_info = object;      rcom = rule;      list = community_list_lookup (bgp_clist, rcom->name, COMMUNITY_LIST_AUTO);      if (! list)	return RMAP_NOMATCH;      if (rcom->exact)	{	  if (community_list_exact_match (bgp_info->attr->community, list))	    return RMAP_MATCH;	}      else	{	  if (community_list_match (bgp_info->attr->community, list))	    return RMAP_MATCH;	}    }  return RMAP_NOMATCH;}/* Compile function for community match. */void *route_match_community_compile (char *arg){  struct rmap_community *rcom;  int len;  char *p;  rcom = XCALLOC (MTYPE_ROUTE_MAP_COMPILED, sizeof (struct rmap_community));  p = strchr (arg, ' ');  if (p)    {      len = p - arg;      rcom->name = XCALLOC (MTYPE_ROUTE_MAP_COMPILED, len + 1);      memcpy (rcom->name, arg, len);      rcom->exact = 1;    }  else    {      rcom->name = XSTRDUP (MTYPE_ROUTE_MAP_COMPILED, arg);      rcom->exact = 0;    }  return rcom;}/* Compile function for community match. */voidroute_match_community_free (void *rule){  struct rmap_community *rcom = rule;  XFREE (MTYPE_ROUTE_MAP_COMPILED, rcom->name);   XFREE (MTYPE_ROUTE_MAP_COMPILED, rcom);}/* Route map commands for community matching. */struct route_map_rule_cmd route_match_community_cmd = {  "community",  route_match_community,  route_match_community_compile,  route_match_community_free};/* Match function for extcommunity match. */route_map_result_troute_match_ecommunity (void *rule, struct prefix *prefix, 			route_map_object_t type, void *object){  struct community_list *list;  struct bgp_info *bgp_info;  if (type == RMAP_BGP)     {      bgp_info = object;      list = community_list_lookup (bgp_clist, (char *) rule,				    EXTCOMMUNITY_LIST_AUTO);      if (! list)	return RMAP_NOMATCH;      if (ecommunity_list_match (bgp_info->attr->ecommunity, list))	return RMAP_MATCH;    }  return RMAP_NOMATCH;}/* Compile function for extcommunity match. */void *route_match_ecommunity_compile (char *arg){  return XSTRDUP (MTYPE_ROUTE_MAP_COMPILED, arg);}/* Compile function for extcommunity match. */voidroute_match_ecommunity_free (void *rule){  XFREE (MTYPE_ROUTE_MAP_COMPILED, rule);}/* Route map commands for community matching. */struct route_map_rule_cmd route_match_ecommunity_cmd = {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -