ppp_generic.c 66 KB

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