rtlwifi: rtl_pci: Fix problem of too small skb->len
[linux/fpc-iii.git] / net / l2tp / l2tp_core.h
blob2db3d50d10a4d85e8bf5fb7f3ab50695ab54a015
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * L2TP internal definitions.
5 * Copyright (c) 2008,2009 Katalix Systems Ltd
6 */
7 #include <linux/refcount.h>
9 #ifndef _L2TP_CORE_H_
10 #define _L2TP_CORE_H_
12 #include <net/dst.h>
13 #include <net/sock.h>
15 #ifdef CONFIG_XFRM
16 #include <net/xfrm.h>
17 #endif
19 /* Just some random numbers */
20 #define L2TP_TUNNEL_MAGIC 0x42114DDA
21 #define L2TP_SESSION_MAGIC 0x0C04EB7D
23 /* Per tunnel, session hash table size */
24 #define L2TP_HASH_BITS 4
25 #define L2TP_HASH_SIZE (1 << L2TP_HASH_BITS)
27 /* System-wide, session hash table size */
28 #define L2TP_HASH_BITS_2 8
29 #define L2TP_HASH_SIZE_2 (1 << L2TP_HASH_BITS_2)
31 struct sk_buff;
33 struct l2tp_stats {
34 atomic_long_t tx_packets;
35 atomic_long_t tx_bytes;
36 atomic_long_t tx_errors;
37 atomic_long_t rx_packets;
38 atomic_long_t rx_bytes;
39 atomic_long_t rx_seq_discards;
40 atomic_long_t rx_oos_packets;
41 atomic_long_t rx_errors;
42 atomic_long_t rx_cookie_discards;
45 struct l2tp_tunnel;
47 /* Describes a session. Contains information to determine incoming
48 * packets and transmit outgoing ones.
50 struct l2tp_session_cfg {
51 enum l2tp_pwtype pw_type;
52 unsigned int recv_seq:1; /* expect receive packets with
53 * sequence numbers? */
54 unsigned int send_seq:1; /* send packets with sequence
55 * numbers? */
56 unsigned int lns_mode:1; /* behave as LNS? LAC enables
57 * sequence numbers under
58 * control of LNS. */
59 int debug; /* bitmask of debug message
60 * categories */
61 u16 l2specific_type; /* Layer 2 specific type */
62 u8 cookie[8]; /* optional cookie */
63 int cookie_len; /* 0, 4 or 8 bytes */
64 u8 peer_cookie[8]; /* peer's cookie */
65 int peer_cookie_len; /* 0, 4 or 8 bytes */
66 int reorder_timeout; /* configured reorder timeout
67 * (in jiffies) */
68 char *ifname;
71 struct l2tp_session {
72 int magic; /* should be
73 * L2TP_SESSION_MAGIC */
74 long dead;
76 struct l2tp_tunnel *tunnel; /* back pointer to tunnel
77 * context */
78 u32 session_id;
79 u32 peer_session_id;
80 u8 cookie[8];
81 int cookie_len;
82 u8 peer_cookie[8];
83 int peer_cookie_len;
84 u16 l2specific_type;
85 u16 hdr_len;
86 u32 nr; /* session NR state (receive) */
87 u32 ns; /* session NR state (send) */
88 struct sk_buff_head reorder_q; /* receive reorder queue */
89 u32 nr_max; /* max NR. Depends on tunnel */
90 u32 nr_window_size; /* NR window size */
91 u32 nr_oos; /* NR of last OOS packet */
92 int nr_oos_count; /* For OOS recovery */
93 int nr_oos_count_max;
94 struct hlist_node hlist; /* Hash list node */
95 refcount_t ref_count;
97 char name[32]; /* for logging */
98 char ifname[IFNAMSIZ];
99 unsigned int recv_seq:1; /* expect receive packets with
100 * sequence numbers? */
101 unsigned int send_seq:1; /* send packets with sequence
102 * numbers? */
103 unsigned int lns_mode:1; /* behave as LNS? LAC enables
104 * sequence numbers under
105 * control of LNS. */
106 int debug; /* bitmask of debug message
107 * categories */
108 int reorder_timeout; /* configured reorder timeout
109 * (in jiffies) */
110 int reorder_skip; /* set if skip to next nr */
111 enum l2tp_pwtype pwtype;
112 struct l2tp_stats stats;
113 struct hlist_node global_hlist; /* Global hash list node */
115 int (*build_header)(struct l2tp_session *session, void *buf);
116 void (*recv_skb)(struct l2tp_session *session, struct sk_buff *skb, int data_len);
117 void (*session_close)(struct l2tp_session *session);
118 void (*show)(struct seq_file *m, void *priv);
119 uint8_t priv[0]; /* private data */
122 /* Describes the tunnel. It contains info to track all the associated
123 * sessions so incoming packets can be sorted out
125 struct l2tp_tunnel_cfg {
126 int debug; /* bitmask of debug message
127 * categories */
128 enum l2tp_encap_type encap;
130 /* Used only for kernel-created sockets */
131 struct in_addr local_ip;
132 struct in_addr peer_ip;
133 #if IS_ENABLED(CONFIG_IPV6)
134 struct in6_addr *local_ip6;
135 struct in6_addr *peer_ip6;
136 #endif
137 u16 local_udp_port;
138 u16 peer_udp_port;
139 unsigned int use_udp_checksums:1,
140 udp6_zero_tx_checksums:1,
141 udp6_zero_rx_checksums:1;
144 struct l2tp_tunnel {
145 int magic; /* Should be L2TP_TUNNEL_MAGIC */
147 unsigned long dead;
149 struct rcu_head rcu;
150 rwlock_t hlist_lock; /* protect session_hlist */
151 bool acpt_newsess; /* Indicates whether this
152 * tunnel accepts new sessions.
153 * Protected by hlist_lock.
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 */
162 char name[20]; /* for logging */
163 int debug; /* bitmask of debug message
164 * categories */
165 enum l2tp_encap_type encap;
166 struct l2tp_stats stats;
168 struct list_head list; /* Keep a list of all tunnels */
169 struct net *l2tp_net; /* the net we belong to */
171 refcount_t ref_count;
172 void (*old_sk_destruct)(struct sock *);
173 struct sock *sock; /* Parent socket */
174 int fd; /* Parent fd, if tunnel socket
175 * was created by userspace */
177 struct work_struct del_work;
180 struct l2tp_nl_cmd_ops {
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);
184 int (*session_delete)(struct l2tp_session *session);
187 static inline void *l2tp_session_priv(struct l2tp_session *session)
189 return &session->priv[0];
192 struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id);
193 struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth);
194 struct l2tp_session *l2tp_tunnel_get_session(struct l2tp_tunnel *tunnel,
195 u32 session_id);
197 void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
199 struct l2tp_session *l2tp_session_get(const struct net *net, u32 session_id);
200 struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth);
201 struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
202 const char *ifname);
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);
207 int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
208 struct l2tp_tunnel_cfg *cfg);
210 void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel);
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);
215 int l2tp_session_register(struct l2tp_session *session,
216 struct l2tp_tunnel *tunnel);
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,
223 int length);
224 int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb);
225 void l2tp_session_set_header_len(struct l2tp_session *session, int version);
227 int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb,
228 int hdr_len);
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);
233 int l2tp_ioctl(struct sock *sk, int cmd, unsigned long arg);
235 static inline void l2tp_tunnel_inc_refcount(struct l2tp_tunnel *tunnel)
237 refcount_inc(&tunnel->ref_count);
240 static inline void l2tp_tunnel_dec_refcount(struct l2tp_tunnel *tunnel)
242 if (refcount_dec_and_test(&tunnel->ref_count))
243 l2tp_tunnel_free(tunnel);
246 /* Session reference counts. Incremented when code obtains a reference
247 * to a session.
249 static inline void l2tp_session_inc_refcount(struct l2tp_session *session)
251 refcount_inc(&session->ref_count);
254 static inline void l2tp_session_dec_refcount(struct l2tp_session *session)
256 if (refcount_dec_and_test(&session->ref_count))
257 l2tp_session_free(session);
260 static inline int l2tp_get_l2specific_len(struct l2tp_session *session)
262 switch (session->l2specific_type) {
263 case L2TP_L2SPECTYPE_DEFAULT:
264 return 4;
265 case L2TP_L2SPECTYPE_NONE:
266 default:
267 return 0;
271 static inline u32 l2tp_tunnel_dst_mtu(const struct l2tp_tunnel *tunnel)
273 struct dst_entry *dst;
274 u32 mtu;
276 dst = sk_dst_get(tunnel->sock);
277 if (!dst)
278 return 0;
280 mtu = dst_mtu(dst);
281 dst_release(dst);
283 return mtu;
286 #ifdef CONFIG_XFRM
287 static inline bool l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel *tunnel)
289 struct sock *sk = tunnel->sock;
291 return sk && (rcu_access_pointer(sk->sk_policy[0]) ||
292 rcu_access_pointer(sk->sk_policy[1]));
294 #else
295 static inline bool l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel *tunnel)
297 return false;
299 #endif
301 static inline int l2tp_v3_ensure_opt_in_linear(struct l2tp_session *session, struct sk_buff *skb,
302 unsigned char **ptr, unsigned char **optr)
304 int opt_len = session->peer_cookie_len + l2tp_get_l2specific_len(session);
306 if (opt_len > 0) {
307 int off = *ptr - *optr;
309 if (!pskb_may_pull(skb, off + opt_len))
310 return -1;
312 if (skb->data != *optr) {
313 *optr = skb->data;
314 *ptr = skb->data + off;
318 return 0;
321 #define l2tp_printk(ptr, type, func, fmt, ...) \
322 do { \
323 if (((ptr)->debug) & (type)) \
324 func(fmt, ##__VA_ARGS__); \
325 } while (0)
327 #define l2tp_warn(ptr, type, fmt, ...) \
328 l2tp_printk(ptr, type, pr_warn, fmt, ##__VA_ARGS__)
329 #define l2tp_info(ptr, type, fmt, ...) \
330 l2tp_printk(ptr, type, pr_info, fmt, ##__VA_ARGS__)
331 #define l2tp_dbg(ptr, type, fmt, ...) \
332 l2tp_printk(ptr, type, pr_debug, fmt, ##__VA_ARGS__)
334 #define MODULE_ALIAS_L2TP_PWTYPE(type) \
335 MODULE_ALIAS("net-l2tp-type-" __stringify(type))
337 #endif /* _L2TP_CORE_H_ */