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