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