ppp_generic.c 67 KB

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