/*
* Copyright (c) 2017 Internet Initiative Japan Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef NOTYET
/* TODO: support ALTQ for inner frame */
#ifdef ALTQ
ALTQ_SAVE_PAYLOAD(m, AF_ETHER);
#endif
#endif
memset(&ip6hdr, 0, sizeof(ip6hdr));
ip6hdr.ip6_src = sin6_src->sin6_addr;
ip6hdr.ip6_dst = sin6_dst->sin6_addr;
/* unlike IPv4, IP version must be filled by caller of ip6_output() */
ip6hdr.ip6_vfc = 0x60;
ip6hdr.ip6_nxt = IPPROTO_L2TP;
ip6hdr.ip6_hlim = ip6_l2tp_hlim;
/* outer IP payload length */
ip6hdr.ip6_plen = 0;
/* session-id length */
ip6hdr.ip6_plen += sizeof(uint32_t);
if (var->lv_use_cookie == L2TP_COOKIE_ON) {
/* cookie length */
ip6hdr.ip6_plen += var->lv_peer_cookie_len;
}
/* TODO: IP_TCPMSS support */
#ifdef IP_TCPMSS
m = l2tp_tcpmss_clamp(ifp, m);
if (m == NULL)
return EINVAL;
#endif
/*
* Payload length.
*
* NOTE: payload length may be changed in ip_tcpmss(). Typical case
* is missing of TCP mss option in original TCP header.
*/
ip6hdr.ip6_plen += m->m_pkthdr.len;
HTONS(ip6hdr.ip6_plen);
/*
* This function is used by encap6_lookup() to decide priority of the encaptab.
* This priority is compared to the match length between mbuf's source/destination
* IPv6 address pair and encaptab's one.
* l2tp(4) does not use address pairs to search matched encaptab, so this
* function must return the length bigger than or equals to IPv6 address pair to
* avoid wrong encaptab.
*/
static int
in6_l2tp_match(struct mbuf *m, int off, int proto, void *arg)
{
struct l2tp_softc *sc = arg;
struct l2tp_variant *var;
struct psref psref;
uint32_t sess_id;
int rv = 0;
KASSERT(proto == IPPROTO_L2TP);
var = l2tp_getref_variant(sc, &psref);
if (__predict_false(var == NULL))
return rv;
/*
* If the packet contains no session ID it cannot match
*/
if (m_length(m) < off + sizeof(uint32_t)) {
rv = 0 ;
goto out;
}
/* get L2TP session ID */
m_copydata(m, off, sizeof(uint32_t), (void *)&sess_id);
NTOHL(sess_id);
if (sess_id == 0) {
/*
* L2TPv3 control packet received.
* userland daemon(l2tpd?) should process.
*/
rv = 128 * 2;
} else if (sess_id == var->lv_my_sess_id)
rv = 128 * 2;
else
rv = 0;