aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/wan/hdlc_ppp.c
diff options
context:
space:
mode:
authorKrzysztof Hałasa <khc@pm.waw.pl>2008-08-14 13:17:38 -0400
committerKrzysztof Hałasa <khc@pm.waw.pl>2008-11-21 20:49:48 -0500
commite022c2f07ae52bfbd92faa273db0db2f34eb28e8 (patch)
treeebac5f98d96e5c8236db012ecda92f5b5d0325ef /drivers/net/wan/hdlc_ppp.c
parente1f024eb5d88e5b4f8e58e99c95082c342f70a1a (diff)
WAN: new synchronous PPP implementation for generic HDLC.
Signed-off-by: Krzysztof Hałasa <khc@pm.waw.pl>
Diffstat (limited to 'drivers/net/wan/hdlc_ppp.c')
-rw-r--r--drivers/net/wan/hdlc_ppp.c648
1 files changed, 601 insertions, 47 deletions
diff --git a/drivers/net/wan/hdlc_ppp.c b/drivers/net/wan/hdlc_ppp.c
index 4efe9e6d32d5..72fae217f1c4 100644
--- a/drivers/net/wan/hdlc_ppp.c
+++ b/drivers/net/wan/hdlc_ppp.c
@@ -2,7 +2,7 @@
2 * Generic HDLC support routines for Linux 2 * Generic HDLC support routines for Linux
3 * Point-to-point protocol support 3 * Point-to-point protocol support
4 * 4 *
5 * Copyright (C) 1999 - 2006 Krzysztof Halasa <khc@pm.waw.pl> 5 * Copyright (C) 1999 - 2008 Krzysztof Halasa <khc@pm.waw.pl>
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify it 7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of version 2 of the GNU General Public License 8 * under the terms of version 2 of the GNU General Public License
@@ -18,87 +18,632 @@
18#include <linux/module.h> 18#include <linux/module.h>
19#include <linux/pkt_sched.h> 19#include <linux/pkt_sched.h>
20#include <linux/poll.h> 20#include <linux/poll.h>
21#include <linux/rtnetlink.h>
22#include <linux/skbuff.h> 21#include <linux/skbuff.h>
23#include <linux/slab.h> 22#include <linux/slab.h>
24#include <net/syncppp.h> 23#include <linux/spinlock.h>
24
25#define DEBUG_CP 0 /* also bytes# to dump */
26#define DEBUG_STATE 0
27#define DEBUG_HARD_HEADER 0
28
29#define HDLC_ADDR_ALLSTATIONS 0xFF
30#define HDLC_CTRL_UI 0x03
31
32#define PID_LCP 0xC021
33#define PID_IP 0x0021
34#define PID_IPCP 0x8021
35#define PID_IPV6 0x0057
36#define PID_IPV6CP 0x8057
37
38enum {IDX_LCP = 0, IDX_IPCP, IDX_IPV6CP, IDX_COUNT};
39enum {CP_CONF_REQ = 1, CP_CONF_ACK, CP_CONF_NAK, CP_CONF_REJ, CP_TERM_REQ,
40 CP_TERM_ACK, CP_CODE_REJ, LCP_PROTO_REJ, LCP_ECHO_REQ, LCP_ECHO_REPLY,
41 LCP_DISC_REQ, CP_CODES};
42#if DEBUG_CP
43static const char *const code_names[CP_CODES] = {
44 "0", "ConfReq", "ConfAck", "ConfNak", "ConfRej", "TermReq",
45 "TermAck", "CodeRej", "ProtoRej", "EchoReq", "EchoReply", "Discard"
46};
47static char debug_buffer[64 + 3 * DEBUG_CP];
48#endif
49
50enum {LCP_OPTION_MRU = 1, LCP_OPTION_ACCM, LCP_OPTION_MAGIC = 5};
51
52struct hdlc_header {
53 u8 address;
54 u8 control;
55 __be16 protocol;
56};
57
58struct cp_header {
59 u8 code;
60 u8 id;
61 __be16 len;
62};
63
25 64
26struct ppp_state { 65struct proto {
27 struct ppp_device pppdev; 66 struct net_device *dev;
28 struct ppp_device *syncppp_ptr; 67 struct timer_list timer;
29 int (*old_change_mtu)(struct net_device *dev, int new_mtu); 68 unsigned long timeout;
69 u16 pid; /* protocol ID */
70 u8 state;
71 u8 cr_id; /* ID of last Configuration-Request */
72 u8 restart_counter;
30}; 73};
31 74
75struct ppp {
76 struct proto protos[IDX_COUNT];
77 spinlock_t lock;
78 unsigned long last_pong;
79 unsigned int req_timeout, cr_retries, term_retries;
80 unsigned int keepalive_interval, keepalive_timeout;
81 u8 seq; /* local sequence number for requests */
82 u8 echo_id; /* ID of last Echo-Request (LCP) */
83};
84
85enum {CLOSED = 0, STOPPED, STOPPING, REQ_SENT, ACK_RECV, ACK_SENT, OPENED,
86 STATES, STATE_MASK = 0xF};
87enum {START = 0, STOP, TO_GOOD, TO_BAD, RCR_GOOD, RCR_BAD, RCA, RCN, RTR, RTA,
88 RUC, RXJ_GOOD, RXJ_BAD, EVENTS};
89enum {INV = 0x10, IRC = 0x20, ZRC = 0x40, SCR = 0x80, SCA = 0x100,
90 SCN = 0x200, STR = 0x400, STA = 0x800, SCJ = 0x1000};
91
92#if DEBUG_STATE
93static const char *const state_names[STATES] = {
94 "Closed", "Stopped", "Stopping", "ReqSent", "AckRecv", "AckSent",
95 "Opened"
96};
97static const char *const event_names[EVENTS] = {
98 "Start", "Stop", "TO+", "TO-", "RCR+", "RCR-", "RCA", "RCN",
99 "RTR", "RTA", "RUC", "RXJ+", "RXJ-"
100};
101#endif
102
103static struct sk_buff_head tx_queue; /* used when holding the spin lock */
104
32static int ppp_ioctl(struct net_device *dev, struct ifreq *ifr); 105static int ppp_ioctl(struct net_device *dev, struct ifreq *ifr);
33 106
107static inline struct ppp* get_ppp(struct net_device *dev)
108{
109 return (struct ppp *)dev_to_hdlc(dev)->state;
110}
34 111
35static inline struct ppp_state* state(hdlc_device *hdlc) 112static inline struct proto* get_proto(struct net_device *dev, u16 pid)
36{ 113{
37 return(struct ppp_state *)(hdlc->state); 114 struct ppp *ppp = get_ppp(dev);
115
116 switch (pid) {
117 case PID_LCP:
118 return &ppp->protos[IDX_LCP];
119 case PID_IPCP:
120 return &ppp->protos[IDX_IPCP];
121 case PID_IPV6CP:
122 return &ppp->protos[IDX_IPV6CP];
123 default:
124 return NULL;
125 }
38} 126}
39 127
128static inline const char* proto_name(u16 pid)
129{
130 switch (pid) {
131 case PID_LCP:
132 return "LCP";
133 case PID_IPCP:
134 return "IPCP";
135 case PID_IPV6CP:
136 return "IPV6CP";
137 default:
138 return NULL;
139 }
140}
40 141
41static int ppp_open(struct net_device *dev) 142static __be16 ppp_type_trans(struct sk_buff *skb, struct net_device *dev)
42{ 143{
43 hdlc_device *hdlc = dev_to_hdlc(dev); 144 struct hdlc_header *data = (struct hdlc_header*)skb->data;
44 int (*old_ioctl)(struct net_device *, struct ifreq *, int); 145
45 int result; 146 if (skb->len < sizeof(struct hdlc_header))
147 return htons(ETH_P_HDLC);
148 if (data->address != HDLC_ADDR_ALLSTATIONS ||
149 data->control != HDLC_CTRL_UI)
150 return htons(ETH_P_HDLC);
151
152 switch (data->protocol) {
153 case __constant_htons(PID_IP):
154 skb_pull(skb, sizeof(struct hdlc_header));
155 return htons(ETH_P_IP);
46 156
47 dev->ml_priv = &state(hdlc)->syncppp_ptr; 157 case __constant_htons(PID_IPV6):
48 state(hdlc)->syncppp_ptr = &state(hdlc)->pppdev; 158 skb_pull(skb, sizeof(struct hdlc_header));
49 state(hdlc)->pppdev.dev = dev; 159 return htons(ETH_P_IPV6);
50 160
51 old_ioctl = dev->do_ioctl; 161 default:
52 state(hdlc)->old_change_mtu = dev->change_mtu; 162 return htons(ETH_P_HDLC);
53 sppp_attach(&state(hdlc)->pppdev);
54 /* sppp_attach nukes them. We don't need syncppp's ioctl */
55 dev->do_ioctl = old_ioctl;
56 state(hdlc)->pppdev.sppp.pp_flags &= ~PP_CISCO;
57 dev->type = ARPHRD_PPP;
58 result = sppp_open(dev);
59 if (result) {
60 sppp_detach(dev);
61 return result;
62 } 163 }
164}
63 165
64 return 0; 166
167static int ppp_hard_header(struct sk_buff *skb, struct net_device *dev,
168 u16 type, const void *daddr, const void *saddr,
169 unsigned int len)
170{
171 struct hdlc_header *data;
172#if DEBUG_HARD_HEADER
173 printk(KERN_DEBUG "%s: ppp_hard_header() called\n", dev->name);
174#endif
175
176 skb_push(skb, sizeof(struct hdlc_header));
177 data = (struct hdlc_header*)skb->data;
178
179 data->address = HDLC_ADDR_ALLSTATIONS;
180 data->control = HDLC_CTRL_UI;
181 switch (type) {
182 case ETH_P_IP:
183 data->protocol = htons(PID_IP);
184 break;
185 case ETH_P_IPV6:
186 data->protocol = htons(PID_IPV6);
187 break;
188 case PID_LCP:
189 case PID_IPCP:
190 case PID_IPV6CP:
191 data->protocol = htons(type);
192 break;
193 default: /* unknown protocol */
194 data->protocol = 0;
195 }
196 return sizeof(struct hdlc_header);
65} 197}
66 198
67 199
200static void ppp_tx_flush(void)
201{
202 struct sk_buff *skb;
203 while ((skb = skb_dequeue(&tx_queue)) != NULL)
204 dev_queue_xmit(skb);
205}
68 206
69static void ppp_close(struct net_device *dev) 207static void ppp_tx_cp(struct net_device *dev, u16 pid, u8 code,
208 u8 id, unsigned int len, const void *data)
70{ 209{
71 hdlc_device *hdlc = dev_to_hdlc(dev); 210 struct sk_buff *skb;
211 struct cp_header *cp;
212 unsigned int magic_len = 0;
213 static u32 magic;
214
215#if DEBUG_CP
216 int i;
217 char *ptr;
218#endif
219
220 if (pid == PID_LCP && (code == LCP_ECHO_REQ || code == LCP_ECHO_REPLY))
221 magic_len = sizeof(magic);
222
223 skb = dev_alloc_skb(sizeof(struct hdlc_header) +
224 sizeof(struct cp_header) + magic_len + len);
225 if (!skb) {
226 printk(KERN_WARNING "%s: out of memory in ppp_tx_cp()\n",
227 dev->name);
228 return;
229 }
230 skb_reserve(skb, sizeof(struct hdlc_header));
231
232 cp = (struct cp_header *)skb_put(skb, sizeof(struct cp_header));
233 cp->code = code;
234 cp->id = id;
235 cp->len = htons(sizeof(struct cp_header) + magic_len + len);
236
237 if (magic_len)
238 memcpy(skb_put(skb, magic_len), &magic, magic_len);
239 if (len)
240 memcpy(skb_put(skb, len), data, len);
241
242#if DEBUG_CP
243 BUG_ON(code >= CP_CODES);
244 ptr = debug_buffer;
245 *ptr = '\x0';
246 for (i = 0; i < min_t(unsigned int, magic_len + len, DEBUG_CP); i++) {
247 sprintf(ptr, " %02X", skb->data[sizeof(struct cp_header) + i]);
248 ptr += strlen(ptr);
249 }
250 printk(KERN_DEBUG "%s: TX %s [%s id 0x%X]%s\n", dev->name,
251 proto_name(pid), code_names[code], id, debug_buffer);
252#endif
72 253
73 sppp_close(dev); 254 ppp_hard_header(skb, dev, pid, NULL, NULL, 0);
74 sppp_detach(dev);
75 255
76 dev->change_mtu = state(hdlc)->old_change_mtu; 256 skb->priority = TC_PRIO_CONTROL;
77 dev->mtu = HDLC_MAX_MTU; 257 skb->dev = dev;
78 dev->hard_header_len = 16; 258 skb_reset_network_header(skb);
259 skb_queue_tail(&tx_queue, skb);
79} 260}
80 261
81 262
263/* State transition table (compare STD-51)
264 Events Actions
265 TO+ = Timeout with counter > 0 irc = Initialize-Restart-Count
266 TO- = Timeout with counter expired zrc = Zero-Restart-Count
267
268 RCR+ = Receive-Configure-Request (Good) scr = Send-Configure-Request
269 RCR- = Receive-Configure-Request (Bad)
270 RCA = Receive-Configure-Ack sca = Send-Configure-Ack
271 RCN = Receive-Configure-Nak/Rej scn = Send-Configure-Nak/Rej
272
273 RTR = Receive-Terminate-Request str = Send-Terminate-Request
274 RTA = Receive-Terminate-Ack sta = Send-Terminate-Ack
275
276 RUC = Receive-Unknown-Code scj = Send-Code-Reject
277 RXJ+ = Receive-Code-Reject (permitted)
278 or Receive-Protocol-Reject
279 RXJ- = Receive-Code-Reject (catastrophic)
280 or Receive-Protocol-Reject
281*/
282static int cp_table[EVENTS][STATES] = {
283 /* CLOSED STOPPED STOPPING REQ_SENT ACK_RECV ACK_SENT OPENED
284 0 1 2 3 4 5 6 */
285 {IRC|SCR|3, INV , INV , INV , INV , INV , INV }, /* START */
286 { INV , 0 , 0 , 0 , 0 , 0 , 0 }, /* STOP */
287 { INV , INV ,STR|2, SCR|3 ,SCR|3, SCR|5 , INV }, /* TO+ */
288 { INV , INV , 1 , 1 , 1 , 1 , INV }, /* TO- */
289 { STA|0 ,IRC|SCR|SCA|5, 2 , SCA|5 ,SCA|6, SCA|5 ,SCR|SCA|5}, /* RCR+ */
290 { STA|0 ,IRC|SCR|SCN|3, 2 , SCN|3 ,SCN|4, SCN|3 ,SCR|SCN|3}, /* RCR- */
291 { STA|0 , STA|1 , 2 , IRC|4 ,SCR|3, 6 , SCR|3 }, /* RCA */
292 { STA|0 , STA|1 , 2 ,IRC|SCR|3,SCR|3,IRC|SCR|5, SCR|3 }, /* RCN */
293 { STA|0 , STA|1 ,STA|2, STA|3 ,STA|3, STA|3 ,ZRC|STA|2}, /* RTR */
294 { 0 , 1 , 1 , 3 , 3 , 5 , SCR|3 }, /* RTA */
295 { SCJ|0 , SCJ|1 ,SCJ|2, SCJ|3 ,SCJ|4, SCJ|5 , SCJ|6 }, /* RUC */
296 { 0 , 1 , 2 , 3 , 3 , 5 , 6 }, /* RXJ+ */
297 { 0 , 1 , 1 , 1 , 1 , 1 ,IRC|STR|2}, /* RXJ- */
298};
299
82 300
83static __be16 ppp_type_trans(struct sk_buff *skb, struct net_device *dev) 301/* SCA: RCR+ must supply id, len and data
302 SCN: RCR- must supply code, id, len and data
303 STA: RTR must supply id
304 SCJ: RUC must supply CP packet len and data */
305static void ppp_cp_event(struct net_device *dev, u16 pid, u16 event, u8 code,
306 u8 id, unsigned int len, void *data)
84{ 307{
85 return __constant_htons(ETH_P_WAN_PPP); 308 int old_state, action;
309 struct ppp *ppp = get_ppp(dev);
310 struct proto *proto = get_proto(dev, pid);
311
312 old_state = proto->state;
313 BUG_ON(old_state >= STATES);
314 BUG_ON(event >= EVENTS);
315
316#if DEBUG_STATE
317 printk(KERN_DEBUG "%s: %s ppp_cp_event(%s) %s ...\n", dev->name,
318 proto_name(pid), event_names[event], state_names[proto->state]);
319#endif
320
321 action = cp_table[event][old_state];
322
323 proto->state = action & STATE_MASK;
324 if (action & (SCR | STR)) /* set Configure-Req/Terminate-Req timer */
325 mod_timer(&proto->timer, proto->timeout =
326 jiffies + ppp->req_timeout * HZ);
327 if (action & ZRC)
328 proto->restart_counter = 0;
329 if (action & IRC)
330 proto->restart_counter = (proto->state == STOPPING) ?
331 ppp->term_retries : ppp->cr_retries;
332
333 if (action & SCR) /* send Configure-Request */
334 ppp_tx_cp(dev, pid, CP_CONF_REQ, proto->cr_id = ++ppp->seq,
335 0, NULL);
336 if (action & SCA) /* send Configure-Ack */
337 ppp_tx_cp(dev, pid, CP_CONF_ACK, id, len, data);
338 if (action & SCN) /* send Configure-Nak/Reject */
339 ppp_tx_cp(dev, pid, code, id, len, data);
340 if (action & STR) /* send Terminate-Request */
341 ppp_tx_cp(dev, pid, CP_TERM_REQ, ++ppp->seq, 0, NULL);
342 if (action & STA) /* send Terminate-Ack */
343 ppp_tx_cp(dev, pid, CP_TERM_ACK, id, 0, NULL);
344 if (action & SCJ) /* send Code-Reject */
345 ppp_tx_cp(dev, pid, CP_CODE_REJ, ++ppp->seq, len, data);
346
347 if (old_state != OPENED && proto->state == OPENED) {
348 printk(KERN_INFO "%s: %s up\n", dev->name, proto_name(pid));
349 if (pid == PID_LCP) {
350 netif_dormant_off(dev);
351 ppp_cp_event(dev, PID_IPCP, START, 0, 0, 0, NULL);
352 ppp_cp_event(dev, PID_IPV6CP, START, 0, 0, 0, NULL);
353 ppp->last_pong = jiffies;
354 mod_timer(&proto->timer, proto->timeout =
355 jiffies + ppp->keepalive_interval * HZ);
356 }
357 }
358 if (old_state == OPENED && proto->state != OPENED) {
359 printk(KERN_INFO "%s: %s down\n", dev->name, proto_name(pid));
360 if (pid == PID_LCP) {
361 netif_dormant_on(dev);
362 ppp_cp_event(dev, PID_IPCP, STOP, 0, 0, 0, NULL);
363 ppp_cp_event(dev, PID_IPV6CP, STOP, 0, 0, 0, NULL);
364 }
365 }
366 if (old_state != CLOSED && proto->state == CLOSED)
367 del_timer(&proto->timer);
368
369#if DEBUG_STATE
370 printk(KERN_DEBUG "%s: %s ppp_cp_event(%s) ... %s\n", dev->name,
371 proto_name(pid), event_names[event], state_names[proto->state]);
372#endif
86} 373}
87 374
88 375
376static void ppp_cp_parse_cr(struct net_device *dev, u16 pid, u8 id,
377 unsigned int len, u8 *data)
378{
379 static u8 const valid_accm[6] = { LCP_OPTION_ACCM, 6, 0, 0, 0, 0 };
380 u8 *opt, *out;
381 unsigned int nak_len = 0, rej_len = 0;
382
383 if (!(out = kmalloc(len, GFP_ATOMIC))) {
384 dev->stats.rx_dropped++;
385 return; /* out of memory, ignore CR packet */
386 }
387
388 for (opt = data; len; len -= opt[1], opt += opt[1]) {
389 if (len < 2 || len < opt[1]) {
390 dev->stats.rx_errors++;
391 return; /* bad packet, drop silently */
392 }
393
394 if (pid == PID_LCP)
395 switch (opt[0]) {
396 case LCP_OPTION_MRU:
397 continue; /* MRU always OK and > 1500 bytes? */
398
399 case LCP_OPTION_ACCM: /* async control character map */
400 if (!memcmp(opt, valid_accm,
401 sizeof(valid_accm)))
402 continue;
403 if (!rej_len) { /* NAK it */
404 memcpy(out + nak_len, valid_accm,
405 sizeof(valid_accm));
406 nak_len += sizeof(valid_accm);
407 continue;
408 }
409 break;
410 case LCP_OPTION_MAGIC:
411 if (opt[1] != 6 || (!opt[2] && !opt[3] &&
412 !opt[4] && !opt[5]))
413 break; /* reject invalid magic number */
414 continue;
415 }
416 /* reject this option */
417 memcpy(out + rej_len, opt, opt[1]);
418 rej_len += opt[1];
419 }
420
421 if (rej_len)
422 ppp_cp_event(dev, pid, RCR_BAD, CP_CONF_REJ, id, rej_len, out);
423 else if (nak_len)
424 ppp_cp_event(dev, pid, RCR_BAD, CP_CONF_NAK, id, nak_len, out);
425 else
426 ppp_cp_event(dev, pid, RCR_GOOD, CP_CONF_ACK, id, len, data);
427
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 = (struct cp_header*)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 goto out;
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 goto out;
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 goto out;
529
530 case CP_TERM_REQ:
531 ppp_cp_event(dev, pid, RTR, 0, cp->id, 0, NULL);
532 goto out;
533
534 case CP_TERM_ACK:
535 ppp_cp_event(dev, pid, RTA, 0, 0, 0, NULL);
536 goto out;
537
538 case CP_CODE_REJ:
539 ppp_cp_event(dev, pid, RXJ_BAD, 0, 0, 0, NULL);
540 goto out;
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 goto out;
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
560
561static void ppp_timer(unsigned long arg)
562{
563 struct proto *proto = (struct proto *)arg;
564 struct ppp *ppp = get_ppp(proto->dev);
565 unsigned long flags;
566
567 spin_lock_irqsave(&ppp->lock, flags);
568 switch (proto->state) {
569 case STOPPING:
570 case REQ_SENT:
571 case ACK_RECV:
572 case ACK_SENT:
573 if (proto->restart_counter) {
574 ppp_cp_event(proto->dev, proto->pid, TO_GOOD, 0, 0,
575 0, NULL);
576 proto->restart_counter--;
577 } else
578 ppp_cp_event(proto->dev, proto->pid, TO_BAD, 0, 0,
579 0, NULL);
580 break;
581
582 case OPENED:
583 if (proto->pid != PID_LCP)
584 break;
585 if (time_after(jiffies, ppp->last_pong +
586 ppp->keepalive_timeout * HZ)) {
587 printk(KERN_INFO "%s: Link down\n", proto->dev->name);
588 ppp_cp_event(proto->dev, PID_LCP, STOP, 0, 0, 0, NULL);
589 ppp_cp_event(proto->dev, PID_LCP, START, 0, 0, 0, NULL);
590 } else { /* send keep-alive packet */
591 ppp->echo_id = ++ppp->seq;
592 ppp_tx_cp(proto->dev, PID_LCP, LCP_ECHO_REQ,
593 ppp->echo_id, 0, NULL);
594 proto->timer.expires = jiffies +
595 ppp->keepalive_interval * HZ;
596 add_timer(&proto->timer);
597 }
598 break;
599 }
600 spin_unlock_irqrestore(&ppp->lock, flags);
601 ppp_tx_flush();
602}
603
604
605static void ppp_start(struct net_device *dev)
606{
607 struct ppp *ppp = get_ppp(dev);
608 int i;
609
610 for (i = 0; i < IDX_COUNT; i++) {
611 struct proto *proto = &ppp->protos[i];
612 proto->dev = dev;
613 init_timer(&proto->timer);
614 proto->timer.function = ppp_timer;
615 proto->timer.data = (unsigned long)proto;
616 proto->state = CLOSED;
617 }
618 ppp->protos[IDX_LCP].pid = PID_LCP;
619 ppp->protos[IDX_IPCP].pid = PID_IPCP;
620 ppp->protos[IDX_IPV6CP].pid = PID_IPV6CP;
621
622 ppp_cp_event(dev, PID_LCP, START, 0, 0, 0, NULL);
623}
624
625static void ppp_stop(struct net_device *dev)
626{
627 ppp_cp_event(dev, PID_LCP, STOP, 0, 0, 0, NULL);
628}
89 629
90static struct hdlc_proto proto = { 630static struct hdlc_proto proto = {
91 .open = ppp_open, 631 .start = ppp_start,
92 .close = ppp_close, 632 .stop = ppp_stop,
93 .type_trans = ppp_type_trans, 633 .type_trans = ppp_type_trans,
94 .ioctl = ppp_ioctl, 634 .ioctl = ppp_ioctl,
635 .netif_rx = ppp_rx,
95 .module = THIS_MODULE, 636 .module = THIS_MODULE,
96}; 637};
97 638
639static const struct header_ops ppp_header_ops = {
640 .create = ppp_hard_header,
641};
98 642
99static int ppp_ioctl(struct net_device *dev, struct ifreq *ifr) 643static int ppp_ioctl(struct net_device *dev, struct ifreq *ifr)
100{ 644{
101 hdlc_device *hdlc = dev_to_hdlc(dev); 645 hdlc_device *hdlc = dev_to_hdlc(dev);
646 struct ppp *ppp;
102 int result; 647 int result;
103 648
104 switch (ifr->ifr_settings.type) { 649 switch (ifr->ifr_settings.type) {
@@ -109,25 +654,35 @@ static int ppp_ioctl(struct net_device *dev, struct ifreq *ifr)
109 return 0; /* return protocol only, no settable parameters */ 654 return 0; /* return protocol only, no settable parameters */
110 655
111 case IF_PROTO_PPP: 656 case IF_PROTO_PPP:
112 if(!capable(CAP_NET_ADMIN)) 657 if (!capable(CAP_NET_ADMIN))
113 return -EPERM; 658 return -EPERM;
114 659
115 if(dev->flags & IFF_UP) 660 if (dev->flags & IFF_UP)
116 return -EBUSY; 661 return -EBUSY;
117 662
118 /* no settable parameters */ 663 /* no settable parameters */
119 664
120 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT); 665 result = hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
121 if (result) 666 if (result)
122 return result; 667 return result;
123 668
124 result = attach_hdlc_protocol(dev, &proto, 669 result = attach_hdlc_protocol(dev, &proto, sizeof(struct ppp));
125 sizeof(struct ppp_state));
126 if (result) 670 if (result)
127 return result; 671 return result;
672
673 ppp = get_ppp(dev);
674 spin_lock_init(&ppp->lock);
675 ppp->req_timeout = 2;
676 ppp->cr_retries = 10;
677 ppp->term_retries = 2;
678 ppp->keepalive_interval = 10;
679 ppp->keepalive_timeout = 60;
680
128 dev->hard_start_xmit = hdlc->xmit; 681 dev->hard_start_xmit = hdlc->xmit;
682 dev->hard_header_len = sizeof(struct hdlc_header);
683 dev->header_ops = &ppp_header_ops;
129 dev->type = ARPHRD_PPP; 684 dev->type = ARPHRD_PPP;
130 netif_dormant_off(dev); 685 netif_dormant_on(dev);
131 return 0; 686 return 0;
132 } 687 }
133 688
@@ -137,12 +692,11 @@ static int ppp_ioctl(struct net_device *dev, struct ifreq *ifr)
137 692
138static int __init mod_init(void) 693static int __init mod_init(void)
139{ 694{
695 skb_queue_head_init(&tx_queue);
140 register_hdlc_protocol(&proto); 696 register_hdlc_protocol(&proto);
141 return 0; 697 return 0;
142} 698}
143 699
144
145
146static void __exit mod_exit(void) 700static void __exit mod_exit(void)
147{ 701{
148 unregister_hdlc_protocol(&proto); 702 unregister_hdlc_protocol(&proto);