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