ppp_generic.c 71 KB

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