ppp_generic.c 70 KB

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