ppp_generic.c 71 KB

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