1929a3cd5ebe9cabb5f72073d532de7eafa6dfce
[releases.git] / dn_fib.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _NET_DN_FIB_H
3 #define _NET_DN_FIB_H
4
5 #include <linux/netlink.h>
6 #include <linux/refcount.h>
7 #include <linux/rtnetlink.h>
8 #include <net/fib_rules.h>
9
10 extern const struct nla_policy rtm_dn_policy[];
11
12 struct dn_fib_res {
13         struct fib_rule *r;
14         struct dn_fib_info *fi;
15         unsigned char prefixlen;
16         unsigned char nh_sel;
17         unsigned char type;
18         unsigned char scope;
19 };
20
21 struct dn_fib_nh {
22         struct net_device       *nh_dev;
23         unsigned int            nh_flags;
24         unsigned char           nh_scope;
25         int                     nh_weight;
26         int                     nh_power;
27         int                     nh_oif;
28         __le16                  nh_gw;
29 };
30
31 struct dn_fib_info {
32         struct dn_fib_info      *fib_next;
33         struct dn_fib_info      *fib_prev;
34         refcount_t              fib_treeref;
35         refcount_t              fib_clntref;
36         int                     fib_dead;
37         unsigned int            fib_flags;
38         int                     fib_protocol;
39         __le16                  fib_prefsrc;
40         __u32                   fib_priority;
41         __u32                   fib_metrics[RTAX_MAX];
42         int                     fib_nhs;
43         int                     fib_power;
44         struct dn_fib_nh        fib_nh[0];
45 #define dn_fib_dev              fib_nh[0].nh_dev
46 };
47
48
49 #define DN_FIB_RES_RESET(res)   ((res).nh_sel = 0)
50 #define DN_FIB_RES_NH(res)      ((res).fi->fib_nh[(res).nh_sel])
51
52 #define DN_FIB_RES_PREFSRC(res) ((res).fi->fib_prefsrc ? : __dn_fib_res_prefsrc(&res))
53 #define DN_FIB_RES_GW(res)      (DN_FIB_RES_NH(res).nh_gw)
54 #define DN_FIB_RES_DEV(res)     (DN_FIB_RES_NH(res).nh_dev)
55 #define DN_FIB_RES_OIF(res)     (DN_FIB_RES_NH(res).nh_oif)
56
57 typedef struct {
58         __le16  datum;
59 } dn_fib_key_t;
60
61 typedef struct {
62         __le16  datum;
63 } dn_fib_hash_t;
64
65 typedef struct {
66         __u16   datum;
67 } dn_fib_idx_t;
68
69 struct dn_fib_node {
70         struct dn_fib_node *fn_next;
71         struct dn_fib_info *fn_info;
72 #define DN_FIB_INFO(f) ((f)->fn_info)
73         dn_fib_key_t    fn_key;
74         u8              fn_type;
75         u8              fn_scope;
76         u8              fn_state;
77 };
78
79
80 struct dn_fib_table {
81         struct hlist_node hlist;
82         u32 n;
83
84         int (*insert)(struct dn_fib_table *t, struct rtmsg *r, 
85                         struct nlattr *attrs[], struct nlmsghdr *n,
86                         struct netlink_skb_parms *req);
87         int (*delete)(struct dn_fib_table *t, struct rtmsg *r,
88                         struct nlattr *attrs[], struct nlmsghdr *n,
89                         struct netlink_skb_parms *req);
90         int (*lookup)(struct dn_fib_table *t, const struct flowidn *fld,
91                         struct dn_fib_res *res);
92         int (*flush)(struct dn_fib_table *t);
93         int (*dump)(struct dn_fib_table *t, struct sk_buff *skb, struct netlink_callback *cb);
94
95         unsigned char data[];
96 };
97
98 #ifdef CONFIG_DECNET_ROUTER
99 /*
100  * dn_fib.c
101  */
102 void dn_fib_init(void);
103 void dn_fib_cleanup(void);
104
105 int dn_fib_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
106 struct dn_fib_info *dn_fib_create_info(const struct rtmsg *r,
107                                        struct nlattr *attrs[],
108                                        const struct nlmsghdr *nlh, int *errp);
109 int dn_fib_semantic_match(int type, struct dn_fib_info *fi,
110                           const struct flowidn *fld, struct dn_fib_res *res);
111 void dn_fib_release_info(struct dn_fib_info *fi);
112 void dn_fib_flush(void);
113 void dn_fib_select_multipath(const struct flowidn *fld, struct dn_fib_res *res);
114
115 /*
116  * dn_tables.c
117  */
118 struct dn_fib_table *dn_fib_get_table(u32 n, int creat);
119 struct dn_fib_table *dn_fib_empty_table(void);
120 void dn_fib_table_init(void);
121 void dn_fib_table_cleanup(void);
122
123 /*
124  * dn_rules.c
125  */
126 void dn_fib_rules_init(void);
127 void dn_fib_rules_cleanup(void);
128 unsigned int dnet_addr_type(__le16 addr);
129 int dn_fib_lookup(struct flowidn *fld, struct dn_fib_res *res);
130
131 int dn_fib_dump(struct sk_buff *skb, struct netlink_callback *cb);
132
133 void dn_fib_free_info(struct dn_fib_info *fi);
134
135 static inline void dn_fib_info_put(struct dn_fib_info *fi)
136 {
137         if (refcount_dec_and_test(&fi->fib_clntref))
138                 dn_fib_free_info(fi);
139 }
140
141 static inline void dn_fib_res_put(struct dn_fib_res *res)
142 {
143         if (res->fi)
144                 dn_fib_info_put(res->fi);
145         if (res->r)
146                 fib_rule_put(res->r);
147 }
148
149 #else /* Endnode */
150
151 #define dn_fib_init()  do { } while(0)
152 #define dn_fib_cleanup() do { } while(0)
153
154 #define dn_fib_lookup(fl, res) (-ESRCH)
155 #define dn_fib_info_put(fi) do { } while(0)
156 #define dn_fib_select_multipath(fl, res) do { } while(0)
157 #define dn_fib_rules_policy(saddr,res,flags) (0)
158 #define dn_fib_res_put(res) do { } while(0)
159
160 #endif /* CONFIG_DECNET_ROUTER */
161
162 static inline __le16 dnet_make_mask(int n)
163 {
164         if (n)
165                 return cpu_to_le16(~((1 << (16 - n)) - 1));
166         return cpu_to_le16(0);
167 }
168
169 #endif /* _NET_DN_FIB_H */