18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Generic HDLC support routines for Linux
48c2ecf20Sopenharmony_ci * Point-to-point protocol support
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 1999 - 2008 Krzysztof Halasa <khc@pm.waw.pl>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/errno.h>
108c2ecf20Sopenharmony_ci#include <linux/hdlc.h>
118c2ecf20Sopenharmony_ci#include <linux/if_arp.h>
128c2ecf20Sopenharmony_ci#include <linux/inetdevice.h>
138c2ecf20Sopenharmony_ci#include <linux/init.h>
148c2ecf20Sopenharmony_ci#include <linux/kernel.h>
158c2ecf20Sopenharmony_ci#include <linux/module.h>
168c2ecf20Sopenharmony_ci#include <linux/pkt_sched.h>
178c2ecf20Sopenharmony_ci#include <linux/poll.h>
188c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
198c2ecf20Sopenharmony_ci#include <linux/slab.h>
208c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define DEBUG_CP		0 /* also bytes# to dump */
238c2ecf20Sopenharmony_ci#define DEBUG_STATE		0
248c2ecf20Sopenharmony_ci#define DEBUG_HARD_HEADER	0
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define HDLC_ADDR_ALLSTATIONS	0xFF
278c2ecf20Sopenharmony_ci#define HDLC_CTRL_UI		0x03
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define PID_LCP			0xC021
308c2ecf20Sopenharmony_ci#define PID_IP			0x0021
318c2ecf20Sopenharmony_ci#define PID_IPCP		0x8021
328c2ecf20Sopenharmony_ci#define PID_IPV6		0x0057
338c2ecf20Sopenharmony_ci#define PID_IPV6CP		0x8057
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cienum {IDX_LCP = 0, IDX_IPCP, IDX_IPV6CP, IDX_COUNT};
368c2ecf20Sopenharmony_cienum {CP_CONF_REQ = 1, CP_CONF_ACK, CP_CONF_NAK, CP_CONF_REJ, CP_TERM_REQ,
378c2ecf20Sopenharmony_ci      CP_TERM_ACK, CP_CODE_REJ, LCP_PROTO_REJ, LCP_ECHO_REQ, LCP_ECHO_REPLY,
388c2ecf20Sopenharmony_ci      LCP_DISC_REQ, CP_CODES};
398c2ecf20Sopenharmony_ci#if DEBUG_CP
408c2ecf20Sopenharmony_cistatic const char *const code_names[CP_CODES] = {
418c2ecf20Sopenharmony_ci	"0", "ConfReq", "ConfAck", "ConfNak", "ConfRej", "TermReq",
428c2ecf20Sopenharmony_ci	"TermAck", "CodeRej", "ProtoRej", "EchoReq", "EchoReply", "Discard"
438c2ecf20Sopenharmony_ci};
448c2ecf20Sopenharmony_cistatic char debug_buffer[64 + 3 * DEBUG_CP];
458c2ecf20Sopenharmony_ci#endif
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cienum {LCP_OPTION_MRU = 1, LCP_OPTION_ACCM, LCP_OPTION_MAGIC = 5};
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistruct hdlc_header {
508c2ecf20Sopenharmony_ci	u8 address;
518c2ecf20Sopenharmony_ci	u8 control;
528c2ecf20Sopenharmony_ci	__be16 protocol;
538c2ecf20Sopenharmony_ci};
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistruct cp_header {
568c2ecf20Sopenharmony_ci	u8 code;
578c2ecf20Sopenharmony_ci	u8 id;
588c2ecf20Sopenharmony_ci	__be16 len;
598c2ecf20Sopenharmony_ci};
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistruct proto {
638c2ecf20Sopenharmony_ci	struct net_device *dev;
648c2ecf20Sopenharmony_ci	struct timer_list timer;
658c2ecf20Sopenharmony_ci	unsigned long timeout;
668c2ecf20Sopenharmony_ci	u16 pid;		/* protocol ID */
678c2ecf20Sopenharmony_ci	u8 state;
688c2ecf20Sopenharmony_ci	u8 cr_id;		/* ID of last Configuration-Request */
698c2ecf20Sopenharmony_ci	u8 restart_counter;
708c2ecf20Sopenharmony_ci};
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistruct ppp {
738c2ecf20Sopenharmony_ci	struct proto protos[IDX_COUNT];
748c2ecf20Sopenharmony_ci	spinlock_t lock;
758c2ecf20Sopenharmony_ci	unsigned long last_pong;
768c2ecf20Sopenharmony_ci	unsigned int req_timeout, cr_retries, term_retries;
778c2ecf20Sopenharmony_ci	unsigned int keepalive_interval, keepalive_timeout;
788c2ecf20Sopenharmony_ci	u8 seq;			/* local sequence number for requests */
798c2ecf20Sopenharmony_ci	u8 echo_id;		/* ID of last Echo-Request (LCP) */
808c2ecf20Sopenharmony_ci};
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cienum {CLOSED = 0, STOPPED, STOPPING, REQ_SENT, ACK_RECV, ACK_SENT, OPENED,
838c2ecf20Sopenharmony_ci      STATES, STATE_MASK = 0xF};
848c2ecf20Sopenharmony_cienum {START = 0, STOP, TO_GOOD, TO_BAD, RCR_GOOD, RCR_BAD, RCA, RCN, RTR, RTA,
858c2ecf20Sopenharmony_ci      RUC, RXJ_GOOD, RXJ_BAD, EVENTS};
868c2ecf20Sopenharmony_cienum {INV = 0x10, IRC = 0x20, ZRC = 0x40, SCR = 0x80, SCA = 0x100,
878c2ecf20Sopenharmony_ci      SCN = 0x200, STR = 0x400, STA = 0x800, SCJ = 0x1000};
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci#if DEBUG_STATE
908c2ecf20Sopenharmony_cistatic const char *const state_names[STATES] = {
918c2ecf20Sopenharmony_ci	"Closed", "Stopped", "Stopping", "ReqSent", "AckRecv", "AckSent",
928c2ecf20Sopenharmony_ci	"Opened"
938c2ecf20Sopenharmony_ci};
948c2ecf20Sopenharmony_cistatic const char *const event_names[EVENTS] = {
958c2ecf20Sopenharmony_ci	"Start", "Stop", "TO+", "TO-", "RCR+", "RCR-", "RCA", "RCN",
968c2ecf20Sopenharmony_ci	"RTR", "RTA", "RUC", "RXJ+", "RXJ-"
978c2ecf20Sopenharmony_ci};
988c2ecf20Sopenharmony_ci#endif
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic struct sk_buff_head tx_queue; /* used when holding the spin lock */
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic int ppp_ioctl(struct net_device *dev, struct ifreq *ifr);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic inline struct ppp* get_ppp(struct net_device *dev)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	return (struct ppp *)dev_to_hdlc(dev)->state;
1078c2ecf20Sopenharmony_ci}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_cistatic inline struct proto* get_proto(struct net_device *dev, u16 pid)
1108c2ecf20Sopenharmony_ci{
1118c2ecf20Sopenharmony_ci	struct ppp *ppp = get_ppp(dev);
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	switch (pid) {
1148c2ecf20Sopenharmony_ci	case PID_LCP:
1158c2ecf20Sopenharmony_ci		return &ppp->protos[IDX_LCP];
1168c2ecf20Sopenharmony_ci	case PID_IPCP:
1178c2ecf20Sopenharmony_ci		return &ppp->protos[IDX_IPCP];
1188c2ecf20Sopenharmony_ci	case PID_IPV6CP:
1198c2ecf20Sopenharmony_ci		return &ppp->protos[IDX_IPV6CP];
1208c2ecf20Sopenharmony_ci	default:
1218c2ecf20Sopenharmony_ci		return NULL;
1228c2ecf20Sopenharmony_ci	}
1238c2ecf20Sopenharmony_ci}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistatic inline const char* proto_name(u16 pid)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	switch (pid) {
1288c2ecf20Sopenharmony_ci	case PID_LCP:
1298c2ecf20Sopenharmony_ci		return "LCP";
1308c2ecf20Sopenharmony_ci	case PID_IPCP:
1318c2ecf20Sopenharmony_ci		return "IPCP";
1328c2ecf20Sopenharmony_ci	case PID_IPV6CP:
1338c2ecf20Sopenharmony_ci		return "IPV6CP";
1348c2ecf20Sopenharmony_ci	default:
1358c2ecf20Sopenharmony_ci		return NULL;
1368c2ecf20Sopenharmony_ci	}
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic __be16 ppp_type_trans(struct sk_buff *skb, struct net_device *dev)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	struct hdlc_header *data = (struct hdlc_header*)skb->data;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	if (skb->len < sizeof(struct hdlc_header))
1448c2ecf20Sopenharmony_ci		return htons(ETH_P_HDLC);
1458c2ecf20Sopenharmony_ci	if (data->address != HDLC_ADDR_ALLSTATIONS ||
1468c2ecf20Sopenharmony_ci	    data->control != HDLC_CTRL_UI)
1478c2ecf20Sopenharmony_ci		return htons(ETH_P_HDLC);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	switch (data->protocol) {
1508c2ecf20Sopenharmony_ci	case cpu_to_be16(PID_IP):
1518c2ecf20Sopenharmony_ci		skb_pull(skb, sizeof(struct hdlc_header));
1528c2ecf20Sopenharmony_ci		return htons(ETH_P_IP);
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	case cpu_to_be16(PID_IPV6):
1558c2ecf20Sopenharmony_ci		skb_pull(skb, sizeof(struct hdlc_header));
1568c2ecf20Sopenharmony_ci		return htons(ETH_P_IPV6);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	default:
1598c2ecf20Sopenharmony_ci		return htons(ETH_P_HDLC);
1608c2ecf20Sopenharmony_ci	}
1618c2ecf20Sopenharmony_ci}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic int ppp_hard_header(struct sk_buff *skb, struct net_device *dev,
1658c2ecf20Sopenharmony_ci			   u16 type, const void *daddr, const void *saddr,
1668c2ecf20Sopenharmony_ci			   unsigned int len)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	struct hdlc_header *data;
1698c2ecf20Sopenharmony_ci#if DEBUG_HARD_HEADER
1708c2ecf20Sopenharmony_ci	printk(KERN_DEBUG "%s: ppp_hard_header() called\n", dev->name);
1718c2ecf20Sopenharmony_ci#endif
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	skb_push(skb, sizeof(struct hdlc_header));
1748c2ecf20Sopenharmony_ci	data = (struct hdlc_header*)skb->data;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	data->address = HDLC_ADDR_ALLSTATIONS;
1778c2ecf20Sopenharmony_ci	data->control = HDLC_CTRL_UI;
1788c2ecf20Sopenharmony_ci	switch (type) {
1798c2ecf20Sopenharmony_ci	case ETH_P_IP:
1808c2ecf20Sopenharmony_ci		data->protocol = htons(PID_IP);
1818c2ecf20Sopenharmony_ci		break;
1828c2ecf20Sopenharmony_ci	case ETH_P_IPV6:
1838c2ecf20Sopenharmony_ci		data->protocol = htons(PID_IPV6);
1848c2ecf20Sopenharmony_ci		break;
1858c2ecf20Sopenharmony_ci	case PID_LCP:
1868c2ecf20Sopenharmony_ci	case PID_IPCP:
1878c2ecf20Sopenharmony_ci	case PID_IPV6CP:
1888c2ecf20Sopenharmony_ci		data->protocol = htons(type);
1898c2ecf20Sopenharmony_ci		break;
1908c2ecf20Sopenharmony_ci	default:		/* unknown protocol */
1918c2ecf20Sopenharmony_ci		data->protocol = 0;
1928c2ecf20Sopenharmony_ci	}
1938c2ecf20Sopenharmony_ci	return sizeof(struct hdlc_header);
1948c2ecf20Sopenharmony_ci}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_cistatic void ppp_tx_flush(void)
1988c2ecf20Sopenharmony_ci{
1998c2ecf20Sopenharmony_ci	struct sk_buff *skb;
2008c2ecf20Sopenharmony_ci	while ((skb = skb_dequeue(&tx_queue)) != NULL)
2018c2ecf20Sopenharmony_ci		dev_queue_xmit(skb);
2028c2ecf20Sopenharmony_ci}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_cistatic void ppp_tx_cp(struct net_device *dev, u16 pid, u8 code,
2058c2ecf20Sopenharmony_ci		      u8 id, unsigned int len, const void *data)
2068c2ecf20Sopenharmony_ci{
2078c2ecf20Sopenharmony_ci	struct sk_buff *skb;
2088c2ecf20Sopenharmony_ci	struct cp_header *cp;
2098c2ecf20Sopenharmony_ci	unsigned int magic_len = 0;
2108c2ecf20Sopenharmony_ci	static u32 magic;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci#if DEBUG_CP
2138c2ecf20Sopenharmony_ci	int i;
2148c2ecf20Sopenharmony_ci	char *ptr;
2158c2ecf20Sopenharmony_ci#endif
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	if (pid == PID_LCP && (code == LCP_ECHO_REQ || code == LCP_ECHO_REPLY))
2188c2ecf20Sopenharmony_ci		magic_len = sizeof(magic);
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	skb = dev_alloc_skb(sizeof(struct hdlc_header) +
2218c2ecf20Sopenharmony_ci			    sizeof(struct cp_header) + magic_len + len);
2228c2ecf20Sopenharmony_ci	if (!skb) {
2238c2ecf20Sopenharmony_ci		netdev_warn(dev, "out of memory in ppp_tx_cp()\n");
2248c2ecf20Sopenharmony_ci		return;
2258c2ecf20Sopenharmony_ci	}
2268c2ecf20Sopenharmony_ci	skb_reserve(skb, sizeof(struct hdlc_header));
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	cp = skb_put(skb, sizeof(struct cp_header));
2298c2ecf20Sopenharmony_ci	cp->code = code;
2308c2ecf20Sopenharmony_ci	cp->id = id;
2318c2ecf20Sopenharmony_ci	cp->len = htons(sizeof(struct cp_header) + magic_len + len);
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	if (magic_len)
2348c2ecf20Sopenharmony_ci		skb_put_data(skb, &magic, magic_len);
2358c2ecf20Sopenharmony_ci	if (len)
2368c2ecf20Sopenharmony_ci		skb_put_data(skb, data, len);
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci#if DEBUG_CP
2398c2ecf20Sopenharmony_ci	BUG_ON(code >= CP_CODES);
2408c2ecf20Sopenharmony_ci	ptr = debug_buffer;
2418c2ecf20Sopenharmony_ci	*ptr = '\x0';
2428c2ecf20Sopenharmony_ci	for (i = 0; i < min_t(unsigned int, magic_len + len, DEBUG_CP); i++) {
2438c2ecf20Sopenharmony_ci		sprintf(ptr, " %02X", skb->data[sizeof(struct cp_header) + i]);
2448c2ecf20Sopenharmony_ci		ptr += strlen(ptr);
2458c2ecf20Sopenharmony_ci	}
2468c2ecf20Sopenharmony_ci	printk(KERN_DEBUG "%s: TX %s [%s id 0x%X]%s\n", dev->name,
2478c2ecf20Sopenharmony_ci	       proto_name(pid), code_names[code], id, debug_buffer);
2488c2ecf20Sopenharmony_ci#endif
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	ppp_hard_header(skb, dev, pid, NULL, NULL, 0);
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	skb->priority = TC_PRIO_CONTROL;
2538c2ecf20Sopenharmony_ci	skb->dev = dev;
2548c2ecf20Sopenharmony_ci	skb->protocol = htons(ETH_P_HDLC);
2558c2ecf20Sopenharmony_ci	skb_reset_network_header(skb);
2568c2ecf20Sopenharmony_ci	skb_queue_tail(&tx_queue, skb);
2578c2ecf20Sopenharmony_ci}
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci/* State transition table (compare STD-51)
2618c2ecf20Sopenharmony_ci   Events                                   Actions
2628c2ecf20Sopenharmony_ci   TO+  = Timeout with counter > 0          irc = Initialize-Restart-Count
2638c2ecf20Sopenharmony_ci   TO-  = Timeout with counter expired      zrc = Zero-Restart-Count
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci   RCR+ = Receive-Configure-Request (Good)  scr = Send-Configure-Request
2668c2ecf20Sopenharmony_ci   RCR- = Receive-Configure-Request (Bad)
2678c2ecf20Sopenharmony_ci   RCA  = Receive-Configure-Ack             sca = Send-Configure-Ack
2688c2ecf20Sopenharmony_ci   RCN  = Receive-Configure-Nak/Rej         scn = Send-Configure-Nak/Rej
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci   RTR  = Receive-Terminate-Request         str = Send-Terminate-Request
2718c2ecf20Sopenharmony_ci   RTA  = Receive-Terminate-Ack             sta = Send-Terminate-Ack
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci   RUC  = Receive-Unknown-Code              scj = Send-Code-Reject
2748c2ecf20Sopenharmony_ci   RXJ+ = Receive-Code-Reject (permitted)
2758c2ecf20Sopenharmony_ci       or Receive-Protocol-Reject
2768c2ecf20Sopenharmony_ci   RXJ- = Receive-Code-Reject (catastrophic)
2778c2ecf20Sopenharmony_ci       or Receive-Protocol-Reject
2788c2ecf20Sopenharmony_ci*/
2798c2ecf20Sopenharmony_cistatic int cp_table[EVENTS][STATES] = {
2808c2ecf20Sopenharmony_ci	/* CLOSED     STOPPED STOPPING REQ_SENT ACK_RECV ACK_SENT OPENED
2818c2ecf20Sopenharmony_ci	     0           1         2       3       4      5          6    */
2828c2ecf20Sopenharmony_ci	{IRC|SCR|3,     INV     , INV ,   INV   , INV ,  INV    ,   INV   }, /* START */
2838c2ecf20Sopenharmony_ci	{   INV   ,      0      ,  0  ,    0    ,  0  ,   0     ,    0    }, /* STOP */
2848c2ecf20Sopenharmony_ci	{   INV   ,     INV     ,STR|2,  SCR|3  ,SCR|3,  SCR|5  ,   INV   }, /* TO+ */
2858c2ecf20Sopenharmony_ci	{   INV   ,     INV     ,  1  ,    1    ,  1  ,    1    ,   INV   }, /* TO- */
2868c2ecf20Sopenharmony_ci	{  STA|0  ,IRC|SCR|SCA|5,  2  ,  SCA|5  ,SCA|6,  SCA|5  ,SCR|SCA|5}, /* RCR+ */
2878c2ecf20Sopenharmony_ci	{  STA|0  ,IRC|SCR|SCN|3,  2  ,  SCN|3  ,SCN|4,  SCN|3  ,SCR|SCN|3}, /* RCR- */
2888c2ecf20Sopenharmony_ci	{  STA|0  ,    STA|1    ,  2  ,  IRC|4  ,SCR|3,    6    , SCR|3   }, /* RCA */
2898c2ecf20Sopenharmony_ci	{  STA|0  ,    STA|1    ,  2  ,IRC|SCR|3,SCR|3,IRC|SCR|5, SCR|3   }, /* RCN */
2908c2ecf20Sopenharmony_ci	{  STA|0  ,    STA|1    ,STA|2,  STA|3  ,STA|3,  STA|3  ,ZRC|STA|2}, /* RTR */
2918c2ecf20Sopenharmony_ci	{    0    ,      1      ,  1  ,    3    ,  3  ,    5    ,  SCR|3  }, /* RTA */
2928c2ecf20Sopenharmony_ci	{  SCJ|0  ,    SCJ|1    ,SCJ|2,  SCJ|3  ,SCJ|4,  SCJ|5  ,  SCJ|6  }, /* RUC */
2938c2ecf20Sopenharmony_ci	{    0    ,      1      ,  2  ,    3    ,  3  ,    5    ,    6    }, /* RXJ+ */
2948c2ecf20Sopenharmony_ci	{    0    ,      1      ,  1  ,    1    ,  1  ,    1    ,IRC|STR|2}, /* RXJ- */
2958c2ecf20Sopenharmony_ci};
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci/* SCA: RCR+ must supply id, len and data
2998c2ecf20Sopenharmony_ci   SCN: RCR- must supply code, id, len and data
3008c2ecf20Sopenharmony_ci   STA: RTR must supply id
3018c2ecf20Sopenharmony_ci   SCJ: RUC must supply CP packet len and data */
3028c2ecf20Sopenharmony_cistatic void ppp_cp_event(struct net_device *dev, u16 pid, u16 event, u8 code,
3038c2ecf20Sopenharmony_ci			 u8 id, unsigned int len, const void *data)
3048c2ecf20Sopenharmony_ci{
3058c2ecf20Sopenharmony_ci	int old_state, action;
3068c2ecf20Sopenharmony_ci	struct ppp *ppp = get_ppp(dev);
3078c2ecf20Sopenharmony_ci	struct proto *proto = get_proto(dev, pid);
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	old_state = proto->state;
3108c2ecf20Sopenharmony_ci	BUG_ON(old_state >= STATES);
3118c2ecf20Sopenharmony_ci	BUG_ON(event >= EVENTS);
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci#if DEBUG_STATE
3148c2ecf20Sopenharmony_ci	printk(KERN_DEBUG "%s: %s ppp_cp_event(%s) %s ...\n", dev->name,
3158c2ecf20Sopenharmony_ci	       proto_name(pid), event_names[event], state_names[proto->state]);
3168c2ecf20Sopenharmony_ci#endif
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	action = cp_table[event][old_state];
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	proto->state = action & STATE_MASK;
3218c2ecf20Sopenharmony_ci	if (action & (SCR | STR)) /* set Configure-Req/Terminate-Req timer */
3228c2ecf20Sopenharmony_ci		mod_timer(&proto->timer, proto->timeout =
3238c2ecf20Sopenharmony_ci			  jiffies + ppp->req_timeout * HZ);
3248c2ecf20Sopenharmony_ci	if (action & ZRC)
3258c2ecf20Sopenharmony_ci		proto->restart_counter = 0;
3268c2ecf20Sopenharmony_ci	if (action & IRC)
3278c2ecf20Sopenharmony_ci		proto->restart_counter = (proto->state == STOPPING) ?
3288c2ecf20Sopenharmony_ci			ppp->term_retries : ppp->cr_retries;
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	if (action & SCR)	/* send Configure-Request */
3318c2ecf20Sopenharmony_ci		ppp_tx_cp(dev, pid, CP_CONF_REQ, proto->cr_id = ++ppp->seq,
3328c2ecf20Sopenharmony_ci			  0, NULL);
3338c2ecf20Sopenharmony_ci	if (action & SCA)	/* send Configure-Ack */
3348c2ecf20Sopenharmony_ci		ppp_tx_cp(dev, pid, CP_CONF_ACK, id, len, data);
3358c2ecf20Sopenharmony_ci	if (action & SCN)	/* send Configure-Nak/Reject */
3368c2ecf20Sopenharmony_ci		ppp_tx_cp(dev, pid, code, id, len, data);
3378c2ecf20Sopenharmony_ci	if (action & STR)	/* send Terminate-Request */
3388c2ecf20Sopenharmony_ci		ppp_tx_cp(dev, pid, CP_TERM_REQ, ++ppp->seq, 0, NULL);
3398c2ecf20Sopenharmony_ci	if (action & STA)	/* send Terminate-Ack */
3408c2ecf20Sopenharmony_ci		ppp_tx_cp(dev, pid, CP_TERM_ACK, id, 0, NULL);
3418c2ecf20Sopenharmony_ci	if (action & SCJ)	/* send Code-Reject */
3428c2ecf20Sopenharmony_ci		ppp_tx_cp(dev, pid, CP_CODE_REJ, ++ppp->seq, len, data);
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	if (old_state != OPENED && proto->state == OPENED) {
3458c2ecf20Sopenharmony_ci		netdev_info(dev, "%s up\n", proto_name(pid));
3468c2ecf20Sopenharmony_ci		if (pid == PID_LCP) {
3478c2ecf20Sopenharmony_ci			netif_dormant_off(dev);
3488c2ecf20Sopenharmony_ci			ppp_cp_event(dev, PID_IPCP, START, 0, 0, 0, NULL);
3498c2ecf20Sopenharmony_ci			ppp_cp_event(dev, PID_IPV6CP, START, 0, 0, 0, NULL);
3508c2ecf20Sopenharmony_ci			ppp->last_pong = jiffies;
3518c2ecf20Sopenharmony_ci			mod_timer(&proto->timer, proto->timeout =
3528c2ecf20Sopenharmony_ci				  jiffies + ppp->keepalive_interval * HZ);
3538c2ecf20Sopenharmony_ci		}
3548c2ecf20Sopenharmony_ci	}
3558c2ecf20Sopenharmony_ci	if (old_state == OPENED && proto->state != OPENED) {
3568c2ecf20Sopenharmony_ci		netdev_info(dev, "%s down\n", proto_name(pid));
3578c2ecf20Sopenharmony_ci		if (pid == PID_LCP) {
3588c2ecf20Sopenharmony_ci			netif_dormant_on(dev);
3598c2ecf20Sopenharmony_ci			ppp_cp_event(dev, PID_IPCP, STOP, 0, 0, 0, NULL);
3608c2ecf20Sopenharmony_ci			ppp_cp_event(dev, PID_IPV6CP, STOP, 0, 0, 0, NULL);
3618c2ecf20Sopenharmony_ci		}
3628c2ecf20Sopenharmony_ci	}
3638c2ecf20Sopenharmony_ci	if (old_state != CLOSED && proto->state == CLOSED)
3648c2ecf20Sopenharmony_ci		del_timer(&proto->timer);
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci#if DEBUG_STATE
3678c2ecf20Sopenharmony_ci	printk(KERN_DEBUG "%s: %s ppp_cp_event(%s) ... %s\n", dev->name,
3688c2ecf20Sopenharmony_ci	       proto_name(pid), event_names[event], state_names[proto->state]);
3698c2ecf20Sopenharmony_ci#endif
3708c2ecf20Sopenharmony_ci}
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_cistatic void ppp_cp_parse_cr(struct net_device *dev, u16 pid, u8 id,
3748c2ecf20Sopenharmony_ci			    unsigned int req_len, const u8 *data)
3758c2ecf20Sopenharmony_ci{
3768c2ecf20Sopenharmony_ci	static u8 const valid_accm[6] = { LCP_OPTION_ACCM, 6, 0, 0, 0, 0 };
3778c2ecf20Sopenharmony_ci	const u8 *opt;
3788c2ecf20Sopenharmony_ci	u8 *out;
3798c2ecf20Sopenharmony_ci	unsigned int len = req_len, nak_len = 0, rej_len = 0;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	if (!(out = kmalloc(len, GFP_ATOMIC))) {
3828c2ecf20Sopenharmony_ci		dev->stats.rx_dropped++;
3838c2ecf20Sopenharmony_ci		return;	/* out of memory, ignore CR packet */
3848c2ecf20Sopenharmony_ci	}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	for (opt = data; len; len -= opt[1], opt += opt[1]) {
3878c2ecf20Sopenharmony_ci		if (len < 2 || opt[1] < 2 || len < opt[1])
3888c2ecf20Sopenharmony_ci			goto err_out;
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci		if (pid == PID_LCP)
3918c2ecf20Sopenharmony_ci			switch (opt[0]) {
3928c2ecf20Sopenharmony_ci			case LCP_OPTION_MRU:
3938c2ecf20Sopenharmony_ci				continue; /* MRU always OK and > 1500 bytes? */
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci			case LCP_OPTION_ACCM: /* async control character map */
3968c2ecf20Sopenharmony_ci				if (opt[1] < sizeof(valid_accm))
3978c2ecf20Sopenharmony_ci					goto err_out;
3988c2ecf20Sopenharmony_ci				if (!memcmp(opt, valid_accm,
3998c2ecf20Sopenharmony_ci					    sizeof(valid_accm)))
4008c2ecf20Sopenharmony_ci					continue;
4018c2ecf20Sopenharmony_ci				if (!rej_len) { /* NAK it */
4028c2ecf20Sopenharmony_ci					memcpy(out + nak_len, valid_accm,
4038c2ecf20Sopenharmony_ci					       sizeof(valid_accm));
4048c2ecf20Sopenharmony_ci					nak_len += sizeof(valid_accm);
4058c2ecf20Sopenharmony_ci					continue;
4068c2ecf20Sopenharmony_ci				}
4078c2ecf20Sopenharmony_ci				break;
4088c2ecf20Sopenharmony_ci			case LCP_OPTION_MAGIC:
4098c2ecf20Sopenharmony_ci				if (len < 6)
4108c2ecf20Sopenharmony_ci					goto err_out;
4118c2ecf20Sopenharmony_ci				if (opt[1] != 6 || (!opt[2] && !opt[3] &&
4128c2ecf20Sopenharmony_ci						    !opt[4] && !opt[5]))
4138c2ecf20Sopenharmony_ci					break; /* reject invalid magic number */
4148c2ecf20Sopenharmony_ci				continue;
4158c2ecf20Sopenharmony_ci			}
4168c2ecf20Sopenharmony_ci		/* reject this option */
4178c2ecf20Sopenharmony_ci		memcpy(out + rej_len, opt, opt[1]);
4188c2ecf20Sopenharmony_ci		rej_len += opt[1];
4198c2ecf20Sopenharmony_ci	}
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	if (rej_len)
4228c2ecf20Sopenharmony_ci		ppp_cp_event(dev, pid, RCR_BAD, CP_CONF_REJ, id, rej_len, out);
4238c2ecf20Sopenharmony_ci	else if (nak_len)
4248c2ecf20Sopenharmony_ci		ppp_cp_event(dev, pid, RCR_BAD, CP_CONF_NAK, id, nak_len, out);
4258c2ecf20Sopenharmony_ci	else
4268c2ecf20Sopenharmony_ci		ppp_cp_event(dev, pid, RCR_GOOD, CP_CONF_ACK, id, req_len, data);
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	kfree(out);
4298c2ecf20Sopenharmony_ci	return;
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_cierr_out:
4328c2ecf20Sopenharmony_ci	dev->stats.rx_errors++;
4338c2ecf20Sopenharmony_ci	kfree(out);
4348c2ecf20Sopenharmony_ci}
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_cistatic int ppp_rx(struct sk_buff *skb)
4378c2ecf20Sopenharmony_ci{
4388c2ecf20Sopenharmony_ci	struct hdlc_header *hdr = (struct hdlc_header*)skb->data;
4398c2ecf20Sopenharmony_ci	struct net_device *dev = skb->dev;
4408c2ecf20Sopenharmony_ci	struct ppp *ppp = get_ppp(dev);
4418c2ecf20Sopenharmony_ci	struct proto *proto;
4428c2ecf20Sopenharmony_ci	struct cp_header *cp;
4438c2ecf20Sopenharmony_ci	unsigned long flags;
4448c2ecf20Sopenharmony_ci	unsigned int len;
4458c2ecf20Sopenharmony_ci	u16 pid;
4468c2ecf20Sopenharmony_ci#if DEBUG_CP
4478c2ecf20Sopenharmony_ci	int i;
4488c2ecf20Sopenharmony_ci	char *ptr;
4498c2ecf20Sopenharmony_ci#endif
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ppp->lock, flags);
4528c2ecf20Sopenharmony_ci	/* Check HDLC header */
4538c2ecf20Sopenharmony_ci	if (skb->len < sizeof(struct hdlc_header))
4548c2ecf20Sopenharmony_ci		goto rx_error;
4558c2ecf20Sopenharmony_ci	cp = skb_pull(skb, sizeof(struct hdlc_header));
4568c2ecf20Sopenharmony_ci	if (hdr->address != HDLC_ADDR_ALLSTATIONS ||
4578c2ecf20Sopenharmony_ci	    hdr->control != HDLC_CTRL_UI)
4588c2ecf20Sopenharmony_ci		goto rx_error;
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	pid = ntohs(hdr->protocol);
4618c2ecf20Sopenharmony_ci	proto = get_proto(dev, pid);
4628c2ecf20Sopenharmony_ci	if (!proto) {
4638c2ecf20Sopenharmony_ci		if (ppp->protos[IDX_LCP].state == OPENED)
4648c2ecf20Sopenharmony_ci			ppp_tx_cp(dev, PID_LCP, LCP_PROTO_REJ,
4658c2ecf20Sopenharmony_ci				  ++ppp->seq, skb->len + 2, &hdr->protocol);
4668c2ecf20Sopenharmony_ci		goto rx_error;
4678c2ecf20Sopenharmony_ci	}
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	len = ntohs(cp->len);
4708c2ecf20Sopenharmony_ci	if (len < sizeof(struct cp_header) /* no complete CP header? */ ||
4718c2ecf20Sopenharmony_ci	    skb->len < len /* truncated packet? */)
4728c2ecf20Sopenharmony_ci		goto rx_error;
4738c2ecf20Sopenharmony_ci	skb_pull(skb, sizeof(struct cp_header));
4748c2ecf20Sopenharmony_ci	len -= sizeof(struct cp_header);
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	/* HDLC and CP headers stripped from skb */
4778c2ecf20Sopenharmony_ci#if DEBUG_CP
4788c2ecf20Sopenharmony_ci	if (cp->code < CP_CODES)
4798c2ecf20Sopenharmony_ci		sprintf(debug_buffer, "[%s id 0x%X]", code_names[cp->code],
4808c2ecf20Sopenharmony_ci			cp->id);
4818c2ecf20Sopenharmony_ci	else
4828c2ecf20Sopenharmony_ci		sprintf(debug_buffer, "[code %u id 0x%X]", cp->code, cp->id);
4838c2ecf20Sopenharmony_ci	ptr = debug_buffer + strlen(debug_buffer);
4848c2ecf20Sopenharmony_ci	for (i = 0; i < min_t(unsigned int, len, DEBUG_CP); i++) {
4858c2ecf20Sopenharmony_ci		sprintf(ptr, " %02X", skb->data[i]);
4868c2ecf20Sopenharmony_ci		ptr += strlen(ptr);
4878c2ecf20Sopenharmony_ci	}
4888c2ecf20Sopenharmony_ci	printk(KERN_DEBUG "%s: RX %s %s\n", dev->name, proto_name(pid),
4898c2ecf20Sopenharmony_ci	       debug_buffer);
4908c2ecf20Sopenharmony_ci#endif
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	/* LCP only */
4938c2ecf20Sopenharmony_ci	if (pid == PID_LCP)
4948c2ecf20Sopenharmony_ci		switch (cp->code) {
4958c2ecf20Sopenharmony_ci		case LCP_PROTO_REJ:
4968c2ecf20Sopenharmony_ci			pid = ntohs(*(__be16*)skb->data);
4978c2ecf20Sopenharmony_ci			if (pid == PID_LCP || pid == PID_IPCP ||
4988c2ecf20Sopenharmony_ci			    pid == PID_IPV6CP)
4998c2ecf20Sopenharmony_ci				ppp_cp_event(dev, pid, RXJ_BAD, 0, 0,
5008c2ecf20Sopenharmony_ci					     0, NULL);
5018c2ecf20Sopenharmony_ci			goto out;
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci		case LCP_ECHO_REQ: /* send Echo-Reply */
5048c2ecf20Sopenharmony_ci			if (len >= 4 && proto->state == OPENED)
5058c2ecf20Sopenharmony_ci				ppp_tx_cp(dev, PID_LCP, LCP_ECHO_REPLY,
5068c2ecf20Sopenharmony_ci					  cp->id, len - 4, skb->data + 4);
5078c2ecf20Sopenharmony_ci			goto out;
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci		case LCP_ECHO_REPLY:
5108c2ecf20Sopenharmony_ci			if (cp->id == ppp->echo_id)
5118c2ecf20Sopenharmony_ci				ppp->last_pong = jiffies;
5128c2ecf20Sopenharmony_ci			goto out;
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci		case LCP_DISC_REQ: /* discard */
5158c2ecf20Sopenharmony_ci			goto out;
5168c2ecf20Sopenharmony_ci		}
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	/* LCP, IPCP and IPV6CP */
5198c2ecf20Sopenharmony_ci	switch (cp->code) {
5208c2ecf20Sopenharmony_ci	case CP_CONF_REQ:
5218c2ecf20Sopenharmony_ci		ppp_cp_parse_cr(dev, pid, cp->id, len, skb->data);
5228c2ecf20Sopenharmony_ci		break;
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	case CP_CONF_ACK:
5258c2ecf20Sopenharmony_ci		if (cp->id == proto->cr_id)
5268c2ecf20Sopenharmony_ci			ppp_cp_event(dev, pid, RCA, 0, 0, 0, NULL);
5278c2ecf20Sopenharmony_ci		break;
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	case CP_CONF_REJ:
5308c2ecf20Sopenharmony_ci	case CP_CONF_NAK:
5318c2ecf20Sopenharmony_ci		if (cp->id == proto->cr_id)
5328c2ecf20Sopenharmony_ci			ppp_cp_event(dev, pid, RCN, 0, 0, 0, NULL);
5338c2ecf20Sopenharmony_ci		break;
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	case CP_TERM_REQ:
5368c2ecf20Sopenharmony_ci		ppp_cp_event(dev, pid, RTR, 0, cp->id, 0, NULL);
5378c2ecf20Sopenharmony_ci		break;
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	case CP_TERM_ACK:
5408c2ecf20Sopenharmony_ci		ppp_cp_event(dev, pid, RTA, 0, 0, 0, NULL);
5418c2ecf20Sopenharmony_ci		break;
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci	case CP_CODE_REJ:
5448c2ecf20Sopenharmony_ci		ppp_cp_event(dev, pid, RXJ_BAD, 0, 0, 0, NULL);
5458c2ecf20Sopenharmony_ci		break;
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	default:
5488c2ecf20Sopenharmony_ci		len += sizeof(struct cp_header);
5498c2ecf20Sopenharmony_ci		if (len > dev->mtu)
5508c2ecf20Sopenharmony_ci			len = dev->mtu;
5518c2ecf20Sopenharmony_ci		ppp_cp_event(dev, pid, RUC, 0, 0, len, cp);
5528c2ecf20Sopenharmony_ci		break;
5538c2ecf20Sopenharmony_ci	}
5548c2ecf20Sopenharmony_ci	goto out;
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_cirx_error:
5578c2ecf20Sopenharmony_ci	dev->stats.rx_errors++;
5588c2ecf20Sopenharmony_ciout:
5598c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ppp->lock, flags);
5608c2ecf20Sopenharmony_ci	dev_kfree_skb_any(skb);
5618c2ecf20Sopenharmony_ci	ppp_tx_flush();
5628c2ecf20Sopenharmony_ci	return NET_RX_DROP;
5638c2ecf20Sopenharmony_ci}
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_cistatic void ppp_timer(struct timer_list *t)
5668c2ecf20Sopenharmony_ci{
5678c2ecf20Sopenharmony_ci	struct proto *proto = from_timer(proto, t, timer);
5688c2ecf20Sopenharmony_ci	struct ppp *ppp = get_ppp(proto->dev);
5698c2ecf20Sopenharmony_ci	unsigned long flags;
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	spin_lock_irqsave(&ppp->lock, flags);
5728c2ecf20Sopenharmony_ci	/* mod_timer could be called after we entered this function but
5738c2ecf20Sopenharmony_ci	 * before we got the lock.
5748c2ecf20Sopenharmony_ci	 */
5758c2ecf20Sopenharmony_ci	if (timer_pending(&proto->timer)) {
5768c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&ppp->lock, flags);
5778c2ecf20Sopenharmony_ci		return;
5788c2ecf20Sopenharmony_ci	}
5798c2ecf20Sopenharmony_ci	switch (proto->state) {
5808c2ecf20Sopenharmony_ci	case STOPPING:
5818c2ecf20Sopenharmony_ci	case REQ_SENT:
5828c2ecf20Sopenharmony_ci	case ACK_RECV:
5838c2ecf20Sopenharmony_ci	case ACK_SENT:
5848c2ecf20Sopenharmony_ci		if (proto->restart_counter) {
5858c2ecf20Sopenharmony_ci			ppp_cp_event(proto->dev, proto->pid, TO_GOOD, 0, 0,
5868c2ecf20Sopenharmony_ci				     0, NULL);
5878c2ecf20Sopenharmony_ci			proto->restart_counter--;
5888c2ecf20Sopenharmony_ci		} else if (netif_carrier_ok(proto->dev))
5898c2ecf20Sopenharmony_ci			ppp_cp_event(proto->dev, proto->pid, TO_GOOD, 0, 0,
5908c2ecf20Sopenharmony_ci				     0, NULL);
5918c2ecf20Sopenharmony_ci		else
5928c2ecf20Sopenharmony_ci			ppp_cp_event(proto->dev, proto->pid, TO_BAD, 0, 0,
5938c2ecf20Sopenharmony_ci				     0, NULL);
5948c2ecf20Sopenharmony_ci		break;
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	case OPENED:
5978c2ecf20Sopenharmony_ci		if (proto->pid != PID_LCP)
5988c2ecf20Sopenharmony_ci			break;
5998c2ecf20Sopenharmony_ci		if (time_after(jiffies, ppp->last_pong +
6008c2ecf20Sopenharmony_ci			       ppp->keepalive_timeout * HZ)) {
6018c2ecf20Sopenharmony_ci			netdev_info(proto->dev, "Link down\n");
6028c2ecf20Sopenharmony_ci			ppp_cp_event(proto->dev, PID_LCP, STOP, 0, 0, 0, NULL);
6038c2ecf20Sopenharmony_ci			ppp_cp_event(proto->dev, PID_LCP, START, 0, 0, 0, NULL);
6048c2ecf20Sopenharmony_ci		} else {	/* send keep-alive packet */
6058c2ecf20Sopenharmony_ci			ppp->echo_id = ++ppp->seq;
6068c2ecf20Sopenharmony_ci			ppp_tx_cp(proto->dev, PID_LCP, LCP_ECHO_REQ,
6078c2ecf20Sopenharmony_ci				  ppp->echo_id, 0, NULL);
6088c2ecf20Sopenharmony_ci			proto->timer.expires = jiffies +
6098c2ecf20Sopenharmony_ci				ppp->keepalive_interval * HZ;
6108c2ecf20Sopenharmony_ci			add_timer(&proto->timer);
6118c2ecf20Sopenharmony_ci		}
6128c2ecf20Sopenharmony_ci		break;
6138c2ecf20Sopenharmony_ci	}
6148c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&ppp->lock, flags);
6158c2ecf20Sopenharmony_ci	ppp_tx_flush();
6168c2ecf20Sopenharmony_ci}
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_cistatic void ppp_start(struct net_device *dev)
6208c2ecf20Sopenharmony_ci{
6218c2ecf20Sopenharmony_ci	struct ppp *ppp = get_ppp(dev);
6228c2ecf20Sopenharmony_ci	int i;
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci	for (i = 0; i < IDX_COUNT; i++) {
6258c2ecf20Sopenharmony_ci		struct proto *proto = &ppp->protos[i];
6268c2ecf20Sopenharmony_ci		proto->dev = dev;
6278c2ecf20Sopenharmony_ci		timer_setup(&proto->timer, ppp_timer, 0);
6288c2ecf20Sopenharmony_ci		proto->state = CLOSED;
6298c2ecf20Sopenharmony_ci	}
6308c2ecf20Sopenharmony_ci	ppp->protos[IDX_LCP].pid = PID_LCP;
6318c2ecf20Sopenharmony_ci	ppp->protos[IDX_IPCP].pid = PID_IPCP;
6328c2ecf20Sopenharmony_ci	ppp->protos[IDX_IPV6CP].pid = PID_IPV6CP;
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	ppp_cp_event(dev, PID_LCP, START, 0, 0, 0, NULL);
6358c2ecf20Sopenharmony_ci}
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_cistatic void ppp_stop(struct net_device *dev)
6388c2ecf20Sopenharmony_ci{
6398c2ecf20Sopenharmony_ci	ppp_cp_event(dev, PID_LCP, STOP, 0, 0, 0, NULL);
6408c2ecf20Sopenharmony_ci}
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_cistatic void ppp_close(struct net_device *dev)
6438c2ecf20Sopenharmony_ci{
6448c2ecf20Sopenharmony_ci	ppp_tx_flush();
6458c2ecf20Sopenharmony_ci}
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_cistatic struct hdlc_proto proto = {
6488c2ecf20Sopenharmony_ci	.start		= ppp_start,
6498c2ecf20Sopenharmony_ci	.stop		= ppp_stop,
6508c2ecf20Sopenharmony_ci	.close		= ppp_close,
6518c2ecf20Sopenharmony_ci	.type_trans	= ppp_type_trans,
6528c2ecf20Sopenharmony_ci	.ioctl		= ppp_ioctl,
6538c2ecf20Sopenharmony_ci	.netif_rx	= ppp_rx,
6548c2ecf20Sopenharmony_ci	.module		= THIS_MODULE,
6558c2ecf20Sopenharmony_ci};
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_cistatic const struct header_ops ppp_header_ops = {
6588c2ecf20Sopenharmony_ci	.create = ppp_hard_header,
6598c2ecf20Sopenharmony_ci};
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_cistatic int ppp_ioctl(struct net_device *dev, struct ifreq *ifr)
6628c2ecf20Sopenharmony_ci{
6638c2ecf20Sopenharmony_ci	hdlc_device *hdlc = dev_to_hdlc(dev);
6648c2ecf20Sopenharmony_ci	struct ppp *ppp;
6658c2ecf20Sopenharmony_ci	int result;
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci	switch (ifr->ifr_settings.type) {
6688c2ecf20Sopenharmony_ci	case IF_GET_PROTO:
6698c2ecf20Sopenharmony_ci		if (dev_to_hdlc(dev)->proto != &proto)
6708c2ecf20Sopenharmony_ci			return -EINVAL;
6718c2ecf20Sopenharmony_ci		ifr->ifr_settings.type = IF_PROTO_PPP;
6728c2ecf20Sopenharmony_ci		return 0; /* return protocol only, no settable parameters */
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	case IF_PROTO_PPP:
6758c2ecf20Sopenharmony_ci		if (!capable(CAP_NET_ADMIN))
6768c2ecf20Sopenharmony_ci			return -EPERM;
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci		if (dev->flags & IFF_UP)
6798c2ecf20Sopenharmony_ci			return -EBUSY;
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci		/* no settable parameters */
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci		result = hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
6848c2ecf20Sopenharmony_ci		if (result)
6858c2ecf20Sopenharmony_ci			return result;
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci		result = attach_hdlc_protocol(dev, &proto, sizeof(struct ppp));
6888c2ecf20Sopenharmony_ci		if (result)
6898c2ecf20Sopenharmony_ci			return result;
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci		ppp = get_ppp(dev);
6928c2ecf20Sopenharmony_ci		spin_lock_init(&ppp->lock);
6938c2ecf20Sopenharmony_ci		ppp->req_timeout = 2;
6948c2ecf20Sopenharmony_ci		ppp->cr_retries = 10;
6958c2ecf20Sopenharmony_ci		ppp->term_retries = 2;
6968c2ecf20Sopenharmony_ci		ppp->keepalive_interval = 10;
6978c2ecf20Sopenharmony_ci		ppp->keepalive_timeout = 60;
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci		dev->hard_header_len = sizeof(struct hdlc_header);
7008c2ecf20Sopenharmony_ci		dev->header_ops = &ppp_header_ops;
7018c2ecf20Sopenharmony_ci		dev->type = ARPHRD_PPP;
7028c2ecf20Sopenharmony_ci		call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
7038c2ecf20Sopenharmony_ci		netif_dormant_on(dev);
7048c2ecf20Sopenharmony_ci		return 0;
7058c2ecf20Sopenharmony_ci	}
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci	return -EINVAL;
7088c2ecf20Sopenharmony_ci}
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_cistatic int __init mod_init(void)
7128c2ecf20Sopenharmony_ci{
7138c2ecf20Sopenharmony_ci	skb_queue_head_init(&tx_queue);
7148c2ecf20Sopenharmony_ci	register_hdlc_protocol(&proto);
7158c2ecf20Sopenharmony_ci	return 0;
7168c2ecf20Sopenharmony_ci}
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_cistatic void __exit mod_exit(void)
7198c2ecf20Sopenharmony_ci{
7208c2ecf20Sopenharmony_ci	unregister_hdlc_protocol(&proto);
7218c2ecf20Sopenharmony_ci}
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_cimodule_init(mod_init);
7258c2ecf20Sopenharmony_cimodule_exit(mod_exit);
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ciMODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
7288c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("PPP protocol support for generic HDLC");
7298c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
730