1 //  $Id$
2 // Copyright (c) 2001,2002                        RIPE NCC
3 //
4 // All Rights Reserved
5 //
6 // Permission to use, copy, modify, and distribute this software and its
7 // documentation for any purpose and without fee is hereby granted,
8 // provided that the above copyright notice appear in all copies and that
9 // both that copyright notice and this permission notice appear in
10 // supporting documentation, and that the name of the author not be
11 // used in advertising or publicity pertaining to distribution of the
12 // software without specific, written prior permission.
13 //
14 // THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
15 // ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
16 // AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
17 // DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
18 // AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 //
21 //
22 //  Copyright (c) 1994 by the University of Southern California
23 //  All rights reserved.
24 //
25 //    Permission is hereby granted, free of charge, to any person obtaining a copy
26 //    of this software and associated documentation files (the "Software"), to deal
27 //    in the Software without restriction, including without limitation the rights
28 //    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
29 //    copies of the Software, and to permit persons to whom the Software is
30 //    furnished to do so, subject to the following conditions:
31 //
32 //    The above copyright notice and this permission notice shall be included in
33 //    all copies or substantial portions of the Software.
34 //
35 //    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36 //    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37 //    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38 //    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39 //    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40 //    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
41 //    THE SOFTWARE.
42 //
43 //  Questions concerning this software should be directed to
44 //  irrtoolset@cs.usc.edu.
45 //
46 //  Author(s): Cengiz Alaettinoglu <cengiz@ISI.EDU>
47 
48 #ifndef F_CISCO_H
49 #define F_CISCO_H
50 
51 #include "config.h"
52 #include "rpsl/List.hh"
53 #include "normalform/FilterOfASPath.hh"
54 #include "irr/irr.hh"
55 #include "irr/autnum.hh"
56 
57 #include <sstream>
58 
59 class FilterOfCommunity;
60 class RangeList;
61 class regexp;
62 class PolicyAction;
63 class PolicyActionList;
64 class PTree;
65 class ItemList;
66 class NormalExpression;
67 class SetOfPrefix;
68 class SetOfIPv6Prefix;
69 class regexp_nf;
70 
71 class ListNodeOf2Ints : public ListNode {
72 public:
73    int start;
74    int end;
75    int flag;    // flag is used for exact community matching
76 
ListNodeOf2Ints()77    ListNodeOf2Ints() {}
ListNodeOf2Ints(int s,int e,int f)78    ListNodeOf2Ints(int s, int e, int f) : start(s), end(e), flag(f) {}
ListNodeOf2Ints(const ListNodeOf2Ints & b)79    ListNodeOf2Ints(const ListNodeOf2Ints& b) :
80       start(b.start), end(b.end), flag(b.flag) {}
81 };
82 
83 class ListOf2Ints : public List<ListNodeOf2Ints> {
84 public:
ListOf2Ints(int flag)85    ListOf2Ints(int flag) : List<ListNodeOf2Ints>() {
86       // create a list whose end is less than its start
87       // so it wont print anything!
88       add(2,1);
89    }
ListOf2Ints()90    ListOf2Ints() : List<ListNodeOf2Ints>() {}
add(int s,int e,int flag=0)91    void add(int s, int e, int flag = 0) {
92       append((new ListNodeOf2Ints(s, e, flag)));
93    }
94 };
95 
96 class CiscoConfig : public RtConfig {
97 public:
CiscoConfig()98    CiscoConfig() : RtConfig() {
99       lastMapName[0] = 0;
100       routeMapGenerated = false;
101       prefixListGenerated = false;
102       distributeListNo = -1;
103       routeMapID = 1;
104    }
105    // REIMPLEMENTED
106    void importP(ASt as, MPPrefix* addr, ASt peerAS, MPPrefix* peerAddr);
107    void exportP(ASt as, MPPrefix* addr, ASt peerAS, MPPrefix* peerAddr);
108    void exportGroup(ASt as, char *pset);
109    void importGroup(ASt as, char *pset);
110    void deflt(ASt as, ASt peerAS);
111    void static2bgp(ASt as, MPPrefix* addr);
112    void networks(ASt as);
113    void IPv6networks(ASt as);
114    void inboundPacketFilter(char *ifname, ASt as, MPPrefix* addr, ASt peerAS, MPPrefix* peerAddr);
115    void outboundPacketFilter(char *ifname, ASt as, MPPrefix* addr, ASt peerAS, MPPrefix* peerAddr);
116 
117 public:
118    // options
119    static char mapName[80];
120    static char mapNameFormat[80];
121    static bool forcedInboundMatchIP;
122    static bool useAclCaches;
123    static bool printRouteMap;
124    static bool compressAcls;
125    static bool usePrefixLists;
126    static bool eliminateDupMapParts;
127    static bool forceTilda;
128    static bool emptyLists;
129    static bool peerTemplates;
130    static int  mapIncrements;
131    static int  mapNumbersStartAt;
132    static int  mapCount;
133 
134 private:
135    char lastMapName[80];
136    bool routeMapGenerated;
137    bool prefixListGenerated;
138    int  distributeListNo;
139    int  routeMapID;
140    bool reSplittable;
141    bool hasTilda;
142    bool inTilda;
143 
144 private:
145    static bool  firstCommunityList;
146    const char  *returnPermitOrDeny(bool);
147    ListOf2Ints *printRoutes(SetOfPrefix& nets);
148    ListOf2Ints *printRoutes(SetOfIPv6Prefix& nets);
149    ListOf2Ints *printPrefixList(SetOfPrefix& nets);
150    ListOf2Ints *printPrefixList(SetOfIPv6Prefix& nets);
151    ListOf2Ints *printCommunities(FilterOfCommunity& cm);
152    ListOf2Ints *printASPaths(regexp_nf& path);
153    void         printREASno(std::ostream& out, const RangeList &no);
154    int          printRE_(std::ostream& os, const regexp& r);
155    void         printRE(std::ostream& os, const regexp& r, int aclID, bool permit);
156 
157    int          printPacketFilter(SetOfPrefix &set);
158    int          printPacketFilter(SetOfIPv6Prefix &set);
159    inline void  printCommunity(std::ostream &os, unsigned int i);
160    void         printCommunityList(std::ostream &os, ItemList *args);
161    int         printCommunitySetList(std::ostream &os, ItemList *args);
162    void         printActions(std::ostream &os, PolicyActionList *action, ItemAFI *afi);
163    int          print(NormalExpression *ne, PolicyActionList *actn, int import_flag, ItemAFI *afi);
164    bool         printNeighbor(int import, ASt asno, ASt peerAS, char *neighbor, bool peerGroup, ItemAFI *peer_afi, ItemAFI *filter_afi);
printAccessList(SetOfPrefix & nets)165    void printAccessList(SetOfPrefix& nets) {
166       bool save = useAclCaches;
167       useAclCaches = false;
168       printRoutes(nets);
169       useAclCaches = save;
170    }
printAccessList(SetOfIPv6Prefix & nets)171    void printAccessList(SetOfIPv6Prefix& nets) {
172       bool save = useAclCaches;
173       useAclCaches = false;
174       printRoutes(nets);
175       useAclCaches = save;
176    }
printAspathAccessList(FilterOfASPath & path)177    void printAspathAccessList(FilterOfASPath& path) {
178       bool save = useAclCaches;
179       useAclCaches = false;
180       printASPaths(path);
181       useAclCaches = save;
182   }
183 };
184 
185 template <class T>
186 class AccessListManager {
187 private :
188    class Node : public ListNode {
189    public:
Node()190       Node()              : data(),       lof2i()        {}
Node(const Node & b)191       Node(const Node &b) : data(b.data), lof2i(b.lof2i) {}
Node(T & _data)192       Node(T& _data)      : data(_data),  lof2i()        {}
193 
194    public:
195       T data;
196       ListOf2Ints lof2i;
197    };
198 
199    List<Node> cache;
200    int next_id;
201 
202 public:
AccessListManager()203    AccessListManager() : cache(), next_id(1) {
204    }
205 
AccessListManager(int firstID)206    AccessListManager(int firstID) : cache(), next_id(firstID) {
207    }
208 
search(T & _data)209    ListOf2Ints* search(T& _data) {
210       for (Node *p = cache.head(); p; p = cache.next(p))
211 	 if (p->data == _data)
212 	    return &(p->lof2i);
213       return NULL;
214    }
215 
add(T & _data)216    ListOf2Ints* add(T& _data) {
217       Node *p = new Node(_data);
218       cache.append(p);
219       return &(p->lof2i);
220    }
221 
setNextID(int id)222    void setNextID(int id) {
223       next_id = id;
224    }
225 
newID()226    int newID() {
227       return next_id++;
228    }
lastID()229    int lastID() {
230       return next_id - 1;
231    }
232 };
233 
234 extern AccessListManager<regexp_nf>         aspathMgr;
235 extern AccessListManager<SetOfPrefix>       prefixMgr;
236 extern AccessListManager<SetOfIPv6Prefix>   ipv6prefixMgr;
237 extern AccessListManager<SetOfPrefix>       pktFilterMgr;
238 extern AccessListManager<SetOfIPv6Prefix>   ipv6pktFilterMgr;
239 extern AccessListManager<FilterOfCommunity> communityMgr;
240 
241 #endif   // F_CISCO_H
242