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