af_packet.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * PACKET - implements raw packet sockets.
  7. *
  8. * Authors: Ross Biro
  9. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  10. * Alan Cox, <gw4pts@gw4pts.ampr.org>
  11. *
  12. * Fixes:
  13. * Alan Cox : verify_area() now used correctly
  14. * Alan Cox : new skbuff lists, look ma no backlogs!
  15. * Alan Cox : tidied skbuff lists.
  16. * Alan Cox : Now uses generic datagram routines I
  17. * added. Also fixed the peek/read crash
  18. * from all old Linux datagram code.
  19. * Alan Cox : Uses the improved datagram code.
  20. * Alan Cox : Added NULL's for socket options.
  21. * Alan Cox : Re-commented the code.
  22. * Alan Cox : Use new kernel side addressing
  23. * Rob Janssen : Correct MTU usage.
  24. * Dave Platt : Counter leaks caused by incorrect
  25. * interrupt locking and some slightly
  26. * dubious gcc output. Can you read
  27. * compiler: it said _VOLATILE_
  28. * Richard Kooijman : Timestamp fixes.
  29. * Alan Cox : New buffers. Use sk->mac.raw.
  30. * Alan Cox : sendmsg/recvmsg support.
  31. * Alan Cox : Protocol setting support
  32. * Alexey Kuznetsov : Untied from IPv4 stack.
  33. * Cyrus Durgin : Fixed kerneld for kmod.
  34. * Michal Ostrowski : Module initialization cleanup.
  35. * Ulises Alonso : Frame number limit removal and
  36. * packet_set_ring memory leak.
  37. * Eric Biederman : Allow for > 8 byte hardware addresses.
  38. * The convention is that longer addresses
  39. * will simply extend the hardware address
  40. * byte arrays at the end of sockaddr_ll
  41. * and packet_mreq.
  42. * Johann Baudy : Added TX RING.
  43. *
  44. * This program is free software; you can redistribute it and/or
  45. * modify it under the terms of the GNU General Public License
  46. * as published by the Free Software Foundation; either version
  47. * 2 of the License, or (at your option) any later version.
  48. *
  49. */
  50. #include <linux/types.h>
  51. #include <linux/mm.h>
  52. #include <linux/capability.h>
  53. #include <linux/fcntl.h>
  54. #include <linux/socket.h>
  55. #include <linux/in.h>
  56. #include <linux/inet.h>
  57. #include <linux/netdevice.h>
  58. #include <linux/if_packet.h>
  59. #include <linux/wireless.h>
  60. #include <linux/kernel.h>
  61. #include <linux/kmod.h>
  62. #include <linux/slab.h>
  63. #include <linux/vmalloc.h>
  64. #include <net/net_namespace.h>
  65. #include <net/ip.h>
  66. #include <net/protocol.h>
  67. #include <linux/skbuff.h>
  68. #include <net/sock.h>
  69. #include <linux/errno.h>
  70. #include <linux/timer.h>
  71. #include <asm/system.h>
  72. #include <asm/uaccess.h>
  73. #include <asm/ioctls.h>
  74. #include <asm/page.h>
  75. #include <asm/cacheflush.h>
  76. #include <asm/io.h>
  77. #include <linux/proc_fs.h>
  78. #include <linux/seq_file.h>
  79. #include <linux/poll.h>
  80. #include <linux/module.h>
  81. #include <linux/init.h>
  82. #include <linux/mutex.h>
  83. #include <linux/if_vlan.h>
  84. #include <linux/virtio_net.h>
  85. #include <linux/errqueue.h>
  86. #include <linux/net_tstamp.h>
  87. #ifdef CONFIG_INET
  88. #include <net/inet_common.h>
  89. #endif
  90. /*
  91. Assumptions:
  92. - if device has no dev->hard_header routine, it adds and removes ll header
  93. inside itself. In this case ll header is invisible outside of device,
  94. but higher levels still should reserve dev->hard_header_len.
  95. Some devices are enough clever to reallocate skb, when header
  96. will not fit to reserved space (tunnel), another ones are silly
  97. (PPP).
  98. - packet socket receives packets with pulled ll header,
  99. so that SOCK_RAW should push it back.
  100. On receive:
  101. -----------
  102. Incoming, dev->hard_header!=NULL
  103. mac_header -> ll header
  104. data -> data
  105. Outgoing, dev->hard_header!=NULL
  106. mac_header -> ll header
  107. data -> ll header
  108. Incoming, dev->hard_header==NULL
  109. mac_header -> UNKNOWN position. It is very likely, that it points to ll
  110. header. PPP makes it, that is wrong, because introduce
  111. assymetry between rx and tx paths.
  112. data -> data
  113. Outgoing, dev->hard_header==NULL
  114. mac_header -> data. ll header is still not built!
  115. data -> data
  116. Resume
  117. If dev->hard_header==NULL we are unlikely to restore sensible ll header.
  118. On transmit:
  119. ------------
  120. dev->hard_header != NULL
  121. mac_header -> ll header
  122. data -> ll header
  123. dev->hard_header == NULL (ll header is added by device, we cannot control it)
  124. mac_header -> data
  125. data -> data
  126. We should set nh.raw on output to correct posistion,
  127. packet classifier depends on it.
  128. */
  129. /* Private packet socket structures. */
  130. struct packet_mclist {
  131. struct packet_mclist *next;
  132. int ifindex;
  133. int count;
  134. unsigned short type;
  135. unsigned short alen;
  136. unsigned char addr[MAX_ADDR_LEN];
  137. };
  138. /* identical to struct packet_mreq except it has
  139. * a longer address field.
  140. */
  141. struct packet_mreq_max {
  142. int mr_ifindex;
  143. unsigned short mr_type;
  144. unsigned short mr_alen;
  145. unsigned char mr_address[MAX_ADDR_LEN];
  146. };
  147. static int packet_set_ring(struct sock *sk, struct tpacket_req *req,
  148. int closing, int tx_ring);
  149. struct pgv {
  150. char *buffer;
  151. };
  152. struct packet_ring_buffer {
  153. struct pgv *pg_vec;
  154. unsigned int head;
  155. unsigned int frames_per_block;
  156. unsigned int frame_size;
  157. unsigned int frame_max;
  158. unsigned int pg_vec_order;
  159. unsigned int pg_vec_pages;
  160. unsigned int pg_vec_len;
  161. atomic_t pending;
  162. };
  163. struct packet_sock;
  164. static int tpacket_snd(struct packet_sock *po, struct msghdr *msg);
  165. static void packet_flush_mclist(struct sock *sk);
  166. struct packet_fanout;
  167. struct packet_sock {
  168. /* struct sock has to be the first member of packet_sock */
  169. struct sock sk;
  170. struct packet_fanout *fanout;
  171. struct tpacket_stats stats;
  172. struct packet_ring_buffer rx_ring;
  173. struct packet_ring_buffer tx_ring;
  174. int copy_thresh;
  175. spinlock_t bind_lock;
  176. struct mutex pg_vec_lock;
  177. unsigned int running:1, /* prot_hook is attached*/
  178. auxdata:1,
  179. origdev:1,
  180. has_vnet_hdr:1;
  181. int ifindex; /* bound device */
  182. __be16 num;
  183. struct packet_mclist *mclist;
  184. atomic_t mapped;
  185. enum tpacket_versions tp_version;
  186. unsigned int tp_hdrlen;
  187. unsigned int tp_reserve;
  188. unsigned int tp_loss:1;
  189. unsigned int tp_tstamp;
  190. struct packet_type prot_hook ____cacheline_aligned_in_smp;
  191. };
  192. #define PACKET_FANOUT_MAX 256
  193. struct packet_fanout {
  194. #ifdef CONFIG_NET_NS
  195. struct net *net;
  196. #endif
  197. unsigned int num_members;
  198. u16 id;
  199. u8 type;
  200. u8 defrag;
  201. atomic_t rr_cur;
  202. struct list_head list;
  203. struct sock *arr[PACKET_FANOUT_MAX];
  204. spinlock_t lock;
  205. atomic_t sk_ref;
  206. struct packet_type prot_hook ____cacheline_aligned_in_smp;
  207. };
  208. struct packet_skb_cb {
  209. unsigned int origlen;
  210. union {
  211. struct sockaddr_pkt pkt;
  212. struct sockaddr_ll ll;
  213. } sa;
  214. };
  215. #define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
  216. static inline struct packet_sock *pkt_sk(struct sock *sk)
  217. {
  218. return (struct packet_sock *)sk;
  219. }
  220. static void __fanout_unlink(struct sock *sk, struct packet_sock *po);
  221. static void __fanout_link(struct sock *sk, struct packet_sock *po);
  222. /* register_prot_hook must be invoked with the po->bind_lock held,
  223. * or from a context in which asynchronous accesses to the packet
  224. * socket is not possible (packet_create()).
  225. */
  226. static void register_prot_hook(struct sock *sk)
  227. {
  228. struct packet_sock *po = pkt_sk(sk);
  229. if (!po->running) {
  230. if (po->fanout)
  231. __fanout_link(sk, po);
  232. else
  233. dev_add_pack(&po->prot_hook);
  234. sock_hold(sk);
  235. po->running = 1;
  236. }
  237. }
  238. /* {,__}unregister_prot_hook() must be invoked with the po->bind_lock
  239. * held. If the sync parameter is true, we will temporarily drop
  240. * the po->bind_lock and do a synchronize_net to make sure no
  241. * asynchronous packet processing paths still refer to the elements
  242. * of po->prot_hook. If the sync parameter is false, it is the
  243. * callers responsibility to take care of this.
  244. */
  245. static void __unregister_prot_hook(struct sock *sk, bool sync)
  246. {
  247. struct packet_sock *po = pkt_sk(sk);
  248. po->running = 0;
  249. if (po->fanout)
  250. __fanout_unlink(sk, po);
  251. else
  252. __dev_remove_pack(&po->prot_hook);
  253. __sock_put(sk);
  254. if (sync) {
  255. spin_unlock(&po->bind_lock);
  256. synchronize_net();
  257. spin_lock(&po->bind_lock);
  258. }
  259. }
  260. static void unregister_prot_hook(struct sock *sk, bool sync)
  261. {
  262. struct packet_sock *po = pkt_sk(sk);
  263. if (po->running)
  264. __unregister_prot_hook(sk, sync);
  265. }
  266. static inline __pure struct page *pgv_to_page(void *addr)
  267. {
  268. if (is_vmalloc_addr(addr))
  269. return vmalloc_to_page(addr);
  270. return virt_to_page(addr);
  271. }
  272. static void __packet_set_status(struct packet_sock *po, void *frame, int status)
  273. {
  274. union {
  275. struct tpacket_hdr *h1;
  276. struct tpacket2_hdr *h2;
  277. void *raw;
  278. } h;
  279. h.raw = frame;
  280. switch (po->tp_version) {
  281. case TPACKET_V1:
  282. h.h1->tp_status = status;
  283. flush_dcache_page(pgv_to_page(&h.h1->tp_status));
  284. break;
  285. case TPACKET_V2:
  286. h.h2->tp_status = status;
  287. flush_dcache_page(pgv_to_page(&h.h2->tp_status));
  288. break;
  289. default:
  290. pr_err("TPACKET version not supported\n");
  291. BUG();
  292. }
  293. smp_wmb();
  294. }
  295. static int __packet_get_status(struct packet_sock *po, void *frame)
  296. {
  297. union {
  298. struct tpacket_hdr *h1;
  299. struct tpacket2_hdr *h2;
  300. void *raw;
  301. } h;
  302. smp_rmb();
  303. h.raw = frame;
  304. switch (po->tp_version) {
  305. case TPACKET_V1:
  306. flush_dcache_page(pgv_to_page(&h.h1->tp_status));
  307. return h.h1->tp_status;
  308. case TPACKET_V2:
  309. flush_dcache_page(pgv_to_page(&h.h2->tp_status));
  310. return h.h2->tp_status;
  311. default:
  312. pr_err("TPACKET version not supported\n");
  313. BUG();
  314. return 0;
  315. }
  316. }
  317. static void *packet_lookup_frame(struct packet_sock *po,
  318. struct packet_ring_buffer *rb,
  319. unsigned int position,
  320. int status)
  321. {
  322. unsigned int pg_vec_pos, frame_offset;
  323. union {
  324. struct tpacket_hdr *h1;
  325. struct tpacket2_hdr *h2;
  326. void *raw;
  327. } h;
  328. pg_vec_pos = position / rb->frames_per_block;
  329. frame_offset = position % rb->frames_per_block;
  330. h.raw = rb->pg_vec[pg_vec_pos].buffer +
  331. (frame_offset * rb->frame_size);
  332. if (status != __packet_get_status(po, h.raw))
  333. return NULL;
  334. return h.raw;
  335. }
  336. static inline void *packet_current_frame(struct packet_sock *po,
  337. struct packet_ring_buffer *rb,
  338. int status)
  339. {
  340. return packet_lookup_frame(po, rb, rb->head, status);
  341. }
  342. static inline void *packet_previous_frame(struct packet_sock *po,
  343. struct packet_ring_buffer *rb,
  344. int status)
  345. {
  346. unsigned int previous = rb->head ? rb->head - 1 : rb->frame_max;
  347. return packet_lookup_frame(po, rb, previous, status);
  348. }
  349. static inline void packet_increment_head(struct packet_ring_buffer *buff)
  350. {
  351. buff->head = buff->head != buff->frame_max ? buff->head+1 : 0;
  352. }
  353. static void packet_sock_destruct(struct sock *sk)
  354. {
  355. skb_queue_purge(&sk->sk_error_queue);
  356. WARN_ON(atomic_read(&sk->sk_rmem_alloc));
  357. WARN_ON(atomic_read(&sk->sk_wmem_alloc));
  358. if (!sock_flag(sk, SOCK_DEAD)) {
  359. pr_err("Attempt to release alive packet socket: %p\n", sk);
  360. return;
  361. }
  362. sk_refcnt_debug_dec(sk);
  363. }
  364. static int fanout_rr_next(struct packet_fanout *f, unsigned int num)
  365. {
  366. int x = atomic_read(&f->rr_cur) + 1;
  367. if (x >= num)
  368. x = 0;
  369. return x;
  370. }
  371. static struct sock *fanout_demux_hash(struct packet_fanout *f, struct sk_buff *skb, unsigned int num)
  372. {
  373. u32 idx, hash = skb->rxhash;
  374. idx = ((u64)hash * num) >> 32;
  375. return f->arr[idx];
  376. }
  377. static struct sock *fanout_demux_lb(struct packet_fanout *f, struct sk_buff *skb, unsigned int num)
  378. {
  379. int cur, old;
  380. cur = atomic_read(&f->rr_cur);
  381. while ((old = atomic_cmpxchg(&f->rr_cur, cur,
  382. fanout_rr_next(f, num))) != cur)
  383. cur = old;
  384. return f->arr[cur];
  385. }
  386. static struct sock *fanout_demux_cpu(struct packet_fanout *f, struct sk_buff *skb, unsigned int num)
  387. {
  388. unsigned int cpu = smp_processor_id();
  389. return f->arr[cpu % num];
  390. }
  391. static struct sk_buff *fanout_check_defrag(struct sk_buff *skb)
  392. {
  393. #ifdef CONFIG_INET
  394. const struct iphdr *iph;
  395. u32 len;
  396. if (skb->protocol != htons(ETH_P_IP))
  397. return skb;
  398. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  399. return skb;
  400. iph = ip_hdr(skb);
  401. if (iph->ihl < 5 || iph->version != 4)
  402. return skb;
  403. if (!pskb_may_pull(skb, iph->ihl*4))
  404. return skb;
  405. iph = ip_hdr(skb);
  406. len = ntohs(iph->tot_len);
  407. if (skb->len < len || len < (iph->ihl * 4))
  408. return skb;
  409. if (ip_is_fragment(ip_hdr(skb))) {
  410. skb = skb_share_check(skb, GFP_ATOMIC);
  411. if (skb) {
  412. if (pskb_trim_rcsum(skb, len))
  413. return skb;
  414. memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
  415. if (ip_defrag(skb, IP_DEFRAG_AF_PACKET))
  416. return NULL;
  417. skb->rxhash = 0;
  418. }
  419. }
  420. #endif
  421. return skb;
  422. }
  423. static int packet_rcv_fanout(struct sk_buff *skb, struct net_device *dev,
  424. struct packet_type *pt, struct net_device *orig_dev)
  425. {
  426. struct packet_fanout *f = pt->af_packet_priv;
  427. unsigned int num = f->num_members;
  428. struct packet_sock *po;
  429. struct sock *sk;
  430. if (!net_eq(dev_net(dev), read_pnet(&f->net)) ||
  431. !num) {
  432. kfree_skb(skb);
  433. return 0;
  434. }
  435. switch (f->type) {
  436. case PACKET_FANOUT_HASH:
  437. default:
  438. if (f->defrag) {
  439. skb = fanout_check_defrag(skb);
  440. if (!skb)
  441. return 0;
  442. }
  443. skb_get_rxhash(skb);
  444. sk = fanout_demux_hash(f, skb, num);
  445. break;
  446. case PACKET_FANOUT_LB:
  447. sk = fanout_demux_lb(f, skb, num);
  448. break;
  449. case PACKET_FANOUT_CPU:
  450. sk = fanout_demux_cpu(f, skb, num);
  451. break;
  452. }
  453. po = pkt_sk(sk);
  454. return po->prot_hook.func(skb, dev, &po->prot_hook, orig_dev);
  455. }
  456. static DEFINE_MUTEX(fanout_mutex);
  457. static LIST_HEAD(fanout_list);
  458. static void __fanout_link(struct sock *sk, struct packet_sock *po)
  459. {
  460. struct packet_fanout *f = po->fanout;
  461. spin_lock(&f->lock);
  462. f->arr[f->num_members] = sk;
  463. smp_wmb();
  464. f->num_members++;
  465. spin_unlock(&f->lock);
  466. }
  467. static void __fanout_unlink(struct sock *sk, struct packet_sock *po)
  468. {
  469. struct packet_fanout *f = po->fanout;
  470. int i;
  471. spin_lock(&f->lock);
  472. for (i = 0; i < f->num_members; i++) {
  473. if (f->arr[i] == sk)
  474. break;
  475. }
  476. BUG_ON(i >= f->num_members);
  477. f->arr[i] = f->arr[f->num_members - 1];
  478. f->num_members--;
  479. spin_unlock(&f->lock);
  480. }
  481. static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
  482. {
  483. struct packet_sock *po = pkt_sk(sk);
  484. struct packet_fanout *f, *match;
  485. u8 type = type_flags & 0xff;
  486. u8 defrag = (type_flags & PACKET_FANOUT_FLAG_DEFRAG) ? 1 : 0;
  487. int err;
  488. switch (type) {
  489. case PACKET_FANOUT_HASH:
  490. case PACKET_FANOUT_LB:
  491. case PACKET_FANOUT_CPU:
  492. break;
  493. default:
  494. return -EINVAL;
  495. }
  496. if (!po->running)
  497. return -EINVAL;
  498. if (po->fanout)
  499. return -EALREADY;
  500. mutex_lock(&fanout_mutex);
  501. match = NULL;
  502. list_for_each_entry(f, &fanout_list, list) {
  503. if (f->id == id &&
  504. read_pnet(&f->net) == sock_net(sk)) {
  505. match = f;
  506. break;
  507. }
  508. }
  509. err = -EINVAL;
  510. if (match && match->defrag != defrag)
  511. goto out;
  512. if (!match) {
  513. err = -ENOMEM;
  514. match = kzalloc(sizeof(*match), GFP_KERNEL);
  515. if (!match)
  516. goto out;
  517. write_pnet(&match->net, sock_net(sk));
  518. match->id = id;
  519. match->type = type;
  520. match->defrag = defrag;
  521. atomic_set(&match->rr_cur, 0);
  522. INIT_LIST_HEAD(&match->list);
  523. spin_lock_init(&match->lock);
  524. atomic_set(&match->sk_ref, 0);
  525. match->prot_hook.type = po->prot_hook.type;
  526. match->prot_hook.dev = po->prot_hook.dev;
  527. match->prot_hook.func = packet_rcv_fanout;
  528. match->prot_hook.af_packet_priv = match;
  529. dev_add_pack(&match->prot_hook);
  530. list_add(&match->list, &fanout_list);
  531. }
  532. err = -EINVAL;
  533. if (match->type == type &&
  534. match->prot_hook.type == po->prot_hook.type &&
  535. match->prot_hook.dev == po->prot_hook.dev) {
  536. err = -ENOSPC;
  537. if (atomic_read(&match->sk_ref) < PACKET_FANOUT_MAX) {
  538. __dev_remove_pack(&po->prot_hook);
  539. po->fanout = match;
  540. atomic_inc(&match->sk_ref);
  541. __fanout_link(sk, po);
  542. err = 0;
  543. }
  544. }
  545. out:
  546. mutex_unlock(&fanout_mutex);
  547. return err;
  548. }
  549. static void fanout_release(struct sock *sk)
  550. {
  551. struct packet_sock *po = pkt_sk(sk);
  552. struct packet_fanout *f;
  553. f = po->fanout;
  554. if (!f)
  555. return;
  556. po->fanout = NULL;
  557. mutex_lock(&fanout_mutex);
  558. if (atomic_dec_and_test(&f->sk_ref)) {
  559. list_del(&f->list);
  560. dev_remove_pack(&f->prot_hook);
  561. kfree(f);
  562. }
  563. mutex_unlock(&fanout_mutex);
  564. }
  565. static const struct proto_ops packet_ops;
  566. static const struct proto_ops packet_ops_spkt;
  567. static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,
  568. struct packet_type *pt, struct net_device *orig_dev)
  569. {
  570. struct sock *sk;
  571. struct sockaddr_pkt *spkt;
  572. /*
  573. * When we registered the protocol we saved the socket in the data
  574. * field for just this event.
  575. */
  576. sk = pt->af_packet_priv;
  577. /*
  578. * Yank back the headers [hope the device set this
  579. * right or kerboom...]
  580. *
  581. * Incoming packets have ll header pulled,
  582. * push it back.
  583. *
  584. * For outgoing ones skb->data == skb_mac_header(skb)
  585. * so that this procedure is noop.
  586. */
  587. if (skb->pkt_type == PACKET_LOOPBACK)
  588. goto out;
  589. if (!net_eq(dev_net(dev), sock_net(sk)))
  590. goto out;
  591. skb = skb_share_check(skb, GFP_ATOMIC);
  592. if (skb == NULL)
  593. goto oom;
  594. /* drop any routing info */
  595. skb_dst_drop(skb);
  596. /* drop conntrack reference */
  597. nf_reset(skb);
  598. spkt = &PACKET_SKB_CB(skb)->sa.pkt;
  599. skb_push(skb, skb->data - skb_mac_header(skb));
  600. /*
  601. * The SOCK_PACKET socket receives _all_ frames.
  602. */
  603. spkt->spkt_family = dev->type;
  604. strlcpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
  605. spkt->spkt_protocol = skb->protocol;
  606. /*
  607. * Charge the memory to the socket. This is done specifically
  608. * to prevent sockets using all the memory up.
  609. */
  610. if (sock_queue_rcv_skb(sk, skb) == 0)
  611. return 0;
  612. out:
  613. kfree_skb(skb);
  614. oom:
  615. return 0;
  616. }
  617. /*
  618. * Output a raw packet to a device layer. This bypasses all the other
  619. * protocol layers and you must therefore supply it with a complete frame
  620. */
  621. static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock,
  622. struct msghdr *msg, size_t len)
  623. {
  624. struct sock *sk = sock->sk;
  625. struct sockaddr_pkt *saddr = (struct sockaddr_pkt *)msg->msg_name;
  626. struct sk_buff *skb = NULL;
  627. struct net_device *dev;
  628. __be16 proto = 0;
  629. int err;
  630. /*
  631. * Get and verify the address.
  632. */
  633. if (saddr) {
  634. if (msg->msg_namelen < sizeof(struct sockaddr))
  635. return -EINVAL;
  636. if (msg->msg_namelen == sizeof(struct sockaddr_pkt))
  637. proto = saddr->spkt_protocol;
  638. } else
  639. return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */
  640. /*
  641. * Find the device first to size check it
  642. */
  643. saddr->spkt_device[13] = 0;
  644. retry:
  645. rcu_read_lock();
  646. dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device);
  647. err = -ENODEV;
  648. if (dev == NULL)
  649. goto out_unlock;
  650. err = -ENETDOWN;
  651. if (!(dev->flags & IFF_UP))
  652. goto out_unlock;
  653. /*
  654. * You may not queue a frame bigger than the mtu. This is the lowest level
  655. * raw protocol and you must do your own fragmentation at this level.
  656. */
  657. err = -EMSGSIZE;
  658. if (len > dev->mtu + dev->hard_header_len + VLAN_HLEN)
  659. goto out_unlock;
  660. if (!skb) {
  661. size_t reserved = LL_RESERVED_SPACE(dev);
  662. unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0;
  663. rcu_read_unlock();
  664. skb = sock_wmalloc(sk, len + reserved, 0, GFP_KERNEL);
  665. if (skb == NULL)
  666. return -ENOBUFS;
  667. /* FIXME: Save some space for broken drivers that write a hard
  668. * header at transmission time by themselves. PPP is the notable
  669. * one here. This should really be fixed at the driver level.
  670. */
  671. skb_reserve(skb, reserved);
  672. skb_reset_network_header(skb);
  673. /* Try to align data part correctly */
  674. if (hhlen) {
  675. skb->data -= hhlen;
  676. skb->tail -= hhlen;
  677. if (len < hhlen)
  678. skb_reset_network_header(skb);
  679. }
  680. err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
  681. if (err)
  682. goto out_free;
  683. goto retry;
  684. }
  685. if (len > (dev->mtu + dev->hard_header_len)) {
  686. /* Earlier code assumed this would be a VLAN pkt,
  687. * double-check this now that we have the actual
  688. * packet in hand.
  689. */
  690. struct ethhdr *ehdr;
  691. skb_reset_mac_header(skb);
  692. ehdr = eth_hdr(skb);
  693. if (ehdr->h_proto != htons(ETH_P_8021Q)) {
  694. err = -EMSGSIZE;
  695. goto out_unlock;
  696. }
  697. }
  698. skb->protocol = proto;
  699. skb->dev = dev;
  700. skb->priority = sk->sk_priority;
  701. skb->mark = sk->sk_mark;
  702. err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
  703. if (err < 0)
  704. goto out_unlock;
  705. dev_queue_xmit(skb);
  706. rcu_read_unlock();
  707. return len;
  708. out_unlock:
  709. rcu_read_unlock();
  710. out_free:
  711. kfree_skb(skb);
  712. return err;
  713. }
  714. static inline unsigned int run_filter(const struct sk_buff *skb,
  715. const struct sock *sk,
  716. unsigned int res)
  717. {
  718. struct sk_filter *filter;
  719. rcu_read_lock();
  720. filter = rcu_dereference(sk->sk_filter);
  721. if (filter != NULL)
  722. res = SK_RUN_FILTER(filter, skb);
  723. rcu_read_unlock();
  724. return res;
  725. }
  726. /*
  727. * This function makes lazy skb cloning in hope that most of packets
  728. * are discarded by BPF.
  729. *
  730. * Note tricky part: we DO mangle shared skb! skb->data, skb->len
  731. * and skb->cb are mangled. It works because (and until) packets
  732. * falling here are owned by current CPU. Output packets are cloned
  733. * by dev_queue_xmit_nit(), input packets are processed by net_bh
  734. * sequencially, so that if we return skb to original state on exit,
  735. * we will not harm anyone.
  736. */
  737. static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
  738. struct packet_type *pt, struct net_device *orig_dev)
  739. {
  740. struct sock *sk;
  741. struct sockaddr_ll *sll;
  742. struct packet_sock *po;
  743. u8 *skb_head = skb->data;
  744. int skb_len = skb->len;
  745. unsigned int snaplen, res;
  746. if (skb->pkt_type == PACKET_LOOPBACK)
  747. goto drop;
  748. sk = pt->af_packet_priv;
  749. po = pkt_sk(sk);
  750. if (!net_eq(dev_net(dev), sock_net(sk)))
  751. goto drop;
  752. skb->dev = dev;
  753. if (dev->header_ops) {
  754. /* The device has an explicit notion of ll header,
  755. * exported to higher levels.
  756. *
  757. * Otherwise, the device hides details of its frame
  758. * structure, so that corresponding packet head is
  759. * never delivered to user.
  760. */
  761. if (sk->sk_type != SOCK_DGRAM)
  762. skb_push(skb, skb->data - skb_mac_header(skb));
  763. else if (skb->pkt_type == PACKET_OUTGOING) {
  764. /* Special case: outgoing packets have ll header at head */
  765. skb_pull(skb, skb_network_offset(skb));
  766. }
  767. }
  768. snaplen = skb->len;
  769. res = run_filter(skb, sk, snaplen);
  770. if (!res)
  771. goto drop_n_restore;
  772. if (snaplen > res)
  773. snaplen = res;
  774. if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
  775. (unsigned)sk->sk_rcvbuf)
  776. goto drop_n_acct;
  777. if (skb_shared(skb)) {
  778. struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
  779. if (nskb == NULL)
  780. goto drop_n_acct;
  781. if (skb_head != skb->data) {
  782. skb->data = skb_head;
  783. skb->len = skb_len;
  784. }
  785. kfree_skb(skb);
  786. skb = nskb;
  787. }
  788. BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8 >
  789. sizeof(skb->cb));
  790. sll = &PACKET_SKB_CB(skb)->sa.ll;
  791. sll->sll_family = AF_PACKET;
  792. sll->sll_hatype = dev->type;
  793. sll->sll_protocol = skb->protocol;
  794. sll->sll_pkttype = skb->pkt_type;
  795. if (unlikely(po->origdev))
  796. sll->sll_ifindex = orig_dev->ifindex;
  797. else
  798. sll->sll_ifindex = dev->ifindex;
  799. sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
  800. PACKET_SKB_CB(skb)->origlen = skb->len;
  801. if (pskb_trim(skb, snaplen))
  802. goto drop_n_acct;
  803. skb_set_owner_r(skb, sk);
  804. skb->dev = NULL;
  805. skb_dst_drop(skb);
  806. /* drop conntrack reference */
  807. nf_reset(skb);
  808. spin_lock(&sk->sk_receive_queue.lock);
  809. po->stats.tp_packets++;
  810. skb->dropcount = atomic_read(&sk->sk_drops);
  811. __skb_queue_tail(&sk->sk_receive_queue, skb);
  812. spin_unlock(&sk->sk_receive_queue.lock);
  813. sk->sk_data_ready(sk, skb->len);
  814. return 0;
  815. drop_n_acct:
  816. po->stats.tp_drops = atomic_inc_return(&sk->sk_drops);
  817. drop_n_restore:
  818. if (skb_head != skb->data && skb_shared(skb)) {
  819. skb->data = skb_head;
  820. skb->len = skb_len;
  821. }
  822. drop:
  823. consume_skb(skb);
  824. return 0;
  825. }
  826. static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
  827. struct packet_type *pt, struct net_device *orig_dev)
  828. {
  829. struct sock *sk;
  830. struct packet_sock *po;
  831. struct sockaddr_ll *sll;
  832. union {
  833. struct tpacket_hdr *h1;
  834. struct tpacket2_hdr *h2;
  835. void *raw;
  836. } h;
  837. u8 *skb_head = skb->data;
  838. int skb_len = skb->len;
  839. unsigned int snaplen, res;
  840. unsigned long status = TP_STATUS_LOSING|TP_STATUS_USER;
  841. unsigned short macoff, netoff, hdrlen;
  842. struct sk_buff *copy_skb = NULL;
  843. struct timeval tv;
  844. struct timespec ts;
  845. struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
  846. if (skb->pkt_type == PACKET_LOOPBACK)
  847. goto drop;
  848. sk = pt->af_packet_priv;
  849. po = pkt_sk(sk);
  850. if (!net_eq(dev_net(dev), sock_net(sk)))
  851. goto drop;
  852. if (dev->header_ops) {
  853. if (sk->sk_type != SOCK_DGRAM)
  854. skb_push(skb, skb->data - skb_mac_header(skb));
  855. else if (skb->pkt_type == PACKET_OUTGOING) {
  856. /* Special case: outgoing packets have ll header at head */
  857. skb_pull(skb, skb_network_offset(skb));
  858. }
  859. }
  860. if (skb->ip_summed == CHECKSUM_PARTIAL)
  861. status |= TP_STATUS_CSUMNOTREADY;
  862. snaplen = skb->len;
  863. res = run_filter(skb, sk, snaplen);
  864. if (!res)
  865. goto drop_n_restore;
  866. if (snaplen > res)
  867. snaplen = res;
  868. if (sk->sk_type == SOCK_DGRAM) {
  869. macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 +
  870. po->tp_reserve;
  871. } else {
  872. unsigned maclen = skb_network_offset(skb);
  873. netoff = TPACKET_ALIGN(po->tp_hdrlen +
  874. (maclen < 16 ? 16 : maclen)) +
  875. po->tp_reserve;
  876. macoff = netoff - maclen;
  877. }
  878. if (macoff + snaplen > po->rx_ring.frame_size) {
  879. if (po->copy_thresh &&
  880. atomic_read(&sk->sk_rmem_alloc) + skb->truesize <
  881. (unsigned)sk->sk_rcvbuf) {
  882. if (skb_shared(skb)) {
  883. copy_skb = skb_clone(skb, GFP_ATOMIC);
  884. } else {
  885. copy_skb = skb_get(skb);
  886. skb_head = skb->data;
  887. }
  888. if (copy_skb)
  889. skb_set_owner_r(copy_skb, sk);
  890. }
  891. snaplen = po->rx_ring.frame_size - macoff;
  892. if ((int)snaplen < 0)
  893. snaplen = 0;
  894. }
  895. spin_lock(&sk->sk_receive_queue.lock);
  896. h.raw = packet_current_frame(po, &po->rx_ring, TP_STATUS_KERNEL);
  897. if (!h.raw)
  898. goto ring_is_full;
  899. packet_increment_head(&po->rx_ring);
  900. po->stats.tp_packets++;
  901. if (copy_skb) {
  902. status |= TP_STATUS_COPY;
  903. __skb_queue_tail(&sk->sk_receive_queue, copy_skb);
  904. }
  905. if (!po->stats.tp_drops)
  906. status &= ~TP_STATUS_LOSING;
  907. spin_unlock(&sk->sk_receive_queue.lock);
  908. skb_copy_bits(skb, 0, h.raw + macoff, snaplen);
  909. switch (po->tp_version) {
  910. case TPACKET_V1:
  911. h.h1->tp_len = skb->len;
  912. h.h1->tp_snaplen = snaplen;
  913. h.h1->tp_mac = macoff;
  914. h.h1->tp_net = netoff;
  915. if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
  916. && shhwtstamps->syststamp.tv64)
  917. tv = ktime_to_timeval(shhwtstamps->syststamp);
  918. else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
  919. && shhwtstamps->hwtstamp.tv64)
  920. tv = ktime_to_timeval(shhwtstamps->hwtstamp);
  921. else if (skb->tstamp.tv64)
  922. tv = ktime_to_timeval(skb->tstamp);
  923. else
  924. do_gettimeofday(&tv);
  925. h.h1->tp_sec = tv.tv_sec;
  926. h.h1->tp_usec = tv.tv_usec;
  927. hdrlen = sizeof(*h.h1);
  928. break;
  929. case TPACKET_V2:
  930. h.h2->tp_len = skb->len;
  931. h.h2->tp_snaplen = snaplen;
  932. h.h2->tp_mac = macoff;
  933. h.h2->tp_net = netoff;
  934. if ((po->tp_tstamp & SOF_TIMESTAMPING_SYS_HARDWARE)
  935. && shhwtstamps->syststamp.tv64)
  936. ts = ktime_to_timespec(shhwtstamps->syststamp);
  937. else if ((po->tp_tstamp & SOF_TIMESTAMPING_RAW_HARDWARE)
  938. && shhwtstamps->hwtstamp.tv64)
  939. ts = ktime_to_timespec(shhwtstamps->hwtstamp);
  940. else if (skb->tstamp.tv64)
  941. ts = ktime_to_timespec(skb->tstamp);
  942. else
  943. getnstimeofday(&ts);
  944. h.h2->tp_sec = ts.tv_sec;
  945. h.h2->tp_nsec = ts.tv_nsec;
  946. if (vlan_tx_tag_present(skb)) {
  947. h.h2->tp_vlan_tci = vlan_tx_tag_get(skb);
  948. status |= TP_STATUS_VLAN_VALID;
  949. } else {
  950. h.h2->tp_vlan_tci = 0;
  951. }
  952. h.h2->tp_padding = 0;
  953. hdrlen = sizeof(*h.h2);
  954. break;
  955. default:
  956. BUG();
  957. }
  958. sll = h.raw + TPACKET_ALIGN(hdrlen);
  959. sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
  960. sll->sll_family = AF_PACKET;
  961. sll->sll_hatype = dev->type;
  962. sll->sll_protocol = skb->protocol;
  963. sll->sll_pkttype = skb->pkt_type;
  964. if (unlikely(po->origdev))
  965. sll->sll_ifindex = orig_dev->ifindex;
  966. else
  967. sll->sll_ifindex = dev->ifindex;
  968. smp_mb();
  969. #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
  970. {
  971. u8 *start, *end;
  972. end = (u8 *)PAGE_ALIGN((unsigned long)h.raw + macoff + snaplen);
  973. for (start = h.raw; start < end; start += PAGE_SIZE)
  974. flush_dcache_page(pgv_to_page(start));
  975. smp_wmb();
  976. }
  977. #endif
  978. __packet_set_status(po, h.raw, status);
  979. sk->sk_data_ready(sk, 0);
  980. drop_n_restore:
  981. if (skb_head != skb->data && skb_shared(skb)) {
  982. skb->data = skb_head;
  983. skb->len = skb_len;
  984. }
  985. drop:
  986. kfree_skb(skb);
  987. return 0;
  988. ring_is_full:
  989. po->stats.tp_drops++;
  990. spin_unlock(&sk->sk_receive_queue.lock);
  991. sk->sk_data_ready(sk, 0);
  992. kfree_skb(copy_skb);
  993. goto drop_n_restore;
  994. }
  995. static void tpacket_destruct_skb(struct sk_buff *skb)
  996. {
  997. struct packet_sock *po = pkt_sk(skb->sk);
  998. void *ph;
  999. BUG_ON(skb == NULL);
  1000. if (likely(po->tx_ring.pg_vec)) {
  1001. ph = skb_shinfo(skb)->destructor_arg;
  1002. BUG_ON(__packet_get_status(po, ph) != TP_STATUS_SENDING);
  1003. BUG_ON(atomic_read(&po->tx_ring.pending) == 0);
  1004. atomic_dec(&po->tx_ring.pending);
  1005. __packet_set_status(po, ph, TP_STATUS_AVAILABLE);
  1006. }
  1007. sock_wfree(skb);
  1008. }
  1009. static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
  1010. void *frame, struct net_device *dev, int size_max,
  1011. __be16 proto, unsigned char *addr)
  1012. {
  1013. union {
  1014. struct tpacket_hdr *h1;
  1015. struct tpacket2_hdr *h2;
  1016. void *raw;
  1017. } ph;
  1018. int to_write, offset, len, tp_len, nr_frags, len_max;
  1019. struct socket *sock = po->sk.sk_socket;
  1020. struct page *page;
  1021. void *data;
  1022. int err;
  1023. ph.raw = frame;
  1024. skb->protocol = proto;
  1025. skb->dev = dev;
  1026. skb->priority = po->sk.sk_priority;
  1027. skb->mark = po->sk.sk_mark;
  1028. skb_shinfo(skb)->destructor_arg = ph.raw;
  1029. switch (po->tp_version) {
  1030. case TPACKET_V2:
  1031. tp_len = ph.h2->tp_len;
  1032. break;
  1033. default:
  1034. tp_len = ph.h1->tp_len;
  1035. break;
  1036. }
  1037. if (unlikely(tp_len > size_max)) {
  1038. pr_err("packet size is too long (%d > %d)\n", tp_len, size_max);
  1039. return -EMSGSIZE;
  1040. }
  1041. skb_reserve(skb, LL_RESERVED_SPACE(dev));
  1042. skb_reset_network_header(skb);
  1043. data = ph.raw + po->tp_hdrlen - sizeof(struct sockaddr_ll);
  1044. to_write = tp_len;
  1045. if (sock->type == SOCK_DGRAM) {
  1046. err = dev_hard_header(skb, dev, ntohs(proto), addr,
  1047. NULL, tp_len);
  1048. if (unlikely(err < 0))
  1049. return -EINVAL;
  1050. } else if (dev->hard_header_len) {
  1051. /* net device doesn't like empty head */
  1052. if (unlikely(tp_len <= dev->hard_header_len)) {
  1053. pr_err("packet size is too short (%d < %d)\n",
  1054. tp_len, dev->hard_header_len);
  1055. return -EINVAL;
  1056. }
  1057. skb_push(skb, dev->hard_header_len);
  1058. err = skb_store_bits(skb, 0, data,
  1059. dev->hard_header_len);
  1060. if (unlikely(err))
  1061. return err;
  1062. data += dev->hard_header_len;
  1063. to_write -= dev->hard_header_len;
  1064. }
  1065. err = -EFAULT;
  1066. offset = offset_in_page(data);
  1067. len_max = PAGE_SIZE - offset;
  1068. len = ((to_write > len_max) ? len_max : to_write);
  1069. skb->data_len = to_write;
  1070. skb->len += to_write;
  1071. skb->truesize += to_write;
  1072. atomic_add(to_write, &po->sk.sk_wmem_alloc);
  1073. while (likely(to_write)) {
  1074. nr_frags = skb_shinfo(skb)->nr_frags;
  1075. if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
  1076. pr_err("Packet exceed the number of skb frags(%lu)\n",
  1077. MAX_SKB_FRAGS);
  1078. return -EFAULT;
  1079. }
  1080. page = pgv_to_page(data);
  1081. data += len;
  1082. flush_dcache_page(page);
  1083. get_page(page);
  1084. skb_fill_page_desc(skb, nr_frags, page, offset, len);
  1085. to_write -= len;
  1086. offset = 0;
  1087. len_max = PAGE_SIZE;
  1088. len = ((to_write > len_max) ? len_max : to_write);
  1089. }
  1090. return tp_len;
  1091. }
  1092. static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
  1093. {
  1094. struct sk_buff *skb;
  1095. struct net_device *dev;
  1096. __be16 proto;
  1097. bool need_rls_dev = false;
  1098. int err, reserve = 0;
  1099. void *ph;
  1100. struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
  1101. int tp_len, size_max;
  1102. unsigned char *addr;
  1103. int len_sum = 0;
  1104. int status = 0;
  1105. mutex_lock(&po->pg_vec_lock);
  1106. err = -EBUSY;
  1107. if (saddr == NULL) {
  1108. dev = po->prot_hook.dev;
  1109. proto = po->num;
  1110. addr = NULL;
  1111. } else {
  1112. err = -EINVAL;
  1113. if (msg->msg_namelen < sizeof(struct sockaddr_ll))
  1114. goto out;
  1115. if (msg->msg_namelen < (saddr->sll_halen
  1116. + offsetof(struct sockaddr_ll,
  1117. sll_addr)))
  1118. goto out;
  1119. proto = saddr->sll_protocol;
  1120. addr = saddr->sll_addr;
  1121. dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex);
  1122. need_rls_dev = true;
  1123. }
  1124. err = -ENXIO;
  1125. if (unlikely(dev == NULL))
  1126. goto out;
  1127. reserve = dev->hard_header_len;
  1128. err = -ENETDOWN;
  1129. if (unlikely(!(dev->flags & IFF_UP)))
  1130. goto out_put;
  1131. size_max = po->tx_ring.frame_size
  1132. - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
  1133. if (size_max > dev->mtu + reserve)
  1134. size_max = dev->mtu + reserve;
  1135. do {
  1136. ph = packet_current_frame(po, &po->tx_ring,
  1137. TP_STATUS_SEND_REQUEST);
  1138. if (unlikely(ph == NULL)) {
  1139. schedule();
  1140. continue;
  1141. }
  1142. status = TP_STATUS_SEND_REQUEST;
  1143. skb = sock_alloc_send_skb(&po->sk,
  1144. LL_ALLOCATED_SPACE(dev)
  1145. + sizeof(struct sockaddr_ll),
  1146. 0, &err);
  1147. if (unlikely(skb == NULL))
  1148. goto out_status;
  1149. tp_len = tpacket_fill_skb(po, skb, ph, dev, size_max, proto,
  1150. addr);
  1151. if (unlikely(tp_len < 0)) {
  1152. if (po->tp_loss) {
  1153. __packet_set_status(po, ph,
  1154. TP_STATUS_AVAILABLE);
  1155. packet_increment_head(&po->tx_ring);
  1156. kfree_skb(skb);
  1157. continue;
  1158. } else {
  1159. status = TP_STATUS_WRONG_FORMAT;
  1160. err = tp_len;
  1161. goto out_status;
  1162. }
  1163. }
  1164. skb->destructor = tpacket_destruct_skb;
  1165. __packet_set_status(po, ph, TP_STATUS_SENDING);
  1166. atomic_inc(&po->tx_ring.pending);
  1167. status = TP_STATUS_SEND_REQUEST;
  1168. err = dev_queue_xmit(skb);
  1169. if (unlikely(err > 0)) {
  1170. err = net_xmit_errno(err);
  1171. if (err && __packet_get_status(po, ph) ==
  1172. TP_STATUS_AVAILABLE) {
  1173. /* skb was destructed already */
  1174. skb = NULL;
  1175. goto out_status;
  1176. }
  1177. /*
  1178. * skb was dropped but not destructed yet;
  1179. * let's treat it like congestion or err < 0
  1180. */
  1181. err = 0;
  1182. }
  1183. packet_increment_head(&po->tx_ring);
  1184. len_sum += tp_len;
  1185. } while (likely((ph != NULL) ||
  1186. ((!(msg->msg_flags & MSG_DONTWAIT)) &&
  1187. (atomic_read(&po->tx_ring.pending))))
  1188. );
  1189. err = len_sum;
  1190. goto out_put;
  1191. out_status:
  1192. __packet_set_status(po, ph, status);
  1193. kfree_skb(skb);
  1194. out_put:
  1195. if (need_rls_dev)
  1196. dev_put(dev);
  1197. out:
  1198. mutex_unlock(&po->pg_vec_lock);
  1199. return err;
  1200. }
  1201. static inline struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
  1202. size_t reserve, size_t len,
  1203. size_t linear, int noblock,
  1204. int *err)
  1205. {
  1206. struct sk_buff *skb;
  1207. /* Under a page? Don't bother with paged skb. */
  1208. if (prepad + len < PAGE_SIZE || !linear)
  1209. linear = len;
  1210. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  1211. err);
  1212. if (!skb)
  1213. return NULL;
  1214. skb_reserve(skb, reserve);
  1215. skb_put(skb, linear);
  1216. skb->data_len = len - linear;
  1217. skb->len += len - linear;
  1218. return skb;
  1219. }
  1220. static int packet_snd(struct socket *sock,
  1221. struct msghdr *msg, size_t len)
  1222. {
  1223. struct sock *sk = sock->sk;
  1224. struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
  1225. struct sk_buff *skb;
  1226. struct net_device *dev;
  1227. __be16 proto;
  1228. bool need_rls_dev = false;
  1229. unsigned char *addr;
  1230. int err, reserve = 0;
  1231. struct virtio_net_hdr vnet_hdr = { 0 };
  1232. int offset = 0;
  1233. int vnet_hdr_len;
  1234. struct packet_sock *po = pkt_sk(sk);
  1235. unsigned short gso_type = 0;
  1236. /*
  1237. * Get and verify the address.
  1238. */
  1239. if (saddr == NULL) {
  1240. dev = po->prot_hook.dev;
  1241. proto = po->num;
  1242. addr = NULL;
  1243. } else {
  1244. err = -EINVAL;
  1245. if (msg->msg_namelen < sizeof(struct sockaddr_ll))
  1246. goto out;
  1247. if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr)))
  1248. goto out;
  1249. proto = saddr->sll_protocol;
  1250. addr = saddr->sll_addr;
  1251. dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);
  1252. need_rls_dev = true;
  1253. }
  1254. err = -ENXIO;
  1255. if (dev == NULL)
  1256. goto out_unlock;
  1257. if (sock->type == SOCK_RAW)
  1258. reserve = dev->hard_header_len;
  1259. err = -ENETDOWN;
  1260. if (!(dev->flags & IFF_UP))
  1261. goto out_unlock;
  1262. if (po->has_vnet_hdr) {
  1263. vnet_hdr_len = sizeof(vnet_hdr);
  1264. err = -EINVAL;
  1265. if (len < vnet_hdr_len)
  1266. goto out_unlock;
  1267. len -= vnet_hdr_len;
  1268. err = memcpy_fromiovec((void *)&vnet_hdr, msg->msg_iov,
  1269. vnet_hdr_len);
  1270. if (err < 0)
  1271. goto out_unlock;
  1272. if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  1273. (vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
  1274. vnet_hdr.hdr_len))
  1275. vnet_hdr.hdr_len = vnet_hdr.csum_start +
  1276. vnet_hdr.csum_offset + 2;
  1277. err = -EINVAL;
  1278. if (vnet_hdr.hdr_len > len)
  1279. goto out_unlock;
  1280. if (vnet_hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  1281. switch (vnet_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  1282. case VIRTIO_NET_HDR_GSO_TCPV4:
  1283. gso_type = SKB_GSO_TCPV4;
  1284. break;
  1285. case VIRTIO_NET_HDR_GSO_TCPV6:
  1286. gso_type = SKB_GSO_TCPV6;
  1287. break;
  1288. case VIRTIO_NET_HDR_GSO_UDP:
  1289. gso_type = SKB_GSO_UDP;
  1290. break;
  1291. default:
  1292. goto out_unlock;
  1293. }
  1294. if (vnet_hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
  1295. gso_type |= SKB_GSO_TCP_ECN;
  1296. if (vnet_hdr.gso_size == 0)
  1297. goto out_unlock;
  1298. }
  1299. }
  1300. err = -EMSGSIZE;
  1301. if (!gso_type && (len > dev->mtu + reserve + VLAN_HLEN))
  1302. goto out_unlock;
  1303. err = -ENOBUFS;
  1304. skb = packet_alloc_skb(sk, LL_ALLOCATED_SPACE(dev),
  1305. LL_RESERVED_SPACE(dev), len, vnet_hdr.hdr_len,
  1306. msg->msg_flags & MSG_DONTWAIT, &err);
  1307. if (skb == NULL)
  1308. goto out_unlock;
  1309. skb_set_network_header(skb, reserve);
  1310. err = -EINVAL;
  1311. if (sock->type == SOCK_DGRAM &&
  1312. (offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len)) < 0)
  1313. goto out_free;
  1314. /* Returns -EFAULT on error */
  1315. err = skb_copy_datagram_from_iovec(skb, offset, msg->msg_iov, 0, len);
  1316. if (err)
  1317. goto out_free;
  1318. err = sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
  1319. if (err < 0)
  1320. goto out_free;
  1321. if (!gso_type && (len > dev->mtu + reserve)) {
  1322. /* Earlier code assumed this would be a VLAN pkt,
  1323. * double-check this now that we have the actual
  1324. * packet in hand.
  1325. */
  1326. struct ethhdr *ehdr;
  1327. skb_reset_mac_header(skb);
  1328. ehdr = eth_hdr(skb);
  1329. if (ehdr->h_proto != htons(ETH_P_8021Q)) {
  1330. err = -EMSGSIZE;
  1331. goto out_free;
  1332. }
  1333. }
  1334. skb->protocol = proto;
  1335. skb->dev = dev;
  1336. skb->priority = sk->sk_priority;
  1337. skb->mark = sk->sk_mark;
  1338. if (po->has_vnet_hdr) {
  1339. if (vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  1340. if (!skb_partial_csum_set(skb, vnet_hdr.csum_start,
  1341. vnet_hdr.csum_offset)) {
  1342. err = -EINVAL;
  1343. goto out_free;
  1344. }
  1345. }
  1346. skb_shinfo(skb)->gso_size = vnet_hdr.gso_size;
  1347. skb_shinfo(skb)->gso_type = gso_type;
  1348. /* Header must be checked, and gso_segs computed. */
  1349. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  1350. skb_shinfo(skb)->gso_segs = 0;
  1351. len += vnet_hdr_len;
  1352. }
  1353. /*
  1354. * Now send it
  1355. */
  1356. err = dev_queue_xmit(skb);
  1357. if (err > 0 && (err = net_xmit_errno(err)) != 0)
  1358. goto out_unlock;
  1359. if (need_rls_dev)
  1360. dev_put(dev);
  1361. return len;
  1362. out_free:
  1363. kfree_skb(skb);
  1364. out_unlock:
  1365. if (dev && need_rls_dev)
  1366. dev_put(dev);
  1367. out:
  1368. return err;
  1369. }
  1370. static int packet_sendmsg(struct kiocb *iocb, struct socket *sock,
  1371. struct msghdr *msg, size_t len)
  1372. {
  1373. struct sock *sk = sock->sk;
  1374. struct packet_sock *po = pkt_sk(sk);
  1375. if (po->tx_ring.pg_vec)
  1376. return tpacket_snd(po, msg);
  1377. else
  1378. return packet_snd(sock, msg, len);
  1379. }
  1380. /*
  1381. * Close a PACKET socket. This is fairly simple. We immediately go
  1382. * to 'closed' state and remove our protocol entry in the device list.
  1383. */
  1384. static int packet_release(struct socket *sock)
  1385. {
  1386. struct sock *sk = sock->sk;
  1387. struct packet_sock *po;
  1388. struct net *net;
  1389. struct tpacket_req req;
  1390. if (!sk)
  1391. return 0;
  1392. net = sock_net(sk);
  1393. po = pkt_sk(sk);
  1394. spin_lock_bh(&net->packet.sklist_lock);
  1395. sk_del_node_init_rcu(sk);
  1396. sock_prot_inuse_add(net, sk->sk_prot, -1);
  1397. spin_unlock_bh(&net->packet.sklist_lock);
  1398. spin_lock(&po->bind_lock);
  1399. unregister_prot_hook(sk, false);
  1400. if (po->prot_hook.dev) {
  1401. dev_put(po->prot_hook.dev);
  1402. po->prot_hook.dev = NULL;
  1403. }
  1404. spin_unlock(&po->bind_lock);
  1405. packet_flush_mclist(sk);
  1406. memset(&req, 0, sizeof(req));
  1407. if (po->rx_ring.pg_vec)
  1408. packet_set_ring(sk, &req, 1, 0);
  1409. if (po->tx_ring.pg_vec)
  1410. packet_set_ring(sk, &req, 1, 1);
  1411. fanout_release(sk);
  1412. synchronize_net();
  1413. /*
  1414. * Now the socket is dead. No more input will appear.
  1415. */
  1416. sock_orphan(sk);
  1417. sock->sk = NULL;
  1418. /* Purge queues */
  1419. skb_queue_purge(&sk->sk_receive_queue);
  1420. sk_refcnt_debug_release(sk);
  1421. sock_put(sk);
  1422. return 0;
  1423. }
  1424. /*
  1425. * Attach a packet hook.
  1426. */
  1427. static int packet_do_bind(struct sock *sk, struct net_device *dev, __be16 protocol)
  1428. {
  1429. struct packet_sock *po = pkt_sk(sk);
  1430. if (po->fanout)
  1431. return -EINVAL;
  1432. lock_sock(sk);
  1433. spin_lock(&po->bind_lock);
  1434. unregister_prot_hook(sk, true);
  1435. po->num = protocol;
  1436. po->prot_hook.type = protocol;
  1437. if (po->prot_hook.dev)
  1438. dev_put(po->prot_hook.dev);
  1439. po->prot_hook.dev = dev;
  1440. po->ifindex = dev ? dev->ifindex : 0;
  1441. if (protocol == 0)
  1442. goto out_unlock;
  1443. if (!dev || (dev->flags & IFF_UP)) {
  1444. register_prot_hook(sk);
  1445. } else {
  1446. sk->sk_err = ENETDOWN;
  1447. if (!sock_flag(sk, SOCK_DEAD))
  1448. sk->sk_error_report(sk);
  1449. }
  1450. out_unlock:
  1451. spin_unlock(&po->bind_lock);
  1452. release_sock(sk);
  1453. return 0;
  1454. }
  1455. /*
  1456. * Bind a packet socket to a device
  1457. */
  1458. static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
  1459. int addr_len)
  1460. {
  1461. struct sock *sk = sock->sk;
  1462. char name[15];
  1463. struct net_device *dev;
  1464. int err = -ENODEV;
  1465. /*
  1466. * Check legality
  1467. */
  1468. if (addr_len != sizeof(struct sockaddr))
  1469. return -EINVAL;
  1470. strlcpy(name, uaddr->sa_data, sizeof(name));
  1471. dev = dev_get_by_name(sock_net(sk), name);
  1472. if (dev)
  1473. err = packet_do_bind(sk, dev, pkt_sk(sk)->num);
  1474. return err;
  1475. }
  1476. static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  1477. {
  1478. struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
  1479. struct sock *sk = sock->sk;
  1480. struct net_device *dev = NULL;
  1481. int err;
  1482. /*
  1483. * Check legality
  1484. */
  1485. if (addr_len < sizeof(struct sockaddr_ll))
  1486. return -EINVAL;
  1487. if (sll->sll_family != AF_PACKET)
  1488. return -EINVAL;
  1489. if (sll->sll_ifindex) {
  1490. err = -ENODEV;
  1491. dev = dev_get_by_index(sock_net(sk), sll->sll_ifindex);
  1492. if (dev == NULL)
  1493. goto out;
  1494. }
  1495. err = packet_do_bind(sk, dev, sll->sll_protocol ? : pkt_sk(sk)->num);
  1496. out:
  1497. return err;
  1498. }
  1499. static struct proto packet_proto = {
  1500. .name = "PACKET",
  1501. .owner = THIS_MODULE,
  1502. .obj_size = sizeof(struct packet_sock),
  1503. };
  1504. /*
  1505. * Create a packet of type SOCK_PACKET.
  1506. */
  1507. static int packet_create(struct net *net, struct socket *sock, int protocol,
  1508. int kern)
  1509. {
  1510. struct sock *sk;
  1511. struct packet_sock *po;
  1512. __be16 proto = (__force __be16)protocol; /* weird, but documented */
  1513. int err;
  1514. if (!capable(CAP_NET_RAW))
  1515. return -EPERM;
  1516. if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
  1517. sock->type != SOCK_PACKET)
  1518. return -ESOCKTNOSUPPORT;
  1519. sock->state = SS_UNCONNECTED;
  1520. err = -ENOBUFS;
  1521. sk = sk_alloc(net, PF_PACKET, GFP_KERNEL, &packet_proto);
  1522. if (sk == NULL)
  1523. goto out;
  1524. sock->ops = &packet_ops;
  1525. if (sock->type == SOCK_PACKET)
  1526. sock->ops = &packet_ops_spkt;
  1527. sock_init_data(sock, sk);
  1528. po = pkt_sk(sk);
  1529. sk->sk_family = PF_PACKET;
  1530. po->num = proto;
  1531. sk->sk_destruct = packet_sock_destruct;
  1532. sk_refcnt_debug_inc(sk);
  1533. /*
  1534. * Attach a protocol block
  1535. */
  1536. spin_lock_init(&po->bind_lock);
  1537. mutex_init(&po->pg_vec_lock);
  1538. po->prot_hook.func = packet_rcv;
  1539. if (sock->type == SOCK_PACKET)
  1540. po->prot_hook.func = packet_rcv_spkt;
  1541. po->prot_hook.af_packet_priv = sk;
  1542. if (proto) {
  1543. po->prot_hook.type = proto;
  1544. register_prot_hook(sk);
  1545. }
  1546. spin_lock_bh(&net->packet.sklist_lock);
  1547. sk_add_node_rcu(sk, &net->packet.sklist);
  1548. sock_prot_inuse_add(net, &packet_proto, 1);
  1549. spin_unlock_bh(&net->packet.sklist_lock);
  1550. return 0;
  1551. out:
  1552. return err;
  1553. }
  1554. static int packet_recv_error(struct sock *sk, struct msghdr *msg, int len)
  1555. {
  1556. struct sock_exterr_skb *serr;
  1557. struct sk_buff *skb, *skb2;
  1558. int copied, err;
  1559. err = -EAGAIN;
  1560. skb = skb_dequeue(&sk->sk_error_queue);
  1561. if (skb == NULL)
  1562. goto out;
  1563. copied = skb->len;
  1564. if (copied > len) {
  1565. msg->msg_flags |= MSG_TRUNC;
  1566. copied = len;
  1567. }
  1568. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  1569. if (err)
  1570. goto out_free_skb;
  1571. sock_recv_timestamp(msg, sk, skb);
  1572. serr = SKB_EXT_ERR(skb);
  1573. put_cmsg(msg, SOL_PACKET, PACKET_TX_TIMESTAMP,
  1574. sizeof(serr->ee), &serr->ee);
  1575. msg->msg_flags |= MSG_ERRQUEUE;
  1576. err = copied;
  1577. /* Reset and regenerate socket error */
  1578. spin_lock_bh(&sk->sk_error_queue.lock);
  1579. sk->sk_err = 0;
  1580. if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
  1581. sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
  1582. spin_unlock_bh(&sk->sk_error_queue.lock);
  1583. sk->sk_error_report(sk);
  1584. } else
  1585. spin_unlock_bh(&sk->sk_error_queue.lock);
  1586. out_free_skb:
  1587. kfree_skb(skb);
  1588. out:
  1589. return err;
  1590. }
  1591. /*
  1592. * Pull a packet from our receive queue and hand it to the user.
  1593. * If necessary we block.
  1594. */
  1595. static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
  1596. struct msghdr *msg, size_t len, int flags)
  1597. {
  1598. struct sock *sk = sock->sk;
  1599. struct sk_buff *skb;
  1600. int copied, err;
  1601. struct sockaddr_ll *sll;
  1602. int vnet_hdr_len = 0;
  1603. err = -EINVAL;
  1604. if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
  1605. goto out;
  1606. #if 0
  1607. /* What error should we return now? EUNATTACH? */
  1608. if (pkt_sk(sk)->ifindex < 0)
  1609. return -ENODEV;
  1610. #endif
  1611. if (flags & MSG_ERRQUEUE) {
  1612. err = packet_recv_error(sk, msg, len);
  1613. goto out;
  1614. }
  1615. /*
  1616. * Call the generic datagram receiver. This handles all sorts
  1617. * of horrible races and re-entrancy so we can forget about it
  1618. * in the protocol layers.
  1619. *
  1620. * Now it will return ENETDOWN, if device have just gone down,
  1621. * but then it will block.
  1622. */
  1623. skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
  1624. /*
  1625. * An error occurred so return it. Because skb_recv_datagram()
  1626. * handles the blocking we don't see and worry about blocking
  1627. * retries.
  1628. */
  1629. if (skb == NULL)
  1630. goto out;
  1631. if (pkt_sk(sk)->has_vnet_hdr) {
  1632. struct virtio_net_hdr vnet_hdr = { 0 };
  1633. err = -EINVAL;
  1634. vnet_hdr_len = sizeof(vnet_hdr);
  1635. if (len < vnet_hdr_len)
  1636. goto out_free;
  1637. len -= vnet_hdr_len;
  1638. if (skb_is_gso(skb)) {
  1639. struct skb_shared_info *sinfo = skb_shinfo(skb);
  1640. /* This is a hint as to how much should be linear. */
  1641. vnet_hdr.hdr_len = skb_headlen(skb);
  1642. vnet_hdr.gso_size = sinfo->gso_size;
  1643. if (sinfo->gso_type & SKB_GSO_TCPV4)
  1644. vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  1645. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  1646. vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  1647. else if (sinfo->gso_type & SKB_GSO_UDP)
  1648. vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
  1649. else if (sinfo->gso_type & SKB_GSO_FCOE)
  1650. goto out_free;
  1651. else
  1652. BUG();
  1653. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  1654. vnet_hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  1655. } else
  1656. vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  1657. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1658. vnet_hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  1659. vnet_hdr.csum_start = skb_checksum_start_offset(skb);
  1660. vnet_hdr.csum_offset = skb->csum_offset;
  1661. } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  1662. vnet_hdr.flags = VIRTIO_NET_HDR_F_DATA_VALID;
  1663. } /* else everything is zero */
  1664. err = memcpy_toiovec(msg->msg_iov, (void *)&vnet_hdr,
  1665. vnet_hdr_len);
  1666. if (err < 0)
  1667. goto out_free;
  1668. }
  1669. /*
  1670. * If the address length field is there to be filled in, we fill
  1671. * it in now.
  1672. */
  1673. sll = &PACKET_SKB_CB(skb)->sa.ll;
  1674. if (sock->type == SOCK_PACKET)
  1675. msg->msg_namelen = sizeof(struct sockaddr_pkt);
  1676. else
  1677. msg->msg_namelen = sll->sll_halen + offsetof(struct sockaddr_ll, sll_addr);
  1678. /*
  1679. * You lose any data beyond the buffer you gave. If it worries a
  1680. * user program they can ask the device for its MTU anyway.
  1681. */
  1682. copied = skb->len;
  1683. if (copied > len) {
  1684. copied = len;
  1685. msg->msg_flags |= MSG_TRUNC;
  1686. }
  1687. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  1688. if (err)
  1689. goto out_free;
  1690. sock_recv_ts_and_drops(msg, sk, skb);
  1691. if (msg->msg_name)
  1692. memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa,
  1693. msg->msg_namelen);
  1694. if (pkt_sk(sk)->auxdata) {
  1695. struct tpacket_auxdata aux;
  1696. aux.tp_status = TP_STATUS_USER;
  1697. if (skb->ip_summed == CHECKSUM_PARTIAL)
  1698. aux.tp_status |= TP_STATUS_CSUMNOTREADY;
  1699. aux.tp_len = PACKET_SKB_CB(skb)->origlen;
  1700. aux.tp_snaplen = skb->len;
  1701. aux.tp_mac = 0;
  1702. aux.tp_net = skb_network_offset(skb);
  1703. if (vlan_tx_tag_present(skb)) {
  1704. aux.tp_vlan_tci = vlan_tx_tag_get(skb);
  1705. aux.tp_status |= TP_STATUS_VLAN_VALID;
  1706. } else {
  1707. aux.tp_vlan_tci = 0;
  1708. }
  1709. aux.tp_padding = 0;
  1710. put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
  1711. }
  1712. /*
  1713. * Free or return the buffer as appropriate. Again this
  1714. * hides all the races and re-entrancy issues from us.
  1715. */
  1716. err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
  1717. out_free:
  1718. skb_free_datagram(sk, skb);
  1719. out:
  1720. return err;
  1721. }
  1722. static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
  1723. int *uaddr_len, int peer)
  1724. {
  1725. struct net_device *dev;
  1726. struct sock *sk = sock->sk;
  1727. if (peer)
  1728. return -EOPNOTSUPP;
  1729. uaddr->sa_family = AF_PACKET;
  1730. rcu_read_lock();
  1731. dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex);
  1732. if (dev)
  1733. strncpy(uaddr->sa_data, dev->name, 14);
  1734. else
  1735. memset(uaddr->sa_data, 0, 14);
  1736. rcu_read_unlock();
  1737. *uaddr_len = sizeof(*uaddr);
  1738. return 0;
  1739. }
  1740. static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
  1741. int *uaddr_len, int peer)
  1742. {
  1743. struct net_device *dev;
  1744. struct sock *sk = sock->sk;
  1745. struct packet_sock *po = pkt_sk(sk);
  1746. DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
  1747. if (peer)
  1748. return -EOPNOTSUPP;
  1749. sll->sll_family = AF_PACKET;
  1750. sll->sll_ifindex = po->ifindex;
  1751. sll->sll_protocol = po->num;
  1752. sll->sll_pkttype = 0;
  1753. rcu_read_lock();
  1754. dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex);
  1755. if (dev) {
  1756. sll->sll_hatype = dev->type;
  1757. sll->sll_halen = dev->addr_len;
  1758. memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
  1759. } else {
  1760. sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
  1761. sll->sll_halen = 0;
  1762. }
  1763. rcu_read_unlock();
  1764. *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
  1765. return 0;
  1766. }
  1767. static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
  1768. int what)
  1769. {
  1770. switch (i->type) {
  1771. case PACKET_MR_MULTICAST:
  1772. if (i->alen != dev->addr_len)
  1773. return -EINVAL;
  1774. if (what > 0)
  1775. return dev_mc_add(dev, i->addr);
  1776. else
  1777. return dev_mc_del(dev, i->addr);
  1778. break;
  1779. case PACKET_MR_PROMISC:
  1780. return dev_set_promiscuity(dev, what);
  1781. break;
  1782. case PACKET_MR_ALLMULTI:
  1783. return dev_set_allmulti(dev, what);
  1784. break;
  1785. case PACKET_MR_UNICAST:
  1786. if (i->alen != dev->addr_len)
  1787. return -EINVAL;
  1788. if (what > 0)
  1789. return dev_uc_add(dev, i->addr);
  1790. else
  1791. return dev_uc_del(dev, i->addr);
  1792. break;
  1793. default:
  1794. break;
  1795. }
  1796. return 0;
  1797. }
  1798. static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what)
  1799. {
  1800. for ( ; i; i = i->next) {
  1801. if (i->ifindex == dev->ifindex)
  1802. packet_dev_mc(dev, i, what);
  1803. }
  1804. }
  1805. static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
  1806. {
  1807. struct packet_sock *po = pkt_sk(sk);
  1808. struct packet_mclist *ml, *i;
  1809. struct net_device *dev;
  1810. int err;
  1811. rtnl_lock();
  1812. err = -ENODEV;
  1813. dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex);
  1814. if (!dev)
  1815. goto done;
  1816. err = -EINVAL;
  1817. if (mreq->mr_alen > dev->addr_len)
  1818. goto done;
  1819. err = -ENOBUFS;
  1820. i = kmalloc(sizeof(*i), GFP_KERNEL);
  1821. if (i == NULL)
  1822. goto done;
  1823. err = 0;
  1824. for (ml = po->mclist; ml; ml = ml->next) {
  1825. if (ml->ifindex == mreq->mr_ifindex &&
  1826. ml->type == mreq->mr_type &&
  1827. ml->alen == mreq->mr_alen &&
  1828. memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
  1829. ml->count++;
  1830. /* Free the new element ... */
  1831. kfree(i);
  1832. goto done;
  1833. }
  1834. }
  1835. i->type = mreq->mr_type;
  1836. i->ifindex = mreq->mr_ifindex;
  1837. i->alen = mreq->mr_alen;
  1838. memcpy(i->addr, mreq->mr_address, i->alen);
  1839. i->count = 1;
  1840. i->next = po->mclist;
  1841. po->mclist = i;
  1842. err = packet_dev_mc(dev, i, 1);
  1843. if (err) {
  1844. po->mclist = i->next;
  1845. kfree(i);
  1846. }
  1847. done:
  1848. rtnl_unlock();
  1849. return err;
  1850. }
  1851. static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
  1852. {
  1853. struct packet_mclist *ml, **mlp;
  1854. rtnl_lock();
  1855. for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) {
  1856. if (ml->ifindex == mreq->mr_ifindex &&
  1857. ml->type == mreq->mr_type &&
  1858. ml->alen == mreq->mr_alen &&
  1859. memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
  1860. if (--ml->count == 0) {
  1861. struct net_device *dev;
  1862. *mlp = ml->next;
  1863. dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
  1864. if (dev)
  1865. packet_dev_mc(dev, ml, -1);
  1866. kfree(ml);
  1867. }
  1868. rtnl_unlock();
  1869. return 0;
  1870. }
  1871. }
  1872. rtnl_unlock();
  1873. return -EADDRNOTAVAIL;
  1874. }
  1875. static void packet_flush_mclist(struct sock *sk)
  1876. {
  1877. struct packet_sock *po = pkt_sk(sk);
  1878. struct packet_mclist *ml;
  1879. if (!po->mclist)
  1880. return;
  1881. rtnl_lock();
  1882. while ((ml = po->mclist) != NULL) {
  1883. struct net_device *dev;
  1884. po->mclist = ml->next;
  1885. dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
  1886. if (dev != NULL)
  1887. packet_dev_mc(dev, ml, -1);
  1888. kfree(ml);
  1889. }
  1890. rtnl_unlock();
  1891. }
  1892. static int
  1893. packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
  1894. {
  1895. struct sock *sk = sock->sk;
  1896. struct packet_sock *po = pkt_sk(sk);
  1897. int ret;
  1898. if (level != SOL_PACKET)
  1899. return -ENOPROTOOPT;
  1900. switch (optname) {
  1901. case PACKET_ADD_MEMBERSHIP:
  1902. case PACKET_DROP_MEMBERSHIP:
  1903. {
  1904. struct packet_mreq_max mreq;
  1905. int len = optlen;
  1906. memset(&mreq, 0, sizeof(mreq));
  1907. if (len < sizeof(struct packet_mreq))
  1908. return -EINVAL;
  1909. if (len > sizeof(mreq))
  1910. len = sizeof(mreq);
  1911. if (copy_from_user(&mreq, optval, len))
  1912. return -EFAULT;
  1913. if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
  1914. return -EINVAL;
  1915. if (optname == PACKET_ADD_MEMBERSHIP)
  1916. ret = packet_mc_add(sk, &mreq);
  1917. else
  1918. ret = packet_mc_drop(sk, &mreq);
  1919. return ret;
  1920. }
  1921. case PACKET_RX_RING:
  1922. case PACKET_TX_RING:
  1923. {
  1924. struct tpacket_req req;
  1925. if (optlen < sizeof(req))
  1926. return -EINVAL;
  1927. if (pkt_sk(sk)->has_vnet_hdr)
  1928. return -EINVAL;
  1929. if (copy_from_user(&req, optval, sizeof(req)))
  1930. return -EFAULT;
  1931. return packet_set_ring(sk, &req, 0, optname == PACKET_TX_RING);
  1932. }
  1933. case PACKET_COPY_THRESH:
  1934. {
  1935. int val;
  1936. if (optlen != sizeof(val))
  1937. return -EINVAL;
  1938. if (copy_from_user(&val, optval, sizeof(val)))
  1939. return -EFAULT;
  1940. pkt_sk(sk)->copy_thresh = val;
  1941. return 0;
  1942. }
  1943. case PACKET_VERSION:
  1944. {
  1945. int val;
  1946. if (optlen != sizeof(val))
  1947. return -EINVAL;
  1948. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
  1949. return -EBUSY;
  1950. if (copy_from_user(&val, optval, sizeof(val)))
  1951. return -EFAULT;
  1952. switch (val) {
  1953. case TPACKET_V1:
  1954. case TPACKET_V2:
  1955. po->tp_version = val;
  1956. return 0;
  1957. default:
  1958. return -EINVAL;
  1959. }
  1960. }
  1961. case PACKET_RESERVE:
  1962. {
  1963. unsigned int val;
  1964. if (optlen != sizeof(val))
  1965. return -EINVAL;
  1966. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
  1967. return -EBUSY;
  1968. if (copy_from_user(&val, optval, sizeof(val)))
  1969. return -EFAULT;
  1970. po->tp_reserve = val;
  1971. return 0;
  1972. }
  1973. case PACKET_LOSS:
  1974. {
  1975. unsigned int val;
  1976. if (optlen != sizeof(val))
  1977. return -EINVAL;
  1978. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
  1979. return -EBUSY;
  1980. if (copy_from_user(&val, optval, sizeof(val)))
  1981. return -EFAULT;
  1982. po->tp_loss = !!val;
  1983. return 0;
  1984. }
  1985. case PACKET_AUXDATA:
  1986. {
  1987. int val;
  1988. if (optlen < sizeof(val))
  1989. return -EINVAL;
  1990. if (copy_from_user(&val, optval, sizeof(val)))
  1991. return -EFAULT;
  1992. po->auxdata = !!val;
  1993. return 0;
  1994. }
  1995. case PACKET_ORIGDEV:
  1996. {
  1997. int val;
  1998. if (optlen < sizeof(val))
  1999. return -EINVAL;
  2000. if (copy_from_user(&val, optval, sizeof(val)))
  2001. return -EFAULT;
  2002. po->origdev = !!val;
  2003. return 0;
  2004. }
  2005. case PACKET_VNET_HDR:
  2006. {
  2007. int val;
  2008. if (sock->type != SOCK_RAW)
  2009. return -EINVAL;
  2010. if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
  2011. return -EBUSY;
  2012. if (optlen < sizeof(val))
  2013. return -EINVAL;
  2014. if (copy_from_user(&val, optval, sizeof(val)))
  2015. return -EFAULT;
  2016. po->has_vnet_hdr = !!val;
  2017. return 0;
  2018. }
  2019. case PACKET_TIMESTAMP:
  2020. {
  2021. int val;
  2022. if (optlen != sizeof(val))
  2023. return -EINVAL;
  2024. if (copy_from_user(&val, optval, sizeof(val)))
  2025. return -EFAULT;
  2026. po->tp_tstamp = val;
  2027. return 0;
  2028. }
  2029. case PACKET_FANOUT:
  2030. {
  2031. int val;
  2032. if (optlen != sizeof(val))
  2033. return -EINVAL;
  2034. if (copy_from_user(&val, optval, sizeof(val)))
  2035. return -EFAULT;
  2036. return fanout_add(sk, val & 0xffff, val >> 16);
  2037. }
  2038. default:
  2039. return -ENOPROTOOPT;
  2040. }
  2041. }
  2042. static int packet_getsockopt(struct socket *sock, int level, int optname,
  2043. char __user *optval, int __user *optlen)
  2044. {
  2045. int len;
  2046. int val;
  2047. struct sock *sk = sock->sk;
  2048. struct packet_sock *po = pkt_sk(sk);
  2049. void *data;
  2050. struct tpacket_stats st;
  2051. if (level != SOL_PACKET)
  2052. return -ENOPROTOOPT;
  2053. if (get_user(len, optlen))
  2054. return -EFAULT;
  2055. if (len < 0)
  2056. return -EINVAL;
  2057. switch (optname) {
  2058. case PACKET_STATISTICS:
  2059. if (len > sizeof(struct tpacket_stats))
  2060. len = sizeof(struct tpacket_stats);
  2061. spin_lock_bh(&sk->sk_receive_queue.lock);
  2062. st = po->stats;
  2063. memset(&po->stats, 0, sizeof(st));
  2064. spin_unlock_bh(&sk->sk_receive_queue.lock);
  2065. st.tp_packets += st.tp_drops;
  2066. data = &st;
  2067. break;
  2068. case PACKET_AUXDATA:
  2069. if (len > sizeof(int))
  2070. len = sizeof(int);
  2071. val = po->auxdata;
  2072. data = &val;
  2073. break;
  2074. case PACKET_ORIGDEV:
  2075. if (len > sizeof(int))
  2076. len = sizeof(int);
  2077. val = po->origdev;
  2078. data = &val;
  2079. break;
  2080. case PACKET_VNET_HDR:
  2081. if (len > sizeof(int))
  2082. len = sizeof(int);
  2083. val = po->has_vnet_hdr;
  2084. data = &val;
  2085. break;
  2086. case PACKET_VERSION:
  2087. if (len > sizeof(int))
  2088. len = sizeof(int);
  2089. val = po->tp_version;
  2090. data = &val;
  2091. break;
  2092. case PACKET_HDRLEN:
  2093. if (len > sizeof(int))
  2094. len = sizeof(int);
  2095. if (copy_from_user(&val, optval, len))
  2096. return -EFAULT;
  2097. switch (val) {
  2098. case TPACKET_V1:
  2099. val = sizeof(struct tpacket_hdr);
  2100. break;
  2101. case TPACKET_V2:
  2102. val = sizeof(struct tpacket2_hdr);
  2103. break;
  2104. default:
  2105. return -EINVAL;
  2106. }
  2107. data = &val;
  2108. break;
  2109. case PACKET_RESERVE:
  2110. if (len > sizeof(unsigned int))
  2111. len = sizeof(unsigned int);
  2112. val = po->tp_reserve;
  2113. data = &val;
  2114. break;
  2115. case PACKET_LOSS:
  2116. if (len > sizeof(unsigned int))
  2117. len = sizeof(unsigned int);
  2118. val = po->tp_loss;
  2119. data = &val;
  2120. break;
  2121. case PACKET_TIMESTAMP:
  2122. if (len > sizeof(int))
  2123. len = sizeof(int);
  2124. val = po->tp_tstamp;
  2125. data = &val;
  2126. break;
  2127. case PACKET_FANOUT:
  2128. if (len > sizeof(int))
  2129. len = sizeof(int);
  2130. val = (po->fanout ?
  2131. ((u32)po->fanout->id |
  2132. ((u32)po->fanout->type << 16)) :
  2133. 0);
  2134. data = &val;
  2135. break;
  2136. default:
  2137. return -ENOPROTOOPT;
  2138. }
  2139. if (put_user(len, optlen))
  2140. return -EFAULT;
  2141. if (copy_to_user(optval, data, len))
  2142. return -EFAULT;
  2143. return 0;
  2144. }
  2145. static int packet_notifier(struct notifier_block *this, unsigned long msg, void *data)
  2146. {
  2147. struct sock *sk;
  2148. struct hlist_node *node;
  2149. struct net_device *dev = data;
  2150. struct net *net = dev_net(dev);
  2151. rcu_read_lock();
  2152. sk_for_each_rcu(sk, node, &net->packet.sklist) {
  2153. struct packet_sock *po = pkt_sk(sk);
  2154. switch (msg) {
  2155. case NETDEV_UNREGISTER:
  2156. if (po->mclist)
  2157. packet_dev_mclist(dev, po->mclist, -1);
  2158. /* fallthrough */
  2159. case NETDEV_DOWN:
  2160. if (dev->ifindex == po->ifindex) {
  2161. spin_lock(&po->bind_lock);
  2162. if (po->running) {
  2163. __unregister_prot_hook(sk, false);
  2164. sk->sk_err = ENETDOWN;
  2165. if (!sock_flag(sk, SOCK_DEAD))
  2166. sk->sk_error_report(sk);
  2167. }
  2168. if (msg == NETDEV_UNREGISTER) {
  2169. po->ifindex = -1;
  2170. if (po->prot_hook.dev)
  2171. dev_put(po->prot_hook.dev);
  2172. po->prot_hook.dev = NULL;
  2173. }
  2174. spin_unlock(&po->bind_lock);
  2175. }
  2176. break;
  2177. case NETDEV_UP:
  2178. if (dev->ifindex == po->ifindex) {
  2179. spin_lock(&po->bind_lock);
  2180. if (po->num)
  2181. register_prot_hook(sk);
  2182. spin_unlock(&po->bind_lock);
  2183. }
  2184. break;
  2185. }
  2186. }
  2187. rcu_read_unlock();
  2188. return NOTIFY_DONE;
  2189. }
  2190. static int packet_ioctl(struct socket *sock, unsigned int cmd,
  2191. unsigned long arg)
  2192. {
  2193. struct sock *sk = sock->sk;
  2194. switch (cmd) {
  2195. case SIOCOUTQ:
  2196. {
  2197. int amount = sk_wmem_alloc_get(sk);
  2198. return put_user(amount, (int __user *)arg);
  2199. }
  2200. case SIOCINQ:
  2201. {
  2202. struct sk_buff *skb;
  2203. int amount = 0;
  2204. spin_lock_bh(&sk->sk_receive_queue.lock);
  2205. skb = skb_peek(&sk->sk_receive_queue);
  2206. if (skb)
  2207. amount = skb->len;
  2208. spin_unlock_bh(&sk->sk_receive_queue.lock);
  2209. return put_user(amount, (int __user *)arg);
  2210. }
  2211. case SIOCGSTAMP:
  2212. return sock_get_timestamp(sk, (struct timeval __user *)arg);
  2213. case SIOCGSTAMPNS:
  2214. return sock_get_timestampns(sk, (struct timespec __user *)arg);
  2215. #ifdef CONFIG_INET
  2216. case SIOCADDRT:
  2217. case SIOCDELRT:
  2218. case SIOCDARP:
  2219. case SIOCGARP:
  2220. case SIOCSARP:
  2221. case SIOCGIFADDR:
  2222. case SIOCSIFADDR:
  2223. case SIOCGIFBRDADDR:
  2224. case SIOCSIFBRDADDR:
  2225. case SIOCGIFNETMASK:
  2226. case SIOCSIFNETMASK:
  2227. case SIOCGIFDSTADDR:
  2228. case SIOCSIFDSTADDR:
  2229. case SIOCSIFFLAGS:
  2230. return inet_dgram_ops.ioctl(sock, cmd, arg);
  2231. #endif
  2232. default:
  2233. return -ENOIOCTLCMD;
  2234. }
  2235. return 0;
  2236. }
  2237. static unsigned int packet_poll(struct file *file, struct socket *sock,
  2238. poll_table *wait)
  2239. {
  2240. struct sock *sk = sock->sk;
  2241. struct packet_sock *po = pkt_sk(sk);
  2242. unsigned int mask = datagram_poll(file, sock, wait);
  2243. spin_lock_bh(&sk->sk_receive_queue.lock);
  2244. if (po->rx_ring.pg_vec) {
  2245. if (!packet_previous_frame(po, &po->rx_ring, TP_STATUS_KERNEL))
  2246. mask |= POLLIN | POLLRDNORM;
  2247. }
  2248. spin_unlock_bh(&sk->sk_receive_queue.lock);
  2249. spin_lock_bh(&sk->sk_write_queue.lock);
  2250. if (po->tx_ring.pg_vec) {
  2251. if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE))
  2252. mask |= POLLOUT | POLLWRNORM;
  2253. }
  2254. spin_unlock_bh(&sk->sk_write_queue.lock);
  2255. return mask;
  2256. }
  2257. /* Dirty? Well, I still did not learn better way to account
  2258. * for user mmaps.
  2259. */
  2260. static void packet_mm_open(struct vm_area_struct *vma)
  2261. {
  2262. struct file *file = vma->vm_file;
  2263. struct socket *sock = file->private_data;
  2264. struct sock *sk = sock->sk;
  2265. if (sk)
  2266. atomic_inc(&pkt_sk(sk)->mapped);
  2267. }
  2268. static void packet_mm_close(struct vm_area_struct *vma)
  2269. {
  2270. struct file *file = vma->vm_file;
  2271. struct socket *sock = file->private_data;
  2272. struct sock *sk = sock->sk;
  2273. if (sk)
  2274. atomic_dec(&pkt_sk(sk)->mapped);
  2275. }
  2276. static const struct vm_operations_struct packet_mmap_ops = {
  2277. .open = packet_mm_open,
  2278. .close = packet_mm_close,
  2279. };
  2280. static void free_pg_vec(struct pgv *pg_vec, unsigned int order,
  2281. unsigned int len)
  2282. {
  2283. int i;
  2284. for (i = 0; i < len; i++) {
  2285. if (likely(pg_vec[i].buffer)) {
  2286. if (is_vmalloc_addr(pg_vec[i].buffer))
  2287. vfree(pg_vec[i].buffer);
  2288. else
  2289. free_pages((unsigned long)pg_vec[i].buffer,
  2290. order);
  2291. pg_vec[i].buffer = NULL;
  2292. }
  2293. }
  2294. kfree(pg_vec);
  2295. }
  2296. static inline char *alloc_one_pg_vec_page(unsigned long order)
  2297. {
  2298. char *buffer = NULL;
  2299. gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP |
  2300. __GFP_ZERO | __GFP_NOWARN | __GFP_NORETRY;
  2301. buffer = (char *) __get_free_pages(gfp_flags, order);
  2302. if (buffer)
  2303. return buffer;
  2304. /*
  2305. * __get_free_pages failed, fall back to vmalloc
  2306. */
  2307. buffer = vzalloc((1 << order) * PAGE_SIZE);
  2308. if (buffer)
  2309. return buffer;
  2310. /*
  2311. * vmalloc failed, lets dig into swap here
  2312. */
  2313. gfp_flags &= ~__GFP_NORETRY;
  2314. buffer = (char *)__get_free_pages(gfp_flags, order);
  2315. if (buffer)
  2316. return buffer;
  2317. /*
  2318. * complete and utter failure
  2319. */
  2320. return NULL;
  2321. }
  2322. static struct pgv *alloc_pg_vec(struct tpacket_req *req, int order)
  2323. {
  2324. unsigned int block_nr = req->tp_block_nr;
  2325. struct pgv *pg_vec;
  2326. int i;
  2327. pg_vec = kcalloc(block_nr, sizeof(struct pgv), GFP_KERNEL);
  2328. if (unlikely(!pg_vec))
  2329. goto out;
  2330. for (i = 0; i < block_nr; i++) {
  2331. pg_vec[i].buffer = alloc_one_pg_vec_page(order);
  2332. if (unlikely(!pg_vec[i].buffer))
  2333. goto out_free_pgvec;
  2334. }
  2335. out:
  2336. return pg_vec;
  2337. out_free_pgvec:
  2338. free_pg_vec(pg_vec, order, block_nr);
  2339. pg_vec = NULL;
  2340. goto out;
  2341. }
  2342. static int packet_set_ring(struct sock *sk, struct tpacket_req *req,
  2343. int closing, int tx_ring)
  2344. {
  2345. struct pgv *pg_vec = NULL;
  2346. struct packet_sock *po = pkt_sk(sk);
  2347. int was_running, order = 0;
  2348. struct packet_ring_buffer *rb;
  2349. struct sk_buff_head *rb_queue;
  2350. __be16 num;
  2351. int err;
  2352. rb = tx_ring ? &po->tx_ring : &po->rx_ring;
  2353. rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
  2354. err = -EBUSY;
  2355. if (!closing) {
  2356. if (atomic_read(&po->mapped))
  2357. goto out;
  2358. if (atomic_read(&rb->pending))
  2359. goto out;
  2360. }
  2361. if (req->tp_block_nr) {
  2362. /* Sanity tests and some calculations */
  2363. err = -EBUSY;
  2364. if (unlikely(rb->pg_vec))
  2365. goto out;
  2366. switch (po->tp_version) {
  2367. case TPACKET_V1:
  2368. po->tp_hdrlen = TPACKET_HDRLEN;
  2369. break;
  2370. case TPACKET_V2:
  2371. po->tp_hdrlen = TPACKET2_HDRLEN;
  2372. break;
  2373. }
  2374. err = -EINVAL;
  2375. if (unlikely((int)req->tp_block_size <= 0))
  2376. goto out;
  2377. if (unlikely(req->tp_block_size & (PAGE_SIZE - 1)))
  2378. goto out;
  2379. if (unlikely(req->tp_frame_size < po->tp_hdrlen +
  2380. po->tp_reserve))
  2381. goto out;
  2382. if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1)))
  2383. goto out;
  2384. rb->frames_per_block = req->tp_block_size/req->tp_frame_size;
  2385. if (unlikely(rb->frames_per_block <= 0))
  2386. goto out;
  2387. if (unlikely((rb->frames_per_block * req->tp_block_nr) !=
  2388. req->tp_frame_nr))
  2389. goto out;
  2390. err = -ENOMEM;
  2391. order = get_order(req->tp_block_size);
  2392. pg_vec = alloc_pg_vec(req, order);
  2393. if (unlikely(!pg_vec))
  2394. goto out;
  2395. }
  2396. /* Done */
  2397. else {
  2398. err = -EINVAL;
  2399. if (unlikely(req->tp_frame_nr))
  2400. goto out;
  2401. }
  2402. lock_sock(sk);
  2403. /* Detach socket from network */
  2404. spin_lock(&po->bind_lock);
  2405. was_running = po->running;
  2406. num = po->num;
  2407. if (was_running) {
  2408. po->num = 0;
  2409. __unregister_prot_hook(sk, false);
  2410. }
  2411. spin_unlock(&po->bind_lock);
  2412. synchronize_net();
  2413. err = -EBUSY;
  2414. mutex_lock(&po->pg_vec_lock);
  2415. if (closing || atomic_read(&po->mapped) == 0) {
  2416. err = 0;
  2417. spin_lock_bh(&rb_queue->lock);
  2418. swap(rb->pg_vec, pg_vec);
  2419. rb->frame_max = (req->tp_frame_nr - 1);
  2420. rb->head = 0;
  2421. rb->frame_size = req->tp_frame_size;
  2422. spin_unlock_bh(&rb_queue->lock);
  2423. swap(rb->pg_vec_order, order);
  2424. swap(rb->pg_vec_len, req->tp_block_nr);
  2425. rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
  2426. po->prot_hook.func = (po->rx_ring.pg_vec) ?
  2427. tpacket_rcv : packet_rcv;
  2428. skb_queue_purge(rb_queue);
  2429. if (atomic_read(&po->mapped))
  2430. pr_err("packet_mmap: vma is busy: %d\n",
  2431. atomic_read(&po->mapped));
  2432. }
  2433. mutex_unlock(&po->pg_vec_lock);
  2434. spin_lock(&po->bind_lock);
  2435. if (was_running) {
  2436. po->num = num;
  2437. register_prot_hook(sk);
  2438. }
  2439. spin_unlock(&po->bind_lock);
  2440. release_sock(sk);
  2441. if (pg_vec)
  2442. free_pg_vec(pg_vec, order, req->tp_block_nr);
  2443. out:
  2444. return err;
  2445. }
  2446. static int packet_mmap(struct file *file, struct socket *sock,
  2447. struct vm_area_struct *vma)
  2448. {
  2449. struct sock *sk = sock->sk;
  2450. struct packet_sock *po = pkt_sk(sk);
  2451. unsigned long size, expected_size;
  2452. struct packet_ring_buffer *rb;
  2453. unsigned long start;
  2454. int err = -EINVAL;
  2455. int i;
  2456. if (vma->vm_pgoff)
  2457. return -EINVAL;
  2458. mutex_lock(&po->pg_vec_lock);
  2459. expected_size = 0;
  2460. for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
  2461. if (rb->pg_vec) {
  2462. expected_size += rb->pg_vec_len
  2463. * rb->pg_vec_pages
  2464. * PAGE_SIZE;
  2465. }
  2466. }
  2467. if (expected_size == 0)
  2468. goto out;
  2469. size = vma->vm_end - vma->vm_start;
  2470. if (size != expected_size)
  2471. goto out;
  2472. start = vma->vm_start;
  2473. for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) {
  2474. if (rb->pg_vec == NULL)
  2475. continue;
  2476. for (i = 0; i < rb->pg_vec_len; i++) {
  2477. struct page *page;
  2478. void *kaddr = rb->pg_vec[i].buffer;
  2479. int pg_num;
  2480. for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) {
  2481. page = pgv_to_page(kaddr);
  2482. err = vm_insert_page(vma, start, page);
  2483. if (unlikely(err))
  2484. goto out;
  2485. start += PAGE_SIZE;
  2486. kaddr += PAGE_SIZE;
  2487. }
  2488. }
  2489. }
  2490. atomic_inc(&po->mapped);
  2491. vma->vm_ops = &packet_mmap_ops;
  2492. err = 0;
  2493. out:
  2494. mutex_unlock(&po->pg_vec_lock);
  2495. return err;
  2496. }
  2497. static const struct proto_ops packet_ops_spkt = {
  2498. .family = PF_PACKET,
  2499. .owner = THIS_MODULE,
  2500. .release = packet_release,
  2501. .bind = packet_bind_spkt,
  2502. .connect = sock_no_connect,
  2503. .socketpair = sock_no_socketpair,
  2504. .accept = sock_no_accept,
  2505. .getname = packet_getname_spkt,
  2506. .poll = datagram_poll,
  2507. .ioctl = packet_ioctl,
  2508. .listen = sock_no_listen,
  2509. .shutdown = sock_no_shutdown,
  2510. .setsockopt = sock_no_setsockopt,
  2511. .getsockopt = sock_no_getsockopt,
  2512. .sendmsg = packet_sendmsg_spkt,
  2513. .recvmsg = packet_recvmsg,
  2514. .mmap = sock_no_mmap,
  2515. .sendpage = sock_no_sendpage,
  2516. };
  2517. static const struct proto_ops packet_ops = {
  2518. .family = PF_PACKET,
  2519. .owner = THIS_MODULE,
  2520. .release = packet_release,
  2521. .bind = packet_bind,
  2522. .connect = sock_no_connect,
  2523. .socketpair = sock_no_socketpair,
  2524. .accept = sock_no_accept,
  2525. .getname = packet_getname,
  2526. .poll = packet_poll,
  2527. .ioctl = packet_ioctl,
  2528. .listen = sock_no_listen,
  2529. .shutdown = sock_no_shutdown,
  2530. .setsockopt = packet_setsockopt,
  2531. .getsockopt = packet_getsockopt,
  2532. .sendmsg = packet_sendmsg,
  2533. .recvmsg = packet_recvmsg,
  2534. .mmap = packet_mmap,
  2535. .sendpage = sock_no_sendpage,
  2536. };
  2537. static const struct net_proto_family packet_family_ops = {
  2538. .family = PF_PACKET,
  2539. .create = packet_create,
  2540. .owner = THIS_MODULE,
  2541. };
  2542. static struct notifier_block packet_netdev_notifier = {
  2543. .notifier_call = packet_notifier,
  2544. };
  2545. #ifdef CONFIG_PROC_FS
  2546. static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
  2547. __acquires(RCU)
  2548. {
  2549. struct net *net = seq_file_net(seq);
  2550. rcu_read_lock();
  2551. return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
  2552. }
  2553. static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2554. {
  2555. struct net *net = seq_file_net(seq);
  2556. return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
  2557. }
  2558. static void packet_seq_stop(struct seq_file *seq, void *v)
  2559. __releases(RCU)
  2560. {
  2561. rcu_read_unlock();
  2562. }
  2563. static int packet_seq_show(struct seq_file *seq, void *v)
  2564. {
  2565. if (v == SEQ_START_TOKEN)
  2566. seq_puts(seq, "sk RefCnt Type Proto Iface R Rmem User Inode\n");
  2567. else {
  2568. struct sock *s = sk_entry(v);
  2569. const struct packet_sock *po = pkt_sk(s);
  2570. seq_printf(seq,
  2571. "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n",
  2572. s,
  2573. atomic_read(&s->sk_refcnt),
  2574. s->sk_type,
  2575. ntohs(po->num),
  2576. po->ifindex,
  2577. po->running,
  2578. atomic_read(&s->sk_rmem_alloc),
  2579. sock_i_uid(s),
  2580. sock_i_ino(s));
  2581. }
  2582. return 0;
  2583. }
  2584. static const struct seq_operations packet_seq_ops = {
  2585. .start = packet_seq_start,
  2586. .next = packet_seq_next,
  2587. .stop = packet_seq_stop,
  2588. .show = packet_seq_show,
  2589. };
  2590. static int packet_seq_open(struct inode *inode, struct file *file)
  2591. {
  2592. return seq_open_net(inode, file, &packet_seq_ops,
  2593. sizeof(struct seq_net_private));
  2594. }
  2595. static const struct file_operations packet_seq_fops = {
  2596. .owner = THIS_MODULE,
  2597. .open = packet_seq_open,
  2598. .read = seq_read,
  2599. .llseek = seq_lseek,
  2600. .release = seq_release_net,
  2601. };
  2602. #endif
  2603. static int __net_init packet_net_init(struct net *net)
  2604. {
  2605. spin_lock_init(&net->packet.sklist_lock);
  2606. INIT_HLIST_HEAD(&net->packet.sklist);
  2607. if (!proc_net_fops_create(net, "packet", 0, &packet_seq_fops))
  2608. return -ENOMEM;
  2609. return 0;
  2610. }
  2611. static void __net_exit packet_net_exit(struct net *net)
  2612. {
  2613. proc_net_remove(net, "packet");
  2614. }
  2615. static struct pernet_operations packet_net_ops = {
  2616. .init = packet_net_init,
  2617. .exit = packet_net_exit,
  2618. };
  2619. static void __exit packet_exit(void)
  2620. {
  2621. unregister_netdevice_notifier(&packet_netdev_notifier);
  2622. unregister_pernet_subsys(&packet_net_ops);
  2623. sock_unregister(PF_PACKET);
  2624. proto_unregister(&packet_proto);
  2625. }
  2626. static int __init packet_init(void)
  2627. {
  2628. int rc = proto_register(&packet_proto, 0);
  2629. if (rc != 0)
  2630. goto out;
  2631. sock_register(&packet_family_ops);
  2632. register_pernet_subsys(&packet_net_ops);
  2633. register_netdevice_notifier(&packet_netdev_notifier);
  2634. out:
  2635. return rc;
  2636. }
  2637. module_init(packet_init);
  2638. module_exit(packet_exit);
  2639. MODULE_LICENSE("GPL");
  2640. MODULE_ALIAS_NETPROTO(PF_PACKET);