<commit_before>/*
 * Copyright (c) 2019 Intel Corporation.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <logging/log.h>
LOG_MODULE_DECLARE(net_l2_ppp, CONFIG_NET_L2_PPP_LOG_LEVEL);

#include <net/net_core.h>
#include <net/net_l2.h>
#include <net/net_if.h>
#include <net/net_pkt.h>
#include <net/net_mgmt.h>

#include <net/ppp.h>

#include "net_private.h"

#include "ppp_stats.h"
#include "ppp_internal.h"

static enum net_verdict lcp_handle_ext(struct ppp_fsm *fsm,
				       enum ppp_packet_type code, u8_t id,
				       struct net_pkt *pkt)
{
	enum net_verdict verdict = NET_DROP;

	switch (code) {
	case PPP_PROTOCOL_REJ:
		NET_DBG("PPP Protocol-Rej");
		return ppp_fsm_recv_protocol_rej(fsm, id, pkt);

	case PPP_ECHO_REQ:
		NET_DBG("PPP Echo-Req");
		return ppp_fsm_recv_echo_req(fsm, id, pkt);

	case PPP_ECHO_REPLY:
		NET_DBG("PPP Echo-Reply");
		return ppp_fsm_recv_echo_reply(fsm, id, pkt);

	case PPP_DISCARD_REQ:
		NET_DBG("PPP Discard-Req");
		return ppp_fsm_recv_discard_req(fsm, id, pkt);

	default:
		break;
	}

	return verdict;
}

static enum net_verdict lcp_handle(struct ppp_context *ctx,
				   struct net_if *iface,
				   struct net_pkt *pkt)
{
	return ppp_fsm_input(&ctx->lcp.fsm, PPP_LCP, pkt);
}

static bool append_to_buf(struct net_buf *buf, u8_t *data, u8_t data_len)
{
	if (data_len > net_buf_tailroom(buf)) {
		return false;
	}

	/* FIXME: use net_pkt api so that we can handle a case where data
	 * might split to two net_buf's
	 */
	net_buf_add_mem(buf, data, data_len);

	return true;
}

static int lcp_config_info_req(struct ppp_fsm *fsm,
			       struct net_pkt *pkt,
			       u16_t length,
			       struct net_buf **buf)
{
	struct ppp_option_pkt options[MAX_LCP_OPTIONS];
	struct ppp_option_pkt nack_options[MAX_LCP_OPTIONS];
	struct net_buf *nack = NULL;
	enum ppp_packet_type code;
	enum net_verdict verdict;
	int i, nack_idx = 0;
	int count_rej = 0, count_nack = 0;

	memset(options, 0, sizeof(options));
	memset(nack_options, 0, sizeof(nack_options));

	verdict = ppp_parse_options(fsm, pkt, length, options,
				    ARRAY_SIZE(options));
	if (verdict != NET_OK) {
		return -EINVAL;
	}

