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