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