1fd558d18SJames Chapman /* 2fd558d18SJames Chapman * L2TP internal definitions. 3fd558d18SJames Chapman * 4fd558d18SJames Chapman * Copyright (c) 2008,2009 Katalix Systems Ltd 5fd558d18SJames Chapman * 6fd558d18SJames Chapman * This program is free software; you can redistribute it and/or modify 7fd558d18SJames Chapman * it under the terms of the GNU General Public License version 2 as 8fd558d18SJames Chapman * published by the Free Software Foundation. 9fd558d18SJames Chapman */ 10fd558d18SJames Chapman 11fd558d18SJames Chapman #ifndef _L2TP_CORE_H_ 12fd558d18SJames Chapman #define _L2TP_CORE_H_ 13fd558d18SJames Chapman 14fd558d18SJames Chapman /* Just some random numbers */ 15fd558d18SJames Chapman #define L2TP_TUNNEL_MAGIC 0x42114DDA 16fd558d18SJames Chapman #define L2TP_SESSION_MAGIC 0x0C04EB7D 17fd558d18SJames Chapman 18f7faffa3SJames Chapman /* Per tunnel, session hash table size */ 19fd558d18SJames Chapman #define L2TP_HASH_BITS 4 20fd558d18SJames Chapman #define L2TP_HASH_SIZE (1 << L2TP_HASH_BITS) 21fd558d18SJames Chapman 22f7faffa3SJames Chapman /* System-wide, session hash table size */ 23f7faffa3SJames Chapman #define L2TP_HASH_BITS_2 8 24f7faffa3SJames Chapman #define L2TP_HASH_SIZE_2 (1 << L2TP_HASH_BITS_2) 25f7faffa3SJames Chapman 26fd558d18SJames Chapman /* Debug message categories for the DEBUG socket option */ 27fd558d18SJames Chapman enum { 28fd558d18SJames Chapman L2TP_MSG_DEBUG = (1 << 0), /* verbose debug (if 29fd558d18SJames Chapman * compiled in) */ 30fd558d18SJames Chapman L2TP_MSG_CONTROL = (1 << 1), /* userspace - kernel 31fd558d18SJames Chapman * interface */ 32fd558d18SJames Chapman L2TP_MSG_SEQ = (1 << 2), /* sequence numbers */ 33fd558d18SJames Chapman L2TP_MSG_DATA = (1 << 3), /* data packets */ 34fd558d18SJames Chapman }; 35fd558d18SJames Chapman 36fd558d18SJames Chapman struct sk_buff; 37fd558d18SJames Chapman 38fd558d18SJames Chapman struct l2tp_stats { 39fd558d18SJames Chapman u64 tx_packets; 40fd558d18SJames Chapman u64 tx_bytes; 41fd558d18SJames Chapman u64 tx_errors; 42fd558d18SJames Chapman u64 rx_packets; 43fd558d18SJames Chapman u64 rx_bytes; 44fd558d18SJames Chapman u64 rx_seq_discards; 45fd558d18SJames Chapman u64 rx_oos_packets; 46fd558d18SJames Chapman u64 rx_errors; 47f7faffa3SJames Chapman u64 rx_cookie_discards; 485de7aee5SJames Chapman struct u64_stats_sync syncp; 49fd558d18SJames Chapman }; 50fd558d18SJames Chapman 51fd558d18SJames Chapman struct l2tp_tunnel; 52fd558d18SJames Chapman 53fd558d18SJames Chapman /* Describes a session. Contains information to determine incoming 54fd558d18SJames Chapman * packets and transmit outgoing ones. 55fd558d18SJames Chapman */ 56fd558d18SJames Chapman struct l2tp_session_cfg { 57f7faffa3SJames Chapman enum l2tp_pwtype pw_type; 5895c96174SEric Dumazet unsigned int data_seq:2; /* data sequencing level 59fd558d18SJames Chapman * 0 => none, 1 => IP only, 60fd558d18SJames Chapman * 2 => all 61fd558d18SJames Chapman */ 6295c96174SEric Dumazet unsigned int recv_seq:1; /* expect receive packets with 63fd558d18SJames Chapman * sequence numbers? */ 6495c96174SEric Dumazet unsigned int send_seq:1; /* send packets with sequence 65fd558d18SJames Chapman * numbers? */ 6695c96174SEric Dumazet unsigned int lns_mode:1; /* behave as LNS? LAC enables 67fd558d18SJames Chapman * sequence numbers under 68fd558d18SJames Chapman * control of LNS. */ 69fd558d18SJames Chapman int debug; /* bitmask of debug message 70fd558d18SJames Chapman * categories */ 71309795f4SJames Chapman u16 vlan_id; /* VLAN pseudowire only */ 72f7faffa3SJames Chapman u16 offset; /* offset to payload */ 73f7faffa3SJames Chapman u16 l2specific_len; /* Layer 2 specific length */ 74f7faffa3SJames Chapman u16 l2specific_type; /* Layer 2 specific type */ 75f7faffa3SJames Chapman u8 cookie[8]; /* optional cookie */ 76f7faffa3SJames Chapman int cookie_len; /* 0, 4 or 8 bytes */ 77f7faffa3SJames Chapman u8 peer_cookie[8]; /* peer's cookie */ 78f7faffa3SJames Chapman int peer_cookie_len; /* 0, 4 or 8 bytes */ 79fd558d18SJames Chapman int reorder_timeout; /* configured reorder timeout 80fd558d18SJames Chapman * (in jiffies) */ 81fd558d18SJames Chapman int mtu; 82fd558d18SJames Chapman int mru; 83309795f4SJames Chapman char *ifname; 84fd558d18SJames Chapman }; 85fd558d18SJames Chapman 86fd558d18SJames Chapman struct l2tp_session { 87fd558d18SJames Chapman int magic; /* should be 88fd558d18SJames Chapman * L2TP_SESSION_MAGIC */ 89fd558d18SJames Chapman 90fd558d18SJames Chapman struct l2tp_tunnel *tunnel; /* back pointer to tunnel 91fd558d18SJames Chapman * context */ 92fd558d18SJames Chapman u32 session_id; 93fd558d18SJames Chapman u32 peer_session_id; 94f7faffa3SJames Chapman u8 cookie[8]; 95f7faffa3SJames Chapman int cookie_len; 96f7faffa3SJames Chapman u8 peer_cookie[8]; 97f7faffa3SJames Chapman int peer_cookie_len; 98f7faffa3SJames Chapman u16 offset; /* offset from end of L2TP header 99f7faffa3SJames Chapman to beginning of data */ 100f7faffa3SJames Chapman u16 l2specific_len; 101f7faffa3SJames Chapman u16 l2specific_type; 102f7faffa3SJames Chapman u16 hdr_len; 103f7faffa3SJames Chapman u32 nr; /* session NR state (receive) */ 104f7faffa3SJames Chapman u32 ns; /* session NR state (send) */ 105fd558d18SJames Chapman struct sk_buff_head reorder_q; /* receive reorder queue */ 106fd558d18SJames Chapman struct hlist_node hlist; /* Hash list node */ 107fd558d18SJames Chapman atomic_t ref_count; 108fd558d18SJames Chapman 109fd558d18SJames Chapman char name[32]; /* for logging */ 110309795f4SJames Chapman char ifname[IFNAMSIZ]; 11195c96174SEric Dumazet unsigned int data_seq:2; /* data sequencing level 112fd558d18SJames Chapman * 0 => none, 1 => IP only, 113fd558d18SJames Chapman * 2 => all 114fd558d18SJames Chapman */ 11595c96174SEric Dumazet unsigned int recv_seq:1; /* expect receive packets with 116fd558d18SJames Chapman * sequence numbers? */ 11795c96174SEric Dumazet unsigned int send_seq:1; /* send packets with sequence 118fd558d18SJames Chapman * numbers? */ 11995c96174SEric Dumazet unsigned int lns_mode:1; /* behave as LNS? LAC enables 120fd558d18SJames Chapman * sequence numbers under 121fd558d18SJames Chapman * control of LNS. */ 122fd558d18SJames Chapman int debug; /* bitmask of debug message 123fd558d18SJames Chapman * categories */ 124fd558d18SJames Chapman int reorder_timeout; /* configured reorder timeout 125fd558d18SJames Chapman * (in jiffies) */ 12638d40b3fSJames Chapman int reorder_skip; /* set if skip to next nr */ 127fd558d18SJames Chapman int mtu; 128fd558d18SJames Chapman int mru; 129f7faffa3SJames Chapman enum l2tp_pwtype pwtype; 130fd558d18SJames Chapman struct l2tp_stats stats; 131f7faffa3SJames Chapman struct hlist_node global_hlist; /* Global hash list node */ 132fd558d18SJames Chapman 133f7faffa3SJames Chapman int (*build_header)(struct l2tp_session *session, void *buf); 134fd558d18SJames Chapman void (*recv_skb)(struct l2tp_session *session, struct sk_buff *skb, int data_len); 135fd558d18SJames Chapman void (*session_close)(struct l2tp_session *session); 136fd558d18SJames Chapman void (*ref)(struct l2tp_session *session); 137fd558d18SJames Chapman void (*deref)(struct l2tp_session *session); 138f66ef2d0SDavid S. Miller #if defined(CONFIG_L2TP_DEBUGFS) || defined(CONFIG_L2TP_DEBUGFS_MODULE) 1390ad66140SJames Chapman void (*show)(struct seq_file *m, void *priv); 1400ad66140SJames Chapman #endif 141fd558d18SJames Chapman uint8_t priv[0]; /* private data */ 142fd558d18SJames Chapman }; 143fd558d18SJames Chapman 144fd558d18SJames Chapman /* Describes the tunnel. It contains info to track all the associated 145fd558d18SJames Chapman * sessions so incoming packets can be sorted out 146fd558d18SJames Chapman */ 147fd558d18SJames Chapman struct l2tp_tunnel_cfg { 148fd558d18SJames Chapman int debug; /* bitmask of debug message 149fd558d18SJames Chapman * categories */ 1500d76751fSJames Chapman enum l2tp_encap_type encap; 151789a4a2cSJames Chapman 152789a4a2cSJames Chapman /* Used only for kernel-created sockets */ 153789a4a2cSJames Chapman struct in_addr local_ip; 154789a4a2cSJames Chapman struct in_addr peer_ip; 155f9bac8dfSChris Elston #if IS_ENABLED(CONFIG_IPV6) 156f9bac8dfSChris Elston struct in6_addr *local_ip6; 157f9bac8dfSChris Elston struct in6_addr *peer_ip6; 158f9bac8dfSChris Elston #endif 159789a4a2cSJames Chapman u16 local_udp_port; 160789a4a2cSJames Chapman u16 peer_udp_port; 1617bddd0dbSEric Dumazet unsigned int use_udp_checksums:1; 162fd558d18SJames Chapman }; 163fd558d18SJames Chapman 164fd558d18SJames Chapman struct l2tp_tunnel { 165fd558d18SJames Chapman int magic; /* Should be L2TP_TUNNEL_MAGIC */ 166fd558d18SJames Chapman rwlock_t hlist_lock; /* protect session_hlist */ 167fd558d18SJames Chapman struct hlist_head session_hlist[L2TP_HASH_SIZE]; 168fd558d18SJames Chapman /* hashed list of sessions, 169fd558d18SJames Chapman * hashed by id */ 170fd558d18SJames Chapman u32 tunnel_id; 171fd558d18SJames Chapman u32 peer_tunnel_id; 172fd558d18SJames Chapman int version; /* 2=>L2TPv2, 3=>L2TPv3 */ 173fd558d18SJames Chapman 174fd558d18SJames Chapman char name[20]; /* for logging */ 175fd558d18SJames Chapman int debug; /* bitmask of debug message 176fd558d18SJames Chapman * categories */ 1770d76751fSJames Chapman enum l2tp_encap_type encap; 178fd558d18SJames Chapman struct l2tp_stats stats; 179fd558d18SJames Chapman 180fd558d18SJames Chapman struct list_head list; /* Keep a list of all tunnels */ 181fd558d18SJames Chapman struct net *l2tp_net; /* the net we belong to */ 182fd558d18SJames Chapman 183fd558d18SJames Chapman atomic_t ref_count; 1840ad66140SJames Chapman #ifdef CONFIG_DEBUG_FS 1850ad66140SJames Chapman void (*show)(struct seq_file *m, void *arg); 1860ad66140SJames Chapman #endif 187fd558d18SJames Chapman int (*recv_payload_hook)(struct sk_buff *skb); 188fd558d18SJames Chapman void (*old_sk_destruct)(struct sock *); 189fd558d18SJames Chapman struct sock *sock; /* Parent socket */ 190fd558d18SJames Chapman int fd; 191fd558d18SJames Chapman 192fd558d18SJames Chapman uint8_t priv[0]; /* private data */ 193fd558d18SJames Chapman }; 194fd558d18SJames Chapman 195309795f4SJames Chapman struct l2tp_nl_cmd_ops { 196309795f4SJames Chapman int (*session_create)(struct net *net, u32 tunnel_id, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg); 197309795f4SJames Chapman int (*session_delete)(struct l2tp_session *session); 198309795f4SJames Chapman }; 199309795f4SJames Chapman 200fd558d18SJames Chapman static inline void *l2tp_tunnel_priv(struct l2tp_tunnel *tunnel) 201fd558d18SJames Chapman { 202fd558d18SJames Chapman return &tunnel->priv[0]; 203fd558d18SJames Chapman } 204fd558d18SJames Chapman 205fd558d18SJames Chapman static inline void *l2tp_session_priv(struct l2tp_session *session) 206fd558d18SJames Chapman { 207fd558d18SJames Chapman return &session->priv[0]; 208fd558d18SJames Chapman } 209fd558d18SJames Chapman 210fd558d18SJames Chapman static inline struct l2tp_tunnel *l2tp_sock_to_tunnel(struct sock *sk) 211fd558d18SJames Chapman { 212fd558d18SJames Chapman struct l2tp_tunnel *tunnel; 213fd558d18SJames Chapman 214fd558d18SJames Chapman if (sk == NULL) 215fd558d18SJames Chapman return NULL; 216fd558d18SJames Chapman 217fd558d18SJames Chapman sock_hold(sk); 218fd558d18SJames Chapman tunnel = (struct l2tp_tunnel *)(sk->sk_user_data); 219fd558d18SJames Chapman if (tunnel == NULL) { 220fd558d18SJames Chapman sock_put(sk); 221fd558d18SJames Chapman goto out; 222fd558d18SJames Chapman } 223fd558d18SJames Chapman 224fd558d18SJames Chapman BUG_ON(tunnel->magic != L2TP_TUNNEL_MAGIC); 225fd558d18SJames Chapman 226fd558d18SJames Chapman out: 227fd558d18SJames Chapman return tunnel; 228fd558d18SJames Chapman } 229fd558d18SJames Chapman 230f7faffa3SJames Chapman extern struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id); 231fd558d18SJames Chapman extern struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth); 232309795f4SJames Chapman extern struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname); 233fd558d18SJames Chapman extern struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id); 234fd558d18SJames Chapman extern struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth); 235fd558d18SJames Chapman 236fd558d18SJames Chapman extern int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg, struct l2tp_tunnel **tunnelp); 237309795f4SJames Chapman extern int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel); 238fd558d18SJames Chapman extern struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg); 239309795f4SJames Chapman extern int l2tp_session_delete(struct l2tp_session *session); 240fd558d18SJames Chapman extern void l2tp_session_free(struct l2tp_session *session); 241f7faffa3SJames Chapman extern void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb, unsigned char *ptr, unsigned char *optr, u16 hdrflags, int length, int (*payload_hook)(struct sk_buff *skb)); 242fd558d18SJames Chapman extern int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb); 243fd558d18SJames Chapman 244fd558d18SJames Chapman extern int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len); 245fd558d18SJames Chapman 246309795f4SJames Chapman extern int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops); 247309795f4SJames Chapman extern void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type); 248309795f4SJames Chapman 249fd558d18SJames Chapman /* Session reference counts. Incremented when code obtains a reference 250fd558d18SJames Chapman * to a session. 251fd558d18SJames Chapman */ 252fd558d18SJames Chapman static inline void l2tp_session_inc_refcount_1(struct l2tp_session *session) 253fd558d18SJames Chapman { 254fd558d18SJames Chapman atomic_inc(&session->ref_count); 255fd558d18SJames Chapman } 256fd558d18SJames Chapman 257fd558d18SJames Chapman static inline void l2tp_session_dec_refcount_1(struct l2tp_session *session) 258fd558d18SJames Chapman { 259fd558d18SJames Chapman if (atomic_dec_and_test(&session->ref_count)) 260fd558d18SJames Chapman l2tp_session_free(session); 261fd558d18SJames Chapman } 262fd558d18SJames Chapman 263fd558d18SJames Chapman #ifdef L2TP_REFCNT_DEBUG 264fd558d18SJames Chapman #define l2tp_session_inc_refcount(_s) do { \ 265fd558d18SJames Chapman printk(KERN_DEBUG "l2tp_session_inc_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_s)->name, atomic_read(&_s->ref_count)); \ 266fd558d18SJames Chapman l2tp_session_inc_refcount_1(_s); \ 267fd558d18SJames Chapman } while (0) 268fd558d18SJames Chapman #define l2tp_session_dec_refcount(_s) do { \ 269fd558d18SJames Chapman printk(KERN_DEBUG "l2tp_session_dec_refcount: %s:%d %s: cnt=%d\n", __func__, __LINE__, (_s)->name, atomic_read(&_s->ref_count)); \ 270fd558d18SJames Chapman l2tp_session_dec_refcount_1(_s); \ 271fd558d18SJames Chapman } while (0) 272fd558d18SJames Chapman #else 273fd558d18SJames Chapman #define l2tp_session_inc_refcount(s) l2tp_session_inc_refcount_1(s) 274fd558d18SJames Chapman #define l2tp_session_dec_refcount(s) l2tp_session_dec_refcount_1(s) 275fd558d18SJames Chapman #endif 276fd558d18SJames Chapman 277fd558d18SJames Chapman #endif /* _L2TP_CORE_H_ */ 278