blob: e9f116e29c221561a3c2e0b4797d56d9c9c3ec56 [file] [log] [blame]
Roopa Prabhu499a2422015-07-21 10:43:46 +02001#ifndef __NET_LWTUNNEL_H
2#define __NET_LWTUNNEL_H 1
3
4#include <linux/lwtunnel.h>
5#include <linux/netdevice.h>
6#include <linux/skbuff.h>
7#include <linux/types.h>
8#include <net/route.h>
9
10#define LWTUNNEL_HASH_BITS 7
11#define LWTUNNEL_HASH_SIZE (1 << LWTUNNEL_HASH_BITS)
12
13/* lw tunnel state flags */
Tom Herbert25368622015-08-17 13:42:24 -070014#define LWTUNNEL_STATE_OUTPUT_REDIRECT BIT(0)
15#define LWTUNNEL_STATE_INPUT_REDIRECT BIT(1)
Roopa Prabhu499a2422015-07-21 10:43:46 +020016
17struct lwtunnel_state {
18 __u16 type;
19 __u16 flags;
20 atomic_t refcnt;
Eric W. Biedermanede20592015-10-07 16:48:47 -050021 int (*orig_output)(struct net *net, struct sock *sk, struct sk_buff *skb);
Tom Herbert25368622015-08-17 13:42:24 -070022 int (*orig_input)(struct sk_buff *);
Roopa Prabhu499a2422015-07-21 10:43:46 +020023 int len;
24 __u8 data[0];
25};
26
27struct lwtunnel_encap_ops {
28 int (*build_state)(struct net_device *dev, struct nlattr *encap,
Tom Herbert127eb7c2015-08-24 09:45:41 -070029 unsigned int family, const void *cfg,
Roopa Prabhu499a2422015-07-21 10:43:46 +020030 struct lwtunnel_state **ts);
Eric W. Biedermanede20592015-10-07 16:48:47 -050031 int (*output)(struct net *net, struct sock *sk, struct sk_buff *skb);
Tom Herbert25368622015-08-17 13:42:24 -070032 int (*input)(struct sk_buff *skb);
Roopa Prabhu499a2422015-07-21 10:43:46 +020033 int (*fill_encap)(struct sk_buff *skb,
34 struct lwtunnel_state *lwtstate);
35 int (*get_encap_size)(struct lwtunnel_state *lwtstate);
36 int (*cmp_encap)(struct lwtunnel_state *a, struct lwtunnel_state *b);
37};
38
Roopa Prabhu499a2422015-07-21 10:43:46 +020039#ifdef CONFIG_LWTUNNEL
Jiri Bencdf383e62015-08-18 18:41:13 +020040static inline void lwtstate_free(struct lwtunnel_state *lws)
41{
42 kfree(lws);
43}
44
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +020045static inline struct lwtunnel_state *
46lwtstate_get(struct lwtunnel_state *lws)
Roopa Prabhu499a2422015-07-21 10:43:46 +020047{
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +020048 if (lws)
49 atomic_inc(&lws->refcnt);
50
51 return lws;
Roopa Prabhu499a2422015-07-21 10:43:46 +020052}
53
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +020054static inline void lwtstate_put(struct lwtunnel_state *lws)
Roopa Prabhu499a2422015-07-21 10:43:46 +020055{
56 if (!lws)
57 return;
58
59 if (atomic_dec_and_test(&lws->refcnt))
Jiri Bencdf383e62015-08-18 18:41:13 +020060 lwtstate_free(lws);
Roopa Prabhu499a2422015-07-21 10:43:46 +020061}
62
63static inline bool lwtunnel_output_redirect(struct lwtunnel_state *lwtstate)
64{
65 if (lwtstate && (lwtstate->flags & LWTUNNEL_STATE_OUTPUT_REDIRECT))
66 return true;
67
68 return false;
69}
70
Tom Herbert25368622015-08-17 13:42:24 -070071static inline bool lwtunnel_input_redirect(struct lwtunnel_state *lwtstate)
72{
73 if (lwtstate && (lwtstate->flags & LWTUNNEL_STATE_INPUT_REDIRECT))
74 return true;
75
76 return false;
77}
Roopa Prabhu499a2422015-07-21 10:43:46 +020078int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *op,
79 unsigned int num);
80int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *op,
81 unsigned int num);
82int lwtunnel_build_state(struct net_device *dev, u16 encap_type,
83 struct nlattr *encap,
Tom Herbert127eb7c2015-08-24 09:45:41 -070084 unsigned int family, const void *cfg,
Roopa Prabhu499a2422015-07-21 10:43:46 +020085 struct lwtunnel_state **lws);
86int lwtunnel_fill_encap(struct sk_buff *skb,
87 struct lwtunnel_state *lwtstate);
88int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate);
89struct lwtunnel_state *lwtunnel_state_alloc(int hdr_len);
90int lwtunnel_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b);
Eric W. Biedermanede20592015-10-07 16:48:47 -050091int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb);
Tom Herbert25368622015-08-17 13:42:24 -070092int lwtunnel_input(struct sk_buff *skb);
Roopa Prabhu499a2422015-07-21 10:43:46 +020093
94#else
95
Ying Xue824e7382015-08-19 15:46:17 +080096static inline void lwtstate_free(struct lwtunnel_state *lws)
97{
98}
99
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +0200100static inline struct lwtunnel_state *
101lwtstate_get(struct lwtunnel_state *lws)
Roopa Prabhu499a2422015-07-21 10:43:46 +0200102{
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +0200103 return lws;
Roopa Prabhu499a2422015-07-21 10:43:46 +0200104}
105
Nicolas Dichtel5a6228a2015-07-24 12:28:36 +0200106static inline void lwtstate_put(struct lwtunnel_state *lws)
Roopa Prabhu499a2422015-07-21 10:43:46 +0200107{
108}
109
110static inline bool lwtunnel_output_redirect(struct lwtunnel_state *lwtstate)
111{
112 return false;
113}
114
Tom Herbert25368622015-08-17 13:42:24 -0700115static inline bool lwtunnel_input_redirect(struct lwtunnel_state *lwtstate)
116{
117 return false;
118}
119
Roopa Prabhu499a2422015-07-21 10:43:46 +0200120static inline int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *op,
121 unsigned int num)
122{
123 return -EOPNOTSUPP;
124
125}
126
127static inline int lwtunnel_encap_del_ops(const struct lwtunnel_encap_ops *op,
128 unsigned int num)
129{
130 return -EOPNOTSUPP;
131}
132
133static inline int lwtunnel_build_state(struct net_device *dev, u16 encap_type,
134 struct nlattr *encap,
Tom Herbert127eb7c2015-08-24 09:45:41 -0700135 unsigned int family, const void *cfg,
Roopa Prabhu499a2422015-07-21 10:43:46 +0200136 struct lwtunnel_state **lws)
137{
138 return -EOPNOTSUPP;
139}
140
141static inline int lwtunnel_fill_encap(struct sk_buff *skb,
142 struct lwtunnel_state *lwtstate)
143{
144 return 0;
145}
146
147static inline int lwtunnel_get_encap_size(struct lwtunnel_state *lwtstate)
148{
149 return 0;
150}
151
152static inline struct lwtunnel_state *lwtunnel_state_alloc(int hdr_len)
153{
154 return NULL;
155}
156
157static inline int lwtunnel_cmp_encap(struct lwtunnel_state *a,
158 struct lwtunnel_state *b)
159{
160 return 0;
161}
162
Eric W. Biedermanede20592015-10-07 16:48:47 -0500163static inline int lwtunnel_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Roopa Prabhuffce4192015-07-21 10:43:49 +0200164{
165 return -EOPNOTSUPP;
166}
167
Tom Herbert25368622015-08-17 13:42:24 -0700168static inline int lwtunnel_input(struct sk_buff *skb)
169{
170 return -EOPNOTSUPP;
171}
172
Robert Shearman745041e2016-02-19 09:43:16 +0000173#endif /* CONFIG_LWTUNNEL */
174
175#define MODULE_ALIAS_RTNL_LWT(encap_type) MODULE_ALIAS("rtnl-lwt-" __stringify(encap_type))
Roopa Prabhu499a2422015-07-21 10:43:46 +0200176
177#endif /* __NET_LWTUNNEL_H */