The JUNOS policy language is very similar to a programming language in the way the configuration is laid out and how it can be subdivided into modules. Individuals familiar with programming will see that JUNOS policy configurations are similar in layout to Pascal or C programs. The same parsing rules apply for curly braces ({ ) and semicolons (;) in JUNOS as they do in the aforementioned programming languages. The most important aspect of using any good programming language is its implementation. The same can be said of the JUNOS policy language. Policies can be broken down into modules with specific actions; this makes the language very scalable and adaptable to the needs of today's large networks. Policy definition is carried out in configuration mode at the [edit policy-options] level of the JUNOS CLI. Policies are defined by using the policy-statement command as follows : [edit] user@SanFrancisco# edit policy-options [edit policy-options] root@SanFran# set policy-statement policyname ? Possible completions: + apply-groups Groups from which to inherit configuration data > from Conditions to match a route's source > term Policy term > then Actions to take if 'from' and 'to' conditions match > to Conditions to match a route's destination After naming a policy, as shown above, you have the option of creating terms or defining a simple policy without terms. The same concepts for configuration development presented in Section 6.3 on firewall filters apply here. Each policy consists of zero or more terms with each term consisting of match conditions specified using a from or to statement and match actions specified using a then statement. If no terms are specified, match conditions and match actions are specified at the policy level only. An analogy to this would be a computer program consisting of subroutines with if then else statements. The computer program represents the policy, and the subroutines are the individual terms within the policy.
Multiple routing policies can be combined to create a policy chain, which uses an ordered list of policies. In a policy chain, each link is a predefined policy. This is a very efficient way of developing complex policies by reusing predefined policies instead of creating longer and more complex ones. The chain of match conditions is stopped once an accept or reject match action is reached in any of the policies. Why would we want to use a chain as opposed to a single policy with multiple terms? Policy chains can assist in optimizing smaller, more concise configurations by restricting common elements to specific policies and reusing those policies instead of re-creating them. A good example of this would be a policy to reject inbound default routes and private addresses from an external AS. This type of policy could be applied to BGP neighbors at the beginning of a chain. The remaining policies included in the chain could apply to specific BGP groups or neighbors. More examples of policy chains are discussed in subsequent sections of this chapter and are shown in Figure 11-3. Figure 11-3. Rating Policy Chain 11.3.1 Default Routing Policy ActionsEach routing protocol has a certain default action that it performs when a routing policy is applied to it. As we have discussed there are both import and export routing policies and if you have not explicitly configured a desired routing policy then the defaults listed in Table 11-2. When referring to this table keep in mind that Juniper Networks refers to directly connected and explicitly routes (ex. Static) as pseudoprotocol routes since they do not originate from a specific routing protocol. Table 11-2. Default Routing Policies
Note You cannot change the default import policy for the link-state protocols IS-IS and OSPF. As link-state protocols, IS-IS and OSPF exchange routes between routers within an autonomous system (AS). All routers within an AS (or area) must share the same link-state database, which includes routes to reachable prefixes and the metrics associated with the prefixes. If an import policy were configured and applied to IS-IS or OSPF, some routes might not be learned or advertised or the metrics for learned routes might be altered , which would make a consistent link-state database impossible . 11.3.2 Policy-Chain TermsA term is a segment of a policy designed to match routes against user-specified criteria and perform specific actions on them once a match is made. A policy may have a single term, multiple terms, or none at all. Each policy can have specific match actions within each term, as well as default actions outside of the terms. This concept is illustrated in Figure 11-4. Figure 11-4. Policy Structure Routes are sequentially passed into the first policy in the chain ”in this example it is referred to as Policy 1. Policy 1 sends the routes through Term 1 and compares them to the match conditions specified. If a match is made against a route and if the route is accepted or rejected, no further comparisons are made and any other actions specified are carried out. If an accept or reject is not specified in the match actions or if there are no matches in Term 1, then the routes are passed on to Term 2. Keep in mind that match actions may occur against a route that simply modifies the properties of a route without accepting or rejecting it. Routes that are not explicitly accepted or rejected by a match action continue to be evaluated in the policy chain. This procedure continues for T terms in Policy 1 where T is a numeric integer defining the number of terms in a policy. If Term T has been evaluated without a match, then the routes are passed to the next policy in the chain, which in this case is Policy 2. Routes that do not match any policies in the chain through an accept action will be rejected. Not all policies or terms will have from , to , and then statements present. There are a number of rules to determine what occurs if all or only some of these statements are present. This process is illustrated in Figure 11-5 and described below.
Figure 11-5. Policy Flow 11.3.3 Match ConditionsBoth from and to statements are made up of match conditions. There are many conditions that a route can be matched against, which are divided into generic and protocol categories. As previously discussed, a protocol-specific match condition, such as the OSPF area number, is considered to be a unique characteristic used by that protocol. A complete list of the possible from statements is contained in the following example from the CLI: [edit] user@Chicago# edit policy-options policy-statement my-new-policy [edit policy-options policy-statement my-new-policy] user@Chicago# set from ? Possible completions: + apply-groups Groups from which to inherit configuration data area OSPF area identifier + as-path Name of AS path regular expression (BGP only) color Color (preference) value color2 Color (preference) value 2 + community BGP community > external External route instance Routing protocol instance + interface Interface name or address level IS-IS level local-preference Local preference associated with a route metric Metric value metric2 Metric value 2 metric3 Metric value 3 metric4 Metric value 4 + neighbor Neighboring router + next-hop Next-hop router origin BGP origin attribute + policy Name of policy to evaluate preference Preference value preference2 Preference value 2 > prefix-list List of prefix-lists of routes to match + protocol Protocol from which route was learned rib Routing table > route-filter List of routes to match > source-address-filter List of source addresses to match tag Tag string tag2 Tag string 2 Table 11-3 describes the each of the generic match conditions in more detail. Table 11-3. Generic Match Conditions
The following example shows a simple policy that rejects OSPF type 2 external routes: policy-statement external-type2-reject { from { protocol ospf; external { type 2; } } then reject; } The next example shows a policy that could be used to change the metric of routes from a specific routing instance, in this case ospf2 , assuming the router is running multiple instances of OSPF. ospf2 is the second instance of OSPF routing configured on the router: policy-statement routes-from-ospf2 { from { instance ospf2; protocol ospf; } then { metric 10; accept; } } Another example is a simple routing policy that matches all RIP routes, as follows: policy-statement match-rip-routes { from protocol rip; then accept; } There are also protocol-specific match conditions for OSPF, IS-IS, and BGP. Table 11-4 lists and describes each of the protocol-specific match conditions. Table 11-4. Protocol-Specific Match Conditions
Examples of the various BGP match conditions will be explored in greater depth later in the chapter and in Chapter 10. 11.3.4 Match ActionsAfter the match conditions in the from and to statements have been selected, it is then time to select a corresponding match action for the policy. All match actions are specified as part of a then statement. As with match conditions, there are many match actions. The possible match actions are listed below: [edit policy-options policy-statement policyname] Lab@Chicago# set then ? Possible completions: accept Accept a route + apply-groups Groups from which to inherit configuration data as-path-prepend Prepend AS numbers to an AS path (BGP only) class Set class-of-service parameters > color Color (preference) value > color2 Color (preference) value 2 > community BGP community properties associated with a route cos-next-hop-map Set CoS based next hop map in forwarding table damping Define BGP route flap damping parameters destination-class Set destination class in forwarding table > external External route > install-nexthop Choose the next-hop to be used for forwarding > load-balance Type of load balancing in forwarding table > local-preference Local preference associated with a route > metric Metric value > metric2 Metric value 2 > metric3 Metric value 3 > metric4 Metric value 4 next Skip to next policy or term > next-hop Set the address of the next-hop router origin BGP path origin > preference Preference value > preference2 Preference value 2 reject Reject a route > tag Tag string > tag2 Tag string 2 trace Log matches to a trace file Match actions can be divided into three distinct groups:
Control actions govern the policy flow. These are the most fundamental match actions and the most frequently used. Table 11-5 lists and describes each of the control actions. Table 11-5. Control Actions
The trace action logs the match to a trace file. You will already have some information on trace files from Section 6.2.5.1, and more tracing examples will be explored in the next chapter. Trace file configurations appear at the routing-options level as follows: routing-options { traceoptions { file matchactionlog replace size 640k files 5 no-world-readable; flag policy; } } The remaining match actions fall under the general policy-actions category and are normally used to modify parameters associated with a route. Table 11-6 lists each of these actions. Table 11-6. General Policy Actions
In the following example, the AS path attribute of all routes received from BGP peer 10.0.0.1 will be prepended with "2 2 2 2" , making these routes less attractive to BGP for inbound traffic, where 2 is the number of the local AS. policy-options { policy-statement as-path-prepend { from neighbor 10.0.0.1; then as-path-prepend "2 2 2 2"; accept; } } The next example enables damping with the options contained in lots-of-damping . The damping parameters for lots-of-damping are specified in a separate section of policy. This will be examined in greater detail in Section 11.6. policy-options { policy-statement my-damp { from neighbor 10.0.0.1; then damping lots-of-damping; } The following example matches against routes coming from a specific next-hop 10.0.0.1 and going to OSPF area 3 while setting the preference value to 10. policy-statement change-pref { from next-hop 10.0.0.1; to area 0.0.0.3; then { preference 10; } } Match conditions can be also specified using route filters. Route filtering is a method of picking out a route or specific routes from the routing table and performing a common action on them. Route filtering will be examined in greater detail later in this chapter. ![]() |