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