This source file includes following definitions.
- ppp_init
- ppp_init_ctrl_blk
- ppp_changedmtu
- ppp_release
- ppp_close
- ppp_open
- ppp_dev_open
- ppp_dev_close
- ppp_output_done
- ppp_kick_tty
- ppp_kick_tty
- ppp_write_wakeup
- ppp_enqueue
- ppp_dump_inqueue
- ppp_tty_input_ready
- ppp_unesc
- ppp_receive_buf
- ppp_doframe
- ppp_do_ip
- ppp_us_queue
- ppp_read
- ppp_stuff_char
- ppp_write
- ppp_ioctl
- ppp_select
- ppp_xmit
- ppp_type_trans
- ppp_header
- ppp_rebuild_header
- ppp_add_arp
- ppp_header
- ppp_rebuild_header
- ppp_get_stats
- ppp_find
- ppp_alloc
- ppp_lock
- ppp_unlock
- ppp_add_fcs
- ppp_check_fcs
- ppp_print_hex
- ppp_print_char
- ppp_print_buffer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 #define NEW_TTY_DRIVERS
33 #define OPTIMIZE_FLAG_TIME ((HZ * 3)/2)
34
35 #include <linux/kernel.h>
36 #include <linux/sched.h>
37 #include <linux/types.h>
38 #include <linux/fcntl.h>
39 #include <linux/interrupt.h>
40 #include <linux/ptrace.h>
41 #include <linux/ioport.h>
42 #include <linux/in.h>
43 #include <linux/malloc.h>
44 #include <linux/tty.h>
45 #include <linux/errno.h>
46 #include <linux/sched.h>
47 #include <linux/string.h>
48 #include <linux/signal.h>
49 #include <asm/system.h>
50 #include <asm/bitops.h>
51 #include <asm/segment.h>
52
53 #ifdef NET02D
54 #include <dev.h>
55 #include <skbuff.h>
56 #include <inet.h>
57 #define skb_queue_head_init(buf) *(buf) = NULL
58 #else
59 #include <linux/netdevice.h>
60 #include <linux/skbuff.h>
61 #include <linux/inet.h>
62 #endif
63
64 #include <linux/ppp.h>
65
66 #include <ip.h>
67 #include <tcp.h>
68
69 #include "slhc.h"
70
71 #include <linux/if_arp.h>
72 #ifndef ARPHRD_PPP
73 #define ARPHRD_PPP 0
74 #endif
75
76 #define PRINTK(p) printk p ;
77 #define ASSERT(p) if (!p) PRINTK ((KERN_CRIT "assertion failed: " # p))
78 #define PRINTKN(n,p) {if (ppp_debug >= n) PRINTK (p)}
79 #define CHECK_PPP(a) if (!ppp->inuse) { PRINTK ((ppp_warning, __LINE__)) return a;}
80 #define CHECK_PPP_VOID() if (!ppp->inuse) { PRINTK ((ppp_warning, __LINE__)) return;}
81
82 #define in_xmap(ppp,c) (ppp->xmit_async_map[(c) >> 5] & (1 << ((c) & 0x1f)))
83 #define in_rmap(ppp,c) ((((unsigned int) (unsigned char) (c)) < 0x20) && \
84 ppp->recv_async_map & (1 << (c)))
85
86 #define bset(p,b) ((p)[(b) >> 5] |= (1 << ((b) & 0x1f)))
87
88 int ppp_debug = 2;
89 int ppp_debug_netpackets = 0;
90
91
92 static char ppp_warning[] = KERN_WARNING "PPP: ALERT! not INUSE! %d\n";
93
94 int ppp_init(struct device *);
95 static void ppp_init_ctrl_blk(struct ppp *);
96 static int ppp_dev_open(struct device *);
97 static int ppp_dev_close(struct device *);
98 static void ppp_kick_tty(struct ppp *);
99
100 #ifdef NEW_TTY_DRIVERS
101 #define ppp_find(tty) ((struct ppp *) tty->disc_data)
102 #else
103 static void ppp_output_done(void *);
104 static void ppp_unesc(struct ppp *ppp, unsigned char *c, int n);
105 static struct ppp *ppp_find(struct tty_struct *);
106 #endif
107
108 static void ppp_doframe(struct ppp *);
109 static int ppp_do_ip(struct ppp *, unsigned short, unsigned char *, int);
110 static int ppp_us_queue(struct ppp *, unsigned short, unsigned char *, int);
111 static int ppp_xmit(struct sk_buff *, struct device *);
112 static unsigned short ppp_type_trans(struct sk_buff *, struct device *);
113
114 #ifdef NET02D
115 static int ppp_header(unsigned char *buff, struct device *dev,
116 unsigned short type, unsigned long daddr,
117 unsigned long saddr, unsigned len);
118 static int ppp_rebuild_header(void *buff, struct device *dev);
119 static void ppp_add_arp(unsigned long addr, struct sk_buff *skb,
120 struct device *dev);
121 #else
122 static int ppp_header(unsigned char *, struct device *, unsigned short,
123 void *, void *, unsigned, struct sk_buff *);
124 static int ppp_rebuild_header(void *, struct device *, unsigned long,
125 struct sk_buff *);
126 #endif
127
128 static struct enet_statistics *ppp_get_stats (struct device *);
129 static struct ppp *ppp_alloc(void);
130 static int ppp_lock(struct ppp *);
131 static void ppp_unlock(struct ppp *);
132 static void ppp_add_fcs(struct ppp *);
133 static int ppp_check_fcs(struct ppp *);
134 static void ppp_print_buffer(const char *,char *,int,int);
135
136 static int ppp_read(struct tty_struct *, struct file *, unsigned char *,
137 unsigned int);
138 static int ppp_write(struct tty_struct *, struct file *, unsigned char *,
139 unsigned int);
140 static int ppp_ioctl(struct tty_struct *, struct file *, unsigned int,
141 unsigned long);
142 static int ppp_select(struct tty_struct *tty, struct inode * inode,
143 struct file * filp, int sel_type, select_table * wait);
144 static int ppp_open(struct tty_struct *);
145 static void ppp_close(struct tty_struct *);
146
147 #ifdef NEW_TTY_DRIVERS
148 static void ppp_receive_buf(struct tty_struct *tty, unsigned char *cp,
149 char *fp, int count);
150 static void ppp_write_wakeup(struct tty_struct *tty);
151 #else
152 static void ppp_tty_input_ready(struct tty_struct *);
153 #endif
154
155
156
157 static unsigned short fcstab[256] = {
158 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
159 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
160 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
161 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
162 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
163 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
164 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
165 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
166 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
167 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
168 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
169 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
170 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
171 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
172 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
173 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
174 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
175 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
176 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
177 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
178 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
179 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
180 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
181 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
182 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
183 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
184 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
185 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
186 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
187 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
188 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
189 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
190 };
191
192 struct tty_ldisc ppp_ldisc;
193
194 static struct ppp ppp_ctrl[PPP_NRUNIT];
195
196
197
198
199
200 static int first_time = 1;
201
202
203
204 int
205 ppp_init(struct device *dev)
206 {
207 struct ppp *ppp;
208 int i;
209
210 ppp = &ppp_ctrl[dev->base_addr];
211
212 if (first_time) {
213 first_time = 0;
214
215 printk (KERN_INFO "PPP: version %s (%d channels)"
216 #ifdef NET02D
217 " NET02D"
218 #endif
219 #ifdef NEW_TTY_DRIVERS
220 " NEW_TTY_DRIVERS"
221 #endif
222 #ifdef OPTIMIZE_FLAG_TIME
223 " OPTIMIZE_FLAGS"
224 #endif
225 "\n", PPP_VERSION, PPP_NRUNIT);
226
227 printk (KERN_INFO
228 "TCP compression code copyright 1989 Regents of the "
229 "University of California\n");
230
231 (void) memset(&ppp_ldisc, 0, sizeof(ppp_ldisc));
232 ppp_ldisc.open = ppp_open;
233 ppp_ldisc.close = ppp_close;
234 ppp_ldisc.read = ppp_read;
235 ppp_ldisc.write = ppp_write;
236 ppp_ldisc.ioctl = ppp_ioctl;
237 ppp_ldisc.select = ppp_select;
238
239 #ifdef NEW_TTY_DRIVERS
240 ppp_ldisc.magic = TTY_LDISC_MAGIC;
241 ppp_ldisc.receive_buf = ppp_receive_buf;
242 ppp_ldisc.write_wakeup = ppp_write_wakeup;
243 #else
244 ppp_ldisc.handler = ppp_tty_input_ready;
245 #endif
246
247 if ((i = tty_register_ldisc(N_PPP, &ppp_ldisc)) == 0)
248 printk(KERN_INFO "PPP line discipline registered.\n");
249 else
250 printk(KERN_ERR "error registering line discipline: %d\n", i);
251 }
252
253
254 ppp_init_ctrl_blk (ppp);
255 ppp->inuse = 0;
256 ppp->line = dev->base_addr;
257 ppp->tty = NULL;
258 ppp->dev = dev;
259
260
261 memset (&ppp->stats, '\0', sizeof (struct ppp_stats));
262
263
264 dev->mtu = PPP_MTU;
265 dev->hard_start_xmit = ppp_xmit;
266 dev->open = ppp_dev_open;
267 dev->stop = ppp_dev_close;
268 dev->get_stats = ppp_get_stats;
269 dev->hard_header = ppp_header;
270 dev->type_trans = ppp_type_trans;
271 dev->rebuild_header = ppp_rebuild_header;
272 dev->hard_header_len = 0;
273 dev->addr_len = 0;
274 dev->type = ARPHRD_PPP;
275
276 #ifdef NET02D
277 dev->add_arp = ppp_add_arp;
278 dev->queue_xmit = dev_queue_xmit;
279 #endif
280
281 for (i = 0; i < DEV_NUMBUFFS; i++)
282 skb_queue_head_init(&dev->buffs[i]);
283
284
285 dev->flags = IFF_POINTOPOINT;
286 dev->family = AF_INET;
287 dev->pa_addr = 0;
288 dev->pa_brdaddr = 0;
289 dev->pa_mask = 0;
290 dev->pa_alen = sizeof(unsigned long);
291
292 return 0;
293 }
294
295 static void
296 ppp_init_ctrl_blk(struct ppp *ppp)
297 {
298 ppp->magic = PPP_MAGIC;
299 ppp->sending = 0;
300 ppp->toss = 0xFE;
301 ppp->escape = 0;
302
303 ppp->flags = 0;
304 ppp->mtu = PPP_MTU;
305 ppp->mru = PPP_MRU;
306 ppp->fcs = 0;
307
308 memset (ppp->xmit_async_map, 0, sizeof (ppp->xmit_async_map));
309 ppp->xmit_async_map[0] = 0xffffffff;
310 ppp->xmit_async_map[3] = 0x60000000;
311 ppp->recv_async_map = 0x00000000;
312
313 ppp->slcomp = NULL;
314 ppp->rbuff = NULL;
315 ppp->xbuff = NULL;
316 ppp->cbuff = NULL;
317
318 ppp->rhead = NULL;
319 ppp->rend = NULL;
320 ppp->rcount = 0;
321 ppp->xhead = NULL;
322 ppp->xtail = NULL;
323
324 ppp->us_rbuff = NULL;
325 ppp->us_rbuff_end = NULL;
326 ppp->us_rbuff_head = NULL;
327 ppp->us_rbuff_tail = NULL;
328 ppp->read_wait = NULL;
329 ppp->write_wait = NULL;
330 ppp->us_rbuff_lock = 0;
331 ppp->inp_sig = 0;
332 ppp->inp_sig_pid = 0;
333
334 #ifdef OPTIMIZE_FLAG_TIME
335 ppp->last_xmit = jiffies - OPTIMIZE_FLAG_TIME;
336 #else
337 ppp->last_xmit = 0;
338 #endif
339
340
341 memset (&ppp->stats, '\0', sizeof (struct ppp_stats));
342
343
344 ppp->ddinfo.ip_sjiffies =
345 ppp->ddinfo.ip_rjiffies =
346 ppp->ddinfo.nip_sjiffies =
347 ppp->ddinfo.nip_rjiffies = jiffies;
348 }
349
350
351
352
353
354
355
356 static void
357 ppp_changedmtu (struct ppp *ppp, int new_mtu, int new_mru)
358 {
359 struct device *dev;
360 unsigned char *new_rbuff, *new_xbuff, *new_cbuff;
361 unsigned char *old_rbuff, *old_xbuff, *old_cbuff;
362 int mtu, mru;
363
364
365
366 dev = ppp->dev;
367 mru = new_mru;
368 mtu = new_mtu;
369
370
371 if (mru < PPP_MRU)
372 mru = PPP_MRU;
373
374 mtu = (mtu * 2) + 20;
375 mru = (mru * 2) + 20;
376
377 PRINTKN (2,(KERN_INFO "ppp: channel %s mtu = %d, mru = %d\n",
378 dev->name, new_mtu, new_mru));
379
380 new_xbuff = (unsigned char *) kmalloc(mtu + 4, GFP_KERNEL);
381 new_rbuff = (unsigned char *) kmalloc(mru + 4, GFP_KERNEL);
382 new_cbuff = (unsigned char *) kmalloc(mru + 4, GFP_KERNEL);
383
384
385
386 if (new_xbuff == NULL || new_rbuff == NULL || new_cbuff == NULL)
387 {
388 PRINTKN (2,(KERN_ERR "ppp: failed to allocate new buffers\n"));
389
390
391
392 if (new_rbuff != NULL)
393 kfree (new_rbuff);
394
395 if (new_xbuff != NULL)
396 kfree (new_xbuff);
397
398 if (new_cbuff != NULL)
399 kfree (new_cbuff);
400 }
401
402
403
404 else
405 {
406 cli();
407 old_xbuff = ppp->xbuff;
408 old_rbuff = ppp->rbuff;
409 old_cbuff = ppp->cbuff;
410
411 ppp->xbuff = new_xbuff;
412 ppp->rbuff = new_rbuff;
413 ppp->cbuff = new_cbuff;
414
415 dev->mem_start = (unsigned long) new_xbuff;
416 dev->mem_end = (unsigned long) (dev->mem_start + mtu);
417
418 dev->rmem_start = (unsigned long) new_rbuff;
419 ppp->rend = (unsigned char *)
420 dev->rmem_end = (unsigned long) (dev->rmem_start + mru);
421
422 ppp->rhead = new_rbuff;
423
424
425
426 ppp->toss = 0xFE;
427 ppp->escape = 0;
428 ppp->sending = 0;
429 ppp->rcount = 0;
430
431 ppp->mru = new_mru;
432
433 ppp->mtu =
434 dev->mtu = new_mtu;
435
436 sti();
437
438
439
440 if (old_rbuff != NULL)
441 kfree (old_rbuff);
442
443 if (old_xbuff != NULL)
444 kfree (old_xbuff);
445
446 if (old_cbuff != NULL)
447 kfree (old_cbuff);
448 }
449 }
450
451
452
453 static void
454 ppp_release(struct ppp *ppp)
455 {
456 #ifdef NEW_TTY_DRIVERS
457 if (ppp->tty != NULL && ppp->tty->disc_data == ppp)
458 ppp->tty->disc_data = NULL;
459 #endif
460
461 if (ppp->dev) {
462 ppp->dev->flags &= ~IFF_UP;
463 ppp->dev->flags |= IFF_POINTOPOINT;
464 }
465
466 kfree (ppp->xbuff);
467 kfree (ppp->cbuff);
468 kfree (ppp->rbuff);
469 kfree (ppp->us_rbuff);
470
471 ppp->xbuff =
472 ppp->cbuff =
473 ppp->rbuff =
474 ppp->us_rbuff = NULL;
475
476 if (ppp->slcomp) {
477 slhc_free(ppp->slcomp);
478 ppp->slcomp = NULL;
479 }
480
481 ppp->inuse = 0;
482 ppp->tty = NULL;
483 }
484
485 static void
486 ppp_close(struct tty_struct *tty)
487 {
488 struct ppp *ppp = ppp_find(tty);
489
490 if (ppp == NULL || ppp->magic != PPP_MAGIC) {
491 PRINTKN (1,(KERN_WARNING "ppp: trying to close unopened tty!\n"));
492 } else {
493 CHECK_PPP_VOID();
494 ppp_release (ppp);
495
496 PRINTKN (2,(KERN_INFO "ppp: channel %s closing.\n", ppp->dev->name));
497 }
498 }
499
500
501 static int
502 ppp_open(struct tty_struct *tty)
503 {
504 struct ppp *ppp = ppp_find(tty);
505
506 if (ppp) {
507 PRINTKN (1,(KERN_ERR "ppp_open: gack! tty already associated to %s!\n",
508 ppp->magic == PPP_MAGIC ? ppp->dev->name : "unknown"));
509 return -EEXIST;
510 }
511
512 ppp = ppp_alloc();
513 if (ppp == NULL) {
514 PRINTKN (1,(KERN_ERR "ppp_open: couldn't allocate ppp channel\n"));
515 return -ENFILE;
516 }
517
518
519 ppp_init_ctrl_blk (ppp);
520
521 ppp->tty = tty;
522
523 #ifdef NEW_TTY_DRIVERS
524 tty->disc_data = ppp;
525 if (tty->driver.flush_buffer)
526 tty->driver.flush_buffer(tty);
527 if (tty->ldisc.flush_buffer)
528 tty->ldisc.flush_buffer(tty);
529 #else
530 tty_read_flush (tty);
531 tty_write_flush (tty);
532 #endif
533
534 if ((ppp->slcomp = slhc_init(16, 16)) == NULL) {
535 PRINTKN (1,(KERN_ERR "ppp: no space for compression buffers!\n"));
536 ppp_release (ppp);
537 return -ENOMEM;
538 }
539
540
541 ppp_changedmtu (ppp, ppp->dev->mtu, ppp->mru);
542 if (ppp->rbuff == NULL) {
543 ppp_release (ppp);
544 return -ENOMEM;
545 }
546
547
548 ppp->us_rbuff = kmalloc (RBUFSIZE, GFP_KERNEL);
549 if (ppp->us_rbuff == NULL) {
550 PRINTKN (1,(KERN_ERR "ppp: no space for user receive buffer\n"));
551 ppp_release (ppp);
552 return -ENOMEM;
553 }
554
555 ppp->us_rbuff_head =
556 ppp->us_rbuff_tail = ppp->us_rbuff;
557 ppp->us_rbuff_end = ppp->us_rbuff + RBUFSIZE;
558
559 PRINTKN (2,(KERN_INFO "ppp: channel %s open\n", ppp->dev->name));
560
561 #ifdef NEW_TTY_DRIVERS
562 return (0);
563 #else
564 return (ppp->line);
565 #endif
566 }
567
568
569
570 static int
571 ppp_dev_open(struct device *dev)
572 {
573 struct ppp *ppp = &ppp_ctrl[dev->base_addr];
574
575
576 dev->flags |= IFF_POINTOPOINT;
577
578 if (ppp->tty == NULL) {
579 PRINTKN (1,(KERN_ERR "ppp: %s not connected to a TTY! can't go open!\n",
580 dev->name));
581 return -ENXIO;
582 }
583
584 PRINTKN (2,(KERN_INFO "ppp: channel %s going up for IP packets!\n",
585 dev->name));
586
587 CHECK_PPP(-ENXIO);
588 return 0;
589 }
590
591 static int
592 ppp_dev_close(struct device *dev)
593 {
594 struct ppp *ppp = &ppp_ctrl[dev->base_addr];
595
596 if (ppp->tty == NULL) {
597 PRINTKN (1,(KERN_ERR "ppp: %s not connected to a TTY! can't go down!\n",
598 dev->name));
599 return -ENXIO;
600 }
601
602 PRINTKN (2,(KERN_INFO "ppp: channel %s going down for IP packets!\n",
603 dev->name));
604 CHECK_PPP(-ENXIO);
605 return 0;
606 }
607
608
609
610
611
612
613
614 #ifdef NEW_TTY_DRIVERS
615 static inline void
616 #else
617 static void
618 #endif
619 ppp_output_done (void *ppp)
620 {
621
622 ppp_unlock ((struct ppp *) ppp);
623
624
625
626 if (((struct ppp *) ppp)->dev->flags & IFF_UP)
627 dev_tint (((struct ppp *) ppp)->dev);
628
629
630 wake_up_interruptible (&((struct ppp *) ppp)->write_wait);
631 }
632
633 #ifndef NEW_TTY_DRIVERS
634 static void
635 ppp_kick_tty (struct ppp *ppp)
636 {
637 register int count = ppp->xhead - ppp->xbuff;
638 register int answer;
639
640 ppp->stats.sbytes += count;
641
642 answer = tty_write_data (ppp->tty,
643 ppp->xbuff,
644 count,
645 ppp_output_done,
646 (void *) ppp);
647
648 if (answer == 0)
649 ppp_output_done (ppp);
650 else
651 if (answer < 0) {
652 ppp->stats.serrors++;
653 ppp_output_done (ppp);
654 }
655 }
656
657 #else
658
659 static void
660 ppp_kick_tty (struct ppp *ppp)
661 {
662 register int count, actual;
663
664 count = ppp->xhead - ppp->xbuff;
665
666 actual = ppp->tty->driver.write(ppp->tty, 0, ppp->xbuff, count);
667 ppp->stats.sbytes += actual;
668 if (actual == count) {
669 ppp_output_done(ppp);
670 } else {
671 ppp->xtail = ppp->xbuff + actual;
672 ppp->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
673 }
674 }
675
676 static void ppp_write_wakeup(struct tty_struct *tty)
677 {
678 register int count, actual;
679 struct ppp *ppp = ppp_find(tty);
680
681 if (!ppp || ppp->magic != PPP_MAGIC) {
682 PRINTKN (1,
683 (KERN_ERR "PPP: write_wakeup called but couldn't "
684 "find PPP struct.\n"));
685 return;
686 }
687
688 if (!ppp->xtail || (ppp->flags & SC_XMIT_BUSY))
689 return;
690
691 cli();
692 if (ppp->flags & SC_XMIT_BUSY)
693 return;
694 ppp->flags |= SC_XMIT_BUSY;
695 sti();
696
697 count = ppp->xhead - ppp->xtail;
698
699 actual = tty->driver.write(tty, 0, ppp->xtail, count);
700 ppp->stats.sbytes += actual;
701 if (actual == count) {
702 ppp->xtail = 0;
703 tty->flags &= ~TTY_DO_WRITE_WAKEUP;
704
705 ppp_output_done(ppp);
706 } else {
707 ppp->xtail += actual;
708 }
709 ppp->flags &= ~SC_XMIT_BUSY;
710 }
711 #endif
712
713
714
715
716
717
718
719
720
721
722
723 inline void
724 ppp_enqueue(struct ppp *ppp, unsigned char c)
725 {
726 unsigned long flags;
727
728 save_flags(flags);
729 cli();
730 if (ppp->rhead < ppp->rend) {
731 *ppp->rhead = c;
732 ppp->rhead++;
733 ppp->rcount++;
734 } else
735 ppp->stats.roverrun++;
736 restore_flags(flags);
737 }
738
739 #ifdef CHECK_CHARACTERS
740 static unsigned paritytab[8] = {
741 0x96696996, 0x69969669, 0x69969669, 0x96696996,
742 0x69969669, 0x96696996, 0x96696996, 0x69969669
743 };
744 #endif
745
746 #ifndef NEW_TTY_DRIVERS
747 static void
748 ppp_dump_inqueue(struct tty_struct *tty)
749 {
750 int head = tty->read_q.head,
751 tail = tty->read_q.tail,
752 i, count;
753 char buffer[8];
754
755 PRINTK ((KERN_DEBUG "INQUEUE: head %d tail %d imode %x:\n", head, tail,
756 (unsigned int) tty->termios->c_iflag))
757
758 i = tail;
759 count = 0;
760
761 while (i != head) {
762 buffer [count] = tty->read_q.buf[i];
763 if (++count == 8) {
764 ppp_print_buffer (NULL, buffer, 8, KERNEL_DS);
765 count = 0;
766 }
767 i = (i + 1) & (TTY_BUF_SIZE - 1);
768 }
769 ppp_print_buffer (NULL, buffer, count, KERNEL_DS);
770 }
771
772
773
774
775 void ppp_tty_input_ready(struct tty_struct *tty)
776 {
777 struct ppp *ppp = ppp_find(tty);
778 int n, error;
779 unsigned char buff[128];
780
781
782 if (!ppp || ppp->magic != PPP_MAGIC) {
783 PRINTKN (1,
784 (KERN_ERR "PPP: handler called but couldn't find PPP struct.\n"));
785 return;
786 }
787
788 CHECK_PPP_VOID();
789
790
791 if (ppp_debug >= 5)
792 ppp_dump_inqueue(ppp->tty);
793
794 do {
795 n = tty_read_raw_data(tty, buff, 128);
796 if ( n == 0 )
797 break;
798
799 if (ppp_debug >= 5)
800 ppp_print_buffer ("receive buffer", buff, n > 0 ? n : -n, KERNEL_DS);
801
802 if ( n < 0 ) {
803
804
805 n = (-n) - 1;
806 error = buff[n];
807 } else error = 0;
808 ppp->stats.rbytes += n;
809 ppp_unesc(ppp,buff,n);
810 if (error)
811 ppp->toss = error;
812 } while (1);
813 }
814
815
816
817
818
819
820 static void
821 ppp_unesc(struct ppp *ppp, unsigned char *c, int n)
822 {
823 int i;
824
825 for (i = 0; i < n; i++, c++) {
826 PRINTKN (6,(KERN_DEBUG "(%x)", (unsigned int) *c));
827
828 #ifdef CHECK_CHARACTERS
829 if (*c & 0x80)
830 sc->sc_flags |= SC_RCV_B7_1;
831 else
832 sc->sc_flags |= SC_RCV_B7_0;
833
834 if (paritytab[*c >> 5] & (1 << (*c & 0x1F)))
835 sc->sc_flags |= SC_RCV_ODDP;
836 else
837 sc->sc_flags |= SC_RCV_EVNP;
838 #endif
839
840 switch (*c) {
841 case PPP_ESC:
842 ppp->escape = PPP_TRANS;
843 break;
844
845 case PPP_FLAG:
846 if (ppp->escape)
847 ppp->toss = 0xFF;
848
849 if ((ppp->toss & 0x80) == 0)
850 ppp_doframe(ppp);
851
852 ppp->rcount = 0;
853 ppp->rhead = ppp->rbuff;
854 ppp->escape = 0;
855 ppp->toss = 0;
856 break;
857
858 default:
859 if (!in_rmap (ppp, *c)) {
860 if (ppp->toss == 0)
861 ppp_enqueue (ppp, *c ^ ppp->escape);
862 ppp->escape = 0;
863 }
864 break;
865 }
866 }
867 }
868
869 #else
870 static void ppp_receive_buf(struct tty_struct *tty, unsigned char *cp,
871 char *fp, int count)
872 {
873 register struct ppp *ppp = ppp_find (tty);
874 unsigned char c;
875
876
877
878 if (!ppp || ppp->magic != PPP_MAGIC) {
879 PRINTKN (1,("PPP: handler called but couldn't find "
880 "PPP struct.\n"));
881 return;
882 }
883
884 CHECK_PPP_VOID();
885
886 if (ppp_debug >= 5) {
887 ppp_print_buffer ("receive buffer", cp, count, KERNEL_DS);
888 }
889
890 while (count-- > 0) {
891 c = *cp++;
892
893 if (fp) {
894 if (*fp && ppp->toss == 0)
895 ppp->toss = *fp;
896 fp++;
897 }
898
899 #ifdef CHECK_CHARACTERS
900 if (c & 0x80)
901 sc->sc_flags |= SC_RCV_B7_1;
902 else
903 sc->sc_flags |= SC_RCV_B7_0;
904
905 if (paritytab[c >> 5] & (1 << (c & 0x1F)))
906 sc->sc_flags |= SC_RCV_ODDP;
907 else
908 sc->sc_flags |= SC_RCV_EVNP;
909 #endif
910
911 switch (c) {
912 case PPP_ESC:
913 ppp->escape = PPP_TRANS;
914 break;
915
916 case PPP_FLAG:
917 if (ppp->escape)
918 ppp->toss = 0xFF;
919
920 if ((ppp->toss & 0x80) == 0)
921 ppp_doframe(ppp);
922
923 ppp->rcount = 0;
924 ppp->rhead = ppp->rbuff;
925 ppp->escape = 0;
926 ppp->toss = 0;
927 break;
928
929 default:
930 if (!in_rmap (ppp, c)) {
931 if (ppp->toss == 0)
932 ppp_enqueue (ppp, c ^ ppp->escape);
933 ppp->escape = 0;
934 }
935 }
936 }
937 }
938 #endif
939
940
941
942 static void
943 ppp_doframe(struct ppp *ppp)
944 {
945 u_char *c = ppp->rbuff;
946 u_short proto;
947 int count = ppp->rcount;
948
949
950 if (ppp->toss) {
951 PRINTKN (1, (KERN_WARNING "ppp_toss: tossing frame, reason = %d\n",
952 ppp->toss));
953 ppp->stats.rerrors++;
954 return;
955 }
956
957
958 if (count == 0)
959 return;
960
961 if (ppp_debug >= 3)
962 ppp_print_buffer ("receive frame", c, count, KERNEL_DS);
963
964 if (count < 4) {
965 PRINTKN (1,(KERN_WARNING "ppp: got runt ppp frame, %d chars\n", count));
966 ppp->stats.runts++;
967 return;
968 }
969
970
971 if (!ppp_check_fcs(ppp)) {
972 PRINTKN (1,(KERN_WARNING "ppp: frame with bad fcs\n"));
973 ppp->stats.rerrors++;
974 return;
975 }
976
977 count -= 2;
978
979
980
981 if ((c[0] == PPP_ADDRESS) && (c[1] == PPP_CONTROL)) {
982 c = c + 2;
983 count -= 2;
984 }
985
986 proto = (u_short) *c++;
987 if (proto & 1) {
988 count--;
989 } else {
990 proto = (proto << 8) | (u_short) *c++;
991 count -= 2;
992 }
993
994
995 if ((ppp->dev->flags & IFF_UP) && ppp_do_ip(ppp, proto, c, count)) {
996 ppp->ddinfo.ip_rjiffies = jiffies;
997 return;
998 }
999
1000
1001
1002
1003
1004
1005
1006 if (ppp_us_queue (ppp, proto, c, count+2)) {
1007 ppp->ddinfo.nip_rjiffies = jiffies;
1008 ppp->stats.rothers++;
1009 return;
1010 }
1011
1012
1013 PRINTKN (1,(KERN_WARNING
1014 "ppp: dropping packet on the floor: nobody could take it.\n"));
1015 ppp->stats.tossed++;
1016 }
1017
1018
1019
1020
1021
1022 static int
1023 ppp_do_ip (struct ppp *ppp, unsigned short proto, unsigned char *c,
1024 int count)
1025 {
1026 int flags, done;
1027
1028 PRINTKN (4,(KERN_DEBUG "ppp_do_ip: proto %x len %d first byte %x\n",
1029 (int) proto, count, c[0]));
1030
1031 if (ppp_debug_netpackets) {
1032 PRINTK (("KERN_DEBUG %s <-- proto %x len %d\n", ppp->dev->name,
1033 (int) proto, count));
1034 }
1035
1036 if (proto == PROTO_IP) {
1037 ppp->stats.runcomp++;
1038 goto sendit;
1039 }
1040
1041 if ((proto == PROTO_VJCOMP) && !(ppp->flags & SC_REJ_COMP_TCP)) {
1042
1043 done = 0;
1044 save_flags (flags);
1045 cli();
1046 if ((ppp->rhead + 80) < ppp->rend) {
1047 ppp->rhead += 80;
1048 ppp->rcount += 80;
1049 done = 1;
1050 }
1051 restore_flags(flags);
1052
1053 if (! done) {
1054 PRINTKN (1,(KERN_NOTICE
1055 "ppp: no space to decompress VJ compressed TCP header.\n"));
1056 ppp->stats.roverrun++;
1057 return 1;
1058 }
1059
1060 count = slhc_uncompress(ppp->slcomp, c, count);
1061 if (count <= 0) {
1062 ppp->stats.rerrors++;
1063 PRINTKN (1,(KERN_NOTICE "ppp: error in VJ decompression\n"));
1064 return 1;
1065 }
1066 ppp->stats.rcomp++;
1067 goto sendit;
1068 }
1069
1070 if ((proto == PROTO_VJUNCOMP) && !(ppp->flags & SC_REJ_COMP_TCP)) {
1071 if (slhc_remember(ppp->slcomp, c, count) <= 0) {
1072 ppp->stats.rerrors++;
1073 PRINTKN (1,(KERN_NOTICE "ppp: error in VJ memorizing\n"));
1074 return 1;
1075 }
1076 ppp->stats.runcomp++;
1077 goto sendit;
1078 }
1079
1080
1081 return 0;
1082
1083 sendit:
1084 if (ppp_debug_netpackets) {
1085 struct iphdr *iph = (struct iphdr *) c;
1086 PRINTK ((KERN_INFO "%s <-- src %lx dst %lx len %d\n", ppp->dev->name,
1087 iph->saddr, iph->daddr, count))
1088 }
1089
1090
1091 while ((dev_rint(c, count, 0, ppp->dev) & ~1) != 0)
1092 ;
1093
1094 return 1;
1095 }
1096
1097
1098
1099
1100 #define PUTC(c,label) *ppp->us_rbuff_head++ = c; \
1101 if (ppp->us_rbuff_head == ppp->us_rbuff_end) \
1102 ppp->us_rbuff_head = ppp->us_rbuff; \
1103 if (ppp->us_rbuff_head == ppp->us_rbuff_tail) \
1104 goto label;
1105 #define GETC(c) c = *ppp->us_rbuff_tail++; \
1106 if (ppp->us_rbuff_tail == ppp->us_rbuff_end) \
1107 ppp->us_rbuff_tail = ppp->us_rbuff;
1108
1109 static int
1110 ppp_us_queue(struct ppp *ppp, unsigned short proto,
1111 unsigned char *buf, int len)
1112 {
1113 int totlen;
1114 unsigned char *saved_head;
1115
1116 totlen = len+2;
1117
1118 if (set_bit(1, &ppp->us_rbuff_lock)) {
1119 PRINTKN (1, (KERN_NOTICE "ppp_us_queue: can't get lock\n"));
1120 return 0;
1121 }
1122 saved_head = ppp->us_rbuff_head;
1123
1124 PUTC((totlen & 0xff00) >> 8, failure);
1125 PUTC(totlen & 0x00ff, failure);
1126 PUTC((proto & 0xff00) >> 8, failure);
1127 PUTC(proto & 0x00ff, failure);
1128
1129 while (len-- > 0) {
1130 PUTC(*buf++, failure);
1131 }
1132
1133 PRINTKN (3, (KERN_INFO "ppp: successfully queued %d bytes\n", totlen));
1134 clear_bit(1, &ppp->us_rbuff_lock);
1135 wake_up_interruptible (&ppp->read_wait);
1136
1137 #ifdef NEW_TTY_DRIVERS
1138 kill_fasync(ppp->tty->fasync, SIGIO);
1139 #endif
1140
1141 if (ppp->inp_sig && ppp->inp_sig_pid)
1142 if (kill_proc (ppp->inp_sig_pid, ppp->inp_sig, 1) != 0) {
1143
1144 PRINTKN (2,(KERN_NOTICE
1145 "ppp: process that requested notification is gone\n"));
1146 ppp->inp_sig = 0;
1147 ppp->inp_sig_pid = 0;
1148 }
1149 return 1;
1150
1151 failure:
1152 ppp->us_rbuff_head = saved_head;
1153 clear_bit(1, &ppp->us_rbuff_lock);
1154
1155 PRINTKN (1, (KERN_NOTICE "ppp_us_queue: ran out of buffer space.\n"));
1156
1157 return 0;
1158 }
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172 static int
1173 ppp_read(struct tty_struct *tty, struct file *file, unsigned char *buf, unsigned int nr)
1174 {
1175 struct ppp *ppp = ppp_find(tty);
1176 unsigned char c;
1177 int len, i;
1178
1179 if (!ppp || ppp->magic != PPP_MAGIC) {
1180 PRINTKN (1,(KERN_ERR "ppp_read: cannnot find ppp channel\n"));
1181 return -EIO;
1182 }
1183
1184 CHECK_PPP(-ENXIO);
1185
1186 PRINTKN (4,(KERN_DEBUG "ppp_read: called %x num %u\n",
1187 (unsigned int) buf,
1188 nr));
1189
1190 do {
1191
1192 if (set_bit(0, &ppp->us_rbuff_lock) == 0) {
1193
1194 if (ppp->us_rbuff_head == ppp->us_rbuff_tail) {
1195
1196 PRINTKN (4,(KERN_DEBUG "ppp_read: no data\n"));
1197 clear_bit(0, &ppp->us_rbuff_lock);
1198 if (ppp->inp_sig) {
1199 PRINTKN (4,(KERN_DEBUG "ppp_read: EWOULDBLOCK\n"));
1200 return -EWOULDBLOCK;
1201 } else goto wait;
1202 }
1203
1204
1205 ppp->ddinfo.nip_rjiffies = jiffies;
1206
1207 GETC (c); len = c << 8; GETC (c); len += c;
1208
1209 PRINTKN (4,(KERN_DEBUG "ppp_read: len = %d\n", len));
1210
1211 if (len + 2 > nr) {
1212
1213 PRINTKN (1,(KERN_DEBUG
1214 "ppp: read of %u bytes too small for %d frame\n",
1215 nr, len+2));
1216 ppp->us_rbuff_head += len;
1217 if (ppp->us_rbuff_head > ppp->us_rbuff_end)
1218 ppp->us_rbuff_head += - (ppp->us_rbuff_end - ppp->us_rbuff);
1219 clear_bit(0, &ppp->us_rbuff_lock);
1220 wake_up_interruptible (&ppp->read_wait);
1221 ppp->stats.rgiants++;
1222 return -EOVERFLOW;
1223 } else {
1224
1225 put_fs_byte (PPP_ADDRESS, buf++);
1226 put_fs_byte (PPP_CONTROL, buf++);
1227 i = len;
1228 while (i-- > 0) {
1229 GETC (c);
1230 put_fs_byte (c, buf++);
1231 }
1232 }
1233
1234 clear_bit(0, &ppp->us_rbuff_lock);
1235 PRINTKN (3,(KERN_DEBUG "ppp_read: passing %d bytes up\n", len + 2));
1236 ppp->stats.rothers++;
1237 return len + 2;
1238 }
1239
1240
1241 wait:
1242 current->timeout = 0;
1243 PRINTKN (3,(KERN_DEBUG "ppp_read: sleeping\n"));
1244 interruptible_sleep_on (&ppp->read_wait);
1245 if (current->signal & ~current->blocked)
1246 return -EINTR;
1247 } while (1);
1248 }
1249
1250
1251
1252
1253 static inline void
1254 ppp_stuff_char(struct ppp *ppp, unsigned char c)
1255 {
1256 int curpt = ppp->xhead - ppp->xbuff;
1257 if ((curpt < 0) || (curpt > 3000)) {
1258 PRINTK ((KERN_DEBUG "ppp_stuff_char: %x %x %d\n",
1259 (unsigned int) ppp->xbuff, (unsigned int) ppp->xhead, curpt))
1260 }
1261 if (in_xmap (ppp, c)) {
1262 *ppp->xhead++ = PPP_ESC;
1263 *ppp->xhead++ = c ^ PPP_TRANS;
1264 } else
1265 *ppp->xhead++ = c;
1266 ppp->fcs = (ppp->fcs >> 8) ^ fcstab[(ppp->fcs ^ c) & 0xff];
1267 }
1268
1269
1270
1271
1272
1273 static int
1274 ppp_write(struct tty_struct *tty, struct file *file, unsigned char *buf, unsigned int nr)
1275 {
1276 struct ppp *ppp = ppp_find(tty);
1277 int i;
1278
1279 if (!ppp || ppp->magic != PPP_MAGIC) {
1280 PRINTKN (1,(KERN_ERR "ppp_write: cannot find ppp unit\n"));
1281 return -EIO;
1282 }
1283
1284 CHECK_PPP(-ENXIO);
1285
1286 if (ppp->mtu != ppp->dev->mtu)
1287 ppp_changedmtu (ppp, ppp->dev->mtu, ppp->mru);
1288
1289 if (nr > ppp->mtu) {
1290 PRINTKN (1,(KERN_WARNING
1291 "ppp_write: truncating user packet from %u to mtu %d\n",
1292 nr, ppp->mtu));
1293 nr = ppp->mtu;
1294 }
1295
1296 if (ppp_debug >= 3)
1297 ppp_print_buffer ("write frame", buf, nr, USER_DS);
1298
1299
1300
1301 while ((ppp->sending == 1) || !ppp_lock(ppp)) {
1302 current->timeout = 0;
1303 PRINTKN (3,(KERN_DEBUG "ppp_write: sleeping\n"));
1304 interruptible_sleep_on(&ppp->write_wait);
1305 if (current->signal & ~current->blocked)
1306 return -EINTR;
1307 }
1308
1309
1310
1311 PRINTKN(4,(KERN_DEBUG "ppp_write: acquired write lock\n"));
1312 ppp->xhead = ppp->xbuff;
1313
1314 #ifdef OPTIMIZE_FLAG_TIME
1315 if (jiffies - ppp->last_xmit > OPTIMIZE_FLAG_TIME)
1316 *ppp->xhead++ = PPP_FLAG;
1317 ppp->last_xmit = jiffies;
1318 #else
1319 *ppp->xhead++ = PPP_FLAG;
1320 #endif
1321
1322 ppp->fcs = PPP_FCS_INIT;
1323 i = nr;
1324 while (i-- > 0)
1325 ppp_stuff_char(ppp,get_fs_byte(buf++));
1326
1327 ppp_add_fcs(ppp);
1328
1329 *ppp->xhead++ = PPP_FLAG;
1330
1331
1332 ppp->ddinfo.nip_sjiffies = jiffies;
1333
1334 if (ppp_debug >= 6)
1335 ppp_print_buffer ("xmit buffer", ppp->xbuff, ppp->xhead - ppp->xbuff, KERNEL_DS);
1336 else {
1337 PRINTKN (4,(KERN_DEBUG
1338 "ppp_write: writing %d chars\n", ppp->xhead - ppp->xbuff));
1339 }
1340
1341
1342 ++ppp->stats.sothers;
1343 ppp_kick_tty(ppp);
1344
1345 return((int)nr);
1346 }
1347
1348 static int
1349 ppp_ioctl(struct tty_struct *tty, struct file *file, unsigned int i,
1350 unsigned long l)
1351 {
1352 struct ppp *ppp = ppp_find(tty);
1353 register int temp_i = 0;
1354 int error;
1355
1356 if (!ppp || ppp->magic != PPP_MAGIC) {
1357 PRINTK ((KERN_ERR "ppp_ioctl: can't find PPP block from tty!\n"))
1358 return -EBADF;
1359 }
1360
1361 CHECK_PPP(-ENXIO);
1362
1363
1364 if (!suser())
1365 return -EPERM;
1366
1367 switch (i) {
1368 case PPPIOCSMRU:
1369 error = verify_area (VERIFY_READ, (void *) l, sizeof (temp_i));
1370 if (error == 0) {
1371 PRINTKN (3,(KERN_INFO "ppp_ioctl: set mru to %x\n", temp_i));
1372 temp_i = (int) get_fs_long (l);
1373 if (ppp->mru != temp_i)
1374 ppp_changedmtu (ppp, ppp->mtu, temp_i);
1375 }
1376 break;
1377
1378 case PPPIOCGFLAGS:
1379 error = verify_area (VERIFY_WRITE, (void *) l, sizeof (temp_i));
1380 if (error == 0) {
1381 temp_i = (ppp->flags & SC_MASK);
1382 #ifndef CHECK_CHARACTERS
1383 temp_i |= SC_RCV_B7_1 | SC_RCV_B7_0 | SC_RCV_ODDP | SC_RCV_EVNP;
1384 #endif
1385 put_fs_long ((long) temp_i, l);
1386 PRINTKN (3,(KERN_DEBUG "ppp_ioctl: get flags: addr %lx flags %x\n",
1387 l,
1388 temp_i));
1389 }
1390 break;
1391
1392 case PPPIOCSFLAGS:
1393 error = verify_area (VERIFY_READ, (void *) l, sizeof (temp_i));
1394 if (error == 0) {
1395 temp_i = (int) get_fs_long (l);
1396 ppp->flags ^= ((ppp->flags ^ temp_i) & SC_MASK);
1397 PRINTKN (3,(KERN_INFO "ppp_ioctl: set flags to %x\n", temp_i));
1398 }
1399 break;
1400
1401 case PPPIOCGASYNCMAP:
1402 error = verify_area (VERIFY_WRITE, (void *) l, sizeof (temp_i));
1403 if (error == 0) {
1404 put_fs_long (ppp->xmit_async_map[0], l);
1405 PRINTKN (3,(KERN_INFO "ppp_ioctl: get asyncmap: addr %lx asyncmap %lx\n",
1406 l, ppp->xmit_async_map[0]));
1407 }
1408 break;
1409
1410 case PPPIOCSASYNCMAP:
1411 error = verify_area (VERIFY_READ, (void *) l, sizeof (temp_i));
1412 if (error == 0) {
1413 memset (ppp->xmit_async_map, 0, sizeof (ppp->xmit_async_map));
1414 ppp->xmit_async_map[0] = get_fs_long (l);
1415 bset (ppp->xmit_async_map, PPP_FLAG);
1416 bset (ppp->xmit_async_map, PPP_ESC);
1417 PRINTKN (3,(KERN_INFO "ppp_ioctl: set xmit asyncmap %lx\n",
1418 ppp->xmit_async_map[0]));
1419 }
1420 break;
1421
1422 case PPPIOCRASYNCMAP:
1423 error = verify_area (VERIFY_READ, (void *) l, sizeof (temp_i));
1424 if (error == 0) {
1425 ppp->recv_async_map = get_fs_long (l);
1426 PRINTKN (3,(KERN_INFO "ppp_ioctl: set recv asyncmap %lx\n",
1427 ppp->recv_async_map));
1428 }
1429 break;
1430
1431 case PPPIOCGUNIT:
1432 error = verify_area (VERIFY_WRITE, (void *) l, sizeof (temp_i));
1433 if (error == 0) {
1434 put_fs_long (ppp->dev->base_addr, l);
1435 PRINTKN (3,(KERN_INFO "ppp_ioctl: get unit: %d", ppp->dev->base_addr));
1436 }
1437 break;
1438
1439 case PPPIOCSINPSIG:
1440 error = verify_area (VERIFY_READ, (void *) l, sizeof (temp_i));
1441 if (error == 0) {
1442 ppp->inp_sig = (int) get_fs_long (l);
1443 ppp->inp_sig_pid = current->pid;
1444 PRINTKN (3,(KERN_INFO "ppp_ioctl: set input signal %d\n", ppp->inp_sig));
1445 }
1446 break;
1447
1448 case PPPIOCSDEBUG:
1449 error = verify_area (VERIFY_READ, (void *) l, sizeof (temp_i));
1450 if (error == 0) {
1451 ppp_debug = (int) get_fs_long (l);
1452 ppp_debug_netpackets = (ppp_debug & 0xff00) >> 8;
1453 ppp_debug &= 0xff;
1454 PRINTKN (1, (KERN_INFO "ppp_ioctl: set debug level %d, netpacket %d\n",
1455 ppp_debug, ppp_debug_netpackets));
1456 }
1457 break;
1458
1459 case PPPIOCGDEBUG:
1460 error = verify_area (VERIFY_WRITE, (void *) l, sizeof (temp_i));
1461 if (error == 0) {
1462 put_fs_long ((long) (ppp_debug | (ppp_debug_netpackets << 8)), l);
1463 PRINTKN (3,(KERN_INFO "ppp_ioctl: get debug level %d\n",
1464 ppp_debug | (ppp_debug_netpackets << 8)));
1465 }
1466 break;
1467
1468 case PPPIOCGSTAT:
1469 error = verify_area (VERIFY_WRITE, (void *) l, sizeof (struct ppp_stats));
1470 if (error == 0) {
1471 memcpy_tofs ((void *) l, &ppp->stats, sizeof (struct ppp_stats));
1472 PRINTKN (3,(KERN_INFO "ppp_ioctl: read statistics\n"));
1473 }
1474 break;
1475
1476 case PPPIOCGTIME:
1477 error = verify_area (VERIFY_WRITE, (void *) l, sizeof (struct ppp_ddinfo));
1478 if (error == 0) {
1479 struct ppp_ddinfo cur_ddinfo;
1480 unsigned long cur_jiffies = jiffies;
1481
1482
1483 cur_ddinfo.ip_sjiffies = cur_jiffies - ppp->ddinfo.ip_sjiffies;
1484 cur_ddinfo.ip_rjiffies = cur_jiffies - ppp->ddinfo.ip_rjiffies;
1485 cur_ddinfo.nip_sjiffies = cur_jiffies - ppp->ddinfo.nip_sjiffies;
1486 cur_ddinfo.nip_rjiffies = cur_jiffies - ppp->ddinfo.nip_rjiffies;
1487
1488 memcpy_tofs ((void *) l, &cur_ddinfo, sizeof (struct ppp_ddinfo));
1489 PRINTKN (3,(KERN_INFO "ppp_ioctl: read demand dial info\n"));
1490 }
1491 break;
1492
1493 case PPPIOCGXASYNCMAP:
1494 error = verify_area (VERIFY_WRITE,
1495 (void *) l,
1496 sizeof (ppp->xmit_async_map));
1497 if (error == 0) {
1498 memcpy_tofs ((void *) l,
1499 ppp->xmit_async_map,
1500 sizeof (ppp->xmit_async_map));
1501 PRINTKN (3,(KERN_INFO "ppp_ioctl: get xasyncmap: addr %lx\n", l));
1502 }
1503 break;
1504
1505 case PPPIOCSXASYNCMAP:
1506 error = verify_area (VERIFY_READ, (void *) l,
1507 sizeof (ppp->xmit_async_map));
1508 if (error == 0) {
1509 unsigned long temp_tbl [8];
1510
1511 memcpy_fromfs (temp_tbl, (void *) l, sizeof (ppp->xmit_async_map));
1512 temp_tbl[1] = 0x00000000;
1513 temp_tbl[2] &= ~0x40000000;
1514 temp_tbl[3] |= 0x60000000;
1515
1516 if ((temp_tbl[2] & temp_tbl[3]) != 0 ||
1517 (temp_tbl[4] & temp_tbl[5]) != 0 ||
1518 (temp_tbl[6] & temp_tbl[7]) != 0)
1519 error = -EINVAL;
1520 else {
1521 memcpy (ppp->xmit_async_map, temp_tbl, sizeof (ppp->xmit_async_map));
1522 PRINTKN (3,(KERN_INFO "ppp_ioctl: set xasyncmap\n"));
1523 }
1524 }
1525 break;
1526
1527 case PPPIOCSMAXCID:
1528 error = verify_area (VERIFY_READ, (void *) l, sizeof (temp_i));
1529 if (error == 0) {
1530 temp_i = (int) get_fs_long (l) + 1;
1531 PRINTKN (3,(KERN_INFO "ppp_ioctl: set maxcid to %d\n", temp_i));
1532 if (ppp->slcomp != NULL)
1533 slhc_free (ppp->slcomp);
1534
1535 ppp->slcomp = slhc_init (temp_i, temp_i);
1536
1537 if (ppp->slcomp == NULL) {
1538 PRINTKN (1,(KERN_ERR "ppp: no space for compression buffers!\n"));
1539 ppp_release (ppp);
1540 error = -ENOMEM;
1541 }
1542 }
1543 break;
1544
1545 #ifdef NEW_TTY_DRIVERS
1546
1547 case TCGETS:
1548 case TCGETA:
1549 error = n_tty_ioctl(tty, file, i, l);
1550 break;
1551 #endif
1552
1553
1554
1555
1556
1557 default:
1558 PRINTKN (1,(KERN_ERR "ppp_ioctl: invalid ioctl: %x, addr %lx\n",
1559 i,
1560 l));
1561 #ifdef NEW_TTY_DRIVERS
1562 error = -ENOIOCTLCMD;
1563 #else
1564 error = -EINVAL;
1565 #endif
1566 break;
1567 }
1568 return error;
1569 }
1570
1571 static int
1572 ppp_select (struct tty_struct *tty, struct inode * inode,
1573 struct file * filp, int sel_type, select_table * wait)
1574 {
1575 struct ppp *ppp = ppp_find (tty);
1576
1577 if (!ppp || ppp->magic != PPP_MAGIC) {
1578 PRINTK ((KERN_ERR "ppp_select: can't find PPP block from tty!\n"))
1579 return -EBADF;
1580 }
1581
1582
1583 CHECK_PPP (0);
1584
1585
1586 switch (sel_type) {
1587 case SEL_IN:
1588 if (set_bit(0, &ppp->us_rbuff_lock) == 0) {
1589
1590 if (ppp->us_rbuff_head != ppp->us_rbuff_tail) {
1591 clear_bit (0, &ppp->us_rbuff_lock);
1592 return 1;
1593 }
1594 clear_bit (0, &ppp->us_rbuff_lock);
1595 }
1596
1597 case SEL_EX:
1598
1599 if (tty->packet && tty->link->ctrl_status)
1600 return 1;
1601
1602
1603 if (tty->flags & (1 << TTY_SLAVE_CLOSED))
1604 return 1;
1605
1606
1607 if (tty_hung_up_p(filp))
1608 return 1;
1609
1610 select_wait (&ppp->read_wait, wait);
1611 break;
1612
1613 case SEL_OUT:
1614 if (ppp_lock (ppp)) {
1615 if (ppp->sending == 0) {
1616 ppp_unlock (ppp);
1617 return 1;
1618 }
1619 ppp_unlock (ppp);
1620 }
1621 select_wait (&ppp->write_wait, wait);
1622 break;
1623 }
1624 return 0;
1625 }
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635 int
1636 ppp_xmit(struct sk_buff *skb, struct device *dev)
1637 {
1638 struct tty_struct *tty;
1639 struct ppp *ppp;
1640 unsigned char *p;
1641 unsigned short proto;
1642 int len;
1643
1644
1645 if (skb == NULL) {
1646 PRINTKN(3,(KERN_WARNING "ppp_xmit: null packet!\n"));
1647 return 0;
1648 }
1649
1650
1651 ppp = &ppp_ctrl[dev->base_addr];
1652 tty = ppp->tty;
1653 p = (unsigned char *) (skb + 1);
1654 len = skb->len;
1655 proto = PROTO_IP;
1656
1657 PRINTKN(4,(KERN_DEBUG "ppp_xmit [%s]: skb %lX busy %d\n", dev->name,
1658 (unsigned long int) skb, ppp->sending));
1659
1660 CHECK_PPP(0);
1661
1662 if (tty == NULL) {
1663 PRINTKN(1,(KERN_ERR "ppp_xmit: %s not connected to a TTY!\n", dev->name));
1664 goto done;
1665 }
1666
1667 if (!(dev->flags & IFF_UP)) {
1668 PRINTKN(1,(KERN_WARNING
1669 "ppp_xmit: packet sent on interface %s, which is down for IP\n",
1670 dev->name));
1671 goto done;
1672 }
1673
1674
1675 if (len < sizeof(struct iphdr)) {
1676 PRINTKN(0,(KERN_ERR "ppp_xmit: given runt packet, ignoring\n"));
1677 return 1;
1678 }
1679 len = ntohs( ((struct iphdr *)(skb->data)) -> tot_len );
1680
1681
1682 if (ppp->flags & SC_IP_DOWN) {
1683 if (ppp->flags & SC_IP_FLUSH == 0) {
1684 if (ppp_us_queue (ppp, proto, p, len))
1685 ppp->flags |= SC_IP_FLUSH;
1686 }
1687 goto done;
1688 }
1689
1690
1691 if (ppp->sending || !ppp_lock(ppp)) {
1692 PRINTKN(3,(KERN_WARNING "ppp_xmit: busy\n"));
1693 ppp->stats.sbusy++;
1694 return 1;
1695 }
1696
1697 ppp->xhead = ppp->xbuff;
1698
1699
1700 if (ppp->flags & SC_COMP_TCP) {
1701
1702 len = slhc_compress(ppp->slcomp, p, len, ppp->cbuff, &p, 0);
1703 if (p[0] & SL_TYPE_COMPRESSED_TCP)
1704 proto = PROTO_VJCOMP;
1705 else {
1706 if (p[0] >= SL_TYPE_UNCOMPRESSED_TCP) {
1707 proto = PROTO_VJUNCOMP;
1708 p[0] = (p[0] & 0x0f) | 0x40;
1709 }
1710 }
1711 }
1712
1713
1714 if (proto == PROTO_VJCOMP)
1715 ++ppp->stats.scomp;
1716 else
1717 ++ppp->stats.suncomp;
1718
1719 if (ppp_debug_netpackets) {
1720 struct iphdr *iph = (struct iphdr *) (skb + 1);
1721 PRINTK ((KERN_DEBUG "%s ==> proto %x len %d src %x dst %x proto %d\n",
1722 dev->name, (int) proto, (int) len, (int) iph->saddr,
1723 (int) iph->daddr, (int) iph->protocol))
1724 }
1725
1726
1727 #ifdef OPTIMIZE_FLAG_TIME
1728 if (jiffies - ppp->last_xmit > OPTIMIZE_FLAG_TIME)
1729 *ppp->xhead++ = PPP_FLAG;
1730 ppp->last_xmit = jiffies;
1731 #else
1732 *ppp->xhead++ = PPP_FLAG;
1733 #endif
1734
1735 ppp->fcs = PPP_FCS_INIT;
1736 if (!(ppp->flags & SC_COMP_AC)) {
1737 ppp_stuff_char(ppp, PPP_ADDRESS);
1738 ppp_stuff_char(ppp, PPP_CONTROL);
1739 }
1740
1741 if (!(ppp->flags & SC_COMP_PROT) || (proto & 0xff00))
1742 ppp_stuff_char(ppp, proto>>8);
1743 ppp_stuff_char(ppp, proto&0xff);
1744
1745
1746 while (len-- > 0)
1747 ppp_stuff_char(ppp, *p++);
1748
1749
1750 ppp_add_fcs(ppp);
1751 *ppp->xhead++ = PPP_FLAG;
1752
1753
1754 ppp->ddinfo.ip_sjiffies = jiffies;
1755
1756
1757 if (ppp_debug >= 6)
1758 ppp_print_buffer ("xmit buffer", ppp->xbuff, ppp->xhead - ppp->xbuff, KERNEL_DS);
1759 else {
1760 PRINTKN (4,(KERN_DEBUG
1761 "ppp_write: writing %d chars\n", ppp->xhead - ppp->xbuff));
1762 }
1763
1764 ppp_kick_tty(ppp);
1765
1766 done:
1767 if (skb->free)
1768 kfree_skb(skb, FREE_WRITE);
1769 return 0;
1770 }
1771
1772 static unsigned short
1773 ppp_type_trans (struct sk_buff *skb, struct device *dev)
1774 {
1775 return(htons(ETH_P_IP));
1776 }
1777
1778 #ifdef NET02D
1779 static int
1780 ppp_header(unsigned char *buff, struct device *dev, unsigned short type,
1781 unsigned long daddr, unsigned long saddr, unsigned len)
1782 {
1783 return(0);
1784 }
1785
1786 static int
1787 ppp_rebuild_header(void *buff, struct device *dev)
1788 {
1789 return(0);
1790 }
1791
1792 static void
1793 ppp_add_arp(unsigned long addr, struct sk_buff *skb, struct device *dev)
1794 {
1795 }
1796
1797 #else
1798
1799 static int
1800 ppp_header(unsigned char *buff, struct device *dev, unsigned short type,
1801 void *daddr, void *saddr, unsigned len, struct sk_buff *skb)
1802 {
1803 return(0);
1804 }
1805
1806 static int
1807 ppp_rebuild_header(void *buff, struct device *dev, unsigned long raddr,
1808 struct sk_buff *skb)
1809 {
1810 return(0);
1811 }
1812 #endif
1813
1814 static struct enet_statistics *
1815 ppp_get_stats (struct device *dev)
1816 {
1817 struct ppp *ppp = &ppp_ctrl[dev->base_addr];
1818 static struct enet_statistics ppp_stats;
1819
1820 ppp_stats.rx_packets = ppp->stats.rcomp + ppp->stats.runcomp;
1821 ppp_stats.rx_errors = ppp->stats.rerrors;
1822 ppp_stats.rx_dropped = ppp->stats.tossed;
1823 ppp_stats.rx_fifo_errors = 0;
1824 ppp_stats.rx_length_errors = ppp->stats.runts;
1825 ppp_stats.rx_over_errors = ppp->stats.roverrun;
1826 ppp_stats.rx_crc_errors = 0;
1827 ppp_stats.rx_frame_errors = 0;
1828 ppp_stats.tx_packets = ppp->stats.scomp + ppp->stats.suncomp;
1829 ppp_stats.tx_errors = ppp->stats.serrors;
1830 ppp_stats.tx_dropped = 0;
1831 ppp_stats.tx_fifo_errors = 0;
1832 ppp_stats.collisions = ppp->stats.sbusy;
1833 ppp_stats.tx_carrier_errors = 0;
1834 ppp_stats.tx_aborted_errors = 0;
1835 ppp_stats.tx_window_errors = 0;
1836 ppp_stats.tx_heartbeat_errors = 0;
1837
1838 PRINTKN (3, (KERN_INFO "ppp_get_stats called"));
1839 return &ppp_stats;
1840 }
1841
1842
1843
1844
1845
1846
1847 #ifndef NEW_TTY_DRIVERS
1848
1849 struct ppp *
1850 ppp_find(struct tty_struct *tty)
1851 {
1852 int i;
1853 for (i = 0; i < PPP_NRUNIT; i++)
1854 if (ppp_ctrl[i].inuse && (ppp_ctrl[i].tty == tty)) return &ppp_ctrl[i];
1855
1856 return NULL;
1857 }
1858 #endif
1859
1860
1861 static struct ppp *
1862 ppp_alloc(void)
1863 {
1864 int i;
1865 for (i = 0; i < PPP_NRUNIT; i++)
1866 if (!set_bit(0, &ppp_ctrl[i].inuse)) return &ppp_ctrl[i];
1867
1868 return NULL;
1869 }
1870
1871
1872
1873
1874
1875
1876 static int
1877 ppp_lock(struct ppp *ppp)
1878 {
1879 int flags, locked;
1880 save_flags(flags);
1881 cli();
1882 locked = ppp->sending;
1883 ppp->sending = 1;
1884 if (ppp->dev->flags & IFF_UP)
1885 ppp->dev->tbusy = 1;
1886 restore_flags(flags);
1887 return locked == 0;
1888 }
1889
1890 static void
1891 ppp_unlock(struct ppp *ppp)
1892 {
1893 int flags;
1894 save_flags(flags);
1895 cli();
1896 ppp->sending = 0;
1897 if (ppp->dev->flags & IFF_UP)
1898 ppp->dev->tbusy = 0;
1899 restore_flags(flags);
1900 }
1901
1902
1903
1904 static void
1905 ppp_add_fcs(struct ppp *ppp)
1906 {
1907 unsigned short fcs = ppp->fcs;
1908
1909 fcs ^= 0xffff;
1910 ppp_stuff_char(ppp, fcs & 0x00ff);
1911 ppp_stuff_char(ppp, (fcs & 0xff00) >> 8);
1912 ASSERT (ppp->fcs == PPP_FCS_GOOD);
1913 PRINTKN (4,(KERN_DEBUG "ppp_add_fcs: fcs is %lx\n",
1914 (long) (unsigned long) fcs));
1915 }
1916
1917 static int
1918 ppp_check_fcs(struct ppp *ppp)
1919 {
1920 unsigned short fcs = PPP_FCS_INIT, msgfcs;
1921 unsigned char *c = ppp->rbuff;
1922 int i;
1923
1924 for (i = 0; i < ppp->rcount - 2; i++, c++)
1925 fcs = (fcs >> 8) ^ fcstab[(fcs ^ *c) & 0xff];
1926
1927 fcs ^= 0xffff;
1928 msgfcs = (c[1] << 8) + c[0];
1929 PRINTKN (4,(KERN_INFO "ppp_check_fcs: got %lx want %lx\n",
1930 (unsigned long) msgfcs, (unsigned long) fcs));
1931 return fcs == msgfcs;
1932 }
1933
1934 static char hex[] = "0123456789ABCDEF";
1935
1936 inline void ppp_print_hex (register char *out, char *in, int count);
1937 inline void ppp_print_hex (register char *out, char *in, int count)
1938 {
1939 register unsigned char next_ch;
1940
1941 while (count-- > 0) {
1942 next_ch = (unsigned char) get_fs_byte (in);
1943
1944 *out++ = hex[(next_ch >> 4) & 0x0F];
1945 *out++ = hex[next_ch & 0x0F];
1946 ++out;
1947 ++in;
1948 }
1949 }
1950
1951 inline void ppp_print_char (register char *out, char *in, int count);
1952 inline void ppp_print_char (register char *out, char *in, int count)
1953 {
1954 register unsigned char next_ch;
1955
1956 while (count-- > 0) {
1957 next_ch = (unsigned char) get_fs_byte (in);
1958
1959 if (next_ch < 0x20 || next_ch > 0x7e)
1960 *out++ = '.';
1961 else {
1962 *out++ = next_ch;
1963 if (next_ch == '%')
1964 *out++ = '%';
1965 }
1966 ++in;
1967 }
1968 *out = '\0';
1969 }
1970
1971 static void ppp_print_buffer(const char *name, char *buf, int count, int seg)
1972 {
1973 char line [44];
1974 int old_fs = get_fs();
1975
1976 set_fs (seg);
1977
1978 if (name != (char *) NULL)
1979 PRINTK ((KERN_DEBUG "ppp: %s, count = %d\n", name, count));
1980
1981 while (count > 8) {
1982 memset (line, ' ', sizeof (line));
1983 ppp_print_hex (line, buf, 8);
1984 ppp_print_char (&line[8 * 3], buf, 8);
1985 PRINTK ((KERN_DEBUG "%s\n", line));
1986 count -= 8;
1987 buf += 8;
1988 }
1989
1990 if (count > 0) {
1991 memset (line, ' ', sizeof (line));
1992 ppp_print_hex (line, buf, count);
1993 ppp_print_char (&line[8 * 3], buf, count);
1994 PRINTK ((KERN_DEBUG "%s\n", line));
1995 }
1996
1997 set_fs (old_fs);
1998 }