Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /* L2TP internal definitions.
0003  *
0004  * Copyright (c) 2008,2009 Katalix Systems Ltd
0005  */
0006 #include <linux/refcount.h>
0007 
0008 #ifndef _L2TP_CORE_H_
0009 #define _L2TP_CORE_H_
0010 
0011 #include <net/dst.h>
0012 #include <net/sock.h>
0013 
0014 #ifdef CONFIG_XFRM
0015 #include <net/xfrm.h>
0016 #endif
0017 
0018 /* Random numbers used for internal consistency checks of tunnel and session structures */
0019 #define L2TP_TUNNEL_MAGIC   0x42114DDA
0020 #define L2TP_SESSION_MAGIC  0x0C04EB7D
0021 
0022 /* Per tunnel session hash table size */
0023 #define L2TP_HASH_BITS  4
0024 #define L2TP_HASH_SIZE  BIT(L2TP_HASH_BITS)
0025 
0026 /* System-wide session hash table size */
0027 #define L2TP_HASH_BITS_2    8
0028 #define L2TP_HASH_SIZE_2    BIT(L2TP_HASH_BITS_2)
0029 
0030 struct sk_buff;
0031 
0032 struct l2tp_stats {
0033     atomic_long_t       tx_packets;
0034     atomic_long_t       tx_bytes;
0035     atomic_long_t       tx_errors;
0036     atomic_long_t       rx_packets;
0037     atomic_long_t       rx_bytes;
0038     atomic_long_t       rx_seq_discards;
0039     atomic_long_t       rx_oos_packets;
0040     atomic_long_t       rx_errors;
0041     atomic_long_t       rx_cookie_discards;
0042     atomic_long_t       rx_invalid;
0043 };
0044 
0045 struct l2tp_tunnel;
0046 
0047 /* L2TP session configuration */
0048 struct l2tp_session_cfg {
0049     enum l2tp_pwtype    pw_type;
0050     unsigned int        recv_seq:1; /* expect receive packets with sequence numbers? */
0051     unsigned int        send_seq:1; /* send packets with sequence numbers? */
0052     unsigned int        lns_mode:1; /* behave as LNS?
0053                          * LAC enables sequence numbers under LNS control.
0054                          */
0055     u16         l2specific_type; /* Layer 2 specific type */
0056     u8          cookie[8];  /* optional cookie */
0057     int         cookie_len; /* 0, 4 or 8 bytes */
0058     u8          peer_cookie[8]; /* peer's cookie */
0059     int         peer_cookie_len; /* 0, 4 or 8 bytes */
0060     int         reorder_timeout; /* configured reorder timeout (in jiffies) */
0061     char            *ifname;
0062 };
0063 
0064 /* Represents a session (pseudowire) instance.
0065  * Tracks runtime state including cookies, dataplane packet sequencing, and IO statistics.
0066  * Is linked into a per-tunnel session hashlist; and in the case of an L2TPv3 session into
0067  * an additional per-net ("global") hashlist.
0068  */
0069 #define L2TP_SESSION_NAME_MAX 32
0070 struct l2tp_session {
0071     int         magic;      /* should be L2TP_SESSION_MAGIC */
0072     long            dead;
0073 
0074     struct l2tp_tunnel  *tunnel;    /* back pointer to tunnel context */
0075     u32         session_id;
0076     u32         peer_session_id;
0077     u8          cookie[8];
0078     int         cookie_len;
0079     u8          peer_cookie[8];
0080     int         peer_cookie_len;
0081     u16         l2specific_type;
0082     u16         hdr_len;
0083     u32         nr;     /* session NR state (receive) */
0084     u32         ns;     /* session NR state (send) */
0085     struct sk_buff_head reorder_q;  /* receive reorder queue */
0086     u32         nr_max;     /* max NR. Depends on tunnel */
0087     u32         nr_window_size; /* NR window size */
0088     u32         nr_oos;     /* NR of last OOS packet */
0089     int         nr_oos_count;   /* for OOS recovery */
0090     int         nr_oos_count_max;
0091     struct hlist_node   hlist;      /* hash list node */
0092     refcount_t      ref_count;
0093 
0094     char            name[L2TP_SESSION_NAME_MAX]; /* for logging */
0095     char            ifname[IFNAMSIZ];
0096     unsigned int        recv_seq:1; /* expect receive packets with sequence numbers? */
0097     unsigned int        send_seq:1; /* send packets with sequence numbers? */
0098     unsigned int        lns_mode:1; /* behave as LNS?
0099                          * LAC enables sequence numbers under LNS control.
0100                          */
0101     int         reorder_timeout; /* configured reorder timeout (in jiffies) */
0102     int         reorder_skip;   /* set if skip to next nr */
0103     enum l2tp_pwtype    pwtype;
0104     struct l2tp_stats   stats;
0105     struct hlist_node   global_hlist;   /* global hash list node */
0106 
0107     /* Session receive handler for data packets.
0108      * Each pseudowire implementation should implement this callback in order to
0109      * handle incoming packets.  Packets are passed to the pseudowire handler after
0110      * reordering, if data sequence numbers are enabled for the session.
0111      */
0112     void (*recv_skb)(struct l2tp_session *session, struct sk_buff *skb, int data_len);
0113 
0114     /* Session close handler.
0115      * Each pseudowire implementation may implement this callback in order to carry
0116      * out pseudowire-specific shutdown actions.
0117      * The callback is called by core after unhashing the session and purging its
0118      * reorder queue.
0119      */
0120     void (*session_close)(struct l2tp_session *session);
0121 
0122     /* Session show handler.
0123      * Pseudowire-specific implementation of debugfs session rendering.
0124      * The callback is called by l2tp_debugfs.c after rendering core session
0125      * information.
0126      */
0127     void (*show)(struct seq_file *m, void *priv);
0128 
0129     u8          priv[];     /* private data */
0130 };
0131 
0132 /* L2TP tunnel configuration */
0133 struct l2tp_tunnel_cfg {
0134     enum l2tp_encap_type    encap;
0135 
0136     /* Used only for kernel-created sockets */
0137     struct in_addr      local_ip;
0138     struct in_addr      peer_ip;
0139 #if IS_ENABLED(CONFIG_IPV6)
0140     struct in6_addr     *local_ip6;
0141     struct in6_addr     *peer_ip6;
0142 #endif
0143     u16         local_udp_port;
0144     u16         peer_udp_port;
0145     unsigned int        use_udp_checksums:1,
0146                 udp6_zero_tx_checksums:1,
0147                 udp6_zero_rx_checksums:1;
0148 };
0149 
0150 /* Represents a tunnel instance.
0151  * Tracks runtime state including IO statistics.
0152  * Holds the tunnel socket (either passed from userspace or directly created by the kernel).
0153  * Maintains a hashlist of sessions belonging to the tunnel instance.
0154  * Is linked into a per-net list of tunnels.
0155  */
0156 #define L2TP_TUNNEL_NAME_MAX 20
0157 struct l2tp_tunnel {
0158     int         magic;      /* Should be L2TP_TUNNEL_MAGIC */
0159 
0160     unsigned long       dead;
0161 
0162     struct rcu_head rcu;
0163     spinlock_t      hlist_lock; /* write-protection for session_hlist */
0164     bool            acpt_newsess;   /* indicates whether this tunnel accepts
0165                          * new sessions. Protected by hlist_lock.
0166                          */
0167     struct hlist_head   session_hlist[L2TP_HASH_SIZE];
0168                         /* hashed list of sessions, hashed by id */
0169     u32         tunnel_id;
0170     u32         peer_tunnel_id;
0171     int         version;    /* 2=>L2TPv2, 3=>L2TPv3 */
0172 
0173     char            name[L2TP_TUNNEL_NAME_MAX]; /* for logging */
0174     enum l2tp_encap_type    encap;
0175     struct l2tp_stats   stats;
0176 
0177     struct list_head    list;       /* list node on per-namespace list of tunnels */
0178     struct net      *l2tp_net;  /* the net we belong to */
0179 
0180     refcount_t      ref_count;
0181     void (*old_sk_destruct)(struct sock *sk);
0182     struct sock     *sock;      /* parent socket */
0183     int         fd;     /* parent fd, if tunnel socket was created
0184                          * by userspace
0185                          */
0186 
0187     struct work_struct  del_work;
0188 };
0189 
0190 /* Pseudowire ops callbacks for use with the l2tp genetlink interface */
0191 struct l2tp_nl_cmd_ops {
0192     /* The pseudowire session create callback is responsible for creating a session
0193      * instance for a specific pseudowire type.
0194      * It must call l2tp_session_create and l2tp_session_register to register the
0195      * session instance, as well as carry out any pseudowire-specific initialisation.
0196      * It must return >= 0 on success, or an appropriate negative errno value on failure.
0197      */
0198     int (*session_create)(struct net *net, struct l2tp_tunnel *tunnel,
0199                   u32 session_id, u32 peer_session_id,
0200                   struct l2tp_session_cfg *cfg);
0201 
0202     /* The pseudowire session delete callback is responsible for initiating the deletion
0203      * of a session instance.
0204      * It must call l2tp_session_delete, as well as carry out any pseudowire-specific
0205      * teardown actions.
0206      */
0207     void (*session_delete)(struct l2tp_session *session);
0208 };
0209 
0210 static inline void *l2tp_session_priv(struct l2tp_session *session)
0211 {
0212     return &session->priv[0];
0213 }
0214 
0215 /* Tunnel and session refcounts */
0216 void l2tp_tunnel_inc_refcount(struct l2tp_tunnel *tunnel);
0217 void l2tp_tunnel_dec_refcount(struct l2tp_tunnel *tunnel);
0218 void l2tp_session_inc_refcount(struct l2tp_session *session);
0219 void l2tp_session_dec_refcount(struct l2tp_session *session);
0220 
0221 /* Tunnel and session lookup.
0222  * These functions take a reference on the instances they return, so
0223  * the caller must ensure that the reference is dropped appropriately.
0224  */
0225 struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id);
0226 struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth);
0227 struct l2tp_session *l2tp_tunnel_get_session(struct l2tp_tunnel *tunnel,
0228                          u32 session_id);
0229 
0230 struct l2tp_session *l2tp_session_get(const struct net *net, u32 session_id);
0231 struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth);
0232 struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
0233                         const char *ifname);
0234 
0235 /* Tunnel and session lifetime management.
0236  * Creation of a new instance is a two-step process: create, then register.
0237  * Destruction is triggered using the *_delete functions, and completes asynchronously.
0238  */
0239 int l2tp_tunnel_create(int fd, int version, u32 tunnel_id,
0240                u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg,
0241                struct l2tp_tunnel **tunnelp);
0242 int l2tp_tunnel_register(struct l2tp_tunnel *tunnel, struct net *net,
0243              struct l2tp_tunnel_cfg *cfg);
0244 void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel);
0245 
0246 struct l2tp_session *l2tp_session_create(int priv_size,
0247                      struct l2tp_tunnel *tunnel,
0248                      u32 session_id, u32 peer_session_id,
0249                      struct l2tp_session_cfg *cfg);
0250 int l2tp_session_register(struct l2tp_session *session,
0251               struct l2tp_tunnel *tunnel);
0252 void l2tp_session_delete(struct l2tp_session *session);
0253 
0254 /* Receive path helpers.  If data sequencing is enabled for the session these
0255  * functions handle queuing and reordering prior to passing packets to the
0256  * pseudowire code to be passed to userspace.
0257  */
0258 void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
0259               unsigned char *ptr, unsigned char *optr, u16 hdrflags,
0260               int length);
0261 int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb);
0262 
0263 /* Transmit path helpers for sending packets over the tunnel socket. */
0264 void l2tp_session_set_header_len(struct l2tp_session *session, int version);
0265 int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb);
0266 
0267 /* Pseudowire management.
0268  * Pseudowires should register with l2tp core on module init, and unregister
0269  * on module exit.
0270  */
0271 int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops);
0272 void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type);
0273 
0274 /* IOCTL helper for IP encap modules. */
0275 int l2tp_ioctl(struct sock *sk, int cmd, unsigned long arg);
0276 
0277 /* Extract the tunnel structure from a socket's sk_user_data pointer,
0278  * validating the tunnel magic feather.
0279  */
0280 struct l2tp_tunnel *l2tp_sk_to_tunnel(struct sock *sk);
0281 
0282 static inline int l2tp_get_l2specific_len(struct l2tp_session *session)
0283 {
0284     switch (session->l2specific_type) {
0285     case L2TP_L2SPECTYPE_DEFAULT:
0286         return 4;
0287     case L2TP_L2SPECTYPE_NONE:
0288     default:
0289         return 0;
0290     }
0291 }
0292 
0293 static inline u32 l2tp_tunnel_dst_mtu(const struct l2tp_tunnel *tunnel)
0294 {
0295     struct dst_entry *dst;
0296     u32 mtu;
0297 
0298     dst = sk_dst_get(tunnel->sock);
0299     if (!dst)
0300         return 0;
0301 
0302     mtu = dst_mtu(dst);
0303     dst_release(dst);
0304 
0305     return mtu;
0306 }
0307 
0308 #ifdef CONFIG_XFRM
0309 static inline bool l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel *tunnel)
0310 {
0311     struct sock *sk = tunnel->sock;
0312 
0313     return sk && (rcu_access_pointer(sk->sk_policy[0]) ||
0314               rcu_access_pointer(sk->sk_policy[1]));
0315 }
0316 #else
0317 static inline bool l2tp_tunnel_uses_xfrm(const struct l2tp_tunnel *tunnel)
0318 {
0319     return false;
0320 }
0321 #endif
0322 
0323 static inline int l2tp_v3_ensure_opt_in_linear(struct l2tp_session *session, struct sk_buff *skb,
0324                            unsigned char **ptr, unsigned char **optr)
0325 {
0326     int opt_len = session->peer_cookie_len + l2tp_get_l2specific_len(session);
0327 
0328     if (opt_len > 0) {
0329         int off = *ptr - *optr;
0330 
0331         if (!pskb_may_pull(skb, off + opt_len))
0332             return -1;
0333 
0334         if (skb->data != *optr) {
0335             *optr = skb->data;
0336             *ptr = skb->data + off;
0337         }
0338     }
0339 
0340     return 0;
0341 }
0342 
0343 #define MODULE_ALIAS_L2TP_PWTYPE(type) \
0344     MODULE_ALIAS("net-l2tp-type-" __stringify(type))
0345 
0346 #endif /* _L2TP_CORE_H_ */