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