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