ppp_generic.c 69 KB

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