ppp_generic.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021
  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 const struct net_device_ops ppp_netdev_ops = {
  942. .ndo_start_xmit = ppp_start_xmit,
  943. .ndo_do_ioctl = ppp_net_ioctl,
  944. .ndo_get_stats64 = ppp_get_stats64,
  945. };
  946. static void ppp_setup(struct net_device *dev)
  947. {
  948. dev->netdev_ops = &ppp_netdev_ops;
  949. dev->hard_header_len = PPP_HDRLEN;
  950. dev->mtu = PPP_MRU;
  951. dev->addr_len = 0;
  952. dev->tx_queue_len = 3;
  953. dev->type = ARPHRD_PPP;
  954. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  955. dev->features |= NETIF_F_NETNS_LOCAL;
  956. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  957. }
  958. /*
  959. * Transmit-side routines.
  960. */
  961. /*
  962. * Called to do any work queued up on the transmit side
  963. * that can now be done.
  964. */
  965. static void
  966. ppp_xmit_process(struct ppp *ppp)
  967. {
  968. struct sk_buff *skb;
  969. ppp_xmit_lock(ppp);
  970. if (!ppp->closing) {
  971. ppp_push(ppp);
  972. while (!ppp->xmit_pending &&
  973. (skb = skb_dequeue(&ppp->file.xq)))
  974. ppp_send_frame(ppp, skb);
  975. /* If there's no work left to do, tell the core net
  976. code that we can accept some more. */
  977. if (!ppp->xmit_pending && !skb_peek(&ppp->file.xq))
  978. netif_wake_queue(ppp->dev);
  979. else
  980. netif_stop_queue(ppp->dev);
  981. }
  982. ppp_xmit_unlock(ppp);
  983. }
  984. static inline struct sk_buff *
  985. pad_compress_skb(struct ppp *ppp, struct sk_buff *skb)
  986. {
  987. struct sk_buff *new_skb;
  988. int len;
  989. int new_skb_size = ppp->dev->mtu +
  990. ppp->xcomp->comp_extra + ppp->dev->hard_header_len;
  991. int compressor_skb_size = ppp->dev->mtu +
  992. ppp->xcomp->comp_extra + PPP_HDRLEN;
  993. new_skb = alloc_skb(new_skb_size, GFP_ATOMIC);
  994. if (!new_skb) {
  995. if (net_ratelimit())
  996. netdev_err(ppp->dev, "PPP: no memory (comp pkt)\n");
  997. return NULL;
  998. }
  999. if (ppp->dev->hard_header_len > PPP_HDRLEN)
  1000. skb_reserve(new_skb,
  1001. ppp->dev->hard_header_len - PPP_HDRLEN);
  1002. /* compressor still expects A/C bytes in hdr */
  1003. len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
  1004. new_skb->data, skb->len + 2,
  1005. compressor_skb_size);
  1006. if (len > 0 && (ppp->flags & SC_CCP_UP)) {
  1007. consume_skb(skb);
  1008. skb = new_skb;
  1009. skb_put(skb, len);
  1010. skb_pull(skb, 2); /* pull off A/C bytes */
  1011. } else if (len == 0) {
  1012. /* didn't compress, or CCP not up yet */
  1013. consume_skb(new_skb);
  1014. new_skb = skb;
  1015. } else {
  1016. /*
  1017. * (len < 0)
  1018. * MPPE requires that we do not send unencrypted
  1019. * frames. The compressor will return -1 if we
  1020. * should drop the frame. We cannot simply test
  1021. * the compress_proto because MPPE and MPPC share
  1022. * the same number.
  1023. */
  1024. if (net_ratelimit())
  1025. netdev_err(ppp->dev, "ppp: compressor dropped pkt\n");
  1026. kfree_skb(skb);
  1027. consume_skb(new_skb);
  1028. new_skb = NULL;
  1029. }
  1030. return new_skb;
  1031. }
  1032. /*
  1033. * Compress and send a frame.
  1034. * The caller should have locked the xmit path,
  1035. * and xmit_pending should be 0.
  1036. */
  1037. static void
  1038. ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
  1039. {
  1040. int proto = PPP_PROTO(skb);
  1041. struct sk_buff *new_skb;
  1042. int len;
  1043. unsigned char *cp;
  1044. if (proto < 0x8000) {
  1045. #ifdef CONFIG_PPP_FILTER
  1046. /* check if we should pass this packet */
  1047. /* the filter instructions are constructed assuming
  1048. a four-byte PPP header on each packet */
  1049. *skb_push(skb, 2) = 1;
  1050. if (ppp->pass_filter &&
  1051. sk_run_filter(skb, ppp->pass_filter) == 0) {
  1052. if (ppp->debug & 1)
  1053. netdev_printk(KERN_DEBUG, ppp->dev,
  1054. "PPP: outbound frame "
  1055. "not passed\n");
  1056. kfree_skb(skb);
  1057. return;
  1058. }
  1059. /* if this packet passes the active filter, record the time */
  1060. if (!(ppp->active_filter &&
  1061. sk_run_filter(skb, ppp->active_filter) == 0))
  1062. ppp->last_xmit = jiffies;
  1063. skb_pull(skb, 2);
  1064. #else
  1065. /* for data packets, record the time */
  1066. ppp->last_xmit = jiffies;
  1067. #endif /* CONFIG_PPP_FILTER */
  1068. }
  1069. ++ppp->stats64.tx_packets;
  1070. ppp->stats64.tx_bytes += skb->len - 2;
  1071. switch (proto) {
  1072. case PPP_IP:
  1073. if (!ppp->vj || (ppp->flags & SC_COMP_TCP) == 0)
  1074. break;
  1075. /* try to do VJ TCP header compression */
  1076. new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2,
  1077. GFP_ATOMIC);
  1078. if (!new_skb) {
  1079. netdev_err(ppp->dev, "PPP: no memory (VJ comp pkt)\n");
  1080. goto drop;
  1081. }
  1082. skb_reserve(new_skb, ppp->dev->hard_header_len - 2);
  1083. cp = skb->data + 2;
  1084. len = slhc_compress(ppp->vj, cp, skb->len - 2,
  1085. new_skb->data + 2, &cp,
  1086. !(ppp->flags & SC_NO_TCP_CCID));
  1087. if (cp == skb->data + 2) {
  1088. /* didn't compress */
  1089. consume_skb(new_skb);
  1090. } else {
  1091. if (cp[0] & SL_TYPE_COMPRESSED_TCP) {
  1092. proto = PPP_VJC_COMP;
  1093. cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
  1094. } else {
  1095. proto = PPP_VJC_UNCOMP;
  1096. cp[0] = skb->data[2];
  1097. }
  1098. consume_skb(skb);
  1099. skb = new_skb;
  1100. cp = skb_put(skb, len + 2);
  1101. cp[0] = 0;
  1102. cp[1] = proto;
  1103. }
  1104. break;
  1105. case PPP_CCP:
  1106. /* peek at outbound CCP frames */
  1107. ppp_ccp_peek(ppp, skb, 0);
  1108. break;
  1109. }
  1110. /* try to do packet compression */
  1111. if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state &&
  1112. proto != PPP_LCP && proto != PPP_CCP) {
  1113. if (!(ppp->flags & SC_CCP_UP) && (ppp->flags & SC_MUST_COMP)) {
  1114. if (net_ratelimit())
  1115. netdev_err(ppp->dev,
  1116. "ppp: compression required but "
  1117. "down - pkt dropped.\n");
  1118. goto drop;
  1119. }
  1120. skb = pad_compress_skb(ppp, skb);
  1121. if (!skb)
  1122. goto drop;
  1123. }
  1124. /*
  1125. * If we are waiting for traffic (demand dialling),
  1126. * queue it up for pppd to receive.
  1127. */
  1128. if (ppp->flags & SC_LOOP_TRAFFIC) {
  1129. if (ppp->file.rq.qlen > PPP_MAX_RQLEN)
  1130. goto drop;
  1131. skb_queue_tail(&ppp->file.rq, skb);
  1132. wake_up_interruptible(&ppp->file.rwait);
  1133. return;
  1134. }
  1135. ppp->xmit_pending = skb;
  1136. ppp_push(ppp);
  1137. return;
  1138. drop:
  1139. kfree_skb(skb);
  1140. ++ppp->dev->stats.tx_errors;
  1141. }
  1142. /*
  1143. * Try to send the frame in xmit_pending.
  1144. * The caller should have the xmit path locked.
  1145. */
  1146. static void
  1147. ppp_push(struct ppp *ppp)
  1148. {
  1149. struct list_head *list;
  1150. struct channel *pch;
  1151. struct sk_buff *skb = ppp->xmit_pending;
  1152. if (!skb)
  1153. return;
  1154. list = &ppp->channels;
  1155. if (list_empty(list)) {
  1156. /* nowhere to send the packet, just drop it */
  1157. ppp->xmit_pending = NULL;
  1158. kfree_skb(skb);
  1159. return;
  1160. }
  1161. if ((ppp->flags & SC_MULTILINK) == 0) {
  1162. /* not doing multilink: send it down the first channel */
  1163. list = list->next;
  1164. pch = list_entry(list, struct channel, clist);
  1165. spin_lock_bh(&pch->downl);
  1166. if (pch->chan) {
  1167. if (pch->chan->ops->start_xmit(pch->chan, skb))
  1168. ppp->xmit_pending = NULL;
  1169. } else {
  1170. /* channel got unregistered */
  1171. kfree_skb(skb);
  1172. ppp->xmit_pending = NULL;
  1173. }
  1174. spin_unlock_bh(&pch->downl);
  1175. return;
  1176. }
  1177. #ifdef CONFIG_PPP_MULTILINK
  1178. /* Multilink: fragment the packet over as many links
  1179. as can take the packet at the moment. */
  1180. if (!ppp_mp_explode(ppp, skb))
  1181. return;
  1182. #endif /* CONFIG_PPP_MULTILINK */
  1183. ppp->xmit_pending = NULL;
  1184. kfree_skb(skb);
  1185. }
  1186. #ifdef CONFIG_PPP_MULTILINK
  1187. static bool mp_protocol_compress __read_mostly = true;
  1188. module_param(mp_protocol_compress, bool, S_IRUGO | S_IWUSR);
  1189. MODULE_PARM_DESC(mp_protocol_compress,
  1190. "compress protocol id in multilink fragments");
  1191. /*
  1192. * Divide a packet to be transmitted into fragments and
  1193. * send them out the individual links.
  1194. */
  1195. static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
  1196. {
  1197. int len, totlen;
  1198. int i, bits, hdrlen, mtu;
  1199. int flen;
  1200. int navail, nfree, nzero;
  1201. int nbigger;
  1202. int totspeed;
  1203. int totfree;
  1204. unsigned char *p, *q;
  1205. struct list_head *list;
  1206. struct channel *pch;
  1207. struct sk_buff *frag;
  1208. struct ppp_channel *chan;
  1209. totspeed = 0; /*total bitrate of the bundle*/
  1210. nfree = 0; /* # channels which have no packet already queued */
  1211. navail = 0; /* total # of usable channels (not deregistered) */
  1212. nzero = 0; /* number of channels with zero speed associated*/
  1213. totfree = 0; /*total # of channels available and
  1214. *having no queued packets before
  1215. *starting the fragmentation*/
  1216. hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
  1217. i = 0;
  1218. list_for_each_entry(pch, &ppp->channels, clist) {
  1219. if (pch->chan) {
  1220. pch->avail = 1;
  1221. navail++;
  1222. pch->speed = pch->chan->speed;
  1223. } else {
  1224. pch->avail = 0;
  1225. }
  1226. if (pch->avail) {
  1227. if (skb_queue_empty(&pch->file.xq) ||
  1228. !pch->had_frag) {
  1229. if (pch->speed == 0)
  1230. nzero++;
  1231. else
  1232. totspeed += pch->speed;
  1233. pch->avail = 2;
  1234. ++nfree;
  1235. ++totfree;
  1236. }
  1237. if (!pch->had_frag && i < ppp->nxchan)
  1238. ppp->nxchan = i;
  1239. }
  1240. ++i;
  1241. }
  1242. /*
  1243. * Don't start sending this packet unless at least half of
  1244. * the channels are free. This gives much better TCP
  1245. * performance if we have a lot of channels.
  1246. */
  1247. if (nfree == 0 || nfree < navail / 2)
  1248. return 0; /* can't take now, leave it in xmit_pending */
  1249. /* Do protocol field compression */
  1250. p = skb->data;
  1251. len = skb->len;
  1252. if (*p == 0 && mp_protocol_compress) {
  1253. ++p;
  1254. --len;
  1255. }
  1256. totlen = len;
  1257. nbigger = len % nfree;
  1258. /* skip to the channel after the one we last used
  1259. and start at that one */
  1260. list = &ppp->channels;
  1261. for (i = 0; i < ppp->nxchan; ++i) {
  1262. list = list->next;
  1263. if (list == &ppp->channels) {
  1264. i = 0;
  1265. break;
  1266. }
  1267. }
  1268. /* create a fragment for each channel */
  1269. bits = B;
  1270. while (len > 0) {
  1271. list = list->next;
  1272. if (list == &ppp->channels) {
  1273. i = 0;
  1274. continue;
  1275. }
  1276. pch = list_entry(list, struct channel, clist);
  1277. ++i;
  1278. if (!pch->avail)
  1279. continue;
  1280. /*
  1281. * Skip this channel if it has a fragment pending already and
  1282. * we haven't given a fragment to all of the free channels.
  1283. */
  1284. if (pch->avail == 1) {
  1285. if (nfree > 0)
  1286. continue;
  1287. } else {
  1288. pch->avail = 1;
  1289. }
  1290. /* check the channel's mtu and whether it is still attached. */
  1291. spin_lock_bh(&pch->downl);
  1292. if (pch->chan == NULL) {
  1293. /* can't use this channel, it's being deregistered */
  1294. if (pch->speed == 0)
  1295. nzero--;
  1296. else
  1297. totspeed -= pch->speed;
  1298. spin_unlock_bh(&pch->downl);
  1299. pch->avail = 0;
  1300. totlen = len;
  1301. totfree--;
  1302. nfree--;
  1303. if (--navail == 0)
  1304. break;
  1305. continue;
  1306. }
  1307. /*
  1308. *if the channel speed is not set divide
  1309. *the packet evenly among the free channels;
  1310. *otherwise divide it according to the speed
  1311. *of the channel we are going to transmit on
  1312. */
  1313. flen = len;
  1314. if (nfree > 0) {
  1315. if (pch->speed == 0) {
  1316. flen = len/nfree;
  1317. if (nbigger > 0) {
  1318. flen++;
  1319. nbigger--;
  1320. }
  1321. } else {
  1322. flen = (((totfree - nzero)*(totlen + hdrlen*totfree)) /
  1323. ((totspeed*totfree)/pch->speed)) - hdrlen;
  1324. if (nbigger > 0) {
  1325. flen += ((totfree - nzero)*pch->speed)/totspeed;
  1326. nbigger -= ((totfree - nzero)*pch->speed)/
  1327. totspeed;
  1328. }
  1329. }
  1330. nfree--;
  1331. }
  1332. /*
  1333. *check if we are on the last channel or
  1334. *we exceded the length of the data to
  1335. *fragment
  1336. */
  1337. if ((nfree <= 0) || (flen > len))
  1338. flen = len;
  1339. /*
  1340. *it is not worth to tx on slow channels:
  1341. *in that case from the resulting flen according to the
  1342. *above formula will be equal or less than zero.
  1343. *Skip the channel in this case
  1344. */
  1345. if (flen <= 0) {
  1346. pch->avail = 2;
  1347. spin_unlock_bh(&pch->downl);
  1348. continue;
  1349. }
  1350. /*
  1351. * hdrlen includes the 2-byte PPP protocol field, but the
  1352. * MTU counts only the payload excluding the protocol field.
  1353. * (RFC1661 Section 2)
  1354. */
  1355. mtu = pch->chan->mtu - (hdrlen - 2);
  1356. if (mtu < 4)
  1357. mtu = 4;
  1358. if (flen > mtu)
  1359. flen = mtu;
  1360. if (flen == len)
  1361. bits |= E;
  1362. frag = alloc_skb(flen + hdrlen + (flen == 0), GFP_ATOMIC);
  1363. if (!frag)
  1364. goto noskb;
  1365. q = skb_put(frag, flen + hdrlen);
  1366. /* make the MP header */
  1367. put_unaligned_be16(PPP_MP, q);
  1368. if (ppp->flags & SC_MP_XSHORTSEQ) {
  1369. q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
  1370. q[3] = ppp->nxseq;
  1371. } else {
  1372. q[2] = bits;
  1373. q[3] = ppp->nxseq >> 16;
  1374. q[4] = ppp->nxseq >> 8;
  1375. q[5] = ppp->nxseq;
  1376. }
  1377. memcpy(q + hdrlen, p, flen);
  1378. /* try to send it down the channel */
  1379. chan = pch->chan;
  1380. if (!skb_queue_empty(&pch->file.xq) ||
  1381. !chan->ops->start_xmit(chan, frag))
  1382. skb_queue_tail(&pch->file.xq, frag);
  1383. pch->had_frag = 1;
  1384. p += flen;
  1385. len -= flen;
  1386. ++ppp->nxseq;
  1387. bits = 0;
  1388. spin_unlock_bh(&pch->downl);
  1389. }
  1390. ppp->nxchan = i;
  1391. return 1;
  1392. noskb:
  1393. spin_unlock_bh(&pch->downl);
  1394. if (ppp->debug & 1)
  1395. netdev_err(ppp->dev, "PPP: no memory (fragment)\n");
  1396. ++ppp->dev->stats.tx_errors;
  1397. ++ppp->nxseq;
  1398. return 1; /* abandon the frame */
  1399. }
  1400. #endif /* CONFIG_PPP_MULTILINK */
  1401. /*
  1402. * Try to send data out on a channel.
  1403. */
  1404. static void
  1405. ppp_channel_push(struct channel *pch)
  1406. {
  1407. struct sk_buff *skb;
  1408. struct ppp *ppp;
  1409. spin_lock_bh(&pch->downl);
  1410. if (pch->chan) {
  1411. while (!skb_queue_empty(&pch->file.xq)) {
  1412. skb = skb_dequeue(&pch->file.xq);
  1413. if (!pch->chan->ops->start_xmit(pch->chan, skb)) {
  1414. /* put the packet back and try again later */
  1415. skb_queue_head(&pch->file.xq, skb);
  1416. break;
  1417. }
  1418. }
  1419. } else {
  1420. /* channel got deregistered */
  1421. skb_queue_purge(&pch->file.xq);
  1422. }
  1423. spin_unlock_bh(&pch->downl);
  1424. /* see if there is anything from the attached unit to be sent */
  1425. if (skb_queue_empty(&pch->file.xq)) {
  1426. read_lock_bh(&pch->upl);
  1427. ppp = pch->ppp;
  1428. if (ppp)
  1429. ppp_xmit_process(ppp);
  1430. read_unlock_bh(&pch->upl);
  1431. }
  1432. }
  1433. /*
  1434. * Receive-side routines.
  1435. */
  1436. struct ppp_mp_skb_parm {
  1437. u32 sequence;
  1438. u8 BEbits;
  1439. };
  1440. #define PPP_MP_CB(skb) ((struct ppp_mp_skb_parm *)((skb)->cb))
  1441. static inline void
  1442. ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
  1443. {
  1444. ppp_recv_lock(ppp);
  1445. if (!ppp->closing)
  1446. ppp_receive_frame(ppp, skb, pch);
  1447. else
  1448. kfree_skb(skb);
  1449. ppp_recv_unlock(ppp);
  1450. }
  1451. void
  1452. ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
  1453. {
  1454. struct channel *pch = chan->ppp;
  1455. int proto;
  1456. if (!pch) {
  1457. kfree_skb(skb);
  1458. return;
  1459. }
  1460. read_lock_bh(&pch->upl);
  1461. if (!pskb_may_pull(skb, 2)) {
  1462. kfree_skb(skb);
  1463. if (pch->ppp) {
  1464. ++pch->ppp->dev->stats.rx_length_errors;
  1465. ppp_receive_error(pch->ppp);
  1466. }
  1467. goto done;
  1468. }
  1469. proto = PPP_PROTO(skb);
  1470. if (!pch->ppp || proto >= 0xc000 || proto == PPP_CCPFRAG) {
  1471. /* put it on the channel queue */
  1472. skb_queue_tail(&pch->file.rq, skb);
  1473. /* drop old frames if queue too long */
  1474. while (pch->file.rq.qlen > PPP_MAX_RQLEN &&
  1475. (skb = skb_dequeue(&pch->file.rq)))
  1476. kfree_skb(skb);
  1477. wake_up_interruptible(&pch->file.rwait);
  1478. } else {
  1479. ppp_do_recv(pch->ppp, skb, pch);
  1480. }
  1481. done:
  1482. read_unlock_bh(&pch->upl);
  1483. }
  1484. /* Put a 0-length skb in the receive queue as an error indication */
  1485. void
  1486. ppp_input_error(struct ppp_channel *chan, int code)
  1487. {
  1488. struct channel *pch = chan->ppp;
  1489. struct sk_buff *skb;
  1490. if (!pch)
  1491. return;
  1492. read_lock_bh(&pch->upl);
  1493. if (pch->ppp) {
  1494. skb = alloc_skb(0, GFP_ATOMIC);
  1495. if (skb) {
  1496. skb->len = 0; /* probably unnecessary */
  1497. skb->cb[0] = code;
  1498. ppp_do_recv(pch->ppp, skb, pch);
  1499. }
  1500. }
  1501. read_unlock_bh(&pch->upl);
  1502. }
  1503. /*
  1504. * We come in here to process a received frame.
  1505. * The receive side of the ppp unit is locked.
  1506. */
  1507. static void
  1508. ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
  1509. {
  1510. /* note: a 0-length skb is used as an error indication */
  1511. if (skb->len > 0) {
  1512. #ifdef CONFIG_PPP_MULTILINK
  1513. /* XXX do channel-level decompression here */
  1514. if (PPP_PROTO(skb) == PPP_MP)
  1515. ppp_receive_mp_frame(ppp, skb, pch);
  1516. else
  1517. #endif /* CONFIG_PPP_MULTILINK */
  1518. ppp_receive_nonmp_frame(ppp, skb);
  1519. } else {
  1520. kfree_skb(skb);
  1521. ppp_receive_error(ppp);
  1522. }
  1523. }
  1524. static void
  1525. ppp_receive_error(struct ppp *ppp)
  1526. {
  1527. ++ppp->dev->stats.rx_errors;
  1528. if (ppp->vj)
  1529. slhc_toss(ppp->vj);
  1530. }
  1531. static void
  1532. ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
  1533. {
  1534. struct sk_buff *ns;
  1535. int proto, len, npi;
  1536. /*
  1537. * Decompress the frame, if compressed.
  1538. * Note that some decompressors need to see uncompressed frames
  1539. * that come in as well as compressed frames.
  1540. */
  1541. if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN) &&
  1542. (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
  1543. skb = ppp_decompress_frame(ppp, skb);
  1544. if (ppp->flags & SC_MUST_COMP && ppp->rstate & SC_DC_FERROR)
  1545. goto err;
  1546. proto = PPP_PROTO(skb);
  1547. switch (proto) {
  1548. case PPP_VJC_COMP:
  1549. /* decompress VJ compressed packets */
  1550. if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
  1551. goto err;
  1552. if (skb_tailroom(skb) < 124 || skb_cloned(skb)) {
  1553. /* copy to a new sk_buff with more tailroom */
  1554. ns = dev_alloc_skb(skb->len + 128);
  1555. if (!ns) {
  1556. netdev_err(ppp->dev, "PPP: no memory "
  1557. "(VJ decomp)\n");
  1558. goto err;
  1559. }
  1560. skb_reserve(ns, 2);
  1561. skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
  1562. consume_skb(skb);
  1563. skb = ns;
  1564. }
  1565. else
  1566. skb->ip_summed = CHECKSUM_NONE;
  1567. len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
  1568. if (len <= 0) {
  1569. netdev_printk(KERN_DEBUG, ppp->dev,
  1570. "PPP: VJ decompression error\n");
  1571. goto err;
  1572. }
  1573. len += 2;
  1574. if (len > skb->len)
  1575. skb_put(skb, len - skb->len);
  1576. else if (len < skb->len)
  1577. skb_trim(skb, len);
  1578. proto = PPP_IP;
  1579. break;
  1580. case PPP_VJC_UNCOMP:
  1581. if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
  1582. goto err;
  1583. /* Until we fix the decompressor need to make sure
  1584. * data portion is linear.
  1585. */
  1586. if (!pskb_may_pull(skb, skb->len))
  1587. goto err;
  1588. if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
  1589. netdev_err(ppp->dev, "PPP: VJ uncompressed error\n");
  1590. goto err;
  1591. }
  1592. proto = PPP_IP;
  1593. break;
  1594. case PPP_CCP:
  1595. ppp_ccp_peek(ppp, skb, 1);
  1596. break;
  1597. }
  1598. ++ppp->stats64.rx_packets;
  1599. ppp->stats64.rx_bytes += skb->len - 2;
  1600. npi = proto_to_npindex(proto);
  1601. if (npi < 0) {
  1602. /* control or unknown frame - pass it to pppd */
  1603. skb_queue_tail(&ppp->file.rq, skb);
  1604. /* limit queue length by dropping old frames */
  1605. while (ppp->file.rq.qlen > PPP_MAX_RQLEN &&
  1606. (skb = skb_dequeue(&ppp->file.rq)))
  1607. kfree_skb(skb);
  1608. /* wake up any process polling or blocking on read */
  1609. wake_up_interruptible(&ppp->file.rwait);
  1610. } else {
  1611. /* network protocol frame - give it to the kernel */
  1612. #ifdef CONFIG_PPP_FILTER
  1613. /* check if the packet passes the pass and active filters */
  1614. /* the filter instructions are constructed assuming
  1615. a four-byte PPP header on each packet */
  1616. if (ppp->pass_filter || ppp->active_filter) {
  1617. if (skb_cloned(skb) &&
  1618. pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  1619. goto err;
  1620. *skb_push(skb, 2) = 0;
  1621. if (ppp->pass_filter &&
  1622. sk_run_filter(skb, ppp->pass_filter) == 0) {
  1623. if (ppp->debug & 1)
  1624. netdev_printk(KERN_DEBUG, ppp->dev,
  1625. "PPP: inbound frame "
  1626. "not passed\n");
  1627. kfree_skb(skb);
  1628. return;
  1629. }
  1630. if (!(ppp->active_filter &&
  1631. sk_run_filter(skb, ppp->active_filter) == 0))
  1632. ppp->last_recv = jiffies;
  1633. __skb_pull(skb, 2);
  1634. } else
  1635. #endif /* CONFIG_PPP_FILTER */
  1636. ppp->last_recv = jiffies;
  1637. if ((ppp->dev->flags & IFF_UP) == 0 ||
  1638. ppp->npmode[npi] != NPMODE_PASS) {
  1639. kfree_skb(skb);
  1640. } else {
  1641. /* chop off protocol */
  1642. skb_pull_rcsum(skb, 2);
  1643. skb->dev = ppp->dev;
  1644. skb->protocol = htons(npindex_to_ethertype[npi]);
  1645. skb_reset_mac_header(skb);
  1646. netif_rx(skb);
  1647. }
  1648. }
  1649. return;
  1650. err:
  1651. kfree_skb(skb);
  1652. ppp_receive_error(ppp);
  1653. }
  1654. static struct sk_buff *
  1655. ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
  1656. {
  1657. int proto = PPP_PROTO(skb);
  1658. struct sk_buff *ns;
  1659. int len;
  1660. /* Until we fix all the decompressor's need to make sure
  1661. * data portion is linear.
  1662. */
  1663. if (!pskb_may_pull(skb, skb->len))
  1664. goto err;
  1665. if (proto == PPP_COMP) {
  1666. int obuff_size;
  1667. switch(ppp->rcomp->compress_proto) {
  1668. case CI_MPPE:
  1669. obuff_size = ppp->mru + PPP_HDRLEN + 1;
  1670. break;
  1671. default:
  1672. obuff_size = ppp->mru + PPP_HDRLEN;
  1673. break;
  1674. }
  1675. ns = dev_alloc_skb(obuff_size);
  1676. if (!ns) {
  1677. netdev_err(ppp->dev, "ppp_decompress_frame: "
  1678. "no memory\n");
  1679. goto err;
  1680. }
  1681. /* the decompressor still expects the A/C bytes in the hdr */
  1682. len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
  1683. skb->len + 2, ns->data, obuff_size);
  1684. if (len < 0) {
  1685. /* Pass the compressed frame to pppd as an
  1686. error indication. */
  1687. if (len == DECOMP_FATALERROR)
  1688. ppp->rstate |= SC_DC_FERROR;
  1689. kfree_skb(ns);
  1690. goto err;
  1691. }
  1692. consume_skb(skb);
  1693. skb = ns;
  1694. skb_put(skb, len);
  1695. skb_pull(skb, 2); /* pull off the A/C bytes */
  1696. } else {
  1697. /* Uncompressed frame - pass to decompressor so it
  1698. can update its dictionary if necessary. */
  1699. if (ppp->rcomp->incomp)
  1700. ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
  1701. skb->len + 2);
  1702. }
  1703. return skb;
  1704. err:
  1705. ppp->rstate |= SC_DC_ERROR;
  1706. ppp_receive_error(ppp);
  1707. return skb;
  1708. }
  1709. #ifdef CONFIG_PPP_MULTILINK
  1710. /*
  1711. * Receive a multilink frame.
  1712. * We put it on the reconstruction queue and then pull off
  1713. * as many completed frames as we can.
  1714. */
  1715. static void
  1716. ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
  1717. {
  1718. u32 mask, seq;
  1719. struct channel *ch;
  1720. int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
  1721. if (!pskb_may_pull(skb, mphdrlen + 1) || ppp->mrru == 0)
  1722. goto err; /* no good, throw it away */
  1723. /* Decode sequence number and begin/end bits */
  1724. if (ppp->flags & SC_MP_SHORTSEQ) {
  1725. seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
  1726. mask = 0xfff;
  1727. } else {
  1728. seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
  1729. mask = 0xffffff;
  1730. }
  1731. PPP_MP_CB(skb)->BEbits = skb->data[2];
  1732. skb_pull(skb, mphdrlen); /* pull off PPP and MP headers */
  1733. /*
  1734. * Do protocol ID decompression on the first fragment of each packet.
  1735. */
  1736. if ((PPP_MP_CB(skb)->BEbits & B) && (skb->data[0] & 1))
  1737. *skb_push(skb, 1) = 0;
  1738. /*
  1739. * Expand sequence number to 32 bits, making it as close
  1740. * as possible to ppp->minseq.
  1741. */
  1742. seq |= ppp->minseq & ~mask;
  1743. if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
  1744. seq += mask + 1;
  1745. else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
  1746. seq -= mask + 1; /* should never happen */
  1747. PPP_MP_CB(skb)->sequence = seq;
  1748. pch->lastseq = seq;
  1749. /*
  1750. * If this packet comes before the next one we were expecting,
  1751. * drop it.
  1752. */
  1753. if (seq_before(seq, ppp->nextseq)) {
  1754. kfree_skb(skb);
  1755. ++ppp->dev->stats.rx_dropped;
  1756. ppp_receive_error(ppp);
  1757. return;
  1758. }
  1759. /*
  1760. * Reevaluate minseq, the minimum over all channels of the
  1761. * last sequence number received on each channel. Because of
  1762. * the increasing sequence number rule, we know that any fragment
  1763. * before `minseq' which hasn't arrived is never going to arrive.
  1764. * The list of channels can't change because we have the receive
  1765. * side of the ppp unit locked.
  1766. */
  1767. list_for_each_entry(ch, &ppp->channels, clist) {
  1768. if (seq_before(ch->lastseq, seq))
  1769. seq = ch->lastseq;
  1770. }
  1771. if (seq_before(ppp->minseq, seq))
  1772. ppp->minseq = seq;
  1773. /* Put the fragment on the reconstruction queue */
  1774. ppp_mp_insert(ppp, skb);
  1775. /* If the queue is getting long, don't wait any longer for packets
  1776. before the start of the queue. */
  1777. if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN) {
  1778. struct sk_buff *mskb = skb_peek(&ppp->mrq);
  1779. if (seq_before(ppp->minseq, PPP_MP_CB(mskb)->sequence))
  1780. ppp->minseq = PPP_MP_CB(mskb)->sequence;
  1781. }
  1782. /* Pull completed packets off the queue and receive them. */
  1783. while ((skb = ppp_mp_reconstruct(ppp))) {
  1784. if (pskb_may_pull(skb, 2))
  1785. ppp_receive_nonmp_frame(ppp, skb);
  1786. else {
  1787. ++ppp->dev->stats.rx_length_errors;
  1788. kfree_skb(skb);
  1789. ppp_receive_error(ppp);
  1790. }
  1791. }
  1792. return;
  1793. err:
  1794. kfree_skb(skb);
  1795. ppp_receive_error(ppp);
  1796. }
  1797. /*
  1798. * Insert a fragment on the MP reconstruction queue.
  1799. * The queue is ordered by increasing sequence number.
  1800. */
  1801. static void
  1802. ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
  1803. {
  1804. struct sk_buff *p;
  1805. struct sk_buff_head *list = &ppp->mrq;
  1806. u32 seq = PPP_MP_CB(skb)->sequence;
  1807. /* N.B. we don't need to lock the list lock because we have the
  1808. ppp unit receive-side lock. */
  1809. skb_queue_walk(list, p) {
  1810. if (seq_before(seq, PPP_MP_CB(p)->sequence))
  1811. break;
  1812. }
  1813. __skb_queue_before(list, p, skb);
  1814. }
  1815. /*
  1816. * Reconstruct a packet from the MP fragment queue.
  1817. * We go through increasing sequence numbers until we find a
  1818. * complete packet, or we get to the sequence number for a fragment
  1819. * which hasn't arrived but might still do so.
  1820. */
  1821. static struct sk_buff *
  1822. ppp_mp_reconstruct(struct ppp *ppp)
  1823. {
  1824. u32 seq = ppp->nextseq;
  1825. u32 minseq = ppp->minseq;
  1826. struct sk_buff_head *list = &ppp->mrq;
  1827. struct sk_buff *p, *tmp;
  1828. struct sk_buff *head, *tail;
  1829. struct sk_buff *skb = NULL;
  1830. int lost = 0, len = 0;
  1831. if (ppp->mrru == 0) /* do nothing until mrru is set */
  1832. return NULL;
  1833. head = list->next;
  1834. tail = NULL;
  1835. skb_queue_walk_safe(list, p, tmp) {
  1836. again:
  1837. if (seq_before(PPP_MP_CB(p)->sequence, seq)) {
  1838. /* this can't happen, anyway ignore the skb */
  1839. netdev_err(ppp->dev, "ppp_mp_reconstruct bad "
  1840. "seq %u < %u\n",
  1841. PPP_MP_CB(p)->sequence, seq);
  1842. __skb_unlink(p, list);
  1843. kfree_skb(p);
  1844. continue;
  1845. }
  1846. if (PPP_MP_CB(p)->sequence != seq) {
  1847. u32 oldseq;
  1848. /* Fragment `seq' is missing. If it is after
  1849. minseq, it might arrive later, so stop here. */
  1850. if (seq_after(seq, minseq))
  1851. break;
  1852. /* Fragment `seq' is lost, keep going. */
  1853. lost = 1;
  1854. oldseq = seq;
  1855. seq = seq_before(minseq, PPP_MP_CB(p)->sequence)?
  1856. minseq + 1: PPP_MP_CB(p)->sequence;
  1857. if (ppp->debug & 1)
  1858. netdev_printk(KERN_DEBUG, ppp->dev,
  1859. "lost frag %u..%u\n",
  1860. oldseq, seq-1);
  1861. goto again;
  1862. }
  1863. /*
  1864. * At this point we know that all the fragments from
  1865. * ppp->nextseq to seq are either present or lost.
  1866. * Also, there are no complete packets in the queue
  1867. * that have no missing fragments and end before this
  1868. * fragment.
  1869. */
  1870. /* B bit set indicates this fragment starts a packet */
  1871. if (PPP_MP_CB(p)->BEbits & B) {
  1872. head = p;
  1873. lost = 0;
  1874. len = 0;
  1875. }
  1876. len += p->len;
  1877. /* Got a complete packet yet? */
  1878. if (lost == 0 && (PPP_MP_CB(p)->BEbits & E) &&
  1879. (PPP_MP_CB(head)->BEbits & B)) {
  1880. if (len > ppp->mrru + 2) {
  1881. ++ppp->dev->stats.rx_length_errors;
  1882. netdev_printk(KERN_DEBUG, ppp->dev,
  1883. "PPP: reconstructed packet"
  1884. " is too long (%d)\n", len);
  1885. } else {
  1886. tail = p;
  1887. break;
  1888. }
  1889. ppp->nextseq = seq + 1;
  1890. }
  1891. /*
  1892. * If this is the ending fragment of a packet,
  1893. * and we haven't found a complete valid packet yet,
  1894. * we can discard up to and including this fragment.
  1895. */
  1896. if (PPP_MP_CB(p)->BEbits & E) {
  1897. struct sk_buff *tmp2;
  1898. skb_queue_reverse_walk_from_safe(list, p, tmp2) {
  1899. if (ppp->debug & 1)
  1900. netdev_printk(KERN_DEBUG, ppp->dev,
  1901. "discarding frag %u\n",
  1902. PPP_MP_CB(p)->sequence);
  1903. __skb_unlink(p, list);
  1904. kfree_skb(p);
  1905. }
  1906. head = skb_peek(list);
  1907. if (!head)
  1908. break;
  1909. }
  1910. ++seq;
  1911. }
  1912. /* If we have a complete packet, copy it all into one skb. */
  1913. if (tail != NULL) {
  1914. /* If we have discarded any fragments,
  1915. signal a receive error. */
  1916. if (PPP_MP_CB(head)->sequence != ppp->nextseq) {
  1917. skb_queue_walk_safe(list, p, tmp) {
  1918. if (p == head)
  1919. break;
  1920. if (ppp->debug & 1)
  1921. netdev_printk(KERN_DEBUG, ppp->dev,
  1922. "discarding frag %u\n",
  1923. PPP_MP_CB(p)->sequence);
  1924. __skb_unlink(p, list);
  1925. kfree_skb(p);
  1926. }
  1927. if (ppp->debug & 1)
  1928. netdev_printk(KERN_DEBUG, ppp->dev,
  1929. " missed pkts %u..%u\n",
  1930. ppp->nextseq,
  1931. PPP_MP_CB(head)->sequence-1);
  1932. ++ppp->dev->stats.rx_dropped;
  1933. ppp_receive_error(ppp);
  1934. }
  1935. skb = head;
  1936. if (head != tail) {
  1937. struct sk_buff **fragpp = &skb_shinfo(skb)->frag_list;
  1938. p = skb_queue_next(list, head);
  1939. __skb_unlink(skb, list);
  1940. skb_queue_walk_from_safe(list, p, tmp) {
  1941. __skb_unlink(p, list);
  1942. *fragpp = p;
  1943. p->next = NULL;
  1944. fragpp = &p->next;
  1945. skb->len += p->len;
  1946. skb->data_len += p->len;
  1947. skb->truesize += p->truesize;
  1948. if (p == tail)
  1949. break;
  1950. }
  1951. } else {
  1952. __skb_unlink(skb, list);
  1953. }
  1954. ppp->nextseq = PPP_MP_CB(tail)->sequence + 1;
  1955. }
  1956. return skb;
  1957. }
  1958. #endif /* CONFIG_PPP_MULTILINK */
  1959. /*
  1960. * Channel interface.
  1961. */
  1962. /* Create a new, unattached ppp channel. */
  1963. int ppp_register_channel(struct ppp_channel *chan)
  1964. {
  1965. return ppp_register_net_channel(current->nsproxy->net_ns, chan);
  1966. }
  1967. /* Create a new, unattached ppp channel for specified net. */
  1968. int ppp_register_net_channel(struct net *net, struct ppp_channel *chan)
  1969. {
  1970. struct channel *pch;
  1971. struct ppp_net *pn;
  1972. pch = kzalloc(sizeof(struct channel), GFP_KERNEL);
  1973. if (!pch)
  1974. return -ENOMEM;
  1975. pn = ppp_pernet(net);
  1976. pch->ppp = NULL;
  1977. pch->chan = chan;
  1978. pch->chan_net = net;
  1979. chan->ppp = pch;
  1980. init_ppp_file(&pch->file, CHANNEL);
  1981. pch->file.hdrlen = chan->hdrlen;
  1982. #ifdef CONFIG_PPP_MULTILINK
  1983. pch->lastseq = -1;
  1984. #endif /* CONFIG_PPP_MULTILINK */
  1985. init_rwsem(&pch->chan_sem);
  1986. spin_lock_init(&pch->downl);
  1987. rwlock_init(&pch->upl);
  1988. spin_lock_bh(&pn->all_channels_lock);
  1989. pch->file.index = ++pn->last_channel_index;
  1990. list_add(&pch->list, &pn->new_channels);
  1991. atomic_inc(&channel_count);
  1992. spin_unlock_bh(&pn->all_channels_lock);
  1993. return 0;
  1994. }
  1995. /*
  1996. * Return the index of a channel.
  1997. */
  1998. int ppp_channel_index(struct ppp_channel *chan)
  1999. {
  2000. struct channel *pch = chan->ppp;
  2001. if (pch)
  2002. return pch->file.index;
  2003. return -1;
  2004. }
  2005. /*
  2006. * Return the PPP unit number to which a channel is connected.
  2007. */
  2008. int ppp_unit_number(struct ppp_channel *chan)
  2009. {
  2010. struct channel *pch = chan->ppp;
  2011. int unit = -1;
  2012. if (pch) {
  2013. read_lock_bh(&pch->upl);
  2014. if (pch->ppp)
  2015. unit = pch->ppp->file.index;
  2016. read_unlock_bh(&pch->upl);
  2017. }
  2018. return unit;
  2019. }
  2020. /*
  2021. * Return the PPP device interface name of a channel.
  2022. */
  2023. char *ppp_dev_name(struct ppp_channel *chan)
  2024. {
  2025. struct channel *pch = chan->ppp;
  2026. char *name = NULL;
  2027. if (pch) {
  2028. read_lock_bh(&pch->upl);
  2029. if (pch->ppp && pch->ppp->dev)
  2030. name = pch->ppp->dev->name;
  2031. read_unlock_bh(&pch->upl);
  2032. }
  2033. return name;
  2034. }
  2035. /*
  2036. * Disconnect a channel from the generic layer.
  2037. * This must be called in process context.
  2038. */
  2039. void
  2040. ppp_unregister_channel(struct ppp_channel *chan)
  2041. {
  2042. struct channel *pch = chan->ppp;
  2043. struct ppp_net *pn;
  2044. if (!pch)
  2045. return; /* should never happen */
  2046. chan->ppp = NULL;
  2047. /*
  2048. * This ensures that we have returned from any calls into the
  2049. * the channel's start_xmit or ioctl routine before we proceed.
  2050. */
  2051. down_write(&pch->chan_sem);
  2052. spin_lock_bh(&pch->downl);
  2053. pch->chan = NULL;
  2054. spin_unlock_bh(&pch->downl);
  2055. up_write(&pch->chan_sem);
  2056. ppp_disconnect_channel(pch);
  2057. pn = ppp_pernet(pch->chan_net);
  2058. spin_lock_bh(&pn->all_channels_lock);
  2059. list_del(&pch->list);
  2060. spin_unlock_bh(&pn->all_channels_lock);
  2061. pch->file.dead = 1;
  2062. wake_up_interruptible(&pch->file.rwait);
  2063. if (atomic_dec_and_test(&pch->file.refcnt))
  2064. ppp_destroy_channel(pch);
  2065. }
  2066. /*
  2067. * Callback from a channel when it can accept more to transmit.
  2068. * This should be called at BH/softirq level, not interrupt level.
  2069. */
  2070. void
  2071. ppp_output_wakeup(struct ppp_channel *chan)
  2072. {
  2073. struct channel *pch = chan->ppp;
  2074. if (!pch)
  2075. return;
  2076. ppp_channel_push(pch);
  2077. }
  2078. /*
  2079. * Compression control.
  2080. */
  2081. /* Process the PPPIOCSCOMPRESS ioctl. */
  2082. static int
  2083. ppp_set_compress(struct ppp *ppp, unsigned long arg)
  2084. {
  2085. int err;
  2086. struct compressor *cp, *ocomp;
  2087. struct ppp_option_data data;
  2088. void *state, *ostate;
  2089. unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
  2090. err = -EFAULT;
  2091. if (copy_from_user(&data, (void __user *) arg, sizeof(data)) ||
  2092. (data.length <= CCP_MAX_OPTION_LENGTH &&
  2093. copy_from_user(ccp_option, (void __user *) data.ptr, data.length)))
  2094. goto out;
  2095. err = -EINVAL;
  2096. if (data.length > CCP_MAX_OPTION_LENGTH ||
  2097. ccp_option[1] < 2 || ccp_option[1] > data.length)
  2098. goto out;
  2099. cp = try_then_request_module(
  2100. find_compressor(ccp_option[0]),
  2101. "ppp-compress-%d", ccp_option[0]);
  2102. if (!cp)
  2103. goto out;
  2104. err = -ENOBUFS;
  2105. if (data.transmit) {
  2106. state = cp->comp_alloc(ccp_option, data.length);
  2107. if (state) {
  2108. ppp_xmit_lock(ppp);
  2109. ppp->xstate &= ~SC_COMP_RUN;
  2110. ocomp = ppp->xcomp;
  2111. ostate = ppp->xc_state;
  2112. ppp->xcomp = cp;
  2113. ppp->xc_state = state;
  2114. ppp_xmit_unlock(ppp);
  2115. if (ostate) {
  2116. ocomp->comp_free(ostate);
  2117. module_put(ocomp->owner);
  2118. }
  2119. err = 0;
  2120. } else
  2121. module_put(cp->owner);
  2122. } else {
  2123. state = cp->decomp_alloc(ccp_option, data.length);
  2124. if (state) {
  2125. ppp_recv_lock(ppp);
  2126. ppp->rstate &= ~SC_DECOMP_RUN;
  2127. ocomp = ppp->rcomp;
  2128. ostate = ppp->rc_state;
  2129. ppp->rcomp = cp;
  2130. ppp->rc_state = state;
  2131. ppp_recv_unlock(ppp);
  2132. if (ostate) {
  2133. ocomp->decomp_free(ostate);
  2134. module_put(ocomp->owner);
  2135. }
  2136. err = 0;
  2137. } else
  2138. module_put(cp->owner);
  2139. }
  2140. out:
  2141. return err;
  2142. }
  2143. /*
  2144. * Look at a CCP packet and update our state accordingly.
  2145. * We assume the caller has the xmit or recv path locked.
  2146. */
  2147. static void
  2148. ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
  2149. {
  2150. unsigned char *dp;
  2151. int len;
  2152. if (!pskb_may_pull(skb, CCP_HDRLEN + 2))
  2153. return; /* no header */
  2154. dp = skb->data + 2;
  2155. switch (CCP_CODE(dp)) {
  2156. case CCP_CONFREQ:
  2157. /* A ConfReq starts negotiation of compression
  2158. * in one direction of transmission,
  2159. * and hence brings it down...but which way?
  2160. *
  2161. * Remember:
  2162. * A ConfReq indicates what the sender would like to receive
  2163. */
  2164. if(inbound)
  2165. /* He is proposing what I should send */
  2166. ppp->xstate &= ~SC_COMP_RUN;
  2167. else
  2168. /* I am proposing to what he should send */
  2169. ppp->rstate &= ~SC_DECOMP_RUN;
  2170. break;
  2171. case CCP_TERMREQ:
  2172. case CCP_TERMACK:
  2173. /*
  2174. * CCP is going down, both directions of transmission
  2175. */
  2176. ppp->rstate &= ~SC_DECOMP_RUN;
  2177. ppp->xstate &= ~SC_COMP_RUN;
  2178. break;
  2179. case CCP_CONFACK:
  2180. if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
  2181. break;
  2182. len = CCP_LENGTH(dp);
  2183. if (!pskb_may_pull(skb, len + 2))
  2184. return; /* too short */
  2185. dp += CCP_HDRLEN;
  2186. len -= CCP_HDRLEN;
  2187. if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
  2188. break;
  2189. if (inbound) {
  2190. /* we will start receiving compressed packets */
  2191. if (!ppp->rc_state)
  2192. break;
  2193. if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
  2194. ppp->file.index, 0, ppp->mru, ppp->debug)) {
  2195. ppp->rstate |= SC_DECOMP_RUN;
  2196. ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
  2197. }
  2198. } else {
  2199. /* we will soon start sending compressed packets */
  2200. if (!ppp->xc_state)
  2201. break;
  2202. if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
  2203. ppp->file.index, 0, ppp->debug))
  2204. ppp->xstate |= SC_COMP_RUN;
  2205. }
  2206. break;
  2207. case CCP_RESETACK:
  2208. /* reset the [de]compressor */
  2209. if ((ppp->flags & SC_CCP_UP) == 0)
  2210. break;
  2211. if (inbound) {
  2212. if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
  2213. ppp->rcomp->decomp_reset(ppp->rc_state);
  2214. ppp->rstate &= ~SC_DC_ERROR;
  2215. }
  2216. } else {
  2217. if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
  2218. ppp->xcomp->comp_reset(ppp->xc_state);
  2219. }
  2220. break;
  2221. }
  2222. }
  2223. /* Free up compression resources. */
  2224. static void
  2225. ppp_ccp_closed(struct ppp *ppp)
  2226. {
  2227. void *xstate, *rstate;
  2228. struct compressor *xcomp, *rcomp;
  2229. ppp_lock(ppp);
  2230. ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
  2231. ppp->xstate = 0;
  2232. xcomp = ppp->xcomp;
  2233. xstate = ppp->xc_state;
  2234. ppp->xc_state = NULL;
  2235. ppp->rstate = 0;
  2236. rcomp = ppp->rcomp;
  2237. rstate = ppp->rc_state;
  2238. ppp->rc_state = NULL;
  2239. ppp_unlock(ppp);
  2240. if (xstate) {
  2241. xcomp->comp_free(xstate);
  2242. module_put(xcomp->owner);
  2243. }
  2244. if (rstate) {
  2245. rcomp->decomp_free(rstate);
  2246. module_put(rcomp->owner);
  2247. }
  2248. }
  2249. /* List of compressors. */
  2250. static LIST_HEAD(compressor_list);
  2251. static DEFINE_SPINLOCK(compressor_list_lock);
  2252. struct compressor_entry {
  2253. struct list_head list;
  2254. struct compressor *comp;
  2255. };
  2256. static struct compressor_entry *
  2257. find_comp_entry(int proto)
  2258. {
  2259. struct compressor_entry *ce;
  2260. list_for_each_entry(ce, &compressor_list, list) {
  2261. if (ce->comp->compress_proto == proto)
  2262. return ce;
  2263. }
  2264. return NULL;
  2265. }
  2266. /* Register a compressor */
  2267. int
  2268. ppp_register_compressor(struct compressor *cp)
  2269. {
  2270. struct compressor_entry *ce;
  2271. int ret;
  2272. spin_lock(&compressor_list_lock);
  2273. ret = -EEXIST;
  2274. if (find_comp_entry(cp->compress_proto))
  2275. goto out;
  2276. ret = -ENOMEM;
  2277. ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC);
  2278. if (!ce)
  2279. goto out;
  2280. ret = 0;
  2281. ce->comp = cp;
  2282. list_add(&ce->list, &compressor_list);
  2283. out:
  2284. spin_unlock(&compressor_list_lock);
  2285. return ret;
  2286. }
  2287. /* Unregister a compressor */
  2288. void
  2289. ppp_unregister_compressor(struct compressor *cp)
  2290. {
  2291. struct compressor_entry *ce;
  2292. spin_lock(&compressor_list_lock);
  2293. ce = find_comp_entry(cp->compress_proto);
  2294. if (ce && ce->comp == cp) {
  2295. list_del(&ce->list);
  2296. kfree(ce);
  2297. }
  2298. spin_unlock(&compressor_list_lock);
  2299. }
  2300. /* Find a compressor. */
  2301. static struct compressor *
  2302. find_compressor(int type)
  2303. {
  2304. struct compressor_entry *ce;
  2305. struct compressor *cp = NULL;
  2306. spin_lock(&compressor_list_lock);
  2307. ce = find_comp_entry(type);
  2308. if (ce) {
  2309. cp = ce->comp;
  2310. if (!try_module_get(cp->owner))
  2311. cp = NULL;
  2312. }
  2313. spin_unlock(&compressor_list_lock);
  2314. return cp;
  2315. }
  2316. /*
  2317. * Miscelleneous stuff.
  2318. */
  2319. static void
  2320. ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
  2321. {
  2322. struct slcompress *vj = ppp->vj;
  2323. memset(st, 0, sizeof(*st));
  2324. st->p.ppp_ipackets = ppp->stats64.rx_packets;
  2325. st->p.ppp_ierrors = ppp->dev->stats.rx_errors;
  2326. st->p.ppp_ibytes = ppp->stats64.rx_bytes;
  2327. st->p.ppp_opackets = ppp->stats64.tx_packets;
  2328. st->p.ppp_oerrors = ppp->dev->stats.tx_errors;
  2329. st->p.ppp_obytes = ppp->stats64.tx_bytes;
  2330. if (!vj)
  2331. return;
  2332. st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
  2333. st->vj.vjs_compressed = vj->sls_o_compressed;
  2334. st->vj.vjs_searches = vj->sls_o_searches;
  2335. st->vj.vjs_misses = vj->sls_o_misses;
  2336. st->vj.vjs_errorin = vj->sls_i_error;
  2337. st->vj.vjs_tossed = vj->sls_i_tossed;
  2338. st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
  2339. st->vj.vjs_compressedin = vj->sls_i_compressed;
  2340. }
  2341. /*
  2342. * Stuff for handling the lists of ppp units and channels
  2343. * and for initialization.
  2344. */
  2345. /*
  2346. * Create a new ppp interface unit. Fails if it can't allocate memory
  2347. * or if there is already a unit with the requested number.
  2348. * unit == -1 means allocate a new number.
  2349. */
  2350. static struct ppp *
  2351. ppp_create_interface(struct net *net, int unit, int *retp)
  2352. {
  2353. struct ppp *ppp;
  2354. struct ppp_net *pn;
  2355. struct net_device *dev = NULL;
  2356. int ret = -ENOMEM;
  2357. int i;
  2358. dev = alloc_netdev(sizeof(struct ppp), "", ppp_setup);
  2359. if (!dev)
  2360. goto out1;
  2361. pn = ppp_pernet(net);
  2362. ppp = netdev_priv(dev);
  2363. ppp->dev = dev;
  2364. ppp->mru = PPP_MRU;
  2365. init_ppp_file(&ppp->file, INTERFACE);
  2366. ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */
  2367. for (i = 0; i < NUM_NP; ++i)
  2368. ppp->npmode[i] = NPMODE_PASS;
  2369. INIT_LIST_HEAD(&ppp->channels);
  2370. spin_lock_init(&ppp->rlock);
  2371. spin_lock_init(&ppp->wlock);
  2372. #ifdef CONFIG_PPP_MULTILINK
  2373. ppp->minseq = -1;
  2374. skb_queue_head_init(&ppp->mrq);
  2375. #endif /* CONFIG_PPP_MULTILINK */
  2376. /*
  2377. * drum roll: don't forget to set
  2378. * the net device is belong to
  2379. */
  2380. dev_net_set(dev, net);
  2381. mutex_lock(&pn->all_ppp_mutex);
  2382. if (unit < 0) {
  2383. unit = unit_get(&pn->units_idr, ppp);
  2384. if (unit < 0) {
  2385. ret = unit;
  2386. goto out2;
  2387. }
  2388. } else {
  2389. ret = -EEXIST;
  2390. if (unit_find(&pn->units_idr, unit))
  2391. goto out2; /* unit already exists */
  2392. /*
  2393. * if caller need a specified unit number
  2394. * lets try to satisfy him, otherwise --
  2395. * he should better ask us for new unit number
  2396. *
  2397. * NOTE: yes I know that returning EEXIST it's not
  2398. * fair but at least pppd will ask us to allocate
  2399. * new unit in this case so user is happy :)
  2400. */
  2401. unit = unit_set(&pn->units_idr, ppp, unit);
  2402. if (unit < 0)
  2403. goto out2;
  2404. }
  2405. /* Initialize the new ppp unit */
  2406. ppp->file.index = unit;
  2407. sprintf(dev->name, "ppp%d", unit);
  2408. ret = register_netdev(dev);
  2409. if (ret != 0) {
  2410. unit_put(&pn->units_idr, unit);
  2411. netdev_err(ppp->dev, "PPP: couldn't register device %s (%d)\n",
  2412. dev->name, ret);
  2413. goto out2;
  2414. }
  2415. ppp->ppp_net = net;
  2416. atomic_inc(&ppp_unit_count);
  2417. mutex_unlock(&pn->all_ppp_mutex);
  2418. *retp = 0;
  2419. return ppp;
  2420. out2:
  2421. mutex_unlock(&pn->all_ppp_mutex);
  2422. free_netdev(dev);
  2423. out1:
  2424. *retp = ret;
  2425. return NULL;
  2426. }
  2427. /*
  2428. * Initialize a ppp_file structure.
  2429. */
  2430. static void
  2431. init_ppp_file(struct ppp_file *pf, int kind)
  2432. {
  2433. pf->kind = kind;
  2434. skb_queue_head_init(&pf->xq);
  2435. skb_queue_head_init(&pf->rq);
  2436. atomic_set(&pf->refcnt, 1);
  2437. init_waitqueue_head(&pf->rwait);
  2438. }
  2439. /*
  2440. * Take down a ppp interface unit - called when the owning file
  2441. * (the one that created the unit) is closed or detached.
  2442. */
  2443. static void ppp_shutdown_interface(struct ppp *ppp)
  2444. {
  2445. struct ppp_net *pn;
  2446. pn = ppp_pernet(ppp->ppp_net);
  2447. mutex_lock(&pn->all_ppp_mutex);
  2448. /* This will call dev_close() for us. */
  2449. ppp_lock(ppp);
  2450. if (!ppp->closing) {
  2451. ppp->closing = 1;
  2452. ppp_unlock(ppp);
  2453. unregister_netdev(ppp->dev);
  2454. unit_put(&pn->units_idr, ppp->file.index);
  2455. } else
  2456. ppp_unlock(ppp);
  2457. ppp->file.dead = 1;
  2458. ppp->owner = NULL;
  2459. wake_up_interruptible(&ppp->file.rwait);
  2460. mutex_unlock(&pn->all_ppp_mutex);
  2461. }
  2462. /*
  2463. * Free the memory used by a ppp unit. This is only called once
  2464. * there are no channels connected to the unit and no file structs
  2465. * that reference the unit.
  2466. */
  2467. static void ppp_destroy_interface(struct ppp *ppp)
  2468. {
  2469. atomic_dec(&ppp_unit_count);
  2470. if (!ppp->file.dead || ppp->n_channels) {
  2471. /* "can't happen" */
  2472. netdev_err(ppp->dev, "ppp: destroying ppp struct %p "
  2473. "but dead=%d n_channels=%d !\n",
  2474. ppp, ppp->file.dead, ppp->n_channels);
  2475. return;
  2476. }
  2477. ppp_ccp_closed(ppp);
  2478. if (ppp->vj) {
  2479. slhc_free(ppp->vj);
  2480. ppp->vj = NULL;
  2481. }
  2482. skb_queue_purge(&ppp->file.xq);
  2483. skb_queue_purge(&ppp->file.rq);
  2484. #ifdef CONFIG_PPP_MULTILINK
  2485. skb_queue_purge(&ppp->mrq);
  2486. #endif /* CONFIG_PPP_MULTILINK */
  2487. #ifdef CONFIG_PPP_FILTER
  2488. kfree(ppp->pass_filter);
  2489. ppp->pass_filter = NULL;
  2490. kfree(ppp->active_filter);
  2491. ppp->active_filter = NULL;
  2492. #endif /* CONFIG_PPP_FILTER */
  2493. kfree_skb(ppp->xmit_pending);
  2494. free_netdev(ppp->dev);
  2495. }
  2496. /*
  2497. * Locate an existing ppp unit.
  2498. * The caller should have locked the all_ppp_mutex.
  2499. */
  2500. static struct ppp *
  2501. ppp_find_unit(struct ppp_net *pn, int unit)
  2502. {
  2503. return unit_find(&pn->units_idr, unit);
  2504. }
  2505. /*
  2506. * Locate an existing ppp channel.
  2507. * The caller should have locked the all_channels_lock.
  2508. * First we look in the new_channels list, then in the
  2509. * all_channels list. If found in the new_channels list,
  2510. * we move it to the all_channels list. This is for speed
  2511. * when we have a lot of channels in use.
  2512. */
  2513. static struct channel *
  2514. ppp_find_channel(struct ppp_net *pn, int unit)
  2515. {
  2516. struct channel *pch;
  2517. list_for_each_entry(pch, &pn->new_channels, list) {
  2518. if (pch->file.index == unit) {
  2519. list_move(&pch->list, &pn->all_channels);
  2520. return pch;
  2521. }
  2522. }
  2523. list_for_each_entry(pch, &pn->all_channels, list) {
  2524. if (pch->file.index == unit)
  2525. return pch;
  2526. }
  2527. return NULL;
  2528. }
  2529. /*
  2530. * Connect a PPP channel to a PPP interface unit.
  2531. */
  2532. static int
  2533. ppp_connect_channel(struct channel *pch, int unit)
  2534. {
  2535. struct ppp *ppp;
  2536. struct ppp_net *pn;
  2537. int ret = -ENXIO;
  2538. int hdrlen;
  2539. pn = ppp_pernet(pch->chan_net);
  2540. mutex_lock(&pn->all_ppp_mutex);
  2541. ppp = ppp_find_unit(pn, unit);
  2542. if (!ppp)
  2543. goto out;
  2544. write_lock_bh(&pch->upl);
  2545. ret = -EINVAL;
  2546. if (pch->ppp)
  2547. goto outl;
  2548. ppp_lock(ppp);
  2549. if (pch->file.hdrlen > ppp->file.hdrlen)
  2550. ppp->file.hdrlen = pch->file.hdrlen;
  2551. hdrlen = pch->file.hdrlen + 2; /* for protocol bytes */
  2552. if (hdrlen > ppp->dev->hard_header_len)
  2553. ppp->dev->hard_header_len = hdrlen;
  2554. list_add_tail(&pch->clist, &ppp->channels);
  2555. ++ppp->n_channels;
  2556. pch->ppp = ppp;
  2557. atomic_inc(&ppp->file.refcnt);
  2558. ppp_unlock(ppp);
  2559. ret = 0;
  2560. outl:
  2561. write_unlock_bh(&pch->upl);
  2562. out:
  2563. mutex_unlock(&pn->all_ppp_mutex);
  2564. return ret;
  2565. }
  2566. /*
  2567. * Disconnect a channel from its ppp unit.
  2568. */
  2569. static int
  2570. ppp_disconnect_channel(struct channel *pch)
  2571. {
  2572. struct ppp *ppp;
  2573. int err = -EINVAL;
  2574. write_lock_bh(&pch->upl);
  2575. ppp = pch->ppp;
  2576. pch->ppp = NULL;
  2577. write_unlock_bh(&pch->upl);
  2578. if (ppp) {
  2579. /* remove it from the ppp unit's list */
  2580. ppp_lock(ppp);
  2581. list_del(&pch->clist);
  2582. if (--ppp->n_channels == 0)
  2583. wake_up_interruptible(&ppp->file.rwait);
  2584. ppp_unlock(ppp);
  2585. if (atomic_dec_and_test(&ppp->file.refcnt))
  2586. ppp_destroy_interface(ppp);
  2587. err = 0;
  2588. }
  2589. return err;
  2590. }
  2591. /*
  2592. * Free up the resources used by a ppp channel.
  2593. */
  2594. static void ppp_destroy_channel(struct channel *pch)
  2595. {
  2596. atomic_dec(&channel_count);
  2597. if (!pch->file.dead) {
  2598. /* "can't happen" */
  2599. pr_err("ppp: destroying undead channel %p !\n", pch);
  2600. return;
  2601. }
  2602. skb_queue_purge(&pch->file.xq);
  2603. skb_queue_purge(&pch->file.rq);
  2604. kfree(pch);
  2605. }
  2606. static void __exit ppp_cleanup(void)
  2607. {
  2608. /* should never happen */
  2609. if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count))
  2610. pr_err("PPP: removing module but units remain!\n");
  2611. unregister_chrdev(PPP_MAJOR, "ppp");
  2612. device_destroy(ppp_class, MKDEV(PPP_MAJOR, 0));
  2613. class_destroy(ppp_class);
  2614. unregister_pernet_device(&ppp_net_ops);
  2615. }
  2616. /*
  2617. * Units handling. Caller must protect concurrent access
  2618. * by holding all_ppp_mutex
  2619. */
  2620. static int __unit_alloc(struct idr *p, void *ptr, int n)
  2621. {
  2622. int unit, err;
  2623. again:
  2624. if (!idr_pre_get(p, GFP_KERNEL)) {
  2625. pr_err("PPP: No free memory for idr\n");
  2626. return -ENOMEM;
  2627. }
  2628. err = idr_get_new_above(p, ptr, n, &unit);
  2629. if (err < 0) {
  2630. if (err == -EAGAIN)
  2631. goto again;
  2632. return err;
  2633. }
  2634. return unit;
  2635. }
  2636. /* associate pointer with specified number */
  2637. static int unit_set(struct idr *p, void *ptr, int n)
  2638. {
  2639. int unit;
  2640. unit = __unit_alloc(p, ptr, n);
  2641. if (unit < 0)
  2642. return unit;
  2643. else if (unit != n) {
  2644. idr_remove(p, unit);
  2645. return -EINVAL;
  2646. }
  2647. return unit;
  2648. }
  2649. /* get new free unit number and associate pointer with it */
  2650. static int unit_get(struct idr *p, void *ptr)
  2651. {
  2652. return __unit_alloc(p, ptr, 0);
  2653. }
  2654. /* put unit number back to a pool */
  2655. static void unit_put(struct idr *p, int n)
  2656. {
  2657. idr_remove(p, n);
  2658. }
  2659. /* get pointer associated with the number */
  2660. static void *unit_find(struct idr *p, int n)
  2661. {
  2662. return idr_find(p, n);
  2663. }
  2664. /* Module/initialization stuff */
  2665. module_init(ppp_init);
  2666. module_exit(ppp_cleanup);
  2667. EXPORT_SYMBOL(ppp_register_net_channel);
  2668. EXPORT_SYMBOL(ppp_register_channel);
  2669. EXPORT_SYMBOL(ppp_unregister_channel);
  2670. EXPORT_SYMBOL(ppp_channel_index);
  2671. EXPORT_SYMBOL(ppp_unit_number);
  2672. EXPORT_SYMBOL(ppp_dev_name);
  2673. EXPORT_SYMBOL(ppp_input);
  2674. EXPORT_SYMBOL(ppp_input_error);
  2675. EXPORT_SYMBOL(ppp_output_wakeup);
  2676. EXPORT_SYMBOL(ppp_register_compressor);
  2677. EXPORT_SYMBOL(ppp_unregister_compressor);
  2678. MODULE_LICENSE("GPL");
  2679. MODULE_ALIAS_CHARDEV(PPP_MAJOR, 0);
  2680. MODULE_ALIAS("devname:ppp");