ppp_generic.c 68 KB

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