]>
Commit | Line | Data |
---|---|---|
942bf97b | 1 | /* Zebra Policy Based Routing (PBR) main handling. |
2 | * Copyright (C) 2018 Cumulus Networks, Inc. | |
3 | * | |
4 | * This file is part of FRR. | |
5 | * | |
6 | * FRR is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2, or (at your option) any | |
9 | * later version. | |
10 | * | |
11 | * FRR is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with FRR; see the file COPYING. If not, write to the Free | |
18 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
19 | * 02111-1307, USA. | |
20 | */ | |
21 | ||
22 | #include <zebra.h> | |
23 | ||
43fe6a2a DS |
24 | #include <jhash.h> |
25 | #include <hash.h> | |
26 | ||
942bf97b | 27 | #include "zebra/zebra_pbr.h" |
28 | #include "zebra/rt.h" | |
29 | ||
30 | /* definitions */ | |
31 | ||
32 | /* static function declarations */ | |
33 | ||
34 | /* Private functions */ | |
35 | ||
36 | /* Public functions */ | |
43fe6a2a | 37 | void zebra_pbr_rules_free(void *arg) |
1fbfe5a5 | 38 | { |
43fe6a2a DS |
39 | struct zebra_pbr_rule *rule; |
40 | ||
41 | rule = (struct zebra_pbr_rule *)arg; | |
42 | ||
a0321978 | 43 | kernel_del_pbr_rule(rule); |
43fe6a2a DS |
44 | XFREE(MTYPE_TMP, rule); |
45 | } | |
46 | ||
47 | uint32_t zebra_pbr_rules_hash_key(void *arg) | |
48 | { | |
49 | struct zebra_pbr_rule *rule; | |
50 | uint32_t key; | |
51 | ||
52 | rule = (struct zebra_pbr_rule *)arg; | |
53 | key = jhash_3words(rule->seq, rule->priority, rule->action.table, | |
54 | prefix_hash_key(&rule->filter.src_ip)); | |
a0321978 DS |
55 | if (rule->ifp) |
56 | key = jhash_1word(rule->ifp->ifindex, key); | |
57 | else | |
58 | key = jhash_1word(0, key); | |
59 | ||
43fe6a2a DS |
60 | return jhash_3words(rule->filter.src_port, rule->filter.dst_port, |
61 | prefix_hash_key(&rule->filter.dst_ip), key); | |
62 | } | |
63 | ||
64 | int zebra_pbr_rules_hash_equal(const void *arg1, const void *arg2) | |
65 | { | |
66 | const struct zebra_pbr_rule *r1, *r2; | |
67 | ||
68 | r1 = (const struct zebra_pbr_rule *)arg1; | |
69 | r2 = (const struct zebra_pbr_rule *)arg2; | |
70 | ||
71 | if (r1->seq != r2->seq) | |
72 | return 0; | |
73 | ||
74 | if (r1->priority != r2->priority) | |
75 | return 0; | |
76 | ||
77 | if (r1->action.table != r2->action.table) | |
78 | return 0; | |
79 | ||
80 | if (r1->filter.src_port != r2->filter.src_port) | |
81 | return 0; | |
82 | ||
83 | if (r1->filter.dst_port != r2->filter.dst_port) | |
84 | return 0; | |
85 | ||
86 | if (!prefix_same(&r1->filter.src_ip, &r2->filter.src_ip)) | |
87 | return 0; | |
88 | ||
89 | if (!prefix_same(&r1->filter.dst_ip, &r2->filter.dst_ip)) | |
90 | return 0; | |
91 | ||
a0321978 DS |
92 | if (r1->ifp != r2->ifp) |
93 | return 0; | |
94 | ||
43fe6a2a DS |
95 | return 1; |
96 | } | |
97 | ||
98 | static void *pbr_rule_alloc_intern(void *arg) | |
99 | { | |
100 | struct zebra_pbr_rule *zpr; | |
101 | struct zebra_pbr_rule *new; | |
102 | ||
103 | zpr = (struct zebra_pbr_rule *)arg; | |
104 | ||
105 | new = XCALLOC(MTYPE_TMP, sizeof(*new)); | |
106 | ||
107 | memcpy(new, zpr, sizeof(*zpr)); | |
108 | ||
109 | return new; | |
110 | } | |
111 | ||
a0321978 | 112 | void zebra_pbr_add_rule(struct zebra_ns *zns, struct zebra_pbr_rule *rule) |
43fe6a2a DS |
113 | { |
114 | (void)hash_get(zns->rules_hash, rule, pbr_rule_alloc_intern); | |
a0321978 | 115 | kernel_add_pbr_rule(rule); |
1fbfe5a5 DS |
116 | } |
117 | ||
a0321978 | 118 | void zebra_pbr_del_rule(struct zebra_ns *zns, struct zebra_pbr_rule *rule) |
1fbfe5a5 | 119 | { |
43fe6a2a DS |
120 | struct zebra_pbr_rule *lookup; |
121 | ||
122 | lookup = hash_lookup(zns->rules_hash, rule); | |
a0321978 | 123 | kernel_del_pbr_rule(rule); |
43fe6a2a DS |
124 | |
125 | if (lookup) | |
126 | XFREE(MTYPE_TMP, lookup); | |
127 | else | |
128 | zlog_warn("%s: Rule being deleted we know nothing about", | |
129 | __PRETTY_FUNCTION__); | |
1fbfe5a5 DS |
130 | } |
131 | ||
942bf97b | 132 | /* |
133 | * Handle success or failure of rule (un)install in the kernel. | |
134 | */ | |
135 | void kernel_pbr_rule_add_del_status(struct zebra_pbr_rule *rule, | |
942bf97b | 136 | enum southbound_results res) |
137 | { | |
138 | } | |
139 | ||
140 | /* | |
141 | * Handle rule delete notification from kernel. | |
142 | */ | |
a0321978 | 143 | int kernel_pbr_rule_del(struct zebra_pbr_rule *rule) |
942bf97b | 144 | { |
145 | return 0; | |
146 | } |