File Index
Files and directories
Sign in
FILE

hdlc_ppp.c

File preview with syntax highlighting.

linux-6.19.14/drivers/net/wan/hdlc_ppp.c C
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Generic HDLC support routines for Linux
  4 * Point-to-point protocol support
  5 *
  6 * Copyright (C) 1999 - 2008 Krzysztof Halasa <khc@pm.waw.pl>
  7 */
  8
  9#include <linux/errno.h>
 10#include <linux/hdlc.h>
 11#include <linux/if_arp.h>
 12#include <linux/inetdevice.h>
 13#include <linux/init.h>
 14#include <linux/kernel.h>
 15#include <linux/module.h>
 16#include <linux/pkt_sched.h>
 17#include <linux/poll.h>
 18#include <linux/skbuff.h>
 19#include <linux/slab.h>
 20#include <linux/spinlock.h>
 21
 22#define DEBUG_CP		0 /* also bytes# to dump */
 23#define DEBUG_STATE		0
 24#define DEBUG_HARD_HEADER	0
 25
 26#define HDLC_ADDR_ALLSTATIONS	0xFF
 27#define HDLC_CTRL_UI		0x03
 28
 29#define PID_LCP			0xC021
 30#define PID_IP			0x0021
 31#define PID_IPCP		0x8021
 32#define PID_IPV6		0x0057
 33#define PID_IPV6CP		0x8057
 34
 35enum {IDX_LCP = 0, IDX_IPCP, IDX_IPV6CP, IDX_COUNT};
 36enum {CP_CONF_REQ = 1, CP_CONF_ACK, CP_CONF_NAK, CP_CONF_REJ, CP_TERM_REQ,
 37      CP_TERM_ACK, CP_CODE_REJ, LCP_PROTO_REJ, LCP_ECHO_REQ, LCP_ECHO_REPLY,
 38      LCP_DISC_REQ, CP_CODES};
 39#if DEBUG_CP
 40static const char *const code_names[CP_CODES] = {
 41	"0", "ConfReq", "ConfAck", "ConfNak", "ConfRej", "TermReq",
 42	"TermAck", "CodeRej", "ProtoRej", "EchoReq", "EchoReply", "Discard"
 43};
 44
 45static char debug_buffer[64 + 3 * DEBUG_CP];
 46#endif
 47
 48enum {LCP_OPTION_MRU = 1, LCP_OPTION_ACCM, LCP_OPTION_MAGIC = 5};
 49
 50struct hdlc_header {
 51	u8 address;
 52	u8 control;
 53	__be16 protocol;
 54};
 55
 56struct cp_header {
 57	u8 code;
 58	u8 id;
 59	__be16 len;
 60};
 61
 62struct proto {
 63	struct net_device *dev;
 64	struct timer_list timer;
 65	unsigned long timeout;
 66	u16 pid;		/* protocol ID */
 67	u8 state;
 68	u8 cr_id;		/* ID of last Configuration-Request */
 69	u8 restart_counter;
 70};
 71
 72struct ppp {
 73	struct proto protos[IDX_COUNT];
 74	spinlock_t lock;
 75	unsigned long last_pong;
 76	unsigned int req_timeout, cr_retries, term_retries;
 77	unsigned int keepalive_interval, keepalive_timeout;
 78	u8 seq;			/* local sequence number for requests */
 79	u8 echo_id;		/* ID of last Echo-Request (LCP) */
 80};
 81
 82enum {CLOSED = 0, STOPPED, STOPPING, REQ_SENT, ACK_RECV, ACK_SENT, OPENED,
 83      STATES, STATE_MASK = 0xF};
 84enum {START = 0, STOP, TO_GOOD, TO_BAD, RCR_GOOD, RCR_BAD, RCA, RCN, RTR, RTA,
 85      RUC, RXJ_GOOD, RXJ_BAD, EVENTS};
 86enum {INV = 0x10, IRC = 0x20, ZRC = 0x40, SCR = 0x80, SCA = 0x100,
 87      SCN = 0x200, STR = 0x400, STA = 0x800, SCJ = 0x1000};
 88
 89#if DEBUG_STATE
 90static const char *const state_names[STATES] = {
 91	"Closed", "Stopped", "Stopping", "ReqSent", "AckRecv", "AckSent",
 92	"Opened"
 93};
 94
 95static const char *const event_names[EVENTS] = {
 96	"Start", "Stop", "TO+", "TO-", "RCR+", "RCR-", "RCA", "RCN",
 97	"RTR", "RTA", "RUC", "RXJ+", "RXJ-"
 98};
 99#endif
