ppp_generic.c 66 KB

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