Home | History | Annotate | Line # | Download | only in net
if_l2tp.h revision 1.7
      1 /*	$NetBSD: if_l2tp.h,v 1.7 2019/09/19 04:59:42 knakahara Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2017 Internet Initiative Japan Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * L2TPv3 kernel interface
     31  */
     32 
     33 #ifndef _NET_IF_L2TP_H_
     34 #define _NET_IF_L2TP_H_
     35 
     36 #include <sys/queue.h>
     37 #include <sys/ioccom.h>
     38 #ifdef _KERNEL
     39 #include <sys/pserialize.h>
     40 #include <sys/psref.h>
     41 #include <sys/pslist.h>
     42 #endif
     43 
     44 #include <net/if_ether.h>
     45 #include <netinet/in.h>
     46 
     47 #define	SIOCSL2TPSESSION	_IOW('i', 151, struct l2tp_req)
     48 #define	SIOCDL2TPSESSION	_IOW('i', 152, struct l2tp_req)
     49 #define	SIOCSL2TPCOOKIE		_IOW('i', 153, struct l2tp_req)
     50 #define	SIOCDL2TPCOOKIE		_IOW('i', 154, struct l2tp_req)
     51 #define	SIOCSL2TPSTATE		_IOW('i', 155, struct l2tp_req)
     52 #define	SIOCGL2TP		SIOCGIFGENERIC
     53 
     54 struct l2tp_req {
     55 	int state;
     56 	u_int my_cookie_len;
     57 	u_int peer_cookie_len;
     58 	uint32_t my_sess_id;
     59 	uint32_t peer_sess_id;
     60 	uint64_t my_cookie;
     61 	uint64_t peer_cookie;
     62 };
     63 
     64 #define	L2TP_STATE_UP	1
     65 #define	L2TP_STATE_DOWN	0
     66 
     67 #define	L2TP_COOKIE_ON	1
     68 #define	L2TP_COOKIE_OFF	0
     69 
     70 #ifdef _KERNEL
     71 extern struct psref_class *lv_psref_class;
     72 
     73 struct l2tp_variant {
     74 	struct l2tp_softc *lv_softc;
     75 
     76 	struct sockaddr	*lv_psrc; /* Physical src addr */
     77 	struct sockaddr	*lv_pdst; /* Physical dst addr */
     78 	const struct encaptab *lv_encap_cookie;
     79 
     80 	/* L2TP session info */
     81 	int lv_state;
     82 	uint32_t lv_my_sess_id;		/* my session ID */
     83 	uint32_t lv_peer_sess_id;	/* peer session ID */
     84 
     85 	int lv_use_cookie;
     86 	u_int lv_my_cookie_len;
     87 	u_int lv_peer_cookie_len;
     88 	uint64_t lv_my_cookie;		/* my cookie */
     89 	uint64_t lv_peer_cookie;	/* peer cookie */
     90 
     91 	struct psref_target lv_psref;
     92 };
     93 
     94 struct l2tp_ro {
     95 	struct route lr_ro;
     96 	kmutex_t *lr_lock;
     97 };
     98 
     99 struct l2tp_softc {
    100 	struct ethercom	l2tp_ec;	/* common area - must be at the top */
    101 					/* to use ether_input(), we must have this */
    102 	percpu_t *l2tp_ro_percpu;	/* struct l2tp_ro */
    103 	struct l2tp_variant *l2tp_var;	/*
    104 					* reader must use l2tp_getref_variant()
    105 					* instead of direct dereference.
    106 					*/
    107 	kmutex_t l2tp_lock;		/* writer lock for l2tp_var */
    108 	pserialize_t l2tp_psz;
    109 
    110 	void *l2tp_si;
    111 	percpu_t *l2tp_ifq_percpu;
    112 
    113 	LIST_ENTRY(l2tp_softc) l2tp_list; /* list of all l2tps */
    114 	struct pslist_entry l2tp_hash;	/* hashed list to lookup by session id */
    115 };
    116 
    117 #define	L2TP_ROUTE_TTL	10
    118 
    119 #define	L2TP_MTU	(1280)	/* Default MTU */
    120 #define	L2TP_MTU_MIN	(1280)	/* Minimum MTU */
    121 #define	L2TP_MTU_MAX	(8192)	/* Maximum MTU */
    122 
    123 /*
    124  * Get l2tp_variant from l2tp_softc.
    125  *
    126  * l2tp_variant itself is protected not to be freed by lv_psref.
    127  * In contrast, sc->sc_var can be changed to NULL even if reader critical
    128  * section. see l2tp_variant_update().
    129  * So, once a reader dereference sc->sc_var by this API, the reader must not
    130  * re-dereference form sc->sc_var.
    131  */
    132 static __inline struct l2tp_variant *
    133 l2tp_getref_variant(struct l2tp_softc *sc, struct psref *psref)
    134 {
    135 	struct l2tp_variant *var;
    136 	int s;
    137 
    138 	s = pserialize_read_enter();
    139 	var = sc->l2tp_var;
    140 	if (var == NULL) {
    141 		pserialize_read_exit(s);
    142 		return NULL;
    143 	}
    144 	membar_datadep_consumer();
    145 	psref_acquire(psref, &var->lv_psref, lv_psref_class);
    146 	pserialize_read_exit(s);
    147 
    148 	return var;
    149 }
    150 
    151 static __inline void
    152 l2tp_putref_variant(struct l2tp_variant *var, struct psref *psref)
    153 {
    154 
    155 	if (var == NULL)
    156 		return;
    157 	psref_release(psref, &var->lv_psref, lv_psref_class);
    158 }
    159 
    160 static __inline bool
    161 l2tp_heldref_variant(struct l2tp_variant *var)
    162 {
    163 
    164 	return psref_held(&var->lv_psref, lv_psref_class);
    165 }
    166 
    167 
    168 /* Prototypes */
    169 void l2tpattach(int);
    170 int l2tpattach0(struct l2tp_softc *);
    171 void l2tp_input(struct mbuf *, struct ifnet *);
    172 int l2tp_ioctl(struct ifnet *, u_long, void *);
    173 
    174 struct l2tp_variant *l2tp_lookup_session_ref(uint32_t, struct psref *);
    175 int l2tp_check_nesting(struct ifnet *, struct mbuf *);
    176 
    177 /* TODO IP_TCPMSS support */
    178 #ifdef IP_TCPMSS
    179 struct mbuf *l2tp_tcpmss_clamp(struct ifnet *, struct mbuf *);
    180 #endif /* IP_TCPMSS */
    181 #endif /* _KERNEL */
    182 
    183 /*
    184  * Locking notes:
    185  * + l2tp_softc_list is protected by l2tp_list_lock (an adaptive mutex)
    186  *       l2tp_softc_list is list of all l2tp_softcs, and it is used to avoid
    187  *       unload while busy.
    188  * + l2tp_hashed_list is protected by
    189  *   - l2tp_hash_lock (an adaptive mutex) for writer
    190  *   - pserialize for reader
    191  *       l2tp_hashed_list is hashed list of all l2tp_softcs, and it is used by
    192  *       input processing to find appropriate softc.
    193  * + l2tp_softc->l2tp_var is protected by
    194  *   - l2tp_softc->l2tp_lock (an adaptive mutex) for writer
    195  *   - l2tp_var->lv_psref for reader
    196  *       l2tp_softc->l2tp_var is used for variant values while the l2tp tunnel
    197  *       exists.
    198  * + struct l2tp_ro->lr_ro is protected by struct l2tp_ro->lr_lock.
    199  *       This lock is required to exclude softnet/0 lwp(such as output
    200  *       processing softint) and  processing lwp(such as DAD timer processing).
    201  *
    202  * Locking order:
    203  *     - encap_lock => struct l2tp_softc->l2tp_lock
    204  * Other mutexes must not hold simultaneously.
    205  *
    206  *   NOTICE
    207  *   - l2tp_softc must not have a variant value while the l2tp tunnel exists.
    208  *     Such variant values must be in l2tp_softc->l2tp_var.
    209  *   - l2tp_softc->l2tp_var is modified like read-copy-update.
    210  *     So, once we dereference l2tp_softc->l2tp_var, we must
    211  *     keep the pointer during the same context. If we re-derefence
    212  *     l2tp_softc->l2tp_var, the l2tp_var may be other one because of
    213  *     concurrent writer processing.
    214  */
    215 #endif /* _NET_IF_L2TP_H_ */
    216