ppp_generic.c 67 KB

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