ppp_generic.c 68 KB

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