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