100
101static struct sk_buff_head tx_queue; /* used when holding the spin lock */
102
103static int ppp_ioctl(struct net_device *dev, struct if_settings *ifs);
104
105static inline struct ppp *get_ppp(struct net_device *dev)
106{
107	return (struct ppp *)dev_to_hdlc(dev)->state;
108}
109
110static inline struct proto *get_proto(struct net_device *dev, u16 pid)
111{
112	struct ppp *ppp = get_ppp(dev);
113
114	switch (pid) {
115	case PID_LCP:
116		return &ppp->protos[IDX_LCP];
117	case PID_IPCP:
118		return &ppp->protos[IDX_IPCP];
119	case PID_IPV6CP:
120		return &ppp->protos[IDX_IPV6CP];
121	default:
122		return NULL;
123	}
124}
125
126static inline const char *proto_name(u16 pid)
127{
128	switch (pid) {
129	case PID_IPCP:
130		return "IPCP";
131	case PID_IPV6CP:
132		return "IPV6CP";
133	default:
134		return "LCP";
135	}
136}
137
138static __be16 ppp_type_trans(struct sk_buff *skb, struct net_device *dev)
139{
140	struct hdlc_header *data = (struct hdlc_header *)skb->data;
141
142	if (skb->len < sizeof(struct hdlc_header))
143		return htons(ETH_P_HDLC);
144	if (data->address != HDLC_ADDR_ALLSTATIONS ||
145	    data->control != HDLC_CTRL_UI)
146		return htons(ETH_P_HDLC);
147
148	switch (data->protocol) {
149	case cpu_to_be16(PID_IP):
150		skb_pull(skb, sizeof(struct hdlc_header));
151		return htons(ETH_P_IP);
152
153	case cpu_to_be16(PID_IPV6):
154		skb_pull(skb, sizeof(struct hdlc_header));
155		return htons(ETH_P_IPV6);
156
157	default:
158		return htons(ETH_P_HDLC);
159	}
160}
161
162static int ppp_hard_header(struct sk_buff *skb, struct net_device *dev,
163			   u16 type, const void *daddr, const void *saddr,
164			   unsigned int len)
165{
166	struct hdlc_header *data;
167#if DEBUG_HARD_HEADER
168	printk(KERN_DEBUG "%s: ppp_hard_header() called\n", dev->name);
169#endif
170
171	skb_push(skb, sizeof(struct hdlc_header));
172	data = (struct hdlc_header *)skb->data;
173
174	data->address = HDLC_ADDR_ALLSTATIONS;
175	data->control = HDLC_CTRL_UI;
176	switch (type) {
177	case ETH_P_IP:
178		data->protocol = htons(PID_IP);
179		break;
180	case ETH_P_IPV6:
181		data->protocol = htons(PID_IPV6);
182		break;
183	case PID_LCP:
184	case PID_IPCP:
185	case PID_IPV6CP:
186		data->protocol = htons(type);
187		break;
188	default:		/* unknown protocol */
189		data->protocol = 0;
190	}
191	return sizeof(struct hdlc_header);
192}
193
194static void ppp_tx_flush(void)
195{
196	struct sk_buff *skb;
197
198	while ((skb = skb_dequeue(&tx_queue)) != NULL)
199		dev_queue_xmit(skb);
200}
201
202static void ppp_tx_cp(struct net_device *dev, u16 pid, u8 code,
203		      u8 id, unsigned int len, const void *data)
204{
205	struct sk_buff *skb;
206	struct cp_header *cp;
207	unsigned int magic_len = 0;
208	static u32 magic;
209
210#if DEBUG_CP
211	int i;
212	char *ptr;
213#endif
214
215	if (pid == PID_LCP && (code == LCP_ECHO_REQ || code == LCP_ECHO_REPLY))
216		magic_len = sizeof(magic);
217
218	skb = dev_alloc_skb(sizeof(struct hdlc_header) +
219			    sizeof(struct cp_header) + magic_len + len);
220	if (!skb)
221		return;
222
223	skb_reserve(skb, sizeof(struct hdlc_header));
224
225	cp = skb_put(skb, sizeof(struct cp_header));
226	cp->code = code;
227	cp->id = id;
228	cp->len = htons(sizeof(struct cp_header) + magic_len + len);
229
230	if (magic_len)
231		skb_put_data(skb, &magic, magic_len);
232	if (len)
233		skb_put_data(skb, data, len);
234
235#if DEBUG_CP
236	BUG_ON(code >= CP_CODES);
237	ptr = debug_buffer;
238	*ptr = '\x0';
239	for (i = 0; i < min_t(unsigned int, magic_len + len, DEBUG_CP); i++) {
240		sprintf(ptr, " %02X", skb->data[sizeof(struct cp_header) + i]);
241		ptr += strlen(ptr);
242	}
243	printk(KERN_DEBUG "%s: TX %s [%s id 0x%X]%s\n", dev->name,
244	       proto_name(pid), code_names[code], id, debug_buffer);
245#endif
246
247	ppp_hard_header(skb, dev, pid, NULL, NULL, 0);
248
249	skb->priority = TC_PRIO_CONTROL;
250	skb->dev = dev;
251	skb->protocol = htons(ETH_P_HDLC);
252	skb_reset_network_header(skb);
253	skb_queue_tail(&tx_queue, skb);
254}
255
256/* State transition table (compare STD-51)
257   Events                                   Actions
258   TO+  = Timeout with counter > 0          irc = Initialize-Restart-Count
259   TO-  = Timeout with counter expired      zrc = Zero-Restart-Count
260
261   RCR+ = Receive-Configure-Request (Good)  scr = Send-Configure-Request
262   RCR- = Receive-Configure-Request (Bad)
263   RCA  = Receive-Configure-Ack             sca = Send-Configure-Ack
264   RCN  = Receive-Configure-Nak/Rej         scn = Send-Configure-Nak/Rej
265
266   RTR  = Receive-Terminate-Request         str = Send-Terminate-Request
267   RTA  = Receive-Terminate-Ack             sta = Send-Terminate-Ack
268
269   RUC  = Receive-Unknown-Code              scj = Send-Code-Reject
270   RXJ+ = Receive-Code-Reject (permitted)
271       or Receive-Protocol-Reject
272   RXJ- = Receive-Code-Reject (catastrophic)
273       or Receive-Protocol-Reject
274*/
275static int cp_table[EVENTS][STATES] = {
276	/* CLOSED     STOPPED STOPPING REQ_SENT ACK_RECV ACK_SENT OPENED
277	     0           1         2       3       4      5          6    */
278	{IRC|SCR|3,     INV     , INV ,   INV   , INV ,  INV    ,   INV   }, /* START */
279	{   INV   ,      0      ,  0  ,    0    ,  0  ,   0     ,    0    }, /* STOP */
280	{   INV   ,     INV     ,STR|2,  SCR|3  ,SCR|3,  SCR|5  ,   INV   }, /* TO+ */
281	{   INV   ,     INV     ,  1  ,    1    ,  1  ,    1    ,   INV   }, /* TO- */
282	{  STA|0  ,IRC|SCR|SCA|5,  2  ,  SCA|5  ,SCA|6,  SCA|5  ,SCR|SCA|5}, /* RCR+ */
283	{  STA|0  ,IRC|SCR|SCN|3,  2  ,  SCN|3  ,SCN|4,  SCN|3  ,SCR|SCN|3}, /* RCR- */
284	{  STA|0  ,    STA|1    ,  2  ,  IRC|4  ,SCR|3,    6    , SCR|3   }, /* RCA */
285	{  STA|0  ,    STA|1    ,  2  ,IRC|SCR|3,SCR|3,IRC|SCR|5, SCR|3   }, /* RCN */
286	{  STA|0  ,    STA|1    ,STA|2,  STA|3  ,STA|3,  STA|3  ,ZRC|STA|2}, /* RTR */
287	{    0    ,      1      ,  1  ,    3    ,  3  ,    5    ,  SCR|3  }, /* RTA */
288	{  SCJ|0  ,    SCJ|1    ,SCJ|2,  SCJ|3  ,SCJ|4,  SCJ|5  ,  SCJ|6  }, /* RUC */
289	{    0    ,      1      ,  2  ,    3    ,  3  ,    5    ,    6    }, /* RXJ+ */
290	{    0    ,      1      ,  1  ,    1    ,  1  ,    1    ,IRC|STR|2}, /* RXJ- */
291};
292
293/* SCA: RCR+ must supply id, len and data
294   SCN: RCR- must supply code, id, len and data
295   STA: RTR must supply id
296   SCJ: RUC must supply CP packet len and data */
297static void ppp_cp_event(struct net_device *dev, u16 pid, u16 event, u8 code,
298			 u8 id, unsigned int len, const void *data)
299{
300	int old_state, action;
301	struct ppp *ppp = get_ppp(dev);
302	struct proto *proto = get_proto(dev, pid);
303
304	old_state = proto->state;
305	BUG_ON(old_state >= STATES);
306	BUG_ON(event >= EVENTS);
307
308#if DEBUG_STATE
309	printk(KERN_DEBUG "%s: %s ppp_cp_event(%s) %s ...\n", dev->name,
310	       proto_name(pid), event_names[event], state_names[proto->state]);
311#endif
312
313	action = cp_table[event][old_state];
314
315	proto->state = action & STATE_MASK;
316	if (action & (SCR | STR)) /* set Configure-Req/Terminate-Req timer */
317		mod_timer(&proto->timer, proto->timeout =
318			  jiffies + ppp->req_timeout * HZ);
319	if (action & ZRC)
320		proto->restart_counter = 0;
321	if (action & IRC)
322		proto->restart_counter = (proto->state == STOPPING) ?
323			ppp->term_retries : ppp->cr_retries;
324
325	if (action & SCR)	/* send Configure-Request */
326		ppp_tx_cp(dev, pid, CP_CONF_REQ, proto->cr_id = ++ppp->seq,
327			  0, NULL);
328	if (action & SCA)	/* send Configure-Ack */
329		ppp_tx_cp(dev, pid, CP_CONF_ACK, id, len, data);
330	if (action & SCN)	/* send Configure-Nak/Reject */
331		ppp_tx_cp(dev, pid, code, id, len, data);
332	if (action & STR)	/* send Terminate-Request */
333		ppp_tx_cp(dev, pid, CP_TERM_REQ, ++ppp->seq, 0, NULL);
334	if (action & STA)	/* send Terminate-Ack */
335		ppp_tx_cp(dev, pid, CP_TERM_ACK, id, 0, NULL);
336	if (action & SCJ)	/* send Code-Reject */
337		ppp_tx_cp(dev, pid, CP_CODE_REJ, ++ppp->seq, len, data);
338
339	if (old_state != OPENED && proto->state == OPENED) {
340		netdev_info(dev, "%s up\n", proto_name(pid));
341		if (pid == PID_LCP) {
342			netif_dormant_off(dev);
343			ppp_cp_event(dev, PID_IPCP, START, 0, 0, 0, NULL);
344			ppp_cp_event(dev, PID_IPV6CP, START, 0, 0, 0, NULL);
345			ppp->last_pong = jiffies;
346			mod_timer(&proto->timer, proto->timeout =
347				  jiffies + ppp->keepalive_interval * HZ);
348		}
349	}
350	if (old_state == OPENED && proto->state != OPENED) {
351		netdev_info(dev, "%s down\n", proto_name(pid));
352		if (pid == PID_LCP) {
353			netif_dormant_on(dev);
354			ppp_cp_event(dev, PID_IPCP, STOP, 0, 0, 0, NULL);
355			ppp_cp_event(dev, PID_IPV6CP, STOP, 0, 0, 0, NULL);
356		}
357	}
358	if (old_state != CLOSED && proto->state == CLOSED)
359		timer_delete(&proto->timer);
360
361#if DEBUG_STATE
362	printk(KERN_DEBUG "%s: %s ppp_cp_event(%s) ... %s\n", dev->name,
363	       proto_name(pid), event_names[event], state_names[proto->state]);
364#endif
365}
366
367static void ppp_cp_parse_cr(struct net_device *dev, u16 pid, u8 id,
368			    unsigned int req_len, const u8 *data)
369{
370	static u8 const valid_accm[6] = { LCP_OPTION_ACCM, 6, 0, 0, 0, 0 };
371	const u8 *opt;
372	u8 *out;
373	unsigned int len = req_len, nak_len = 0, rej_len = 0;
374
375	out = kmalloc(len, GFP_ATOMIC);
376	if (!out) {
377		dev->stats.rx_dropped++;
378		return;	/* out of memory, ignore CR packet */
379	}
380
381	for (opt = data; len; len -= opt[1], opt += opt[1]) {
382		if (len < 2 || opt[1] < 2 || len < opt[1])
383			goto err_out;
384
385		if (pid == PID_LCP)
386			switch (opt[0]) {
387			case LCP_OPTION_MRU:
388				continue; /* MRU always OK and > 1500 bytes? */
389
390			case LCP_OPTION_ACCM: /* async control character map */
391				if (opt[1] < sizeof(valid_accm))
392					goto err_out;
393				if (!memcmp(opt, valid_accm,
394					    sizeof(valid_accm)))
395					continue;
396				if (!rej_len) { /* NAK it */
397					memcpy(out + nak_len, valid_accm,
398					       sizeof(valid_accm));
399					nak_len += sizeof(valid_accm);
400					continue;
401				}
402				break;
403			case LCP_OPTION_MAGIC:
404				if (len < 6)
405					goto err_out;
406				if (opt[1] != 6 || (!opt[2] && !opt[3] &&
407						    !opt[4] && !opt[5]))
408					break; /* reject invalid magic number */
409				continue;
410			}
411		/* reject this option */
412		memcpy(out + rej_len, opt, opt[1]);
413		rej_len += opt[1];
414	}
415
416	if (rej_len)
417		ppp_cp_event(dev, pid, RCR_BAD, CP_CONF_REJ, id, rej_len, out);
418	else if (nak_len)
419		ppp_cp_event(dev, pid, RCR_BAD, CP_CONF_NAK, id, nak_len, out);
420	else
421		ppp_cp_event(dev, pid, RCR_GOOD, CP_CONF_ACK, id, req_len, data);
422
423	kfree(out);
424	return;
425
426err_out:
427	dev->stats.rx_errors++;
428	kfree(out);
429}
430
431static int ppp_rx(struct sk_buff *skb)
432{
433	struct hdlc_header *hdr = (struct hdlc_header *)skb->data;
434	struct net_device *dev = skb->dev;
435	struct ppp *ppp = get_ppp(dev);
436	struct proto *proto;
437	struct cp_header *cp;
438	unsigned long flags;
439	unsigned int len;
440	u16 pid;
441#if DEBUG_CP
442	int i;
443	char *ptr;
444#endif
445
446	spin_lock_irqsave(&ppp->lock, flags);
447	/* Check HDLC header */
448	if (skb->len < sizeof(struct hdlc_header))
449		goto rx_error;
450	cp = skb_pull(skb, sizeof(struct hdlc_header));
451	if (hdr->address != HDLC_ADDR_ALLSTATIONS ||
452	    hdr->control != HDLC_CTRL_UI)
453		goto rx_error;
454
455	pid = ntohs(hdr->protocol);
456	proto = get_proto(dev, pid);
457	if (!proto) {
458		if (ppp->protos[IDX_LCP].state == OPENED)
459			ppp_tx_cp(dev, PID_LCP, LCP_PROTO_REJ,
460				  ++ppp->seq, skb->len + 2, &hdr->protocol);
461		goto rx_error;
462	}
463
464	len = ntohs(cp->len);
465	if (len < sizeof(struct cp_header) /* no complete CP header? */ ||
466	    skb->len < len /* truncated packet? */)
467		goto rx_error;
468	skb_pull(skb, sizeof(struct cp_header));
469	len -= sizeof(struct cp_header);
470
471	/* HDLC and CP headers stripped from skb */
472#if DEBUG_CP
473	if (cp->code < CP_CODES)
474		sprintf(debug_buffer, "[%s id 0x%X]", code_names[cp->code],
475			cp->id);
476	else
477		sprintf(debug_buffer, "[code %u id 0x%X]", cp->code, cp->id);
478	ptr = debug_buffer + strlen(debug_buffer);
479	for (i = 0; i < min_t(unsigned int, len, DEBUG_CP); i++) {
480		sprintf(ptr, " %02X", skb->data[i]);
481		ptr += strlen(ptr);
482	}
483	printk(KERN_DEBUG "%s: RX %s %s\n", dev->name, proto_name(pid),
484	       debug_buffer);
485#endif
486
487	/* LCP only */
488	if (pid == PID_LCP)
489		switch (cp->code) {
490		case LCP_PROTO_REJ:
491			pid = ntohs(*(__be16 *)skb->data);
492			if (pid == PID_LCP || pid == PID_IPCP ||
493			    pid == PID_IPV6CP)
494				ppp_cp_event(dev, pid, RXJ_BAD, 0, 0,
495					     0, NULL);
496			goto out;
497
498		case LCP_ECHO_REQ: /* send Echo-Reply */
499			if (len >= 4 && proto->state == OPENED)
500				ppp_tx_cp(dev, PID_LCP, LCP_ECHO_REPLY,
501					  cp->id, len - 4, skb->data + 4);
502			goto out;
503
504		case LCP_ECHO_REPLY:
505			if (cp->id == ppp->echo_id)
506				ppp->last_pong = jiffies;
507			goto out;
508
509		case LCP_DISC_REQ: /* discard */
510			goto out;
511		}
512
513	/* LCP, IPCP and IPV6CP */
514	switch (cp->code) {
515	case CP_CONF_REQ:
516		ppp_cp_parse_cr(dev, pid, cp->id, len, skb->data);
517		break;
518
519	case CP_CONF_ACK:
520		if (cp->id == proto->cr_id)
521			ppp_cp_event(dev, pid, RCA, 0, 0, 0, NULL);
522		break;
523
524	case CP_CONF_REJ:
525	case CP_CONF_NAK:
526		if (cp->id == proto->cr_id)
527			ppp_cp_event(dev, pid, RCN, 0, 0, 0, NULL);
528		break;
529
530	case CP_TERM_REQ:
531		ppp_cp_event(dev, pid, RTR, 0, cp->id, 0, NULL);
532		break;
533
534	case CP_TERM_ACK:
535		ppp_cp_event(dev, pid, RTA, 0, 0, 0, NULL);
536		break;
537
538	case CP_CODE_REJ:
539		ppp_cp_event(dev, pid, RXJ_BAD, 0, 0, 0, NULL);
540		break;
541
542	default:
543		len += sizeof(struct cp_header);
544		if (len > dev->mtu)
545			len = dev->mtu;
546		ppp_cp_event(dev, pid, RUC, 0, 0, len, cp);
547		break;
548	}
549	goto out;
550
551rx_error:
552	dev->stats.rx_errors++;
553out:
554	spin_unlock_irqrestore(&ppp->lock, flags);
555	dev_kfree_skb_any(skb);
556	ppp_tx_flush();
557	return NET_RX_DROP;
558}
559
560static void ppp_timer(struct timer_list *t)
561{
562	struct proto *proto = timer_container_of(proto, t, timer);
563	struct ppp *ppp = get_ppp(proto->dev);
564	unsigned long flags;
565
566	spin_lock_irqsave(&ppp->lock, flags);
567	/* mod_timer could be called after we entered this function but
568	 * before we got the lock.
569	 */
570	if (timer_pending(&proto->timer)) {
571		spin_unlock_irqrestore(&ppp->lock, flags);
572		return;
573	}
574	switch (proto->state) {
575	case STOPPING:
576	case REQ_SENT:
577	case ACK_RECV:
578	case ACK_SENT:
579		if (proto->restart_counter) {
580			ppp_cp_event(proto->dev, proto->pid, TO_GOOD, 0, 0,
581				     0, NULL);
582			proto->restart_counter--;
583		} else if (netif_carrier_ok(proto->dev))
584			ppp_cp_event(proto->dev, proto->pid, TO_GOOD, 0, 0,
585				     0, NULL);
586		else
587			ppp_cp_event(proto->dev, proto->pid, TO_BAD, 0, 0,
588				     0, NULL);
589		break;
590
591	case OPENED:
592		if (proto->pid != PID_LCP)
593			break;
594		if (time_after(jiffies, ppp->last_pong +
595			       ppp->keepalive_timeout * HZ)) {
596			netdev_info(proto->dev, "Link down\n");
597			ppp_cp_event(proto->dev, PID_LCP, STOP, 0, 0, 0, NULL);
598			ppp_cp_event(proto->dev, PID_LCP, START, 0, 0, 0, NULL);
599		} else {	/* send keep-alive packet */
600			ppp->echo_id = ++ppp->seq;
601			ppp_tx_cp(proto->dev, PID_LCP, LCP_ECHO_REQ,
602				  ppp->echo_id, 0, NULL);
603			proto->timer.expires = jiffies +
604				ppp->keepalive_interval * HZ;
605			add_timer(&proto->timer);
606		}
607		break;
608	}
609	spin_unlock_irqrestore(&ppp->lock, flags);
610	ppp_tx_flush();
611}
612
613static void ppp_start(struct net_device *dev)
614{
615	struct ppp *ppp = get_ppp(dev);
616	int i;
617
618	for (i = 0; i < IDX_COUNT; i++) {
619		struct proto *proto = &ppp->protos[i];
620
621		proto->dev = dev;
622		timer_setup(&proto->timer, ppp_timer, 0);
623		proto->state = CLOSED;
624	}
625	ppp->protos[IDX_LCP].pid = PID_LCP;
626	ppp->protos[IDX_IPCP].pid = PID_IPCP;
627	ppp->protos[IDX_IPV6CP].pid = PID_IPV6CP;
628
629	ppp_cp_event(dev, PID_LCP, START, 0, 0, 0, NULL);
630}
631
632static void ppp_stop(struct net_device *dev)
633{
634	ppp_cp_event(dev, PID_LCP, STOP, 0, 0, 0, NULL);
635}
636
637static void ppp_close(struct net_device *dev)
638{
639	ppp_tx_flush();
640}
641
642static struct hdlc_proto proto = {
643	.start		= ppp_start,
644	.stop		= ppp_stop,
645	.close		= ppp_close,
646	.type_trans	= ppp_type_trans,
647	.ioctl		= ppp_ioctl,
648	.netif_rx	= ppp_rx,
649	.module		= THIS_MODULE,
650};
651
652static const struct header_ops ppp_header_ops = {
653	.create = ppp_hard_header,
654};
655
656static int ppp_ioctl(struct net_device *dev, struct if_settings *ifs)
657{
658	hdlc_device *hdlc = dev_to_hdlc(dev);
659	struct ppp *ppp;
660	int result;
661
662	switch (ifs->type) {
663	case IF_GET_PROTO:
664		if (dev_to_hdlc(dev)->proto != &proto)
665			return -EINVAL;
666		ifs->type = IF_PROTO_PPP;
667		return 0; /* return protocol only, no settable parameters */
668
669	case IF_PROTO_PPP:
670		if (!capable(CAP_NET_ADMIN))
671			return -EPERM;
672
673		if (dev->flags & IFF_UP)
674			return -EBUSY;
675
676		/* no settable parameters */
677
678		result = hdlc->attach(dev, ENCODING_NRZ,
679				      PARITY_CRC16_PR1_CCITT);
680		if (result)
681			return result;
682
683		result = attach_hdlc_protocol(dev, &proto, sizeof(struct ppp));
684		if (result)
685			return result;
686
687		ppp = get_ppp(dev);
688		spin_lock_init(&ppp->lock);
689		ppp->req_timeout = 2;
690		ppp->cr_retries = 10;
691		ppp->term_retries = 2;
692		ppp->keepalive_interval = 10;
693		ppp->keepalive_timeout = 60;
694
695		dev->hard_header_len = sizeof(struct hdlc_header);
696		dev->header_ops = &ppp_header_ops;
697		dev->type = ARPHRD_PPP;
698		call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
699		netif_dormant_on(dev);
700		return 0;
701	}
702
703	return -EINVAL;
704}
705
706static int __init hdlc_ppp_init(void)
707{
708	skb_queue_head_init(&tx_queue);
709	register_hdlc_protocol(&proto);
710	return 0;
711}
712
713static void __exit hdlc_ppp_exit(void)
714{
715	unregister_hdlc_protocol(&proto);
716}
717
718module_init(hdlc_ppp_init);
719module_exit(hdlc_ppp_exit);
720
721MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
722MODULE_DESCRIPTION("PPP protocol support for generic HDLC");
723MODULE_LICENSE("GPL v2");
~/ Read-only file preview.