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