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