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