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