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