ppp_generic.c 70 KB

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