ppp_generic.c 72 KB

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