ppp_generic.c 71 KB

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