Commit | Line | Data |
---|---|---|
fd558d18 JC |
1 | /* |
2 | * L2TP internal definitions. | |
3 | * | |
4 | * Copyright (c) 2008,2009 Katalix Systems Ltd | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | */ | |
fbea9e07 | 10 | #include <linux/refcount.h> |
fd558d18 JC |
11 | |
12 | #ifndef _L2TP_CORE_H_ | |
13 | #define _L2TP_CORE_H_ | |
14 | ||
15 | /* Just some random numbers */ | |
16 | #define L2TP_TUNNEL_MAGIC 0x42114DDA | |
17 | #define L2TP_SESSION_MAGIC 0x0C04EB7D | |
18 | ||
f7faffa3 | 19 | /* Per tunnel, session hash table size */ |
fd558d18 JC |
20 | #define L2TP_HASH_BITS 4 |
21 | #define L2TP_HASH_SIZE (1 << L2TP_HASH_BITS) | |
22 | ||
f7faffa3 JC |
23 | /* System-wide, session hash table size */ |
24 | #define L2TP_HASH_BITS_2 8 | |
25 | #define L2TP_HASH_SIZE_2 (1 << L2TP_HASH_BITS_2) | |
26 | ||
fd558d18 JC |
27 | struct sk_buff; |
28 | ||
29 | struct l2tp_stats { | |
7b7c0719 TP |
30 | atomic_long_t tx_packets; |
31 | atomic_long_t tx_bytes; | |
32 | atomic_long_t tx_errors; | |
33 | atomic_long_t rx_packets; | |
34 | atomic_long_t rx_bytes; | |
35 | atomic_long_t rx_seq_discards; | |
36 | atomic_long_t rx_oos_packets; | |
37 | atomic_long_t rx_errors; | |
38 | atomic_long_t rx_cookie_discards; | |
fd558d18 JC |
39 | }; |
40 | ||
41 | struct l2tp_tunnel; | |
42 | ||
43 | /* Describes a session. Contains information to determine incoming | |
44 | * packets and transmit outgoing ones. | |
45 | */ | |
46 | struct l2tp_session_cfg { | |
f7faffa3 | 47 | enum l2tp_pwtype pw_type; |
95c96174 | 48 | unsigned int recv_seq:1; /* expect receive packets with |
fd558d18 | 49 | * sequence numbers? */ |
95c96174 | 50 | unsigned int send_seq:1; /* send packets with sequence |
fd558d18 | 51 | * numbers? */ |
95c96174 | 52 | unsigned int lns_mode:1; /* behave as LNS? LAC enables |
fd558d18 JC |
53 | * sequence numbers under |
54 | * control of LNS. */ | |
55 | int debug; /* bitmask of debug message | |
56 | * categories */ | |
f7faffa3 JC |
57 | u16 l2specific_type; /* Layer 2 specific type */ |
58 | u8 cookie[8]; /* optional cookie */ | |
59 | int cookie_len; /* 0, 4 or 8 bytes */ | |
60 | u8 peer_cookie[8]; /* peer's cookie */ | |
61 | int peer_cookie_len; /* 0, 4 or 8 bytes */ | |
fd558d18 JC |
62 | int reorder_timeout; /* configured reorder timeout |
63 | * (in jiffies) */ | |
64 | int mtu; | |
309795f4 | 65 | char *ifname; |
fd558d18 JC |
66 | }; |
67 | ||
68 | struct l2tp_session { | |
69 | int magic; /* should be | |
70 | * L2TP_SESSION_MAGIC */ | |
b228a940 | 71 | long dead; |
fd558d18 JC |
72 | |
73 | struct l2tp_tunnel *tunnel; /* back pointer to tunnel | |
74 | * context */ | |
75 | u32 session_id; | |
76 | u32 peer_session_id; | |
f7faffa3 JC |
77 | u8 cookie[8]; |
78 | int cookie_len; | |
79 | u8 peer_cookie[8]; | |
80 | int peer_cookie_len; | |
f7faffa3 JC |
81 | u16 l2specific_type; |
82 | u16 hdr_len; | |
83 | u32 nr; /* session NR state (receive) */ | |
84 | u32 ns; /* session NR state (send) */ | |
fd558d18 | 85 | struct sk_buff_head reorder_q; /* receive reorder queue */ |
8a1631d5 JC |
86 | u32 nr_max; /* max NR. Depends on tunnel */ |
87 | u32 nr_window_size; /* NR window size */ | |
a0dbd822 JC |
88 | u32 nr_oos; /* NR of last OOS packet */ |
89 | int nr_oos_count; /* For OOS recovery */ | |
90 | int nr_oos_count_max; | |
fd558d18 | 91 | struct hlist_node hlist; /* Hash list node */ |
f00c854c | 92 | refcount_t ref_count; |
fd558d18 JC |
93 | |
94 | char name[32]; /* for logging */ | |
309795f4 | 95 | char ifname[IFNAMSIZ]; |
95c96174 | 96 | unsigned int recv_seq:1; /* expect receive packets with |
fd558d18 | 97 | * sequence numbers? */ |
95c96174 | 98 | unsigned int send_seq:1; /* send packets with sequence |
fd558d18 | 99 | * numbers? */ |
95c96174 | 100 | unsigned int lns_mode:1; /* behave as LNS? LAC enables |
fd558d18 JC |
101 | * sequence numbers under |
102 | * control of LNS. */ | |
103 | int debug; /* bitmask of debug message | |
104 | * categories */ | |
105 | int reorder_timeout; /* configured reorder timeout | |
106 | * (in jiffies) */ | |
38d40b3f | 107 | int reorder_skip; /* set if skip to next nr */ |
fd558d18 | 108 | int mtu; |
f7faffa3 | 109 | enum l2tp_pwtype pwtype; |
fd558d18 | 110 | struct l2tp_stats stats; |
f7faffa3 | 111 | struct hlist_node global_hlist; /* Global hash list node */ |
fd558d18 | 112 | |
f7faffa3 | 113 | int (*build_header)(struct l2tp_session *session, void *buf); |
fd558d18 JC |
114 | void (*recv_skb)(struct l2tp_session *session, struct sk_buff *skb, int data_len); |
115 | void (*session_close)(struct l2tp_session *session); | |
9dd79945 | 116 | #if IS_ENABLED(CONFIG_L2TP_DEBUGFS) |
0ad66140 JC |
117 | void (*show)(struct seq_file *m, void *priv); |
118 | #endif | |
fd558d18 JC |
119 | uint8_t priv[0]; /* private data */ |
120 | }; | |
121 | ||
122 | /* Describes the tunnel. It contains info to track all the associated | |
123 | * sessions so incoming packets can be sorted out | |
124 | */ | |
125 | struct l2tp_tunnel_cfg { | |
126 | int debug; /* bitmask of debug message | |
127 | * categories */ | |
0d76751f | 128 | enum l2tp_encap_type encap; |
789a4a2c JC |
129 | |
130 | /* Used only for kernel-created sockets */ | |
131 | struct in_addr local_ip; | |
132 | struct in_addr peer_ip; | |
f9bac8df CE |
133 | #if IS_ENABLED(CONFIG_IPV6) |
134 | struct in6_addr *local_ip6; | |
135 | struct in6_addr *peer_ip6; | |
136 | #endif | |
789a4a2c JC |
137 | u16 local_udp_port; |
138 | u16 peer_udp_port; | |
6b649fea TH |
139 | unsigned int use_udp_checksums:1, |
140 | udp6_zero_tx_checksums:1, | |
141 | udp6_zero_rx_checksums:1; | |
fd558d18 JC |
142 | }; |
143 | ||
144 | struct l2tp_tunnel { | |
145 | int magic; /* Should be L2TP_TUNNEL_MAGIC */ | |
62b982ee SD |
146 | |
147 | unsigned long dead; | |
148 | ||
99469c32 | 149 | struct rcu_head rcu; |
fd558d18 | 150 | rwlock_t hlist_lock; /* protect session_hlist */ |
f3c66d4e GN |
151 | bool acpt_newsess; /* Indicates whether this |
152 | * tunnel accepts new sessions. | |
153 | * Protected by hlist_lock. | |
154 | */ | |
fd558d18 JC |
155 | struct hlist_head session_hlist[L2TP_HASH_SIZE]; |
156 | /* hashed list of sessions, | |
157 | * hashed by id */ | |
158 | u32 tunnel_id; | |
159 | u32 peer_tunnel_id; | |
160 | int version; /* 2=>L2TPv2, 3=>L2TPv3 */ | |
161 | ||
162 | char name[20]; /* for logging */ | |
163 | int debug; /* bitmask of debug message | |
164 | * categories */ | |
0d76751f | 165 | enum l2tp_encap_type encap; |
fd558d18 JC |
166 | struct l2tp_stats stats; |
167 | ||
168 | struct list_head list; /* Keep a list of all tunnels */ | |
169 | struct net *l2tp_net; /* the net we belong to */ | |
170 | ||
fbea9e07 | 171 | refcount_t ref_count; |
fd558d18 JC |
172 | void (*old_sk_destruct)(struct sock *); |
173 | struct sock *sock; /* Parent socket */ | |
80d84ef3 TP |
174 | int fd; /* Parent fd, if tunnel socket |
175 | * was created by userspace */ | |
fd558d18 | 176 | |
f8ccac0e | 177 | struct work_struct del_work; |
fd558d18 JC |
178 | }; |
179 | ||
309795f4 | 180 | struct l2tp_nl_cmd_ops { |
f026bc29 GN |
181 | int (*session_create)(struct net *net, struct l2tp_tunnel *tunnel, |
182 | u32 session_id, u32 peer_session_id, | |
183 | struct l2tp_session_cfg *cfg); | |
309795f4 JC |
184 | int (*session_delete)(struct l2tp_session *session); |
185 | }; | |
186 | ||
fd558d18 JC |
187 | static inline void *l2tp_session_priv(struct l2tp_session *session) |
188 | { | |
189 | return &session->priv[0]; | |
190 | } | |
191 | ||
54652eb1 | 192 | struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id); |
5846c131 GN |
193 | struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth); |
194 | ||
d00fa9ad | 195 | void l2tp_tunnel_free(struct l2tp_tunnel *tunnel); |
54652eb1 | 196 | |
9aaef50c | 197 | struct l2tp_session *l2tp_session_get(const struct net *net, |
61b9a047 | 198 | struct l2tp_tunnel *tunnel, |
a4346210 GN |
199 | u32 session_id); |
200 | struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth); | |
9aaef50c | 201 | struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net, |
a4346210 | 202 | const char *ifname); |
c1b1203d JP |
203 | |
204 | int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, | |
205 | u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg, | |
206 | struct l2tp_tunnel **tunnelp); | |
6b9f3423 GN |
207 | int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net, |
208 | struct l2tp_tunnel_cfg *cfg); | |
209 | ||
62b982ee | 210 | void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel); |
c1b1203d JP |
211 | struct l2tp_session *l2tp_session_create(int priv_size, |
212 | struct l2tp_tunnel *tunnel, | |
213 | u32 session_id, u32 peer_session_id, | |
214 | struct l2tp_session_cfg *cfg); | |
3953ae7b GN |
215 | int l2tp_session_register(struct l2tp_session *session, |
216 | struct l2tp_tunnel *tunnel); | |
217 | ||
c1b1203d JP |
218 | void __l2tp_session_unhash(struct l2tp_session *session); |
219 | int l2tp_session_delete(struct l2tp_session *session); | |
220 | void l2tp_session_free(struct l2tp_session *session); | |
221 | void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb, | |
222 | unsigned char *ptr, unsigned char *optr, u16 hdrflags, | |
2b139e6b | 223 | int length); |
c1b1203d | 224 | int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb); |
bb5016ea | 225 | void l2tp_session_set_header_len(struct l2tp_session *session, int version); |
c1b1203d JP |
226 | |
227 | int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, | |
228 | int hdr_len); | |
229 | ||
230 | int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, | |
231 | const struct l2tp_nl_cmd_ops *ops); | |
232 | void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type); | |
72fb96e7 | 233 | int l2tp_ioctl(struct sock *sk, int cmd, unsigned long arg); |
309795f4 | 234 | |
54652eb1 GN |
235 | static inline void l2tp_tunnel_inc_refcount(struct l2tp_tunnel *tunnel) |
236 | { | |
237 | refcount_inc(&tunnel->ref_count); | |
238 | } | |
239 | ||
240 | static inline void l2tp_tunnel_dec_refcount(struct l2tp_tunnel *tunnel) | |
241 | { | |
242 | if (refcount_dec_and_test(&tunnel->ref_count)) | |
d00fa9ad | 243 | l2tp_tunnel_free(tunnel); |
54652eb1 GN |
244 | } |
245 | ||
fd558d18 JC |
246 | /* Session reference counts. Incremented when code obtains a reference |
247 | * to a session. | |
248 | */ | |
9ff672ba | 249 | static inline void l2tp_session_inc_refcount(struct l2tp_session *session) |
fd558d18 | 250 | { |
f00c854c | 251 | refcount_inc(&session->ref_count); |
fd558d18 JC |
252 | } |
253 | ||
9ff672ba | 254 | static inline void l2tp_session_dec_refcount(struct l2tp_session *session) |
fd558d18 | 255 | { |
f00c854c | 256 | if (refcount_dec_and_test(&session->ref_count)) |
fd558d18 JC |
257 | l2tp_session_free(session); |
258 | } | |
259 | ||
62e7b6a5 LB |
260 | static inline int l2tp_get_l2specific_len(struct l2tp_session *session) |
261 | { | |
262 | switch (session->l2specific_type) { | |
263 | case L2TP_L2SPECTYPE_DEFAULT: | |
264 | return 4; | |
265 | case L2TP_L2SPECTYPE_NONE: | |
266 | default: | |
267 | return 0; | |
268 | } | |
269 | } | |
270 | ||
a4ca44fa JP |
271 | #define l2tp_printk(ptr, type, func, fmt, ...) \ |
272 | do { \ | |
273 | if (((ptr)->debug) & (type)) \ | |
274 | func(fmt, ##__VA_ARGS__); \ | |
275 | } while (0) | |
276 | ||
277 | #define l2tp_warn(ptr, type, fmt, ...) \ | |
278 | l2tp_printk(ptr, type, pr_warn, fmt, ##__VA_ARGS__) | |
279 | #define l2tp_info(ptr, type, fmt, ...) \ | |
280 | l2tp_printk(ptr, type, pr_info, fmt, ##__VA_ARGS__) | |
281 | #define l2tp_dbg(ptr, type, fmt, ...) \ | |
282 | l2tp_printk(ptr, type, pr_debug, fmt, ##__VA_ARGS__) | |
283 | ||
f1f39f91 | 284 | #define MODULE_ALIAS_L2TP_PWTYPE(type) \ |
285 | MODULE_ALIAS("net-l2tp-type-" __stringify(type)) | |
286 | ||
fd558d18 | 287 | #endif /* _L2TP_CORE_H_ */ |