	for (i = 0; i < ARRAY_SIZE(options); i++) {
		if (options[i].type.lcp != LCP_OPTION_RESERVED) {
			NET_DBG("[%s/%p] %s option %s (%d) len %d",
				fsm->name, fsm, "Check",
				ppp_option2str(PPP_LCP, options[i].type.lcp),
				options[i].type.lcp, options[i].len);
		}

		switch (options[i].type.lcp) {
		case LCP_OPTION_RESERVED:
			continue;

		case LCP_OPTION_MRU:
			break;

		/* TODO: Check from ctx->lcp.my_options what options to accept
		 */
		case LCP_OPTION_ASYNC_CTRL_CHAR_MAP:
			count_nack++;
			goto ignore_option;

		case LCP_OPTION_AUTH_PROTO:
			count_nack++;
			goto ignore_option;

		case LCP_OPTION_QUALITY_PROTO:
			count_rej++;
			goto ignore_option;

		case LCP_OPTION_MAGIC_NUMBER:
			count_nack++;
			goto ignore_option;

		case LCP_OPTION_PROTO_COMPRESS:
			count_rej++;
			goto ignore_option;

		case LCP_OPTION_ADDR_CTRL_COMPRESS:
			count_rej++;
			goto ignore_option;

		default:
		ignore_option:
			nack_options[nack_idx].type.lcp = options[i].type.lcp;
			nack_options[nack_idx].len = options[i].len;

			if (options[i].len > 2) {
				memcpy(&nack_options[nack_idx].value,
				       &options[i].value,
				       sizeof(nack_options[nack_idx].value));
			}

			nack_idx++;
			break;
		}<commit_msg>net: ppp: lcp: reject all not recognized and not acceptable options<commit_after>/*
 * Copyright (c) 2019 Intel Corporation.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <logging/log.h>
LOG_MODULE_DECLARE(net_l2_ppp, CONFIG_NET_L2_PPP_LOG_LEVEL);

#include <net/net_core.h>
#include <net/net_l2.h>
#include <net/net_if.h>
#include <net/net_pkt.h>
#include <net/net_mgmt.h>

#include <net/ppp.h>

#include "net_private.h"

#include "ppp_stats.h"
#include "ppp_internal.h"

static enum net_verdict lcp_handle_ext(struct ppp_fsm *fsm,
				       enum ppp_packet_type code, u8_t id,
				       struct net_pkt *pkt)
{
	enum net_verdict verdict = NET_DROP;

	switch (code) {
	case PPP_PROTOCOL_REJ:
		NET_DBG("PPP Protocol-Rej");
		return ppp_fsm_recv_protocol_rej(fsm, id, pkt);

	case PPP_ECHO_REQ:
		NET_DBG("PPP Echo-Req");
		return ppp_fsm_recv_echo_req(fsm, id, pkt);

	case PPP_ECHO_REPLY:
		NET_DBG("PPP Echo-Reply");
		return ppp_fsm_recv_echo_reply(fsm, id, pkt);

	case PPP_DISCARD_REQ:
		NET_DBG("PPP Discard-Req");
		return ppp_fsm_recv_discard_req(fsm, id, pkt);

	default:
		break;
	}

	return verdict;
}

static enum net_verdict lcp_handle(struct ppp_context *ctx,
				   struct net_if *iface,
				   struct net_pkt *pkt)
{
	return ppp_fsm_input(&ctx->lcp.fsm, PPP_LCP, pkt);
}

static bool append_to_buf(struct net_buf *buf, u8_t *data, u8_t data_len)
{
	if (data_len > net_buf_tailroom(buf)) {
		return false;
	}

	/* FIXME: use net_pkt api so that we can handle a case where data
	 * might split to two net_buf's
	 */
	net_buf_add_mem(buf, data, data_len);

	return true;
}

static int lcp_config_info_req(struct ppp_fsm *fsm,
			       struct net_pkt *pkt,
			       u16_t length,
			       struct net_buf **buf)
{
	struct ppp_option_pkt options[MAX_LCP_OPTIONS];
	struct ppp_option_pkt nack_options[MAX_LCP_OPTIONS];
	struct net_buf *nack = NULL;
	enum ppp_packet_type code;
	enum net_verdict verdict;
	int i, nack_idx = 0;
	int count_rej = 0, count_nack = 0;

	memset(options, 0, sizeof(options));
	memset(nack_options, 0, sizeof(nack_options));

	verdict = ppp_parse_options(fsm, pkt, length, options,
				    ARRAY_SIZE(options));
	if (verdict != NET_OK) {
		return -EINVAL;
	}

	for (i = 0; i < ARRAY_SIZE(options); i++) {
		if (options[i].type.lcp != LCP_OPTION_RESERVED) {
			NET_DBG("[%s/%p] %s option %s (%d) len %d",
				fsm->name, fsm, "Check",
				ppp_option2str(PPP_LCP, options[i].type.lcp),
				options[i].type.lcp, options[i].len);
		}

		switch (options[i].type.lcp) {
		case LCP_OPTION_RESERVED:
			continue;

		case LCP_OPTION_MRU:
			break;

		/* TODO: Check from ctx->lcp.my_options what options to accept
		 */
		case LCP_OPTION_ASYNC_CTRL_CHAR_MAP:
			count_nack++;
			goto ignore_option;

		case LCP_OPTION_MAGIC_NUMBER:
			count_nack++;
			goto ignore_option;

		default:
			count_rej++;
		ignore_option:
			nack_options[nack_idx].type.lcp = options[i].type.lcp;
			nack_options[nack_idx].len = options[i].len;

			if (options[i].len > 2) {
				memcpy(&nack_options[nack_idx].value,
				       &options[i].value,
				       sizeof(nack_options[nack_idx].value));
			}

			nack_idx++;
			break;
		}
{'fraction_non_alphanumeric': 0.12147239263803682, 'fraction_numerical': 0.006748466257668712, 'mean_word_length': 3.98928844682479, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '886995', 'n_tokens_mistral': 2959, 'n_tokens_neox': 2708, 'n_words': 545}