1 /* 2 * Generic PPP layer for Linux. 3 * 4 * Copyright 1999-2002 Paul Mackerras. 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; either version 9 * 2 of the License, or (at your option) any later version. 10 * 11 * The generic PPP layer handles the PPP network interfaces, the 12 * /dev/ppp device, packet and VJ compression, and multilink. 13 * It talks to PPP `channels' via the interface defined in 14 * include/linux/ppp_channel.h. Channels provide the basic means for 15 * sending and receiving PPP frames on some kind of communications 16 * channel. 17 * 18 * Part of the code in this driver was inspired by the old async-only 19 * PPP driver, written by Michael Callahan and Al Longyear, and 20 * subsequently hacked by Paul Mackerras. 21 * 22 * ==FILEVERSION 20041108== 23 */ 24 25 #include <linux/module.h> 26 #include <linux/kernel.h> 27 #include <linux/sched/signal.h> 28 #include <linux/kmod.h> 29 #include <linux/init.h> 30 #include <linux/list.h> 31 #include <linux/idr.h> 32 #include <linux/netdevice.h> 33 #include <linux/poll.h> 34 #include <linux/ppp_defs.h> 35 #include <linux/filter.h> 36 #include <linux/ppp-ioctl.h> 37 #include <linux/ppp_channel.h> 38 #include <linux/ppp-comp.h> 39 #include <linux/skbuff.h> 40 #include <linux/rtnetlink.h> 41 #include <linux/if_arp.h> 42 #include <linux/ip.h> 43 #include <linux/tcp.h> 44 #include <linux/spinlock.h> 45 #include <linux/rwsem.h> 46 #include <linux/stddef.h> 47 #include <linux/device.h> 48 #include <linux/mutex.h> 49 #include <linux/slab.h> 50 #include <linux/file.h> 51 #include <asm/unaligned.h> 52 #include <net/slhc_vj.h> 53 #include <linux/atomic.h> 54 #include <linux/refcount.h> 55 56 #include <linux/nsproxy.h> 57 #include <net/net_namespace.h> 58 #include <net/netns/generic.h> 59 60 #define PPP_VERSION "2.4.2" 61 62 /* 63 * Network protocols we support. 64 */ 65 #define NP_IP 0 /* Internet Protocol V4 */ 66 #define NP_IPV6 1 /* Internet Protocol V6 */ 67 #define NP_IPX 2 /* IPX protocol */ 68 #define NP_AT 3 /* Appletalk protocol */ 69 #define NP_MPLS_UC 4 /* MPLS unicast */ 70 #define NP_MPLS_MC 5 /* MPLS multicast */ 71 #define NUM_NP 6 /* Number of NPs. */ 72 73 #define MPHDRLEN 6 /* multilink protocol header length */ 74 #define MPHDRLEN_SSN 4 /* ditto with short sequence numbers */ 75 76 /* 77 * An instance of /dev/ppp can be associated with either a ppp 78 * interface unit or a ppp channel. In both cases, file->private_data 79 * points to one of these. 80 */ 81 struct ppp_file { 82 enum { 83 INTERFACE=1, CHANNEL 84 } kind; 85 struct sk_buff_head xq; /* pppd transmit queue */ 86 struct sk_buff_head rq; /* receive queue for pppd */ 87 wait_queue_head_t rwait; /* for poll on reading /dev/ppp */ 88 refcount_t refcnt; /* # refs (incl /dev/ppp attached) */ 89 int hdrlen; /* space to leave for headers */ 90 int index; /* interface unit / channel number */ 91 int dead; /* unit/channel has been shut down */ 92 }; 93 94 #define PF_TO_X(pf, X) container_of(pf, X, file) 95 96 #define PF_TO_PPP(pf) PF_TO_X(pf, struct ppp) 97 #define PF_TO_CHANNEL(pf) PF_TO_X(pf, struct channel) 98 99 /* 100 * Data structure to hold primary network stats for which 101 * we want to use 64 bit storage. Other network stats 102 * are stored in dev->stats of the ppp strucute. 103 */ 104 struct ppp_link_stats { 105 u64 rx_packets; 106 u64 tx_packets; 107 u64 rx_bytes; 108 u64 tx_bytes; 109 }; 110 111 /* 112 * Data structure describing one ppp unit. 113 * A ppp unit corresponds to a ppp network interface device 114 * and represents a multilink bundle. 115 * It can have 0 or more ppp channels connected to it. 116 */ 117 struct ppp { 118 struct ppp_file file; /* stuff for read/write/poll 0 */ 119 struct file *owner; /* file that owns this unit 48 */ 120 struct list_head channels; /* list of attached channels 4c */ 121 int n_channels; /* how many channels are attached 54 */ 122 spinlock_t rlock; /* lock for receive side 58 */ 123 spinlock_t wlock; /* lock for transmit side 5c */ 124 int __percpu *xmit_recursion; /* xmit recursion detect */ 125 int mru; /* max receive unit 60 */ 126 unsigned int flags; /* control bits 64 */ 127 unsigned int xstate; /* transmit state bits 68 */ 128 unsigned int rstate; /* receive state bits 6c */ 129 int debug; /* debug flags 70 */ 130 struct slcompress *vj; /* state for VJ header compression */ 131 enum NPmode npmode[NUM_NP]; /* what to do with each net proto 78 */ 132 struct sk_buff *xmit_pending; /* a packet ready to go out 88 */ 133 struct compressor *xcomp; /* transmit packet compressor 8c */ 134 void *xc_state; /* its internal state 90 */ 135 struct compressor *rcomp; /* receive decompressor 94 */ 136 void *rc_state; /* its internal state 98 */ 137 unsigned long last_xmit; /* jiffies when last pkt sent 9c */ 138 unsigned long last_recv; /* jiffies when last pkt rcvd a0 */ 139 struct net_device *dev; /* network interface device a4 */ 140 int closing; /* is device closing down? a8 */ 141 #ifdef CONFIG_PPP_MULTILINK 142 int nxchan; /* next channel to send something on */ 143 u32 nxseq; /* next sequence number to send */ 144 int mrru; /* MP: max reconst. receive unit */ 145 u32 nextseq; /* MP: seq no of next packet */ 146 u32 minseq; /* MP: min of most recent seqnos */ 147 struct sk_buff_head mrq; /* MP: receive reconstruction queue */ 148 #endif /* CONFIG_PPP_MULTILINK */ 149 #ifdef CONFIG_PPP_FILTER 150 struct bpf_prog *pass_filter; /* filter for packets to pass */ 151 struct bpf_prog *active_filter; /* filter for pkts to reset idle */ 152 #endif /* CONFIG_PPP_FILTER */ 153 struct net *ppp_net; /* the net we belong to */ 154 struct ppp_link_stats stats64; /* 64 bit network stats */ 155 }; 156 157 /* 158 * Bits in flags: SC_NO_TCP_CCID, SC_CCP_OPEN, SC_CCP_UP, SC_LOOP_TRAFFIC, 159 * SC_MULTILINK, SC_MP_SHORTSEQ, SC_MP_XSHORTSEQ, SC_COMP_TCP, SC_REJ_COMP_TCP, 160 * SC_MUST_COMP 161 * Bits in rstate: SC_DECOMP_RUN, SC_DC_ERROR, SC_DC_FERROR. 162 * Bits in xstate: SC_COMP_RUN 163 */ 164 #define SC_FLAG_BITS (SC_NO_TCP_CCID|SC_CCP_OPEN|SC_CCP_UP|SC_LOOP_TRAFFIC \ 165 |SC_MULTILINK|SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ \ 166 |SC_COMP_TCP|SC_REJ_COMP_TCP|SC_MUST_COMP) 167 168 /* 169 * Private data structure for each channel. 170 * This includes the data structure used for multilink. 171 */ 172 struct channel { 173 struct ppp_file file; /* stuff for read/write/poll */ 174 struct list_head list; /* link in all/new_channels list */ 175 struct ppp_channel *chan; /* public channel data structure */ 176 struct rw_semaphore chan_sem; /* protects `chan' during chan ioctl */ 177 spinlock_t downl; /* protects `chan', file.xq dequeue */ 178 struct ppp *ppp; /* ppp unit we're connected to */ 179 struct net *chan_net; /* the net channel belongs to */ 180 struct list_head clist; /* link in list of channels per unit */ 181 rwlock_t upl; /* protects `ppp' */ 182 #ifdef CONFIG_PPP_MULTILINK 183 u8 avail; /* flag used in multilink stuff */ 184 u8 had_frag; /* >= 1 fragments have been sent */ 185 u32 lastseq; /* MP: last sequence # received */ 186 int speed; /* speed of the corresponding ppp channel*/ 187 #endif /* CONFIG_PPP_MULTILINK */ 188 }; 189 190 struct ppp_config { 191 struct file *file; 192 s32 unit; 193 bool ifname_is_set; 194 }; 195 196 /* 197 * SMP locking issues: 198 * Both the ppp.rlock and ppp.wlock locks protect the ppp.channels 199 * list and the ppp.n_channels field, you need to take both locks 200 * before you modify them. 201 * The lock ordering is: channel.upl -> ppp.wlock -> ppp.rlock -> 202 * channel.downl. 203 */ 204 205 static DEFINE_MUTEX(ppp_mutex); 206 static atomic_t ppp_unit_count = ATOMIC_INIT(0); 207 static atomic_t channel_count = ATOMIC_INIT(0); 208 209 /* per-net private data for this module */ 210 static unsigned int ppp_net_id __read_mostly; 211 struct ppp_net { 212 /* units to ppp mapping */ 213 struct idr units_idr; 214 215 /* 216 * all_ppp_mutex protects the units_idr mapping. 217 * It also ensures that finding a ppp unit in the units_idr 218 * map and updating its file.refcnt field is atomic. 219 */ 220 struct mutex all_ppp_mutex; 221 222 /* channels */ 223 struct list_head all_channels; 224 struct list_head new_channels; 225 int last_channel_index; 226 227 /* 228 * all_channels_lock protects all_channels and 229 * last_channel_index, and the atomicity of find 230 * a channel and updating its file.refcnt field. 231 */ 232 spinlock_t all_channels_lock; 233 }; 234 235 /* Get the PPP protocol number from a skb */ 236 #define PPP_PROTO(skb) get_unaligned_be16((skb)->data) 237 238 /* We limit the length of ppp->file.rq to this (arbitrary) value */ 239 #define PPP_MAX_RQLEN 32 240 241 /* 242 * Maximum number of multilink fragments queued up. 243 * This has to be large enough to cope with the maximum latency of 244 * the slowest channel relative to the others. Strictly it should 245 * depend on the number of channels and their characteristics. 246 */ 247 #define PPP_MP_MAX_QLEN 128 248 249 /* Multilink header bits. */ 250 #define B 0x80 /* this fragment begins a packet */ 251 #define E 0x40 /* this fragment ends a packet */ 252 253 /* Compare multilink sequence numbers (assumed to be 32 bits wide) */ 254 #define seq_before(a, b) ((s32)((a) - (b)) < 0) 255 #define seq_after(a, b) ((s32)((a) - (b)) > 0) 256 257 /* Prototypes. */ 258 static int ppp_unattached_ioctl(struct net *net, struct ppp_file *pf, 259 struct file *file, unsigned int cmd, unsigned long arg); 260 static void ppp_xmit_process(struct ppp *ppp); 261 static void ppp_send_frame(struct ppp *ppp, struct sk_buff *skb); 262 static void ppp_push(struct ppp *ppp); 263 static void ppp_channel_push(struct channel *pch); 264 static void ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, 265 struct channel *pch); 266 static void ppp_receive_error(struct ppp *ppp); 267 static void ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb); 268 static struct sk_buff *ppp_decompress_frame(struct ppp *ppp, 269 struct sk_buff *skb); 270 #ifdef CONFIG_PPP_MULTILINK 271 static void ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, 272 struct channel *pch); 273 static void ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb); 274 static struct sk_buff *ppp_mp_reconstruct(struct ppp *ppp); 275 static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb); 276 #endif /* CONFIG_PPP_MULTILINK */ 277 static int ppp_set_compress(struct ppp *ppp, unsigned long arg); 278 static void ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound); 279 static void ppp_ccp_closed(struct ppp *ppp); 280 static struct compressor *find_compressor(int type); 281 static void ppp_get_stats(struct ppp *ppp, struct ppp_stats *st); 282 static int ppp_create_interface(struct net *net, struct file *file, int *unit); 283 static void init_ppp_file(struct ppp_file *pf, int kind); 284 static void ppp_destroy_interface(struct ppp *ppp); 285 static struct ppp *ppp_find_unit(struct ppp_net *pn, int unit); 286 static struct channel *ppp_find_channel(struct ppp_net *pn, int unit); 287 static int ppp_connect_channel(struct channel *pch, int unit); 288 static int ppp_disconnect_channel(struct channel *pch); 289 static void ppp_destroy_channel(struct channel *pch); 290 static int unit_get(struct idr *p, void *ptr); 291 static int unit_set(struct idr *p, void *ptr, int n); 292 static void unit_put(struct idr *p, int n); 293 static void *unit_find(struct idr *p, int n); 294 static void ppp_setup(struct net_device *dev); 295 296 static const struct net_device_ops ppp_netdev_ops; 297 298 static struct class *ppp_class; 299 300 /* per net-namespace data */ 301 static inline struct ppp_net *ppp_pernet(struct net *net) 302 { 303 BUG_ON(!net); 304 305 return net_generic(net, ppp_net_id); 306 } 307 308 /* Translates a PPP protocol number to a NP index (NP == network protocol) */ 309 static inline int proto_to_npindex(int proto) 310 { 311 switch (proto) { 312 case PPP_IP: 313 return NP_IP; 314 case PPP_IPV6: 315 return NP_IPV6; 316 case PPP_IPX: 317 return NP_IPX; 318 case PPP_AT: 319 return NP_AT; 320 case PPP_MPLS_UC: 321 return NP_MPLS_UC; 322 case PPP_MPLS_MC: 323 return NP_MPLS_MC; 324 } 325 return -EINVAL; 326 } 327 328 /* Translates an NP index into a PPP protocol number */ 329 static const int npindex_to_proto[NUM_NP] = { 330 PPP_IP, 331 PPP_IPV6, 332 PPP_IPX, 333 PPP_AT, 334 PPP_MPLS_UC, 335 PPP_MPLS_MC, 336 }; 337 338 /* Translates an ethertype into an NP index */ 339 static inline int ethertype_to_npindex(int ethertype) 340 { 341 switch (ethertype) { 342 case ETH_P_IP: 343 return NP_IP; 344 case ETH_P_IPV6: 345 return NP_IPV6; 346 case ETH_P_IPX: 347 return NP_IPX; 348 case ETH_P_PPPTALK: 349 case ETH_P_ATALK: 350 return NP_AT; 351 case ETH_P_MPLS_UC: 352 return NP_MPLS_UC; 353 case ETH_P_MPLS_MC: 354 return NP_MPLS_MC; 355 } 356 return -1; 357 } 358 359 /* Translates an NP index into an ethertype */ 360 static const int npindex_to_ethertype[NUM_NP] = { 361 ETH_P_IP, 362 ETH_P_IPV6, 363 ETH_P_IPX, 364 ETH_P_PPPTALK, 365 ETH_P_MPLS_UC, 366 ETH_P_MPLS_MC, 367 }; 368 369 /* 370 * Locking shorthand. 371 */ 372 #define ppp_xmit_lock(ppp) spin_lock_bh(&(ppp)->wlock) 373 #define ppp_xmit_unlock(ppp) spin_unlock_bh(&(ppp)->wlock) 374 #define ppp_recv_lock(ppp) spin_lock_bh(&(ppp)->rlock) 375 #define ppp_recv_unlock(ppp) spin_unlock_bh(&(ppp)->rlock) 376 #define ppp_lock(ppp) do { ppp_xmit_lock(ppp); \ 377 ppp_recv_lock(ppp); } while (0) 378 #define ppp_unlock(ppp) do { ppp_recv_unlock(ppp); \ 379 ppp_xmit_unlock(ppp); } while (0) 380 381 /* 382 * /dev/ppp device routines. 383 * The /dev/ppp device is used by pppd to control the ppp unit. 384 * It supports the read, write, ioctl and poll functions. 385 * Open instances of /dev/ppp can be in one of three states: 386 * unattached, attached to a ppp unit, or attached to a ppp channel. 387 */ 388 static int ppp_open(struct inode *inode, struct file *file) 389 { 390 /* 391 * This could (should?) be enforced by the permissions on /dev/ppp. 392 */ 393 if (!ns_capable(file->f_cred->user_ns, CAP_NET_ADMIN)) 394 return -EPERM; 395 return 0; 396 } 397 398 static int ppp_release(struct inode *unused, struct file *file) 399 { 400 struct ppp_file *pf = file->private_data; 401 struct ppp *ppp; 402 403 if (pf) { 404 file->private_data = NULL; 405 if (pf->kind == INTERFACE) { 406 ppp = PF_TO_PPP(pf); 407 rtnl_lock(); 408 if (file == ppp->owner) 409 unregister_netdevice(ppp->dev); 410 rtnl_unlock(); 411 } 412 if (refcount_dec_and_test(&pf->refcnt)) { 413 switch (pf->kind) { 414 case INTERFACE: 415 ppp_destroy_interface(PF_TO_PPP(pf)); 416 break; 417 case CHANNEL: 418 ppp_destroy_channel(PF_TO_CHANNEL(pf)); 419 break; 420 } 421 } 422 } 423 return 0; 424 } 425 426 static ssize_t ppp_read(struct file *file, char __user *buf, 427 size_t count, loff_t *ppos) 428 { 429 struct ppp_file *pf = file->private_data; 430 DECLARE_WAITQUEUE(wait, current); 431 ssize_t ret; 432 struct sk_buff *skb = NULL; 433 struct iovec iov; 434 struct iov_iter to; 435 436 ret = count; 437 438 if (!pf) 439 return -ENXIO; 440 add_wait_queue(&pf->rwait, &wait); 441 for (;;) { 442 set_current_state(TASK_INTERRUPTIBLE); 443 skb = skb_dequeue(&pf->rq); 444 if (skb) 445 break; 446 ret = 0; 447 if (pf->dead) 448 break; 449 if (pf->kind == INTERFACE) { 450 /* 451 * Return 0 (EOF) on an interface that has no 452 * channels connected, unless it is looping 453 * network traffic (demand mode). 454 */ 455 struct ppp *ppp = PF_TO_PPP(pf); 456 457 ppp_recv_lock(ppp); 458 if (ppp->n_channels == 0 && 459 (ppp->flags & SC_LOOP_TRAFFIC) == 0) { 460 ppp_recv_unlock(ppp); 461 break; 462 } 463 ppp_recv_unlock(ppp); 464 } 465 ret = -EAGAIN; 466 if (file->f_flags & O_NONBLOCK) 467 break; 468 ret = -ERESTARTSYS; 469 if (signal_pending(current)) 470 break; 471 schedule(); 472 } 473 set_current_state(TASK_RUNNING); 474 remove_wait_queue(&pf->rwait, &wait); 475 476 if (!skb) 477 goto out; 478 479 ret = -EOVERFLOW; 480 if (skb->len > count) 481 goto outf; 482 ret = -EFAULT; 483 iov.iov_base = buf; 484 iov.iov_len = count; 485 iov_iter_init(&to, READ, &iov, 1, count); 486 if (skb_copy_datagram_iter(skb, 0, &to, skb->len)) 487 goto outf; 488 ret = skb->len; 489 490 outf: 491 kfree_skb(skb); 492 out: 493 return ret; 494 } 495 496 static ssize_t ppp_write(struct file *file, const char __user *buf, 497 size_t count, loff_t *ppos) 498 { 499 struct ppp_file *pf = file->private_data; 500 struct sk_buff *skb; 501 ssize_t ret; 502 503 if (!pf) 504 return -ENXIO; 505 ret = -ENOMEM; 506 skb = alloc_skb(count + pf->hdrlen, GFP_KERNEL); 507 if (!skb) 508 goto out; 509 skb_reserve(skb, pf->hdrlen); 510 ret = -EFAULT; 511 if (copy_from_user(skb_put(skb, count), buf, count)) { 512 kfree_skb(skb); 513 goto out; 514 } 515 516 skb_queue_tail(&pf->xq, skb); 517 518 switch (pf->kind) { 519 case INTERFACE: 520 ppp_xmit_process(PF_TO_PPP(pf)); 521 break; 522 case CHANNEL: 523 ppp_channel_push(PF_TO_CHANNEL(pf)); 524 break; 525 } 526 527 ret = count; 528 529 out: 530 return ret; 531 } 532 533 /* No kernel lock - fine */ 534 static unsigned int ppp_poll(struct file *file, poll_table *wait) 535 { 536 struct ppp_file *pf = file->private_data; 537 unsigned int mask; 538 539 if (!pf) 540 return 0; 541 poll_wait(file, &pf->rwait, wait); 542 mask = POLLOUT | POLLWRNORM; 543 if (skb_peek(&pf->rq)) 544 mask |= POLLIN | POLLRDNORM; 545 if (pf->dead) 546 mask |= POLLHUP; 547 else if (pf->kind == INTERFACE) { 548 /* see comment in ppp_read */ 549 struct ppp *ppp = PF_TO_PPP(pf); 550 551 ppp_recv_lock(ppp); 552 if (ppp->n_channels == 0 && 553 (ppp->flags & SC_LOOP_TRAFFIC) == 0) 554 mask |= POLLIN | POLLRDNORM; 555 ppp_recv_unlock(ppp); 556 } 557 558 return mask; 559 } 560 561 #ifdef CONFIG_PPP_FILTER 562 static int get_filter(void __user *arg, struct sock_filter **p) 563 { 564 struct sock_fprog uprog; 565 struct sock_filter *code = NULL; 566 int len; 567 568 if (copy_from_user(&uprog, arg, sizeof(uprog))) 569 return -EFAULT; 570 571 if (!uprog.len) { 572 *p = NULL; 573 return 0; 574 } 575 576 len = uprog.len * sizeof(struct sock_filter); 577 code = memdup_user(uprog.filter, len); 578 if (IS_ERR(code)) 579 return PTR_ERR(code); 580 581 *p = code; 582 return uprog.len; 583 } 584 #endif /* CONFIG_PPP_FILTER */ 585 586 static long ppp_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 587 { 588 struct ppp_file *pf; 589 struct ppp *ppp; 590 int err = -EFAULT, val, val2, i; 591 struct ppp_idle idle; 592 struct npioctl npi; 593 int unit, cflags; 594 struct slcompress *vj; 595 void __user *argp = (void __user *)arg; 596 int __user *p = argp; 597 598 mutex_lock(&ppp_mutex); 599 600 pf = file->private_data; 601 if (!pf) { 602 err = ppp_unattached_ioctl(current->nsproxy->net_ns, 603 pf, file, cmd, arg); 604 goto out; 605 } 606 607 if (cmd == PPPIOCDETACH) { 608 /* 609 * We have to be careful here... if the file descriptor 610 * has been dup'd, we could have another process in the 611 * middle of a poll using the same file *, so we had 612 * better not free the interface data structures - 613 * instead we fail the ioctl. Even in this case, we 614 * shut down the interface if we are the owner of it. 615 * Actually, we should get rid of PPPIOCDETACH, userland 616 * (i.e. pppd) could achieve the same effect by closing 617 * this fd and reopening /dev/ppp. 618 */ 619 err = -EINVAL; 620 if (pf->kind == INTERFACE) { 621 ppp = PF_TO_PPP(pf); 622 rtnl_lock(); 623 if (file == ppp->owner) 624 unregister_netdevice(ppp->dev); 625 rtnl_unlock(); 626 } 627 if (atomic_long_read(&file->f_count) < 2) { 628 ppp_release(NULL, file); 629 err = 0; 630 } else 631 pr_warn("PPPIOCDETACH file->f_count=%ld\n", 632 atomic_long_read(&file->f_count)); 633 goto out; 634 } 635 636 if (pf->kind == CHANNEL) { 637 struct channel *pch; 638 struct ppp_channel *chan; 639 640 pch = PF_TO_CHANNEL(pf); 641 642 switch (cmd) { 643 case PPPIOCCONNECT: 644 if (get_user(unit, p)) 645 break; 646 err = ppp_connect_channel(pch, unit); 647 break; 648 649 case PPPIOCDISCONN: 650 err = ppp_disconnect_channel(pch); 651 break; 652 653 default: 654 down_read(&pch->chan_sem); 655 chan = pch->chan; 656 err = -ENOTTY; 657 if (chan && chan->ops->ioctl) 658 err = chan->ops->ioctl(chan, cmd, arg); 659 up_read(&pch->chan_sem); 660 } 661 goto out; 662 } 663 664 if (pf->kind != INTERFACE) { 665 /* can't happen */ 666 pr_err("PPP: not interface or channel??\n"); 667 err = -EINVAL; 668 goto out; 669 } 670 671 ppp = PF_TO_PPP(pf); 672 switch (cmd) { 673 case PPPIOCSMRU: 674 if (get_user(val, p)) 675 break; 676 ppp->mru = val; 677 err = 0; 678 break; 679 680 case PPPIOCSFLAGS: 681 if (get_user(val, p)) 682 break; 683 ppp_lock(ppp); 684 cflags = ppp->flags & ~val; 685 #ifdef CONFIG_PPP_MULTILINK 686 if (!(ppp->flags & SC_MULTILINK) && (val & SC_MULTILINK)) 687 ppp->nextseq = 0; 688 #endif 689 ppp->flags = val & SC_FLAG_BITS; 690 ppp_unlock(ppp); 691 if (cflags & SC_CCP_OPEN) 692 ppp_ccp_closed(ppp); 693 err = 0; 694 break; 695 696 case PPPIOCGFLAGS: 697 val = ppp->flags | ppp->xstate | ppp->rstate; 698 if (put_user(val, p)) 699 break; 700 err = 0; 701 break; 702 703 case PPPIOCSCOMPRESS: 704 err = ppp_set_compress(ppp, arg); 705 break; 706 707 case PPPIOCGUNIT: 708 if (put_user(ppp->file.index, p)) 709 break; 710 err = 0; 711 break; 712 713 case PPPIOCSDEBUG: 714 if (get_user(val, p)) 715 break; 716 ppp->debug = val; 717 err = 0; 718 break; 719 720 case PPPIOCGDEBUG: 721 if (put_user(ppp->debug, p)) 722 break; 723 err = 0; 724 break; 725 726 case PPPIOCGIDLE: 727 idle.xmit_idle = (jiffies - ppp->last_xmit) / HZ; 728 idle.recv_idle = (jiffies - ppp->last_recv) / HZ; 729 if (copy_to_user(argp, &idle, sizeof(idle))) 730 break; 731 err = 0; 732 break; 733 734 case PPPIOCSMAXCID: 735 if (get_user(val, p)) 736 break; 737 val2 = 15; 738 if ((val >> 16) != 0) { 739 val2 = val >> 16; 740 val &= 0xffff; 741 } 742 vj = slhc_init(val2+1, val+1); 743 if (IS_ERR(vj)) { 744 err = PTR_ERR(vj); 745 break; 746 } 747 ppp_lock(ppp); 748 if (ppp->vj) 749 slhc_free(ppp->vj); 750 ppp->vj = vj; 751 ppp_unlock(ppp); 752 err = 0; 753 break; 754 755 case PPPIOCGNPMODE: 756 case PPPIOCSNPMODE: 757 if (copy_from_user(&npi, argp, sizeof(npi))) 758 break; 759 err = proto_to_npindex(npi.protocol); 760 if (err < 0) 761 break; 762 i = err; 763 if (cmd == PPPIOCGNPMODE) { 764 err = -EFAULT; 765 npi.mode = ppp->npmode[i]; 766 if (copy_to_user(argp, &npi, sizeof(npi))) 767 break; 768 } else { 769 ppp->npmode[i] = npi.mode; 770 /* we may be able to transmit more packets now (??) */ 771 netif_wake_queue(ppp->dev); 772 } 773 err = 0; 774 break; 775 776 #ifdef CONFIG_PPP_FILTER 777 case PPPIOCSPASS: 778 { 779 struct sock_filter *code; 780 781 err = get_filter(argp, &code); 782 if (err >= 0) { 783 struct bpf_prog *pass_filter = NULL; 784 struct sock_fprog_kern fprog = { 785 .len = err, 786 .filter = code, 787 }; 788 789 err = 0; 790 if (fprog.filter) 791 err = bpf_prog_create(&pass_filter, &fprog); 792 if (!err) { 793 ppp_lock(ppp); 794 if (ppp->pass_filter) 795 bpf_prog_destroy(ppp->pass_filter); 796 ppp->pass_filter = pass_filter; 797 ppp_unlock(ppp); 798 } 799 kfree(code); 800 } 801 break; 802 } 803 case PPPIOCSACTIVE: 804 { 805 struct sock_filter *code; 806 807 err = get_filter(argp, &code); 808 if (err >= 0) { 809 struct bpf_prog *active_filter = NULL; 810 struct sock_fprog_kern fprog = { 811 .len = err, 812 .filter = code, 813 }; 814 815 err = 0; 816 if (fprog.filter) 817 err = bpf_prog_create(&active_filter, &fprog); 818 if (!err) { 819 ppp_lock(ppp); 820 if (ppp->active_filter) 821 bpf_prog_destroy(ppp->active_filter); 822 ppp->active_filter = active_filter; 823 ppp_unlock(ppp); 824 } 825 kfree(code); 826 } 827 break; 828 } 829 #endif /* CONFIG_PPP_FILTER */ 830 831 #ifdef CONFIG_PPP_MULTILINK 832 case PPPIOCSMRRU: 833 if (get_user(val, p)) 834 break; 835 ppp_recv_lock(ppp); 836 ppp->mrru = val; 837 ppp_recv_unlock(ppp); 838 err = 0; 839 break; 840 #endif /* CONFIG_PPP_MULTILINK */ 841 842 default: 843 err = -ENOTTY; 844 } 845 846 out: 847 mutex_unlock(&ppp_mutex); 848 849 return err; 850 } 851 852 static int ppp_unattached_ioctl(struct net *net, struct ppp_file *pf, 853 struct file *file, unsigned int cmd, unsigned long arg) 854 { 855 int unit, err = -EFAULT; 856 struct ppp *ppp; 857 struct channel *chan; 858 struct ppp_net *pn; 859 int __user *p = (int __user *)arg; 860 861 switch (cmd) { 862 case PPPIOCNEWUNIT: 863 /* Create a new ppp unit */ 864 if (get_user(unit, p)) 865 break; 866 err = ppp_create_interface(net, file, &unit); 867 if (err < 0) 868 break; 869 870 err = -EFAULT; 871 if (put_user(unit, p)) 872 break; 873 err = 0; 874 break; 875 876 case PPPIOCATTACH: 877 /* Attach to an existing ppp unit */ 878 if (get_user(unit, p)) 879 break; 880 err = -ENXIO; 881 pn = ppp_pernet(net); 882 mutex_lock(&pn->all_ppp_mutex); 883 ppp = ppp_find_unit(pn, unit); 884 if (ppp) { 885 refcount_inc(&ppp->file.refcnt); 886 file->private_data = &ppp->file; 887 err = 0; 888 } 889 mutex_unlock(&pn->all_ppp_mutex); 890 break; 891 892 case PPPIOCATTCHAN: 893 if (get_user(unit, p)) 894 break; 895 err = -ENXIO; 896 pn = ppp_pernet(net); 897 spin_lock_bh(&pn->all_channels_lock); 898 chan = ppp_find_channel(pn, unit); 899 if (chan) { 900 refcount_inc(&chan->file.refcnt); 901 file->private_data = &chan->file; 902 err = 0; 903 } 904 spin_unlock_bh(&pn->all_channels_lock); 905 break; 906 907 default: 908 err = -ENOTTY; 909 } 910 911 return err; 912 } 913 914 static const struct file_operations ppp_device_fops = { 915 .owner = THIS_MODULE, 916 .read = ppp_read, 917 .write = ppp_write, 918 .poll = ppp_poll, 919 .unlocked_ioctl = ppp_ioctl, 920 .open = ppp_open, 921 .release = ppp_release, 922 .llseek = noop_llseek, 923 }; 924 925 static __net_init int ppp_init_net(struct net *net) 926 { 927 struct ppp_net *pn = net_generic(net, ppp_net_id); 928 929 idr_init(&pn->units_idr); 930 mutex_init(&pn->all_ppp_mutex); 931 932 INIT_LIST_HEAD(&pn->all_channels); 933 INIT_LIST_HEAD(&pn->new_channels); 934 935 spin_lock_init(&pn->all_channels_lock); 936 937 return 0; 938 } 939 940 static __net_exit void ppp_exit_net(struct net *net) 941 { 942 struct ppp_net *pn = net_generic(net, ppp_net_id); 943 struct net_device *dev; 944 struct net_device *aux; 945 struct ppp *ppp; 946 LIST_HEAD(list); 947 int id; 948 949 rtnl_lock(); 950 for_each_netdev_safe(net, dev, aux) { 951 if (dev->netdev_ops == &ppp_netdev_ops) 952 unregister_netdevice_queue(dev, &list); 953 } 954 955 idr_for_each_entry(&pn->units_idr, ppp, id) 956 /* Skip devices already unregistered by previous loop */ 957 if (!net_eq(dev_net(ppp->dev), net)) 958 unregister_netdevice_queue(ppp->dev, &list); 959 960 unregister_netdevice_many(&list); 961 rtnl_unlock(); 962 963 idr_destroy(&pn->units_idr); 964 } 965 966 static struct pernet_operations ppp_net_ops = { 967 .init = ppp_init_net, 968 .exit = ppp_exit_net, 969 .id = &ppp_net_id, 970 .size = sizeof(struct ppp_net), 971 }; 972 973 static int ppp_unit_register(struct ppp *ppp, int unit, bool ifname_is_set) 974 { 975 struct ppp_net *pn = ppp_pernet(ppp->ppp_net); 976 int ret; 977 978 mutex_lock(&pn->all_ppp_mutex); 979 980 if (unit < 0) { 981 ret = unit_get(&pn->units_idr, ppp); 982 if (ret < 0) 983 goto err; 984 } else { 985 /* Caller asked for a specific unit number. Fail with -EEXIST 986 * if unavailable. For backward compatibility, return -EEXIST 987 * too if idr allocation fails; this makes pppd retry without 988 * requesting a specific unit number. 989 */ 990 if (unit_find(&pn->units_idr, unit)) { 991 ret = -EEXIST; 992 goto err; 993 } 994 ret = unit_set(&pn->units_idr, ppp, unit); 995 if (ret < 0) { 996 /* Rewrite error for backward compatibility */ 997 ret = -EEXIST; 998 goto err; 999 } 1000 } 1001 ppp->file.index = ret; 1002 1003 if (!ifname_is_set) 1004 snprintf(ppp->dev->name, IFNAMSIZ, "ppp%i", ppp->file.index); 1005 1006 ret = register_netdevice(ppp->dev); 1007 if (ret < 0) 1008 goto err_unit; 1009 1010 atomic_inc(&ppp_unit_count); 1011 1012 mutex_unlock(&pn->all_ppp_mutex); 1013 1014 return 0; 1015 1016 err_unit: 1017 unit_put(&pn->units_idr, ppp->file.index); 1018 err: 1019 mutex_unlock(&pn->all_ppp_mutex); 1020 1021 return ret; 1022 } 1023 1024 static int ppp_dev_configure(struct net *src_net, struct net_device *dev, 1025 const struct ppp_config *conf) 1026 { 1027 struct ppp *ppp = netdev_priv(dev); 1028 int indx; 1029 int err; 1030 int cpu; 1031 1032 ppp->dev = dev; 1033 ppp->ppp_net = src_net; 1034 ppp->mru = PPP_MRU; 1035 ppp->owner = conf->file; 1036 1037 init_ppp_file(&ppp->file, INTERFACE); 1038 ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */ 1039 1040 for (indx = 0; indx < NUM_NP; ++indx) 1041 ppp->npmode[indx] = NPMODE_PASS; 1042 INIT_LIST_HEAD(&ppp->channels); 1043 spin_lock_init(&ppp->rlock); 1044 spin_lock_init(&ppp->wlock); 1045 1046 ppp->xmit_recursion = alloc_percpu(int); 1047 if (!ppp->xmit_recursion) { 1048 err = -ENOMEM; 1049 goto err1; 1050 } 1051 for_each_possible_cpu(cpu) 1052 (*per_cpu_ptr(ppp->xmit_recursion, cpu)) = 0; 1053 1054 #ifdef CONFIG_PPP_MULTILINK 1055 ppp->minseq = -1; 1056 skb_queue_head_init(&ppp->mrq); 1057 #endif /* CONFIG_PPP_MULTILINK */ 1058 #ifdef CONFIG_PPP_FILTER 1059 ppp->pass_filter = NULL; 1060 ppp->active_filter = NULL; 1061 #endif /* CONFIG_PPP_FILTER */ 1062 1063 err = ppp_unit_register(ppp, conf->unit, conf->ifname_is_set); 1064 if (err < 0) 1065 goto err2; 1066 1067 conf->file->private_data = &ppp->file; 1068 1069 return 0; 1070 err2: 1071 free_percpu(ppp->xmit_recursion); 1072 err1: 1073 return err; 1074 } 1075 1076 static const struct nla_policy ppp_nl_policy[IFLA_PPP_MAX + 1] = { 1077 [IFLA_PPP_DEV_FD] = { .type = NLA_S32 }, 1078 }; 1079 1080 static int ppp_nl_validate(struct nlattr *tb[], struct nlattr *data[], 1081 struct netlink_ext_ack *extack) 1082 { 1083 if (!data) 1084 return -EINVAL; 1085 1086 if (!data[IFLA_PPP_DEV_FD]) 1087 return -EINVAL; 1088 if (nla_get_s32(data[IFLA_PPP_DEV_FD]) < 0) 1089 return -EBADF; 1090 1091 return 0; 1092 } 1093 1094 static int ppp_nl_newlink(struct net *src_net, struct net_device *dev, 1095 struct nlattr *tb[], struct nlattr *data[], 1096 struct netlink_ext_ack *extack) 1097 { 1098 struct ppp_config conf = { 1099 .unit = -1, 1100 .ifname_is_set = true, 1101 }; 1102 struct file *file; 1103 int err; 1104 1105 file = fget(nla_get_s32(data[IFLA_PPP_DEV_FD])); 1106 if (!file) 1107 return -EBADF; 1108 1109 /* rtnl_lock is already held here, but ppp_create_interface() locks 1110 * ppp_mutex before holding rtnl_lock. Using mutex_trylock() avoids 1111 * possible deadlock due to lock order inversion, at the cost of 1112 * pushing the problem back to userspace. 1113 */ 1114 if (!mutex_trylock(&ppp_mutex)) { 1115 err = -EBUSY; 1116 goto out; 1117 } 1118 1119 if (file->f_op != &ppp_device_fops || file->private_data) { 1120 err = -EBADF; 1121 goto out_unlock; 1122 } 1123 1124 conf.file = file; 1125 1126 /* Don't use device name generated by the rtnetlink layer when ifname 1127 * isn't specified. Let ppp_dev_configure() set the device name using 1128 * the PPP unit identifer as suffix (i.e. ppp<unit_id>). This allows 1129 * userspace to infer the device name using to the PPPIOCGUNIT ioctl. 1130 */ 1131 if (!tb[IFLA_IFNAME]) 1132 conf.ifname_is_set = false; 1133 1134 err = ppp_dev_configure(src_net, dev, &conf); 1135 1136 out_unlock: 1137 mutex_unlock(&ppp_mutex); 1138 out: 1139 fput(file); 1140 1141 return err; 1142 } 1143 1144 static void ppp_nl_dellink(struct net_device *dev, struct list_head *head) 1145 { 1146 unregister_netdevice_queue(dev, head); 1147 } 1148 1149 static size_t ppp_nl_get_size(const struct net_device *dev) 1150 { 1151 return 0; 1152 } 1153 1154 static int ppp_nl_fill_info(struct sk_buff *skb, const struct net_device *dev) 1155 { 1156 return 0; 1157 } 1158 1159 static struct net *ppp_nl_get_link_net(const struct net_device *dev) 1160 { 1161 struct ppp *ppp = netdev_priv(dev); 1162 1163 return ppp->ppp_net; 1164 } 1165 1166 static struct rtnl_link_ops ppp_link_ops __read_mostly = { 1167 .kind = "ppp", 1168 .maxtype = IFLA_PPP_MAX, 1169 .policy = ppp_nl_policy, 1170 .priv_size = sizeof(struct ppp), 1171 .setup = ppp_setup, 1172 .validate = ppp_nl_validate, 1173 .newlink = ppp_nl_newlink, 1174 .dellink = ppp_nl_dellink, 1175 .get_size = ppp_nl_get_size, 1176 .fill_info = ppp_nl_fill_info, 1177 .get_link_net = ppp_nl_get_link_net, 1178 }; 1179 1180 #define PPP_MAJOR 108 1181 1182 /* Called at boot time if ppp is compiled into the kernel, 1183 or at module load time (from init_module) if compiled as a module. */ 1184 static int __init ppp_init(void) 1185 { 1186 int err; 1187 1188 pr_info("PPP generic driver version " PPP_VERSION "\n"); 1189 1190 err = register_pernet_device(&ppp_net_ops); 1191 if (err) { 1192 pr_err("failed to register PPP pernet device (%d)\n", err); 1193 goto out; 1194 } 1195 1196 err = register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops); 1197 if (err) { 1198 pr_err("failed to register PPP device (%d)\n", err); 1199 goto out_net; 1200 } 1201 1202 ppp_class = class_create(THIS_MODULE, "ppp"); 1203 if (IS_ERR(ppp_class)) { 1204 err = PTR_ERR(ppp_class); 1205 goto out_chrdev; 1206 } 1207 1208 err = rtnl_link_register(&ppp_link_ops); 1209 if (err) { 1210 pr_err("failed to register rtnetlink PPP handler\n"); 1211 goto out_class; 1212 } 1213 1214 /* not a big deal if we fail here :-) */ 1215 device_create(ppp_class, NULL, MKDEV(PPP_MAJOR, 0), NULL, "ppp"); 1216 1217 return 0; 1218 1219 out_class: 1220 class_destroy(ppp_class); 1221 out_chrdev: 1222 unregister_chrdev(PPP_MAJOR, "ppp"); 1223 out_net: 1224 unregister_pernet_device(&ppp_net_ops); 1225 out: 1226 return err; 1227 } 1228 1229 /* 1230 * Network interface unit routines. 1231 */ 1232 static netdev_tx_t 1233 ppp_start_xmit(struct sk_buff *skb, struct net_device *dev) 1234 { 1235 struct ppp *ppp = netdev_priv(dev); 1236 int npi, proto; 1237 unsigned char *pp; 1238 1239 npi = ethertype_to_npindex(ntohs(skb->protocol)); 1240 if (npi < 0) 1241 goto outf; 1242 1243 /* Drop, accept or reject the packet */ 1244 switch (ppp->npmode[npi]) { 1245 case NPMODE_PASS: 1246 break; 1247 case NPMODE_QUEUE: 1248 /* it would be nice to have a way to tell the network 1249 system to queue this one up for later. */ 1250 goto outf; 1251 case NPMODE_DROP: 1252 case NPMODE_ERROR: 1253 goto outf; 1254 } 1255 1256 /* Put the 2-byte PPP protocol number on the front, 1257 making sure there is room for the address and control fields. */ 1258 if (skb_cow_head(skb, PPP_HDRLEN)) 1259 goto outf; 1260 1261 pp = skb_push(skb, 2); 1262 proto = npindex_to_proto[npi]; 1263 put_unaligned_be16(proto, pp); 1264 1265 skb_scrub_packet(skb, !net_eq(ppp->ppp_net, dev_net(dev))); 1266 skb_queue_tail(&ppp->file.xq, skb); 1267 ppp_xmit_process(ppp); 1268 return NETDEV_TX_OK; 1269 1270 outf: 1271 kfree_skb(skb); 1272 ++dev->stats.tx_dropped; 1273 return NETDEV_TX_OK; 1274 } 1275 1276 static int 1277 ppp_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 1278 { 1279 struct ppp *ppp = netdev_priv(dev); 1280 int err = -EFAULT; 1281 void __user *addr = (void __user *) ifr->ifr_ifru.ifru_data; 1282 struct ppp_stats stats; 1283 struct ppp_comp_stats cstats; 1284 char *vers; 1285 1286 switch (cmd) { 1287 case SIOCGPPPSTATS: 1288 ppp_get_stats(ppp, &stats); 1289 if (copy_to_user(addr, &stats, sizeof(stats))) 1290 break; 1291 err = 0; 1292 break; 1293 1294 case SIOCGPPPCSTATS: 1295 memset(&cstats, 0, sizeof(cstats)); 1296 if (ppp->xc_state) 1297 ppp->xcomp->comp_stat(ppp->xc_state, &cstats.c); 1298 if (ppp->rc_state) 1299 ppp->rcomp->decomp_stat(ppp->rc_state, &cstats.d); 1300 if (copy_to_user(addr, &cstats, sizeof(cstats))) 1301 break; 1302 err = 0; 1303 break; 1304 1305 case SIOCGPPPVER: 1306 vers = PPP_VERSION; 1307 if (copy_to_user(addr, vers, strlen(vers) + 1)) 1308 break; 1309 err = 0; 1310 break; 1311 1312 default: 1313 err = -EINVAL; 1314 } 1315 1316 return err; 1317 } 1318 1319 static void 1320 ppp_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats64) 1321 { 1322 struct ppp *ppp = netdev_priv(dev); 1323 1324 ppp_recv_lock(ppp); 1325 stats64->rx_packets = ppp->stats64.rx_packets; 1326 stats64->rx_bytes = ppp->stats64.rx_bytes; 1327 ppp_recv_unlock(ppp); 1328 1329 ppp_xmit_lock(ppp); 1330 stats64->tx_packets = ppp->stats64.tx_packets; 1331 stats64->tx_bytes = ppp->stats64.tx_bytes; 1332 ppp_xmit_unlock(ppp); 1333 1334 stats64->rx_errors = dev->stats.rx_errors; 1335 stats64->tx_errors = dev->stats.tx_errors; 1336 stats64->rx_dropped = dev->stats.rx_dropped; 1337 stats64->tx_dropped = dev->stats.tx_dropped; 1338 stats64->rx_length_errors = dev->stats.rx_length_errors; 1339 } 1340 1341 static int ppp_dev_init(struct net_device *dev) 1342 { 1343 struct ppp *ppp; 1344 1345 netdev_lockdep_set_classes(dev); 1346 1347 ppp = netdev_priv(dev); 1348 /* Let the netdevice take a reference on the ppp file. This ensures 1349 * that ppp_destroy_interface() won't run before the device gets 1350 * unregistered. 1351 */ 1352 refcount_inc(&ppp->file.refcnt); 1353 1354 return 0; 1355 } 1356 1357 static void ppp_dev_uninit(struct net_device *dev) 1358 { 1359 struct ppp *ppp = netdev_priv(dev); 1360 struct ppp_net *pn = ppp_pernet(ppp->ppp_net); 1361 1362 ppp_lock(ppp); 1363 ppp->closing = 1; 1364 ppp_unlock(ppp); 1365 1366 mutex_lock(&pn->all_ppp_mutex); 1367 unit_put(&pn->units_idr, ppp->file.index); 1368 mutex_unlock(&pn->all_ppp_mutex); 1369 1370 ppp->owner = NULL; 1371 1372 ppp->file.dead = 1; 1373 wake_up_interruptible(&ppp->file.rwait); 1374 } 1375 1376 static void ppp_dev_priv_destructor(struct net_device *dev) 1377 { 1378 struct ppp *ppp; 1379 1380 ppp = netdev_priv(dev); 1381 if (refcount_dec_and_test(&ppp->file.refcnt)) 1382 ppp_destroy_interface(ppp); 1383 } 1384 1385 static const struct net_device_ops ppp_netdev_ops = { 1386 .ndo_init = ppp_dev_init, 1387 .ndo_uninit = ppp_dev_uninit, 1388 .ndo_start_xmit = ppp_start_xmit, 1389 .ndo_do_ioctl = ppp_net_ioctl, 1390 .ndo_get_stats64 = ppp_get_stats64, 1391 }; 1392 1393 static struct device_type ppp_type = { 1394 .name = "ppp", 1395 }; 1396 1397 static void ppp_setup(struct net_device *dev) 1398 { 1399 dev->netdev_ops = &ppp_netdev_ops; 1400 SET_NETDEV_DEVTYPE(dev, &ppp_type); 1401 1402 dev->features |= NETIF_F_LLTX; 1403 1404 dev->hard_header_len = PPP_HDRLEN; 1405 dev->mtu = PPP_MRU; 1406 dev->addr_len = 0; 1407 dev->tx_queue_len = 3; 1408 dev->type = ARPHRD_PPP; 1409 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST; 1410 dev->priv_destructor = ppp_dev_priv_destructor; 1411 netif_keep_dst(dev); 1412 } 1413 1414 /* 1415 * Transmit-side routines. 1416 */ 1417 1418 /* Called to do any work queued up on the transmit side that can now be done */ 1419 static void __ppp_xmit_process(struct ppp *ppp) 1420 { 1421 struct sk_buff *skb; 1422 1423 ppp_xmit_lock(ppp); 1424 if (!ppp->closing) { 1425 ppp_push(ppp); 1426 while (!ppp->xmit_pending && 1427 (skb = skb_dequeue(&ppp->file.xq))) 1428 ppp_send_frame(ppp, skb); 1429 /* If there's no work left to do, tell the core net 1430 code that we can accept some more. */ 1431 if (!ppp->xmit_pending && !skb_peek(&ppp->file.xq)) 1432 netif_wake_queue(ppp->dev); 1433 else 1434 netif_stop_queue(ppp->dev); 1435 } 1436 ppp_xmit_unlock(ppp); 1437 } 1438 1439 static void ppp_xmit_process(struct ppp *ppp) 1440 { 1441 local_bh_disable(); 1442 1443 if (unlikely(*this_cpu_ptr(ppp->xmit_recursion))) 1444 goto err; 1445 1446 (*this_cpu_ptr(ppp->xmit_recursion))++; 1447 __ppp_xmit_process(ppp); 1448 (*this_cpu_ptr(ppp->xmit_recursion))--; 1449 1450 local_bh_enable(); 1451 1452 return; 1453 1454 err: 1455 local_bh_enable(); 1456 1457 if (net_ratelimit()) 1458 netdev_err(ppp->dev, "recursion detected\n"); 1459 } 1460 1461 static inline struct sk_buff * 1462 pad_compress_skb(struct ppp *ppp, struct sk_buff *skb) 1463 { 1464 struct sk_buff *new_skb; 1465 int len; 1466 int new_skb_size = ppp->dev->mtu + 1467 ppp->xcomp->comp_extra + ppp->dev->hard_header_len; 1468 int compressor_skb_size = ppp->dev->mtu + 1469 ppp->xcomp->comp_extra + PPP_HDRLEN; 1470 new_skb = alloc_skb(new_skb_size, GFP_ATOMIC); 1471 if (!new_skb) { 1472 if (net_ratelimit()) 1473 netdev_err(ppp->dev, "PPP: no memory (comp pkt)\n"); 1474 return NULL; 1475 } 1476 if (ppp->dev->hard_header_len > PPP_HDRLEN) 1477 skb_reserve(new_skb, 1478 ppp->dev->hard_header_len - PPP_HDRLEN); 1479 1480 /* compressor still expects A/C bytes in hdr */ 1481 len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2, 1482 new_skb->data, skb->len + 2, 1483 compressor_skb_size); 1484 if (len > 0 && (ppp->flags & SC_CCP_UP)) { 1485 consume_skb(skb); 1486 skb = new_skb; 1487 skb_put(skb, len); 1488 skb_pull(skb, 2); /* pull off A/C bytes */ 1489 } else if (len == 0) { 1490 /* didn't compress, or CCP not up yet */ 1491 consume_skb(new_skb); 1492 new_skb = skb; 1493 } else { 1494 /* 1495 * (len < 0) 1496 * MPPE requires that we do not send unencrypted 1497 * frames. The compressor will return -1 if we 1498 * should drop the frame. We cannot simply test 1499 * the compress_proto because MPPE and MPPC share 1500 * the same number. 1501 */ 1502 if (net_ratelimit()) 1503 netdev_err(ppp->dev, "ppp: compressor dropped pkt\n"); 1504 kfree_skb(skb); 1505 consume_skb(new_skb); 1506 new_skb = NULL; 1507 } 1508 return new_skb; 1509 } 1510 1511 /* 1512 * Compress and send a frame. 1513 * The caller should have locked the xmit path, 1514 * and xmit_pending should be 0. 1515 */ 1516 static void 1517 ppp_send_frame(struct ppp *ppp, struct sk_buff *skb) 1518 { 1519 int proto = PPP_PROTO(skb); 1520 struct sk_buff *new_skb; 1521 int len; 1522 unsigned char *cp; 1523 1524 if (proto < 0x8000) { 1525 #ifdef CONFIG_PPP_FILTER 1526 /* check if we should pass this packet */ 1527 /* the filter instructions are constructed assuming 1528 a four-byte PPP header on each packet */ 1529 *(u8 *)skb_push(skb, 2) = 1; 1530 if (ppp->pass_filter && 1531 BPF_PROG_RUN(ppp->pass_filter, skb) == 0) { 1532 if (ppp->debug & 1) 1533 netdev_printk(KERN_DEBUG, ppp->dev, 1534 "PPP: outbound frame " 1535 "not passed\n"); 1536 kfree_skb(skb); 1537 return; 1538 } 1539 /* if this packet passes the active filter, record the time */ 1540 if (!(ppp->active_filter && 1541 BPF_PROG_RUN(ppp->active_filter, skb) == 0)) 1542 ppp->last_xmit = jiffies; 1543 skb_pull(skb, 2); 1544 #else 1545 /* for data packets, record the time */ 1546 ppp->last_xmit = jiffies; 1547 #endif /* CONFIG_PPP_FILTER */ 1548 } 1549 1550 ++ppp->stats64.tx_packets; 1551 ppp->stats64.tx_bytes += skb->len - 2; 1552 1553 switch (proto) { 1554 case PPP_IP: 1555 if (!ppp->vj || (ppp->flags & SC_COMP_TCP) == 0) 1556 break; 1557 /* try to do VJ TCP header compression */ 1558 new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2, 1559 GFP_ATOMIC); 1560 if (!new_skb) { 1561 netdev_err(ppp->dev, "PPP: no memory (VJ comp pkt)\n"); 1562 goto drop; 1563 } 1564 skb_reserve(new_skb, ppp->dev->hard_header_len - 2); 1565 cp = skb->data + 2; 1566 len = slhc_compress(ppp->vj, cp, skb->len - 2, 1567 new_skb->data + 2, &cp, 1568 !(ppp->flags & SC_NO_TCP_CCID)); 1569 if (cp == skb->data + 2) { 1570 /* didn't compress */ 1571 consume_skb(new_skb); 1572 } else { 1573 if (cp[0] & SL_TYPE_COMPRESSED_TCP) { 1574 proto = PPP_VJC_COMP; 1575 cp[0] &= ~SL_TYPE_COMPRESSED_TCP; 1576 } else { 1577 proto = PPP_VJC_UNCOMP; 1578 cp[0] = skb->data[2]; 1579 } 1580 consume_skb(skb); 1581 skb = new_skb; 1582 cp = skb_put(skb, len + 2); 1583 cp[0] = 0; 1584 cp[1] = proto; 1585 } 1586 break; 1587 1588 case PPP_CCP: 1589 /* peek at outbound CCP frames */ 1590 ppp_ccp_peek(ppp, skb, 0); 1591 break; 1592 } 1593 1594 /* try to do packet compression */ 1595 if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state && 1596 proto != PPP_LCP && proto != PPP_CCP) { 1597 if (!(ppp->flags & SC_CCP_UP) && (ppp->flags & SC_MUST_COMP)) { 1598 if (net_ratelimit()) 1599 netdev_err(ppp->dev, 1600 "ppp: compression required but " 1601 "down - pkt dropped.\n"); 1602 goto drop; 1603 } 1604 skb = pad_compress_skb(ppp, skb); 1605 if (!skb) 1606 goto drop; 1607 } 1608 1609 /* 1610 * If we are waiting for traffic (demand dialling), 1611 * queue it up for pppd to receive. 1612 */ 1613 if (ppp->flags & SC_LOOP_TRAFFIC) { 1614 if (ppp->file.rq.qlen > PPP_MAX_RQLEN) 1615 goto drop; 1616 skb_queue_tail(&ppp->file.rq, skb); 1617 wake_up_interruptible(&ppp->file.rwait); 1618 return; 1619 } 1620 1621 ppp->xmit_pending = skb; 1622 ppp_push(ppp); 1623 return; 1624 1625 drop: 1626 kfree_skb(skb); 1627 ++ppp->dev->stats.tx_errors; 1628 } 1629 1630 /* 1631 * Try to send the frame in xmit_pending. 1632 * The caller should have the xmit path locked. 1633 */ 1634 static void 1635 ppp_push(struct ppp *ppp) 1636 { 1637 struct list_head *list; 1638 struct channel *pch; 1639 struct sk_buff *skb = ppp->xmit_pending; 1640 1641 if (!skb) 1642 return; 1643 1644 list = &ppp->channels; 1645 if (list_empty(list)) { 1646 /* nowhere to send the packet, just drop it */ 1647 ppp->xmit_pending = NULL; 1648 kfree_skb(skb); 1649 return; 1650 } 1651 1652 if ((ppp->flags & SC_MULTILINK) == 0) { 1653 /* not doing multilink: send it down the first channel */ 1654 list = list->next; 1655 pch = list_entry(list, struct channel, clist); 1656 1657 spin_lock(&pch->downl); 1658 if (pch->chan) { 1659 if (pch->chan->ops->start_xmit(pch->chan, skb)) 1660 ppp->xmit_pending = NULL; 1661 } else { 1662 /* channel got unregistered */ 1663 kfree_skb(skb); 1664 ppp->xmit_pending = NULL; 1665 } 1666 spin_unlock(&pch->downl); 1667 return; 1668 } 1669 1670 #ifdef CONFIG_PPP_MULTILINK 1671 /* Multilink: fragment the packet over as many links 1672 as can take the packet at the moment. */ 1673 if (!ppp_mp_explode(ppp, skb)) 1674 return; 1675 #endif /* CONFIG_PPP_MULTILINK */ 1676 1677 ppp->xmit_pending = NULL; 1678 kfree_skb(skb); 1679 } 1680 1681 #ifdef CONFIG_PPP_MULTILINK 1682 static bool mp_protocol_compress __read_mostly = true; 1683 module_param(mp_protocol_compress, bool, S_IRUGO | S_IWUSR); 1684 MODULE_PARM_DESC(mp_protocol_compress, 1685 "compress protocol id in multilink fragments"); 1686 1687 /* 1688 * Divide a packet to be transmitted into fragments and 1689 * send them out the individual links. 1690 */ 1691 static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb) 1692 { 1693 int len, totlen; 1694 int i, bits, hdrlen, mtu; 1695 int flen; 1696 int navail, nfree, nzero; 1697 int nbigger; 1698 int totspeed; 1699 int totfree; 1700 unsigned char *p, *q; 1701 struct list_head *list; 1702 struct channel *pch; 1703 struct sk_buff *frag; 1704 struct ppp_channel *chan; 1705 1706 totspeed = 0; /*total bitrate of the bundle*/ 1707 nfree = 0; /* # channels which have no packet already queued */ 1708 navail = 0; /* total # of usable channels (not deregistered) */ 1709 nzero = 0; /* number of channels with zero speed associated*/ 1710 totfree = 0; /*total # of channels available and 1711 *having no queued packets before 1712 *starting the fragmentation*/ 1713 1714 hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN; 1715 i = 0; 1716 list_for_each_entry(pch, &ppp->channels, clist) { 1717 if (pch->chan) { 1718 pch->avail = 1; 1719 navail++; 1720 pch->speed = pch->chan->speed; 1721 } else { 1722 pch->avail = 0; 1723 } 1724 if (pch->avail) { 1725 if (skb_queue_empty(&pch->file.xq) || 1726 !pch->had_frag) { 1727 if (pch->speed == 0) 1728 nzero++; 1729 else 1730 totspeed += pch->speed; 1731 1732 pch->avail = 2; 1733 ++nfree; 1734 ++totfree; 1735 } 1736 if (!pch->had_frag && i < ppp->nxchan) 1737 ppp->nxchan = i; 1738 } 1739 ++i; 1740 } 1741 /* 1742 * Don't start sending this packet unless at least half of 1743 * the channels are free. This gives much better TCP 1744 * performance if we have a lot of channels. 1745 */ 1746 if (nfree == 0 || nfree < navail / 2) 1747 return 0; /* can't take now, leave it in xmit_pending */ 1748 1749 /* Do protocol field compression */ 1750 p = skb->data; 1751 len = skb->len; 1752 if (*p == 0 && mp_protocol_compress) { 1753 ++p; 1754 --len; 1755 } 1756 1757 totlen = len; 1758 nbigger = len % nfree; 1759 1760 /* skip to the channel after the one we last used 1761 and start at that one */ 1762 list = &ppp->channels; 1763 for (i = 0; i < ppp->nxchan; ++i) { 1764 list = list->next; 1765 if (list == &ppp->channels) { 1766 i = 0; 1767 break; 1768 } 1769 } 1770 1771 /* create a fragment for each channel */ 1772 bits = B; 1773 while (len > 0) { 1774 list = list->next; 1775 if (list == &ppp->channels) { 1776 i = 0; 1777 continue; 1778 } 1779 pch = list_entry(list, struct channel, clist); 1780 ++i; 1781 if (!pch->avail) 1782 continue; 1783 1784 /* 1785 * Skip this channel if it has a fragment pending already and 1786 * we haven't given a fragment to all of the free channels. 1787 */ 1788 if (pch->avail == 1) { 1789 if (nfree > 0) 1790 continue; 1791 } else { 1792 pch->avail = 1; 1793 } 1794 1795 /* check the channel's mtu and whether it is still attached. */ 1796 spin_lock(&pch->downl); 1797 if (pch->chan == NULL) { 1798 /* can't use this channel, it's being deregistered */ 1799 if (pch->speed == 0) 1800 nzero--; 1801 else 1802 totspeed -= pch->speed; 1803 1804 spin_unlock(&pch->downl); 1805 pch->avail = 0; 1806 totlen = len; 1807 totfree--; 1808 nfree--; 1809 if (--navail == 0) 1810 break; 1811 continue; 1812 } 1813 1814 /* 1815 *if the channel speed is not set divide 1816 *the packet evenly among the free channels; 1817 *otherwise divide it according to the speed 1818 *of the channel we are going to transmit on 1819 */ 1820 flen = len; 1821 if (nfree > 0) { 1822 if (pch->speed == 0) { 1823 flen = len/nfree; 1824 if (nbigger > 0) { 1825 flen++; 1826 nbigger--; 1827 } 1828 } else { 1829 flen = (((totfree - nzero)*(totlen + hdrlen*totfree)) / 1830 ((totspeed*totfree)/pch->speed)) - hdrlen; 1831 if (nbigger > 0) { 1832 flen += ((totfree - nzero)*pch->speed)/totspeed; 1833 nbigger -= ((totfree - nzero)*pch->speed)/ 1834 totspeed; 1835 } 1836 } 1837 nfree--; 1838 } 1839 1840 /* 1841 *check if we are on the last channel or 1842 *we exceded the length of the data to 1843 *fragment 1844 */ 1845 if ((nfree <= 0) || (flen > len)) 1846 flen = len; 1847 /* 1848 *it is not worth to tx on slow channels: 1849 *in that case from the resulting flen according to the 1850 *above formula will be equal or less than zero. 1851 *Skip the channel in this case 1852 */ 1853 if (flen <= 0) { 1854 pch->avail = 2; 1855 spin_unlock(&pch->downl); 1856 continue; 1857 } 1858 1859 /* 1860 * hdrlen includes the 2-byte PPP protocol field, but the 1861 * MTU counts only the payload excluding the protocol field. 1862 * (RFC1661 Section 2) 1863 */ 1864 mtu = pch->chan->mtu - (hdrlen - 2); 1865 if (mtu < 4) 1866 mtu = 4; 1867 if (flen > mtu) 1868 flen = mtu; 1869 if (flen == len) 1870 bits |= E; 1871 frag = alloc_skb(flen + hdrlen + (flen == 0), GFP_ATOMIC); 1872 if (!frag) 1873 goto noskb; 1874 q = skb_put(frag, flen + hdrlen); 1875 1876 /* make the MP header */ 1877 put_unaligned_be16(PPP_MP, q); 1878 if (ppp->flags & SC_MP_XSHORTSEQ) { 1879 q[2] = bits + ((ppp->nxseq >> 8) & 0xf); 1880 q[3] = ppp->nxseq; 1881 } else { 1882 q[2] = bits; 1883 q[3] = ppp->nxseq >> 16; 1884 q[4] = ppp->nxseq >> 8; 1885 q[5] = ppp->nxseq; 1886 } 1887 1888 memcpy(q + hdrlen, p, flen); 1889 1890 /* try to send it down the channel */ 1891 chan = pch->chan; 1892 if (!skb_queue_empty(&pch->file.xq) || 1893 !chan->ops->start_xmit(chan, frag)) 1894 skb_queue_tail(&pch->file.xq, frag); 1895 pch->had_frag = 1; 1896 p += flen; 1897 len -= flen; 1898 ++ppp->nxseq; 1899 bits = 0; 1900 spin_unlock(&pch->downl); 1901 } 1902 ppp->nxchan = i; 1903 1904 return 1; 1905 1906 noskb: 1907 spin_unlock(&pch->downl); 1908 if (ppp->debug & 1) 1909 netdev_err(ppp->dev, "PPP: no memory (fragment)\n"); 1910 ++ppp->dev->stats.tx_errors; 1911 ++ppp->nxseq; 1912 return 1; /* abandon the frame */ 1913 } 1914 #endif /* CONFIG_PPP_MULTILINK */ 1915 1916 /* Try to send data out on a channel */ 1917 static void __ppp_channel_push(struct channel *pch) 1918 { 1919 struct sk_buff *skb; 1920 struct ppp *ppp; 1921 1922 spin_lock(&pch->downl); 1923 if (pch->chan) { 1924 while (!skb_queue_empty(&pch->file.xq)) { 1925 skb = skb_dequeue(&pch->file.xq); 1926 if (!pch->chan->ops->start_xmit(pch->chan, skb)) { 1927 /* put the packet back and try again later */ 1928 skb_queue_head(&pch->file.xq, skb); 1929 break; 1930 } 1931 } 1932 } else { 1933 /* channel got deregistered */ 1934 skb_queue_purge(&pch->file.xq); 1935 } 1936 spin_unlock(&pch->downl); 1937 /* see if there is anything from the attached unit to be sent */ 1938 if (skb_queue_empty(&pch->file.xq)) { 1939 ppp = pch->ppp; 1940 if (ppp) 1941 __ppp_xmit_process(ppp); 1942 } 1943 } 1944 1945 static void ppp_channel_push(struct channel *pch) 1946 { 1947 read_lock_bh(&pch->upl); 1948 if (pch->ppp) { 1949 (*this_cpu_ptr(pch->ppp->xmit_recursion))++; 1950 __ppp_channel_push(pch); 1951 (*this_cpu_ptr(pch->ppp->xmit_recursion))--; 1952 } else { 1953 __ppp_channel_push(pch); 1954 } 1955 read_unlock_bh(&pch->upl); 1956 } 1957 1958 /* 1959 * Receive-side routines. 1960 */ 1961 1962 struct ppp_mp_skb_parm { 1963 u32 sequence; 1964 u8 BEbits; 1965 }; 1966 #define PPP_MP_CB(skb) ((struct ppp_mp_skb_parm *)((skb)->cb)) 1967 1968 static inline void 1969 ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch) 1970 { 1971 ppp_recv_lock(ppp); 1972 if (!ppp->closing) 1973 ppp_receive_frame(ppp, skb, pch); 1974 else 1975 kfree_skb(skb); 1976 ppp_recv_unlock(ppp); 1977 } 1978 1979 void 1980 ppp_input(struct ppp_channel *chan, struct sk_buff *skb) 1981 { 1982 struct channel *pch = chan->ppp; 1983 int proto; 1984 1985 if (!pch) { 1986 kfree_skb(skb); 1987 return; 1988 } 1989 1990 read_lock_bh(&pch->upl); 1991 if (!pskb_may_pull(skb, 2)) { 1992 kfree_skb(skb); 1993 if (pch->ppp) { 1994 ++pch->ppp->dev->stats.rx_length_errors; 1995 ppp_receive_error(pch->ppp); 1996 } 1997 goto done; 1998 } 1999 2000 proto = PPP_PROTO(skb); 2001 if (!pch->ppp || proto >= 0xc000 || proto == PPP_CCPFRAG) { 2002 /* put it on the channel queue */ 2003 skb_queue_tail(&pch->file.rq, skb); 2004 /* drop old frames if queue too long */ 2005 while (pch->file.rq.qlen > PPP_MAX_RQLEN && 2006 (skb = skb_dequeue(&pch->file.rq))) 2007 kfree_skb(skb); 2008 wake_up_interruptible(&pch->file.rwait); 2009 } else { 2010 ppp_do_recv(pch->ppp, skb, pch); 2011 } 2012 2013 done: 2014 read_unlock_bh(&pch->upl); 2015 } 2016 2017 /* Put a 0-length skb in the receive queue as an error indication */ 2018 void 2019 ppp_input_error(struct ppp_channel *chan, int code) 2020 { 2021 struct channel *pch = chan->ppp; 2022 struct sk_buff *skb; 2023 2024 if (!pch) 2025 return; 2026 2027 read_lock_bh(&pch->upl); 2028 if (pch->ppp) { 2029 skb = alloc_skb(0, GFP_ATOMIC); 2030 if (skb) { 2031 skb->len = 0; /* probably unnecessary */ 2032 skb->cb[0] = code; 2033 ppp_do_recv(pch->ppp, skb, pch); 2034 } 2035 } 2036 read_unlock_bh(&pch->upl); 2037 } 2038 2039 /* 2040 * We come in here to process a received frame. 2041 * The receive side of the ppp unit is locked. 2042 */ 2043 static void 2044 ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch) 2045 { 2046 /* note: a 0-length skb is used as an error indication */ 2047 if (skb->len > 0) { 2048 skb_checksum_complete_unset(skb); 2049 #ifdef CONFIG_PPP_MULTILINK 2050 /* XXX do channel-level decompression here */ 2051 if (PPP_PROTO(skb) == PPP_MP) 2052 ppp_receive_mp_frame(ppp, skb, pch); 2053 else 2054 #endif /* CONFIG_PPP_MULTILINK */ 2055 ppp_receive_nonmp_frame(ppp, skb); 2056 } else { 2057 kfree_skb(skb); 2058 ppp_receive_error(ppp); 2059 } 2060 } 2061 2062 static void 2063 ppp_receive_error(struct ppp *ppp) 2064 { 2065 ++ppp->dev->stats.rx_errors; 2066 if (ppp->vj) 2067 slhc_toss(ppp->vj); 2068 } 2069 2070 static void 2071 ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb) 2072 { 2073 struct sk_buff *ns; 2074 int proto, len, npi; 2075 2076 /* 2077 * Decompress the frame, if compressed. 2078 * Note that some decompressors need to see uncompressed frames 2079 * that come in as well as compressed frames. 2080 */ 2081 if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN) && 2082 (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0) 2083 skb = ppp_decompress_frame(ppp, skb); 2084 2085 if (ppp->flags & SC_MUST_COMP && ppp->rstate & SC_DC_FERROR) 2086 goto err; 2087 2088 proto = PPP_PROTO(skb); 2089 switch (proto) { 2090 case PPP_VJC_COMP: 2091 /* decompress VJ compressed packets */ 2092 if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP)) 2093 goto err; 2094 2095 if (skb_tailroom(skb) < 124 || skb_cloned(skb)) { 2096 /* copy to a new sk_buff with more tailroom */ 2097 ns = dev_alloc_skb(skb->len + 128); 2098 if (!ns) { 2099 netdev_err(ppp->dev, "PPP: no memory " 2100 "(VJ decomp)\n"); 2101 goto err; 2102 } 2103 skb_reserve(ns, 2); 2104 skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len); 2105 consume_skb(skb); 2106 skb = ns; 2107 } 2108 else 2109 skb->ip_summed = CHECKSUM_NONE; 2110 2111 len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2); 2112 if (len <= 0) { 2113 netdev_printk(KERN_DEBUG, ppp->dev, 2114 "PPP: VJ decompression error\n"); 2115 goto err; 2116 } 2117 len += 2; 2118 if (len > skb->len) 2119 skb_put(skb, len - skb->len); 2120 else if (len < skb->len) 2121 skb_trim(skb, len); 2122 proto = PPP_IP; 2123 break; 2124 2125 case PPP_VJC_UNCOMP: 2126 if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP)) 2127 goto err; 2128 2129 /* Until we fix the decompressor need to make sure 2130 * data portion is linear. 2131 */ 2132 if (!pskb_may_pull(skb, skb->len)) 2133 goto err; 2134 2135 if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) { 2136 netdev_err(ppp->dev, "PPP: VJ uncompressed error\n"); 2137 goto err; 2138 } 2139 proto = PPP_IP; 2140 break; 2141 2142 case PPP_CCP: 2143 ppp_ccp_peek(ppp, skb, 1); 2144 break; 2145 } 2146 2147 ++ppp->stats64.rx_packets; 2148 ppp->stats64.rx_bytes += skb->len - 2; 2149 2150 npi = proto_to_npindex(proto); 2151 if (npi < 0) { 2152 /* control or unknown frame - pass it to pppd */ 2153 skb_queue_tail(&ppp->file.rq, skb); 2154 /* limit queue length by dropping old frames */ 2155 while (ppp->file.rq.qlen > PPP_MAX_RQLEN && 2156 (skb = skb_dequeue(&ppp->file.rq))) 2157 kfree_skb(skb); 2158 /* wake up any process polling or blocking on read */ 2159 wake_up_interruptible(&ppp->file.rwait); 2160 2161 } else { 2162 /* network protocol frame - give it to the kernel */ 2163 2164 #ifdef CONFIG_PPP_FILTER 2165 /* check if the packet passes the pass and active filters */ 2166 /* the filter instructions are constructed assuming 2167 a four-byte PPP header on each packet */ 2168 if (ppp->pass_filter || ppp->active_filter) { 2169 if (skb_unclone(skb, GFP_ATOMIC)) 2170 goto err; 2171 2172 *(u8 *)skb_push(skb, 2) = 0; 2173 if (ppp->pass_filter && 2174 BPF_PROG_RUN(ppp->pass_filter, skb) == 0) { 2175 if (ppp->debug & 1) 2176 netdev_printk(KERN_DEBUG, ppp->dev, 2177 "PPP: inbound frame " 2178 "not passed\n"); 2179 kfree_skb(skb); 2180 return; 2181 } 2182 if (!(ppp->active_filter && 2183 BPF_PROG_RUN(ppp->active_filter, skb) == 0)) 2184 ppp->last_recv = jiffies; 2185 __skb_pull(skb, 2); 2186 } else 2187 #endif /* CONFIG_PPP_FILTER */ 2188 ppp->last_recv = jiffies; 2189 2190 if ((ppp->dev->flags & IFF_UP) == 0 || 2191 ppp->npmode[npi] != NPMODE_PASS) { 2192 kfree_skb(skb); 2193 } else { 2194 /* chop off protocol */ 2195 skb_pull_rcsum(skb, 2); 2196 skb->dev = ppp->dev; 2197 skb->protocol = htons(npindex_to_ethertype[npi]); 2198 skb_reset_mac_header(skb); 2199 skb_scrub_packet(skb, !net_eq(ppp->ppp_net, 2200 dev_net(ppp->dev))); 2201 netif_rx(skb); 2202 } 2203 } 2204 return; 2205 2206 err: 2207 kfree_skb(skb); 2208 ppp_receive_error(ppp); 2209 } 2210 2211 static struct sk_buff * 2212 ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb) 2213 { 2214 int proto = PPP_PROTO(skb); 2215 struct sk_buff *ns; 2216 int len; 2217 2218 /* Until we fix all the decompressor's need to make sure 2219 * data portion is linear. 2220 */ 2221 if (!pskb_may_pull(skb, skb->len)) 2222 goto err; 2223 2224 if (proto == PPP_COMP) { 2225 int obuff_size; 2226 2227 switch(ppp->rcomp->compress_proto) { 2228 case CI_MPPE: 2229 obuff_size = ppp->mru + PPP_HDRLEN + 1; 2230 break; 2231 default: 2232 obuff_size = ppp->mru + PPP_HDRLEN; 2233 break; 2234 } 2235 2236 ns = dev_alloc_skb(obuff_size); 2237 if (!ns) { 2238 netdev_err(ppp->dev, "ppp_decompress_frame: " 2239 "no memory\n"); 2240 goto err; 2241 } 2242 /* the decompressor still expects the A/C bytes in the hdr */ 2243 len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2, 2244 skb->len + 2, ns->data, obuff_size); 2245 if (len < 0) { 2246 /* Pass the compressed frame to pppd as an 2247 error indication. */ 2248 if (len == DECOMP_FATALERROR) 2249 ppp->rstate |= SC_DC_FERROR; 2250 kfree_skb(ns); 2251 goto err; 2252 } 2253 2254 consume_skb(skb); 2255 skb = ns; 2256 skb_put(skb, len); 2257 skb_pull(skb, 2); /* pull off the A/C bytes */ 2258 2259 } else { 2260 /* Uncompressed frame - pass to decompressor so it 2261 can update its dictionary if necessary. */ 2262 if (ppp->rcomp->incomp) 2263 ppp->rcomp->incomp(ppp->rc_state, skb->data - 2, 2264 skb->len + 2); 2265 } 2266 2267 return skb; 2268 2269 err: 2270 ppp->rstate |= SC_DC_ERROR; 2271 ppp_receive_error(ppp); 2272 return skb; 2273 } 2274 2275 #ifdef CONFIG_PPP_MULTILINK 2276 /* 2277 * Receive a multilink frame. 2278 * We put it on the reconstruction queue and then pull off 2279 * as many completed frames as we can. 2280 */ 2281 static void 2282 ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch) 2283 { 2284 u32 mask, seq; 2285 struct channel *ch; 2286 int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN; 2287 2288 if (!pskb_may_pull(skb, mphdrlen + 1) || ppp->mrru == 0) 2289 goto err; /* no good, throw it away */ 2290 2291 /* Decode sequence number and begin/end bits */ 2292 if (ppp->flags & SC_MP_SHORTSEQ) { 2293 seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3]; 2294 mask = 0xfff; 2295 } else { 2296 seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5]; 2297 mask = 0xffffff; 2298 } 2299 PPP_MP_CB(skb)->BEbits = skb->data[2]; 2300 skb_pull(skb, mphdrlen); /* pull off PPP and MP headers */ 2301 2302 /* 2303 * Do protocol ID decompression on the first fragment of each packet. 2304 */ 2305 if ((PPP_MP_CB(skb)->BEbits & B) && (skb->data[0] & 1)) 2306 *(u8 *)skb_push(skb, 1) = 0; 2307 2308 /* 2309 * Expand sequence number to 32 bits, making it as close 2310 * as possible to ppp->minseq. 2311 */ 2312 seq |= ppp->minseq & ~mask; 2313 if ((int)(ppp->minseq - seq) > (int)(mask >> 1)) 2314 seq += mask + 1; 2315 else if ((int)(seq - ppp->minseq) > (int)(mask >> 1)) 2316 seq -= mask + 1; /* should never happen */ 2317 PPP_MP_CB(skb)->sequence = seq; 2318 pch->lastseq = seq; 2319 2320 /* 2321 * If this packet comes before the next one we were expecting, 2322 * drop it. 2323 */ 2324 if (seq_before(seq, ppp->nextseq)) { 2325 kfree_skb(skb); 2326 ++ppp->dev->stats.rx_dropped; 2327 ppp_receive_error(ppp); 2328 return; 2329 } 2330 2331 /* 2332 * Reevaluate minseq, the minimum over all channels of the 2333 * last sequence number received on each channel. Because of 2334 * the increasing sequence number rule, we know that any fragment 2335 * before `minseq' which hasn't arrived is never going to arrive. 2336 * The list of channels can't change because we have the receive 2337 * side of the ppp unit locked. 2338 */ 2339 list_for_each_entry(ch, &ppp->channels, clist) { 2340 if (seq_before(ch->lastseq, seq)) 2341 seq = ch->lastseq; 2342 } 2343 if (seq_before(ppp->minseq, seq)) 2344 ppp->minseq = seq; 2345 2346 /* Put the fragment on the reconstruction queue */ 2347 ppp_mp_insert(ppp, skb); 2348 2349 /* If the queue is getting long, don't wait any longer for packets 2350 before the start of the queue. */ 2351 if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN) { 2352 struct sk_buff *mskb = skb_peek(&ppp->mrq); 2353 if (seq_before(ppp->minseq, PPP_MP_CB(mskb)->sequence)) 2354 ppp->minseq = PPP_MP_CB(mskb)->sequence; 2355 } 2356 2357 /* Pull completed packets off the queue and receive them. */ 2358 while ((skb = ppp_mp_reconstruct(ppp))) { 2359 if (pskb_may_pull(skb, 2)) 2360 ppp_receive_nonmp_frame(ppp, skb); 2361 else { 2362 ++ppp->dev->stats.rx_length_errors; 2363 kfree_skb(skb); 2364 ppp_receive_error(ppp); 2365 } 2366 } 2367 2368 return; 2369 2370 err: 2371 kfree_skb(skb); 2372 ppp_receive_error(ppp); 2373 } 2374 2375 /* 2376 * Insert a fragment on the MP reconstruction queue. 2377 * The queue is ordered by increasing sequence number. 2378 */ 2379 static void 2380 ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb) 2381 { 2382 struct sk_buff *p; 2383 struct sk_buff_head *list = &ppp->mrq; 2384 u32 seq = PPP_MP_CB(skb)->sequence; 2385 2386 /* N.B. we don't need to lock the list lock because we have the 2387 ppp unit receive-side lock. */ 2388 skb_queue_walk(list, p) { 2389 if (seq_before(seq, PPP_MP_CB(p)->sequence)) 2390 break; 2391 } 2392 __skb_queue_before(list, p, skb); 2393 } 2394 2395 /* 2396 * Reconstruct a packet from the MP fragment queue. 2397 * We go through increasing sequence numbers until we find a 2398 * complete packet, or we get to the sequence number for a fragment 2399 * which hasn't arrived but might still do so. 2400 */ 2401 static struct sk_buff * 2402 ppp_mp_reconstruct(struct ppp *ppp) 2403 { 2404 u32 seq = ppp->nextseq; 2405 u32 minseq = ppp->minseq; 2406 struct sk_buff_head *list = &ppp->mrq; 2407 struct sk_buff *p, *tmp; 2408 struct sk_buff *head, *tail; 2409 struct sk_buff *skb = NULL; 2410 int lost = 0, len = 0; 2411 2412 if (ppp->mrru == 0) /* do nothing until mrru is set */ 2413 return NULL; 2414 head = list->next; 2415 tail = NULL; 2416 skb_queue_walk_safe(list, p, tmp) { 2417 again: 2418 if (seq_before(PPP_MP_CB(p)->sequence, seq)) { 2419 /* this can't happen, anyway ignore the skb */ 2420 netdev_err(ppp->dev, "ppp_mp_reconstruct bad " 2421 "seq %u < %u\n", 2422 PPP_MP_CB(p)->sequence, seq); 2423 __skb_unlink(p, list); 2424 kfree_skb(p); 2425 continue; 2426 } 2427 if (PPP_MP_CB(p)->sequence != seq) { 2428 u32 oldseq; 2429 /* Fragment `seq' is missing. If it is after 2430 minseq, it might arrive later, so stop here. */ 2431 if (seq_after(seq, minseq)) 2432 break; 2433 /* Fragment `seq' is lost, keep going. */ 2434 lost = 1; 2435 oldseq = seq; 2436 seq = seq_before(minseq, PPP_MP_CB(p)->sequence)? 2437 minseq + 1: PPP_MP_CB(p)->sequence; 2438 2439 if (ppp->debug & 1) 2440 netdev_printk(KERN_DEBUG, ppp->dev, 2441 "lost frag %u..%u\n", 2442 oldseq, seq-1); 2443 2444 goto again; 2445 } 2446 2447 /* 2448 * At this point we know that all the fragments from 2449 * ppp->nextseq to seq are either present or lost. 2450 * Also, there are no complete packets in the queue 2451 * that have no missing fragments and end before this 2452 * fragment. 2453 */ 2454 2455 /* B bit set indicates this fragment starts a packet */ 2456 if (PPP_MP_CB(p)->BEbits & B) { 2457 head = p; 2458 lost = 0; 2459 len = 0; 2460 } 2461 2462 len += p->len; 2463 2464 /* Got a complete packet yet? */ 2465 if (lost == 0 && (PPP_MP_CB(p)->BEbits & E) && 2466 (PPP_MP_CB(head)->BEbits & B)) { 2467 if (len > ppp->mrru + 2) { 2468 ++ppp->dev->stats.rx_length_errors; 2469 netdev_printk(KERN_DEBUG, ppp->dev, 2470 "PPP: reconstructed packet" 2471 " is too long (%d)\n", len); 2472 } else { 2473 tail = p; 2474 break; 2475 } 2476 ppp->nextseq = seq + 1; 2477 } 2478 2479 /* 2480 * If this is the ending fragment of a packet, 2481 * and we haven't found a complete valid packet yet, 2482 * we can discard up to and including this fragment. 2483 */ 2484 if (PPP_MP_CB(p)->BEbits & E) { 2485 struct sk_buff *tmp2; 2486 2487 skb_queue_reverse_walk_from_safe(list, p, tmp2) { 2488 if (ppp->debug & 1) 2489 netdev_printk(KERN_DEBUG, ppp->dev, 2490 "discarding frag %u\n", 2491 PPP_MP_CB(p)->sequence); 2492 __skb_unlink(p, list); 2493 kfree_skb(p); 2494 } 2495 head = skb_peek(list); 2496 if (!head) 2497 break; 2498 } 2499 ++seq; 2500 } 2501 2502 /* If we have a complete packet, copy it all into one skb. */ 2503 if (tail != NULL) { 2504 /* If we have discarded any fragments, 2505 signal a receive error. */ 2506 if (PPP_MP_CB(head)->sequence != ppp->nextseq) { 2507 skb_queue_walk_safe(list, p, tmp) { 2508 if (p == head) 2509 break; 2510 if (ppp->debug & 1) 2511 netdev_printk(KERN_DEBUG, ppp->dev, 2512 "discarding frag %u\n", 2513 PPP_MP_CB(p)->sequence); 2514 __skb_unlink(p, list); 2515 kfree_skb(p); 2516 } 2517 2518 if (ppp->debug & 1) 2519 netdev_printk(KERN_DEBUG, ppp->dev, 2520 " missed pkts %u..%u\n", 2521 ppp->nextseq, 2522 PPP_MP_CB(head)->sequence-1); 2523 ++ppp->dev->stats.rx_dropped; 2524 ppp_receive_error(ppp); 2525 } 2526 2527 skb = head; 2528 if (head != tail) { 2529 struct sk_buff **fragpp = &skb_shinfo(skb)->frag_list; 2530 p = skb_queue_next(list, head); 2531 __skb_unlink(skb, list); 2532 skb_queue_walk_from_safe(list, p, tmp) { 2533 __skb_unlink(p, list); 2534 *fragpp = p; 2535 p->next = NULL; 2536 fragpp = &p->next; 2537 2538 skb->len += p->len; 2539 skb->data_len += p->len; 2540 skb->truesize += p->truesize; 2541 2542 if (p == tail) 2543 break; 2544 } 2545 } else { 2546 __skb_unlink(skb, list); 2547 } 2548 2549 ppp->nextseq = PPP_MP_CB(tail)->sequence + 1; 2550 } 2551 2552 return skb; 2553 } 2554 #endif /* CONFIG_PPP_MULTILINK */ 2555 2556 /* 2557 * Channel interface. 2558 */ 2559 2560 /* Create a new, unattached ppp channel. */ 2561 int ppp_register_channel(struct ppp_channel *chan) 2562 { 2563 return ppp_register_net_channel(current->nsproxy->net_ns, chan); 2564 } 2565 2566 /* Create a new, unattached ppp channel for specified net. */ 2567 int ppp_register_net_channel(struct net *net, struct ppp_channel *chan) 2568 { 2569 struct channel *pch; 2570 struct ppp_net *pn; 2571 2572 pch = kzalloc(sizeof(struct channel), GFP_KERNEL); 2573 if (!pch) 2574 return -ENOMEM; 2575 2576 pn = ppp_pernet(net); 2577 2578 pch->ppp = NULL; 2579 pch->chan = chan; 2580 pch->chan_net = get_net(net); 2581 chan->ppp = pch; 2582 init_ppp_file(&pch->file, CHANNEL); 2583 pch->file.hdrlen = chan->hdrlen; 2584 #ifdef CONFIG_PPP_MULTILINK 2585 pch->lastseq = -1; 2586 #endif /* CONFIG_PPP_MULTILINK */ 2587 init_rwsem(&pch->chan_sem); 2588 spin_lock_init(&pch->downl); 2589 rwlock_init(&pch->upl); 2590 2591 spin_lock_bh(&pn->all_channels_lock); 2592 pch->file.index = ++pn->last_channel_index; 2593 list_add(&pch->list, &pn->new_channels); 2594 atomic_inc(&channel_count); 2595 spin_unlock_bh(&pn->all_channels_lock); 2596 2597 return 0; 2598 } 2599 2600 /* 2601 * Return the index of a channel. 2602 */ 2603 int ppp_channel_index(struct ppp_channel *chan) 2604 { 2605 struct channel *pch = chan->ppp; 2606 2607 if (pch) 2608 return pch->file.index; 2609 return -1; 2610 } 2611 2612 /* 2613 * Return the PPP unit number to which a channel is connected. 2614 */ 2615 int ppp_unit_number(struct ppp_channel *chan) 2616 { 2617 struct channel *pch = chan->ppp; 2618 int unit = -1; 2619 2620 if (pch) { 2621 read_lock_bh(&pch->upl); 2622 if (pch->ppp) 2623 unit = pch->ppp->file.index; 2624 read_unlock_bh(&pch->upl); 2625 } 2626 return unit; 2627 } 2628 2629 /* 2630 * Return the PPP device interface name of a channel. 2631 */ 2632 char *ppp_dev_name(struct ppp_channel *chan) 2633 { 2634 struct channel *pch = chan->ppp; 2635 char *name = NULL; 2636 2637 if (pch) { 2638 read_lock_bh(&pch->upl); 2639 if (pch->ppp && pch->ppp->dev) 2640 name = pch->ppp->dev->name; 2641 read_unlock_bh(&pch->upl); 2642 } 2643 return name; 2644 } 2645 2646 2647 /* 2648 * Disconnect a channel from the generic layer. 2649 * This must be called in process context. 2650 */ 2651 void 2652 ppp_unregister_channel(struct ppp_channel *chan) 2653 { 2654 struct channel *pch = chan->ppp; 2655 struct ppp_net *pn; 2656 2657 if (!pch) 2658 return; /* should never happen */ 2659 2660 chan->ppp = NULL; 2661 2662 /* 2663 * This ensures that we have returned from any calls into the 2664 * the channel's start_xmit or ioctl routine before we proceed. 2665 */ 2666 down_write(&pch->chan_sem); 2667 spin_lock_bh(&pch->downl); 2668 pch->chan = NULL; 2669 spin_unlock_bh(&pch->downl); 2670 up_write(&pch->chan_sem); 2671 ppp_disconnect_channel(pch); 2672 2673 pn = ppp_pernet(pch->chan_net); 2674 spin_lock_bh(&pn->all_channels_lock); 2675 list_del(&pch->list); 2676 spin_unlock_bh(&pn->all_channels_lock); 2677 2678 pch->file.dead = 1; 2679 wake_up_interruptible(&pch->file.rwait); 2680 if (refcount_dec_and_test(&pch->file.refcnt)) 2681 ppp_destroy_channel(pch); 2682 } 2683 2684 /* 2685 * Callback from a channel when it can accept more to transmit. 2686 * This should be called at BH/softirq level, not interrupt level. 2687 */ 2688 void 2689 ppp_output_wakeup(struct ppp_channel *chan) 2690 { 2691 struct channel *pch = chan->ppp; 2692 2693 if (!pch) 2694 return; 2695 ppp_channel_push(pch); 2696 } 2697 2698 /* 2699 * Compression control. 2700 */ 2701 2702 /* Process the PPPIOCSCOMPRESS ioctl. */ 2703 static int 2704 ppp_set_compress(struct ppp *ppp, unsigned long arg) 2705 { 2706 int err; 2707 struct compressor *cp, *ocomp; 2708 struct ppp_option_data data; 2709 void *state, *ostate; 2710 unsigned char ccp_option[CCP_MAX_OPTION_LENGTH]; 2711 2712 err = -EFAULT; 2713 if (copy_from_user(&data, (void __user *) arg, sizeof(data))) 2714 goto out; 2715 if (data.length > CCP_MAX_OPTION_LENGTH) 2716 goto out; 2717 if (copy_from_user(ccp_option, (void __user *) data.ptr, data.length)) 2718 goto out; 2719 2720 err = -EINVAL; 2721 if (data.length < 2 || ccp_option[1] < 2 || ccp_option[1] > data.length) 2722 goto out; 2723 2724 cp = try_then_request_module( 2725 find_compressor(ccp_option[0]), 2726 "ppp-compress-%d", ccp_option[0]); 2727 if (!cp) 2728 goto out; 2729 2730 err = -ENOBUFS; 2731 if (data.transmit) { 2732 state = cp->comp_alloc(ccp_option, data.length); 2733 if (state) { 2734 ppp_xmit_lock(ppp); 2735 ppp->xstate &= ~SC_COMP_RUN; 2736 ocomp = ppp->xcomp; 2737 ostate = ppp->xc_state; 2738 ppp->xcomp = cp; 2739 ppp->xc_state = state; 2740 ppp_xmit_unlock(ppp); 2741 if (ostate) { 2742 ocomp->comp_free(ostate); 2743 module_put(ocomp->owner); 2744 } 2745 err = 0; 2746 } else 2747 module_put(cp->owner); 2748 2749 } else { 2750 state = cp->decomp_alloc(ccp_option, data.length); 2751 if (state) { 2752 ppp_recv_lock(ppp); 2753 ppp->rstate &= ~SC_DECOMP_RUN; 2754 ocomp = ppp->rcomp; 2755 ostate = ppp->rc_state; 2756 ppp->rcomp = cp; 2757 ppp->rc_state = state; 2758 ppp_recv_unlock(ppp); 2759 if (ostate) { 2760 ocomp->decomp_free(ostate); 2761 module_put(ocomp->owner); 2762 } 2763 err = 0; 2764 } else 2765 module_put(cp->owner); 2766 } 2767 2768 out: 2769 return err; 2770 } 2771 2772 /* 2773 * Look at a CCP packet and update our state accordingly. 2774 * We assume the caller has the xmit or recv path locked. 2775 */ 2776 static void 2777 ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound) 2778 { 2779 unsigned char *dp; 2780 int len; 2781 2782 if (!pskb_may_pull(skb, CCP_HDRLEN + 2)) 2783 return; /* no header */ 2784 dp = skb->data + 2; 2785 2786 switch (CCP_CODE(dp)) { 2787 case CCP_CONFREQ: 2788 2789 /* A ConfReq starts negotiation of compression 2790 * in one direction of transmission, 2791 * and hence brings it down...but which way? 2792 * 2793 * Remember: 2794 * A ConfReq indicates what the sender would like to receive 2795 */ 2796 if(inbound) 2797 /* He is proposing what I should send */ 2798 ppp->xstate &= ~SC_COMP_RUN; 2799 else 2800 /* I am proposing to what he should send */ 2801 ppp->rstate &= ~SC_DECOMP_RUN; 2802 2803 break; 2804 2805 case CCP_TERMREQ: 2806 case CCP_TERMACK: 2807 /* 2808 * CCP is going down, both directions of transmission 2809 */ 2810 ppp->rstate &= ~SC_DECOMP_RUN; 2811 ppp->xstate &= ~SC_COMP_RUN; 2812 break; 2813 2814 case CCP_CONFACK: 2815 if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN) 2816 break; 2817 len = CCP_LENGTH(dp); 2818 if (!pskb_may_pull(skb, len + 2)) 2819 return; /* too short */ 2820 dp += CCP_HDRLEN; 2821 len -= CCP_HDRLEN; 2822 if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp)) 2823 break; 2824 if (inbound) { 2825 /* we will start receiving compressed packets */ 2826 if (!ppp->rc_state) 2827 break; 2828 if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len, 2829 ppp->file.index, 0, ppp->mru, ppp->debug)) { 2830 ppp->rstate |= SC_DECOMP_RUN; 2831 ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR); 2832 } 2833 } else { 2834 /* we will soon start sending compressed packets */ 2835 if (!ppp->xc_state) 2836 break; 2837 if (ppp->xcomp->comp_init(ppp->xc_state, dp, len, 2838 ppp->file.index, 0, ppp->debug)) 2839 ppp->xstate |= SC_COMP_RUN; 2840 } 2841 break; 2842 2843 case CCP_RESETACK: 2844 /* reset the [de]compressor */ 2845 if ((ppp->flags & SC_CCP_UP) == 0) 2846 break; 2847 if (inbound) { 2848 if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) { 2849 ppp->rcomp->decomp_reset(ppp->rc_state); 2850 ppp->rstate &= ~SC_DC_ERROR; 2851 } 2852 } else { 2853 if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN)) 2854 ppp->xcomp->comp_reset(ppp->xc_state); 2855 } 2856 break; 2857 } 2858 } 2859 2860 /* Free up compression resources. */ 2861 static void 2862 ppp_ccp_closed(struct ppp *ppp) 2863 { 2864 void *xstate, *rstate; 2865 struct compressor *xcomp, *rcomp; 2866 2867 ppp_lock(ppp); 2868 ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP); 2869 ppp->xstate = 0; 2870 xcomp = ppp->xcomp; 2871 xstate = ppp->xc_state; 2872 ppp->xc_state = NULL; 2873 ppp->rstate = 0; 2874 rcomp = ppp->rcomp; 2875 rstate = ppp->rc_state; 2876 ppp->rc_state = NULL; 2877 ppp_unlock(ppp); 2878 2879 if (xstate) { 2880 xcomp->comp_free(xstate); 2881 module_put(xcomp->owner); 2882 } 2883 if (rstate) { 2884 rcomp->decomp_free(rstate); 2885 module_put(rcomp->owner); 2886 } 2887 } 2888 2889 /* List of compressors. */ 2890 static LIST_HEAD(compressor_list); 2891 static DEFINE_SPINLOCK(compressor_list_lock); 2892 2893 struct compressor_entry { 2894 struct list_head list; 2895 struct compressor *comp; 2896 }; 2897 2898 static struct compressor_entry * 2899 find_comp_entry(int proto) 2900 { 2901 struct compressor_entry *ce; 2902 2903 list_for_each_entry(ce, &compressor_list, list) { 2904 if (ce->comp->compress_proto == proto) 2905 return ce; 2906 } 2907 return NULL; 2908 } 2909 2910 /* Register a compressor */ 2911 int 2912 ppp_register_compressor(struct compressor *cp) 2913 { 2914 struct compressor_entry *ce; 2915 int ret; 2916 spin_lock(&compressor_list_lock); 2917 ret = -EEXIST; 2918 if (find_comp_entry(cp->compress_proto)) 2919 goto out; 2920 ret = -ENOMEM; 2921 ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC); 2922 if (!ce) 2923 goto out; 2924 ret = 0; 2925 ce->comp = cp; 2926 list_add(&ce->list, &compressor_list); 2927 out: 2928 spin_unlock(&compressor_list_lock); 2929 return ret; 2930 } 2931 2932 /* Unregister a compressor */ 2933 void 2934 ppp_unregister_compressor(struct compressor *cp) 2935 { 2936 struct compressor_entry *ce; 2937 2938 spin_lock(&compressor_list_lock); 2939 ce = find_comp_entry(cp->compress_proto); 2940 if (ce && ce->comp == cp) { 2941 list_del(&ce->list); 2942 kfree(ce); 2943 } 2944 spin_unlock(&compressor_list_lock); 2945 } 2946 2947 /* Find a compressor. */ 2948 static struct compressor * 2949 find_compressor(int type) 2950 { 2951 struct compressor_entry *ce; 2952 struct compressor *cp = NULL; 2953 2954 spin_lock(&compressor_list_lock); 2955 ce = find_comp_entry(type); 2956 if (ce) { 2957 cp = ce->comp; 2958 if (!try_module_get(cp->owner)) 2959 cp = NULL; 2960 } 2961 spin_unlock(&compressor_list_lock); 2962 return cp; 2963 } 2964 2965 /* 2966 * Miscelleneous stuff. 2967 */ 2968 2969 static void 2970 ppp_get_stats(struct ppp *ppp, struct ppp_stats *st) 2971 { 2972 struct slcompress *vj = ppp->vj; 2973 2974 memset(st, 0, sizeof(*st)); 2975 st->p.ppp_ipackets = ppp->stats64.rx_packets; 2976 st->p.ppp_ierrors = ppp->dev->stats.rx_errors; 2977 st->p.ppp_ibytes = ppp->stats64.rx_bytes; 2978 st->p.ppp_opackets = ppp->stats64.tx_packets; 2979 st->p.ppp_oerrors = ppp->dev->stats.tx_errors; 2980 st->p.ppp_obytes = ppp->stats64.tx_bytes; 2981 if (!vj) 2982 return; 2983 st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed; 2984 st->vj.vjs_compressed = vj->sls_o_compressed; 2985 st->vj.vjs_searches = vj->sls_o_searches; 2986 st->vj.vjs_misses = vj->sls_o_misses; 2987 st->vj.vjs_errorin = vj->sls_i_error; 2988 st->vj.vjs_tossed = vj->sls_i_tossed; 2989 st->vj.vjs_uncompressedin = vj->sls_i_uncompressed; 2990 st->vj.vjs_compressedin = vj->sls_i_compressed; 2991 } 2992 2993 /* 2994 * Stuff for handling the lists of ppp units and channels 2995 * and for initialization. 2996 */ 2997 2998 /* 2999 * Create a new ppp interface unit. Fails if it can't allocate memory 3000 * or if there is already a unit with the requested number. 3001 * unit == -1 means allocate a new number. 3002 */ 3003 static int ppp_create_interface(struct net *net, struct file *file, int *unit) 3004 { 3005 struct ppp_config conf = { 3006 .file = file, 3007 .unit = *unit, 3008 .ifname_is_set = false, 3009 }; 3010 struct net_device *dev; 3011 struct ppp *ppp; 3012 int err; 3013 3014 dev = alloc_netdev(sizeof(struct ppp), "", NET_NAME_ENUM, ppp_setup); 3015 if (!dev) { 3016 err = -ENOMEM; 3017 goto err; 3018 } 3019 dev_net_set(dev, net); 3020 dev->rtnl_link_ops = &ppp_link_ops; 3021 3022 rtnl_lock(); 3023 3024 err = ppp_dev_configure(net, dev, &conf); 3025 if (err < 0) 3026 goto err_dev; 3027 ppp = netdev_priv(dev); 3028 *unit = ppp->file.index; 3029 3030 rtnl_unlock(); 3031 3032 return 0; 3033 3034 err_dev: 3035 rtnl_unlock(); 3036 free_netdev(dev); 3037 err: 3038 return err; 3039 } 3040 3041 /* 3042 * Initialize a ppp_file structure. 3043 */ 3044 static void 3045 init_ppp_file(struct ppp_file *pf, int kind) 3046 { 3047 pf->kind = kind; 3048 skb_queue_head_init(&pf->xq); 3049 skb_queue_head_init(&pf->rq); 3050 refcount_set(&pf->refcnt, 1); 3051 init_waitqueue_head(&pf->rwait); 3052 } 3053 3054 /* 3055 * Free the memory used by a ppp unit. This is only called once 3056 * there are no channels connected to the unit and no file structs 3057 * that reference the unit. 3058 */ 3059 static void ppp_destroy_interface(struct ppp *ppp) 3060 { 3061 atomic_dec(&ppp_unit_count); 3062 3063 if (!ppp->file.dead || ppp->n_channels) { 3064 /* "can't happen" */ 3065 netdev_err(ppp->dev, "ppp: destroying ppp struct %p " 3066 "but dead=%d n_channels=%d !\n", 3067 ppp, ppp->file.dead, ppp->n_channels); 3068 return; 3069 } 3070 3071 ppp_ccp_closed(ppp); 3072 if (ppp->vj) { 3073 slhc_free(ppp->vj); 3074 ppp->vj = NULL; 3075 } 3076 skb_queue_purge(&ppp->file.xq); 3077 skb_queue_purge(&ppp->file.rq); 3078 #ifdef CONFIG_PPP_MULTILINK 3079 skb_queue_purge(&ppp->mrq); 3080 #endif /* CONFIG_PPP_MULTILINK */ 3081 #ifdef CONFIG_PPP_FILTER 3082 if (ppp->pass_filter) { 3083 bpf_prog_destroy(ppp->pass_filter); 3084 ppp->pass_filter = NULL; 3085 } 3086 3087 if (ppp->active_filter) { 3088 bpf_prog_destroy(ppp->active_filter); 3089 ppp->active_filter = NULL; 3090 } 3091 #endif /* CONFIG_PPP_FILTER */ 3092 3093 kfree_skb(ppp->xmit_pending); 3094 free_percpu(ppp->xmit_recursion); 3095 3096 free_netdev(ppp->dev); 3097 } 3098 3099 /* 3100 * Locate an existing ppp unit. 3101 * The caller should have locked the all_ppp_mutex. 3102 */ 3103 static struct ppp * 3104 ppp_find_unit(struct ppp_net *pn, int unit) 3105 { 3106 return unit_find(&pn->units_idr, unit); 3107 } 3108 3109 /* 3110 * Locate an existing ppp channel. 3111 * The caller should have locked the all_channels_lock. 3112 * First we look in the new_channels list, then in the 3113 * all_channels list. If found in the new_channels list, 3114 * we move it to the all_channels list. This is for speed 3115 * when we have a lot of channels in use. 3116 */ 3117 static struct channel * 3118 ppp_find_channel(struct ppp_net *pn, int unit) 3119 { 3120 struct channel *pch; 3121 3122 list_for_each_entry(pch, &pn->new_channels, list) { 3123 if (pch->file.index == unit) { 3124 list_move(&pch->list, &pn->all_channels); 3125 return pch; 3126 } 3127 } 3128 3129 list_for_each_entry(pch, &pn->all_channels, list) { 3130 if (pch->file.index == unit) 3131 return pch; 3132 } 3133 3134 return NULL; 3135 } 3136 3137 /* 3138 * Connect a PPP channel to a PPP interface unit. 3139 */ 3140 static int 3141 ppp_connect_channel(struct channel *pch, int unit) 3142 { 3143 struct ppp *ppp; 3144 struct ppp_net *pn; 3145 int ret = -ENXIO; 3146 int hdrlen; 3147 3148 pn = ppp_pernet(pch->chan_net); 3149 3150 mutex_lock(&pn->all_ppp_mutex); 3151 ppp = ppp_find_unit(pn, unit); 3152 if (!ppp) 3153 goto out; 3154 write_lock_bh(&pch->upl); 3155 ret = -EINVAL; 3156 if (pch->ppp) 3157 goto outl; 3158 3159 ppp_lock(ppp); 3160 if (pch->file.hdrlen > ppp->file.hdrlen) 3161 ppp->file.hdrlen = pch->file.hdrlen; 3162 hdrlen = pch->file.hdrlen + 2; /* for protocol bytes */ 3163 if (hdrlen > ppp->dev->hard_header_len) 3164 ppp->dev->hard_header_len = hdrlen; 3165 list_add_tail(&pch->clist, &ppp->channels); 3166 ++ppp->n_channels; 3167 pch->ppp = ppp; 3168 refcount_inc(&ppp->file.refcnt); 3169 ppp_unlock(ppp); 3170 ret = 0; 3171 3172 outl: 3173 write_unlock_bh(&pch->upl); 3174 out: 3175 mutex_unlock(&pn->all_ppp_mutex); 3176 return ret; 3177 } 3178 3179 /* 3180 * Disconnect a channel from its ppp unit. 3181 */ 3182 static int 3183 ppp_disconnect_channel(struct channel *pch) 3184 { 3185 struct ppp *ppp; 3186 int err = -EINVAL; 3187 3188 write_lock_bh(&pch->upl); 3189 ppp = pch->ppp; 3190 pch->ppp = NULL; 3191 write_unlock_bh(&pch->upl); 3192 if (ppp) { 3193 /* remove it from the ppp unit's list */ 3194 ppp_lock(ppp); 3195 list_del(&pch->clist); 3196 if (--ppp->n_channels == 0) 3197 wake_up_interruptible(&ppp->file.rwait); 3198 ppp_unlock(ppp); 3199 if (refcount_dec_and_test(&ppp->file.refcnt)) 3200 ppp_destroy_interface(ppp); 3201 err = 0; 3202 } 3203 return err; 3204 } 3205 3206 /* 3207 * Free up the resources used by a ppp channel. 3208 */ 3209 static void ppp_destroy_channel(struct channel *pch) 3210 { 3211 put_net(pch->chan_net); 3212 pch->chan_net = NULL; 3213 3214 atomic_dec(&channel_count); 3215 3216 if (!pch->file.dead) { 3217 /* "can't happen" */ 3218 pr_err("ppp: destroying undead channel %p !\n", pch); 3219 return; 3220 } 3221 skb_queue_purge(&pch->file.xq); 3222 skb_queue_purge(&pch->file.rq); 3223 kfree(pch); 3224 } 3225 3226 static void __exit ppp_cleanup(void) 3227 { 3228 /* should never happen */ 3229 if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count)) 3230 pr_err("PPP: removing module but units remain!\n"); 3231 rtnl_link_unregister(&ppp_link_ops); 3232 unregister_chrdev(PPP_MAJOR, "ppp"); 3233 device_destroy(ppp_class, MKDEV(PPP_MAJOR, 0)); 3234 class_destroy(ppp_class); 3235 unregister_pernet_device(&ppp_net_ops); 3236 } 3237 3238 /* 3239 * Units handling. Caller must protect concurrent access 3240 * by holding all_ppp_mutex 3241 */ 3242 3243 /* associate pointer with specified number */ 3244 static int unit_set(struct idr *p, void *ptr, int n) 3245 { 3246 int unit; 3247 3248 unit = idr_alloc(p, ptr, n, n + 1, GFP_KERNEL); 3249 if (unit == -ENOSPC) 3250 unit = -EINVAL; 3251 return unit; 3252 } 3253 3254 /* get new free unit number and associate pointer with it */ 3255 static int unit_get(struct idr *p, void *ptr) 3256 { 3257 return idr_alloc(p, ptr, 0, 0, GFP_KERNEL); 3258 } 3259 3260 /* put unit number back to a pool */ 3261 static void unit_put(struct idr *p, int n) 3262 { 3263 idr_remove(p, n); 3264 } 3265 3266 /* get pointer associated with the number */ 3267 static void *unit_find(struct idr *p, int n) 3268 { 3269 return idr_find(p, n); 3270 } 3271 3272 /* Module/initialization stuff */ 3273 3274 module_init(ppp_init); 3275 module_exit(ppp_cleanup); 3276 3277 EXPORT_SYMBOL(ppp_register_net_channel); 3278 EXPORT_SYMBOL(ppp_register_channel); 3279 EXPORT_SYMBOL(ppp_unregister_channel); 3280 EXPORT_SYMBOL(ppp_channel_index); 3281 EXPORT_SYMBOL(ppp_unit_number); 3282 EXPORT_SYMBOL(ppp_dev_name); 3283 EXPORT_SYMBOL(ppp_input); 3284 EXPORT_SYMBOL(ppp_input_error); 3285 EXPORT_SYMBOL(ppp_output_wakeup); 3286 EXPORT_SYMBOL(ppp_register_compressor); 3287 EXPORT_SYMBOL(ppp_unregister_compressor); 3288 MODULE_LICENSE("GPL"); 3289 MODULE_ALIAS_CHARDEV(PPP_MAJOR, 0); 3290 MODULE_ALIAS_RTNL_LINK("ppp"); 3291 MODULE_ALIAS("devname:ppp"); 3292